blob: e1fa2e4e2157fd4e50bc1669f72aa030f6e2b993 [file] [log] [blame]
Tom Rini10e47792018-05-06 17:58:06 -04001/* SPDX-License-Identifier: GPL-2.0+ */
Prafulla Wadaskar07329412009-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 Wadaskar07329412009-09-07 15:05:02 +05306 */
7
8#ifndef _KWBIMAGE_H_
9#define _KWBIMAGE_H_
10
Reinhard Pfau3efeaae2015-11-29 15:48:25 +010011#include <compiler.h>
Prafulla Wadaskar07329412009-09-07 15:05:02 +053012#include <stdint.h>
13
Philippe Reynes77a1d5b2024-12-19 14:05:46 +010014#include <linux/compiler_attributes.h>
Pali Rohárf01adfd2021-07-23 11:14:14 +020015
Prafulla Wadaskar07329412009-09-07 15:05:02 +053016#define KWBIMAGE_MAX_CONFIG ((0x1dc - 0x20)/sizeof(struct reg_config))
17#define MAX_TEMPBUF_LEN 32
18
19/* NAND ECC Mode */
20#define IBR_HDR_ECC_DEFAULT 0x00
21#define IBR_HDR_ECC_FORCED_HAMMING 0x01
Wolfgang Denk62fb2b42021-09-27 17:42:39 +020022#define IBR_HDR_ECC_FORCED_RS 0x02
23#define IBR_HDR_ECC_DISABLED 0x03
Prafulla Wadaskar07329412009-09-07 15:05:02 +053024
25/* Boot Type - block ID */
26#define IBR_HDR_I2C_ID 0x4D
27#define IBR_HDR_SPI_ID 0x5A
28#define IBR_HDR_NAND_ID 0x8B
29#define IBR_HDR_SATA_ID 0x78
30#define IBR_HDR_PEX_ID 0x9C
31#define IBR_HDR_UART_ID 0x69
Marek Behún696dfa42021-07-23 11:13:58 +020032#define IBR_HDR_SDIO_ID 0xAE
Marek Behúnabf9a8d2021-07-23 11:14:05 +020033#define IBR_DEF_ATTRIB 0x00
Prafulla Wadaskar07329412009-09-07 15:05:02 +053034
Stefan Roesec74165d2015-09-29 09:19:59 +020035/* Structure of the main header, version 0 (Kirkwood, Dove) */
36struct main_hdr_v0 {
Baruch Siach3ef2e4e2017-07-04 20:23:39 +030037 uint8_t blockid; /* 0x0 */
38 uint8_t nandeccmode; /* 0x1 */
39 uint16_t nandpagesize; /* 0x2-0x3 */
40 uint32_t blocksize; /* 0x4-0x7 */
Pali Rohárc990f942021-11-08 18:12:42 +010041 uint8_t version; /* 0x8 */
42 uint8_t rsvd1[3]; /* 0x9-0xB */
Baruch Siach3ef2e4e2017-07-04 20:23:39 +030043 uint32_t srcaddr; /* 0xC-0xF */
44 uint32_t destaddr; /* 0x10-0x13 */
45 uint32_t execaddr; /* 0x14-0x17 */
46 uint8_t satapiomode; /* 0x18 */
Pali Rohár370f54c2022-02-17 10:43:34 +010047 uint8_t nandblocksize; /* 0x19 */
48 union {
49 uint8_t nandbadblklocation; /* 0x1A */
Baruch Siach3ef2e4e2017-07-04 20:23:39 +030050 uint16_t ddrinitdelay; /* 0x1A-0x1B */
Pali Rohár370f54c2022-02-17 10:43:34 +010051 };
52 uint8_t rsvd2; /* 0x1C */
53 uint8_t bin; /* 0x1D */
Baruch Siach3ef2e4e2017-07-04 20:23:39 +030054 uint8_t ext; /* 0x1E */
55 uint8_t checksum; /* 0x1F */
Pali Rohárf01adfd2021-07-23 11:14:14 +020056} __packed;
Prafulla Wadaskar07329412009-09-07 15:05:02 +053057
Stefan Roesec74165d2015-09-29 09:19:59 +020058struct ext_hdr_v0_reg {
Prafulla Wadaskar07329412009-09-07 15:05:02 +053059 uint32_t raddr;
60 uint32_t rdata;
Pali Rohárf01adfd2021-07-23 11:14:14 +020061} __packed;
Prafulla Wadaskar07329412009-09-07 15:05:02 +053062
Pali Rohár370f54c2022-02-17 10:43:34 +010063/* Structure of the extension header, version 0 (Kirkwood, Dove) */
Stefan Roesec74165d2015-09-29 09:19:59 +020064struct ext_hdr_v0 {
Pali Rohár370f54c2022-02-17 10:43:34 +010065 /*
66 * Beware that extension header offsets specified in 88AP510 Functional
67 * Specifications are relative to the start of the main header, not to
68 * the start of the extension header itself.
69 */
70 uint32_t offset; /* 0x0-0x3 */
71 uint8_t rsvd1[8]; /* 0x4-0xB */
72 uint32_t enddelay; /* 0xC-0xF */
73 uint32_t match_addr; /* 0x10-0x13 */
74 uint32_t match_mask; /* 0x14-0x17 */
75 uint32_t match_value; /* 0x18-0x1B */
76 uint8_t ddrwritetype; /* 0x1C */
77 uint8_t ddrresetmpp; /* 0x1D */
78 uint8_t ddrclkenmpp; /* 0x1E */
79 uint8_t ddrinitdelay; /* 0x1F */
80 struct ext_hdr_v0_reg rcfg[55]; /* 0x20-0x1D7 */
81 uint8_t rsvd2[7]; /* 0x1D8-0x1DE */
82 uint8_t checksum; /* 0x1DF */
83} __packed;
84
85/* Structure of the binary code header, version 0 (Dove) */
86struct bin_hdr_v0 {
87 uint32_t match_addr; /* 0x00-0x03 */
88 uint32_t match_mask; /* 0x04-0x07 */
89 uint32_t match_value; /* 0x08-0x0B */
90 uint32_t offset; /* 0x0C-0x0F */
91 uint32_t destaddr; /* 0x10-0x13 */
92 uint32_t size; /* 0x14-0x17 */
93 uint32_t execaddr; /* 0x18-0x1B */
94 uint32_t params[4]; /* 0x1C-0x2B */
95 uint8_t params_flags; /* 0x2C */
96 uint8_t rsvd1; /* 0x2D */
97 uint8_t rsvd2; /* 0x2E */
98 uint8_t checksum; /* 0x2F */
99 uint8_t code[2000]; /* 0x30-0x7FF */
Pali Rohárf01adfd2021-07-23 11:14:14 +0200100} __packed;
Prafulla Wadaskar07329412009-09-07 15:05:02 +0530101
Pali Rohárbeddea82021-09-24 23:07:02 +0200102/* Structure of the main header, version 1 (Armada 370/XP/375/38x/39x) */
Stefan Roesec74165d2015-09-29 09:19:59 +0200103struct main_hdr_v1 {
Baruch Siach3ef2e4e2017-07-04 20:23:39 +0300104 uint8_t blockid; /* 0x0 */
105 uint8_t flags; /* 0x1 */
Pali Rohárbf9a89e2021-10-22 12:37:46 +0200106 uint16_t nandpagesize; /* 0x2-0x3 */
Baruch Siach3ef2e4e2017-07-04 20:23:39 +0300107 uint32_t blocksize; /* 0x4-0x7 */
108 uint8_t version; /* 0x8 */
109 uint8_t headersz_msb; /* 0x9 */
110 uint16_t headersz_lsb; /* 0xA-0xB */
111 uint32_t srcaddr; /* 0xC-0xF */
112 uint32_t destaddr; /* 0x10-0x13 */
113 uint32_t execaddr; /* 0x14-0x17 */
114 uint8_t options; /* 0x18 */
115 uint8_t nandblocksize; /* 0x19 */
116 uint8_t nandbadblklocation; /* 0x1A */
117 uint8_t reserved4; /* 0x1B */
118 uint16_t reserved5; /* 0x1C-0x1D */
119 uint8_t ext; /* 0x1E */
120 uint8_t checksum; /* 0x1F */
Pali Rohárf01adfd2021-07-23 11:14:14 +0200121} __packed;
Stefan Roesec74165d2015-09-29 09:19:59 +0200122
123/*
Chris Packham883bf452016-11-09 22:07:45 +1300124 * Main header options
125 */
126#define MAIN_HDR_V1_OPT_BAUD_DEFAULT 0
127#define MAIN_HDR_V1_OPT_BAUD_2400 0x1
128#define MAIN_HDR_V1_OPT_BAUD_4800 0x2
129#define MAIN_HDR_V1_OPT_BAUD_9600 0x3
130#define MAIN_HDR_V1_OPT_BAUD_19200 0x4
131#define MAIN_HDR_V1_OPT_BAUD_38400 0x5
132#define MAIN_HDR_V1_OPT_BAUD_57600 0x6
133#define MAIN_HDR_V1_OPT_BAUD_115200 0x7
134
135/*
Pali Rohárbeddea82021-09-24 23:07:02 +0200136 * Header for the optional headers, version 1 (Armada 370/XP/375/38x/39x)
Stefan Roesec74165d2015-09-29 09:19:59 +0200137 */
138struct opt_hdr_v1 {
139 uint8_t headertype;
140 uint8_t headersz_msb;
141 uint16_t headersz_lsb;
142 char data[0];
Pali Rohárf01adfd2021-07-23 11:14:14 +0200143} __packed;
Stefan Roesec74165d2015-09-29 09:19:59 +0200144
145/*
Mario Six10d14492017-01-11 16:01:00 +0100146 * Public Key data in DER format
147 */
148struct pubkey_der_v1 {
149 uint8_t key[524];
Pali Rohárf01adfd2021-07-23 11:14:14 +0200150} __packed;
Mario Six10d14492017-01-11 16:01:00 +0100151
152/*
153 * Signature (RSA 2048)
154 */
155struct sig_v1 {
156 uint8_t sig[256];
Pali Rohárf01adfd2021-07-23 11:14:14 +0200157} __packed;
Mario Six10d14492017-01-11 16:01:00 +0100158
159/*
Pali Rohárbeddea82021-09-24 23:07:02 +0200160 * Structure of secure header (Armada XP/375/38x/39x)
Mario Six10d14492017-01-11 16:01:00 +0100161 */
162struct secure_hdr_v1 {
163 uint8_t headertype; /* 0x0 */
164 uint8_t headersz_msb; /* 0x1 */
165 uint16_t headersz_lsb; /* 0x2 - 0x3 */
166 uint32_t reserved1; /* 0x4 - 0x7 */
167 struct pubkey_der_v1 kak; /* 0x8 - 0x213 */
168 uint8_t jtag_delay; /* 0x214 */
169 uint8_t reserved2; /* 0x215 */
170 uint16_t reserved3; /* 0x216 - 0x217 */
171 uint32_t boxid; /* 0x218 - 0x21B */
172 uint32_t flashid; /* 0x21C - 0x21F */
173 struct sig_v1 hdrsig; /* 0x220 - 0x31F */
174 struct sig_v1 imgsig; /* 0x320 - 0x41F */
175 struct pubkey_der_v1 csk[16]; /* 0x420 - 0x24DF */
176 struct sig_v1 csksig; /* 0x24E0 - 0x25DF */
177 uint8_t next; /* 0x25E0 */
178 uint8_t reserved4; /* 0x25E1 */
179 uint16_t reserved5; /* 0x25E2 - 0x25E3 */
Pali Rohárf01adfd2021-07-23 11:14:14 +0200180} __packed;
Mario Six10d14492017-01-11 16:01:00 +0100181
182/*
Pali Rohárfbe10ac2021-07-23 11:14:11 +0200183 * Structure of register set
184 */
185struct register_set_hdr_v1 {
186 uint8_t headertype; /* 0x0 */
187 uint8_t headersz_msb; /* 0x1 */
188 uint16_t headersz_lsb; /* 0x2 - 0x3 */
189 union {
190 struct {
191 uint32_t address; /* 0x4+8*N - 0x7+8*N */
192 uint32_t value; /* 0x8+8*N - 0xB+8*N */
Pali Rohárf01adfd2021-07-23 11:14:14 +0200193 } __packed entry;
Pali Rohárfbe10ac2021-07-23 11:14:11 +0200194 struct {
195 uint8_t next; /* 0xC+8*N */
196 uint8_t delay; /* 0xD+8*N */
197 uint16_t reserved; /* 0xE+8*N - 0xF+8*N */
Pali Rohárf01adfd2021-07-23 11:14:14 +0200198 } __packed last_entry;
Pali Rohárfbe10ac2021-07-23 11:14:11 +0200199 } data[];
Pali Rohárf01adfd2021-07-23 11:14:14 +0200200} __packed;
Pali Rohárfbe10ac2021-07-23 11:14:11 +0200201
202/*
203 * Value 0 in register_set_hdr_v1 delay field is special.
204 * Instead of delay it setup SDRAM Controller.
205 */
206#define REGISTER_SET_HDR_OPT_DELAY_SDRAM_SETUP 0
207#define REGISTER_SET_HDR_OPT_DELAY_MS(val) ((val) ?: 1)
208
209/*
Stefan Roesec74165d2015-09-29 09:19:59 +0200210 * Various values for the opt_hdr_v1->headertype field, describing the
211 * different types of optional headers. The "secure" header contains
212 * informations related to secure boot (encryption keys, etc.). The
213 * "binary" header contains ARM binary code to be executed prior to
214 * executing the main payload (usually the bootloader). This is
215 * typically used to execute DDR3 training code. The "register" header
216 * allows to describe a set of (address, value) tuples that are
217 * generally used to configure the DRAM controller.
218 */
219#define OPT_HDR_V1_SECURE_TYPE 0x1
220#define OPT_HDR_V1_BINARY_TYPE 0x2
221#define OPT_HDR_V1_REGISTER_TYPE 0x3
222
Stefan Roesec74165d2015-09-29 09:19:59 +0200223/*
224 * Byte 8 of the image header contains the version number. In the v0
225 * header, byte 8 was reserved, and always set to 0. In the v1 header,
226 * byte 8 has been changed to a proper field, set to 1.
227 */
Marek Behúnfa9caec2021-09-24 23:07:00 +0200228static inline unsigned int kwbimage_version(const void *header)
Stefan Roesec74165d2015-09-29 09:19:59 +0200229{
Pali Rohárbed18ef2021-09-24 23:06:48 +0200230 const unsigned char *ptr = header;
Stefan Roesec74165d2015-09-29 09:19:59 +0200231 return ptr[8];
232}
233
Marek Behúnd1b0b032021-09-24 23:07:01 +0200234static inline size_t kwbheader_size(const void *header)
235{
236 if (kwbimage_version(header) == 0) {
237 const struct main_hdr_v0 *hdr = header;
238
Pali Rohár524597e2022-02-17 10:43:35 +0100239 /*
240 * First extension header starts immediately after the main
241 * header without any padding. Between extension headers is
242 * 0x20 byte padding. There is no padding after the last
243 * extension header. First binary code header starts immediately
244 * after the last extension header (or immediately after the
245 * main header if there is no extension header) without any
246 * padding. There is no padding between binary code headers and
247 * neither after the last binary code header.
248 */
Marek Behúnd1b0b032021-09-24 23:07:01 +0200249 return sizeof(*hdr) +
Pali Rohár524597e2022-02-17 10:43:35 +0100250 hdr->ext * sizeof(struct ext_hdr_v0) +
251 ((hdr->ext > 1) ? ((hdr->ext - 1) * 0x20) : 0) +
252 hdr->bin * sizeof(struct bin_hdr_v0);
Marek Behúnd1b0b032021-09-24 23:07:01 +0200253 } else {
254 const struct main_hdr_v1 *hdr = header;
255
256 return (hdr->headersz_msb << 16) |
257 le16_to_cpu(hdr->headersz_lsb);
258 }
259}
260
261static inline size_t kwbheader_size_for_csum(const void *header)
262{
263 if (kwbimage_version(header) == 0)
264 return sizeof(struct main_hdr_v0);
265 else
266 return kwbheader_size(header);
267}
268
Pali Roháre581f192022-02-17 10:43:36 +0100269static inline struct ext_hdr_v0 *ext_hdr_v0_first(void *img)
270{
271 struct main_hdr_v0 *mhdr;
272
273 if (kwbimage_version(img) != 0)
274 return NULL;
275
276 mhdr = img;
277 if (mhdr->ext)
278 return (struct ext_hdr_v0 *)(mhdr + 1);
279 else
280 return NULL;
281}
282
283static inline void *_ext_hdr_v0_end(struct main_hdr_v0 *mhdr)
284{
285 return (uint8_t *)mhdr + kwbheader_size(mhdr) - mhdr->bin * sizeof(struct bin_hdr_v0);
286}
287
288static inline struct ext_hdr_v0 *ext_hdr_v0_next(void *img, struct ext_hdr_v0 *cur)
289{
290 if ((void *)(cur + 1) < _ext_hdr_v0_end(img))
291 return (struct ext_hdr_v0 *)((uint8_t *)(cur + 1) + 0x20);
292 else
293 return NULL;
294}
295
296#define for_each_ext_hdr_v0(ehdr, img) \
297 for ((ehdr) = ext_hdr_v0_first((img)); \
298 (ehdr) != NULL; \
299 (ehdr) = ext_hdr_v0_next((img), (ehdr)))
300
301static inline struct bin_hdr_v0 *bin_hdr_v0_first(void *img)
302{
303 struct main_hdr_v0 *mhdr;
304
305 if (kwbimage_version(img) != 0)
306 return NULL;
307
308 mhdr = img;
309 if (mhdr->bin)
310 return _ext_hdr_v0_end(mhdr);
311 else
312 return NULL;
313}
314
315#define for_each_bin_hdr_v0(bhdr, img) \
316 for ((bhdr) = bin_hdr_v0_first((img)); \
317 (bhdr) && (void *)(bhdr) < (void *)((uint8_t *)img + kwbheader_size(img)); \
318 (bhdr) = (struct bin_hdr_v0 *)((bhdr))+1)
319
Marek Behúnb1a76502021-08-18 00:59:15 +0200320static inline uint32_t opt_hdr_v1_size(const struct opt_hdr_v1 *ohdr)
321{
322 return (ohdr->headersz_msb << 16) | le16_to_cpu(ohdr->headersz_lsb);
323}
324
325static inline int opt_hdr_v1_valid_size(const struct opt_hdr_v1 *ohdr,
326 const void *mhdr_end)
327{
328 uint32_t ohdr_size;
329
Pali Rohár7183b952022-01-12 18:20:51 +0100330 if ((const void *)(ohdr + 1) > mhdr_end)
Marek Behúnb1a76502021-08-18 00:59:15 +0200331 return 0;
332
333 ohdr_size = opt_hdr_v1_size(ohdr);
Pali Rohár7183b952022-01-12 18:20:51 +0100334 if (ohdr_size < 8 || (const void *)((const uint8_t *)ohdr + ohdr_size) > mhdr_end)
Marek Behúnb1a76502021-08-18 00:59:15 +0200335 return 0;
336
337 return 1;
338}
339
340static inline struct opt_hdr_v1 *opt_hdr_v1_first(void *img) {
341 struct main_hdr_v1 *mhdr;
342
Marek Behúnfa9caec2021-09-24 23:07:00 +0200343 if (kwbimage_version(img) != 1)
Marek Behúnb1a76502021-08-18 00:59:15 +0200344 return NULL;
345
346 mhdr = img;
Pali Rohár32eec192022-01-12 18:20:52 +0100347 if (mhdr->ext)
Marek Behúnb1a76502021-08-18 00:59:15 +0200348 return (struct opt_hdr_v1 *)(mhdr + 1);
349 else
350 return NULL;
351}
352
353static inline uint8_t *opt_hdr_v1_ext(struct opt_hdr_v1 *cur)
354{
355 uint32_t size = opt_hdr_v1_size(cur);
356
357 return (uint8_t *)cur + size - 4;
358}
359
360static inline struct opt_hdr_v1 *_opt_hdr_v1_next(struct opt_hdr_v1 *cur)
361{
362 return (struct opt_hdr_v1 *)((uint8_t *)cur + opt_hdr_v1_size(cur));
363}
364
365static inline struct opt_hdr_v1 *opt_hdr_v1_next(struct opt_hdr_v1 *cur)
366{
Pali Rohár32eec192022-01-12 18:20:52 +0100367 if (*opt_hdr_v1_ext(cur))
Marek Behúnb1a76502021-08-18 00:59:15 +0200368 return _opt_hdr_v1_next(cur);
369 else
370 return NULL;
371}
372
373#define for_each_opt_hdr_v1(ohdr, img) \
374 for ((ohdr) = opt_hdr_v1_first((img)); \
375 (ohdr) != NULL; \
376 (ohdr) = opt_hdr_v1_next((ohdr)))
377
Prafulla Wadaskar07329412009-09-07 15:05:02 +0530378#endif /* _KWBIMAGE_H_ */