blob: a09dbf19adf07cac01fb013bcec07d0889fb8541 [file] [log] [blame]
Tom Rini83d290c2018-05-06 17:58:06 -04001/* SPDX-License-Identifier: GPL-2.0+ */
Prafulla Wadaskaraa0c7a82009-09-07 15:05:02 +05302/*
3 * (C) Copyright 2008
4 * Marvell Semiconductor <www.marvell.com>
5 * Written-by: Prafulla Wadaskar <prafulla@marvell.com>
Prafulla Wadaskaraa0c7a82009-09-07 15:05:02 +05306 */
7
8#ifndef _KWBIMAGE_H_
9#define _KWBIMAGE_H_
10
Reinhard Pfaua8840dc2015-11-29 15:48:25 +010011#include <compiler.h>
Prafulla Wadaskaraa0c7a82009-09-07 15:05:02 +053012#include <stdint.h>
13
Pali Rohára107c612021-07-23 11:14:14 +020014#ifdef __GNUC__
15#define __packed __attribute((packed))
16#else
17#define __packed
18#endif
19
Prafulla Wadaskaraa0c7a82009-09-07 15:05:02 +053020#define KWBIMAGE_MAX_CONFIG ((0x1dc - 0x20)/sizeof(struct reg_config))
21#define MAX_TEMPBUF_LEN 32
22
23/* NAND ECC Mode */
24#define IBR_HDR_ECC_DEFAULT 0x00
25#define IBR_HDR_ECC_FORCED_HAMMING 0x01
Wolfgang Denk0cf207e2021-09-27 17:42:39 +020026#define IBR_HDR_ECC_FORCED_RS 0x02
27#define IBR_HDR_ECC_DISABLED 0x03
Prafulla Wadaskaraa0c7a82009-09-07 15:05:02 +053028
29/* Boot Type - block ID */
30#define IBR_HDR_I2C_ID 0x4D
31#define IBR_HDR_SPI_ID 0x5A
32#define IBR_HDR_NAND_ID 0x8B
33#define IBR_HDR_SATA_ID 0x78
34#define IBR_HDR_PEX_ID 0x9C
35#define IBR_HDR_UART_ID 0x69
Marek Behúnbd487ce2021-07-23 11:13:58 +020036#define IBR_HDR_SDIO_ID 0xAE
Marek Behún35fd1002021-07-23 11:14:05 +020037#define IBR_DEF_ATTRIB 0x00
Prafulla Wadaskaraa0c7a82009-09-07 15:05:02 +053038
Stefan Roesee29f1db2015-09-29 09:19:59 +020039/* Structure of the main header, version 0 (Kirkwood, Dove) */
40struct main_hdr_v0 {
Baruch Siach37d108b2017-07-04 20:23:39 +030041 uint8_t blockid; /* 0x0 */
42 uint8_t nandeccmode; /* 0x1 */
43 uint16_t nandpagesize; /* 0x2-0x3 */
44 uint32_t blocksize; /* 0x4-0x7 */
45 uint32_t rsvd1; /* 0x8-0xB */
46 uint32_t srcaddr; /* 0xC-0xF */
47 uint32_t destaddr; /* 0x10-0x13 */
48 uint32_t execaddr; /* 0x14-0x17 */
49 uint8_t satapiomode; /* 0x18 */
50 uint8_t rsvd3; /* 0x19 */
51 uint16_t ddrinitdelay; /* 0x1A-0x1B */
52 uint16_t rsvd2; /* 0x1C-0x1D */
53 uint8_t ext; /* 0x1E */
54 uint8_t checksum; /* 0x1F */
Pali Rohára107c612021-07-23 11:14:14 +020055} __packed;
Stefan Roesee29f1db2015-09-29 09:19:59 +020056
57struct ext_hdr_v0_reg {
58 uint32_t raddr;
59 uint32_t rdata;
Pali Rohára107c612021-07-23 11:14:14 +020060} __packed;
Stefan Roesee29f1db2015-09-29 09:19:59 +020061
62#define EXT_HDR_V0_REG_COUNT ((0x1dc - 0x20) / sizeof(struct ext_hdr_v0_reg))
63
64struct ext_hdr_v0 {
65 uint32_t offset;
66 uint8_t reserved[0x20 - sizeof(uint32_t)];
67 struct ext_hdr_v0_reg rcfg[EXT_HDR_V0_REG_COUNT];
68 uint8_t reserved2[7];
69 uint8_t checksum;
Pali Rohára107c612021-07-23 11:14:14 +020070} __packed;
Stefan Roesee29f1db2015-09-29 09:19:59 +020071
72struct kwb_header {
73 struct main_hdr_v0 kwb_hdr;
74 struct ext_hdr_v0 kwb_exthdr;
Pali Rohára107c612021-07-23 11:14:14 +020075} __packed;
Stefan Roesee29f1db2015-09-29 09:19:59 +020076
Baruch Siached727412017-07-04 20:23:38 +030077/* Structure of the main header, version 1 (Armada 370/38x/XP) */
Stefan Roesee29f1db2015-09-29 09:19:59 +020078struct main_hdr_v1 {
Baruch Siach37d108b2017-07-04 20:23:39 +030079 uint8_t blockid; /* 0x0 */
80 uint8_t flags; /* 0x1 */
81 uint16_t reserved2; /* 0x2-0x3 */
82 uint32_t blocksize; /* 0x4-0x7 */
83 uint8_t version; /* 0x8 */
84 uint8_t headersz_msb; /* 0x9 */
85 uint16_t headersz_lsb; /* 0xA-0xB */
86 uint32_t srcaddr; /* 0xC-0xF */
87 uint32_t destaddr; /* 0x10-0x13 */
88 uint32_t execaddr; /* 0x14-0x17 */
89 uint8_t options; /* 0x18 */
90 uint8_t nandblocksize; /* 0x19 */
91 uint8_t nandbadblklocation; /* 0x1A */
92 uint8_t reserved4; /* 0x1B */
93 uint16_t reserved5; /* 0x1C-0x1D */
94 uint8_t ext; /* 0x1E */
95 uint8_t checksum; /* 0x1F */
Pali Rohára107c612021-07-23 11:14:14 +020096} __packed;
Stefan Roesee29f1db2015-09-29 09:19:59 +020097
98/*
Chris Packham4bdb5472016-11-09 22:07:45 +130099 * Main header options
100 */
101#define MAIN_HDR_V1_OPT_BAUD_DEFAULT 0
102#define MAIN_HDR_V1_OPT_BAUD_2400 0x1
103#define MAIN_HDR_V1_OPT_BAUD_4800 0x2
104#define MAIN_HDR_V1_OPT_BAUD_9600 0x3
105#define MAIN_HDR_V1_OPT_BAUD_19200 0x4
106#define MAIN_HDR_V1_OPT_BAUD_38400 0x5
107#define MAIN_HDR_V1_OPT_BAUD_57600 0x6
108#define MAIN_HDR_V1_OPT_BAUD_115200 0x7
109
110/*
Stefan Roesee29f1db2015-09-29 09:19:59 +0200111 * Header for the optional headers, version 1 (Armada 370, Armada XP)
112 */
113struct opt_hdr_v1 {
114 uint8_t headertype;
115 uint8_t headersz_msb;
116 uint16_t headersz_lsb;
117 char data[0];
Pali Rohára107c612021-07-23 11:14:14 +0200118} __packed;
Stefan Roesee29f1db2015-09-29 09:19:59 +0200119
120/*
Mario Sixa1b6b0a2017-01-11 16:01:00 +0100121 * Public Key data in DER format
122 */
123struct pubkey_der_v1 {
124 uint8_t key[524];
Pali Rohára107c612021-07-23 11:14:14 +0200125} __packed;
Mario Sixa1b6b0a2017-01-11 16:01:00 +0100126
127/*
128 * Signature (RSA 2048)
129 */
130struct sig_v1 {
131 uint8_t sig[256];
Pali Rohára107c612021-07-23 11:14:14 +0200132} __packed;
Mario Sixa1b6b0a2017-01-11 16:01:00 +0100133
134/*
135 * Structure of secure header (Armada 38x)
136 */
137struct secure_hdr_v1 {
138 uint8_t headertype; /* 0x0 */
139 uint8_t headersz_msb; /* 0x1 */
140 uint16_t headersz_lsb; /* 0x2 - 0x3 */
141 uint32_t reserved1; /* 0x4 - 0x7 */
142 struct pubkey_der_v1 kak; /* 0x8 - 0x213 */
143 uint8_t jtag_delay; /* 0x214 */
144 uint8_t reserved2; /* 0x215 */
145 uint16_t reserved3; /* 0x216 - 0x217 */
146 uint32_t boxid; /* 0x218 - 0x21B */
147 uint32_t flashid; /* 0x21C - 0x21F */
148 struct sig_v1 hdrsig; /* 0x220 - 0x31F */
149 struct sig_v1 imgsig; /* 0x320 - 0x41F */
150 struct pubkey_der_v1 csk[16]; /* 0x420 - 0x24DF */
151 struct sig_v1 csksig; /* 0x24E0 - 0x25DF */
152 uint8_t next; /* 0x25E0 */
153 uint8_t reserved4; /* 0x25E1 */
154 uint16_t reserved5; /* 0x25E2 - 0x25E3 */
Pali Rohára107c612021-07-23 11:14:14 +0200155} __packed;
Mario Sixa1b6b0a2017-01-11 16:01:00 +0100156
157/*
Pali Rohár02ba70a2021-07-23 11:14:11 +0200158 * Structure of register set
159 */
160struct register_set_hdr_v1 {
161 uint8_t headertype; /* 0x0 */
162 uint8_t headersz_msb; /* 0x1 */
163 uint16_t headersz_lsb; /* 0x2 - 0x3 */
164 union {
165 struct {
166 uint32_t address; /* 0x4+8*N - 0x7+8*N */
167 uint32_t value; /* 0x8+8*N - 0xB+8*N */
Pali Rohára107c612021-07-23 11:14:14 +0200168 } __packed entry;
Pali Rohár02ba70a2021-07-23 11:14:11 +0200169 struct {
170 uint8_t next; /* 0xC+8*N */
171 uint8_t delay; /* 0xD+8*N */
172 uint16_t reserved; /* 0xE+8*N - 0xF+8*N */
Pali Rohára107c612021-07-23 11:14:14 +0200173 } __packed last_entry;
Pali Rohár02ba70a2021-07-23 11:14:11 +0200174 } data[];
Pali Rohára107c612021-07-23 11:14:14 +0200175} __packed;
Pali Rohár02ba70a2021-07-23 11:14:11 +0200176
177/*
178 * Value 0 in register_set_hdr_v1 delay field is special.
179 * Instead of delay it setup SDRAM Controller.
180 */
181#define REGISTER_SET_HDR_OPT_DELAY_SDRAM_SETUP 0
182#define REGISTER_SET_HDR_OPT_DELAY_MS(val) ((val) ?: 1)
183
184/*
Stefan Roesee29f1db2015-09-29 09:19:59 +0200185 * Various values for the opt_hdr_v1->headertype field, describing the
186 * different types of optional headers. The "secure" header contains
187 * informations related to secure boot (encryption keys, etc.). The
188 * "binary" header contains ARM binary code to be executed prior to
189 * executing the main payload (usually the bootloader). This is
190 * typically used to execute DDR3 training code. The "register" header
191 * allows to describe a set of (address, value) tuples that are
192 * generally used to configure the DRAM controller.
193 */
194#define OPT_HDR_V1_SECURE_TYPE 0x1
195#define OPT_HDR_V1_BINARY_TYPE 0x2
196#define OPT_HDR_V1_REGISTER_TYPE 0x3
197
Pali Rohár2ef87f72021-09-24 23:06:48 +0200198#define KWBHEADER_V0_SIZE(hdr) \
199 (((hdr)->ext & 0x1) ? sizeof(struct kwb_header) : \
200 sizeof(struct main_hdr_v0))
201
Stefan Roesee29f1db2015-09-29 09:19:59 +0200202#define KWBHEADER_V1_SIZE(hdr) \
Reinhard Pfaua8840dc2015-11-29 15:48:25 +0100203 (((hdr)->headersz_msb << 16) | le16_to_cpu((hdr)->headersz_lsb))
Stefan Roesee29f1db2015-09-29 09:19:59 +0200204
Prafulla Wadaskaraa0c7a82009-09-07 15:05:02 +0530205enum kwbimage_cmd {
206 CMD_INVALID,
207 CMD_BOOT_FROM,
208 CMD_NAND_ECC_MODE,
209 CMD_NAND_PAGE_SIZE,
210 CMD_SATA_PIO_MODE,
211 CMD_DDR_INIT_DELAY,
212 CMD_DATA
213};
214
215enum kwbimage_cmd_types {
216 CFG_INVALID = -1,
217 CFG_COMMAND,
218 CFG_DATA0,
219 CFG_DATA1
220};
221
Prafulla Wadaskaraa0c7a82009-09-07 15:05:02 +0530222/*
223 * functions
224 */
225void init_kwb_image_type (void);
226
Stefan Roesee29f1db2015-09-29 09:19:59 +0200227/*
228 * Byte 8 of the image header contains the version number. In the v0
229 * header, byte 8 was reserved, and always set to 0. In the v1 header,
230 * byte 8 has been changed to a proper field, set to 1.
231 */
Pali Rohár2ef87f72021-09-24 23:06:48 +0200232static inline unsigned int image_version(const void *header)
Stefan Roesee29f1db2015-09-29 09:19:59 +0200233{
Pali Rohár2ef87f72021-09-24 23:06:48 +0200234 const unsigned char *ptr = header;
Stefan Roesee29f1db2015-09-29 09:19:59 +0200235 return ptr[8];
236}
237
Marek Behún732c9302021-08-18 00:59:15 +0200238static inline uint32_t opt_hdr_v1_size(const struct opt_hdr_v1 *ohdr)
239{
240 return (ohdr->headersz_msb << 16) | le16_to_cpu(ohdr->headersz_lsb);
241}
242
243static inline int opt_hdr_v1_valid_size(const struct opt_hdr_v1 *ohdr,
244 const void *mhdr_end)
245{
246 uint32_t ohdr_size;
247
248 if ((void *)(ohdr + 1) > mhdr_end)
249 return 0;
250
251 ohdr_size = opt_hdr_v1_size(ohdr);
252 if (ohdr_size < 8 || (void *)((uint8_t *)ohdr + ohdr_size) > mhdr_end)
253 return 0;
254
255 return 1;
256}
257
258static inline struct opt_hdr_v1 *opt_hdr_v1_first(void *img) {
259 struct main_hdr_v1 *mhdr;
260
261 if (image_version(img) != 1)
262 return NULL;
263
264 mhdr = img;
265 if (mhdr->ext & 0x1)
266 return (struct opt_hdr_v1 *)(mhdr + 1);
267 else
268 return NULL;
269}
270
271static inline uint8_t *opt_hdr_v1_ext(struct opt_hdr_v1 *cur)
272{
273 uint32_t size = opt_hdr_v1_size(cur);
274
275 return (uint8_t *)cur + size - 4;
276}
277
278static inline struct opt_hdr_v1 *_opt_hdr_v1_next(struct opt_hdr_v1 *cur)
279{
280 return (struct opt_hdr_v1 *)((uint8_t *)cur + opt_hdr_v1_size(cur));
281}
282
283static inline struct opt_hdr_v1 *opt_hdr_v1_next(struct opt_hdr_v1 *cur)
284{
285 if (*opt_hdr_v1_ext(cur) & 0x1)
286 return _opt_hdr_v1_next(cur);
287 else
288 return NULL;
289}
290
291#define for_each_opt_hdr_v1(ohdr, img) \
292 for ((ohdr) = opt_hdr_v1_first((img)); \
293 (ohdr) != NULL; \
294 (ohdr) = opt_hdr_v1_next((ohdr)))
295
Prafulla Wadaskaraa0c7a82009-09-07 15:05:02 +0530296#endif /* _KWBIMAGE_H_ */