---
title: "mkyaffs2image"
author: "neiloid"
date: "2011-12-03T16:53:00Z"
tags: []
slug: "mkyaffs2image"
draft: false
lastmod: "2011-12-04T09:33:41Z"
id: 1290931
url: "https://neiloid.iteye.com/blog/1290931"
---

终于可以成功制作页大小为2K + 64 Bytes 的nand flash的image 文件了。令人兴奋啊 ：)

这两天一直苦于自己制作的yaffs2 image文件系统无法被linux-2.6.29.1内核识别，而使用FriendlyARM的mkyaffs2imag-128M工具就没有这个问题。一开始便判断是mkyaffs2image工具有问题，没有生成正确格式的yaffs2 image文件，从而导致内核无法识别，并抛出以下错误信息：

\----------------------------------------------------------------------------------------------------------------

……

block 155 is bad  
yaffs\_read\_super: isCheckpointed 0  
VFS: Mounted root (yaffs filesystem) readonly on device 31:2.  
Freeing init memory: 164K  
Warning: unable to open an initial console.  
Failed to execute /linuxrc. Attempting defaults...  
Kernel panic - not syncing: No init found. Try passing init= option to kernel.

\----------------------------------------------------------------------------------------------------------------

网上查了很多相关的资料，对2K data + 64bytes spare 的nand flash上yaffs2文件的存储格式并没有较为清晰的说明。不知道数据的存储模式，就无法正确生成image文件。苦恼了好几天，甚至有些想放弃了。。。

然而今天得到了一个意外的收获，[http://www.bluewatersys.com/quickstart/9260sambootassistant.php](http://www.bluewatersys.com/quickstart/9260sambootassistant.php)上大概阐述了2K yaffs2 nand flash中数据的组织形式(虽然后来发现该文章中关于结构yaffs2\_PackedTags的大小表述有错，应该是28字节，而非25字节)，经过修改后，并且纠正相关bug，成功生成正确的yaffs2 image 文件，download进s3c2440开发板后，顺利启动系统。

有一点很重要，就是mkyaffs2image生成的image 文件的OOB区数据格式要与linux kernel对nand flash进行读取时的数据格式保持一致，所以mkyaffs2image.c中来自linux kernel的数据结构，即包含了mtd-abi.h头文件。

PS：下面本人会附上mkyaffs2image.c的全部内容，供朋友们参考，希望给meet with同样error的兄弟们一些帮助：)

**PART2 About 64bytes OOB**

下图为64bytes OOB区中的数据组织结构：

图1 64bytes OOB area

PS：

1\) OOB最前端的2bytes为0xFF 0xFF，用于标识为非坏块。

2\) 每256bytes的数据需要3bytes有ECC校验，因此2K的数据需要24bytes的ECC。

以下为经过修改过后的mkyaffs2image.c的源码：

/*  
\* YAFFS: Yet Another Flash File System. A NAND-flash specific file system.  
\*  
\* Copyright (C) 2002-2007 Aleph One Ltd.  
\* for Toby Churchill Ltd and Brightstar Engineering  
\*  
\* Created by Charles Manning &lt;[charles@aleph1.co.uk](mailto:charles@aleph1.co.uk)&gt;  
\* Nick Bane modifications flagged NCB  
\* Endian handling patches by James Ng.  
\* mkyaffs2image hacks by NCB  
\*  
\* This program is free software; you can redistribute it and/or modify  
\* it under the terms of the GNU General Public License version 2 as  
\* published by the Free Software Foundation.  
\*/

/*  
\* makeyaffs2image.c  
\*  
\* Makes a YAFFS2 file system image that can be used to load up a file system.  
\* Uses default Linux MTD layout - change if you need something different.  
\*/

#include &lt;stdlib.h&gt;  
#include &lt;stdio.h&gt;  
#include &lt;fcntl.h&gt;  
#include &lt;sys/types.h&gt;  
#include &lt;sys/stat.h&gt;  
#include &lt;dirent.h&gt;  
#include &lt;string.h&gt;  
#include &lt;unistd.h&gt;  
#define \_\_user  
#include &lt;mtd/mtd-user.h&gt;  
#include "yaffs\_ecc.h"  
#include "yaffs\_guts.h"

#include "yaffs\_packedtags2.h"

#include "mtd-abi.h"//ADDED BY LIUHAO 2009-11-11

unsigned yaffs\_traceMask=0;

#define MAX\_OBJECTS 10000

#define chunkSize 2048  
#define spareSize 64

//MODIFIED BY LIUHAO 2009-11-11 START  
//#define PT2\_BYTES 25//Original  
#define PT2\_BYTES 28  
//MODIFIED BY LIUHAO 2009-11-11 END

const char * mkyaffsimage\_c\_version = "$Id: mkyaffs2image.c,v 1.4 2007-02-14 01:09:06 wookey Exp $";

static intlayout\_no;

static struct nand\_oobinfo oob\_layout\[] = {  
/* KSI:  
\* Dummy "raw" layout - no ECC, all the bytes are free. Does NOT  
\* really work, only used for compatibility with CVS YAFFS2 that  
\* never ever worked with any stock MTD.  
\*/  
{  
.useecc = MTD\_NANDECC\_AUTOPLACE,  
.eccbytes = 0,  
.eccpos = {},  
.oobfree = { {0, 64} }  
},  
/* KSI:  
\* Regular MTD AUTOPLACED ECC for large page NAND devices, the  
\* only one existing in stock MTD so far. It corresponds to layout# 1  
\* in command line arguments. Any other layouts could be added to  
\* the list when they made their way in kernel's MTD. The structure  
\* is simply copied from kernel's drivers/mtd/nand/nand\_base.c as-is.  
\*/  
{  
.useecc = MTD\_NANDECC\_AUTOPLACE,  
.eccbytes = 24,  
.eccpos = {  
40, 41, 42, 43, 44, 45, 46, 47,  
48, 49, 50, 51, 52, 53, 54, 55,  
56, 57, 58, 59, 60, 61, 62, 63},  
.oobfree = { {2, 38} }  
},  
/* End-of-list marker \*/  
{  
.useecc = -1,  
}  
};

typedef struct {  
dev\_t dev;  
ino\_t ino;  
int obj;  
} objItem;

static objItem obj\_list\[MAX\_OBJECTS];  
static int n\_obj = 0;  
static int obj\_id = YAFFS\_NOBJECT\_BUCKETS + 1;

static int nObjects = 0, nDirectories = 0, nPages = 0;

static int outFile;

static int error;

static int convert\_endian = 0;

static int obj\_compare(const void \*a, const void * b)  
{  
objItem \*oa, \*ob;

oa = (objItem \*)a;  
ob = (objItem \*)b;

if(oa-&gt;dev &lt; ob-&gt;dev) return -1;  
if(oa-&gt;dev &gt; ob-&gt;dev) return 1;  
if(oa-&gt;ino &lt; ob-&gt;ino) return -1;  
if(oa-&gt;ino &gt; ob-&gt;ino) return 1;

return 0;  
}

static void add\_obj\_to\_list(dev\_t dev, ino\_t ino, int obj)  
{  
if(n\_obj &lt; MAX\_OBJECTS)  
{  
obj\_list\[n\_obj].dev = dev;  
obj\_list\[n\_obj].ino = ino;  
obj\_list\[n\_obj].obj = obj;  
n\_obj++;  
qsort(obj\_list,n\_obj,sizeof(objItem),obj\_compare);

}  
else  
{  
// oops! not enough space in the object array  
fprintf(stderr,"Not enough space in object array/n");  
exit(2);  
}  
}

static int find\_obj\_in\_list(dev\_t dev, ino\_t ino)  
{  
objItem \*i = NULL;  
objItem test;

test.dev = dev;  
test.ino = ino;

if(n\_obj &gt; 0)  
{  
i = bsearch(&test,obj\_list,n\_obj,sizeof(objItem),obj\_compare);  
}

if(i)  
{  
return i-&gt;obj;  
}  
return -1;  
}

static void yaffs\_PutDataECC(const \_\_u8 * data, unsigned char \*oob\_buf)  
{  
unsigned char ecc\_code\[3];  
int eccsteps = chunkSize / 256;  
int eccidx = 0, datidx = 0, i;  
struct nand\_oobinfo \*oobsel;  
\_\_u32 \*oob\_config;

oobsel = &oob\_layout\[layout\_no];  
oob\_config = oobsel-&gt;eccpos;//LIUHAO: 24bytes的ECC在spare area中的位置

for (; eccsteps; eccsteps--) {  
yaffs\_ECCCalculate (&data\[datidx], ecc\_code);  
for (i = 0; i &lt; 3; i++, eccidx++)  
oob\_buf\[oob\_config\[eccidx]] = ecc\_code\[i];  
datidx += 256;  
}  
}

