xref: /OK3568_Linux_fs/u-boot/tools/kwbimage.h (revision 4882a59341e53eb6f0b4789bf948001014eff981)
1*4882a593Smuzhiyun /*
2*4882a593Smuzhiyun  * (C) Copyright 2008
3*4882a593Smuzhiyun  * Marvell Semiconductor <www.marvell.com>
4*4882a593Smuzhiyun  * Written-by: Prafulla Wadaskar <prafulla@marvell.com>
5*4882a593Smuzhiyun  *
6*4882a593Smuzhiyun  * SPDX-License-Identifier:	GPL-2.0+
7*4882a593Smuzhiyun  */
8*4882a593Smuzhiyun 
9*4882a593Smuzhiyun #ifndef _KWBIMAGE_H_
10*4882a593Smuzhiyun #define _KWBIMAGE_H_
11*4882a593Smuzhiyun 
12*4882a593Smuzhiyun #include <compiler.h>
13*4882a593Smuzhiyun #include <stdint.h>
14*4882a593Smuzhiyun 
15*4882a593Smuzhiyun #define KWBIMAGE_MAX_CONFIG	((0x1dc - 0x20)/sizeof(struct reg_config))
16*4882a593Smuzhiyun #define MAX_TEMPBUF_LEN		32
17*4882a593Smuzhiyun 
18*4882a593Smuzhiyun /* NAND ECC Mode */
19*4882a593Smuzhiyun #define IBR_HDR_ECC_DEFAULT		0x00
20*4882a593Smuzhiyun #define IBR_HDR_ECC_FORCED_HAMMING	0x01
21*4882a593Smuzhiyun #define IBR_HDR_ECC_FORCED_RS  		0x02
22*4882a593Smuzhiyun #define IBR_HDR_ECC_DISABLED  		0x03
23*4882a593Smuzhiyun 
24*4882a593Smuzhiyun /* Boot Type - block ID */
25*4882a593Smuzhiyun #define IBR_HDR_I2C_ID			0x4D
26*4882a593Smuzhiyun #define IBR_HDR_SPI_ID			0x5A
27*4882a593Smuzhiyun #define IBR_HDR_NAND_ID			0x8B
28*4882a593Smuzhiyun #define IBR_HDR_SATA_ID			0x78
29*4882a593Smuzhiyun #define IBR_HDR_PEX_ID			0x9C
30*4882a593Smuzhiyun #define IBR_HDR_UART_ID			0x69
31*4882a593Smuzhiyun #define IBR_DEF_ATTRIB	 		0x00
32*4882a593Smuzhiyun 
33*4882a593Smuzhiyun #define ALIGN_SUP(x, a) (((x) + (a - 1)) & ~(a - 1))
34*4882a593Smuzhiyun 
35*4882a593Smuzhiyun /* Structure of the main header, version 0 (Kirkwood, Dove) */
36*4882a593Smuzhiyun struct main_hdr_v0 {
37*4882a593Smuzhiyun 	uint8_t  blockid;		/* 0x0       */
38*4882a593Smuzhiyun 	uint8_t  nandeccmode;		/* 0x1       */
39*4882a593Smuzhiyun 	uint16_t nandpagesize;		/* 0x2-0x3   */
40*4882a593Smuzhiyun 	uint32_t blocksize;		/* 0x4-0x7   */
41*4882a593Smuzhiyun 	uint32_t rsvd1;			/* 0x8-0xB   */
42*4882a593Smuzhiyun 	uint32_t srcaddr;		/* 0xC-0xF   */
43*4882a593Smuzhiyun 	uint32_t destaddr;		/* 0x10-0x13 */
44*4882a593Smuzhiyun 	uint32_t execaddr;		/* 0x14-0x17 */
45*4882a593Smuzhiyun 	uint8_t  satapiomode;		/* 0x18      */
46*4882a593Smuzhiyun 	uint8_t  rsvd3;			/* 0x19      */
47*4882a593Smuzhiyun 	uint16_t ddrinitdelay;		/* 0x1A-0x1B */
48*4882a593Smuzhiyun 	uint16_t rsvd2;			/* 0x1C-0x1D */
49*4882a593Smuzhiyun 	uint8_t  ext;			/* 0x1E      */
50*4882a593Smuzhiyun 	uint8_t  checksum;		/* 0x1F      */
51*4882a593Smuzhiyun };
52*4882a593Smuzhiyun 
53*4882a593Smuzhiyun struct ext_hdr_v0_reg {
54*4882a593Smuzhiyun 	uint32_t raddr;
55*4882a593Smuzhiyun 	uint32_t rdata;
56*4882a593Smuzhiyun };
57*4882a593Smuzhiyun 
58*4882a593Smuzhiyun #define EXT_HDR_V0_REG_COUNT ((0x1dc - 0x20) / sizeof(struct ext_hdr_v0_reg))
59*4882a593Smuzhiyun 
60*4882a593Smuzhiyun struct ext_hdr_v0 {
61*4882a593Smuzhiyun 	uint32_t              offset;
62*4882a593Smuzhiyun 	uint8_t               reserved[0x20 - sizeof(uint32_t)];
63*4882a593Smuzhiyun 	struct ext_hdr_v0_reg rcfg[EXT_HDR_V0_REG_COUNT];
64*4882a593Smuzhiyun 	uint8_t               reserved2[7];
65*4882a593Smuzhiyun 	uint8_t               checksum;
66*4882a593Smuzhiyun };
67*4882a593Smuzhiyun 
68*4882a593Smuzhiyun struct kwb_header {
69*4882a593Smuzhiyun 	struct main_hdr_v0	kwb_hdr;
70*4882a593Smuzhiyun 	struct ext_hdr_v0	kwb_exthdr;
71*4882a593Smuzhiyun };
72*4882a593Smuzhiyun 
73*4882a593Smuzhiyun /* Structure of the main header, version 1 (Armada 370/38x/XP) */
74*4882a593Smuzhiyun struct main_hdr_v1 {
75*4882a593Smuzhiyun 	uint8_t  blockid;               /* 0x0       */
76*4882a593Smuzhiyun 	uint8_t  flags;                 /* 0x1       */
77*4882a593Smuzhiyun 	uint16_t reserved2;             /* 0x2-0x3   */
78*4882a593Smuzhiyun 	uint32_t blocksize;             /* 0x4-0x7   */
79*4882a593Smuzhiyun 	uint8_t  version;               /* 0x8       */
80*4882a593Smuzhiyun 	uint8_t  headersz_msb;          /* 0x9       */
81*4882a593Smuzhiyun 	uint16_t headersz_lsb;          /* 0xA-0xB   */
82*4882a593Smuzhiyun 	uint32_t srcaddr;               /* 0xC-0xF   */
83*4882a593Smuzhiyun 	uint32_t destaddr;              /* 0x10-0x13 */
84*4882a593Smuzhiyun 	uint32_t execaddr;              /* 0x14-0x17 */
85*4882a593Smuzhiyun 	uint8_t  options;               /* 0x18      */
86*4882a593Smuzhiyun 	uint8_t  nandblocksize;         /* 0x19      */
87*4882a593Smuzhiyun 	uint8_t  nandbadblklocation;    /* 0x1A      */
88*4882a593Smuzhiyun 	uint8_t  reserved4;             /* 0x1B      */
89*4882a593Smuzhiyun 	uint16_t reserved5;             /* 0x1C-0x1D */
90*4882a593Smuzhiyun 	uint8_t  ext;                   /* 0x1E      */
91*4882a593Smuzhiyun 	uint8_t  checksum;              /* 0x1F      */
92*4882a593Smuzhiyun };
93*4882a593Smuzhiyun 
94*4882a593Smuzhiyun /*
95*4882a593Smuzhiyun  * Main header options
96*4882a593Smuzhiyun  */
97*4882a593Smuzhiyun #define MAIN_HDR_V1_OPT_BAUD_DEFAULT	0
98*4882a593Smuzhiyun #define MAIN_HDR_V1_OPT_BAUD_2400	0x1
99*4882a593Smuzhiyun #define MAIN_HDR_V1_OPT_BAUD_4800	0x2
100*4882a593Smuzhiyun #define MAIN_HDR_V1_OPT_BAUD_9600	0x3
101*4882a593Smuzhiyun #define MAIN_HDR_V1_OPT_BAUD_19200	0x4
102*4882a593Smuzhiyun #define MAIN_HDR_V1_OPT_BAUD_38400	0x5
103*4882a593Smuzhiyun #define MAIN_HDR_V1_OPT_BAUD_57600	0x6
104*4882a593Smuzhiyun #define MAIN_HDR_V1_OPT_BAUD_115200	0x7
105*4882a593Smuzhiyun 
106*4882a593Smuzhiyun /*
107*4882a593Smuzhiyun  * Header for the optional headers, version 1 (Armada 370, Armada XP)
108*4882a593Smuzhiyun  */
109*4882a593Smuzhiyun struct opt_hdr_v1 {
110*4882a593Smuzhiyun 	uint8_t  headertype;
111*4882a593Smuzhiyun 	uint8_t  headersz_msb;
112*4882a593Smuzhiyun 	uint16_t headersz_lsb;
113*4882a593Smuzhiyun 	char     data[0];
114*4882a593Smuzhiyun };
115*4882a593Smuzhiyun 
116*4882a593Smuzhiyun /*
117*4882a593Smuzhiyun  * Public Key data in DER format
118*4882a593Smuzhiyun  */
119*4882a593Smuzhiyun struct pubkey_der_v1 {
120*4882a593Smuzhiyun 	uint8_t key[524];
121*4882a593Smuzhiyun };
122*4882a593Smuzhiyun 
123*4882a593Smuzhiyun /*
124*4882a593Smuzhiyun  * Signature (RSA 2048)
125*4882a593Smuzhiyun  */
126*4882a593Smuzhiyun struct sig_v1 {
127*4882a593Smuzhiyun 	uint8_t sig[256];
128*4882a593Smuzhiyun };
129*4882a593Smuzhiyun 
130*4882a593Smuzhiyun /*
131*4882a593Smuzhiyun  * Structure of secure header (Armada 38x)
132*4882a593Smuzhiyun  */
133*4882a593Smuzhiyun struct secure_hdr_v1 {
134*4882a593Smuzhiyun 	uint8_t  headertype;		/* 0x0 */
135*4882a593Smuzhiyun 	uint8_t  headersz_msb;		/* 0x1 */
136*4882a593Smuzhiyun 	uint16_t headersz_lsb;		/* 0x2 - 0x3 */
137*4882a593Smuzhiyun 	uint32_t reserved1;		/* 0x4 - 0x7 */
138*4882a593Smuzhiyun 	struct pubkey_der_v1 kak;	/* 0x8 - 0x213 */
139*4882a593Smuzhiyun 	uint8_t  jtag_delay;		/* 0x214 */
140*4882a593Smuzhiyun 	uint8_t  reserved2;		/* 0x215 */
141*4882a593Smuzhiyun 	uint16_t reserved3;		/* 0x216 - 0x217 */
142*4882a593Smuzhiyun 	uint32_t boxid;			/* 0x218 - 0x21B */
143*4882a593Smuzhiyun 	uint32_t flashid;		/* 0x21C - 0x21F */
144*4882a593Smuzhiyun 	struct sig_v1 hdrsig;		/* 0x220 - 0x31F */
145*4882a593Smuzhiyun 	struct sig_v1 imgsig;		/* 0x320 - 0x41F */
146*4882a593Smuzhiyun 	struct pubkey_der_v1 csk[16];	/* 0x420 - 0x24DF */
147*4882a593Smuzhiyun 	struct sig_v1 csksig;		/* 0x24E0 - 0x25DF */
148*4882a593Smuzhiyun 	uint8_t  next;			/* 0x25E0 */
149*4882a593Smuzhiyun 	uint8_t  reserved4;		/* 0x25E1 */
150*4882a593Smuzhiyun 	uint16_t reserved5;		/* 0x25E2 - 0x25E3 */
151*4882a593Smuzhiyun };
152*4882a593Smuzhiyun 
153*4882a593Smuzhiyun /*
154*4882a593Smuzhiyun  * Various values for the opt_hdr_v1->headertype field, describing the
155*4882a593Smuzhiyun  * different types of optional headers. The "secure" header contains
156*4882a593Smuzhiyun  * informations related to secure boot (encryption keys, etc.). The
157*4882a593Smuzhiyun  * "binary" header contains ARM binary code to be executed prior to
158*4882a593Smuzhiyun  * executing the main payload (usually the bootloader). This is
159*4882a593Smuzhiyun  * typically used to execute DDR3 training code. The "register" header
160*4882a593Smuzhiyun  * allows to describe a set of (address, value) tuples that are
161*4882a593Smuzhiyun  * generally used to configure the DRAM controller.
162*4882a593Smuzhiyun  */
163*4882a593Smuzhiyun #define OPT_HDR_V1_SECURE_TYPE   0x1
164*4882a593Smuzhiyun #define OPT_HDR_V1_BINARY_TYPE   0x2
165*4882a593Smuzhiyun #define OPT_HDR_V1_REGISTER_TYPE 0x3
166*4882a593Smuzhiyun 
167*4882a593Smuzhiyun #define KWBHEADER_V1_SIZE(hdr) \
168*4882a593Smuzhiyun 	(((hdr)->headersz_msb << 16) | le16_to_cpu((hdr)->headersz_lsb))
169*4882a593Smuzhiyun 
170*4882a593Smuzhiyun enum kwbimage_cmd {
171*4882a593Smuzhiyun 	CMD_INVALID,
172*4882a593Smuzhiyun 	CMD_BOOT_FROM,
173*4882a593Smuzhiyun 	CMD_NAND_ECC_MODE,
174*4882a593Smuzhiyun 	CMD_NAND_PAGE_SIZE,
175*4882a593Smuzhiyun 	CMD_SATA_PIO_MODE,
176*4882a593Smuzhiyun 	CMD_DDR_INIT_DELAY,
177*4882a593Smuzhiyun 	CMD_DATA
178*4882a593Smuzhiyun };
179*4882a593Smuzhiyun 
180*4882a593Smuzhiyun enum kwbimage_cmd_types {
181*4882a593Smuzhiyun 	CFG_INVALID = -1,
182*4882a593Smuzhiyun 	CFG_COMMAND,
183*4882a593Smuzhiyun 	CFG_DATA0,
184*4882a593Smuzhiyun 	CFG_DATA1
185*4882a593Smuzhiyun };
186*4882a593Smuzhiyun 
187*4882a593Smuzhiyun /*
188*4882a593Smuzhiyun  * functions
189*4882a593Smuzhiyun  */
190*4882a593Smuzhiyun void init_kwb_image_type (void);
191*4882a593Smuzhiyun 
192*4882a593Smuzhiyun /*
193*4882a593Smuzhiyun  * Byte 8 of the image header contains the version number. In the v0
194*4882a593Smuzhiyun  * header, byte 8 was reserved, and always set to 0. In the v1 header,
195*4882a593Smuzhiyun  * byte 8 has been changed to a proper field, set to 1.
196*4882a593Smuzhiyun  */
image_version(void * header)197*4882a593Smuzhiyun static inline unsigned int image_version(void *header)
198*4882a593Smuzhiyun {
199*4882a593Smuzhiyun 	unsigned char *ptr = header;
200*4882a593Smuzhiyun 	return ptr[8];
201*4882a593Smuzhiyun }
202*4882a593Smuzhiyun 
203*4882a593Smuzhiyun #endif /* _KWBIMAGE_H_ */
204