Tom Rini | 10e4779 | 2018-05-06 17:58:06 -0400 | [diff] [blame] | 1 | // SPDX-License-Identifier: GPL-2.0+ |
Heiko Schocher | 3cb743c | 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 | 3cb743c | 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 | |
Pali Rohár | 0ed41e2 | 2023-03-29 21:25:54 +0200 | [diff] [blame] | 26 | void fit_print_header(const void *fit, struct image_tool_params *params) |
| 27 | { |
| 28 | fit_print_contents(fit); |
| 29 | } |
| 30 | |
Heiko Schocher | 3cb743c | 2014-03-03 12:19:29 +0100 | [diff] [blame] | 31 | int fit_verify_header(unsigned char *ptr, int image_size, |
| 32 | struct image_tool_params *params) |
| 33 | { |
Stefan Eichenberger | a522870 | 2022-01-10 18:48:32 +0100 | [diff] [blame] | 34 | int ret; |
| 35 | |
| 36 | if (fdt_check_header(ptr) != EXIT_SUCCESS) |
Jordan Hand | 256b7f9 | 2019-03-05 14:47:56 -0800 | [diff] [blame] | 37 | return EXIT_FAILURE; |
| 38 | |
Stefan Eichenberger | a522870 | 2022-01-10 18:48:32 +0100 | [diff] [blame] | 39 | ret = fit_check_format(ptr, IMAGE_SIZE_INVAL); |
| 40 | if (ret) { |
| 41 | if (ret != -EADDRNOTAVAIL) |
| 42 | return EXIT_FAILURE; |
| 43 | fprintf(stderr, "Image contains unit addresses @, this will break signing\n"); |
| 44 | } |
| 45 | |
Jordan Hand | 256b7f9 | 2019-03-05 14:47:56 -0800 | [diff] [blame] | 46 | return EXIT_SUCCESS; |
Heiko Schocher | 3cb743c | 2014-03-03 12:19:29 +0100 | [diff] [blame] | 47 | } |
| 48 | |
| 49 | int fit_check_image_types(uint8_t type) |
| 50 | { |
| 51 | if (type == IH_TYPE_FLATDT) |
| 52 | return EXIT_SUCCESS; |
| 53 | else |
| 54 | return EXIT_FAILURE; |
| 55 | } |
| 56 | |
Simon Glass | 802aa82 | 2014-06-02 22:04:53 -0600 | [diff] [blame] | 57 | int mmap_fdt(const char *cmdname, const char *fname, size_t size_inc, |
Luca Boccassi | bff733f | 2019-05-14 19:35:02 +0100 | [diff] [blame] | 58 | void **blobp, struct stat *sbuf, bool delete_on_error, |
| 59 | bool read_only) |
Heiko Schocher | 3cb743c | 2014-03-03 12:19:29 +0100 | [diff] [blame] | 60 | { |
| 61 | void *ptr; |
| 62 | int fd; |
| 63 | |
| 64 | /* Load FIT blob into memory (we need to write hashes/signatures) */ |
Luca Boccassi | bff733f | 2019-05-14 19:35:02 +0100 | [diff] [blame] | 65 | fd = open(fname, (read_only ? O_RDONLY : O_RDWR) | O_BINARY); |
Heiko Schocher | 3cb743c | 2014-03-03 12:19:29 +0100 | [diff] [blame] | 66 | |
| 67 | if (fd < 0) { |
| 68 | fprintf(stderr, "%s: Can't open %s: %s\n", |
| 69 | cmdname, fname, strerror(errno)); |
Simon Glass | d25c0d9 | 2014-06-02 22:04:52 -0600 | [diff] [blame] | 70 | goto err; |
Heiko Schocher | 3cb743c | 2014-03-03 12:19:29 +0100 | [diff] [blame] | 71 | } |
| 72 | |
| 73 | if (fstat(fd, sbuf) < 0) { |
| 74 | fprintf(stderr, "%s: Can't stat %s: %s\n", |
| 75 | cmdname, fname, strerror(errno)); |
Simon Glass | d25c0d9 | 2014-06-02 22:04:52 -0600 | [diff] [blame] | 76 | goto err; |
Heiko Schocher | 3cb743c | 2014-03-03 12:19:29 +0100 | [diff] [blame] | 77 | } |
| 78 | |
Simon Glass | 802aa82 | 2014-06-02 22:04:53 -0600 | [diff] [blame] | 79 | if (size_inc) { |
| 80 | sbuf->st_size += size_inc; |
| 81 | if (ftruncate(fd, sbuf->st_size)) { |
| 82 | fprintf(stderr, "%s: Can't expand %s: %s\n", |
| 83 | cmdname, fname, strerror(errno)); |
| 84 | goto err; |
| 85 | } |
| 86 | } |
| 87 | |
Heiko Schocher | 3cb743c | 2014-03-03 12:19:29 +0100 | [diff] [blame] | 88 | errno = 0; |
Luca Boccassi | bff733f | 2019-05-14 19:35:02 +0100 | [diff] [blame] | 89 | ptr = mmap(0, sbuf->st_size, |
| 90 | (read_only ? PROT_READ : PROT_READ | PROT_WRITE), MAP_SHARED, |
| 91 | fd, 0); |
Heiko Schocher | 3cb743c | 2014-03-03 12:19:29 +0100 | [diff] [blame] | 92 | if ((ptr == MAP_FAILED) || (errno != 0)) { |
| 93 | fprintf(stderr, "%s: Can't read %s: %s\n", |
| 94 | cmdname, fname, strerror(errno)); |
Simon Glass | d25c0d9 | 2014-06-02 22:04:52 -0600 | [diff] [blame] | 95 | goto err; |
Heiko Schocher | 3cb743c | 2014-03-03 12:19:29 +0100 | [diff] [blame] | 96 | } |
| 97 | |
| 98 | /* check if ptr has a valid blob */ |
| 99 | if (fdt_check_header(ptr)) { |
| 100 | fprintf(stderr, "%s: Invalid FIT blob\n", cmdname); |
Simon Glass | d25c0d9 | 2014-06-02 22:04:52 -0600 | [diff] [blame] | 101 | goto err; |
Heiko Schocher | 3cb743c | 2014-03-03 12:19:29 +0100 | [diff] [blame] | 102 | } |
| 103 | |
Simon Glass | 802aa82 | 2014-06-02 22:04:53 -0600 | [diff] [blame] | 104 | /* expand if needed */ |
| 105 | if (size_inc) { |
| 106 | int ret; |
| 107 | |
| 108 | ret = fdt_open_into(ptr, ptr, sbuf->st_size); |
| 109 | if (ret) { |
| 110 | fprintf(stderr, "%s: Cannot expand FDT: %s\n", |
| 111 | cmdname, fdt_strerror(ret)); |
| 112 | goto err; |
| 113 | } |
| 114 | } |
| 115 | |
Heiko Schocher | 3cb743c | 2014-03-03 12:19:29 +0100 | [diff] [blame] | 116 | *blobp = ptr; |
| 117 | return fd; |
Simon Glass | d25c0d9 | 2014-06-02 22:04:52 -0600 | [diff] [blame] | 118 | |
| 119 | err: |
| 120 | if (fd >= 0) |
| 121 | close(fd); |
| 122 | if (delete_on_error) |
| 123 | unlink(fname); |
| 124 | |
| 125 | return -1; |
Heiko Schocher | 3cb743c | 2014-03-03 12:19:29 +0100 | [diff] [blame] | 126 | } |
Simon Glass | 674fc7f | 2021-11-12 12:28:04 -0700 | [diff] [blame] | 127 | |
| 128 | int copyfile(const char *src, const char *dst) |
| 129 | { |
| 130 | int fd_src = -1, fd_dst = -1; |
| 131 | void *buf = NULL; |
| 132 | ssize_t size; |
| 133 | size_t count; |
| 134 | int ret = -1; |
| 135 | |
| 136 | fd_src = open(src, O_RDONLY); |
| 137 | if (fd_src < 0) { |
| 138 | printf("Can't open file %s (%s)\n", src, strerror(errno)); |
| 139 | goto out; |
| 140 | } |
| 141 | |
Simon Glass | 5c5797f | 2021-11-12 12:28:05 -0700 | [diff] [blame] | 142 | fd_dst = open(dst, O_WRONLY | O_CREAT | O_TRUNC, 0666); |
Simon Glass | 674fc7f | 2021-11-12 12:28:04 -0700 | [diff] [blame] | 143 | if (fd_dst < 0) { |
| 144 | printf("Can't open file %s (%s)\n", dst, strerror(errno)); |
| 145 | goto out; |
| 146 | } |
| 147 | |
| 148 | buf = calloc(1, 512); |
| 149 | if (!buf) { |
| 150 | printf("Can't allocate buffer to copy file\n"); |
| 151 | goto out; |
| 152 | } |
| 153 | |
| 154 | while (1) { |
| 155 | size = read(fd_src, buf, 512); |
| 156 | if (size < 0) { |
| 157 | printf("Can't read file %s\n", src); |
| 158 | goto out; |
| 159 | } |
| 160 | if (!size) |
| 161 | break; |
| 162 | |
| 163 | count = size; |
| 164 | size = write(fd_dst, buf, count); |
| 165 | if (size < 0) { |
| 166 | printf("Can't write file %s\n", dst); |
| 167 | goto out; |
| 168 | } |
| 169 | } |
| 170 | |
| 171 | ret = 0; |
| 172 | |
| 173 | out: |
| 174 | if (fd_src >= 0) |
| 175 | close(fd_src); |
| 176 | if (fd_dst >= 0) |
| 177 | close(fd_dst); |
| 178 | if (buf) |
| 179 | free(buf); |
| 180 | |
| 181 | return ret; |
| 182 | } |
Simon Glass | e460726 | 2021-11-12 12:28:13 -0700 | [diff] [blame] | 183 | |
| 184 | void summary_show(struct image_summary *summary, const char *imagefile, |
| 185 | const char *keydest) |
| 186 | { |
| 187 | if (summary->sig_offset) { |
| 188 | printf("Signature written to '%s', node '%s'\n", imagefile, |
| 189 | summary->sig_path); |
| 190 | if (keydest) { |
| 191 | printf("Public key written to '%s', node '%s'\n", |
| 192 | keydest, summary->keydest_path); |
| 193 | } |
| 194 | } |
| 195 | } |