rkcommon.c 9.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378
  1. // SPDX-License-Identifier: GPL-2.0+
  2. /*
  3. * (C) Copyright 2015 Google, Inc
  4. * Written by Simon Glass <sjg@chromium.org>
  5. *
  6. * (C) 2017 Theobroma Systems Design und Consulting GmbH
  7. *
  8. * Helper functions for Rockchip images
  9. */
  10. #include "imagetool.h"
  11. #include <image.h>
  12. #include <rc4.h>
  13. #include "mkimage.h"
  14. #include "rkcommon.h"
  15. #define DIV_ROUND_UP(n, d) (((n) + (d) - 1) / (d))
  16. enum {
  17. RK_SIGNATURE = 0x0ff0aa55,
  18. };
  19. /**
  20. * struct header0_info - header block for boot ROM
  21. *
  22. * This is stored at SD card block 64 (where each block is 512 bytes, or at
  23. * the start of SPI flash. It is encoded with RC4.
  24. *
  25. * @signature: Signature (must be RKSD_SIGNATURE)
  26. * @disable_rc4: 0 to use rc4 for boot image, 1 to use plain binary
  27. * @init_offset: Offset in blocks of the SPL code from this header
  28. * block. E.g. 4 means 2KB after the start of this header.
  29. * Other fields are not used by U-Boot
  30. */
  31. struct header0_info {
  32. uint32_t signature;
  33. uint8_t reserved[4];
  34. uint32_t disable_rc4;
  35. uint16_t init_offset;
  36. uint8_t reserved1[492];
  37. uint16_t init_size;
  38. uint16_t init_boot_size;
  39. uint8_t reserved2[2];
  40. };
  41. /**
  42. * struct header1_info
  43. */
  44. struct header1_info {
  45. uint32_t magic;
  46. };
  47. /**
  48. * struct spl_info - spl info for each chip
  49. *
  50. * @imagename: Image name(passed by "mkimage -n")
  51. * @spl_hdr: Boot ROM requires a 4-bytes spl header
  52. * @spl_size: Spl size(include extra 4-bytes spl header)
  53. * @spl_rc4: RC4 encode the SPL binary (same key as header)
  54. */
  55. struct spl_info {
  56. const char *imagename;
  57. const char *spl_hdr;
  58. const uint32_t spl_size;
  59. const bool spl_rc4;
  60. };
  61. static struct spl_info spl_infos[] = {
  62. { "rk3036", "RK30", 0x1000, false },
  63. { "rk3128", "RK31", 0x1800, false },
  64. { "rk3188", "RK31", 0x8000 - 0x800, true },
  65. { "rk322x", "RK32", 0x8000 - 0x1000, false },
  66. { "rk3288", "RK32", 0x8000, false },
  67. { "rk3328", "RK32", 0x8000 - 0x1000, false },
  68. { "rk3368", "RK33", 0x8000 - 0x1000, false },
  69. { "rk3399", "RK33", 0x30000 - 0x2000, false },
  70. { "rv1108", "RK11", 0x1800, false },
  71. };
  72. static unsigned char rc4_key[16] = {
  73. 124, 78, 3, 4, 85, 5, 9, 7,
  74. 45, 44, 123, 56, 23, 13, 23, 17
  75. };
  76. static struct spl_info *rkcommon_get_spl_info(char *imagename)
  77. {
  78. int i;
  79. if (!imagename)
  80. return NULL;
  81. for (i = 0; i < ARRAY_SIZE(spl_infos); i++)
  82. if (!strncmp(imagename, spl_infos[i].imagename, 6))
  83. return spl_infos + i;
  84. return NULL;
  85. }
  86. int rkcommon_check_params(struct image_tool_params *params)
  87. {
  88. int i;
  89. if (rkcommon_get_spl_info(params->imagename) != NULL)
  90. return EXIT_SUCCESS;
  91. /*
  92. * If this is a operation (list or extract), the don't require
  93. * imagename to be set.
  94. */
  95. if (params->lflag || params->iflag)
  96. return EXIT_SUCCESS;
  97. fprintf(stderr, "ERROR: imagename (%s) is not supported!\n",
  98. params->imagename ? params->imagename : "NULL");
  99. fprintf(stderr, "Available imagename:");
  100. for (i = 0; i < ARRAY_SIZE(spl_infos); i++)
  101. fprintf(stderr, "\t%s", spl_infos[i].imagename);
  102. fprintf(stderr, "\n");
  103. return EXIT_FAILURE;
  104. }
  105. const char *rkcommon_get_spl_hdr(struct image_tool_params *params)
  106. {
  107. struct spl_info *info = rkcommon_get_spl_info(params->imagename);
  108. /*
  109. * info would not be NULL, because of we checked params before.
  110. */
  111. return info->spl_hdr;
  112. }
  113. int rkcommon_get_spl_size(struct image_tool_params *params)
  114. {
  115. struct spl_info *info = rkcommon_get_spl_info(params->imagename);
  116. /*
  117. * info would not be NULL, because of we checked params before.
  118. */
  119. return info->spl_size;
  120. }
  121. bool rkcommon_need_rc4_spl(struct image_tool_params *params)
  122. {
  123. struct spl_info *info = rkcommon_get_spl_info(params->imagename);
  124. /*
  125. * info would not be NULL, because of we checked params before.
  126. */
  127. return info->spl_rc4;
  128. }
  129. static void rkcommon_set_header0(void *buf, uint file_size,
  130. struct image_tool_params *params)
  131. {
  132. struct header0_info *hdr = buf;
  133. memset(buf, '\0', RK_INIT_OFFSET * RK_BLK_SIZE);
  134. hdr->signature = RK_SIGNATURE;
  135. hdr->disable_rc4 = !rkcommon_need_rc4_spl(params);
  136. hdr->init_offset = RK_INIT_OFFSET;
  137. hdr->init_size = DIV_ROUND_UP(file_size, RK_BLK_SIZE);
  138. /*
  139. * The init_size has to be a multiple of 4 blocks (i.e. of 2K)
  140. * or the BootROM will not boot the image.
  141. *
  142. * Note: To verify that this is not a legacy constraint, we
  143. * rechecked this against the RK3399 BootROM.
  144. */
  145. hdr->init_size = ROUND(hdr->init_size, 4);
  146. /*
  147. * init_boot_size needs to be set, as it is read by the BootROM
  148. * to determine the size of the next-stage bootloader (e.g. U-Boot
  149. * proper), when used with the back-to-bootrom functionality.
  150. *
  151. * see https://lists.denx.de/pipermail/u-boot/2017-May/293267.html
  152. * for a more detailed explanation by Andy Yan
  153. */
  154. hdr->init_boot_size = hdr->init_size + RK_MAX_BOOT_SIZE / RK_BLK_SIZE;
  155. rc4_encode(buf, RK_BLK_SIZE, rc4_key);
  156. }
  157. int rkcommon_set_header(void *buf, uint file_size,
  158. struct image_tool_params *params)
  159. {
  160. struct header1_info *hdr = buf + RK_SPL_HDR_START;
  161. if (file_size > rkcommon_get_spl_size(params))
  162. return -ENOSPC;
  163. rkcommon_set_header0(buf, file_size, params);
  164. /* Set up the SPL name (i.e. copy spl_hdr over) */
  165. memcpy(&hdr->magic, rkcommon_get_spl_hdr(params), RK_SPL_HDR_SIZE);
  166. if (rkcommon_need_rc4_spl(params))
  167. rkcommon_rc4_encode_spl(buf, RK_SPL_HDR_START,
  168. params->file_size - RK_SPL_HDR_START);
  169. return 0;
  170. }
  171. static inline unsigned rkcommon_offset_to_spi(unsigned offset)
  172. {
  173. /*
  174. * While SD/MMC images use a flat addressing, SPI images are padded
  175. * to use the first 2K of every 4K sector only.
  176. */
  177. return ((offset & ~0x7ff) << 1) + (offset & 0x7ff);
  178. }
  179. static int rkcommon_parse_header(const void *buf, struct header0_info *header0,
  180. struct spl_info **spl_info)
  181. {
  182. unsigned hdr1_offset;
  183. struct header1_info *hdr1_sdmmc, *hdr1_spi;
  184. int i;
  185. if (spl_info)
  186. *spl_info = NULL;
  187. /*
  188. * The first header (hdr0) is always RC4 encoded, so try to decrypt
  189. * with the well-known key.
  190. */
  191. memcpy((void *)header0, buf, sizeof(struct header0_info));
  192. rc4_encode((void *)header0, sizeof(struct header0_info), rc4_key);
  193. if (header0->signature != RK_SIGNATURE)
  194. return -EPROTO;
  195. /* We don't support RC4 encoded image payloads here, yet... */
  196. if (header0->disable_rc4 == 0)
  197. return -ENOSYS;
  198. hdr1_offset = header0->init_offset * RK_BLK_SIZE;
  199. hdr1_sdmmc = (struct header1_info *)(buf + hdr1_offset);
  200. hdr1_spi = (struct header1_info *)(buf +
  201. rkcommon_offset_to_spi(hdr1_offset));
  202. for (i = 0; i < ARRAY_SIZE(spl_infos); i++) {
  203. if (!memcmp(&hdr1_sdmmc->magic, spl_infos[i].spl_hdr, 4)) {
  204. if (spl_info)
  205. *spl_info = &spl_infos[i];
  206. return IH_TYPE_RKSD;
  207. } else if (!memcmp(&hdr1_spi->magic, spl_infos[i].spl_hdr, 4)) {
  208. if (spl_info)
  209. *spl_info = &spl_infos[i];
  210. return IH_TYPE_RKSPI;
  211. }
  212. }
  213. return -1;
  214. }
  215. int rkcommon_verify_header(unsigned char *buf, int size,
  216. struct image_tool_params *params)
  217. {
  218. struct header0_info header0;
  219. struct spl_info *img_spl_info, *spl_info;
  220. int ret;
  221. ret = rkcommon_parse_header(buf, &header0, &img_spl_info);
  222. /* If this is the (unimplemented) RC4 case, then rewrite the result */
  223. if (ret == -ENOSYS)
  224. return 0;
  225. if (ret < 0)
  226. return ret;
  227. /*
  228. * If no 'imagename' is specified via the commandline (e.g. if this is
  229. * 'dumpimage -l' w/o any further constraints), we accept any spl_info.
  230. */
  231. if (params->imagename == NULL)
  232. return 0;
  233. /* Match the 'imagename' against the 'spl_hdr' found */
  234. spl_info = rkcommon_get_spl_info(params->imagename);
  235. if (spl_info && img_spl_info)
  236. return strcmp(spl_info->spl_hdr, img_spl_info->spl_hdr);
  237. return -ENOENT;
  238. }
  239. void rkcommon_print_header(const void *buf)
  240. {
  241. struct header0_info header0;
  242. struct spl_info *spl_info;
  243. uint8_t image_type;
  244. int ret;
  245. ret = rkcommon_parse_header(buf, &header0, &spl_info);
  246. /* If this is the (unimplemented) RC4 case, then fail silently */
  247. if (ret == -ENOSYS)
  248. return;
  249. if (ret < 0) {
  250. fprintf(stderr, "Error: image verification failed\n");
  251. return;
  252. }
  253. image_type = ret;
  254. printf("Image Type: Rockchip %s (%s) boot image\n",
  255. spl_info->spl_hdr,
  256. (image_type == IH_TYPE_RKSD) ? "SD/MMC" : "SPI");
  257. printf("Data Size: %d bytes\n", header0.init_size * RK_BLK_SIZE);
  258. }
  259. void rkcommon_rc4_encode_spl(void *buf, unsigned int offset, unsigned int size)
  260. {
  261. unsigned int remaining = size;
  262. while (remaining > 0) {
  263. int step = (remaining > RK_BLK_SIZE) ? RK_BLK_SIZE : remaining;
  264. rc4_encode(buf + offset, step, rc4_key);
  265. offset += RK_BLK_SIZE;
  266. remaining -= step;
  267. }
  268. }
  269. int rkcommon_vrec_header(struct image_tool_params *params,
  270. struct image_type_params *tparams,
  271. unsigned int alignment)
  272. {
  273. unsigned int unpadded_size;
  274. unsigned int padded_size;
  275. /*
  276. * The SPL image looks as follows:
  277. *
  278. * 0x0 header0 (see rkcommon.c)
  279. * 0x800 spl_name ('RK30', ..., 'RK33')
  280. * (start of the payload for AArch64 payloads: we expect the
  281. * first 4 bytes to be available for overwriting with our
  282. * spl_name)
  283. * 0x804 first instruction to be executed
  284. * (start of the image/payload for 32bit payloads)
  285. *
  286. * For AArch64 (ARMv8) payloads, natural alignment (8-bytes) is
  287. * required for its sections (so the image we receive needs to
  288. * have the first 4 bytes reserved for the spl_name). Reserving
  289. * these 4 bytes is done using the BOOT0_HOOK infrastructure.
  290. *
  291. * The header is always at 0x800 (as we now use a payload
  292. * prepadded using the boot0 hook for all targets): the first
  293. * 4 bytes of these images can safely be overwritten using the
  294. * boot magic.
  295. */
  296. tparams->header_size = RK_SPL_HDR_START;
  297. /* Allocate, clear and install the header */
  298. tparams->hdr = malloc(tparams->header_size);
  299. if (!tparams->hdr)
  300. return -ENOMEM;
  301. memset(tparams->hdr, 0, tparams->header_size);
  302. /*
  303. * If someone passed in 0 for the alignment, we'd better handle
  304. * it correctly...
  305. */
  306. if (!alignment)
  307. alignment = 1;
  308. unpadded_size = tparams->header_size + params->file_size;
  309. padded_size = ROUND(unpadded_size, alignment);
  310. return padded_size - unpadded_size;
  311. }