mtk_image.c 23 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813
  1. // SPDX-License-Identifier: GPL-2.0+
  2. /*
  3. * Generate MediaTek BootROM header for SPL/U-Boot images
  4. *
  5. * Copyright (C) 2018 MediaTek Inc.
  6. * Author: Weijie Gao <weijie.gao@mediatek.com>
  7. */
  8. #include <image.h>
  9. #include <u-boot/sha256.h>
  10. #include "imagetool.h"
  11. #include "mtk_image.h"
  12. /* NAND header for SPI-NAND with 2KB page + 64B spare */
  13. static const union nand_boot_header snand_hdr_2k_64_data = {
  14. .data = {
  15. 0x42, 0x4F, 0x4F, 0x54, 0x4C, 0x4F, 0x41, 0x44,
  16. 0x45, 0x52, 0x21, 0x00, 0x56, 0x30, 0x30, 0x36,
  17. 0x4E, 0x46, 0x49, 0x49, 0x4E, 0x46, 0x4F, 0x00,
  18. 0x00, 0x00, 0x00, 0x08, 0x03, 0x00, 0x40, 0x00,
  19. 0x40, 0x00, 0x00, 0x08, 0x10, 0x00, 0x16, 0x00,
  20. 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 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, 0x7B, 0xC4, 0x17, 0x9D,
  28. 0xCA, 0x42, 0x90, 0xD0, 0x98, 0xD0, 0xE0, 0xF7,
  29. 0xDB, 0xCD, 0x16, 0xF6, 0x03, 0x73, 0xD2, 0xB8,
  30. 0x93, 0xB2, 0x56, 0x5A, 0x84, 0x6E, 0x00, 0x00
  31. }
  32. };
  33. /* NAND header for SPI-NAND with 2KB page + 120B/128B spare */
  34. static const union nand_boot_header snand_hdr_2k_128_data = {
  35. .data = {
  36. 0x42, 0x4F, 0x4F, 0x54, 0x4C, 0x4F, 0x41, 0x44,
  37. 0x45, 0x52, 0x21, 0x00, 0x56, 0x30, 0x30, 0x36,
  38. 0x4E, 0x46, 0x49, 0x49, 0x4E, 0x46, 0x4F, 0x00,
  39. 0x00, 0x00, 0x00, 0x08, 0x05, 0x00, 0x70, 0x00,
  40. 0x40, 0x00, 0x00, 0x08, 0x10, 0x00, 0x16, 0x00,
  41. 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 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, 0x90, 0x28, 0xED, 0x13,
  49. 0x7F, 0x12, 0x22, 0xCD, 0x3D, 0x06, 0xF1, 0xB3,
  50. 0x6F, 0x2E, 0xD9, 0xA0, 0x9D, 0x7A, 0xBD, 0xD7,
  51. 0xB3, 0x28, 0x3C, 0x13, 0xDB, 0x4E, 0x00, 0x00
  52. }
  53. };
  54. /* NAND header for SPI-NAND with 4KB page + 256B spare */
  55. static const union nand_boot_header snand_hdr_4k_256_data = {
  56. .data = {
  57. 0x42, 0x4F, 0x4F, 0x54, 0x4C, 0x4F, 0x41, 0x44,
  58. 0x45, 0x52, 0x21, 0x00, 0x56, 0x30, 0x30, 0x36,
  59. 0x4E, 0x46, 0x49, 0x49, 0x4E, 0x46, 0x4F, 0x00,
  60. 0x00, 0x00, 0x00, 0x10, 0x05, 0x00, 0xE0, 0x00,
  61. 0x40, 0x00, 0x00, 0x08, 0x10, 0x00, 0x16, 0x00,
  62. 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 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, 0x47, 0xED, 0x0E, 0xC3,
  70. 0x83, 0xBF, 0x41, 0xD2, 0x85, 0x21, 0x97, 0x57,
  71. 0xC4, 0x2E, 0x6B, 0x7A, 0x40, 0xE0, 0xCF, 0x8F,
  72. 0x37, 0xBD, 0x17, 0xB6, 0xC7, 0xFE, 0x00, 0x00
  73. }
  74. };
  75. /* NAND header for Parallel NAND 1Gb with 2KB page + 64B spare */
  76. static const union nand_boot_header nand_hdr_1gb_2k_64_data = {
  77. .data = {
  78. 0x42, 0x4F, 0x4F, 0x54, 0x4C, 0x4F, 0x41, 0x44,
  79. 0x45, 0x52, 0x21, 0x00, 0x56, 0x30, 0x30, 0x36,
  80. 0x4E, 0x46, 0x49, 0x49, 0x4E, 0x46, 0x4F, 0x00,
  81. 0x00, 0x00, 0x00, 0x08, 0x05, 0x00, 0x40, 0x00,
  82. 0x40, 0x00, 0x00, 0x04, 0x0B, 0x00, 0x11, 0x00,
  83. 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 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, 0x12, 0x28, 0x1C, 0x12,
  91. 0x8F, 0xFD, 0xF8, 0x32, 0x6F, 0x6D, 0xCF, 0x6C,
  92. 0xDA, 0x21, 0x70, 0x8C, 0xDA, 0x0A, 0x22, 0x82,
  93. 0xAA, 0x59, 0xFA, 0x7C, 0x42, 0x2D, 0x00, 0x00
  94. }
  95. };
  96. /* NAND header for Parallel NAND 2Gb with 2KB page + 64B spare */
  97. static const union nand_boot_header nand_hdr_2gb_2k_64_data = {
  98. .data = {
  99. 0x42, 0x4F, 0x4F, 0x54, 0x4C, 0x4F, 0x41, 0x44,
  100. 0x45, 0x52, 0x21, 0x00, 0x56, 0x30, 0x30, 0x36,
  101. 0x4E, 0x46, 0x49, 0x49, 0x4E, 0x46, 0x4F, 0x00,
  102. 0x00, 0x00, 0x00, 0x08, 0x05, 0x00, 0x40, 0x00,
  103. 0x40, 0x00, 0x00, 0x08, 0x0B, 0x00, 0x11, 0x00,
  104. 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  105. 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  106. 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  107. 0x00, 0x00, 0x00, 0x00, 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, 0x20, 0x9C, 0x3D, 0x2D,
  112. 0x7B, 0x68, 0x63, 0x52, 0x2E, 0x04, 0x63, 0xF1,
  113. 0x35, 0x4E, 0x44, 0x3E, 0xF8, 0xAC, 0x9B, 0x95,
  114. 0xAB, 0xFE, 0xE4, 0xE1, 0xD5, 0xF9, 0x00, 0x00
  115. }
  116. };
  117. /* NAND header for Parallel NAND 4Gb with 2KB page + 64B spare */
  118. static const union nand_boot_header nand_hdr_4gb_2k_64_data = {
  119. .data = {
  120. 0x42, 0x4F, 0x4F, 0x54, 0x4C, 0x4F, 0x41, 0x44,
  121. 0x45, 0x52, 0x21, 0x00, 0x56, 0x30, 0x30, 0x36,
  122. 0x4E, 0x46, 0x49, 0x49, 0x4E, 0x46, 0x4F, 0x00,
  123. 0x00, 0x00, 0x00, 0x08, 0x05, 0x00, 0x40, 0x00,
  124. 0x40, 0x00, 0x00, 0x10, 0x0B, 0x00, 0x11, 0x00,
  125. 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 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, 0xE3, 0x0F, 0x86, 0x32,
  133. 0x68, 0x05, 0xD9, 0xC8, 0x13, 0xDF, 0xC5, 0x0B,
  134. 0x35, 0x3A, 0x68, 0xA5, 0x3C, 0x0C, 0x73, 0x87,
  135. 0x63, 0xB0, 0xBE, 0xCC, 0x84, 0x47, 0x00, 0x00
  136. }
  137. };
  138. /* NAND header for Parallel NAND 2Gb with 2KB page + 128B spare */
  139. static const union nand_boot_header nand_hdr_2gb_2k_128_data = {
  140. .data = {
  141. 0x42, 0x4F, 0x4F, 0x54, 0x4C, 0x4F, 0x41, 0x44,
  142. 0x45, 0x52, 0x21, 0x00, 0x56, 0x30, 0x30, 0x36,
  143. 0x4E, 0x46, 0x49, 0x49, 0x4E, 0x46, 0x4F, 0x00,
  144. 0x00, 0x00, 0x00, 0x08, 0x05, 0x00, 0x70, 0x00,
  145. 0x40, 0x00, 0x00, 0x08, 0x0B, 0x00, 0x11, 0x00,
  146. 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 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, 0x01, 0xA5, 0xE9, 0x5A,
  154. 0xDF, 0x58, 0x62, 0x41, 0xD6, 0x26, 0x77, 0xBC,
  155. 0x76, 0x1F, 0x27, 0x4E, 0x4F, 0x6C, 0xC3, 0xF0,
  156. 0x36, 0xDE, 0xD9, 0xB3, 0xFF, 0x93, 0x00, 0x00
  157. }
  158. };
  159. /* NAND header for Parallel NAND 4Gb with 2KB page + 128B spare */
  160. static const union nand_boot_header nand_hdr_4gb_2k_128_data = {
  161. .data = {
  162. 0x42, 0x4F, 0x4F, 0x54, 0x4C, 0x4F, 0x41, 0x44,
  163. 0x45, 0x52, 0x21, 0x00, 0x56, 0x30, 0x30, 0x36,
  164. 0x4E, 0x46, 0x49, 0x49, 0x4E, 0x46, 0x4F, 0x00,
  165. 0x00, 0x00, 0x00, 0x08, 0x05, 0x00, 0x70, 0x00,
  166. 0x40, 0x00, 0x00, 0x10, 0x0B, 0x00, 0x11, 0x00,
  167. 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 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, 0xC2, 0x36, 0x52, 0x45,
  175. 0xCC, 0x35, 0xD8, 0xDB, 0xEB, 0xFD, 0xD1, 0x46,
  176. 0x76, 0x6B, 0x0B, 0xD5, 0x8B, 0xCC, 0x2B, 0xE2,
  177. 0xFE, 0x90, 0x83, 0x9E, 0xAE, 0x2D, 0x00, 0x00
  178. }
  179. };
  180. static const struct nand_header_type {
  181. const char *name;
  182. const union nand_boot_header *data;
  183. } nand_headers[] = {
  184. {
  185. .name = "2k+64",
  186. .data = &snand_hdr_2k_64_data
  187. }, {
  188. .name = "2k+120",
  189. .data = &snand_hdr_2k_128_data
  190. }, {
  191. .name = "2k+128",
  192. .data = &snand_hdr_2k_128_data
  193. }, {
  194. .name = "4k+256",
  195. .data = &snand_hdr_4k_256_data
  196. }, {
  197. .name = "1g:2k+64",
  198. .data = &nand_hdr_1gb_2k_64_data
  199. }, {
  200. .name = "2g:2k+64",
  201. .data = &nand_hdr_2gb_2k_64_data
  202. }, {
  203. .name = "4g:2k+64",
  204. .data = &nand_hdr_4gb_2k_64_data
  205. }, {
  206. .name = "2g:2k+128",
  207. .data = &nand_hdr_2gb_2k_128_data
  208. }, {
  209. .name = "4g:2k+128",
  210. .data = &nand_hdr_4gb_2k_128_data
  211. }
  212. };
  213. static const struct brom_img_type {
  214. const char *name;
  215. enum brlyt_img_type type;
  216. } brom_images[] = {
  217. {
  218. .name = "nand",
  219. .type = BRLYT_TYPE_NAND
  220. }, {
  221. .name = "emmc",
  222. .type = BRLYT_TYPE_EMMC
  223. }, {
  224. .name = "nor",
  225. .type = BRLYT_TYPE_NOR
  226. }, {
  227. .name = "sdmmc",
  228. .type = BRLYT_TYPE_SDMMC
  229. }, {
  230. .name = "snand",
  231. .type = BRLYT_TYPE_SNAND
  232. }
  233. };
  234. /* Indicates whether we're generating or verifying */
  235. static bool img_gen;
  236. static uint32_t img_size;
  237. /* Image type selected by user */
  238. static enum brlyt_img_type hdr_media;
  239. static uint32_t hdr_offset;
  240. static int use_lk_hdr;
  241. static bool is_arm64_image;
  242. /* LK image name */
  243. static char lk_name[32] = "U-Boot";
  244. /* NAND header selected by user */
  245. static const union nand_boot_header *hdr_nand;
  246. /* GFH header + 2 * 4KB pages of NAND */
  247. static char hdr_tmp[sizeof(struct gfh_header) + 0x2000];
  248. static int mtk_image_check_image_types(uint8_t type)
  249. {
  250. if (type == IH_TYPE_MTKIMAGE)
  251. return EXIT_SUCCESS;
  252. else
  253. return EXIT_FAILURE;
  254. }
  255. static int mtk_brom_parse_imagename(const char *imagename)
  256. {
  257. #define is_blank_char(c) \
  258. ((c) == '\t' || (c) == '\n' || (c) == '\r' || (c) == ' ')
  259. char *buf = strdup(imagename), *key, *val, *end, *next;
  260. int i;
  261. /* User passed arguments from image name */
  262. static const char *media = "";
  263. static const char *hdr_offs = "";
  264. static const char *nandinfo = "";
  265. static const char *lk = "";
  266. static const char *arm64_param = "";
  267. key = buf;
  268. while (key) {
  269. next = strchr(key, ';');
  270. if (next)
  271. *next = 0;
  272. val = strchr(key, '=');
  273. if (val) {
  274. *val++ = 0;
  275. /* Trim key */
  276. while (is_blank_char(*key))
  277. key++;
  278. end = key + strlen(key) - 1;
  279. while ((end >= key) && is_blank_char(*end))
  280. end--;
  281. end++;
  282. if (is_blank_char(*end))
  283. *end = 0;
  284. /* Trim value */
  285. while (is_blank_char(*val))
  286. val++;
  287. end = val + strlen(val) - 1;
  288. while ((end >= val) && is_blank_char(*end))
  289. end--;
  290. end++;
  291. if (is_blank_char(*end))
  292. *end = 0;
  293. /* record user passed arguments */
  294. if (!strcmp(key, "media"))
  295. media = val;
  296. if (!strcmp(key, "hdroffset"))
  297. hdr_offs = val;
  298. if (!strcmp(key, "nandinfo"))
  299. nandinfo = val;
  300. if (!strcmp(key, "lk"))
  301. lk = val;
  302. if (!strcmp(key, "lkname"))
  303. snprintf(lk_name, sizeof(lk_name), "%s", val);
  304. if (!strcmp(key, "arm64"))
  305. arm64_param = val;
  306. }
  307. if (next)
  308. key = next + 1;
  309. else
  310. break;
  311. }
  312. /* if user specified LK image header, skip following checks */
  313. if (lk && lk[0] == '1') {
  314. use_lk_hdr = 1;
  315. free(buf);
  316. return 0;
  317. }
  318. /* parse media type */
  319. for (i = 0; i < ARRAY_SIZE(brom_images); i++) {
  320. if (!strcmp(brom_images[i].name, media)) {
  321. hdr_media = brom_images[i].type;
  322. break;
  323. }
  324. }
  325. /* parse nand header type */
  326. for (i = 0; i < ARRAY_SIZE(nand_headers); i++) {
  327. if (!strcmp(nand_headers[i].name, nandinfo)) {
  328. hdr_nand = nand_headers[i].data;
  329. break;
  330. }
  331. }
  332. /* parse device header offset */
  333. if (hdr_offs && hdr_offs[0])
  334. hdr_offset = strtoul(hdr_offs, NULL, 0);
  335. if (arm64_param && arm64_param[0] == '1')
  336. is_arm64_image = true;
  337. free(buf);
  338. if (hdr_media == BRLYT_TYPE_INVALID) {
  339. fprintf(stderr, "Error: media type is invalid or missing.\n");
  340. fprintf(stderr, " Please specify -n \"media=<type>\"\n");
  341. return -EINVAL;
  342. }
  343. if ((hdr_media == BRLYT_TYPE_NAND || hdr_media == BRLYT_TYPE_SNAND) &&
  344. !hdr_nand) {
  345. fprintf(stderr, "Error: nand info is invalid or missing.\n");
  346. fprintf(stderr, " Please specify -n \"media=%s;"
  347. "nandinfo=<info>\"\n", media);
  348. return -EINVAL;
  349. }
  350. return 0;
  351. }
  352. static int mtk_image_check_params(struct image_tool_params *params)
  353. {
  354. if (!params->addr) {
  355. fprintf(stderr, "Error: Load Address must be set.\n");
  356. return -EINVAL;
  357. }
  358. if (!params->imagename) {
  359. fprintf(stderr, "Error: Image Name must be set.\n");
  360. return -EINVAL;
  361. }
  362. return mtk_brom_parse_imagename(params->imagename);
  363. }
  364. static int mtk_image_vrec_header(struct image_tool_params *params,
  365. struct image_type_params *tparams)
  366. {
  367. if (use_lk_hdr) {
  368. tparams->header_size = sizeof(union lk_hdr);
  369. tparams->hdr = &hdr_tmp;
  370. memset(&hdr_tmp, 0xff, tparams->header_size);
  371. return 0;
  372. }
  373. if (hdr_media == BRLYT_TYPE_NAND || hdr_media == BRLYT_TYPE_SNAND)
  374. tparams->header_size = 2 * le16_to_cpu(hdr_nand->pagesize);
  375. else
  376. tparams->header_size = sizeof(struct gen_device_header);
  377. tparams->header_size += sizeof(struct gfh_header);
  378. tparams->hdr = &hdr_tmp;
  379. memset(&hdr_tmp, 0xff, tparams->header_size);
  380. return SHA256_SUM_LEN;
  381. }
  382. static int mtk_image_verify_gen_header(const uint8_t *ptr, int print)
  383. {
  384. union gen_boot_header *gbh = (union gen_boot_header *)ptr;
  385. uint32_t gfh_offset, total_size, devh_size;
  386. struct brom_layout_header *bh;
  387. struct gfh_header *gfh;
  388. const char *bootmedia;
  389. if (!strcmp(gbh->name, SF_BOOT_NAME))
  390. bootmedia = "Serial NOR";
  391. else if (!strcmp(gbh->name, EMMC_BOOT_NAME))
  392. bootmedia = "eMMC";
  393. else if (!strcmp(gbh->name, SDMMC_BOOT_NAME))
  394. bootmedia = "SD/MMC";
  395. else
  396. return -1;
  397. if (print)
  398. printf("Boot Media: %s\n", bootmedia);
  399. if (le32_to_cpu(gbh->version) != 1 ||
  400. le32_to_cpu(gbh->size) != sizeof(union gen_boot_header))
  401. return -1;
  402. bh = (struct brom_layout_header *)(ptr + le32_to_cpu(gbh->size));
  403. if (strcmp(bh->name, BRLYT_NAME))
  404. return -1;
  405. if (le32_to_cpu(bh->magic) != BRLYT_MAGIC ||
  406. (le32_to_cpu(bh->type) != BRLYT_TYPE_NOR &&
  407. le32_to_cpu(bh->type) != BRLYT_TYPE_EMMC &&
  408. le32_to_cpu(bh->type) != BRLYT_TYPE_SDMMC))
  409. return -1;
  410. devh_size = sizeof(struct gen_device_header);
  411. if (img_gen) {
  412. gfh_offset = devh_size;
  413. } else {
  414. gfh_offset = le32_to_cpu(bh->header_size);
  415. if (gfh_offset + sizeof(struct gfh_header) > img_size) {
  416. /*
  417. * This may happen if the hdr_offset used to generate
  418. * this image is not zero.
  419. * Since device header size is not fixed, we can't
  420. * cover all possible cases.
  421. * Assuming the image is valid only if the real
  422. * device header size equals to devh_size.
  423. */
  424. total_size = le32_to_cpu(bh->total_size);
  425. if (total_size - gfh_offset > img_size - devh_size)
  426. return -1;
  427. gfh_offset = devh_size;
  428. }
  429. }
  430. gfh = (struct gfh_header *)(ptr + gfh_offset);
  431. if (strcmp(gfh->file_info.name, GFH_FILE_INFO_NAME))
  432. return -1;
  433. if (le32_to_cpu(gfh->file_info.flash_type) != GFH_FLASH_TYPE_GEN)
  434. return -1;
  435. if (print)
  436. printf("Load Address: %08x\n",
  437. le32_to_cpu(gfh->file_info.load_addr) +
  438. le32_to_cpu(gfh->file_info.jump_offset));
  439. if (print)
  440. printf("Architecture: %s\n", is_arm64_image ? "ARM64" : "ARM");
  441. return 0;
  442. }
  443. static int mtk_image_verify_nand_header(const uint8_t *ptr, int print)
  444. {
  445. union nand_boot_header *nh = (union nand_boot_header *)ptr;
  446. struct brom_layout_header *bh;
  447. struct gfh_header *gfh;
  448. const char *bootmedia;
  449. if (strncmp(nh->version, NAND_BOOT_VERSION, sizeof(nh->version)) ||
  450. strcmp(nh->id, NAND_BOOT_ID))
  451. return -1;
  452. bh = (struct brom_layout_header *)(ptr + le16_to_cpu(nh->pagesize));
  453. if (strcmp(bh->name, BRLYT_NAME))
  454. return -1;
  455. if (le32_to_cpu(bh->magic) != BRLYT_MAGIC) {
  456. return -1;
  457. } else {
  458. if (le32_to_cpu(bh->type) == BRLYT_TYPE_NAND)
  459. bootmedia = "Parallel NAND";
  460. else if (le32_to_cpu(bh->type) == BRLYT_TYPE_SNAND)
  461. bootmedia = "Serial NAND";
  462. else
  463. return -1;
  464. }
  465. if (print) {
  466. printf("Boot Media: %s\n", bootmedia);
  467. if (le32_to_cpu(bh->type) == BRLYT_TYPE_NAND) {
  468. uint64_t capacity =
  469. (uint64_t)le16_to_cpu(nh->numblocks) *
  470. (uint64_t)le16_to_cpu(nh->pages_of_block) *
  471. (uint64_t)le16_to_cpu(nh->pagesize) * 8;
  472. printf("Capacity: %dGb\n",
  473. (uint32_t)(capacity >> 30));
  474. }
  475. if (le16_to_cpu(nh->pagesize) >= 1024)
  476. printf("Page Size: %dKB\n",
  477. le16_to_cpu(nh->pagesize) >> 10);
  478. else
  479. printf("Page Size: %dB\n",
  480. le16_to_cpu(nh->pagesize));
  481. printf("Spare Size: %dB\n", le16_to_cpu(nh->oobsize));
  482. }
  483. gfh = (struct gfh_header *)(ptr + 2 * le16_to_cpu(nh->pagesize));
  484. if (strcmp(gfh->file_info.name, GFH_FILE_INFO_NAME))
  485. return -1;
  486. if (le32_to_cpu(gfh->file_info.flash_type) != GFH_FLASH_TYPE_NAND)
  487. return -1;
  488. if (print)
  489. printf("Load Address: %08x\n",
  490. le32_to_cpu(gfh->file_info.load_addr) +
  491. le32_to_cpu(gfh->file_info.jump_offset));
  492. if (print)
  493. printf("Architecture: %s\n", is_arm64_image ? "ARM64" : "ARM");
  494. return 0;
  495. }
  496. static int mtk_image_verify_header(unsigned char *ptr, int image_size,
  497. struct image_tool_params *params)
  498. {
  499. union lk_hdr *lk = (union lk_hdr *)ptr;
  500. /* nothing to verify for LK image header */
  501. if (le32_to_cpu(lk->magic) == LK_PART_MAGIC)
  502. return 0;
  503. img_size = image_size;
  504. if (!strcmp((char *)ptr, NAND_BOOT_NAME))
  505. return mtk_image_verify_nand_header(ptr, 0);
  506. else
  507. return mtk_image_verify_gen_header(ptr, 0);
  508. return -1;
  509. }
  510. static void mtk_image_print_header(const void *ptr)
  511. {
  512. union lk_hdr *lk = (union lk_hdr *)ptr;
  513. if (le32_to_cpu(lk->magic) == LK_PART_MAGIC) {
  514. printf("Image Type: MediaTek LK Image\n");
  515. printf("Load Address: %08x\n", le32_to_cpu(lk->loadaddr));
  516. return;
  517. }
  518. printf("Image Type: MediaTek BootROM Loadable Image\n");
  519. if (!strcmp((char *)ptr, NAND_BOOT_NAME))
  520. mtk_image_verify_nand_header(ptr, 1);
  521. else
  522. mtk_image_verify_gen_header(ptr, 1);
  523. }
  524. static void put_brom_layout_header(struct brom_layout_header *hdr, int type)
  525. {
  526. strncpy(hdr->name, BRLYT_NAME, sizeof(hdr->name));
  527. hdr->version = cpu_to_le32(1);
  528. hdr->magic = cpu_to_le32(BRLYT_MAGIC);
  529. hdr->type = cpu_to_le32(type);
  530. }
  531. static void put_ghf_common_header(struct gfh_common_header *gfh, int size,
  532. int type, int ver)
  533. {
  534. memcpy(gfh->magic, GFH_HEADER_MAGIC, sizeof(gfh->magic));
  535. gfh->version = ver;
  536. gfh->size = cpu_to_le16(size);
  537. gfh->type = cpu_to_le16(type);
  538. }
  539. static void put_ghf_header(struct gfh_header *gfh, int file_size,
  540. int dev_hdr_size, int load_addr, int flash_type)
  541. {
  542. uint32_t cfg_bits;
  543. memset(gfh, 0, sizeof(struct gfh_header));
  544. /* GFH_FILE_INFO header */
  545. put_ghf_common_header(&gfh->file_info.gfh, sizeof(gfh->file_info),
  546. GFH_TYPE_FILE_INFO, 1);
  547. strncpy(gfh->file_info.name, GFH_FILE_INFO_NAME,
  548. sizeof(gfh->file_info.name));
  549. gfh->file_info.unused = cpu_to_le32(1);
  550. gfh->file_info.file_type = cpu_to_le16(1);
  551. gfh->file_info.flash_type = flash_type;
  552. gfh->file_info.sig_type = GFH_SIG_TYPE_SHA256;
  553. gfh->file_info.load_addr = cpu_to_le32(load_addr - sizeof(*gfh));
  554. gfh->file_info.total_size = cpu_to_le32(file_size - dev_hdr_size);
  555. gfh->file_info.max_size = cpu_to_le32(file_size);
  556. gfh->file_info.hdr_size = sizeof(*gfh);
  557. gfh->file_info.sig_size = SHA256_SUM_LEN;
  558. gfh->file_info.jump_offset = sizeof(*gfh);
  559. gfh->file_info.processed = cpu_to_le32(1);
  560. /* GFH_BL_INFO header */
  561. put_ghf_common_header(&gfh->bl_info.gfh, sizeof(gfh->bl_info),
  562. GFH_TYPE_BL_INFO, 1);
  563. gfh->bl_info.attr = cpu_to_le32(1);
  564. /* GFH_BROM_CFG header */
  565. put_ghf_common_header(&gfh->brom_cfg.gfh, sizeof(gfh->brom_cfg),
  566. GFH_TYPE_BROM_CFG, 3);
  567. cfg_bits = GFH_BROM_CFG_USBDL_AUTO_DETECT_DIS |
  568. GFH_BROM_CFG_USBDL_BY_KCOL0_TIMEOUT_EN |
  569. GFH_BROM_CFG_USBDL_BY_FLAG_TIMEOUT_EN;
  570. gfh->brom_cfg.usbdl_by_kcol0_timeout_ms = cpu_to_le32(5000);
  571. if (is_arm64_image) {
  572. gfh->brom_cfg.jump_bl_arm64 = GFH_BROM_CFG_JUMP_BL_ARM64;
  573. cfg_bits |= GFH_BROM_CFG_JUMP_BL_ARM64_EN;
  574. }
  575. gfh->brom_cfg.cfg_bits = cpu_to_le32(cfg_bits);
  576. /* GFH_BL_SEC_KEY header */
  577. put_ghf_common_header(&gfh->bl_sec_key.gfh, sizeof(gfh->bl_sec_key),
  578. GFH_TYPE_BL_SEC_KEY, 1);
  579. /* GFH_ANTI_CLONE header */
  580. put_ghf_common_header(&gfh->anti_clone.gfh, sizeof(gfh->anti_clone),
  581. GFH_TYPE_ANTI_CLONE, 1);
  582. gfh->anti_clone.ac_offset = cpu_to_le32(0x10);
  583. gfh->anti_clone.ac_len = cpu_to_le32(0x80);
  584. /* GFH_BROM_SEC_CFG header */
  585. put_ghf_common_header(&gfh->brom_sec_cfg.gfh,
  586. sizeof(gfh->brom_sec_cfg),
  587. GFH_TYPE_BROM_SEC_CFG, 1);
  588. gfh->brom_sec_cfg.cfg_bits =
  589. cpu_to_le32(BROM_SEC_CFG_JTAG_EN | BROM_SEC_CFG_UART_EN);
  590. }
  591. static void put_hash(uint8_t *buff, int size)
  592. {
  593. sha256_context ctx;
  594. sha256_starts(&ctx);
  595. sha256_update(&ctx, buff, size);
  596. sha256_finish(&ctx, buff + size);
  597. }
  598. static void mtk_image_set_gen_header(void *ptr, off_t filesize,
  599. uint32_t loadaddr)
  600. {
  601. struct gen_device_header *hdr = (struct gen_device_header *)ptr;
  602. struct gfh_header *gfh;
  603. const char *bootname = NULL;
  604. if (hdr_media == BRLYT_TYPE_NOR)
  605. bootname = SF_BOOT_NAME;
  606. else if (hdr_media == BRLYT_TYPE_EMMC)
  607. bootname = EMMC_BOOT_NAME;
  608. else if (hdr_media == BRLYT_TYPE_SDMMC)
  609. bootname = SDMMC_BOOT_NAME;
  610. /* Generic device header */
  611. snprintf(hdr->boot.name, sizeof(hdr->boot.name), "%s", bootname);
  612. hdr->boot.version = cpu_to_le32(1);
  613. hdr->boot.size = cpu_to_le32(sizeof(hdr->boot));
  614. /* BRLYT header */
  615. put_brom_layout_header(&hdr->brlyt, hdr_media);
  616. hdr->brlyt.header_size = cpu_to_le32(hdr_offset + sizeof(*hdr));
  617. hdr->brlyt.total_size = cpu_to_le32(hdr_offset + filesize);
  618. hdr->brlyt.header_size_2 = hdr->brlyt.header_size;
  619. hdr->brlyt.total_size_2 = hdr->brlyt.total_size;
  620. /* GFH header */
  621. gfh = (struct gfh_header *)(ptr + sizeof(struct gen_device_header));
  622. put_ghf_header(gfh, filesize, sizeof(struct gen_device_header),
  623. loadaddr, GFH_FLASH_TYPE_GEN);
  624. /* Generate SHA256 hash */
  625. put_hash((uint8_t *)gfh,
  626. filesize - sizeof(struct gen_device_header) - SHA256_SUM_LEN);
  627. }
  628. static void mtk_image_set_nand_header(void *ptr, off_t filesize,
  629. uint32_t loadaddr)
  630. {
  631. union nand_boot_header *nh = (union nand_boot_header *)ptr;
  632. struct brom_layout_header *brlyt;
  633. struct gfh_header *gfh;
  634. uint32_t payload_pages;
  635. int i;
  636. /* NAND device header, repeat 4 times */
  637. for (i = 0; i < 4; i++)
  638. memcpy(nh + i, hdr_nand, sizeof(union nand_boot_header));
  639. /* BRLYT header */
  640. payload_pages = (filesize + le16_to_cpu(hdr_nand->pagesize) - 1) /
  641. le16_to_cpu(hdr_nand->pagesize);
  642. brlyt = (struct brom_layout_header *)
  643. (ptr + le16_to_cpu(hdr_nand->pagesize));
  644. put_brom_layout_header(brlyt, hdr_media);
  645. brlyt->header_size = cpu_to_le32(2);
  646. brlyt->total_size = cpu_to_le32(payload_pages);
  647. brlyt->header_size_2 = brlyt->header_size;
  648. brlyt->total_size_2 = brlyt->total_size;
  649. brlyt->unused = cpu_to_le32(1);
  650. /* GFH header */
  651. gfh = (struct gfh_header *)(ptr + 2 * le16_to_cpu(hdr_nand->pagesize));
  652. put_ghf_header(gfh, filesize, 2 * le16_to_cpu(hdr_nand->pagesize),
  653. loadaddr, GFH_FLASH_TYPE_NAND);
  654. /* Generate SHA256 hash */
  655. put_hash((uint8_t *)gfh,
  656. filesize - 2 * le16_to_cpu(hdr_nand->pagesize) - SHA256_SUM_LEN);
  657. }
  658. static void mtk_image_set_header(void *ptr, struct stat *sbuf, int ifd,
  659. struct image_tool_params *params)
  660. {
  661. union lk_hdr *lk = (union lk_hdr *)ptr;
  662. if (use_lk_hdr) {
  663. lk->magic = cpu_to_le32(LK_PART_MAGIC);
  664. lk->size = cpu_to_le32(sbuf->st_size - sizeof(union lk_hdr));
  665. lk->loadaddr = cpu_to_le32(params->addr);
  666. lk->mode = 0xffffffff; /* must be non-zero */
  667. memset(lk->name, 0, sizeof(lk->name));
  668. strncpy(lk->name, lk_name, sizeof(lk->name));
  669. return;
  670. }
  671. img_gen = true;
  672. img_size = sbuf->st_size;
  673. if (hdr_media == BRLYT_TYPE_NAND || hdr_media == BRLYT_TYPE_SNAND)
  674. mtk_image_set_nand_header(ptr, sbuf->st_size, params->addr);
  675. else
  676. mtk_image_set_gen_header(ptr, sbuf->st_size, params->addr);
  677. }
  678. U_BOOT_IMAGE_TYPE(
  679. mtk_image,
  680. "MediaTek BootROM Loadable Image support",
  681. 0,
  682. NULL,
  683. mtk_image_check_params,
  684. mtk_image_verify_header,
  685. mtk_image_print_header,
  686. mtk_image_set_header,
  687. NULL,
  688. mtk_image_check_image_types,
  689. NULL,
  690. mtk_image_vrec_header
  691. );