Blame view

tools/pblimage.c 7.26 KB
5d898a00f   Shaohui Xie   powerpc/CoreNet: ...
1
  /*
94cb17d07   Alison Wang   ls102xa: pblimage...
2
   * Copyright 2012-2014 Freescale Semiconductor, Inc.
5d898a00f   Shaohui Xie   powerpc/CoreNet: ...
3
   *
1a4596601   Wolfgang Denk   Add GPL-2.0+ SPDX...
4
   * SPDX-License-Identifier:	GPL-2.0+
5d898a00f   Shaohui Xie   powerpc/CoreNet: ...
5
   */
f86ed6a8d   Guilherme Maciel Ferreira   tools: moved code...
6
  #include "imagetool.h"
5d898a00f   Shaohui Xie   powerpc/CoreNet: ...
7
8
  #include <image.h>
  #include "pblimage.h"
abbc67eed   Charles Manning   mkimage : Split o...
9
  #include "pbl_crc32.h"
5d898a00f   Shaohui Xie   powerpc/CoreNet: ...
10

94cb17d07   Alison Wang   ls102xa: pblimage...
11
12
13
  #define roundup(x, y)		((((x) + ((y) - 1)) / (y)) * (y))
  #define PBL_ACS_CONT_CMD	0x81000000
  #define PBL_ADDR_24BIT_MASK	0x00ffffff
5d898a00f   Shaohui Xie   powerpc/CoreNet: ...
14
  /*
0d3efd805   Chris Packham   powerpc/CoreNet: ...
15
   * Initialize to an invalid value.
5d898a00f   Shaohui Xie   powerpc/CoreNet: ...
16
   */
0d3efd805   Chris Packham   powerpc/CoreNet: ...
17
  static uint32_t next_pbl_cmd = 0x82000000;
5d898a00f   Shaohui Xie   powerpc/CoreNet: ...
18
19
20
21
  /*
   * need to store all bytes in memory for calculating crc32, then write the
   * bytes to image file for PBL boot.
   */
0d3efd805   Chris Packham   powerpc/CoreNet: ...
22
  static unsigned char mem_buf[1000000];
5d898a00f   Shaohui Xie   powerpc/CoreNet: ...
23
24
25
26
27
  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;
94cb17d07   Alison Wang   ls102xa: pblimage...
28
29
30
31
32
33
34
  static int uboot_size;
  static int arch_flag;
  
  static uint32_t pbl_cmd_initaddr;
  static uint32_t pbi_crc_cmd1;
  static uint32_t pbi_crc_cmd2;
  static uint32_t pbl_end_cmd[4];
5d898a00f   Shaohui Xie   powerpc/CoreNet: ...
35
36
37
38
39
40
41
42
  
  static union
  {
  	char c[4];
  	unsigned char l;
  } endian_test = { {'l', '?', '?', 'b'} };
  
  #define ENDIANNESS ((char)endian_test.l)
0d3efd805   Chris Packham   powerpc/CoreNet: ...
43
44
45
46
47
48
49
  /*
   * 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.
   */
5d898a00f   Shaohui Xie   powerpc/CoreNet: ...
50
51
52
53
54
55
56
57
58
59
60
61
62
63
  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)
  {
94cb17d07   Alison Wang   ls102xa: pblimage...
64
  	unsigned char c = 0xff;
5d898a00f   Shaohui Xie   powerpc/CoreNet: ...
65
  	int c_temp;
94cb17d07   Alison Wang   ls102xa: pblimage...
66
67
68
69
70
71
  	while (size) {
  		c_temp = fgetc(stream);
  		if (c_temp != EOF)
  			c = (unsigned char)c_temp;
  		else if ((c_temp == EOF) && (arch_flag == IH_ARCH_ARM))
  			c = 0xff;
5d898a00f   Shaohui Xie   powerpc/CoreNet: ...
72
73
74
75
76
77
78
79
80
  		*pmem_buf++ = c;
  		pbl_size++;
  		size--;
  	}
  }
  
  /* load split u-boot with PBI command 81xxxxxx. */
  static void load_uboot(FILE *fp_uboot)
  {
94cb17d07   Alison Wang   ls102xa: pblimage...
81
82
  	next_pbl_cmd = pbl_cmd_initaddr - uboot_size;
  	while (next_pbl_cmd < pbl_cmd_initaddr) {
5d898a00f   Shaohui Xie   powerpc/CoreNet: ...
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
  		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);
  }
5d898a00f   Shaohui Xie   powerpc/CoreNet: ...
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
  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)
  {
5d898a00f   Shaohui Xie   powerpc/CoreNet: ...
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
  	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;
94cb17d07   Alison Wang   ls102xa: pblimage...
173
174
  	*pmem_buf++ = pbi_crc_cmd1;
  	*pmem_buf++ = pbi_crc_cmd2;
5d898a00f   Shaohui Xie   powerpc/CoreNet: ...
175
176
177
178
179
180
181
182
183
184
  	*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;
5d898a00f   Shaohui Xie   powerpc/CoreNet: ...
185
  }
f86ed6a8d   Guilherme Maciel Ferreira   tools: moved code...
186
  void pbl_load_uboot(int ifd, struct image_tool_params *params)
