kwbimage.c 44 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959
  1. // SPDX-License-Identifier: GPL-2.0+
  2. /*
  3. * Image manipulator for Marvell SoCs
  4. * supports Kirkwood, Dove, Armada 370, Armada XP, and Armada 38x
  5. *
  6. * (C) Copyright 2013 Thomas Petazzoni
  7. * <thomas.petazzoni@free-electrons.com>
  8. */
  9. #include "imagetool.h"
  10. #include <limits.h>
  11. #include <image.h>
  12. #include <stdarg.h>
  13. #include <stdint.h>
  14. #include "kwbimage.h"
  15. #include <openssl/bn.h>
  16. #include <openssl/rsa.h>
  17. #include <openssl/pem.h>
  18. #include <openssl/err.h>
  19. #include <openssl/evp.h>
  20. #if OPENSSL_VERSION_NUMBER < 0x10100000L || \
  21. (defined(LIBRESSL_VERSION_NUMBER) && LIBRESSL_VERSION_NUMBER < 0x2070000fL)
  22. static void RSA_get0_key(const RSA *r,
  23. const BIGNUM **n, const BIGNUM **e, const BIGNUM **d)
  24. {
  25. if (n != NULL)
  26. *n = r->n;
  27. if (e != NULL)
  28. *e = r->e;
  29. if (d != NULL)
  30. *d = r->d;
  31. }
  32. #elif !defined(LIBRESSL_VERSION_NUMBER)
  33. void EVP_MD_CTX_cleanup(EVP_MD_CTX *ctx)
  34. {
  35. EVP_MD_CTX_reset(ctx);
  36. }
  37. #endif
  38. static struct image_cfg_element *image_cfg;
  39. static int cfgn;
  40. static int verbose_mode;
  41. struct boot_mode {
  42. unsigned int id;
  43. const char *name;
  44. };
  45. /*
  46. * SHA2-256 hash
  47. */
  48. struct hash_v1 {
  49. uint8_t hash[32];
  50. };
  51. struct boot_mode boot_modes[] = {
  52. { IBR_HDR_I2C_ID, "i2c" },
  53. { IBR_HDR_SPI_ID, "spi" },
  54. { IBR_HDR_NAND_ID, "nand" },
  55. { IBR_HDR_SATA_ID, "sata" },
  56. { IBR_HDR_PEX_ID, "pex" },
  57. { IBR_HDR_UART_ID, "uart" },
  58. { IBR_HDR_SDIO_ID, "sdio" },
  59. {},
  60. };
  61. struct nand_ecc_mode {
  62. unsigned int id;
  63. const char *name;
  64. };
  65. struct nand_ecc_mode nand_ecc_modes[] = {
  66. { IBR_HDR_ECC_DEFAULT, "default" },
  67. { IBR_HDR_ECC_FORCED_HAMMING, "hamming" },
  68. { IBR_HDR_ECC_FORCED_RS, "rs" },
  69. { IBR_HDR_ECC_DISABLED, "disabled" },
  70. {},
  71. };
  72. /* Used to identify an undefined execution or destination address */
  73. #define ADDR_INVALID ((uint32_t)-1)
  74. #define BINARY_MAX_ARGS 255
  75. /* In-memory representation of a line of the configuration file */
  76. enum image_cfg_type {
  77. IMAGE_CFG_VERSION = 0x1,
  78. IMAGE_CFG_BOOT_FROM,
  79. IMAGE_CFG_DEST_ADDR,
  80. IMAGE_CFG_EXEC_ADDR,
  81. IMAGE_CFG_NAND_BLKSZ,
  82. IMAGE_CFG_NAND_BADBLK_LOCATION,
  83. IMAGE_CFG_NAND_ECC_MODE,
  84. IMAGE_CFG_NAND_PAGESZ,
  85. IMAGE_CFG_BINARY,
  86. IMAGE_CFG_DATA,
  87. IMAGE_CFG_DATA_DELAY,
  88. IMAGE_CFG_BAUDRATE,
  89. IMAGE_CFG_DEBUG,
  90. IMAGE_CFG_KAK,
  91. IMAGE_CFG_CSK,
  92. IMAGE_CFG_CSK_INDEX,
  93. IMAGE_CFG_JTAG_DELAY,
  94. IMAGE_CFG_BOX_ID,
  95. IMAGE_CFG_FLASH_ID,
  96. IMAGE_CFG_SEC_COMMON_IMG,
  97. IMAGE_CFG_SEC_SPECIALIZED_IMG,
  98. IMAGE_CFG_SEC_BOOT_DEV,
  99. IMAGE_CFG_SEC_FUSE_DUMP,
  100. IMAGE_CFG_COUNT
  101. } type;
  102. static const char * const id_strs[] = {
  103. [IMAGE_CFG_VERSION] = "VERSION",
  104. [IMAGE_CFG_BOOT_FROM] = "BOOT_FROM",
  105. [IMAGE_CFG_DEST_ADDR] = "DEST_ADDR",
  106. [IMAGE_CFG_EXEC_ADDR] = "EXEC_ADDR",
  107. [IMAGE_CFG_NAND_BLKSZ] = "NAND_BLKSZ",
  108. [IMAGE_CFG_NAND_BADBLK_LOCATION] = "NAND_BADBLK_LOCATION",
  109. [IMAGE_CFG_NAND_ECC_MODE] = "NAND_ECC_MODE",
  110. [IMAGE_CFG_NAND_PAGESZ] = "NAND_PAGE_SIZE",
  111. [IMAGE_CFG_BINARY] = "BINARY",
  112. [IMAGE_CFG_DATA] = "DATA",
  113. [IMAGE_CFG_DATA_DELAY] = "DATA_DELAY",
  114. [IMAGE_CFG_BAUDRATE] = "BAUDRATE",
  115. [IMAGE_CFG_DEBUG] = "DEBUG",
  116. [IMAGE_CFG_KAK] = "KAK",
  117. [IMAGE_CFG_CSK] = "CSK",
  118. [IMAGE_CFG_CSK_INDEX] = "CSK_INDEX",
  119. [IMAGE_CFG_JTAG_DELAY] = "JTAG_DELAY",
  120. [IMAGE_CFG_BOX_ID] = "BOX_ID",
  121. [IMAGE_CFG_FLASH_ID] = "FLASH_ID",
  122. [IMAGE_CFG_SEC_COMMON_IMG] = "SEC_COMMON_IMG",
  123. [IMAGE_CFG_SEC_SPECIALIZED_IMG] = "SEC_SPECIALIZED_IMG",
  124. [IMAGE_CFG_SEC_BOOT_DEV] = "SEC_BOOT_DEV",
  125. [IMAGE_CFG_SEC_FUSE_DUMP] = "SEC_FUSE_DUMP"
  126. };
  127. struct image_cfg_element {
  128. enum image_cfg_type type;
  129. union {
  130. unsigned int version;
  131. unsigned int bootfrom;
  132. struct {
  133. const char *file;
  134. unsigned int args[BINARY_MAX_ARGS];
  135. unsigned int nargs;
  136. } binary;
  137. unsigned int dstaddr;
  138. unsigned int execaddr;
  139. unsigned int nandblksz;
  140. unsigned int nandbadblklocation;
  141. unsigned int nandeccmode;
  142. unsigned int nandpagesz;
  143. struct ext_hdr_v0_reg regdata;
  144. unsigned int regdata_delay;
  145. unsigned int baudrate;
  146. unsigned int debug;
  147. const char *key_name;
  148. int csk_idx;
  149. uint8_t jtag_delay;
  150. uint32_t boxid;
  151. uint32_t flashid;
  152. bool sec_specialized_img;
  153. unsigned int sec_boot_dev;
  154. const char *name;
  155. };
  156. };
  157. #define IMAGE_CFG_ELEMENT_MAX 256
  158. /*
  159. * Utility functions to manipulate boot mode and ecc modes (convert
  160. * them back and forth between description strings and the
  161. * corresponding numerical identifiers).
  162. */
  163. static const char *image_boot_mode_name(unsigned int id)
  164. {
  165. int i;
  166. for (i = 0; boot_modes[i].name; i++)
  167. if (boot_modes[i].id == id)
  168. return boot_modes[i].name;
  169. return NULL;
  170. }
  171. int image_boot_mode_id(const char *boot_mode_name)
  172. {
  173. int i;
  174. for (i = 0; boot_modes[i].name; i++)
  175. if (!strcmp(boot_modes[i].name, boot_mode_name))
  176. return boot_modes[i].id;
  177. return -1;
  178. }
  179. int image_nand_ecc_mode_id(const char *nand_ecc_mode_name)
  180. {
  181. int i;
  182. for (i = 0; nand_ecc_modes[i].name; i++)
  183. if (!strcmp(nand_ecc_modes[i].name, nand_ecc_mode_name))
  184. return nand_ecc_modes[i].id;
  185. return -1;
  186. }
  187. static struct image_cfg_element *
  188. image_find_option(unsigned int optiontype)
  189. {
  190. int i;
  191. for (i = 0; i < cfgn; i++) {
  192. if (image_cfg[i].type == optiontype)
  193. return &image_cfg[i];
  194. }
  195. return NULL;
  196. }
  197. static unsigned int
  198. image_count_options(unsigned int optiontype)
  199. {
  200. int i;
  201. unsigned int count = 0;
  202. for (i = 0; i < cfgn; i++)
  203. if (image_cfg[i].type == optiontype)
  204. count++;
  205. return count;
  206. }
  207. static int image_get_csk_index(void)
  208. {
  209. struct image_cfg_element *e;
  210. e = image_find_option(IMAGE_CFG_CSK_INDEX);
  211. if (!e)
  212. return -1;
  213. return e->csk_idx;
  214. }
  215. static bool image_get_spezialized_img(void)
  216. {
  217. struct image_cfg_element *e;
  218. e = image_find_option(IMAGE_CFG_SEC_SPECIALIZED_IMG);
  219. if (!e)
  220. return false;
  221. return e->sec_specialized_img;
  222. }
  223. /*
  224. * Compute a 8-bit checksum of a memory area. This algorithm follows
  225. * the requirements of the Marvell SoC BootROM specifications.
  226. */
  227. static uint8_t image_checksum8(void *start, uint32_t len)
  228. {
  229. uint8_t csum = 0;
  230. uint8_t *p = start;
  231. /* check len and return zero checksum if invalid */
  232. if (!len)
  233. return 0;
  234. do {
  235. csum += *p;
  236. p++;
  237. } while (--len);
  238. return csum;
  239. }
  240. size_t kwbimage_header_size(unsigned char *ptr)
  241. {
  242. if (image_version((void *)ptr) == 0)
  243. return sizeof(struct main_hdr_v0);
  244. else
  245. return KWBHEADER_V1_SIZE((struct main_hdr_v1 *)ptr);
  246. }
  247. /*
  248. * Verify checksum over a complete header that includes the checksum field.
  249. * Return 1 when OK, otherwise 0.
  250. */
  251. static int main_hdr_checksum_ok(void *hdr)
  252. {
  253. /* Offsets of checksum in v0 and v1 headers are the same */
  254. struct main_hdr_v0 *main_hdr = (struct main_hdr_v0 *)hdr;
  255. uint8_t checksum;
  256. checksum = image_checksum8(hdr, kwbimage_header_size(hdr));
  257. /* Calculated checksum includes the header checksum field. Compensate
  258. * for that.
  259. */
  260. checksum -= main_hdr->checksum;
  261. return checksum == main_hdr->checksum;
  262. }
  263. static uint32_t image_checksum32(void *start, uint32_t len)
  264. {
  265. uint32_t csum = 0;
  266. uint32_t *p = start;
  267. /* check len and return zero checksum if invalid */
  268. if (!len)
  269. return 0;
  270. if (len % sizeof(uint32_t)) {
  271. fprintf(stderr, "Length %d is not in multiple of %zu\n",
  272. len, sizeof(uint32_t));
  273. return 0;
  274. }
  275. do {
  276. csum += *p;
  277. p++;
  278. len -= sizeof(uint32_t);
  279. } while (len > 0);
  280. return csum;
  281. }
  282. static uint8_t baudrate_to_option(unsigned int baudrate)
  283. {
  284. switch (baudrate) {
  285. case 2400:
  286. return MAIN_HDR_V1_OPT_BAUD_2400;
  287. case 4800:
  288. return MAIN_HDR_V1_OPT_BAUD_4800;
  289. case 9600:
  290. return MAIN_HDR_V1_OPT_BAUD_9600;
  291. case 19200:
  292. return MAIN_HDR_V1_OPT_BAUD_19200;
  293. case 38400:
  294. return MAIN_HDR_V1_OPT_BAUD_38400;
  295. case 57600:
  296. return MAIN_HDR_V1_OPT_BAUD_57600;
  297. case 115200:
  298. return MAIN_HDR_V1_OPT_BAUD_115200;
  299. default:
  300. return MAIN_HDR_V1_OPT_BAUD_DEFAULT;
  301. }
  302. }
  303. static void kwb_msg(const char *fmt, ...)
  304. {
  305. if (verbose_mode) {
  306. va_list ap;
  307. va_start(ap, fmt);
  308. vfprintf(stdout, fmt, ap);
  309. va_end(ap);
  310. }
  311. }
  312. static int openssl_err(const char *msg)
  313. {
  314. unsigned long ssl_err = ERR_get_error();
  315. fprintf(stderr, "%s", msg);
  316. fprintf(stderr, ": %s\n",
  317. ERR_error_string(ssl_err, 0));
  318. return -1;
  319. }
  320. static int kwb_load_rsa_key(const char *keydir, const char *name, RSA **p_rsa)
  321. {
  322. char path[PATH_MAX];
  323. RSA *rsa;
  324. FILE *f;
  325. if (!keydir)
  326. keydir = ".";
  327. snprintf(path, sizeof(path), "%s/%s.key", keydir, name);
  328. f = fopen(path, "r");
  329. if (!f) {
  330. fprintf(stderr, "Couldn't open RSA private key: '%s': %s\n",
  331. path, strerror(errno));
  332. return -ENOENT;
  333. }
  334. rsa = PEM_read_RSAPrivateKey(f, 0, NULL, "");
  335. if (!rsa) {
  336. openssl_err("Failure reading private key");
  337. fclose(f);
  338. return -EPROTO;
  339. }
  340. fclose(f);
  341. *p_rsa = rsa;
  342. return 0;
  343. }
  344. static int kwb_load_cfg_key(struct image_tool_params *params,
  345. unsigned int cfg_option, const char *key_name,
  346. RSA **p_key)
  347. {
  348. struct image_cfg_element *e_key;
  349. RSA *key;
  350. int res;
  351. *p_key = NULL;
  352. e_key = image_find_option(cfg_option);
  353. if (!e_key) {
  354. fprintf(stderr, "%s not configured\n", key_name);
  355. return -ENOENT;
  356. }
  357. res = kwb_load_rsa_key(params->keydir, e_key->key_name, &key);
  358. if (res < 0) {
  359. fprintf(stderr, "Failed to load %s\n", key_name);
  360. return -ENOENT;
  361. }
  362. *p_key = key;
  363. return 0;
  364. }
  365. static int kwb_load_kak(struct image_tool_params *params, RSA **p_kak)
  366. {
  367. return kwb_load_cfg_key(params, IMAGE_CFG_KAK, "KAK", p_kak);
  368. }
  369. static int kwb_load_csk(struct image_tool_params *params, RSA **p_csk)
  370. {
  371. return kwb_load_cfg_key(params, IMAGE_CFG_CSK, "CSK", p_csk);
  372. }
  373. static int kwb_compute_pubkey_hash(struct pubkey_der_v1 *pk,
  374. struct hash_v1 *hash)
  375. {
  376. EVP_MD_CTX *ctx;
  377. unsigned int key_size;
  378. unsigned int hash_size;
  379. int ret = 0;
  380. if (!pk || !hash || pk->key[0] != 0x30 || pk->key[1] != 0x82)
  381. return -EINVAL;
  382. key_size = (pk->key[2] << 8) + pk->key[3] + 4;
  383. ctx = EVP_MD_CTX_create();
  384. if (!ctx)
  385. return openssl_err("EVP context creation failed");
  386. EVP_MD_CTX_init(ctx);
  387. if (!EVP_DigestInit(ctx, EVP_sha256())) {
  388. ret = openssl_err("Digest setup failed");
  389. goto hash_err_ctx;
  390. }
  391. if (!EVP_DigestUpdate(ctx, pk->key, key_size)) {
  392. ret = openssl_err("Hashing data failed");
  393. goto hash_err_ctx;
  394. }
  395. if (!EVP_DigestFinal(ctx, hash->hash, &hash_size)) {
  396. ret = openssl_err("Could not obtain hash");
  397. goto hash_err_ctx;
  398. }
  399. EVP_MD_CTX_cleanup(ctx);
  400. hash_err_ctx:
  401. EVP_MD_CTX_destroy(ctx);
  402. return ret;
  403. }
  404. static int kwb_import_pubkey(RSA **key, struct pubkey_der_v1 *src, char *keyname)
  405. {
  406. RSA *rsa;
  407. const unsigned char *ptr;
  408. if (!key || !src)
  409. goto fail;
  410. ptr = src->key;
  411. rsa = d2i_RSAPublicKey(key, &ptr, sizeof(src->key));
  412. if (!rsa) {
  413. openssl_err("error decoding public key");
  414. goto fail;
  415. }
  416. return 0;
  417. fail:
  418. fprintf(stderr, "Failed to decode %s pubkey\n", keyname);
  419. return -EINVAL;
  420. }
  421. static int kwb_export_pubkey(RSA *key, struct pubkey_der_v1 *dst, FILE *hashf,
  422. char *keyname)
  423. {
  424. int size_exp, size_mod, size_seq;
  425. const BIGNUM *key_e, *key_n;
  426. uint8_t *cur;
  427. char *errmsg = "Failed to encode %s\n";
  428. RSA_get0_key(key, NULL, &key_e, NULL);
  429. RSA_get0_key(key, &key_n, NULL, NULL);
  430. if (!key || !key_e || !key_n || !dst) {
  431. fprintf(stderr, "export pk failed: (%p, %p, %p, %p)",
  432. key, key_e, key_n, dst);
  433. fprintf(stderr, errmsg, keyname);
  434. return -EINVAL;
  435. }
  436. /*
  437. * According to the specs, the key should be PKCS#1 DER encoded.
  438. * But unfortunately the really required encoding seems to be different;
  439. * it violates DER...! (But it still conformes to BER.)
  440. * (Length always in long form w/ 2 byte length code; no leading zero
  441. * when MSB of first byte is set...)
  442. * So we cannot use the encoding func provided by OpenSSL and have to
  443. * do the encoding manually.
  444. */
  445. size_exp = BN_num_bytes(key_e);
  446. size_mod = BN_num_bytes(key_n);
  447. size_seq = 4 + size_mod + 4 + size_exp;
  448. if (size_mod > 256) {
  449. fprintf(stderr, "export pk failed: wrong mod size: %d\n",
  450. size_mod);
  451. fprintf(stderr, errmsg, keyname);
  452. return -EINVAL;
  453. }
  454. if (4 + size_seq > sizeof(dst->key)) {
  455. fprintf(stderr, "export pk failed: seq too large (%d, %lu)\n",
  456. 4 + size_seq, sizeof(dst->key));
  457. fprintf(stderr, errmsg, keyname);
  458. return -ENOBUFS;
  459. }
  460. cur = dst->key;
  461. /* PKCS#1 (RFC3447) RSAPublicKey structure */
  462. *cur++ = 0x30; /* SEQUENCE */
  463. *cur++ = 0x82;
  464. *cur++ = (size_seq >> 8) & 0xFF;
  465. *cur++ = size_seq & 0xFF;
  466. /* Modulus */
  467. *cur++ = 0x02; /* INTEGER */
  468. *cur++ = 0x82;
  469. *cur++ = (size_mod >> 8) & 0xFF;
  470. *cur++ = size_mod & 0xFF;
  471. BN_bn2bin(key_n, cur);
  472. cur += size_mod;
  473. /* Exponent */
  474. *cur++ = 0x02; /* INTEGER */
  475. *cur++ = 0x82;
  476. *cur++ = (size_exp >> 8) & 0xFF;
  477. *cur++ = size_exp & 0xFF;
  478. BN_bn2bin(key_e, cur);
  479. if (hashf) {
  480. struct hash_v1 pk_hash;
  481. int i;
  482. int ret = 0;
  483. ret = kwb_compute_pubkey_hash(dst, &pk_hash);
  484. if (ret < 0) {
  485. fprintf(stderr, errmsg, keyname);
  486. return ret;
  487. }
  488. fprintf(hashf, "SHA256 = ");
  489. for (i = 0 ; i < sizeof(pk_hash.hash); ++i)
  490. fprintf(hashf, "%02X", pk_hash.hash[i]);
  491. fprintf(hashf, "\n");
  492. }
  493. return 0;
  494. }
  495. int kwb_sign(RSA *key, void *data, int datasz, struct sig_v1 *sig, char *signame)
  496. {
  497. EVP_PKEY *evp_key;
  498. EVP_MD_CTX *ctx;
  499. unsigned int sig_size;
  500. int size;
  501. int ret = 0;
  502. evp_key = EVP_PKEY_new();
  503. if (!evp_key)
  504. return openssl_err("EVP_PKEY object creation failed");
  505. if (!EVP_PKEY_set1_RSA(evp_key, key)) {
  506. ret = openssl_err("EVP key setup failed");
  507. goto err_key;
  508. }
  509. size = EVP_PKEY_size(evp_key);
  510. if (size > sizeof(sig->sig)) {
  511. fprintf(stderr, "Buffer to small for signature (%d bytes)\n",
  512. size);
  513. ret = -ENOBUFS;
  514. goto err_key;
  515. }
  516. ctx = EVP_MD_CTX_create();
  517. if (!ctx) {
  518. ret = openssl_err("EVP context creation failed");
  519. goto err_key;
  520. }
  521. EVP_MD_CTX_init(ctx);
  522. if (!EVP_SignInit(ctx, EVP_sha256())) {
  523. ret = openssl_err("Signer setup failed");
  524. goto err_ctx;
  525. }
  526. if (!EVP_SignUpdate(ctx, data, datasz)) {
  527. ret = openssl_err("Signing data failed");
  528. goto err_ctx;
  529. }
  530. if (!EVP_SignFinal(ctx, sig->sig, &sig_size, evp_key)) {
  531. ret = openssl_err("Could not obtain signature");
  532. goto err_ctx;
  533. }
  534. EVP_MD_CTX_cleanup(ctx);
  535. EVP_MD_CTX_destroy(ctx);
  536. EVP_PKEY_free(evp_key);
  537. return 0;
  538. err_ctx:
  539. EVP_MD_CTX_destroy(ctx);
  540. err_key:
  541. EVP_PKEY_free(evp_key);
  542. fprintf(stderr, "Failed to create %s signature\n", signame);
  543. return ret;
  544. }
  545. int kwb_verify(RSA *key, void *data, int datasz, struct sig_v1 *sig,
  546. char *signame)
  547. {
  548. EVP_PKEY *evp_key;
  549. EVP_MD_CTX *ctx;
  550. int size;
  551. int ret = 0;
  552. evp_key = EVP_PKEY_new();
  553. if (!evp_key)
  554. return openssl_err("EVP_PKEY object creation failed");
  555. if (!EVP_PKEY_set1_RSA(evp_key, key)) {
  556. ret = openssl_err("EVP key setup failed");
  557. goto err_key;
  558. }
  559. size = EVP_PKEY_size(evp_key);
  560. if (size > sizeof(sig->sig)) {
  561. fprintf(stderr, "Invalid signature size (%d bytes)\n",
  562. size);
  563. ret = -EINVAL;
  564. goto err_key;
  565. }
  566. ctx = EVP_MD_CTX_create();
  567. if (!ctx) {
  568. ret = openssl_err("EVP context creation failed");
  569. goto err_key;
  570. }
  571. EVP_MD_CTX_init(ctx);
  572. if (!EVP_VerifyInit(ctx, EVP_sha256())) {
  573. ret = openssl_err("Verifier setup failed");
  574. goto err_ctx;
  575. }
  576. if (!EVP_VerifyUpdate(ctx, data, datasz)) {
  577. ret = openssl_err("Hashing data failed");
  578. goto err_ctx;
  579. }
  580. if (EVP_VerifyFinal(ctx, sig->sig, sizeof(sig->sig), evp_key) != 1) {
  581. ret = openssl_err("Could not verify signature");
  582. goto err_ctx;
  583. }
  584. EVP_MD_CTX_cleanup(ctx);
  585. EVP_MD_CTX_destroy(ctx);
  586. EVP_PKEY_free(evp_key);
  587. return 0;
  588. err_ctx:
  589. EVP_MD_CTX_destroy(ctx);
  590. err_key:
  591. EVP_PKEY_free(evp_key);
  592. fprintf(stderr, "Failed to verify %s signature\n", signame);
  593. return ret;
  594. }
  595. int kwb_sign_and_verify(RSA *key, void *data, int datasz, struct sig_v1 *sig,
  596. char *signame)
  597. {
  598. if (kwb_sign(key, data, datasz, sig, signame) < 0)
  599. return -1;
  600. if (kwb_verify(key, data, datasz, sig, signame) < 0)
  601. return -1;
  602. return 0;
  603. }
  604. int kwb_dump_fuse_cmds_38x(FILE *out, struct secure_hdr_v1 *sec_hdr)
  605. {
  606. struct hash_v1 kak_pub_hash;
  607. struct image_cfg_element *e;
  608. unsigned int fuse_line;
  609. int i, idx;
  610. uint8_t *ptr;
  611. uint32_t val;
  612. int ret = 0;
  613. if (!out || !sec_hdr)
  614. return -EINVAL;
  615. ret = kwb_compute_pubkey_hash(&sec_hdr->kak, &kak_pub_hash);
  616. if (ret < 0)
  617. goto done;
  618. fprintf(out, "# burn KAK pub key hash\n");
  619. ptr = kak_pub_hash.hash;
  620. for (fuse_line = 26; fuse_line <= 30; ++fuse_line) {
  621. fprintf(out, "fuse prog -y %u 0 ", fuse_line);
  622. for (i = 4; i-- > 0;)
  623. fprintf(out, "%02hx", (ushort)ptr[i]);
  624. ptr += 4;
  625. fprintf(out, " 00");
  626. if (fuse_line < 30) {
  627. for (i = 3; i-- > 0;)
  628. fprintf(out, "%02hx", (ushort)ptr[i]);
  629. ptr += 3;
  630. } else {
  631. fprintf(out, "000000");
  632. }
  633. fprintf(out, " 1\n");
  634. }
  635. fprintf(out, "# burn CSK selection\n");
  636. idx = image_get_csk_index();
  637. if (idx < 0 || idx > 15) {
  638. ret = -EINVAL;
  639. goto done;
  640. }
  641. if (idx > 0) {
  642. for (fuse_line = 31; fuse_line < 31 + idx; ++fuse_line)
  643. fprintf(out, "fuse prog -y %u 0 00000001 00000000 1\n",
  644. fuse_line);
  645. } else {
  646. fprintf(out, "# CSK index is 0; no mods needed\n");
  647. }
  648. e = image_find_option(IMAGE_CFG_BOX_ID);
  649. if (e) {
  650. fprintf(out, "# set box ID\n");
  651. fprintf(out, "fuse prog -y 48 0 %08x 00000000 1\n", e->boxid);
  652. }
  653. e = image_find_option(IMAGE_CFG_FLASH_ID);
  654. if (e) {
  655. fprintf(out, "# set flash ID\n");
  656. fprintf(out, "fuse prog -y 47 0 %08x 00000000 1\n", e->flashid);
  657. }
  658. fprintf(out, "# enable secure mode ");
  659. fprintf(out, "(must be the last fuse line written)\n");
  660. val = 1;
  661. e = image_find_option(IMAGE_CFG_SEC_BOOT_DEV);
  662. if (!e) {
  663. fprintf(stderr, "ERROR: secured mode boot device not given\n");
  664. ret = -EINVAL;
  665. goto done;
  666. }
  667. if (e->sec_boot_dev > 0xff) {
  668. fprintf(stderr, "ERROR: secured mode boot device invalid\n");
  669. ret = -EINVAL;
  670. goto done;
  671. }
  672. val |= (e->sec_boot_dev << 8);
  673. fprintf(out, "fuse prog -y 24 0 %08x 0103e0a9 1\n", val);
  674. fprintf(out, "# lock (unused) fuse lines (0-23)s\n");
  675. for (fuse_line = 0; fuse_line < 24; ++fuse_line)
  676. fprintf(out, "fuse prog -y %u 2 1\n", fuse_line);
  677. fprintf(out, "# OK, that's all :-)\n");
  678. done:
  679. return ret;
  680. }
  681. static int kwb_dump_fuse_cmds(struct secure_hdr_v1 *sec_hdr)
  682. {
  683. int ret = 0;
  684. struct image_cfg_element *e;
  685. e = image_find_option(IMAGE_CFG_SEC_FUSE_DUMP);
  686. if (!e)
  687. return 0;
  688. if (!strcmp(e->name, "a38x")) {
  689. FILE *out = fopen("kwb_fuses_a38x.txt", "w+");
  690. if (!out) {
  691. fprintf(stderr, "Couldn't open eFuse settings: '%s': %s\n",
  692. "kwb_fuses_a38x.txt", strerror(errno));
  693. return -ENOENT;
  694. }
  695. kwb_dump_fuse_cmds_38x(out, sec_hdr);
  696. fclose(out);
  697. goto done;
  698. }
  699. ret = -ENOSYS;
  700. done:
  701. return ret;
  702. }
  703. static void *image_create_v0(size_t *imagesz, struct image_tool_params *params,
  704. int payloadsz)
  705. {
  706. struct image_cfg_element *e;
  707. size_t headersz;
  708. struct main_hdr_v0 *main_hdr;
  709. uint8_t *image;
  710. int has_ext = 0;
  711. /*
  712. * Calculate the size of the header and the size of the
  713. * payload
  714. */
  715. headersz = sizeof(struct main_hdr_v0);
  716. if (image_count_options(IMAGE_CFG_DATA) > 0) {
  717. has_ext = 1;
  718. headersz += sizeof(struct ext_hdr_v0);
  719. }
  720. image = malloc(headersz);
  721. if (!image) {
  722. fprintf(stderr, "Cannot allocate memory for image\n");
  723. return NULL;
  724. }
  725. memset(image, 0, headersz);
  726. main_hdr = (struct main_hdr_v0 *)image;
  727. /* Fill in the main header */
  728. main_hdr->blocksize =
  729. cpu_to_le32(payloadsz - headersz);
  730. main_hdr->srcaddr = cpu_to_le32(headersz);
  731. main_hdr->ext = has_ext;
  732. main_hdr->destaddr = cpu_to_le32(params->addr);
  733. main_hdr->execaddr = cpu_to_le32(params->ep);
  734. e = image_find_option(IMAGE_CFG_BOOT_FROM);
  735. if (e)
  736. main_hdr->blockid = e->bootfrom;
  737. e = image_find_option(IMAGE_CFG_NAND_ECC_MODE);
  738. if (e)
  739. main_hdr->nandeccmode = e->nandeccmode;
  740. e = image_find_option(IMAGE_CFG_NAND_PAGESZ);
  741. if (e)
  742. main_hdr->nandpagesize = cpu_to_le16(e->nandpagesz);
  743. main_hdr->checksum = image_checksum8(image,
  744. sizeof(struct main_hdr_v0));
  745. /* Generate the ext header */
  746. if (has_ext) {
  747. struct ext_hdr_v0 *ext_hdr;
  748. int cfgi, datai;
  749. ext_hdr = (struct ext_hdr_v0 *)
  750. (image + sizeof(struct main_hdr_v0));
  751. ext_hdr->offset = cpu_to_le32(0x40);
  752. for (cfgi = 0, datai = 0; cfgi < cfgn; cfgi++) {
  753. e = &image_cfg[cfgi];
  754. if (e->type != IMAGE_CFG_DATA)
  755. continue;
  756. ext_hdr->rcfg[datai].raddr =
  757. cpu_to_le32(e->regdata.raddr);
  758. ext_hdr->rcfg[datai].rdata =
  759. cpu_to_le32(e->regdata.rdata);
  760. datai++;
  761. }
  762. ext_hdr->checksum = image_checksum8(ext_hdr,
  763. sizeof(struct ext_hdr_v0));
  764. }
  765. *imagesz = headersz;
  766. return image;
  767. }
  768. static size_t image_headersz_v1(int *hasext)
  769. {
  770. struct image_cfg_element *binarye;
  771. unsigned int count;
  772. size_t headersz;
  773. int cfgi;
  774. /*
  775. * Calculate the size of the header and the size of the
  776. * payload
  777. */
  778. headersz = sizeof(struct main_hdr_v1);
  779. count = image_count_options(IMAGE_CFG_DATA);
  780. if (count > 0)
  781. headersz += sizeof(struct register_set_hdr_v1) + 8 * count + 4;
  782. for (cfgi = 0; cfgi < cfgn; cfgi++) {
  783. int ret;
  784. struct stat s;
  785. binarye = &image_cfg[cfgi];
  786. if (binarye->type != IMAGE_CFG_BINARY)
  787. continue;
  788. ret = stat(binarye->binary.file, &s);
  789. if (ret < 0) {
  790. char cwd[PATH_MAX];
  791. char *dir = cwd;
  792. memset(cwd, 0, sizeof(cwd));
  793. if (!getcwd(cwd, sizeof(cwd))) {
  794. dir = "current working directory";
  795. perror("getcwd() failed");
  796. }
  797. fprintf(stderr,
  798. "Didn't find the file '%s' in '%s' which is mandatory to generate the image\n"
  799. "This file generally contains the DDR3 training code, and should be extracted from an existing bootable\n"
  800. "image for your board. Use 'dumpimage -T kwbimage -p 0' to extract it from an existing image.\n",
  801. binarye->binary.file, dir);
  802. return 0;
  803. }
  804. headersz += sizeof(struct opt_hdr_v1) +
  805. ALIGN(s.st_size, 4) +
  806. (binarye->binary.nargs + 2) * sizeof(uint32_t);
  807. if (hasext)
  808. *hasext = 1;
  809. }
  810. if (image_get_csk_index() >= 0) {
  811. headersz += sizeof(struct secure_hdr_v1);
  812. if (hasext)
  813. *hasext = 1;
  814. }
  815. /*
  816. * The payload should be aligned on some reasonable
  817. * boundary
  818. */
  819. return ALIGN(headersz, 4096);
  820. }
  821. int add_binary_header_v1(uint8_t **cur, uint8_t **next_ext,
  822. struct image_cfg_element *binarye)
  823. {
  824. struct opt_hdr_v1 *hdr = (struct opt_hdr_v1 *)*cur;
  825. uint32_t *args;
  826. size_t binhdrsz;
  827. struct stat s;
  828. int argi;
  829. FILE *bin;
  830. int ret;
  831. hdr->headertype = OPT_HDR_V1_BINARY_TYPE;
  832. bin = fopen(binarye->binary.file, "r");
  833. if (!bin) {
  834. fprintf(stderr, "Cannot open binary file %s\n",
  835. binarye->binary.file);
  836. return -1;
  837. }
  838. if (fstat(fileno(bin), &s)) {
  839. fprintf(stderr, "Cannot stat binary file %s\n",
  840. binarye->binary.file);
  841. goto err_close;
  842. }
  843. binhdrsz = sizeof(struct opt_hdr_v1) +
  844. (binarye->binary.nargs + 2) * sizeof(uint32_t) +
  845. ALIGN(s.st_size, 4);
  846. hdr->headersz_lsb = cpu_to_le16(binhdrsz & 0xFFFF);
  847. hdr->headersz_msb = (binhdrsz & 0xFFFF0000) >> 16;
  848. *cur += sizeof(struct opt_hdr_v1);
  849. args = (uint32_t *)*cur;
  850. *args = cpu_to_le32(binarye->binary.nargs);
  851. args++;
  852. for (argi = 0; argi < binarye->binary.nargs; argi++)
  853. args[argi] = cpu_to_le32(binarye->binary.args[argi]);
  854. *cur += (binarye->binary.nargs + 1) * sizeof(uint32_t);
  855. ret = fread(*cur, s.st_size, 1, bin);
  856. if (ret != 1) {
  857. fprintf(stderr,
  858. "Could not read binary image %s\n",
  859. binarye->binary.file);
  860. goto err_close;
  861. }
  862. fclose(bin);
  863. *cur += ALIGN(s.st_size, 4);
  864. *((uint32_t *)*cur) = 0x00000000;
  865. **next_ext = 1;
  866. *next_ext = *cur;
  867. *cur += sizeof(uint32_t);
  868. return 0;
  869. err_close:
  870. fclose(bin);
  871. return -1;
  872. }
  873. int export_pub_kak_hash(RSA *kak, struct secure_hdr_v1 *secure_hdr)
  874. {
  875. FILE *hashf;
  876. int res;
  877. hashf = fopen("pub_kak_hash.txt", "w");
  878. if (!hashf) {
  879. fprintf(stderr, "Couldn't open hash file: '%s': %s\n",
  880. "pub_kak_hash.txt", strerror(errno));
  881. return 1;
  882. }
  883. res = kwb_export_pubkey(kak, &secure_hdr->kak, hashf, "KAK");
  884. fclose(hashf);
  885. return res < 0 ? 1 : 0;
  886. }
  887. int kwb_sign_csk_with_kak(struct image_tool_params *params,
  888. struct secure_hdr_v1 *secure_hdr, RSA *csk)
  889. {
  890. RSA *kak = NULL;
  891. RSA *kak_pub = NULL;
  892. int csk_idx = image_get_csk_index();
  893. struct sig_v1 tmp_sig;
  894. if (csk_idx < 0 || csk_idx > 15) {
  895. fprintf(stderr, "Invalid CSK index %d\n", csk_idx);
  896. return 1;
  897. }
  898. if (kwb_load_kak(params, &kak) < 0)
  899. return 1;
  900. if (export_pub_kak_hash(kak, secure_hdr))
  901. return 1;
  902. if (kwb_import_pubkey(&kak_pub, &secure_hdr->kak, "KAK") < 0)
  903. return 1;
  904. if (kwb_export_pubkey(csk, &secure_hdr->csk[csk_idx], NULL, "CSK") < 0)
  905. return 1;
  906. if (kwb_sign_and_verify(kak, &secure_hdr->csk,
  907. sizeof(secure_hdr->csk) +
  908. sizeof(secure_hdr->csksig),
  909. &tmp_sig, "CSK") < 0)
  910. return 1;
  911. if (kwb_verify(kak_pub, &secure_hdr->csk,
  912. sizeof(secure_hdr->csk) +
  913. sizeof(secure_hdr->csksig),
  914. &tmp_sig, "CSK (2)") < 0)
  915. return 1;
  916. secure_hdr->csksig = tmp_sig;
  917. return 0;
  918. }
  919. int add_secure_header_v1(struct image_tool_params *params, uint8_t *ptr,
  920. int payloadsz, size_t headersz, uint8_t *image,
  921. struct secure_hdr_v1 *secure_hdr)
  922. {
  923. struct image_cfg_element *e_jtagdelay;
  924. struct image_cfg_element *e_boxid;
  925. struct image_cfg_element *e_flashid;
  926. RSA *csk = NULL;
  927. unsigned char *image_ptr;
  928. size_t image_size;
  929. struct sig_v1 tmp_sig;
  930. bool specialized_img = image_get_spezialized_img();
  931. kwb_msg("Create secure header content\n");
  932. e_jtagdelay = image_find_option(IMAGE_CFG_JTAG_DELAY);
  933. e_boxid = image_find_option(IMAGE_CFG_BOX_ID);
  934. e_flashid = image_find_option(IMAGE_CFG_FLASH_ID);
  935. if (kwb_load_csk(params, &csk) < 0)
  936. return 1;
  937. secure_hdr->headertype = OPT_HDR_V1_SECURE_TYPE;
  938. secure_hdr->headersz_msb = 0;
  939. secure_hdr->headersz_lsb = cpu_to_le16(sizeof(struct secure_hdr_v1));
  940. if (e_jtagdelay)
  941. secure_hdr->jtag_delay = e_jtagdelay->jtag_delay;
  942. if (e_boxid && specialized_img)
  943. secure_hdr->boxid = cpu_to_le32(e_boxid->boxid);
  944. if (e_flashid && specialized_img)
  945. secure_hdr->flashid = cpu_to_le32(e_flashid->flashid);
  946. if (kwb_sign_csk_with_kak(params, secure_hdr, csk))
  947. return 1;
  948. image_ptr = ptr + headersz;
  949. image_size = payloadsz - headersz;
  950. if (kwb_sign_and_verify(csk, image_ptr, image_size,
  951. &secure_hdr->imgsig, "image") < 0)
  952. return 1;
  953. if (kwb_sign_and_verify(csk, image, headersz, &tmp_sig, "header") < 0)
  954. return 1;
  955. secure_hdr->hdrsig = tmp_sig;
  956. kwb_dump_fuse_cmds(secure_hdr);
  957. return 0;
  958. }
  959. static void *image_create_v1(size_t *imagesz, struct image_tool_params *params,
  960. uint8_t *ptr, int payloadsz)
  961. {
  962. struct image_cfg_element *e;
  963. struct main_hdr_v1 *main_hdr;
  964. struct register_set_hdr_v1 *register_set_hdr;
  965. struct secure_hdr_v1 *secure_hdr = NULL;
  966. size_t headersz;
  967. uint8_t *image, *cur;
  968. int hasext = 0;
  969. uint8_t *next_ext = NULL;
  970. int cfgi, datai, size;
  971. /*
  972. * Calculate the size of the header and the size of the
  973. * payload
  974. */
  975. headersz = image_headersz_v1(&hasext);
  976. if (headersz == 0)
  977. return NULL;
  978. image = malloc(headersz);
  979. if (!image) {
  980. fprintf(stderr, "Cannot allocate memory for image\n");
  981. return NULL;
  982. }
  983. memset(image, 0, headersz);
  984. main_hdr = (struct main_hdr_v1 *)image;
  985. cur = image;
  986. cur += sizeof(struct main_hdr_v1);
  987. next_ext = &main_hdr->ext;
  988. /* Fill the main header */
  989. main_hdr->blocksize =
  990. cpu_to_le32(payloadsz - headersz);
  991. main_hdr->headersz_lsb = cpu_to_le16(headersz & 0xFFFF);
  992. main_hdr->headersz_msb = (headersz & 0xFFFF0000) >> 16;
  993. main_hdr->destaddr = cpu_to_le32(params->addr);
  994. main_hdr->execaddr = cpu_to_le32(params->ep);
  995. main_hdr->srcaddr = cpu_to_le32(headersz);
  996. main_hdr->ext = hasext;
  997. main_hdr->version = 1;
  998. e = image_find_option(IMAGE_CFG_BOOT_FROM);
  999. if (e)
  1000. main_hdr->blockid = e->bootfrom;
  1001. e = image_find_option(IMAGE_CFG_NAND_BLKSZ);
  1002. if (e)
  1003. main_hdr->nandblocksize = e->nandblksz / (64 * 1024);
  1004. e = image_find_option(IMAGE_CFG_NAND_BADBLK_LOCATION);
  1005. if (e)
  1006. main_hdr->nandbadblklocation = e->nandbadblklocation;
  1007. e = image_find_option(IMAGE_CFG_BAUDRATE);
  1008. if (e)
  1009. main_hdr->options = baudrate_to_option(e->baudrate);
  1010. e = image_find_option(IMAGE_CFG_DEBUG);
  1011. if (e)
  1012. main_hdr->flags = e->debug ? 0x1 : 0;
  1013. /*
  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. * For SDIO srcaddr is specified in number of sectors starting from
  1023. * sector 0. The main header is stored at sector number 0.
  1024. * This expects sector size to be 512 bytes.
  1025. * Header size is already aligned.
  1026. */
  1027. if (main_hdr->blockid == IBR_HDR_SDIO_ID)
  1028. main_hdr->srcaddr = cpu_to_le32(headersz / 512);
  1029. /* For PCIe srcaddr is not used and must be set to 0xFFFFFFFF. */
  1030. if (main_hdr->blockid == IBR_HDR_PEX_ID)
  1031. main_hdr->srcaddr = cpu_to_le32(0xFFFFFFFF);
  1032. if (image_get_csk_index() >= 0) {
  1033. /*
  1034. * only reserve the space here; we fill the header later since
  1035. * we need the header to be complete to compute the signatures
  1036. */
  1037. secure_hdr = (struct secure_hdr_v1 *)cur;
  1038. cur += sizeof(struct secure_hdr_v1);
  1039. *next_ext = 1;
  1040. next_ext = &secure_hdr->next;
  1041. }
  1042. datai = 0;
  1043. register_set_hdr = (struct register_set_hdr_v1 *)cur;
  1044. for (cfgi = 0; cfgi < cfgn; cfgi++) {
  1045. e = &image_cfg[cfgi];
  1046. if (e->type != IMAGE_CFG_DATA &&
  1047. e->type != IMAGE_CFG_DATA_DELAY)
  1048. continue;
  1049. if (e->type == IMAGE_CFG_DATA_DELAY) {
  1050. size = sizeof(struct register_set_hdr_v1) + 8 * datai + 4;
  1051. register_set_hdr->headertype = OPT_HDR_V1_REGISTER_TYPE;
  1052. register_set_hdr->headersz_lsb = cpu_to_le16(size & 0xFFFF);
  1053. register_set_hdr->headersz_msb = size >> 16;
  1054. register_set_hdr->data[datai].last_entry.delay = e->regdata_delay;
  1055. cur += size;
  1056. *next_ext = 1;
  1057. next_ext = &register_set_hdr->data[datai].last_entry.next;
  1058. datai = 0;
  1059. continue;
  1060. }
  1061. register_set_hdr->data[datai].entry.address =
  1062. cpu_to_le32(e->regdata.raddr);
  1063. register_set_hdr->data[datai].entry.value =
  1064. cpu_to_le32(e->regdata.rdata);
  1065. datai++;
  1066. }
  1067. if (datai != 0) {
  1068. size = sizeof(struct register_set_hdr_v1) + 8 * datai + 4;
  1069. register_set_hdr->headertype = OPT_HDR_V1_REGISTER_TYPE;
  1070. register_set_hdr->headersz_lsb = cpu_to_le16(size & 0xFFFF);
  1071. register_set_hdr->headersz_msb = size >> 16;
  1072. /* Set delay to the smallest possible value 1ms. */
  1073. register_set_hdr->data[datai].last_entry.delay = 1;
  1074. cur += size;
  1075. *next_ext = 1;
  1076. next_ext = &register_set_hdr->data[datai].last_entry.next;
  1077. }
  1078. for (cfgi = 0; cfgi < cfgn; cfgi++) {
  1079. e = &image_cfg[cfgi];
  1080. if (e->type != IMAGE_CFG_BINARY)
  1081. continue;
  1082. if (add_binary_header_v1(&cur, &next_ext, e))
  1083. return NULL;
  1084. }
  1085. if (secure_hdr && add_secure_header_v1(params, ptr, payloadsz,
  1086. headersz, image, secure_hdr))
  1087. return NULL;
  1088. /* Calculate and set the header checksum */
  1089. main_hdr->checksum = image_checksum8(main_hdr, headersz);
  1090. *imagesz = headersz;
  1091. return image;
  1092. }
  1093. int recognize_keyword(char *keyword)
  1094. {
  1095. int kw_id;
  1096. for (kw_id = 1; kw_id < IMAGE_CFG_COUNT; ++kw_id)
  1097. if (!strcmp(keyword, id_strs[kw_id]))
  1098. return kw_id;
  1099. return 0;
  1100. }
  1101. static int image_create_config_parse_oneline(char *line,
  1102. struct image_cfg_element *el)
  1103. {
  1104. char *keyword, *saveptr, *value1, *value2;
  1105. char delimiters[] = " \t";
  1106. int keyword_id, ret, argi;
  1107. char *unknown_msg = "Ignoring unknown line '%s'\n";
  1108. keyword = strtok_r(line, delimiters, &saveptr);
  1109. keyword_id = recognize_keyword(keyword);
  1110. if (!keyword_id) {
  1111. fprintf(stderr, unknown_msg, line);
  1112. return 0;
  1113. }
  1114. el->type = keyword_id;
  1115. value1 = strtok_r(NULL, delimiters, &saveptr);
  1116. if (!value1) {
  1117. fprintf(stderr, "Parameter missing in line '%s'\n", line);
  1118. return -1;
  1119. }
  1120. switch (keyword_id) {
  1121. case IMAGE_CFG_VERSION:
  1122. el->version = atoi(value1);
  1123. break;
  1124. case IMAGE_CFG_BOOT_FROM:
  1125. ret = image_boot_mode_id(value1);
  1126. if (ret < 0) {
  1127. fprintf(stderr, "Invalid boot media '%s'\n", value1);
  1128. return -1;
  1129. }
  1130. el->bootfrom = ret;
  1131. break;
  1132. case IMAGE_CFG_NAND_BLKSZ:
  1133. el->nandblksz = strtoul(value1, NULL, 16);
  1134. break;
  1135. case IMAGE_CFG_NAND_BADBLK_LOCATION:
  1136. el->nandbadblklocation = strtoul(value1, NULL, 16);
  1137. break;
  1138. case IMAGE_CFG_NAND_ECC_MODE:
  1139. ret = image_nand_ecc_mode_id(value1);
  1140. if (ret < 0) {
  1141. fprintf(stderr, "Invalid NAND ECC mode '%s'\n", value1);
  1142. return -1;
  1143. }
  1144. el->nandeccmode = ret;
  1145. break;
  1146. case IMAGE_CFG_NAND_PAGESZ:
  1147. el->nandpagesz = strtoul(value1, NULL, 16);
  1148. break;
  1149. case IMAGE_CFG_BINARY:
  1150. argi = 0;
  1151. el->binary.file = strdup(value1);
  1152. while (1) {
  1153. char *value = strtok_r(NULL, delimiters, &saveptr);
  1154. if (!value)
  1155. break;
  1156. el->binary.args[argi] = strtoul(value, NULL, 16);
  1157. argi++;
  1158. if (argi >= BINARY_MAX_ARGS) {
  1159. fprintf(stderr,
  1160. "Too many arguments for BINARY\n");
  1161. return -1;
  1162. }
  1163. }
  1164. el->binary.nargs = argi;
  1165. break;
  1166. case IMAGE_CFG_DATA:
  1167. value2 = strtok_r(NULL, delimiters, &saveptr);
  1168. if (!value1 || !value2) {
  1169. fprintf(stderr,
  1170. "Invalid number of arguments for DATA\n");
  1171. return -1;
  1172. }
  1173. el->regdata.raddr = strtoul(value1, NULL, 16);
  1174. el->regdata.rdata = strtoul(value2, NULL, 16);
  1175. break;
  1176. case IMAGE_CFG_DATA_DELAY:
  1177. if (!strcmp(value1, "SDRAM_SETUP"))
  1178. el->regdata_delay = REGISTER_SET_HDR_OPT_DELAY_SDRAM_SETUP;
  1179. else
  1180. el->regdata_delay = REGISTER_SET_HDR_OPT_DELAY_MS(strtoul(value1, NULL, 10));
  1181. break;
  1182. case IMAGE_CFG_BAUDRATE:
  1183. el->baudrate = strtoul(value1, NULL, 10);
  1184. break;
  1185. case IMAGE_CFG_DEBUG:
  1186. el->debug = strtoul(value1, NULL, 10);
  1187. break;
  1188. case IMAGE_CFG_KAK:
  1189. el->key_name = strdup(value1);
  1190. break;
  1191. case IMAGE_CFG_CSK:
  1192. el->key_name = strdup(value1);
  1193. break;
  1194. case IMAGE_CFG_CSK_INDEX:
  1195. el->csk_idx = strtol(value1, NULL, 0);
  1196. break;
  1197. case IMAGE_CFG_JTAG_DELAY:
  1198. el->jtag_delay = strtoul(value1, NULL, 0);
  1199. break;
  1200. case IMAGE_CFG_BOX_ID:
  1201. el->boxid = strtoul(value1, NULL, 0);
  1202. break;
  1203. case IMAGE_CFG_FLASH_ID:
  1204. el->flashid = strtoul(value1, NULL, 0);
  1205. break;
  1206. case IMAGE_CFG_SEC_SPECIALIZED_IMG:
  1207. el->sec_specialized_img = true;
  1208. break;
  1209. case IMAGE_CFG_SEC_COMMON_IMG:
  1210. el->sec_specialized_img = false;
  1211. break;
  1212. case IMAGE_CFG_SEC_BOOT_DEV:
  1213. el->sec_boot_dev = strtoul(value1, NULL, 0);
  1214. break;
  1215. case IMAGE_CFG_SEC_FUSE_DUMP:
  1216. el->name = strdup(value1);
  1217. break;
  1218. default:
  1219. fprintf(stderr, unknown_msg, line);
  1220. }
  1221. return 0;
  1222. }
  1223. /*
  1224. * Parse the configuration file 'fcfg' into the array of configuration
  1225. * elements 'image_cfg', and return the number of configuration
  1226. * elements in 'cfgn'.
  1227. */
  1228. static int image_create_config_parse(FILE *fcfg)
  1229. {
  1230. int ret;
  1231. int cfgi = 0;
  1232. /* Parse the configuration file */
  1233. while (!feof(fcfg)) {
  1234. char *line;
  1235. char buf[256];
  1236. /* Read the current line */
  1237. memset(buf, 0, sizeof(buf));
  1238. line = fgets(buf, sizeof(buf), fcfg);
  1239. if (!line)
  1240. break;
  1241. /* Ignore useless lines */
  1242. if (line[0] == '\n' || line[0] == '#')
  1243. continue;
  1244. /* Strip final newline */
  1245. if (line[strlen(line) - 1] == '\n')
  1246. line[strlen(line) - 1] = 0;
  1247. /* Parse the current line */
  1248. ret = image_create_config_parse_oneline(line,
  1249. &image_cfg[cfgi]);
  1250. if (ret)
  1251. return ret;
  1252. cfgi++;
  1253. if (cfgi >= IMAGE_CFG_ELEMENT_MAX) {
  1254. fprintf(stderr,
  1255. "Too many configuration elements in .cfg file\n");
  1256. return -1;
  1257. }
  1258. }
  1259. cfgn = cfgi;
  1260. return 0;
  1261. }
  1262. static int image_get_version(void)
  1263. {
  1264. struct image_cfg_element *e;
  1265. e = image_find_option(IMAGE_CFG_VERSION);
  1266. if (!e)
  1267. return -1;
  1268. return e->version;
  1269. }
  1270. static int image_get_bootfrom(void)
  1271. {
  1272. struct image_cfg_element *e;
  1273. e = image_find_option(IMAGE_CFG_BOOT_FROM);
  1274. if (!e)
  1275. return -1;
  1276. return e->bootfrom;
  1277. }
  1278. static void kwbimage_set_header(void *ptr, struct stat *sbuf, int ifd,
  1279. struct image_tool_params *params)
  1280. {
  1281. FILE *fcfg;
  1282. void *image = NULL;
  1283. int version;
  1284. size_t headersz = 0;
  1285. uint32_t checksum;
  1286. int ret;
  1287. fcfg = fopen(params->imagename, "r");
  1288. if (!fcfg) {
  1289. fprintf(stderr, "Could not open input file %s\n",
  1290. params->imagename);
  1291. exit(EXIT_FAILURE);
  1292. }
  1293. image_cfg = malloc(IMAGE_CFG_ELEMENT_MAX *
  1294. sizeof(struct image_cfg_element));
  1295. if (!image_cfg) {
  1296. fprintf(stderr, "Cannot allocate memory\n");
  1297. fclose(fcfg);
  1298. exit(EXIT_FAILURE);
  1299. }
  1300. memset(image_cfg, 0,
  1301. IMAGE_CFG_ELEMENT_MAX * sizeof(struct image_cfg_element));
  1302. rewind(fcfg);
  1303. ret = image_create_config_parse(fcfg);
  1304. fclose(fcfg);
  1305. if (ret) {
  1306. free(image_cfg);
  1307. exit(EXIT_FAILURE);
  1308. }
  1309. version = image_get_version();
  1310. switch (version) {
  1311. /*
  1312. * Fallback to version 0 if no version is provided in the
  1313. * cfg file
  1314. */
  1315. case -1:
  1316. case 0:
  1317. image = image_create_v0(&headersz, params, sbuf->st_size);
  1318. break;
  1319. case 1:
  1320. image = image_create_v1(&headersz, params, ptr, sbuf->st_size);
  1321. break;
  1322. default:
  1323. fprintf(stderr, "Unsupported version %d\n", version);
  1324. free(image_cfg);
  1325. exit(EXIT_FAILURE);
  1326. }
  1327. if (!image) {
  1328. fprintf(stderr, "Could not create image\n");
  1329. free(image_cfg);
  1330. exit(EXIT_FAILURE);
  1331. }
  1332. free(image_cfg);
  1333. /* Build and add image checksum header */
  1334. checksum = cpu_to_le32(image_checksum32((uint8_t *)ptr + headersz,
  1335. sbuf->st_size - headersz - sizeof(uint32_t)));
  1336. memcpy((uint8_t *)ptr + sbuf->st_size - sizeof(uint32_t), &checksum,
  1337. sizeof(uint32_t));
  1338. /* Finally copy the header into the image area */
  1339. memcpy(ptr, image, headersz);
  1340. free(image);
  1341. }
  1342. static void kwbimage_print_header(const void *ptr)
  1343. {
  1344. struct main_hdr_v0 *mhdr = (struct main_hdr_v0 *)ptr;
  1345. printf("Image Type: MVEBU Boot from %s Image\n",
  1346. image_boot_mode_name(mhdr->blockid));
  1347. printf("Image version:%d\n", image_version((void *)ptr));
  1348. if (image_version((void *)ptr) == 1) {
  1349. struct main_hdr_v1 *mhdr = (struct main_hdr_v1 *)ptr;
  1350. if (mhdr->ext & 0x1) {
  1351. struct opt_hdr_v1 *ohdr = (struct opt_hdr_v1 *)
  1352. ((uint8_t *)ptr +
  1353. sizeof(*mhdr));
  1354. while (1) {
  1355. uint32_t ohdr_size;
  1356. ohdr_size = (ohdr->headersz_msb << 16) |
  1357. le16_to_cpu(ohdr->headersz_lsb);
  1358. if (ohdr->headertype == OPT_HDR_V1_BINARY_TYPE) {
  1359. printf("BIN Hdr Size: ");
  1360. genimg_print_size(ohdr_size - 12 - 4 * ohdr->data[0]);
  1361. }
  1362. if (!(*((uint8_t *)ohdr + ohdr_size - 4) & 0x1))
  1363. break;
  1364. ohdr = (struct opt_hdr_v1 *)((uint8_t *)ohdr +
  1365. ohdr_size);
  1366. }
  1367. }
  1368. }
  1369. printf("Data Size: ");
  1370. genimg_print_size(mhdr->blocksize - sizeof(uint32_t));
  1371. printf("Load Address: %08x\n", mhdr->destaddr);
  1372. printf("Entry Point: %08x\n", mhdr->execaddr);
  1373. }
  1374. static int kwbimage_check_image_types(uint8_t type)
  1375. {
  1376. if (type == IH_TYPE_KWBIMAGE)
  1377. return EXIT_SUCCESS;
  1378. return EXIT_FAILURE;
  1379. }
  1380. static int kwbimage_verify_header(unsigned char *ptr, int image_size,
  1381. struct image_tool_params *params)
  1382. {
  1383. uint8_t checksum;
  1384. size_t header_size = kwbimage_header_size(ptr);
  1385. if (header_size > image_size)
  1386. return -FDT_ERR_BADSTRUCTURE;
  1387. if (!main_hdr_checksum_ok(ptr))
  1388. return -FDT_ERR_BADSTRUCTURE;
  1389. /* Only version 0 extended header has checksum */
  1390. if (image_version((void *)ptr) == 0) {
  1391. struct main_hdr_v0 *mhdr = (struct main_hdr_v0 *)ptr;
  1392. if (mhdr->ext & 0x1) {
  1393. struct ext_hdr_v0 *ext_hdr;
  1394. if (header_size + sizeof(*ext_hdr) > image_size)
  1395. return -FDT_ERR_BADSTRUCTURE;
  1396. ext_hdr = (struct ext_hdr_v0 *)
  1397. (ptr + sizeof(struct main_hdr_v0));
  1398. checksum = image_checksum8(ext_hdr,
  1399. sizeof(struct ext_hdr_v0)
  1400. - sizeof(uint8_t));
  1401. if (checksum != ext_hdr->checksum)
  1402. return -FDT_ERR_BADSTRUCTURE;
  1403. }
  1404. } else if (image_version((void *)ptr) == 1) {
  1405. struct main_hdr_v1 *mhdr = (struct main_hdr_v1 *)ptr;
  1406. uint32_t offset;
  1407. uint32_t size;
  1408. if (mhdr->ext & 0x1) {
  1409. uint32_t ohdr_size;
  1410. struct opt_hdr_v1 *ohdr = (struct opt_hdr_v1 *)
  1411. (ptr + sizeof(*mhdr));
  1412. while (1) {
  1413. if ((uint8_t *)ohdr + sizeof(*ohdr) >
  1414. (uint8_t *)mhdr + header_size)
  1415. return -FDT_ERR_BADSTRUCTURE;
  1416. ohdr_size = (ohdr->headersz_msb << 16) |
  1417. le16_to_cpu(ohdr->headersz_lsb);
  1418. if (ohdr_size < 8 ||
  1419. (uint8_t *)ohdr + ohdr_size >
  1420. (uint8_t *)mhdr + header_size)
  1421. return -FDT_ERR_BADSTRUCTURE;
  1422. if (!(*((uint8_t *)ohdr + ohdr_size - 4) & 0x1))
  1423. break;
  1424. ohdr = (struct opt_hdr_v1 *)((uint8_t *)ohdr +
  1425. ohdr_size);
  1426. }
  1427. }
  1428. offset = le32_to_cpu(mhdr->srcaddr);
  1429. /*
  1430. * For SATA srcaddr is specified in number of sectors.
  1431. * The main header is must be stored at sector number 1.
  1432. * This expects that sector size is 512 bytes and recalculates
  1433. * data offset to bytes relative to the main header.
  1434. */
  1435. if (mhdr->blockid == IBR_HDR_SATA_ID) {
  1436. if (offset < 1)
  1437. return -FDT_ERR_BADSTRUCTURE;
  1438. offset -= 1;
  1439. offset *= 512;
  1440. }
  1441. /*
  1442. * For SDIO srcaddr is specified in number of sectors.
  1443. * This expects that sector size is 512 bytes and recalculates
  1444. * data offset to bytes.
  1445. */
  1446. if (mhdr->blockid == IBR_HDR_SDIO_ID)
  1447. offset *= 512;
  1448. /*
  1449. * For PCIe srcaddr is always set to 0xFFFFFFFF.
  1450. * This expects that data starts after all headers.
  1451. */
  1452. if (mhdr->blockid == IBR_HDR_PEX_ID && offset == 0xFFFFFFFF)
  1453. offset = header_size;
  1454. if (offset > image_size || offset % 4 != 0)
  1455. return -FDT_ERR_BADSTRUCTURE;
  1456. size = le32_to_cpu(mhdr->blocksize);
  1457. if (size < 4 || offset + size > image_size || size % 4 != 0)
  1458. return -FDT_ERR_BADSTRUCTURE;
  1459. if (image_checksum32(ptr + offset, size - 4) !=
  1460. *(uint32_t *)(ptr + offset + size - 4))
  1461. return -FDT_ERR_BADSTRUCTURE;
  1462. } else {
  1463. return -FDT_ERR_BADSTRUCTURE;
  1464. }
  1465. return 0;
  1466. }
  1467. static int kwbimage_generate(struct image_tool_params *params,
  1468. struct image_type_params *tparams)
  1469. {
  1470. FILE *fcfg;
  1471. struct stat s;
  1472. int alloc_len;
  1473. int bootfrom;
  1474. int version;
  1475. void *hdr;
  1476. int ret;
  1477. fcfg = fopen(params->imagename, "r");
  1478. if (!fcfg) {
  1479. fprintf(stderr, "Could not open input file %s\n",
  1480. params->imagename);
  1481. exit(EXIT_FAILURE);
  1482. }
  1483. if (stat(params->datafile, &s)) {
  1484. fprintf(stderr, "Could not stat data file %s: %s\n",
  1485. params->datafile, strerror(errno));
  1486. exit(EXIT_FAILURE);
  1487. }
  1488. image_cfg = malloc(IMAGE_CFG_ELEMENT_MAX *
  1489. sizeof(struct image_cfg_element));
  1490. if (!image_cfg) {
  1491. fprintf(stderr, "Cannot allocate memory\n");
  1492. fclose(fcfg);
  1493. exit(EXIT_FAILURE);
  1494. }
  1495. memset(image_cfg, 0,
  1496. IMAGE_CFG_ELEMENT_MAX * sizeof(struct image_cfg_element));
  1497. rewind(fcfg);
  1498. ret = image_create_config_parse(fcfg);
  1499. fclose(fcfg);
  1500. if (ret) {
  1501. free(image_cfg);
  1502. exit(EXIT_FAILURE);
  1503. }
  1504. bootfrom = image_get_bootfrom();
  1505. version = image_get_version();
  1506. switch (version) {
  1507. /*
  1508. * Fallback to version 0 if no version is provided in the
  1509. * cfg file
  1510. */
  1511. case -1:
  1512. case 0:
  1513. alloc_len = sizeof(struct main_hdr_v0) +
  1514. sizeof(struct ext_hdr_v0);
  1515. break;
  1516. case 1:
  1517. alloc_len = image_headersz_v1(NULL);
  1518. break;
  1519. default:
  1520. fprintf(stderr, "Unsupported version %d\n", version);
  1521. free(image_cfg);
  1522. exit(EXIT_FAILURE);
  1523. }
  1524. free(image_cfg);
  1525. hdr = malloc(alloc_len);
  1526. if (!hdr) {
  1527. fprintf(stderr, "%s: malloc return failure: %s\n",
  1528. params->cmdname, strerror(errno));
  1529. exit(EXIT_FAILURE);
  1530. }
  1531. memset(hdr, 0, alloc_len);
  1532. tparams->header_size = alloc_len;
  1533. tparams->hdr = hdr;
  1534. /*
  1535. * The resulting image needs to be 4-byte aligned. At least
  1536. * the Marvell hdrparser tool complains if its unaligned.
  1537. * After the image data is stored 4-byte checksum.
  1538. * Final SPI and NAND images must be aligned to 256 bytes.
  1539. * Final SATA and SDIO images must be aligned to 512 bytes.
  1540. */
  1541. if (bootfrom == IBR_HDR_SPI_ID || bootfrom == IBR_HDR_NAND_ID)
  1542. return 4 + (256 - (alloc_len + s.st_size + 4) % 256) % 256;
  1543. else if (bootfrom == IBR_HDR_SATA_ID || bootfrom == IBR_HDR_SDIO_ID)
  1544. return 4 + (512 - (alloc_len + s.st_size + 4) % 512) % 512;
  1545. else
  1546. return 4 + (4 - s.st_size % 4) % 4;
  1547. }
  1548. static int kwbimage_extract_subimage(void *ptr, struct image_tool_params *params)
  1549. {
  1550. struct main_hdr_v1 *mhdr = (struct main_hdr_v1 *)ptr;
  1551. size_t header_size = kwbimage_header_size(ptr);
  1552. int idx = params->pflag;
  1553. int cur_idx = 0;
  1554. uint32_t offset;
  1555. ulong image;
  1556. ulong size;
  1557. if (image_version((void *)ptr) == 1 && (mhdr->ext & 0x1)) {
  1558. struct opt_hdr_v1 *ohdr = (struct opt_hdr_v1 *)
  1559. ((uint8_t *)ptr +
  1560. sizeof(*mhdr));
  1561. while (1) {
  1562. uint32_t ohdr_size = (ohdr->headersz_msb << 16) |
  1563. le16_to_cpu(ohdr->headersz_lsb);
  1564. if (ohdr->headertype == OPT_HDR_V1_BINARY_TYPE) {
  1565. if (idx == cur_idx) {
  1566. image = (ulong)&ohdr->data[4 +
  1567. 4 * ohdr->data[0]];
  1568. size = ohdr_size - 12 -
  1569. 4 * ohdr->data[0];
  1570. goto extract;
  1571. }
  1572. ++cur_idx;
  1573. }
  1574. if (!(*((uint8_t *)ohdr + ohdr_size - 4) & 0x1))
  1575. break;
  1576. ohdr = (struct opt_hdr_v1 *)((uint8_t *)ohdr +
  1577. ohdr_size);
  1578. }
  1579. }
  1580. if (idx != cur_idx) {
  1581. printf("Image %d is not present\n", idx);
  1582. return -1;
  1583. }
  1584. offset = le32_to_cpu(mhdr->srcaddr);
  1585. if (mhdr->blockid == IBR_HDR_SATA_ID) {
  1586. offset -= 1;
  1587. offset *= 512;
  1588. }
  1589. if (mhdr->blockid == IBR_HDR_SDIO_ID)
  1590. offset *= 512;
  1591. if (mhdr->blockid == IBR_HDR_PEX_ID && offset == 0xFFFFFFFF)
  1592. offset = header_size;
  1593. image = (ulong)((uint8_t *)ptr + offset);
  1594. size = le32_to_cpu(mhdr->blocksize) - 4;
  1595. extract:
  1596. return imagetool_save_subimage(params->outfile, image, size);
  1597. }
  1598. /*
  1599. * Report Error if xflag is set in addition to default
  1600. */
  1601. static int kwbimage_check_params(struct image_tool_params *params)
  1602. {
  1603. if (!params->iflag && (!params->imagename || !strlen(params->imagename))) {
  1604. char *msg = "Configuration file for kwbimage creation omitted";
  1605. fprintf(stderr, "Error:%s - %s\n", params->cmdname, msg);
  1606. return CFG_INVALID;
  1607. }
  1608. return (params->dflag && (params->fflag || params->lflag)) ||
  1609. (params->fflag && (params->dflag || params->lflag)) ||
  1610. (params->lflag && (params->dflag || params->fflag)) ||
  1611. (params->xflag);
  1612. }
  1613. /*
  1614. * kwbimage type parameters definition
  1615. */
  1616. U_BOOT_IMAGE_TYPE(
  1617. kwbimage,
  1618. "Marvell MVEBU Boot Image support",
  1619. 0,
  1620. NULL,
  1621. kwbimage_check_params,
  1622. kwbimage_verify_header,
  1623. kwbimage_print_header,
  1624. kwbimage_set_header,
  1625. kwbimage_extract_subimage,
  1626. kwbimage_check_image_types,
  1627. NULL,
  1628. kwbimage_generate
  1629. );