2 * Copyright 2012 Freescale Semiconductor, Inc.
4 * SPDX-License-Identifier: GPL-2.0+
12 * Initialize to an invalid value.
14 static uint32_t next_pbl_cmd = 0x82000000;
16 * need to store all bytes in memory for calculating crc32, then write the
17 * bytes to image file for PBL boot.
19 static unsigned char mem_buf[1000000];
20 static unsigned char *pmem_buf = mem_buf;
22 static char *fname = "Unknown";
23 static int lineno = -1;
24 static struct pbl_header pblimage_header;
30 } endian_test = { {'l', '?', '?', 'b'} };
32 #define ENDIANNESS ((char)endian_test.l)
35 * The PBL can load up to 64 bytes at a time, so we split the U-Boot
36 * image into 64 byte chunks. PBL needs a command for each piece, of
37 * the form "81xxxxxx", where "xxxxxx" is the offset. Calculate the
38 * start offset by subtracting the size of the u-boot image from the
39 * top of the allowable 24-bit range.
41 static void init_next_pbl_cmd(FILE *fp_uboot)
44 int fd = fileno(fp_uboot);
46 if (fstat(fd, &st) == -1) {
47 printf("Error: Could not determine u-boot image size. %s\n",
52 next_pbl_cmd = 0x82000000 - st.st_size;
55 static void generate_pbl_cmd(void)
57 uint32_t val = next_pbl_cmd;
61 for (i = 3; i >= 0; i--) {
62 *pmem_buf++ = (val >> (i * 8)) & 0xff;
67 static void pbl_fget(size_t size, FILE *stream)
72 while (size && (c_temp = fgetc(stream)) != EOF) {
73 c = (unsigned char)c_temp;
80 /* load split u-boot with PBI command 81xxxxxx. */
81 static void load_uboot(FILE *fp_uboot)
83 init_next_pbl_cmd(fp_uboot);
84 while (next_pbl_cmd < 0x82000000) {
86 pbl_fget(64, fp_uboot);
90 static void check_get_hexval(char *token)
95 if (!sscanf(token, "%x", &hexval)) {
96 printf("Error:%s[%d] - Invalid hex data(%s)\n", fname,
100 for (i = 3; i >= 0; i--) {
101 *pmem_buf++ = (hexval >> (i * 8)) & 0xff;
106 static void pbl_parser(char *name)
110 char *token, *saveptr1, *saveptr2;
114 fd = fopen(name, "r");
116 printf("Error:%s - Can't open\n", fname);
120 while ((getline(&line, &len, fd)) > 0) {
122 token = strtok_r(line, "\r\n", &saveptr1);
123 /* drop all lines with zero tokens (= empty lines) */
126 for (line = token;; line = NULL) {
127 token = strtok_r(line, " \t", &saveptr2);
130 /* Drop all text starting with '#' as comments */
133 check_get_hexval(token);
141 static uint32_t reverse_byte(uint32_t val)
148 p1 = (unsigned char *)&temp;
149 for (j = 3; j >= 0; j--)
150 *p1++ = (val >> (j * 8)) & 0xff;
154 /* write end command and crc command to memory. */
155 static void add_end_cmd(void)
157 uint32_t pbl_end_cmd[4] = {0x09138000, 0x00000000,
158 0x091380c0, 0x00000000};
161 unsigned char *p = (unsigned char *)&pbl_end_cmd;
163 if (ENDIANNESS == 'l') {
164 for (i = 0; i < 4; i++)
165 pbl_end_cmd[i] = reverse_byte(pbl_end_cmd[i]);
168 for (i = 0; i < 16; i++) {
173 /* Add PBI CRC command. */
180 /* calculated CRC32 and write it to memory. */
181 crc32_pbl = pbl_crc32(0, (const char *)mem_buf, pbl_size);
182 *pmem_buf++ = (crc32_pbl >> 24) & 0xff;
183 *pmem_buf++ = (crc32_pbl >> 16) & 0xff;
184 *pmem_buf++ = (crc32_pbl >> 8) & 0xff;
185 *pmem_buf++ = (crc32_pbl) & 0xff;
188 if ((pbl_size % 16) != 0) {
189 for (i = 0; i < 8; i++) {
194 if ((pbl_size % 16 != 0)) {
195 printf("Error: Bad size of image file\n");
200 void pbl_load_uboot(int ifd, struct image_tool_params *params)
205 /* parse the rcw.cfg file. */
206 pbl_parser(params->imagename);
208 /* parse the pbi.cfg file. */
209 pbl_parser(params->imagename2);
211 fp_uboot = fopen(params->datafile, "r");
212 if (fp_uboot == NULL) {
213 printf("Error: %s open failed\n", params->datafile);
217 load_uboot(fp_uboot);
220 lseek(ifd, 0, SEEK_SET);
223 if (write(ifd, (const void *)&mem_buf, size) != size) {
224 fprintf(stderr, "Write error on %s: %s\n",
225 params->imagefile, strerror(errno));
230 static int pblimage_check_image_types(uint8_t type)
232 if (type == IH_TYPE_PBLIMAGE)
238 static int pblimage_verify_header(unsigned char *ptr, int image_size,
239 struct image_tool_params *params)
241 struct pbl_header *pbl_hdr = (struct pbl_header *) ptr;
243 /* Only a few checks can be done: search for magic numbers */
244 if (ENDIANNESS == 'l') {
245 if (pbl_hdr->preamble != reverse_byte(RCW_PREAMBLE))
246 return -FDT_ERR_BADSTRUCTURE;
248 if (pbl_hdr->rcwheader != reverse_byte(RCW_HEADER))
249 return -FDT_ERR_BADSTRUCTURE;
251 if (pbl_hdr->preamble != RCW_PREAMBLE)
252 return -FDT_ERR_BADSTRUCTURE;
254 if (pbl_hdr->rcwheader != RCW_HEADER)
255 return -FDT_ERR_BADSTRUCTURE;
260 static void pblimage_print_header(const void *ptr)
262 printf("Image Type: Freescale PBL Boot Image\n");
265 static void pblimage_set_header(void *ptr, struct stat *sbuf, int ifd,
266 struct image_tool_params *params)
268 /*nothing need to do, pbl_load_uboot takes care of whole file. */
271 /* pblimage parameters */
272 static struct image_type_params pblimage_params = {
273 .name = "Freescale PBL Boot Image support",
274 .header_size = sizeof(struct pbl_header),
275 .hdr = (void *)&pblimage_header,
276 .check_image_type = pblimage_check_image_types,
277 .verify_header = pblimage_verify_header,
278 .print_header = pblimage_print_header,
279 .set_header = pblimage_set_header,
282 void init_pbl_image_type(void)
285 register_image_type(&pblimage_params);