5d898a00f   Shaohui Xie   powerpc/CoreNet: ...
187
188
189
190
191
192
193
194
  {
  	FILE *fp_uboot;
  	int size;
  
  	/* parse the rcw.cfg file. */
  	pbl_parser(params->imagename);
  
  	/* parse the pbi.cfg file. */
33ed57495   yuan linyu   tools: plbimage s...
195
196
197
198
199
200
201
202
203
204
  	if (params->imagename2[0] != '\0')
  		pbl_parser(params->imagename2);
  
  	if (params->datafile) {
  		fp_uboot = fopen(params->datafile, "r");
  		if (fp_uboot == NULL) {
  			printf("Error: %s open failed
  ", params->datafile);
  			exit(EXIT_FAILURE);
  		}
5d898a00f   Shaohui Xie   powerpc/CoreNet: ...
205

33ed57495   yuan linyu   tools: plbimage s...
206
207
  		load_uboot(fp_uboot);
  		fclose(fp_uboot);
5d898a00f   Shaohui Xie   powerpc/CoreNet: ...
208
  	}
5d898a00f   Shaohui Xie   powerpc/CoreNet: ...
209
  	add_end_cmd();
5d898a00f   Shaohui Xie   powerpc/CoreNet: ...
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
  	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   Guilherme Maciel Ferreira   tools: moved code...
230
  			struct image_tool_params *params)
5d898a00f   Shaohui Xie   powerpc/CoreNet: ...
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
  {
  	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   Guilherme Maciel Ferreira   tools: moved code...
258
  				struct image_tool_params *params)
5d898a00f   Shaohui Xie   powerpc/CoreNet: ...
259
260
261
  {
  	/*nothing need to do, pbl_load_uboot takes care of whole file. */
  }
94cb17d07   Alison Wang   ls102xa: pblimage...
262
263
264
265
266
267
268
269
  int pblimage_check_params(struct image_tool_params *params)
  {
  	FILE *fp_uboot;
  	int fd;
  	struct stat st;
  
  	if (!params)
  		return EXIT_FAILURE;
33ed57495   yuan linyu   tools: plbimage s...
270
271
272
273
274
275
276
277
  	if (params->datafile) {
  		fp_uboot = fopen(params->datafile, "r");
  		if (fp_uboot == NULL) {
  			printf("Error: %s open failed
  ", params->datafile);
  			exit(EXIT_FAILURE);
  		}
  		fd = fileno(fp_uboot);
94cb17d07   Alison Wang   ls102xa: pblimage...
278

33ed57495   yuan linyu   tools: plbimage s...
279
280
281
282
283
284
  		if (fstat(fd, &st) == -1) {
  			printf("Error: Could not determine u-boot image size. %s
  ",
  			       strerror(errno));
  			exit(EXIT_FAILURE);
  		}
94cb17d07   Alison Wang   ls102xa: pblimage...
285

33ed57495   yuan linyu   tools: plbimage s...
286
287
288
289
  		/* For the variable size, pad it to 64 byte boundary */
  		uboot_size = roundup(st.st_size, 64);
  		fclose(fp_uboot);
  	}
94cb17d07   Alison Wang   ls102xa: pblimage...
290
291
292
293
294
295
296
  
  	if (params->arch == IH_ARCH_ARM) {
  		arch_flag = IH_ARCH_ARM;
  		pbi_crc_cmd1 = 0x61;
  		pbi_crc_cmd2 = 0;
  		pbl_cmd_initaddr = params->addr & PBL_ADDR_24BIT_MASK;
  		pbl_cmd_initaddr |= PBL_ACS_CONT_CMD;
05e35d26d   York Sun   tools: pblimage: ...
297
  		pbl_cmd_initaddr += uboot_size;
94cb17d07   Alison Wang   ls102xa: pblimage...
298
299
300
301
302
303
304
305
306
  		pbl_end_cmd[0] = 0x09610000;
  		pbl_end_cmd[1] = 0x00000000;
  		pbl_end_cmd[2] = 0x096100c0;
  		pbl_end_cmd[3] = 0x00000000;
  	} else if (params->arch == IH_ARCH_PPC) {
  		arch_flag = IH_ARCH_PPC;
  		pbi_crc_cmd1 = 0x13;
  		pbi_crc_cmd2 = 0x80;
  		pbl_cmd_initaddr = 0x82000000;
c5938c10e   Zhao Qiang   pbl: use "wait" c...
307
  		pbl_end_cmd[0] = 0x091380c0;
94cb17d07   Alison Wang   ls102xa: pblimage...
308
309
310
311
312
313
314
315
  		pbl_end_cmd[1] = 0x00000000;
  		pbl_end_cmd[2] = 0x091380c0;
  		pbl_end_cmd[3] = 0x00000000;
  	}
  
  	next_pbl_cmd = pbl_cmd_initaddr;
  	return 0;
  };
5d898a00f   Shaohui Xie   powerpc/CoreNet: ...
316
  /* pblimage parameters */
a93648d19   Guilherme Maciel Ferreira   imagetool: replac...
317
318
319
320
321
322
323
324
325
326
327
328
329
330
  U_BOOT_IMAGE_TYPE(
  	pblimage,
  	"Freescale PBL Boot Image support",
  	sizeof(struct pbl_header),
  	(void *)&pblimage_header,
  	pblimage_check_params,
  	pblimage_verify_header,
  	pblimage_print_header,
  	pblimage_set_header,
  	NULL,
  	pblimage_check_image_types,
  	NULL,
  	NULL
  );