/* KSI:  
\* No big endian for now. This is left for a later time. The existing code  
\* is FUBAR.  
\*/  
#if 0  
/* This little function converts a little endian tag to a big endian tag.  
\* NOTE: The tag is not usable after this other than calculating the CRC  
\* with.  
\*/  
static void little\_to\_big\_endian(yaffs\_Tags \*tagsPtr)  
{  
#if 0 // FIXME NCB  
yaffs\_TagsUnion * tags = (yaffs\_TagsUnion* )tagsPtr; // Work in bytes.  
yaffs\_TagsUnion temp;

memset(&temp, 0, sizeof(temp));  
// Ick, I hate magic numbers.  
temp.asBytes\[0] = ((tags-&gt;asBytes\[2] & 0x0F) &lt;&lt; 4) | ((tags-&gt;asBytes\[1] & 0xF0) &gt;&gt; 4);  
temp.asBytes\[1] = ((tags-&gt;asBytes\[1] & 0x0F) &lt;&lt; 4) | ((tags-&gt;asBytes\[0] & 0xF0) &gt;&gt; 4);  
temp.asBytes\[2] = ((tags-&gt;asBytes\[0] & 0x0F) &lt;&lt; 4) | ((tags-&gt;asBytes\[2] & 0x30) &gt;&gt; 2) | ((tags-&gt;asBytes\[3] & 0xC0) &gt;&gt; 6);  
temp.asBytes\[3] = ((tags-&gt;asBytes\[3] & 0x3F) &lt;&lt; 2) | ((tags-&gt;asBytes\[2] & 0xC0) &gt;&gt; 6);  
temp.asBytes\[4] = ((tags-&gt;asBytes\[6] & 0x03) &lt;&lt; 6) | ((tags-&gt;asBytes\[5] & 0xFC) &gt;&gt; 2);  
temp.asBytes\[5] = ((tags-&gt;asBytes\[5] & 0x03) &lt;&lt; 6) | ((tags-&gt;asBytes\[4] & 0xFC) &gt;&gt; 2);  
temp.asBytes\[6] = ((tags-&gt;asBytes\[4] & 0x03) &lt;&lt; 6) | (tags-&gt;asBytes\[7] & 0x3F);  
temp.asBytes\[7] = (tags-&gt;asBytes\[6] & 0xFC) | ((tags-&gt;asBytes\[7] & 0xC0) &gt;&gt; 6);

// Now copy it back.  
tags-&gt;asBytes\[0] = temp.asBytes\[0];  
tags-&gt;asBytes\[1] = temp.asBytes\[1];  
tags-&gt;asBytes\[2] = temp.asBytes\[2];  
tags-&gt;asBytes\[3] = temp.asBytes\[3];  
tags-&gt;asBytes\[4] = temp.asBytes\[4];  
tags-&gt;asBytes\[5] = temp.asBytes\[5];  
tags-&gt;asBytes\[6] = temp.asBytes\[6];  
tags-&gt;asBytes\[7] = temp.asBytes\[7];  
#endif  
}  
#endif

static void nandmtd2\_pt2buf(unsigned char \*buf, yaffs\_PackedTags2 \*pt)  
{  
inti, j = 0, k, n;  
//MODIFIED BY LIUHAO 2009-11-11  
//unsigned charpt2\_byte\_buf\[PT2\_BYTES];//Original  
unsigned charpt2\_byte\_buf\[PT2\_BYTES] = {0};

\*((unsigned int \*) &pt2\_byte\_buf\[0]) = pt-&gt;t.sequenceNumber;  
\*((unsigned int \*) &pt2\_byte\_buf\[4]) = pt-&gt;t.objectId;  
\*((unsigned int \*) &pt2\_byte\_buf\[8]) = pt-&gt;t.chunkId;  
\*((unsigned int \*) &pt2\_byte\_buf\[12]) = pt-&gt;t.byteCount;  
pt2\_byte\_buf\[16] = pt-&gt;ecc.colParity;  
//MODIFIED BY LIUHAO 2009-11-11 START  
#if 0  
//Original  
pt2\_byte\_buf\[17] = pt-&gt;ecc.lineParity & 0xff;  
pt2\_byte\_buf\[18] = (pt-&gt;ecc.lineParity &gt;&gt; 8) & 0xff;  
pt2\_byte\_buf\[19] = (pt-&gt;ecc.lineParity &gt;&gt; 16) & 0xff;  
pt2\_byte\_buf\[20] = (pt-&gt;ecc.lineParity &gt;&gt; 24) & 0xff;  
pt2\_byte\_buf\[21] = pt-&gt;ecc.lineParityPrime & 0xff;  
pt2\_byte\_buf\[22] = (pt-&gt;ecc.lineParityPrime &gt;&gt; 8) & 0xff;  
pt2\_byte\_buf\[23] = (pt-&gt;ecc.lineParityPrime &gt;&gt; 16) & 0xff;  
pt2\_byte\_buf\[24] = (pt-&gt;ecc.lineParityPrime &gt;&gt; 24) & 0xff;  
#else  
pt2\_byte\_buf\[20] = pt-&gt;ecc.lineParity & 0xff;  
pt2\_byte\_buf\[21] = (pt-&gt;ecc.lineParity &gt;&gt; 8) & 0xff;  
pt2\_byte\_buf\[22] = (pt-&gt;ecc.lineParity &gt;&gt; 16) & 0xff;  
pt2\_byte\_buf\[23] = (pt-&gt;ecc.lineParity &gt;&gt; 24) & 0xff;  
pt2\_byte\_buf\[24] = pt-&gt;ecc.lineParityPrime & 0xff;  
pt2\_byte\_buf\[25] = (pt-&gt;ecc.lineParityPrime &gt;&gt; 8) & 0xff;  
pt2\_byte\_buf\[26] = (pt-&gt;ecc.lineParityPrime &gt;&gt; 16) & 0xff;  
pt2\_byte\_buf\[27] = (pt-&gt;ecc.lineParityPrime &gt;&gt; 24) & 0xff;  
#endif

k = oob\_layout\[layout\_no].oobfree\[j]\[0];  
n = oob\_layout\[layout\_no].oobfree\[j]\[1];

if (n == 0) {  
fprintf(stderr, "No OOB space for tags");  
exit(-1);  
}

for (i = 0; i &lt; PT2\_BYTES; i++) {  
if (n == 0) {  
j++;  
k = oob\_layout\[layout\_no].oobfree\[j]\[0];  
n = oob\_layout\[layout\_no].oobfree\[j]\[1];  
if (n == 0) {  
fprintf(stderr, "No OOB space for tags");  
exit(-1);  
}  
}  
buf\[k++] = pt2\_byte\_buf\[i];  
n--;  
}  
}

static int write\_chunk(\_\_u8 \*data, \_\_u32 objId, \_\_u32 chunkId, \_\_u32 nBytes)  
{  
yaffs\_ExtendedTags t;  
yaffs\_PackedTags2 pt;

yaffs\_PackedTags2TagsPart ptt;//ADDED BY LIUHAO 2009-11-11

//ADDED BY LIUHAO 2009-11-11  
//unsigned charspare\_buf\[spareSize];//Original  
unsigned charspare\_buf\[spareSize] = {0};

error = write(outFile,data,chunkSize);  
if(error &lt; 0) return error;

yaffs\_InitialiseTags(&t);

t.chunkId = chunkId;  
//t.serialNumber = 0;  
t.serialNumber = 1;// \*\*CHECK\**  
t.byteCount = nBytes;  
t.objectId = objId;

t.sequenceNumber = YAFFS\_LOWEST\_SEQUENCE\_NUMBER;

// added NCB \*\*CHECK\**  
t.chunkUsed = 1;

//ADDED BY LIUHAO 2009-11-11  
//t.extraHeaderInfoAvailable = 1;

/* KSI: Broken anyway -- e.g. &t is pointer to a wrong type... \*/  
#if 0  
if (convert\_endian)  
{  
little\_to\_big\_endian(&t);  
}  
#endif

//MODIFIED BY LIUHAO 2009-11-11 START  
#if 0  
yaffs\_PackTags2(&pt,&t);//Original  
#else  
memset(&pt, 0, sizeof(yaffs\_PackedTags2));  
yaffs\_PackTags2TagsPart(&pt.t,&t);  
yaffs\_ECCCalculateOther(  
(unsigned char \*)&pt.t,  
sizeof(yaffs\_PackedTags2TagsPart),  
&pt.ecc);  
#endif  
//MODIFIED BY LIUHAO 2009-11-11 END

memset(spare\_buf, 0xff, sizeof(spare\_buf));

if (layout\_no == 0) {  
memcpy(spare\_buf, &pt, sizeof(yaffs\_PackedTags2));  
} else {  
nandmtd2\_pt2buf(spare\_buf, &pt);  
}

yaffs\_PutDataECC(data, &spare\_buf\[0]);

nPages++;

return write(outFile,spare\_buf,spareSize);  
}

