021-tools-mtk_image-split-the-code-of-generating-NAND-he.patch 28 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821
  1. From 20ebf03eab571b25e9f62b2764ab84932111dcd6 Mon Sep 17 00:00:00 2001
  2. From: Weijie Gao <[email protected]>
  3. Date: Tue, 2 Aug 2022 17:23:57 +0800
  4. Subject: [PATCH 29/31] tools: mtk_image: split the code of generating NAND
  5. header into a new file
  6. The predefined NAND headers take too much spaces in the mtk_image.c.
  7. Moving them into a new file can significantly improve the readability of
  8. both mtk_image.c and the new mtk_nand_headers.c.
  9. This is a preparation for adding more NAND headers.
  10. Reviewed-by: Simon Glass <[email protected]>
  11. Signed-off-by: Weijie Gao <[email protected]>
  12. ---
  13. tools/Makefile | 1 +
  14. tools/mtk_image.c | 305 ++++++---------------------------------
  15. tools/mtk_image.h | 25 ----
  16. tools/mtk_nand_headers.c | 286 ++++++++++++++++++++++++++++++++++++
  17. tools/mtk_nand_headers.h | 61 ++++++++
  18. 5 files changed, 389 insertions(+), 289 deletions(-)
  19. create mode 100644 tools/mtk_nand_headers.c
  20. create mode 100644 tools/mtk_nand_headers.h
  21. --- a/tools/Makefile
  22. +++ b/tools/Makefile
  23. @@ -147,6 +147,7 @@ dumpimage-mkimage-objs := aisimage.o \
  24. gpimage.o \
  25. gpimage-common.o \
  26. mtk_image.o \
  27. + mtk_nand_headers.o \
  28. $(ECDSA_OBJS-y) \
  29. $(RSA_OBJS-y) \
  30. $(AES_OBJS-y)
  31. --- a/tools/mtk_image.c
  32. +++ b/tools/mtk_image.c
  33. @@ -12,216 +12,7 @@
  34. #include <u-boot/sha256.h>
  35. #include "imagetool.h"
  36. #include "mtk_image.h"
  37. -
  38. -/* NAND header for SPI-NAND with 2KB page + 64B spare */
  39. -static const union nand_boot_header snand_hdr_2k_64_data = {
  40. - .data = {
  41. - 0x42, 0x4F, 0x4F, 0x54, 0x4C, 0x4F, 0x41, 0x44,
  42. - 0x45, 0x52, 0x21, 0x00, 0x56, 0x30, 0x30, 0x36,
  43. - 0x4E, 0x46, 0x49, 0x49, 0x4E, 0x46, 0x4F, 0x00,
  44. - 0x00, 0x00, 0x00, 0x08, 0x03, 0x00, 0x40, 0x00,
  45. - 0x40, 0x00, 0x00, 0x08, 0x10, 0x00, 0x16, 0x00,
  46. - 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  47. - 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  48. - 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  49. - 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  50. - 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  51. - 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  52. - 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  53. - 0x00, 0x00, 0x00, 0x00, 0x7B, 0xC4, 0x17, 0x9D,
  54. - 0xCA, 0x42, 0x90, 0xD0, 0x98, 0xD0, 0xE0, 0xF7,
  55. - 0xDB, 0xCD, 0x16, 0xF6, 0x03, 0x73, 0xD2, 0xB8,
  56. - 0x93, 0xB2, 0x56, 0x5A, 0x84, 0x6E, 0x00, 0x00
  57. - }
  58. -};
  59. -
  60. -/* NAND header for SPI-NAND with 2KB page + 120B/128B spare */
  61. -static const union nand_boot_header snand_hdr_2k_128_data = {
  62. - .data = {
  63. - 0x42, 0x4F, 0x4F, 0x54, 0x4C, 0x4F, 0x41, 0x44,
  64. - 0x45, 0x52, 0x21, 0x00, 0x56, 0x30, 0x30, 0x36,
  65. - 0x4E, 0x46, 0x49, 0x49, 0x4E, 0x46, 0x4F, 0x00,
  66. - 0x00, 0x00, 0x00, 0x08, 0x05, 0x00, 0x70, 0x00,
  67. - 0x40, 0x00, 0x00, 0x08, 0x10, 0x00, 0x16, 0x00,
  68. - 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  69. - 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  70. - 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  71. - 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  72. - 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  73. - 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  74. - 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  75. - 0x00, 0x00, 0x00, 0x00, 0x90, 0x28, 0xED, 0x13,
  76. - 0x7F, 0x12, 0x22, 0xCD, 0x3D, 0x06, 0xF1, 0xB3,
  77. - 0x6F, 0x2E, 0xD9, 0xA0, 0x9D, 0x7A, 0xBD, 0xD7,
  78. - 0xB3, 0x28, 0x3C, 0x13, 0xDB, 0x4E, 0x00, 0x00
  79. - }
  80. -};
  81. -
  82. -/* NAND header for SPI-NAND with 4KB page + 256B spare */
  83. -static const union nand_boot_header snand_hdr_4k_256_data = {
  84. - .data = {
  85. - 0x42, 0x4F, 0x4F, 0x54, 0x4C, 0x4F, 0x41, 0x44,
  86. - 0x45, 0x52, 0x21, 0x00, 0x56, 0x30, 0x30, 0x36,
  87. - 0x4E, 0x46, 0x49, 0x49, 0x4E, 0x46, 0x4F, 0x00,
  88. - 0x00, 0x00, 0x00, 0x10, 0x05, 0x00, 0xE0, 0x00,
  89. - 0x40, 0x00, 0x00, 0x08, 0x10, 0x00, 0x16, 0x00,
  90. - 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  91. - 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  92. - 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  93. - 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  94. - 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  95. - 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  96. - 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  97. - 0x00, 0x00, 0x00, 0x00, 0x47, 0xED, 0x0E, 0xC3,
  98. - 0x83, 0xBF, 0x41, 0xD2, 0x85, 0x21, 0x97, 0x57,
  99. - 0xC4, 0x2E, 0x6B, 0x7A, 0x40, 0xE0, 0xCF, 0x8F,
  100. - 0x37, 0xBD, 0x17, 0xB6, 0xC7, 0xFE, 0x00, 0x00
  101. - }
  102. -};
  103. -
  104. -/* NAND header for Parallel NAND 1Gb with 2KB page + 64B spare */
  105. -static const union nand_boot_header nand_hdr_1gb_2k_64_data = {
  106. - .data = {
  107. - 0x42, 0x4F, 0x4F, 0x54, 0x4C, 0x4F, 0x41, 0x44,
  108. - 0x45, 0x52, 0x21, 0x00, 0x56, 0x30, 0x30, 0x36,
  109. - 0x4E, 0x46, 0x49, 0x49, 0x4E, 0x46, 0x4F, 0x00,
  110. - 0x00, 0x00, 0x00, 0x08, 0x05, 0x00, 0x40, 0x00,
  111. - 0x40, 0x00, 0x00, 0x04, 0x0B, 0x00, 0x11, 0x00,
  112. - 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  113. - 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  114. - 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  115. - 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  116. - 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  117. - 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  118. - 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  119. - 0x00, 0x00, 0x00, 0x00, 0x12, 0x28, 0x1C, 0x12,
  120. - 0x8F, 0xFD, 0xF8, 0x32, 0x6F, 0x6D, 0xCF, 0x6C,
  121. - 0xDA, 0x21, 0x70, 0x8C, 0xDA, 0x0A, 0x22, 0x82,
  122. - 0xAA, 0x59, 0xFA, 0x7C, 0x42, 0x2D, 0x00, 0x00
  123. - }
  124. -};
  125. -
  126. -/* NAND header for Parallel NAND 2Gb with 2KB page + 64B spare */
  127. -static const union nand_boot_header nand_hdr_2gb_2k_64_data = {
  128. - .data = {
  129. - 0x42, 0x4F, 0x4F, 0x54, 0x4C, 0x4F, 0x41, 0x44,
  130. - 0x45, 0x52, 0x21, 0x00, 0x56, 0x30, 0x30, 0x36,
  131. - 0x4E, 0x46, 0x49, 0x49, 0x4E, 0x46, 0x4F, 0x00,
  132. - 0x00, 0x00, 0x00, 0x08, 0x05, 0x00, 0x40, 0x00,
  133. - 0x40, 0x00, 0x00, 0x08, 0x0B, 0x00, 0x11, 0x00,
  134. - 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  135. - 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  136. - 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  137. - 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  138. - 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  139. - 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  140. - 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  141. - 0x00, 0x00, 0x00, 0x00, 0x20, 0x9C, 0x3D, 0x2D,
  142. - 0x7B, 0x68, 0x63, 0x52, 0x2E, 0x04, 0x63, 0xF1,
  143. - 0x35, 0x4E, 0x44, 0x3E, 0xF8, 0xAC, 0x9B, 0x95,
  144. - 0xAB, 0xFE, 0xE4, 0xE1, 0xD5, 0xF9, 0x00, 0x00
  145. - }
  146. -};
  147. -
  148. -/* NAND header for Parallel NAND 4Gb with 2KB page + 64B spare */
  149. -static const union nand_boot_header nand_hdr_4gb_2k_64_data = {
  150. - .data = {
  151. - 0x42, 0x4F, 0x4F, 0x54, 0x4C, 0x4F, 0x41, 0x44,
  152. - 0x45, 0x52, 0x21, 0x00, 0x56, 0x30, 0x30, 0x36,
  153. - 0x4E, 0x46, 0x49, 0x49, 0x4E, 0x46, 0x4F, 0x00,
  154. - 0x00, 0x00, 0x00, 0x08, 0x05, 0x00, 0x40, 0x00,
  155. - 0x40, 0x00, 0x00, 0x10, 0x0B, 0x00, 0x11, 0x00,
  156. - 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  157. - 0x00, 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, 0xE3, 0x0F, 0x86, 0x32,
  164. - 0x68, 0x05, 0xD9, 0xC8, 0x13, 0xDF, 0xC5, 0x0B,
  165. - 0x35, 0x3A, 0x68, 0xA5, 0x3C, 0x0C, 0x73, 0x87,
  166. - 0x63, 0xB0, 0xBE, 0xCC, 0x84, 0x47, 0x00, 0x00
  167. - }
  168. -};
  169. -
  170. -/* NAND header for Parallel NAND 2Gb with 2KB page + 128B spare */
  171. -static const union nand_boot_header nand_hdr_2gb_2k_128_data = {
  172. - .data = {
  173. - 0x42, 0x4F, 0x4F, 0x54, 0x4C, 0x4F, 0x41, 0x44,
  174. - 0x45, 0x52, 0x21, 0x00, 0x56, 0x30, 0x30, 0x36,
  175. - 0x4E, 0x46, 0x49, 0x49, 0x4E, 0x46, 0x4F, 0x00,
  176. - 0x00, 0x00, 0x00, 0x08, 0x05, 0x00, 0x70, 0x00,
  177. - 0x40, 0x00, 0x00, 0x08, 0x0B, 0x00, 0x11, 0x00,
  178. - 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  179. - 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  180. - 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  181. - 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  182. - 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  183. - 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  184. - 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  185. - 0x00, 0x00, 0x00, 0x00, 0x01, 0xA5, 0xE9, 0x5A,
  186. - 0xDF, 0x58, 0x62, 0x41, 0xD6, 0x26, 0x77, 0xBC,
  187. - 0x76, 0x1F, 0x27, 0x4E, 0x4F, 0x6C, 0xC3, 0xF0,
  188. - 0x36, 0xDE, 0xD9, 0xB3, 0xFF, 0x93, 0x00, 0x00
  189. - }
  190. -};
  191. -
  192. -/* NAND header for Parallel NAND 4Gb with 2KB page + 128B spare */
  193. -static const union nand_boot_header nand_hdr_4gb_2k_128_data = {
  194. - .data = {
  195. - 0x42, 0x4F, 0x4F, 0x54, 0x4C, 0x4F, 0x41, 0x44,
  196. - 0x45, 0x52, 0x21, 0x00, 0x56, 0x30, 0x30, 0x36,
  197. - 0x4E, 0x46, 0x49, 0x49, 0x4E, 0x46, 0x4F, 0x00,
  198. - 0x00, 0x00, 0x00, 0x08, 0x05, 0x00, 0x70, 0x00,
  199. - 0x40, 0x00, 0x00, 0x10, 0x0B, 0x00, 0x11, 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. - 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  205. - 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  206. - 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  207. - 0x00, 0x00, 0x00, 0x00, 0xC2, 0x36, 0x52, 0x45,
  208. - 0xCC, 0x35, 0xD8, 0xDB, 0xEB, 0xFD, 0xD1, 0x46,
  209. - 0x76, 0x6B, 0x0B, 0xD5, 0x8B, 0xCC, 0x2B, 0xE2,
  210. - 0xFE, 0x90, 0x83, 0x9E, 0xAE, 0x2D, 0x00, 0x00
  211. - }
  212. -};
  213. -
  214. -static const struct nand_header_type {
  215. - const char *name;
  216. - const union nand_boot_header *data;
  217. -} nand_headers[] = {
  218. - {
  219. - .name = "2k+64",
  220. - .data = &snand_hdr_2k_64_data
  221. - }, {
  222. - .name = "2k+120",
  223. - .data = &snand_hdr_2k_128_data
  224. - }, {
  225. - .name = "2k+128",
  226. - .data = &snand_hdr_2k_128_data
  227. - }, {
  228. - .name = "4k+256",
  229. - .data = &snand_hdr_4k_256_data
  230. - }, {
  231. - .name = "1g:2k+64",
  232. - .data = &nand_hdr_1gb_2k_64_data
  233. - }, {
  234. - .name = "2g:2k+64",
  235. - .data = &nand_hdr_2gb_2k_64_data
  236. - }, {
  237. - .name = "4g:2k+64",
  238. - .data = &nand_hdr_4gb_2k_64_data
  239. - }, {
  240. - .name = "2g:2k+128",
  241. - .data = &nand_hdr_2gb_2k_128_data
  242. - }, {
  243. - .name = "4g:2k+128",
  244. - .data = &nand_hdr_4gb_2k_128_data
  245. - }
  246. -};
  247. +#include "mtk_nand_headers.h"
  248. static const struct brom_img_type {
  249. const char *name;
  250. @@ -264,6 +55,7 @@ static uint32_t crc32tbl[256];
  251. /* NAND header selected by user */
  252. static const union nand_boot_header *hdr_nand;
  253. +static uint32_t hdr_nand_size;
  254. /* GFH header + 2 * 4KB pages of NAND */
  255. static char hdr_tmp[sizeof(struct gfh_header) + 0x2000];
  256. @@ -402,12 +194,7 @@ static int mtk_brom_parse_imagename(cons
  257. }
  258. /* parse nand header type */
  259. - for (i = 0; i < ARRAY_SIZE(nand_headers); i++) {
  260. - if (!strcmp(nand_headers[i].name, nandinfo)) {
  261. - hdr_nand = nand_headers[i].data;
  262. - break;
  263. - }
  264. - }
  265. + hdr_nand = mtk_nand_header_find(nandinfo);
  266. /* parse device header offset */
  267. if (hdr_offs && hdr_offs[0])
  268. @@ -432,6 +219,9 @@ static int mtk_brom_parse_imagename(cons
  269. return -EINVAL;
  270. }
  271. + if (hdr_media == BRLYT_TYPE_NAND || hdr_media == BRLYT_TYPE_SNAND)
  272. + hdr_nand_size = mtk_nand_header_size(hdr_nand);
  273. +
  274. return 0;
  275. }
  276. @@ -468,7 +258,7 @@ static int mtk_image_vrec_header(struct
  277. }
  278. if (hdr_media == BRLYT_TYPE_NAND || hdr_media == BRLYT_TYPE_SNAND)
  279. - tparams->header_size = 2 * le16_to_cpu(hdr_nand->pagesize);
  280. + tparams->header_size = hdr_nand_size;
  281. else
  282. tparams->header_size = sizeof(struct gen_device_header);
  283. @@ -566,16 +356,17 @@ static int mtk_image_verify_gen_header(c
  284. static int mtk_image_verify_nand_header(const uint8_t *ptr, int print)
  285. {
  286. - union nand_boot_header *nh = (union nand_boot_header *)ptr;
  287. struct brom_layout_header *bh;
  288. + struct nand_header_info info;
  289. struct gfh_header *gfh;
  290. const char *bootmedia;
  291. + int ret;
  292. - if (strncmp(nh->version, NAND_BOOT_VERSION, sizeof(nh->version)) ||
  293. - strcmp(nh->id, NAND_BOOT_ID))
  294. - return -1;
  295. + ret = mtk_nand_header_info(ptr, &info);
  296. + if (ret < 0)
  297. + return ret;
  298. - bh = (struct brom_layout_header *)(ptr + le16_to_cpu(nh->pagesize));
  299. + bh = (struct brom_layout_header *)(ptr + info.page_size);
  300. if (strcmp(bh->name, BRLYT_NAME))
  301. return -1;
  302. @@ -586,34 +377,23 @@ static int mtk_image_verify_nand_header(
  303. if (le32_to_cpu(bh->type) == BRLYT_TYPE_NAND)
  304. bootmedia = "Parallel NAND";
  305. else if (le32_to_cpu(bh->type) == BRLYT_TYPE_SNAND)
  306. - bootmedia = "Serial NAND";
  307. + bootmedia = "Serial NAND (SNFI/AP)";
  308. else
  309. return -1;
  310. }
  311. if (print) {
  312. - printf("Boot Media: %s\n", bootmedia);
  313. -
  314. - if (le32_to_cpu(bh->type) == BRLYT_TYPE_NAND) {
  315. - uint64_t capacity =
  316. - (uint64_t)le16_to_cpu(nh->numblocks) *
  317. - (uint64_t)le16_to_cpu(nh->pages_of_block) *
  318. - (uint64_t)le16_to_cpu(nh->pagesize) * 8;
  319. - printf("Capacity: %dGb\n",
  320. - (uint32_t)(capacity >> 30));
  321. - }
  322. + printf("Boot Media: %s\n", bootmedia);
  323. - if (le16_to_cpu(nh->pagesize) >= 1024)
  324. - printf("Page Size: %dKB\n",
  325. - le16_to_cpu(nh->pagesize) >> 10);
  326. + if (info.page_size >= 1024)
  327. + printf("Page Size: %dKB\n", info.page_size >> 10);
  328. else
  329. - printf("Page Size: %dB\n",
  330. - le16_to_cpu(nh->pagesize));
  331. + printf("Page Size: %dB\n", info.page_size);
  332. - printf("Spare Size: %dB\n", le16_to_cpu(nh->oobsize));
  333. + printf("Spare Size: %dB\n", info.spare_size);
  334. }
  335. - gfh = (struct gfh_header *)(ptr + 2 * le16_to_cpu(nh->pagesize));
  336. + gfh = (struct gfh_header *)(ptr + info.gfh_offset);
  337. return mtk_image_verify_gfh(gfh, GFH_FLASH_TYPE_NAND, print);
  338. }
  339. @@ -713,7 +493,7 @@ static int mtk_image_verify_header(unsig
  340. if (image_get_magic(hdr) == IH_MAGIC)
  341. return mtk_image_verify_mt7621_header(ptr, 0);
  342. - if (!strcmp((char *)ptr, NAND_BOOT_NAME))
  343. + if (is_mtk_nand_header(ptr))
  344. return mtk_image_verify_nand_header(ptr, 0);
  345. else
  346. return mtk_image_verify_gen_header(ptr, 0);
  347. @@ -739,7 +519,7 @@ static void mtk_image_print_header(const
  348. return;
  349. }
  350. - if (!strcmp((char *)ptr, NAND_BOOT_NAME))
  351. + if (is_mtk_nand_header(ptr))
  352. mtk_image_verify_nand_header(ptr, 1);
  353. else
  354. mtk_image_verify_gen_header(ptr, 1);
  355. @@ -870,36 +650,33 @@ static void mtk_image_set_gen_header(voi
  356. static void mtk_image_set_nand_header(void *ptr, off_t filesize,
  357. uint32_t loadaddr)
  358. {
  359. - union nand_boot_header *nh = (union nand_boot_header *)ptr;
  360. struct brom_layout_header *brlyt;
  361. struct gfh_header *gfh;
  362. - uint32_t payload_pages;
  363. - int i;
  364. + uint32_t payload_pages, nand_page_size;
  365. - /* NAND device header, repeat 4 times */
  366. - for (i = 0; i < 4; i++)
  367. - memcpy(nh + i, hdr_nand, sizeof(union nand_boot_header));
  368. + /* NAND header */
  369. + nand_page_size = mtk_nand_header_put(hdr_nand, ptr);
  370. - /* BRLYT header */
  371. - payload_pages = (filesize + le16_to_cpu(hdr_nand->pagesize) - 1) /
  372. - le16_to_cpu(hdr_nand->pagesize);
  373. - brlyt = (struct brom_layout_header *)
  374. - (ptr + le16_to_cpu(hdr_nand->pagesize));
  375. - put_brom_layout_header(brlyt, hdr_media);
  376. - brlyt->header_size = cpu_to_le32(2);
  377. - brlyt->total_size = cpu_to_le32(payload_pages);
  378. - brlyt->header_size_2 = brlyt->header_size;
  379. - brlyt->total_size_2 = brlyt->total_size;
  380. - brlyt->unused = cpu_to_le32(1);
  381. + if (nand_page_size) {
  382. + /* BRLYT header */
  383. + payload_pages = (filesize + nand_page_size - 1) /
  384. + nand_page_size;
  385. + brlyt = (struct brom_layout_header *)(ptr + nand_page_size);
  386. + put_brom_layout_header(brlyt, hdr_media);
  387. + brlyt->header_size = cpu_to_le32(2);
  388. + brlyt->total_size = cpu_to_le32(payload_pages);
  389. + brlyt->header_size_2 = brlyt->header_size;
  390. + brlyt->total_size_2 = brlyt->total_size;
  391. + brlyt->unused = cpu_to_le32(1);
  392. + }
  393. /* GFH header */
  394. - gfh = (struct gfh_header *)(ptr + 2 * le16_to_cpu(hdr_nand->pagesize));
  395. - put_ghf_header(gfh, filesize, 2 * le16_to_cpu(hdr_nand->pagesize),
  396. - loadaddr, GFH_FLASH_TYPE_NAND);
  397. + gfh = (struct gfh_header *)(ptr + hdr_nand_size);
  398. + put_ghf_header(gfh, filesize, hdr_nand_size, loadaddr,
  399. + GFH_FLASH_TYPE_NAND);
  400. /* Generate SHA256 hash */
  401. - put_hash((uint8_t *)gfh,
  402. - filesize - 2 * le16_to_cpu(hdr_nand->pagesize) - SHA256_SUM_LEN);
  403. + put_hash((uint8_t *)gfh, filesize - hdr_nand_size - SHA256_SUM_LEN);
  404. }
  405. static void mtk_image_set_mt7621_header(void *ptr, off_t filesize,
  406. --- a/tools/mtk_image.h
  407. +++ b/tools/mtk_image.h
  408. @@ -26,31 +26,6 @@ union gen_boot_header {
  409. #define SF_BOOT_NAME "SF_BOOT"
  410. #define SDMMC_BOOT_NAME "SDMMC_BOOT"
  411. -/* Header for NAND */
  412. -union nand_boot_header {
  413. - struct {
  414. - char name[12];
  415. - char version[4];
  416. - char id[8];
  417. - uint16_t ioif;
  418. - uint16_t pagesize;
  419. - uint16_t addrcycles;
  420. - uint16_t oobsize;
  421. - uint16_t pages_of_block;
  422. - uint16_t numblocks;
  423. - uint16_t writesize_shift;
  424. - uint16_t erasesize_shift;
  425. - uint8_t dummy[60];
  426. - uint8_t ecc_parity[28];
  427. - };
  428. -
  429. - uint8_t data[0x80];
  430. -};
  431. -
  432. -#define NAND_BOOT_NAME "BOOTLOADER!"
  433. -#define NAND_BOOT_VERSION "V006"
  434. -#define NAND_BOOT_ID "NFIINFO"
  435. -
  436. /* BootROM layout header */
  437. struct brom_layout_header {
  438. char name[8];
  439. --- /dev/null
  440. +++ b/tools/mtk_nand_headers.c
  441. @@ -0,0 +1,286 @@
  442. +// SPDX-License-Identifier: GPL-2.0+
  443. +/*
  444. + * MediaTek BootROM NAND header definitions
  445. + *
  446. + * Copyright (C) 2022 MediaTek Inc.
  447. + * Author: Weijie Gao <[email protected]>
  448. + */
  449. +
  450. +#include <stdint.h>
  451. +#include <string.h>
  452. +#include "imagetool.h"
  453. +#include "mtk_image.h"
  454. +#include "mtk_nand_headers.h"
  455. +
  456. +/* NAND header for SPI-NAND with 2KB page + 64B spare */
  457. +static const union nand_boot_header snand_hdr_2k_64_data = {
  458. + .data = {
  459. + 0x42, 0x4F, 0x4F, 0x54, 0x4C, 0x4F, 0x41, 0x44,
  460. + 0x45, 0x52, 0x21, 0x00, 0x56, 0x30, 0x30, 0x36,
  461. + 0x4E, 0x46, 0x49, 0x49, 0x4E, 0x46, 0x4F, 0x00,
  462. + 0x00, 0x00, 0x00, 0x08, 0x03, 0x00, 0x40, 0x00,
  463. + 0x40, 0x00, 0x00, 0x08, 0x10, 0x00, 0x16, 0x00,
  464. + 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  465. + 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  466. + 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  467. + 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  468. + 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  469. + 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  470. + 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  471. + 0x00, 0x00, 0x00, 0x00, 0x7B, 0xC4, 0x17, 0x9D,
  472. + 0xCA, 0x42, 0x90, 0xD0, 0x98, 0xD0, 0xE0, 0xF7,
  473. + 0xDB, 0xCD, 0x16, 0xF6, 0x03, 0x73, 0xD2, 0xB8,
  474. + 0x93, 0xB2, 0x56, 0x5A, 0x84, 0x6E, 0x00, 0x00
  475. + }
  476. +};
  477. +
  478. +/* NAND header for SPI-NAND with 2KB page + 120B/128B spare */
  479. +static const union nand_boot_header snand_hdr_2k_128_data = {
  480. + .data = {
  481. + 0x42, 0x4F, 0x4F, 0x54, 0x4C, 0x4F, 0x41, 0x44,
  482. + 0x45, 0x52, 0x21, 0x00, 0x56, 0x30, 0x30, 0x36,
  483. + 0x4E, 0x46, 0x49, 0x49, 0x4E, 0x46, 0x4F, 0x00,
  484. + 0x00, 0x00, 0x00, 0x08, 0x05, 0x00, 0x70, 0x00,
  485. + 0x40, 0x00, 0x00, 0x08, 0x10, 0x00, 0x16, 0x00,
  486. + 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  487. + 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  488. + 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  489. + 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  490. + 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  491. + 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  492. + 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  493. + 0x00, 0x00, 0x00, 0x00, 0x90, 0x28, 0xED, 0x13,
  494. + 0x7F, 0x12, 0x22, 0xCD, 0x3D, 0x06, 0xF1, 0xB3,
  495. + 0x6F, 0x2E, 0xD9, 0xA0, 0x9D, 0x7A, 0xBD, 0xD7,
  496. + 0xB3, 0x28, 0x3C, 0x13, 0xDB, 0x4E, 0x00, 0x00
  497. + }
  498. +};
  499. +
  500. +/* NAND header for SPI-NAND with 4KB page + 256B spare */
  501. +static const union nand_boot_header snand_hdr_4k_256_data = {
  502. + .data = {
  503. + 0x42, 0x4F, 0x4F, 0x54, 0x4C, 0x4F, 0x41, 0x44,
  504. + 0x45, 0x52, 0x21, 0x00, 0x56, 0x30, 0x30, 0x36,
  505. + 0x4E, 0x46, 0x49, 0x49, 0x4E, 0x46, 0x4F, 0x00,
  506. + 0x00, 0x00, 0x00, 0x10, 0x05, 0x00, 0xE0, 0x00,
  507. + 0x40, 0x00, 0x00, 0x08, 0x10, 0x00, 0x16, 0x00,
  508. + 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  509. + 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  510. + 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  511. + 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  512. + 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  513. + 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  514. + 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  515. + 0x00, 0x00, 0x00, 0x00, 0x47, 0xED, 0x0E, 0xC3,
  516. + 0x83, 0xBF, 0x41, 0xD2, 0x85, 0x21, 0x97, 0x57,
  517. + 0xC4, 0x2E, 0x6B, 0x7A, 0x40, 0xE0, 0xCF, 0x8F,
  518. + 0x37, 0xBD, 0x17, 0xB6, 0xC7, 0xFE, 0x00, 0x00
  519. + }
  520. +};
  521. +
  522. +/* NAND header for Parallel NAND 1Gb with 2KB page + 64B spare */
  523. +static const union nand_boot_header nand_hdr_1gb_2k_64_data = {
  524. + .data = {
  525. + 0x42, 0x4F, 0x4F, 0x54, 0x4C, 0x4F, 0x41, 0x44,
  526. + 0x45, 0x52, 0x21, 0x00, 0x56, 0x30, 0x30, 0x36,
  527. + 0x4E, 0x46, 0x49, 0x49, 0x4E, 0x46, 0x4F, 0x00,
  528. + 0x00, 0x00, 0x00, 0x08, 0x05, 0x00, 0x40, 0x00,
  529. + 0x40, 0x00, 0x00, 0x04, 0x0B, 0x00, 0x11, 0x00,
  530. + 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  531. + 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  532. + 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  533. + 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  534. + 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  535. + 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  536. + 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  537. + 0x00, 0x00, 0x00, 0x00, 0x12, 0x28, 0x1C, 0x12,
  538. + 0x8F, 0xFD, 0xF8, 0x32, 0x6F, 0x6D, 0xCF, 0x6C,
  539. + 0xDA, 0x21, 0x70, 0x8C, 0xDA, 0x0A, 0x22, 0x82,
  540. + 0xAA, 0x59, 0xFA, 0x7C, 0x42, 0x2D, 0x00, 0x00
  541. + }
  542. +};
  543. +
  544. +/* NAND header for Parallel NAND 2Gb with 2KB page + 64B spare */
  545. +static const union nand_boot_header nand_hdr_2gb_2k_64_data = {
  546. + .data = {
  547. + 0x42, 0x4F, 0x4F, 0x54, 0x4C, 0x4F, 0x41, 0x44,
  548. + 0x45, 0x52, 0x21, 0x00, 0x56, 0x30, 0x30, 0x36,
  549. + 0x4E, 0x46, 0x49, 0x49, 0x4E, 0x46, 0x4F, 0x00,
  550. + 0x00, 0x00, 0x00, 0x08, 0x05, 0x00, 0x40, 0x00,
  551. + 0x40, 0x00, 0x00, 0x08, 0x0B, 0x00, 0x11, 0x00,
  552. + 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  553. + 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  554. + 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  555. + 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  556. + 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  557. + 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  558. + 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  559. + 0x00, 0x00, 0x00, 0x00, 0x20, 0x9C, 0x3D, 0x2D,
  560. + 0x7B, 0x68, 0x63, 0x52, 0x2E, 0x04, 0x63, 0xF1,
  561. + 0x35, 0x4E, 0x44, 0x3E, 0xF8, 0xAC, 0x9B, 0x95,
  562. + 0xAB, 0xFE, 0xE4, 0xE1, 0xD5, 0xF9, 0x00, 0x00
  563. + }
  564. +};
  565. +
  566. +/* NAND header for Parallel NAND 4Gb with 2KB page + 64B spare */
  567. +static const union nand_boot_header nand_hdr_4gb_2k_64_data = {
  568. + .data = {
  569. + 0x42, 0x4F, 0x4F, 0x54, 0x4C, 0x4F, 0x41, 0x44,
  570. + 0x45, 0x52, 0x21, 0x00, 0x56, 0x30, 0x30, 0x36,
  571. + 0x4E, 0x46, 0x49, 0x49, 0x4E, 0x46, 0x4F, 0x00,
  572. + 0x00, 0x00, 0x00, 0x08, 0x05, 0x00, 0x40, 0x00,
  573. + 0x40, 0x00, 0x00, 0x10, 0x0B, 0x00, 0x11, 0x00,
  574. + 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  575. + 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  576. + 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  577. + 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  578. + 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  579. + 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  580. + 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  581. + 0x00, 0x00, 0x00, 0x00, 0xE3, 0x0F, 0x86, 0x32,
  582. + 0x68, 0x05, 0xD9, 0xC8, 0x13, 0xDF, 0xC5, 0x0B,
  583. + 0x35, 0x3A, 0x68, 0xA5, 0x3C, 0x0C, 0x73, 0x87,
  584. + 0x63, 0xB0, 0xBE, 0xCC, 0x84, 0x47, 0x00, 0x00
  585. + }
  586. +};
  587. +
  588. +/* NAND header for Parallel NAND 2Gb with 2KB page + 128B spare */
  589. +static const union nand_boot_header nand_hdr_2gb_2k_128_data = {
  590. + .data = {
  591. + 0x42, 0x4F, 0x4F, 0x54, 0x4C, 0x4F, 0x41, 0x44,
  592. + 0x45, 0x52, 0x21, 0x00, 0x56, 0x30, 0x30, 0x36,
  593. + 0x4E, 0x46, 0x49, 0x49, 0x4E, 0x46, 0x4F, 0x00,
  594. + 0x00, 0x00, 0x00, 0x08, 0x05, 0x00, 0x70, 0x00,
  595. + 0x40, 0x00, 0x00, 0x08, 0x0B, 0x00, 0x11, 0x00,
  596. + 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  597. + 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  598. + 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  599. + 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  600. + 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  601. + 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  602. + 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  603. + 0x00, 0x00, 0x00, 0x00, 0x01, 0xA5, 0xE9, 0x5A,
  604. + 0xDF, 0x58, 0x62, 0x41, 0xD6, 0x26, 0x77, 0xBC,
  605. + 0x76, 0x1F, 0x27, 0x4E, 0x4F, 0x6C, 0xC3, 0xF0,
  606. + 0x36, 0xDE, 0xD9, 0xB3, 0xFF, 0x93, 0x00, 0x00
  607. + }
  608. +};
  609. +
  610. +/* NAND header for Parallel NAND 4Gb with 2KB page + 128B spare */
  611. +static const union nand_boot_header nand_hdr_4gb_2k_128_data = {
  612. + .data = {
  613. + 0x42, 0x4F, 0x4F, 0x54, 0x4C, 0x4F, 0x41, 0x44,
  614. + 0x45, 0x52, 0x21, 0x00, 0x56, 0x30, 0x30, 0x36,
  615. + 0x4E, 0x46, 0x49, 0x49, 0x4E, 0x46, 0x4F, 0x00,
  616. + 0x00, 0x00, 0x00, 0x08, 0x05, 0x00, 0x70, 0x00,
  617. + 0x40, 0x00, 0x00, 0x10, 0x0B, 0x00, 0x11, 0x00,
  618. + 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  619. + 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  620. + 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  621. + 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  622. + 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  623. + 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  624. + 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  625. + 0x00, 0x00, 0x00, 0x00, 0xC2, 0x36, 0x52, 0x45,
  626. + 0xCC, 0x35, 0xD8, 0xDB, 0xEB, 0xFD, 0xD1, 0x46,
  627. + 0x76, 0x6B, 0x0B, 0xD5, 0x8B, 0xCC, 0x2B, 0xE2,
  628. + 0xFE, 0x90, 0x83, 0x9E, 0xAE, 0x2D, 0x00, 0x00
  629. + }
  630. +};
  631. +
  632. +static const struct nand_header_type {
  633. + const char *name;
  634. + const union nand_boot_header *data;
  635. +} nand_headers[] = {
  636. + {
  637. + .name = "2k+64",
  638. + .data = &snand_hdr_2k_64_data
  639. + }, {
  640. + .name = "2k+120",
  641. + .data = &snand_hdr_2k_128_data
  642. + }, {
  643. + .name = "2k+128",
  644. + .data = &snand_hdr_2k_128_data
  645. + }, {
  646. + .name = "4k+256",
  647. + .data = &snand_hdr_4k_256_data
  648. + }, {
  649. + .name = "1g:2k+64",
  650. + .data = &nand_hdr_1gb_2k_64_data
  651. + }, {
  652. + .name = "2g:2k+64",
  653. + .data = &nand_hdr_2gb_2k_64_data
  654. + }, {
  655. + .name = "4g:2k+64",
  656. + .data = &nand_hdr_4gb_2k_64_data
  657. + }, {
  658. + .name = "2g:2k+128",
  659. + .data = &nand_hdr_2gb_2k_128_data
  660. + }, {
  661. + .name = "4g:2k+128",
  662. + .data = &nand_hdr_4gb_2k_128_data
  663. + }
  664. +};
  665. +
  666. +const union nand_boot_header *mtk_nand_header_find(const char *name)
  667. +{
  668. + uint32_t i;
  669. +
  670. + for (i = 0; i < ARRAY_SIZE(nand_headers); i++) {
  671. + if (!strcmp(nand_headers[i].name, name))
  672. + return nand_headers[i].data;
  673. + }
  674. +
  675. + return NULL;
  676. +}
  677. +
  678. +uint32_t mtk_nand_header_size(const union nand_boot_header *hdr_nand)
  679. +{
  680. + return 2 * le16_to_cpu(hdr_nand->pagesize);
  681. +}
  682. +
  683. +static int mtk_nand_header_ap_info(const void *ptr,
  684. + struct nand_header_info *info)
  685. +{
  686. + union nand_boot_header *nh = (union nand_boot_header *)ptr;
  687. +
  688. + if (strncmp(nh->version, NAND_BOOT_VERSION, sizeof(nh->version)) ||
  689. + strcmp(nh->id, NAND_BOOT_ID))
  690. + return -1;
  691. +
  692. + info->page_size = le16_to_cpu(nh->pagesize);
  693. + info->spare_size = le16_to_cpu(nh->oobsize);
  694. + info->gfh_offset = 2 * info->page_size;
  695. +
  696. + return 0;
  697. +}
  698. +
  699. +int mtk_nand_header_info(const void *ptr, struct nand_header_info *info)
  700. +{
  701. + if (!strcmp((char *)ptr, NAND_BOOT_NAME))
  702. + return mtk_nand_header_ap_info(ptr, info);
  703. +
  704. + return -1;
  705. +}
  706. +
  707. +bool is_mtk_nand_header(const void *ptr)
  708. +{
  709. + struct nand_header_info info;
  710. +
  711. + if (mtk_nand_header_info(ptr, &info) >= 0)
  712. + return true;
  713. +
  714. + return false;
  715. +}
  716. +
  717. +uint32_t mtk_nand_header_put(const union nand_boot_header *hdr_nand, void *ptr)
  718. +{
  719. + union nand_boot_header *nh = (union nand_boot_header *)ptr;
  720. + int i;
  721. +
  722. + /* NAND device header, repeat 4 times */
  723. + for (i = 0; i < 4; i++)
  724. + memcpy(nh + i, hdr_nand, sizeof(union nand_boot_header));
  725. +
  726. + return le16_to_cpu(hdr_nand->pagesize);
  727. +}
  728. --- /dev/null
  729. +++ b/tools/mtk_nand_headers.h
  730. @@ -0,0 +1,61 @@
  731. +/* SPDX-License-Identifier: GPL-2.0+ */
  732. +/*
  733. + * MediaTek BootROM NAND header definitions
  734. + *
  735. + * Copyright (C) 2022 MediaTek Inc.
  736. + * Author: Weijie Gao <[email protected]>
  737. + */
  738. +
  739. +#ifndef _MTK_NAND_HEADERS_H
  740. +#define _MTK_NAND_HEADERS_H
  741. +
  742. +#include <stdint.h>
  743. +#include <stdbool.h>
  744. +
  745. +struct nand_header_info {
  746. + uint32_t page_size;
  747. + uint32_t spare_size;
  748. + uint32_t gfh_offset;
  749. +};
  750. +
  751. +/* AP BROM Header for NAND */
  752. +union nand_boot_header {
  753. + struct {
  754. + char name[12];
  755. + char version[4];
  756. + char id[8];
  757. + uint16_t ioif; /* I/O interface */
  758. + uint16_t pagesize; /* NAND page size */
  759. + uint16_t addrcycles; /* Address cycles */
  760. + uint16_t oobsize; /* NAND page spare size */
  761. + uint16_t pages_of_block; /* Pages of one block */
  762. + uint16_t numblocks; /* Total blocks of NAND chip */
  763. + uint16_t writesize_shift;
  764. + uint16_t erasesize_shift;
  765. + uint8_t dummy[60];
  766. + uint8_t ecc_parity[28]; /* ECC parity of this header */
  767. + };
  768. +
  769. + uint8_t data[0x80];
  770. +};
  771. +
  772. +#define NAND_BOOT_NAME "BOOTLOADER!"
  773. +#define NAND_BOOT_VERSION "V006"
  774. +#define NAND_BOOT_ID "NFIINFO"
  775. +
  776. +/* Find nand header data by name */
  777. +const union nand_boot_header *mtk_nand_header_find(const char *name);
  778. +
  779. +/* Device header size using this nand header */
  780. +uint32_t mtk_nand_header_size(const union nand_boot_header *hdr_nand);
  781. +
  782. +/* Get nand info from nand header (page size, spare size, ...) */
  783. +int mtk_nand_header_info(const void *ptr, struct nand_header_info *info);
  784. +
  785. +/* Whether given header data is valid */
  786. +bool is_mtk_nand_header(const void *ptr);
  787. +
  788. +/* Generate Device header using give nand header */
  789. +uint32_t mtk_nand_header_put(const union nand_boot_header *hdr_nand, void *ptr);
  790. +
  791. +#endif /* _MTK_NAND_HEADERS_H */