blob: 1ea072b8d4da500c791f1e53578510086254391d [file] [log] [blame]
Simon Glassa131c1f2015-08-30 16:55:24 -06001/*
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
16enum {
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
Jeffy Chen36413392015-11-17 14:20:30 +080028 * @init_offset: Offset in blocks of the SPL code from this header
Simon Glassa131c1f2015-08-30 16:55:24 -060029 * block. E.g. 4 means 2KB after the start of this header.
30 * Other fields are not used by U-Boot
31 */
32struct header0_info {
33 uint32_t signature;
34 uint8_t reserved[4];
35 uint32_t disable_rc4;
Jeffy Chen36413392015-11-17 14:20:30 +080036 uint16_t init_offset;
37 uint8_t reserved1[492];
38 uint16_t init_size;
39 uint16_t init_boot_size;
Simon Glassa131c1f2015-08-30 16:55:24 -060040 uint8_t reserved2[2];
41};
42
Jeffy Chen7bf274b2015-11-27 12:07:17 +080043/**
Philipp Tomsich111bcc42017-03-15 12:08:43 +010044 * struct header1_info
45 */
46struct header1_info {
47 uint32_t magic;
48 uint32_t first_insn;
49};
50
51/**
Jeffy Chen7bf274b2015-11-27 12:07:17 +080052 * struct spl_info - spl info for each chip
53 *
54 * @imagename: Image name(passed by "mkimage -n")
55 * @spl_hdr: Boot ROM requires a 4-bytes spl header
56 * @spl_size: Spl size(include extra 4-bytes spl header)
Heiko Stübnercfbcdad2017-02-18 19:46:27 +010057 * @spl_rc4: RC4 encode the SPL binary (same key as header)
Philipp Tomsich111bcc42017-03-15 12:08:43 +010058 * @spl_aarch64: Pad the header with an AArch64 'nop's to 8-bytes
Jeffy Chen7bf274b2015-11-27 12:07:17 +080059 */
Philipp Tomsich111bcc42017-03-15 12:08:43 +010060
Jeffy Chen7bf274b2015-11-27 12:07:17 +080061struct spl_info {
62 const char *imagename;
63 const char *spl_hdr;
64 const uint32_t spl_size;
Heiko Stübnercfbcdad2017-02-18 19:46:27 +010065 const bool spl_rc4;
Philipp Tomsich111bcc42017-03-15 12:08:43 +010066 const bool spl_aarch64;
Jeffy Chen7bf274b2015-11-27 12:07:17 +080067};
68
69static struct spl_info spl_infos[] = {
Philipp Tomsich111bcc42017-03-15 12:08:43 +010070 { "rk3036", "RK30", 0x1000, false, false },
71 { "rk3188", "RK31", 0x8000 - 0x800, true, false },
72 { "rk3288", "RK32", 0x8000, false, false },
73 { "rk3399", "RK33", 0x20000, false, true },
Jeffy Chen7bf274b2015-11-27 12:07:17 +080074};
75
Simon Glassa131c1f2015-08-30 16:55:24 -060076static unsigned char rc4_key[16] = {
77 124, 78, 3, 4, 85, 5, 9, 7,
78 45, 44, 123, 56, 23, 13, 23, 17
79};
80
Jeffy Chen7bf274b2015-11-27 12:07:17 +080081static struct spl_info *rkcommon_get_spl_info(char *imagename)
82{
83 int i;
84
85 for (i = 0; i < ARRAY_SIZE(spl_infos); i++)
86 if (!strncmp(imagename, spl_infos[i].imagename, 6))
87 return spl_infos + i;
88
89 return NULL;
90}
91
92int rkcommon_check_params(struct image_tool_params *params)
93{
94 int i;
95
96 if (rkcommon_get_spl_info(params->imagename) != NULL)
97 return 0;
98
99 fprintf(stderr, "ERROR: imagename (%s) is not supported!\n",
100 strlen(params->imagename) > 0 ? params->imagename : "NULL");
101
102 fprintf(stderr, "Available imagename:");
103 for (i = 0; i < ARRAY_SIZE(spl_infos); i++)
104 fprintf(stderr, "\t%s", spl_infos[i].imagename);
105 fprintf(stderr, "\n");
106
107 return -1;
108}
109
110const char *rkcommon_get_spl_hdr(struct image_tool_params *params)
111{
112 struct spl_info *info = rkcommon_get_spl_info(params->imagename);
113
114 /*
115 * info would not be NULL, because of we checked params before.
116 */
117 return info->spl_hdr;
118}
119
Philipp Tomsich111bcc42017-03-15 12:08:43 +0100120const bool rkcommon_get_spl_hdr_padto8(struct image_tool_params *params)
121{
122 struct spl_info *info = rkcommon_get_spl_info(params->imagename);
123
124 /*
125 * info would not be NULL, because of we checked params before.
126 */
127 return info->spl_aarch64;
128}
129
Jeffy Chen7bf274b2015-11-27 12:07:17 +0800130int rkcommon_get_spl_size(struct image_tool_params *params)
131{
132 struct spl_info *info = rkcommon_get_spl_info(params->imagename);
133
134 /*
135 * info would not be NULL, because of we checked params before.
136 */
137 return info->spl_size;
138}
139
Heiko Stübnercfbcdad2017-02-18 19:46:27 +0100140bool rkcommon_need_rc4_spl(struct image_tool_params *params)
141{
142 struct spl_info *info = rkcommon_get_spl_info(params->imagename);
143
144 /*
145 * info would not be NULL, because of we checked params before.
146 */
147 return info->spl_rc4;
148}
149
Philipp Tomsich111bcc42017-03-15 12:08:43 +0100150static void rkcommon_set_header0(void *buf, uint file_size,
151 struct image_tool_params *params)
Simon Glassa131c1f2015-08-30 16:55:24 -0600152{
Philipp Tomsich111bcc42017-03-15 12:08:43 +0100153 struct header0_info *hdr = buf;
Simon Glassa131c1f2015-08-30 16:55:24 -0600154
Philipp Tomsich111bcc42017-03-15 12:08:43 +0100155 memset(buf, '\0', RK_INIT_OFFSET * RK_BLK_SIZE);
Simon Glassa131c1f2015-08-30 16:55:24 -0600156 hdr->signature = RK_SIGNATURE;
Heiko Stübnercfbcdad2017-02-18 19:46:27 +0100157 hdr->disable_rc4 = !rkcommon_need_rc4_spl(params);
Jeffy Chen36413392015-11-17 14:20:30 +0800158 hdr->init_offset = RK_INIT_OFFSET;
Simon Glassa131c1f2015-08-30 16:55:24 -0600159
Jeffy Chen36413392015-11-17 14:20:30 +0800160 hdr->init_size = (file_size + RK_BLK_SIZE - 1) / RK_BLK_SIZE;
161 hdr->init_size = (hdr->init_size + 3) & ~3;
162 hdr->init_boot_size = hdr->init_size + RK_MAX_BOOT_SIZE / RK_BLK_SIZE;
Simon Glassa131c1f2015-08-30 16:55:24 -0600163
164 rc4_encode(buf, RK_BLK_SIZE, rc4_key);
Philipp Tomsich111bcc42017-03-15 12:08:43 +0100165}
166
167int rkcommon_set_header(void *buf, uint file_size,
168 struct image_tool_params *params)
169{
170 struct header1_info *hdr = buf + RK_SPL_HDR_START;
171
172 if (file_size > rkcommon_get_spl_size(params))
173 return -ENOSPC;
174
175 rkcommon_set_header0(buf, file_size, params);
176
177 /* Set up the SPL name and add the AArch64 'nop' padding, if needed */
178 memcpy(&hdr->magic, rkcommon_get_spl_hdr(params), RK_SPL_HDR_SIZE);
179
180 /*
181 * Pad the 4-byte header to 8-bytes using an AArch64 'nop'.
182 * Note that AArch64 insns are always encoded as little-endian.
183 */
184 if (rkcommon_get_spl_hdr_padto8(params))
185 hdr->first_insn = cpu_to_le32(0xd503201f);
186
187 if (rkcommon_need_rc4_spl(params))
188 rkcommon_rc4_encode_spl(buf, RK_SPL_HDR_START,
189 params->file_size - RK_SPL_HDR_START);
Simon Glassa131c1f2015-08-30 16:55:24 -0600190
191 return 0;
192}
Heiko Stübnercfbcdad2017-02-18 19:46:27 +0100193
194void rkcommon_rc4_encode_spl(void *buf, unsigned int offset, unsigned int size)
195{
196 unsigned int remaining = size;
197
198 while (remaining > 0) {
199 int step = (remaining > RK_BLK_SIZE) ? RK_BLK_SIZE : remaining;
200
201 rc4_encode(buf + offset, step, rc4_key);
202 offset += RK_BLK_SIZE;
203 remaining -= step;
204 }
205}
Philipp Tomsich111bcc42017-03-15 12:08:43 +0100206
207void rkcommon_vrec_header(struct image_tool_params *params,
208 struct image_type_params *tparams)
209{
210 /*
211 * The SPL image looks as follows:
212 *
213 * 0x0 header0 (see rkcommon.c)
214 * 0x800 spl_name ('RK30', ..., 'RK33')
215 * 0x804 first instruction to be executed
216 * (image start for AArch32, 'nop' for AArch64))
217 * 0x808 second instruction to be executed
218 * (image start for AArch64)
219 *
220 * For AArch64 (ARMv8) payloads, we receive an input file that
221 * needs to start on an 8-byte boundary (natural alignment), so
222 * we need to put a NOP at 0x804.
223 *
224 * Depending on this, the header is either 0x804 or 0x808 bytes
225 * in length.
226 */
227 if (rkcommon_get_spl_hdr_padto8(params))
228 tparams->header_size = RK_SPL_HDR_START + 8;
229 else
230 tparams->header_size = RK_SPL_HDR_START + 4;
231
232 /* Allocate, clear and install the header */
233 tparams->hdr = malloc(tparams->header_size);
234 memset(tparams->hdr, 0, tparams->header_size);
235 tparams->header_size = tparams->header_size;
236}