#define SWAP32(x) ((((x) & 0x000000FF) &lt;&lt; 24) | /  
(((x) & 0x0000FF00) &lt;&lt; 8 ) | /  
(((x) & 0x00FF0000) &gt;&gt; 8 ) | /  
(((x) & 0xFF000000) &gt;&gt; 24))

#define SWAP16(x) ((((x) & 0x00FF) &lt;&lt; 8) | /  
(((x) & 0xFF00) &gt;&gt; 8))

/* KSI: Removed for now. TBD later when the proper util (from scratch) is written \*/  
#if 0  
// This one is easier, since the types are more standard. No funky shifts here.  
static void object\_header\_little\_to\_big\_endian(yaffs\_ObjectHeader* oh)  
{  
oh-&gt;type = SWAP32(oh-&gt;type); // GCC makes enums 32 bits.  
oh-&gt;parentObjectId = SWAP32(oh-&gt;parentObjectId); // int  
oh-&gt;sum\_\_NoLongerUsed = SWAP16(oh-&gt;sum\_\_NoLongerUsed); // \_\_u16 - Not used, but done for completeness.  
// name = skip. Char array. Not swapped.  
oh-&gt;yst\_mode = SWAP32(oh-&gt;yst\_mode);  
#ifdef CONFIG\_YAFFS\_WINCE // WinCE doesn't implement this, but we need to just in case.  
// In fact, WinCE would be \*THE* place where this would be an issue!  
oh-&gt;notForWinCE\[0] = SWAP32(oh-&gt;notForWinCE\[0]);  
oh-&gt;notForWinCE\[1] = SWAP32(oh-&gt;notForWinCE\[1]);  
oh-&gt;notForWinCE\[2] = SWAP32(oh-&gt;notForWinCE\[2]);  
oh-&gt;notForWinCE\[3] = SWAP32(oh-&gt;notForWinCE\[3]);  
oh-&gt;notForWinCE\[4] = SWAP32(oh-&gt;notForWinCE\[4]);  
#else  
// Regular POSIX.  
oh-&gt;yst\_uid = SWAP32(oh-&gt;yst\_uid);  
oh-&gt;yst\_gid = SWAP32(oh-&gt;yst\_gid);  
oh-&gt;yst\_atime = SWAP32(oh-&gt;yst\_atime);  
oh-&gt;yst\_mtime = SWAP32(oh-&gt;yst\_mtime);  
oh-&gt;yst\_ctime = SWAP32(oh-&gt;yst\_ctime);  
#endif

oh-&gt;fileSize = SWAP32(oh-&gt;fileSize); // Aiee. An int... signed, at that!  
oh-&gt;equivalentObjectId = SWAP32(oh-&gt;equivalentObjectId);  
// alias - char array.  
oh-&gt;yst\_rdev = SWAP32(oh-&gt;yst\_rdev);

#ifdef CONFIG\_YAFFS\_WINCE  
oh-&gt;win\_ctime\[0] = SWAP32(oh-&gt;win\_ctime\[0]);  
oh-&gt;win\_ctime\[1] = SWAP32(oh-&gt;win\_ctime\[1]);  
oh-&gt;win\_atime\[0] = SWAP32(oh-&gt;win\_atime\[0]);  
oh-&gt;win\_atime\[1] = SWAP32(oh-&gt;win\_atime\[1]);  
oh-&gt;win\_mtime\[0] = SWAP32(oh-&gt;win\_mtime\[0]);  
oh-&gt;win\_mtime\[1] = SWAP32(oh-&gt;win\_mtime\[1]);  
oh-&gt;roomToGrow\[0] = SWAP32(oh-&gt;roomToGrow\[0]);  
oh-&gt;roomToGrow\[1] = SWAP32(oh-&gt;roomToGrow\[1]);  
oh-&gt;roomToGrow\[2] = SWAP32(oh-&gt;roomToGrow\[2]);  
oh-&gt;roomToGrow\[3] = SWAP32(oh-&gt;roomToGrow\[3]);  
oh-&gt;roomToGrow\[4] = SWAP32(oh-&gt;roomToGrow\[4]);  
oh-&gt;roomToGrow\[5] = SWAP32(oh-&gt;roomToGrow\[5]);  
#else  
oh-&gt;roomToGrow\[0] = SWAP32(oh-&gt;roomToGrow\[0]);  
oh-&gt;roomToGrow\[1] = SWAP32(oh-&gt;roomToGrow\[1]);  
oh-&gt;roomToGrow\[2] = SWAP32(oh-&gt;roomToGrow\[2]);  
oh-&gt;roomToGrow\[3] = SWAP32(oh-&gt;roomToGrow\[3]);  
oh-&gt;roomToGrow\[4] = SWAP32(oh-&gt;roomToGrow\[4]);  
oh-&gt;roomToGrow\[5] = SWAP32(oh-&gt;roomToGrow\[5]);  
oh-&gt;roomToGrow\[6] = SWAP32(oh-&gt;roomToGrow\[6]);  
oh-&gt;roomToGrow\[7] = SWAP32(oh-&gt;roomToGrow\[7]);  
oh-&gt;roomToGrow\[8] = SWAP32(oh-&gt;roomToGrow\[8]);  
oh-&gt;roomToGrow\[9] = SWAP32(oh-&gt;roomToGrow\[9]);  
oh-&gt;roomToGrow\[10] = SWAP32(oh-&gt;roomToGrow\[10]);  
oh-&gt;roomToGrow\[11] = SWAP32(oh-&gt;roomToGrow\[11]);  
#endif  
}  
#endif

static int write\_object\_header(int objId, yaffs\_ObjectType t, struct stat \*s, int parent, const char \*name, int equivalentObj, const char * alias)  
{  
\_\_u8 bytes\[chunkSize];

yaffs\_ObjectHeader \*oh = (yaffs\_ObjectHeader \*)bytes;

memset(bytes,0xff,sizeof(bytes));

oh-&gt;type = t;

oh-&gt;parentObjectId = parent;

strncpy(oh-&gt;name,name,YAFFS\_MAX\_NAME\_LENGTH);

if(t != YAFFS\_OBJECT\_TYPE\_HARDLINK)  
{  
oh-&gt;yst\_mode = s-&gt;st\_mode;  
oh-&gt;yst\_uid = s-&gt;st\_uid;  
// NCB 12/9/02oh-&gt;yst\_gid = s-&gt;yst\_uid;  
oh-&gt;yst\_gid = s-&gt;st\_gid;  
oh-&gt;yst\_atime = s-&gt;st\_atime;  
oh-&gt;yst\_mtime = s-&gt;st\_mtime;  
oh-&gt;yst\_ctime = s-&gt;st\_ctime;  
oh-&gt;yst\_rdev = s-&gt;st\_rdev;  
}

if(t == YAFFS\_OBJECT\_TYPE\_FILE)  
{  
oh-&gt;fileSize = s-&gt;st\_size;  
}

if(t == YAFFS\_OBJECT\_TYPE\_HARDLINK)  
{  
oh-&gt;equivalentObjectId = equivalentObj;  
}

if(t == YAFFS\_OBJECT\_TYPE\_SYMLINK)  
{  
strncpy(oh-&gt;alias,alias,YAFFS\_MAX\_ALIAS\_LENGTH);  
}

/* KSI: FUBAR. Left for a leter time. \*/  
#if 0  
if (convert\_endian)  
{  
object\_header\_little\_to\_big\_endian(oh);  
}  
#endif

return write\_chunk(bytes,objId,0,0xffff);

}

