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" |
Heiko Schocher | 6bf4ca0 | 2014-03-03 12:19:29 +0100 | [diff] [blame] | 18 | #include "fit_common.h" |
Prafulla Wadaskar | 89a4d6b | 2009-08-19 17:36:46 +0530 | [diff] [blame] | 19 | #include "mkimage.h" |
| 20 | #include <image.h> |
| 21 | #include <u-boot/crc.h> |
| 22 | |
| 23 | static image_header_t header; |
| 24 | |
Prafulla Wadaskar | 89a4d6b | 2009-08-19 17:36:46 +0530 | [diff] [blame] | 25 | /** |
| 26 | * fit_handle_file - main FIT file processing function |
| 27 | * |
| 28 | * fit_handle_file() runs dtc to convert .its to .itb, includes |
| 29 | * binary data, updates timestamp property and calculates hashes. |
| 30 | * |
| 31 | * datafile - .its file |
| 32 | * imagefile - .itb file |
| 33 | * |
| 34 | * returns: |
| 35 | * only on success, otherwise calls exit (EXIT_FAILURE); |
| 36 | */ |
Guilherme Maciel Ferreira | f86ed6a | 2013-12-01 12:43:10 -0700 | [diff] [blame] | 37 | static int fit_handle_file(struct image_tool_params *params) |
Prafulla Wadaskar | 89a4d6b | 2009-08-19 17:36:46 +0530 | [diff] [blame] | 38 | { |
| 39 | char tmpfile[MKIMAGE_MAX_TMPFILE_LEN]; |
| 40 | char cmd[MKIMAGE_MAX_DTC_CMDLINE_LEN]; |
Simon Glass | e29495d | 2013-06-13 15:10:04 -0700 | [diff] [blame] | 41 | int tfd, destfd = 0; |
| 42 | void *dest_blob = NULL; |
Prafulla Wadaskar | 89a4d6b | 2009-08-19 17:36:46 +0530 | [diff] [blame] | 43 | struct stat sbuf; |
Simon Glass | aa6d6db | 2013-05-08 08:05:57 +0000 | [diff] [blame] | 44 | void *ptr; |
Simon Glass | e29495d | 2013-06-13 15:10:04 -0700 | [diff] [blame] | 45 | off_t destfd_size = 0; |
Prafulla Wadaskar | 89a4d6b | 2009-08-19 17:36:46 +0530 | [diff] [blame] | 46 | |
| 47 | /* Flattened Image Tree (FIT) format handling */ |
| 48 | debug ("FIT format handling\n"); |
| 49 | |
| 50 | /* call dtc to include binary properties into the tmp file */ |
| 51 | if (strlen (params->imagefile) + |
| 52 | strlen (MKIMAGE_TMPFILE_SUFFIX) + 1 > sizeof (tmpfile)) { |
| 53 | fprintf (stderr, "%s: Image file name (%s) too long, " |
| 54 | "can't create tmpfile", |
| 55 | params->imagefile, params->cmdname); |
| 56 | return (EXIT_FAILURE); |
| 57 | } |
| 58 | sprintf (tmpfile, "%s%s", params->imagefile, MKIMAGE_TMPFILE_SUFFIX); |
| 59 | |
Simon Glass | 95d77b4 | 2013-06-13 15:10:05 -0700 | [diff] [blame] | 60 | /* We either compile the source file, or use the existing FIT image */ |
| 61 | if (params->datafile) { |
| 62 | /* dtc -I dts -O dtb -p 500 datafile > tmpfile */ |
| 63 | snprintf(cmd, sizeof(cmd), "%s %s %s > %s", |
| 64 | MKIMAGE_DTC, params->dtc, params->datafile, tmpfile); |
| 65 | debug("Trying to execute \"%s\"\n", cmd); |
| 66 | } else { |
| 67 | snprintf(cmd, sizeof(cmd), "cp %s %s", |
| 68 | params->imagefile, tmpfile); |
| 69 | } |
Prafulla Wadaskar | 89a4d6b | 2009-08-19 17:36:46 +0530 | [diff] [blame] | 70 | if (system (cmd) == -1) { |
| 71 | fprintf (stderr, "%s: system(%s) failed: %s\n", |
| 72 | params->cmdname, cmd, strerror(errno)); |
Simon Glass | aa6d6db | 2013-05-08 08:05:57 +0000 | [diff] [blame] | 73 | goto err_system; |
Prafulla Wadaskar | 89a4d6b | 2009-08-19 17:36:46 +0530 | [diff] [blame] | 74 | } |
| 75 | |
Simon Glass | e29495d | 2013-06-13 15:10:04 -0700 | [diff] [blame] | 76 | if (params->keydest) { |
Heiko Schocher | 6bf4ca0 | 2014-03-03 12:19:29 +0100 | [diff] [blame] | 77 | destfd = mmap_fdt(params->cmdname, params->keydest, |
| 78 | &dest_blob, &sbuf, 1); |
Simon Glass | e29495d | 2013-06-13 15:10:04 -0700 | [diff] [blame] | 79 | if (destfd < 0) |
| 80 | goto err_keydest; |
| 81 | destfd_size = sbuf.st_size; |
| 82 | } |
| 83 | |
Heiko Schocher | 6bf4ca0 | 2014-03-03 12:19:29 +0100 | [diff] [blame] | 84 | tfd = mmap_fdt(params->cmdname, tmpfile, &ptr, &sbuf, 1); |
Simon Glass | aa6d6db | 2013-05-08 08:05:57 +0000 | [diff] [blame] | 85 | if (tfd < 0) |
| 86 | goto err_mmap; |
Prafulla Wadaskar | 89a4d6b | 2009-08-19 17:36:46 +0530 | [diff] [blame] | 87 | |
| 88 | /* set hashes for images in the blob */ |
Simon Glass | 399c744 | 2013-06-13 15:10:07 -0700 | [diff] [blame] | 89 | if (fit_add_verification_data(params->keydir, |
| 90 | dest_blob, ptr, params->comment, |
| 91 | params->require_keys)) { |
| 92 | fprintf(stderr, "%s Can't add hashes to FIT blob\n", |
| 93 | params->cmdname); |
Simon Glass | aa6d6db | 2013-05-08 08:05:57 +0000 | [diff] [blame] | 94 | goto err_add_hashes; |
Prafulla Wadaskar | 89a4d6b | 2009-08-19 17:36:46 +0530 | [diff] [blame] | 95 | } |
| 96 | |
Simon Glass | 95d77b4 | 2013-06-13 15:10:05 -0700 | [diff] [blame] | 97 | /* for first image creation, add a timestamp at offset 0 i.e., root */ |
| 98 | if (params->datafile && fit_set_timestamp(ptr, 0, sbuf.st_mtime)) { |
Prafulla Wadaskar | 89a4d6b | 2009-08-19 17:36:46 +0530 | [diff] [blame] | 99 | fprintf (stderr, "%s: Can't add image timestamp\n", |
| 100 | params->cmdname); |
Simon Glass | aa6d6db | 2013-05-08 08:05:57 +0000 | [diff] [blame] | 101 | goto err_add_timestamp; |
Prafulla Wadaskar | 89a4d6b | 2009-08-19 17:36:46 +0530 | [diff] [blame] | 102 | } |
| 103 | debug ("Added timestamp successfully\n"); |
| 104 | |
| 105 | munmap ((void *)ptr, sbuf.st_size); |
| 106 | close (tfd); |
Simon Glass | e29495d | 2013-06-13 15:10:04 -0700 | [diff] [blame] | 107 | if (dest_blob) { |
| 108 | munmap(dest_blob, destfd_size); |
| 109 | close(destfd); |
| 110 | } |
Prafulla Wadaskar | 89a4d6b | 2009-08-19 17:36:46 +0530 | [diff] [blame] | 111 | |
| 112 | if (rename (tmpfile, params->imagefile) == -1) { |
| 113 | fprintf (stderr, "%s: Can't rename %s to %s: %s\n", |
| 114 | params->cmdname, tmpfile, params->imagefile, |
| 115 | strerror (errno)); |
| 116 | unlink (tmpfile); |
| 117 | unlink (params->imagefile); |
| 118 | return (EXIT_FAILURE); |
| 119 | } |
| 120 | return (EXIT_SUCCESS); |
Simon Glass | aa6d6db | 2013-05-08 08:05:57 +0000 | [diff] [blame] | 121 | |
| 122 | err_add_timestamp: |
| 123 | err_add_hashes: |
| 124 | munmap(ptr, sbuf.st_size); |
| 125 | err_mmap: |
Simon Glass | e29495d | 2013-06-13 15:10:04 -0700 | [diff] [blame] | 126 | if (dest_blob) |
| 127 | munmap(dest_blob, destfd_size); |
| 128 | err_keydest: |
Simon Glass | aa6d6db | 2013-05-08 08:05:57 +0000 | [diff] [blame] | 129 | err_system: |
| 130 | unlink(tmpfile); |
| 131 | return -1; |
Prafulla Wadaskar | 89a4d6b | 2009-08-19 17:36:46 +0530 | [diff] [blame] | 132 | } |
| 133 | |
Guilherme Maciel Ferreira | f86ed6a | 2013-12-01 12:43:10 -0700 | [diff] [blame] | 134 | static int fit_check_params(struct image_tool_params *params) |
Prafulla Wadaskar | 89a4d6b | 2009-08-19 17:36:46 +0530 | [diff] [blame] | 135 | { |
| 136 | return ((params->dflag && (params->fflag || params->lflag)) || |
| 137 | (params->fflag && (params->dflag || params->lflag)) || |
| 138 | (params->lflag && (params->dflag || params->fflag))); |
| 139 | } |
| 140 | |
| 141 | static struct image_type_params fitimage_params = { |
| 142 | .name = "FIT Image support", |
| 143 | .header_size = sizeof(image_header_t), |
| 144 | .hdr = (void*)&header, |
| 145 | .verify_header = fit_verify_header, |
| 146 | .print_header = fit_print_contents, |
| 147 | .check_image_type = fit_check_image_types, |
| 148 | .fflag_handle = fit_handle_file, |
Peter Tyser | 8e1c896 | 2009-11-24 16:42:09 -0600 | [diff] [blame] | 149 | .set_header = NULL, /* FIT images use DTB header */ |
Prafulla Wadaskar | 89a4d6b | 2009-08-19 17:36:46 +0530 | [diff] [blame] | 150 | .check_params = fit_check_params, |
| 151 | }; |
| 152 | |
| 153 | void init_fit_image_type (void) |
| 154 | { |
Guilherme Maciel Ferreira | f86ed6a | 2013-12-01 12:43:10 -0700 | [diff] [blame] | 155 | register_image_type(&fitimage_params); |
Prafulla Wadaskar | 89a4d6b | 2009-08-19 17:36:46 +0530 | [diff] [blame] | 156 | } |