TF卡烧写目前就这样了,现在来重新分析一下流程。最开始的时候他是需要按update启动的,所以应该从这个地方开始入手,看看为什么按着update按键启动就能进入烧写模式呢?
首先来到u-boot-2020.01/board/vendor/ss928v100/ss928v100.c目录下,先到更前面把,更前面是在common/board_r.c 的misc_init_r调用了,具体如下所示:
#ifdef CONFIG_MISC_INIT_R
misc_init_r, /* miscellaneous platform-dependent init */
#endif
然后找到该函数,具体内容如下所示:
int misc_init_r(void)
{
#ifdef CONFIG_RANDOM_ETHADDR
random_init_r();
#endif
env_set("verify", "n");
#if (CONFIG_AUTO_UPDATE == 1)
/* auto update flag */
if (is_auto_update())
auto_update_flag = 1;
else
auto_update_flag = 0;
/* bare chip program flag */
if (is_bare_program())
bare_chip_program = 1;
else
bare_chip_program = 0;
#ifdef CFG_MMU_HANDLEOK
dcache_stop();
#endif
#ifdef CFG_MMU_HANDLEOK
dcache_start();
#endif
#endif /*CONFIG_AUTO_UPDATE*/
#if (CONFIG_AUTO_UPDATE == 1)
if (auto_update_flag)
do_auto_update();
if (bare_chip_program && !auto_update_flag)
save_bootdata_to_flash();
#endif
set_bootloader_download_process_flag();
return 0;
}
第10行的is_auto_update如果成立,就会将auto_update_flag标志位置1,那这个判断条件是什么呢?判断条件函数如下所示:
int is_auto_update(void)
{
/* to add some judgement if neccessary */
int ret = 0;
unsigned int pinout;
unsigned int val[NUM_3];
pinout = readl(REG_GPIO0_0_PINOUT);
writel(0x1100,REG_GPIO0_0_PINOUT);
writel(0, REG_BASE_GPIO0 + GPIO_DIR_OFST);
val[NUM_0] = readl(REG_BASE_GPIO0 + GPIO0_0_DATA_OFST);
if (val[NUM_0])
goto END;
udelay(10000); /* delay 10000 us */
val[NUM_1] = readl(REG_BASE_GPIO0 + GPIO0_0_DATA_OFST);
udelay(10000); /* delay 10000 us */
val[NUM_2] = readl(REG_BASE_GPIO0 + GPIO0_0_DATA_OFST);
udelay(10000); /* delay 10000 us */
if (val[NUM_0] == val[NUM_1] && val[NUM_1] == val[NUM_2] && val[NUM_0] == NUM_0)
ret = 1; /* update enable */
END:
writel(pinout,REG_GPIO0_0_PINOUT);
return ret;
}
这里的GPIO0_0对应的正是update引脚