static int process\_directory(int parent, const char \*path)  
{

DIR \*dir;  
struct dirent \*entry;

nDirectories++;

dir = opendir(path);

if(dir)  
{  
while((entry = readdir(dir)) != NULL)  
{

/* Ignore . and .. \*/  
if(strcmp(entry-&gt;d\_name,".") &&  
strcmp(entry-&gt;d\_name,".."))  
{  
char full\_name\[500];  
struct stat stats;  
int equivalentObj;  
int newObj;

sprintf(full\_name,"%s/%s",path,entry-&gt;d\_name);

lstat(full\_name,&stats);

if(S\_ISLNK(stats.st\_mode) ||  
S\_ISREG(stats.st\_mode) ||  
S\_ISDIR(stats.st\_mode) ||  
S\_ISFIFO(stats.st\_mode) ||  
S\_ISBLK(stats.st\_mode) ||  
S\_ISCHR(stats.st\_mode) ||  
S\_ISSOCK(stats.st\_mode))  
{

newObj = obj\_id++;  
nObjects++;

printf("Object %d, %s is a ",newObj,full\_name);

/* We're going to create an object for it \*/  
if((equivalentObj = find\_obj\_in\_list(stats.st\_dev, stats.st\_ino)) &gt; 0)  
{  
/* we need to make a hard link \*/  
printf("hard link to object %d/n",equivalentObj);  
error = write\_object\_header(newObj, YAFFS\_OBJECT\_TYPE\_HARDLINK, &stats, parent, entry-&gt;d\_name, equivalentObj, NULL);  
}  
else  
{

add\_obj\_to\_list(stats.st\_dev,stats.st\_ino,newObj);

if(S\_ISLNK(stats.st\_mode))  
{

char symname\[500];

memset(symname,0, sizeof(symname));

readlink(full\_name,symname,sizeof(symname) -1);

printf("symlink to /"%s/"/n",symname);  
error = write\_object\_header(newObj, YAFFS\_OBJECT\_TYPE\_SYMLINK, &stats, parent, entry-&gt;d\_name, -1, symname);

}  
else if(S\_ISREG(stats.st\_mode))  
{  
printf("file, ");  
error = write\_object\_header(newObj, YAFFS\_OBJECT\_TYPE\_FILE, &stats, parent, entry-&gt;d\_name, -1, NULL);

if(error &gt;= 0)  
{  
int h;  
\_\_u8 bytes\[chunkSize];  
int nBytes;  
int chunk = 0;

h = open(full\_name,O\_RDONLY);  
if(h &gt;= 0)  
{  
memset(bytes,0xff,sizeof(bytes));  
while((nBytes = read(h,bytes,sizeof(bytes))) &gt; 0)  
{  
chunk++;  
write\_chunk(bytes,newObj,chunk,nBytes);  
memset(bytes,0xff,sizeof(bytes));  
}  
if(nBytes &lt; 0)  
error = nBytes;

printf("%d data chunks written/n",chunk);  
close(h);  
}  
else  
{  
perror("Error opening file");  
}

}

}  
else if(S\_ISSOCK(stats.st\_mode))  
{  
printf("socket/n");  
error = write\_object\_header(newObj, YAFFS\_OBJECT\_TYPE\_SPECIAL, &stats, parent, entry-&gt;d\_name, -1, NULL);  
}  
else if(S\_ISFIFO(stats.st\_mode))  
{  
printf("fifo/n");  
error = write\_object\_header(newObj, YAFFS\_OBJECT\_TYPE\_SPECIAL, &stats, parent, entry-&gt;d\_name, -1, NULL);  
}  
else if(S\_ISCHR(stats.st\_mode))  
{  
printf("character device/n");  
error = write\_object\_header(newObj, YAFFS\_OBJECT\_TYPE\_SPECIAL, &stats, parent, entry-&gt;d\_name, -1, NULL);  
}  
else if(S\_ISBLK(stats.st\_mode))  
{  
printf("block device/n");  
error = write\_object\_header(newObj, YAFFS\_OBJECT\_TYPE\_SPECIAL, &stats, parent, entry-&gt;d\_name, -1, NULL);  
}  
else if(S\_ISDIR(stats.st\_mode))  
{  
printf("directory/n");  
error = write\_object\_header(newObj, YAFFS\_OBJECT\_TYPE\_DIRECTORY, &stats, parent, entry-&gt;d\_name, -1, NULL);  
process\_directory(newObj,full\_name);  
}  
}  
}  
else  
{  
printf(" we don't handle this type/n");  
}  
}  
}  
closedir(dir);  
}

return 0;

}

void usage(void)  
{  
printf("usage: mkyaffs2image layout# dir image\_file \[convert]/n");  
printf("layout# NAND OOB layout # (0 - raw, 1 - nand\_oob\_64)/n");  
printf("dir the directory tree to be converted/n");  
printf("image\_file the output file to hold the image/n");  
printf("'convert' make a big-endian img on a little-endian machine. BROKEN !/n");  
exit(1);  
}

int main(int argc, char \*argv\[])  
{  
struct stat stats;  
inti;

printf("mkyaffs2image: image building tool for YAFFS2 built "\_\_DATE\_\_"/n");

if ((argc &lt; 4) || (sscanf(argv\[1], "%u", &layout\_no) != 1))  
{  
usage();  
}

i = 0;

while (oob\_layout\[i].useecc != -1)  
i++;

if (layout\_no &gt;= i)  
usage();

if ((argc == 5) && (!strncmp(argv\[4], "convert", strlen("convert"))))  
{  
fprintf (stderr, "WARNING: ENDIAN CONVERSION IS BROKEN/n");  
/* KSI: Broken as of now. TBD. Fail. \*/  
usage();  
convert\_endian = 1;  
}

if(stat(argv\[2],&stats) &lt; 0)  
{  
printf("Could not stat %s/n",argv\[2]);  
exit(1);  
}

if(!S\_ISDIR(stats.st\_mode))  
{  
printf(" %s is not a directory/n",argv\[2]);  
exit(1);  
}

outFile = open(argv\[3],O\_CREAT | O\_TRUNC | O\_WRONLY, S\_IREAD | S\_IWRITE);

if(outFile &lt; 0)  
{  
printf("Could not open output file %s/n",argv\[3]);  
exit(1);  
}

printf("Processing directory %s into image file %s/n",argv\[2],argv\[3]);  
error = write\_object\_header(1, YAFFS\_OBJECT\_TYPE\_DIRECTORY, &stats, 1,"", -1, NULL);

if(error)  
error = process\_directory(YAFFS\_OBJECTID\_ROOT,argv\[2]);

close(outFile);

if(error &lt; 0)  
{  
perror("operation incomplete");  
exit(1);  
}  
else  
{  
printf("Operation complete./n"  
"%d objects in %d directories/n"  
"%d NAND pages/n",nObjects, nDirectories, nPages);  
}

close(outFile);

exit(0);  
}

**PART2 About 16bytes OOB**

另外，下面用关键性的数据结构简单展示一下**16**bytes OOB区内的组织格式：

typedef struct {  
unsigned chunkId:20;  
unsigned serialNumber:2;  
unsigned byteCountLSB:10;  
unsigned objectId:18;  
unsigned ecc:12;  
unsigned byteCountMSB:2;  
} yaffs\_Tags;

//LIUHAO: yaffs\_TagsUnion共用体，方便实现对yaffs\_Tags中数据的处理。  
typedef union {  
yaffs\_Tags asTags;  
\_\_u8 asBytes\[8];  
} yaffs\_TagsUnion;

//LIUHAO: 512bytes data + 16bytes spare时spare区中的数据格式  
//其中ecc1\[3]为512bytes前半区的ecc校验码  
//ecc2\[3]为512bytes后半区的ecc校验码

/* Spare structure for YAFFS1 \*/  
typedef struct {  
\_\_u8 tagByte0;  
\_\_u8 tagByte1;  
\_\_u8 tagByte2;  
\_\_u8 tagByte3;  
\_\_u8 pageStatus;/* set to 0 to delete the chunk \*/  
\_\_u8 blockStatus;  
\_\_u8 tagByte4;  
\_\_u8 tagByte5;  
\_\_u8 ecc1\[3];  
\_\_u8 tagByte6;  
\_\_u8 tagByte7;  
\_\_u8 ecc2\[3];  
} yaffs\_Spare;

总结：

