mtk_image.c 22 KB

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