blob: 1466164f0ae067e643e5cddf54d6da26a32dc12e [file] [log] [blame]
Prafulla Wadaskar89a4d6b2009-08-19 17:36:46 +05301/*
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 Denk1a459662013-07-08 09:37:19 +020014 * SPDX-License-Identifier: GPL-2.0+
Prafulla Wadaskar89a4d6b2009-08-19 17:36:46 +053015 */
16
Guilherme Maciel Ferreiraf86ed6a2013-12-01 12:43:10 -070017#include "imagetool.h"
Prafulla Wadaskar89a4d6b2009-08-19 17:36:46 +053018#include "mkimage.h"
19#include <image.h>
20#include <u-boot/crc.h>
21
22static image_header_t header;
23
24static int fit_verify_header (unsigned char *ptr, int image_size,
Guilherme Maciel Ferreiraf86ed6a2013-12-01 12:43:10 -070025 struct image_tool_params *params)
Prafulla Wadaskar89a4d6b2009-08-19 17:36:46 +053026{
Masahiro Yamada2f0877c2013-09-19 12:10:18 +090027 return fdt_check_header(ptr);
Prafulla Wadaskar89a4d6b2009-08-19 17:36:46 +053028}
29
30static 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 Ferreiraf86ed6a2013-12-01 12:43:10 -070038int mmap_fdt(struct image_tool_params *params, const char *fname, void **blobp,
Simon Glassaa6d6db2013-05-08 08:05:57 +000039 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 Wadaskar89a4d6b2009-08-19 17:36:46 +053080/**
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 Ferreiraf86ed6a2013-12-01 12:43:10 -070092static int fit_handle_file(struct image_tool_params *params)
Prafulla Wadaskar89a4d6b2009-08-19 17:36:46 +053093{
94 char tmpfile[MKIMAGE_MAX_TMPFILE_LEN];
95 char cmd[MKIMAGE_MAX_DTC_CMDLINE_LEN];
Simon Glasse29495d2013-06-13 15:10:04 -070096 int tfd, destfd = 0;
97 void *dest_blob = NULL;
Prafulla Wadaskar89a4d6b2009-08-19 17:36:46 +053098 struct stat sbuf;
Simon Glassaa6d6db2013-05-08 08:05:57 +000099 void *ptr;
Simon Glasse29495d2013-06-13 15:10:04 -0700100 off_t destfd_size = 0;
Prafulla Wadaskar89a4d6b2009-08-19 17:36:46 +0530101
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 Glass95d77b42013-06-13 15:10:05 -0700115 /* 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 Wadaskar89a4d6b2009-08-19 17:36:46 +0530125 if (system (cmd) == -1) {
126 fprintf (stderr, "%s: system(%s) failed: %s\n",
127 params->cmdname, cmd, strerror(errno));
Simon Glassaa6d6db2013-05-08 08:05:57 +0000128 goto err_system;
Prafulla Wadaskar89a4d6b2009-08-19 17:36:46 +0530129 }
130
Simon Glasse29495d2013-06-13 15:10:04 -0700131 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 Glassaa6d6db2013-05-08 08:05:57 +0000138 tfd = mmap_fdt(params, tmpfile, &ptr, &sbuf);
139 if (tfd < 0)
140 goto err_mmap;
Prafulla Wadaskar89a4d6b2009-08-19 17:36:46 +0530141
142 /* set hashes for images in the blob */
Simon Glass399c7442013-06-13 15:10:07 -0700143 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 Glassaa6d6db2013-05-08 08:05:57 +0000148 goto err_add_hashes;
Prafulla Wadaskar89a4d6b2009-08-19 17:36:46 +0530149 }
150
Simon Glass95d77b42013-06-13 15:10:05 -0700151 /* 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 Wadaskar89a4d6b2009-08-19 17:36:46 +0530153 fprintf (stderr, "%s: Can't add image timestamp\n",
154 params->cmdname);
Simon Glassaa6d6db2013-05-08 08:05:57 +0000155 goto err_add_timestamp;
Prafulla Wadaskar89a4d6b2009-08-19 17:36:46 +0530156 }
157 debug ("Added timestamp successfully\n");
158
159 munmap ((void *)ptr, sbuf.st_size);
160 close (tfd);
Simon Glasse29495d2013-06-13 15:10:04 -0700161 if (dest_blob) {
162 munmap(dest_blob, destfd_size);
163 close(destfd);
164 }
Prafulla Wadaskar89a4d6b2009-08-19 17:36:46 +0530165
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 Glassaa6d6db2013-05-08 08:05:57 +0000175
176err_add_timestamp:
177err_add_hashes:
178 munmap(ptr, sbuf.st_size);
179err_mmap:
Simon Glasse29495d2013-06-13 15:10:04 -0700180 if (dest_blob)
181 munmap(dest_blob, destfd_size);
182err_keydest:
Simon Glassaa6d6db2013-05-08 08:05:57 +0000183err_system:
184 unlink(tmpfile);
185 return -1;
Prafulla Wadaskar89a4d6b2009-08-19 17:36:46 +0530186}
187
Guilherme Maciel Ferreiraf86ed6a2013-12-01 12:43:10 -0700188static int fit_check_params(struct image_tool_params *params)
Prafulla Wadaskar89a4d6b2009-08-19 17:36:46 +0530189{
190 return ((params->dflag && (params->fflag || params->lflag)) ||
191 (params->fflag && (params->dflag || params->lflag)) ||
192 (params->lflag && (params->dflag || params->fflag)));
193}
194
195static 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 Tyser8e1c8962009-11-24 16:42:09 -0600203 .set_header = NULL, /* FIT images use DTB header */
Prafulla Wadaskar89a4d6b2009-08-19 17:36:46 +0530204 .check_params = fit_check_params,
205};
206
207void init_fit_image_type (void)
208{
Guilherme Maciel Ferreiraf86ed6a2013-12-01 12:43:10 -0700209 register_image_type(&fitimage_params);
Prafulla Wadaskar89a4d6b2009-08-19 17:36:46 +0530210}