A note of porting yaffs2 file system to linux 2.6.18, part one.

A note of porting yaffs2 file system to linux 2.6.18, part one.

Platform: broadcom 97405, mips(support SMP), linux 2.6.18.
Nand flash: 128MB
Page size: (2048 + 64 spare) Bytes
Block size:(128K + 4K spare) Bytes

1. Get yaffs and yaffs2 source code
yaffs2 download URL
http://www.aleph1.co.uk/cgi-bin/viewcvs.cgi/, click "Download GNU tarball" linker, then you will get a tar file that include yaffs and yaffs2.
Note: broadcom's kernel support yaffs2 and yaffs file system, and make a lot of modifications, so I can skipt patch for
linux kernel.

2. A patchs for mkyaffs2image.c
--- mkyaffs2image.c_original    2010-04-09 11:21:05.000000000 +0800
+++ mkyaffs2image.c    2010-04-12 11:14:14.000000000 +0800
@@ -64,6 +64,8 @@
 static int error;
 
 static int convert_endian = 0;
+#define OOB_TAGS_POSITION_DEFAULT 0;
+static int oob_tags_pos = OOB_TAGS_POSITION_DEFAULT;
 
 static int obj_compare(const void *a, const void * b)
 {
@@ -158,6 +160,7 @@
 {
     yaffs_ExtendedTags t;
     yaffs_PackedTags2 pt;
+    char oobData[spareSize];
 
     error = write(outFile,data,chunkSize);
     if(error < 0) return error;
@@ -183,10 +186,18 @@
     nPages++;
 
     yaffs_PackTags2(&pt,&t,1);
-   
-//    return write(outFile,&pt,sizeof(yaffs_PackedTags2));
-    return write(outFile,&pt,spareSize);
-   
+
+    // * Need to write to the oob tags to the right position within the oob space
+    if((oob_tags_pos < 0) || (oob_tags_pos > (spareSize-sizeof(yaffs_PackedTags2))))
+    {
+       oob_tags_pos = OOB_TAGS_POSITION_DEFAULT;
+    }
+    memset(oobData,0xFF,spareSize);
+    memcpy(&(oobData[oob_tags_pos]),&pt,sizeof(yaffs_PackedTags2));
+    return write(outFile,oobData,spareSize);
+
+    //return write(outFile,&pt,sizeof(yaffs_PackedTags2));
+    //return write(outFile,&pt,spareSize);    
 }
 
 #define SWAP32(x)   ((((x) & 0x000000FF) << 24) | /
@@ -461,18 +472,34 @@
    
     if(argc < 3)
     {
-        printf("usage: mkyaffs2image dir image_file [convert]/n");
-        printf("           dir        the directory tree to be converted/n");
-        printf("           image_file the output file to hold the image/n");
-        printf("           'convert'  produce a big-endian image from a little-endian machine/n");
+        printf("usage: mkyaffs2image dir image_file [oob_ecc_size] [convert]/n");
+        printf(" dir the directory tree to be converted/n");
+        printf(" image_file the output file to hold the image/n");
+        printf(" [oob_ecc_size] the number of oob bytes reserved for ecc data (default is 0)/n");
+        printf(" ['convert'] produce a big-endian image from a little-endian machine/n");        
         exit(1);
     }
 
-    if ((argc == 4) && (!strncmp(argv[3], "convert", strlen("convert"))))
-    {
-        convert_endian = 1;
-    }
-   
+    if(argc == 4)
+    {
+         if(!strncmp(argv[3], "convert", strlen("convert")))
+         {
+             convert_endian = 1;
+         }
+         else
+         {
+             oob_tags_pos = atoi(argv[3]);
+         }
+    }
+    else if(argc == 5)
+    {
+         oob_tags_pos = atoi(argv[3]);
+         if(!strncmp(argv[4], "convert", strlen("convert")))
+         {
+             convert_endian = 1;
+         }
+    }
+
     if(stat(argv[1],&stats) < 0)
     {
         printf("Could not stat %s/n",argv[1]);

3. A patch for nadnwrite.c
--- nandwrite_original.c    2008-12-12 08:07:38.000000000 +0800
+++ nandwrite.c    2010-04-13 13:25:58.000000000 +0800
@@ -430,6 +430,7 @@
                  * such as the layout used by diskonchip.c
                  */
                 if (!oobinfochanged && (old_oobinfo.useecc == MTD_NANDECC_AUTOPLACE)) {
+                    #if 0
                     for (i = 0;old_oobinfo.oobfree[i][1]; i++) {
                         /* Set the reserved bytes to 0xff */
                         start = old_oobinfo.oobfree[i][0];
@@ -438,6 +439,27 @@
                             oobreadbuf + start,
                             len);
                     }
+                    #else
+                    {
+                        int totallen = 28;
+                        int offset = 0;
+                        memset(oobbuf, 0xFF, 64);
+                        for (i = 0; old_oobinfo.oobfree[i][1]; i++)
+                        {
+                            /* Set the reserved bytes to 0xff */
+                            start = old_oobinfo.oobfree[i][0];
+                            len = old_oobinfo.oobfree[i][1];                     
+
+                            if (len >= totallen)
+                               len = totallen;
+                            totallen -= len;
+                            memcpy(oobbuf + start, oobreadbuf + offset, len);
+                            if (totallen == 0)
+                                break;
+                            offset += len;
+                        }
+                    }                   
+                    #endif
                 } else {
                     /* Set at least the ecc byte positions to 0xff */
                     start = old_oobinfo.eccbytes;
                    
4. test yaffs2 file system, read and write.
build a yaffs2 image.
#./mkyaffs2image wenxy wenxy_yaffs2.image

see kernel support file system type.
#cat /proc/filesystems

eraseal a MTD partition.
#flash_eraseall /dev/mtd4

write image to a MTD partions, caution, don't use mkyaffs tool.
#nandwrite -a -o /dev/mtd4 wenxy_yaffs2.image

mount the MTD block devices to a /mnt/yaffs2 point
#mount -t yaffs2 /dev/mtdblock4 /mnt/yaffs2

list directory contents
# ls -al
drwxr-xr-x    1 root     root         2048 Apr 13 05:42 .
drwxrwxrwx   11 root     root         4096 Apr 12 03:31 ..
-rw-r--r--    1 root     root          201 Apr 13 05:42 1.txt
drw-rw-rw-    1 root     root         2048 Apr 13 08:04 lost+found
-rw-r--r--    1 root     root           35 Apr 12 03:37 test.txt

bug: there is a lost+found directory, why? Let me research, part two maybe have answer.

5. Referense to material
http://www.yaffs.net/yaffs-porting-guide
http://blog.csdn.net/colorant/archive/2007/04/12/1561830.aspx
http://www.armchina.cn/article.asp?id=116
http://www.aleph1.co.uk/lurker/message/20070912.140012.12f2d093.pt.html
http://blog.chinaunix.net/u/21948/showart_157145.html







你可能感兴趣的:(linux,image,File,System,download,tags)