mtk_nand_headers.c 23 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668
  1. // SPDX-License-Identifier: GPL-2.0+
  2. /*
  3. * MediaTek BootROM NAND header definitions
  4. *
  5. * Copyright (C) 2022 MediaTek Inc.
  6. * Author: Weijie Gao <weijie.gao@mediatek.com>
  7. */
  8. #include <stdint.h>
  9. #include <string.h>
  10. #include "imagetool.h"
  11. #include "mtk_image.h"
  12. #include "mtk_nand_headers.h"
  13. /* NAND header for SPI-NAND with 2KB page + 64B spare */
  14. static const union nand_boot_header snand_hdr_2k_64_data = {
  15. .data = {
  16. 0x42, 0x4F, 0x4F, 0x54, 0x4C, 0x4F, 0x41, 0x44,
  17. 0x45, 0x52, 0x21, 0x00, 0x56, 0x30, 0x30, 0x36,
  18. 0x4E, 0x46, 0x49, 0x49, 0x4E, 0x46, 0x4F, 0x00,
  19. 0x00, 0x00, 0x00, 0x08, 0x03, 0x00, 0x40, 0x00,
  20. 0x40, 0x00, 0x00, 0x08, 0x10, 0x00, 0x16, 0x00,
  21. 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  22. 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  23. 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  24. 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  25. 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  26. 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  27. 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  28. 0x00, 0x00, 0x00, 0x00, 0x7B, 0xC4, 0x17, 0x9D,
  29. 0xCA, 0x42, 0x90, 0xD0, 0x98, 0xD0, 0xE0, 0xF7,
  30. 0xDB, 0xCD, 0x16, 0xF6, 0x03, 0x73, 0xD2, 0xB8,
  31. 0x93, 0xB2, 0x56, 0x5A, 0x84, 0x6E, 0x00, 0x00
  32. }
  33. };
  34. /* NAND header for SPI-NAND with 2KB page + 120B/128B spare */
  35. static const union nand_boot_header snand_hdr_2k_128_data = {
  36. .data = {
  37. 0x42, 0x4F, 0x4F, 0x54, 0x4C, 0x4F, 0x41, 0x44,
  38. 0x45, 0x52, 0x21, 0x00, 0x56, 0x30, 0x30, 0x36,
  39. 0x4E, 0x46, 0x49, 0x49, 0x4E, 0x46, 0x4F, 0x00,
  40. 0x00, 0x00, 0x00, 0x08, 0x05, 0x00, 0x70, 0x00,
  41. 0x40, 0x00, 0x00, 0x08, 0x10, 0x00, 0x16, 0x00,
  42. 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  43. 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  44. 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  45. 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 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, 0x90, 0x28, 0xED, 0x13,
  50. 0x7F, 0x12, 0x22, 0xCD, 0x3D, 0x06, 0xF1, 0xB3,
  51. 0x6F, 0x2E, 0xD9, 0xA0, 0x9D, 0x7A, 0xBD, 0xD7,
  52. 0xB3, 0x28, 0x3C, 0x13, 0xDB, 0x4E, 0x00, 0x00
  53. }
  54. };
  55. /* NAND header for SPI-NAND with 4KB page + 256B spare */
  56. static const union nand_boot_header snand_hdr_4k_256_data = {
  57. .data = {
  58. 0x42, 0x4F, 0x4F, 0x54, 0x4C, 0x4F, 0x41, 0x44,
  59. 0x45, 0x52, 0x21, 0x00, 0x56, 0x30, 0x30, 0x36,
  60. 0x4E, 0x46, 0x49, 0x49, 0x4E, 0x46, 0x4F, 0x00,
  61. 0x00, 0x00, 0x00, 0x10, 0x05, 0x00, 0xE0, 0x00,
  62. 0x40, 0x00, 0x00, 0x08, 0x10, 0x00, 0x16, 0x00,
  63. 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  64. 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  65. 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  66. 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  67. 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  68. 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  69. 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  70. 0x00, 0x00, 0x00, 0x00, 0x47, 0xED, 0x0E, 0xC3,
  71. 0x83, 0xBF, 0x41, 0xD2, 0x85, 0x21, 0x97, 0x57,
  72. 0xC4, 0x2E, 0x6B, 0x7A, 0x40, 0xE0, 0xCF, 0x8F,
  73. 0x37, 0xBD, 0x17, 0xB6, 0xC7, 0xFE, 0x00, 0x00
  74. }
  75. };
  76. /* NAND header for Parallel NAND 1Gb with 2KB page + 64B spare */
  77. static const union nand_boot_header nand_hdr_1gb_2k_64_data = {
  78. .data = {
  79. 0x42, 0x4F, 0x4F, 0x54, 0x4C, 0x4F, 0x41, 0x44,
  80. 0x45, 0x52, 0x21, 0x00, 0x56, 0x30, 0x30, 0x36,
  81. 0x4E, 0x46, 0x49, 0x49, 0x4E, 0x46, 0x4F, 0x00,
  82. 0x00, 0x00, 0x00, 0x08, 0x05, 0x00, 0x40, 0x00,
  83. 0x40, 0x00, 0x00, 0x04, 0x0B, 0x00, 0x11, 0x00,
  84. 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  85. 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  86. 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  87. 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  88. 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  89. 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  90. 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  91. 0x00, 0x00, 0x00, 0x00, 0x12, 0x28, 0x1C, 0x12,
  92. 0x8F, 0xFD, 0xF8, 0x32, 0x6F, 0x6D, 0xCF, 0x6C,
  93. 0xDA, 0x21, 0x70, 0x8C, 0xDA, 0x0A, 0x22, 0x82,
  94. 0xAA, 0x59, 0xFA, 0x7C, 0x42, 0x2D, 0x00, 0x00
  95. }
  96. };
  97. /* NAND header for Parallel NAND 2Gb with 2KB page + 64B spare */
  98. static const union nand_boot_header nand_hdr_2gb_2k_64_data = {
  99. .data = {
  100. 0x42, 0x4F, 0x4F, 0x54, 0x4C, 0x4F, 0x41, 0x44,
  101. 0x45, 0x52, 0x21, 0x00, 0x56, 0x30, 0x30, 0x36,
  102. 0x4E, 0x46, 0x49, 0x49, 0x4E, 0x46, 0x4F, 0x00,
  103. 0x00, 0x00, 0x00, 0x08, 0x05, 0x00, 0x40, 0x00,
  104. 0x40, 0x00, 0x00, 0x08, 0x0B, 0x00, 0x11, 0x00,
  105. 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  106. 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  107. 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  108. 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  109. 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  110. 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  111. 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  112. 0x00, 0x00, 0x00, 0x00, 0x20, 0x9C, 0x3D, 0x2D,
  113. 0x7B, 0x68, 0x63, 0x52, 0x2E, 0x04, 0x63, 0xF1,
  114. 0x35, 0x4E, 0x44, 0x3E, 0xF8, 0xAC, 0x9B, 0x95,
  115. 0xAB, 0xFE, 0xE4, 0xE1, 0xD5, 0xF9, 0x00, 0x00
  116. }
  117. };
  118. /* NAND header for Parallel NAND 4Gb with 2KB page + 64B spare */
  119. static const union nand_boot_header nand_hdr_4gb_2k_64_data = {
  120. .data = {
  121. 0x42, 0x4F, 0x4F, 0x54, 0x4C, 0x4F, 0x41, 0x44,
  122. 0x45, 0x52, 0x21, 0x00, 0x56, 0x30, 0x30, 0x36,
  123. 0x4E, 0x46, 0x49, 0x49, 0x4E, 0x46, 0x4F, 0x00,
  124. 0x00, 0x00, 0x00, 0x08, 0x05, 0x00, 0x40, 0x00,
  125. 0x40, 0x00, 0x00, 0x10, 0x0B, 0x00, 0x11, 0x00,
  126. 0x00, 0x00, 0x00, 0x00, 0x00, 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. 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  131. 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  132. 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  133. 0x00, 0x00, 0x00, 0x00, 0xE3, 0x0F, 0x86, 0x32,
  134. 0x68, 0x05, 0xD9, 0xC8, 0x13, 0xDF, 0xC5, 0x0B,
  135. 0x35, 0x3A, 0x68, 0xA5, 0x3C, 0x0C, 0x73, 0x87,
  136. 0x63, 0xB0, 0xBE, 0xCC, 0x84, 0x47, 0x00, 0x00
  137. }
  138. };
  139. /* NAND header for Parallel NAND 2Gb with 2KB page + 128B spare */
  140. static const union nand_boot_header nand_hdr_2gb_2k_128_data = {
  141. .data = {
  142. 0x42, 0x4F, 0x4F, 0x54, 0x4C, 0x4F, 0x41, 0x44,
  143. 0x45, 0x52, 0x21, 0x00, 0x56, 0x30, 0x30, 0x36,
  144. 0x4E, 0x46, 0x49, 0x49, 0x4E, 0x46, 0x4F, 0x00,
  145. 0x00, 0x00, 0x00, 0x08, 0x05, 0x00, 0x70, 0x00,
  146. 0x40, 0x00, 0x00, 0x08, 0x0B, 0x00, 0x11, 0x00,
  147. 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  148. 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  149. 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  150. 0x00, 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, 0x01, 0xA5, 0xE9, 0x5A,
  155. 0xDF, 0x58, 0x62, 0x41, 0xD6, 0x26, 0x77, 0xBC,
  156. 0x76, 0x1F, 0x27, 0x4E, 0x4F, 0x6C, 0xC3, 0xF0,
  157. 0x36, 0xDE, 0xD9, 0xB3, 0xFF, 0x93, 0x00, 0x00
  158. }
  159. };
  160. /* NAND header for Parallel NAND 4Gb with 2KB page + 128B spare */
  161. static const union nand_boot_header nand_hdr_4gb_2k_128_data = {
  162. .data = {
  163. 0x42, 0x4F, 0x4F, 0x54, 0x4C, 0x4F, 0x41, 0x44,
  164. 0x45, 0x52, 0x21, 0x00, 0x56, 0x30, 0x30, 0x36,
  165. 0x4E, 0x46, 0x49, 0x49, 0x4E, 0x46, 0x4F, 0x00,
  166. 0x00, 0x00, 0x00, 0x08, 0x05, 0x00, 0x70, 0x00,
  167. 0x40, 0x00, 0x00, 0x10, 0x0B, 0x00, 0x11, 0x00,
  168. 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  169. 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  170. 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  171. 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  172. 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  173. 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  174. 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  175. 0x00, 0x00, 0x00, 0x00, 0xC2, 0x36, 0x52, 0x45,
  176. 0xCC, 0x35, 0xD8, 0xDB, 0xEB, 0xFD, 0xD1, 0x46,
  177. 0x76, 0x6B, 0x0B, 0xD5, 0x8B, 0xCC, 0x2B, 0xE2,
  178. 0xFE, 0x90, 0x83, 0x9E, 0xAE, 0x2D, 0x00, 0x00
  179. }
  180. };
  181. /* HSM BROM NAND header for SPI NAND with 2KB page + 64B spare */
  182. static const union hsm_nand_boot_header hsm_nand_hdr_2k_64_data = {
  183. .data = {
  184. 0x4E, 0x41, 0x4E, 0x44, 0x43, 0x46, 0x47, 0x21,
  185. 0x01, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  186. 0x00, 0x04, 0x00, 0x00, 0x08, 0x00, 0x00, 0x00,
  187. 0x01, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  188. 0x00, 0x08, 0x00, 0x00, 0x40, 0x00, 0x00, 0x00,
  189. 0x40, 0x00, 0x00, 0x00, 0x00, 0x08, 0x00, 0x00,
  190. 0x0C, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  191. 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  192. 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  193. 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  194. 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  195. 0x00, 0x00, 0x00, 0x00, 0x01, 0x00, 0x00, 0x00,
  196. 0xFF, 0x00, 0x00, 0x00, 0x21, 0xD2, 0xEE, 0xF6,
  197. 0xAE, 0xDD, 0x5E, 0xC2, 0x82, 0x8E, 0x9A, 0x62,
  198. 0x09, 0x8E, 0x80, 0xE2, 0x37, 0x0D, 0xC9, 0xFA,
  199. 0xA9, 0xDD, 0xFC, 0x92, 0x34, 0x2A, 0xED, 0x51,
  200. 0xA4, 0x1B, 0xF7, 0x63, 0xCC, 0x5A, 0xC7, 0xFB,
  201. 0xED, 0x21, 0x02, 0x23, 0x51, 0x31
  202. }
  203. };
  204. /* HSM BROM NAND header for SPI NAND with 2KB page + 128B spare */
  205. static const union hsm_nand_boot_header hsm_nand_hdr_2k_128_data = {
  206. .data = {
  207. 0x4E, 0x41, 0x4E, 0x44, 0x43, 0x46, 0x47, 0x21,
  208. 0x01, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  209. 0x00, 0x04, 0x00, 0x00, 0x08, 0x00, 0x00, 0x00,
  210. 0x01, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  211. 0x00, 0x08, 0x00, 0x00, 0x80, 0x00, 0x00, 0x00,
  212. 0x40, 0x00, 0x00, 0x00, 0x00, 0x04, 0x00, 0x00,
  213. 0x0C, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  214. 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  215. 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  216. 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  217. 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  218. 0x00, 0x00, 0x00, 0x00, 0x01, 0x00, 0x00, 0x00,
  219. 0xFF, 0x00, 0x00, 0x00, 0x71, 0x7f, 0x71, 0xAC,
  220. 0x42, 0xD0, 0x5B, 0xD2, 0x12, 0x81, 0x15, 0x0A,
  221. 0x0C, 0xD4, 0xF6, 0x32, 0x1E, 0x63, 0xE7, 0x81,
  222. 0x8A, 0x7F, 0xDE, 0xF9, 0x4B, 0x91, 0xEC, 0xC2,
  223. 0x70, 0x00, 0x7F, 0x57, 0xAF, 0xDC, 0xE4, 0x24,
  224. 0x57, 0x09, 0xBC, 0xC5, 0x35, 0xDC
  225. }
  226. };
  227. /* HSM BROM NAND header for SPI NAND with 4KB page + 256B spare */
  228. static const union hsm_nand_boot_header hsm_nand_hdr_4k_256_data = {
  229. .data = {
  230. 0x4E, 0x41, 0x4E, 0x44, 0x43, 0x46, 0x47, 0x21,
  231. 0x01, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  232. 0x00, 0x04, 0x00, 0x00, 0x08, 0x00, 0x00, 0x00,
  233. 0x01, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  234. 0x00, 0x10, 0x00, 0x00, 0x00, 0x01, 0x00, 0x00,
  235. 0x40, 0x00, 0x00, 0x00, 0x00, 0x08, 0x00, 0x00,
  236. 0x0C, 0x00, 0x00, 0x00, 0x04, 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. 0x01, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  241. 0x00, 0x00, 0x00, 0x00, 0x01, 0x00, 0x00, 0x00,
  242. 0xFF, 0x00, 0x00, 0x00, 0x62, 0x04, 0xD6, 0x1F,
  243. 0x2B, 0x57, 0x7A, 0x2D, 0xFE, 0xBB, 0x4A, 0x50,
  244. 0xEC, 0xF8, 0x70, 0x1A, 0x44, 0x15, 0xF6, 0xA2,
  245. 0x8E, 0xB0, 0xFD, 0xFA, 0xDC, 0xAA, 0x5A, 0x4E,
  246. 0xCB, 0x8E, 0xC9, 0x72, 0x08, 0xDC, 0x20, 0xB9,
  247. 0x98, 0xC8, 0x82, 0xD8, 0xBE, 0x44
  248. }
  249. };
  250. /* HSM2.0 BROM NAND header for SPI NAND with 2KB page + 64B spare */
  251. static const union hsm20_nand_boot_header hsm20_nand_hdr_2k_64_data = {
  252. .data = {
  253. 0x4E, 0x41, 0x4E, 0x44, 0x43, 0x46, 0x47, 0x21,
  254. 0x01, 0x00, 0x00, 0x00, 0x00, 0x00, 0x10, 0x00,
  255. 0x00, 0x04, 0x00, 0x00, 0x08, 0x00, 0x00, 0x00,
  256. 0x01, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  257. 0x00, 0x08, 0x00, 0x00, 0x40, 0x00, 0x00, 0x00,
  258. 0x40, 0x00, 0x00, 0x00, 0x00, 0x08, 0x00, 0x00,
  259. 0x0C, 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. 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  263. 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  264. 0x00, 0x00, 0x00, 0x00, 0xFF, 0xFF, 0xFF, 0xFF,
  265. 0x01, 0x00, 0x00, 0x00, 0xFF, 0x00, 0x00, 0x00,
  266. 0x01, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  267. 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  268. 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  269. 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  270. 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  271. 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  272. 0x00, 0x00, 0x00, 0x00, 0x00, 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. 0x5F, 0x4B, 0xB2, 0x5B, 0x8B, 0x1C, 0x35, 0xDA,
  278. 0x83, 0xE6, 0x6C, 0xC3, 0xFB, 0x8C, 0x78, 0x23,
  279. 0xD0, 0x89, 0x24, 0xD9, 0x6C, 0x35, 0x2C, 0x5D,
  280. 0x8F, 0xBB, 0xFC, 0x10, 0xD0, 0xE2, 0x22, 0x7D,
  281. 0xC8, 0x97, 0x9A, 0xEF, 0xC6, 0xB5, 0xA7, 0x4E,
  282. 0x4E, 0x0E
  283. }
  284. };
  285. /* HSM2.0 BROM NAND header for SPI NAND with 2KB page + 128B spare */
  286. static const union hsm20_nand_boot_header hsm20_nand_hdr_2k_128_data = {
  287. .data = {
  288. 0x4E, 0x41, 0x4E, 0x44, 0x43, 0x46, 0x47, 0x21,
  289. 0x01, 0x00, 0x00, 0x00, 0x00, 0x00, 0x10, 0x00,
  290. 0x00, 0x04, 0x00, 0x00, 0x08, 0x00, 0x00, 0x00,
  291. 0x01, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  292. 0x00, 0x08, 0x00, 0x00, 0x80, 0x00, 0x00, 0x00,
  293. 0x40, 0x00, 0x00, 0x00, 0x00, 0x08, 0x00, 0x00,
  294. 0x0C, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  295. 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  296. 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  297. 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  298. 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  299. 0x00, 0x00, 0x00, 0x00, 0xFF, 0xFF, 0xFF, 0xFF,
  300. 0x01, 0x00, 0x00, 0x00, 0xFF, 0x00, 0x00, 0x00,
  301. 0x01, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  302. 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  303. 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  304. 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  305. 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  306. 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  307. 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  308. 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  309. 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  310. 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  311. 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  312. 0xF8, 0x7E, 0xC1, 0x5D, 0x61, 0x54, 0xEA, 0x9F,
  313. 0x5E, 0x66, 0x39, 0x66, 0x21, 0xFF, 0x8C, 0x3B,
  314. 0xBE, 0xA7, 0x5A, 0x9E, 0xD7, 0xBD, 0x9E, 0x89,
  315. 0xEE, 0x7E, 0x10, 0x31, 0x9A, 0x1D, 0x82, 0x49,
  316. 0xA3, 0x4E, 0xD8, 0x47, 0xD7, 0x19, 0xF4, 0x2D,
  317. 0x8E, 0x53
  318. }
  319. };
  320. /* HSM2.0 BROM NAND header for SPI NAND with 4KB page + 256B spare */
  321. static const union hsm20_nand_boot_header hsm20_nand_hdr_4k_256_data = {
  322. .data = {
  323. 0x4E, 0x41, 0x4E, 0x44, 0x43, 0x46, 0x47, 0x21,
  324. 0x01, 0x00, 0x00, 0x00, 0x00, 0x00, 0x10, 0x00,
  325. 0x00, 0x04, 0x00, 0x00, 0x08, 0x00, 0x00, 0x00,
  326. 0x01, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  327. 0x00, 0x10, 0x00, 0x00, 0x00, 0x01, 0x00, 0x00,
  328. 0x40, 0x00, 0x00, 0x00, 0x00, 0x08, 0x00, 0x00,
  329. 0x0C, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  330. 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  331. 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  332. 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  333. 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  334. 0x00, 0x00, 0x00, 0x00, 0xFF, 0xFF, 0xFF, 0xFF,
  335. 0x01, 0x00, 0x00, 0x00, 0xFF, 0x00, 0x00, 0x00,
  336. 0x01, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  337. 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  338. 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  339. 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  340. 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  341. 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  342. 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  343. 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  344. 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  345. 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  346. 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  347. 0x79, 0x01, 0x1F, 0x86, 0x62, 0x6A, 0x43, 0xAE,
  348. 0xE6, 0xF8, 0xDD, 0x5B, 0x29, 0xB7, 0xA2, 0x7F,
  349. 0x29, 0x72, 0x54, 0x37, 0xBE, 0x50, 0xD4, 0x24,
  350. 0xAB, 0x60, 0xF4, 0x44, 0x97, 0x3B, 0x65, 0x21,
  351. 0x73, 0x24, 0x1F, 0x93, 0x0E, 0x9E, 0x96, 0x88,
  352. 0x78, 0x6C
  353. }
  354. };
  355. /* SPIM-NAND header for SPI NAND with 2KB page + 64B spare */
  356. static const union spim_nand_boot_header spim_nand_hdr_2k_64_data = {
  357. .data = {
  358. 0x53, 0x50, 0x49, 0x4e, 0x41, 0x4e, 0x44, 0x21,
  359. 0x01, 0x00, 0x00, 0x00, 0x10, 0x00, 0x00, 0x00,
  360. 0x00, 0x08, 0x00, 0x00, 0x40, 0x00, 0x00, 0x00,
  361. 0x40, 0x00, 0x0c, 0x00, 0x00, 0x00, 0x20, 0x30,
  362. 0x01, 0x00, 0x00, 0x00, 0xff, 0x00, 0x00, 0x00,
  363. 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  364. 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  365. 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  366. 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  367. 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00
  368. }
  369. };
  370. /* SPIM-NAND header for SPI NAND with 2KB page + 128B spare */
  371. static const union spim_nand_boot_header spim_nand_hdr_2k_128_data = {
  372. .data = {
  373. 0x53, 0x50, 0x49, 0x4e, 0x41, 0x4e, 0x44, 0x21,
  374. 0x01, 0x00, 0x00, 0x00, 0x10, 0x00, 0x00, 0x00,
  375. 0x00, 0x08, 0x00, 0x00, 0x80, 0x00, 0x00, 0x00,
  376. 0x40, 0x00, 0x0c, 0x00, 0x00, 0x00, 0x20, 0x30,
  377. 0x01, 0x00, 0x00, 0x00, 0xff, 0x00, 0x00, 0x00,
  378. 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  379. 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  380. 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  381. 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  382. 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00
  383. }
  384. };
  385. /* SPIM-NAND header for SPI NAND with 4KB page + 256B spare */
  386. static const union spim_nand_boot_header spim_nand_hdr_4k_256_data = {
  387. .data = {
  388. 0x53, 0x50, 0x49, 0x4e, 0x41, 0x4e, 0x44, 0x21,
  389. 0x01, 0x00, 0x00, 0x00, 0x10, 0x00, 0x00, 0x00,
  390. 0x00, 0x10, 0x00, 0x00, 0x00, 0x01, 0x00, 0x00,
  391. 0x40, 0x00, 0x0d, 0x00, 0x00, 0x00, 0x20, 0x30,
  392. 0x01, 0x00, 0x00, 0x00, 0xff, 0x00, 0x00, 0x00,
  393. 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  394. 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  395. 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  396. 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  397. 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00
  398. }
  399. };
  400. struct nand_header_type {
  401. const char *name;
  402. enum nand_boot_header_type type;
  403. union {
  404. const union nand_boot_header *ap;
  405. const union hsm_nand_boot_header *hsm;
  406. const union hsm20_nand_boot_header *hsm20;
  407. const union spim_nand_boot_header *spim;
  408. };
  409. } nand_headers[] = {
  410. {
  411. .name = "2k+64",
  412. .type = NAND_BOOT_AP_HEADER,
  413. .ap = &snand_hdr_2k_64_data,
  414. }, {
  415. .name = "2k+120",
  416. .type = NAND_BOOT_AP_HEADER,
  417. .ap = &snand_hdr_2k_128_data,
  418. }, {
  419. .name = "2k+128",
  420. .type = NAND_BOOT_AP_HEADER,
  421. .ap = &snand_hdr_2k_128_data,
  422. }, {
  423. .name = "4k+256",
  424. .type = NAND_BOOT_AP_HEADER,
  425. .ap = &snand_hdr_4k_256_data,
  426. }, {
  427. .name = "1g:2k+64",
  428. .type = NAND_BOOT_AP_HEADER,
  429. .ap = &nand_hdr_1gb_2k_64_data,
  430. }, {
  431. .name = "2g:2k+64",
  432. .type = NAND_BOOT_AP_HEADER,
  433. .ap = &nand_hdr_2gb_2k_64_data,
  434. }, {
  435. .name = "4g:2k+64",
  436. .type = NAND_BOOT_AP_HEADER,
  437. .ap = &nand_hdr_4gb_2k_64_data,
  438. }, {
  439. .name = "2g:2k+128",
  440. .type = NAND_BOOT_AP_HEADER,
  441. .ap = &nand_hdr_2gb_2k_128_data,
  442. }, {
  443. .name = "4g:2k+128",
  444. .type = NAND_BOOT_AP_HEADER,
  445. .ap = &nand_hdr_4gb_2k_128_data,
  446. }, {
  447. .name = "hsm:2k+64",
  448. .type = NAND_BOOT_HSM_HEADER,
  449. .hsm = &hsm_nand_hdr_2k_64_data,
  450. }, {
  451. .name = "hsm:2k+128",
  452. .type = NAND_BOOT_HSM_HEADER,
  453. .hsm = &hsm_nand_hdr_2k_128_data,
  454. }, {
  455. .name = "hsm:4k+256",
  456. .type = NAND_BOOT_HSM_HEADER,
  457. .hsm = &hsm_nand_hdr_4k_256_data,
  458. }, {
  459. .name = "hsm20:2k+64",
  460. .type = NAND_BOOT_HSM20_HEADER,
  461. .hsm20 = &hsm20_nand_hdr_2k_64_data,
  462. }, {
  463. .name = "hsm20:2k+128",
  464. .type = NAND_BOOT_HSM20_HEADER,
  465. .hsm20 = &hsm20_nand_hdr_2k_128_data,
  466. }, {
  467. .name = "hsm20:4k+256",
  468. .type = NAND_BOOT_HSM20_HEADER,
  469. .hsm20 = &hsm20_nand_hdr_4k_256_data,
  470. }, {
  471. .name = "spim:2k+64",
  472. .type = NAND_BOOT_SPIM_HEADER,
  473. .spim = &spim_nand_hdr_2k_64_data,
  474. }, {
  475. .name = "spim:2k+128",
  476. .type = NAND_BOOT_SPIM_HEADER,
  477. .spim = &spim_nand_hdr_2k_128_data,
  478. }, {
  479. .name = "spim:4k+256",
  480. .type = NAND_BOOT_SPIM_HEADER,
  481. .spim = &spim_nand_hdr_4k_256_data,
  482. }
  483. };
  484. const struct nand_header_type *mtk_nand_header_find(const char *name)
  485. {
  486. uint32_t i;
  487. for (i = 0; i < ARRAY_SIZE(nand_headers); i++) {
  488. if (!strcmp(nand_headers[i].name, name))
  489. return &nand_headers[i];
  490. }
  491. return NULL;
  492. }
  493. uint32_t mtk_nand_header_size(const struct nand_header_type *hdr_nand)
  494. {
  495. switch (hdr_nand->type) {
  496. case NAND_BOOT_HSM_HEADER:
  497. return le32_to_cpu(hdr_nand->hsm->page_size);
  498. case NAND_BOOT_HSM20_HEADER:
  499. return le32_to_cpu(hdr_nand->hsm20->page_size);
  500. case NAND_BOOT_SPIM_HEADER:
  501. return le32_to_cpu(hdr_nand->spim->page_size);
  502. default:
  503. return 2 * le16_to_cpu(hdr_nand->ap->pagesize);
  504. }
  505. }
  506. static int mtk_nand_header_ap_info(const void *ptr,
  507. struct nand_header_info *info)
  508. {
  509. union nand_boot_header *nh = (union nand_boot_header *)ptr;
  510. if (strncmp(nh->version, NAND_BOOT_VERSION, sizeof(nh->version)) ||
  511. strcmp(nh->id, NAND_BOOT_ID))
  512. return -1;
  513. info->page_size = le16_to_cpu(nh->pagesize);
  514. info->spare_size = le16_to_cpu(nh->oobsize);
  515. info->gfh_offset = 2 * info->page_size;
  516. info->snfi = true;
  517. return 0;
  518. }
  519. static int mtk_nand_header_hsm_info(const void *ptr,
  520. struct nand_header_info *info)
  521. {
  522. union hsm_nand_boot_header *nh = (union hsm_nand_boot_header *)ptr;
  523. info->page_size = le16_to_cpu(nh->page_size);
  524. info->spare_size = le16_to_cpu(nh->spare_size);
  525. info->gfh_offset = info->page_size;
  526. info->snfi = true;
  527. return 1;
  528. }
  529. static int mtk_nand_header_spim_info(const void *ptr,
  530. struct nand_header_info *info)
  531. {
  532. union spim_nand_boot_header *nh = (union spim_nand_boot_header *)ptr;
  533. info->page_size = le16_to_cpu(nh->page_size);
  534. info->spare_size = le16_to_cpu(nh->spare_size);
  535. info->gfh_offset = info->page_size;
  536. info->snfi = false;
  537. return 1;
  538. }
  539. int mtk_nand_header_info(const void *ptr, struct nand_header_info *info)
  540. {
  541. if (!strcmp((char *)ptr, NAND_BOOT_NAME))
  542. return mtk_nand_header_ap_info(ptr, info);
  543. else if (!strncmp((char *)ptr, HSM_NAND_BOOT_NAME, 8))
  544. return mtk_nand_header_hsm_info(ptr, info);
  545. else if (!strncmp((char *)ptr, SPIM_NAND_BOOT_NAME, 8))
  546. return mtk_nand_header_spim_info(ptr, info);
  547. return -1;
  548. }
  549. bool is_mtk_nand_header(const void *ptr)
  550. {
  551. struct nand_header_info info;
  552. if (mtk_nand_header_info(ptr, &info) >= 0)
  553. return true;
  554. return false;
  555. }
  556. static uint16_t crc16(const uint8_t *p, uint32_t len)
  557. {
  558. uint16_t crc = 0x4f4e;
  559. uint32_t i;
  560. while (len--) {
  561. crc ^= *p++ << 8;
  562. for (i = 0; i < 8; i++)
  563. crc = (crc << 1) ^ ((crc & 0x8000) ? 0x8005 : 0);
  564. }
  565. return crc;
  566. }
  567. static uint32_t mtk_nand_header_put_ap(const struct nand_header_type *hdr_nand,
  568. void *ptr)
  569. {
  570. int i;
  571. /* NAND device header, repeat 4 times */
  572. for (i = 0; i < 4; i++) {
  573. memcpy(ptr, hdr_nand->ap, sizeof(*hdr_nand->ap));
  574. ptr += sizeof(*hdr_nand->ap);
  575. }
  576. return le16_to_cpu(hdr_nand->ap->pagesize);
  577. }
  578. static uint32_t mtk_nand_header_put_hsm(const struct nand_header_type *hdr_nand,
  579. void *ptr)
  580. {
  581. memcpy(ptr, hdr_nand->hsm, sizeof(*hdr_nand->hsm));
  582. return 0;
  583. }
  584. static uint32_t mtk_nand_header_put_hsm20(const struct nand_header_type *hdr_nand,
  585. void *ptr)
  586. {
  587. memcpy(ptr, hdr_nand->hsm20, sizeof(*hdr_nand->hsm20));
  588. return 0;
  589. }
  590. static uint32_t mtk_nand_header_put_spim(const struct nand_header_type *hdr_nand,
  591. void *ptr)
  592. {
  593. uint16_t crc;
  594. memcpy(ptr, hdr_nand->spim, sizeof(*hdr_nand->spim));
  595. crc = crc16(ptr, 0x4e);
  596. memcpy(ptr + 0x4e, &crc, sizeof(uint16_t));
  597. return 0;
  598. }
  599. uint32_t mtk_nand_header_put(const struct nand_header_type *hdr_nand, void *ptr)
  600. {
  601. switch (hdr_nand->type) {
  602. case NAND_BOOT_HSM_HEADER:
  603. return mtk_nand_header_put_hsm(hdr_nand, ptr);
  604. case NAND_BOOT_HSM20_HEADER:
  605. return mtk_nand_header_put_hsm20(hdr_nand, ptr);
  606. case NAND_BOOT_SPIM_HEADER:
  607. return mtk_nand_header_put_spim(hdr_nand, ptr);
  608. default:
  609. return mtk_nand_header_put_ap(hdr_nand, ptr);
  610. }
  611. }