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 * SPDX-License-Identifier: GPL-2.0+ 15 */ 16 17 #include "imagetool.h" 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, 25 struct image_tool_params *params) 26 { 27 return fdt_check_header(ptr); 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 38 int mmap_fdt(struct image_tool_params *params, const char *fname, void **blobp, 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 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 */ 92 static int fit_handle_file(struct image_tool_params *params) 93 { 94 char tmpfile[MKIMAGE_MAX_TMPFILE_LEN]; 95 char cmd[MKIMAGE_MAX_DTC_CMDLINE_LEN]; 96 int tfd, destfd = 0; 97 void *dest_blob = NULL; 98 struct stat sbuf; 99 void *ptr; 100 off_t destfd_size = 0; 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 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 } 125 if (system (cmd) == -1) { 126 fprintf (stderr, "%s: system(%s) failed: %s\n", 127 params->cmdname, cmd, strerror(errno)); 128 goto err_system; 129 } 130 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 138 tfd = mmap_fdt(params, tmpfile, &ptr, &sbuf); 139 if (tfd < 0) 140 goto err_mmap; 141 142 /* set hashes for images in the blob */ 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); 148 goto err_add_hashes; 149 } 150 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)) { 153 fprintf (stderr, "%s: Can't add image timestamp\n", 154 params->cmdname); 155 goto err_add_timestamp; 156 } 157 debug ("Added timestamp successfully\n"); 158 159 munmap ((void *)ptr, sbuf.st_size); 160 close (tfd); 161 if (dest_blob) { 162 munmap(dest_blob, destfd_size); 163 close(destfd); 164 } 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); 175 176 err_add_timestamp: 177 err_add_hashes: 178 munmap(ptr, sbuf.st_size); 179 err_mmap: 180 if (dest_blob) 181 munmap(dest_blob, destfd_size); 182 err_keydest: 183 err_system: 184 unlink(tmpfile); 185 return -1; 186 } 187 188 static int fit_check_params(struct image_tool_params *params) 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, 203 .set_header = NULL, /* FIT images use DTB header */ 204 .check_params = fit_check_params, 205 }; 206 207 void init_fit_image_type (void) 208 { 209 register_image_type(&fitimage_params); 210 } 211