【环境】
1:Ubuntu 10.10
2:u-boot-2010.03
3:优龙FS2410
4:交叉编译器:arm-none-linux-gnueabi-gcc version 4.3.2
第一步:在内核里添加yaffs2的支持
1)下载源码
tony@Ubuntu:~/ win$ sudo apt-get install git
tony@Ubuntu:~/ win$ git clone git://www.aleph1.co.uk/yaffs2
2)给内核打补丁
tony@Ubuntu:~/win/yaffs2$./patch-ker.sh c m /home/tony/win/linux-2.6.35 // 这里是内核源码路径
3)配置内核 (1:没有打过补丁的内核 2:已设置好交叉编译器)
tony@Ubuntu:~/win/linux-2.6.35$ make menuconfig
File systems --->
[*] Miscellaneous filesystems --->
<*> YAFFS2 file system support
-*- 512 byte / page devices
[*] Lets yaffs do its own ECC
-*- 2048 byte (or larger) / page devices
[*] Autoselect yaffs2 format
[*] Enable yaffs2 xattr support
4)禁用内核NandFlash的ECC校样
tony@Ubuntu:~/win/linux-2.6.35$vim drivers/mtd/nand/s3c2410.c
842行:
修改:chip->eccmode = NAND_ECC_SOFT;
改为:chip->eccmode = NAND_ECC_NONE;
5)重新编译内核
tony@Ubuntu:~/win/linux-2.6.35$ make uImage
6)拷贝内核到TFTP目录
tony@Ubuntu:~/win/linux-2.6.35$ cp arch/arm/boot/uImage /tftpboot
7)重新启动开发板
# cat /proc/filesystems
nodev sysfs
nodev rootfs
nodev bdev
nodev proc
nodev tmpfs
nodev sockfs
nodev usbfs
nodev pipefs
nodev anon_inodefs
nodev rpc_pipefs
nodev devpts
ext3
ext2
cramfs
nodev ramfs
vfat
msdos
iso9660
nodev nfs
nodev jffs2
romfs
yaffs
yaffs2
nodev mtd_inodefs
第二步:实现u-boot对Yaffs2文件系统的支持
①、在include/configs/smdk2440.h头文件中定义一个管理对Yaffs2支持的宏和开启u-boot中对Nand Flash默认分区的宏
tony@Ubuntu:~/win/u-boot-2010.03$vim include/configs/fs2410.h
#define CONFIG_MTD_NAND_YAFFS2 1 //定义一个管理对Yaffs2支持的宏
//开启Nand Flash默认分区,注意此处的分区要和你的内核中的分区保持一致
#define MTDIDS_DEFAULT "nand0=nandflash0"
#define MTDPARTS_DEFAULT "mtdparts=nandflash0:1m(bootloader u-boot-2010.03)," \
"4m(kernel linux-2.6.35)," \
"8m(rootfs busybox-1.17.3)," \
"-(usrfs)"
②、在common/cmd_nand.c中,对Nand操作的命令集列表中添加Yaffs2对Nand的写命令(在U_BOOT_CMD中添加)
tony@Ubuntu:~/win/u-boot-2010.03$vim common/cmd_nand.c
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
);
接着,在该文件中对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数据成员
tony@Ubuntu:~/win/u-boot-2010.03$vim include/linux/mtd/mtd.h
#if defined(CONFIG_MTD_NAND_YAFFS2)
u_char rw_oob;
u_char skipfirstblk;
#endif
④、在第二步关联的drivers/mtd/nand/nand_util.c 的nand_write_skip_bad函数中添加对Nand OOB的相关操作
tony@Ubuntu:~/win/u-boot-2010.03$vim drivers/mtd/nand/nand_util.c
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
#if 0
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;
//offset += write_size;
//p_buffer += write_size;
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函数进行了访问,所以这一步是到drivers/mtd/nand/nand_base.c 的nand_write函数中添加对yaffs2的支持
tony@Ubuntu:~/win/u-boot-2010.03$vim drivers/mtd/nand/nand_base.c
2095行
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;
//chip->ops.oobbuf = NULL;
#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中的命令了。
第三步:用busybox制作yaffs2根文件系统
1)参看--》根文件系统制作
2)目标板NandFlash是64MB的,所以要使用mkyaffs2image_2这个版本的可执行文件来生成映像
tony@Ubuntu:/opt$ mkyaffsimage_2 filesystem filesystem.yaffs2
tony@Ubuntu:/opt$cp filesystem.yaffs2 /tftpboot/
3)烧录到NandFlash中并测试
tftp 30008000 filesystem.yaffs2
nand erase 500000 800000
nand write.yaffs2 30008000 500000 实际大小
setenv bootargs root=/dev/mtdblock2 init=/linuxrc rootfstype=yaffs2 rw console=ttySAC0,115200