mtk_image.c 22 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749
  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. /* Image type selected by user */
  235. static enum brlyt_img_type hdr_media;
  236. static int use_lk_hdr;
  237. /* LK image name */
  238. static char lk_name[32] = "U-Boot";
  239. /* NAND header selected by user */
  240. static const union nand_boot_header *hdr_nand;
  241. /* GFH header + 2 * 4KB pages of NAND */
  242. static char hdr_tmp[sizeof(struct gfh_header) + 0x2000];
  243. static int mtk_image_check_image_types(uint8_t type)
  244. {
  245. if (type == IH_TYPE_MTKIMAGE)
  246. return EXIT_SUCCESS;
  247. else
  248. return EXIT_FAILURE;
  249. }
  250. static int mtk_brom_parse_imagename(const char *imagename)
  251. {
  252. #define is_blank_char(c) \
  253. ((c) == '\t' || (c) == '\n' || (c) == '\r' || (c) == ' ')
  254. char *buf = strdup(imagename), *key, *val, *end, *next;
  255. int i;
  256. /* User passed arguments from image name */
  257. static const char *media = "";
  258. static const char *nandinfo = "";
  259. static const char *lk = "";
  260. key = buf;
  261. while (key) {
  262. next = strchr(key, ';');
  263. if (next)
  264. *next = 0;
  265. val = strchr(key, '=');
  266. if (val) {
  267. *val++ = 0;
  268. /* Trim key */
  269. while (is_blank_char(*key))
  270. key++;
  271. end = key + strlen(key) - 1;
  272. while ((end >= key) && is_blank_char(*end))
  273. end--;
  274. end++;
  275. if (is_blank_char(*end))
  276. *end = 0;
  277. /* Trim value */
  278. while (is_blank_char(*val))
  279. val++;
  280. end = val + strlen(val) - 1;
  281. while ((end >= val) && is_blank_char(*end))
  282. end--;
  283. end++;
  284. if (is_blank_char(*end))
  285. *end = 0;
  286. /* record user passed arguments */
  287. if (!strcmp(key, "media"))
  288. media = val;
  289. if (!strcmp(key, "nandinfo"))
  290. nandinfo = val;
  291. if (!strcmp(key, "lk"))
  292. lk = val;
  293. if (!strcmp(key, "lkname"))
  294. snprintf(lk_name, sizeof(lk_name), "%s", val);
  295. }
  296. if (next)
  297. key = next + 1;
  298. else
  299. break;
  300. }
  301. /* if user specified LK image header, skip following checks */
  302. if (lk && lk[0] == '1') {
  303. use_lk_hdr = 1;
  304. free(buf);
  305. return 0;
  306. }
  307. /* parse media type */
  308. for (i = 0; i < ARRAY_SIZE(brom_images); i++) {
  309. if (!strcmp(brom_images[i].name, media)) {
  310. hdr_media = brom_images[i].type;
  311. break;
  312. }
  313. }
  314. /* parse nand header type */
  315. for (i = 0; i < ARRAY_SIZE(nand_headers); i++) {
  316. if (!strcmp(nand_headers[i].name, nandinfo)) {
  317. hdr_nand = nand_headers[i].data;
  318. break;
  319. }
  320. }
  321. free(buf);
  322. if (hdr_media == BRLYT_TYPE_INVALID) {
  323. fprintf(stderr, "Error: media type is invalid or missing.\n");
  324. fprintf(stderr, " Please specify -n \"media=<type>\"\n");
  325. return -EINVAL;
  326. }
  327. if ((hdr_media == BRLYT_TYPE_NAND || hdr_media == BRLYT_TYPE_SNAND) &&
  328. !hdr_nand) {
  329. fprintf(stderr, "Error: nand info is invalid or missing.\n");
  330. fprintf(stderr, " Please specify -n \"media=%s;"
  331. "nandinfo=<info>\"\n", media);
  332. return -EINVAL;
  333. }
  334. return 0;
  335. }
  336. static int mtk_image_check_params(struct image_tool_params *params)
  337. {
  338. if (!params->addr) {
  339. fprintf(stderr, "Error: Load Address must be set.\n");
  340. return -EINVAL;
  341. }
  342. if (!params->imagename) {
  343. fprintf(stderr, "Error: Image Name must be set.\n");
  344. return -EINVAL;
  345. }
  346. return mtk_brom_parse_imagename(params->imagename);
  347. }
  348. static int mtk_image_vrec_header(struct image_tool_params *params,
  349. struct image_type_params *tparams)
  350. {
  351. if (use_lk_hdr) {
  352. tparams->header_size = sizeof(union lk_hdr);
  353. tparams->hdr = &hdr_tmp;
  354. memset(&hdr_tmp, 0xff, tparams->header_size);
  355. return 0;
  356. }
  357. if (hdr_media == BRLYT_TYPE_NAND || hdr_media == BRLYT_TYPE_SNAND)
  358. tparams->header_size = 2 * le16_to_cpu(hdr_nand->pagesize);
  359. else
  360. tparams->header_size = sizeof(struct gen_device_header);
  361. tparams->header_size += sizeof(struct gfh_header);
  362. tparams->hdr = &hdr_tmp;
  363. memset(&hdr_tmp, 0xff, tparams->header_size);
  364. return SHA256_SUM_LEN;
  365. }
  366. static int mtk_image_verify_gen_header(const uint8_t *ptr, int print)
  367. {
  368. union gen_boot_header *gbh = (union gen_boot_header *)ptr;
  369. struct brom_layout_header *bh;
  370. struct gfh_header *gfh;
  371. const char *bootmedia;
  372. if (!strcmp(gbh->name, SF_BOOT_NAME))
  373. bootmedia = "Serial NOR";
  374. else if (!strcmp(gbh->name, EMMC_BOOT_NAME))
  375. bootmedia = "eMMC";
  376. else if (!strcmp(gbh->name, SDMMC_BOOT_NAME))
  377. bootmedia = "SD/MMC";
  378. else
  379. return -1;
  380. if (print)
  381. printf("Boot Media: %s\n", bootmedia);
  382. if (le32_to_cpu(gbh->version) != 1 ||
  383. le32_to_cpu(gbh->size) != sizeof(union gen_boot_header))
  384. return -1;
  385. bh = (struct brom_layout_header *)(ptr + le32_to_cpu(gbh->size));
  386. if (strcmp(bh->name, BRLYT_NAME))
  387. return -1;
  388. if (le32_to_cpu(bh->magic) != BRLYT_MAGIC ||
  389. (le32_to_cpu(bh->type) != BRLYT_TYPE_NOR &&
  390. le32_to_cpu(bh->type) != BRLYT_TYPE_EMMC &&
  391. le32_to_cpu(bh->type) != BRLYT_TYPE_SDMMC))
  392. return -1;
  393. gfh = (struct gfh_header *)(ptr + le32_to_cpu(bh->header_size));
  394. if (strcmp(gfh->file_info.name, GFH_FILE_INFO_NAME))
  395. return -1;
  396. if (le32_to_cpu(gfh->file_info.flash_type) != GFH_FLASH_TYPE_GEN)
  397. return -1;
  398. if (print)
  399. printf("Load Address: %08x\n",
  400. le32_to_cpu(gfh->file_info.load_addr) +
  401. le32_to_cpu(gfh->file_info.jump_offset));
  402. return 0;
  403. }
  404. static int mtk_image_verify_nand_header(const uint8_t *ptr, int print)
  405. {
  406. union nand_boot_header *nh = (union nand_boot_header *)ptr;
  407. struct brom_layout_header *bh;
  408. struct gfh_header *gfh;
  409. const char *bootmedia;
  410. if (strncmp(nh->version, NAND_BOOT_VERSION, sizeof(nh->version)) ||
  411. strcmp(nh->id, NAND_BOOT_ID))
  412. return -1;
  413. bh = (struct brom_layout_header *)(ptr + le16_to_cpu(nh->pagesize));
  414. if (strcmp(bh->name, BRLYT_NAME))
  415. return -1;
  416. if (le32_to_cpu(bh->magic) != BRLYT_MAGIC) {
  417. return -1;
  418. } else {
  419. if (le32_to_cpu(bh->type) == BRLYT_TYPE_NAND)
  420. bootmedia = "Parallel NAND";
  421. else if (le32_to_cpu(bh->type) == BRLYT_TYPE_SNAND)
  422. bootmedia = "Serial NAND";
  423. else
  424. return -1;
  425. }
  426. if (print) {
  427. printf("Boot Media: %s\n", bootmedia);
  428. if (le32_to_cpu(bh->type) == BRLYT_TYPE_NAND) {
  429. uint64_t capacity =
  430. (uint64_t)le16_to_cpu(nh->numblocks) *
  431. (uint64_t)le16_to_cpu(nh->pages_of_block) *
  432. (uint64_t)le16_to_cpu(nh->pagesize) * 8;
  433. printf("Capacity: %dGb\n",
  434. (uint32_t)(capacity >> 30));
  435. }
  436. if (le16_to_cpu(nh->pagesize) >= 1024)
  437. printf("Page Size: %dKB\n",
  438. le16_to_cpu(nh->pagesize) >> 10);
  439. else
  440. printf("Page Size: %dB\n",
  441. le16_to_cpu(nh->pagesize));
  442. printf("Spare Size: %dB\n", le16_to_cpu(nh->oobsize));
  443. }
  444. gfh = (struct gfh_header *)(ptr + 2 * le16_to_cpu(nh->pagesize));
  445. if (strcmp(gfh->file_info.name, GFH_FILE_INFO_NAME))
  446. return -1;
  447. if (le32_to_cpu(gfh->file_info.flash_type) != GFH_FLASH_TYPE_NAND)
  448. return -1;
  449. if (print)
  450. printf("Load Address: %08x\n",
  451. le32_to_cpu(gfh->file_info.load_addr) +
  452. le32_to_cpu(gfh->file_info.jump_offset));
  453. return 0;
  454. }
  455. static int mtk_image_verify_header(unsigned char *ptr, int image_size,
  456. struct image_tool_params *params)
  457. {
  458. union lk_hdr *lk = (union lk_hdr *)ptr;
  459. /* nothing to verify for LK image header */
  460. if (le32_to_cpu(lk->magic) == LK_PART_MAGIC)
  461. return 0;
  462. if (!strcmp((char *)ptr, NAND_BOOT_NAME))
  463. return mtk_image_verify_nand_header(ptr, 0);
  464. else
  465. return mtk_image_verify_gen_header(ptr, 0);
  466. return -1;
  467. }
  468. static void mtk_image_print_header(const void *ptr)
  469. {
  470. union lk_hdr *lk = (union lk_hdr *)ptr;
  471. if (le32_to_cpu(lk->magic) == LK_PART_MAGIC) {
  472. printf("Image Type: MediaTek LK Image\n");
  473. printf("Load Address: %08x\n", le32_to_cpu(lk->loadaddr));
  474. return;
  475. }
  476. printf("Image Type: MediaTek BootROM Loadable Image\n");
  477. if (!strcmp((char *)ptr, NAND_BOOT_NAME))
  478. mtk_image_verify_nand_header(ptr, 1);
  479. else
  480. mtk_image_verify_gen_header(ptr, 1);
  481. }
  482. static void put_brom_layout_header(struct brom_layout_header *hdr, int type)
  483. {
  484. strncpy(hdr->name, BRLYT_NAME, sizeof(hdr->name));
  485. hdr->version = cpu_to_le32(1);
  486. hdr->magic = cpu_to_le32(BRLYT_MAGIC);
  487. hdr->type = cpu_to_le32(type);
  488. }
  489. static void put_ghf_common_header(struct gfh_common_header *gfh, int size,
  490. int type, int ver)
  491. {
  492. memcpy(gfh->magic, GFH_HEADER_MAGIC, sizeof(gfh->magic));
  493. gfh->version = ver;
  494. gfh->size = cpu_to_le16(size);
  495. gfh->type = cpu_to_le16(type);
  496. }
  497. static void put_ghf_header(struct gfh_header *gfh, int file_size,
  498. int dev_hdr_size, int load_addr, int flash_type)
  499. {
  500. memset(gfh, 0, sizeof(struct gfh_header));
  501. /* GFH_FILE_INFO header */
  502. put_ghf_common_header(&gfh->file_info.gfh, sizeof(gfh->file_info),
  503. GFH_TYPE_FILE_INFO, 1);
  504. strncpy(gfh->file_info.name, GFH_FILE_INFO_NAME,
  505. sizeof(gfh->file_info.name));
  506. gfh->file_info.unused = cpu_to_le32(1);
  507. gfh->file_info.file_type = cpu_to_le16(1);
  508. gfh->file_info.flash_type = flash_type;
  509. gfh->file_info.sig_type = GFH_SIG_TYPE_SHA256;
  510. gfh->file_info.load_addr = cpu_to_le32(load_addr - sizeof(*gfh));
  511. gfh->file_info.total_size = cpu_to_le32(file_size - dev_hdr_size);
  512. gfh->file_info.max_size = cpu_to_le32(file_size);
  513. gfh->file_info.hdr_size = sizeof(*gfh);
  514. gfh->file_info.sig_size = SHA256_SUM_LEN;
  515. gfh->file_info.jump_offset = sizeof(*gfh);
  516. gfh->file_info.processed = cpu_to_le32(1);
  517. /* GFH_BL_INFO header */
  518. put_ghf_common_header(&gfh->bl_info.gfh, sizeof(gfh->bl_info),
  519. GFH_TYPE_BL_INFO, 1);
  520. gfh->bl_info.attr = cpu_to_le32(1);
  521. /* GFH_BROM_CFG header */
  522. put_ghf_common_header(&gfh->brom_cfg.gfh, sizeof(gfh->brom_cfg),
  523. GFH_TYPE_BROM_CFG, 3);
  524. gfh->brom_cfg.cfg_bits = cpu_to_le32(
  525. GFH_BROM_CFG_USBDL_AUTO_DETECT_DIS |
  526. GFH_BROM_CFG_USBDL_BY_KCOL0_TIMEOUT_EN |
  527. GFH_BROM_CFG_USBDL_BY_FLAG_TIMEOUT_EN);
  528. gfh->brom_cfg.usbdl_by_kcol0_timeout_ms = cpu_to_le32(5000);
  529. /* GFH_BL_SEC_KEY header */
  530. put_ghf_common_header(&gfh->bl_sec_key.gfh, sizeof(gfh->bl_sec_key),
  531. GFH_TYPE_BL_SEC_KEY, 1);
  532. /* GFH_ANTI_CLONE header */
  533. put_ghf_common_header(&gfh->anti_clone.gfh, sizeof(gfh->anti_clone),
  534. GFH_TYPE_ANTI_CLONE, 1);
  535. gfh->anti_clone.ac_offset = cpu_to_le32(0x10);
  536. gfh->anti_clone.ac_len = cpu_to_le32(0x80);
  537. /* GFH_BROM_SEC_CFG header */
  538. put_ghf_common_header(&gfh->brom_sec_cfg.gfh,
  539. sizeof(gfh->brom_sec_cfg),
  540. GFH_TYPE_BROM_SEC_CFG, 1);
  541. gfh->brom_sec_cfg.cfg_bits =
  542. cpu_to_le32(BROM_SEC_CFG_JTAG_EN | BROM_SEC_CFG_UART_EN);
  543. }
  544. static void put_hash(uint8_t *buff, int size)
  545. {
  546. sha256_context ctx;
  547. sha256_starts(&ctx);
  548. sha256_update(&ctx, buff, size);
  549. sha256_finish(&ctx, buff + size);
  550. }
  551. static void mtk_image_set_gen_header(void *ptr, off_t filesize,
  552. uint32_t loadaddr)
  553. {
  554. struct gen_device_header *hdr = (struct gen_device_header *)ptr;
  555. struct gfh_header *gfh;
  556. const char *bootname = NULL;
  557. if (hdr_media == BRLYT_TYPE_NOR)
  558. bootname = SF_BOOT_NAME;
  559. else if (hdr_media == BRLYT_TYPE_EMMC)
  560. bootname = EMMC_BOOT_NAME;
  561. else if (hdr_media == BRLYT_TYPE_SDMMC)
  562. bootname = SDMMC_BOOT_NAME;
  563. /* Generic device header */
  564. snprintf(hdr->boot.name, sizeof(hdr->boot.name), "%s", bootname);
  565. hdr->boot.version = cpu_to_le32(1);
  566. hdr->boot.size = cpu_to_le32(sizeof(hdr->boot));
  567. /* BRLYT header */
  568. put_brom_layout_header(&hdr->brlyt, hdr_media);
  569. hdr->brlyt.header_size = cpu_to_le32(sizeof(struct gen_device_header));
  570. hdr->brlyt.total_size = cpu_to_le32(filesize);
  571. hdr->brlyt.header_size_2 = hdr->brlyt.header_size;
  572. hdr->brlyt.total_size_2 = hdr->brlyt.total_size;
  573. /* GFH header */
  574. gfh = (struct gfh_header *)(ptr + sizeof(struct gen_device_header));
  575. put_ghf_header(gfh, filesize, sizeof(struct gen_device_header),
  576. loadaddr, GFH_FLASH_TYPE_GEN);
  577. /* Generate SHA256 hash */
  578. put_hash((uint8_t *)gfh,
  579. filesize - sizeof(struct gen_device_header) - SHA256_SUM_LEN);
  580. }
  581. static void mtk_image_set_nand_header(void *ptr, off_t filesize,
  582. uint32_t loadaddr)
  583. {
  584. union nand_boot_header *nh = (union nand_boot_header *)ptr;
  585. struct brom_layout_header *brlyt;
  586. struct gfh_header *gfh;
  587. uint32_t payload_pages;
  588. int i;
  589. /* NAND device header, repeat 4 times */
  590. for (i = 0; i < 4; i++)
  591. memcpy(nh + i, hdr_nand, sizeof(union nand_boot_header));
  592. /* BRLYT header */
  593. payload_pages = (filesize + le16_to_cpu(hdr_nand->pagesize) - 1) /
  594. le16_to_cpu(hdr_nand->pagesize);
  595. brlyt = (struct brom_layout_header *)
  596. (ptr + le16_to_cpu(hdr_nand->pagesize));
  597. put_brom_layout_header(brlyt, hdr_media);
  598. brlyt->header_size = cpu_to_le32(2);
  599. brlyt->total_size = cpu_to_le32(payload_pages);
  600. brlyt->header_size_2 = brlyt->header_size;
  601. brlyt->total_size_2 = brlyt->total_size;
  602. brlyt->unused = cpu_to_le32(1);
  603. /* GFH header */
  604. gfh = (struct gfh_header *)(ptr + 2 * le16_to_cpu(hdr_nand->pagesize));
  605. put_ghf_header(gfh, filesize, 2 * le16_to_cpu(hdr_nand->pagesize),
  606. loadaddr, GFH_FLASH_TYPE_NAND);
  607. /* Generate SHA256 hash */
  608. put_hash((uint8_t *)gfh,
  609. filesize - 2 * le16_to_cpu(hdr_nand->pagesize) - SHA256_SUM_LEN);
  610. }
  611. static void mtk_image_set_header(void *ptr, struct stat *sbuf, int ifd,
  612. struct image_tool_params *params)
  613. {
  614. union lk_hdr *lk = (union lk_hdr *)ptr;
  615. if (use_lk_hdr) {
  616. lk->magic = cpu_to_le32(LK_PART_MAGIC);
  617. lk->size = cpu_to_le32(sbuf->st_size - sizeof(union lk_hdr));
  618. lk->loadaddr = cpu_to_le32(params->addr);
  619. lk->mode = 0xffffffff; /* must be non-zero */
  620. memset(lk->name, 0, sizeof(lk->name));
  621. strncpy(lk->name, lk_name, sizeof(lk->name));
  622. return;
  623. }
  624. if (hdr_media == BRLYT_TYPE_NAND || hdr_media == BRLYT_TYPE_SNAND)
  625. mtk_image_set_nand_header(ptr, sbuf->st_size, params->addr);
  626. else
  627. mtk_image_set_gen_header(ptr, sbuf->st_size, params->addr);
  628. }
  629. U_BOOT_IMAGE_TYPE(
  630. mtk_image,
  631. "MediaTek BootROM Loadable Image support",
  632. 0,
  633. NULL,
  634. mtk_image_check_params,
  635. mtk_image_verify_header,
  636. mtk_image_print_header,
  637. mtk_image_set_header,
  638. NULL,
  639. mtk_image_check_image_types,
  640. NULL,
  641. mtk_image_vrec_header
  642. );