blob: 09d52d47652fb3080f18efe8cf9fd0f78eb4baf9 [file] [log] [blame]
Tom Rini10e47792018-05-06 17:58:06 -04001// SPDX-License-Identifier: GPL-2.0+
Prafulla Wadaskar07329412009-09-07 15:05:02 +05302/*
Stefan Roese3b8b19d2014-10-22 12:13:23 +02003 * Image manipulator for Marvell SoCs
Pali Rohárbeddea82021-09-24 23:07:02 +02004 * supports Kirkwood, Dove, Armada 370, Armada XP, Armada 375, Armada 38x and
5 * Armada 39x
Stefan Roese3b8b19d2014-10-22 12:13:23 +02006 *
7 * (C) Copyright 2013 Thomas Petazzoni
8 * <thomas.petazzoni@free-electrons.com>
Pali Rohárbc7bf642022-02-17 10:43:40 +01009 *
10 * (C) Copyright 2022 Pali Rohár <pali@kernel.org>
Prafulla Wadaskar07329412009-09-07 15:05:02 +053011 */
12
Heinrich Schuchardt43ee8012021-12-18 11:25:12 +010013#define OPENSSL_API_COMPAT 0x10101000L
14
Guilherme Maciel Ferreira8ed4d1c2013-12-01 12:43:10 -070015#include "imagetool.h"
Andreas Bießmann7abec5b2014-10-24 23:39:11 +020016#include <limits.h>
Prafulla Wadaskar07329412009-09-07 15:05:02 +053017#include <image.h>
Mario Six10d14492017-01-11 16:01:00 +010018#include <stdarg.h>
Stefan Roese3b8b19d2014-10-22 12:13:23 +020019#include <stdint.h>
Prafulla Wadaskar07329412009-09-07 15:05:02 +053020#include "kwbimage.h"
21
Jelle van der Waae0e55592017-05-08 21:31:20 +020022#include <openssl/bn.h>
Mario Six10d14492017-01-11 16:01:00 +010023#include <openssl/rsa.h>
24#include <openssl/pem.h>
25#include <openssl/err.h>
26#include <openssl/evp.h>
Jelle van der Waae0e55592017-05-08 21:31:20 +020027
Jonathan Gray237d0592018-02-21 02:59:01 +110028#if OPENSSL_VERSION_NUMBER < 0x10100000L || \
29 (defined(LIBRESSL_VERSION_NUMBER) && LIBRESSL_VERSION_NUMBER < 0x2070000fL)
Jelle van der Waae0e55592017-05-08 21:31:20 +020030static void RSA_get0_key(const RSA *r,
31 const BIGNUM **n, const BIGNUM **e, const BIGNUM **d)
32{
33 if (n != NULL)
34 *n = r->n;
35 if (e != NULL)
36 *e = r->e;
37 if (d != NULL)
38 *d = r->d;
39}
40
Jonathan Gray237d0592018-02-21 02:59:01 +110041#elif !defined(LIBRESSL_VERSION_NUMBER)
Jelle van der Waae0e55592017-05-08 21:31:20 +020042void EVP_MD_CTX_cleanup(EVP_MD_CTX *ctx)
43{
44 EVP_MD_CTX_reset(ctx);
45}
46#endif
Mario Six10d14492017-01-11 16:01:00 +010047
Pali Roháre581f192022-02-17 10:43:36 +010048/* fls - find last (most-significant) bit set in 4-bit integer */
49static inline int fls4(int num)
50{
51 if (num & 0x8)
52 return 4;
53 else if (num & 0x4)
54 return 3;
55 else if (num & 0x2)
56 return 2;
57 else if (num & 0x1)
58 return 1;
59 else
60 return 0;
61}
62
Stefan Roese3b8b19d2014-10-22 12:13:23 +020063static struct image_cfg_element *image_cfg;
64static int cfgn;
Mario Six10d14492017-01-11 16:01:00 +010065static int verbose_mode;
Stefan Roese3b8b19d2014-10-22 12:13:23 +020066
67struct boot_mode {
68 unsigned int id;
69 const char *name;
70};
71
Mario Six10d14492017-01-11 16:01:00 +010072/*
73 * SHA2-256 hash
74 */
75struct hash_v1 {
76 uint8_t hash[32];
77};
78
Stefan Roese3b8b19d2014-10-22 12:13:23 +020079struct boot_mode boot_modes[] = {
Pali Rohár611a16b2021-08-11 10:14:17 +020080 { IBR_HDR_I2C_ID, "i2c" },
81 { IBR_HDR_SPI_ID, "spi" },
82 { IBR_HDR_NAND_ID, "nand" },
83 { IBR_HDR_SATA_ID, "sata" },
84 { IBR_HDR_PEX_ID, "pex" },
85 { IBR_HDR_UART_ID, "uart" },
86 { IBR_HDR_SDIO_ID, "sdio" },
Stefan Roese3b8b19d2014-10-22 12:13:23 +020087 {},
Prafulla Wadaskar07329412009-09-07 15:05:02 +053088};
89
Stefan Roese3b8b19d2014-10-22 12:13:23 +020090struct nand_ecc_mode {
91 unsigned int id;
92 const char *name;
93};
94
95struct nand_ecc_mode nand_ecc_modes[] = {
Pali Rohár611a16b2021-08-11 10:14:17 +020096 { IBR_HDR_ECC_DEFAULT, "default" },
97 { IBR_HDR_ECC_FORCED_HAMMING, "hamming" },
98 { IBR_HDR_ECC_FORCED_RS, "rs" },
99 { IBR_HDR_ECC_DISABLED, "disabled" },
Stefan Roese3b8b19d2014-10-22 12:13:23 +0200100 {},
101};
102
103/* Used to identify an undefined execution or destination address */
104#define ADDR_INVALID ((uint32_t)-1)
105
Pali Rohár13b70402021-07-23 11:14:07 +0200106#define BINARY_MAX_ARGS 255
Stefan Roese3b8b19d2014-10-22 12:13:23 +0200107
108/* In-memory representation of a line of the configuration file */
Mario Six62da6762017-01-11 16:00:59 +0100109
110enum image_cfg_type {
111 IMAGE_CFG_VERSION = 0x1,
112 IMAGE_CFG_BOOT_FROM,
113 IMAGE_CFG_DEST_ADDR,
114 IMAGE_CFG_EXEC_ADDR,
115 IMAGE_CFG_NAND_BLKSZ,
116 IMAGE_CFG_NAND_BADBLK_LOCATION,
117 IMAGE_CFG_NAND_ECC_MODE,
118 IMAGE_CFG_NAND_PAGESZ,
Pali Rohár6b76c1c2022-01-12 18:20:40 +0100119 IMAGE_CFG_CPU,
Mario Six62da6762017-01-11 16:00:59 +0100120 IMAGE_CFG_BINARY,
Mario Six62da6762017-01-11 16:00:59 +0100121 IMAGE_CFG_DATA,
Pali Rohárc0cfd1a2021-07-23 11:14:12 +0200122 IMAGE_CFG_DATA_DELAY,
Mario Six62da6762017-01-11 16:00:59 +0100123 IMAGE_CFG_BAUDRATE,
Pali Rohárd8840942021-11-08 18:12:41 +0100124 IMAGE_CFG_UART_PORT,
125 IMAGE_CFG_UART_MPP,
Mario Six62da6762017-01-11 16:00:59 +0100126 IMAGE_CFG_DEBUG,
Mario Six10d14492017-01-11 16:01:00 +0100127 IMAGE_CFG_KAK,
128 IMAGE_CFG_CSK,
129 IMAGE_CFG_CSK_INDEX,
130 IMAGE_CFG_JTAG_DELAY,
131 IMAGE_CFG_BOX_ID,
132 IMAGE_CFG_FLASH_ID,
133 IMAGE_CFG_SEC_COMMON_IMG,
134 IMAGE_CFG_SEC_SPECIALIZED_IMG,
135 IMAGE_CFG_SEC_BOOT_DEV,
136 IMAGE_CFG_SEC_FUSE_DUMP,
Mario Six62da6762017-01-11 16:00:59 +0100137
138 IMAGE_CFG_COUNT
139} type;
140
141static const char * const id_strs[] = {
142 [IMAGE_CFG_VERSION] = "VERSION",
143 [IMAGE_CFG_BOOT_FROM] = "BOOT_FROM",
144 [IMAGE_CFG_DEST_ADDR] = "DEST_ADDR",
145 [IMAGE_CFG_EXEC_ADDR] = "EXEC_ADDR",
146 [IMAGE_CFG_NAND_BLKSZ] = "NAND_BLKSZ",
147 [IMAGE_CFG_NAND_BADBLK_LOCATION] = "NAND_BADBLK_LOCATION",
148 [IMAGE_CFG_NAND_ECC_MODE] = "NAND_ECC_MODE",
149 [IMAGE_CFG_NAND_PAGESZ] = "NAND_PAGE_SIZE",
Pali Rohár6b76c1c2022-01-12 18:20:40 +0100150 [IMAGE_CFG_CPU] = "CPU",
Mario Six62da6762017-01-11 16:00:59 +0100151 [IMAGE_CFG_BINARY] = "BINARY",
Mario Six62da6762017-01-11 16:00:59 +0100152 [IMAGE_CFG_DATA] = "DATA",
Pali Rohárc0cfd1a2021-07-23 11:14:12 +0200153 [IMAGE_CFG_DATA_DELAY] = "DATA_DELAY",
Mario Six62da6762017-01-11 16:00:59 +0100154 [IMAGE_CFG_BAUDRATE] = "BAUDRATE",
Pali Rohárd8840942021-11-08 18:12:41 +0100155 [IMAGE_CFG_UART_PORT] = "UART_PORT",
156 [IMAGE_CFG_UART_MPP] = "UART_MPP",
Mario Six62da6762017-01-11 16:00:59 +0100157 [IMAGE_CFG_DEBUG] = "DEBUG",
Mario Six10d14492017-01-11 16:01:00 +0100158 [IMAGE_CFG_KAK] = "KAK",
159 [IMAGE_CFG_CSK] = "CSK",
160 [IMAGE_CFG_CSK_INDEX] = "CSK_INDEX",
161 [IMAGE_CFG_JTAG_DELAY] = "JTAG_DELAY",
162 [IMAGE_CFG_BOX_ID] = "BOX_ID",
163 [IMAGE_CFG_FLASH_ID] = "FLASH_ID",
164 [IMAGE_CFG_SEC_COMMON_IMG] = "SEC_COMMON_IMG",
165 [IMAGE_CFG_SEC_SPECIALIZED_IMG] = "SEC_SPECIALIZED_IMG",
166 [IMAGE_CFG_SEC_BOOT_DEV] = "SEC_BOOT_DEV",
167 [IMAGE_CFG_SEC_FUSE_DUMP] = "SEC_FUSE_DUMP"
Mario Six62da6762017-01-11 16:00:59 +0100168};
169
Stefan Roese3b8b19d2014-10-22 12:13:23 +0200170struct image_cfg_element {
Mario Six62da6762017-01-11 16:00:59 +0100171 enum image_cfg_type type;
Stefan Roese3b8b19d2014-10-22 12:13:23 +0200172 union {
173 unsigned int version;
Pali Rohár6b76c1c2022-01-12 18:20:40 +0100174 unsigned int cpu_sheeva;
Stefan Roese3b8b19d2014-10-22 12:13:23 +0200175 unsigned int bootfrom;
176 struct {
177 const char *file;
Pali Rohárfc719962022-01-12 18:20:41 +0100178 unsigned int loadaddr;
Stefan Roese3b8b19d2014-10-22 12:13:23 +0200179 unsigned int args[BINARY_MAX_ARGS];
180 unsigned int nargs;
181 } binary;
Stefan Roese3b8b19d2014-10-22 12:13:23 +0200182 unsigned int dstaddr;
183 unsigned int execaddr;
184 unsigned int nandblksz;
185 unsigned int nandbadblklocation;
186 unsigned int nandeccmode;
187 unsigned int nandpagesz;
188 struct ext_hdr_v0_reg regdata;
Pali Rohárc0cfd1a2021-07-23 11:14:12 +0200189 unsigned int regdata_delay;
Chris Packham883bf452016-11-09 22:07:45 +1300190 unsigned int baudrate;
Pali Rohárd8840942021-11-08 18:12:41 +0100191 unsigned int uart_port;
192 unsigned int uart_mpp;
Chris Packham1e0728a2016-11-09 22:21:45 +1300193 unsigned int debug;
Mario Six10d14492017-01-11 16:01:00 +0100194 const char *key_name;
195 int csk_idx;
196 uint8_t jtag_delay;
197 uint32_t boxid;
198 uint32_t flashid;
199 bool sec_specialized_img;
200 unsigned int sec_boot_dev;
201 const char *name;
Stefan Roese3b8b19d2014-10-22 12:13:23 +0200202 };
203};
204
205#define IMAGE_CFG_ELEMENT_MAX 256
Prafulla Wadaskar07329412009-09-07 15:05:02 +0530206
207/*
Stefan Roese3b8b19d2014-10-22 12:13:23 +0200208 * Utility functions to manipulate boot mode and ecc modes (convert
209 * them back and forth between description strings and the
210 * corresponding numerical identifiers).
211 */
212
213static const char *image_boot_mode_name(unsigned int id)
Prafulla Wadaskar07329412009-09-07 15:05:02 +0530214{
Stefan Roese3b8b19d2014-10-22 12:13:23 +0200215 int i;
Mario Sixd6009d72017-01-11 16:00:54 +0100216
Stefan Roese3b8b19d2014-10-22 12:13:23 +0200217 for (i = 0; boot_modes[i].name; i++)
218 if (boot_modes[i].id == id)
219 return boot_modes[i].name;
220 return NULL;
221}
Prafulla Wadaskar07329412009-09-07 15:05:02 +0530222
Pali Rohár31376142022-01-12 18:20:35 +0100223static int image_boot_mode_id(const char *boot_mode_name)
Stefan Roese3b8b19d2014-10-22 12:13:23 +0200224{
225 int i;
Mario Sixd6009d72017-01-11 16:00:54 +0100226
Stefan Roese3b8b19d2014-10-22 12:13:23 +0200227 for (i = 0; boot_modes[i].name; i++)
228 if (!strcmp(boot_modes[i].name, boot_mode_name))
229 return boot_modes[i].id;
230
231 return -1;
232}
233
Pali Rohár3d015782022-01-12 18:20:50 +0100234static const char *image_nand_ecc_mode_name(unsigned int id)
235{
236 int i;
237
238 for (i = 0; nand_ecc_modes[i].name; i++)
239 if (nand_ecc_modes[i].id == id)
240 return nand_ecc_modes[i].name;
241
242 return NULL;
243}
244
Pali Rohár31376142022-01-12 18:20:35 +0100245static int image_nand_ecc_mode_id(const char *nand_ecc_mode_name)
Stefan Roese3b8b19d2014-10-22 12:13:23 +0200246{
247 int i;
Mario Sixd6009d72017-01-11 16:00:54 +0100248
Stefan Roese3b8b19d2014-10-22 12:13:23 +0200249 for (i = 0; nand_ecc_modes[i].name; i++)
250 if (!strcmp(nand_ecc_modes[i].name, nand_ecc_mode_name))
251 return nand_ecc_modes[i].id;
252 return -1;
253}
254
255static struct image_cfg_element *
256image_find_option(unsigned int optiontype)
257{
258 int i;
259
260 for (i = 0; i < cfgn; i++) {
261 if (image_cfg[i].type == optiontype)
262 return &image_cfg[i];
Prafulla Wadaskar07329412009-09-07 15:05:02 +0530263 }
Stefan Roese3b8b19d2014-10-22 12:13:23 +0200264
265 return NULL;
Prafulla Wadaskar07329412009-09-07 15:05:02 +0530266}
267
Stefan Roese3b8b19d2014-10-22 12:13:23 +0200268static unsigned int
269image_count_options(unsigned int optiontype)
270{
271 int i;
272 unsigned int count = 0;
273
274 for (i = 0; i < cfgn; i++)
275 if (image_cfg[i].type == optiontype)
276 count++;
277
278 return count;
279}
280
Mario Six10d14492017-01-11 16:01:00 +0100281static int image_get_csk_index(void)
282{
283 struct image_cfg_element *e;
284
285 e = image_find_option(IMAGE_CFG_CSK_INDEX);
286 if (!e)
287 return -1;
288
289 return e->csk_idx;
290}
291
292static bool image_get_spezialized_img(void)
293{
294 struct image_cfg_element *e;
295
296 e = image_find_option(IMAGE_CFG_SEC_SPECIALIZED_IMG);
297 if (!e)
298 return false;
299
300 return e->sec_specialized_img;
301}
302
Pali Rohár1e2d7fb2021-11-08 18:12:43 +0100303static int image_get_bootfrom(void)
304{
305 struct image_cfg_element *e;
306
307 e = image_find_option(IMAGE_CFG_BOOT_FROM);
308 if (!e)
309 /* fallback to SPI if no BOOT_FROM is not provided */
310 return IBR_HDR_SPI_ID;
311
312 return e->bootfrom;
313}
314
Pali Rohár6b76c1c2022-01-12 18:20:40 +0100315static int image_is_cpu_sheeva(void)
316{
317 struct image_cfg_element *e;
318
319 e = image_find_option(IMAGE_CFG_CPU);
320 if (!e)
321 return 0;
322
323 return e->cpu_sheeva;
324}
325
Prafulla Wadaskar07329412009-09-07 15:05:02 +0530326/*
Stefan Roese3b8b19d2014-10-22 12:13:23 +0200327 * Compute a 8-bit checksum of a memory area. This algorithm follows
328 * the requirements of the Marvell SoC BootROM specifications.
Prafulla Wadaskar07329412009-09-07 15:05:02 +0530329 */
Stefan Roese3b8b19d2014-10-22 12:13:23 +0200330static uint8_t image_checksum8(void *start, uint32_t len)
Prafulla Wadaskar07329412009-09-07 15:05:02 +0530331{
Stefan Roese3b8b19d2014-10-22 12:13:23 +0200332 uint8_t csum = 0;
333 uint8_t *p = start;
Prafulla Wadaskar07329412009-09-07 15:05:02 +0530334
335 /* check len and return zero checksum if invalid */
336 if (!len)
337 return 0;
338
339 do {
Stefan Roese3b8b19d2014-10-22 12:13:23 +0200340 csum += *p;
Prafulla Wadaskar07329412009-09-07 15:05:02 +0530341 p++;
342 } while (--len);
Stefan Roese3b8b19d2014-10-22 12:13:23 +0200343
344 return csum;
Prafulla Wadaskar07329412009-09-07 15:05:02 +0530345}
346
Baruch Siach4a5b99b2017-07-04 20:23:40 +0300347/*
348 * Verify checksum over a complete header that includes the checksum field.
349 * Return 1 when OK, otherwise 0.
350 */
351static int main_hdr_checksum_ok(void *hdr)
352{
353 /* Offsets of checksum in v0 and v1 headers are the same */
354 struct main_hdr_v0 *main_hdr = (struct main_hdr_v0 *)hdr;
355 uint8_t checksum;
356
Marek Behúnd1b0b032021-09-24 23:07:01 +0200357 checksum = image_checksum8(hdr, kwbheader_size_for_csum(hdr));
Baruch Siach4a5b99b2017-07-04 20:23:40 +0300358 /* Calculated checksum includes the header checksum field. Compensate
359 * for that.
360 */
361 checksum -= main_hdr->checksum;
362
363 return checksum == main_hdr->checksum;
364}
365
Stefan Roese3b8b19d2014-10-22 12:13:23 +0200366static uint32_t image_checksum32(void *start, uint32_t len)
Prafulla Wadaskar07329412009-09-07 15:05:02 +0530367{
Stefan Roese3b8b19d2014-10-22 12:13:23 +0200368 uint32_t csum = 0;
369 uint32_t *p = start;
Prafulla Wadaskar07329412009-09-07 15:05:02 +0530370
371 /* check len and return zero checksum if invalid */
372 if (!len)
373 return 0;
374
375 if (len % sizeof(uint32_t)) {
Stefan Roese3b8b19d2014-10-22 12:13:23 +0200376 fprintf(stderr, "Length %d is not in multiple of %zu\n",
377 len, sizeof(uint32_t));
Prafulla Wadaskar07329412009-09-07 15:05:02 +0530378 return 0;
379 }
380
381 do {
Stefan Roese3b8b19d2014-10-22 12:13:23 +0200382 csum += *p;
Prafulla Wadaskar07329412009-09-07 15:05:02 +0530383 p++;
384 len -= sizeof(uint32_t);
385 } while (len > 0);
Stefan Roese3b8b19d2014-10-22 12:13:23 +0200386
387 return csum;
Prafulla Wadaskar07329412009-09-07 15:05:02 +0530388}
389
Pali Rohár3d015782022-01-12 18:20:50 +0100390static unsigned int options_to_baudrate(uint8_t options)
391{
392 switch (options & 0x7) {
393 case MAIN_HDR_V1_OPT_BAUD_2400:
394 return 2400;
395 case MAIN_HDR_V1_OPT_BAUD_4800:
396 return 4800;
397 case MAIN_HDR_V1_OPT_BAUD_9600:
398 return 9600;
399 case MAIN_HDR_V1_OPT_BAUD_19200:
400 return 19200;
401 case MAIN_HDR_V1_OPT_BAUD_38400:
402 return 38400;
403 case MAIN_HDR_V1_OPT_BAUD_57600:
404 return 57600;
405 case MAIN_HDR_V1_OPT_BAUD_115200:
406 return 115200;
407 case MAIN_HDR_V1_OPT_BAUD_DEFAULT:
408 default:
409 return 0;
410 }
411}
412
Chris Packham883bf452016-11-09 22:07:45 +1300413static uint8_t baudrate_to_option(unsigned int baudrate)
414{
415 switch (baudrate) {
416 case 2400:
417 return MAIN_HDR_V1_OPT_BAUD_2400;
418 case 4800:
419 return MAIN_HDR_V1_OPT_BAUD_4800;
420 case 9600:
421 return MAIN_HDR_V1_OPT_BAUD_9600;
422 case 19200:
423 return MAIN_HDR_V1_OPT_BAUD_19200;
424 case 38400:
425 return MAIN_HDR_V1_OPT_BAUD_38400;
426 case 57600:
427 return MAIN_HDR_V1_OPT_BAUD_57600;
428 case 115200:
429 return MAIN_HDR_V1_OPT_BAUD_115200;
430 default:
431 return MAIN_HDR_V1_OPT_BAUD_DEFAULT;
432 }
433}
434
Mario Six10d14492017-01-11 16:01:00 +0100435static void kwb_msg(const char *fmt, ...)
436{
437 if (verbose_mode) {
438 va_list ap;
439
440 va_start(ap, fmt);
441 vfprintf(stdout, fmt, ap);
442 va_end(ap);
443 }
444}
445
446static int openssl_err(const char *msg)
447{
448 unsigned long ssl_err = ERR_get_error();
449
450 fprintf(stderr, "%s", msg);
451 fprintf(stderr, ": %s\n",
452 ERR_error_string(ssl_err, 0));
453
454 return -1;
455}
456
457static int kwb_load_rsa_key(const char *keydir, const char *name, RSA **p_rsa)
458{
459 char path[PATH_MAX];
460 RSA *rsa;
461 FILE *f;
462
463 if (!keydir)
464 keydir = ".";
465
466 snprintf(path, sizeof(path), "%s/%s.key", keydir, name);
467 f = fopen(path, "r");
468 if (!f) {
469 fprintf(stderr, "Couldn't open RSA private key: '%s': %s\n",
470 path, strerror(errno));
471 return -ENOENT;
472 }
473
474 rsa = PEM_read_RSAPrivateKey(f, 0, NULL, "");
475 if (!rsa) {
476 openssl_err("Failure reading private key");
477 fclose(f);
478 return -EPROTO;
479 }
480 fclose(f);
481 *p_rsa = rsa;
482
483 return 0;
484}
485
486static int kwb_load_cfg_key(struct image_tool_params *params,
487 unsigned int cfg_option, const char *key_name,
488 RSA **p_key)
489{
490 struct image_cfg_element *e_key;
491 RSA *key;
492 int res;
493
494 *p_key = NULL;
495
496 e_key = image_find_option(cfg_option);
497 if (!e_key) {
498 fprintf(stderr, "%s not configured\n", key_name);
499 return -ENOENT;
500 }
501
502 res = kwb_load_rsa_key(params->keydir, e_key->key_name, &key);
503 if (res < 0) {
504 fprintf(stderr, "Failed to load %s\n", key_name);
505 return -ENOENT;
506 }
507
508 *p_key = key;
509
510 return 0;
511}
512
513static int kwb_load_kak(struct image_tool_params *params, RSA **p_kak)
514{
515 return kwb_load_cfg_key(params, IMAGE_CFG_KAK, "KAK", p_kak);
516}
517
518static int kwb_load_csk(struct image_tool_params *params, RSA **p_csk)
519{
520 return kwb_load_cfg_key(params, IMAGE_CFG_CSK, "CSK", p_csk);
521}
522
523static int kwb_compute_pubkey_hash(struct pubkey_der_v1 *pk,
524 struct hash_v1 *hash)
525{
526 EVP_MD_CTX *ctx;
527 unsigned int key_size;
528 unsigned int hash_size;
529 int ret = 0;
530
531 if (!pk || !hash || pk->key[0] != 0x30 || pk->key[1] != 0x82)
532 return -EINVAL;
533
534 key_size = (pk->key[2] << 8) + pk->key[3] + 4;
535
536 ctx = EVP_MD_CTX_create();
537 if (!ctx)
538 return openssl_err("EVP context creation failed");
539
540 EVP_MD_CTX_init(ctx);
541 if (!EVP_DigestInit(ctx, EVP_sha256())) {
542 ret = openssl_err("Digest setup failed");
543 goto hash_err_ctx;
544 }
545
546 if (!EVP_DigestUpdate(ctx, pk->key, key_size)) {
547 ret = openssl_err("Hashing data failed");
548 goto hash_err_ctx;
549 }
550
551 if (!EVP_DigestFinal(ctx, hash->hash, &hash_size)) {
552 ret = openssl_err("Could not obtain hash");
553 goto hash_err_ctx;
554 }
555
556 EVP_MD_CTX_cleanup(ctx);
557
558hash_err_ctx:
559 EVP_MD_CTX_destroy(ctx);
560 return ret;
561}
562
563static int kwb_import_pubkey(RSA **key, struct pubkey_der_v1 *src, char *keyname)
564{
565 RSA *rsa;
566 const unsigned char *ptr;
567
568 if (!key || !src)
569 goto fail;
570
571 ptr = src->key;
572 rsa = d2i_RSAPublicKey(key, &ptr, sizeof(src->key));
573 if (!rsa) {
574 openssl_err("error decoding public key");
575 goto fail;
576 }
577
578 return 0;
579fail:
580 fprintf(stderr, "Failed to decode %s pubkey\n", keyname);
581 return -EINVAL;
582}
583
584static int kwb_export_pubkey(RSA *key, struct pubkey_der_v1 *dst, FILE *hashf,
585 char *keyname)
586{
587 int size_exp, size_mod, size_seq;
Jelle van der Waae0e55592017-05-08 21:31:20 +0200588 const BIGNUM *key_e, *key_n;
Mario Six10d14492017-01-11 16:01:00 +0100589 uint8_t *cur;
590 char *errmsg = "Failed to encode %s\n";
591
Jelle van der Waae0e55592017-05-08 21:31:20 +0200592 RSA_get0_key(key, NULL, &key_e, NULL);
593 RSA_get0_key(key, &key_n, NULL, NULL);
594
595 if (!key || !key_e || !key_n || !dst) {
Mario Six10d14492017-01-11 16:01:00 +0100596 fprintf(stderr, "export pk failed: (%p, %p, %p, %p)",
Jelle van der Waae0e55592017-05-08 21:31:20 +0200597 key, key_e, key_n, dst);
Mario Six10d14492017-01-11 16:01:00 +0100598 fprintf(stderr, errmsg, keyname);
599 return -EINVAL;
600 }
601
602 /*
603 * According to the specs, the key should be PKCS#1 DER encoded.
604 * But unfortunately the really required encoding seems to be different;
605 * it violates DER...! (But it still conformes to BER.)
606 * (Length always in long form w/ 2 byte length code; no leading zero
607 * when MSB of first byte is set...)
608 * So we cannot use the encoding func provided by OpenSSL and have to
609 * do the encoding manually.
610 */
611
Jelle van der Waae0e55592017-05-08 21:31:20 +0200612 size_exp = BN_num_bytes(key_e);
613 size_mod = BN_num_bytes(key_n);
Mario Six10d14492017-01-11 16:01:00 +0100614 size_seq = 4 + size_mod + 4 + size_exp;
615
616 if (size_mod > 256) {
617 fprintf(stderr, "export pk failed: wrong mod size: %d\n",
618 size_mod);
619 fprintf(stderr, errmsg, keyname);
620 return -EINVAL;
621 }
622
623 if (4 + size_seq > sizeof(dst->key)) {
Marek Behúnf7b7f7c2021-09-24 23:06:38 +0200624 fprintf(stderr, "export pk failed: seq too large (%d, %zu)\n",
Mario Six10d14492017-01-11 16:01:00 +0100625 4 + size_seq, sizeof(dst->key));
626 fprintf(stderr, errmsg, keyname);
627 return -ENOBUFS;
628 }
629
630 cur = dst->key;
631
632 /* PKCS#1 (RFC3447) RSAPublicKey structure */
633 *cur++ = 0x30; /* SEQUENCE */
634 *cur++ = 0x82;
635 *cur++ = (size_seq >> 8) & 0xFF;
636 *cur++ = size_seq & 0xFF;
637 /* Modulus */
638 *cur++ = 0x02; /* INTEGER */
639 *cur++ = 0x82;
640 *cur++ = (size_mod >> 8) & 0xFF;
641 *cur++ = size_mod & 0xFF;
Jelle van der Waae0e55592017-05-08 21:31:20 +0200642 BN_bn2bin(key_n, cur);
Mario Six10d14492017-01-11 16:01:00 +0100643 cur += size_mod;
644 /* Exponent */
645 *cur++ = 0x02; /* INTEGER */
646 *cur++ = 0x82;
647 *cur++ = (size_exp >> 8) & 0xFF;
648 *cur++ = size_exp & 0xFF;
Jelle van der Waae0e55592017-05-08 21:31:20 +0200649 BN_bn2bin(key_e, cur);
Mario Six10d14492017-01-11 16:01:00 +0100650
651 if (hashf) {
652 struct hash_v1 pk_hash;
653 int i;
654 int ret = 0;
655
656 ret = kwb_compute_pubkey_hash(dst, &pk_hash);
657 if (ret < 0) {
658 fprintf(stderr, errmsg, keyname);
659 return ret;
660 }
661
662 fprintf(hashf, "SHA256 = ");
663 for (i = 0 ; i < sizeof(pk_hash.hash); ++i)
664 fprintf(hashf, "%02X", pk_hash.hash[i]);
665 fprintf(hashf, "\n");
666 }
667
668 return 0;
669}
670
Pali Rohár31376142022-01-12 18:20:35 +0100671static int kwb_sign(RSA *key, void *data, int datasz, struct sig_v1 *sig,
672 char *signame)
Mario Six10d14492017-01-11 16:01:00 +0100673{
674 EVP_PKEY *evp_key;
675 EVP_MD_CTX *ctx;
676 unsigned int sig_size;
677 int size;
678 int ret = 0;
679
680 evp_key = EVP_PKEY_new();
681 if (!evp_key)
682 return openssl_err("EVP_PKEY object creation failed");
683
684 if (!EVP_PKEY_set1_RSA(evp_key, key)) {
685 ret = openssl_err("EVP key setup failed");
686 goto err_key;
687 }
688
689 size = EVP_PKEY_size(evp_key);
690 if (size > sizeof(sig->sig)) {
691 fprintf(stderr, "Buffer to small for signature (%d bytes)\n",
692 size);
693 ret = -ENOBUFS;
694 goto err_key;
695 }
696
697 ctx = EVP_MD_CTX_create();
698 if (!ctx) {
699 ret = openssl_err("EVP context creation failed");
700 goto err_key;
701 }
702 EVP_MD_CTX_init(ctx);
703 if (!EVP_SignInit(ctx, EVP_sha256())) {
704 ret = openssl_err("Signer setup failed");
705 goto err_ctx;
706 }
707
708 if (!EVP_SignUpdate(ctx, data, datasz)) {
709 ret = openssl_err("Signing data failed");
710 goto err_ctx;
711 }
712
713 if (!EVP_SignFinal(ctx, sig->sig, &sig_size, evp_key)) {
714 ret = openssl_err("Could not obtain signature");
715 goto err_ctx;
716 }
717
718 EVP_MD_CTX_cleanup(ctx);
719 EVP_MD_CTX_destroy(ctx);
720 EVP_PKEY_free(evp_key);
721
722 return 0;
723
724err_ctx:
725 EVP_MD_CTX_destroy(ctx);
726err_key:
727 EVP_PKEY_free(evp_key);
728 fprintf(stderr, "Failed to create %s signature\n", signame);
729 return ret;
730}
731
Pali Rohár31376142022-01-12 18:20:35 +0100732static int kwb_verify(RSA *key, void *data, int datasz, struct sig_v1 *sig,
733 char *signame)
Mario Six10d14492017-01-11 16:01:00 +0100734{
735 EVP_PKEY *evp_key;
736 EVP_MD_CTX *ctx;
737 int size;
738 int ret = 0;
739
740 evp_key = EVP_PKEY_new();
741 if (!evp_key)
742 return openssl_err("EVP_PKEY object creation failed");
743
744 if (!EVP_PKEY_set1_RSA(evp_key, key)) {
745 ret = openssl_err("EVP key setup failed");
746 goto err_key;
747 }
748
749 size = EVP_PKEY_size(evp_key);
750 if (size > sizeof(sig->sig)) {
751 fprintf(stderr, "Invalid signature size (%d bytes)\n",
752 size);
753 ret = -EINVAL;
754 goto err_key;
755 }
756
757 ctx = EVP_MD_CTX_create();
758 if (!ctx) {
759 ret = openssl_err("EVP context creation failed");
760 goto err_key;
761 }
762 EVP_MD_CTX_init(ctx);
763 if (!EVP_VerifyInit(ctx, EVP_sha256())) {
764 ret = openssl_err("Verifier setup failed");
765 goto err_ctx;
766 }
767
768 if (!EVP_VerifyUpdate(ctx, data, datasz)) {
769 ret = openssl_err("Hashing data failed");
770 goto err_ctx;
771 }
772
Young Xiaoda575f52019-04-17 17:20:24 +0800773 if (EVP_VerifyFinal(ctx, sig->sig, sizeof(sig->sig), evp_key) != 1) {
Mario Six10d14492017-01-11 16:01:00 +0100774 ret = openssl_err("Could not verify signature");
775 goto err_ctx;
776 }
777
778 EVP_MD_CTX_cleanup(ctx);
779 EVP_MD_CTX_destroy(ctx);
780 EVP_PKEY_free(evp_key);
781
782 return 0;
783
784err_ctx:
785 EVP_MD_CTX_destroy(ctx);
786err_key:
787 EVP_PKEY_free(evp_key);
788 fprintf(stderr, "Failed to verify %s signature\n", signame);
789 return ret;
790}
791
Pali Rohár31376142022-01-12 18:20:35 +0100792static int kwb_sign_and_verify(RSA *key, void *data, int datasz,
793 struct sig_v1 *sig, char *signame)
Mario Six10d14492017-01-11 16:01:00 +0100794{
795 if (kwb_sign(key, data, datasz, sig, signame) < 0)
796 return -1;
797
798 if (kwb_verify(key, data, datasz, sig, signame) < 0)
799 return -1;
800
801 return 0;
802}
803
804
Pali Rohár31376142022-01-12 18:20:35 +0100805static int kwb_dump_fuse_cmds_38x(FILE *out, struct secure_hdr_v1 *sec_hdr)
Mario Six10d14492017-01-11 16:01:00 +0100806{
807 struct hash_v1 kak_pub_hash;
808 struct image_cfg_element *e;
809 unsigned int fuse_line;
810 int i, idx;
811 uint8_t *ptr;
812 uint32_t val;
813 int ret = 0;
814
815 if (!out || !sec_hdr)
816 return -EINVAL;
817
818 ret = kwb_compute_pubkey_hash(&sec_hdr->kak, &kak_pub_hash);
819 if (ret < 0)
820 goto done;
821
822 fprintf(out, "# burn KAK pub key hash\n");
823 ptr = kak_pub_hash.hash;
824 for (fuse_line = 26; fuse_line <= 30; ++fuse_line) {
825 fprintf(out, "fuse prog -y %u 0 ", fuse_line);
826
827 for (i = 4; i-- > 0;)
828 fprintf(out, "%02hx", (ushort)ptr[i]);
829 ptr += 4;
830 fprintf(out, " 00");
831
832 if (fuse_line < 30) {
833 for (i = 3; i-- > 0;)
834 fprintf(out, "%02hx", (ushort)ptr[i]);
835 ptr += 3;
836 } else {
837 fprintf(out, "000000");
838 }
839
840 fprintf(out, " 1\n");
841 }
842
843 fprintf(out, "# burn CSK selection\n");
844
845 idx = image_get_csk_index();
846 if (idx < 0 || idx > 15) {
847 ret = -EINVAL;
848 goto done;
849 }
850 if (idx > 0) {
851 for (fuse_line = 31; fuse_line < 31 + idx; ++fuse_line)
852 fprintf(out, "fuse prog -y %u 0 00000001 00000000 1\n",
853 fuse_line);
854 } else {
855 fprintf(out, "# CSK index is 0; no mods needed\n");
856 }
857
858 e = image_find_option(IMAGE_CFG_BOX_ID);
859 if (e) {
860 fprintf(out, "# set box ID\n");
861 fprintf(out, "fuse prog -y 48 0 %08x 00000000 1\n", e->boxid);
862 }
863
864 e = image_find_option(IMAGE_CFG_FLASH_ID);
865 if (e) {
866 fprintf(out, "# set flash ID\n");
867 fprintf(out, "fuse prog -y 47 0 %08x 00000000 1\n", e->flashid);
868 }
869
870 fprintf(out, "# enable secure mode ");
871 fprintf(out, "(must be the last fuse line written)\n");
872
873 val = 1;
874 e = image_find_option(IMAGE_CFG_SEC_BOOT_DEV);
875 if (!e) {
876 fprintf(stderr, "ERROR: secured mode boot device not given\n");
877 ret = -EINVAL;
878 goto done;
879 }
880
881 if (e->sec_boot_dev > 0xff) {
882 fprintf(stderr, "ERROR: secured mode boot device invalid\n");
883 ret = -EINVAL;
884 goto done;
885 }
886
887 val |= (e->sec_boot_dev << 8);
888
889 fprintf(out, "fuse prog -y 24 0 %08x 0103e0a9 1\n", val);
890
891 fprintf(out, "# lock (unused) fuse lines (0-23)s\n");
892 for (fuse_line = 0; fuse_line < 24; ++fuse_line)
893 fprintf(out, "fuse prog -y %u 2 1\n", fuse_line);
894
895 fprintf(out, "# OK, that's all :-)\n");
896
897done:
898 return ret;
899}
900
901static int kwb_dump_fuse_cmds(struct secure_hdr_v1 *sec_hdr)
902{
903 int ret = 0;
904 struct image_cfg_element *e;
905
906 e = image_find_option(IMAGE_CFG_SEC_FUSE_DUMP);
907 if (!e)
908 return 0;
909
910 if (!strcmp(e->name, "a38x")) {
911 FILE *out = fopen("kwb_fuses_a38x.txt", "w+");
912
Heinrich Schuchardt379ec092021-08-17 07:03:20 +0200913 if (!out) {
914 fprintf(stderr, "Couldn't open eFuse settings: '%s': %s\n",
915 "kwb_fuses_a38x.txt", strerror(errno));
916 return -ENOENT;
917 }
918
Mario Six10d14492017-01-11 16:01:00 +0100919 kwb_dump_fuse_cmds_38x(out, sec_hdr);
920 fclose(out);
921 goto done;
922 }
923
924 ret = -ENOSYS;
925
926done:
927 return ret;
928}
929
Pali Rohár14c05962021-11-08 18:12:48 +0100930static size_t image_headersz_align(size_t headersz, uint8_t blockid)
931{
932 /*
933 * Header needs to be 4-byte aligned, which is already ensured by code
934 * above. Moreover UART images must have header aligned to 128 bytes
935 * (xmodem block size), NAND images to 256 bytes (ECC calculation),
936 * and SATA and SDIO images to 512 bytes (storage block size).
937 * Note that SPI images do not have to have header size aligned
938 * to 256 bytes because it is possible to read from SPI storage from
939 * any offset (read offset does not have to be aligned to block size).
940 */
941 if (blockid == IBR_HDR_UART_ID)
942 return ALIGN(headersz, 128);
943 else if (blockid == IBR_HDR_NAND_ID)
944 return ALIGN(headersz, 256);
945 else if (blockid == IBR_HDR_SATA_ID || blockid == IBR_HDR_SDIO_ID)
946 return ALIGN(headersz, 512);
947 else
948 return headersz;
949}
950
Pali Rohárecb3ca72021-11-08 18:12:50 +0100951static size_t image_headersz_v0(int *hasext)
952{
953 size_t headersz;
954
955 headersz = sizeof(struct main_hdr_v0);
956 if (image_count_options(IMAGE_CFG_DATA) > 0) {
957 headersz += sizeof(struct ext_hdr_v0);
958 if (hasext)
959 *hasext = 1;
960 }
961
962 return image_headersz_align(headersz, image_get_bootfrom());
963}
964
Stefan Roese3b8b19d2014-10-22 12:13:23 +0200965static void *image_create_v0(size_t *imagesz, struct image_tool_params *params,
966 int payloadsz)
Prafulla Wadaskar07329412009-09-07 15:05:02 +0530967{
Stefan Roese3b8b19d2014-10-22 12:13:23 +0200968 struct image_cfg_element *e;
969 size_t headersz;
970 struct main_hdr_v0 *main_hdr;
Mario Six7497cd62017-01-11 16:00:55 +0100971 uint8_t *image;
Stefan Roese3b8b19d2014-10-22 12:13:23 +0200972 int has_ext = 0;
973
974 /*
975 * Calculate the size of the header and the size of the
976 * payload
977 */
Pali Rohárecb3ca72021-11-08 18:12:50 +0100978 headersz = image_headersz_v0(&has_ext);
Stefan Roese3b8b19d2014-10-22 12:13:23 +0200979
Stefan Roese3b8b19d2014-10-22 12:13:23 +0200980 image = malloc(headersz);
981 if (!image) {
982 fprintf(stderr, "Cannot allocate memory for image\n");
983 return NULL;
984 }
985
986 memset(image, 0, headersz);
987
Mario Six7497cd62017-01-11 16:00:55 +0100988 main_hdr = (struct main_hdr_v0 *)image;
Prafulla Wadaskar07329412009-09-07 15:05:02 +0530989
Stefan Roese3b8b19d2014-10-22 12:13:23 +0200990 /* Fill in the main header */
Reinhard Pfau3efeaae2015-11-29 15:48:25 +0100991 main_hdr->blocksize =
Pali Rohárdce60c52021-11-08 18:12:47 +0100992 cpu_to_le32(payloadsz);
Reinhard Pfau3efeaae2015-11-29 15:48:25 +0100993 main_hdr->srcaddr = cpu_to_le32(headersz);
Stefan Roese3b8b19d2014-10-22 12:13:23 +0200994 main_hdr->ext = has_ext;
Pali Rohárc990f942021-11-08 18:12:42 +0100995 main_hdr->version = 0;
Reinhard Pfau3efeaae2015-11-29 15:48:25 +0100996 main_hdr->destaddr = cpu_to_le32(params->addr);
997 main_hdr->execaddr = cpu_to_le32(params->ep);
Pali Rohár1e2d7fb2021-11-08 18:12:43 +0100998 main_hdr->blockid = image_get_bootfrom();
Prafulla Wadaskar07329412009-09-07 15:05:02 +0530999
Stefan Roese3b8b19d2014-10-22 12:13:23 +02001000 e = image_find_option(IMAGE_CFG_NAND_ECC_MODE);
1001 if (e)
1002 main_hdr->nandeccmode = e->nandeccmode;
Pali Rohár94671fd2022-02-17 10:43:38 +01001003 e = image_find_option(IMAGE_CFG_NAND_BLKSZ);
1004 if (e)
1005 main_hdr->nandblocksize = e->nandblksz / (64 * 1024);
Stefan Roese3b8b19d2014-10-22 12:13:23 +02001006 e = image_find_option(IMAGE_CFG_NAND_PAGESZ);
1007 if (e)
Reinhard Pfau3efeaae2015-11-29 15:48:25 +01001008 main_hdr->nandpagesize = cpu_to_le16(e->nandpagesz);
Pali Rohár94671fd2022-02-17 10:43:38 +01001009 e = image_find_option(IMAGE_CFG_NAND_BADBLK_LOCATION);
1010 if (e)
1011 main_hdr->nandbadblklocation = e->nandbadblklocation;
Stefan Roese3b8b19d2014-10-22 12:13:23 +02001012 main_hdr->checksum = image_checksum8(image,
1013 sizeof(struct main_hdr_v0));
Prafulla Wadaskar07329412009-09-07 15:05:02 +05301014
Pali Rohárf8f134d2021-11-08 18:12:51 +01001015 /*
1016 * For SATA srcaddr is specified in number of sectors starting from
1017 * sector 0. The main header is stored at sector number 1.
1018 * This expects the sector size to be 512 bytes.
1019 * Header size is already aligned.
1020 */
1021 if (main_hdr->blockid == IBR_HDR_SATA_ID)
1022 main_hdr->srcaddr = cpu_to_le32(headersz / 512 + 1);
1023
Pali Rohárf8f134d2021-11-08 18:12:51 +01001024 /* For PCIe srcaddr is not used and must be set to 0xFFFFFFFF. */
1025 if (main_hdr->blockid == IBR_HDR_PEX_ID)
1026 main_hdr->srcaddr = cpu_to_le32(0xFFFFFFFF);
1027
Stefan Roese3b8b19d2014-10-22 12:13:23 +02001028 /* Generate the ext header */
1029 if (has_ext) {
Mario Six6f273632017-01-11 16:00:56 +01001030 struct ext_hdr_v0 *ext_hdr;
Stefan Roese3b8b19d2014-10-22 12:13:23 +02001031 int cfgi, datai;
Prafulla Wadaskar07329412009-09-07 15:05:02 +05301032
Mario Six7497cd62017-01-11 16:00:55 +01001033 ext_hdr = (struct ext_hdr_v0 *)
1034 (image + sizeof(struct main_hdr_v0));
Reinhard Pfau3efeaae2015-11-29 15:48:25 +01001035 ext_hdr->offset = cpu_to_le32(0x40);
Prafulla Wadaskar07329412009-09-07 15:05:02 +05301036
Stefan Roese3b8b19d2014-10-22 12:13:23 +02001037 for (cfgi = 0, datai = 0; cfgi < cfgn; cfgi++) {
1038 e = &image_cfg[cfgi];
1039 if (e->type != IMAGE_CFG_DATA)
1040 continue;
Prafulla Wadaskar07329412009-09-07 15:05:02 +05301041
Reinhard Pfau3efeaae2015-11-29 15:48:25 +01001042 ext_hdr->rcfg[datai].raddr =
1043 cpu_to_le32(e->regdata.raddr);
1044 ext_hdr->rcfg[datai].rdata =
1045 cpu_to_le32(e->regdata.rdata);
Stefan Roese3b8b19d2014-10-22 12:13:23 +02001046 datai++;
1047 }
1048
1049 ext_hdr->checksum = image_checksum8(ext_hdr,
1050 sizeof(struct ext_hdr_v0));
Prafulla Wadaskar07329412009-09-07 15:05:02 +05301051 }
Prafulla Wadaskar07329412009-09-07 15:05:02 +05301052
Stefan Roese3b8b19d2014-10-22 12:13:23 +02001053 *imagesz = headersz;
1054 return image;
Prafulla Wadaskar07329412009-09-07 15:05:02 +05301055}
1056
Mario Six855cf9e2017-01-11 16:00:57 +01001057static size_t image_headersz_v1(int *hasext)
Stefan Roese3b8b19d2014-10-22 12:13:23 +02001058{
Pali Rohárfc719962022-01-12 18:20:41 +01001059 struct image_cfg_element *e;
Pali Rohárfbe10ac2021-07-23 11:14:11 +02001060 unsigned int count;
Stefan Roese3b8b19d2014-10-22 12:13:23 +02001061 size_t headersz;
Pali Rohárfc719962022-01-12 18:20:41 +01001062 int cpu_sheeva;
1063 struct stat s;
Pali Roháre0a6dc72021-07-23 11:14:09 +02001064 int cfgi;
Pali Rohárfc719962022-01-12 18:20:41 +01001065 int ret;
Prafulla Wadaskar07329412009-09-07 15:05:02 +05301066
Stefan Roese3b8b19d2014-10-22 12:13:23 +02001067 /*
1068 * Calculate the size of the header and the size of the
1069 * payload
1070 */
1071 headersz = sizeof(struct main_hdr_v1);
Prafulla Wadaskar07329412009-09-07 15:05:02 +05301072
Pali Rohár46ebc0e2021-10-21 16:46:07 +02001073 if (image_get_csk_index() >= 0) {
1074 headersz += sizeof(struct secure_hdr_v1);
1075 if (hasext)
1076 *hasext = 1;
1077 }
1078
Pali Rohárfc719962022-01-12 18:20:41 +01001079 cpu_sheeva = image_is_cpu_sheeva();
1080
Pali Rohárd8683252022-01-12 18:20:37 +01001081 count = 0;
1082 for (cfgi = 0; cfgi < cfgn; cfgi++) {
1083 e = &image_cfg[cfgi];
1084
1085 if (e->type == IMAGE_CFG_DATA)
1086 count++;
1087
Pali Rohárfeae5e02022-01-12 18:20:38 +01001088 if (e->type == IMAGE_CFG_DATA_DELAY ||
1089 (e->type == IMAGE_CFG_BINARY && count > 0)) {
Pali Rohárd8683252022-01-12 18:20:37 +01001090 headersz += sizeof(struct register_set_hdr_v1) + 8 * count + 4;
1091 count = 0;
1092 }
Pali Rohárfbe10ac2021-07-23 11:14:11 +02001093
Pali Rohárfc719962022-01-12 18:20:41 +01001094 if (e->type != IMAGE_CFG_BINARY)
Pali Roháre0a6dc72021-07-23 11:14:09 +02001095 continue;
1096
Pali Rohárfc719962022-01-12 18:20:41 +01001097 ret = stat(e->binary.file, &s);
Stefan Roese3b8b19d2014-10-22 12:13:23 +02001098 if (ret < 0) {
Andreas Bießmann7abec5b2014-10-24 23:39:11 +02001099 char cwd[PATH_MAX];
1100 char *dir = cwd;
1101
1102 memset(cwd, 0, sizeof(cwd));
1103 if (!getcwd(cwd, sizeof(cwd))) {
1104 dir = "current working directory";
1105 perror("getcwd() failed");
1106 }
1107
Stefan Roese3b8b19d2014-10-22 12:13:23 +02001108 fprintf(stderr,
1109 "Didn't find the file '%s' in '%s' which is mandatory to generate the image\n"
1110 "This file generally contains the DDR3 training code, and should be extracted from an existing bootable\n"
Pali Rohár74bf2392022-02-17 10:43:39 +01001111 "image for your board. Use 'dumpimage -T kwbimage -p 1' to extract it from an existing image.\n",
Pali Rohárfc719962022-01-12 18:20:41 +01001112 e->binary.file, dir);
Stefan Roese3b8b19d2014-10-22 12:13:23 +02001113 return 0;
1114 }
Prafulla Wadaskar07329412009-09-07 15:05:02 +05301115
Pali Rohár46ebc0e2021-10-21 16:46:07 +02001116 headersz += sizeof(struct opt_hdr_v1) + sizeof(uint32_t) +
Pali Rohárfc719962022-01-12 18:20:41 +01001117 (e->binary.nargs) * sizeof(uint32_t);
1118
1119 if (e->binary.loadaddr) {
1120 /*
1121 * BootROM loads kwbimage header (in which the
1122 * executable code is also stored) to address
1123 * 0x40004000 or 0x40000000. Thus there is
1124 * restriction for the load address of the N-th
1125 * BINARY image.
1126 */
1127 unsigned int base_addr, low_addr, high_addr;
1128
1129 base_addr = cpu_sheeva ? 0x40004000 : 0x40000000;
1130 low_addr = base_addr + headersz;
1131 high_addr = low_addr +
1132 (BINARY_MAX_ARGS - e->binary.nargs) * sizeof(uint32_t);
1133
1134 if (cpu_sheeva && e->binary.loadaddr % 16) {
1135 fprintf(stderr,
1136 "Invalid LOAD_ADDRESS 0x%08x for BINARY %s with %d args.\n"
1137 "Address for CPU SHEEVA must be 16-byte aligned.\n",
1138 e->binary.loadaddr, e->binary.file, e->binary.nargs);
1139 return 0;
1140 }
1141
1142 if (e->binary.loadaddr % 4 || e->binary.loadaddr < low_addr ||
1143 e->binary.loadaddr > high_addr) {
1144 fprintf(stderr,
1145 "Invalid LOAD_ADDRESS 0x%08x for BINARY %s with %d args.\n"
1146 "Address must be 4-byte aligned and in range 0x%08x-0x%08x.\n",
1147 e->binary.loadaddr, e->binary.file,
1148 e->binary.nargs, low_addr, high_addr);
1149 return 0;
1150 }
1151 headersz = e->binary.loadaddr - base_addr;
Pali Rohár22529eb2022-01-12 18:20:46 +01001152 } else if (cpu_sheeva) {
Pali Rohárfc719962022-01-12 18:20:41 +01001153 headersz = ALIGN(headersz, 16);
Pali Rohár22529eb2022-01-12 18:20:46 +01001154 } else {
1155 headersz = ALIGN(headersz, 4);
Pali Rohárfc719962022-01-12 18:20:41 +01001156 }
1157
Pali Rohár46ebc0e2021-10-21 16:46:07 +02001158 headersz += ALIGN(s.st_size, 4) + sizeof(uint32_t);
Stefan Roese3b8b19d2014-10-22 12:13:23 +02001159 if (hasext)
1160 *hasext = 1;
1161 }
Prafulla Wadaskar07329412009-09-07 15:05:02 +05301162
Pali Rohárfc719962022-01-12 18:20:41 +01001163 if (count > 0)
1164 headersz += sizeof(struct register_set_hdr_v1) + 8 * count + 4;
1165
Pali Rohár14c05962021-11-08 18:12:48 +01001166 return image_headersz_align(headersz, image_get_bootfrom());
Stefan Roese3b8b19d2014-10-22 12:13:23 +02001167}
Prafulla Wadaskar07329412009-09-07 15:05:02 +05301168
Pali Rohár31376142022-01-12 18:20:35 +01001169static int add_binary_header_v1(uint8_t **cur, uint8_t **next_ext,
1170 struct image_cfg_element *binarye,
1171 struct main_hdr_v1 *main_hdr)
Mario Six030ca162017-01-11 16:00:58 +01001172{
Pali Roháre0a6dc72021-07-23 11:14:09 +02001173 struct opt_hdr_v1 *hdr = (struct opt_hdr_v1 *)*cur;
Pali Rohárfc719962022-01-12 18:20:41 +01001174 uint32_t base_addr;
Pali Rohár46ebc0e2021-10-21 16:46:07 +02001175 uint32_t add_args;
1176 uint32_t offset;
Mario Six030ca162017-01-11 16:00:58 +01001177 uint32_t *args;
1178 size_t binhdrsz;
Pali Rohárfc719962022-01-12 18:20:41 +01001179 int cpu_sheeva;
Mario Six030ca162017-01-11 16:00:58 +01001180 struct stat s;
1181 int argi;
1182 FILE *bin;
1183 int ret;
1184
Mario Six030ca162017-01-11 16:00:58 +01001185 hdr->headertype = OPT_HDR_V1_BINARY_TYPE;
1186
1187 bin = fopen(binarye->binary.file, "r");
1188 if (!bin) {
1189 fprintf(stderr, "Cannot open binary file %s\n",
1190 binarye->binary.file);
1191 return -1;
1192 }
1193
Mario Sixe3edf162017-02-13 10:11:55 +01001194 if (fstat(fileno(bin), &s)) {
1195 fprintf(stderr, "Cannot stat binary file %s\n",
1196 binarye->binary.file);
1197 goto err_close;
1198 }
Mario Six030ca162017-01-11 16:00:58 +01001199
Pali Roháre0a6dc72021-07-23 11:14:09 +02001200 *cur += sizeof(struct opt_hdr_v1);
Mario Six030ca162017-01-11 16:00:58 +01001201
Pali Roháre0a6dc72021-07-23 11:14:09 +02001202 args = (uint32_t *)*cur;
Mario Six030ca162017-01-11 16:00:58 +01001203 *args = cpu_to_le32(binarye->binary.nargs);
1204 args++;
1205 for (argi = 0; argi < binarye->binary.nargs; argi++)
1206 args[argi] = cpu_to_le32(binarye->binary.args[argi]);
1207
Pali Roháre0a6dc72021-07-23 11:14:09 +02001208 *cur += (binarye->binary.nargs + 1) * sizeof(uint32_t);
Mario Six030ca162017-01-11 16:00:58 +01001209
Pali Rohár46ebc0e2021-10-21 16:46:07 +02001210 /*
Pali Rohár22529eb2022-01-12 18:20:46 +01001211 * ARM executable code inside the BIN header on platforms with Sheeva
1212 * CPU (A370 and AXP) must always be aligned with the 128-bit boundary.
Pali Rohárfc719962022-01-12 18:20:41 +01001213 * In the case when this code is not position independent (e.g. ARM
1214 * SPL), it must be placed at fixed load and execute address.
Pali Rohár46ebc0e2021-10-21 16:46:07 +02001215 * This requirement can be met by inserting dummy arguments into
1216 * BIN header, if needed.
1217 */
Pali Rohárfc719962022-01-12 18:20:41 +01001218 cpu_sheeva = image_is_cpu_sheeva();
1219 base_addr = cpu_sheeva ? 0x40004000 : 0x40000000;
Pali Rohár46ebc0e2021-10-21 16:46:07 +02001220 offset = *cur - (uint8_t *)main_hdr;
Pali Rohárfc719962022-01-12 18:20:41 +01001221 if (binarye->binary.loadaddr)
1222 add_args = (binarye->binary.loadaddr - base_addr - offset) / sizeof(uint32_t);
Pali Rohár22529eb2022-01-12 18:20:46 +01001223 else if (cpu_sheeva)
Pali Rohárfc719962022-01-12 18:20:41 +01001224 add_args = ((16 - offset % 16) % 16) / sizeof(uint32_t);
Pali Rohár22529eb2022-01-12 18:20:46 +01001225 else
1226 add_args = 0;
Pali Rohár46ebc0e2021-10-21 16:46:07 +02001227 if (add_args) {
1228 *(args - 1) = cpu_to_le32(binarye->binary.nargs + add_args);
1229 *cur += add_args * sizeof(uint32_t);
1230 }
1231
Pali Roháre0a6dc72021-07-23 11:14:09 +02001232 ret = fread(*cur, s.st_size, 1, bin);
Mario Six030ca162017-01-11 16:00:58 +01001233 if (ret != 1) {
1234 fprintf(stderr,
1235 "Could not read binary image %s\n",
1236 binarye->binary.file);
Mario Sixe3edf162017-02-13 10:11:55 +01001237 goto err_close;
Mario Six030ca162017-01-11 16:00:58 +01001238 }
1239
1240 fclose(bin);
1241
Pali Roháre0a6dc72021-07-23 11:14:09 +02001242 *cur += ALIGN(s.st_size, 4);
Mario Six030ca162017-01-11 16:00:58 +01001243
Pali Roháre0a6dc72021-07-23 11:14:09 +02001244 *((uint32_t *)*cur) = 0x00000000;
1245 **next_ext = 1;
1246 *next_ext = *cur;
Mario Six030ca162017-01-11 16:00:58 +01001247
Pali Roháre0a6dc72021-07-23 11:14:09 +02001248 *cur += sizeof(uint32_t);
Mario Six030ca162017-01-11 16:00:58 +01001249
Pali Rohár46ebc0e2021-10-21 16:46:07 +02001250 binhdrsz = sizeof(struct opt_hdr_v1) +
1251 (binarye->binary.nargs + add_args + 2) * sizeof(uint32_t) +
1252 ALIGN(s.st_size, 4);
1253 hdr->headersz_lsb = cpu_to_le16(binhdrsz & 0xFFFF);
1254 hdr->headersz_msb = (binhdrsz & 0xFFFF0000) >> 16;
1255
Mario Six030ca162017-01-11 16:00:58 +01001256 return 0;
Mario Sixe3edf162017-02-13 10:11:55 +01001257
1258err_close:
1259 fclose(bin);
1260
1261 return -1;
Mario Six030ca162017-01-11 16:00:58 +01001262}
Mario Six10d14492017-01-11 16:01:00 +01001263
Pali Rohár31376142022-01-12 18:20:35 +01001264static int export_pub_kak_hash(RSA *kak, struct secure_hdr_v1 *secure_hdr)
Mario Six10d14492017-01-11 16:01:00 +01001265{
1266 FILE *hashf;
1267 int res;
1268
1269 hashf = fopen("pub_kak_hash.txt", "w");
Heinrich Schuchardt379ec092021-08-17 07:03:20 +02001270 if (!hashf) {
1271 fprintf(stderr, "Couldn't open hash file: '%s': %s\n",
1272 "pub_kak_hash.txt", strerror(errno));
1273 return 1;
1274 }
Mario Six10d14492017-01-11 16:01:00 +01001275
1276 res = kwb_export_pubkey(kak, &secure_hdr->kak, hashf, "KAK");
1277
1278 fclose(hashf);
1279
1280 return res < 0 ? 1 : 0;
1281}
1282
Pali Rohár31376142022-01-12 18:20:35 +01001283static int kwb_sign_csk_with_kak(struct image_tool_params *params,
1284 struct secure_hdr_v1 *secure_hdr, RSA *csk)
Mario Six10d14492017-01-11 16:01:00 +01001285{
1286 RSA *kak = NULL;
1287 RSA *kak_pub = NULL;
1288 int csk_idx = image_get_csk_index();
1289 struct sig_v1 tmp_sig;
1290
Heinrich Schuchardtd8f0f1a2021-08-17 07:11:58 +02001291 if (csk_idx < 0 || csk_idx > 15) {
Mario Six10d14492017-01-11 16:01:00 +01001292 fprintf(stderr, "Invalid CSK index %d\n", csk_idx);
1293 return 1;
1294 }
1295
1296 if (kwb_load_kak(params, &kak) < 0)
1297 return 1;
1298
1299 if (export_pub_kak_hash(kak, secure_hdr))
1300 return 1;
1301
1302 if (kwb_import_pubkey(&kak_pub, &secure_hdr->kak, "KAK") < 0)
1303 return 1;
1304
1305 if (kwb_export_pubkey(csk, &secure_hdr->csk[csk_idx], NULL, "CSK") < 0)
1306 return 1;
1307
1308 if (kwb_sign_and_verify(kak, &secure_hdr->csk,
1309 sizeof(secure_hdr->csk) +
1310 sizeof(secure_hdr->csksig),
1311 &tmp_sig, "CSK") < 0)
1312 return 1;
1313
1314 if (kwb_verify(kak_pub, &secure_hdr->csk,
1315 sizeof(secure_hdr->csk) +
1316 sizeof(secure_hdr->csksig),
1317 &tmp_sig, "CSK (2)") < 0)
1318 return 1;
1319
1320 secure_hdr->csksig = tmp_sig;
1321
1322 return 0;
1323}
1324
Pali Rohár31376142022-01-12 18:20:35 +01001325static int add_secure_header_v1(struct image_tool_params *params, uint8_t *ptr,
1326 int payloadsz, size_t headersz, uint8_t *image,
1327 struct secure_hdr_v1 *secure_hdr)
Mario Six10d14492017-01-11 16:01:00 +01001328{
1329 struct image_cfg_element *e_jtagdelay;
1330 struct image_cfg_element *e_boxid;
1331 struct image_cfg_element *e_flashid;
1332 RSA *csk = NULL;
1333 unsigned char *image_ptr;
1334 size_t image_size;
1335 struct sig_v1 tmp_sig;
1336 bool specialized_img = image_get_spezialized_img();
1337
1338 kwb_msg("Create secure header content\n");
1339
1340 e_jtagdelay = image_find_option(IMAGE_CFG_JTAG_DELAY);
1341 e_boxid = image_find_option(IMAGE_CFG_BOX_ID);
1342 e_flashid = image_find_option(IMAGE_CFG_FLASH_ID);
1343
1344 if (kwb_load_csk(params, &csk) < 0)
1345 return 1;
1346
1347 secure_hdr->headertype = OPT_HDR_V1_SECURE_TYPE;
1348 secure_hdr->headersz_msb = 0;
1349 secure_hdr->headersz_lsb = cpu_to_le16(sizeof(struct secure_hdr_v1));
1350 if (e_jtagdelay)
1351 secure_hdr->jtag_delay = e_jtagdelay->jtag_delay;
1352 if (e_boxid && specialized_img)
1353 secure_hdr->boxid = cpu_to_le32(e_boxid->boxid);
1354 if (e_flashid && specialized_img)
1355 secure_hdr->flashid = cpu_to_le32(e_flashid->flashid);
1356
1357 if (kwb_sign_csk_with_kak(params, secure_hdr, csk))
1358 return 1;
1359
1360 image_ptr = ptr + headersz;
1361 image_size = payloadsz - headersz;
1362
1363 if (kwb_sign_and_verify(csk, image_ptr, image_size,
1364 &secure_hdr->imgsig, "image") < 0)
1365 return 1;
1366
1367 if (kwb_sign_and_verify(csk, image, headersz, &tmp_sig, "header") < 0)
1368 return 1;
1369
1370 secure_hdr->hdrsig = tmp_sig;
1371
1372 kwb_dump_fuse_cmds(secure_hdr);
1373
1374 return 0;
1375}
Mario Six030ca162017-01-11 16:00:58 +01001376
Pali Rohára82a7d62022-01-12 18:20:36 +01001377static void finish_register_set_header_v1(uint8_t **cur, uint8_t **next_ext,
1378 struct register_set_hdr_v1 *register_set_hdr,
1379 int *datai, uint8_t delay)
1380{
1381 int size = sizeof(struct register_set_hdr_v1) + 8 * (*datai) + 4;
1382
1383 register_set_hdr->headertype = OPT_HDR_V1_REGISTER_TYPE;
1384 register_set_hdr->headersz_lsb = cpu_to_le16(size & 0xFFFF);
1385 register_set_hdr->headersz_msb = size >> 16;
1386 register_set_hdr->data[*datai].last_entry.delay = delay;
1387 *cur += size;
1388 **next_ext = 1;
1389 *next_ext = &register_set_hdr->data[*datai].last_entry.next;
1390 *datai = 0;
1391}
1392
Stefan Roese3b8b19d2014-10-22 12:13:23 +02001393static void *image_create_v1(size_t *imagesz, struct image_tool_params *params,
Mario Six10d14492017-01-11 16:01:00 +01001394 uint8_t *ptr, int payloadsz)
Stefan Roese3b8b19d2014-10-22 12:13:23 +02001395{
Mario Six030ca162017-01-11 16:00:58 +01001396 struct image_cfg_element *e;
Stefan Roese3b8b19d2014-10-22 12:13:23 +02001397 struct main_hdr_v1 *main_hdr;
Pali Roháre4118422021-11-08 18:12:49 +01001398 struct opt_hdr_v1 *ohdr;
Pali Rohárfbe10ac2021-07-23 11:14:11 +02001399 struct register_set_hdr_v1 *register_set_hdr;
Mario Six10d14492017-01-11 16:01:00 +01001400 struct secure_hdr_v1 *secure_hdr = NULL;
Stefan Roese3b8b19d2014-10-22 12:13:23 +02001401 size_t headersz;
Mario Six7497cd62017-01-11 16:00:55 +01001402 uint8_t *image, *cur;
Stefan Roese3b8b19d2014-10-22 12:13:23 +02001403 int hasext = 0;
Mario Six10d14492017-01-11 16:01:00 +01001404 uint8_t *next_ext = NULL;
Pali Rohára82a7d62022-01-12 18:20:36 +01001405 int cfgi, datai;
Pali Rohárfeae5e02022-01-12 18:20:38 +01001406 uint8_t delay;
Prafulla Wadaskar07329412009-09-07 15:05:02 +05301407
Stefan Roese3b8b19d2014-10-22 12:13:23 +02001408 /*
1409 * Calculate the size of the header and the size of the
1410 * payload
1411 */
Mario Six855cf9e2017-01-11 16:00:57 +01001412 headersz = image_headersz_v1(&hasext);
Stefan Roese3b8b19d2014-10-22 12:13:23 +02001413 if (headersz == 0)
1414 return NULL;
Prafulla Wadaskar07329412009-09-07 15:05:02 +05301415
Stefan Roese3b8b19d2014-10-22 12:13:23 +02001416 image = malloc(headersz);
1417 if (!image) {
1418 fprintf(stderr, "Cannot allocate memory for image\n");
1419 return NULL;
1420 }
Prafulla Wadaskar07329412009-09-07 15:05:02 +05301421
Stefan Roese3b8b19d2014-10-22 12:13:23 +02001422 memset(image, 0, headersz);
1423
Mario Six7497cd62017-01-11 16:00:55 +01001424 main_hdr = (struct main_hdr_v1 *)image;
Mario Six10d14492017-01-11 16:01:00 +01001425 cur = image;
1426 cur += sizeof(struct main_hdr_v1);
1427 next_ext = &main_hdr->ext;
Stefan Roese3b8b19d2014-10-22 12:13:23 +02001428
1429 /* Fill the main header */
Reinhard Pfau3efeaae2015-11-29 15:48:25 +01001430 main_hdr->blocksize =
Pali Rohárdce60c52021-11-08 18:12:47 +01001431 cpu_to_le32(payloadsz);
Reinhard Pfau3efeaae2015-11-29 15:48:25 +01001432 main_hdr->headersz_lsb = cpu_to_le16(headersz & 0xFFFF);
Stefan Roese3b8b19d2014-10-22 12:13:23 +02001433 main_hdr->headersz_msb = (headersz & 0xFFFF0000) >> 16;
Pali Rohárf8171a52021-07-23 11:14:06 +02001434 main_hdr->destaddr = cpu_to_le32(params->addr);
Reinhard Pfau3efeaae2015-11-29 15:48:25 +01001435 main_hdr->execaddr = cpu_to_le32(params->ep);
1436 main_hdr->srcaddr = cpu_to_le32(headersz);
Stefan Roese3b8b19d2014-10-22 12:13:23 +02001437 main_hdr->ext = hasext;
1438 main_hdr->version = 1;
Pali Rohár1e2d7fb2021-11-08 18:12:43 +01001439 main_hdr->blockid = image_get_bootfrom();
1440
Stefan Roese3b8b19d2014-10-22 12:13:23 +02001441 e = image_find_option(IMAGE_CFG_NAND_BLKSZ);
1442 if (e)
1443 main_hdr->nandblocksize = e->nandblksz / (64 * 1024);
Pali Rohárbf9a89e2021-10-22 12:37:46 +02001444 e = image_find_option(IMAGE_CFG_NAND_PAGESZ);
1445 if (e)
1446 main_hdr->nandpagesize = cpu_to_le16(e->nandpagesz);
Stefan Roese3b8b19d2014-10-22 12:13:23 +02001447 e = image_find_option(IMAGE_CFG_NAND_BADBLK_LOCATION);
1448 if (e)
1449 main_hdr->nandbadblklocation = e->nandbadblklocation;
Chris Packham883bf452016-11-09 22:07:45 +13001450 e = image_find_option(IMAGE_CFG_BAUDRATE);
1451 if (e)
Pali Rohárd8840942021-11-08 18:12:41 +01001452 main_hdr->options |= baudrate_to_option(e->baudrate);
1453 e = image_find_option(IMAGE_CFG_UART_PORT);
1454 if (e)
1455 main_hdr->options |= (e->uart_port & 3) << 3;
1456 e = image_find_option(IMAGE_CFG_UART_MPP);
1457 if (e)
1458 main_hdr->options |= (e->uart_mpp & 7) << 5;
Chris Packham1e0728a2016-11-09 22:21:45 +13001459 e = image_find_option(IMAGE_CFG_DEBUG);
1460 if (e)
1461 main_hdr->flags = e->debug ? 0x1 : 0;
Stefan Roese3b8b19d2014-10-22 12:13:23 +02001462
Pali Rohár6f6f65e2021-07-23 11:13:59 +02001463 /*
1464 * For SATA srcaddr is specified in number of sectors starting from
1465 * sector 0. The main header is stored at sector number 1.
1466 * This expects the sector size to be 512 bytes.
1467 * Header size is already aligned.
1468 */
1469 if (main_hdr->blockid == IBR_HDR_SATA_ID)
1470 main_hdr->srcaddr = cpu_to_le32(headersz / 512 + 1);
1471
Pali Rohár6f6f65e2021-07-23 11:13:59 +02001472 /* For PCIe srcaddr is not used and must be set to 0xFFFFFFFF. */
1473 if (main_hdr->blockid == IBR_HDR_PEX_ID)
1474 main_hdr->srcaddr = cpu_to_le32(0xFFFFFFFF);
1475
Mario Six10d14492017-01-11 16:01:00 +01001476 if (image_get_csk_index() >= 0) {
1477 /*
1478 * only reserve the space here; we fill the header later since
1479 * we need the header to be complete to compute the signatures
1480 */
1481 secure_hdr = (struct secure_hdr_v1 *)cur;
1482 cur += sizeof(struct secure_hdr_v1);
Pali Roháre0a6dc72021-07-23 11:14:09 +02001483 *next_ext = 1;
Mario Six10d14492017-01-11 16:01:00 +01001484 next_ext = &secure_hdr->next;
1485 }
Mario Six10d14492017-01-11 16:01:00 +01001486
Pali Rohárfbe10ac2021-07-23 11:14:11 +02001487 datai = 0;
Pali Rohárfbe10ac2021-07-23 11:14:11 +02001488 for (cfgi = 0; cfgi < cfgn; cfgi++) {
1489 e = &image_cfg[cfgi];
Pali Rohárc0cfd1a2021-07-23 11:14:12 +02001490 if (e->type != IMAGE_CFG_DATA &&
Pali Rohárfeae5e02022-01-12 18:20:38 +01001491 e->type != IMAGE_CFG_DATA_DELAY &&
1492 e->type != IMAGE_CFG_BINARY)
Pali Rohárfbe10ac2021-07-23 11:14:11 +02001493 continue;
Pali Rohárfeae5e02022-01-12 18:20:38 +01001494
Pali Rohárd8683252022-01-12 18:20:37 +01001495 if (datai == 0)
1496 register_set_hdr = (struct register_set_hdr_v1 *)cur;
Pali Rohárfeae5e02022-01-12 18:20:38 +01001497
1498 /* If delay is not specified, use the smallest possible value. */
1499 if (e->type == IMAGE_CFG_DATA_DELAY)
1500 delay = e->regdata_delay;
1501 else
1502 delay = REGISTER_SET_HDR_OPT_DELAY_MS(0);
1503
1504 /*
1505 * DATA_DELAY command is the last entry in the register set
1506 * header and BINARY command inserts new binary header.
1507 * Therefore BINARY command requires to finish register set
1508 * header if some DATA command was specified. And DATA_DELAY
1509 * command automatically finish register set header even when
1510 * there was no DATA command.
1511 */
1512 if (e->type == IMAGE_CFG_DATA_DELAY ||
1513 (e->type == IMAGE_CFG_BINARY && datai != 0))
Pali Rohára82a7d62022-01-12 18:20:36 +01001514 finish_register_set_header_v1(&cur, &next_ext, register_set_hdr,
Pali Rohárfeae5e02022-01-12 18:20:38 +01001515 &datai, delay);
1516
1517 if (e->type == IMAGE_CFG_DATA) {
1518 register_set_hdr->data[datai].entry.address =
1519 cpu_to_le32(e->regdata.raddr);
1520 register_set_hdr->data[datai].entry.value =
1521 cpu_to_le32(e->regdata.rdata);
1522 datai++;
Pali Rohárc0cfd1a2021-07-23 11:14:12 +02001523 }
Pali Rohárfeae5e02022-01-12 18:20:38 +01001524
1525 if (e->type == IMAGE_CFG_BINARY) {
1526 if (add_binary_header_v1(&cur, &next_ext, e, main_hdr))
1527 return NULL;
1528 }
Pali Rohárfbe10ac2021-07-23 11:14:11 +02001529 }
1530 if (datai != 0) {
Pali Rohára82a7d62022-01-12 18:20:36 +01001531 /* Set delay to the smallest possible value. */
Pali Rohárfeae5e02022-01-12 18:20:38 +01001532 delay = REGISTER_SET_HDR_OPT_DELAY_MS(0);
Pali Rohára82a7d62022-01-12 18:20:36 +01001533 finish_register_set_header_v1(&cur, &next_ext, register_set_hdr,
Pali Rohárfeae5e02022-01-12 18:20:38 +01001534 &datai, delay);
Pali Roháre0a6dc72021-07-23 11:14:09 +02001535 }
Mario Six10d14492017-01-11 16:01:00 +01001536
Pali Rohárdce60c52021-11-08 18:12:47 +01001537 if (secure_hdr && add_secure_header_v1(params, ptr, payloadsz + headersz,
Mario Six10d14492017-01-11 16:01:00 +01001538 headersz, image, secure_hdr))
1539 return NULL;
Stefan Roese3b8b19d2014-10-22 12:13:23 +02001540
Stefan Roese3b8b19d2014-10-22 12:13:23 +02001541 *imagesz = headersz;
Pali Roháre4118422021-11-08 18:12:49 +01001542
1543 /* Fill the real header size without padding into the main header */
1544 headersz = sizeof(*main_hdr);
1545 for_each_opt_hdr_v1 (ohdr, main_hdr)
1546 headersz += opt_hdr_v1_size(ohdr);
1547 main_hdr->headersz_lsb = cpu_to_le16(headersz & 0xFFFF);
1548 main_hdr->headersz_msb = (headersz & 0xFFFF0000) >> 16;
1549
Pierre Bourdon3fb703d2021-12-25 20:50:19 +01001550 /* Calculate and set the header checksum */
1551 main_hdr->checksum = image_checksum8(main_hdr, headersz);
1552
Stefan Roese3b8b19d2014-10-22 12:13:23 +02001553 return image;
1554}
1555
Pali Rohár31376142022-01-12 18:20:35 +01001556static int recognize_keyword(char *keyword)
Mario Six62da6762017-01-11 16:00:59 +01001557{
1558 int kw_id;
1559
1560 for (kw_id = 1; kw_id < IMAGE_CFG_COUNT; ++kw_id)
1561 if (!strcmp(keyword, id_strs[kw_id]))
1562 return kw_id;
1563
1564 return 0;
1565}
1566
Stefan Roese3b8b19d2014-10-22 12:13:23 +02001567static int image_create_config_parse_oneline(char *line,
1568 struct image_cfg_element *el)
1569{
Mario Six62da6762017-01-11 16:00:59 +01001570 char *keyword, *saveptr, *value1, *value2;
1571 char delimiters[] = " \t";
1572 int keyword_id, ret, argi;
1573 char *unknown_msg = "Ignoring unknown line '%s'\n";
1574
1575 keyword = strtok_r(line, delimiters, &saveptr);
1576 keyword_id = recognize_keyword(keyword);
1577
1578 if (!keyword_id) {
1579 fprintf(stderr, unknown_msg, line);
1580 return 0;
1581 }
Stefan Roese3b8b19d2014-10-22 12:13:23 +02001582
Mario Six62da6762017-01-11 16:00:59 +01001583 el->type = keyword_id;
Mario Sixd6009d72017-01-11 16:00:54 +01001584
Mario Six62da6762017-01-11 16:00:59 +01001585 value1 = strtok_r(NULL, delimiters, &saveptr);
1586
1587 if (!value1) {
1588 fprintf(stderr, "Parameter missing in line '%s'\n", line);
1589 return -1;
1590 }
1591
1592 switch (keyword_id) {
1593 case IMAGE_CFG_VERSION:
1594 el->version = atoi(value1);
1595 break;
Pali Rohár6b76c1c2022-01-12 18:20:40 +01001596 case IMAGE_CFG_CPU:
1597 if (strcmp(value1, "FEROCEON") == 0)
1598 el->cpu_sheeva = 0;
1599 else if (strcmp(value1, "SHEEVA") == 0)
1600 el->cpu_sheeva = 1;
1601 else if (strcmp(value1, "A9") == 0)
1602 el->cpu_sheeva = 0;
1603 else {
1604 fprintf(stderr, "Invalid CPU %s\n", value1);
1605 return -1;
1606 }
1607 break;
Mario Six62da6762017-01-11 16:00:59 +01001608 case IMAGE_CFG_BOOT_FROM:
1609 ret = image_boot_mode_id(value1);
Mario Sixd6009d72017-01-11 16:00:54 +01001610
Andreas Bießmann4c40e352014-10-24 23:25:52 +02001611 if (ret < 0) {
Mario Six62da6762017-01-11 16:00:59 +01001612 fprintf(stderr, "Invalid boot media '%s'\n", value1);
Stefan Roese3b8b19d2014-10-22 12:13:23 +02001613 return -1;
1614 }
Andreas Bießmann4c40e352014-10-24 23:25:52 +02001615 el->bootfrom = ret;
Mario Six62da6762017-01-11 16:00:59 +01001616 break;
1617 case IMAGE_CFG_NAND_BLKSZ:
1618 el->nandblksz = strtoul(value1, NULL, 16);
1619 break;
1620 case IMAGE_CFG_NAND_BADBLK_LOCATION:
1621 el->nandbadblklocation = strtoul(value1, NULL, 16);
1622 break;
1623 case IMAGE_CFG_NAND_ECC_MODE:
1624 ret = image_nand_ecc_mode_id(value1);
Mario Sixd6009d72017-01-11 16:00:54 +01001625
Andreas Bießmann4c40e352014-10-24 23:25:52 +02001626 if (ret < 0) {
Mario Six62da6762017-01-11 16:00:59 +01001627 fprintf(stderr, "Invalid NAND ECC mode '%s'\n", value1);
Stefan Roese3b8b19d2014-10-22 12:13:23 +02001628 return -1;
1629 }
Andreas Bießmann4c40e352014-10-24 23:25:52 +02001630 el->nandeccmode = ret;
Mario Six62da6762017-01-11 16:00:59 +01001631 break;
1632 case IMAGE_CFG_NAND_PAGESZ:
1633 el->nandpagesz = strtoul(value1, NULL, 16);
1634 break;
1635 case IMAGE_CFG_BINARY:
1636 argi = 0;
Mario Sixd6009d72017-01-11 16:00:54 +01001637
Mario Six62da6762017-01-11 16:00:59 +01001638 el->binary.file = strdup(value1);
Stefan Roese3b8b19d2014-10-22 12:13:23 +02001639 while (1) {
Mario Six62da6762017-01-11 16:00:59 +01001640 char *value = strtok_r(NULL, delimiters, &saveptr);
Pali Rohárfc719962022-01-12 18:20:41 +01001641 char *endptr;
Mario Six62da6762017-01-11 16:00:59 +01001642
Stefan Roese3b8b19d2014-10-22 12:13:23 +02001643 if (!value)
1644 break;
Pali Rohárfc719962022-01-12 18:20:41 +01001645
1646 if (!strcmp(value, "LOAD_ADDRESS")) {
1647 value = strtok_r(NULL, delimiters, &saveptr);
1648 if (!value) {
1649 fprintf(stderr,
1650 "Missing address argument for BINARY LOAD_ADDRESS\n");
1651 return -1;
1652 }
1653 el->binary.loadaddr = strtoul(value, &endptr, 16);
1654 if (*endptr) {
1655 fprintf(stderr,
1656 "Invalid argument '%s' for BINARY LOAD_ADDRESS\n",
1657 value);
1658 return -1;
1659 }
1660 value = strtok_r(NULL, delimiters, &saveptr);
1661 if (value) {
1662 fprintf(stderr,
1663 "Unexpected argument '%s' after BINARY LOAD_ADDRESS\n",
1664 value);
1665 return -1;
1666 }
1667 break;
1668 }
1669
1670 el->binary.args[argi] = strtoul(value, &endptr, 16);
1671 if (*endptr) {
1672 fprintf(stderr, "Invalid argument '%s' for BINARY\n", value);
1673 return -1;
1674 }
Stefan Roese3b8b19d2014-10-22 12:13:23 +02001675 argi++;
1676 if (argi >= BINARY_MAX_ARGS) {
1677 fprintf(stderr,
Mario Six62da6762017-01-11 16:00:59 +01001678 "Too many arguments for BINARY\n");
Stefan Roese3b8b19d2014-10-22 12:13:23 +02001679 return -1;
Prafulla Wadaskar07329412009-09-07 15:05:02 +05301680 }
Prafulla Wadaskar07329412009-09-07 15:05:02 +05301681 }
Stefan Roese3b8b19d2014-10-22 12:13:23 +02001682 el->binary.nargs = argi;
Mario Six62da6762017-01-11 16:00:59 +01001683 break;
1684 case IMAGE_CFG_DATA:
1685 value2 = strtok_r(NULL, delimiters, &saveptr);
Stefan Roese3b8b19d2014-10-22 12:13:23 +02001686
1687 if (!value1 || !value2) {
1688 fprintf(stderr,
1689 "Invalid number of arguments for DATA\n");
1690 return -1;
1691 }
1692
Stefan Roese3b8b19d2014-10-22 12:13:23 +02001693 el->regdata.raddr = strtoul(value1, NULL, 16);
1694 el->regdata.rdata = strtoul(value2, NULL, 16);
Mario Six62da6762017-01-11 16:00:59 +01001695 break;
Pali Rohárc0cfd1a2021-07-23 11:14:12 +02001696 case IMAGE_CFG_DATA_DELAY:
1697 if (!strcmp(value1, "SDRAM_SETUP"))
1698 el->regdata_delay = REGISTER_SET_HDR_OPT_DELAY_SDRAM_SETUP;
1699 else
1700 el->regdata_delay = REGISTER_SET_HDR_OPT_DELAY_MS(strtoul(value1, NULL, 10));
Pali Rohár80e2ee12022-01-12 18:20:48 +01001701 if (el->regdata_delay > 255) {
1702 fprintf(stderr, "Maximal DATA_DELAY is 255\n");
1703 return -1;
1704 }
Pali Rohárc0cfd1a2021-07-23 11:14:12 +02001705 break;
Mario Six62da6762017-01-11 16:00:59 +01001706 case IMAGE_CFG_BAUDRATE:
1707 el->baudrate = strtoul(value1, NULL, 10);
1708 break;
Pali Rohárd8840942021-11-08 18:12:41 +01001709 case IMAGE_CFG_UART_PORT:
1710 el->uart_port = strtoul(value1, NULL, 16);
1711 break;
1712 case IMAGE_CFG_UART_MPP:
1713 el->uart_mpp = strtoul(value1, NULL, 16);
1714 break;
Mario Six62da6762017-01-11 16:00:59 +01001715 case IMAGE_CFG_DEBUG:
1716 el->debug = strtoul(value1, NULL, 10);
1717 break;
Mario Six10d14492017-01-11 16:01:00 +01001718 case IMAGE_CFG_KAK:
1719 el->key_name = strdup(value1);
1720 break;
1721 case IMAGE_CFG_CSK:
1722 el->key_name = strdup(value1);
1723 break;
1724 case IMAGE_CFG_CSK_INDEX:
1725 el->csk_idx = strtol(value1, NULL, 0);
1726 break;
1727 case IMAGE_CFG_JTAG_DELAY:
1728 el->jtag_delay = strtoul(value1, NULL, 0);
1729 break;
1730 case IMAGE_CFG_BOX_ID:
1731 el->boxid = strtoul(value1, NULL, 0);
1732 break;
1733 case IMAGE_CFG_FLASH_ID:
1734 el->flashid = strtoul(value1, NULL, 0);
1735 break;
1736 case IMAGE_CFG_SEC_SPECIALIZED_IMG:
1737 el->sec_specialized_img = true;
1738 break;
1739 case IMAGE_CFG_SEC_COMMON_IMG:
1740 el->sec_specialized_img = false;
1741 break;
1742 case IMAGE_CFG_SEC_BOOT_DEV:
1743 el->sec_boot_dev = strtoul(value1, NULL, 0);
1744 break;
1745 case IMAGE_CFG_SEC_FUSE_DUMP:
1746 el->name = strdup(value1);
1747 break;
Mario Six62da6762017-01-11 16:00:59 +01001748 default:
1749 fprintf(stderr, unknown_msg, line);
Prafulla Wadaskar07329412009-09-07 15:05:02 +05301750 }
Prafulla Wadaskar07329412009-09-07 15:05:02 +05301751
Stefan Roese3b8b19d2014-10-22 12:13:23 +02001752 return 0;
1753}
Prafulla Wadaskar07329412009-09-07 15:05:02 +05301754
1755/*
Stefan Roese3b8b19d2014-10-22 12:13:23 +02001756 * Parse the configuration file 'fcfg' into the array of configuration
1757 * elements 'image_cfg', and return the number of configuration
1758 * elements in 'cfgn'.
Prafulla Wadaskar07329412009-09-07 15:05:02 +05301759 */
Stefan Roese3b8b19d2014-10-22 12:13:23 +02001760static int image_create_config_parse(FILE *fcfg)
1761{
1762 int ret;
1763 int cfgi = 0;
1764
1765 /* Parse the configuration file */
1766 while (!feof(fcfg)) {
1767 char *line;
1768 char buf[256];
1769
1770 /* Read the current line */
1771 memset(buf, 0, sizeof(buf));
1772 line = fgets(buf, sizeof(buf), fcfg);
1773 if (!line)
1774 break;
1775
1776 /* Ignore useless lines */
1777 if (line[0] == '\n' || line[0] == '#')
1778 continue;
1779
1780 /* Strip final newline */
1781 if (line[strlen(line) - 1] == '\n')
1782 line[strlen(line) - 1] = 0;
1783
1784 /* Parse the current line */
1785 ret = image_create_config_parse_oneline(line,
1786 &image_cfg[cfgi]);
1787 if (ret)
1788 return ret;
1789
1790 cfgi++;
1791
1792 if (cfgi >= IMAGE_CFG_ELEMENT_MAX) {
1793 fprintf(stderr,
1794 "Too many configuration elements in .cfg file\n");
1795 return -1;
1796 }
1797 }
1798
1799 cfgn = cfgi;
1800 return 0;
Prafulla Wadaskar07329412009-09-07 15:05:02 +05301801}
1802
Stefan Roese3b8b19d2014-10-22 12:13:23 +02001803static int image_get_version(void)
1804{
1805 struct image_cfg_element *e;
1806
1807 e = image_find_option(IMAGE_CFG_VERSION);
1808 if (!e)
1809 return -1;
1810
1811 return e->version;
1812}
1813
Stefan Roese3b8b19d2014-10-22 12:13:23 +02001814static void kwbimage_set_header(void *ptr, struct stat *sbuf, int ifd,
Guilherme Maciel Ferreira8ed4d1c2013-12-01 12:43:10 -07001815 struct image_tool_params *params)
Prafulla Wadaskar07329412009-09-07 15:05:02 +05301816{
Stefan Roese3b8b19d2014-10-22 12:13:23 +02001817 FILE *fcfg;
1818 void *image = NULL;
1819 int version;
Łukasz Majewskif04dab42014-11-21 09:22:43 +01001820 size_t headersz = 0;
Pali Rohárdce60c52021-11-08 18:12:47 +01001821 size_t datasz;
Prafulla Wadaskar07329412009-09-07 15:05:02 +05301822 uint32_t checksum;
Pali Rohárdce60c52021-11-08 18:12:47 +01001823 struct stat s;
Stefan Roese3b8b19d2014-10-22 12:13:23 +02001824 int ret;
Prafulla Wadaskar07329412009-09-07 15:05:02 +05301825
Pali Rohárdce60c52021-11-08 18:12:47 +01001826 /*
1827 * Do not use sbuf->st_size as it contains size with padding.
1828 * We need original image data size, so stat original file.
1829 */
1830 if (stat(params->datafile, &s)) {
1831 fprintf(stderr, "Could not stat data file %s: %s\n",
1832 params->datafile, strerror(errno));
1833 exit(EXIT_FAILURE);
1834 }
1835 datasz = ALIGN(s.st_size, 4);
1836
Stefan Roese3b8b19d2014-10-22 12:13:23 +02001837 fcfg = fopen(params->imagename, "r");
1838 if (!fcfg) {
1839 fprintf(stderr, "Could not open input file %s\n",
1840 params->imagename);
1841 exit(EXIT_FAILURE);
1842 }
1843
1844 image_cfg = malloc(IMAGE_CFG_ELEMENT_MAX *
1845 sizeof(struct image_cfg_element));
1846 if (!image_cfg) {
1847 fprintf(stderr, "Cannot allocate memory\n");
1848 fclose(fcfg);
1849 exit(EXIT_FAILURE);
1850 }
1851
1852 memset(image_cfg, 0,
1853 IMAGE_CFG_ELEMENT_MAX * sizeof(struct image_cfg_element));
1854 rewind(fcfg);
1855
1856 ret = image_create_config_parse(fcfg);
1857 fclose(fcfg);
1858 if (ret) {
1859 free(image_cfg);
1860 exit(EXIT_FAILURE);
1861 }
1862
1863 version = image_get_version();
Stefan Roese933918c2014-10-28 11:32:24 +01001864 switch (version) {
1865 /*
1866 * Fallback to version 0 if no version is provided in the
1867 * cfg file
1868 */
1869 case -1:
1870 case 0:
Pali Rohárdce60c52021-11-08 18:12:47 +01001871 image = image_create_v0(&headersz, params, datasz + 4);
Stefan Roese933918c2014-10-28 11:32:24 +01001872 break;
1873
1874 case 1:
Pali Rohárdce60c52021-11-08 18:12:47 +01001875 image = image_create_v1(&headersz, params, ptr, datasz + 4);
Stefan Roese933918c2014-10-28 11:32:24 +01001876 break;
1877
1878 default:
1879 fprintf(stderr, "Unsupported version %d\n", version);
1880 free(image_cfg);
1881 exit(EXIT_FAILURE);
1882 }
Prafulla Wadaskar07329412009-09-07 15:05:02 +05301883
Stefan Roese3b8b19d2014-10-22 12:13:23 +02001884 if (!image) {
1885 fprintf(stderr, "Could not create image\n");
1886 free(image_cfg);
1887 exit(EXIT_FAILURE);
1888 }
1889
1890 free(image_cfg);
1891
Pali Rohárdce60c52021-11-08 18:12:47 +01001892 /* Build and add image data checksum */
Pali Rohárcfb60a92021-07-23 11:13:56 +02001893 checksum = cpu_to_le32(image_checksum32((uint8_t *)ptr + headersz,
Pali Rohárdce60c52021-11-08 18:12:47 +01001894 datasz));
1895 memcpy((uint8_t *)ptr + headersz + datasz, &checksum, sizeof(uint32_t));
Prafulla Wadaskar07329412009-09-07 15:05:02 +05301896
Stefan Roese3b8b19d2014-10-22 12:13:23 +02001897 /* Finally copy the header into the image area */
1898 memcpy(ptr, image, headersz);
1899
1900 free(image);
1901}
1902
1903static void kwbimage_print_header(const void *ptr)
1904{
1905 struct main_hdr_v0 *mhdr = (struct main_hdr_v0 *)ptr;
Pali Roháre581f192022-02-17 10:43:36 +01001906 struct bin_hdr_v0 *bhdr;
Marek Behúnb1a76502021-08-18 00:59:15 +02001907 struct opt_hdr_v1 *ohdr;
Stefan Roese3b8b19d2014-10-22 12:13:23 +02001908
1909 printf("Image Type: MVEBU Boot from %s Image\n",
1910 image_boot_mode_name(mhdr->blockid));
Marek Behúnfa9caec2021-09-24 23:07:00 +02001911 printf("Image version:%d\n", kwbimage_version(ptr));
Pali Rohárcd614ad2021-07-23 11:14:04 +02001912
Marek Behúnb1a76502021-08-18 00:59:15 +02001913 for_each_opt_hdr_v1 (ohdr, mhdr) {
1914 if (ohdr->headertype == OPT_HDR_V1_BINARY_TYPE) {
Pali Rohár6e16d392022-01-12 18:20:49 +01001915 printf("BIN Img Size: ");
Marek Behúnb1a76502021-08-18 00:59:15 +02001916 genimg_print_size(opt_hdr_v1_size(ohdr) - 12 -
1917 4 * ohdr->data[0]);
Pali Rohár6e16d392022-01-12 18:20:49 +01001918 printf("BIN Img Offs: %08x\n",
1919 (unsigned)((uint8_t *)ohdr - (uint8_t *)mhdr) +
1920 8 + 4 * ohdr->data[0]);
Pali Rohárcd614ad2021-07-23 11:14:04 +02001921 }
1922 }
Marek Behúnb1a76502021-08-18 00:59:15 +02001923
Pali Roháre581f192022-02-17 10:43:36 +01001924 for_each_bin_hdr_v0(bhdr, mhdr) {
1925 printf("BIN Img Size: ");
1926 genimg_print_size(le32_to_cpu(bhdr->size));
1927 printf("BIN Img Addr: %08x\n", le32_to_cpu(bhdr->destaddr));
1928 printf("BIN Img Entr: %08x\n", le32_to_cpu(bhdr->execaddr));
1929 }
1930
Gerald Kerma8f9e5832014-10-31 01:03:27 +01001931 printf("Data Size: ");
Stefan Roese3b8b19d2014-10-22 12:13:23 +02001932 genimg_print_size(mhdr->blocksize - sizeof(uint32_t));
1933 printf("Load Address: %08x\n", mhdr->destaddr);
1934 printf("Entry Point: %08x\n", mhdr->execaddr);
1935}
Prafulla Wadaskar07329412009-09-07 15:05:02 +05301936
Stefan Roese3b8b19d2014-10-22 12:13:23 +02001937static int kwbimage_check_image_types(uint8_t type)
1938{
1939 if (type == IH_TYPE_KWBIMAGE)
1940 return EXIT_SUCCESS;
Mario Sixd6009d72017-01-11 16:00:54 +01001941
1942 return EXIT_FAILURE;
Prafulla Wadaskar07329412009-09-07 15:05:02 +05301943}
1944
Stefan Roese3b8b19d2014-10-22 12:13:23 +02001945static int kwbimage_verify_header(unsigned char *ptr, int image_size,
1946 struct image_tool_params *params)
Prafulla Wadaskar07329412009-09-07 15:05:02 +05301947{
Marek Behúnd1b0b032021-09-24 23:07:01 +02001948 size_t header_size = kwbheader_size(ptr);
Pali Rohár17414cb2021-11-08 18:12:44 +01001949 uint8_t blockid;
1950 uint32_t offset;
1951 uint32_t size;
Marek Behúnd1b0b032021-09-24 23:07:01 +02001952 uint8_t csum;
Alexander Graf22e87fc2018-03-15 11:14:19 +01001953
Pali Rohár55f9e492022-09-18 18:39:18 +02001954 if (header_size > 192*1024)
1955 return -FDT_ERR_BADSTRUCTURE;
1956
Alexander Graf22e87fc2018-03-15 11:14:19 +01001957 if (header_size > image_size)
1958 return -FDT_ERR_BADSTRUCTURE;
Prafulla Wadaskar07329412009-09-07 15:05:02 +05301959
Baruch Siach4a5b99b2017-07-04 20:23:40 +03001960 if (!main_hdr_checksum_ok(ptr))
Stefan Roese3b8b19d2014-10-22 12:13:23 +02001961 return -FDT_ERR_BADSTRUCTURE;
Prafulla Wadaskar07329412009-09-07 15:05:02 +05301962
Stefan Roese3b8b19d2014-10-22 12:13:23 +02001963 /* Only version 0 extended header has checksum */
Marek Behúnfa9caec2021-09-24 23:07:00 +02001964 if (kwbimage_version(ptr) == 0) {
Pali Rohár03345b92021-07-23 11:14:01 +02001965 struct main_hdr_v0 *mhdr = (struct main_hdr_v0 *)ptr;
Pali Roháre581f192022-02-17 10:43:36 +01001966 struct ext_hdr_v0 *ext_hdr;
1967 struct bin_hdr_v0 *bhdr;
Pali Rohára98cc272021-08-11 10:14:15 +02001968
Pali Roháre581f192022-02-17 10:43:36 +01001969 for_each_ext_hdr_v0(ext_hdr, ptr) {
Marek Behúnd1b0b032021-09-24 23:07:01 +02001970 csum = image_checksum8(ext_hdr, sizeof(*ext_hdr) - 1);
1971 if (csum != ext_hdr->checksum)
Pali Rohár03345b92021-07-23 11:14:01 +02001972 return -FDT_ERR_BADSTRUCTURE;
1973 }
Pali Rohár17414cb2021-11-08 18:12:44 +01001974
Pali Roháre581f192022-02-17 10:43:36 +01001975 for_each_bin_hdr_v0(bhdr, ptr) {
1976 csum = image_checksum8(bhdr, (uint8_t *)&bhdr->checksum - (uint8_t *)bhdr - 1);
1977 if (csum != bhdr->checksum)
1978 return -FDT_ERR_BADSTRUCTURE;
1979
1980 if (bhdr->offset > sizeof(*bhdr) || bhdr->offset % 4 != 0)
1981 return -FDT_ERR_BADSTRUCTURE;
1982
1983 if (bhdr->offset + bhdr->size + 4 > sizeof(*bhdr) || bhdr->size % 4 != 0)
1984 return -FDT_ERR_BADSTRUCTURE;
1985
1986 if (image_checksum32((uint8_t *)bhdr + bhdr->offset, bhdr->size) !=
1987 *(uint32_t *)((uint8_t *)bhdr + bhdr->offset + bhdr->size))
1988 return -FDT_ERR_BADSTRUCTURE;
1989 }
1990
Pali Rohár17414cb2021-11-08 18:12:44 +01001991 blockid = mhdr->blockid;
1992 offset = le32_to_cpu(mhdr->srcaddr);
1993 size = le32_to_cpu(mhdr->blocksize);
Marek Behúnfa9caec2021-09-24 23:07:00 +02001994 } else if (kwbimage_version(ptr) == 1) {
Pali Rohárfdb575a2021-07-23 11:14:02 +02001995 struct main_hdr_v1 *mhdr = (struct main_hdr_v1 *)ptr;
Marek Behúnb1a76502021-08-18 00:59:15 +02001996 const uint8_t *mhdr_end;
1997 struct opt_hdr_v1 *ohdr;
Pali Rohárfdb575a2021-07-23 11:14:02 +02001998
Marek Behúnb1a76502021-08-18 00:59:15 +02001999 mhdr_end = (uint8_t *)mhdr + header_size;
2000 for_each_opt_hdr_v1 (ohdr, ptr)
2001 if (!opt_hdr_v1_valid_size(ohdr, mhdr_end))
2002 return -FDT_ERR_BADSTRUCTURE;
Pali Rohár48bc68c2021-07-23 11:14:03 +02002003
Pali Rohár17414cb2021-11-08 18:12:44 +01002004 blockid = mhdr->blockid;
Pali Rohár48bc68c2021-07-23 11:14:03 +02002005 offset = le32_to_cpu(mhdr->srcaddr);
Pali Rohár17414cb2021-11-08 18:12:44 +01002006 size = le32_to_cpu(mhdr->blocksize);
2007 } else {
2008 return -FDT_ERR_BADSTRUCTURE;
2009 }
Pali Rohár48bc68c2021-07-23 11:14:03 +02002010
Pali Rohár17414cb2021-11-08 18:12:44 +01002011 /*
2012 * For SATA srcaddr is specified in number of sectors.
2013 * The main header is must be stored at sector number 1.
2014 * This expects that sector size is 512 bytes and recalculates
2015 * data offset to bytes relative to the main header.
2016 */
2017 if (blockid == IBR_HDR_SATA_ID) {
2018 if (offset < 1)
2019 return -FDT_ERR_BADSTRUCTURE;
2020 offset -= 1;
2021 offset *= 512;
2022 }
Pali Rohár48bc68c2021-07-23 11:14:03 +02002023
Pali Rohár17414cb2021-11-08 18:12:44 +01002024 /*
Pali Rohár17414cb2021-11-08 18:12:44 +01002025 * For PCIe srcaddr is always set to 0xFFFFFFFF.
2026 * This expects that data starts after all headers.
2027 */
2028 if (blockid == IBR_HDR_PEX_ID && offset == 0xFFFFFFFF)
2029 offset = header_size;
Pali Rohár48bc68c2021-07-23 11:14:03 +02002030
Pali Rohár17414cb2021-11-08 18:12:44 +01002031 if (offset > image_size || offset % 4 != 0)
2032 return -FDT_ERR_BADSTRUCTURE;
Pali Rohár48bc68c2021-07-23 11:14:03 +02002033
Pali Rohár17414cb2021-11-08 18:12:44 +01002034 if (size < 4 || offset + size > image_size || size % 4 != 0)
2035 return -FDT_ERR_BADSTRUCTURE;
Pali Rohár48bc68c2021-07-23 11:14:03 +02002036
Pali Rohár17414cb2021-11-08 18:12:44 +01002037 if (image_checksum32(ptr + offset, size - 4) !=
2038 *(uint32_t *)(ptr + offset + size - 4))
Pali Rohár010e2522021-08-11 10:14:14 +02002039 return -FDT_ERR_BADSTRUCTURE;
Pali Rohárfdb575a2021-07-23 11:14:02 +02002040
Prafulla Wadaskar07329412009-09-07 15:05:02 +05302041 return 0;
2042}
2043
Stefan Roese3b8b19d2014-10-22 12:13:23 +02002044static int kwbimage_generate(struct image_tool_params *params,
2045 struct image_type_params *tparams)
Prafulla Wadaskar07329412009-09-07 15:05:02 +05302046{
Patrick Wildtef84f822017-05-10 22:18:54 +02002047 FILE *fcfg;
Pali Rohárcfb60a92021-07-23 11:13:56 +02002048 struct stat s;
Stefan Roese3b8b19d2014-10-22 12:13:23 +02002049 int alloc_len;
Pali Rohár04785152021-07-23 11:13:57 +02002050 int bootfrom;
Patrick Wildtef84f822017-05-10 22:18:54 +02002051 int version;
Stefan Roese3b8b19d2014-10-22 12:13:23 +02002052 void *hdr;
Patrick Wildtef84f822017-05-10 22:18:54 +02002053 int ret;
Prafulla Wadaskar07329412009-09-07 15:05:02 +05302054
Patrick Wildtef84f822017-05-10 22:18:54 +02002055 fcfg = fopen(params->imagename, "r");
2056 if (!fcfg) {
2057 fprintf(stderr, "Could not open input file %s\n",
2058 params->imagename);
2059 exit(EXIT_FAILURE);
2060 }
2061
Pali Rohárcfb60a92021-07-23 11:13:56 +02002062 if (stat(params->datafile, &s)) {
2063 fprintf(stderr, "Could not stat data file %s: %s\n",
2064 params->datafile, strerror(errno));
2065 exit(EXIT_FAILURE);
2066 }
2067
Patrick Wildtef84f822017-05-10 22:18:54 +02002068 image_cfg = malloc(IMAGE_CFG_ELEMENT_MAX *
2069 sizeof(struct image_cfg_element));
2070 if (!image_cfg) {
2071 fprintf(stderr, "Cannot allocate memory\n");
2072 fclose(fcfg);
2073 exit(EXIT_FAILURE);
2074 }
2075
2076 memset(image_cfg, 0,
2077 IMAGE_CFG_ELEMENT_MAX * sizeof(struct image_cfg_element));
2078 rewind(fcfg);
2079
2080 ret = image_create_config_parse(fcfg);
2081 fclose(fcfg);
2082 if (ret) {
2083 free(image_cfg);
2084 exit(EXIT_FAILURE);
2085 }
2086
Pali Rohár04785152021-07-23 11:13:57 +02002087 bootfrom = image_get_bootfrom();
Patrick Wildtef84f822017-05-10 22:18:54 +02002088 version = image_get_version();
2089 switch (version) {
2090 /*
2091 * Fallback to version 0 if no version is provided in the
2092 * cfg file
2093 */
2094 case -1:
2095 case 0:
Pali Rohárecb3ca72021-11-08 18:12:50 +01002096 alloc_len = image_headersz_v0(NULL);
Patrick Wildtef84f822017-05-10 22:18:54 +02002097 break;
2098
2099 case 1:
Mario Six855cf9e2017-01-11 16:00:57 +01002100 alloc_len = image_headersz_v1(NULL);
Pali Rohár4bfc16e2022-01-12 18:20:42 +01002101 if (!alloc_len) {
2102 free(image_cfg);
2103 exit(EXIT_FAILURE);
2104 }
Pali Rohár08c51742022-01-12 18:20:43 +01002105 if (alloc_len > 192*1024) {
2106 fprintf(stderr, "Header is too big (%u bytes), maximal kwbimage header size is %u bytes\n", alloc_len, 192*1024);
2107 free(image_cfg);
2108 exit(EXIT_FAILURE);
2109 }
Patrick Wildtef84f822017-05-10 22:18:54 +02002110 break;
2111
2112 default:
2113 fprintf(stderr, "Unsupported version %d\n", version);
2114 free(image_cfg);
2115 exit(EXIT_FAILURE);
Stefan Roese3b8b19d2014-10-22 12:13:23 +02002116 }
2117
Patrick Wildtef84f822017-05-10 22:18:54 +02002118 free(image_cfg);
2119
Stefan Roese3b8b19d2014-10-22 12:13:23 +02002120 hdr = malloc(alloc_len);
2121 if (!hdr) {
2122 fprintf(stderr, "%s: malloc return failure: %s\n",
2123 params->cmdname, strerror(errno));
2124 exit(EXIT_FAILURE);
2125 }
2126
2127 memset(hdr, 0, alloc_len);
2128 tparams->header_size = alloc_len;
2129 tparams->hdr = hdr;
2130
Stefan Roeseda43fd32015-11-24 09:14:59 +01002131 /*
2132 * The resulting image needs to be 4-byte aligned. At least
2133 * the Marvell hdrparser tool complains if its unaligned.
Pali Rohárcfb60a92021-07-23 11:13:56 +02002134 * After the image data is stored 4-byte checksum.
Pali Rohárdeba0ad2021-11-08 18:12:46 +01002135 * Final UART image must be aligned to 128 bytes.
Pali Rohár04785152021-07-23 11:13:57 +02002136 * Final SPI and NAND images must be aligned to 256 bytes.
Pali Rohár6f6f65e2021-07-23 11:13:59 +02002137 * Final SATA and SDIO images must be aligned to 512 bytes.
Stefan Roeseda43fd32015-11-24 09:14:59 +01002138 */
Pali Rohár04785152021-07-23 11:13:57 +02002139 if (bootfrom == IBR_HDR_SPI_ID || bootfrom == IBR_HDR_NAND_ID)
2140 return 4 + (256 - (alloc_len + s.st_size + 4) % 256) % 256;
Pali Rohár6f6f65e2021-07-23 11:13:59 +02002141 else if (bootfrom == IBR_HDR_SATA_ID || bootfrom == IBR_HDR_SDIO_ID)
2142 return 4 + (512 - (alloc_len + s.st_size + 4) % 512) % 512;
Pali Rohárdeba0ad2021-11-08 18:12:46 +01002143 else if (bootfrom == IBR_HDR_UART_ID)
2144 return 4 + (128 - (alloc_len + s.st_size + 4) % 128) % 128;
Pali Rohár04785152021-07-23 11:13:57 +02002145 else
2146 return 4 + (4 - s.st_size % 4) % 4;
Prafulla Wadaskar07329412009-09-07 15:05:02 +05302147}
2148
Pali Rohár3d015782022-01-12 18:20:50 +01002149static int kwbimage_generate_config(void *ptr, struct image_tool_params *params)
2150{
2151 struct main_hdr_v0 *mhdr0 = (struct main_hdr_v0 *)ptr;
2152 struct main_hdr_v1 *mhdr = (struct main_hdr_v1 *)ptr;
2153 size_t header_size = kwbheader_size(ptr);
2154 struct register_set_hdr_v1 *regset_hdr;
2155 struct ext_hdr_v0_reg *regdata;
2156 struct ext_hdr_v0 *ehdr0;
Pali Roháre581f192022-02-17 10:43:36 +01002157 struct bin_hdr_v0 *bhdr0;
Pali Rohár3d015782022-01-12 18:20:50 +01002158 struct opt_hdr_v1 *ohdr;
Pali Roháre581f192022-02-17 10:43:36 +01002159 int params_count;
Pali Rohár3d015782022-01-12 18:20:50 +01002160 unsigned offset;
Pali Roháre581f192022-02-17 10:43:36 +01002161 int is_v0_ext;
Pali Rohár3d015782022-01-12 18:20:50 +01002162 int cur_idx;
2163 int version;
2164 FILE *f;
2165 int i;
2166
2167 f = fopen(params->outfile, "w");
2168 if (!f) {
2169 fprintf(stderr, "Can't open \"%s\": %s\n", params->outfile, strerror(errno));
2170 return -1;
2171 }
2172
2173 version = kwbimage_version(ptr);
2174
Pali Roháre581f192022-02-17 10:43:36 +01002175 is_v0_ext = 0;
2176 if (version == 0) {
2177 if (mhdr0->ext > 1 || mhdr0->bin ||
2178 ((ehdr0 = ext_hdr_v0_first(ptr)) &&
2179 (ehdr0->match_addr || ehdr0->match_mask || ehdr0->match_value)))
2180 is_v0_ext = 1;
2181 }
2182
Pali Rohár3d015782022-01-12 18:20:50 +01002183 if (version != 0)
2184 fprintf(f, "VERSION %d\n", version);
2185
2186 fprintf(f, "BOOT_FROM %s\n", image_boot_mode_name(mhdr->blockid) ?: "<unknown>");
2187
2188 if (version == 0 && mhdr->blockid == IBR_HDR_NAND_ID)
2189 fprintf(f, "NAND_ECC_MODE %s\n", image_nand_ecc_mode_name(mhdr0->nandeccmode));
2190
2191 if (mhdr->blockid == IBR_HDR_NAND_ID)
2192 fprintf(f, "NAND_PAGE_SIZE 0x%x\n", (unsigned)mhdr->nandpagesize);
2193
Pali Roháre581f192022-02-17 10:43:36 +01002194 if (version != 0 && mhdr->blockid == IBR_HDR_NAND_ID)
Pali Rohár3d015782022-01-12 18:20:50 +01002195 fprintf(f, "NAND_BLKSZ 0x%x\n", (unsigned)mhdr->nandblocksize);
Pali Roháre581f192022-02-17 10:43:36 +01002196
2197 if (mhdr->blockid == IBR_HDR_NAND_ID && (mhdr->nandbadblklocation != 0 || is_v0_ext))
Pali Rohár3d015782022-01-12 18:20:50 +01002198 fprintf(f, "NAND_BADBLK_LOCATION 0x%x\n", (unsigned)mhdr->nandbadblklocation);
Pali Rohár3d015782022-01-12 18:20:50 +01002199
2200 if (version == 0 && mhdr->blockid == IBR_HDR_SATA_ID)
2201 fprintf(f, "SATA_PIO_MODE %u\n", (unsigned)mhdr0->satapiomode);
2202
2203 /*
2204 * Addresses and sizes which are specified by mkimage command line
2205 * arguments and not in kwbimage config file
2206 */
2207
2208 if (version != 0)
2209 fprintf(f, "#HEADER_SIZE 0x%x\n",
2210 ((unsigned)mhdr->headersz_msb << 8) | le16_to_cpu(mhdr->headersz_lsb));
2211
2212 fprintf(f, "#SRC_ADDRESS 0x%x\n", le32_to_cpu(mhdr->srcaddr));
2213 fprintf(f, "#BLOCK_SIZE 0x%x\n", le32_to_cpu(mhdr->blocksize));
2214 fprintf(f, "#DEST_ADDRESS 0x%08x\n", le32_to_cpu(mhdr->destaddr));
2215 fprintf(f, "#EXEC_ADDRESS 0x%08x\n", le32_to_cpu(mhdr->execaddr));
2216
2217 if (version != 0) {
2218 if (options_to_baudrate(mhdr->options))
2219 fprintf(f, "BAUDRATE %u\n", options_to_baudrate(mhdr->options));
2220 if (options_to_baudrate(mhdr->options) ||
2221 ((mhdr->options >> 3) & 0x3) || ((mhdr->options >> 5) & 0x7)) {
2222 fprintf(f, "UART_PORT %u\n", (unsigned)((mhdr->options >> 3) & 0x3));
2223 fprintf(f, "UART_MPP 0x%x\n", (unsigned)((mhdr->options >> 5) & 0x7));
2224 }
2225 if (mhdr->flags & 0x1)
2226 fprintf(f, "DEBUG 1\n");
2227 }
2228
2229 cur_idx = 1;
2230 for_each_opt_hdr_v1(ohdr, ptr) {
2231 if (ohdr->headertype == OPT_HDR_V1_SECURE_TYPE) {
2232 fprintf(f, "#SECURE_HEADER\n");
2233 } else if (ohdr->headertype == OPT_HDR_V1_BINARY_TYPE) {
2234 fprintf(f, "BINARY binary%d.bin", cur_idx);
2235 for (i = 0; i < ohdr->data[0]; i++)
2236 fprintf(f, " 0x%x", le32_to_cpu(((uint32_t *)ohdr->data)[i + 1]));
2237 offset = (unsigned)((uint8_t *)ohdr - (uint8_t *)mhdr) + 8 + 4 * ohdr->data[0];
2238 fprintf(f, " LOAD_ADDRESS 0x%08x\n", 0x40000000 + offset);
2239 fprintf(f, " # for CPU SHEEVA: LOAD_ADDRESS 0x%08x\n", 0x40004000 + offset);
2240 cur_idx++;
2241 } else if (ohdr->headertype == OPT_HDR_V1_REGISTER_TYPE) {
2242 regset_hdr = (struct register_set_hdr_v1 *)ohdr;
2243 for (i = 0;
2244 i < opt_hdr_v1_size(ohdr) - sizeof(struct opt_hdr_v1) -
2245 sizeof(regset_hdr->data[0].last_entry);
2246 i++)
2247 fprintf(f, "DATA 0x%08x 0x%08x\n",
2248 le32_to_cpu(regset_hdr->data[i].entry.address),
2249 le32_to_cpu(regset_hdr->data[i].entry.value));
2250 if (opt_hdr_v1_size(ohdr) - sizeof(struct opt_hdr_v1) >=
2251 sizeof(regset_hdr->data[0].last_entry)) {
2252 if (regset_hdr->data[0].last_entry.delay)
2253 fprintf(f, "DATA_DELAY %u\n",
2254 (unsigned)regset_hdr->data[0].last_entry.delay);
2255 else
2256 fprintf(f, "DATA_DELAY SDRAM_SETUP\n");
2257 }
2258 }
2259 }
2260
Pali Roháre581f192022-02-17 10:43:36 +01002261 if (version == 0 && !is_v0_ext && le16_to_cpu(mhdr0->ddrinitdelay))
2262 fprintf(f, "DDR_INIT_DELAY %u\n", (unsigned)le16_to_cpu(mhdr0->ddrinitdelay));
2263
2264 for_each_ext_hdr_v0(ehdr0, ptr) {
2265 if (is_v0_ext) {
2266 fprintf(f, "\nMATCH ADDRESS 0x%08x MASK 0x%08x VALUE 0x%08x\n",
2267 le32_to_cpu(ehdr0->match_addr),
2268 le32_to_cpu(ehdr0->match_mask),
2269 le32_to_cpu(ehdr0->match_value));
2270 if (ehdr0->rsvd1[0] || ehdr0->rsvd1[1] || ehdr0->rsvd1[2] ||
2271 ehdr0->rsvd1[3] || ehdr0->rsvd1[4] || ehdr0->rsvd1[5] ||
2272 ehdr0->rsvd1[6] || ehdr0->rsvd1[7])
2273 fprintf(f, "#DDR_RSVD1 0x%x 0x%x 0x%x 0x%x 0x%x 0x%x 0x%x 0x%x\n",
2274 ehdr0->rsvd1[0], ehdr0->rsvd1[1], ehdr0->rsvd1[2],
2275 ehdr0->rsvd1[3], ehdr0->rsvd1[4], ehdr0->rsvd1[5],
2276 ehdr0->rsvd1[6], ehdr0->rsvd1[7]);
2277 if (ehdr0->rsvd2[0] || ehdr0->rsvd2[1] || ehdr0->rsvd2[2] ||
2278 ehdr0->rsvd2[3] || ehdr0->rsvd2[4] || ehdr0->rsvd2[5] ||
2279 ehdr0->rsvd2[6])
2280 fprintf(f, "#DDR_RSVD2 0x%x 0x%x 0x%x 0x%x 0x%x 0x%x 0x%x\n",
2281 ehdr0->rsvd2[0], ehdr0->rsvd2[1], ehdr0->rsvd2[2],
2282 ehdr0->rsvd2[3], ehdr0->rsvd2[4], ehdr0->rsvd2[5],
2283 ehdr0->rsvd2[6]);
2284 if (ehdr0->ddrwritetype)
2285 fprintf(f, "DDR_WRITE_TYPE %u\n", (unsigned)ehdr0->ddrwritetype);
2286 if (ehdr0->ddrresetmpp)
2287 fprintf(f, "DDR_RESET_MPP 0x%x\n", (unsigned)ehdr0->ddrresetmpp);
2288 if (ehdr0->ddrclkenmpp)
2289 fprintf(f, "DDR_CLKEN_MPP 0x%x\n", (unsigned)ehdr0->ddrclkenmpp);
2290 if (ehdr0->ddrinitdelay)
2291 fprintf(f, "DDR_INIT_DELAY %u\n", (unsigned)ehdr0->ddrinitdelay);
2292 }
2293
Pali Rohár3d015782022-01-12 18:20:50 +01002294 if (ehdr0->offset) {
2295 for (regdata = (struct ext_hdr_v0_reg *)((uint8_t *)ptr + ehdr0->offset);
Pali Rohár0e129b82022-02-13 01:04:33 +01002296 (uint8_t *)regdata < (uint8_t *)ptr + header_size &&
2297 (regdata->raddr || regdata->rdata);
Pali Rohár3d015782022-01-12 18:20:50 +01002298 regdata++)
2299 fprintf(f, "DATA 0x%08x 0x%08x\n", le32_to_cpu(regdata->raddr),
2300 le32_to_cpu(regdata->rdata));
Pali Rohár0e129b82022-02-13 01:04:33 +01002301 if ((uint8_t *)regdata != (uint8_t *)ptr + ehdr0->offset)
2302 fprintf(f, "DATA 0x0 0x0\n");
Pali Rohár3d015782022-01-12 18:20:50 +01002303 }
Pali Roháre581f192022-02-17 10:43:36 +01002304
2305 if (le32_to_cpu(ehdr0->enddelay))
2306 fprintf(f, "DATA_DELAY %u\n", le32_to_cpu(ehdr0->enddelay));
2307 else if (is_v0_ext)
2308 fprintf(f, "DATA_DELAY SDRAM_SETUP\n");
Pali Rohár3d015782022-01-12 18:20:50 +01002309 }
2310
Pali Roháre581f192022-02-17 10:43:36 +01002311 cur_idx = 1;
2312 for_each_bin_hdr_v0(bhdr0, ptr) {
2313 fprintf(f, "\nMATCH ADDRESS 0x%08x MASK 0x%08x VALUE 0x%08x\n",
2314 le32_to_cpu(bhdr0->match_addr),
2315 le32_to_cpu(bhdr0->match_mask),
2316 le32_to_cpu(bhdr0->match_value));
2317
2318 fprintf(f, "BINARY binary%d.bin", cur_idx);
2319 params_count = fls4(bhdr0->params_flags & 0xF);
2320 for (i = 0; i < params_count; i++)
2321 fprintf(f, " 0x%x", (bhdr0->params[i] & (1 << i)) ? bhdr0->params[i] : 0);
2322 fprintf(f, " LOAD_ADDRESS 0x%08x", le32_to_cpu(bhdr0->destaddr));
2323 fprintf(f, " EXEC_ADDRESS 0x%08x", le32_to_cpu(bhdr0->execaddr));
2324 fprintf(f, "\n");
2325
2326 fprintf(f, "#BINARY_OFFSET 0x%x\n", le32_to_cpu(bhdr0->offset));
2327 fprintf(f, "#BINARY_SIZE 0x%x\n", le32_to_cpu(bhdr0->size));
2328
2329 if (bhdr0->rsvd1)
2330 fprintf(f, "#BINARY_RSVD1 0x%x\n", (unsigned)bhdr0->rsvd1);
2331 if (bhdr0->rsvd2)
2332 fprintf(f, "#BINARY_RSVD2 0x%x\n", (unsigned)bhdr0->rsvd2);
2333
2334 cur_idx++;
2335 }
Pali Rohár3d015782022-01-12 18:20:50 +01002336
2337 /* Undocumented reserved fields */
2338
2339 if (version == 0 && (mhdr0->rsvd1[0] || mhdr0->rsvd1[1] || mhdr0->rsvd1[2]))
2340 fprintf(f, "#RSVD1 0x%x 0x%x 0x%x\n", (unsigned)mhdr0->rsvd1[0],
2341 (unsigned)mhdr0->rsvd1[1], (unsigned)mhdr0->rsvd1[2]);
2342
Pali Rohár3d015782022-01-12 18:20:50 +01002343 if (version == 0 && le16_to_cpu(mhdr0->rsvd2))
2344 fprintf(f, "#RSVD2 0x%x\n", (unsigned)le16_to_cpu(mhdr0->rsvd2));
2345
2346 if (version != 0 && mhdr->reserved4)
2347 fprintf(f, "#RESERVED4 0x%x\n", (unsigned)mhdr->reserved4);
2348
2349 if (version != 0 && mhdr->reserved5)
2350 fprintf(f, "#RESERVED5 0x%x\n", (unsigned)le16_to_cpu(mhdr->reserved5));
2351
2352 fclose(f);
2353
2354 return 0;
2355}
2356
Pali Rohár5ec2c582021-07-23 11:14:34 +02002357static int kwbimage_extract_subimage(void *ptr, struct image_tool_params *params)
2358{
2359 struct main_hdr_v1 *mhdr = (struct main_hdr_v1 *)ptr;
Marek Behúnd1b0b032021-09-24 23:07:01 +02002360 size_t header_size = kwbheader_size(ptr);
Pali Roháre581f192022-02-17 10:43:36 +01002361 struct bin_hdr_v0 *bhdr;
Marek Behúnb1a76502021-08-18 00:59:15 +02002362 struct opt_hdr_v1 *ohdr;
Pali Rohár5ec2c582021-07-23 11:14:34 +02002363 int idx = params->pflag;
Pali Rohár7e18d0a2022-01-12 18:20:53 +01002364 int cur_idx;
Pali Rohár5ec2c582021-07-23 11:14:34 +02002365 uint32_t offset;
2366 ulong image;
2367 ulong size;
2368
Pali Rohár3d015782022-01-12 18:20:50 +01002369 /* Generate kwbimage config file when '-p -1' is specified */
2370 if (idx == -1)
2371 return kwbimage_generate_config(ptr, params);
2372
Pali Rohár7e18d0a2022-01-12 18:20:53 +01002373 image = 0;
2374 size = 0;
Pali Rohár5ec2c582021-07-23 11:14:34 +02002375
Pali Rohár7e18d0a2022-01-12 18:20:53 +01002376 if (idx == 0) {
2377 /* Extract data image when -p is not specified or when '-p 0' is specified */
2378 offset = le32_to_cpu(mhdr->srcaddr);
Marek Behúnb1a76502021-08-18 00:59:15 +02002379
Pali Rohár7e18d0a2022-01-12 18:20:53 +01002380 if (mhdr->blockid == IBR_HDR_SATA_ID) {
2381 offset -= 1;
2382 offset *= 512;
2383 }
Pali Rohár5ec2c582021-07-23 11:14:34 +02002384
Pali Rohár7e18d0a2022-01-12 18:20:53 +01002385 if (mhdr->blockid == IBR_HDR_PEX_ID && offset == 0xFFFFFFFF)
2386 offset = header_size;
Pali Rohár5ec2c582021-07-23 11:14:34 +02002387
Pali Rohár7e18d0a2022-01-12 18:20:53 +01002388 image = (ulong)((uint8_t *)ptr + offset);
2389 size = le32_to_cpu(mhdr->blocksize) - 4;
2390 } else {
2391 /* Extract N-th binary header executabe image when other '-p N' is specified */
2392 cur_idx = 1;
2393 for_each_opt_hdr_v1(ohdr, ptr) {
2394 if (ohdr->headertype != OPT_HDR_V1_BINARY_TYPE)
2395 continue;
Pali Rohár5ec2c582021-07-23 11:14:34 +02002396
Pali Rohár7e18d0a2022-01-12 18:20:53 +01002397 if (idx == cur_idx) {
2398 image = (ulong)&ohdr->data[4 + 4 * ohdr->data[0]];
2399 size = opt_hdr_v1_size(ohdr) - 12 - 4 * ohdr->data[0];
2400 break;
2401 }
Pali Rohár5ec2c582021-07-23 11:14:34 +02002402
Pali Rohár7e18d0a2022-01-12 18:20:53 +01002403 ++cur_idx;
2404 }
Pali Roháre581f192022-02-17 10:43:36 +01002405 for_each_bin_hdr_v0(bhdr, ptr) {
2406 if (idx == cur_idx) {
2407 image = (ulong)bhdr + bhdr->offset;
2408 size = bhdr->size;
2409 break;
2410 }
2411 ++cur_idx;
2412 }
Pali Rohár5ec2c582021-07-23 11:14:34 +02002413
Pali Rohár7e18d0a2022-01-12 18:20:53 +01002414 if (!image) {
2415 fprintf(stderr, "Argument -p %d is invalid\n", idx);
2416 fprintf(stderr, "Available subimages:\n");
2417 fprintf(stderr, " -p -1 - kwbimage config file\n");
2418 fprintf(stderr, " -p 0 - data image\n");
2419 if (cur_idx - 1 > 0)
2420 fprintf(stderr, " -p N - Nth binary header image (totally: %d)\n",
2421 cur_idx - 1);
2422 return -1;
2423 }
2424 }
Pali Rohár5ec2c582021-07-23 11:14:34 +02002425
Pali Rohár5ec2c582021-07-23 11:14:34 +02002426 return imagetool_save_subimage(params->outfile, image, size);
2427}
2428
Stefan Roese3b8b19d2014-10-22 12:13:23 +02002429/*
2430 * Report Error if xflag is set in addition to default
2431 */
2432static int kwbimage_check_params(struct image_tool_params *params)
Prafulla Wadaskar07329412009-09-07 15:05:02 +05302433{
Pali Rohár2dbaf402022-02-17 10:43:37 +01002434 if (!params->lflag && !params->iflag && !params->pflag &&
Pali Rohára0722ce2022-01-12 18:20:54 +01002435 (!params->imagename || !strlen(params->imagename))) {
Mario Sixd6009d72017-01-11 16:00:54 +01002436 char *msg = "Configuration file for kwbimage creation omitted";
2437
2438 fprintf(stderr, "Error:%s - %s\n", params->cmdname, msg);
Pali Rohárac0c3482021-11-08 18:12:45 +01002439 return 1;
Stefan Roese3b8b19d2014-10-22 12:13:23 +02002440 }
2441
2442 return (params->dflag && (params->fflag || params->lflag)) ||
2443 (params->fflag && (params->dflag || params->lflag)) ||
2444 (params->lflag && (params->dflag || params->fflag)) ||
Pali Rohár5ec2c582021-07-23 11:14:34 +02002445 (params->xflag);
Prafulla Wadaskar07329412009-09-07 15:05:02 +05302446}
2447
2448/*
2449 * kwbimage type parameters definition
2450 */
Guilherme Maciel Ferreira28be1cf2015-01-15 02:48:07 -02002451U_BOOT_IMAGE_TYPE(
2452 kwbimage,
2453 "Marvell MVEBU Boot Image support",
2454 0,
2455 NULL,
2456 kwbimage_check_params,
2457 kwbimage_verify_header,
2458 kwbimage_print_header,
2459 kwbimage_set_header,
Pali Rohár5ec2c582021-07-23 11:14:34 +02002460 kwbimage_extract_subimage,
Guilherme Maciel Ferreira28be1cf2015-01-15 02:48:07 -02002461 kwbimage_check_image_types,
2462 NULL,
2463 kwbimage_generate
2464);