2
0

002-0030-tools-mtk_image-add-support-for-nand-headers-used-by.patch 24 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702
  1. From fbf296f9ed5daab70020686e9ba072efe663bbab Mon Sep 17 00:00:00 2001
  2. From: Weijie Gao <[email protected]>
  3. Date: Wed, 3 Aug 2022 11:14:36 +0800
  4. Subject: [PATCH 30/31] tools: mtk_image: add support for nand headers used by
  5. newer chips
  6. This patch adds more nand headers in two new types:
  7. 1. HSM header, used for spi-nand thru SNFI interface
  8. 2. SPIM header, used for spi-nand thru spi-mem interface
  9. The original nand header is renamed to AP header.
  10. Signed-off-by: Weijie Gao <[email protected]>
  11. ---
  12. tools/mtk_image.c | 23 ++-
  13. tools/mtk_nand_headers.c | 422 +++++++++++++++++++++++++++++++++++++--
  14. tools/mtk_nand_headers.h | 110 +++++++++-
  15. 3 files changed, 525 insertions(+), 30 deletions(-)
  16. --- a/tools/mtk_image.c
  17. +++ b/tools/mtk_image.c
  18. @@ -33,6 +33,9 @@ static const struct brom_img_type {
  19. }, {
  20. .name = "snand",
  21. .type = BRLYT_TYPE_SNAND
  22. + }, {
  23. + .name = "spim-nand",
  24. + .type = BRLYT_TYPE_SNAND
  25. }
  26. };
  27. @@ -54,7 +57,7 @@ static char lk_name[32] = "U-Boot";
  28. static uint32_t crc32tbl[256];
  29. /* NAND header selected by user */
  30. -static const union nand_boot_header *hdr_nand;
  31. +static const struct nand_header_type *hdr_nand;
  32. static uint32_t hdr_nand_size;
  33. /* GFH header + 2 * 4KB pages of NAND */
  34. @@ -366,20 +369,26 @@ static int mtk_image_verify_nand_header(
  35. if (ret < 0)
  36. return ret;
  37. - bh = (struct brom_layout_header *)(ptr + info.page_size);
  38. + if (!ret) {
  39. + bh = (struct brom_layout_header *)(ptr + info.page_size);
  40. - if (strcmp(bh->name, BRLYT_NAME))
  41. - return -1;
  42. + if (strcmp(bh->name, BRLYT_NAME))
  43. + return -1;
  44. +
  45. + if (le32_to_cpu(bh->magic) != BRLYT_MAGIC)
  46. + return -1;
  47. - if (le32_to_cpu(bh->magic) != BRLYT_MAGIC) {
  48. - return -1;
  49. - } else {
  50. if (le32_to_cpu(bh->type) == BRLYT_TYPE_NAND)
  51. bootmedia = "Parallel NAND";
  52. else if (le32_to_cpu(bh->type) == BRLYT_TYPE_SNAND)
  53. bootmedia = "Serial NAND (SNFI/AP)";
  54. else
  55. return -1;
  56. + } else {
  57. + if (info.snfi)
  58. + bootmedia = "Serial NAND (SNFI/HSM)";
  59. + else
  60. + bootmedia = "Serial NAND (SPIM)";
  61. }
  62. if (print) {
  63. --- a/tools/mtk_nand_headers.c
  64. +++ b/tools/mtk_nand_headers.c
  65. @@ -188,55 +188,346 @@ static const union nand_boot_header nand
  66. }
  67. };
  68. -static const struct nand_header_type {
  69. +/* HSM BROM NAND header for SPI NAND with 2KB page + 64B spare */
  70. +static const union hsm_nand_boot_header hsm_nand_hdr_2k_64_data = {
  71. + .data = {
  72. + 0x4E, 0x41, 0x4E, 0x44, 0x43, 0x46, 0x47, 0x21,
  73. + 0x01, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  74. + 0x00, 0x04, 0x00, 0x00, 0x08, 0x00, 0x00, 0x00,
  75. + 0x01, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  76. + 0x00, 0x08, 0x00, 0x00, 0x40, 0x00, 0x00, 0x00,
  77. + 0x40, 0x00, 0x00, 0x00, 0x00, 0x08, 0x00, 0x00,
  78. + 0x0C, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  79. + 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  80. + 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  81. + 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  82. + 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  83. + 0x00, 0x00, 0x00, 0x00, 0x01, 0x00, 0x00, 0x00,
  84. + 0xFF, 0x00, 0x00, 0x00, 0x21, 0xD2, 0xEE, 0xF6,
  85. + 0xAE, 0xDD, 0x5E, 0xC2, 0x82, 0x8E, 0x9A, 0x62,
  86. + 0x09, 0x8E, 0x80, 0xE2, 0x37, 0x0D, 0xC9, 0xFA,
  87. + 0xA9, 0xDD, 0xFC, 0x92, 0x34, 0x2A, 0xED, 0x51,
  88. + 0xA4, 0x1B, 0xF7, 0x63, 0xCC, 0x5A, 0xC7, 0xFB,
  89. + 0xED, 0x21, 0x02, 0x23, 0x51, 0x31
  90. + }
  91. +};
  92. +
  93. +/* HSM BROM NAND header for SPI NAND with 2KB page + 128B spare */
  94. +static const union hsm_nand_boot_header hsm_nand_hdr_2k_128_data = {
  95. + .data = {
  96. + 0x4E, 0x41, 0x4E, 0x44, 0x43, 0x46, 0x47, 0x21,
  97. + 0x01, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  98. + 0x00, 0x04, 0x00, 0x00, 0x08, 0x00, 0x00, 0x00,
  99. + 0x01, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  100. + 0x00, 0x08, 0x00, 0x00, 0x80, 0x00, 0x00, 0x00,
  101. + 0x40, 0x00, 0x00, 0x00, 0x00, 0x04, 0x00, 0x00,
  102. + 0x0C, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  103. + 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  104. + 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  105. + 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  106. + 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  107. + 0x00, 0x00, 0x00, 0x00, 0x01, 0x00, 0x00, 0x00,
  108. + 0xFF, 0x00, 0x00, 0x00, 0x71, 0x7f, 0x71, 0xAC,
  109. + 0x42, 0xD0, 0x5B, 0xD2, 0x12, 0x81, 0x15, 0x0A,
  110. + 0x0C, 0xD4, 0xF6, 0x32, 0x1E, 0x63, 0xE7, 0x81,
  111. + 0x8A, 0x7F, 0xDE, 0xF9, 0x4B, 0x91, 0xEC, 0xC2,
  112. + 0x70, 0x00, 0x7F, 0x57, 0xAF, 0xDC, 0xE4, 0x24,
  113. + 0x57, 0x09, 0xBC, 0xC5, 0x35, 0xDC
  114. + }
  115. +};
  116. +
  117. +/* HSM BROM NAND header for SPI NAND with 4KB page + 256B spare */
  118. +static const union hsm_nand_boot_header hsm_nand_hdr_4k_256_data = {
  119. + .data = {
  120. + 0x4E, 0x41, 0x4E, 0x44, 0x43, 0x46, 0x47, 0x21,
  121. + 0x01, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  122. + 0x00, 0x04, 0x00, 0x00, 0x08, 0x00, 0x00, 0x00,
  123. + 0x01, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  124. + 0x00, 0x10, 0x00, 0x00, 0x00, 0x01, 0x00, 0x00,
  125. + 0x40, 0x00, 0x00, 0x00, 0x00, 0x08, 0x00, 0x00,
  126. + 0x0C, 0x00, 0x00, 0x00, 0x04, 0x00, 0x00, 0x00,
  127. + 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  128. + 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  129. + 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  130. + 0x01, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  131. + 0x00, 0x00, 0x00, 0x00, 0x01, 0x00, 0x00, 0x00,
  132. + 0xFF, 0x00, 0x00, 0x00, 0x62, 0x04, 0xD6, 0x1F,
  133. + 0x2B, 0x57, 0x7A, 0x2D, 0xFE, 0xBB, 0x4A, 0x50,
  134. + 0xEC, 0xF8, 0x70, 0x1A, 0x44, 0x15, 0xF6, 0xA2,
  135. + 0x8E, 0xB0, 0xFD, 0xFA, 0xDC, 0xAA, 0x5A, 0x4E,
  136. + 0xCB, 0x8E, 0xC9, 0x72, 0x08, 0xDC, 0x20, 0xB9,
  137. + 0x98, 0xC8, 0x82, 0xD8, 0xBE, 0x44
  138. + }
  139. +};
  140. +
  141. +/* HSM2.0 BROM NAND header for SPI NAND with 2KB page + 64B spare */
  142. +static const union hsm20_nand_boot_header hsm20_nand_hdr_2k_64_data = {
  143. + .data = {
  144. + 0x4E, 0x41, 0x4E, 0x44, 0x43, 0x46, 0x47, 0x21,
  145. + 0x01, 0x00, 0x00, 0x00, 0x00, 0x00, 0x10, 0x00,
  146. + 0x00, 0x04, 0x00, 0x00, 0x08, 0x00, 0x00, 0x00,
  147. + 0x01, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  148. + 0x00, 0x08, 0x00, 0x00, 0x40, 0x00, 0x00, 0x00,
  149. + 0x40, 0x00, 0x00, 0x00, 0x00, 0x08, 0x00, 0x00,
  150. + 0x0C, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  151. + 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  152. + 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  153. + 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  154. + 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  155. + 0x00, 0x00, 0x00, 0x00, 0xFF, 0xFF, 0xFF, 0xFF,
  156. + 0x01, 0x00, 0x00, 0x00, 0xFF, 0x00, 0x00, 0x00,
  157. + 0x01, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  158. + 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  159. + 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  160. + 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  161. + 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  162. + 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  163. + 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  164. + 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  165. + 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  166. + 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  167. + 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  168. + 0x5F, 0x4B, 0xB2, 0x5B, 0x8B, 0x1C, 0x35, 0xDA,
  169. + 0x83, 0xE6, 0x6C, 0xC3, 0xFB, 0x8C, 0x78, 0x23,
  170. + 0xD0, 0x89, 0x24, 0xD9, 0x6C, 0x35, 0x2C, 0x5D,
  171. + 0x8F, 0xBB, 0xFC, 0x10, 0xD0, 0xE2, 0x22, 0x7D,
  172. + 0xC8, 0x97, 0x9A, 0xEF, 0xC6, 0xB5, 0xA7, 0x4E,
  173. + 0x4E, 0x0E
  174. + }
  175. +};
  176. +
  177. +/* HSM2.0 BROM NAND header for SPI NAND with 2KB page + 128B spare */
  178. +static const union hsm20_nand_boot_header hsm20_nand_hdr_2k_128_data = {
  179. + .data = {
  180. + 0x4E, 0x41, 0x4E, 0x44, 0x43, 0x46, 0x47, 0x21,
  181. + 0x01, 0x00, 0x00, 0x00, 0x00, 0x00, 0x10, 0x00,
  182. + 0x00, 0x04, 0x00, 0x00, 0x08, 0x00, 0x00, 0x00,
  183. + 0x01, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  184. + 0x00, 0x08, 0x00, 0x00, 0x80, 0x00, 0x00, 0x00,
  185. + 0x40, 0x00, 0x00, 0x00, 0x00, 0x08, 0x00, 0x00,
  186. + 0x0C, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  187. + 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  188. + 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  189. + 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  190. + 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  191. + 0x00, 0x00, 0x00, 0x00, 0xFF, 0xFF, 0xFF, 0xFF,
  192. + 0x01, 0x00, 0x00, 0x00, 0xFF, 0x00, 0x00, 0x00,
  193. + 0x01, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  194. + 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  195. + 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  196. + 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  197. + 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  198. + 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  199. + 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  200. + 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  201. + 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  202. + 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  203. + 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  204. + 0xF8, 0x7E, 0xC1, 0x5D, 0x61, 0x54, 0xEA, 0x9F,
  205. + 0x5E, 0x66, 0x39, 0x66, 0x21, 0xFF, 0x8C, 0x3B,
  206. + 0xBE, 0xA7, 0x5A, 0x9E, 0xD7, 0xBD, 0x9E, 0x89,
  207. + 0xEE, 0x7E, 0x10, 0x31, 0x9A, 0x1D, 0x82, 0x49,
  208. + 0xA3, 0x4E, 0xD8, 0x47, 0xD7, 0x19, 0xF4, 0x2D,
  209. + 0x8E, 0x53
  210. + }
  211. +};
  212. +
  213. +/* HSM2.0 BROM NAND header for SPI NAND with 4KB page + 256B spare */
  214. +static const union hsm20_nand_boot_header hsm20_nand_hdr_4k_256_data = {
  215. + .data = {
  216. + 0x4E, 0x41, 0x4E, 0x44, 0x43, 0x46, 0x47, 0x21,
  217. + 0x01, 0x00, 0x00, 0x00, 0x00, 0x00, 0x10, 0x00,
  218. + 0x00, 0x04, 0x00, 0x00, 0x08, 0x00, 0x00, 0x00,
  219. + 0x01, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  220. + 0x00, 0x10, 0x00, 0x00, 0x00, 0x01, 0x00, 0x00,
  221. + 0x40, 0x00, 0x00, 0x00, 0x00, 0x08, 0x00, 0x00,
  222. + 0x0C, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  223. + 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  224. + 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  225. + 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  226. + 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  227. + 0x00, 0x00, 0x00, 0x00, 0xFF, 0xFF, 0xFF, 0xFF,
  228. + 0x01, 0x00, 0x00, 0x00, 0xFF, 0x00, 0x00, 0x00,
  229. + 0x01, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  230. + 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  231. + 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  232. + 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  233. + 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  234. + 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  235. + 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  236. + 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  237. + 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  238. + 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  239. + 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  240. + 0x79, 0x01, 0x1F, 0x86, 0x62, 0x6A, 0x43, 0xAE,
  241. + 0xE6, 0xF8, 0xDD, 0x5B, 0x29, 0xB7, 0xA2, 0x7F,
  242. + 0x29, 0x72, 0x54, 0x37, 0xBE, 0x50, 0xD4, 0x24,
  243. + 0xAB, 0x60, 0xF4, 0x44, 0x97, 0x3B, 0x65, 0x21,
  244. + 0x73, 0x24, 0x1F, 0x93, 0x0E, 0x9E, 0x96, 0x88,
  245. + 0x78, 0x6C
  246. + }
  247. +};
  248. +
  249. +/* SPIM-NAND header for SPI NAND with 2KB page + 64B spare */
  250. +static const union spim_nand_boot_header spim_nand_hdr_2k_64_data = {
  251. + .data = {
  252. + 0x53, 0x50, 0x49, 0x4e, 0x41, 0x4e, 0x44, 0x21,
  253. + 0x01, 0x00, 0x00, 0x00, 0x10, 0x00, 0x00, 0x00,
  254. + 0x00, 0x08, 0x00, 0x00, 0x40, 0x00, 0x00, 0x00,
  255. + 0x40, 0x00, 0x0c, 0x00, 0x00, 0x00, 0x20, 0x30,
  256. + 0x01, 0x00, 0x00, 0x00, 0xff, 0x00, 0x00, 0x00,
  257. + 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  258. + 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  259. + 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  260. + 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  261. + 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00
  262. + }
  263. +};
  264. +
  265. +/* SPIM-NAND header for SPI NAND with 2KB page + 128B spare */
  266. +static const union spim_nand_boot_header spim_nand_hdr_2k_128_data = {
  267. + .data = {
  268. + 0x53, 0x50, 0x49, 0x4e, 0x41, 0x4e, 0x44, 0x21,
  269. + 0x01, 0x00, 0x00, 0x00, 0x10, 0x00, 0x00, 0x00,
  270. + 0x00, 0x08, 0x00, 0x00, 0x80, 0x00, 0x00, 0x00,
  271. + 0x40, 0x00, 0x0c, 0x00, 0x00, 0x00, 0x20, 0x30,
  272. + 0x01, 0x00, 0x00, 0x00, 0xff, 0x00, 0x00, 0x00,
  273. + 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  274. + 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  275. + 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  276. + 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  277. + 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00
  278. + }
  279. +};
  280. +
  281. +/* SPIM-NAND header for SPI NAND with 4KB page + 256B spare */
  282. +static const union spim_nand_boot_header spim_nand_hdr_4k_256_data = {
  283. + .data = {
  284. + 0x53, 0x50, 0x49, 0x4e, 0x41, 0x4e, 0x44, 0x21,
  285. + 0x01, 0x00, 0x00, 0x00, 0x10, 0x00, 0x00, 0x00,
  286. + 0x00, 0x10, 0x00, 0x00, 0x00, 0x01, 0x00, 0x00,
  287. + 0x40, 0x00, 0x0d, 0x00, 0x00, 0x00, 0x20, 0x30,
  288. + 0x01, 0x00, 0x00, 0x00, 0xff, 0x00, 0x00, 0x00,
  289. + 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  290. + 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  291. + 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  292. + 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  293. + 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00
  294. + }
  295. +};
  296. +
  297. +struct nand_header_type {
  298. const char *name;
  299. - const union nand_boot_header *data;
  300. + enum nand_boot_header_type type;
  301. + union {
  302. + const union nand_boot_header *ap;
  303. + const union hsm_nand_boot_header *hsm;
  304. + const union hsm20_nand_boot_header *hsm20;
  305. + const union spim_nand_boot_header *spim;
  306. + };
  307. } nand_headers[] = {
  308. {
  309. .name = "2k+64",
  310. - .data = &snand_hdr_2k_64_data
  311. + .type = NAND_BOOT_AP_HEADER,
  312. + .ap = &snand_hdr_2k_64_data,
  313. }, {
  314. .name = "2k+120",
  315. - .data = &snand_hdr_2k_128_data
  316. + .type = NAND_BOOT_AP_HEADER,
  317. + .ap = &snand_hdr_2k_128_data,
  318. }, {
  319. .name = "2k+128",
  320. - .data = &snand_hdr_2k_128_data
  321. + .type = NAND_BOOT_AP_HEADER,
  322. + .ap = &snand_hdr_2k_128_data,
  323. }, {
  324. .name = "4k+256",
  325. - .data = &snand_hdr_4k_256_data
  326. + .type = NAND_BOOT_AP_HEADER,
  327. + .ap = &snand_hdr_4k_256_data,
  328. }, {
  329. .name = "1g:2k+64",
  330. - .data = &nand_hdr_1gb_2k_64_data
  331. + .type = NAND_BOOT_AP_HEADER,
  332. + .ap = &nand_hdr_1gb_2k_64_data,
  333. }, {
  334. .name = "2g:2k+64",
  335. - .data = &nand_hdr_2gb_2k_64_data
  336. + .type = NAND_BOOT_AP_HEADER,
  337. + .ap = &nand_hdr_2gb_2k_64_data,
  338. }, {
  339. .name = "4g:2k+64",
  340. - .data = &nand_hdr_4gb_2k_64_data
  341. + .type = NAND_BOOT_AP_HEADER,
  342. + .ap = &nand_hdr_4gb_2k_64_data,
  343. }, {
  344. .name = "2g:2k+128",
  345. - .data = &nand_hdr_2gb_2k_128_data
  346. + .type = NAND_BOOT_AP_HEADER,
  347. + .ap = &nand_hdr_2gb_2k_128_data,
  348. }, {
  349. .name = "4g:2k+128",
  350. - .data = &nand_hdr_4gb_2k_128_data
  351. + .type = NAND_BOOT_AP_HEADER,
  352. + .ap = &nand_hdr_4gb_2k_128_data,
  353. + }, {
  354. + .name = "hsm:2k+64",
  355. + .type = NAND_BOOT_HSM_HEADER,
  356. + .hsm = &hsm_nand_hdr_2k_64_data,
  357. + }, {
  358. + .name = "hsm:2k+128",
  359. + .type = NAND_BOOT_HSM_HEADER,
  360. + .hsm = &hsm_nand_hdr_2k_128_data,
  361. + }, {
  362. + .name = "hsm:4k+256",
  363. + .type = NAND_BOOT_HSM_HEADER,
  364. + .hsm = &hsm_nand_hdr_4k_256_data,
  365. + }, {
  366. + .name = "hsm20:2k+64",
  367. + .type = NAND_BOOT_HSM20_HEADER,
  368. + .hsm20 = &hsm20_nand_hdr_2k_64_data,
  369. + }, {
  370. + .name = "hsm20:2k+128",
  371. + .type = NAND_BOOT_HSM20_HEADER,
  372. + .hsm20 = &hsm20_nand_hdr_2k_128_data,
  373. + }, {
  374. + .name = "hsm20:4k+256",
  375. + .type = NAND_BOOT_HSM20_HEADER,
  376. + .hsm20 = &hsm20_nand_hdr_4k_256_data,
  377. + }, {
  378. + .name = "spim:2k+64",
  379. + .type = NAND_BOOT_SPIM_HEADER,
  380. + .spim = &spim_nand_hdr_2k_64_data,
  381. + }, {
  382. + .name = "spim:2k+128",
  383. + .type = NAND_BOOT_SPIM_HEADER,
  384. + .spim = &spim_nand_hdr_2k_128_data,
  385. + }, {
  386. + .name = "spim:4k+256",
  387. + .type = NAND_BOOT_SPIM_HEADER,
  388. + .spim = &spim_nand_hdr_4k_256_data,
  389. }
  390. };
  391. -const union nand_boot_header *mtk_nand_header_find(const char *name)
  392. +const struct nand_header_type *mtk_nand_header_find(const char *name)
  393. {
  394. uint32_t i;
  395. for (i = 0; i < ARRAY_SIZE(nand_headers); i++) {
  396. if (!strcmp(nand_headers[i].name, name))
  397. - return nand_headers[i].data;
  398. + return &nand_headers[i];
  399. }
  400. return NULL;
  401. }
  402. -uint32_t mtk_nand_header_size(const union nand_boot_header *hdr_nand)
  403. +uint32_t mtk_nand_header_size(const struct nand_header_type *hdr_nand)
  404. {
  405. - return 2 * le16_to_cpu(hdr_nand->pagesize);
  406. + switch (hdr_nand->type) {
  407. + case NAND_BOOT_HSM_HEADER:
  408. + return le32_to_cpu(hdr_nand->hsm->page_size);
  409. +
  410. + case NAND_BOOT_HSM20_HEADER:
  411. + return le32_to_cpu(hdr_nand->hsm20->page_size);
  412. +
  413. + case NAND_BOOT_SPIM_HEADER:
  414. + return le32_to_cpu(hdr_nand->spim->page_size);
  415. +
  416. + default:
  417. + return 2 * le16_to_cpu(hdr_nand->ap->pagesize);
  418. + }
  419. }
  420. static int mtk_nand_header_ap_info(const void *ptr,
  421. @@ -251,14 +542,45 @@ static int mtk_nand_header_ap_info(const
  422. info->page_size = le16_to_cpu(nh->pagesize);
  423. info->spare_size = le16_to_cpu(nh->oobsize);
  424. info->gfh_offset = 2 * info->page_size;
  425. + info->snfi = true;
  426. return 0;
  427. }
  428. +static int mtk_nand_header_hsm_info(const void *ptr,
  429. + struct nand_header_info *info)
  430. +{
  431. + union hsm_nand_boot_header *nh = (union hsm_nand_boot_header *)ptr;
  432. +
  433. + info->page_size = le16_to_cpu(nh->page_size);
  434. + info->spare_size = le16_to_cpu(nh->spare_size);
  435. + info->gfh_offset = info->page_size;
  436. + info->snfi = true;
  437. +
  438. + return 1;
  439. +}
  440. +
  441. +static int mtk_nand_header_spim_info(const void *ptr,
  442. + struct nand_header_info *info)
  443. +{
  444. + union spim_nand_boot_header *nh = (union spim_nand_boot_header *)ptr;
  445. +
  446. + info->page_size = le16_to_cpu(nh->page_size);
  447. + info->spare_size = le16_to_cpu(nh->spare_size);
  448. + info->gfh_offset = info->page_size;
  449. + info->snfi = false;
  450. +
  451. + return 1;
  452. +}
  453. +
  454. int mtk_nand_header_info(const void *ptr, struct nand_header_info *info)
  455. {
  456. if (!strcmp((char *)ptr, NAND_BOOT_NAME))
  457. return mtk_nand_header_ap_info(ptr, info);
  458. + else if (!strncmp((char *)ptr, HSM_NAND_BOOT_NAME, 8))
  459. + return mtk_nand_header_hsm_info(ptr, info);
  460. + else if (!strncmp((char *)ptr, SPIM_NAND_BOOT_NAME, 8))
  461. + return mtk_nand_header_spim_info(ptr, info);
  462. return -1;
  463. }
  464. @@ -273,14 +595,74 @@ bool is_mtk_nand_header(const void *ptr)
  465. return false;
  466. }
  467. -uint32_t mtk_nand_header_put(const union nand_boot_header *hdr_nand, void *ptr)
  468. +static uint16_t crc16(const uint8_t *p, uint32_t len)
  469. +{
  470. + uint16_t crc = 0x4f4e;
  471. + uint32_t i;
  472. +
  473. + while (len--) {
  474. + crc ^= *p++ << 8;
  475. + for (i = 0; i < 8; i++)
  476. + crc = (crc << 1) ^ ((crc & 0x8000) ? 0x8005 : 0);
  477. + }
  478. +
  479. + return crc;
  480. +}
  481. +
  482. +static uint32_t mtk_nand_header_put_ap(const struct nand_header_type *hdr_nand,
  483. + void *ptr)
  484. {
  485. - union nand_boot_header *nh = (union nand_boot_header *)ptr;
  486. int i;
  487. /* NAND device header, repeat 4 times */
  488. - for (i = 0; i < 4; i++)
  489. - memcpy(nh + i, hdr_nand, sizeof(union nand_boot_header));
  490. + for (i = 0; i < 4; i++) {
  491. + memcpy(ptr, hdr_nand->ap, sizeof(*hdr_nand->ap));
  492. + ptr += sizeof(*hdr_nand->ap);
  493. + }
  494. +
  495. + return le16_to_cpu(hdr_nand->ap->pagesize);
  496. +}
  497. - return le16_to_cpu(hdr_nand->pagesize);
  498. +static uint32_t mtk_nand_header_put_hsm(const struct nand_header_type *hdr_nand,
  499. + void *ptr)
  500. +{
  501. + memcpy(ptr, hdr_nand->hsm, sizeof(*hdr_nand->hsm));
  502. + return 0;
  503. +}
  504. +
  505. +static uint32_t mtk_nand_header_put_hsm20(const struct nand_header_type *hdr_nand,
  506. + void *ptr)
  507. +{
  508. + memcpy(ptr, hdr_nand->hsm20, sizeof(*hdr_nand->hsm20));
  509. + return 0;
  510. +}
  511. +
  512. +static uint32_t mtk_nand_header_put_spim(const struct nand_header_type *hdr_nand,
  513. + void *ptr)
  514. +{
  515. + uint16_t crc;
  516. +
  517. + memcpy(ptr, hdr_nand->spim, sizeof(*hdr_nand->spim));
  518. +
  519. + crc = crc16(ptr, 0x4e);
  520. + memcpy(ptr + 0x4e, &crc, sizeof(uint16_t));
  521. +
  522. + return 0;
  523. +}
  524. +
  525. +uint32_t mtk_nand_header_put(const struct nand_header_type *hdr_nand, void *ptr)
  526. +{
  527. + switch (hdr_nand->type) {
  528. + case NAND_BOOT_HSM_HEADER:
  529. + return mtk_nand_header_put_hsm(hdr_nand, ptr);
  530. +
  531. + case NAND_BOOT_HSM20_HEADER:
  532. + return mtk_nand_header_put_hsm20(hdr_nand, ptr);
  533. +
  534. + case NAND_BOOT_SPIM_HEADER:
  535. + return mtk_nand_header_put_spim(hdr_nand, ptr);
  536. +
  537. + default:
  538. + return mtk_nand_header_put_ap(hdr_nand, ptr);
  539. + }
  540. }
  541. --- a/tools/mtk_nand_headers.h
  542. +++ b/tools/mtk_nand_headers.h
  543. @@ -16,6 +16,7 @@ struct nand_header_info {
  544. uint32_t page_size;
  545. uint32_t spare_size;
  546. uint32_t gfh_offset;
  547. + bool snfi;
  548. };
  549. /* AP BROM Header for NAND */
  550. @@ -39,15 +40,117 @@ union nand_boot_header {
  551. uint8_t data[0x80];
  552. };
  553. +/* HSM BROM Header for NAND */
  554. +union hsm_nand_boot_header {
  555. + struct {
  556. + char id[8];
  557. + uint32_t version; /* Header version */
  558. + uint32_t config; /* Header config */
  559. + uint32_t sector_size; /* ECC step size */
  560. + uint32_t fdm_size; /* User OOB size of a step */
  561. + uint32_t fdm_ecc_size; /* ECC parity size of a step */
  562. + uint32_t lbs;
  563. + uint32_t page_size; /* NAND page size */
  564. + uint32_t spare_size; /* NAND page spare size */
  565. + uint32_t page_per_block; /* Pages of one block */
  566. + uint32_t blocks; /* Total blocks of NAND chip */
  567. + uint32_t plane_sel_position; /* Plane bit position */
  568. + uint32_t pll; /* Value of pll reg */
  569. + uint32_t acccon; /* Value of access timing reg */
  570. + uint32_t strobe_sel; /* Value of DQS selection reg*/
  571. + uint32_t acccon1; /* Value of access timing reg */
  572. + uint32_t dqs_mux; /* Value of DQS mux reg */
  573. + uint32_t dqs_ctrl; /* Value of DQS control reg */
  574. + uint32_t delay_ctrl; /* Value of delay ctrl reg */
  575. + uint32_t latch_lat; /* Value of latch latency reg */
  576. + uint32_t sample_delay; /* Value of sample delay reg */
  577. + uint32_t driving; /* Value of driving reg */
  578. + uint32_t bl_start; /* Bootloader start addr */
  579. + uint32_t bl_end; /* Bootloader end addr */
  580. + uint8_t ecc_parity[42]; /* ECC parity of this header */
  581. + };
  582. +
  583. + uint8_t data[0x8E];
  584. +};
  585. +
  586. +/* HSM2.0 BROM Header for NAND */
  587. +union hsm20_nand_boot_header {
  588. + struct {
  589. + char id[8];
  590. + uint32_t version; /* Header version */
  591. + uint32_t config; /* Header config */
  592. + uint32_t sector_size; /* ECC step size */
  593. + uint32_t fdm_size; /* User OOB size of a step */
  594. + uint32_t fdm_ecc_size; /* ECC parity size of a step */
  595. + uint32_t lbs;
  596. + uint32_t page_size; /* NAND page size */
  597. + uint32_t spare_size; /* NAND page spare size */
  598. + uint32_t page_per_block; /* Pages of one block */
  599. + uint32_t blocks; /* Total blocks of NAND chip */
  600. + uint32_t plane_sel_position; /* Plane bit position */
  601. + uint32_t pll; /* Value of pll reg */
  602. + uint32_t acccon; /* Value of access timing reg */
  603. + uint32_t strobe_sel; /* Value of DQS selection reg*/
  604. + uint32_t acccon1; /* Value of access timing reg */
  605. + uint32_t dqs_mux; /* Value of DQS mux reg */
  606. + uint32_t dqs_ctrl; /* Value of DQS control reg */
  607. + uint32_t delay_ctrl; /* Value of delay ctrl reg */
  608. + uint32_t latch_lat; /* Value of latch latency reg */
  609. + uint32_t sample_delay; /* Value of sample delay reg */
  610. + uint32_t driving; /* Value of driving reg */
  611. + uint32_t reserved;
  612. + uint32_t bl0_start; /* Bootloader start addr */
  613. + uint32_t bl0_end; /* Bootloader end addr */
  614. + uint32_t bl0_type; /* Bootloader type */
  615. + uint8_t bl_reserve[84];
  616. + uint8_t ecc_parity[42]; /* ECC parity of this header */
  617. + };
  618. +
  619. + uint8_t data[0xEA];
  620. +};
  621. +
  622. +/* SPIM BROM Header for SPI-NAND */
  623. +union spim_nand_boot_header {
  624. + struct {
  625. + char id[8];
  626. + uint32_t version; /* Header version */
  627. + uint32_t config; /* Header config */
  628. + uint32_t page_size; /* NAND page size */
  629. + uint32_t spare_size; /* NAND page spare size */
  630. + uint16_t page_per_block; /* Pages of one block */
  631. + uint16_t plane_sel_position; /* Plane bit position */
  632. + uint16_t reserve_reg;
  633. + uint16_t reserve_val;
  634. + uint16_t ecc_error; /* ECC error reg addr */
  635. + uint16_t ecc_mask; /* ECC error bit mask */
  636. + uint32_t bl_start; /* Bootloader start addr */
  637. + uint32_t bl_end; /* Bootloader end addr */
  638. + uint8_t ecc_parity[32]; /* ECC parity of this header */
  639. + uint32_t integrity_crc; /* CRC of this header */
  640. + };
  641. +
  642. + uint8_t data[0x50];
  643. +};
  644. +
  645. +enum nand_boot_header_type {
  646. + NAND_BOOT_AP_HEADER,
  647. + NAND_BOOT_HSM_HEADER,
  648. + NAND_BOOT_HSM20_HEADER,
  649. + NAND_BOOT_SPIM_HEADER
  650. +};
  651. +
  652. #define NAND_BOOT_NAME "BOOTLOADER!"
  653. #define NAND_BOOT_VERSION "V006"
  654. #define NAND_BOOT_ID "NFIINFO"
  655. +#define HSM_NAND_BOOT_NAME "NANDCFG!"
  656. +#define SPIM_NAND_BOOT_NAME "SPINAND!"
  657. +
  658. /* Find nand header data by name */
  659. -const union nand_boot_header *mtk_nand_header_find(const char *name);
  660. +const struct nand_header_type *mtk_nand_header_find(const char *name);
  661. /* Device header size using this nand header */
  662. -uint32_t mtk_nand_header_size(const union nand_boot_header *hdr_nand);
  663. +uint32_t mtk_nand_header_size(const struct nand_header_type *hdr_nand);
  664. /* Get nand info from nand header (page size, spare size, ...) */
  665. int mtk_nand_header_info(const void *ptr, struct nand_header_info *info);
  666. @@ -56,6 +159,7 @@ int mtk_nand_header_info(const void *ptr
  667. bool is_mtk_nand_header(const void *ptr);
  668. /* Generate Device header using give nand header */
  669. -uint32_t mtk_nand_header_put(const union nand_boot_header *hdr_nand, void *ptr);
  670. +uint32_t mtk_nand_header_put(const struct nand_header_type *hdr_nand,
  671. + void *ptr);
  672. #endif /* _MTK_NAND_HEADERS_H */