blob: f270dcf242b639da6955607c0b5bf775a58214b3 [file] [log] [blame]
Tom Rini83d290c2018-05-06 17:58:06 -04001// SPDX-License-Identifier: GPL-2.0+
Prafulla Wadaskaraa0c7a82009-09-07 15:05:02 +05302/*
Stefan Roese4acd2d22014-10-22 12:13:23 +02003 * Image manipulator for Marvell SoCs
Pali Rohár8010f4f2021-09-24 23:07:02 +02004 * supports Kirkwood, Dove, Armada 370, Armada XP, Armada 375, Armada 38x and
5 * Armada 39x
Stefan Roese4acd2d22014-10-22 12:13:23 +02006 *
7 * (C) Copyright 2013 Thomas Petazzoni
8 * <thomas.petazzoni@free-electrons.com>
Prafulla Wadaskaraa0c7a82009-09-07 15:05:02 +05309 */
10
Heinrich Schuchardt3a8b9192021-12-18 11:25:12 +010011#define OPENSSL_API_COMPAT 0x10101000L
12
Guilherme Maciel Ferreiraf86ed6a2013-12-01 12:43:10 -070013#include "imagetool.h"
Andreas Bießmanne5f1a582014-10-24 23:39:11 +020014#include <limits.h>
Prafulla Wadaskaraa0c7a82009-09-07 15:05:02 +053015#include <image.h>
Mario Sixa1b6b0a2017-01-11 16:01:00 +010016#include <stdarg.h>
Stefan Roese4acd2d22014-10-22 12:13:23 +020017#include <stdint.h>
Prafulla Wadaskaraa0c7a82009-09-07 15:05:02 +053018#include "kwbimage.h"
19
Jelle van der Waae15843b2017-05-08 21:31:20 +020020#include <openssl/bn.h>
Mario Sixa1b6b0a2017-01-11 16:01:00 +010021#include <openssl/rsa.h>
22#include <openssl/pem.h>
23#include <openssl/err.h>
24#include <openssl/evp.h>
Jelle van der Waae15843b2017-05-08 21:31:20 +020025
Jonathan Graya2d5efd2018-02-21 02:59:01 +110026#if OPENSSL_VERSION_NUMBER < 0x10100000L || \
27 (defined(LIBRESSL_VERSION_NUMBER) && LIBRESSL_VERSION_NUMBER < 0x2070000fL)
Jelle van der Waae15843b2017-05-08 21:31:20 +020028static void RSA_get0_key(const RSA *r,
29 const BIGNUM **n, const BIGNUM **e, const BIGNUM **d)
30{
31 if (n != NULL)
32 *n = r->n;
33 if (e != NULL)
34 *e = r->e;
35 if (d != NULL)
36 *d = r->d;
37}
38
Jonathan Graya2d5efd2018-02-21 02:59:01 +110039#elif !defined(LIBRESSL_VERSION_NUMBER)
Jelle van der Waae15843b2017-05-08 21:31:20 +020040void EVP_MD_CTX_cleanup(EVP_MD_CTX *ctx)
41{
42 EVP_MD_CTX_reset(ctx);
43}
44#endif
Mario Sixa1b6b0a2017-01-11 16:01:00 +010045
Pali Rohárf76ae252022-02-17 10:43:36 +010046/* fls - find last (most-significant) bit set in 4-bit integer */
47static inline int fls4(int num)
48{
49 if (num & 0x8)
50 return 4;
51 else if (num & 0x4)
52 return 3;
53 else if (num & 0x2)
54 return 2;
55 else if (num & 0x1)
56 return 1;
57 else
58 return 0;
59}
60
Stefan Roese4acd2d22014-10-22 12:13:23 +020061static struct image_cfg_element *image_cfg;
62static int cfgn;
Mario Sixa1b6b0a2017-01-11 16:01:00 +010063static int verbose_mode;
Stefan Roese4acd2d22014-10-22 12:13:23 +020064
65struct boot_mode {
66 unsigned int id;
67 const char *name;
Prafulla Wadaskaraa0c7a82009-09-07 15:05:02 +053068};
69
Mario Sixa1b6b0a2017-01-11 16:01:00 +010070/*
71 * SHA2-256 hash
72 */
73struct hash_v1 {
74 uint8_t hash[32];
75};
76
Stefan Roese4acd2d22014-10-22 12:13:23 +020077struct boot_mode boot_modes[] = {
Pali Roháre515a332021-08-11 10:14:17 +020078 { IBR_HDR_I2C_ID, "i2c" },
79 { IBR_HDR_SPI_ID, "spi" },
80 { IBR_HDR_NAND_ID, "nand" },
81 { IBR_HDR_SATA_ID, "sata" },
82 { IBR_HDR_PEX_ID, "pex" },
83 { IBR_HDR_UART_ID, "uart" },
84 { IBR_HDR_SDIO_ID, "sdio" },
Stefan Roese4acd2d22014-10-22 12:13:23 +020085 {},
86};
87
88struct nand_ecc_mode {
89 unsigned int id;
90 const char *name;
91};
92
93struct nand_ecc_mode nand_ecc_modes[] = {
Pali Roháre515a332021-08-11 10:14:17 +020094 { IBR_HDR_ECC_DEFAULT, "default" },
95 { IBR_HDR_ECC_FORCED_HAMMING, "hamming" },
96 { IBR_HDR_ECC_FORCED_RS, "rs" },
97 { IBR_HDR_ECC_DISABLED, "disabled" },
Stefan Roese4acd2d22014-10-22 12:13:23 +020098 {},
99};
100
101/* Used to identify an undefined execution or destination address */
102#define ADDR_INVALID ((uint32_t)-1)
103
Pali Rohár6c7f1522021-07-23 11:14:07 +0200104#define BINARY_MAX_ARGS 255
Stefan Roese4acd2d22014-10-22 12:13:23 +0200105
106/* In-memory representation of a line of the configuration file */
Mario Six4991b4f2017-01-11 16:00:59 +0100107
108enum image_cfg_type {
109 IMAGE_CFG_VERSION = 0x1,
110 IMAGE_CFG_BOOT_FROM,
111 IMAGE_CFG_DEST_ADDR,
112 IMAGE_CFG_EXEC_ADDR,
113 IMAGE_CFG_NAND_BLKSZ,
114 IMAGE_CFG_NAND_BADBLK_LOCATION,
115 IMAGE_CFG_NAND_ECC_MODE,
116 IMAGE_CFG_NAND_PAGESZ,
Pali Roháraf496052022-01-12 18:20:40 +0100117 IMAGE_CFG_CPU,
Mario Six4991b4f2017-01-11 16:00:59 +0100118 IMAGE_CFG_BINARY,
Mario Six4991b4f2017-01-11 16:00:59 +0100119 IMAGE_CFG_DATA,
Pali Rohárf63c5832021-07-23 11:14:12 +0200120 IMAGE_CFG_DATA_DELAY,
Mario Six4991b4f2017-01-11 16:00:59 +0100121 IMAGE_CFG_BAUDRATE,
Pali Rohár12f2c032021-11-08 18:12:41 +0100122 IMAGE_CFG_UART_PORT,
123 IMAGE_CFG_UART_MPP,
Mario Six4991b4f2017-01-11 16:00:59 +0100124 IMAGE_CFG_DEBUG,
Mario Sixa1b6b0a2017-01-11 16:01:00 +0100125 IMAGE_CFG_KAK,
126 IMAGE_CFG_CSK,
127 IMAGE_CFG_CSK_INDEX,
128 IMAGE_CFG_JTAG_DELAY,
129 IMAGE_CFG_BOX_ID,
130 IMAGE_CFG_FLASH_ID,
131 IMAGE_CFG_SEC_COMMON_IMG,
132 IMAGE_CFG_SEC_SPECIALIZED_IMG,
133 IMAGE_CFG_SEC_BOOT_DEV,
134 IMAGE_CFG_SEC_FUSE_DUMP,
Mario Six4991b4f2017-01-11 16:00:59 +0100135
136 IMAGE_CFG_COUNT
137} type;
138
139static const char * const id_strs[] = {
140 [IMAGE_CFG_VERSION] = "VERSION",
141 [IMAGE_CFG_BOOT_FROM] = "BOOT_FROM",
142 [IMAGE_CFG_DEST_ADDR] = "DEST_ADDR",
143 [IMAGE_CFG_EXEC_ADDR] = "EXEC_ADDR",
144 [IMAGE_CFG_NAND_BLKSZ] = "NAND_BLKSZ",
145 [IMAGE_CFG_NAND_BADBLK_LOCATION] = "NAND_BADBLK_LOCATION",
146 [IMAGE_CFG_NAND_ECC_MODE] = "NAND_ECC_MODE",
147 [IMAGE_CFG_NAND_PAGESZ] = "NAND_PAGE_SIZE",
Pali Roháraf496052022-01-12 18:20:40 +0100148 [IMAGE_CFG_CPU] = "CPU",
Mario Six4991b4f2017-01-11 16:00:59 +0100149 [IMAGE_CFG_BINARY] = "BINARY",
Mario Six4991b4f2017-01-11 16:00:59 +0100150 [IMAGE_CFG_DATA] = "DATA",
Pali Rohárf63c5832021-07-23 11:14:12 +0200151 [IMAGE_CFG_DATA_DELAY] = "DATA_DELAY",
Mario Six4991b4f2017-01-11 16:00:59 +0100152 [IMAGE_CFG_BAUDRATE] = "BAUDRATE",
Pali Rohár12f2c032021-11-08 18:12:41 +0100153 [IMAGE_CFG_UART_PORT] = "UART_PORT",
154 [IMAGE_CFG_UART_MPP] = "UART_MPP",
Mario Six4991b4f2017-01-11 16:00:59 +0100155 [IMAGE_CFG_DEBUG] = "DEBUG",
Mario Sixa1b6b0a2017-01-11 16:01:00 +0100156 [IMAGE_CFG_KAK] = "KAK",
157 [IMAGE_CFG_CSK] = "CSK",
158 [IMAGE_CFG_CSK_INDEX] = "CSK_INDEX",
159 [IMAGE_CFG_JTAG_DELAY] = "JTAG_DELAY",
160 [IMAGE_CFG_BOX_ID] = "BOX_ID",
161 [IMAGE_CFG_FLASH_ID] = "FLASH_ID",
162 [IMAGE_CFG_SEC_COMMON_IMG] = "SEC_COMMON_IMG",
163 [IMAGE_CFG_SEC_SPECIALIZED_IMG] = "SEC_SPECIALIZED_IMG",
164 [IMAGE_CFG_SEC_BOOT_DEV] = "SEC_BOOT_DEV",
165 [IMAGE_CFG_SEC_FUSE_DUMP] = "SEC_FUSE_DUMP"
Mario Six4991b4f2017-01-11 16:00:59 +0100166};
167
Stefan Roese4acd2d22014-10-22 12:13:23 +0200168struct image_cfg_element {
Mario Six4991b4f2017-01-11 16:00:59 +0100169 enum image_cfg_type type;
Stefan Roese4acd2d22014-10-22 12:13:23 +0200170 union {
171 unsigned int version;
Pali Roháraf496052022-01-12 18:20:40 +0100172 unsigned int cpu_sheeva;
Stefan Roese4acd2d22014-10-22 12:13:23 +0200173 unsigned int bootfrom;
174 struct {
175 const char *file;
Pali Rohár0aca27e2022-01-12 18:20:41 +0100176 unsigned int loadaddr;
Stefan Roese4acd2d22014-10-22 12:13:23 +0200177 unsigned int args[BINARY_MAX_ARGS];
178 unsigned int nargs;
179 } binary;
Stefan Roese4acd2d22014-10-22 12:13:23 +0200180 unsigned int dstaddr;
181 unsigned int execaddr;
182 unsigned int nandblksz;
183 unsigned int nandbadblklocation;
184 unsigned int nandeccmode;
185 unsigned int nandpagesz;
186 struct ext_hdr_v0_reg regdata;
Pali Rohárf63c5832021-07-23 11:14:12 +0200187 unsigned int regdata_delay;
Chris Packham4bdb5472016-11-09 22:07:45 +1300188 unsigned int baudrate;
Pali Rohár12f2c032021-11-08 18:12:41 +0100189 unsigned int uart_port;
190 unsigned int uart_mpp;
Chris Packham2611c052016-11-09 22:21:45 +1300191 unsigned int debug;
Mario Sixa1b6b0a2017-01-11 16:01:00 +0100192 const char *key_name;
193 int csk_idx;
194 uint8_t jtag_delay;
195 uint32_t boxid;
196 uint32_t flashid;
197 bool sec_specialized_img;
198 unsigned int sec_boot_dev;
199 const char *name;
Stefan Roese4acd2d22014-10-22 12:13:23 +0200200 };
201};
202
203#define IMAGE_CFG_ELEMENT_MAX 256
Prafulla Wadaskaraa0c7a82009-09-07 15:05:02 +0530204
205/*
Stefan Roese4acd2d22014-10-22 12:13:23 +0200206 * Utility functions to manipulate boot mode and ecc modes (convert
207 * them back and forth between description strings and the
208 * corresponding numerical identifiers).
Prafulla Wadaskaraa0c7a82009-09-07 15:05:02 +0530209 */
Stefan Roese4acd2d22014-10-22 12:13:23 +0200210
211static const char *image_boot_mode_name(unsigned int id)
Prafulla Wadaskaraa0c7a82009-09-07 15:05:02 +0530212{
Stefan Roese4acd2d22014-10-22 12:13:23 +0200213 int i;
Mario Six94490a42017-01-11 16:00:54 +0100214
Stefan Roese4acd2d22014-10-22 12:13:23 +0200215 for (i = 0; boot_modes[i].name; i++)
216 if (boot_modes[i].id == id)
217 return boot_modes[i].name;
218 return NULL;
219}
220
Pali Rohár6eb20bb2022-01-12 18:20:35 +0100221static int image_boot_mode_id(const char *boot_mode_name)
Stefan Roese4acd2d22014-10-22 12:13:23 +0200222{
223 int i;
Mario Six94490a42017-01-11 16:00:54 +0100224
Stefan Roese4acd2d22014-10-22 12:13:23 +0200225 for (i = 0; boot_modes[i].name; i++)
226 if (!strcmp(boot_modes[i].name, boot_mode_name))
227 return boot_modes[i].id;
228
229 return -1;
230}
231
Pali Rohár1a8e6b62022-01-12 18:20:50 +0100232static const char *image_nand_ecc_mode_name(unsigned int id)
233{
234 int i;
235
236 for (i = 0; nand_ecc_modes[i].name; i++)
237 if (nand_ecc_modes[i].id == id)
238 return nand_ecc_modes[i].name;
239
240 return NULL;
241}
242
Pali Rohár6eb20bb2022-01-12 18:20:35 +0100243static int image_nand_ecc_mode_id(const char *nand_ecc_mode_name)
Stefan Roese4acd2d22014-10-22 12:13:23 +0200244{
245 int i;
Mario Six94490a42017-01-11 16:00:54 +0100246
Stefan Roese4acd2d22014-10-22 12:13:23 +0200247 for (i = 0; nand_ecc_modes[i].name; i++)
248 if (!strcmp(nand_ecc_modes[i].name, nand_ecc_mode_name))
249 return nand_ecc_modes[i].id;
250 return -1;
251}
252
253static struct image_cfg_element *
254image_find_option(unsigned int optiontype)
255{
256 int i;
257
258 for (i = 0; i < cfgn; i++) {
259 if (image_cfg[i].type == optiontype)
260 return &image_cfg[i];
261 }
262
263 return NULL;
264}
265
266static unsigned int
267image_count_options(unsigned int optiontype)
268{
269 int i;
270 unsigned int count = 0;
271
272 for (i = 0; i < cfgn; i++)
273 if (image_cfg[i].type == optiontype)
274 count++;
275
276 return count;
277}
278
Mario Sixa1b6b0a2017-01-11 16:01:00 +0100279static int image_get_csk_index(void)
280{
281 struct image_cfg_element *e;
282
283 e = image_find_option(IMAGE_CFG_CSK_INDEX);
284 if (!e)
285 return -1;
286
287 return e->csk_idx;
288}
289
290static bool image_get_spezialized_img(void)
291{
292 struct image_cfg_element *e;
293
294 e = image_find_option(IMAGE_CFG_SEC_SPECIALIZED_IMG);
295 if (!e)
296 return false;
297
298 return e->sec_specialized_img;
299}
300
Pali Rohárd1547b32021-11-08 18:12:43 +0100301static int image_get_bootfrom(void)
302{
303 struct image_cfg_element *e;
304
305 e = image_find_option(IMAGE_CFG_BOOT_FROM);
306 if (!e)
307 /* fallback to SPI if no BOOT_FROM is not provided */
308 return IBR_HDR_SPI_ID;
309
310 return e->bootfrom;
311}
312
Pali Roháraf496052022-01-12 18:20:40 +0100313static int image_is_cpu_sheeva(void)
314{
315 struct image_cfg_element *e;
316
317 e = image_find_option(IMAGE_CFG_CPU);
318 if (!e)
319 return 0;
320
321 return e->cpu_sheeva;
322}
323
Stefan Roese4acd2d22014-10-22 12:13:23 +0200324/*
325 * Compute a 8-bit checksum of a memory area. This algorithm follows
326 * the requirements of the Marvell SoC BootROM specifications.
327 */
328static uint8_t image_checksum8(void *start, uint32_t len)
329{
330 uint8_t csum = 0;
331 uint8_t *p = start;
Prafulla Wadaskaraa0c7a82009-09-07 15:05:02 +0530332
333 /* check len and return zero checksum if invalid */
334 if (!len)
335 return 0;
336
337 do {
Stefan Roese4acd2d22014-10-22 12:13:23 +0200338 csum += *p;
Prafulla Wadaskaraa0c7a82009-09-07 15:05:02 +0530339 p++;
340 } while (--len);
Stefan Roese4acd2d22014-10-22 12:13:23 +0200341
342 return csum;
Prafulla Wadaskaraa0c7a82009-09-07 15:05:02 +0530343}
344
Baruch Siachdb7cd4ed2017-07-04 20:23:40 +0300345/*
346 * Verify checksum over a complete header that includes the checksum field.
347 * Return 1 when OK, otherwise 0.
348 */
349static int main_hdr_checksum_ok(void *hdr)
350{
351 /* Offsets of checksum in v0 and v1 headers are the same */
352 struct main_hdr_v0 *main_hdr = (struct main_hdr_v0 *)hdr;
353 uint8_t checksum;
354
Marek Behúnfe2fd732021-09-24 23:07:01 +0200355 checksum = image_checksum8(hdr, kwbheader_size_for_csum(hdr));
Baruch Siachdb7cd4ed2017-07-04 20:23:40 +0300356 /* Calculated checksum includes the header checksum field. Compensate
357 * for that.
358 */
359 checksum -= main_hdr->checksum;
360
361 return checksum == main_hdr->checksum;
362}
363
Stefan Roese4acd2d22014-10-22 12:13:23 +0200364static uint32_t image_checksum32(void *start, uint32_t len)
Prafulla Wadaskaraa0c7a82009-09-07 15:05:02 +0530365{
Stefan Roese4acd2d22014-10-22 12:13:23 +0200366 uint32_t csum = 0;
367 uint32_t *p = start;
Prafulla Wadaskaraa0c7a82009-09-07 15:05:02 +0530368
369 /* check len and return zero checksum if invalid */
370 if (!len)
371 return 0;
372
373 if (len % sizeof(uint32_t)) {
Stefan Roese4acd2d22014-10-22 12:13:23 +0200374 fprintf(stderr, "Length %d is not in multiple of %zu\n",
375 len, sizeof(uint32_t));
Prafulla Wadaskaraa0c7a82009-09-07 15:05:02 +0530376 return 0;
377 }
378
379 do {
Stefan Roese4acd2d22014-10-22 12:13:23 +0200380 csum += *p;
Prafulla Wadaskaraa0c7a82009-09-07 15:05:02 +0530381 p++;
382 len -= sizeof(uint32_t);
383 } while (len > 0);
Stefan Roese4acd2d22014-10-22 12:13:23 +0200384
385 return csum;
Prafulla Wadaskaraa0c7a82009-09-07 15:05:02 +0530386}
387
Pali Rohár1a8e6b62022-01-12 18:20:50 +0100388static unsigned int options_to_baudrate(uint8_t options)
389{
390 switch (options & 0x7) {
391 case MAIN_HDR_V1_OPT_BAUD_2400:
392 return 2400;
393 case MAIN_HDR_V1_OPT_BAUD_4800:
394 return 4800;
395 case MAIN_HDR_V1_OPT_BAUD_9600:
396 return 9600;
397 case MAIN_HDR_V1_OPT_BAUD_19200:
398 return 19200;
399 case MAIN_HDR_V1_OPT_BAUD_38400:
400 return 38400;
401 case MAIN_HDR_V1_OPT_BAUD_57600:
402 return 57600;
403 case MAIN_HDR_V1_OPT_BAUD_115200:
404 return 115200;
405 case MAIN_HDR_V1_OPT_BAUD_DEFAULT:
406 default:
407 return 0;
408 }
409}
410
Chris Packham4bdb5472016-11-09 22:07:45 +1300411static uint8_t baudrate_to_option(unsigned int baudrate)
412{
413 switch (baudrate) {
414 case 2400:
415 return MAIN_HDR_V1_OPT_BAUD_2400;
416 case 4800:
417 return MAIN_HDR_V1_OPT_BAUD_4800;
418 case 9600:
419 return MAIN_HDR_V1_OPT_BAUD_9600;
420 case 19200:
421 return MAIN_HDR_V1_OPT_BAUD_19200;
422 case 38400:
423 return MAIN_HDR_V1_OPT_BAUD_38400;
424 case 57600:
425 return MAIN_HDR_V1_OPT_BAUD_57600;
426 case 115200:
427 return MAIN_HDR_V1_OPT_BAUD_115200;
428 default:
429 return MAIN_HDR_V1_OPT_BAUD_DEFAULT;
430 }
431}
432
Mario Sixa1b6b0a2017-01-11 16:01:00 +0100433static void kwb_msg(const char *fmt, ...)
434{
435 if (verbose_mode) {
436 va_list ap;
437
438 va_start(ap, fmt);
439 vfprintf(stdout, fmt, ap);
440 va_end(ap);
441 }
442}
443
444static int openssl_err(const char *msg)
445{
446 unsigned long ssl_err = ERR_get_error();
447
448 fprintf(stderr, "%s", msg);
449 fprintf(stderr, ": %s\n",
450 ERR_error_string(ssl_err, 0));
451
452 return -1;
453}
454
455static int kwb_load_rsa_key(const char *keydir, const char *name, RSA **p_rsa)
456{
457 char path[PATH_MAX];
458 RSA *rsa;
459 FILE *f;
460
461 if (!keydir)
462 keydir = ".";
463
464 snprintf(path, sizeof(path), "%s/%s.key", keydir, name);
465 f = fopen(path, "r");
466 if (!f) {
467 fprintf(stderr, "Couldn't open RSA private key: '%s': %s\n",
468 path, strerror(errno));
469 return -ENOENT;
470 }
471
472 rsa = PEM_read_RSAPrivateKey(f, 0, NULL, "");
473 if (!rsa) {
474 openssl_err("Failure reading private key");
475 fclose(f);
476 return -EPROTO;
477 }
478 fclose(f);
479 *p_rsa = rsa;
480
481 return 0;
482}
483
484static int kwb_load_cfg_key(struct image_tool_params *params,
485 unsigned int cfg_option, const char *key_name,
486 RSA **p_key)
487{
488 struct image_cfg_element *e_key;
489 RSA *key;
490 int res;
491
492 *p_key = NULL;
493
494 e_key = image_find_option(cfg_option);
495 if (!e_key) {
496 fprintf(stderr, "%s not configured\n", key_name);
497 return -ENOENT;
498 }
499
500 res = kwb_load_rsa_key(params->keydir, e_key->key_name, &key);
501 if (res < 0) {
502 fprintf(stderr, "Failed to load %s\n", key_name);
503 return -ENOENT;
504 }
505
506 *p_key = key;
507
508 return 0;
509}
510
511static int kwb_load_kak(struct image_tool_params *params, RSA **p_kak)
512{
513 return kwb_load_cfg_key(params, IMAGE_CFG_KAK, "KAK", p_kak);
514}
515
516static int kwb_load_csk(struct image_tool_params *params, RSA **p_csk)
517{
518 return kwb_load_cfg_key(params, IMAGE_CFG_CSK, "CSK", p_csk);
519}
520
521static int kwb_compute_pubkey_hash(struct pubkey_der_v1 *pk,
522 struct hash_v1 *hash)
523{
524 EVP_MD_CTX *ctx;
525 unsigned int key_size;
526 unsigned int hash_size;
527 int ret = 0;
528
529 if (!pk || !hash || pk->key[0] != 0x30 || pk->key[1] != 0x82)
530 return -EINVAL;
531
532 key_size = (pk->key[2] << 8) + pk->key[3] + 4;
533
534 ctx = EVP_MD_CTX_create();
535 if (!ctx)
536 return openssl_err("EVP context creation failed");
537
538 EVP_MD_CTX_init(ctx);
539 if (!EVP_DigestInit(ctx, EVP_sha256())) {
540 ret = openssl_err("Digest setup failed");
541 goto hash_err_ctx;
542 }
543
544 if (!EVP_DigestUpdate(ctx, pk->key, key_size)) {
545 ret = openssl_err("Hashing data failed");
546 goto hash_err_ctx;
547 }
548
549 if (!EVP_DigestFinal(ctx, hash->hash, &hash_size)) {
550 ret = openssl_err("Could not obtain hash");
551 goto hash_err_ctx;
552 }
553
554 EVP_MD_CTX_cleanup(ctx);
555
556hash_err_ctx:
557 EVP_MD_CTX_destroy(ctx);
558 return ret;
559}
560
561static int kwb_import_pubkey(RSA **key, struct pubkey_der_v1 *src, char *keyname)
562{
563 RSA *rsa;
564 const unsigned char *ptr;
565
566 if (!key || !src)
567 goto fail;
568
569 ptr = src->key;
570 rsa = d2i_RSAPublicKey(key, &ptr, sizeof(src->key));
571 if (!rsa) {
572 openssl_err("error decoding public key");
573 goto fail;
574 }
575
576 return 0;
577fail:
578 fprintf(stderr, "Failed to decode %s pubkey\n", keyname);
579 return -EINVAL;
580}
581
582static int kwb_export_pubkey(RSA *key, struct pubkey_der_v1 *dst, FILE *hashf,
583 char *keyname)
584{
585 int size_exp, size_mod, size_seq;
Jelle van der Waae15843b2017-05-08 21:31:20 +0200586 const BIGNUM *key_e, *key_n;
Mario Sixa1b6b0a2017-01-11 16:01:00 +0100587 uint8_t *cur;
588 char *errmsg = "Failed to encode %s\n";
589
Jelle van der Waae15843b2017-05-08 21:31:20 +0200590 RSA_get0_key(key, NULL, &key_e, NULL);
591 RSA_get0_key(key, &key_n, NULL, NULL);
592
593 if (!key || !key_e || !key_n || !dst) {
Mario Sixa1b6b0a2017-01-11 16:01:00 +0100594 fprintf(stderr, "export pk failed: (%p, %p, %p, %p)",
Jelle van der Waae15843b2017-05-08 21:31:20 +0200595 key, key_e, key_n, dst);
Mario Sixa1b6b0a2017-01-11 16:01:00 +0100596 fprintf(stderr, errmsg, keyname);
597 return -EINVAL;
598 }
599
600 /*
601 * According to the specs, the key should be PKCS#1 DER encoded.
602 * But unfortunately the really required encoding seems to be different;
603 * it violates DER...! (But it still conformes to BER.)
604 * (Length always in long form w/ 2 byte length code; no leading zero
605 * when MSB of first byte is set...)
606 * So we cannot use the encoding func provided by OpenSSL and have to
607 * do the encoding manually.
608 */
609
Jelle van der Waae15843b2017-05-08 21:31:20 +0200610 size_exp = BN_num_bytes(key_e);
611 size_mod = BN_num_bytes(key_n);
Mario Sixa1b6b0a2017-01-11 16:01:00 +0100612 size_seq = 4 + size_mod + 4 + size_exp;
613
614 if (size_mod > 256) {
615 fprintf(stderr, "export pk failed: wrong mod size: %d\n",
616 size_mod);
617 fprintf(stderr, errmsg, keyname);
618 return -EINVAL;
619 }
620
621 if (4 + size_seq > sizeof(dst->key)) {
Marek Behún3b5da642021-09-24 23:06:38 +0200622 fprintf(stderr, "export pk failed: seq too large (%d, %zu)\n",
Mario Sixa1b6b0a2017-01-11 16:01:00 +0100623 4 + size_seq, sizeof(dst->key));
624 fprintf(stderr, errmsg, keyname);
625 return -ENOBUFS;
626 }
627
628 cur = dst->key;
629
630 /* PKCS#1 (RFC3447) RSAPublicKey structure */
631 *cur++ = 0x30; /* SEQUENCE */
632 *cur++ = 0x82;
633 *cur++ = (size_seq >> 8) & 0xFF;
634 *cur++ = size_seq & 0xFF;
635 /* Modulus */
636 *cur++ = 0x02; /* INTEGER */
637 *cur++ = 0x82;
638 *cur++ = (size_mod >> 8) & 0xFF;
639 *cur++ = size_mod & 0xFF;
Jelle van der Waae15843b2017-05-08 21:31:20 +0200640 BN_bn2bin(key_n, cur);
Mario Sixa1b6b0a2017-01-11 16:01:00 +0100641 cur += size_mod;
642 /* Exponent */
643 *cur++ = 0x02; /* INTEGER */
644 *cur++ = 0x82;
645 *cur++ = (size_exp >> 8) & 0xFF;
646 *cur++ = size_exp & 0xFF;
Jelle van der Waae15843b2017-05-08 21:31:20 +0200647 BN_bn2bin(key_e, cur);
Mario Sixa1b6b0a2017-01-11 16:01:00 +0100648
649 if (hashf) {
650 struct hash_v1 pk_hash;
651 int i;
652 int ret = 0;
653
654 ret = kwb_compute_pubkey_hash(dst, &pk_hash);
655 if (ret < 0) {
656 fprintf(stderr, errmsg, keyname);
657 return ret;
658 }
659
660 fprintf(hashf, "SHA256 = ");
661 for (i = 0 ; i < sizeof(pk_hash.hash); ++i)
662 fprintf(hashf, "%02X", pk_hash.hash[i]);
663 fprintf(hashf, "\n");
664 }
665
666 return 0;
667}
668
Pali Rohár6eb20bb2022-01-12 18:20:35 +0100669static int kwb_sign(RSA *key, void *data, int datasz, struct sig_v1 *sig,
670 char *signame)
Mario Sixa1b6b0a2017-01-11 16:01:00 +0100671{
672 EVP_PKEY *evp_key;
673 EVP_MD_CTX *ctx;
674 unsigned int sig_size;
675 int size;
676 int ret = 0;
677
678 evp_key = EVP_PKEY_new();
679 if (!evp_key)
680 return openssl_err("EVP_PKEY object creation failed");
681
682 if (!EVP_PKEY_set1_RSA(evp_key, key)) {
683 ret = openssl_err("EVP key setup failed");
684 goto err_key;
685 }
686
687 size = EVP_PKEY_size(evp_key);
688 if (size > sizeof(sig->sig)) {
689 fprintf(stderr, "Buffer to small for signature (%d bytes)\n",
690 size);
691 ret = -ENOBUFS;
692 goto err_key;
693 }
694
695 ctx = EVP_MD_CTX_create();
696 if (!ctx) {
697 ret = openssl_err("EVP context creation failed");
698 goto err_key;
699 }
700 EVP_MD_CTX_init(ctx);
701 if (!EVP_SignInit(ctx, EVP_sha256())) {
702 ret = openssl_err("Signer setup failed");
703 goto err_ctx;
704 }
705
706 if (!EVP_SignUpdate(ctx, data, datasz)) {
707 ret = openssl_err("Signing data failed");
708 goto err_ctx;
709 }
710
711 if (!EVP_SignFinal(ctx, sig->sig, &sig_size, evp_key)) {
712 ret = openssl_err("Could not obtain signature");
713 goto err_ctx;
714 }
715
716 EVP_MD_CTX_cleanup(ctx);
717 EVP_MD_CTX_destroy(ctx);
718 EVP_PKEY_free(evp_key);
719
720 return 0;
721
722err_ctx:
723 EVP_MD_CTX_destroy(ctx);
724err_key:
725 EVP_PKEY_free(evp_key);
726 fprintf(stderr, "Failed to create %s signature\n", signame);
727 return ret;
728}
729
Pali Rohár6eb20bb2022-01-12 18:20:35 +0100730static int kwb_verify(RSA *key, void *data, int datasz, struct sig_v1 *sig,
731 char *signame)
Mario Sixa1b6b0a2017-01-11 16:01:00 +0100732{
733 EVP_PKEY *evp_key;
734 EVP_MD_CTX *ctx;
735 int size;
736 int ret = 0;
737
738 evp_key = EVP_PKEY_new();
739 if (!evp_key)
740 return openssl_err("EVP_PKEY object creation failed");
741
742 if (!EVP_PKEY_set1_RSA(evp_key, key)) {
743 ret = openssl_err("EVP key setup failed");
744 goto err_key;
745 }
746
747 size = EVP_PKEY_size(evp_key);
748 if (size > sizeof(sig->sig)) {
749 fprintf(stderr, "Invalid signature size (%d bytes)\n",
750 size);
751 ret = -EINVAL;
752 goto err_key;
753 }
754
755 ctx = EVP_MD_CTX_create();
756 if (!ctx) {
757 ret = openssl_err("EVP context creation failed");
758 goto err_key;
759 }
760 EVP_MD_CTX_init(ctx);
761 if (!EVP_VerifyInit(ctx, EVP_sha256())) {
762 ret = openssl_err("Verifier setup failed");
763 goto err_ctx;
764 }
765
766 if (!EVP_VerifyUpdate(ctx, data, datasz)) {
767 ret = openssl_err("Hashing data failed");
768 goto err_ctx;
769 }
770
Young Xiao22515122019-04-17 17:20:24 +0800771 if (EVP_VerifyFinal(ctx, sig->sig, sizeof(sig->sig), evp_key) != 1) {
Mario Sixa1b6b0a2017-01-11 16:01:00 +0100772 ret = openssl_err("Could not verify signature");
773 goto err_ctx;
774 }
775
776 EVP_MD_CTX_cleanup(ctx);
777 EVP_MD_CTX_destroy(ctx);
778 EVP_PKEY_free(evp_key);
779
780 return 0;
781
782err_ctx:
783 EVP_MD_CTX_destroy(ctx);
784err_key:
785 EVP_PKEY_free(evp_key);
786 fprintf(stderr, "Failed to verify %s signature\n", signame);
787 return ret;
788}
789
Pali Rohár6eb20bb2022-01-12 18:20:35 +0100790static int kwb_sign_and_verify(RSA *key, void *data, int datasz,
791 struct sig_v1 *sig, char *signame)
Mario Sixa1b6b0a2017-01-11 16:01:00 +0100792{
793 if (kwb_sign(key, data, datasz, sig, signame) < 0)
794 return -1;
795
796 if (kwb_verify(key, data, datasz, sig, signame) < 0)
797 return -1;
798
799 return 0;
800}
801
802
Pali Rohár6eb20bb2022-01-12 18:20:35 +0100803static int kwb_dump_fuse_cmds_38x(FILE *out, struct secure_hdr_v1 *sec_hdr)
Mario Sixa1b6b0a2017-01-11 16:01:00 +0100804{
805 struct hash_v1 kak_pub_hash;
806 struct image_cfg_element *e;
807 unsigned int fuse_line;
808 int i, idx;
809 uint8_t *ptr;
810 uint32_t val;
811 int ret = 0;
812
813 if (!out || !sec_hdr)
814 return -EINVAL;
815
816 ret = kwb_compute_pubkey_hash(&sec_hdr->kak, &kak_pub_hash);
817 if (ret < 0)
818 goto done;
819
820 fprintf(out, "# burn KAK pub key hash\n");
821 ptr = kak_pub_hash.hash;
822 for (fuse_line = 26; fuse_line <= 30; ++fuse_line) {
823 fprintf(out, "fuse prog -y %u 0 ", fuse_line);
824
825 for (i = 4; i-- > 0;)
826 fprintf(out, "%02hx", (ushort)ptr[i]);
827 ptr += 4;
828 fprintf(out, " 00");
829
830 if (fuse_line < 30) {
831 for (i = 3; i-- > 0;)
832 fprintf(out, "%02hx", (ushort)ptr[i]);
833 ptr += 3;
834 } else {
835 fprintf(out, "000000");
836 }
837
838 fprintf(out, " 1\n");
839 }
840
841 fprintf(out, "# burn CSK selection\n");
842
843 idx = image_get_csk_index();
844 if (idx < 0 || idx > 15) {
845 ret = -EINVAL;
846 goto done;
847 }
848 if (idx > 0) {
849 for (fuse_line = 31; fuse_line < 31 + idx; ++fuse_line)
850 fprintf(out, "fuse prog -y %u 0 00000001 00000000 1\n",
851 fuse_line);
852 } else {
853 fprintf(out, "# CSK index is 0; no mods needed\n");
854 }
855
856 e = image_find_option(IMAGE_CFG_BOX_ID);
857 if (e) {
858 fprintf(out, "# set box ID\n");
859 fprintf(out, "fuse prog -y 48 0 %08x 00000000 1\n", e->boxid);
860 }
861
862 e = image_find_option(IMAGE_CFG_FLASH_ID);
863 if (e) {
864 fprintf(out, "# set flash ID\n");
865 fprintf(out, "fuse prog -y 47 0 %08x 00000000 1\n", e->flashid);
866 }
867
868 fprintf(out, "# enable secure mode ");
869 fprintf(out, "(must be the last fuse line written)\n");
870
871 val = 1;
872 e = image_find_option(IMAGE_CFG_SEC_BOOT_DEV);
873 if (!e) {
874 fprintf(stderr, "ERROR: secured mode boot device not given\n");
875 ret = -EINVAL;
876 goto done;
877 }
878
879 if (e->sec_boot_dev > 0xff) {
880 fprintf(stderr, "ERROR: secured mode boot device invalid\n");
881 ret = -EINVAL;
882 goto done;
883 }
884
885 val |= (e->sec_boot_dev << 8);
886
887 fprintf(out, "fuse prog -y 24 0 %08x 0103e0a9 1\n", val);
888
889 fprintf(out, "# lock (unused) fuse lines (0-23)s\n");
890 for (fuse_line = 0; fuse_line < 24; ++fuse_line)
891 fprintf(out, "fuse prog -y %u 2 1\n", fuse_line);
892
893 fprintf(out, "# OK, that's all :-)\n");
894
895done:
896 return ret;
897}
898
899static int kwb_dump_fuse_cmds(struct secure_hdr_v1 *sec_hdr)
900{
901 int ret = 0;
902 struct image_cfg_element *e;
903
904 e = image_find_option(IMAGE_CFG_SEC_FUSE_DUMP);
905 if (!e)
906 return 0;
907
908 if (!strcmp(e->name, "a38x")) {
909 FILE *out = fopen("kwb_fuses_a38x.txt", "w+");
910
Heinrich Schuchardtf858bb22021-08-17 07:03:20 +0200911 if (!out) {
912 fprintf(stderr, "Couldn't open eFuse settings: '%s': %s\n",
913 "kwb_fuses_a38x.txt", strerror(errno));
914 return -ENOENT;
915 }
916
Mario Sixa1b6b0a2017-01-11 16:01:00 +0100917 kwb_dump_fuse_cmds_38x(out, sec_hdr);
918 fclose(out);
919 goto done;
920 }
921
922 ret = -ENOSYS;
923
924done:
925 return ret;
926}
927
Pali Rohár5cad2e62021-11-08 18:12:48 +0100928static size_t image_headersz_align(size_t headersz, uint8_t blockid)
929{
930 /*
931 * Header needs to be 4-byte aligned, which is already ensured by code
932 * above. Moreover UART images must have header aligned to 128 bytes
933 * (xmodem block size), NAND images to 256 bytes (ECC calculation),
934 * and SATA and SDIO images to 512 bytes (storage block size).
935 * Note that SPI images do not have to have header size aligned
936 * to 256 bytes because it is possible to read from SPI storage from
937 * any offset (read offset does not have to be aligned to block size).
938 */
939 if (blockid == IBR_HDR_UART_ID)
940 return ALIGN(headersz, 128);
941 else if (blockid == IBR_HDR_NAND_ID)
942 return ALIGN(headersz, 256);
943 else if (blockid == IBR_HDR_SATA_ID || blockid == IBR_HDR_SDIO_ID)
944 return ALIGN(headersz, 512);
945 else
946 return headersz;
947}
948
Pali Rohár851114b2021-11-08 18:12:50 +0100949static size_t image_headersz_v0(int *hasext)
950{
951 size_t headersz;
952
953 headersz = sizeof(struct main_hdr_v0);
954 if (image_count_options(IMAGE_CFG_DATA) > 0) {
955 headersz += sizeof(struct ext_hdr_v0);
956 if (hasext)
957 *hasext = 1;
958 }
959
960 return image_headersz_align(headersz, image_get_bootfrom());
961}
962
Stefan Roese4acd2d22014-10-22 12:13:23 +0200963static void *image_create_v0(size_t *imagesz, struct image_tool_params *params,
964 int payloadsz)
Prafulla Wadaskaraa0c7a82009-09-07 15:05:02 +0530965{
Stefan Roese4acd2d22014-10-22 12:13:23 +0200966 struct image_cfg_element *e;
967 size_t headersz;
968 struct main_hdr_v0 *main_hdr;
Mario Six885fba12017-01-11 16:00:55 +0100969 uint8_t *image;
Stefan Roese4acd2d22014-10-22 12:13:23 +0200970 int has_ext = 0;
Prafulla Wadaskaraa0c7a82009-09-07 15:05:02 +0530971
Stefan Roese4acd2d22014-10-22 12:13:23 +0200972 /*
973 * Calculate the size of the header and the size of the
974 * payload
975 */
Pali Rohár851114b2021-11-08 18:12:50 +0100976 headersz = image_headersz_v0(&has_ext);
Prafulla Wadaskaraa0c7a82009-09-07 15:05:02 +0530977
Stefan Roese4acd2d22014-10-22 12:13:23 +0200978 image = malloc(headersz);
979 if (!image) {
980 fprintf(stderr, "Cannot allocate memory for image\n");
981 return NULL;
982 }
Prafulla Wadaskaraa0c7a82009-09-07 15:05:02 +0530983
Stefan Roese4acd2d22014-10-22 12:13:23 +0200984 memset(image, 0, headersz);
Prafulla Wadaskaraa0c7a82009-09-07 15:05:02 +0530985
Mario Six885fba12017-01-11 16:00:55 +0100986 main_hdr = (struct main_hdr_v0 *)image;
Prafulla Wadaskaraa0c7a82009-09-07 15:05:02 +0530987
Stefan Roese4acd2d22014-10-22 12:13:23 +0200988 /* Fill in the main header */
Reinhard Pfaua8840dc2015-11-29 15:48:25 +0100989 main_hdr->blocksize =
Pali Roháre23ad5d2021-11-08 18:12:47 +0100990 cpu_to_le32(payloadsz);
Reinhard Pfaua8840dc2015-11-29 15:48:25 +0100991 main_hdr->srcaddr = cpu_to_le32(headersz);
Stefan Roese4acd2d22014-10-22 12:13:23 +0200992 main_hdr->ext = has_ext;
Pali Rohár01bdac62021-11-08 18:12:42 +0100993 main_hdr->version = 0;
Reinhard Pfaua8840dc2015-11-29 15:48:25 +0100994 main_hdr->destaddr = cpu_to_le32(params->addr);
995 main_hdr->execaddr = cpu_to_le32(params->ep);
Pali Rohárd1547b32021-11-08 18:12:43 +0100996 main_hdr->blockid = image_get_bootfrom();
Prafulla Wadaskaraa0c7a82009-09-07 15:05:02 +0530997
Stefan Roese4acd2d22014-10-22 12:13:23 +0200998 e = image_find_option(IMAGE_CFG_NAND_ECC_MODE);
999 if (e)
1000 main_hdr->nandeccmode = e->nandeccmode;
Pali Rohára6661a02022-02-17 10:43:38 +01001001 e = image_find_option(IMAGE_CFG_NAND_BLKSZ);
1002 if (e)
1003 main_hdr->nandblocksize = e->nandblksz / (64 * 1024);
Stefan Roese4acd2d22014-10-22 12:13:23 +02001004 e = image_find_option(IMAGE_CFG_NAND_PAGESZ);
1005 if (e)
Reinhard Pfaua8840dc2015-11-29 15:48:25 +01001006 main_hdr->nandpagesize = cpu_to_le16(e->nandpagesz);
Pali Rohára6661a02022-02-17 10:43:38 +01001007 e = image_find_option(IMAGE_CFG_NAND_BADBLK_LOCATION);
1008 if (e)
1009 main_hdr->nandbadblklocation = e->nandbadblklocation;
Stefan Roese4acd2d22014-10-22 12:13:23 +02001010 main_hdr->checksum = image_checksum8(image,
1011 sizeof(struct main_hdr_v0));
Prafulla Wadaskaraa0c7a82009-09-07 15:05:02 +05301012
Pali Rohár5c617102021-11-08 18:12:51 +01001013 /*
1014 * For SATA srcaddr is specified in number of sectors starting from
1015 * sector 0. The main header is stored at sector number 1.
1016 * This expects the sector size to be 512 bytes.
1017 * Header size is already aligned.
1018 */
1019 if (main_hdr->blockid == IBR_HDR_SATA_ID)
1020 main_hdr->srcaddr = cpu_to_le32(headersz / 512 + 1);
1021
1022 /*
1023 * For SDIO srcaddr is specified in number of sectors starting from
1024 * sector 0. The main header is stored at sector number 0.
1025 * This expects sector size to be 512 bytes.
1026 * Header size is already aligned.
1027 */
1028 if (main_hdr->blockid == IBR_HDR_SDIO_ID)
1029 main_hdr->srcaddr = cpu_to_le32(headersz / 512);
1030
1031 /* For PCIe srcaddr is not used and must be set to 0xFFFFFFFF. */
1032 if (main_hdr->blockid == IBR_HDR_PEX_ID)
1033 main_hdr->srcaddr = cpu_to_le32(0xFFFFFFFF);
1034
Stefan Roese4acd2d22014-10-22 12:13:23 +02001035 /* Generate the ext header */
1036 if (has_ext) {
Mario Sixe89016c2017-01-11 16:00:56 +01001037 struct ext_hdr_v0 *ext_hdr;
Stefan Roese4acd2d22014-10-22 12:13:23 +02001038 int cfgi, datai;
Prafulla Wadaskaraa0c7a82009-09-07 15:05:02 +05301039
Mario Six885fba12017-01-11 16:00:55 +01001040 ext_hdr = (struct ext_hdr_v0 *)
1041 (image + sizeof(struct main_hdr_v0));
Reinhard Pfaua8840dc2015-11-29 15:48:25 +01001042 ext_hdr->offset = cpu_to_le32(0x40);
Prafulla Wadaskaraa0c7a82009-09-07 15:05:02 +05301043
Stefan Roese4acd2d22014-10-22 12:13:23 +02001044 for (cfgi = 0, datai = 0; cfgi < cfgn; cfgi++) {
1045 e = &image_cfg[cfgi];
1046 if (e->type != IMAGE_CFG_DATA)
1047 continue;
1048
Reinhard Pfaua8840dc2015-11-29 15:48:25 +01001049 ext_hdr->rcfg[datai].raddr =
1050 cpu_to_le32(e->regdata.raddr);
1051 ext_hdr->rcfg[datai].rdata =
1052 cpu_to_le32(e->regdata.rdata);
Stefan Roese4acd2d22014-10-22 12:13:23 +02001053 datai++;
Prafulla Wadaskaraa0c7a82009-09-07 15:05:02 +05301054 }
Prafulla Wadaskaraa0c7a82009-09-07 15:05:02 +05301055
Stefan Roese4acd2d22014-10-22 12:13:23 +02001056 ext_hdr->checksum = image_checksum8(ext_hdr,
1057 sizeof(struct ext_hdr_v0));
Prafulla Wadaskaraa0c7a82009-09-07 15:05:02 +05301058 }
1059
Stefan Roese4acd2d22014-10-22 12:13:23 +02001060 *imagesz = headersz;
1061 return image;
Prafulla Wadaskaraa0c7a82009-09-07 15:05:02 +05301062}
1063
Mario Sixe93cf532017-01-11 16:00:57 +01001064static size_t image_headersz_v1(int *hasext)
Prafulla Wadaskaraa0c7a82009-09-07 15:05:02 +05301065{
Pali Rohár0aca27e2022-01-12 18:20:41 +01001066 struct image_cfg_element *e;
Pali Rohár02ba70a2021-07-23 11:14:11 +02001067 unsigned int count;
Stefan Roese4acd2d22014-10-22 12:13:23 +02001068 size_t headersz;
Pali Rohár0aca27e2022-01-12 18:20:41 +01001069 int cpu_sheeva;
1070 struct stat s;
Pali Rohárd9fb82c2021-07-23 11:14:09 +02001071 int cfgi;
Pali Rohár0aca27e2022-01-12 18:20:41 +01001072 int ret;
Prafulla Wadaskaraa0c7a82009-09-07 15:05:02 +05301073
Stefan Roese4acd2d22014-10-22 12:13:23 +02001074 /*
1075 * Calculate the size of the header and the size of the
1076 * payload
1077 */
1078 headersz = sizeof(struct main_hdr_v1);
Prafulla Wadaskaraa0c7a82009-09-07 15:05:02 +05301079
Pali Roháre58f08b2021-10-21 16:46:07 +02001080 if (image_get_csk_index() >= 0) {
1081 headersz += sizeof(struct secure_hdr_v1);
1082 if (hasext)
1083 *hasext = 1;
1084 }
1085
Pali Rohár0aca27e2022-01-12 18:20:41 +01001086 cpu_sheeva = image_is_cpu_sheeva();
1087
Pali Rohárd737d5d2022-01-12 18:20:37 +01001088 count = 0;
1089 for (cfgi = 0; cfgi < cfgn; cfgi++) {
1090 e = &image_cfg[cfgi];
1091
1092 if (e->type == IMAGE_CFG_DATA)
1093 count++;
1094
Pali Rohár3db9c412022-01-12 18:20:38 +01001095 if (e->type == IMAGE_CFG_DATA_DELAY ||
1096 (e->type == IMAGE_CFG_BINARY && count > 0)) {
Pali Rohárd737d5d2022-01-12 18:20:37 +01001097 headersz += sizeof(struct register_set_hdr_v1) + 8 * count + 4;
1098 count = 0;
1099 }
Pali Rohár02ba70a2021-07-23 11:14:11 +02001100
Pali Rohár0aca27e2022-01-12 18:20:41 +01001101 if (e->type != IMAGE_CFG_BINARY)
Pali Rohárd9fb82c2021-07-23 11:14:09 +02001102 continue;
1103
Pali Rohár0aca27e2022-01-12 18:20:41 +01001104 ret = stat(e->binary.file, &s);
Stefan Roese4acd2d22014-10-22 12:13:23 +02001105 if (ret < 0) {
Andreas Bießmanne5f1a582014-10-24 23:39:11 +02001106 char cwd[PATH_MAX];
1107 char *dir = cwd;
1108
1109 memset(cwd, 0, sizeof(cwd));
1110 if (!getcwd(cwd, sizeof(cwd))) {
1111 dir = "current working directory";
1112 perror("getcwd() failed");
1113 }
1114
Stefan Roese4acd2d22014-10-22 12:13:23 +02001115 fprintf(stderr,
1116 "Didn't find the file '%s' in '%s' which is mandatory to generate the image\n"
1117 "This file generally contains the DDR3 training code, and should be extracted from an existing bootable\n"
Pali Rohár107d5872022-02-17 10:43:39 +01001118 "image for your board. Use 'dumpimage -T kwbimage -p 1' to extract it from an existing image.\n",
Pali Rohár0aca27e2022-01-12 18:20:41 +01001119 e->binary.file, dir);
Stefan Roese4acd2d22014-10-22 12:13:23 +02001120 return 0;
1121 }
1122
Pali Roháre58f08b2021-10-21 16:46:07 +02001123 headersz += sizeof(struct opt_hdr_v1) + sizeof(uint32_t) +
Pali Rohár0aca27e2022-01-12 18:20:41 +01001124 (e->binary.nargs) * sizeof(uint32_t);
1125
1126 if (e->binary.loadaddr) {
1127 /*
1128 * BootROM loads kwbimage header (in which the
1129 * executable code is also stored) to address
1130 * 0x40004000 or 0x40000000. Thus there is
1131 * restriction for the load address of the N-th
1132 * BINARY image.
1133 */
1134 unsigned int base_addr, low_addr, high_addr;
1135
1136 base_addr = cpu_sheeva ? 0x40004000 : 0x40000000;
1137 low_addr = base_addr + headersz;
1138 high_addr = low_addr +
1139 (BINARY_MAX_ARGS - e->binary.nargs) * sizeof(uint32_t);
1140
1141 if (cpu_sheeva && e->binary.loadaddr % 16) {
1142 fprintf(stderr,
1143 "Invalid LOAD_ADDRESS 0x%08x for BINARY %s with %d args.\n"
1144 "Address for CPU SHEEVA must be 16-byte aligned.\n",
1145 e->binary.loadaddr, e->binary.file, e->binary.nargs);
1146 return 0;
1147 }
1148
1149 if (e->binary.loadaddr % 4 || e->binary.loadaddr < low_addr ||
1150 e->binary.loadaddr > high_addr) {
1151 fprintf(stderr,
1152 "Invalid LOAD_ADDRESS 0x%08x for BINARY %s with %d args.\n"
1153 "Address must be 4-byte aligned and in range 0x%08x-0x%08x.\n",
1154 e->binary.loadaddr, e->binary.file,
1155 e->binary.nargs, low_addr, high_addr);
1156 return 0;
1157 }
1158 headersz = e->binary.loadaddr - base_addr;
Pali Rohárbdf8c9f2022-01-12 18:20:46 +01001159 } else if (cpu_sheeva) {
Pali Rohár0aca27e2022-01-12 18:20:41 +01001160 headersz = ALIGN(headersz, 16);
Pali Rohárbdf8c9f2022-01-12 18:20:46 +01001161 } else {
1162 headersz = ALIGN(headersz, 4);
Pali Rohár0aca27e2022-01-12 18:20:41 +01001163 }
1164
Pali Roháre58f08b2021-10-21 16:46:07 +02001165 headersz += ALIGN(s.st_size, 4) + sizeof(uint32_t);
Mario Sixa1b6b0a2017-01-11 16:01:00 +01001166 if (hasext)
1167 *hasext = 1;
1168 }
Mario Sixa1b6b0a2017-01-11 16:01:00 +01001169
Pali Rohár0aca27e2022-01-12 18:20:41 +01001170 if (count > 0)
1171 headersz += sizeof(struct register_set_hdr_v1) + 8 * count + 4;
1172
Pali Rohár5cad2e62021-11-08 18:12:48 +01001173 return image_headersz_align(headersz, image_get_bootfrom());
Stefan Roese4acd2d22014-10-22 12:13:23 +02001174}
1175
Pali Rohár6eb20bb2022-01-12 18:20:35 +01001176static int add_binary_header_v1(uint8_t **cur, uint8_t **next_ext,
1177 struct image_cfg_element *binarye,
1178 struct main_hdr_v1 *main_hdr)
Mario Six79066ef2017-01-11 16:00:58 +01001179{
Pali Rohárd9fb82c2021-07-23 11:14:09 +02001180 struct opt_hdr_v1 *hdr = (struct opt_hdr_v1 *)*cur;
Pali Rohár0aca27e2022-01-12 18:20:41 +01001181 uint32_t base_addr;
Pali Roháre58f08b2021-10-21 16:46:07 +02001182 uint32_t add_args;
1183 uint32_t offset;
Mario Six79066ef2017-01-11 16:00:58 +01001184 uint32_t *args;
1185 size_t binhdrsz;
Pali Rohár0aca27e2022-01-12 18:20:41 +01001186 int cpu_sheeva;
Mario Six79066ef2017-01-11 16:00:58 +01001187 struct stat s;
1188 int argi;
1189 FILE *bin;
1190 int ret;
1191
Mario Six79066ef2017-01-11 16:00:58 +01001192 hdr->headertype = OPT_HDR_V1_BINARY_TYPE;
1193
1194 bin = fopen(binarye->binary.file, "r");
1195 if (!bin) {
1196 fprintf(stderr, "Cannot open binary file %s\n",
1197 binarye->binary.file);
1198 return -1;
1199 }
1200
Mario Six1f6c8a52017-02-13 10:11:55 +01001201 if (fstat(fileno(bin), &s)) {
1202 fprintf(stderr, "Cannot stat binary file %s\n",
1203 binarye->binary.file);
1204 goto err_close;
1205 }
Mario Six79066ef2017-01-11 16:00:58 +01001206
Pali Rohárd9fb82c2021-07-23 11:14:09 +02001207 *cur += sizeof(struct opt_hdr_v1);
Mario Six79066ef2017-01-11 16:00:58 +01001208
Pali Rohárd9fb82c2021-07-23 11:14:09 +02001209 args = (uint32_t *)*cur;
Mario Six79066ef2017-01-11 16:00:58 +01001210 *args = cpu_to_le32(binarye->binary.nargs);
1211 args++;
1212 for (argi = 0; argi < binarye->binary.nargs; argi++)
1213 args[argi] = cpu_to_le32(binarye->binary.args[argi]);
1214
Pali Rohárd9fb82c2021-07-23 11:14:09 +02001215 *cur += (binarye->binary.nargs + 1) * sizeof(uint32_t);
Mario Six79066ef2017-01-11 16:00:58 +01001216
Pali Roháre58f08b2021-10-21 16:46:07 +02001217 /*
Pali Rohárbdf8c9f2022-01-12 18:20:46 +01001218 * ARM executable code inside the BIN header on platforms with Sheeva
1219 * CPU (A370 and AXP) must always be aligned with the 128-bit boundary.
Pali Rohár0aca27e2022-01-12 18:20:41 +01001220 * In the case when this code is not position independent (e.g. ARM
1221 * SPL), it must be placed at fixed load and execute address.
Pali Roháre58f08b2021-10-21 16:46:07 +02001222 * This requirement can be met by inserting dummy arguments into
1223 * BIN header, if needed.
1224 */
Pali Rohár0aca27e2022-01-12 18:20:41 +01001225 cpu_sheeva = image_is_cpu_sheeva();
1226 base_addr = cpu_sheeva ? 0x40004000 : 0x40000000;
Pali Roháre58f08b2021-10-21 16:46:07 +02001227 offset = *cur - (uint8_t *)main_hdr;
Pali Rohár0aca27e2022-01-12 18:20:41 +01001228 if (binarye->binary.loadaddr)
1229 add_args = (binarye->binary.loadaddr - base_addr - offset) / sizeof(uint32_t);
Pali Rohárbdf8c9f2022-01-12 18:20:46 +01001230 else if (cpu_sheeva)
Pali Rohár0aca27e2022-01-12 18:20:41 +01001231 add_args = ((16 - offset % 16) % 16) / sizeof(uint32_t);
Pali Rohárbdf8c9f2022-01-12 18:20:46 +01001232 else
1233 add_args = 0;
Pali Roháre58f08b2021-10-21 16:46:07 +02001234 if (add_args) {
1235 *(args - 1) = cpu_to_le32(binarye->binary.nargs + add_args);
1236 *cur += add_args * sizeof(uint32_t);
1237 }
1238
Pali Rohárd9fb82c2021-07-23 11:14:09 +02001239 ret = fread(*cur, s.st_size, 1, bin);
Mario Six79066ef2017-01-11 16:00:58 +01001240 if (ret != 1) {
1241 fprintf(stderr,
1242 "Could not read binary image %s\n",
1243 binarye->binary.file);
Mario Six1f6c8a52017-02-13 10:11:55 +01001244 goto err_close;
Mario Six79066ef2017-01-11 16:00:58 +01001245 }
1246
1247 fclose(bin);
1248
Pali Rohárd9fb82c2021-07-23 11:14:09 +02001249 *cur += ALIGN(s.st_size, 4);
Mario Six79066ef2017-01-11 16:00:58 +01001250
Pali Rohárd9fb82c2021-07-23 11:14:09 +02001251 *((uint32_t *)*cur) = 0x00000000;
1252 **next_ext = 1;
1253 *next_ext = *cur;
Mario Six79066ef2017-01-11 16:00:58 +01001254
Pali Rohárd9fb82c2021-07-23 11:14:09 +02001255 *cur += sizeof(uint32_t);
Mario Six79066ef2017-01-11 16:00:58 +01001256
Pali Roháre58f08b2021-10-21 16:46:07 +02001257 binhdrsz = sizeof(struct opt_hdr_v1) +
1258 (binarye->binary.nargs + add_args + 2) * sizeof(uint32_t) +
1259 ALIGN(s.st_size, 4);
1260 hdr->headersz_lsb = cpu_to_le16(binhdrsz & 0xFFFF);
1261 hdr->headersz_msb = (binhdrsz & 0xFFFF0000) >> 16;
1262
Mario Six79066ef2017-01-11 16:00:58 +01001263 return 0;
Mario Six1f6c8a52017-02-13 10:11:55 +01001264
1265err_close:
1266 fclose(bin);
1267
1268 return -1;
Mario Six79066ef2017-01-11 16:00:58 +01001269}
1270
Pali Rohár6eb20bb2022-01-12 18:20:35 +01001271static int export_pub_kak_hash(RSA *kak, struct secure_hdr_v1 *secure_hdr)
Mario Sixa1b6b0a2017-01-11 16:01:00 +01001272{
1273 FILE *hashf;
1274 int res;
1275
1276 hashf = fopen("pub_kak_hash.txt", "w");
Heinrich Schuchardtf858bb22021-08-17 07:03:20 +02001277 if (!hashf) {
1278 fprintf(stderr, "Couldn't open hash file: '%s': %s\n",
1279 "pub_kak_hash.txt", strerror(errno));
1280 return 1;
1281 }
Mario Sixa1b6b0a2017-01-11 16:01:00 +01001282
1283 res = kwb_export_pubkey(kak, &secure_hdr->kak, hashf, "KAK");
1284
1285 fclose(hashf);
1286
1287 return res < 0 ? 1 : 0;
1288}
1289
Pali Rohár6eb20bb2022-01-12 18:20:35 +01001290static int kwb_sign_csk_with_kak(struct image_tool_params *params,
1291 struct secure_hdr_v1 *secure_hdr, RSA *csk)
Mario Sixa1b6b0a2017-01-11 16:01:00 +01001292{
1293 RSA *kak = NULL;
1294 RSA *kak_pub = NULL;
1295 int csk_idx = image_get_csk_index();
1296 struct sig_v1 tmp_sig;
1297
Heinrich Schuchardtf0317d72021-08-17 07:11:58 +02001298 if (csk_idx < 0 || csk_idx > 15) {
Mario Sixa1b6b0a2017-01-11 16:01:00 +01001299 fprintf(stderr, "Invalid CSK index %d\n", csk_idx);
1300 return 1;
1301 }
1302
1303 if (kwb_load_kak(params, &kak) < 0)
1304 return 1;
1305
1306 if (export_pub_kak_hash(kak, secure_hdr))
1307 return 1;
1308
1309 if (kwb_import_pubkey(&kak_pub, &secure_hdr->kak, "KAK") < 0)
1310 return 1;
1311
1312 if (kwb_export_pubkey(csk, &secure_hdr->csk[csk_idx], NULL, "CSK") < 0)
1313 return 1;
1314
1315 if (kwb_sign_and_verify(kak, &secure_hdr->csk,
1316 sizeof(secure_hdr->csk) +
1317 sizeof(secure_hdr->csksig),
1318 &tmp_sig, "CSK") < 0)
1319 return 1;
1320
1321 if (kwb_verify(kak_pub, &secure_hdr->csk,
1322 sizeof(secure_hdr->csk) +
1323 sizeof(secure_hdr->csksig),
1324 &tmp_sig, "CSK (2)") < 0)
1325 return 1;
1326
1327 secure_hdr->csksig = tmp_sig;
1328
1329 return 0;
1330}
1331
Pali Rohár6eb20bb2022-01-12 18:20:35 +01001332static int add_secure_header_v1(struct image_tool_params *params, uint8_t *ptr,
1333 int payloadsz, size_t headersz, uint8_t *image,
1334 struct secure_hdr_v1 *secure_hdr)
Mario Sixa1b6b0a2017-01-11 16:01:00 +01001335{
1336 struct image_cfg_element *e_jtagdelay;
1337 struct image_cfg_element *e_boxid;
1338 struct image_cfg_element *e_flashid;
1339 RSA *csk = NULL;
1340 unsigned char *image_ptr;
1341 size_t image_size;
1342 struct sig_v1 tmp_sig;
1343 bool specialized_img = image_get_spezialized_img();
1344
1345 kwb_msg("Create secure header content\n");
1346
1347 e_jtagdelay = image_find_option(IMAGE_CFG_JTAG_DELAY);
1348 e_boxid = image_find_option(IMAGE_CFG_BOX_ID);
1349 e_flashid = image_find_option(IMAGE_CFG_FLASH_ID);
1350
1351 if (kwb_load_csk(params, &csk) < 0)
1352 return 1;
1353
1354 secure_hdr->headertype = OPT_HDR_V1_SECURE_TYPE;
1355 secure_hdr->headersz_msb = 0;
1356 secure_hdr->headersz_lsb = cpu_to_le16(sizeof(struct secure_hdr_v1));
1357 if (e_jtagdelay)
1358 secure_hdr->jtag_delay = e_jtagdelay->jtag_delay;
1359 if (e_boxid && specialized_img)
1360 secure_hdr->boxid = cpu_to_le32(e_boxid->boxid);
1361 if (e_flashid && specialized_img)
1362 secure_hdr->flashid = cpu_to_le32(e_flashid->flashid);
1363
1364 if (kwb_sign_csk_with_kak(params, secure_hdr, csk))
1365 return 1;
1366
1367 image_ptr = ptr + headersz;
1368 image_size = payloadsz - headersz;
1369
1370 if (kwb_sign_and_verify(csk, image_ptr, image_size,
1371 &secure_hdr->imgsig, "image") < 0)
1372 return 1;
1373
1374 if (kwb_sign_and_verify(csk, image, headersz, &tmp_sig, "header") < 0)
1375 return 1;
1376
1377 secure_hdr->hdrsig = tmp_sig;
1378
1379 kwb_dump_fuse_cmds(secure_hdr);
1380
1381 return 0;
1382}
Mario Sixa1b6b0a2017-01-11 16:01:00 +01001383
Pali Rohár9ac1def2022-01-12 18:20:36 +01001384static void finish_register_set_header_v1(uint8_t **cur, uint8_t **next_ext,
1385 struct register_set_hdr_v1 *register_set_hdr,
1386 int *datai, uint8_t delay)
1387{
1388 int size = sizeof(struct register_set_hdr_v1) + 8 * (*datai) + 4;
1389
1390 register_set_hdr->headertype = OPT_HDR_V1_REGISTER_TYPE;
1391 register_set_hdr->headersz_lsb = cpu_to_le16(size & 0xFFFF);
1392 register_set_hdr->headersz_msb = size >> 16;
1393 register_set_hdr->data[*datai].last_entry.delay = delay;
1394 *cur += size;
1395 **next_ext = 1;
1396 *next_ext = &register_set_hdr->data[*datai].last_entry.next;
1397 *datai = 0;
1398}
1399
Stefan Roese4acd2d22014-10-22 12:13:23 +02001400static void *image_create_v1(size_t *imagesz, struct image_tool_params *params,
Mario Sixa1b6b0a2017-01-11 16:01:00 +01001401 uint8_t *ptr, int payloadsz)
Stefan Roese4acd2d22014-10-22 12:13:23 +02001402{
Mario Six79066ef2017-01-11 16:00:58 +01001403 struct image_cfg_element *e;
Stefan Roese4acd2d22014-10-22 12:13:23 +02001404 struct main_hdr_v1 *main_hdr;
Pali Rohár2b0980c2021-11-08 18:12:49 +01001405 struct opt_hdr_v1 *ohdr;
Pali Rohár02ba70a2021-07-23 11:14:11 +02001406 struct register_set_hdr_v1 *register_set_hdr;
Mario Sixa1b6b0a2017-01-11 16:01:00 +01001407 struct secure_hdr_v1 *secure_hdr = NULL;
Stefan Roese4acd2d22014-10-22 12:13:23 +02001408 size_t headersz;
Mario Six885fba12017-01-11 16:00:55 +01001409 uint8_t *image, *cur;
Stefan Roese4acd2d22014-10-22 12:13:23 +02001410 int hasext = 0;
Mario Sixa1b6b0a2017-01-11 16:01:00 +01001411 uint8_t *next_ext = NULL;
Pali Rohár9ac1def2022-01-12 18:20:36 +01001412 int cfgi, datai;
Pali Rohár3db9c412022-01-12 18:20:38 +01001413 uint8_t delay;
Stefan Roese4acd2d22014-10-22 12:13:23 +02001414
1415 /*
1416 * Calculate the size of the header and the size of the
1417 * payload
1418 */
Mario Sixe93cf532017-01-11 16:00:57 +01001419 headersz = image_headersz_v1(&hasext);
Stefan Roese4acd2d22014-10-22 12:13:23 +02001420 if (headersz == 0)
1421 return NULL;
1422
1423 image = malloc(headersz);
1424 if (!image) {
1425 fprintf(stderr, "Cannot allocate memory for image\n");
1426 return NULL;
1427 }
1428
1429 memset(image, 0, headersz);
1430
Mario Six885fba12017-01-11 16:00:55 +01001431 main_hdr = (struct main_hdr_v1 *)image;
Mario Sixa1b6b0a2017-01-11 16:01:00 +01001432 cur = image;
1433 cur += sizeof(struct main_hdr_v1);
1434 next_ext = &main_hdr->ext;
Stefan Roese4acd2d22014-10-22 12:13:23 +02001435
1436 /* Fill the main header */
Reinhard Pfaua8840dc2015-11-29 15:48:25 +01001437 main_hdr->blocksize =
Pali Roháre23ad5d2021-11-08 18:12:47 +01001438 cpu_to_le32(payloadsz);
Reinhard Pfaua8840dc2015-11-29 15:48:25 +01001439 main_hdr->headersz_lsb = cpu_to_le16(headersz & 0xFFFF);
Stefan Roese4acd2d22014-10-22 12:13:23 +02001440 main_hdr->headersz_msb = (headersz & 0xFFFF0000) >> 16;
Pali Rohárcc3443f2021-07-23 11:14:06 +02001441 main_hdr->destaddr = cpu_to_le32(params->addr);
Reinhard Pfaua8840dc2015-11-29 15:48:25 +01001442 main_hdr->execaddr = cpu_to_le32(params->ep);
1443 main_hdr->srcaddr = cpu_to_le32(headersz);
Stefan Roese4acd2d22014-10-22 12:13:23 +02001444 main_hdr->ext = hasext;
1445 main_hdr->version = 1;
Pali Rohárd1547b32021-11-08 18:12:43 +01001446 main_hdr->blockid = image_get_bootfrom();
1447
Stefan Roese4acd2d22014-10-22 12:13:23 +02001448 e = image_find_option(IMAGE_CFG_NAND_BLKSZ);
1449 if (e)
1450 main_hdr->nandblocksize = e->nandblksz / (64 * 1024);
Pali Rohár2fdba4f2021-10-22 12:37:46 +02001451 e = image_find_option(IMAGE_CFG_NAND_PAGESZ);
1452 if (e)
1453 main_hdr->nandpagesize = cpu_to_le16(e->nandpagesz);
Stefan Roese4acd2d22014-10-22 12:13:23 +02001454 e = image_find_option(IMAGE_CFG_NAND_BADBLK_LOCATION);
1455 if (e)
1456 main_hdr->nandbadblklocation = e->nandbadblklocation;
Chris Packham4bdb5472016-11-09 22:07:45 +13001457 e = image_find_option(IMAGE_CFG_BAUDRATE);
1458 if (e)
Pali Rohár12f2c032021-11-08 18:12:41 +01001459 main_hdr->options |= baudrate_to_option(e->baudrate);
1460 e = image_find_option(IMAGE_CFG_UART_PORT);
1461 if (e)
1462 main_hdr->options |= (e->uart_port & 3) << 3;
1463 e = image_find_option(IMAGE_CFG_UART_MPP);
1464 if (e)
1465 main_hdr->options |= (e->uart_mpp & 7) << 5;
Chris Packham2611c052016-11-09 22:21:45 +13001466 e = image_find_option(IMAGE_CFG_DEBUG);
1467 if (e)
1468 main_hdr->flags = e->debug ? 0x1 : 0;
Stefan Roese4acd2d22014-10-22 12:13:23 +02001469
Pali Rohár501a54a2021-07-23 11:13:59 +02001470 /*
1471 * For SATA srcaddr is specified in number of sectors starting from
1472 * sector 0. The main header is stored at sector number 1.
1473 * This expects the sector size to be 512 bytes.
1474 * Header size is already aligned.
1475 */
1476 if (main_hdr->blockid == IBR_HDR_SATA_ID)
1477 main_hdr->srcaddr = cpu_to_le32(headersz / 512 + 1);
1478
1479 /*
1480 * For SDIO srcaddr is specified in number of sectors starting from
1481 * sector 0. The main header is stored at sector number 0.
1482 * This expects sector size to be 512 bytes.
1483 * Header size is already aligned.
1484 */
1485 if (main_hdr->blockid == IBR_HDR_SDIO_ID)
1486 main_hdr->srcaddr = cpu_to_le32(headersz / 512);
1487
1488 /* For PCIe srcaddr is not used and must be set to 0xFFFFFFFF. */
1489 if (main_hdr->blockid == IBR_HDR_PEX_ID)
1490 main_hdr->srcaddr = cpu_to_le32(0xFFFFFFFF);
1491
Mario Sixa1b6b0a2017-01-11 16:01:00 +01001492 if (image_get_csk_index() >= 0) {
1493 /*
1494 * only reserve the space here; we fill the header later since
1495 * we need the header to be complete to compute the signatures
1496 */
1497 secure_hdr = (struct secure_hdr_v1 *)cur;
1498 cur += sizeof(struct secure_hdr_v1);
Pali Rohárd9fb82c2021-07-23 11:14:09 +02001499 *next_ext = 1;
Mario Sixa1b6b0a2017-01-11 16:01:00 +01001500 next_ext = &secure_hdr->next;
1501 }
Mario Sixa1b6b0a2017-01-11 16:01:00 +01001502
Pali Rohár02ba70a2021-07-23 11:14:11 +02001503 datai = 0;
Pali Rohár02ba70a2021-07-23 11:14:11 +02001504 for (cfgi = 0; cfgi < cfgn; cfgi++) {
1505 e = &image_cfg[cfgi];
Pali Rohárf63c5832021-07-23 11:14:12 +02001506 if (e->type != IMAGE_CFG_DATA &&
Pali Rohár3db9c412022-01-12 18:20:38 +01001507 e->type != IMAGE_CFG_DATA_DELAY &&
1508 e->type != IMAGE_CFG_BINARY)
Pali Rohár02ba70a2021-07-23 11:14:11 +02001509 continue;
Pali Rohár3db9c412022-01-12 18:20:38 +01001510
Pali Rohárd737d5d2022-01-12 18:20:37 +01001511 if (datai == 0)
1512 register_set_hdr = (struct register_set_hdr_v1 *)cur;
Pali Rohár3db9c412022-01-12 18:20:38 +01001513
1514 /* If delay is not specified, use the smallest possible value. */
1515 if (e->type == IMAGE_CFG_DATA_DELAY)
1516 delay = e->regdata_delay;
1517 else
1518 delay = REGISTER_SET_HDR_OPT_DELAY_MS(0);
1519
1520 /*
1521 * DATA_DELAY command is the last entry in the register set
1522 * header and BINARY command inserts new binary header.
1523 * Therefore BINARY command requires to finish register set
1524 * header if some DATA command was specified. And DATA_DELAY
1525 * command automatically finish register set header even when
1526 * there was no DATA command.
1527 */
1528 if (e->type == IMAGE_CFG_DATA_DELAY ||
1529 (e->type == IMAGE_CFG_BINARY && datai != 0))
Pali Rohár9ac1def2022-01-12 18:20:36 +01001530 finish_register_set_header_v1(&cur, &next_ext, register_set_hdr,
Pali Rohár3db9c412022-01-12 18:20:38 +01001531 &datai, delay);
1532
1533 if (e->type == IMAGE_CFG_DATA) {
1534 register_set_hdr->data[datai].entry.address =
1535 cpu_to_le32(e->regdata.raddr);
1536 register_set_hdr->data[datai].entry.value =
1537 cpu_to_le32(e->regdata.rdata);
1538 datai++;
Pali Rohárf63c5832021-07-23 11:14:12 +02001539 }
Pali Rohár3db9c412022-01-12 18:20:38 +01001540
1541 if (e->type == IMAGE_CFG_BINARY) {
1542 if (add_binary_header_v1(&cur, &next_ext, e, main_hdr))
1543 return NULL;
1544 }
Pali Rohár02ba70a2021-07-23 11:14:11 +02001545 }
1546 if (datai != 0) {
Pali Rohár9ac1def2022-01-12 18:20:36 +01001547 /* Set delay to the smallest possible value. */
Pali Rohár3db9c412022-01-12 18:20:38 +01001548 delay = REGISTER_SET_HDR_OPT_DELAY_MS(0);
Pali Rohár9ac1def2022-01-12 18:20:36 +01001549 finish_register_set_header_v1(&cur, &next_ext, register_set_hdr,
Pali Rohár3db9c412022-01-12 18:20:38 +01001550 &datai, delay);
Pali Rohárd9fb82c2021-07-23 11:14:09 +02001551 }
Stefan Roese4acd2d22014-10-22 12:13:23 +02001552
Pali Roháre23ad5d2021-11-08 18:12:47 +01001553 if (secure_hdr && add_secure_header_v1(params, ptr, payloadsz + headersz,
Mario Sixa1b6b0a2017-01-11 16:01:00 +01001554 headersz, image, secure_hdr))
1555 return NULL;
Mario Sixa1b6b0a2017-01-11 16:01:00 +01001556
Stefan Roese4acd2d22014-10-22 12:13:23 +02001557 *imagesz = headersz;
Pali Rohár2b0980c2021-11-08 18:12:49 +01001558
1559 /* Fill the real header size without padding into the main header */
1560 headersz = sizeof(*main_hdr);
1561 for_each_opt_hdr_v1 (ohdr, main_hdr)
1562 headersz += opt_hdr_v1_size(ohdr);
1563 main_hdr->headersz_lsb = cpu_to_le16(headersz & 0xFFFF);
1564 main_hdr->headersz_msb = (headersz & 0xFFFF0000) >> 16;
1565
Pierre Bourdon9203c732021-12-25 20:50:19 +01001566 /* Calculate and set the header checksum */
1567 main_hdr->checksum = image_checksum8(main_hdr, headersz);
1568
Stefan Roese4acd2d22014-10-22 12:13:23 +02001569 return image;
1570}
1571
Pali Rohár6eb20bb2022-01-12 18:20:35 +01001572static int recognize_keyword(char *keyword)
Mario Six4991b4f2017-01-11 16:00:59 +01001573{
1574 int kw_id;
1575
1576 for (kw_id = 1; kw_id < IMAGE_CFG_COUNT; ++kw_id)
1577 if (!strcmp(keyword, id_strs[kw_id]))
1578 return kw_id;
1579
1580 return 0;
1581}
1582
Stefan Roese4acd2d22014-10-22 12:13:23 +02001583static int image_create_config_parse_oneline(char *line,
1584 struct image_cfg_element *el)
1585{
Mario Six4991b4f2017-01-11 16:00:59 +01001586 char *keyword, *saveptr, *value1, *value2;
1587 char delimiters[] = " \t";
1588 int keyword_id, ret, argi;
1589 char *unknown_msg = "Ignoring unknown line '%s'\n";
Stefan Roese4acd2d22014-10-22 12:13:23 +02001590
Mario Six4991b4f2017-01-11 16:00:59 +01001591 keyword = strtok_r(line, delimiters, &saveptr);
1592 keyword_id = recognize_keyword(keyword);
Mario Six94490a42017-01-11 16:00:54 +01001593
Mario Six4991b4f2017-01-11 16:00:59 +01001594 if (!keyword_id) {
1595 fprintf(stderr, unknown_msg, line);
1596 return 0;
1597 }
1598
1599 el->type = keyword_id;
1600
1601 value1 = strtok_r(NULL, delimiters, &saveptr);
1602
1603 if (!value1) {
1604 fprintf(stderr, "Parameter missing in line '%s'\n", line);
1605 return -1;
1606 }
1607
1608 switch (keyword_id) {
1609 case IMAGE_CFG_VERSION:
1610 el->version = atoi(value1);
1611 break;
Pali Roháraf496052022-01-12 18:20:40 +01001612 case IMAGE_CFG_CPU:
1613 if (strcmp(value1, "FEROCEON") == 0)
1614 el->cpu_sheeva = 0;
1615 else if (strcmp(value1, "SHEEVA") == 0)
1616 el->cpu_sheeva = 1;
1617 else if (strcmp(value1, "A9") == 0)
1618 el->cpu_sheeva = 0;
1619 else {
1620 fprintf(stderr, "Invalid CPU %s\n", value1);
1621 return -1;
1622 }
1623 break;
Mario Six4991b4f2017-01-11 16:00:59 +01001624 case IMAGE_CFG_BOOT_FROM:
1625 ret = image_boot_mode_id(value1);
Mario Six94490a42017-01-11 16:00:54 +01001626
Andreas Bießmannf411b8f2014-10-24 23:25:52 +02001627 if (ret < 0) {
Mario Six4991b4f2017-01-11 16:00:59 +01001628 fprintf(stderr, "Invalid boot media '%s'\n", value1);
Stefan Roese4acd2d22014-10-22 12:13:23 +02001629 return -1;
1630 }
Andreas Bießmannf411b8f2014-10-24 23:25:52 +02001631 el->bootfrom = ret;
Mario Six4991b4f2017-01-11 16:00:59 +01001632 break;
1633 case IMAGE_CFG_NAND_BLKSZ:
1634 el->nandblksz = strtoul(value1, NULL, 16);
1635 break;
1636 case IMAGE_CFG_NAND_BADBLK_LOCATION:
1637 el->nandbadblklocation = strtoul(value1, NULL, 16);
1638 break;
1639 case IMAGE_CFG_NAND_ECC_MODE:
1640 ret = image_nand_ecc_mode_id(value1);
Mario Six94490a42017-01-11 16:00:54 +01001641
Andreas Bießmannf411b8f2014-10-24 23:25:52 +02001642 if (ret < 0) {
Mario Six4991b4f2017-01-11 16:00:59 +01001643 fprintf(stderr, "Invalid NAND ECC mode '%s'\n", value1);
Stefan Roese4acd2d22014-10-22 12:13:23 +02001644 return -1;
1645 }
Andreas Bießmannf411b8f2014-10-24 23:25:52 +02001646 el->nandeccmode = ret;
Mario Six4991b4f2017-01-11 16:00:59 +01001647 break;
1648 case IMAGE_CFG_NAND_PAGESZ:
1649 el->nandpagesz = strtoul(value1, NULL, 16);
1650 break;
1651 case IMAGE_CFG_BINARY:
1652 argi = 0;
Mario Six94490a42017-01-11 16:00:54 +01001653
Mario Six4991b4f2017-01-11 16:00:59 +01001654 el->binary.file = strdup(value1);
Stefan Roese4acd2d22014-10-22 12:13:23 +02001655 while (1) {
Mario Six4991b4f2017-01-11 16:00:59 +01001656 char *value = strtok_r(NULL, delimiters, &saveptr);
Pali Rohár0aca27e2022-01-12 18:20:41 +01001657 char *endptr;
Mario Six4991b4f2017-01-11 16:00:59 +01001658
Stefan Roese4acd2d22014-10-22 12:13:23 +02001659 if (!value)
1660 break;
Pali Rohár0aca27e2022-01-12 18:20:41 +01001661
1662 if (!strcmp(value, "LOAD_ADDRESS")) {
1663 value = strtok_r(NULL, delimiters, &saveptr);
1664 if (!value) {
1665 fprintf(stderr,
1666 "Missing address argument for BINARY LOAD_ADDRESS\n");
1667 return -1;
1668 }
1669 el->binary.loadaddr = strtoul(value, &endptr, 16);
1670 if (*endptr) {
1671 fprintf(stderr,
1672 "Invalid argument '%s' for BINARY LOAD_ADDRESS\n",
1673 value);
1674 return -1;
1675 }
1676 value = strtok_r(NULL, delimiters, &saveptr);
1677 if (value) {
1678 fprintf(stderr,
1679 "Unexpected argument '%s' after BINARY LOAD_ADDRESS\n",
1680 value);
1681 return -1;
1682 }
1683 break;
1684 }
1685
1686 el->binary.args[argi] = strtoul(value, &endptr, 16);
1687 if (*endptr) {
1688 fprintf(stderr, "Invalid argument '%s' for BINARY\n", value);
1689 return -1;
1690 }
Stefan Roese4acd2d22014-10-22 12:13:23 +02001691 argi++;
1692 if (argi >= BINARY_MAX_ARGS) {
1693 fprintf(stderr,
Mario Six4991b4f2017-01-11 16:00:59 +01001694 "Too many arguments for BINARY\n");
Stefan Roese4acd2d22014-10-22 12:13:23 +02001695 return -1;
1696 }
1697 }
1698 el->binary.nargs = argi;
Mario Six4991b4f2017-01-11 16:00:59 +01001699 break;
1700 case IMAGE_CFG_DATA:
1701 value2 = strtok_r(NULL, delimiters, &saveptr);
Stefan Roese4acd2d22014-10-22 12:13:23 +02001702
1703 if (!value1 || !value2) {
1704 fprintf(stderr,
1705 "Invalid number of arguments for DATA\n");
1706 return -1;
1707 }
1708
Stefan Roese4acd2d22014-10-22 12:13:23 +02001709 el->regdata.raddr = strtoul(value1, NULL, 16);
1710 el->regdata.rdata = strtoul(value2, NULL, 16);
Mario Six4991b4f2017-01-11 16:00:59 +01001711 break;
Pali Rohárf63c5832021-07-23 11:14:12 +02001712 case IMAGE_CFG_DATA_DELAY:
1713 if (!strcmp(value1, "SDRAM_SETUP"))
1714 el->regdata_delay = REGISTER_SET_HDR_OPT_DELAY_SDRAM_SETUP;
1715 else
1716 el->regdata_delay = REGISTER_SET_HDR_OPT_DELAY_MS(strtoul(value1, NULL, 10));
Pali Rohárfdcae262022-01-12 18:20:48 +01001717 if (el->regdata_delay > 255) {
1718 fprintf(stderr, "Maximal DATA_DELAY is 255\n");
1719 return -1;
1720 }
Pali Rohárf63c5832021-07-23 11:14:12 +02001721 break;
Mario Six4991b4f2017-01-11 16:00:59 +01001722 case IMAGE_CFG_BAUDRATE:
1723 el->baudrate = strtoul(value1, NULL, 10);
1724 break;
Pali Rohár12f2c032021-11-08 18:12:41 +01001725 case IMAGE_CFG_UART_PORT:
1726 el->uart_port = strtoul(value1, NULL, 16);
1727 break;
1728 case IMAGE_CFG_UART_MPP:
1729 el->uart_mpp = strtoul(value1, NULL, 16);
1730 break;
Mario Six4991b4f2017-01-11 16:00:59 +01001731 case IMAGE_CFG_DEBUG:
1732 el->debug = strtoul(value1, NULL, 10);
1733 break;
Mario Sixa1b6b0a2017-01-11 16:01:00 +01001734 case IMAGE_CFG_KAK:
1735 el->key_name = strdup(value1);
1736 break;
1737 case IMAGE_CFG_CSK:
1738 el->key_name = strdup(value1);
1739 break;
1740 case IMAGE_CFG_CSK_INDEX:
1741 el->csk_idx = strtol(value1, NULL, 0);
1742 break;
1743 case IMAGE_CFG_JTAG_DELAY:
1744 el->jtag_delay = strtoul(value1, NULL, 0);
1745 break;
1746 case IMAGE_CFG_BOX_ID:
1747 el->boxid = strtoul(value1, NULL, 0);
1748 break;
1749 case IMAGE_CFG_FLASH_ID:
1750 el->flashid = strtoul(value1, NULL, 0);
1751 break;
1752 case IMAGE_CFG_SEC_SPECIALIZED_IMG:
1753 el->sec_specialized_img = true;
1754 break;
1755 case IMAGE_CFG_SEC_COMMON_IMG:
1756 el->sec_specialized_img = false;
1757 break;
1758 case IMAGE_CFG_SEC_BOOT_DEV:
1759 el->sec_boot_dev = strtoul(value1, NULL, 0);
1760 break;
1761 case IMAGE_CFG_SEC_FUSE_DUMP:
1762 el->name = strdup(value1);
1763 break;
Mario Six4991b4f2017-01-11 16:00:59 +01001764 default:
1765 fprintf(stderr, unknown_msg, line);
Stefan Roese4acd2d22014-10-22 12:13:23 +02001766 }
Prafulla Wadaskaraa0c7a82009-09-07 15:05:02 +05301767
1768 return 0;
1769}
1770
Stefan Roese4acd2d22014-10-22 12:13:23 +02001771/*
1772 * Parse the configuration file 'fcfg' into the array of configuration
1773 * elements 'image_cfg', and return the number of configuration
1774 * elements in 'cfgn'.
1775 */
1776static int image_create_config_parse(FILE *fcfg)
Prafulla Wadaskaraa0c7a82009-09-07 15:05:02 +05301777{
Stefan Roese4acd2d22014-10-22 12:13:23 +02001778 int ret;
1779 int cfgi = 0;
Prafulla Wadaskaraa0c7a82009-09-07 15:05:02 +05301780
Stefan Roese4acd2d22014-10-22 12:13:23 +02001781 /* Parse the configuration file */
1782 while (!feof(fcfg)) {
1783 char *line;
1784 char buf[256];
1785
1786 /* Read the current line */
1787 memset(buf, 0, sizeof(buf));
1788 line = fgets(buf, sizeof(buf), fcfg);
1789 if (!line)
1790 break;
1791
1792 /* Ignore useless lines */
1793 if (line[0] == '\n' || line[0] == '#')
1794 continue;
1795
1796 /* Strip final newline */
1797 if (line[strlen(line) - 1] == '\n')
1798 line[strlen(line) - 1] = 0;
1799
1800 /* Parse the current line */
1801 ret = image_create_config_parse_oneline(line,
1802 &image_cfg[cfgi]);
1803 if (ret)
1804 return ret;
1805
1806 cfgi++;
1807
1808 if (cfgi >= IMAGE_CFG_ELEMENT_MAX) {
1809 fprintf(stderr,
1810 "Too many configuration elements in .cfg file\n");
1811 return -1;
1812 }
1813 }
1814
1815 cfgn = cfgi;
1816 return 0;
Prafulla Wadaskaraa0c7a82009-09-07 15:05:02 +05301817}
1818
Stefan Roese4acd2d22014-10-22 12:13:23 +02001819static int image_get_version(void)
1820{
1821 struct image_cfg_element *e;
1822
1823 e = image_find_option(IMAGE_CFG_VERSION);
1824 if (!e)
1825 return -1;
1826
1827 return e->version;
1828}
1829
Stefan Roese4acd2d22014-10-22 12:13:23 +02001830static void kwbimage_set_header(void *ptr, struct stat *sbuf, int ifd,
1831 struct image_tool_params *params)
1832{
1833 FILE *fcfg;
1834 void *image = NULL;
1835 int version;
Łukasz Majewski93e93712014-11-21 09:22:43 +01001836 size_t headersz = 0;
Pali Roháre23ad5d2021-11-08 18:12:47 +01001837 size_t datasz;
Stefan Roese4acd2d22014-10-22 12:13:23 +02001838 uint32_t checksum;
Pali Roháre23ad5d2021-11-08 18:12:47 +01001839 struct stat s;
Stefan Roese4acd2d22014-10-22 12:13:23 +02001840 int ret;
Stefan Roese4acd2d22014-10-22 12:13:23 +02001841
Pali Roháre23ad5d2021-11-08 18:12:47 +01001842 /*
1843 * Do not use sbuf->st_size as it contains size with padding.
1844 * We need original image data size, so stat original file.
1845 */
1846 if (stat(params->datafile, &s)) {
1847 fprintf(stderr, "Could not stat data file %s: %s\n",
1848 params->datafile, strerror(errno));
1849 exit(EXIT_FAILURE);
1850 }
1851 datasz = ALIGN(s.st_size, 4);
1852
Stefan Roese4acd2d22014-10-22 12:13:23 +02001853 fcfg = fopen(params->imagename, "r");
1854 if (!fcfg) {
1855 fprintf(stderr, "Could not open input file %s\n",
1856 params->imagename);
1857 exit(EXIT_FAILURE);
1858 }
1859
1860 image_cfg = malloc(IMAGE_CFG_ELEMENT_MAX *
1861 sizeof(struct image_cfg_element));
1862 if (!image_cfg) {
1863 fprintf(stderr, "Cannot allocate memory\n");
1864 fclose(fcfg);
1865 exit(EXIT_FAILURE);
1866 }
1867
1868 memset(image_cfg, 0,
1869 IMAGE_CFG_ELEMENT_MAX * sizeof(struct image_cfg_element));
1870 rewind(fcfg);
1871
1872 ret = image_create_config_parse(fcfg);
1873 fclose(fcfg);
1874 if (ret) {
1875 free(image_cfg);
1876 exit(EXIT_FAILURE);
1877 }
1878
1879 version = image_get_version();
Stefan Roese934a5292014-10-28 11:32:24 +01001880 switch (version) {
1881 /*
1882 * Fallback to version 0 if no version is provided in the
1883 * cfg file
1884 */
1885 case -1:
1886 case 0:
Pali Roháre23ad5d2021-11-08 18:12:47 +01001887 image = image_create_v0(&headersz, params, datasz + 4);
Stefan Roese934a5292014-10-28 11:32:24 +01001888 break;
1889
1890 case 1:
Pali Roháre23ad5d2021-11-08 18:12:47 +01001891 image = image_create_v1(&headersz, params, ptr, datasz + 4);
Stefan Roese934a5292014-10-28 11:32:24 +01001892 break;
1893
1894 default:
1895 fprintf(stderr, "Unsupported version %d\n", version);
1896 free(image_cfg);
1897 exit(EXIT_FAILURE);
1898 }
Stefan Roese4acd2d22014-10-22 12:13:23 +02001899
1900 if (!image) {
1901 fprintf(stderr, "Could not create image\n");
1902 free(image_cfg);
1903 exit(EXIT_FAILURE);
1904 }
1905
1906 free(image_cfg);
1907
Pali Roháre23ad5d2021-11-08 18:12:47 +01001908 /* Build and add image data checksum */
Pali Rohár37cb9c12021-07-23 11:13:56 +02001909 checksum = cpu_to_le32(image_checksum32((uint8_t *)ptr + headersz,
Pali Roháre23ad5d2021-11-08 18:12:47 +01001910 datasz));
1911 memcpy((uint8_t *)ptr + headersz + datasz, &checksum, sizeof(uint32_t));
Stefan Roese4acd2d22014-10-22 12:13:23 +02001912
1913 /* Finally copy the header into the image area */
1914 memcpy(ptr, image, headersz);
1915
1916 free(image);
1917}
1918
1919static void kwbimage_print_header(const void *ptr)
1920{
1921 struct main_hdr_v0 *mhdr = (struct main_hdr_v0 *)ptr;
Pali Rohárf76ae252022-02-17 10:43:36 +01001922 struct bin_hdr_v0 *bhdr;
Marek Behún732c9302021-08-18 00:59:15 +02001923 struct opt_hdr_v1 *ohdr;
Stefan Roese4acd2d22014-10-22 12:13:23 +02001924
1925 printf("Image Type: MVEBU Boot from %s Image\n",
1926 image_boot_mode_name(mhdr->blockid));
Marek Behúnacb0b382021-09-24 23:07:00 +02001927 printf("Image version:%d\n", kwbimage_version(ptr));
Pali Rohár34dcf952021-07-23 11:14:04 +02001928
Marek Behún732c9302021-08-18 00:59:15 +02001929 for_each_opt_hdr_v1 (ohdr, mhdr) {
1930 if (ohdr->headertype == OPT_HDR_V1_BINARY_TYPE) {
Pali Rohárc934c9a2022-01-12 18:20:49 +01001931 printf("BIN Img Size: ");
Marek Behún732c9302021-08-18 00:59:15 +02001932 genimg_print_size(opt_hdr_v1_size(ohdr) - 12 -
1933 4 * ohdr->data[0]);
Pali Rohárc934c9a2022-01-12 18:20:49 +01001934 printf("BIN Img Offs: %08x\n",
1935 (unsigned)((uint8_t *)ohdr - (uint8_t *)mhdr) +
1936 8 + 4 * ohdr->data[0]);
Pali Rohár34dcf952021-07-23 11:14:04 +02001937 }
1938 }
Marek Behún732c9302021-08-18 00:59:15 +02001939
Pali Rohárf76ae252022-02-17 10:43:36 +01001940 for_each_bin_hdr_v0(bhdr, mhdr) {
1941 printf("BIN Img Size: ");
1942 genimg_print_size(le32_to_cpu(bhdr->size));
1943 printf("BIN Img Addr: %08x\n", le32_to_cpu(bhdr->destaddr));
1944 printf("BIN Img Entr: %08x\n", le32_to_cpu(bhdr->execaddr));
1945 }
1946
Gerald Kerma26f195c2014-10-31 01:03:27 +01001947 printf("Data Size: ");
Stefan Roese4acd2d22014-10-22 12:13:23 +02001948 genimg_print_size(mhdr->blocksize - sizeof(uint32_t));
1949 printf("Load Address: %08x\n", mhdr->destaddr);
1950 printf("Entry Point: %08x\n", mhdr->execaddr);
1951}
1952
1953static int kwbimage_check_image_types(uint8_t type)
Prafulla Wadaskaraa0c7a82009-09-07 15:05:02 +05301954{
1955 if (type == IH_TYPE_KWBIMAGE)
1956 return EXIT_SUCCESS;
Mario Six94490a42017-01-11 16:00:54 +01001957
1958 return EXIT_FAILURE;
Prafulla Wadaskaraa0c7a82009-09-07 15:05:02 +05301959}
1960
Stefan Roese4acd2d22014-10-22 12:13:23 +02001961static int kwbimage_verify_header(unsigned char *ptr, int image_size,
1962 struct image_tool_params *params)
1963{
Marek Behúnfe2fd732021-09-24 23:07:01 +02001964 size_t header_size = kwbheader_size(ptr);
Pali Rohár700ea982021-11-08 18:12:44 +01001965 uint8_t blockid;
1966 uint32_t offset;
1967 uint32_t size;
Marek Behúnfe2fd732021-09-24 23:07:01 +02001968 uint8_t csum;
Alexander Graf6cd56782018-03-15 11:14:19 +01001969
1970 if (header_size > image_size)
1971 return -FDT_ERR_BADSTRUCTURE;
Stefan Roese4acd2d22014-10-22 12:13:23 +02001972
Baruch Siachdb7cd4ed2017-07-04 20:23:40 +03001973 if (!main_hdr_checksum_ok(ptr))
Stefan Roese4acd2d22014-10-22 12:13:23 +02001974 return -FDT_ERR_BADSTRUCTURE;
1975
1976 /* Only version 0 extended header has checksum */
Marek Behúnacb0b382021-09-24 23:07:00 +02001977 if (kwbimage_version(ptr) == 0) {
Pali Rohárfe2c0e22021-07-23 11:14:01 +02001978 struct main_hdr_v0 *mhdr = (struct main_hdr_v0 *)ptr;
Pali Rohárf76ae252022-02-17 10:43:36 +01001979 struct ext_hdr_v0 *ext_hdr;
1980 struct bin_hdr_v0 *bhdr;
Mario Sixe89016c2017-01-11 16:00:56 +01001981
Pali Rohárf76ae252022-02-17 10:43:36 +01001982 for_each_ext_hdr_v0(ext_hdr, ptr) {
Marek Behúnfe2fd732021-09-24 23:07:01 +02001983 csum = image_checksum8(ext_hdr, sizeof(*ext_hdr) - 1);
1984 if (csum != ext_hdr->checksum)
Pali Rohárfe2c0e22021-07-23 11:14:01 +02001985 return -FDT_ERR_BADSTRUCTURE;
1986 }
Pali Rohár700ea982021-11-08 18:12:44 +01001987
Pali Rohárf76ae252022-02-17 10:43:36 +01001988 for_each_bin_hdr_v0(bhdr, ptr) {
1989 csum = image_checksum8(bhdr, (uint8_t *)&bhdr->checksum - (uint8_t *)bhdr - 1);
1990 if (csum != bhdr->checksum)
1991 return -FDT_ERR_BADSTRUCTURE;
1992
1993 if (bhdr->offset > sizeof(*bhdr) || bhdr->offset % 4 != 0)
1994 return -FDT_ERR_BADSTRUCTURE;
1995
1996 if (bhdr->offset + bhdr->size + 4 > sizeof(*bhdr) || bhdr->size % 4 != 0)
1997 return -FDT_ERR_BADSTRUCTURE;
1998
1999 if (image_checksum32((uint8_t *)bhdr + bhdr->offset, bhdr->size) !=
2000 *(uint32_t *)((uint8_t *)bhdr + bhdr->offset + bhdr->size))
2001 return -FDT_ERR_BADSTRUCTURE;
2002 }
2003
Pali Rohár700ea982021-11-08 18:12:44 +01002004 blockid = mhdr->blockid;
2005 offset = le32_to_cpu(mhdr->srcaddr);
2006 size = le32_to_cpu(mhdr->blocksize);
Marek Behúnacb0b382021-09-24 23:07:00 +02002007 } else if (kwbimage_version(ptr) == 1) {
Pali Rohár93804452021-07-23 11:14:02 +02002008 struct main_hdr_v1 *mhdr = (struct main_hdr_v1 *)ptr;
Marek Behún732c9302021-08-18 00:59:15 +02002009 const uint8_t *mhdr_end;
2010 struct opt_hdr_v1 *ohdr;
Pali Rohár93804452021-07-23 11:14:02 +02002011
Marek Behún732c9302021-08-18 00:59:15 +02002012 mhdr_end = (uint8_t *)mhdr + header_size;
2013 for_each_opt_hdr_v1 (ohdr, ptr)
2014 if (!opt_hdr_v1_valid_size(ohdr, mhdr_end))
2015 return -FDT_ERR_BADSTRUCTURE;
Pali Roháre0c243c2021-07-23 11:14:03 +02002016
Pali Rohár700ea982021-11-08 18:12:44 +01002017 blockid = mhdr->blockid;
Pali Roháre0c243c2021-07-23 11:14:03 +02002018 offset = le32_to_cpu(mhdr->srcaddr);
Pali Roháre0c243c2021-07-23 11:14:03 +02002019 size = le32_to_cpu(mhdr->blocksize);
Pali Rohárb9840562021-08-11 10:14:14 +02002020 } else {
2021 return -FDT_ERR_BADSTRUCTURE;
Pali Rohár93804452021-07-23 11:14:02 +02002022 }
2023
Pali Rohár700ea982021-11-08 18:12:44 +01002024 /*
2025 * For SATA srcaddr is specified in number of sectors.
2026 * The main header is must be stored at sector number 1.
2027 * This expects that sector size is 512 bytes and recalculates
2028 * data offset to bytes relative to the main header.
2029 */
2030 if (blockid == IBR_HDR_SATA_ID) {
2031 if (offset < 1)
2032 return -FDT_ERR_BADSTRUCTURE;
2033 offset -= 1;
2034 offset *= 512;
2035 }
2036
2037 /*
2038 * For SDIO srcaddr is specified in number of sectors.
2039 * This expects that sector size is 512 bytes and recalculates
2040 * data offset to bytes.
2041 */
2042 if (blockid == IBR_HDR_SDIO_ID)
2043 offset *= 512;
2044
2045 /*
2046 * For PCIe srcaddr is always set to 0xFFFFFFFF.
2047 * This expects that data starts after all headers.
2048 */
2049 if (blockid == IBR_HDR_PEX_ID && offset == 0xFFFFFFFF)
2050 offset = header_size;
2051
2052 if (offset > image_size || offset % 4 != 0)
2053 return -FDT_ERR_BADSTRUCTURE;
2054
2055 if (size < 4 || offset + size > image_size || size % 4 != 0)
2056 return -FDT_ERR_BADSTRUCTURE;
2057
2058 if (image_checksum32(ptr + offset, size - 4) !=
2059 *(uint32_t *)(ptr + offset + size - 4))
2060 return -FDT_ERR_BADSTRUCTURE;
2061
Stefan Roese4acd2d22014-10-22 12:13:23 +02002062 return 0;
2063}
2064
2065static int kwbimage_generate(struct image_tool_params *params,
2066 struct image_type_params *tparams)
2067{
Patrick Wildt6cbf7ed2017-05-10 22:18:54 +02002068 FILE *fcfg;
Pali Rohár37cb9c12021-07-23 11:13:56 +02002069 struct stat s;
Stefan Roese4acd2d22014-10-22 12:13:23 +02002070 int alloc_len;
Pali Rohárc934aad2021-07-23 11:13:57 +02002071 int bootfrom;
Patrick Wildt6cbf7ed2017-05-10 22:18:54 +02002072 int version;
Stefan Roese4acd2d22014-10-22 12:13:23 +02002073 void *hdr;
Patrick Wildt6cbf7ed2017-05-10 22:18:54 +02002074 int ret;
Stefan Roese4acd2d22014-10-22 12:13:23 +02002075
Patrick Wildt6cbf7ed2017-05-10 22:18:54 +02002076 fcfg = fopen(params->imagename, "r");
2077 if (!fcfg) {
2078 fprintf(stderr, "Could not open input file %s\n",
2079 params->imagename);
2080 exit(EXIT_FAILURE);
2081 }
2082
Pali Rohár37cb9c12021-07-23 11:13:56 +02002083 if (stat(params->datafile, &s)) {
2084 fprintf(stderr, "Could not stat data file %s: %s\n",
2085 params->datafile, strerror(errno));
2086 exit(EXIT_FAILURE);
2087 }
2088
Patrick Wildt6cbf7ed2017-05-10 22:18:54 +02002089 image_cfg = malloc(IMAGE_CFG_ELEMENT_MAX *
2090 sizeof(struct image_cfg_element));
2091 if (!image_cfg) {
2092 fprintf(stderr, "Cannot allocate memory\n");
2093 fclose(fcfg);
2094 exit(EXIT_FAILURE);
2095 }
2096
2097 memset(image_cfg, 0,
2098 IMAGE_CFG_ELEMENT_MAX * sizeof(struct image_cfg_element));
2099 rewind(fcfg);
2100
2101 ret = image_create_config_parse(fcfg);
2102 fclose(fcfg);
2103 if (ret) {
2104 free(image_cfg);
2105 exit(EXIT_FAILURE);
2106 }
2107
Pali Rohárc934aad2021-07-23 11:13:57 +02002108 bootfrom = image_get_bootfrom();
Patrick Wildt6cbf7ed2017-05-10 22:18:54 +02002109 version = image_get_version();
2110 switch (version) {
2111 /*
2112 * Fallback to version 0 if no version is provided in the
2113 * cfg file
2114 */
2115 case -1:
2116 case 0:
Pali Rohár851114b2021-11-08 18:12:50 +01002117 alloc_len = image_headersz_v0(NULL);
Patrick Wildt6cbf7ed2017-05-10 22:18:54 +02002118 break;
2119
2120 case 1:
Mario Sixe93cf532017-01-11 16:00:57 +01002121 alloc_len = image_headersz_v1(NULL);
Pali Rohár252e7c32022-01-12 18:20:42 +01002122 if (!alloc_len) {
2123 free(image_cfg);
2124 exit(EXIT_FAILURE);
2125 }
Pali Rohár78d997f2022-01-12 18:20:43 +01002126 if (alloc_len > 192*1024) {
2127 fprintf(stderr, "Header is too big (%u bytes), maximal kwbimage header size is %u bytes\n", alloc_len, 192*1024);
2128 free(image_cfg);
2129 exit(EXIT_FAILURE);
2130 }
Patrick Wildt6cbf7ed2017-05-10 22:18:54 +02002131 break;
2132
2133 default:
2134 fprintf(stderr, "Unsupported version %d\n", version);
2135 free(image_cfg);
2136 exit(EXIT_FAILURE);
Stefan Roese4acd2d22014-10-22 12:13:23 +02002137 }
2138
Patrick Wildt6cbf7ed2017-05-10 22:18:54 +02002139 free(image_cfg);
2140
Stefan Roese4acd2d22014-10-22 12:13:23 +02002141 hdr = malloc(alloc_len);
2142 if (!hdr) {
2143 fprintf(stderr, "%s: malloc return failure: %s\n",
2144 params->cmdname, strerror(errno));
2145 exit(EXIT_FAILURE);
2146 }
2147
2148 memset(hdr, 0, alloc_len);
2149 tparams->header_size = alloc_len;
2150 tparams->hdr = hdr;
2151
Stefan Roese77720852015-11-24 09:14:59 +01002152 /*
2153 * The resulting image needs to be 4-byte aligned. At least
2154 * the Marvell hdrparser tool complains if its unaligned.
Pali Rohár37cb9c12021-07-23 11:13:56 +02002155 * After the image data is stored 4-byte checksum.
Pali Rohár188099e2021-11-08 18:12:46 +01002156 * Final UART image must be aligned to 128 bytes.
Pali Rohárc934aad2021-07-23 11:13:57 +02002157 * Final SPI and NAND images must be aligned to 256 bytes.
Pali Rohár501a54a2021-07-23 11:13:59 +02002158 * Final SATA and SDIO images must be aligned to 512 bytes.
Stefan Roese77720852015-11-24 09:14:59 +01002159 */
Pali Rohárc934aad2021-07-23 11:13:57 +02002160 if (bootfrom == IBR_HDR_SPI_ID || bootfrom == IBR_HDR_NAND_ID)
2161 return 4 + (256 - (alloc_len + s.st_size + 4) % 256) % 256;
Pali Rohár501a54a2021-07-23 11:13:59 +02002162 else if (bootfrom == IBR_HDR_SATA_ID || bootfrom == IBR_HDR_SDIO_ID)
2163 return 4 + (512 - (alloc_len + s.st_size + 4) % 512) % 512;
Pali Rohár188099e2021-11-08 18:12:46 +01002164 else if (bootfrom == IBR_HDR_UART_ID)
2165 return 4 + (128 - (alloc_len + s.st_size + 4) % 128) % 128;
Pali Rohárc934aad2021-07-23 11:13:57 +02002166 else
2167 return 4 + (4 - s.st_size % 4) % 4;
Stefan Roese4acd2d22014-10-22 12:13:23 +02002168}
2169
Pali Rohár1a8e6b62022-01-12 18:20:50 +01002170static int kwbimage_generate_config(void *ptr, struct image_tool_params *params)
2171{
2172 struct main_hdr_v0 *mhdr0 = (struct main_hdr_v0 *)ptr;
2173 struct main_hdr_v1 *mhdr = (struct main_hdr_v1 *)ptr;
2174 size_t header_size = kwbheader_size(ptr);
2175 struct register_set_hdr_v1 *regset_hdr;
2176 struct ext_hdr_v0_reg *regdata;
2177 struct ext_hdr_v0 *ehdr0;
Pali Rohárf76ae252022-02-17 10:43:36 +01002178 struct bin_hdr_v0 *bhdr0;
Pali Rohár1a8e6b62022-01-12 18:20:50 +01002179 struct opt_hdr_v1 *ohdr;
Pali Rohárf76ae252022-02-17 10:43:36 +01002180 int params_count;
Pali Rohár1a8e6b62022-01-12 18:20:50 +01002181 unsigned offset;
Pali Rohárf76ae252022-02-17 10:43:36 +01002182 int is_v0_ext;
Pali Rohár1a8e6b62022-01-12 18:20:50 +01002183 int cur_idx;
2184 int version;
2185 FILE *f;
2186 int i;
2187
2188 f = fopen(params->outfile, "w");
2189 if (!f) {
2190 fprintf(stderr, "Can't open \"%s\": %s\n", params->outfile, strerror(errno));
2191 return -1;
2192 }
2193
2194 version = kwbimage_version(ptr);
2195
Pali Rohárf76ae252022-02-17 10:43:36 +01002196 is_v0_ext = 0;
2197 if (version == 0) {
2198 if (mhdr0->ext > 1 || mhdr0->bin ||
2199 ((ehdr0 = ext_hdr_v0_first(ptr)) &&
2200 (ehdr0->match_addr || ehdr0->match_mask || ehdr0->match_value)))
2201 is_v0_ext = 1;
2202 }
2203
Pali Rohár1a8e6b62022-01-12 18:20:50 +01002204 if (version != 0)
2205 fprintf(f, "VERSION %d\n", version);
2206
2207 fprintf(f, "BOOT_FROM %s\n", image_boot_mode_name(mhdr->blockid) ?: "<unknown>");
2208
2209 if (version == 0 && mhdr->blockid == IBR_HDR_NAND_ID)
2210 fprintf(f, "NAND_ECC_MODE %s\n", image_nand_ecc_mode_name(mhdr0->nandeccmode));
2211
2212 if (mhdr->blockid == IBR_HDR_NAND_ID)
2213 fprintf(f, "NAND_PAGE_SIZE 0x%x\n", (unsigned)mhdr->nandpagesize);
2214
Pali Rohárf76ae252022-02-17 10:43:36 +01002215 if (version != 0 && mhdr->blockid == IBR_HDR_NAND_ID)
Pali Rohár1a8e6b62022-01-12 18:20:50 +01002216 fprintf(f, "NAND_BLKSZ 0x%x\n", (unsigned)mhdr->nandblocksize);
Pali Rohárf76ae252022-02-17 10:43:36 +01002217
2218 if (mhdr->blockid == IBR_HDR_NAND_ID && (mhdr->nandbadblklocation != 0 || is_v0_ext))
Pali Rohár1a8e6b62022-01-12 18:20:50 +01002219 fprintf(f, "NAND_BADBLK_LOCATION 0x%x\n", (unsigned)mhdr->nandbadblklocation);
Pali Rohár1a8e6b62022-01-12 18:20:50 +01002220
2221 if (version == 0 && mhdr->blockid == IBR_HDR_SATA_ID)
2222 fprintf(f, "SATA_PIO_MODE %u\n", (unsigned)mhdr0->satapiomode);
2223
2224 /*
2225 * Addresses and sizes which are specified by mkimage command line
2226 * arguments and not in kwbimage config file
2227 */
2228
2229 if (version != 0)
2230 fprintf(f, "#HEADER_SIZE 0x%x\n",
2231 ((unsigned)mhdr->headersz_msb << 8) | le16_to_cpu(mhdr->headersz_lsb));
2232
2233 fprintf(f, "#SRC_ADDRESS 0x%x\n", le32_to_cpu(mhdr->srcaddr));
2234 fprintf(f, "#BLOCK_SIZE 0x%x\n", le32_to_cpu(mhdr->blocksize));
2235 fprintf(f, "#DEST_ADDRESS 0x%08x\n", le32_to_cpu(mhdr->destaddr));
2236 fprintf(f, "#EXEC_ADDRESS 0x%08x\n", le32_to_cpu(mhdr->execaddr));
2237
2238 if (version != 0) {
2239 if (options_to_baudrate(mhdr->options))
2240 fprintf(f, "BAUDRATE %u\n", options_to_baudrate(mhdr->options));
2241 if (options_to_baudrate(mhdr->options) ||
2242 ((mhdr->options >> 3) & 0x3) || ((mhdr->options >> 5) & 0x7)) {
2243 fprintf(f, "UART_PORT %u\n", (unsigned)((mhdr->options >> 3) & 0x3));
2244 fprintf(f, "UART_MPP 0x%x\n", (unsigned)((mhdr->options >> 5) & 0x7));
2245 }
2246 if (mhdr->flags & 0x1)
2247 fprintf(f, "DEBUG 1\n");
2248 }
2249
2250 cur_idx = 1;
2251 for_each_opt_hdr_v1(ohdr, ptr) {
2252 if (ohdr->headertype == OPT_HDR_V1_SECURE_TYPE) {
2253 fprintf(f, "#SECURE_HEADER\n");
2254 } else if (ohdr->headertype == OPT_HDR_V1_BINARY_TYPE) {
2255 fprintf(f, "BINARY binary%d.bin", cur_idx);
2256 for (i = 0; i < ohdr->data[0]; i++)
2257 fprintf(f, " 0x%x", le32_to_cpu(((uint32_t *)ohdr->data)[i + 1]));
2258 offset = (unsigned)((uint8_t *)ohdr - (uint8_t *)mhdr) + 8 + 4 * ohdr->data[0];
2259 fprintf(f, " LOAD_ADDRESS 0x%08x\n", 0x40000000 + offset);
2260 fprintf(f, " # for CPU SHEEVA: LOAD_ADDRESS 0x%08x\n", 0x40004000 + offset);
2261 cur_idx++;
2262 } else if (ohdr->headertype == OPT_HDR_V1_REGISTER_TYPE) {
2263 regset_hdr = (struct register_set_hdr_v1 *)ohdr;
2264 for (i = 0;
2265 i < opt_hdr_v1_size(ohdr) - sizeof(struct opt_hdr_v1) -
2266 sizeof(regset_hdr->data[0].last_entry);
2267 i++)
2268 fprintf(f, "DATA 0x%08x 0x%08x\n",
2269 le32_to_cpu(regset_hdr->data[i].entry.address),
2270 le32_to_cpu(regset_hdr->data[i].entry.value));
2271 if (opt_hdr_v1_size(ohdr) - sizeof(struct opt_hdr_v1) >=
2272 sizeof(regset_hdr->data[0].last_entry)) {
2273 if (regset_hdr->data[0].last_entry.delay)
2274 fprintf(f, "DATA_DELAY %u\n",
2275 (unsigned)regset_hdr->data[0].last_entry.delay);
2276 else
2277 fprintf(f, "DATA_DELAY SDRAM_SETUP\n");
2278 }
2279 }
2280 }
2281
Pali Rohárf76ae252022-02-17 10:43:36 +01002282 if (version == 0 && !is_v0_ext && le16_to_cpu(mhdr0->ddrinitdelay))
2283 fprintf(f, "DDR_INIT_DELAY %u\n", (unsigned)le16_to_cpu(mhdr0->ddrinitdelay));
2284
2285 for_each_ext_hdr_v0(ehdr0, ptr) {
2286 if (is_v0_ext) {
2287 fprintf(f, "\nMATCH ADDRESS 0x%08x MASK 0x%08x VALUE 0x%08x\n",
2288 le32_to_cpu(ehdr0->match_addr),
2289 le32_to_cpu(ehdr0->match_mask),
2290 le32_to_cpu(ehdr0->match_value));
2291 if (ehdr0->rsvd1[0] || ehdr0->rsvd1[1] || ehdr0->rsvd1[2] ||
2292 ehdr0->rsvd1[3] || ehdr0->rsvd1[4] || ehdr0->rsvd1[5] ||
2293 ehdr0->rsvd1[6] || ehdr0->rsvd1[7])
2294 fprintf(f, "#DDR_RSVD1 0x%x 0x%x 0x%x 0x%x 0x%x 0x%x 0x%x 0x%x\n",
2295 ehdr0->rsvd1[0], ehdr0->rsvd1[1], ehdr0->rsvd1[2],
2296 ehdr0->rsvd1[3], ehdr0->rsvd1[4], ehdr0->rsvd1[5],
2297 ehdr0->rsvd1[6], ehdr0->rsvd1[7]);
2298 if (ehdr0->rsvd2[0] || ehdr0->rsvd2[1] || ehdr0->rsvd2[2] ||
2299 ehdr0->rsvd2[3] || ehdr0->rsvd2[4] || ehdr0->rsvd2[5] ||
2300 ehdr0->rsvd2[6])
2301 fprintf(f, "#DDR_RSVD2 0x%x 0x%x 0x%x 0x%x 0x%x 0x%x 0x%x\n",
2302 ehdr0->rsvd2[0], ehdr0->rsvd2[1], ehdr0->rsvd2[2],
2303 ehdr0->rsvd2[3], ehdr0->rsvd2[4], ehdr0->rsvd2[5],
2304 ehdr0->rsvd2[6]);
2305 if (ehdr0->ddrwritetype)
2306 fprintf(f, "DDR_WRITE_TYPE %u\n", (unsigned)ehdr0->ddrwritetype);
2307 if (ehdr0->ddrresetmpp)
2308 fprintf(f, "DDR_RESET_MPP 0x%x\n", (unsigned)ehdr0->ddrresetmpp);
2309 if (ehdr0->ddrclkenmpp)
2310 fprintf(f, "DDR_CLKEN_MPP 0x%x\n", (unsigned)ehdr0->ddrclkenmpp);
2311 if (ehdr0->ddrinitdelay)
2312 fprintf(f, "DDR_INIT_DELAY %u\n", (unsigned)ehdr0->ddrinitdelay);
2313 }
2314
Pali Rohár1a8e6b62022-01-12 18:20:50 +01002315 if (ehdr0->offset) {
2316 for (regdata = (struct ext_hdr_v0_reg *)((uint8_t *)ptr + ehdr0->offset);
Pali Rohára2389212022-02-13 01:04:33 +01002317 (uint8_t *)regdata < (uint8_t *)ptr + header_size &&
2318 (regdata->raddr || regdata->rdata);
Pali Rohár1a8e6b62022-01-12 18:20:50 +01002319 regdata++)
2320 fprintf(f, "DATA 0x%08x 0x%08x\n", le32_to_cpu(regdata->raddr),
2321 le32_to_cpu(regdata->rdata));
Pali Rohára2389212022-02-13 01:04:33 +01002322 if ((uint8_t *)regdata != (uint8_t *)ptr + ehdr0->offset)
2323 fprintf(f, "DATA 0x0 0x0\n");
Pali Rohár1a8e6b62022-01-12 18:20:50 +01002324 }
Pali Rohárf76ae252022-02-17 10:43:36 +01002325
2326 if (le32_to_cpu(ehdr0->enddelay))
2327 fprintf(f, "DATA_DELAY %u\n", le32_to_cpu(ehdr0->enddelay));
2328 else if (is_v0_ext)
2329 fprintf(f, "DATA_DELAY SDRAM_SETUP\n");
Pali Rohár1a8e6b62022-01-12 18:20:50 +01002330 }
2331
Pali Rohárf76ae252022-02-17 10:43:36 +01002332 cur_idx = 1;
2333 for_each_bin_hdr_v0(bhdr0, ptr) {
2334 fprintf(f, "\nMATCH ADDRESS 0x%08x MASK 0x%08x VALUE 0x%08x\n",
2335 le32_to_cpu(bhdr0->match_addr),
2336 le32_to_cpu(bhdr0->match_mask),
2337 le32_to_cpu(bhdr0->match_value));
2338
2339 fprintf(f, "BINARY binary%d.bin", cur_idx);
2340 params_count = fls4(bhdr0->params_flags & 0xF);
2341 for (i = 0; i < params_count; i++)
2342 fprintf(f, " 0x%x", (bhdr0->params[i] & (1 << i)) ? bhdr0->params[i] : 0);
2343 fprintf(f, " LOAD_ADDRESS 0x%08x", le32_to_cpu(bhdr0->destaddr));
2344 fprintf(f, " EXEC_ADDRESS 0x%08x", le32_to_cpu(bhdr0->execaddr));
2345 fprintf(f, "\n");
2346
2347 fprintf(f, "#BINARY_OFFSET 0x%x\n", le32_to_cpu(bhdr0->offset));
2348 fprintf(f, "#BINARY_SIZE 0x%x\n", le32_to_cpu(bhdr0->size));
2349
2350 if (bhdr0->rsvd1)
2351 fprintf(f, "#BINARY_RSVD1 0x%x\n", (unsigned)bhdr0->rsvd1);
2352 if (bhdr0->rsvd2)
2353 fprintf(f, "#BINARY_RSVD2 0x%x\n", (unsigned)bhdr0->rsvd2);
2354
2355 cur_idx++;
2356 }
Pali Rohár1a8e6b62022-01-12 18:20:50 +01002357
2358 /* Undocumented reserved fields */
2359
2360 if (version == 0 && (mhdr0->rsvd1[0] || mhdr0->rsvd1[1] || mhdr0->rsvd1[2]))
2361 fprintf(f, "#RSVD1 0x%x 0x%x 0x%x\n", (unsigned)mhdr0->rsvd1[0],
2362 (unsigned)mhdr0->rsvd1[1], (unsigned)mhdr0->rsvd1[2]);
2363
Pali Rohár1a8e6b62022-01-12 18:20:50 +01002364 if (version == 0 && le16_to_cpu(mhdr0->rsvd2))
2365 fprintf(f, "#RSVD2 0x%x\n", (unsigned)le16_to_cpu(mhdr0->rsvd2));
2366
2367 if (version != 0 && mhdr->reserved4)
2368 fprintf(f, "#RESERVED4 0x%x\n", (unsigned)mhdr->reserved4);
2369
2370 if (version != 0 && mhdr->reserved5)
2371 fprintf(f, "#RESERVED5 0x%x\n", (unsigned)le16_to_cpu(mhdr->reserved5));
2372
2373 fclose(f);
2374
2375 return 0;
2376}
2377
Pali Roháraa6943c2021-07-23 11:14:34 +02002378static int kwbimage_extract_subimage(void *ptr, struct image_tool_params *params)
2379{
2380 struct main_hdr_v1 *mhdr = (struct main_hdr_v1 *)ptr;
Marek Behúnfe2fd732021-09-24 23:07:01 +02002381 size_t header_size = kwbheader_size(ptr);
Pali Rohárf76ae252022-02-17 10:43:36 +01002382 struct bin_hdr_v0 *bhdr;
Marek Behún732c9302021-08-18 00:59:15 +02002383 struct opt_hdr_v1 *ohdr;
Pali Roháraa6943c2021-07-23 11:14:34 +02002384 int idx = params->pflag;
Pali Rohár1972c7e2022-01-12 18:20:53 +01002385 int cur_idx;
Pali Roháraa6943c2021-07-23 11:14:34 +02002386 uint32_t offset;
2387 ulong image;
2388 ulong size;
2389
Pali Rohár1a8e6b62022-01-12 18:20:50 +01002390 /* Generate kwbimage config file when '-p -1' is specified */
2391 if (idx == -1)
2392 return kwbimage_generate_config(ptr, params);
2393
Pali Rohár1972c7e2022-01-12 18:20:53 +01002394 image = 0;
2395 size = 0;
Pali Roháraa6943c2021-07-23 11:14:34 +02002396
Pali Rohár1972c7e2022-01-12 18:20:53 +01002397 if (idx == 0) {
2398 /* Extract data image when -p is not specified or when '-p 0' is specified */
2399 offset = le32_to_cpu(mhdr->srcaddr);
2400
2401 if (mhdr->blockid == IBR_HDR_SATA_ID) {
2402 offset -= 1;
2403 offset *= 512;
Pali Roháraa6943c2021-07-23 11:14:34 +02002404 }
Marek Behún732c9302021-08-18 00:59:15 +02002405
Pali Rohár1972c7e2022-01-12 18:20:53 +01002406 if (mhdr->blockid == IBR_HDR_SDIO_ID)
2407 offset *= 512;
2408
2409 if (mhdr->blockid == IBR_HDR_PEX_ID && offset == 0xFFFFFFFF)
2410 offset = header_size;
2411
2412 image = (ulong)((uint8_t *)ptr + offset);
2413 size = le32_to_cpu(mhdr->blocksize) - 4;
2414 } else {
2415 /* Extract N-th binary header executabe image when other '-p N' is specified */
2416 cur_idx = 1;
2417 for_each_opt_hdr_v1(ohdr, ptr) {
2418 if (ohdr->headertype != OPT_HDR_V1_BINARY_TYPE)
2419 continue;
2420
2421 if (idx == cur_idx) {
2422 image = (ulong)&ohdr->data[4 + 4 * ohdr->data[0]];
2423 size = opt_hdr_v1_size(ohdr) - 12 - 4 * ohdr->data[0];
2424 break;
2425 }
2426
2427 ++cur_idx;
2428 }
Pali Rohárf76ae252022-02-17 10:43:36 +01002429 for_each_bin_hdr_v0(bhdr, ptr) {
2430 if (idx == cur_idx) {
2431 image = (ulong)bhdr + bhdr->offset;
2432 size = bhdr->size;
2433 break;
2434 }
2435 ++cur_idx;
2436 }
Pali Rohár1972c7e2022-01-12 18:20:53 +01002437
2438 if (!image) {
2439 fprintf(stderr, "Argument -p %d is invalid\n", idx);
2440 fprintf(stderr, "Available subimages:\n");
2441 fprintf(stderr, " -p -1 - kwbimage config file\n");
2442 fprintf(stderr, " -p 0 - data image\n");
2443 if (cur_idx - 1 > 0)
2444 fprintf(stderr, " -p N - Nth binary header image (totally: %d)\n",
2445 cur_idx - 1);
2446 return -1;
2447 }
Pali Roháraa6943c2021-07-23 11:14:34 +02002448 }
2449
Pali Roháraa6943c2021-07-23 11:14:34 +02002450 return imagetool_save_subimage(params->outfile, image, size);
2451}
2452
Stefan Roese4acd2d22014-10-22 12:13:23 +02002453/*
2454 * Report Error if xflag is set in addition to default
2455 */
2456static int kwbimage_check_params(struct image_tool_params *params)
2457{
Pali Roháre65ea142022-02-17 10:43:37 +01002458 if (!params->lflag && !params->iflag && !params->pflag &&
Pali Rohár32860b02022-01-12 18:20:54 +01002459 (!params->imagename || !strlen(params->imagename))) {
Mario Six94490a42017-01-11 16:00:54 +01002460 char *msg = "Configuration file for kwbimage creation omitted";
2461
2462 fprintf(stderr, "Error:%s - %s\n", params->cmdname, msg);
Pali Rohár56087c12021-11-08 18:12:45 +01002463 return 1;
Stefan Roese4acd2d22014-10-22 12:13:23 +02002464 }
2465
2466 return (params->dflag && (params->fflag || params->lflag)) ||
2467 (params->fflag && (params->dflag || params->lflag)) ||
2468 (params->lflag && (params->dflag || params->fflag)) ||
Pali Roháraa6943c2021-07-23 11:14:34 +02002469 (params->xflag);
Stefan Roese4acd2d22014-10-22 12:13:23 +02002470}
2471
Prafulla Wadaskaraa0c7a82009-09-07 15:05:02 +05302472/*
2473 * kwbimage type parameters definition
2474 */
Guilherme Maciel Ferreiraa93648d2015-01-15 02:48:07 -02002475U_BOOT_IMAGE_TYPE(
2476 kwbimage,
2477 "Marvell MVEBU Boot Image support",
2478 0,
2479 NULL,
2480 kwbimage_check_params,
2481 kwbimage_verify_header,
2482 kwbimage_print_header,
2483 kwbimage_set_header,
Pali Roháraa6943c2021-07-23 11:14:34 +02002484 kwbimage_extract_subimage,
Guilherme Maciel Ferreiraa93648d2015-01-15 02:48:07 -02002485 kwbimage_check_image_types,
2486 NULL,
2487 kwbimage_generate
2488);