您的位置 首页 传感器

Uboot在S3C2440上的移植详解(五)

一、移植环境主机:VMWare–Fedora9开发板:Mini2440–64MBNand,Kernel:2.6.30.4编译器:arm-linux-gcc-4.3.2.tgzu-boot:u-bo

一、移植环境

  • 主 机:VMWare–Fedora 9

  • 开发板:Mini2440–64MB Nand,Kernel:2.6.30.4

  • 编译器:arm-linux-gcc-4.3.2.tgz

  • u-boot:u-boot-2009.08.tar.bz2

二、移植过程

9)完成u-boot对yaffs/yaffs2文件体系下载的支撑。
留意:此篇对Nand的操作是根据MTD架构方法,在“u-boot-2009.08在2440上的移植详解(三)”中讲到过。
一般一个Nnad Flash存储设备由若干块组成,1个块由若干页组成。一般128MB以下容量的Nand Flash芯片,一页巨细为528B,被顺次分为2个256B的主数据区和16B的额定空间;128MB以上容量的Nand Flash芯片,一页巨细一般为2KB。由于Nand Flash呈现位回转的概率较大,一般在读写时需求运用ECC进行过错查验和康复。
Yaffs/yaffs2文件体系的规划充沛考虑到Nand Flash以页为存取单位等的特色,将文件安排成固定巨细的段(Chunk)。以528B的页为例,Yaffs/yaffs2文件体系运用前512B存储数据和16B的额定空间寄存数据的ECC和文件体系的安排信息等(称为OOB数据)。经过OOB数据,不但能完成过错检测和坏块处理,一起还能够防止加载时对整个存储介质的扫描,加快了文件体系的加载速度。以下是Yaffs/yaffs2文件体系页的结构阐明:

Yaffs页结构阐明
==============================================
字节 用处
==============================================
0 – 511 存储数据(分为两个半部)
512 – 515 体系信息
516 数据状态字
517 块状态字
518 – 519 体系信息
520 – 522 后半部256字节的ECC
523 – 524 体系信息
525 – 527 前半部256字节的ECC
==============================================

好了,在了解Nand Flash组成和Yaffs/yaffs2文件体系结构后,咱们再回到u-boot中。现在,在u-boot中已经有对Cramfs、Jffs2等文件体系的读写支撑,但与带有数据校验等功用的OOB区的Yaffs/Yaffs2文件体系比较,他们是将一切文件数据简略的以线性表方式安排的。所以,咱们只需在此基础上经过修正u-boot的Nand Flash读写指令,增加处理00B区域数据的功用,即能够完成对Yaffs/Yaffs2文件体系的读写支撑。
完成对Yaffs或许Yaffs2文件体系的读写支撑过程如下:
①、在include/configs/my2440.h头文件中界说一个办理对Yaffs2支撑的宏和敞开u-boot中对Nand Flash默许分区的宏,如下:

#gedit include/configs/my2440.h //增加到文件结尾即可

#define CONFIG_MTD_NAND_YAFFS2 1//界说一个办理对Yaffs2支撑的宏

//敞开Nand Flash默许分区,留意此处的分区要和你的内核中的分区保持共同
#define MTDIDS_DEFAULT “nand0=nandflash0”
#define MTDPARTS_DEFAULT “mtdparts=nandflash0:192k(bootloader),” \
“64k(params),” \
“2m(kernel),” \
“-(root)”

②、在本来对Nand操作的指令集列表中增加Yaffs2对Nand的写指令,如下:

接着,在该文件中对nand操作的do_nand函数中增加yaffs2对nand的操作,如下:

if (strncmp(cmd, “read”, 4) == 0 || strncmp(cmd, “write”, 5) == 0)
{
int read;

if (argc < 4)
goto usage;

addr = (ulong)simple_strtoul(argv[2], NULL, 16);

read = strncmp(cmd, “read”, 4) == 0; /* 1 = read, 0 = write */
printf(“\nNAND %s: “, read ? “read” : “write”);
if (arg_off_size(argc – 3, argv + 3, nand, &off, &size) != 0)
return 1;

s = strchr(cmd, .);
if (!s || !strcmp(s, “.jffs2”) || !strcmp(s, “.e”) || !strcmp(s, “.i”))
{
if (read)
ret = nand_read_skip_bad(nand, off, &size, (u_char *)addr);
else
ret = nand_write_skip_bad(nand, off, &size, (u_char *)addr);
}

//增加yaffs2相关操作,留意该处又相关到nand_write_skip_bad函数

#if defined(CONFIG_MTD_NAND_YAFFS2)
else if (s != NULL && (!strcmp(s, “.yaffs2”)))
{
nand->rw_oob = 1;
nand->skipfirstblk = 1;
ret = nand_write_skip_bad(nand,off,&size,(u_char *)addr);
nand->skipfirstblk = 0;
nand->rw_oob = 0;
}
#endif

else if (!strcmp(s, “.oob”))
{
/* out-of-band data */
mtd_oob_ops_t ops =
{
.oobbuf = (u8 *)addr,
.ooblen = size,
.mode = MTD_OOB_RAW
};

if (read)
ret = nand->read_oob(nand, off, &ops);
else
ret = nand->write_oob(nand, off, &ops);
}
else
{
printf(“Unknown nand command suffix %s.\n”, s);
return 1;
}

printf(” %zu bytes %s: %s\n”, size, read ? “read” : “written”, ret ? “ERROR” : “OK”);

return ret == 0 ? 0 : 1;
}

