Prafulla Wadaskar | c713892 | 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 | |
| 50 | /** |
| 51 | * fit_handle_file - main FIT file processing function |
| 52 | * |
| 53 | * fit_handle_file() runs dtc to convert .its to .itb, includes |
| 54 | * binary data, updates timestamp property and calculates hashes. |
| 55 | * |
| 56 | * datafile - .its file |
| 57 | * imagefile - .itb file |
| 58 | * |
| 59 | * returns: |
| 60 | * only on success, otherwise calls exit (EXIT_FAILURE); |
| 61 | */ |
| 62 | static int fit_handle_file (struct mkimage_params *params) |
| 63 | { |
| 64 | char tmpfile[MKIMAGE_MAX_TMPFILE_LEN]; |
| 65 | char cmd[MKIMAGE_MAX_DTC_CMDLINE_LEN]; |
| 66 | int tfd; |
| 67 | struct stat sbuf; |
| 68 | unsigned char *ptr; |
| 69 | |
| 70 | /* Flattened Image Tree (FIT) format handling */ |
| 71 | debug ("FIT format handling\n"); |
| 72 | |
| 73 | /* call dtc to include binary properties into the tmp file */ |
| 74 | if (strlen (params->imagefile) + |
| 75 | strlen (MKIMAGE_TMPFILE_SUFFIX) + 1 > sizeof (tmpfile)) { |
| 76 | fprintf (stderr, "%s: Image file name (%s) too long, " |
| 77 | "can't create tmpfile", |
| 78 | params->imagefile, params->cmdname); |
| 79 | return (EXIT_FAILURE); |
| 80 | } |
| 81 | sprintf (tmpfile, "%s%s", params->imagefile, MKIMAGE_TMPFILE_SUFFIX); |
| 82 | |
| 83 | /* dtc -I dts -O -p 200 datafile > tmpfile */ |
| 84 | sprintf (cmd, "%s %s %s > %s", |
| 85 | MKIMAGE_DTC, params->dtc, params->datafile, tmpfile); |
| 86 | debug ("Trying to execute \"%s\"\n", cmd); |
| 87 | if (system (cmd) == -1) { |
| 88 | fprintf (stderr, "%s: system(%s) failed: %s\n", |
| 89 | params->cmdname, cmd, strerror(errno)); |
| 90 | unlink (tmpfile); |
| 91 | return (EXIT_FAILURE); |
| 92 | } |
| 93 | |
| 94 | /* load FIT blob into memory */ |
| 95 | tfd = open (tmpfile, O_RDWR|O_BINARY); |
| 96 | |
| 97 | if (tfd < 0) { |
| 98 | fprintf (stderr, "%s: Can't open %s: %s\n", |
| 99 | params->cmdname, tmpfile, strerror(errno)); |
| 100 | unlink (tmpfile); |
| 101 | return (EXIT_FAILURE); |
| 102 | } |
| 103 | |
| 104 | if (fstat (tfd, &sbuf) < 0) { |
| 105 | fprintf (stderr, "%s: Can't stat %s: %s\n", |
| 106 | params->cmdname, tmpfile, strerror(errno)); |
| 107 | unlink (tmpfile); |
| 108 | return (EXIT_FAILURE); |
| 109 | } |
| 110 | |
| 111 | ptr = mmap (0, sbuf.st_size, PROT_READ|PROT_WRITE, MAP_SHARED, |
| 112 | tfd, 0); |
| 113 | if (ptr == MAP_FAILED) { |
| 114 | fprintf (stderr, "%s: Can't read %s: %s\n", |
| 115 | params->cmdname, tmpfile, strerror(errno)); |
| 116 | unlink (tmpfile); |
| 117 | return (EXIT_FAILURE); |
| 118 | } |
| 119 | |
| 120 | /* check if ptr has a valid blob */ |
| 121 | if (fdt_check_header (ptr)) { |
| 122 | fprintf (stderr, "%s: Invalid FIT blob\n", params->cmdname); |
| 123 | unlink (tmpfile); |
| 124 | return (EXIT_FAILURE); |
| 125 | } |
| 126 | |
| 127 | /* set hashes for images in the blob */ |
| 128 | if (fit_set_hashes (ptr)) { |
| 129 | fprintf (stderr, "%s Can't add hashes to FIT blob", |
| 130 | params->cmdname); |
| 131 | unlink (tmpfile); |
| 132 | return (EXIT_FAILURE); |
| 133 | } |
| 134 | |
| 135 | /* add a timestamp at offset 0 i.e., root */ |
| 136 | if (fit_set_timestamp (ptr, 0, sbuf.st_mtime)) { |
| 137 | fprintf (stderr, "%s: Can't add image timestamp\n", |
| 138 | params->cmdname); |
| 139 | unlink (tmpfile); |
| 140 | return (EXIT_FAILURE); |
| 141 | } |
| 142 | debug ("Added timestamp successfully\n"); |
| 143 | |
| 144 | munmap ((void *)ptr, sbuf.st_size); |
| 145 | close (tfd); |
| 146 | |
| 147 | if (rename (tmpfile, params->imagefile) == -1) { |
| 148 | fprintf (stderr, "%s: Can't rename %s to %s: %s\n", |
| 149 | params->cmdname, tmpfile, params->imagefile, |
| 150 | strerror (errno)); |
| 151 | unlink (tmpfile); |
| 152 | unlink (params->imagefile); |
| 153 | return (EXIT_FAILURE); |
| 154 | } |
| 155 | return (EXIT_SUCCESS); |
| 156 | } |
| 157 | |
| 158 | static void fit_set_header (void *ptr, struct stat *sbuf, int ifd, |
| 159 | struct mkimage_params *params) |
| 160 | { |
| 161 | uint32_t checksum; |
| 162 | |
| 163 | image_header_t * hdr = (image_header_t *)ptr; |
| 164 | |
| 165 | checksum = crc32 (0, |
| 166 | (const unsigned char *)(ptr + |
| 167 | sizeof(image_header_t)), |
| 168 | sbuf->st_size - sizeof(image_header_t)); |
| 169 | |
| 170 | /* Build new header */ |
| 171 | image_set_magic (hdr, IH_MAGIC); |
| 172 | image_set_time (hdr, sbuf->st_mtime); |
| 173 | image_set_size (hdr, sbuf->st_size - sizeof(image_header_t)); |
| 174 | image_set_load (hdr, params->addr); |
| 175 | image_set_ep (hdr, params->ep); |
| 176 | image_set_dcrc (hdr, checksum); |
| 177 | image_set_os (hdr, params->os); |
| 178 | image_set_arch (hdr, params->arch); |
| 179 | image_set_type (hdr, params->type); |
| 180 | image_set_comp (hdr, params->comp); |
| 181 | |
| 182 | image_set_name (hdr, params->imagename); |
| 183 | |
| 184 | checksum = crc32 (0, (const unsigned char *)hdr, |
| 185 | sizeof(image_header_t)); |
| 186 | |
| 187 | image_set_hcrc (hdr, checksum); |
| 188 | } |
| 189 | |
| 190 | static int fit_check_params (struct mkimage_params *params) |
| 191 | { |
| 192 | return ((params->dflag && (params->fflag || params->lflag)) || |
| 193 | (params->fflag && (params->dflag || params->lflag)) || |
| 194 | (params->lflag && (params->dflag || params->fflag))); |
| 195 | } |
| 196 | |
| 197 | static struct image_type_params fitimage_params = { |
| 198 | .name = "FIT Image support", |
| 199 | .header_size = sizeof(image_header_t), |
| 200 | .hdr = (void*)&header, |
| 201 | .verify_header = fit_verify_header, |
| 202 | .print_header = fit_print_contents, |
| 203 | .check_image_type = fit_check_image_types, |
| 204 | .fflag_handle = fit_handle_file, |
| 205 | .set_header = fit_set_header, |
| 206 | .check_params = fit_check_params, |
| 207 | }; |
| 208 | |
| 209 | void init_fit_image_type (void) |
| 210 | { |
| 211 | mkimage_register (&fitimage_params); |
| 212 | } |