对于未知的领域和问题，唯独我们坚持，才会最终得到正确的答案。一句话说得好：人类最伟大的品质在于坚持，所有的梦想和目标都会因些而实现。

终于可以成功制作页大小为2K + 64 Bytes 的nand flash的image 文件了。令人兴奋啊 ：)

这两天一直苦于自己制作的yaffs2 image文件系统无法被linux-2.6.29.1内核识别，而使用FriendlyARM的mkyaffs2imag-128M工具就没有这个问题。一开始便判断是mkyaffs2image工具有问题，没有生成正确格式的yaffs2 image文件，从而导致内核无法识别，并抛出以下错误信息：

\----------------------------------------------------------------------------------------------------------------

……

block 155 is bad  
yaffs\_read\_super: isCheckpointed 0  
VFS: Mounted root (yaffs filesystem) readonly on device 31:2.  
Freeing init memory: 164K  
Warning: unable to open an initial console.  
Failed to execute /linuxrc. Attempting defaults...  
Kernel panic - not syncing: No init found. Try passing init= option to kernel.

\----------------------------------------------------------------------------------------------------------------

网上查了很多相关的资料，对2K data + 64bytes spare 的nand flash上yaffs2文件的存储格式并没有较为清晰的说明。不知道数据的存储模式，就无法正确生成image文件。苦恼了好几天，甚至有些想放弃了。。。

然而今天得到了一个意外的收获，[http://www.bluewatersys.com/quickstart/9260sambootassistant.php](http://www.bluewatersys.com/quickstart/9260sambootassistant.php)上大概阐述了2K yaffs2 nand flash中数据的组织形式(虽然后来发现该文章中关于结构yaffs2\_PackedTags的大小表述有错，应该是28字节，而非25字节)，经过修改后，并且纠正相关bug，成功生成正确的yaffs2 image 文件，download进s3c2440开发板后，顺利启动系统。

有一点很重要，就是mkyaffs2image生成的image 文件的OOB区数据格式要与linux kernel对nand flash进行读取时的数据格式保持一致，所以mkyaffs2image.c中来自linux kernel的数据结构，即包含了mtd-abi.h头文件。

PS：下面本人会附上mkyaffs2image.c的全部内容，供朋友们参考，希望给meet with同样error的兄弟们一些帮助：)

**PART2 About 64bytes OOB**

下图为64bytes OOB区中的数据组织结构：

图1 64bytes OOB area

PS：

1\) OOB最前端的2bytes为0xFF 0xFF，用于标识为非坏块。

2\) 每256bytes的数据需要3bytes有ECC校验，因此2K的数据需要24bytes的ECC。

以下为经过修改过后的mkyaffs2image.c的源码：

/*  
\* YAFFS: Yet Another Flash File System. A NAND-flash specific file system.  
\*  
\* Copyright (C) 2002-2007 Aleph One Ltd.  
\* for Toby Churchill Ltd and Brightstar Engineering  
\*  
\* Created by Charles Manning &lt;[charles@aleph1.co.uk](mailto:charles@aleph1.co.uk)&gt;  
\* Nick Bane modifications flagged NCB  
\* Endian handling patches by James Ng.  
\* mkyaffs2image hacks by NCB  
\*  
\* This program is free software; you can redistribute it and/or modify  
\* it under the terms of the GNU General Public License version 2 as  
\* published by the Free Software Foundation.  
\*/

/*  
\* makeyaffs2image.c  
\*  
\* Makes a YAFFS2 file system image that can be used to load up a file system.  
\* Uses default Linux MTD layout - change if you need something different.  
\*/

#include &lt;stdlib.h&gt;  
#include &lt;stdio.h&gt;  
#include &lt;fcntl.h&gt;  
#include &lt;sys/types.h&gt;  
#include &lt;sys/stat.h&gt;  
#include &lt;dirent.h&gt;  
#include &lt;string.h&gt;  
#include &lt;unistd.h&gt;  
#define \_\_user  
#include &lt;mtd/mtd-user.h&gt;  
#include "yaffs\_ecc.h"  
#include "yaffs\_guts.h"

#include "yaffs\_packedtags2.h"

#include "mtd-abi.h"//ADDED BY LIUHAO 2009-11-11

unsigned yaffs\_traceMask=0;

#define MAX\_OBJECTS 10000

#define chunkSize 2048  
#define spareSize 64

//MODIFIED BY LIUHAO 2009-11-11 START  
//#define PT2\_BYTES 25//Original  
#define PT2\_BYTES 28  
//MODIFIED BY LIUHAO 2009-11-11 END

const char * mkyaffsimage\_c\_version = "$Id: mkyaffs2image.c,v 1.4 2007-02-14 01:09:06 wookey Exp $";

static intlayout\_no;

static struct nand\_oobinfo oob\_layout\[] = {  
/* KSI:  
\* Dummy "raw" layout - no ECC, all the bytes are free. Does NOT  
\* really work, only used for compatibility with CVS YAFFS2 that  
\* never ever worked with any stock MTD.  
\*/  
{  
.useecc = MTD\_NANDECC\_AUTOPLACE,  
.eccbytes = 0,  
.eccpos = {},  
.oobfree = { {0, 64} }  
},  
/* KSI:  
\* Regular MTD AUTOPLACED ECC for large page NAND devices, the  
\* only one existing in stock MTD so far. It corresponds to layout# 1  
\* in command line arguments. Any other layouts could be added to  
\* the list when they made their way in kernel's MTD. The structure  
\* is simply copied from kernel's drivers/mtd/nand/nand\_base.c as-is.  
\*/  
{  
.useecc = MTD\_NANDECC\_AUTOPLACE,  
.eccbytes = 24,  
.eccpos = {  
40, 41, 42, 43, 44, 45, 46, 47,  
48, 49, 50, 51, 52, 53, 54, 55,  
56, 57, 58, 59, 60, 61, 62, 63},  
.oobfree = { {2, 38} }  
},  
/* End-of-list marker \*/  
{  
.useecc = -1,  
}  
};

typedef struct {  
dev\_t dev;  
ino\_t ino;  
int obj;  
} objItem;

static objItem obj\_list\[MAX\_OBJECTS];  
static int n\_obj = 0;  
static int obj\_id = YAFFS\_NOBJECT\_BUCKETS + 1;

static int nObjects = 0, nDirectories = 0, nPages = 0;

static int outFile;

static int error;

static int convert\_endian = 0;

static int obj\_compare(const void \*a, const void * b)  
{  
objItem \*oa, \*ob;

oa = (objItem \*)a;  
ob = (objItem \*)b;

if(oa-&gt;dev &lt; ob-&gt;dev) return -1;  
if(oa-&gt;dev &gt; ob-&gt;dev) return 1;  
if(oa-&gt;ino &lt; ob-&gt;ino) return -1;  
if(oa-&gt;ino &gt; ob-&gt;ino) return 1;

return 0;  
}

static void add\_obj\_to\_list(dev\_t dev, ino\_t ino, int obj)  
{  
if(n\_obj &lt; MAX\_OBJECTS)  
{  
obj\_list\[n\_obj].dev = dev;  
obj\_list\[n\_obj].ino = ino;  
obj\_list\[n\_obj].obj = obj;  
n\_obj++;  
qsort(obj\_list,n\_obj,sizeof(objItem),obj\_compare);

}  
else  
{  
// oops! not enough space in the object array  
fprintf(stderr,"Not enough space in object array/n");  
exit(2);  
}  
}

static int find\_obj\_in\_list(dev\_t dev, ino\_t ino)  
{  
objItem \*i = NULL;  
objItem test;

test.dev = dev;  
test.ino = ino;

if(n\_obj &gt; 0)  
{  
i = bsearch(&test,obj\_list,n\_obj,sizeof(objItem),obj\_compare);  
}

if(i)  
{  
return i-&gt;obj;  
}  
return -1;  
}

static void yaffs\_PutDataECC(const \_\_u8 * data, unsigned char \*oob\_buf)  
{  
unsigned char ecc\_code\[3];  
int eccsteps = chunkSize / 256;  
int eccidx = 0, datidx = 0, i;  
struct nand\_oobinfo \*oobsel;  
\_\_u32 \*oob\_config;

oobsel = &oob\_layout\[layout\_no];  
oob\_config = oobsel-&gt;eccpos;//LIUHAO: 24bytes的ECC在spare area中的位置

for (; eccsteps; eccsteps--) {  
yaffs\_ECCCalculate (&data\[datidx], ecc\_code);  
for (i = 0; i &lt; 3; i++, eccidx++)  
oob\_buf\[oob\_config\[eccidx]] = ecc\_code\[i];  
datidx += 256;  
}  
}

