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 | * |
| 14 | * This program is free software; you can redistribute it and/or |
| 15 | * modify it under the terms of the GNU General Public License as |
| 16 | * published by the Free Software Foundation; either version 2 of |
| 17 | * the License, or (at your option) any later version. |
| 18 | * |
| 19 | * This program is distributed in the hope that it will be useful, |
| 20 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 21 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 22 | * GNU General Public License for more details. |
| 23 | * |
| 24 | * You should have received a copy of the GNU General Public License |
| 25 | * along with this program; if not, write to the Free Software |
| 26 | * Foundation, Inc., 59 Temple Place, Suite 330, Boston, |
| 27 | * MA 02111-1307 USA |
| 28 | */ |
| 29 | |
| 30 | #include "mkimage.h" |
| 31 | #include <image.h> |
| 32 | #include <u-boot/crc.h> |
| 33 | |
| 34 | static image_header_t header; |
| 35 | |
| 36 | static int fit_verify_header (unsigned char *ptr, int image_size, |
| 37 | struct mkimage_params *params) |
| 38 | { |
| 39 | return fdt_check_header ((void *)ptr); |
| 40 | } |
| 41 | |
| 42 | static int fit_check_image_types (uint8_t type) |
| 43 | { |
| 44 | if (type == IH_TYPE_FLATDT) |
| 45 | return EXIT_SUCCESS; |
| 46 | else |
| 47 | return EXIT_FAILURE; |
| 48 | } |
| 49 | |
Simon Glass | aa6d6db | 2013-05-08 08:05:57 +0000 | [diff] [blame] | 50 | int mmap_fdt(struct mkimage_params *params, const char *fname, void **blobp, |
| 51 | struct stat *sbuf) |
| 52 | { |
| 53 | void *ptr; |
| 54 | int fd; |
| 55 | |
| 56 | /* Load FIT blob into memory (we need to write hashes/signatures) */ |
| 57 | fd = open(fname, O_RDWR | O_BINARY); |
| 58 | |
| 59 | if (fd < 0) { |
| 60 | fprintf(stderr, "%s: Can't open %s: %s\n", |
| 61 | params->cmdname, fname, strerror(errno)); |
| 62 | unlink(fname); |
| 63 | return -1; |
| 64 | } |
| 65 | |
| 66 | if (fstat(fd, sbuf) < 0) { |
| 67 | fprintf(stderr, "%s: Can't stat %s: %s\n", |
| 68 | params->cmdname, fname, strerror(errno)); |
| 69 | unlink(fname); |
| 70 | return -1; |
| 71 | } |
| 72 | |
| 73 | ptr = mmap(0, sbuf->st_size, PROT_READ|PROT_WRITE, MAP_SHARED, fd, 0); |
| 74 | if (ptr == MAP_FAILED) { |
| 75 | fprintf(stderr, "%s: Can't read %s: %s\n", |
| 76 | params->cmdname, fname, strerror(errno)); |
| 77 | unlink(fname); |
| 78 | return -1; |
| 79 | } |
| 80 | |
| 81 | /* check if ptr has a valid blob */ |
| 82 | if (fdt_check_header(ptr)) { |
| 83 | fprintf(stderr, "%s: Invalid FIT blob\n", params->cmdname); |
| 84 | unlink(fname); |
| 85 | return -1; |
| 86 | } |
| 87 | |
| 88 | *blobp = ptr; |
| 89 | return fd; |
| 90 | } |
| 91 | |
Prafulla Wadaskar | 89a4d6b | 2009-08-19 17:36:46 +0530 | [diff] [blame] | 92 | /** |
| 93 | * fit_handle_file - main FIT file processing function |
| 94 | * |
| 95 | * fit_handle_file() runs dtc to convert .its to .itb, includes |
| 96 | * binary data, updates timestamp property and calculates hashes. |
| 97 | * |
| 98 | * datafile - .its file |
| 99 | * imagefile - .itb file |
| 100 | * |
| 101 | * returns: |
| 102 | * only on success, otherwise calls exit (EXIT_FAILURE); |
| 103 | */ |
| 104 | static int fit_handle_file (struct mkimage_params *params) |
| 105 | { |
| 106 | char tmpfile[MKIMAGE_MAX_TMPFILE_LEN]; |
| 107 | char cmd[MKIMAGE_MAX_DTC_CMDLINE_LEN]; |
Simon Glass | e29495d | 2013-06-13 15:10:04 -0700 | [diff] [blame^] | 108 | int tfd, destfd = 0; |
| 109 | void *dest_blob = NULL; |
Prafulla Wadaskar | 89a4d6b | 2009-08-19 17:36:46 +0530 | [diff] [blame] | 110 | struct stat sbuf; |
Simon Glass | aa6d6db | 2013-05-08 08:05:57 +0000 | [diff] [blame] | 111 | void *ptr; |
Simon Glass | e29495d | 2013-06-13 15:10:04 -0700 | [diff] [blame^] | 112 | off_t destfd_size = 0; |
Prafulla Wadaskar | 89a4d6b | 2009-08-19 17:36:46 +0530 | [diff] [blame] | 113 | |
| 114 | /* Flattened Image Tree (FIT) format handling */ |
| 115 | debug ("FIT format handling\n"); |
| 116 | |
| 117 | /* call dtc to include binary properties into the tmp file */ |
| 118 | if (strlen (params->imagefile) + |
| 119 | strlen (MKIMAGE_TMPFILE_SUFFIX) + 1 > sizeof (tmpfile)) { |
| 120 | fprintf (stderr, "%s: Image file name (%s) too long, " |
| 121 | "can't create tmpfile", |
| 122 | params->imagefile, params->cmdname); |
| 123 | return (EXIT_FAILURE); |
| 124 | } |
| 125 | sprintf (tmpfile, "%s%s", params->imagefile, MKIMAGE_TMPFILE_SUFFIX); |
| 126 | |
Lars Rasmusson | ee7c0fc | 2012-12-11 11:11:52 +0100 | [diff] [blame] | 127 | /* dtc -I dts -O dtb -p 500 datafile > tmpfile */ |
Prafulla Wadaskar | 89a4d6b | 2009-08-19 17:36:46 +0530 | [diff] [blame] | 128 | sprintf (cmd, "%s %s %s > %s", |
| 129 | MKIMAGE_DTC, params->dtc, params->datafile, tmpfile); |
| 130 | debug ("Trying to execute \"%s\"\n", cmd); |
| 131 | if (system (cmd) == -1) { |
| 132 | fprintf (stderr, "%s: system(%s) failed: %s\n", |
| 133 | params->cmdname, cmd, strerror(errno)); |
Simon Glass | aa6d6db | 2013-05-08 08:05:57 +0000 | [diff] [blame] | 134 | goto err_system; |
Prafulla Wadaskar | 89a4d6b | 2009-08-19 17:36:46 +0530 | [diff] [blame] | 135 | } |
| 136 | |
Simon Glass | e29495d | 2013-06-13 15:10:04 -0700 | [diff] [blame^] | 137 | if (params->keydest) { |
| 138 | destfd = mmap_fdt(params, params->keydest, &dest_blob, &sbuf); |
| 139 | if (destfd < 0) |
| 140 | goto err_keydest; |
| 141 | destfd_size = sbuf.st_size; |
| 142 | } |
| 143 | |
Simon Glass | aa6d6db | 2013-05-08 08:05:57 +0000 | [diff] [blame] | 144 | tfd = mmap_fdt(params, tmpfile, &ptr, &sbuf); |
| 145 | if (tfd < 0) |
| 146 | goto err_mmap; |
Prafulla Wadaskar | 89a4d6b | 2009-08-19 17:36:46 +0530 | [diff] [blame] | 147 | |
| 148 | /* set hashes for images in the blob */ |
Simon Glass | e29495d | 2013-06-13 15:10:04 -0700 | [diff] [blame^] | 149 | if (fit_add_verification_data(params->keydir, dest_blob, ptr, |
| 150 | NULL, 0)) { |
Prafulla Wadaskar | 89a4d6b | 2009-08-19 17:36:46 +0530 | [diff] [blame] | 151 | fprintf (stderr, "%s Can't add hashes to FIT blob", |
| 152 | params->cmdname); |
Simon Glass | aa6d6db | 2013-05-08 08:05:57 +0000 | [diff] [blame] | 153 | goto err_add_hashes; |
Prafulla Wadaskar | 89a4d6b | 2009-08-19 17:36:46 +0530 | [diff] [blame] | 154 | } |
| 155 | |
| 156 | /* add a timestamp at offset 0 i.e., root */ |
| 157 | if (fit_set_timestamp (ptr, 0, sbuf.st_mtime)) { |
| 158 | fprintf (stderr, "%s: Can't add image timestamp\n", |
| 159 | params->cmdname); |
Simon Glass | aa6d6db | 2013-05-08 08:05:57 +0000 | [diff] [blame] | 160 | goto err_add_timestamp; |
Prafulla Wadaskar | 89a4d6b | 2009-08-19 17:36:46 +0530 | [diff] [blame] | 161 | } |
| 162 | debug ("Added timestamp successfully\n"); |
| 163 | |
| 164 | munmap ((void *)ptr, sbuf.st_size); |
| 165 | close (tfd); |
Simon Glass | e29495d | 2013-06-13 15:10:04 -0700 | [diff] [blame^] | 166 | if (dest_blob) { |
| 167 | munmap(dest_blob, destfd_size); |
| 168 | close(destfd); |
| 169 | } |
Prafulla Wadaskar | 89a4d6b | 2009-08-19 17:36:46 +0530 | [diff] [blame] | 170 | |
| 171 | if (rename (tmpfile, params->imagefile) == -1) { |
| 172 | fprintf (stderr, "%s: Can't rename %s to %s: %s\n", |
| 173 | params->cmdname, tmpfile, params->imagefile, |
| 174 | strerror (errno)); |
| 175 | unlink (tmpfile); |
| 176 | unlink (params->imagefile); |
| 177 | return (EXIT_FAILURE); |
| 178 | } |
| 179 | return (EXIT_SUCCESS); |
Simon Glass | aa6d6db | 2013-05-08 08:05:57 +0000 | [diff] [blame] | 180 | |
| 181 | err_add_timestamp: |
| 182 | err_add_hashes: |
| 183 | munmap(ptr, sbuf.st_size); |
| 184 | err_mmap: |
Simon Glass | e29495d | 2013-06-13 15:10:04 -0700 | [diff] [blame^] | 185 | if (dest_blob) |
| 186 | munmap(dest_blob, destfd_size); |
| 187 | err_keydest: |
Simon Glass | aa6d6db | 2013-05-08 08:05:57 +0000 | [diff] [blame] | 188 | err_system: |
| 189 | unlink(tmpfile); |
| 190 | return -1; |
Prafulla Wadaskar | 89a4d6b | 2009-08-19 17:36:46 +0530 | [diff] [blame] | 191 | } |
| 192 | |
Prafulla Wadaskar | 89a4d6b | 2009-08-19 17:36:46 +0530 | [diff] [blame] | 193 | static int fit_check_params (struct mkimage_params *params) |
| 194 | { |
| 195 | return ((params->dflag && (params->fflag || params->lflag)) || |
| 196 | (params->fflag && (params->dflag || params->lflag)) || |
| 197 | (params->lflag && (params->dflag || params->fflag))); |
| 198 | } |
| 199 | |
| 200 | static struct image_type_params fitimage_params = { |
| 201 | .name = "FIT Image support", |
| 202 | .header_size = sizeof(image_header_t), |
| 203 | .hdr = (void*)&header, |
| 204 | .verify_header = fit_verify_header, |
| 205 | .print_header = fit_print_contents, |
| 206 | .check_image_type = fit_check_image_types, |
| 207 | .fflag_handle = fit_handle_file, |
Peter Tyser | 8e1c896 | 2009-11-24 16:42:09 -0600 | [diff] [blame] | 208 | .set_header = NULL, /* FIT images use DTB header */ |
Prafulla Wadaskar | 89a4d6b | 2009-08-19 17:36:46 +0530 | [diff] [blame] | 209 | .check_params = fit_check_params, |
| 210 | }; |
| 211 | |
| 212 | void init_fit_image_type (void) |
| 213 | { |
| 214 | mkimage_register (&fitimage_params); |
| 215 | } |