-
-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathcmd_sdupdate.c
executable file
·481 lines (412 loc) · 10.8 KB
/
cmd_sdupdate.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
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
81
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
247
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
288
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
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
/*
*SD update support
*/
#include <common.h>
#include <environment.h>
#include <command.h>
#include <malloc.h>
#include <image.h>
#include <asm/byteorder.h>
#include <asm/io.h>
#include <spi_flash.h>
#include <linux/mtd/mtd.h>
#include <fat.h>
#ifdef CONFIG_AUTO_UPDATE /* cover the whole file */
#ifdef CONFIG_AUTO_SD_UPDATE
#ifndef CONFIG_MMC
#error "should have defined CONFIG_MMC"
#endif
#include <mmc.h>
#endif
#undef AU_DEBUG
#undef debug
/*#define AU_DEBUG*/
#ifdef AU_DEBUG
#define debug(fmt, args...) printf(fmt, ##args)
#else
#define debug(fmt, args...)
#endif /* AU_DEBUG */
/* possible names of files on the medium. */
#define AU_UBOOT "autoupdate-uboot.img"
#define AU_KERNEL "autoupdate-kernel.img"
#define AU_ROOTFS "autoupdate-rootfs.img"
#define AU_FW "autoupdate-full.img"
struct flash_layout {
long start;
long end;
};
static struct spi_flash *flash;
struct medium_interface {
char name[20];
int (*init) (void);
void (*exit) (void);
};
/* layout of the FLASH. ST = start address, ND = end address. */
#define AU_FL_UBOOT_ST 0x0
#define AU_FL_UBOOT_ND 0x40000
#define AU_FL_KERNEL_ST 0x50000
#define AU_FL_KERNEL_ND 0x350000
#define AU_FL_ROOTFS_ST 0x350000
#define AU_FL_ROOTFS_ND 0xd50000
#define AU_FL_FW_ST 0x000000
#define AU_FL_FW_ND 0x1000000
static int au_stor_curr_dev; /* current device */
/* index of each file in the following arrays */
#define IDX_UBOOT 0
#define IDX_KERNEL 1
#define IDX_ROOTFS 2
#define IDX_FW 3
/* max. number of files which could interest us */
#define AU_MAXFILES 4
/* pointers to file names */
char *aufile[AU_MAXFILES] = {
AU_UBOOT,
AU_KERNEL,
AU_ROOTFS,
AU_FW
};
/* sizes of flash areas for each file */
long ausize[AU_MAXFILES] = {
AU_FL_UBOOT_ND - AU_FL_UBOOT_ST,
AU_FL_KERNEL_ND - AU_FL_KERNEL_ST,
AU_FL_ROOTFS_ND - AU_FL_ROOTFS_ST,
AU_FL_FW_ND - AU_FL_FW_ST
};
/* array of flash areas start and end addresses */
struct flash_layout aufl_layout[AU_MAXFILES] = {
{ AU_FL_UBOOT_ST, AU_FL_UBOOT_ND, },
{ AU_FL_KERNEL_ST, AU_FL_KERNEL_ND, },
{ AU_FL_ROOTFS_ST, AU_FL_ROOTFS_ND, },
{ AU_FL_FW_ST, AU_FL_FW_ND, },
};
/* where to load files into memory */
#define LOAD_ADDR ((unsigned char *)0x82000000)
/* the app is the largest image */
#define MAX_LOADSZ ausize[IDX_FW]
int LOAD_ID = -1; //default update all
static int au_check_cksum_valid(int idx, long nbytes)
{
image_header_t *hdr;
unsigned long checksum;
hdr = (image_header_t *)LOAD_ADDR;
if (nbytes != (sizeof(*hdr) + ntohl(hdr->ih_size))) {
printf("Image %s bad total SIZE\n", aufile[idx]);
return -1;
}
/* check the data CRC */
checksum = ntohl(hdr->ih_dcrc);
if (crc32(0, (unsigned char const *)(LOAD_ADDR + sizeof(*hdr)),
ntohl(hdr->ih_size)) != checksum) {
printf("Image %s bad data checksum\n", aufile[idx]);
return -1;
}
return 0;
}
static int au_check_header_valid(int idx, long nbytes)
{
image_header_t *hdr;
unsigned long checksum;
char env[20];
char auversion[20];
hdr = (image_header_t *)LOAD_ADDR;
/* check the easy ones first */
#if 0
#define CHECK_VALID_DEBUG
#else
#undef CHECK_VALID_DEBUG
#endif
#ifdef CHECK_VALID_DEBUG
printf("\nmagic %#x %#x\n", ntohl(hdr->ih_magic), IH_MAGIC);
printf("arch %#x %#x\n", hdr->ih_arch, IH_ARCH_MIPS);
printf("size %#x %#lx\n", ntohl(hdr->ih_size), nbytes);
printf("type %#x %#x\n", hdr->ih_type, IH_TYPE_FIRMWARE);
#endif
if (nbytes < sizeof(*hdr)) {
printf("Image %s bad header SIZE\n", aufile[idx]);
return -1;
}
if (ntohl(hdr->ih_magic) != IH_MAGIC || hdr->ih_arch != IH_ARCH_MIPS) {
printf("Image %s bad MAGIC or ARCH\n", aufile[idx]);
return -1;
}
/* check the hdr CRC */
checksum = ntohl(hdr->ih_hcrc);
hdr->ih_hcrc = 0;
if (crc32(0, (unsigned char const *)hdr, sizeof(*hdr)) != checksum) {
printf("Image %s bad header checksum\n", aufile[idx]);
return -1;
}
hdr->ih_hcrc = htonl(checksum);
/* check the type - could do this all in one gigantic if() */
if ((idx == IDX_UBOOT) && (hdr->ih_type != IH_TYPE_FIRMWARE)) {
printf("Image %s wrong type\n", aufile[idx]);
return -1;
}
if ((idx == IDX_KERNEL) && (hdr->ih_type != IH_TYPE_KERNEL)) {
printf("Image %s wrong type\n", aufile[idx]);
return -1;
}
if ((idx == IDX_ROOTFS) &&
(hdr->ih_type != IH_TYPE_RAMDISK) &&
(hdr->ih_type != IH_TYPE_FILESYSTEM)) {
printf("Image %s wrong type\n", aufile[idx]);
ausize[idx] = 0;
return -1;
}
if ((idx == IDX_FW) && (hdr->ih_type != IH_TYPE_FIRMWARE)) {
printf("Image %s wrong type\n", aufile[idx]);
return -1;
}
/* recycle checksum */
checksum = ntohl(hdr->ih_size);
/* for kernel and app the image header must also fit into flash */
if ((idx == IDX_KERNEL) && (idx == IH_TYPE_RAMDISK))
checksum += sizeof(*hdr);
/* check the size does not exceed space in flash. HUSH scripts */
/* all have ausize[] set to 0 */
if ((ausize[idx] != 0) && (ausize[idx] < checksum)) {
printf("Image %s is bigger than FLASH\n", aufile[idx]);
return -1;
}
sprintf(env, "%lx", (unsigned long)ntohl(hdr->ih_time));
/*setenv(auversion, env);*/
return 0;
}
static int au_do_update(int idx, long sz)
{
image_header_t *hdr;
unsigned long start, len;
unsigned long write_len;
int rc;
void *buf;
char *pbuf;
hdr = (image_header_t *)LOAD_ADDR;
// start = aufl_layout[idx].start;
// len = aufl_layout[idx].end - aufl_layout[idx].start;
start = ntohl(hdr->ih_load);
len = ntohl(hdr->ih_ep) - ntohl(hdr->ih_load);
/*
* erase the address range.
*/
printf("flash erase...\n");
rc = flash->erase(flash, start, len);
if (rc) {
printf("SPI flash sector erase failed\n");
return 1;
}
buf = map_physmem((unsigned long)LOAD_ADDR, len, MAP_WRBACK);
if (!buf) {
puts("Failed to map physical memory\n");
return 1;
}
/* strip the header - except for the kernel and ramdisk */
if (hdr->ih_type == IH_TYPE_RAMDISK) {
pbuf = buf;
write_len = sizeof(*hdr) + ntohl(hdr->ih_size);
} else {
pbuf = (buf + sizeof(*hdr));
write_len = ntohl(hdr->ih_size);
}
/* copy the data from RAM to FLASH */
printf("flash write...\n");
rc = flash->write(flash, start, write_len, pbuf);
if (rc) {
printf("SPI flash write failed, return %d\n", rc);
return 1;
}
/* check the dcrc of the copy */
if (crc32(0, (unsigned char const *)(buf + sizeof(*hdr)),
ntohl(hdr->ih_size)) != ntohl(hdr->ih_dcrc)) {
printf("Image %s Bad Data Checksum After COPY\n", aufile[idx]);
return -1;
}
unmap_physmem(buf, len);
return 0;
}
/*
* If none of the update file(u-boot, kernel or rootfs) was found
* in the medium, return -1;
* If u-boot has been updated, return 1;
* Others, return 0;
*/
static int update_to_flash(void)
{
int i = 0;
long sz;
int res, cnt;
int uboot_updated = 0;
int image_found = 0;
/* just loop thru all the possible files */
for (i = 0; i < AU_MAXFILES; i++) {
if (LOAD_ID != -1)
i = LOAD_ID;
/* just read the header */
sz = file_fat_read(aufile[i], LOAD_ADDR,
sizeof(image_header_t));
debug("read %s sz %ld hdr %d\n",
aufile[i], sz, sizeof(image_header_t));
if (sz <= 0 || sz < sizeof(image_header_t)) {
debug("%s not found\n", aufile[i]);
if (LOAD_ID != -1)
break;
else
continue;
}
image_found = 1;
if (au_check_header_valid(i, sz) < 0) {
debug("%s header not valid\n", aufile[i]);
if (LOAD_ID != -1)
break;
else
continue;
}
sz = file_fat_read(aufile[i], LOAD_ADDR, MAX_LOADSZ);
debug("read %s sz %ld hdr %d\n",
aufile[i], sz, sizeof(image_header_t));
if (sz <= 0 || sz <= sizeof(image_header_t)) {
debug("%s not found\n", aufile[i]);
if (LOAD_ID != -1)
break;
else
continue;
}
if (au_check_cksum_valid(i, sz) < 0) {
debug("%s checksum not valid\n", aufile[i]);
if (LOAD_ID != -1)
break;
else
continue;
}
/* If u-boot had been updated, we need to
* save current env to flash */
if (0 == strcmp((char *)AU_UBOOT, aufile[i]))
uboot_updated = 1;
/* this is really not a good idea, but it's what the */
/* customer wants. */
cnt = 0;
res = au_do_update(i, sz);
if (LOAD_ID != -1)
break;
}
if (1 == uboot_updated)
return 1;
if (1 == image_found)
return 0;
return -1;
}
/*
* This is called by board_init() after the hardware has been set up
* and is usable. Only if SPI flash initialization failed will this function
* return -1, otherwise it will return 0;
*/
int do_auto_update(cmd_tbl_t *cmdtp, int flag, int argc, char * const argv[])
{
block_dev_desc_t *stor_dev;
int old_ctrlc;
int j;
int state = -1;
long start = -1, end = 0;
if (argc == 1)
;/*to do*/
else if (argc == 2) {
LOAD_ID = simple_strtoul(argv[1], NULL, 16);
if (LOAD_ID < IDX_UBOOT || LOAD_ID > AU_MAXFILES) {
printf("unsupport id!\n");
return CMD_RET_USAGE;
}
} else if (argc == 4) {
LOAD_ID = simple_strtoul(argv[1], NULL, 16);
if (LOAD_ID < IDX_UBOOT || LOAD_ID > AU_MAXFILES) {
printf("unsupport id!\n");
return CMD_RET_USAGE;
}
start = simple_strtoul(argv[2], NULL, 16);
end = simple_strtoul(argv[3], NULL, 16);
if (start >= 0 && end && end > start) {
ausize[LOAD_ID] = end - start;
aufl_layout[LOAD_ID].start = start;
aufl_layout[LOAD_ID].end = end;
} else {
printf("error addr,use default\n");
}
} else {
return CMD_RET_USAGE;
}
debug("device name %s!\n", "mmc");
stor_dev = get_dev("mmc", 0);
if (NULL == stor_dev) {
debug("Unknow device type!\n");
return 0;
}
if (fat_register_device(stor_dev, 1) != 0) {
debug("Unable to use %s %d:%d for fatls\n",
"mmc",
au_stor_curr_dev,
1);
return -1;
}
if (file_fat_detectfs() != 0) {
debug("file_fat_detectfs failed\n");
return -1;
}
/*
* make sure that we see CTRL-C
* and save the old state
*/
old_ctrlc = disable_ctrlc(0);
/*
* CONFIG_SF_DEFAULT_SPEED=1000000,
* CONFIG_SF_DEFAULT_MODE=0x3
*/
flash = spi_flash_probe(0, 0, 1000000, 0x3);
if (!flash) {
printf("Failed to initialize SPI flash\n");
return -1;
}
state = update_to_flash();
/* restore the old state */
disable_ctrlc(old_ctrlc);
LOAD_ID = -1;
/*
* no update file found
*/
/*if (-1 == state)*/
/*continue;*/
/*
* update files have been found on current medium,
* so just break here
*/
if (state >= 0) {
if (flash->size > 8388608) {
printf("Setting partition layout for 16M flash \n");
run_command(
"run mtdpartsnor16m; setenv bootcmd ${bootcmdnor}; saveenv", 0);
}else{
printf("Setting partition layout for 8M flash \n");
run_command(
"run mtdpartsnor8m; setenv bootcmd ${bootcmdnor}; saveenv", 0);
}
}
/*
* If u-boot has been updated, it's better to save environment to flash
*/
if (1 == state) {
/*env_crc_update();*/
saveenv();
}
return 0;
}
U_BOOT_CMD(
sdupdate, 9, 1, do_auto_update,
"auto upgrade file from mmc to flash",
"LOAD_ID ADDR_START ADDR_END\n"
"LOAD_ID: 0-->u-boot\n"
" 1-->kernel\n"
" 2-->rootfs\n"
" 3-->full\n"
"ex:\n"
" sdupdate (update all)\n"
"or \n"
" sdupdate 0 0x0 0x40000"
);
#endif /* CONFIG_AUTO_UPDATE */