Tom Rini | 83d290c | 2018-05-06 17:58:06 -0400 | [diff] [blame] | 1 | // SPDX-License-Identifier: GPL-2.0+ |
Heiko Schocher | 6bf4ca0 | 2014-03-03 12:19:29 +0100 | [diff] [blame] | 2 | /* |
| 3 | * (C) Copyright 2014 |
| 4 | * DENX Software Engineering |
| 5 | * Heiko Schocher <hs@denx.de> |
| 6 | * |
| 7 | * (C) Copyright 2008 Semihalf |
| 8 | * |
| 9 | * (C) Copyright 2000-2004 |
| 10 | * DENX Software Engineering |
| 11 | * Wolfgang Denk, wd@denx.de |
| 12 | * |
| 13 | * Updated-by: Prafulla Wadaskar <prafulla@marvell.com> |
| 14 | * FIT image specific code abstracted from mkimage.c |
| 15 | * some functions added to address abstraction |
| 16 | * |
| 17 | * All rights reserved. |
Heiko Schocher | 6bf4ca0 | 2014-03-03 12:19:29 +0100 | [diff] [blame] | 18 | */ |
| 19 | |
| 20 | #include "imagetool.h" |
| 21 | #include "mkimage.h" |
| 22 | #include "fit_common.h" |
| 23 | #include <image.h> |
| 24 | #include <u-boot/crc.h> |
| 25 | |
| 26 | int fit_verify_header(unsigned char *ptr, int image_size, |
| 27 | struct image_tool_params *params) |
| 28 | { |
Stefan Eichenberger | 6f08eee | 2022-01-10 18:48:32 +0100 | [diff] [blame] | 29 | int ret; |
| 30 | |
| 31 | if (fdt_check_header(ptr) != EXIT_SUCCESS) |
Jordan Hand | d32aa3c | 2019-03-05 14:47:56 -0800 | [diff] [blame] | 32 | return EXIT_FAILURE; |
| 33 | |
Stefan Eichenberger | 6f08eee | 2022-01-10 18:48:32 +0100 | [diff] [blame] | 34 | ret = fit_check_format(ptr, IMAGE_SIZE_INVAL); |
| 35 | if (ret) { |
| 36 | if (ret != -EADDRNOTAVAIL) |
| 37 | return EXIT_FAILURE; |
| 38 | fprintf(stderr, "Image contains unit addresses @, this will break signing\n"); |
| 39 | } |
| 40 | |
Jordan Hand | d32aa3c | 2019-03-05 14:47:56 -0800 | [diff] [blame] | 41 | return EXIT_SUCCESS; |
Heiko Schocher | 6bf4ca0 | 2014-03-03 12:19:29 +0100 | [diff] [blame] | 42 | } |
| 43 | |
| 44 | int fit_check_image_types(uint8_t type) |
| 45 | { |
| 46 | if (type == IH_TYPE_FLATDT) |
| 47 | return EXIT_SUCCESS; |
| 48 | else |
| 49 | return EXIT_FAILURE; |
| 50 | } |
| 51 | |
Simon Glass | a946811 | 2014-06-02 22:04:53 -0600 | [diff] [blame] | 52 | int mmap_fdt(const char *cmdname, const char *fname, size_t size_inc, |
Luca Boccassi | 7d57485 | 2019-05-14 19:35:02 +0100 | [diff] [blame] | 53 | void **blobp, struct stat *sbuf, bool delete_on_error, |
| 54 | bool read_only) |
Heiko Schocher | 6bf4ca0 | 2014-03-03 12:19:29 +0100 | [diff] [blame] | 55 | { |
| 56 | void *ptr; |
| 57 | int fd; |
| 58 | |
| 59 | /* Load FIT blob into memory (we need to write hashes/signatures) */ |
Luca Boccassi | 7d57485 | 2019-05-14 19:35:02 +0100 | [diff] [blame] | 60 | fd = open(fname, (read_only ? O_RDONLY : O_RDWR) | O_BINARY); |
Heiko Schocher | 6bf4ca0 | 2014-03-03 12:19:29 +0100 | [diff] [blame] | 61 | |
| 62 | if (fd < 0) { |
| 63 | fprintf(stderr, "%s: Can't open %s: %s\n", |
| 64 | cmdname, fname, strerror(errno)); |
Simon Glass | ef0af64 | 2014-06-02 22:04:52 -0600 | [diff] [blame] | 65 | goto err; |
Heiko Schocher | 6bf4ca0 | 2014-03-03 12:19:29 +0100 | [diff] [blame] | 66 | } |
| 67 | |
| 68 | if (fstat(fd, sbuf) < 0) { |
| 69 | fprintf(stderr, "%s: Can't stat %s: %s\n", |
| 70 | cmdname, fname, strerror(errno)); |
Simon Glass | ef0af64 | 2014-06-02 22:04:52 -0600 | [diff] [blame] | 71 | goto err; |
Heiko Schocher | 6bf4ca0 | 2014-03-03 12:19:29 +0100 | [diff] [blame] | 72 | } |
| 73 | |
Simon Glass | a946811 | 2014-06-02 22:04:53 -0600 | [diff] [blame] | 74 | if (size_inc) { |
| 75 | sbuf->st_size += size_inc; |
| 76 | if (ftruncate(fd, sbuf->st_size)) { |
| 77 | fprintf(stderr, "%s: Can't expand %s: %s\n", |
| 78 | cmdname, fname, strerror(errno)); |
| 79 | goto err; |
| 80 | } |
| 81 | } |
| 82 | |
Heiko Schocher | 6bf4ca0 | 2014-03-03 12:19:29 +0100 | [diff] [blame] | 83 | errno = 0; |
Luca Boccassi | 7d57485 | 2019-05-14 19:35:02 +0100 | [diff] [blame] | 84 | ptr = mmap(0, sbuf->st_size, |
| 85 | (read_only ? PROT_READ : PROT_READ | PROT_WRITE), MAP_SHARED, |
| 86 | fd, 0); |
Heiko Schocher | 6bf4ca0 | 2014-03-03 12:19:29 +0100 | [diff] [blame] | 87 | if ((ptr == MAP_FAILED) || (errno != 0)) { |
| 88 | fprintf(stderr, "%s: Can't read %s: %s\n", |
| 89 | cmdname, fname, strerror(errno)); |
Simon Glass | ef0af64 | 2014-06-02 22:04:52 -0600 | [diff] [blame] | 90 | goto err; |
Heiko Schocher | 6bf4ca0 | 2014-03-03 12:19:29 +0100 | [diff] [blame] | 91 | } |
| 92 | |
| 93 | /* check if ptr has a valid blob */ |
| 94 | if (fdt_check_header(ptr)) { |
| 95 | fprintf(stderr, "%s: Invalid FIT blob\n", cmdname); |
Simon Glass | ef0af64 | 2014-06-02 22:04:52 -0600 | [diff] [blame] | 96 | goto err; |
Heiko Schocher | 6bf4ca0 | 2014-03-03 12:19:29 +0100 | [diff] [blame] | 97 | } |
| 98 | |
Simon Glass | a946811 | 2014-06-02 22:04:53 -0600 | [diff] [blame] | 99 | /* expand if needed */ |
| 100 | if (size_inc) { |
| 101 | int ret; |
| 102 | |
| 103 | ret = fdt_open_into(ptr, ptr, sbuf->st_size); |
| 104 | if (ret) { |
| 105 | fprintf(stderr, "%s: Cannot expand FDT: %s\n", |
| 106 | cmdname, fdt_strerror(ret)); |
| 107 | goto err; |
| 108 | } |
| 109 | } |
| 110 | |
Heiko Schocher | 6bf4ca0 | 2014-03-03 12:19:29 +0100 | [diff] [blame] | 111 | *blobp = ptr; |
| 112 | return fd; |
Simon Glass | ef0af64 | 2014-06-02 22:04:52 -0600 | [diff] [blame] | 113 | |
| 114 | err: |
| 115 | if (fd >= 0) |
| 116 | close(fd); |
| 117 | if (delete_on_error) |
| 118 | unlink(fname); |
| 119 | |
| 120 | return -1; |
Heiko Schocher | 6bf4ca0 | 2014-03-03 12:19:29 +0100 | [diff] [blame] | 121 | } |
Simon Glass | e291a5c | 2021-11-12 12:28:04 -0700 | [diff] [blame^] | 122 | |
| 123 | int copyfile(const char *src, const char *dst) |
| 124 | { |
| 125 | int fd_src = -1, fd_dst = -1; |
| 126 | void *buf = NULL; |
| 127 | ssize_t size; |
| 128 | size_t count; |
| 129 | int ret = -1; |
| 130 | |
| 131 | fd_src = open(src, O_RDONLY); |
| 132 | if (fd_src < 0) { |
| 133 | printf("Can't open file %s (%s)\n", src, strerror(errno)); |
| 134 | goto out; |
| 135 | } |
| 136 | |
| 137 | fd_dst = open(dst, O_WRONLY | O_CREAT, 0666); |
| 138 | if (fd_dst < 0) { |
| 139 | printf("Can't open file %s (%s)\n", dst, strerror(errno)); |
| 140 | goto out; |
| 141 | } |
| 142 | |
| 143 | buf = calloc(1, 512); |
| 144 | if (!buf) { |
| 145 | printf("Can't allocate buffer to copy file\n"); |
| 146 | goto out; |
| 147 | } |
| 148 | |
| 149 | while (1) { |
| 150 | size = read(fd_src, buf, 512); |
| 151 | if (size < 0) { |
| 152 | printf("Can't read file %s\n", src); |
| 153 | goto out; |
| 154 | } |
| 155 | if (!size) |
| 156 | break; |
| 157 | |
| 158 | count = size; |
| 159 | size = write(fd_dst, buf, count); |
| 160 | if (size < 0) { |
| 161 | printf("Can't write file %s\n", dst); |
| 162 | goto out; |
| 163 | } |
| 164 | } |
| 165 | |
| 166 | ret = 0; |
| 167 | |
| 168 | out: |
| 169 | if (fd_src >= 0) |
| 170 | close(fd_src); |
| 171 | if (fd_dst >= 0) |
| 172 | close(fd_dst); |
| 173 | if (buf) |
| 174 | free(buf); |
| 175 | |
| 176 | return ret; |
| 177 | } |