2 * Copyright 2012-2014 Freescale Semiconductor, Inc.
4 * SPDX-License-Identifier: GPL-2.0+
11 #define roundup(x, y) ((((x) + ((y) - 1)) / (y)) * (y))
12 #define PBL_ACS_CONT_CMD 0x81000000
13 #define PBL_ADDR_24BIT_MASK 0x00ffffff
16 * Initialize to an invalid value.
18 static uint32_t next_pbl_cmd
= 0x82000000;
20 * need to store all bytes in memory for calculating crc32, then write the
21 * bytes to image file for PBL boot.
23 static unsigned char mem_buf
[1000000];
24 static unsigned char *pmem_buf
= mem_buf
;
26 static char *fname
= "Unknown";
27 static int lineno
= -1;
28 static struct pbl_header pblimage_header
;
29 static int uboot_size
;
32 static uint32_t pbl_cmd_initaddr
;
33 static uint32_t pbi_crc_cmd1
;
34 static uint32_t pbi_crc_cmd2
;
35 static uint32_t pbl_end_cmd
[4];
41 } endian_test
= { {'l', '?', '?', 'b'} };
43 #define ENDIANNESS ((char)endian_test.l)
46 * The PBL can load up to 64 bytes at a time, so we split the U-Boot
47 * image into 64 byte chunks. PBL needs a command for each piece, of
48 * the form "81xxxxxx", where "xxxxxx" is the offset. Calculate the
49 * start offset by subtracting the size of the u-boot image from the
50 * top of the allowable 24-bit range.
52 static void generate_pbl_cmd(void)
54 uint32_t val
= next_pbl_cmd
;
58 for (i
= 3; i
>= 0; i
--) {
59 *pmem_buf
++ = (val
>> (i
* 8)) & 0xff;
64 static void pbl_fget(size_t size
, FILE *stream
)
66 unsigned char c
= 0xff;
70 c_temp
= fgetc(stream
);
72 c
= (unsigned char)c_temp
;
73 else if ((c_temp
== EOF
) && (arch_flag
== IH_ARCH_ARM
))
81 /* load split u-boot with PBI command 81xxxxxx. */
82 static void load_uboot(FILE *fp_uboot
)
84 next_pbl_cmd
= pbl_cmd_initaddr
- uboot_size
;
85 while (next_pbl_cmd
< pbl_cmd_initaddr
) {
87 pbl_fget(64, fp_uboot
);
91 static void check_get_hexval(char *token
)
96 if (!sscanf(token
, "%x", &hexval
)) {
97 printf("Error:%s[%d] - Invalid hex data(%s)\n", fname
,
101 for (i
= 3; i
>= 0; i
--) {
102 *pmem_buf
++ = (hexval
>> (i
* 8)) & 0xff;
107 static void pbl_parser(char *name
)
111 char *token
, *saveptr1
, *saveptr2
;
115 fd
= fopen(name
, "r");
117 printf("Error:%s - Can't open\n", fname
);
121 while ((getline(&line
, &len
, fd
)) > 0) {
123 token
= strtok_r(line
, "\r\n", &saveptr1
);
124 /* drop all lines with zero tokens (= empty lines) */
127 for (line
= token
;; line
= NULL
) {
128 token
= strtok_r(line
, " \t", &saveptr2
);
131 /* Drop all text starting with '#' as comments */
134 check_get_hexval(token
);
142 static uint32_t reverse_byte(uint32_t val
)
149 p1
= (unsigned char *)&temp
;
150 for (j
= 3; j
>= 0; j
--)
151 *p1
++ = (val
>> (j
* 8)) & 0xff;
155 /* write end command and crc command to memory. */
156 static void add_end_cmd(void)
160 unsigned char *p
= (unsigned char *)&pbl_end_cmd
;
162 if (ENDIANNESS
== 'l') {
163 for (i
= 0; i
< 4; i
++)
164 pbl_end_cmd
[i
] = reverse_byte(pbl_end_cmd
[i
]);
167 for (i
= 0; i
< 16; i
++) {
172 /* Add PBI CRC command. */
174 *pmem_buf
++ = pbi_crc_cmd1
;
175 *pmem_buf
++ = pbi_crc_cmd2
;
179 /* calculated CRC32 and write it to memory. */
180 crc32_pbl
= pbl_crc32(0, (const char *)mem_buf
, pbl_size
);
181 *pmem_buf
++ = (crc32_pbl
>> 24) & 0xff;
182 *pmem_buf
++ = (crc32_pbl
>> 16) & 0xff;
183 *pmem_buf
++ = (crc32_pbl
>> 8) & 0xff;
184 *pmem_buf
++ = (crc32_pbl
) & 0xff;
188 void pbl_load_uboot(int ifd
, struct image_tool_params
*params
)
193 /* parse the rcw.cfg file. */
194 pbl_parser(params
->imagename
);
196 /* parse the pbi.cfg file. */
197 pbl_parser(params
->imagename2
);
199 fp_uboot
= fopen(params
->datafile
, "r");
200 if (fp_uboot
== NULL
) {
201 printf("Error: %s open failed\n", params
->datafile
);
205 load_uboot(fp_uboot
);
208 lseek(ifd
, 0, SEEK_SET
);
211 if (write(ifd
, (const void *)&mem_buf
, size
) != size
) {
212 fprintf(stderr
, "Write error on %s: %s\n",
213 params
->imagefile
, strerror(errno
));
218 static int pblimage_check_image_types(uint8_t type
)
220 if (type
== IH_TYPE_PBLIMAGE
)
226 static int pblimage_verify_header(unsigned char *ptr
, int image_size
,
227 struct image_tool_params
*params
)
229 struct pbl_header
*pbl_hdr
= (struct pbl_header
*) ptr
;
231 /* Only a few checks can be done: search for magic numbers */
232 if (ENDIANNESS
== 'l') {
233 if (pbl_hdr
->preamble
!= reverse_byte(RCW_PREAMBLE
))
234 return -FDT_ERR_BADSTRUCTURE
;
236 if (pbl_hdr
->rcwheader
!= reverse_byte(RCW_HEADER
))
237 return -FDT_ERR_BADSTRUCTURE
;
239 if (pbl_hdr
->preamble
!= RCW_PREAMBLE
)
240 return -FDT_ERR_BADSTRUCTURE
;
242 if (pbl_hdr
->rcwheader
!= RCW_HEADER
)
243 return -FDT_ERR_BADSTRUCTURE
;
248 static void pblimage_print_header(const void *ptr
)
250 printf("Image Type: Freescale PBL Boot Image\n");
253 static void pblimage_set_header(void *ptr
, struct stat
*sbuf
, int ifd
,
254 struct image_tool_params
*params
)
256 /*nothing need to do, pbl_load_uboot takes care of whole file. */
259 int pblimage_check_params(struct image_tool_params
*params
)
268 fp_uboot
= fopen(params
->datafile
, "r");
269 if (fp_uboot
== NULL
) {
270 printf("Error: %s open failed\n", params
->datafile
);
273 fd
= fileno(fp_uboot
);
275 if (fstat(fd
, &st
) == -1) {
276 printf("Error: Could not determine u-boot image size. %s\n",
281 /* For the variable size, we need to pad it to 64 byte boundary */
282 uboot_size
= roundup(st
.st_size
, 64);
284 if (params
->arch
== IH_ARCH_ARM
) {
285 arch_flag
= IH_ARCH_ARM
;
288 pbl_cmd_initaddr
= params
->addr
& PBL_ADDR_24BIT_MASK
;
289 pbl_cmd_initaddr
|= PBL_ACS_CONT_CMD
;
290 pbl_cmd_initaddr
|= uboot_size
;
291 pbl_end_cmd
[0] = 0x09610000;
292 pbl_end_cmd
[1] = 0x00000000;
293 pbl_end_cmd
[2] = 0x096100c0;
294 pbl_end_cmd
[3] = 0x00000000;
295 } else if (params
->arch
== IH_ARCH_PPC
) {
296 arch_flag
= IH_ARCH_PPC
;
299 pbl_cmd_initaddr
= 0x82000000;
300 pbl_end_cmd
[0] = 0x09138000;
301 pbl_end_cmd
[1] = 0x00000000;
302 pbl_end_cmd
[2] = 0x091380c0;
303 pbl_end_cmd
[3] = 0x00000000;
306 next_pbl_cmd
= pbl_cmd_initaddr
;
310 /* pblimage parameters */
313 "Freescale PBL Boot Image support",
314 sizeof(struct pbl_header
),
315 (void *)&pblimage_header
,
316 pblimage_check_params
,
317 pblimage_verify_header
,
318 pblimage_print_header
,
321 pblimage_check_image_types
,