Fork of the vendor (Boundary Devices) u-boot for Reform 2, with minor tweaks. The goal is to migrate to mainstream u-boot or barebox ASAP. The main impediment so far is the 4GB RAM config.
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
This repo is archived. You can view files and clone it, but cannot push or open issues/pull-requests.
 
 
 
 
 
 

399 lines
10 KiB

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