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