在u-boot里面加入Android lk bootloader的一键烧写功能(2)

3 篇文章 0 订阅
3 篇文章 1 订阅

      上篇文章介绍了Android镜像的构成。我们来看看lk怎么将这个mtd.img大镜像进行解析和烧写到NandFlash的分区中的。我们的环境依然是基于SEP6200开发板的。首先我们来看看镜像在nandflash中的存放规则:

Dev

地址空间

Size

blocksize

Name

(预留)

0x00000000 ~ 0x001FFFFF

00200000

2MB

00200000

Lk

Mtd0

0x00200000 ~ 0x003FFFFF

00200000

2MB

00200000

Misc

Mtd1

0x00400000 ~0x00DFFFFF

00a00000

10MB

00200000

Recovery

Mtd2

0x00E00000~ 0x018FFFFF

00a00000

10MB

00200000

Boot

Mtd3

0x01900000~ 0x338FFFFF

32000000

800MB

00200000

System

Mtd4

0x33900000~ 0x39CFFFFF

06400000

100MB

00200000

Cache

Mtd5

0x39D00000~ 0x411FFFFF

06400000

100MB

00200000

Userdata

Mtd6

0x41200000~ 0xC11FFFFF

80000000

3GB

00200000

FTL



      解析这个文件的源码位于lk-release\platform\sep6200\cmd_mtd.c

      首先定义了结构体,是我们需要解析文件的头部。 cmd_mtd.c就是对mtd.img进行解析的,而写入的所有的信息都已经存放在了每个文件的头部中,即stBunchHeader。而对所有的写入操作都是由一个函数完成的nandwrite函数

for(i=0; i<img_cnt; i++)
    {
        stBunchHeader = (tagDiskImageBunchHeaderType *)ptr;//每个镜像的头部
    
        img_part = stBunchHeader->ullPart;
        img_size = stBunchHeader->ullLength;
    
        printf("|*************************************************|\n");
        printf(" >img[%d]\n", i);
        printf(" >  ullPart = %d\n", img_part);
        printf(" >  ullLength = 0x%x\n", img_size);

        ptr += 16; //sizeof(stBunchHeader);//跳过头部信息
        nandwrite((u_char *)ptr, mtd_part[img_part], img_size);//写入
        printf(" >Write img[%d] to nand OK!\n", i);    
        ptr += img_size;
    }

       那么,这个nandwrite函数究竟做了哪些事情呢?跟踪到nand_write_yaffs.c文件中发现其根据不同的文件类型采用了不同的写入方式,对于有文件系统的文件,如system.img是采用了yaffs文件系统,那么写入的时候就要以文件系统的方式写入到NandFlash中的。我们来看看nandwrite函数的实现:

int nandwrite(u_char *buf, const char *name, unsigned int len)
{
	struct ptentry *ptn;
	unsigned int i, found;
	found = 0;
	for(i = 0; i < sizeof(board_part_list); i++)
	{
		ptn = &board_part_list[i];
		if (!strcmp(ptn->name, name))
		{
			found = 1;
			break;
		}
	}
	if(found == 0)
	{
		printf("error! partition not found!\n");
		return -1;
	}
	switch(ptn->flags){
		case(WRITE_LOADER):
			printf("Write %s 0x%0x bytes from 0x%0x, MODE:WRITE_LOADER\n", ptn->name, len, buf);
			burn_nandboot(buf);
			return 0;
		case(WRITE_RAW):
			printf("Write %s 0x%0x bytes from 0x%0x, MODE:WRITE_RAW\n", ptn->name, len, buf);
			write_nand(buf, ptn->start, len);
			return 0;
		case(WRITE_FS):
			printf("Write %s 0x%0x bytes from 0x%0x, MODE:WRITE_FS\n", ptn->name, len, buf);
			/*当涉及到文件系统的时候,我们需要擦除所有的相关块*/
			write_nand_yaffs(buf, ptn->start, len, ptn->length);
			return 0;
		default:
			printf("Error! Unknown write type for %s\n", ptn->name);
			return -1;
	}
}
      board_part_list定义了我们文章开始的时候所列的表,即每个镜像在NandFlash中的存放位置。 这里我们知道了,Nand的写入是根据每个镜像的标签来识别的,WRITE_LOADER类型则是烧写bootloader的函数去完成;WRITE_RAW则采用直接烧写的方式;WRITE_FS则是烧写yaffs文件系统。


现在大概知道了lk的对于mtd.img的解析过程和烧写
过程。但是仔细跟代码发现lk对NandFlash的操作和u-boot中的方式并不一样。所以要么是将lk的nand驱动移植到u-boot中,要么是采用u-boot的方式去实现lk的烧写功能。权衡之后采用了后者,因为:如果修改u-boot的nand驱动则对其他的命令会造成不兼容。


下面为u-boot的mtd解析命令的移植过程

1.在u-boot/common/目录下添加cmd_mtd.c文件,尤其注意添加u-boot的命令格式:

U_BOOT_CMD(
	nandmtd,	2,	1,	do_nandmtd,
	"nandmtd  [addr]",
	"Resolve xxx_mtd.img at ddr address and write [boot.img system.img recovery.img] to nand."
);


   用u-boot的调用方式实现了nandwrite命令,其中几个写入Flash的关键函数如下:

int nand_erase_block(loff_t off, unsigned int len)
{	nand_erase_options_t opts;
		
	nand_info_t *nand;
	nand = &nand_info[0];
	memset(&opts, 0, sizeof(opts));
		opts.offset = off;
		opts.length = len;
	nand_erase_opts(nand, &opts);
	return 0;
}




int write_nand(u_char *buf, unsigned start_blk, unsigned int len)
{	loff_t off;
	nand_info_t *nand;
	nand = &nand_info[0];
	size_t rwsize;
	rwsize = (size_t)len;
	off = 2*(start_blk<<20);
	printf("\n\nthe offset is : 0x%0x\n\n",off);
	printf("\n\nthe rwsize is : 0x%0x\n\n",rwsize);
	nand_erase_block(off,len);
	nand_write_skip_bad(nand, off, &rwsize,buf,0);
	return 0;
}

int write_nand_yaffs(u_char *buf, unsigned start_blk, unsigned int len, unsigned int blk_erase)
{
	loff_t off;
	nand_info_t *nand;
	nand = &nand_info[0];
	size_t rwsize;
	rwsize = (size_t)len;
	off = 2*(start_blk<<20);
	printf("\n\nthe offset is : 0x%0x\n\n",off);
	printf("\n\nthe rwsize is : 0x%0x\n\n",rwsize);
	nand_erase_block(off,2*(blk_erase<<20));
	nand_write_skip_bad(nand, off, &rwsize,buf,1);
	return 0;
}



2.在u-boot/common/Makefile中添加需要编译的命令文件,COBJS-$(CONFIG_CMD_MTD) += cmd_mtd.o ,可以让cmd_mtd.c文件可以编译生成.o文件


3.在u-boot\include\config_cmd_all.h u-boot\include\config_cmd_default.h两个文件中加入预定义宏 #define CONFIG_CMD_MTD,使得uboot在编译命令时能加入该命令。


4.在u-boot\include\configs\SEP0611.h文件中同样加入宏定义#define CONFIG_CMD_MTD

5.最后编译即可

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值