1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24#include "imagetool.h"
25#include <compiler.h>
26#include <image.h>
27
28
29
30
31
32struct nand_page_0_boot_header {
33 uint32_t data[129];
34 uint32_t pad[383];
35};
36
37
38
39
40
41
42#ifndef LPC32XX_BOOT_ICR
43#define LPC32XX_BOOT_ICR 0x00000096
44#endif
45
46
47
48
49
50#ifndef LPC32XX_BOOT_NAND_PAGESIZE
51#define LPC32XX_BOOT_NAND_PAGESIZE 2048
52#endif
53
54
55
56
57
58#ifndef LPC32XX_BOOT_NAND_PAGES_PER_SECTOR
59#define LPC32XX_BOOT_NAND_PAGES_PER_SECTOR 64
60#endif
61
62
63
64
65
66#ifndef LPC32XX_BOOT_CODESIZE
67#define LPC32XX_BOOT_CODESIZE (56*1024)
68#endif
69
70
71
72#define LPC32XX_BOOT_BLOCK_OK 0xaa
73
74static struct nand_page_0_boot_header lpc32xximage_header;
75
76static 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
83static 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
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
124static 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
129static 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
140static 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
147 int image_size_in_pages = ((sbuf->st_size
148 + LPC32XX_BOOT_NAND_PAGESIZE - 1)
149 / LPC32XX_BOOT_NAND_PAGESIZE);
150
151
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
164
165U_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