第9行向GPIO0_0寄存器写入0x1100,表示开启上拉,第11行向方向寄存器写入0,表示引脚输入,13-15行就是一个读取加判断,如果你没按下update就直接退出,如果按下就继续执行,后面会判断多次,都判断为按下之后最后会在24行返回1,然后回到misc_init_r函数,返回1之后auto_update_flag也会为1,就到了31-36行,执行do_auto_update函数,do_auto_update函数定义在u-boot-2020.01\product\update\auto_update_adaptation.c文件中,具体内容如下所示:
int do_auto_update(void)
{
struct blk_desc *stor_dev = NULL;
int old_ctrlc;
int j;
int state;
for (j = 0; j < MAX_UPDATE_INTF; j++) {
stor_dev = detect_external_storage(j);
if (stor_dev == NULL)
continue;
#ifdef CONFIG_EMMC
if (target_dev == 0) {
/* emmc upgrade */
bootargs_getend();
/* re-do the entry->test(dev_desc) to set the
* dev_desc->part_type to PART_TYPE_DOS
*/
part_init(stor_dev);
}
#endif
if (fat_register_device(stor_dev, 1) != 0) {
debug_print("Unable to use %s %d:%d for fatls\n", s_intf[j].name,
au_stor_curr_dev, 1);
continue;
}
if (file_fat_detectfs() != 0) {
debug_print("file_fat_detectfs failed\n");
continue;
}
get_boot_info();
update_intf_p->init();
if (get_env_from_config()) {
#if !(defined(CONFIG_TARGET_SS528V100) || defined(CONFIG_TARGET_SS625V100) || \
defined(CONFIG_TARGET_SS524V100) || defined(CONFIG_TARGET_SS522V101) || \
defined(CONFIG_TARGET_SS615V100) || defined(CONFIG_TARGET_SS522V100))
auto_update_flag = 0;
#endif
printf("Warning:no config! Try to use old env!\n");
};
if (bootargs_analyze()) {
printf("ERROR:bootargs analyze fail!\n");
continue;
}
/* make sure that we see CTRL-C and save the old state */
old_ctrlc = disable_ctrlc(0);
state = update_to_flash();
/* restore the old state */
disable_ctrlc(old_ctrlc);
s_intf[j].exit();
if (state == -1)
continue;
/* update files have been found on current medium, so just break here */
break;
}
/* no update file found */
if (state == -1)
return -1;
env_save();
return 0;
}
这里的重点在第34行的get_boot_info,从名字就可以看出来他是获取介质信息的,具体内容如下所示:
void get_boot_info(void)
{
unsigned int reg;
reg = readl((void *)(SYS_CTRL_REG_BASE + REG_SYSSTAT));
#if (defined CONFIG_TARGET_SS918V100) || (defined CONFIG_TARGET_SS318V100) || \
(defined CONFIG_TARGET_SS528V100) || (defined CONFIG_TARGET_SS625V100) || \
(defined CONFIG_TARGET_SS524V100) || (defined CONFIG_TARGET_SS522V101) || \
(defined CONFIG_TARGET_SS013V100) || (defined CONFIG_TARGET_SS615V100) || \
(defined CONFIG_TARGET_SS928V100) || (defined CONFIG_TARGET_SS927V100) || \
(defined CONFIG_TARGET_SS522V100)
if (get_sys_boot_mode(reg) == BOOT_FROM_SPI_NAND) {
boot_medium_type = BOOT_FROM_NAND;
update_intf_p = &update_intf[UPDATE_MEDIUM_NAND];
} else if (get_sys_boot_mode(reg) == BOOT_FROM_SPI) {
boot_medium_type = BOOT_FROM_SPI;
update_intf_p = &update_intf[UPDATE_MEDIUM_SPINOR];
} else if (get_sys_boot_mode(reg) == BOOT_FROM_NAND) {
boot_medium_type = BOOT_FROM_NAND;
update_intf_p = &update_intf[UPDATE_MEDIUM_NAND];
} else if (get_sys_boot_mode(reg) == BOOT_FROM_EMMC) {
boot_medium_type = BOOT_FROM_EMMC;
update_intf_p = &update_intf[UPDATE_MEDIUM_EMMC];
}
#else
if (get_sys_boot_mode(reg) == BOOT_FROM_SPI) {
/* SFC_DEVICE_MODE:0:SPINOR 1:SPINAND */
if (get_spi_device_type(reg)) {
boot_medium_type = BOOT_FROM_NAND;
update_intf_p = &update_intf[UPDATE_MEDIUM_NAND];
} else {
boot_medium_type = BOOT_FROM_SPI;
update_intf_p = &update_intf[UPDATE_MEDIUM_SPINOR];
}
} else if (get_sys_boot_mode(reg) == BOOT_FROM_NAND) {
boot_medium_type = BOOT_FROM_NAND;
update_intf_p = &update_intf[UPDATE_MEDIUM_NAND];
} else if (get_sys_boot_mode(reg) == BOOT_FROM_EMMC) {
boot_medium_type = BOOT_FROM_EMMC;
update_intf_p = &update_intf[UPDATE_MEDIUM_EMMC];
}
#endif
}
很明显现在是要烧写EMMC,所以肯定就是第38行、39行的这一个赋值,update_intf是一个结构体数组,具体内容如下所示:
static struct update_medium_interface update_intf[3] = {
#ifdef CONFIG_CMD_SF
{"spinor", spinor_flash_init, spinor_flash_erase, spinor_flash_write,
NULL, NULL},
#else
{"none", NULL, NULL, NULL, NULL, NULL},
#endif
#ifdef CONFIG_CMD_NAND
{"nand", nand_flash_init, nand_flash_erase, nand_flash_write,
nand_flash_yaffs_write, NULL},
#else
{"none", NULL, NULL, NULL, NULL, NULL},
#endif
#ifdef CONFIG_SUPPORT_EMMC_BOOT
{"emmc", mmc_save_init, NULL, mmc_save_write, NULL, mmc_save_write_ext4}
#endif
};
EMMC对应的是第16行的内容,具体要如何用还得回到do_auto_update,看get_boot_info下一行的函数,update_intf_p->init();根据打印现象重点应该就在这里了,它的结构体是static struct update_medium_interface *update_intf_p;我应该去看一下这个结构体定义在哪里,它的定义在同一文件下,具体内容如下所示:
struct update_medium_interface {
char name[NAME_LEN];
int (*init)(void);
int (*erase)(unsigned long offset, unsigned long len);
int (*write)(unsigned long offset, unsigned long len,
unsigned char *buf);
int (*write_yaffs)(unsigned long offset, unsigned long len,
unsigned char *buf);
int (*write_ext4)(unsigned long offset, unsigned long len,
unsigned char *buf, char is_sparse);
};
update_intf_p->init()就对应mmc_save_init,mmc_save_init函数主要是用来初始化emmc的,也没啥可看的,继续向下分析do_auto_update就行了,get_env_from_config用来获取配置,这个肯定是有用的,最后也会打印出来各个分区,然后将重点放在这四行
old_ctrlc = disable_ctrlc(0);
state = update_to_flash();
/* restore the old state */
disable_ctrlc(old_ctrlc);
第一和第四行不看,就是ctrl + c的使能和取消使能,中间的update_to_flash才是最重要的,具体内容如下所示:
static int update_to_flash(void)
{
int i;
loff_t sz;
int res;
int cnt;
int uboot_updated = 0;
/* just loop thru all the possible files */
for (i = 0; i < AU_MAXFILES && aufile[i] != NULL; i++) {
printf("\n");
if (!fat_exists(aufile[i])) {
printf("%s not found!\n", aufile[i]);
continue;
}
/* get file's real size */
if (!fat_size(aufile[i], &sz)) {
aufile_size[i] = ALIGN((unsigned long)sz, CONFIG_SYS_CACHELINE_SIZE);
} else {
printf("get size of %s failed!\n", aufile[i]);
continue;
}
printf("aligned size=0x%08lx\n", aufile_size[i]);
(void)memset_s(LOAD_ADDR, aufile_size[i], 0xff, aufile_size[i]);
sz = file_fat_read(aufile[i], LOAD_ADDR, (unsigned long)sz);
debug_print("read %s sz %ld hdr %lu\n",
aufile[i], (unsigned long)sz, (unsigned long)sizeof(image_header_t));
if (sz <= 0) {
printf("read %s failed!\n", aufile[i]);
continue;
}
/* get file's real size */
aufile_size[i] = (unsigned long)sz;
printf("size=0x%08lx\n", aufile_size[i]);
/* If u-boot had been updated, we need to
* save current env to flash */
if (strcmp((char *)AU_FIRMWARE, aufile[i]) == 0)
uboot_updated = 1;
/* this is really not a good idea, but it's what the */
/* customer wants. */
cnt = 0;
do {
res = au_do_update(i, (unsigned long)sz);
/* let the user break out of the loop */
if (ctrlc() || had_ctrlc()) {
clear_ctrlc();
break;
}
cnt++;
} while (res < 0);
}
if (uboot_updated == 1)
return 1;
else
return -1;
}
打印的话是卡在这里
(void)memset_s(LOAD_ADDR, aufile_size[i], 0xff, aufile_size[i]);
sz = file_fat_read(aufile[i], LOAD_ADDR, (unsigned long)sz);
debug_print("read %s sz %ld hdr %lu\n",
aufile[i], (unsigned long)sz, (unsigned long)sizeof(image_header_t));
if (sz <= 0) {
printf("read %s failed!\n", aufile[i]);
continue;
}
/* get file's real size */
aufile_size[i] = (unsigned long)sz;
printf("size=0x%08lx\n", aufile_size[i]);
这一段操作的话是将文件内容读取到内存,并通过实际读取的字节数来获取文件的真实大小,但假如内存只有512M,但是要读取的镜像有2G,这就导致镜像烧写阶段卡死在真实大小读取阶段,也不会再向下进行。所以也就不会进行下面的au_do_update操作。
#ifndef CONFIG_VENDOR_UPGRADE_BY_SEGMENT
static int au_do_update(int idx, long sz)
{
unsigned long start;
unsigned long len;
unsigned long write_len;
int rc = 0;
char *buf;
void *pbuf;
char sparse_flag = 0;
start = aufl_layout[idx].start;
len = aufl_layout[idx].end - aufl_layout[idx].start + 1;
/* erase the address range. */
if (boot_medium_type == BOOT_FROM_SPI || boot_medium_type == BOOT_FROM_NAND) {
printf("%s erase...\n", update_intf_p->name);
rc = update_intf_p->erase(start, len);
if (rc) {
printf("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;
}
/* write the whole file to flash;uboot and rootfs image have head
* kernel has head,it's head also be writed to flash
*/
pbuf = buf;
write_len = aufile_size[idx];
/* copy the data from RAM to FLASH */
printf("\n%s write...\n", update_intf_p->name);
if (strstr(aufile[idx], "yaffs")) {
if (update_intf_p->write_yaffs)
rc = update_intf_p->write_yaffs(start, write_len, pbuf);
} else if (strstr(aufile[idx], "ext4")) {
if (update_intf_p->write_ext4) {
#ifdef CONFIG_EXT4_SPARSE
sparse_flag = is_sparse_image((sparse_header_t *)pbuf);
#endif
rc = update_intf_p->write_ext4(start, len, pbuf, sparse_flag);
}
} else {
rc = update_intf_p->write(start, write_len, pbuf);
}
if (rc) {
printf("write failed, return %d\n", rc);
return 1;
}
unmap_physmem(buf, len);
return 0;
}