blob: eeee484cdec096eecda45ad9942fc8354f38f4f3 [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"
Heiko Schocher6bf4ca02014-03-03 12:19:29 +010018#include "fit_common.h"
Prafulla Wadaskar89a4d6b2009-08-19 17:36:46 +053019#include "mkimage.h"
20#include <image.h>
21#include <u-boot/crc.h>
22
23static image_header_t header;
24
Prafulla Wadaskar89a4d6b2009-08-19 17:36:46 +053025/**
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 Ferreiraf86ed6a2013-12-01 12:43:10 -070037static int fit_handle_file(struct image_tool_params *params)
Prafulla Wadaskar89a4d6b2009-08-19 17:36:46 +053038{
39 char tmpfile[MKIMAGE_MAX_TMPFILE_LEN];
40 char cmd[MKIMAGE_MAX_DTC_CMDLINE_LEN];
Simon Glasse29495d2013-06-13 15:10:04 -070041 int tfd, destfd = 0;
42 void *dest_blob = NULL;
Prafulla Wadaskar89a4d6b2009-08-19 17:36:46 +053043 struct stat sbuf;
Simon Glassaa6d6db2013-05-08 08:05:57 +000044 void *ptr;
Simon Glasse29495d2013-06-13 15:10:04 -070045 off_t destfd_size = 0;
Prafulla Wadaskar89a4d6b2009-08-19 17:36:46 +053046
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 Glass95d77b42013-06-13 15:10:05 -070060 /* 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 Wadaskar89a4d6b2009-08-19 17:36:46 +053070 if (system (cmd) == -1) {
71 fprintf (stderr, "%s: system(%s) failed: %s\n",
72 params->cmdname, cmd, strerror(errno));
Simon Glassaa6d6db2013-05-08 08:05:57 +000073 goto err_system;
Prafulla Wadaskar89a4d6b2009-08-19 17:36:46 +053074 }
75
Simon Glasse29495d2013-06-13 15:10:04 -070076 if (params->keydest) {
Heiko Schocher6bf4ca02014-03-03 12:19:29 +010077 destfd = mmap_fdt(params->cmdname, params->keydest,
78 &dest_blob, &sbuf, 1);
Simon Glasse29495d2013-06-13 15:10:04 -070079 if (destfd < 0)
80 goto err_keydest;
81 destfd_size = sbuf.st_size;
82 }
83
Heiko Schocher6bf4ca02014-03-03 12:19:29 +010084 tfd = mmap_fdt(params->cmdname, tmpfile, &ptr, &sbuf, 1);
Simon Glassaa6d6db2013-05-08 08:05:57 +000085 if (tfd < 0)
86 goto err_mmap;
Prafulla Wadaskar89a4d6b2009-08-19 17:36:46 +053087
88 /* set hashes for images in the blob */
Simon Glass399c7442013-06-13 15:10:07 -070089 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 Glassaa6d6db2013-05-08 08:05:57 +000094 goto err_add_hashes;
Prafulla Wadaskar89a4d6b2009-08-19 17:36:46 +053095 }
96
Simon Glass95d77b42013-06-13 15:10:05 -070097 /* 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 Wadaskar89a4d6b2009-08-19 17:36:46 +053099 fprintf (stderr, "%s: Can't add image timestamp\n",
100 params->cmdname);
Simon Glassaa6d6db2013-05-08 08:05:57 +0000101 goto err_add_timestamp;
Prafulla Wadaskar89a4d6b2009-08-19 17:36:46 +0530102 }
103 debug ("Added timestamp successfully\n");
104
105 munmap ((void *)ptr, sbuf.st_size);
106 close (tfd);
Simon Glasse29495d2013-06-13 15:10:04 -0700107 if (dest_blob) {
108 munmap(dest_blob, destfd_size);
109 close(destfd);
110 }
Prafulla Wadaskar89a4d6b2009-08-19 17:36:46 +0530111
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 Glassaa6d6db2013-05-08 08:05:57 +0000121
122err_add_timestamp:
123err_add_hashes:
124 munmap(ptr, sbuf.st_size);
125err_mmap:
Simon Glasse29495d2013-06-13 15:10:04 -0700126 if (dest_blob)
127 munmap(dest_blob, destfd_size);
128err_keydest:
Simon Glassaa6d6db2013-05-08 08:05:57 +0000129err_system:
130 unlink(tmpfile);
131 return -1;
Prafulla Wadaskar89a4d6b2009-08-19 17:36:46 +0530132}
133
Guilherme Maciel Ferreiraf86ed6a2013-12-01 12:43:10 -0700134static int fit_check_params(struct image_tool_params *params)
Prafulla Wadaskar89a4d6b2009-08-19 17:36:46 +0530135{
136 return ((params->dflag && (params->fflag || params->lflag)) ||
137 (params->fflag && (params->dflag || params->lflag)) ||
138 (params->lflag && (params->dflag || params->fflag)));
139}
140
141static 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 Tyser8e1c8962009-11-24 16:42:09 -0600149 .set_header = NULL, /* FIT images use DTB header */
Prafulla Wadaskar89a4d6b2009-08-19 17:36:46 +0530150 .check_params = fit_check_params,
151};
152
153void init_fit_image_type (void)
154{
Guilherme Maciel Ferreiraf86ed6a2013-12-01 12:43:10 -0700155 register_image_type(&fitimage_params);
Prafulla Wadaskar89a4d6b2009-08-19 17:36:46 +0530156}