/* KSI:  
\* No big endian for now. This is left for a later time. The existing code  
\* is FUBAR.  
\*/  
#if 0  
/* This little function converts a little endian tag to a big endian tag.  
\* NOTE: The tag is not usable after this other than calculating the CRC  
\* with.  
\*/  
static void little\_to\_big\_endian(yaffs\_Tags \*tagsPtr)  
{  
#if 0 // FIXME NCB  
yaffs\_TagsUnion * tags = (yaffs\_TagsUnion* )tagsPtr; // Work in bytes.  
yaffs\_TagsUnion temp;

memset(&temp, 0, sizeof(temp));  
// Ick, I hate magic numbers.  
temp.asBytes\[0] = ((tags-&gt;asBytes\[2] & 0x0F) &lt;&lt; 4) | ((tags-&gt;asBytes\[1] & 0xF0) &gt;&gt; 4);  
temp.asBytes\[1] = ((tags-&gt;asBytes\[1] & 0x0F) &lt;&lt; 4) | ((tags-&gt;asBytes\[0] & 0xF0) &gt;&gt; 4);  
temp.asBytes\[2] = ((tags-&gt;asBytes\[0] & 0x0F) &lt;&lt; 4) | ((tags-&gt;asBytes\[2] & 0x30) &gt;&gt; 2) | ((tags-&gt;asBytes\[3] & 0xC0) &gt;&gt; 6);  
temp.asBytes\[3] = ((tags-&gt;asBytes\[3] & 0x3F) &lt;&lt; 2) | ((tags-&gt;asBytes\[2] & 0xC0) &gt;&gt; 6);  
temp.asBytes\[4] = ((tags-&gt;asBytes\[6] & 0x03) &lt;&lt; 6) | ((tags-&gt;asBytes\[5] & 0xFC) &gt;&gt; 2);  
temp.asBytes\[5] = ((tags-&gt;asBytes\[5] & 0x03) &lt;&lt; 6) | ((tags-&gt;asBytes\[4] & 0xFC) &gt;&gt; 2);  
temp.asBytes\[6] = ((tags-&gt;asBytes\[4] & 0x03) &lt;&lt; 6) | (tags-&gt;asBytes\[7] & 0x3F);  
temp.asBytes\[7] = (tags-&gt;asBytes\[6] & 0xFC) | ((tags-&gt;asBytes\[7] & 0xC0) &gt;&gt; 6);

// Now copy it back.  
tags-&gt;asBytes\[0] = temp.asBytes\[0];  
tags-&gt;asBytes\[1] = temp.asBytes\[1];  
tags-&gt;asBytes\[2] = temp.asBytes\[2];  
tags-&gt;asBytes\[3] = temp.asBytes\[3];  
tags-&gt;asBytes\[4] = temp.asBytes\[4];  
tags-&gt;asBytes\[5] = temp.asBytes\[5];  
tags-&gt;asBytes\[6] = temp.asBytes\[6];  
tags-&gt;asBytes\[7] = temp.asBytes\[7];  
#endif  
}  
#endif

static void nandmtd2\_pt2buf(unsigned char \*buf, yaffs\_PackedTags2 \*pt)  
{  
inti, j = 0, k, n;  
//MODIFIED BY LIUHAO 2009-11-11  
//unsigned charpt2\_byte\_buf\[PT2\_BYTES];//Original  
unsigned charpt2\_byte\_buf\[PT2\_BYTES] = {0};

\*((unsigned int \*) &pt2\_byte\_buf\[0]) = pt-&gt;t.sequenceNumber;  
\*((unsigned int \*) &pt2\_byte\_buf\[4]) = pt-&gt;t.objectId;  
\*((unsigned int \*) &pt2\_byte\_buf\[8]) = pt-&gt;t.chunkId;  
\*((unsigned int \*) &pt2\_byte\_buf\[12]) = pt-&gt;t.byteCount;  
pt2\_byte\_buf\[16] = pt-&gt;ecc.colParity;  
//MODIFIED BY LIUHAO 2009-11-11 START  
#if 0  
//Original  
pt2\_byte\_buf\[17] = pt-&gt;ecc.lineParity & 0xff;  
pt2\_byte\_buf\[18] = (pt-&gt;ecc.lineParity &gt;&gt; 8) & 0xff;  
pt2\_byte\_buf\[19] = (pt-&gt;ecc.lineParity &gt;&gt; 16) & 0xff;  
pt2\_byte\_buf\[20] = (pt-&gt;ecc.lineParity &gt;&gt; 24) & 0xff;  
pt2\_byte\_buf\[21] = pt-&gt;ecc.lineParityPrime & 0xff;  
pt2\_byte\_buf\[22] = (pt-&gt;ecc.lineParityPrime &gt;&gt; 8) & 0xff;  
pt2\_byte\_buf\[23] = (pt-&gt;ecc.lineParityPrime &gt;&gt; 16) & 0xff;  
pt2\_byte\_buf\[24] = (pt-&gt;ecc.lineParityPrime &gt;&gt; 24) & 0xff;  
#else  
pt2\_byte\_buf\[20] = pt-&gt;ecc.lineParity & 0xff;  
pt2\_byte\_buf\[21] = (pt-&gt;ecc.lineParity &gt;&gt; 8) & 0xff;  
pt2\_byte\_buf\[22] = (pt-&gt;ecc.lineParity &gt;&gt; 16) & 0xff;  
pt2\_byte\_buf\[23] = (pt-&gt;ecc.lineParity &gt;&gt; 24) & 0xff;  
pt2\_byte\_buf\[24] = pt-&gt;ecc.lineParityPrime & 0xff;  
pt2\_byte\_buf\[25] = (pt-&gt;ecc.lineParityPrime &gt;&gt; 8) & 0xff;  
pt2\_byte\_buf\[26] = (pt-&gt;ecc.lineParityPrime &gt;&gt; 16) & 0xff;  
pt2\_byte\_buf\[27] = (pt-&gt;ecc.lineParityPrime &gt;&gt; 24) & 0xff;  
#endif

k = oob\_layout\[layout\_no].oobfree\[j]\[0];  
n = oob\_layout\[layout\_no].oobfree\[j]\[1];

if (n == 0) {  
fprintf(stderr, "No OOB space for tags");  
exit(-1);  
}

for (i = 0; i &lt; PT2\_BYTES; i++) {  
if (n == 0) {  
j++;  
k = oob\_layout\[layout\_no].oobfree\[j]\[0];  
n = oob\_layout\[layout\_no].oobfree\[j]\[1];  
if (n == 0) {  
fprintf(stderr, "No OOB space for tags");  
exit(-1);  
}  
}  
buf\[k++] = pt2\_byte\_buf\[i];  
n--;  
}  
}

static int write\_chunk(\_\_u8 \*data, \_\_u32 objId, \_\_u32 chunkId, \_\_u32 nBytes)  
{  
yaffs\_ExtendedTags t;  
yaffs\_PackedTags2 pt;

yaffs\_PackedTags2TagsPart ptt;//ADDED BY LIUHAO 2009-11-11

//ADDED BY LIUHAO 2009-11-11  
//unsigned charspare\_buf\[spareSize];//Original  
unsigned charspare\_buf\[spareSize] = {0};

error = write(outFile,data,chunkSize);  
if(error &lt; 0) return error;

yaffs\_InitialiseTags(&t);

t.chunkId = chunkId;  
//t.serialNumber = 0;  
t.serialNumber = 1;// \*\*CHECK\**  
t.byteCount = nBytes;  
t.objectId = objId;

t.sequenceNumber = YAFFS\_LOWEST\_SEQUENCE\_NUMBER;

// added NCB \*\*CHECK\**  
t.chunkUsed = 1;

//ADDED BY LIUHAO 2009-11-11  
//t.extraHeaderInfoAvailable = 1;

/* KSI: Broken anyway -- e.g. &t is pointer to a wrong type... \*/  
#if 0  
if (convert\_endian)  
{  
little\_to\_big\_endian(&t);  
}  
#endif

//MODIFIED BY LIUHAO 2009-11-11 START  
#if 0  
yaffs\_PackTags2(&pt,&t);//Original  
#else  
memset(&pt, 0, sizeof(yaffs\_PackedTags2));  
yaffs\_PackTags2TagsPart(&pt.t,&t);  
yaffs\_ECCCalculateOther(  
(unsigned char \*)&pt.t,  
sizeof(yaffs\_PackedTags2TagsPart),  
&pt.ecc);  
#endif  
//MODIFIED BY LIUHAO 2009-11-11 END

memset(spare\_buf, 0xff, sizeof(spare\_buf));

if (layout\_no == 0) {  
memcpy(spare\_buf, &pt, sizeof(yaffs\_PackedTags2));  
} else {  
nandmtd2\_pt2buf(spare\_buf, &pt);  
}

yaffs\_PutDataECC(data, &spare\_buf\[0]);

nPages++;

return write(outFile,spare\_buf,spareSize);  
}

