创建yaffs文件制作工具

来源:互联网 发布:滨波羽绒服质量 知乎 编辑:程序博客网 时间:2024/05/21 17:00

     移植yaffs根文件系统,开发板是mini2440。NAND flash是64M的,由于yaffs2/utils中的mkyaffsimage程序只能生成老式的yaffs1映象文件,需要修改才能支持新格式。yaffs1新、老格式的不同在于oob区的使用发生了变化:一是ECC检验码的位置发生了变化,二是可用空间即标记(tag)的数据结构定义发生了变化。

     移植uboot版本为uboot-2009.11,内核版本2.6.32.2;根文件系统制作采用busybox1.19.3。

1. 内核配置yaffs选项选择yaffs do it's own ECC

     由于yaffs采用自己的ECC校验,那么linux应该关闭ECC校验,否则无法挂载根文件系统,选择kernel为NAND_ECC_NONE。(在/drivers/mtd/nand/s3c2410.c中修改)

2. 没有选择yaffs do it's own ECC

    这时使用的mtd的ecc校验,linux应该选择NAND_ECC_SOFT(不能使用硬件ECC,因为mkyaffsimage程序中使用的是mtd的软件ecc校验)。


拷贝yaffs_packedtags1.c, yaffs_packedtags1.h,linux/drivers/mtd/nand/nand_ecc.c到utils目录

修改Makefile文件:
MKYAFFSSOURCES = mkyaffsimage.c  yaffs_packedtags1.c nand-ecc.c

mkyaffsimage.c添加:

#include "yaffs_packedtags1.h"

extern int nand_calculate_ecc(const unsigned char *buf, unsigned char *code);

主要修改write_chunk函数:

static int write_chunk(u8 *data, u32 obj_id, u32 chunk_id, u32 n_bytes)
{
#ifdef CONFIG_YAFFS_9BYTE_TAGS // old program
    struct yaffs_tags t;
    struct yaffs_spare s;


    error = write(outFile,data,512);
    if(error < 0) return error;

    memset(&t,0xff,sizeof (struct yaffs_tags));
    memset(&s,0xff,sizeof (struct yaffs_spare));

    t.chunk_id = chunk_id;
    t.serial_number = 0;
    t.n_bytes_lsb = n_bytes;
    t.obj_id = obj_id;

   if (convert_endian)
   {
        little_to_big_endian(&t);
   }
    yaffs_calc_tags_ecc(&t);
    yaffs_load_tags_to_spare(&s,&t);
    yaffs_calc_ecc(data,&s);
    nPages++;

    return write(outFile,&s,sizeof(struct yaffs_spare));
#endif  

    struct yaffs_packed_tags1 pt1;
    struct yaffs_ext_tags et1;
    char eccoob[16];
#ifdef YAFFS_OWN_ECC
    struct yaffs_spare s;
#else
    char ecc_code[6];
#endif

    error = write(outFile,data,512);
    if(error < 0) 
return error;


    memset(eccoob,0xff,16);

   et1.chunk_id = chunk_id;
   et1.serial_number = 0;
   et1.n_bytes = n_bytes;
   et1.obj_id = obj_id;
   et1.is_deleted = 0;

    yaffs_pack_tags1(&pt1, &et1);
    yaffs_calc_tags_ecc((struct yaffs_tags *)&pt1);
    memcpy(eccoob+8, &pt1, 8);

#ifdef YAFFS_OWN_ECC  
    yaffs_calc_ecc(data,&s);
    eccoob[0] = s.ecc1[0];
    eccoob[1] = s.ecc1[1];
    eccoob[2] = s.ecc1[2];
    eccoob[3] = s.ecc2[0];
    eccoob[6] = s.ecc2[1];
    eccoob[7] = s.ecc2[2];
#else
    memset(ecc_code, 0, 6);
    nand_calculate_ecc(data, &ecc_code[0]);
    nand_calculate_ecc(data+256, &ecc_code[3]);
    eccoob[0] = ecc_code[0];
    eccoob[1] = ecc_code[1];
    eccoob[2] = ecc_code[2];
    eccoob[3] = ecc_code[3];
    eccoob[6] = ecc_code[4];
    eccoob[7] = ecc_code[5];
#endif

   nPages++;
   return write(outFile,eccoob,16);
}

修改nand_ecc.c,修改nand_calculate_ecc函数,只需保留:

