=========================================
cmd_fastboot.c
命令格式:
sdfuse flash kernel zImage
sdfuse flash system system.ext3
=========================================
int do_sdfuse (cmd_tbl_t *cmdtp, int flag, int argc, char *argv[])
{
int ret = 1;
int enable_reset = 0;
struct mmc *mmc = find_mmc_device(CFG_FASTBOOT_SDFUSE_MMCDEV);
if (mmc_init(mmc)) {
printf("sdmmc init is failed.\n");
}
interface.nand_block_size = CFG_FASTBOOT_PAGESIZE * 64;
interface.transfer_buffer = (unsigned char *) CFG_FASTBOOT_TRANSFER_BUFFER;
interface.transfer_buffer_size = CFG_FASTBOOT_TRANSFER_BUFFER_SIZE;
//add by xiao@2012-04-17 :当emmc为空片时,可以支持sd卡直接格式化emmc,并分区。
#ifdef CONFIG_EMMC_INAND
printf("[ Check emmc 0 MBR label..]\n");
if(fdisk_emmc0_partition() == 0){
//format_part_dos(); //mmcblk0p1
format_part_ext3(2); //mmcblk0p2
format_part_ext3(3); //mmcblk0p3
format_part_ext3(4); //mmcblk0p4
}
#endif
printf("[Fusing Image from SD Card.]\n");
if (set_partition_table())
return 1;
if ((argc == 2) && !strcmp(argv[1], "flashall"))
{
//sdfuse flash bootloader u-boot.bin
//sdfuse flash kernel kernel.img
//sdfuse flash ramdisk ramdisk.img
//sdfuse flash system system.img
//add by xiao@2012-04-17 修改文件,支持sd烧写
if (update_from_sd("bootloader", "u-boot_emmc.bin"))
goto err_sdfuse;
if (update_from_sd("kernel", "zImage"))
goto err_sdfuse;
if (update_from_sd("system", "system.ext3"))
goto err_sdfuse;
if (update_from_sd("userdata", NULL))
goto err_sdfuse;
if (update_from_sd("cache", NULL))
goto err_sdfuse;
delay1s();
delay1s();
delay1s();
enable_reset = 1;
ret = 0;
}
else if ((argc == 4) && !strcmp(argv[1], "flash"))
{
LCD_turnon();
if (update_from_sd(argv[2], argv[3]))
goto err_sdfuse;
ret = 0;
}
else if ((argc == 3) && !strcmp(argv[1], "erase"))
{
LCD_turnon();
if (update_from_sd(argv[2], NULL))
goto err_sdfuse;
ret = 0;
}
else
{
printf("Usage:\n%s\n", cmdtp->usage);
return 1;
}
err_sdfuse:
LCD_setfgcolor(0x000010);
LCD_setleftcolor(0x000010);
LCD_setprogress(100);
if (enable_reset)
do_reset (NULL, 0, 0, NULL);
return ret;
}
static int update_from_sd (char *part, char *file) { int ret = 1; /* Read file */ if (file != NULL) { long size; unsigned long offset; unsigned long count; char filename[32]; block_dev_desc_t *dev_desc=NULL; printf("Partition: %s, File: %s/%s\n", part, CFG_FASTBOOT_SDFUSE_DIR, file); // LCD_setfgcolor(0x2E8B57); // LCD_setprogress(100); dev_desc = get_dev("mmc", CFG_FASTBOOT_SDFUSE_MMCDEV); if (dev_desc == NULL) { printf ("** Invalid boot device **\n"); return 1; } if (fat_register_device(dev_desc, CFG_FASTBOOT_SDFUSE_MMCPART) != 0) { printf ("** Invalid partition **\n"); return 1; } sprintf(filename, "%s/%s", CFG_FASTBOOT_SDFUSE_DIR, file); offset = CFG_FASTBOOT_TRANSFER_BUFFER; count = 0; size = file_fat_read (filename, (unsigned char *) offset, count); if (size == -1) { printf("Failed to read %s\n", filename); return 1; } download_size = 0; // should be 0 download_bytes = size; printf ("%ld (0x%08x) bytes read\n", size, size); } else { printf("Partition: %s\n", part); download_size = 0; // should be 0 download_bytes = 0; } /* Write image into partition */ /* If file is empty or NULL, just erase the part. */ { char command[32]; if (download_bytes == 0) sprintf(command, "%s:%s", "erase", part); else sprintf(command, "%s:%s", "flash", part); //最后还是调用fastboot的这个接口,来烧写文件。 ret = rx_handler(command, sizeof(command)); } return ret; }
SD卡支持自动格式,分区emmc命令:
//add by xiao@2012-04-17 for: test emmc 0 format partition #ifdef CONFIG_EMMC_INAND //ext3format static void format_part_ext3(int dev_num) { char run_cmd[80]; int status = 1; char response[65]; sprintf(run_cmd, "ext3format mmc 0:%d",dev_num); status = run_command(run_cmd, 0); if (status) { sprintf(response,"FAILfailed to ext3format partition"); } else { //printf("partition [ %d ] erased\n", dev_num); sprintf(response, "OKAY"); } fastboot_tx_status(response, strlen(response), FASTBOOT_TX_ASYNC); } //vfat static void format_part_dos () { char run_cmd[80]; int status = 1; char response[65]; sprintf(run_cmd, "fatformat mmc 0:1"); status = run_command(run_cmd, 0); if (status) { sprintf(response,"FAILfailed to fatformat partition"); } else { //printf("partition '%s' fatformat\n", "fat"); sprintf(response, "OKAY"); } fastboot_tx_status(response, strlen(response), FASTBOOT_TX_ASYNC); } //fdisk partition int fdisk_emmc0_partition() { unsigned char buffer[512]; block_dev_desc_t *dev_desc=NULL; char run_cmd[80]; int status = 1; char response[65]; int ret =1; struct mmc *mmc = find_mmc_device(0); if (mmc_init(mmc)) { printf("emmc 0 init is failed.\n"); } dev_desc = get_dev("mmc", 0); if (dev_desc == NULL) { printf ("** Invalid boot device : emmc 0 **\n"); return ret; } //检测是否是MBR分区列表 if ((dev_desc->block_read(dev_desc->dev, 0, 1, (ulong *) buffer) != 1) || (buffer[0x1fe] != 0x55 || buffer[0x1ff] != 0xaa)) { printf ("bad MBR sector! so fdisk emmc 0 now\n"); sprintf(run_cmd, "fdisk -c 0"); status = run_command(run_cmd, 0); if (status) { sprintf(response,"FAILfailed to fdisk emmc 0"); } else { printf("fdisk emmc 0 success! \n"); sprintf(response, "OKAY"); } ret =status; fastboot_tx_status(response, strlen(response), FASTBOOT_TX_ASYNC); } return ret; } #endif