forked from MarvellEmbeddedProcessors/u-boot-marvell
/
cmd_bubt.c
executable file
·576 lines (487 loc) · 14.9 KB
/
cmd_bubt.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
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
/*******************************************************************************
Copyright (C) Marvell International Ltd. and its affiliates
********************************************************************************
Marvell GPL License Option
If you received this File from Marvell, you may opt to use, redistribute and/or
modify this File in accordance with the terms and conditions of the General
Public License Version 2, June 1991 (the "GPL License"), a copy of which is
available along with the File in the license.txt file or by writing to the Free
Software Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 or
on the worldwide web at http://www.gnu.org/licenses/gpl.txt.
THE FILE IS DISTRIBUTED AS-IS, WITHOUT WARRANTY OF ANY KIND, AND THE IMPLIED
WARRANTIES OF MERCHANTABILITY OR FITNESS FOR A PARTICULAR PURPOSE ARE EXPRESSLY
DISCLAIMED. The GPL License provides additional details about this warranty
disclaimer.
*******************************************************************************/
#include <config.h>
#include <common.h>
#include "mvCommon.h"
#include <command.h>
#include <net.h>
#include <environment.h>
#if defined(MV_INCLUDE_USB)
#include <usb.h>
#endif
#include <fs.h>
#if defined(CONFIG_CMD_NAND)
#include <nand.h>
extern nand_info_t nand_info[]; /* info for NAND chips */
#endif
#ifdef CONFIG_CMD_SF
#include <spi_flash.h>
extern struct spi_flash *flash;
#endif
#ifdef CONFIG_CMD_FLASH
#include <flash.h>
extern flash_info_t flash_info[]; /* info for FLASH chips */
#endif
#if defined(MV_MMC_BOOT)
#include <mmc.h>
extern ulong mmc_bwrite(int dev_num, lbaint_t start, lbaint_t blkcnt, const void *src);
#endif
#if 0 /* def MV_NOR_BOOT */
static unsigned int flash_in_which_sec(flash_info_t *fl,unsigned int offset)
{
unsigned int sec_num;
if(NULL == fl)
return 0xFFFFFFFF;
for( sec_num = 0; sec_num < fl->sector_count ; sec_num++){
/* If last sector*/
if (sec_num == fl->sector_count -1)
{
if((offset >= fl->start[sec_num]) &&
(offset <= (fl->size + fl->start[0] - 1)) )
{
return sec_num;
}
}
else
{
if((offset >= fl->start[sec_num]) &&
(offset < fl->start[sec_num + 1]) )
{
return sec_num;
}
}
}
/* return illegal sector Number */
return 0xFFFFFFFF;
}
#endif /* !defined(MV_NOR_BOOT) */
#ifdef MV_INCLUDE_USB
/*
* Load u-boot bin file from usb device
* file_name is the name of u-boot file
*/
int load_from_usb(const char* file_name)
{
const char *addr_str;
unsigned long addr;
int filesize = 0;
usb_stop();
printf("(Re)start USB...\n");
if (usb_init() < 0) {
printf("usb_init failed\n");
return 0;
}
/* try to recognize storage devices immediately */
if (-1 == usb_stor_scan(1)) {
printf("USB storage device not found\n");
return 0;
}
/* always load from usb 0 */
if (fs_set_blk_dev("usb", "0", FS_TYPE_ANY))
{
printf("USB 0 not found\n");
return 0;
}
addr_str = getenv("loadaddr");
if (addr_str != NULL)
addr = simple_strtoul(addr_str, NULL, 16);
else
addr = CONFIG_SYS_LOAD_ADDR;
filesize = fs_read(file_name, addr, 0, 0);
return filesize;
}
#endif
/*
* Extract arguments from bubt command line
* argc, argv are the input arguments of bubt command line
* loadfrom is pointer to the extracted argument: from where to load the u-boot bin file
*/
MV_STATUS fetch_bubt_cmd_args(int argc, char * const argv[], MV_U32 *loadfrom)
{
*loadfrom = 0;
/* bubt */
if (argc < 2) {
copy_filename (BootFile, "u-boot.bin", sizeof(BootFile));
printf("Using default filename \"u-boot.bin\" \n");
}
/* "bubt filename" or "bubt destination"*/
else if (argc == 2) {
if ((0 == strcmp(argv[1], "spi")) || (0 == strcmp(argv[1], "nand"))
|| (0 == strcmp(argv[1], "nor")))
{
copy_filename (BootFile, "u-boot.bin", sizeof(BootFile));
printf("Using default filename \"u-boot.bin\" \n");
}
else
{
copy_filename (BootFile, argv[1], sizeof(BootFile));
}
}
/* "bubt filename destination" or "bubt destination source" */
else if (argc == 3) {
if ((0 == strcmp(argv[1], "spi")) || (0 == strcmp(argv[1], "nand"))
|| (0 == strcmp(argv[1], "nor")))
{
copy_filename (BootFile, "u-boot.bin", sizeof(BootFile));
printf("Using default filename \"u-boot.bin\" \n");
if (0 == strcmp("usb", argv[2])) {
#ifdef MV_INCLUDE_USB
*loadfrom = 1;
#else
printf("Error: USB is not supported on current machine\n");
return MV_ERROR;
#endif
}
}
else
{
copy_filename (BootFile, argv[1], sizeof(BootFile));
}
}
/* "bubt filename destination source" */
else
{
copy_filename (BootFile, argv[1], sizeof(BootFile));
if (0 == strcmp("usb", argv[3])) {
#ifdef MV_INCLUDE_USB
*loadfrom = 1;
#else
printf("Error: USB is not supported on current machine\n");
return MV_ERROR;
#endif
}
}
return MV_OK;
}
/*
* Load u-boot bin file into ram from external device: tftp, usb or other devices
* loadfrom specifies the source device: tftp, usb or other devices
* (currently only tftp and usb are supported )
*/
int fetch_uboot_file (int loadfrom)
{
int filesize = 0;
switch (loadfrom) {
#ifdef MV_INCLUDE_USB
case 1:
{
filesize = load_from_usb(BootFile);
if (filesize <= 0)
{
printf("Failed to read file %s\n", BootFile);
return 0;
}
break;
}
#endif
default:
{
filesize = NetLoop(TFTPGET);
printf("Checking file size:");
if (filesize == -1)
{
printf("\t[Fail]\n");
return 0;
}
break;
}
}
return filesize;
}
#if defined(MV_NAND_BOOT)
/* Boot from NAND flash */
/* Write u-boot image into the nand flash */
int nand_burn_uboot_cmd(cmd_tbl_t *cmdtp, int flag, int argc, char * const argv[])
{
int filesize = 0;
MV_U32 ret = 0;
extern char console_buffer[];
nand_info_t *nand = &nand_info[0];
size_t blocksize = nand_info[0].erasesize;
size_t env_offset = CONFIG_ENV_OFFSET;
size_t size = CONFIG_UBOOT_SIZE;
size_t offset = 0;
MV_U32 loadfrom = 0; /* 0 - from tftp, 1 - from USB */
/* Align U-Boot size to currently used blocksize */
size = ((size + (blocksize - 1)) & (~(blocksize-1)));
#if defined(CONFIG_SKIP_BAD_BLOCK)
int i = 0;
int sum = 0;
while(i * blocksize < nand_info[0].size) {
if (!nand_block_isbad(&nand_info[0], (i * blocksize)))
sum += blocksize;
else {
sum = 0;
offset = (i + 1) * blocksize;
}
if (sum >= size)
break;
i++;
}
#endif
/* verify requested source is valid */
if (fetch_bubt_cmd_args(argc, argv, &loadfrom) != MV_OK)
return 0;
if ((filesize = fetch_uboot_file (loadfrom)) <= 0)
return 0;
if (filesize > CONFIG_UBOOT_SIZE) {
printf("Boot image is too big. Maximum size is %d bytes\n", CONFIG_UBOOT_SIZE);
return 0;
}
printf("\t[Done]\n");
printf("Override Env parameters to default? [y/N]");
readline(" ");
if( strcmp(console_buffer,"Y") == 0 ||
strcmp(console_buffer,"yes") == 0 ||
strcmp(console_buffer,"y") == 0 ) {
printf("Erasing 0x%x - 0x%x:",env_offset, env_offset + CONFIG_ENV_RANGE);
nand_erase(nand, env_offset, CONFIG_ENV_RANGE);
printf("\t[Done]\n");
}
printf("Erasing 0x%x - 0x%x: ", offset, offset + size);
nand_erase(nand, offset, size);
printf("\t[Done]\n");
printf("Writing image to NAND:");
ret = nand_write(nand, offset, &size, (u_char *)load_addr);
if (ret)
printf("\t[Fail]\n");
else
printf("\t[Done]\n");
return 1;
}
U_BOOT_CMD(
bubt, 4, 1, nand_burn_uboot_cmd,
"bubt - Burn an image on the Boot Nand Flash.\n",
"[file-name] [destination [source]] \n"
"\tBurn a binary image on the Boot Flash, default file-name is u-boot.bin .\n"
"\tdestination is nand, spi or nor.\n"
"\tsource can be tftp or usb, default is tftp.\n"
);
#endif /* defined(CONFIG_NAND_BOOT) */
#if defined(MV_SPI_BOOT)
/* Boot from SPI flash */
/* Write u-boot image into the SPI flash */
int spi_burn_uboot_cmd(cmd_tbl_t *cmdtp, int flag, int argc, char * const argv[])
{
int filesize = 0;
MV_U32 ret = 0;
extern char console_buffer[];
load_addr = CONFIG_SYS_LOAD_ADDR;
MV_U32 loadfrom = 0; /* 0 - from tftp, 1 - from USB */
if(!flash) {
flash = spi_flash_probe(CONFIG_ENV_SPI_BUS, CONFIG_ENV_SPI_CS,
CONFIG_SF_DEFAULT_SPEED, CONFIG_SF_DEFAULT_MODE);
if (!flash) {
printf("Failed to probe SPI Flash\n");
set_default_env("!spi_flash_probe() failed");
return 0;
}
}
/* verify requested source is valid */
if (fetch_bubt_cmd_args(argc, argv, &loadfrom) != MV_OK)
return 0;
if ((filesize = fetch_uboot_file (loadfrom)) <= 0)
return 0;
printf("\t\t[Done]\n");
printf("Override Env parameters to default? [y/N]");
readline(" ");
#ifdef CONFIG_SPI_FLASH_PROTECTION
printf("Unprotecting flash:");
spi_flash_protect(flash, 0);
printf("\t\t[Done]\n");
#endif
if( strcmp(console_buffer,"Y") == 0 ||
strcmp(console_buffer,"yes") == 0 ||
strcmp(console_buffer,"y") == 0 ) {
printf("Erasing 0x%x - 0x%x:",CONFIG_ENV_OFFSET, CONFIG_ENV_OFFSET + CONFIG_ENV_SIZE);
spi_flash_erase(flash, CONFIG_ENV_OFFSET, CONFIG_ENV_SIZE);
printf("\t[Done]\n");
}
if (filesize > CONFIG_ENV_OFFSET)
{
printf("Error: Image size (%x) exceeds environment variables offset (%x). ",filesize,CONFIG_ENV_OFFSET);
return 0;
}
printf("Erasing 0x%x - 0x%x: ",0, 0 + CONFIG_ENV_OFFSET);
spi_flash_erase(flash, 0, CONFIG_ENV_OFFSET);
printf("\t\t[Done]\n");
printf("Writing image to flash:");
ret = spi_flash_write(flash, 0, filesize, (const void *)load_addr);
if (ret)
printf("\t\t[Err!]\n");
else
printf("\t\t[Done]\n");
#ifdef CONFIG_SPI_FLASH_PROTECTION
printf("Protecting flash:");
spi_flash_protect(flash, 1);
printf("\t\t[Done]\n");
#endif
return 1;
}
U_BOOT_CMD(
bubt, 4, 1, spi_burn_uboot_cmd,
"bubt - Burn an image on the Boot SPI Flash.\n",
" file-name \n"
"[file-name] [destination [source]] \n"
"\tBurn a binary image on the Boot Flash, default file-name is u-boot.bin .\n"
"\tdestination is nand, spi or nor.\n"
"\tsource can be tftp or usb, default is tftp.\n"
);
#endif
#if defined(MV_NOR_BOOT)
/* Boot from Nor flash */
int nor_burn_uboot_cmd(cmd_tbl_t *cmdtp, int flag, int argc, char * const argv[])
{
int filesize = 0;
MV_U32 ret = 0;
extern char console_buffer[];
load_addr = CONFIG_SYS_LOAD_ADDR;
// MV_U32 s_first,s_end,env_sec;
ulong stop_addr;
ulong start_addr;
// s_first = flash_in_which_sec(&flash_info[0], CONFIG_SYS_MONITOR_BASE);
// s_end = flash_in_which_sec(&flash_info[0], CONFIG_SYS_MONITOR_BASE + CONFIG_SYS_MONITOR_LEN -1);
// env_sec = flash_in_which_sec(&flash_info[0], CONFIG_ENV_ADDR);
MV_U32 loadfrom = 0; /* 0 - from tftp, 1 - from USB */
/* verify requested source is valid */
if (fetch_bubt_cmd_args(argc, argv, &loadfrom) != MV_OK)
return 0;
if ((filesize = fetch_uboot_file (loadfrom)) <= 0)
return 0;
printf("\t[Done]\n");
printf("Override Env parameters to default? [y/N]");
readline(" ");
if( strcmp(console_buffer,"Y") == 0 ||
strcmp(console_buffer,"yes") == 0 ||
strcmp(console_buffer,"y") == 0 ) {
start_addr = CONFIG_ENV_ADDR;
stop_addr = start_addr + CONFIG_ENV_SIZE - 1;
printf("Erasing sector 0x%x:",CONFIG_ENV_OFFSET);
flash_sect_protect (0, start_addr, stop_addr);
flash_sect_erase (start_addr, stop_addr);
flash_sect_protect (1, start_addr, stop_addr);
printf("\t[Done]\n");
}
start_addr = NOR_CS_BASE;
stop_addr = start_addr + CONFIG_ENV_OFFSET - 1;
flash_sect_protect (0, start_addr, stop_addr);
printf("Erasing 0x%x - 0x%x: ", (unsigned int)start_addr, (unsigned int)(start_addr + CONFIG_ENV_OFFSET));
flash_sect_erase (start_addr, stop_addr);
printf("\t[Done]\n");
printf("Writing image to NOR:");
ret = flash_write((char *)CONFIG_SYS_LOAD_ADDR, start_addr, filesize);
if (ret)
printf("\t[Err!]\n");
else
printf("\t[Done]\n");
flash_sect_protect (1, start_addr, stop_addr);
return 1;
}
U_BOOT_CMD(
bubt, 4, 1, nor_burn_uboot_cmd,
"bubt - Burn an image on the Boot Flash.\n",
" file-name \n"
"[file-name] [destination [source]] \n"
"\tBurn a binary image on the Boot Flash, default file-name is u-boot.bin .\n"
"\tdestination is nand, spi or nor.\n"
"\tsource can be tftp or usb, default is tftp.\n"
);
#endif /* MV_NOR_BOOT */
#if defined(MV_MMC_BOOT)
/* Boot from SD/MMC/eMMC */
/* Write u-boot image into SD/MMC/eMMC device */
int mmc_burn_uboot_cmd(cmd_tbl_t *cmdtp, int flag, int argc, char * const argv[])
{
int filesize = 0;
extern char console_buffer[];
load_addr = CONFIG_SYS_LOAD_ADDR;
MV_U32 loadfrom = 0; /* 0 - from tftp, 1 - from USB */
lbaint_t start_lba;
lbaint_t blk_count;
ulong blk_written;
ALLOC_CACHE_ALIGN_BUFFER(char, buf, CONFIG_ENV_SIZE);
struct mmc *mmc;
start_lba = CONFIG_ENV_ADDR / CONFIG_ENV_SECT_SIZE;
blk_count = CONFIG_ENV_SIZE / CONFIG_ENV_SECT_SIZE;
mmc = find_mmc_device(CONFIG_SYS_MMC_ENV_DEV);
if (!mmc) {
printf("No SD/MMC/eMMC card found\n");
return 1;
}
if (mmc_init(mmc)) {
printf("%s(%d) init failed\n", IS_SD(mmc) ? "SD" : "MMC", CONFIG_SYS_MMC_ENV_DEV);
return 1;
}
#ifdef CONFIG_SYS_MMC_ENV_PART
if (CONFIG_SYS_MMC_ENV_PART != mmc->part_num) {
if (mmc_switch_part(CONFIG_SYS_MMC_ENV_DEV, CONFIG_SYS_MMC_ENV_PART)) {
printf("MMC partition switch failed\n");
return 1;
}
}
#endif
/* verify requested source is valid */
if (fetch_bubt_cmd_args(argc, argv, &loadfrom) != MV_OK)
return 0;
if ((filesize = fetch_uboot_file (loadfrom)) <= 0)
return 0;
printf("\t\t[Done]\n");
printf("Override Env parameters to default? [y/N]");
readline(" ");
if( strcmp(console_buffer,"Y") == 0 ||
strcmp(console_buffer,"yes") == 0 ||
strcmp(console_buffer,"y") == 0 ) {
printf("Erasing 0x"LBAF" blocks starting at sector 0x"LBAF" :", blk_count, start_lba);
memset(buf, 0, CONFIG_ENV_SIZE);
blk_written = mmc_bwrite(CONFIG_SYS_MMC_ENV_DEV, start_lba, blk_count, buf);
if (blk_written != blk_count) {
printf("\t[FAIL] - erased %#lx blocks\n", blk_written);
return 0;
} else
printf("\t[Done]\n");
}
if (filesize > CONFIG_ENV_OFFSET) {
printf("Error: Image size (%x) exceeds environment variables offset (%x). ", filesize, CONFIG_ENV_OFFSET);
return 0;
}
/* SD reserves LBA-0 for MBR and boots from LBA-1, MMC/eMMC boots from LBA-0 */
start_lba = IS_SD(mmc) ? 1 : 0;
blk_count = filesize / CONFIG_ENV_SECT_SIZE;
if (filesize % CONFIG_ENV_SECT_SIZE)
blk_count += 1;
printf("Writing image to %s(%d) at LBA 0x"LBAF" (0x"LBAF" blocks):",
IS_SD(mmc) ? "SD" : "MMC", CONFIG_SYS_MMC_ENV_DEV, start_lba, blk_count);
blk_written = mmc_bwrite(CONFIG_SYS_MMC_ENV_DEV, start_lba, blk_count, (char *)CONFIG_SYS_LOAD_ADDR);
if (blk_written != blk_count) {
printf("\t[FAIL] - written %#lx blocks\n", blk_written);
return 0;
} else
printf("\t[Done]\n");
#ifdef CONFIG_SYS_MMC_ENV_PART
if (CONFIG_SYS_MMC_ENV_PART != mmc->part_num)
mmc_switch_part(CONFIG_SYS_MMC_ENV_DEV, mmc->part_num);
#endif
return 1;
}
U_BOOT_CMD(
bubt, 3, 1, mmc_burn_uboot_cmd,
"bubt - Burn an image on the Boot SD/MMC/eMMC device.\n",
" file-name \n"
"[file-name] [source] \n"
"\tBurn a binary image on the Boot Device, default file-name is u-boot.bin .\n"
"\tsource can be tftp or usb, default is tftp.\n"
);
#endif