xref: /openbmc/u-boot/tools/lpc32xximage.c (revision d2eaec60)
1 /*
2  * Image manipulator for LPC32XX SoCs
3  *
4  * (C) Copyright 2015  DENX Software Engineering GmbH
5  * Written-by: Albert ARIBAUD <albert.aribaud@3adev.fr>
6  *
7  * Derived from omapimage.c:
8  *
9  * (C) Copyright 2010
10  * Linaro LTD, www.linaro.org
11  * Author: John Rigby <john.rigby@linaro.org>
12  * Based on TI's signGP.c
13  *
14  * (C) Copyright 2009
15  * Stefano Babic, DENX Software Engineering, sbabic@denx.de.
16  *
17  * (C) Copyright 2008
18  * Marvell Semiconductor <www.marvell.com>
19  * Written-by: Prafulla Wadaskar <prafulla@marvell.com>
20  *
21  * SPDX-License-Identifier:	GPL-2.0+
22  */
23 
24 #include "imagetool.h"
25 #include <compiler.h>
26 #include <image.h>
27 
28 /*
29  * NAND page 0 boot header
30  */
31 
32 struct nand_page_0_boot_header {
33 	uint32_t data[129];
34 	uint32_t pad[383];
35 };
36 
37 /*
38  * Default ICC (interface configuration data [sic]) if none specified
39  * in board config
40  */
41 
42 #ifndef LPC32XX_BOOT_ICR
43 #define LPC32XX_BOOT_ICR 0x00000096
44 #endif
45 
46 /*
47  * Default boot NAND page size if none specified in board config
48  */
49 
50 #ifndef LPC32XX_BOOT_NAND_PAGESIZE
51 #define LPC32XX_BOOT_NAND_PAGESIZE 2048
52 #endif
53 
54 /*
55  * Default boot NAND pages per sector if none specified in board config
56  */
57 
58 #ifndef LPC32XX_BOOT_NAND_PAGES_PER_SECTOR
59 #define LPC32XX_BOOT_NAND_PAGES_PER_SECTOR 64
60 #endif
61 
62 /*
63  * Maximum size for boot code is 56K unless defined in board config
64  */
65 
66 #ifndef LPC32XX_BOOT_CODESIZE
67 #define LPC32XX_BOOT_CODESIZE (56*1024)
68 #endif
69 
70 /* signature byte for a readable block */
71 
72 #define LPC32XX_BOOT_BLOCK_OK 0xaa
73 
74 static struct nand_page_0_boot_header lpc32xximage_header;
75 
76 static int lpc32xximage_check_image_types(uint8_t type)
77 {
78 	if (type == IH_TYPE_LPC32XXIMAGE)
79 		return EXIT_SUCCESS;
80 	return EXIT_FAILURE;
81 }
82 
83 static int lpc32xximage_verify_header(unsigned char *ptr, int image_size,
84 			struct image_tool_params *params)
85 {
86 	struct nand_page_0_boot_header *hdr =
87 		(struct nand_page_0_boot_header *)ptr;
88 
89 	/* turn image size from bytes to NAND pages, page 0 included */
90 	int image_size_in_pages = ((image_size - 1)
91 				  / LPC32XX_BOOT_NAND_PAGESIZE);
92 
93 	if (hdr->data[0] != (0xff & LPC32XX_BOOT_ICR))
94 		return -1;
95 	if (hdr->data[1] != (0xff & ~LPC32XX_BOOT_ICR))
96 		return -1;
97 	if (hdr->data[2] != (0xff & LPC32XX_BOOT_ICR))
98 		return -1;
99 	if (hdr->data[3] != (0xff & ~LPC32XX_BOOT_ICR))
100 		return -1;
101 	if (hdr->data[4] != (0xff & image_size_in_pages))
102 		return -1;
103 	if (hdr->data[5] != (0xff & ~image_size_in_pages))
104 		return -1;
105 	if (hdr->data[6] != (0xff & image_size_in_pages))
106 		return -1;
107 	if (hdr->data[7] != (0xff & ~image_size_in_pages))
108 		return -1;
109 	if (hdr->data[8] != (0xff & image_size_in_pages))
110 		return -1;
111 	if (hdr->data[9] != (0xff & ~image_size_in_pages))
112 		return -1;
113 	if (hdr->data[10] != (0xff & image_size_in_pages))
114 		return -1;
115 	if (hdr->data[11] != (0xff & ~image_size_in_pages))
116 		return -1;
117 	if (hdr->data[12] != LPC32XX_BOOT_BLOCK_OK)
118 		return -1;
119 	if (hdr->data[128] != LPC32XX_BOOT_BLOCK_OK)
120 		return -1;
121 	return 0;
122 }
123 
124 static void print_hdr_byte(struct nand_page_0_boot_header *hdr, int ofs)
125 {
126 	printf("header[%d] = %02x\n", ofs, hdr->data[ofs]);
127 }
128 
129 static void lpc32xximage_print_header(const void *ptr)
130 {
131 	struct nand_page_0_boot_header *hdr =
132 		(struct nand_page_0_boot_header *)ptr;
133 	int ofs;
134 
135 	for (ofs = 0; ofs <= 12; ofs++)
136 		print_hdr_byte(hdr, ofs);
137 	print_hdr_byte(hdr, 128);
138 }
139 
140 static void lpc32xximage_set_header(void *ptr, struct stat *sbuf, int ifd,
141 				struct image_tool_params *params)
142 {
143 	struct nand_page_0_boot_header *hdr =
144 		(struct nand_page_0_boot_header *)ptr;
145 
146 	/* turn image size from bytes to NAND pages, page 0 included */
147 	int image_size_in_pages = ((sbuf->st_size
148 				  + LPC32XX_BOOT_NAND_PAGESIZE - 1)
149 				  / LPC32XX_BOOT_NAND_PAGESIZE);
150 
151 	/* fill header -- default byte value is 0x00, not 0xFF */
152 	memset((void *)hdr, 0, sizeof(*hdr));
153 	hdr->data[0] = (hdr->data[2] = 0xff & LPC32XX_BOOT_ICR);
154 	hdr->data[1] = (hdr->data[3] = 0xff & ~LPC32XX_BOOT_ICR);
155 	hdr->data[4] = (hdr->data[6] = (hdr->data[8]
156 		       = (hdr->data[10] = 0xff & image_size_in_pages)));
157 	hdr->data[5] = (hdr->data[7] = (hdr->data[9]
158 		       = (hdr->data[11] = 0xff & ~image_size_in_pages)));
159 	hdr->data[12] = (hdr->data[128] = LPC32XX_BOOT_BLOCK_OK);
160 }
161 
162 /*
163  * lpc32xximage parameters
164  */
165 U_BOOT_IMAGE_TYPE(
166 	lpc32xximage,
167 	"LPC32XX Boot Image",
168 	sizeof(lpc32xximage_header),
169 	(void *)&lpc32xximage_header,
170 	NULL,
171 	lpc32xximage_verify_header,
172 	lpc32xximage_print_header,
173 	lpc32xximage_set_header,
174 	NULL,
175 	lpc32xximage_check_image_types,
176 	NULL,
177 	NULL
178 );
179