1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30#include "mkimage.h"
31#include <image.h>
32#include <u-boot/crc.h>
33
34static image_header_t header;
35
36static 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
42static 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
52
53
54
55
56
57
58
59
60
61
62static 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
71 debug ("FIT format handling\n");
72
73
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
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
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
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
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
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
158static int fit_check_params (struct mkimage_params *params)
159{
160 return ((params->dflag && (params->fflag || params->lflag)) ||
161 (params->fflag && (params->dflag || params->lflag)) ||
162 (params->lflag && (params->dflag || params->fflag)));
163}
164
165static struct image_type_params fitimage_params = {
166 .name = "FIT Image support",
167 .header_size = sizeof(image_header_t),
168 .hdr = (void*)&header,
169 .verify_header = fit_verify_header,
170 .print_header = fit_print_contents,
171 .check_image_type = fit_check_image_types,
172 .fflag_handle = fit_handle_file,
173 .set_header = NULL,
174 .check_params = fit_check_params,
175};
176
177void init_fit_image_type (void)
178{
179 mkimage_register (&fitimage_params);
180}
181