Tom Rini | 10e4779 | 2018-05-06 17:58:06 -0400 | [diff] [blame] | 1 | // SPDX-License-Identifier: GPL-2.0+ |
Shaohui Xie | ea65fd8 | 2012-08-10 02:49:35 +0000 | [diff] [blame] | 2 | /* |
Alison Wang | 3cfec8f | 2014-12-03 15:00:41 +0800 | [diff] [blame] | 3 | * Copyright 2012-2014 Freescale Semiconductor, Inc. |
Shaohui Xie | ea65fd8 | 2012-08-10 02:49:35 +0000 | [diff] [blame] | 4 | */ |
Guilherme Maciel Ferreira | 8ed4d1c | 2013-12-01 12:43:10 -0700 | [diff] [blame] | 5 | #include "imagetool.h" |
Shaohui Xie | ea65fd8 | 2012-08-10 02:49:35 +0000 | [diff] [blame] | 6 | #include <image.h> |
| 7 | #include "pblimage.h" |
Charles Manning | 71e994c | 2014-05-14 14:45:00 +1200 | [diff] [blame] | 8 | #include "pbl_crc32.h" |
Shaohui Xie | ea65fd8 | 2012-08-10 02:49:35 +0000 | [diff] [blame] | 9 | |
Alison Wang | 3cfec8f | 2014-12-03 15:00:41 +0800 | [diff] [blame] | 10 | #define roundup(x, y) ((((x) + ((y) - 1)) / (y)) * (y)) |
| 11 | #define PBL_ACS_CONT_CMD 0x81000000 |
| 12 | #define PBL_ADDR_24BIT_MASK 0x00ffffff |
| 13 | |
Shaohui Xie | ea65fd8 | 2012-08-10 02:49:35 +0000 | [diff] [blame] | 14 | /* |
Chris Packham | c253dfa | 2013-05-27 10:51:46 +1200 | [diff] [blame] | 15 | * Initialize to an invalid value. |
Shaohui Xie | ea65fd8 | 2012-08-10 02:49:35 +0000 | [diff] [blame] | 16 | */ |
Chris Packham | c253dfa | 2013-05-27 10:51:46 +1200 | [diff] [blame] | 17 | static uint32_t next_pbl_cmd = 0x82000000; |
Shaohui Xie | ea65fd8 | 2012-08-10 02:49:35 +0000 | [diff] [blame] | 18 | /* |
| 19 | * need to store all bytes in memory for calculating crc32, then write the |
| 20 | * bytes to image file for PBL boot. |
| 21 | */ |
Chris Packham | c253dfa | 2013-05-27 10:51:46 +1200 | [diff] [blame] | 22 | static unsigned char mem_buf[1000000]; |
Shaohui Xie | ea65fd8 | 2012-08-10 02:49:35 +0000 | [diff] [blame] | 23 | static unsigned char *pmem_buf = mem_buf; |
| 24 | static int pbl_size; |
| 25 | static char *fname = "Unknown"; |
| 26 | static int lineno = -1; |
| 27 | static struct pbl_header pblimage_header; |
Alison Wang | 3cfec8f | 2014-12-03 15:00:41 +0800 | [diff] [blame] | 28 | static int uboot_size; |
| 29 | static int arch_flag; |
| 30 | |
| 31 | static uint32_t pbl_cmd_initaddr; |
| 32 | static uint32_t pbi_crc_cmd1; |
| 33 | static uint32_t pbi_crc_cmd2; |
| 34 | static uint32_t pbl_end_cmd[4]; |
Shaohui Xie | ea65fd8 | 2012-08-10 02:49:35 +0000 | [diff] [blame] | 35 | |
| 36 | static union |
| 37 | { |
| 38 | char c[4]; |
| 39 | unsigned char l; |
| 40 | } endian_test = { {'l', '?', '?', 'b'} }; |
| 41 | |
| 42 | #define ENDIANNESS ((char)endian_test.l) |
| 43 | |
Chris Packham | c253dfa | 2013-05-27 10:51:46 +1200 | [diff] [blame] | 44 | /* |
| 45 | * The PBL can load up to 64 bytes at a time, so we split the U-Boot |
| 46 | * image into 64 byte chunks. PBL needs a command for each piece, of |
| 47 | * the form "81xxxxxx", where "xxxxxx" is the offset. Calculate the |
| 48 | * start offset by subtracting the size of the u-boot image from the |
| 49 | * top of the allowable 24-bit range. |
| 50 | */ |
Shaohui Xie | ea65fd8 | 2012-08-10 02:49:35 +0000 | [diff] [blame] | 51 | static void generate_pbl_cmd(void) |
| 52 | { |
| 53 | uint32_t val = next_pbl_cmd; |
| 54 | next_pbl_cmd += 0x40; |
| 55 | int i; |
| 56 | |
| 57 | for (i = 3; i >= 0; i--) { |
| 58 | *pmem_buf++ = (val >> (i * 8)) & 0xff; |
| 59 | pbl_size++; |
| 60 | } |
| 61 | } |
| 62 | |
| 63 | static void pbl_fget(size_t size, FILE *stream) |
| 64 | { |
Alison Wang | 3cfec8f | 2014-12-03 15:00:41 +0800 | [diff] [blame] | 65 | unsigned char c = 0xff; |
Shaohui Xie | ea65fd8 | 2012-08-10 02:49:35 +0000 | [diff] [blame] | 66 | int c_temp; |
| 67 | |
Alison Wang | 3cfec8f | 2014-12-03 15:00:41 +0800 | [diff] [blame] | 68 | while (size) { |
| 69 | c_temp = fgetc(stream); |
| 70 | if (c_temp != EOF) |
| 71 | c = (unsigned char)c_temp; |
| 72 | else if ((c_temp == EOF) && (arch_flag == IH_ARCH_ARM)) |
| 73 | c = 0xff; |
Shaohui Xie | ea65fd8 | 2012-08-10 02:49:35 +0000 | [diff] [blame] | 74 | *pmem_buf++ = c; |
| 75 | pbl_size++; |
| 76 | size--; |
| 77 | } |
| 78 | } |
| 79 | |
| 80 | /* load split u-boot with PBI command 81xxxxxx. */ |
| 81 | static void load_uboot(FILE *fp_uboot) |
| 82 | { |
Alison Wang | 3cfec8f | 2014-12-03 15:00:41 +0800 | [diff] [blame] | 83 | next_pbl_cmd = pbl_cmd_initaddr - uboot_size; |
| 84 | while (next_pbl_cmd < pbl_cmd_initaddr) { |
Shaohui Xie | ea65fd8 | 2012-08-10 02:49:35 +0000 | [diff] [blame] | 85 | generate_pbl_cmd(); |
| 86 | pbl_fget(64, fp_uboot); |
| 87 | } |
| 88 | } |
| 89 | |
| 90 | static void check_get_hexval(char *token) |
| 91 | { |
| 92 | uint32_t hexval; |
| 93 | int i; |
| 94 | |
| 95 | if (!sscanf(token, "%x", &hexval)) { |
| 96 | printf("Error:%s[%d] - Invalid hex data(%s)\n", fname, |
| 97 | lineno, token); |
| 98 | exit(EXIT_FAILURE); |
| 99 | } |
| 100 | for (i = 3; i >= 0; i--) { |
| 101 | *pmem_buf++ = (hexval >> (i * 8)) & 0xff; |
| 102 | pbl_size++; |
| 103 | } |
| 104 | } |
| 105 | |
| 106 | static void pbl_parser(char *name) |
| 107 | { |
| 108 | FILE *fd = NULL; |
| 109 | char *line = NULL; |
| 110 | char *token, *saveptr1, *saveptr2; |
| 111 | size_t len = 0; |
| 112 | |
| 113 | fname = name; |
| 114 | fd = fopen(name, "r"); |
| 115 | if (fd == NULL) { |
| 116 | printf("Error:%s - Can't open\n", fname); |
| 117 | exit(EXIT_FAILURE); |
| 118 | } |
| 119 | |
| 120 | while ((getline(&line, &len, fd)) > 0) { |
| 121 | lineno++; |
| 122 | token = strtok_r(line, "\r\n", &saveptr1); |
| 123 | /* drop all lines with zero tokens (= empty lines) */ |
| 124 | if (token == NULL) |
| 125 | continue; |
| 126 | for (line = token;; line = NULL) { |
| 127 | token = strtok_r(line, " \t", &saveptr2); |
| 128 | if (token == NULL) |
| 129 | break; |
| 130 | /* Drop all text starting with '#' as comments */ |
| 131 | if (token[0] == '#') |
| 132 | break; |
| 133 | check_get_hexval(token); |
| 134 | } |
| 135 | } |
| 136 | if (line) |
| 137 | free(line); |
| 138 | fclose(fd); |
| 139 | } |
| 140 | |
Shaohui Xie | ea65fd8 | 2012-08-10 02:49:35 +0000 | [diff] [blame] | 141 | static uint32_t reverse_byte(uint32_t val) |
| 142 | { |
| 143 | uint32_t temp; |
| 144 | unsigned char *p1; |
| 145 | int j; |
| 146 | |
| 147 | temp = val; |
| 148 | p1 = (unsigned char *)&temp; |
| 149 | for (j = 3; j >= 0; j--) |
| 150 | *p1++ = (val >> (j * 8)) & 0xff; |
| 151 | return temp; |
| 152 | } |
| 153 | |
| 154 | /* write end command and crc command to memory. */ |
| 155 | static void add_end_cmd(void) |
| 156 | { |
Shaohui Xie | ea65fd8 | 2012-08-10 02:49:35 +0000 | [diff] [blame] | 157 | uint32_t crc32_pbl; |
| 158 | int i; |
| 159 | unsigned char *p = (unsigned char *)&pbl_end_cmd; |
| 160 | |
| 161 | if (ENDIANNESS == 'l') { |
| 162 | for (i = 0; i < 4; i++) |
| 163 | pbl_end_cmd[i] = reverse_byte(pbl_end_cmd[i]); |
| 164 | } |
| 165 | |
| 166 | for (i = 0; i < 16; i++) { |
| 167 | *pmem_buf++ = *p++; |
| 168 | pbl_size++; |
| 169 | } |
| 170 | |
| 171 | /* Add PBI CRC command. */ |
| 172 | *pmem_buf++ = 0x08; |
Alison Wang | 3cfec8f | 2014-12-03 15:00:41 +0800 | [diff] [blame] | 173 | *pmem_buf++ = pbi_crc_cmd1; |
| 174 | *pmem_buf++ = pbi_crc_cmd2; |
Shaohui Xie | ea65fd8 | 2012-08-10 02:49:35 +0000 | [diff] [blame] | 175 | *pmem_buf++ = 0x40; |
| 176 | pbl_size += 4; |
| 177 | |
| 178 | /* calculated CRC32 and write it to memory. */ |
| 179 | crc32_pbl = pbl_crc32(0, (const char *)mem_buf, pbl_size); |
| 180 | *pmem_buf++ = (crc32_pbl >> 24) & 0xff; |
| 181 | *pmem_buf++ = (crc32_pbl >> 16) & 0xff; |
| 182 | *pmem_buf++ = (crc32_pbl >> 8) & 0xff; |
| 183 | *pmem_buf++ = (crc32_pbl) & 0xff; |
| 184 | pbl_size += 4; |
Shaohui Xie | ea65fd8 | 2012-08-10 02:49:35 +0000 | [diff] [blame] | 185 | } |
| 186 | |
Guilherme Maciel Ferreira | 8ed4d1c | 2013-12-01 12:43:10 -0700 | [diff] [blame] | 187 | void pbl_load_uboot(int ifd, struct image_tool_params *params) |
Shaohui Xie | ea65fd8 | 2012-08-10 02:49:35 +0000 | [diff] [blame] | 188 | { |
| 189 | FILE *fp_uboot; |
| 190 | int size; |
| 191 | |
| 192 | /* parse the rcw.cfg file. */ |
| 193 | pbl_parser(params->imagename); |
| 194 | |
| 195 | /* parse the pbi.cfg file. */ |
yuan linyu | d3873fd | 2017-02-26 08:38:27 +0800 | [diff] [blame] | 196 | if (params->imagename2[0] != '\0') |
| 197 | pbl_parser(params->imagename2); |
Shaohui Xie | ea65fd8 | 2012-08-10 02:49:35 +0000 | [diff] [blame] | 198 | |
yuan linyu | d3873fd | 2017-02-26 08:38:27 +0800 | [diff] [blame] | 199 | if (params->datafile) { |
| 200 | fp_uboot = fopen(params->datafile, "r"); |
| 201 | if (fp_uboot == NULL) { |
| 202 | printf("Error: %s open failed\n", params->datafile); |
| 203 | exit(EXIT_FAILURE); |
| 204 | } |
Shaohui Xie | ea65fd8 | 2012-08-10 02:49:35 +0000 | [diff] [blame] | 205 | |
yuan linyu | d3873fd | 2017-02-26 08:38:27 +0800 | [diff] [blame] | 206 | load_uboot(fp_uboot); |
| 207 | fclose(fp_uboot); |
| 208 | } |
Shaohui Xie | ea65fd8 | 2012-08-10 02:49:35 +0000 | [diff] [blame] | 209 | add_end_cmd(); |
Shaohui Xie | ea65fd8 | 2012-08-10 02:49:35 +0000 | [diff] [blame] | 210 | lseek(ifd, 0, SEEK_SET); |
| 211 | |
| 212 | size = pbl_size; |
| 213 | if (write(ifd, (const void *)&mem_buf, size) != size) { |
| 214 | fprintf(stderr, "Write error on %s: %s\n", |
| 215 | params->imagefile, strerror(errno)); |
| 216 | exit(EXIT_FAILURE); |
| 217 | } |
| 218 | } |
| 219 | |
| 220 | static int pblimage_check_image_types(uint8_t type) |
| 221 | { |
| 222 | if (type == IH_TYPE_PBLIMAGE) |
| 223 | return EXIT_SUCCESS; |
| 224 | else |
| 225 | return EXIT_FAILURE; |
| 226 | } |
| 227 | |
| 228 | static int pblimage_verify_header(unsigned char *ptr, int image_size, |
Guilherme Maciel Ferreira | 8ed4d1c | 2013-12-01 12:43:10 -0700 | [diff] [blame] | 229 | struct image_tool_params *params) |
Shaohui Xie | ea65fd8 | 2012-08-10 02:49:35 +0000 | [diff] [blame] | 230 | { |
| 231 | struct pbl_header *pbl_hdr = (struct pbl_header *) ptr; |
| 232 | |
| 233 | /* Only a few checks can be done: search for magic numbers */ |
| 234 | if (ENDIANNESS == 'l') { |
| 235 | if (pbl_hdr->preamble != reverse_byte(RCW_PREAMBLE)) |
| 236 | return -FDT_ERR_BADSTRUCTURE; |
| 237 | |
| 238 | if (pbl_hdr->rcwheader != reverse_byte(RCW_HEADER)) |
| 239 | return -FDT_ERR_BADSTRUCTURE; |
| 240 | } else { |
| 241 | if (pbl_hdr->preamble != RCW_PREAMBLE) |
| 242 | return -FDT_ERR_BADSTRUCTURE; |
| 243 | |
| 244 | if (pbl_hdr->rcwheader != RCW_HEADER) |
| 245 | return -FDT_ERR_BADSTRUCTURE; |
| 246 | } |
| 247 | return 0; |
| 248 | } |
| 249 | |
| 250 | static void pblimage_print_header(const void *ptr) |
| 251 | { |
| 252 | printf("Image Type: Freescale PBL Boot Image\n"); |
| 253 | } |
| 254 | |
| 255 | static void pblimage_set_header(void *ptr, struct stat *sbuf, int ifd, |
Guilherme Maciel Ferreira | 8ed4d1c | 2013-12-01 12:43:10 -0700 | [diff] [blame] | 256 | struct image_tool_params *params) |
Shaohui Xie | ea65fd8 | 2012-08-10 02:49:35 +0000 | [diff] [blame] | 257 | { |
| 258 | /*nothing need to do, pbl_load_uboot takes care of whole file. */ |
| 259 | } |
| 260 | |
Alison Wang | 3cfec8f | 2014-12-03 15:00:41 +0800 | [diff] [blame] | 261 | int pblimage_check_params(struct image_tool_params *params) |
| 262 | { |
| 263 | FILE *fp_uboot; |
| 264 | int fd; |
| 265 | struct stat st; |
| 266 | |
| 267 | if (!params) |
| 268 | return EXIT_FAILURE; |
| 269 | |
yuan linyu | d3873fd | 2017-02-26 08:38:27 +0800 | [diff] [blame] | 270 | if (params->datafile) { |
| 271 | fp_uboot = fopen(params->datafile, "r"); |
| 272 | if (fp_uboot == NULL) { |
| 273 | printf("Error: %s open failed\n", params->datafile); |
| 274 | exit(EXIT_FAILURE); |
| 275 | } |
| 276 | fd = fileno(fp_uboot); |
Alison Wang | 3cfec8f | 2014-12-03 15:00:41 +0800 | [diff] [blame] | 277 | |
yuan linyu | d3873fd | 2017-02-26 08:38:27 +0800 | [diff] [blame] | 278 | if (fstat(fd, &st) == -1) { |
| 279 | printf("Error: Could not determine u-boot image size. %s\n", |
| 280 | strerror(errno)); |
| 281 | exit(EXIT_FAILURE); |
| 282 | } |
Alison Wang | 3cfec8f | 2014-12-03 15:00:41 +0800 | [diff] [blame] | 283 | |
yuan linyu | d3873fd | 2017-02-26 08:38:27 +0800 | [diff] [blame] | 284 | /* For the variable size, pad it to 64 byte boundary */ |
| 285 | uboot_size = roundup(st.st_size, 64); |
| 286 | fclose(fp_uboot); |
| 287 | } |
Alison Wang | 3cfec8f | 2014-12-03 15:00:41 +0800 | [diff] [blame] | 288 | |
| 289 | if (params->arch == IH_ARCH_ARM) { |
| 290 | arch_flag = IH_ARCH_ARM; |
| 291 | pbi_crc_cmd1 = 0x61; |
| 292 | pbi_crc_cmd2 = 0; |
| 293 | pbl_cmd_initaddr = params->addr & PBL_ADDR_24BIT_MASK; |
| 294 | pbl_cmd_initaddr |= PBL_ACS_CONT_CMD; |
York Sun | 1fbf072 | 2017-08-15 11:14:41 -0700 | [diff] [blame] | 295 | pbl_cmd_initaddr += uboot_size; |
Alison Wang | 3cfec8f | 2014-12-03 15:00:41 +0800 | [diff] [blame] | 296 | pbl_end_cmd[0] = 0x09610000; |
| 297 | pbl_end_cmd[1] = 0x00000000; |
| 298 | pbl_end_cmd[2] = 0x096100c0; |
| 299 | pbl_end_cmd[3] = 0x00000000; |
| 300 | } else if (params->arch == IH_ARCH_PPC) { |
| 301 | arch_flag = IH_ARCH_PPC; |
| 302 | pbi_crc_cmd1 = 0x13; |
| 303 | pbi_crc_cmd2 = 0x80; |
| 304 | pbl_cmd_initaddr = 0x82000000; |
Zhao Qiang | 2374f31 | 2016-09-08 12:55:33 +0800 | [diff] [blame] | 305 | pbl_end_cmd[0] = 0x091380c0; |
Alison Wang | 3cfec8f | 2014-12-03 15:00:41 +0800 | [diff] [blame] | 306 | pbl_end_cmd[1] = 0x00000000; |
| 307 | pbl_end_cmd[2] = 0x091380c0; |
| 308 | pbl_end_cmd[3] = 0x00000000; |
| 309 | } |
| 310 | |
| 311 | next_pbl_cmd = pbl_cmd_initaddr; |
| 312 | return 0; |
| 313 | }; |
| 314 | |
Shaohui Xie | ea65fd8 | 2012-08-10 02:49:35 +0000 | [diff] [blame] | 315 | /* pblimage parameters */ |
Guilherme Maciel Ferreira | 28be1cf | 2015-01-15 02:48:07 -0200 | [diff] [blame] | 316 | U_BOOT_IMAGE_TYPE( |
| 317 | pblimage, |
| 318 | "Freescale PBL Boot Image support", |
| 319 | sizeof(struct pbl_header), |
| 320 | (void *)&pblimage_header, |
| 321 | pblimage_check_params, |
| 322 | pblimage_verify_header, |
| 323 | pblimage_print_header, |
| 324 | pblimage_set_header, |
| 325 | NULL, |
| 326 | pblimage_check_image_types, |
| 327 | NULL, |
| 328 | NULL |
| 329 | ); |