③、在include/linux/mtd/mtd.h头文件的mtd_info结构体中增加上面用到rw_oob和skipfirstblk数据成员,如下:

#gedit include/linux/mtd/mtd.h //在mtd_info结构体中增加

#if defined(CONFIG_MTD_NAND_YAFFS2)
u_char rw_oob;
u_char skipfirstblk;
#endif

④、在第二步相关的nand_write_skip_bad函数中增加对Nand OOB的相关操作,如下:

#gedit drivers/mtd/nand/nand_util.c //在nand_write_skip_bad函数中增加

int nand_write_skip_bad(nand_info_t *nand, loff_t offset, size_t *length, u_char *buffer)
{
int rval;
size_t left_to_write = *length;
size_t len_incl_bad;
u_char *p_buffer = buffer;

#if defined(CONFIG_MTD_NAND_YAFFS2) //add yaffs2 file system support
if(nand->rw_oob==1)
{
size_t oobsize = nand->oobsize;
size_t datasize = nand->writesize;
int datapages = 0;

if (((*length)%(nand->oobsize+nand->writesize)) != 0)
{
printf (“Attempt to write error length data!\n”);
return -EINVAL;
}

datapages = *length/(datasize+oobsize);
*length = datapages*datasize;
left_to_write = *length;
}
#endif

/* Reject writes, which are not page aligned */
if ((offset & (nand->writesize – 1)) != 0 ||
(*length & (nand->writesize – 1)) != 0) {
printf (“Attempt to write non page aligned data\n”);
return -EINVAL;
}

len_incl_bad = get_len_incl_bad (nand, offset, *length);

if ((offset + len_incl_bad) >= nand->size) {
printf (“Attempt to write outside the flash area\n”);
return -EINVAL;
}

#if !defined(CONFIG_MTD_NAND_YAFFS2) //add yaffs2 file system support
if (len_incl_bad == *length) {
rval = nand_write (nand, offset, length, buffer);
if (rval != 0)
printf (“NAND write to offset %llx failed %d\n”,
offset, rval);

return rval;
}
#endif

while (left_to_write > 0) {
size_t block_offset = offset & (nand->erasesize – 1);
size_t write_size;

WATCHDOG_RESET ();

if (nand_block_isbad (nand, offset & ~(nand->erasesize – 1))) {
printf (“Skip bad block 0x%08llx\n”,
offset & ~(nand->erasesize – 1));
offset += nand->erasesize – block_offset;
continue;
}

#if defined(CONFIG_MTD_NAND_YAFFS2) //add yaffs2 file system support
if(nand->skipfirstblk==1)
{
nand->skipfirstblk=0;
printf (“Skip the first good block %llx\n”, offset & ~(nand->erasesize – 1));
offset += nand->erasesize – block_offset;
continue;
}
#endif

if (left_to_write < (nand->erasesize – block_offset))
write_size = left_to_write;
else
write_size = nand->erasesize – block_offset;

printf(“\rWriting at 0x%llx — “,offset);//add yaffs2 file system support

rval = nand_write (nand, offset, &write_size, p_buffer);
if (rval != 0) {
printf (“NAND write to offset %llx failed %d\n”,
offset, rval);
*length -= left_to_write;
return rval;
}

left_to_write -= write_size;
printf(“%d%% is complete.”,100-(left_to_write/(*length/100)));
offset += write_size;

#if defined(CONFIG_MTD_NAND_YAFFS2) //add yaffs2 file system support
if(nand->rw_oob==1)
{
p_buffer += write_size+(write_size/nand->writesize*nand->oobsize);
}
else
{
p_buffer += write_size;
}
#else
p_buffer += write_size;
#endif

}

return 0;
}

⑤、在第四步nand_write_skip_bad函数中咱们看到又对nand_write函数进行了拜访,所以这一步是到nand_write函数中增加对yaffs2的支撑,如下:

#gedit drivers/mtd/nand/nand_base.c //在nand_write函数中增加

