rkcommon.c 5.8 KB

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