2 * Copyright 2012 Freescale Semiconductor, Inc.
4 * SPDX-License-Identifier: GPL-2.0+
11 * Initialize to an invalid value.
13 static uint32_t next_pbl_cmd = 0x82000000;
15 * need to store all bytes in memory for calculating crc32, then write the
16 * bytes to image file for PBL boot.
18 static unsigned char mem_buf[1000000];
19 static unsigned char *pmem_buf = mem_buf;
21 static char *fname = "Unknown";
22 static int lineno = -1;
23 static struct pbl_header pblimage_header;
29 } endian_test = { {'l', '?', '?', 'b'} };
31 #define ENDIANNESS ((char)endian_test.l)
34 * The PBL can load up to 64 bytes at a time, so we split the U-Boot
35 * image into 64 byte chunks. PBL needs a command for each piece, of
36 * the form "81xxxxxx", where "xxxxxx" is the offset. Calculate the
37 * start offset by subtracting the size of the u-boot image from the
38 * top of the allowable 24-bit range.
40 static void init_next_pbl_cmd(FILE *fp_uboot)
43 int fd = fileno(fp_uboot);
45 if (fstat(fd, &st) == -1) {
46 printf("Error: Could not determine u-boot image size. %s\n",
51 next_pbl_cmd = 0x82000000 - st.st_size;
54 static void generate_pbl_cmd(void)
56 uint32_t val = next_pbl_cmd;
60 for (i = 3; i >= 0; i--) {
61 *pmem_buf++ = (val >> (i * 8)) & 0xff;
66 static void pbl_fget(size_t size, FILE *stream)
71 while (size && (c_temp = fgetc(stream)) != EOF) {
72 c = (unsigned char)c_temp;
79 /* load split u-boot with PBI command 81xxxxxx. */
80 static void load_uboot(FILE *fp_uboot)
82 init_next_pbl_cmd(fp_uboot);
83 while (next_pbl_cmd < 0x82000000) {
85 pbl_fget(64, fp_uboot);
89 static void check_get_hexval(char *token)
94 if (!sscanf(token, "%x", &hexval)) {
95 printf("Error:%s[%d] - Invalid hex data(%s)\n", fname,
99 for (i = 3; i >= 0; i--) {
100 *pmem_buf++ = (hexval >> (i * 8)) & 0xff;
105 static void pbl_parser(char *name)
109 char *token, *saveptr1, *saveptr2;
113 fd = fopen(name, "r");
115 printf("Error:%s - Can't open\n", fname);
119 while ((getline(&line, &len, fd)) > 0) {
121 token = strtok_r(line, "\r\n", &saveptr1);
122 /* drop all lines with zero tokens (= empty lines) */
125 for (line = token;; line = NULL) {
126 token = strtok_r(line, " \t", &saveptr2);
129 /* Drop all text starting with '#' as comments */
132 check_get_hexval(token);
140 static uint32_t crc_table[256];
142 static void make_crc_table(void)
146 uint32_t poly; /* polynomial exclusive-or pattern */
149 * the polynomial used by PBL is 1 + x1 + x2 + x4 + x5 + x7 + x8 + x10
150 * + x11 + x12 + x16 + x22 + x23 + x26 + x32.
154 for (i = 0; i < 256; i++) {
156 for (j = 0; j < 8; j++) {
157 if (mask & 0x80000000)
158 mask = (mask << 1) ^ poly;
166 unsigned long pbl_crc32(unsigned long crc, const char *buf, uint32_t len)
168 uint32_t crc32_val = 0xffffffff;
174 for (i = 0; i < len; i++)
175 crc32_val = (crc32_val << 8) ^
176 crc_table[(crc32_val >> 24) ^ (*buf++ & 0xff)];
178 crc32_val = crc32_val ^ xor;
180 crc32_val += 0xffffffff;
186 static uint32_t reverse_byte(uint32_t val)
193 p1 = (unsigned char *)&temp;
194 for (j = 3; j >= 0; j--)
195 *p1++ = (val >> (j * 8)) & 0xff;
199 /* write end command and crc command to memory. */
200 static void add_end_cmd(void)
202 uint32_t pbl_end_cmd[4] = {0x09138000, 0x00000000,
203 0x091380c0, 0x00000000};
206 unsigned char *p = (unsigned char *)&pbl_end_cmd;
208 if (ENDIANNESS == 'l') {
209 for (i = 0; i < 4; i++)
210 pbl_end_cmd[i] = reverse_byte(pbl_end_cmd[i]);
213 for (i = 0; i < 16; i++) {
218 /* Add PBI CRC command. */
225 /* calculated CRC32 and write it to memory. */
226 crc32_pbl = pbl_crc32(0, (const char *)mem_buf, pbl_size);
227 *pmem_buf++ = (crc32_pbl >> 24) & 0xff;
228 *pmem_buf++ = (crc32_pbl >> 16) & 0xff;
229 *pmem_buf++ = (crc32_pbl >> 8) & 0xff;
230 *pmem_buf++ = (crc32_pbl) & 0xff;
233 if ((pbl_size % 16) != 0) {
234 for (i = 0; i < 8; i++) {
239 if ((pbl_size % 16 != 0)) {
240 printf("Error: Bad size of image file\n");
245 void pbl_load_uboot(int ifd, struct image_tool_params *params)
250 /* parse the rcw.cfg file. */
251 pbl_parser(params->imagename);
253 /* parse the pbi.cfg file. */
254 pbl_parser(params->imagename2);
256 fp_uboot = fopen(params->datafile, "r");
257 if (fp_uboot == NULL) {
258 printf("Error: %s open failed\n", params->datafile);
262 load_uboot(fp_uboot);
265 lseek(ifd, 0, SEEK_SET);
268 if (write(ifd, (const void *)&mem_buf, size) != size) {
269 fprintf(stderr, "Write error on %s: %s\n",
270 params->imagefile, strerror(errno));
275 static int pblimage_check_image_types(uint8_t type)
277 if (type == IH_TYPE_PBLIMAGE)
283 static int pblimage_verify_header(unsigned char *ptr, int image_size,
284 struct image_tool_params *params)
286 struct pbl_header *pbl_hdr = (struct pbl_header *) ptr;
288 /* Only a few checks can be done: search for magic numbers */
289 if (ENDIANNESS == 'l') {
290 if (pbl_hdr->preamble != reverse_byte(RCW_PREAMBLE))
291 return -FDT_ERR_BADSTRUCTURE;
293 if (pbl_hdr->rcwheader != reverse_byte(RCW_HEADER))
294 return -FDT_ERR_BADSTRUCTURE;
296 if (pbl_hdr->preamble != RCW_PREAMBLE)
297 return -FDT_ERR_BADSTRUCTURE;
299 if (pbl_hdr->rcwheader != RCW_HEADER)
300 return -FDT_ERR_BADSTRUCTURE;
305 static void pblimage_print_header(const void *ptr)
307 printf("Image Type: Freescale PBL Boot Image\n");
310 static void pblimage_set_header(void *ptr, struct stat *sbuf, int ifd,
311 struct image_tool_params *params)
313 /*nothing need to do, pbl_load_uboot takes care of whole file. */
316 /* pblimage parameters */
317 static struct image_type_params pblimage_params = {
318 .name = "Freescale PBL Boot Image support",
319 .header_size = sizeof(struct pbl_header),
320 .hdr = (void *)&pblimage_header,
321 .check_image_type = pblimage_check_image_types,
322 .verify_header = pblimage_verify_header,
323 .print_header = pblimage_print_header,
324 .set_header = pblimage_set_header,
327 void init_pbl_image_type(void)
330 register_image_type(&pblimage_params);