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