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