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. @@ -10,216 +10,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. @@ -258,6 +49,7 @@ static char lk_name[32] = "U-Boot";
  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. @@ -361,12 +153,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. @@ -391,6 +178,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. @@ -420,7 +210,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. @@ -518,16 +308,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. @@ -538,34 +329,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. @@ -581,7 +361,7 @@ static int mtk_image_verify_header(unsig
  340. img_size = image_size;
  341. - if (!strcmp((char *)ptr, NAND_BOOT_NAME))
  342. + if (is_mtk_nand_header(ptr))
  343. return mtk_image_verify_nand_header(ptr, 0);
  344. else
  345. return mtk_image_verify_gen_header(ptr, 0);
  346. @@ -601,7 +381,7 @@ static void mtk_image_print_header(const
  347. printf("Image Type: MediaTek BootROM Loadable Image\n");
  348. - if (!strcmp((char *)ptr, NAND_BOOT_NAME))
  349. + if (is_mtk_nand_header(ptr))
  350. mtk_image_verify_nand_header(ptr, 1);
  351. else
  352. mtk_image_verify_gen_header(ptr, 1);
  353. @@ -732,36 +512,33 @@ static void mtk_image_set_gen_header(voi
  354. static void mtk_image_set_nand_header(void *ptr, off_t filesize,
  355. uint32_t loadaddr)
  356. {
  357. - union nand_boot_header *nh = (union nand_boot_header *)ptr;
  358. struct brom_layout_header *brlyt;
  359. struct gfh_header *gfh;
  360. - uint32_t payload_pages;
  361. - int i;
  362. + uint32_t payload_pages, nand_page_size;
  363. - /* NAND device header, repeat 4 times */
  364. - for (i = 0; i < 4; i++)
  365. - memcpy(nh + i, hdr_nand, sizeof(union nand_boot_header));
  366. + /* NAND header */
  367. + nand_page_size = mtk_nand_header_put(hdr_nand, ptr);
  368. - /* BRLYT header */
  369. - payload_pages = (filesize + le16_to_cpu(hdr_nand->pagesize) - 1) /
  370. - le16_to_cpu(hdr_nand->pagesize);
  371. - brlyt = (struct brom_layout_header *)
  372. - (ptr + le16_to_cpu(hdr_nand->pagesize));
  373. - put_brom_layout_header(brlyt, hdr_media);
  374. - brlyt->header_size = cpu_to_le32(2);
  375. - brlyt->total_size = cpu_to_le32(payload_pages);
  376. - brlyt->header_size_2 = brlyt->header_size;
  377. - brlyt->total_size_2 = brlyt->total_size;
  378. - brlyt->unused = cpu_to_le32(1);
  379. + if (nand_page_size) {
  380. + /* BRLYT header */
  381. + payload_pages = (filesize + nand_page_size - 1) /
  382. + nand_page_size;
  383. + brlyt = (struct brom_layout_header *)(ptr + nand_page_size);
  384. + put_brom_layout_header(brlyt, hdr_media);
  385. + brlyt->header_size = cpu_to_le32(2);
  386. + brlyt->total_size = cpu_to_le32(payload_pages);
  387. + brlyt->header_size_2 = brlyt->header_size;
  388. + brlyt->total_size_2 = brlyt->total_size;
  389. + brlyt->unused = cpu_to_le32(1);
  390. + }
  391. /* GFH header */
  392. - gfh = (struct gfh_header *)(ptr + 2 * le16_to_cpu(hdr_nand->pagesize));
  393. - put_ghf_header(gfh, filesize, 2 * le16_to_cpu(hdr_nand->pagesize),
  394. - loadaddr, GFH_FLASH_TYPE_NAND);
  395. + gfh = (struct gfh_header *)(ptr + hdr_nand_size);
  396. + put_ghf_header(gfh, filesize, hdr_nand_size, loadaddr,
  397. + GFH_FLASH_TYPE_NAND);
  398. /* Generate SHA256 hash */
  399. - put_hash((uint8_t *)gfh,
  400. - filesize - 2 * le16_to_cpu(hdr_nand->pagesize) - SHA256_SUM_LEN);
  401. + put_hash((uint8_t *)gfh, filesize - hdr_nand_size - SHA256_SUM_LEN);
  402. }
  403. static void mtk_image_set_header(void *ptr, struct stat *sbuf, int ifd,
  404. --- a/tools/mtk_image.h
  405. +++ b/tools/mtk_image.h
  406. @@ -26,31 +26,6 @@ union gen_boot_header {
  407. #define SF_BOOT_NAME "SF_BOOT"
  408. #define SDMMC_BOOT_NAME "SDMMC_BOOT"
  409. -/* Header for NAND */
  410. -union nand_boot_header {
  411. - struct {
  412. - char name[12];
  413. - char version[4];
  414. - char id[8];
  415. - uint16_t ioif;
  416. - uint16_t pagesize;
  417. - uint16_t addrcycles;
  418. - uint16_t oobsize;
  419. - uint16_t pages_of_block;
  420. - uint16_t numblocks;
  421. - uint16_t writesize_shift;
  422. - uint16_t erasesize_shift;
  423. - uint8_t dummy[60];
  424. - uint8_t ecc_parity[28];
  425. - };
  426. -
  427. - uint8_t data[0x80];
  428. -};
  429. -
  430. -#define NAND_BOOT_NAME "BOOTLOADER!"
  431. -#define NAND_BOOT_VERSION "V006"
  432. -#define NAND_BOOT_ID "NFIINFO"
  433. -
  434. /* BootROM layout header */
  435. struct brom_layout_header {
  436. char name[8];
  437. --- /dev/null
  438. +++ b/tools/mtk_nand_headers.c
  439. @@ -0,0 +1,286 @@
  440. +// SPDX-License-Identifier: GPL-2.0+
  441. +/*
  442. + * MediaTek BootROM NAND header definitions
  443. + *
  444. + * Copyright (C) 2022 MediaTek Inc.
  445. + * Author: Weijie Gao <[email protected]>
  446. + */
  447. +
  448. +#include <stdint.h>
  449. +#include <string.h>
  450. +#include "imagetool.h"
  451. +#include "mtk_image.h"
  452. +#include "mtk_nand_headers.h"
  453. +
  454. +/* NAND header for SPI-NAND with 2KB page + 64B spare */
  455. +static const union nand_boot_header snand_hdr_2k_64_data = {
  456. + .data = {
  457. + 0x42, 0x4F, 0x4F, 0x54, 0x4C, 0x4F, 0x41, 0x44,
  458. + 0x45, 0x52, 0x21, 0x00, 0x56, 0x30, 0x30, 0x36,
  459. + 0x4E, 0x46, 0x49, 0x49, 0x4E, 0x46, 0x4F, 0x00,
  460. + 0x00, 0x00, 0x00, 0x08, 0x03, 0x00, 0x40, 0x00,
  461. + 0x40, 0x00, 0x00, 0x08, 0x10, 0x00, 0x16, 0x00,
  462. + 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  463. + 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 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, 0x7B, 0xC4, 0x17, 0x9D,
  470. + 0xCA, 0x42, 0x90, 0xD0, 0x98, 0xD0, 0xE0, 0xF7,
  471. + 0xDB, 0xCD, 0x16, 0xF6, 0x03, 0x73, 0xD2, 0xB8,
  472. + 0x93, 0xB2, 0x56, 0x5A, 0x84, 0x6E, 0x00, 0x00
  473. + }
  474. +};
  475. +
  476. +/* NAND header for SPI-NAND with 2KB page + 120B/128B spare */
  477. +static const union nand_boot_header snand_hdr_2k_128_data = {
  478. + .data = {
  479. + 0x42, 0x4F, 0x4F, 0x54, 0x4C, 0x4F, 0x41, 0x44,
  480. + 0x45, 0x52, 0x21, 0x00, 0x56, 0x30, 0x30, 0x36,
  481. + 0x4E, 0x46, 0x49, 0x49, 0x4E, 0x46, 0x4F, 0x00,
  482. + 0x00, 0x00, 0x00, 0x08, 0x05, 0x00, 0x70, 0x00,
  483. + 0x40, 0x00, 0x00, 0x08, 0x10, 0x00, 0x16, 0x00,
  484. + 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  485. + 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 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, 0x90, 0x28, 0xED, 0x13,
  492. + 0x7F, 0x12, 0x22, 0xCD, 0x3D, 0x06, 0xF1, 0xB3,
  493. + 0x6F, 0x2E, 0xD9, 0xA0, 0x9D, 0x7A, 0xBD, 0xD7,
  494. + 0xB3, 0x28, 0x3C, 0x13, 0xDB, 0x4E, 0x00, 0x00
  495. + }
  496. +};
  497. +
  498. +/* NAND header for SPI-NAND with 4KB page + 256B spare */
  499. +static const union nand_boot_header snand_hdr_4k_256_data = {
  500. + .data = {
  501. + 0x42, 0x4F, 0x4F, 0x54, 0x4C, 0x4F, 0x41, 0x44,
  502. + 0x45, 0x52, 0x21, 0x00, 0x56, 0x30, 0x30, 0x36,
  503. + 0x4E, 0x46, 0x49, 0x49, 0x4E, 0x46, 0x4F, 0x00,
  504. + 0x00, 0x00, 0x00, 0x10, 0x05, 0x00, 0xE0, 0x00,
  505. + 0x40, 0x00, 0x00, 0x08, 0x10, 0x00, 0x16, 0x00,
  506. + 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  507. + 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 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, 0x47, 0xED, 0x0E, 0xC3,
  514. + 0x83, 0xBF, 0x41, 0xD2, 0x85, 0x21, 0x97, 0x57,
  515. + 0xC4, 0x2E, 0x6B, 0x7A, 0x40, 0xE0, 0xCF, 0x8F,
  516. + 0x37, 0xBD, 0x17, 0xB6, 0xC7, 0xFE, 0x00, 0x00
  517. + }
  518. +};
  519. +
  520. +/* NAND header for Parallel NAND 1Gb with 2KB page + 64B spare */
  521. +static const union nand_boot_header nand_hdr_1gb_2k_64_data = {
  522. + .data = {
  523. + 0x42, 0x4F, 0x4F, 0x54, 0x4C, 0x4F, 0x41, 0x44,
  524. + 0x45, 0x52, 0x21, 0x00, 0x56, 0x30, 0x30, 0x36,
  525. + 0x4E, 0x46, 0x49, 0x49, 0x4E, 0x46, 0x4F, 0x00,
  526. + 0x00, 0x00, 0x00, 0x08, 0x05, 0x00, 0x40, 0x00,
  527. + 0x40, 0x00, 0x00, 0x04, 0x0B, 0x00, 0x11, 0x00,
  528. + 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  529. + 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 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, 0x12, 0x28, 0x1C, 0x12,
  536. + 0x8F, 0xFD, 0xF8, 0x32, 0x6F, 0x6D, 0xCF, 0x6C,
  537. + 0xDA, 0x21, 0x70, 0x8C, 0xDA, 0x0A, 0x22, 0x82,
  538. + 0xAA, 0x59, 0xFA, 0x7C, 0x42, 0x2D, 0x00, 0x00
  539. + }
  540. +};
  541. +
  542. +/* NAND header for Parallel NAND 2Gb with 2KB page + 64B spare */
  543. +static const union nand_boot_header nand_hdr_2gb_2k_64_data = {
  544. + .data = {
  545. + 0x42, 0x4F, 0x4F, 0x54, 0x4C, 0x4F, 0x41, 0x44,
  546. + 0x45, 0x52, 0x21, 0x00, 0x56, 0x30, 0x30, 0x36,
  547. + 0x4E, 0x46, 0x49, 0x49, 0x4E, 0x46, 0x4F, 0x00,
  548. + 0x00, 0x00, 0x00, 0x08, 0x05, 0x00, 0x40, 0x00,
  549. + 0x40, 0x00, 0x00, 0x08, 0x0B, 0x00, 0x11, 0x00,
  550. + 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  551. + 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 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, 0x20, 0x9C, 0x3D, 0x2D,
  558. + 0x7B, 0x68, 0x63, 0x52, 0x2E, 0x04, 0x63, 0xF1,
  559. + 0x35, 0x4E, 0x44, 0x3E, 0xF8, 0xAC, 0x9B, 0x95,
  560. + 0xAB, 0xFE, 0xE4, 0xE1, 0xD5, 0xF9, 0x00, 0x00
  561. + }
  562. +};
  563. +
  564. +/* NAND header for Parallel NAND 4Gb with 2KB page + 64B spare */
  565. +static const union nand_boot_header nand_hdr_4gb_2k_64_data = {
  566. + .data = {
  567. + 0x42, 0x4F, 0x4F, 0x54, 0x4C, 0x4F, 0x41, 0x44,
  568. + 0x45, 0x52, 0x21, 0x00, 0x56, 0x30, 0x30, 0x36,
  569. + 0x4E, 0x46, 0x49, 0x49, 0x4E, 0x46, 0x4F, 0x00,
  570. + 0x00, 0x00, 0x00, 0x08, 0x05, 0x00, 0x40, 0x00,
  571. + 0x40, 0x00, 0x00, 0x10, 0x0B, 0x00, 0x11, 0x00,
  572. + 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  573. + 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 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, 0xE3, 0x0F, 0x86, 0x32,
  580. + 0x68, 0x05, 0xD9, 0xC8, 0x13, 0xDF, 0xC5, 0x0B,
  581. + 0x35, 0x3A, 0x68, 0xA5, 0x3C, 0x0C, 0x73, 0x87,
  582. + 0x63, 0xB0, 0xBE, 0xCC, 0x84, 0x47, 0x00, 0x00
  583. + }
  584. +};
  585. +
  586. +/* NAND header for Parallel NAND 2Gb with 2KB page + 128B spare */
  587. +static const union nand_boot_header nand_hdr_2gb_2k_128_data = {
  588. + .data = {
  589. + 0x42, 0x4F, 0x4F, 0x54, 0x4C, 0x4F, 0x41, 0x44,
  590. + 0x45, 0x52, 0x21, 0x00, 0x56, 0x30, 0x30, 0x36,
  591. + 0x4E, 0x46, 0x49, 0x49, 0x4E, 0x46, 0x4F, 0x00,
  592. + 0x00, 0x00, 0x00, 0x08, 0x05, 0x00, 0x70, 0x00,
  593. + 0x40, 0x00, 0x00, 0x08, 0x0B, 0x00, 0x11, 0x00,
  594. + 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  595. + 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 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, 0x01, 0xA5, 0xE9, 0x5A,
  602. + 0xDF, 0x58, 0x62, 0x41, 0xD6, 0x26, 0x77, 0xBC,
  603. + 0x76, 0x1F, 0x27, 0x4E, 0x4F, 0x6C, 0xC3, 0xF0,
  604. + 0x36, 0xDE, 0xD9, 0xB3, 0xFF, 0x93, 0x00, 0x00
  605. + }
  606. +};
  607. +
  608. +/* NAND header for Parallel NAND 4Gb with 2KB page + 128B spare */
  609. +static const union nand_boot_header nand_hdr_4gb_2k_128_data = {
  610. + .data = {
  611. + 0x42, 0x4F, 0x4F, 0x54, 0x4C, 0x4F, 0x41, 0x44,
  612. + 0x45, 0x52, 0x21, 0x00, 0x56, 0x30, 0x30, 0x36,
  613. + 0x4E, 0x46, 0x49, 0x49, 0x4E, 0x46, 0x4F, 0x00,
  614. + 0x00, 0x00, 0x00, 0x08, 0x05, 0x00, 0x70, 0x00,
  615. + 0x40, 0x00, 0x00, 0x10, 0x0B, 0x00, 0x11, 0x00,
  616. + 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  617. + 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 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, 0xC2, 0x36, 0x52, 0x45,
  624. + 0xCC, 0x35, 0xD8, 0xDB, 0xEB, 0xFD, 0xD1, 0x46,
  625. + 0x76, 0x6B, 0x0B, 0xD5, 0x8B, 0xCC, 0x2B, 0xE2,
  626. + 0xFE, 0x90, 0x83, 0x9E, 0xAE, 0x2D, 0x00, 0x00
  627. + }
  628. +};
  629. +
  630. +static const struct nand_header_type {
  631. + const char *name;
  632. + const union nand_boot_header *data;
  633. +} nand_headers[] = {
  634. + {
  635. + .name = "2k+64",
  636. + .data = &snand_hdr_2k_64_data
  637. + }, {
  638. + .name = "2k+120",
  639. + .data = &snand_hdr_2k_128_data
  640. + }, {
  641. + .name = "2k+128",
  642. + .data = &snand_hdr_2k_128_data
  643. + }, {
  644. + .name = "4k+256",
  645. + .data = &snand_hdr_4k_256_data
  646. + }, {
  647. + .name = "1g:2k+64",
  648. + .data = &nand_hdr_1gb_2k_64_data
  649. + }, {
  650. + .name = "2g:2k+64",
  651. + .data = &nand_hdr_2gb_2k_64_data
  652. + }, {
  653. + .name = "4g:2k+64",
  654. + .data = &nand_hdr_4gb_2k_64_data
  655. + }, {
  656. + .name = "2g:2k+128",
  657. + .data = &nand_hdr_2gb_2k_128_data
  658. + }, {
  659. + .name = "4g:2k+128",
  660. + .data = &nand_hdr_4gb_2k_128_data
  661. + }
  662. +};
  663. +
  664. +const union nand_boot_header *mtk_nand_header_find(const char *name)
  665. +{
  666. + uint32_t i;
  667. +
  668. + for (i = 0; i < ARRAY_SIZE(nand_headers); i++) {
  669. + if (!strcmp(nand_headers[i].name, name))
  670. + return nand_headers[i].data;
  671. + }
  672. +
  673. + return NULL;
  674. +}
  675. +
  676. +uint32_t mtk_nand_header_size(const union nand_boot_header *hdr_nand)
  677. +{
  678. + return 2 * le16_to_cpu(hdr_nand->pagesize);
  679. +}
  680. +
  681. +static int mtk_nand_header_ap_info(const void *ptr,
  682. + struct nand_header_info *info)
  683. +{
  684. + union nand_boot_header *nh = (union nand_boot_header *)ptr;
  685. +
  686. + if (strncmp(nh->version, NAND_BOOT_VERSION, sizeof(nh->version)) ||
  687. + strcmp(nh->id, NAND_BOOT_ID))
  688. + return -1;
  689. +
  690. + info->page_size = le16_to_cpu(nh->pagesize);
  691. + info->spare_size = le16_to_cpu(nh->oobsize);
  692. + info->gfh_offset = 2 * info->page_size;
  693. +
  694. + return 0;
  695. +}
  696. +
  697. +int mtk_nand_header_info(const void *ptr, struct nand_header_info *info)
  698. +{
  699. + if (!strcmp((char *)ptr, NAND_BOOT_NAME))
  700. + return mtk_nand_header_ap_info(ptr, info);
  701. +
  702. + return -1;
  703. +}
  704. +
  705. +bool is_mtk_nand_header(const void *ptr)
  706. +{
  707. + struct nand_header_info info;
  708. +
  709. + if (mtk_nand_header_info(ptr, &info) >= 0)
  710. + return true;
  711. +
  712. + return false;
  713. +}
  714. +
  715. +uint32_t mtk_nand_header_put(const union nand_boot_header *hdr_nand, void *ptr)
  716. +{
  717. + union nand_boot_header *nh = (union nand_boot_header *)ptr;
  718. + int i;
  719. +
  720. + /* NAND device header, repeat 4 times */
  721. + for (i = 0; i < 4; i++)
  722. + memcpy(nh + i, hdr_nand, sizeof(union nand_boot_header));
  723. +
  724. + return le16_to_cpu(hdr_nand->pagesize);
  725. +}
  726. --- /dev/null
  727. +++ b/tools/mtk_nand_headers.h
  728. @@ -0,0 +1,61 @@
  729. +/* SPDX-License-Identifier: GPL-2.0+ */
  730. +/*
  731. + * MediaTek BootROM NAND header definitions
  732. + *
  733. + * Copyright (C) 2022 MediaTek Inc.
  734. + * Author: Weijie Gao <[email protected]>
  735. + */
  736. +
  737. +#ifndef _MTK_NAND_HEADERS_H
  738. +#define _MTK_NAND_HEADERS_H
  739. +
  740. +#include <stdint.h>
  741. +#include <stdbool.h>
  742. +
  743. +struct nand_header_info {
  744. + uint32_t page_size;
  745. + uint32_t spare_size;
  746. + uint32_t gfh_offset;
  747. +};
  748. +
  749. +/* AP BROM Header for NAND */
  750. +union nand_boot_header {
  751. + struct {
  752. + char name[12];
  753. + char version[4];
  754. + char id[8];
  755. + uint16_t ioif; /* I/O interface */
  756. + uint16_t pagesize; /* NAND page size */
  757. + uint16_t addrcycles; /* Address cycles */
  758. + uint16_t oobsize; /* NAND page spare size */
  759. + uint16_t pages_of_block; /* Pages of one block */
  760. + uint16_t numblocks; /* Total blocks of NAND chip */
  761. + uint16_t writesize_shift;
  762. + uint16_t erasesize_shift;
  763. + uint8_t dummy[60];
  764. + uint8_t ecc_parity[28]; /* ECC parity of this header */
  765. + };
  766. +
  767. + uint8_t data[0x80];
  768. +};
  769. +
  770. +#define NAND_BOOT_NAME "BOOTLOADER!"
  771. +#define NAND_BOOT_VERSION "V006"
  772. +#define NAND_BOOT_ID "NFIINFO"
  773. +
  774. +/* Find nand header data by name */
  775. +const union nand_boot_header *mtk_nand_header_find(const char *name);
  776. +
  777. +/* Device header size using this nand header */
  778. +uint32_t mtk_nand_header_size(const union nand_boot_header *hdr_nand);
  779. +
  780. +/* Get nand info from nand header (page size, spare size, ...) */
  781. +int mtk_nand_header_info(const void *ptr, struct nand_header_info *info);
  782. +
  783. +/* Whether given header data is valid */
  784. +bool is_mtk_nand_header(const void *ptr);
  785. +
  786. +/* Generate Device header using give nand header */
  787. +uint32_t mtk_nand_header_put(const union nand_boot_header *hdr_nand, void *ptr);
  788. +
  789. +#endif /* _MTK_NAND_HEADERS_H */