rkcommon.c 6.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236
  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. uint32_t first_insn;
  45. };
  46. /**
  47. * struct spl_info - spl info for each chip
  48. *
  49. * @imagename: Image name(passed by "mkimage -n")
  50. * @spl_hdr: Boot ROM requires a 4-bytes spl header
  51. * @spl_size: Spl size(include extra 4-bytes spl header)
  52. * @spl_rc4: RC4 encode the SPL binary (same key as header)
  53. * @spl_aarch64: Pad the header with an AArch64 'nop's to 8-bytes
  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. const bool spl_aarch64;
  61. };
  62. static struct spl_info spl_infos[] = {
  63. { "rk3036", "RK30", 0x1000, false, false },
  64. { "rk3188", "RK31", 0x8000 - 0x800, true, false },
  65. { "rk3288", "RK32", 0x8000, false, false },
  66. { "rk3399", "RK33", 0x20000, false, true },
  67. };
  68. static unsigned char rc4_key[16] = {
  69. 124, 78, 3, 4, 85, 5, 9, 7,
  70. 45, 44, 123, 56, 23, 13, 23, 17
  71. };
  72. static struct spl_info *rkcommon_get_spl_info(char *imagename)
  73. {
  74. int i;
  75. for (i = 0; i < ARRAY_SIZE(spl_infos); i++)
  76. if (!strncmp(imagename, spl_infos[i].imagename, 6))
  77. return spl_infos + i;
  78. return NULL;
  79. }
  80. int rkcommon_check_params(struct image_tool_params *params)
  81. {
  82. int i;
  83. if (rkcommon_get_spl_info(params->imagename) != NULL)
  84. return 0;
  85. fprintf(stderr, "ERROR: imagename (%s) is not supported!\n",
  86. strlen(params->imagename) > 0 ? params->imagename : "NULL");
  87. fprintf(stderr, "Available imagename:");
  88. for (i = 0; i < ARRAY_SIZE(spl_infos); i++)
  89. fprintf(stderr, "\t%s", spl_infos[i].imagename);
  90. fprintf(stderr, "\n");
  91. return -1;
  92. }
  93. const char *rkcommon_get_spl_hdr(struct image_tool_params *params)
  94. {
  95. struct spl_info *info = rkcommon_get_spl_info(params->imagename);
  96. /*
  97. * info would not be NULL, because of we checked params before.
  98. */
  99. return info->spl_hdr;
  100. }
  101. const bool rkcommon_get_spl_hdr_padto8(struct image_tool_params *params)
  102. {
  103. struct spl_info *info = rkcommon_get_spl_info(params->imagename);
  104. /*
  105. * info would not be NULL, because of we checked params before.
  106. */
  107. return info->spl_aarch64;
  108. }
  109. int rkcommon_get_spl_size(struct image_tool_params *params)
  110. {
  111. struct spl_info *info = rkcommon_get_spl_info(params->imagename);
  112. /*
  113. * info would not be NULL, because of we checked params before.
  114. */
  115. return info->spl_size;
  116. }
  117. bool rkcommon_need_rc4_spl(struct image_tool_params *params)
  118. {
  119. struct spl_info *info = rkcommon_get_spl_info(params->imagename);
  120. /*
  121. * info would not be NULL, because of we checked params before.
  122. */
  123. return info->spl_rc4;
  124. }
  125. static void rkcommon_set_header0(void *buf, uint file_size,
  126. struct image_tool_params *params)
  127. {
  128. struct header0_info *hdr = buf;
  129. memset(buf, '\0', RK_INIT_OFFSET * RK_BLK_SIZE);
  130. hdr->signature = RK_SIGNATURE;
  131. hdr->disable_rc4 = !rkcommon_need_rc4_spl(params);
  132. hdr->init_offset = RK_INIT_OFFSET;
  133. hdr->init_size = (file_size + RK_BLK_SIZE - 1) / RK_BLK_SIZE;
  134. hdr->init_size = (hdr->init_size + 3) & ~3;
  135. hdr->init_boot_size = hdr->init_size + RK_MAX_BOOT_SIZE / RK_BLK_SIZE;
  136. rc4_encode(buf, RK_BLK_SIZE, rc4_key);
  137. }
  138. int rkcommon_set_header(void *buf, uint file_size,
  139. struct image_tool_params *params)
  140. {
  141. struct header1_info *hdr = buf + RK_SPL_HDR_START;
  142. if (file_size > rkcommon_get_spl_size(params))
  143. return -ENOSPC;
  144. rkcommon_set_header0(buf, file_size, params);
  145. /* Set up the SPL name and add the AArch64 'nop' padding, if needed */
  146. memcpy(&hdr->magic, rkcommon_get_spl_hdr(params), RK_SPL_HDR_SIZE);
  147. /*
  148. * Pad the 4-byte header to 8-bytes using an AArch64 'nop'.
  149. * Note that AArch64 insns are always encoded as little-endian.
  150. */
  151. if (rkcommon_get_spl_hdr_padto8(params))
  152. hdr->first_insn = cpu_to_le32(0xd503201f);
  153. if (rkcommon_need_rc4_spl(params))
  154. rkcommon_rc4_encode_spl(buf, RK_SPL_HDR_START,
  155. params->file_size - RK_SPL_HDR_START);
  156. return 0;
  157. }
  158. void rkcommon_rc4_encode_spl(void *buf, unsigned int offset, unsigned int size)
  159. {
  160. unsigned int remaining = size;
  161. while (remaining > 0) {
  162. int step = (remaining > RK_BLK_SIZE) ? RK_BLK_SIZE : remaining;
  163. rc4_encode(buf + offset, step, rc4_key);
  164. offset += RK_BLK_SIZE;
  165. remaining -= step;
  166. }
  167. }
  168. void rkcommon_vrec_header(struct image_tool_params *params,
  169. struct image_type_params *tparams)
  170. {
  171. /*
  172. * The SPL image looks as follows:
  173. *
  174. * 0x0 header0 (see rkcommon.c)
  175. * 0x800 spl_name ('RK30', ..., 'RK33')
  176. * 0x804 first instruction to be executed
  177. * (image start for AArch32, 'nop' for AArch64))
  178. * 0x808 second instruction to be executed
  179. * (image start for AArch64)
  180. *
  181. * For AArch64 (ARMv8) payloads, we receive an input file that
  182. * needs to start on an 8-byte boundary (natural alignment), so
  183. * we need to put a NOP at 0x804.
  184. *
  185. * Depending on this, the header is either 0x804 or 0x808 bytes
  186. * in length.
  187. */
  188. if (rkcommon_get_spl_hdr_padto8(params))
  189. tparams->header_size = RK_SPL_HDR_START + 8;
  190. else
  191. tparams->header_size = RK_SPL_HDR_START + 4;
  192. /* Allocate, clear and install the header */
  193. tparams->hdr = malloc(tparams->header_size);
  194. memset(tparams->hdr, 0, tparams->header_size);
  195. tparams->header_size = tparams->header_size;
  196. }