rkcommon.c 6.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252
  1. /*
  2. * (C) Copyright 2015 Google, Inc
  3. * Written by Simon Glass <sjg@chromium.org>
  4. *
  5. * SPDX-License-Identifier: GPL-2.0+
  6. *
  7. * Helper functions for Rockchip images
  8. */
  9. #include "imagetool.h"
  10. #include <image.h>
  11. #include <rc4.h>
  12. #include "mkimage.h"
  13. #include "rkcommon.h"
  14. enum {
  15. RK_SIGNATURE = 0x0ff0aa55,
  16. };
  17. /**
  18. * struct header0_info - header block for boot ROM
  19. *
  20. * This is stored at SD card block 64 (where each block is 512 bytes, or at
  21. * the start of SPI flash. It is encoded with RC4.
  22. *
  23. * @signature: Signature (must be RKSD_SIGNATURE)
  24. * @disable_rc4: 0 to use rc4 for boot image, 1 to use plain binary
  25. * @init_offset: Offset in blocks of the SPL code from this header
  26. * block. E.g. 4 means 2KB after the start of this header.
  27. * Other fields are not used by U-Boot
  28. */
  29. struct header0_info {
  30. uint32_t signature;
  31. uint8_t reserved[4];
  32. uint32_t disable_rc4;
  33. uint16_t init_offset;
  34. uint8_t reserved1[492];
  35. uint16_t init_size;
  36. uint16_t init_boot_size;
  37. uint8_t reserved2[2];
  38. };
  39. /**
  40. * struct header1_info
  41. */
  42. struct header1_info {
  43. uint32_t magic;
  44. };
  45. /**
  46. * struct spl_info - spl info for each chip
  47. *
  48. * @imagename: Image name(passed by "mkimage -n")
  49. * @spl_hdr: Boot ROM requires a 4-bytes spl header
  50. * @spl_size: Spl size(include extra 4-bytes spl header)
  51. * @spl_rc4: RC4 encode the SPL binary (same key as header)
  52. * @spl_boot0: A new-style (ARM_SOC_BOOT0_HOOK) image that should
  53. * have the boot magic (e.g. 'RK33') written to its first
  54. * word.
  55. */
  56. struct spl_info {
  57. const char *imagename;
  58. const char *spl_hdr;
  59. const uint32_t spl_size;
  60. const bool spl_rc4;
  61. const bool spl_boot0;
  62. };
  63. static struct spl_info spl_infos[] = {
  64. { "rk3036", "RK30", 0x1000, false, false },
  65. { "rk3188", "RK31", 0x8000 - 0x800, true, false },
  66. { "rk3288", "RK32", 0x8000, false, false },
  67. { "rk3328", "RK32", 0x8000 - 0x1000, false, false },
  68. { "rk3399", "RK33", 0x20000, false, true },
  69. };
  70. static unsigned char rc4_key[16] = {
  71. 124, 78, 3, 4, 85, 5, 9, 7,
  72. 45, 44, 123, 56, 23, 13, 23, 17
  73. };
  74. static struct spl_info *rkcommon_get_spl_info(char *imagename)
  75. {
  76. int i;
  77. for (i = 0; i < ARRAY_SIZE(spl_infos); i++)
  78. if (!strncmp(imagename, spl_infos[i].imagename, 6))
  79. return spl_infos + i;
  80. return NULL;
  81. }
  82. int rkcommon_check_params(struct image_tool_params *params)
  83. {
  84. int i;
  85. if (rkcommon_get_spl_info(params->imagename) != NULL)
  86. return 0;
  87. fprintf(stderr, "ERROR: imagename (%s) is not supported!\n",
  88. strlen(params->imagename) > 0 ? params->imagename : "NULL");
  89. fprintf(stderr, "Available imagename:");
  90. for (i = 0; i < ARRAY_SIZE(spl_infos); i++)
  91. fprintf(stderr, "\t%s", spl_infos[i].imagename);
  92. fprintf(stderr, "\n");
  93. return -1;
  94. }
  95. const char *rkcommon_get_spl_hdr(struct image_tool_params *params)
  96. {
  97. struct spl_info *info = rkcommon_get_spl_info(params->imagename);
  98. /*
  99. * info would not be NULL, because of we checked params before.
  100. */
  101. return info->spl_hdr;
  102. }
  103. int rkcommon_get_spl_size(struct image_tool_params *params)
  104. {
  105. struct spl_info *info = rkcommon_get_spl_info(params->imagename);
  106. /*
  107. * info would not be NULL, because of we checked params before.
  108. */
  109. return info->spl_size;
  110. }
  111. bool rkcommon_need_rc4_spl(struct image_tool_params *params)
  112. {
  113. struct spl_info *info = rkcommon_get_spl_info(params->imagename);
  114. /*
  115. * info would not be NULL, because of we checked params before.
  116. */
  117. return info->spl_rc4;
  118. }
  119. bool rkcommon_spl_is_boot0(struct image_tool_params *params)
  120. {
  121. struct spl_info *info = rkcommon_get_spl_info(params->imagename);
  122. /*
  123. * info would not be NULL, because of we checked params before.
  124. */
  125. return info->spl_boot0;
  126. }
  127. static void rkcommon_set_header0(void *buf, uint file_size,
  128. struct image_tool_params *params)
  129. {
  130. struct header0_info *hdr = buf;
  131. memset(buf, '\0', RK_INIT_OFFSET * RK_BLK_SIZE);
  132. hdr->signature = RK_SIGNATURE;
  133. hdr->disable_rc4 = !rkcommon_need_rc4_spl(params);
  134. hdr->init_offset = RK_INIT_OFFSET;
  135. hdr->init_size = (file_size + RK_BLK_SIZE - 1) / RK_BLK_SIZE;
  136. hdr->init_size = (hdr->init_size + 3) & ~3;
  137. hdr->init_boot_size = hdr->init_size + RK_MAX_BOOT_SIZE / RK_BLK_SIZE;
  138. rc4_encode(buf, RK_BLK_SIZE, rc4_key);
  139. }
  140. int rkcommon_set_header(void *buf, uint file_size,
  141. struct image_tool_params *params)
  142. {
  143. struct header1_info *hdr = buf + RK_SPL_HDR_START;
  144. if (file_size > rkcommon_get_spl_size(params))
  145. return -ENOSPC;
  146. rkcommon_set_header0(buf, file_size, params);
  147. /* Set up the SPL name */
  148. memcpy(&hdr->magic, rkcommon_get_spl_hdr(params), RK_SPL_HDR_SIZE);
  149. if (rkcommon_need_rc4_spl(params))
  150. rkcommon_rc4_encode_spl(buf, RK_SPL_HDR_START,
  151. params->file_size - RK_SPL_HDR_START);
  152. return 0;
  153. }
  154. void rkcommon_rc4_encode_spl(void *buf, unsigned int offset, unsigned int size)
  155. {
  156. unsigned int remaining = size;
  157. while (remaining > 0) {
  158. int step = (remaining > RK_BLK_SIZE) ? RK_BLK_SIZE : remaining;
  159. rc4_encode(buf + offset, step, rc4_key);
  160. offset += RK_BLK_SIZE;
  161. remaining -= step;
  162. }
  163. }
  164. int rkcommon_vrec_header(struct image_tool_params *params,
  165. struct image_type_params *tparams,
  166. unsigned int alignment)
  167. {
  168. unsigned int unpadded_size;
  169. unsigned int padded_size;
  170. /*
  171. * The SPL image looks as follows:
  172. *
  173. * 0x0 header0 (see rkcommon.c)
  174. * 0x800 spl_name ('RK30', ..., 'RK33')
  175. * (start of the payload for AArch64 payloads: we expect the
  176. * first 4 bytes to be available for overwriting with our
  177. * spl_name)
  178. * 0x804 first instruction to be executed
  179. * (start of the image/payload for 32bit payloads)
  180. *
  181. * For AArch64 (ARMv8) payloads, natural alignment (8-bytes) is
  182. * required for its sections (so the image we receive needs to
  183. * have the first 4 bytes reserved for the spl_name). Reserving
  184. * these 4 bytes is done using the BOOT0_HOOK infrastructure.
  185. *
  186. * Depending on this, the header is either 0x800 (if this is a
  187. * 'boot0'-style payload, which has reserved 4 bytes at the
  188. * beginning for the 'spl_name' and expects us to overwrite
  189. * its first 4 bytes) or 0x804 bytes in length.
  190. */
  191. if (rkcommon_spl_is_boot0(params))
  192. tparams->header_size = RK_SPL_HDR_START;
  193. else
  194. tparams->header_size = RK_SPL_HDR_START + 4;
  195. /* Allocate, clear and install the header */
  196. tparams->hdr = malloc(tparams->header_size);
  197. memset(tparams->hdr, 0, tparams->header_size);
  198. tparams->header_size = tparams->header_size;
  199. /*
  200. * If someone passed in 0 for the alignment, we'd better handle
  201. * it correctly...
  202. */
  203. if (!alignment)
  204. alignment = 1;
  205. unpadded_size = tparams->header_size + params->file_size;
  206. padded_size = ROUND(unpadded_size, alignment);
  207. return padded_size - unpadded_size;
  208. }