#include <linux/types.h>
static const char invparity[256] = {
1, 0, 0, 1, 0, 1, 1, 0, 0, 1, 1, 0, 1, 0, 0, 1,
0, 1, 1, 0, 1, 0, 0, 1, 1, 0, 0, 1, 0, 1, 1, 0,
0, 1, 1, 0, 1, 0, 0, 1, 1, 0, 0, 1, 0, 1, 1, 0,
1, 0, 0, 1, 0, 1, 1, 0, 0, 1, 1, 0, 1, 0, 0, 1,
0, 1, 1, 0, 1, 0, 0, 1, 1, 0, 0, 1, 0, 1, 1, 0,
1, 0, 0, 1, 0, 1, 1, 0, 0, 1, 1, 0, 1, 0, 0, 1,
1, 0, 0, 1, 0, 1, 1, 0, 0, 1, 1, 0, 1, 0, 0, 1,
0, 1, 1, 0, 1, 0, 0, 1, 1, 0, 0, 1, 0, 1, 1, 0,
0, 1, 1, 0, 1, 0, 0, 1, 1, 0, 0, 1, 0, 1, 1, 0,
1, 0, 0, 1, 0, 1, 1, 0, 0, 1, 1, 0, 1, 0, 0, 1,
1, 0, 0, 1, 0, 1, 1, 0, 0, 1, 1, 0, 1, 0, 0, 1,
0, 1, 1, 0, 1, 0, 0, 1, 1, 0, 0, 1, 0, 1, 1, 0,
1, 0, 0, 1, 0, 1, 1, 0, 0, 1, 1, 0, 1, 0, 0, 1,
0, 1, 1, 0, 1, 0, 0, 1, 1, 0, 0, 1, 0, 1, 1, 0,
0, 1, 1, 0, 1, 0, 0, 1, 1, 0, 0, 1, 0, 1, 1, 0,
1, 0, 0, 1, 0, 1, 1, 0, 0, 1, 1, 0, 1, 0, 0, 1
};
int nand_calculate_ecc(const unsigned char *buf, unsigned char *code)
{
int i;
const uint32_t *bp = (uint32_t *)buf;
/* 256 or 512 bytes/ecc  */
const uint32_t eccsize_mult = 1;
uint32_t cur; /* current value in buffer */
/* rp0..rp15..rp17 are the various accumulated parities (per byte) */
uint32_t rp0, rp1, rp2, rp3, rp4, rp5, rp6, rp7;
uint32_t rp8, rp9, rp10, rp11, rp12, rp13, rp14, rp15, rp16;
uint32_t rp17;/* to make compiler happy */
uint32_t par; /* the cumulative parity for all data */
uint32_t tmppar;

par = 0;
rp4 = 0;
rp6 = 0;
rp8 = 0;
rp10 = 0;
rp12 = 0;
rp14 = 0;
rp16 = 0;

for (i = 0; i < eccsize_mult << 2; i++) {
cur = *bp++;
tmppar = cur;
rp4 ^= cur;
cur = *bp++;
tmppar ^= cur;
rp6 ^= tmppar;
cur = *bp++;
tmppar ^= cur;
rp4 ^= cur;
cur = *bp++;
tmppar ^= cur;
rp8 ^= tmppar;


cur = *bp++;
tmppar ^= cur;
rp4 ^= cur;
rp6 ^= cur;
cur = *bp++;
tmppar ^= cur;
rp6 ^= cur;
cur = *bp++;
tmppar ^= cur;
rp4 ^= cur;
cur = *bp++;
tmppar ^= cur;
rp10 ^= tmppar;


cur = *bp++;
tmppar ^= cur;
rp4 ^= cur;
rp6 ^= cur;
rp8 ^= cur;
cur = *bp++;
tmppar ^= cur;
rp6 ^= cur;
rp8 ^= cur;
cur = *bp++;
tmppar ^= cur;
rp4 ^= cur;
rp8 ^= cur;
cur = *bp++;
tmppar ^= cur;
rp8 ^= cur;


cur = *bp++;
tmppar ^= cur;
rp4 ^= cur;
rp6 ^= cur;
cur = *bp++;
tmppar ^= cur;
rp6 ^= cur;
cur = *bp++;
tmppar ^= cur;
rp4 ^= cur;
cur = *bp++;
tmppar ^= cur;


par ^= tmppar;
if ((i & 0x1) == 0)
rp12 ^= tmppar;
if ((i & 0x2) == 0)
rp14 ^= tmppar;
if (eccsize_mult == 2 && (i & 0x4) == 0)
rp16 ^= tmppar;
}

/*
* handle the fact that we use longword operations
* we'll bring rp4..rp14..rp16 back to single byte entities by
* shifting and xoring first fold the upper and lower 16 bits,
* then the upper and lower 8 bits.
*/
rp4 ^= (rp4 >> 16);
rp4 ^= (rp4 >> 8);
rp4 &= 0xff;
rp6 ^= (rp6 >> 16);
rp6 ^= (rp6 >> 8);
rp6 &= 0xff;
rp8 ^= (rp8 >> 16);
rp8 ^= (rp8 >> 8);
rp8 &= 0xff;
rp10 ^= (rp10 >> 16);
rp10 ^= (rp10 >> 8);
rp10 &= 0xff;
rp12 ^= (rp12 >> 16);
rp12 ^= (rp12 >> 8);
rp12 &= 0xff;
rp14 ^= (rp14 >> 16);
rp14 ^= (rp14 >> 8);
rp14 &= 0xff;
if (eccsize_mult == 2) {
rp16 ^= (rp16 >> 16);
rp16 ^= (rp16 >> 8);
rp16 &= 0xff;
}


/*
* we also need to calculate the row parity for rp0..rp3
* This is present in par, because par is now
* rp3 rp3 rp2 rp2 in little endian and
* rp2 rp2 rp3 rp3 in big endian
* as well as
* rp1 rp0 rp1 rp0 in little endian and
* rp0 rp1 rp0 rp1 in big endian
* First calculate rp2 and rp3
*/
#ifdef __BIG_ENDIAN
rp2 = (par >> 16);
rp2 ^= (rp2 >> 8);
rp2 &= 0xff;
rp3 = par & 0xffff;
rp3 ^= (rp3 >> 8);
rp3 &= 0xff;
#else
rp3 = (par >> 16);
rp3 ^= (rp3 >> 8);
rp3 &= 0xff;
rp2 = par & 0xffff;
rp2 ^= (rp2 >> 8);
rp2 &= 0xff;
#endif


/* reduce par to 16 bits then calculate rp1 and rp0 */
par ^= (par >> 16);
#ifdef __BIG_ENDIAN
rp0 = (par >> 8) & 0xff;
rp1 = (par & 0xff);
#else
rp1 = (par >> 8) & 0xff;
rp0 = (par & 0xff);
#endif

/* finally reduce par to 8 bits */
par ^= (par >> 8);
par &= 0xff;
/*
* and calculate rp5..rp15..rp17
* note that par = rp4 ^ rp5 and due to the commutative property
* of the ^ operator we can say:
* rp5 = (par ^ rp4);
* The & 0xff seems superfluous, but benchmarking learned that
* leaving it out gives slightly worse results. No idea why, probably
* it has to do with the way the pipeline in pentium is organized.
*/
rp5 = (par ^ rp4) & 0xff;
rp7 = (par ^ rp6) & 0xff;
rp9 = (par ^ rp8) & 0xff;
rp11 = (par ^ rp10) & 0xff;
rp13 = (par ^ rp12) & 0xff;
rp15 = (par ^ rp14) & 0xff;
if (eccsize_mult == 2)
rp17 = (par ^ rp16) & 0xff;
/*
* Finally calculate the ecc bits.
* Again here it might seem that there are performance optimisations
* possible, but benchmarks showed that on the system this is developed
* the code below is the fastest
*/
#ifdef CONFIG_MTD_NAND_ECC_SMC
code[0] =
   (invparity[rp7] << 7) |
   (invparity[rp6] << 6) |
   (invparity[rp5] << 5) |
   (invparity[rp4] << 4) |
   (invparity[rp3] << 3) |
   (invparity[rp2] << 2) |
   (invparity[rp1] << 1) |
   (invparity[rp0]);
code[1] =
   (invparity[rp15] << 7) |
   (invparity[rp14] << 6) |
   (invparity[rp13] << 5) |
   (invparity[rp12] << 4) |
   (invparity[rp11] << 3) |
   (invparity[rp10] << 2) |
   (invparity[rp9] << 1)  |
   (invparity[rp8]);
#else
code[1] =
   (invparity[rp7] << 7) |
   (invparity[rp6] << 6) |
   (invparity[rp5] << 5) |
   (invparity[rp4] << 4) |
   (invparity[rp3] << 3) |
   (invparity[rp2] << 2) |
   (invparity[rp1] << 1) |
   (invparity[rp0]);
code[0] =
   (invparity[rp15] << 7) |
   (invparity[rp14] << 6) |
   (invparity[rp13] << 5) |
   (invparity[rp12] << 4) |
   (invparity[rp11] << 3) |
   (invparity[rp10] << 2) |
   (invparity[rp9] << 1)  |
   (invparity[rp8]);
#endif
if (eccsize_mult == 1)
code[2] =
   (invparity[par & 0xf0] << 7) |
   (invparity[par & 0x0f] << 6) |
   (invparity[par & 0xcc] << 5) |
   (invparity[par & 0x33] << 4) |
   (invparity[par & 0xaa] << 3) |
   (invparity[par & 0x55] << 2) |
   3;
else
code[2] =
   (invparity[par & 0xf0] << 7) |
   (invparity[par & 0x0f] << 6) |
   (invparity[par & 0xcc] << 5) |
   (invparity[par & 0x33] << 4) |
   (invparity[par & 0xaa] << 3) |
   (invparity[par & 0x55] << 2) |
   (invparity[rp17] << 1) |
   (invparity[rp16] << 0);
   return 0;
}

    

    



原创粉丝点击