#define SWAP32(x) ((((x) & 0x000000FF) &lt;&lt; 24) | /  
(((x) & 0x0000FF00) &lt;&lt; 8 ) | /  
(((x) & 0x00FF0000) &gt;&gt; 8 ) | /  
(((x) & 0xFF000000) &gt;&gt; 24))

#define SWAP16(x) ((((x) & 0x00FF) &lt;&lt; 8) | /  
(((x) & 0xFF00) &gt;&gt; 8))

/* KSI: Removed for now. TBD later when the proper util (from scratch) is written \*/  
#if 0  
// This one is easier, since the types are more standard. No funky shifts here.  
static void object\_header\_little\_to\_big\_endian(yaffs\_ObjectHeader* oh)  
{  
oh-&gt;type = SWAP32(oh-&gt;type); // GCC makes enums 32 bits.  
oh-&gt;parentObjectId = SWAP32(oh-&gt;parentObjectId); // int  
oh-&gt;sum\_\_NoLongerUsed = SWAP16(oh-&gt;sum\_\_NoLongerUsed); // \_\_u16 - Not used, but done for completeness.  
// name = skip. Char array. Not swapped.  
oh-&gt;yst\_mode = SWAP32(oh-&gt;yst\_mode);  
#ifdef CONFIG\_YAFFS\_WINCE // WinCE doesn't implement this, but we need to just in case.  
// In fact, WinCE would be \*THE* place where this would be an issue!  
oh-&gt;notForWinCE\[0] = SWAP32(oh-&gt;notForWinCE\[0]);  
oh-&gt;notForWinCE\[1] = SWAP32(oh-&gt;notForWinCE\[1]);  
oh-&gt;notForWinCE\[2] = SWAP32(oh-&gt;notForWinCE\[2]);  
oh-&gt;notForWinCE\[3] = SWAP32(oh-&gt;notForWinCE\[3]);  
oh-&gt;notForWinCE\[4] = SWAP32(oh-&gt;notForWinCE\[4]);  
#else  
// Regular POSIX.  
oh-&gt;yst\_uid = SWAP32(oh-&gt;yst\_uid);  
oh-&gt;yst\_gid = SWAP32(oh-&gt;yst\_gid);  
oh-&gt;yst\_atime = SWAP32(oh-&gt;yst\_atime);  
oh-&gt;yst\_mtime = SWAP32(oh-&gt;yst\_mtime);  
oh-&gt;yst\_ctime = SWAP32(oh-&gt;yst\_ctime);  
#endif

oh-&gt;fileSize = SWAP32(oh-&gt;fileSize); // Aiee. An int... signed, at that!  
oh-&gt;equivalentObjectId = SWAP32(oh-&gt;equivalentObjectId);  
// alias - char array.  
oh-&gt;yst\_rdev = SWAP32(oh-&gt;yst\_rdev);

#ifdef CONFIG\_YAFFS\_WINCE  
oh-&gt;win\_ctime\[0] = SWAP32(oh-&gt;win\_ctime\[0]);  
oh-&gt;win\_ctime\[1] = SWAP32(oh-&gt;win\_ctime\[1]);  
oh-&gt;win\_atime\[0] = SWAP32(oh-&gt;win\_atime\[0]);  
oh-&gt;win\_atime\[1] = SWAP32(oh-&gt;win\_atime\[1]);  
oh-&gt;win\_mtime\[0] = SWAP32(oh-&gt;win\_mtime\[0]);  
oh-&gt;win\_mtime\[1] = SWAP32(oh-&gt;win\_mtime\[1]);  
oh-&gt;roomToGrow\[0] = SWAP32(oh-&gt;roomToGrow\[0]);  
oh-&gt;roomToGrow\[1] = SWAP32(oh-&gt;roomToGrow\[1]);  
oh-&gt;roomToGrow\[2] = SWAP32(oh-&gt;roomToGrow\[2]);  
oh-&gt;roomToGrow\[3] = SWAP32(oh-&gt;roomToGrow\[3]);  
oh-&gt;roomToGrow\[4] = SWAP32(oh-&gt;roomToGrow\[4]);  
oh-&gt;roomToGrow\[5] = SWAP32(oh-&gt;roomToGrow\[5]);  
#else  
oh-&gt;roomToGrow\[0] = SWAP32(oh-&gt;roomToGrow\[0]);  
oh-&gt;roomToGrow\[1] = SWAP32(oh-&gt;roomToGrow\[1]);  
oh-&gt;roomToGrow\[2] = SWAP32(oh-&gt;roomToGrow\[2]);  
oh-&gt;roomToGrow\[3] = SWAP32(oh-&gt;roomToGrow\[3]);  
oh-&gt;roomToGrow\[4] = SWAP32(oh-&gt;roomToGrow\[4]);  
oh-&gt;roomToGrow\[5] = SWAP32(oh-&gt;roomToGrow\[5]);  
oh-&gt;roomToGrow\[6] = SWAP32(oh-&gt;roomToGrow\[6]);  
oh-&gt;roomToGrow\[7] = SWAP32(oh-&gt;roomToGrow\[7]);  
oh-&gt;roomToGrow\[8] = SWAP32(oh-&gt;roomToGrow\[8]);  
oh-&gt;roomToGrow\[9] = SWAP32(oh-&gt;roomToGrow\[9]);  
oh-&gt;roomToGrow\[10] = SWAP32(oh-&gt;roomToGrow\[10]);  
oh-&gt;roomToGrow\[11] = SWAP32(oh-&gt;roomToGrow\[11]);  
#endif  
}  
#endif

static int write\_object\_header(int objId, yaffs\_ObjectType t, struct stat \*s, int parent, const char \*name, int equivalentObj, const char * alias)  
{  
\_\_u8 bytes\[chunkSize];

yaffs\_ObjectHeader \*oh = (yaffs\_ObjectHeader \*)bytes;

memset(bytes,0xff,sizeof(bytes));

oh-&gt;type = t;

oh-&gt;parentObjectId = parent;

strncpy(oh-&gt;name,name,YAFFS\_MAX\_NAME\_LENGTH);

if(t != YAFFS\_OBJECT\_TYPE\_HARDLINK)  
{  
oh-&gt;yst\_mode = s-&gt;st\_mode;  
oh-&gt;yst\_uid = s-&gt;st\_uid;  
// NCB 12/9/02oh-&gt;yst\_gid = s-&gt;yst\_uid;  
oh-&gt;yst\_gid = s-&gt;st\_gid;  
oh-&gt;yst\_atime = s-&gt;st\_atime;  
oh-&gt;yst\_mtime = s-&gt;st\_mtime;  
oh-&gt;yst\_ctime = s-&gt;st\_ctime;  
oh-&gt;yst\_rdev = s-&gt;st\_rdev;  
}

if(t == YAFFS\_OBJECT\_TYPE\_FILE)  
{  
oh-&gt;fileSize = s-&gt;st\_size;  
}

if(t == YAFFS\_OBJECT\_TYPE\_HARDLINK)  
{  
oh-&gt;equivalentObjectId = equivalentObj;  
}

if(t == YAFFS\_OBJECT\_TYPE\_SYMLINK)  
{  
strncpy(oh-&gt;alias,alias,YAFFS\_MAX\_ALIAS\_LENGTH);  
}

/* KSI: FUBAR. Left for a leter time. \*/  
#if 0  
if (convert\_endian)  
{  
object\_header\_little\_to\_big\_endian(oh);  
}  
#endif

return write\_chunk(bytes,objId,0,0xffff);

}

