mtk_image.c 19 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774
  1. // SPDX-License-Identifier: GPL-2.0+
  2. /*
  3. * Generate MediaTek BootROM header for SPL/U-Boot images
  4. *
  5. * Copyright (C) 2018 MediaTek Inc.
  6. * Author: Weijie Gao <weijie.gao@mediatek.com>
  7. */
  8. #include <time.h>
  9. #include <image.h>
  10. #include <u-boot/crc.h>
  11. #include <u-boot/sha256.h>
  12. #include "imagetool.h"
  13. #include "mtk_image.h"
  14. #include "mtk_nand_headers.h"
  15. static const struct brom_img_type {
  16. const char *name;
  17. enum brlyt_img_type type;
  18. } brom_images[] = {
  19. {
  20. .name = "nand",
  21. .type = BRLYT_TYPE_NAND
  22. }, {
  23. .name = "emmc",
  24. .type = BRLYT_TYPE_EMMC
  25. }, {
  26. .name = "nor",
  27. .type = BRLYT_TYPE_NOR
  28. }, {
  29. .name = "sdmmc",
  30. .type = BRLYT_TYPE_SDMMC
  31. }, {
  32. .name = "snand",
  33. .type = BRLYT_TYPE_SNAND
  34. }, {
  35. .name = "spim-nand",
  36. .type = BRLYT_TYPE_SNAND
  37. }
  38. };
  39. /* Indicates whether we're generating or verifying */
  40. static bool img_gen;
  41. static uint32_t img_size;
  42. /* Image type selected by user */
  43. static enum brlyt_img_type hdr_media;
  44. static uint32_t hdr_offset;
  45. static int use_lk_hdr;
  46. static int use_mt7621_hdr;
  47. static bool is_arm64_image;
  48. /* LK image name */
  49. static char lk_name[32] = "U-Boot";
  50. /* CRC32 normal table required by MT7621 image */
  51. static uint32_t crc32tbl[256];
  52. /* NAND header selected by user */
  53. static const struct nand_header_type *hdr_nand;
  54. static uint32_t hdr_nand_size;
  55. /* GFH header + 2 * 4KB pages of NAND */
  56. static char hdr_tmp[sizeof(struct gfh_header) + 0x2000];
  57. static uint32_t crc32_normal_cal(uint32_t crc, const void *data, size_t length,
  58. const uint32_t *crc32c_table)
  59. {
  60. const uint8_t *p = data;
  61. while (length--)
  62. crc = crc32c_table[(uint8_t)((crc >> 24) ^ *p++)] ^ (crc << 8);
  63. return crc;
  64. }
  65. static void crc32_normal_init(uint32_t *crc32c_table, uint32_t poly)
  66. {
  67. uint32_t v, i, j;
  68. for (i = 0; i < 256; i++) {
  69. v = i << 24;
  70. for (j = 0; j < 8; j++)
  71. v = (v << 1) ^ ((v & (1 << 31)) ? poly : 0);
  72. crc32c_table[i] = v;
  73. }
  74. }
  75. static int mtk_image_check_image_types(uint8_t type)
  76. {
  77. if (type == IH_TYPE_MTKIMAGE)
  78. return EXIT_SUCCESS;
  79. else
  80. return EXIT_FAILURE;
  81. }
  82. static int mtk_brom_parse_imagename(const char *imagename)
  83. {
  84. #define is_blank_char(c) \
  85. ((c) == '\t' || (c) == '\n' || (c) == '\r' || (c) == ' ')
  86. char *buf = strdup(imagename), *key, *val, *end, *next;
  87. int i;
  88. /* User passed arguments from image name */
  89. static const char *media = "";
  90. static const char *hdr_offs = "";
  91. static const char *nandinfo = "";
  92. static const char *lk = "";
  93. static const char *mt7621 = "";
  94. static const char *arm64_param = "";
  95. key = buf;
  96. while (key) {
  97. next = strchr(key, ';');
  98. if (next)
  99. *next = 0;
  100. val = strchr(key, '=');
  101. if (val) {
  102. *val++ = 0;
  103. /* Trim key */
  104. while (is_blank_char(*key))
  105. key++;
  106. end = key + strlen(key) - 1;
  107. while ((end >= key) && is_blank_char(*end))
  108. end--;
  109. end++;
  110. if (is_blank_char(*end))
  111. *end = 0;
  112. /* Trim value */
  113. while (is_blank_char(*val))
  114. val++;
  115. end = val + strlen(val) - 1;
  116. while ((end >= val) && is_blank_char(*end))
  117. end--;
  118. end++;
  119. if (is_blank_char(*end))
  120. *end = 0;
  121. /* record user passed arguments */
  122. if (!strcmp(key, "media"))
  123. media = val;
  124. if (!strcmp(key, "hdroffset"))
  125. hdr_offs = val;
  126. if (!strcmp(key, "nandinfo"))
  127. nandinfo = val;
  128. if (!strcmp(key, "lk"))
  129. lk = val;
  130. if (!strcmp(key, "mt7621"))
  131. mt7621 = val;
  132. if (!strcmp(key, "lkname"))
  133. snprintf(lk_name, sizeof(lk_name), "%s", val);
  134. if (!strcmp(key, "arm64"))
  135. arm64_param = val;
  136. }
  137. if (next)
  138. key = next + 1;
  139. else
  140. break;
  141. }
  142. /* if user specified LK image header, skip following checks */
  143. if (lk && lk[0] == '1') {
  144. use_lk_hdr = 1;
  145. free(buf);
  146. return 0;
  147. }
  148. /* if user specified MT7621 image header, skip following checks */
  149. if (mt7621 && mt7621[0] == '1') {
  150. use_mt7621_hdr = 1;
  151. free(buf);
  152. return 0;
  153. }
  154. /* parse media type */
  155. for (i = 0; i < ARRAY_SIZE(brom_images); i++) {
  156. if (!strcmp(brom_images[i].name, media)) {
  157. hdr_media = brom_images[i].type;
  158. break;
  159. }
  160. }
  161. /* parse nand header type */
  162. hdr_nand = mtk_nand_header_find(nandinfo);
  163. /* parse device header offset */
  164. if (hdr_offs && hdr_offs[0])
  165. hdr_offset = strtoul(hdr_offs, NULL, 0);
  166. if (arm64_param && arm64_param[0] == '1')
  167. is_arm64_image = true;
  168. free(buf);
  169. if (hdr_media == BRLYT_TYPE_INVALID) {
  170. fprintf(stderr, "Error: media type is invalid or missing.\n");
  171. fprintf(stderr, " Please specify -n \"media=<type>\"\n");
  172. return -EINVAL;
  173. }
  174. if ((hdr_media == BRLYT_TYPE_NAND || hdr_media == BRLYT_TYPE_SNAND) &&
  175. !hdr_nand) {
  176. fprintf(stderr, "Error: nand info is invalid or missing.\n");
  177. fprintf(stderr, " Please specify -n \"media=%s;"
  178. "nandinfo=<info>\"\n", media);
  179. return -EINVAL;
  180. }
  181. if (hdr_media == BRLYT_TYPE_NAND || hdr_media == BRLYT_TYPE_SNAND)
  182. hdr_nand_size = mtk_nand_header_size(hdr_nand);
  183. return 0;
  184. }
  185. static int mtk_image_check_params(struct image_tool_params *params)
  186. {
  187. if (!params->addr) {
  188. fprintf(stderr, "Error: Load Address must be set.\n");
  189. return -EINVAL;
  190. }
  191. if (!params->imagename) {
  192. fprintf(stderr, "Error: Image Name must be set.\n");
  193. return -EINVAL;
  194. }
  195. return mtk_brom_parse_imagename(params->imagename);
  196. }
  197. static int mtk_image_vrec_header(struct image_tool_params *params,
  198. struct image_type_params *tparams)
  199. {
  200. if (use_lk_hdr) {
  201. tparams->header_size = sizeof(union lk_hdr);
  202. tparams->hdr = &hdr_tmp;
  203. memset(&hdr_tmp, 0xff, tparams->header_size);
  204. return 0;
  205. }
  206. if (use_mt7621_hdr) {
  207. tparams->header_size = image_get_header_size();
  208. tparams->hdr = &hdr_tmp;
  209. memset(&hdr_tmp, 0, tparams->header_size);
  210. return 0;
  211. }
  212. if (hdr_media == BRLYT_TYPE_NAND || hdr_media == BRLYT_TYPE_SNAND)
  213. tparams->header_size = hdr_nand_size;
  214. else
  215. tparams->header_size = sizeof(struct gen_device_header);
  216. tparams->header_size += sizeof(struct gfh_header);
  217. tparams->hdr = &hdr_tmp;
  218. memset(&hdr_tmp, 0xff, tparams->header_size);
  219. return SHA256_SUM_LEN;
  220. }
  221. static int mtk_image_verify_gfh(struct gfh_header *gfh, uint32_t type, int print)
  222. {
  223. if (strcmp(gfh->file_info.name, GFH_FILE_INFO_NAME))
  224. return -1;
  225. if (le32_to_cpu(gfh->file_info.flash_type) != type)
  226. return -1;
  227. if (print)
  228. printf("Load Address: %08x\n",
  229. le32_to_cpu(gfh->file_info.load_addr) +
  230. le32_to_cpu(gfh->file_info.jump_offset));
  231. if (print)
  232. printf("Architecture: %s\n", is_arm64_image ? "ARM64" : "ARM");
  233. return 0;
  234. }
  235. static int mtk_image_verify_gen_header(const uint8_t *ptr, int print)
  236. {
  237. union gen_boot_header *gbh = (union gen_boot_header *)ptr;
  238. uint32_t gfh_offset, total_size, devh_size;
  239. struct brom_layout_header *bh;
  240. struct gfh_header *gfh;
  241. const char *bootmedia;
  242. if (!strcmp(gbh->name, SF_BOOT_NAME))
  243. bootmedia = "Serial NOR";
  244. else if (!strcmp(gbh->name, EMMC_BOOT_NAME))
  245. bootmedia = "eMMC";
  246. else if (!strcmp(gbh->name, SDMMC_BOOT_NAME))
  247. bootmedia = "SD/MMC";
  248. else
  249. return -1;
  250. if (print)
  251. printf("Boot Media: %s\n", bootmedia);
  252. if (le32_to_cpu(gbh->version) != 1 ||
  253. le32_to_cpu(gbh->size) != sizeof(union gen_boot_header))
  254. return -1;
  255. bh = (struct brom_layout_header *)(ptr + le32_to_cpu(gbh->size));
  256. if (strcmp(bh->name, BRLYT_NAME))
  257. return -1;
  258. if (le32_to_cpu(bh->magic) != BRLYT_MAGIC ||
  259. (le32_to_cpu(bh->type) != BRLYT_TYPE_NOR &&
  260. le32_to_cpu(bh->type) != BRLYT_TYPE_EMMC &&
  261. le32_to_cpu(bh->type) != BRLYT_TYPE_SDMMC))
  262. return -1;
  263. devh_size = sizeof(struct gen_device_header);
  264. if (img_gen) {
  265. gfh_offset = devh_size;
  266. } else {
  267. gfh_offset = le32_to_cpu(bh->header_size);
  268. if (gfh_offset + sizeof(struct gfh_header) > img_size) {
  269. /*
  270. * This may happen if the hdr_offset used to generate
  271. * this image is not zero.
  272. * Since device header size is not fixed, we can't
  273. * cover all possible cases.
  274. * Assuming the image is valid only if the real
  275. * device header size equals to devh_size.
  276. */
  277. total_size = le32_to_cpu(bh->total_size);
  278. if (total_size - gfh_offset > img_size - devh_size)
  279. return -1;
  280. gfh_offset = devh_size;
  281. }
  282. }
  283. gfh = (struct gfh_header *)(ptr + gfh_offset);
  284. return mtk_image_verify_gfh(gfh, GFH_FLASH_TYPE_GEN, print);
  285. }
  286. static int mtk_image_verify_nand_header(const uint8_t *ptr, int print)
  287. {
  288. struct brom_layout_header *bh;
  289. struct nand_header_info info;
  290. struct gfh_header *gfh;
  291. const char *bootmedia;
  292. int ret;
  293. ret = mtk_nand_header_info(ptr, &info);
  294. if (ret < 0)
  295. return ret;
  296. if (!ret) {
  297. bh = (struct brom_layout_header *)(ptr + info.page_size);
  298. if (strcmp(bh->name, BRLYT_NAME))
  299. return -1;
  300. if (le32_to_cpu(bh->magic) != BRLYT_MAGIC)
  301. return -1;
  302. if (le32_to_cpu(bh->type) == BRLYT_TYPE_NAND)
  303. bootmedia = "Parallel NAND";
  304. else if (le32_to_cpu(bh->type) == BRLYT_TYPE_SNAND)
  305. bootmedia = "Serial NAND (SNFI/AP)";
  306. else
  307. return -1;
  308. } else {
  309. if (info.snfi)
  310. bootmedia = "Serial NAND (SNFI/HSM)";
  311. else
  312. bootmedia = "Serial NAND (SPIM)";
  313. }
  314. if (print) {
  315. printf("Boot Media: %s\n", bootmedia);
  316. if (info.page_size >= 1024)
  317. printf("Page Size: %dKB\n", info.page_size >> 10);
  318. else
  319. printf("Page Size: %dB\n", info.page_size);
  320. printf("Spare Size: %dB\n", info.spare_size);
  321. }
  322. gfh = (struct gfh_header *)(ptr + info.gfh_offset);
  323. return mtk_image_verify_gfh(gfh, GFH_FLASH_TYPE_NAND, print);
  324. }
  325. static uint32_t crc32be_cal(const void *data, size_t length)
  326. {
  327. uint32_t crc = 0;
  328. uint8_t c;
  329. if (crc32tbl[1] != MT7621_IH_CRC_POLYNOMIAL)
  330. crc32_normal_init(crc32tbl, MT7621_IH_CRC_POLYNOMIAL);
  331. crc = crc32_normal_cal(crc, data, length, crc32tbl);
  332. for (; length; length >>= 8) {
  333. c = length & 0xff;
  334. crc = crc32_normal_cal(crc, &c, 1, crc32tbl);
  335. }
  336. return ~crc;
  337. }
  338. static int mtk_image_verify_mt7621_header(const uint8_t *ptr, int print)
  339. {
  340. const struct legacy_img_hdr *hdr = (const struct legacy_img_hdr *)ptr;
  341. struct mt7621_nand_header *nhdr;
  342. uint32_t spl_size, crcval;
  343. struct legacy_img_hdr header;
  344. int ret;
  345. spl_size = image_get_size(hdr);
  346. if (spl_size > img_size) {
  347. if (print)
  348. printf("Incomplete SPL image\n");
  349. return -1;
  350. }
  351. ret = image_check_hcrc(hdr);
  352. if (!ret) {
  353. if (print)
  354. printf("Bad header CRC\n");
  355. return -1;
  356. }
  357. ret = image_check_dcrc(hdr);
  358. if (!ret) {
  359. if (print)
  360. printf("Bad data CRC\n");
  361. return -1;
  362. }
  363. /* Copy header so we can blank CRC field for re-calculation */
  364. memmove(&header, hdr, image_get_header_size());
  365. image_set_hcrc(&header, 0);
  366. nhdr = (struct mt7621_nand_header *)header.ih_name;
  367. crcval = be32_to_cpu(nhdr->crc);
  368. nhdr->crc = 0;
  369. if (crcval != crc32be_cal(&header, image_get_header_size())) {
  370. if (print)
  371. printf("Bad NAND header CRC\n");
  372. return -1;
  373. }
  374. if (print) {
  375. printf("Load Address: %08x\n", image_get_load(hdr));
  376. printf("Image Name: %.*s\n", MT7621_IH_NMLEN,
  377. image_get_name(hdr));
  378. if (IMAGE_ENABLE_TIMESTAMP) {
  379. printf("Created: ");
  380. genimg_print_time((time_t)image_get_time(hdr));
  381. }
  382. printf("Data Size: ");
  383. genimg_print_size(image_get_data_size(hdr));
  384. }
  385. return 0;
  386. }
  387. static int mtk_image_verify_header(unsigned char *ptr, int image_size,
  388. struct image_tool_params *params)
  389. {
  390. struct legacy_img_hdr *hdr = (struct legacy_img_hdr *)ptr;
  391. union lk_hdr *lk = (union lk_hdr *)ptr;
  392. /* nothing to verify for LK image header */
  393. if (le32_to_cpu(lk->magic) == LK_PART_MAGIC)
  394. return 0;
  395. img_size = image_size;
  396. if (image_get_magic(hdr) == IH_MAGIC)
  397. return mtk_image_verify_mt7621_header(ptr, 0);
  398. if (is_mtk_nand_header(ptr))
  399. return mtk_image_verify_nand_header(ptr, 0);
  400. else
  401. return mtk_image_verify_gen_header(ptr, 0);
  402. return -1;
  403. }
  404. static void mtk_image_print_header(const void *ptr, struct image_tool_params *params)
  405. {
  406. struct legacy_img_hdr *hdr = (struct legacy_img_hdr *)ptr;
  407. union lk_hdr *lk = (union lk_hdr *)ptr;
  408. if (le32_to_cpu(lk->magic) == LK_PART_MAGIC) {
  409. printf("Image Type: MediaTek LK Image\n");
  410. printf("Load Address: %08x\n", le32_to_cpu(lk->loadaddr));
  411. return;
  412. }
  413. printf("Image Type: MediaTek BootROM Loadable Image\n");
  414. if (image_get_magic(hdr) == IH_MAGIC) {
  415. mtk_image_verify_mt7621_header(ptr, 1);
  416. return;
  417. }
  418. if (is_mtk_nand_header(ptr))
  419. mtk_image_verify_nand_header(ptr, 1);
  420. else
  421. mtk_image_verify_gen_header(ptr, 1);
  422. }
  423. static void put_brom_layout_header(struct brom_layout_header *hdr, int type)
  424. {
  425. strncpy(hdr->name, BRLYT_NAME, sizeof(hdr->name));
  426. hdr->version = cpu_to_le32(1);
  427. hdr->magic = cpu_to_le32(BRLYT_MAGIC);
  428. hdr->type = cpu_to_le32(type);
  429. }
  430. static void put_ghf_common_header(struct gfh_common_header *gfh, uint16_t size,
  431. uint16_t type, uint8_t ver)
  432. {
  433. uint32_t magic_version = GFH_HEADER_MAGIC |
  434. (uint32_t)ver << GFH_HEADER_VERSION_SHIFT;
  435. gfh->magic_version = cpu_to_le32(magic_version);
  436. gfh->size = cpu_to_le16(size);
  437. gfh->type = cpu_to_le16(type);
  438. }
  439. static void put_ghf_header(struct gfh_header *gfh, int file_size,
  440. int dev_hdr_size, int load_addr, int flash_type)
  441. {
  442. uint32_t cfg_bits;
  443. memset(gfh, 0, sizeof(struct gfh_header));
  444. /* GFH_FILE_INFO header */
  445. put_ghf_common_header(&gfh->file_info.gfh, sizeof(gfh->file_info),
  446. GFH_TYPE_FILE_INFO, 1);
  447. strncpy(gfh->file_info.name, GFH_FILE_INFO_NAME,
  448. sizeof(gfh->file_info.name));
  449. gfh->file_info.unused = cpu_to_le32(1);
  450. gfh->file_info.file_type = cpu_to_le16(1);
  451. gfh->file_info.flash_type = flash_type;
  452. gfh->file_info.sig_type = GFH_SIG_TYPE_SHA256;
  453. gfh->file_info.load_addr = cpu_to_le32(load_addr - sizeof(*gfh));
  454. gfh->file_info.total_size = cpu_to_le32(file_size - dev_hdr_size);
  455. gfh->file_info.max_size = cpu_to_le32(file_size);
  456. gfh->file_info.hdr_size = sizeof(*gfh);
  457. gfh->file_info.sig_size = SHA256_SUM_LEN;
  458. gfh->file_info.jump_offset = sizeof(*gfh);
  459. gfh->file_info.processed = cpu_to_le32(1);
  460. /* GFH_BL_INFO header */
  461. put_ghf_common_header(&gfh->bl_info.gfh, sizeof(gfh->bl_info),
  462. GFH_TYPE_BL_INFO, 1);
  463. gfh->bl_info.attr = cpu_to_le32(1);
  464. /* GFH_BROM_CFG header */
  465. put_ghf_common_header(&gfh->brom_cfg.gfh, sizeof(gfh->brom_cfg),
  466. GFH_TYPE_BROM_CFG, 3);
  467. cfg_bits = GFH_BROM_CFG_USBDL_AUTO_DETECT_DIS |
  468. GFH_BROM_CFG_USBDL_BY_KCOL0_TIMEOUT_EN |
  469. GFH_BROM_CFG_USBDL_BY_FLAG_TIMEOUT_EN;
  470. gfh->brom_cfg.usbdl_by_kcol0_timeout_ms = cpu_to_le32(5000);
  471. if (is_arm64_image) {
  472. gfh->brom_cfg.jump_bl_arm64 = GFH_BROM_CFG_JUMP_BL_ARM64;
  473. cfg_bits |= GFH_BROM_CFG_JUMP_BL_ARM64_EN;
  474. }
  475. gfh->brom_cfg.cfg_bits = cpu_to_le32(cfg_bits);
  476. /* GFH_BL_SEC_KEY header */
  477. put_ghf_common_header(&gfh->bl_sec_key.gfh, sizeof(gfh->bl_sec_key),
  478. GFH_TYPE_BL_SEC_KEY, 1);
  479. /* GFH_ANTI_CLONE header */
  480. put_ghf_common_header(&gfh->anti_clone.gfh, sizeof(gfh->anti_clone),
  481. GFH_TYPE_ANTI_CLONE, 1);
  482. gfh->anti_clone.ac_offset = cpu_to_le32(0x10);
  483. gfh->anti_clone.ac_len = cpu_to_le32(0x80);
  484. /* GFH_BROM_SEC_CFG header */
  485. put_ghf_common_header(&gfh->brom_sec_cfg.gfh,
  486. sizeof(gfh->brom_sec_cfg),
  487. GFH_TYPE_BROM_SEC_CFG, 1);
  488. gfh->brom_sec_cfg.cfg_bits =
  489. cpu_to_le32(BROM_SEC_CFG_JTAG_EN | BROM_SEC_CFG_UART_EN);
  490. }
  491. static void put_hash(uint8_t *buff, int size)
  492. {
  493. sha256_context ctx;
  494. sha256_starts(&ctx);
  495. sha256_update(&ctx, buff, size);
  496. sha256_finish(&ctx, buff + size);
  497. }
  498. static void mtk_image_set_gen_header(void *ptr, off_t filesize,
  499. uint32_t loadaddr)
  500. {
  501. struct gen_device_header *hdr = (struct gen_device_header *)ptr;
  502. struct gfh_header *gfh;
  503. const char *bootname = NULL;
  504. if (hdr_media == BRLYT_TYPE_NOR)
  505. bootname = SF_BOOT_NAME;
  506. else if (hdr_media == BRLYT_TYPE_EMMC)
  507. bootname = EMMC_BOOT_NAME;
  508. else if (hdr_media == BRLYT_TYPE_SDMMC)
  509. bootname = SDMMC_BOOT_NAME;
  510. /* Generic device header */
  511. snprintf(hdr->boot.name, sizeof(hdr->boot.name), "%s", bootname);
  512. hdr->boot.version = cpu_to_le32(1);
  513. hdr->boot.size = cpu_to_le32(sizeof(hdr->boot));
  514. /* BRLYT header */
  515. put_brom_layout_header(&hdr->brlyt, hdr_media);
  516. hdr->brlyt.header_size = cpu_to_le32(hdr_offset + sizeof(*hdr));
  517. hdr->brlyt.total_size = cpu_to_le32(hdr_offset + filesize);
  518. hdr->brlyt.header_size_2 = hdr->brlyt.header_size;
  519. hdr->brlyt.total_size_2 = hdr->brlyt.total_size;
  520. /* GFH header */
  521. gfh = (struct gfh_header *)(ptr + sizeof(struct gen_device_header));
  522. put_ghf_header(gfh, filesize, sizeof(struct gen_device_header),
  523. loadaddr, GFH_FLASH_TYPE_GEN);
  524. /* Generate SHA256 hash */
  525. put_hash((uint8_t *)gfh,
  526. filesize - sizeof(struct gen_device_header) - SHA256_SUM_LEN);
  527. }
  528. static void mtk_image_set_nand_header(void *ptr, off_t filesize,
  529. uint32_t loadaddr)
  530. {
  531. struct brom_layout_header *brlyt;
  532. struct gfh_header *gfh;
  533. uint32_t payload_pages, nand_page_size;
  534. /* NAND header */
  535. nand_page_size = mtk_nand_header_put(hdr_nand, ptr);
  536. if (nand_page_size) {
  537. /* BRLYT header */
  538. payload_pages = (filesize + nand_page_size - 1) /
  539. nand_page_size;
  540. brlyt = (struct brom_layout_header *)(ptr + nand_page_size);
  541. put_brom_layout_header(brlyt, hdr_media);
  542. brlyt->header_size = cpu_to_le32(2);
  543. brlyt->total_size = cpu_to_le32(payload_pages);
  544. brlyt->header_size_2 = brlyt->header_size;
  545. brlyt->total_size_2 = brlyt->total_size;
  546. brlyt->unused = cpu_to_le32(1);
  547. }
  548. /* GFH header */
  549. gfh = (struct gfh_header *)(ptr + hdr_nand_size);
  550. put_ghf_header(gfh, filesize, hdr_nand_size, loadaddr,
  551. GFH_FLASH_TYPE_NAND);
  552. /* Generate SHA256 hash */
  553. put_hash((uint8_t *)gfh, filesize - hdr_nand_size - SHA256_SUM_LEN);
  554. }
  555. static void mtk_image_set_mt7621_header(void *ptr, off_t filesize,
  556. uint32_t loadaddr)
  557. {
  558. struct legacy_img_hdr *hdr = (struct legacy_img_hdr *)ptr;
  559. struct mt7621_stage1_header *shdr;
  560. struct mt7621_nand_header *nhdr;
  561. uint32_t datasize, crcval;
  562. datasize = filesize - image_get_header_size();
  563. nhdr = (struct mt7621_nand_header *)hdr->ih_name;
  564. shdr = (struct mt7621_stage1_header *)(ptr + image_get_header_size());
  565. shdr->ep = cpu_to_be32(loadaddr);
  566. shdr->stage_size = cpu_to_be32(datasize);
  567. image_set_magic(hdr, IH_MAGIC);
  568. image_set_time(hdr, time(NULL));
  569. image_set_size(hdr, datasize);
  570. image_set_load(hdr, loadaddr);
  571. image_set_ep(hdr, loadaddr);
  572. image_set_os(hdr, IH_OS_U_BOOT);
  573. image_set_arch(hdr, IH_ARCH_MIPS);
  574. image_set_type(hdr, IH_TYPE_STANDALONE);
  575. image_set_comp(hdr, IH_COMP_NONE);
  576. crcval = crc32(0, (uint8_t *)shdr, datasize);
  577. image_set_dcrc(hdr, crcval);
  578. strncpy(nhdr->ih_name, "MT7621 NAND", MT7621_IH_NMLEN);
  579. nhdr->ih_stage_offset = cpu_to_be32(image_get_header_size());
  580. crcval = crc32be_cal(hdr, image_get_header_size());
  581. nhdr->crc = cpu_to_be32(crcval);
  582. crcval = crc32(0, (uint8_t *)hdr, image_get_header_size());
  583. image_set_hcrc(hdr, crcval);
  584. }
  585. static void mtk_image_set_header(void *ptr, struct stat *sbuf, int ifd,
  586. struct image_tool_params *params)
  587. {
  588. union lk_hdr *lk = (union lk_hdr *)ptr;
  589. if (use_lk_hdr) {
  590. lk->magic = cpu_to_le32(LK_PART_MAGIC);
  591. lk->size = cpu_to_le32(sbuf->st_size - sizeof(union lk_hdr));
  592. lk->loadaddr = cpu_to_le32(params->addr);
  593. lk->mode = 0xffffffff; /* must be non-zero */
  594. memset(lk->name, 0, sizeof(lk->name));
  595. strncpy(lk->name, lk_name, sizeof(lk->name));
  596. return;
  597. }
  598. img_gen = true;
  599. img_size = sbuf->st_size;
  600. if (use_mt7621_hdr) {
  601. mtk_image_set_mt7621_header(ptr, sbuf->st_size, params->addr);
  602. return;
  603. }
  604. if (hdr_media == BRLYT_TYPE_NAND || hdr_media == BRLYT_TYPE_SNAND)
  605. mtk_image_set_nand_header(ptr, sbuf->st_size, params->addr);
  606. else
  607. mtk_image_set_gen_header(ptr, sbuf->st_size, params->addr);
  608. }
  609. U_BOOT_IMAGE_TYPE(
  610. mtk_image,
  611. "MediaTek BootROM Loadable Image support",
  612. 0,
  613. NULL,
  614. mtk_image_check_params,
  615. mtk_image_verify_header,
  616. mtk_image_print_header,
  617. mtk_image_set_header,
  618. NULL,
  619. mtk_image_check_image_types,
  620. NULL,
  621. mtk_image_vrec_header
  622. );