001-mtk-0024-tools-mtk_image-add-support-for-MT7621-NAND-images.patch 8.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333
  1. From 18dd1ef9417d0880f2f492b55bd4d9ede499f137 Mon Sep 17 00:00:00 2001
  2. From: Weijie Gao <[email protected]>
  3. Date: Fri, 20 May 2022 11:24:10 +0800
  4. Subject: [PATCH 24/25] tools: mtk_image: add support for MT7621 NAND images
  5. The BootROM of MT7621 requires a image header for SPL to record its size
  6. and load address when booting from NAND.
  7. To create such an image, one can use the following command line:
  8. mkimage -T mtk_image -a 0x80200000 -e 0x80200000 -n "mt7621=1"
  9. -d u-boot-spl-ddr.bin u-boot-spl-ddr.img
  10. Reviewed-by: Daniel Schwierzeck <[email protected]>
  11. Signed-off-by: Weijie Gao <[email protected]>
  12. ---
  13. tools/mtk_image.c | 182 ++++++++++++++++++++++++++++++++++++++++++++++
  14. tools/mtk_image.h | 24 ++++++
  15. 2 files changed, 206 insertions(+)
  16. --- a/tools/mtk_image.c
  17. +++ b/tools/mtk_image.c
  18. @@ -6,7 +6,9 @@
  19. * Author: Weijie Gao <[email protected]>
  20. */
  21. +#include <time.h>
  22. #include <image.h>
  23. +#include <u-boot/crc.h>
  24. #include <u-boot/sha256.h>
  25. #include "imagetool.h"
  26. #include "mtk_image.h"
  27. @@ -251,17 +253,45 @@ static uint32_t img_size;
  28. static enum brlyt_img_type hdr_media;
  29. static uint32_t hdr_offset;
  30. static int use_lk_hdr;
  31. +static int use_mt7621_hdr;
  32. static bool is_arm64_image;
  33. /* LK image name */
  34. static char lk_name[32] = "U-Boot";
  35. +/* CRC32 normal table required by MT7621 image */
  36. +static uint32_t crc32tbl[256];
  37. +
  38. /* NAND header selected by user */
  39. static const union nand_boot_header *hdr_nand;
  40. /* GFH header + 2 * 4KB pages of NAND */
  41. static char hdr_tmp[sizeof(struct gfh_header) + 0x2000];
  42. +static uint32_t crc32_normal_cal(uint32_t crc, const void *data, size_t length,
  43. + const uint32_t *crc32c_table)
  44. +{
  45. + const uint8_t *p = data;
  46. +
  47. + while (length--)
  48. + crc = crc32c_table[(uint8_t)((crc >> 24) ^ *p++)] ^ (crc << 8);
  49. +
  50. + return crc;
  51. +}
  52. +
  53. +static void crc32_normal_init(uint32_t *crc32c_table, uint32_t poly)
  54. +{
  55. + uint32_t v, i, j;
  56. +
  57. + for (i = 0; i < 256; i++) {
  58. + v = i << 24;
  59. + for (j = 0; j < 8; j++)
  60. + v = (v << 1) ^ ((v & (1 << 31)) ? poly : 0);
  61. +
  62. + crc32c_table[i] = v;
  63. + }
  64. +}
  65. +
  66. static int mtk_image_check_image_types(uint8_t type)
  67. {
  68. if (type == IH_TYPE_MTKIMAGE)
  69. @@ -283,6 +313,7 @@ static int mtk_brom_parse_imagename(cons
  70. static const char *hdr_offs = "";
  71. static const char *nandinfo = "";
  72. static const char *lk = "";
  73. + static const char *mt7621 = "";
  74. static const char *arm64_param = "";
  75. key = buf;
  76. @@ -332,6 +363,9 @@ static int mtk_brom_parse_imagename(cons
  77. if (!strcmp(key, "lk"))
  78. lk = val;
  79. + if (!strcmp(key, "mt7621"))
  80. + mt7621 = val;
  81. +
  82. if (!strcmp(key, "lkname"))
  83. snprintf(lk_name, sizeof(lk_name), "%s", val);
  84. @@ -352,6 +386,13 @@ static int mtk_brom_parse_imagename(cons
  85. return 0;
  86. }
  87. + /* if user specified MT7621 image header, skip following checks */
  88. + if (mt7621 && mt7621[0] == '1') {
  89. + use_mt7621_hdr = 1;
  90. + free(buf);
  91. + return 0;
  92. + }
  93. +
  94. /* parse media type */
  95. for (i = 0; i < ARRAY_SIZE(brom_images); i++) {
  96. if (!strcmp(brom_images[i].name, media)) {
  97. @@ -419,6 +460,13 @@ static int mtk_image_vrec_header(struct
  98. return 0;
  99. }
  100. + if (use_mt7621_hdr) {
  101. + tparams->header_size = image_get_header_size();
  102. + tparams->hdr = &hdr_tmp;
  103. + memset(&hdr_tmp, 0, tparams->header_size);
  104. + return 0;
  105. + }
  106. +
  107. if (hdr_media == BRLYT_TYPE_NAND || hdr_media == BRLYT_TYPE_SNAND)
  108. tparams->header_size = 2 * le16_to_cpu(hdr_nand->pagesize);
  109. else
  110. @@ -579,9 +627,90 @@ static int mtk_image_verify_nand_header(
  111. return 0;
  112. }
  113. +static uint32_t crc32be_cal(const void *data, size_t length)
  114. +{
  115. + uint32_t crc = 0;
  116. + uint8_t c;
  117. +
  118. + if (crc32tbl[1] != MT7621_IH_CRC_POLYNOMIAL)
  119. + crc32_normal_init(crc32tbl, MT7621_IH_CRC_POLYNOMIAL);
  120. +
  121. + crc = crc32_normal_cal(crc, data, length, crc32tbl);
  122. +
  123. + for (; length; length >>= 8) {
  124. + c = length & 0xff;
  125. + crc = crc32_normal_cal(crc, &c, 1, crc32tbl);
  126. + }
  127. +
  128. + return ~crc;
  129. +}
  130. +
  131. +static int mtk_image_verify_mt7621_header(const uint8_t *ptr, int print)
  132. +{
  133. + const image_header_t *hdr = (const image_header_t *)ptr;
  134. + struct mt7621_nand_header *nhdr;
  135. + uint32_t spl_size, crcval;
  136. + image_header_t header;
  137. + int ret;
  138. +
  139. + spl_size = image_get_size(hdr);
  140. +
  141. + if (spl_size > img_size) {
  142. + if (print)
  143. + printf("Incomplete SPL image\n");
  144. + return -1;
  145. + }
  146. +
  147. + ret = image_check_hcrc(hdr);
  148. + if (!ret) {
  149. + if (print)
  150. + printf("Bad header CRC\n");
  151. + return -1;
  152. + }
  153. +
  154. + ret = image_check_dcrc(hdr);
  155. + if (!ret) {
  156. + if (print)
  157. + printf("Bad data CRC\n");
  158. + return -1;
  159. + }
  160. +
  161. + /* Copy header so we can blank CRC field for re-calculation */
  162. + memmove(&header, hdr, image_get_header_size());
  163. + image_set_hcrc(&header, 0);
  164. +
  165. + nhdr = (struct mt7621_nand_header *)header.ih_name;
  166. + crcval = be32_to_cpu(nhdr->crc);
  167. + nhdr->crc = 0;
  168. +
  169. + if (crcval != crc32be_cal(&header, image_get_header_size())) {
  170. + if (print)
  171. + printf("Bad NAND header CRC\n");
  172. + return -1;
  173. + }
  174. +
  175. + if (print) {
  176. + printf("Load Address: %08x\n", image_get_load(hdr));
  177. +
  178. + printf("Image Name: %.*s\n", MT7621_IH_NMLEN,
  179. + image_get_name(hdr));
  180. +
  181. + if (IMAGE_ENABLE_TIMESTAMP) {
  182. + printf("Created: ");
  183. + genimg_print_time((time_t)image_get_time(hdr));
  184. + }
  185. +
  186. + printf("Data Size: ");
  187. + genimg_print_size(image_get_data_size(hdr));
  188. + }
  189. +
  190. + return 0;
  191. +}
  192. +
  193. static int mtk_image_verify_header(unsigned char *ptr, int image_size,
  194. struct image_tool_params *params)
  195. {
  196. + image_header_t *hdr = (image_header_t *)ptr;
  197. union lk_hdr *lk = (union lk_hdr *)ptr;
  198. /* nothing to verify for LK image header */
  199. @@ -590,6 +719,9 @@ static int mtk_image_verify_header(unsig
  200. img_size = image_size;
  201. + if (image_get_magic(hdr) == IH_MAGIC)
  202. + return mtk_image_verify_mt7621_header(ptr, 0);
  203. +
  204. if (!strcmp((char *)ptr, NAND_BOOT_NAME))
  205. return mtk_image_verify_nand_header(ptr, 0);
  206. else
  207. @@ -600,6 +732,7 @@ static int mtk_image_verify_header(unsig
  208. static void mtk_image_print_header(const void *ptr)
  209. {
  210. + image_header_t *hdr = (image_header_t *)ptr;
  211. union lk_hdr *lk = (union lk_hdr *)ptr;
  212. if (le32_to_cpu(lk->magic) == LK_PART_MAGIC) {
  213. @@ -610,6 +743,11 @@ static void mtk_image_print_header(const
  214. printf("Image Type: MediaTek BootROM Loadable Image\n");
  215. + if (image_get_magic(hdr) == IH_MAGIC) {
  216. + mtk_image_verify_mt7621_header(ptr, 1);
  217. + return;
  218. + }
  219. +
  220. if (!strcmp((char *)ptr, NAND_BOOT_NAME))
  221. mtk_image_verify_nand_header(ptr, 1);
  222. else
  223. @@ -773,6 +911,45 @@ static void mtk_image_set_nand_header(vo
  224. filesize - 2 * le16_to_cpu(hdr_nand->pagesize) - SHA256_SUM_LEN);
  225. }
  226. +static void mtk_image_set_mt7621_header(void *ptr, off_t filesize,
  227. + uint32_t loadaddr)
  228. +{
  229. + image_header_t *hdr = (image_header_t *)ptr;
  230. + struct mt7621_stage1_header *shdr;
  231. + struct mt7621_nand_header *nhdr;
  232. + uint32_t datasize, crcval;
  233. +
  234. + datasize = filesize - image_get_header_size();
  235. + nhdr = (struct mt7621_nand_header *)hdr->ih_name;
  236. + shdr = (struct mt7621_stage1_header *)(ptr + image_get_header_size());
  237. +
  238. + shdr->ep = cpu_to_be32(loadaddr);
  239. + shdr->stage_size = cpu_to_be32(datasize);
  240. +
  241. + image_set_magic(hdr, IH_MAGIC);
  242. + image_set_time(hdr, time(NULL));
  243. + image_set_size(hdr, datasize);
  244. + image_set_load(hdr, loadaddr);
  245. + image_set_ep(hdr, loadaddr);
  246. + image_set_os(hdr, IH_OS_U_BOOT);
  247. + image_set_arch(hdr, IH_ARCH_MIPS);
  248. + image_set_type(hdr, IH_TYPE_STANDALONE);
  249. + image_set_comp(hdr, IH_COMP_NONE);
  250. +
  251. + crcval = crc32(0, (uint8_t *)shdr, datasize);
  252. + image_set_dcrc(hdr, crcval);
  253. +
  254. + strncpy(nhdr->ih_name, "MT7621 NAND", MT7621_IH_NMLEN);
  255. +
  256. + nhdr->ih_stage_offset = cpu_to_be32(image_get_header_size());
  257. +
  258. + crcval = crc32be_cal(hdr, image_get_header_size());
  259. + nhdr->crc = cpu_to_be32(crcval);
  260. +
  261. + crcval = crc32(0, (uint8_t *)hdr, image_get_header_size());
  262. + image_set_hcrc(hdr, crcval);
  263. +}
  264. +
  265. static void mtk_image_set_header(void *ptr, struct stat *sbuf, int ifd,
  266. struct image_tool_params *params)
  267. {
  268. @@ -791,6 +968,11 @@ static void mtk_image_set_header(void *p
  269. img_gen = true;
  270. img_size = sbuf->st_size;
  271. + if (use_mt7621_hdr) {
  272. + mtk_image_set_mt7621_header(ptr, sbuf->st_size, params->addr);
  273. + return;
  274. + }
  275. +
  276. if (hdr_media == BRLYT_TYPE_NAND || hdr_media == BRLYT_TYPE_SNAND)
  277. mtk_image_set_nand_header(ptr, sbuf->st_size, params->addr);
  278. else
  279. --- a/tools/mtk_image.h
  280. +++ b/tools/mtk_image.h
  281. @@ -200,4 +200,28 @@ union lk_hdr {
  282. #define LK_PART_MAGIC 0x58881688
  283. +/* MT7621 NAND SPL image header */
  284. +
  285. +#define MT7621_IH_NMLEN 12
  286. +#define MT7621_IH_CRC_POLYNOMIAL 0x04c11db7
  287. +
  288. +struct mt7621_nand_header {
  289. + char ih_name[MT7621_IH_NMLEN];
  290. + uint32_t nand_ac_timing;
  291. + uint32_t ih_stage_offset;
  292. + uint32_t ih_bootloader_offset;
  293. + uint32_t nand_info_1_data;
  294. + uint32_t crc;
  295. +};
  296. +
  297. +struct mt7621_stage1_header {
  298. + uint32_t jump_insn[2];
  299. + uint32_t ep;
  300. + uint32_t stage_size;
  301. + uint32_t has_stage2;
  302. + uint32_t next_ep;
  303. + uint32_t next_size;
  304. + uint32_t next_offset;
  305. +};
  306. +
  307. #endif /* _MTK_IMAGE_H */