Prafulla Wadaskar | 89a4d6b | 2009-08-19 17:36:46 +0530 | [diff] [blame] | 1 | /* |
| 2 | * (C) Copyright 2008 Semihalf |
| 3 | * |
| 4 | * (C) Copyright 2000-2004 |
| 5 | * DENX Software Engineering |
| 6 | * Wolfgang Denk, wd@denx.de |
| 7 | * |
| 8 | * Updated-by: Prafulla Wadaskar <prafulla@marvell.com> |
| 9 | * FIT image specific code abstracted from mkimage.c |
| 10 | * some functions added to address abstraction |
| 11 | * |
| 12 | * All rights reserved. |
| 13 | * |
Wolfgang Denk | 1a45966 | 2013-07-08 09:37:19 +0200 | [diff] [blame] | 14 | * SPDX-License-Identifier: GPL-2.0+ |
Prafulla Wadaskar | 89a4d6b | 2009-08-19 17:36:46 +0530 | [diff] [blame] | 15 | */ |
| 16 | |
Guilherme Maciel Ferreira | f86ed6a | 2013-12-01 12:43:10 -0700 | [diff] [blame^] | 17 | #include "imagetool.h" |
Prafulla Wadaskar | 89a4d6b | 2009-08-19 17:36:46 +0530 | [diff] [blame] | 18 | #include "mkimage.h" |
| 19 | #include <image.h> |
| 20 | #include <u-boot/crc.h> |
| 21 | |
| 22 | static image_header_t header; |
| 23 | |
| 24 | static int fit_verify_header (unsigned char *ptr, int image_size, |
Guilherme Maciel Ferreira | f86ed6a | 2013-12-01 12:43:10 -0700 | [diff] [blame^] | 25 | struct image_tool_params *params) |
Prafulla Wadaskar | 89a4d6b | 2009-08-19 17:36:46 +0530 | [diff] [blame] | 26 | { |
Masahiro Yamada | 2f0877c | 2013-09-19 12:10:18 +0900 | [diff] [blame] | 27 | return fdt_check_header(ptr); |
Prafulla Wadaskar | 89a4d6b | 2009-08-19 17:36:46 +0530 | [diff] [blame] | 28 | } |
| 29 | |
| 30 | static int fit_check_image_types (uint8_t type) |
| 31 | { |
| 32 | if (type == IH_TYPE_FLATDT) |
| 33 | return EXIT_SUCCESS; |
| 34 | else |
| 35 | return EXIT_FAILURE; |
| 36 | } |
| 37 | |
Guilherme Maciel Ferreira | f86ed6a | 2013-12-01 12:43:10 -0700 | [diff] [blame^] | 38 | int mmap_fdt(struct image_tool_params *params, const char *fname, void **blobp, |
Simon Glass | aa6d6db | 2013-05-08 08:05:57 +0000 | [diff] [blame] | 39 | struct stat *sbuf) |
| 40 | { |
| 41 | void *ptr; |
| 42 | int fd; |
| 43 | |
| 44 | /* Load FIT blob into memory (we need to write hashes/signatures) */ |
| 45 | fd = open(fname, O_RDWR | O_BINARY); |
| 46 | |
| 47 | if (fd < 0) { |
| 48 | fprintf(stderr, "%s: Can't open %s: %s\n", |
| 49 | params->cmdname, fname, strerror(errno)); |
| 50 | unlink(fname); |
| 51 | return -1; |
| 52 | } |
| 53 | |
| 54 | if (fstat(fd, sbuf) < 0) { |
| 55 | fprintf(stderr, "%s: Can't stat %s: %s\n", |
| 56 | params->cmdname, fname, strerror(errno)); |
| 57 | unlink(fname); |
| 58 | return -1; |
| 59 | } |
| 60 | |
| 61 | ptr = mmap(0, sbuf->st_size, PROT_READ|PROT_WRITE, MAP_SHARED, fd, 0); |
| 62 | if (ptr == MAP_FAILED) { |
| 63 | fprintf(stderr, "%s: Can't read %s: %s\n", |
| 64 | params->cmdname, fname, strerror(errno)); |
| 65 | unlink(fname); |
| 66 | return -1; |
| 67 | } |
| 68 | |
| 69 | /* check if ptr has a valid blob */ |
| 70 | if (fdt_check_header(ptr)) { |
| 71 | fprintf(stderr, "%s: Invalid FIT blob\n", params->cmdname); |
| 72 | unlink(fname); |
| 73 | return -1; |
| 74 | } |
| 75 | |
| 76 | *blobp = ptr; |
| 77 | return fd; |
| 78 | } |
| 79 | |
Prafulla Wadaskar | 89a4d6b | 2009-08-19 17:36:46 +0530 | [diff] [blame] | 80 | /** |
| 81 | * fit_handle_file - main FIT file processing function |
| 82 | * |
| 83 | * fit_handle_file() runs dtc to convert .its to .itb, includes |
| 84 | * binary data, updates timestamp property and calculates hashes. |
| 85 | * |
| 86 | * datafile - .its file |
| 87 | * imagefile - .itb file |
| 88 | * |
| 89 | * returns: |
| 90 | * only on success, otherwise calls exit (EXIT_FAILURE); |
| 91 | */ |
Guilherme Maciel Ferreira | f86ed6a | 2013-12-01 12:43:10 -0700 | [diff] [blame^] | 92 | static int fit_handle_file(struct image_tool_params *params) |
Prafulla Wadaskar | 89a4d6b | 2009-08-19 17:36:46 +0530 | [diff] [blame] | 93 | { |
| 94 | char tmpfile[MKIMAGE_MAX_TMPFILE_LEN]; |
| 95 | char cmd[MKIMAGE_MAX_DTC_CMDLINE_LEN]; |
Simon Glass | e29495d | 2013-06-13 15:10:04 -0700 | [diff] [blame] | 96 | int tfd, destfd = 0; |
| 97 | void *dest_blob = NULL; |
Prafulla Wadaskar | 89a4d6b | 2009-08-19 17:36:46 +0530 | [diff] [blame] | 98 | struct stat sbuf; |
Simon Glass | aa6d6db | 2013-05-08 08:05:57 +0000 | [diff] [blame] | 99 | void *ptr; |
Simon Glass | e29495d | 2013-06-13 15:10:04 -0700 | [diff] [blame] | 100 | off_t destfd_size = 0; |
Prafulla Wadaskar | 89a4d6b | 2009-08-19 17:36:46 +0530 | [diff] [blame] | 101 | |
| 102 | /* Flattened Image Tree (FIT) format handling */ |
| 103 | debug ("FIT format handling\n"); |
| 104 | |
| 105 | /* call dtc to include binary properties into the tmp file */ |
| 106 | if (strlen (params->imagefile) + |
| 107 | strlen (MKIMAGE_TMPFILE_SUFFIX) + 1 > sizeof (tmpfile)) { |
| 108 | fprintf (stderr, "%s: Image file name (%s) too long, " |
| 109 | "can't create tmpfile", |
| 110 | params->imagefile, params->cmdname); |
| 111 | return (EXIT_FAILURE); |
| 112 | } |
| 113 | sprintf (tmpfile, "%s%s", params->imagefile, MKIMAGE_TMPFILE_SUFFIX); |
| 114 | |
Simon Glass | 95d77b4 | 2013-06-13 15:10:05 -0700 | [diff] [blame] | 115 | /* We either compile the source file, or use the existing FIT image */ |
| 116 | if (params->datafile) { |
| 117 | /* dtc -I dts -O dtb -p 500 datafile > tmpfile */ |
| 118 | snprintf(cmd, sizeof(cmd), "%s %s %s > %s", |
| 119 | MKIMAGE_DTC, params->dtc, params->datafile, tmpfile); |
| 120 | debug("Trying to execute \"%s\"\n", cmd); |
| 121 | } else { |
| 122 | snprintf(cmd, sizeof(cmd), "cp %s %s", |
| 123 | params->imagefile, tmpfile); |
| 124 | } |
Prafulla Wadaskar | 89a4d6b | 2009-08-19 17:36:46 +0530 | [diff] [blame] | 125 | if (system (cmd) == -1) { |
| 126 | fprintf (stderr, "%s: system(%s) failed: %s\n", |
| 127 | params->cmdname, cmd, strerror(errno)); |
Simon Glass | aa6d6db | 2013-05-08 08:05:57 +0000 | [diff] [blame] | 128 | goto err_system; |
Prafulla Wadaskar | 89a4d6b | 2009-08-19 17:36:46 +0530 | [diff] [blame] | 129 | } |
| 130 | |
Simon Glass | e29495d | 2013-06-13 15:10:04 -0700 | [diff] [blame] | 131 | if (params->keydest) { |
| 132 | destfd = mmap_fdt(params, params->keydest, &dest_blob, &sbuf); |
| 133 | if (destfd < 0) |
| 134 | goto err_keydest; |
| 135 | destfd_size = sbuf.st_size; |
| 136 | } |
| 137 | |
Simon Glass | aa6d6db | 2013-05-08 08:05:57 +0000 | [diff] [blame] | 138 | tfd = mmap_fdt(params, tmpfile, &ptr, &sbuf); |
| 139 | if (tfd < 0) |
| 140 | goto err_mmap; |
Prafulla Wadaskar | 89a4d6b | 2009-08-19 17:36:46 +0530 | [diff] [blame] | 141 | |
| 142 | /* set hashes for images in the blob */ |
Simon Glass | 399c744 | 2013-06-13 15:10:07 -0700 | [diff] [blame] | 143 | if (fit_add_verification_data(params->keydir, |
| 144 | dest_blob, ptr, params->comment, |
| 145 | params->require_keys)) { |
| 146 | fprintf(stderr, "%s Can't add hashes to FIT blob\n", |
| 147 | params->cmdname); |
Simon Glass | aa6d6db | 2013-05-08 08:05:57 +0000 | [diff] [blame] | 148 | goto err_add_hashes; |
Prafulla Wadaskar | 89a4d6b | 2009-08-19 17:36:46 +0530 | [diff] [blame] | 149 | } |
| 150 | |
Simon Glass | 95d77b4 | 2013-06-13 15:10:05 -0700 | [diff] [blame] | 151 | /* for first image creation, add a timestamp at offset 0 i.e., root */ |
| 152 | if (params->datafile && fit_set_timestamp(ptr, 0, sbuf.st_mtime)) { |
Prafulla Wadaskar | 89a4d6b | 2009-08-19 17:36:46 +0530 | [diff] [blame] | 153 | fprintf (stderr, "%s: Can't add image timestamp\n", |
| 154 | params->cmdname); |
Simon Glass | aa6d6db | 2013-05-08 08:05:57 +0000 | [diff] [blame] | 155 | goto err_add_timestamp; |
Prafulla Wadaskar | 89a4d6b | 2009-08-19 17:36:46 +0530 | [diff] [blame] | 156 | } |
| 157 | debug ("Added timestamp successfully\n"); |
| 158 | |
| 159 | munmap ((void *)ptr, sbuf.st_size); |
| 160 | close (tfd); |
Simon Glass | e29495d | 2013-06-13 15:10:04 -0700 | [diff] [blame] | 161 | if (dest_blob) { |
| 162 | munmap(dest_blob, destfd_size); |
| 163 | close(destfd); |
| 164 | } |
Prafulla Wadaskar | 89a4d6b | 2009-08-19 17:36:46 +0530 | [diff] [blame] | 165 | |
| 166 | if (rename (tmpfile, params->imagefile) == -1) { |
| 167 | fprintf (stderr, "%s: Can't rename %s to %s: %s\n", |
| 168 | params->cmdname, tmpfile, params->imagefile, |
| 169 | strerror (errno)); |
| 170 | unlink (tmpfile); |
| 171 | unlink (params->imagefile); |
| 172 | return (EXIT_FAILURE); |
| 173 | } |
| 174 | return (EXIT_SUCCESS); |
Simon Glass | aa6d6db | 2013-05-08 08:05:57 +0000 | [diff] [blame] | 175 | |
| 176 | err_add_timestamp: |
| 177 | err_add_hashes: |
| 178 | munmap(ptr, sbuf.st_size); |
| 179 | err_mmap: |
Simon Glass | e29495d | 2013-06-13 15:10:04 -0700 | [diff] [blame] | 180 | if (dest_blob) |
| 181 | munmap(dest_blob, destfd_size); |
| 182 | err_keydest: |
Simon Glass | aa6d6db | 2013-05-08 08:05:57 +0000 | [diff] [blame] | 183 | err_system: |
| 184 | unlink(tmpfile); |
| 185 | return -1; |
Prafulla Wadaskar | 89a4d6b | 2009-08-19 17:36:46 +0530 | [diff] [blame] | 186 | } |
| 187 | |
Guilherme Maciel Ferreira | f86ed6a | 2013-12-01 12:43:10 -0700 | [diff] [blame^] | 188 | static int fit_check_params(struct image_tool_params *params) |
Prafulla Wadaskar | 89a4d6b | 2009-08-19 17:36:46 +0530 | [diff] [blame] | 189 | { |
| 190 | return ((params->dflag && (params->fflag || params->lflag)) || |
| 191 | (params->fflag && (params->dflag || params->lflag)) || |
| 192 | (params->lflag && (params->dflag || params->fflag))); |
| 193 | } |
| 194 | |
| 195 | static struct image_type_params fitimage_params = { |
| 196 | .name = "FIT Image support", |
| 197 | .header_size = sizeof(image_header_t), |
| 198 | .hdr = (void*)&header, |
| 199 | .verify_header = fit_verify_header, |
| 200 | .print_header = fit_print_contents, |
| 201 | .check_image_type = fit_check_image_types, |
| 202 | .fflag_handle = fit_handle_file, |
Peter Tyser | 8e1c896 | 2009-11-24 16:42:09 -0600 | [diff] [blame] | 203 | .set_header = NULL, /* FIT images use DTB header */ |
Prafulla Wadaskar | 89a4d6b | 2009-08-19 17:36:46 +0530 | [diff] [blame] | 204 | .check_params = fit_check_params, |
| 205 | }; |
| 206 | |
| 207 | void init_fit_image_type (void) |
| 208 | { |
Guilherme Maciel Ferreira | f86ed6a | 2013-12-01 12:43:10 -0700 | [diff] [blame^] | 209 | register_image_type(&fitimage_params); |
Prafulla Wadaskar | 89a4d6b | 2009-08-19 17:36:46 +0530 | [diff] [blame] | 210 | } |