android系统移植emmc记录(6)--sd卡烧写文件分析 .

=========================================

cmd_fastboot.c

命令格式:

sdfuse flash kernel zImage

sdfuse flash system system.ext3

=========================================

  1. int do_sdfuse (cmd_tbl_t *cmdtp, int flag, int argc, char *argv[])  
  2. {  
  3.     int ret = 1;  
  4.     int enable_reset = 0;  
  5.     struct mmc *mmc = find_mmc_device(CFG_FASTBOOT_SDFUSE_MMCDEV);  
  6.   
  7.     if (mmc_init(mmc)) {  
  8.         printf("sdmmc init is failed.\n");  
  9.     }  
  10.   
  11.     interface.nand_block_size   = CFG_FASTBOOT_PAGESIZE * 64;  
  12.     interface.transfer_buffer   = (unsigned char *) CFG_FASTBOOT_TRANSFER_BUFFER;  
  13.     interface.transfer_buffer_size  = CFG_FASTBOOT_TRANSFER_BUFFER_SIZE;  
  14.     //add by <A href="mailto:xiao@2012-04-17">xiao@2012-04-17</A> :当emmc为空片时,可以支持sd卡直接格式化emmc,并分区。   
  15.     #ifdef CONFIG_EMMC_INAND   
  16.         printf("[ Check emmc 0 MBR label..]\n");  
  17.         if(fdisk_emmc0_partition() == 0){  
  18.            //format_part_dos();    //mmcblk0p1   
  19.             format_part_ext3(2); //mmcblk0p2   
  20.             format_part_ext3(3); //mmcblk0p3   
  21.             format_part_ext3(4); //mmcblk0p4   
  22.         }  
  23.     #endif   
  24.   
  25.     printf("[Fusing Image from SD Card.]\n");  
  26.   
  27.     if (set_partition_table())  
  28.         return 1;  
  29.          if ((argc == 2) && !strcmp(argv[1], "flashall"))  
  30.     {  
  31.   
  32. //sdfuse flash bootloader u-boot.bin   
  33. //sdfuse flash kernel kernel.img   
  34. //sdfuse flash ramdisk ramdisk.img   
  35. //sdfuse flash system system.img   
  36.         //add by xiao@2012-04-17     修改文件,支持sd烧写      
  37.         if (update_from_sd("bootloader""u-boot_emmc.bin"))  
  38.             goto err_sdfuse;  
  39.               
  40.         if (update_from_sd("kernel""zImage"))  
  41.             goto err_sdfuse;  
  42.              
  43.         if (update_from_sd("system""system.ext3"))  
  44.             goto err_sdfuse;  
  45.                 
  46.         if (update_from_sd("userdata", NULL))  
  47.             goto err_sdfuse;  
  48.                  
  49.     if (update_from_sd("cache", NULL))  
  50.             goto err_sdfuse;  
  51.         delay1s();  
  52.         delay1s();  
  53.         delay1s();  
  54.         enable_reset = 1;  
  55.         ret = 0;  
  56.     }  
  57.     else if ((argc == 4) && !strcmp(argv[1], "flash"))  
  58.     {  
  59.         LCD_turnon();  
  60.   
  61.         if (update_from_sd(argv[2], argv[3]))  
  62.             goto err_sdfuse;  
  63.   
  64.         ret = 0;  
  65.     }  
  66.     else if ((argc == 3) && !strcmp(argv[1], "erase"))  
  67.     {  
  68.         LCD_turnon();  
  69.   
  70.         if (update_from_sd(argv[2], NULL))  
  71.             goto err_sdfuse;  
  72.   
  73.         ret = 0;  
  74.     }  
  75.     else  
  76.     {  
  77.         printf("Usage:\n%s\n", cmdtp->usage);  
  78.         return 1;  
  79.     }  
  80.   
  81.   
  82.   
  83. err_sdfuse:  
  84.     LCD_setfgcolor(0x000010);  
  85.     LCD_setleftcolor(0x000010);  
  86.     LCD_setprogress(100);  
  87.   
  88.     if (enable_reset)  
  89.         do_reset (NULL, 0, 0, NULL);  
  90.   
  91.     return ret;  
  92. }  
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;
}
  1. static int update_from_sd (char *part, char *file)  
  2. {  
  3.     int ret = 1;  
  4.   
  5.     /* Read file */  
  6.     if (file != NULL)  
  7.     {  
  8.         long size;  
  9.         unsigned long offset;  
  10.         unsigned long count;  
  11.         char filename[32];  
  12.         block_dev_desc_t *dev_desc=NULL;  
  13.   
  14.         printf("Partition: %s, File: %s/%s\n", part, CFG_FASTBOOT_SDFUSE_DIR, file);  
  15.         // LCD_setfgcolor(0x2E8B57);   
  16.         // LCD_setprogress(100);   
  17.         dev_desc = get_dev("mmc", CFG_FASTBOOT_SDFUSE_MMCDEV);  
  18.         if (dev_desc == NULL) {  
  19.             printf ("** Invalid boot device **\n");  
  20.             return 1;  
  21.         }  
  22.         if (fat_register_device(dev_desc, CFG_FASTBOOT_SDFUSE_MMCPART) != 0) {  
  23.             printf ("** Invalid partition **\n");  
  24.             return 1;  
  25.         }  
  26.         sprintf(filename, "%s/%s", CFG_FASTBOOT_SDFUSE_DIR, file);  
  27.         offset = CFG_FASTBOOT_TRANSFER_BUFFER;  
  28.         count = 0;  
  29.         size = file_fat_read (filename, (unsigned char *) offset, count);  
  30.   
  31.         if (size == -1) {  
  32.             printf("Failed to read %s\n", filename);  
  33.             return 1;  
  34.         }  
  35.   
  36.         download_size = 0;  // should be 0   
  37.         download_bytes = size;  
  38.   
  39.         printf ("%ld (0x%08x) bytes read\n", size, size);  
  40.     }  
  41.     else {  
  42.         printf("Partition: %s\n", part);  
  43.   
  44.         download_size = 0;  // should be 0   
  45.         download_bytes = 0;  
  46.     }  
  47.   
  48.     /* Write image into partition */  
  49.     /* If file is empty or NULL, just erase the part. */  
  50.     {  
  51.         char command[32];  
  52.   
  53.         if (download_bytes == 0)  
  54.             sprintf(command, "%s:%s""erase", part);  
  55.         else  
  56.             sprintf(command, "%s:%s""flash", part);  
  57.                   //最后还是调用fastboot的这个接口,来烧写文件。   
  58.         ret = rx_handler(command, sizeof(command));  
  59.     }  
  60.   
  61.     return ret;  
  62. }  
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命令:

  1. //add by xiao@2012-04-17 for: test emmc 0 format partition   
  2. #ifdef CONFIG_EMMC_INAND   
  3. //ext3format   
  4. static void format_part_ext3(int dev_num)  
  5. {  
  6.     char run_cmd[80];  
  7.     int status = 1;  
  8.     char response[65];  
  9.   
  10.     sprintf(run_cmd, "ext3format mmc 0:%d",dev_num);  
  11.     status = run_command(run_cmd, 0);  
  12.     if (status)  
  13.     {  
  14.         sprintf(response,"FAILfailed to ext3format partition");  
  15.     }  
  16.     else  
  17.     {  
  18.         //printf("partition [ %d ]  erased\n", dev_num);   
  19.         sprintf(response, "OKAY");  
  20.     }  
  21.   
  22.     fastboot_tx_status(response, strlen(response), FASTBOOT_TX_ASYNC);  
  23.   
  24. }  
  25. //vfat   
  26. static void format_part_dos ()  
  27. {  
  28.     char run_cmd[80];  
  29.     int status = 1;  
  30.     char response[65];  
  31.       
  32.     sprintf(run_cmd, "fatformat mmc 0:1");  
  33.     status = run_command(run_cmd, 0);  
  34.     if (status)  
  35.     {  
  36.         sprintf(response,"FAILfailed to fatformat partition");  
  37.     }  
  38.     else  
  39.     {  
  40.         //printf("partition '%s' fatformat\n", "fat");   
  41.         sprintf(response, "OKAY");  
  42.     }  
  43.   
  44.     fastboot_tx_status(response, strlen(response), FASTBOOT_TX_ASYNC);  
  45. }  
  46.   
  47. //fdisk partition   
  48. int fdisk_emmc0_partition()  
  49. {  
  50.     unsigned char buffer[512];  
  51.     block_dev_desc_t *dev_desc=NULL;  
  52.     char run_cmd[80];  
  53.     int status = 1;  
  54.     char response[65];  
  55.     int ret =1;  
  56.       
  57.     struct mmc *mmc = find_mmc_device(0);  
  58.   
  59.     if (mmc_init(mmc)) {  
  60.         printf("emmc 0 init is failed.\n");  
  61.     }  
  62.       
  63.     dev_desc = get_dev("mmc", 0);  
  64.     if (dev_desc == NULL) {  
  65.             printf ("** Invalid boot device : emmc 0 **\n");  
  66.             return ret;  
  67.     }  
  68.     //检测是否是MBR分区列表   
  69.     if ((dev_desc->block_read(dev_desc->dev, 0, 1, (ulong *) buffer) != 1) ||  
  70.          (buffer[0x1fe] != 0x55 ||  
  71.          buffer[0x1ff] != 0xaa)) {  
  72.         printf ("bad MBR sector! so fdisk emmc 0 now\n");  
  73.   
  74.         sprintf(run_cmd, "fdisk -c 0");  
  75.         status = run_command(run_cmd, 0);  
  76.         if (status)  
  77.         {  
  78.             sprintf(response,"FAILfailed to fdisk emmc 0");  
  79.         }  
  80.         else  
  81.         {  
  82.             printf("fdisk emmc 0 success! \n");  
  83.             sprintf(response, "OKAY");  
  84.         }    
  85.         ret =status;  
  86.         fastboot_tx_status(response, strlen(response), FASTBOOT_TX_ASYNC);  
  87.     }  
  88.     return ret;  
  89. }  
  90. #endif  
posted @ 2013-09-05 16:52  莫回头  阅读(756)  评论(0编辑  收藏  举报