]> git.sur5r.net Git - u-boot/blob - tools/rkcommon.c
rockchip: mkimage: pad the header to 8-bytes (using a 'nop') for RK3399
[u-boot] / tools / rkcommon.c
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         uint32_t first_insn;
49 };
50
51 /**
52  * 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)
57  * @spl_rc4:            RC4 encode the SPL binary (same key as header)
58  * @spl_aarch64:        Pad the header with an AArch64 'nop's to 8-bytes
59  */
60
61 struct spl_info {
62         const char *imagename;
63         const char *spl_hdr;
64         const uint32_t spl_size;
65         const bool spl_rc4;
66         const bool spl_aarch64;
67 };
68
69 static struct spl_info spl_infos[] = {
70         { "rk3036", "RK30", 0x1000, false, false },
71         { "rk3188", "RK31", 0x8000 - 0x800, true, false },
72         { "rk3288", "RK32", 0x8000, false, false },
73         { "rk3399", "RK33", 0x20000, false, true },
74 };
75
76 static 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
81 static 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
92 int 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
110 const 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
120 const 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
130 int 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
140 bool 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
150 static void rkcommon_set_header0(void *buf, uint file_size,
151                                  struct image_tool_params *params)
152 {
153         struct header0_info *hdr = buf;
154
155         memset(buf, '\0', RK_INIT_OFFSET * RK_BLK_SIZE);
156         hdr->signature = RK_SIGNATURE;
157         hdr->disable_rc4 = !rkcommon_need_rc4_spl(params);
158         hdr->init_offset = RK_INIT_OFFSET;
159
160         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;
163
164         rc4_encode(buf, RK_BLK_SIZE, rc4_key);
165 }
166
167 int 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);
190
191         return 0;
192 }
193
194 void 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 }
206
207 void 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 }