static int process\_directory(int parent, const char \*path)  
{

DIR \*dir;  
struct dirent \*entry;

nDirectories++;

dir = opendir(path);

if(dir)  
{  
while((entry = readdir(dir)) != NULL)  
{

/* Ignore . and .. \*/  
if(strcmp(entry-&gt;d\_name,".") &&  
strcmp(entry-&gt;d\_name,".."))  
{  
char full\_name\[500];  
struct stat stats;  
int equivalentObj;  
int newObj;

sprintf(full\_name,"%s/%s",path,entry-&gt;d\_name);

lstat(full\_name,&stats);

if(S\_ISLNK(stats.st\_mode) ||  
S\_ISREG(stats.st\_mode) ||  
S\_ISDIR(stats.st\_mode) ||  
S\_ISFIFO(stats.st\_mode) ||  
S\_ISBLK(stats.st\_mode) ||  
S\_ISCHR(stats.st\_mode) ||  
S\_ISSOCK(stats.st\_mode))  
{

newObj = obj\_id++;  
nObjects++;

printf("Object %d, %s is a ",newObj,full\_name);

/* We're going to create an object for it \*/  
if((equivalentObj = find\_obj\_in\_list(stats.st\_dev, stats.st\_ino)) &gt; 0)  
{  
/* we need to make a hard link \*/  
printf("hard link to object %d/n",equivalentObj);  
error = write\_object\_header(newObj, YAFFS\_OBJECT\_TYPE\_HARDLINK, &stats, parent, entry-&gt;d\_name, equivalentObj, NULL);  
}  
else  
{

add\_obj\_to\_list(stats.st\_dev,stats.st\_ino,newObj);

if(S\_ISLNK(stats.st\_mode))  
{

char symname\[500];

memset(symname,0, sizeof(symname));

readlink(full\_name,symname,sizeof(symname) -1);

printf("symlink to /"%s/"/n",symname);  
error = write\_object\_header(newObj, YAFFS\_OBJECT\_TYPE\_SYMLINK, &stats, parent, entry-&gt;d\_name, -1, symname);

}  
else if(S\_ISREG(stats.st\_mode))  
{  
printf("file, ");  
error = write\_object\_header(newObj, YAFFS\_OBJECT\_TYPE\_FILE, &stats, parent, entry-&gt;d\_name, -1, NULL);

if(error &gt;= 0)  
{  
int h;  
\_\_u8 bytes\[chunkSize];  
int nBytes;  
int chunk = 0;

h = open(full\_name,O\_RDONLY);  
if(h &gt;= 0)  
{  
memset(bytes,0xff,sizeof(bytes));  
while((nBytes = read(h,bytes,sizeof(bytes))) &gt; 0)  
{  
chunk++;  
write\_chunk(bytes,newObj,chunk,nBytes);  
memset(bytes,0xff,sizeof(bytes));  
}  
if(nBytes &lt; 0)  
error = nBytes;

printf("%d data chunks written/n",chunk);  
close(h);  
}  
else  
{  
perror("Error opening file");  
}

}

}  
else if(S\_ISSOCK(stats.st\_mode))  
{  
printf("socket/n");  
error = write\_object\_header(newObj, YAFFS\_OBJECT\_TYPE\_SPECIAL, &stats, parent, entry-&gt;d\_name, -1, NULL);  
}  
else if(S\_ISFIFO(stats.st\_mode))  
{  
printf("fifo/n");  
error = write\_object\_header(newObj, YAFFS\_OBJECT\_TYPE\_SPECIAL, &stats, parent, entry-&gt;d\_name, -1, NULL);  
}  
else if(S\_ISCHR(stats.st\_mode))  
{  
printf("character device/n");  
error = write\_object\_header(newObj, YAFFS\_OBJECT\_TYPE\_SPECIAL, &stats, parent, entry-&gt;d\_name, -1, NULL);  
}  
else if(S\_ISBLK(stats.st\_mode))  
{  
printf("block device/n");  
error = write\_object\_header(newObj, YAFFS\_OBJECT\_TYPE\_SPECIAL, &stats, parent, entry-&gt;d\_name, -1, NULL);  
}  
else if(S\_ISDIR(stats.st\_mode))  
{  
printf("directory/n");  
error = write\_object\_header(newObj, YAFFS\_OBJECT\_TYPE\_DIRECTORY, &stats, parent, entry-&gt;d\_name, -1, NULL);  
process\_directory(newObj,full\_name);  
}  
}  
}  
else  
{  
printf(" we don't handle this type/n");  
}  
}  
}  
closedir(dir);  
}

return 0;

}

void usage(void)  
{  
printf("usage: mkyaffs2image layout# dir image\_file \[convert]/n");  
printf("layout# NAND OOB layout # (0 - raw, 1 - nand\_oob\_64)/n");  
printf("dir the directory tree to be converted/n");  
printf("image\_file the output file to hold the image/n");  
printf("'convert' make a big-endian img on a little-endian machine. BROKEN !/n");  
exit(1);  
}

int main(int argc, char \*argv\[])  
{  
struct stat stats;  
inti;

printf("mkyaffs2image: image building tool for YAFFS2 built "\_\_DATE\_\_"/n");

if ((argc &lt; 4) || (sscanf(argv\[1], "%u", &layout\_no) != 1))  
{  
usage();  
}

i = 0;

while (oob\_layout\[i].useecc != -1)  
i++;

if (layout\_no &gt;= i)  
usage();

if ((argc == 5) && (!strncmp(argv\[4], "convert", strlen("convert"))))  
{  
fprintf (stderr, "WARNING: ENDIAN CONVERSION IS BROKEN/n");  
/* KSI: Broken as of now. TBD. Fail. \*/  
usage();  
convert\_endian = 1;  
}

if(stat(argv\[2],&stats) &lt; 0)  
{  
printf("Could not stat %s/n",argv\[2]);  
exit(1);  
}

if(!S\_ISDIR(stats.st\_mode))  
{  
printf(" %s is not a directory/n",argv\[2]);  
exit(1);  
}

outFile = open(argv\[3],O\_CREAT | O\_TRUNC | O\_WRONLY, S\_IREAD | S\_IWRITE);

if(outFile &lt; 0)  
{  
printf("Could not open output file %s/n",argv\[3]);  
exit(1);  
}

printf("Processing directory %s into image file %s/n",argv\[2],argv\[3]);  
error = write\_object\_header(1, YAFFS\_OBJECT\_TYPE\_DIRECTORY, &stats, 1,"", -1, NULL);

if(error)  
error = process\_directory(YAFFS\_OBJECTID\_ROOT,argv\[2]);

close(outFile);

if(error &lt; 0)  
{  
perror("operation incomplete");  
exit(1);  
}  
else  
{  
printf("Operation complete./n"  
"%d objects in %d directories/n"  
"%d NAND pages/n",nObjects, nDirectories, nPages);  
}

close(outFile);

exit(0);  
}

**PART2 About 16bytes OOB**

另外，下面用关键性的数据结构简单展示一下**16**bytes OOB区内的组织格式：

typedef struct {  
unsigned chunkId:20;  
unsigned serialNumber:2;  
unsigned byteCountLSB:10;  
unsigned objectId:18;  
unsigned ecc:12;  
unsigned byteCountMSB:2;  
} yaffs\_Tags;

//LIUHAO: yaffs\_TagsUnion共用体，方便实现对yaffs\_Tags中数据的处理。  
typedef union {  
yaffs\_Tags asTags;  
\_\_u8 asBytes\[8];  
} yaffs\_TagsUnion;

//LIUHAO: 512bytes data + 16bytes spare时spare区中的数据格式  
//其中ecc1\[3]为512bytes前半区的ecc校验码  
//ecc2\[3]为512bytes后半区的ecc校验码

/* Spare structure for YAFFS1 \*/  
typedef struct {  
\_\_u8 tagByte0;  
\_\_u8 tagByte1;  
\_\_u8 tagByte2;  
\_\_u8 tagByte3;  
\_\_u8 pageStatus;/* set to 0 to delete the chunk \*/  
\_\_u8 blockStatus;  
\_\_u8 tagByte4;  
\_\_u8 tagByte5;  
\_\_u8 ecc1\[3];  
\_\_u8 tagByte6;  
\_\_u8 tagByte7;  
\_\_u8 ecc2\[3];  
} yaffs\_Spare;

总结：

对于未知的领域和问题，唯独我们坚持，才会最终得到正确的答案。一句话说得好：人类最伟大的品质在于坚持，所有的梦想和目标都会因些而实现。