static int nand_write(struct mtd_info *mtd, loff_t to, size_t len, size_t *retlen, const uint8_t *buf)
{
struct nand_chip *chip = mtd->priv;
int ret;

#if defined(CONFIG_MTD_NAND_YAFFS2) //add yaffs2 file system support

int oldopsmode = 0;

if(mtd->rw_oob==1)
{
int i = 0;
int datapages = 0;

size_t oobsize = mtd->oobsize;
size_t datasize = mtd->writesize;

uint8_t oobtemp[oobsize];
datapages = len / (datasize);

for(i = 0; i < (datapages); i++)
{
memcpy((void *)oobtemp, (void *)(buf + datasize * (i + 1)), oobsize);
memmove((void *)(buf + datasize * (i + 1)), (void *)(buf + datasize * (i + 1) + oobsize), (datapages – (i + 1)) * (datasize) + (datapages – 1) * oobsize);
memcpy((void *)(buf+(datapages) * (datasize + oobsize) – oobsize), (void *)(oobtemp), oobsize);
}
}
#endif

/* Do not allow reads past end of device */
if ((to + len) > mtd->size)
return -EINVAL;
if (!len)
return 0;

nand_get_device(chip, mtd, FL_WRITING);

chip->ops.len = len;
chip->ops.datbuf = (uint8_t *)buf;

#if defined(CONFIG_MTD_NAND_YAFFS2) //add yaffs2 file system support

if(mtd->rw_oob!=1)
{
chip->ops.oobbuf = NULL;
}
else
{
chip->ops.oobbuf = (uint8_t *)(buf + len);
chip->ops.ooblen = mtd->oobsize;
oldopsmode = chip->ops.mode;
chip->ops.mode = MTD_OOB_RAW;
}
#else
chip->ops.oobbuf = NULL;
#endif

ret = nand_do_write_ops(mtd, to, &chip->ops);

*retlen = chip->ops.retlen;

nand_release_device(mtd);

#if defined(CONFIG_MTD_NAND_YAFFS2) //add yaffs2 file system support

chip->ops.mode = oldopsmode;
#endif

return ret;
}

OK,对yaffs2支撑的代码已修正结束,从头编译u-boot并下载到nand中,发动开发板,在u-boot的指令行输入:nand help检查nand的指令,能够看到多了一个nand write[.yaffs2]的指令,这个便是用来下载yaffs2文件体系到nand中的指令了。

⑥、运用nand write[.yaffs2]指令把事前制造好的yaffs2文件体系下载到Nand Flash中(yaffs2文件体系的制造请参阅:Linux-2.6.30.4在2440上的移植之文件体系),下载操作过程和效果图如下:

⑦、结合u-boot和内核来测验发动下载的yaffs2文件体系
设置u-boot发动参数bootargs,留意:这一长串参数要与内核装备里边的Boot options–>Default kernel command string的设置要共同。特别是mtdblock3要根据内核详细的分区来设,在上一篇中讲到了内核中Nand的分区状况,u-boot归于mtdblock0,param归于mtdblock1,kernel归于mtdblock2,root就归于mtdblock3,所以这儿要设置成root=/dev/mtdblock3,不然文件体系无法发动成功,会呈现一些什么I/O之类的过错

好了,最终重启开发板,内核引导成功,yaffs2文件体系也挂载成功,效果图如下:

tftp 0x30000000 root-2.6.30.4.bin //用tftp将yaffs2文件体系下载到内存的0x30000000方位

nand erase 0x250000 0x3dac000 //擦除Nand的文件体系分区

nand write.yaffs2 0x30000000 0x250000 0x658170 //将内存中的yaffs2文件体系写入Nand的文件体系分区,留意这儿的0x658170是yaffs2文件体系的实践巨细(能够在tftp传送完后能够看到),要写正确,不然会构成假坏块

#gedit common/cmd_nand.c //在U_BOOT_CMD中增加

U_BOOT_CMD(nand, CONFIG_SYS_MAXARGS, 1, do_nand,
“NAND sub-system”,
“info – show available NAND devices\n”
“nand device [dev] – show or set current device\n”
“nand read – addr off|partition size\n”
“nand write – addr off|partition size\n”
” read/write size bytes starting at offset off\n”
” to/from memory address addr, skipping bad blocks.\n”

//留意:这儿只增加了yaffs2的写指令,由于咱们只用u-boot下载(即写)功用,所以咱们没有增加yaffs2读的指令
#if defined(CONFIG_MTD_NAND_YAFFS2)
“nand write[.yaffs2] – addr off|partition size – write `size byte yaffs image\n”
” starting at offset off from memory address addr (.yaffs2 for 512+16 NAND)\n”
#endif

“nand erase [clean] [off size] – erase size bytes from\n”
” offset off (entire device if not specified)\n”
“nand bad – show bad blocks\n”
“nand dump[.oob] off – dump page\n”
“nand scrub – really clean NAND erasing bad blocks (UNSAFE)\n”
“nand markbad off […] – mark bad block(s) at offset (UNSAFE)\n”
“nand biterr off – make a bit error at offset (UNSAFE)”
#ifdef CONFIG_CMD_NAND_LOCK_UNLOCK
“\n”
“nand lock [tight] [status]\n”
” bring nand to lock state or display locked pages\n”
“nand unlock [offset] [size] – unlock section”
#endif
);

声明:本文内容来自网络转载或用户投稿,文章版权归原作者和原出处所有。文中观点,不代表本站立场。若有侵权请联系本站删除(kf@86ic.com)https://www.86ic.net/yingyong/chuanganqi/277455.html

为您推荐

联系我们

联系我们

在线咨询: QQ交谈

邮箱: kf@86ic.com

关注微信
微信扫一扫关注我们

微信扫一扫关注我们

返回顶部