Hi folks,
Andre's patch helped me a lot. Even if filling a yaffs partition is can easily
be done by just copying the files onto it, I like writing an image. It also
can be a little faster.
I modified Andre's patch a little more so that also works on big endian PowerPC
platforms. First tests have been successful, so that I do not want to keep
the patch in secret.
Have fun.
Matthias
Index: mkyaffs2image.c
===================================================================
RCS file: /home/aleph1/cvs/yaffs2/utils/mkyaffs2image.c,v
retrieving revision 1.4
diff -r1.4 mkyaffs2image.c
21a22,49
> *
> * Changes by Sergey Kubushin flagged KSI
> *
> */
>
> /* KSI:
> * All this nightmare should be rewritten from ground up. Why save return
> * values if nobody checks them? The read/write function returns only one
> * error, -1. Positive return value does NOT mean read/write operation has
> * been completed successfully. If somebody opens files, he MUST close them
> * when they are not longer needed. Only those brave enough can write 64
> * bytes from a yaffs_PackedTags2 structure. The list is too long, there is
> * enough bugs here to write a couple of thick books on how NOT to write
> * programs...
> *
> * And BTW, what was one supposed to do with that file that this horror
> * occasionally managed to generate?
> *
> * MF:
> * Answer: You can write it into an MTD character device on your target
> * using the 'nandwrite' utility. For a big endian PowerPC system with
> * a large block NAND chip I tested it like this:
> *
> * on-my-PC> ./mkyaffs2image 1 directory yaffs2_be_oob64.img convert
> *
> * on-my-target> flash_eraseall /dev/mtdX
> * on-my-target> nandwrite -a -o /dev/mtdX yaffs2_be_oob64.img
> * on-my-target> mount -t yaffs2 /dev/mtdblockX /nand
31a60,61
> #define __user
> #include <mtd/mtd-user.h>
35d64
< #include "yaffs_tagsvalidity.h"
43a73
> #define PT2_BYTES 25
46a77,111
> static int layout_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,
> }
> };
60c125
< static int nObjects, nDirectories, nPages;
---
> static int nObjects = 0, nDirectories = 0, nPages = 0;
123a189,219
> /**
> * Calculate the ECC using the YAFFS internal functions,
> * and then put it into the oob buffer in the correct place
> */
> 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->eccpos;
>
> for (; eccsteps; eccsteps--) {
> yaffs_ECCCalculate (&data[datidx], ecc_code);
> for (i = 0; i < 3; i++, eccidx++)
> oob_buf[oob_config[eccidx]] = ecc_code[i];
> datidx += 256;
> }
> }
>
> #define SWAP32(x) ((((x) & 0x000000FF) << 24) | \
> (((x) & 0x0000FF00) << 8 ) | \
> (((x) & 0x00FF0000) >> 8 ) | \
> (((x) & 0xFF000000) >> 24))
>
> #define SWAP16(x) ((((x) & 0x00FF) << 8) | \
> (((x) & 0xFF00) >> 8))
>
128c224
< static void little_to_big_endian(yaffs_Tags *tagsPtr)
---
> static void little_to_big_endian(yaffs_ExtendedTags *tagsPtr)
130,154c226,300
< #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->asBytes[2] & 0x0F) << 4) | ((tags->asBytes[1] & 0xF0) >> 4);
< temp.asBytes[1] = ((tags->asBytes[1] & 0x0F) << 4) | ((tags->asBytes[0] & 0xF0) >> 4);
< temp.asBytes[2] = ((tags->asBytes[0] & 0x0F) << 4) | ((tags->asBytes[2] & 0x30) >> 2) | ((tags->asBytes[3] & 0xC0) >> 6);
< temp.asBytes[3] = ((tags->asBytes[3] & 0x3F) << 2) | ((tags->asBytes[2] & 0xC0) >> 6);
< temp.asBytes[4] = ((tags->asBytes[6] & 0x03) << 6) | ((tags->asBytes[5] & 0xFC) >> 2);
< temp.asBytes[5] = ((tags->asBytes[5] & 0x03) << 6) | ((tags->asBytes[4] & 0xFC) >> 2);
< temp.asBytes[6] = ((tags->asBytes[4] & 0x03) << 6) | (tags->asBytes[7] & 0x3F);
< temp.asBytes[7] = (tags->asBytes[6] & 0xFC) | ((tags->asBytes[7] & 0xC0) >> 6);
<
< // Now copy it back.
< tags->asBytes[0] = temp.asBytes[0];
< tags->asBytes[1] = temp.asBytes[1];
< tags->asBytes[2] = temp.asBytes[2];
< tags->asBytes[3] = temp.asBytes[3];
< tags->asBytes[4] = temp.asBytes[4];
< tags->asBytes[5] = temp.asBytes[5];
< tags->asBytes[6] = temp.asBytes[6];
< tags->asBytes[7] = temp.asBytes[7];
< #endif
---
> tagsPtr->validMarker0 = SWAP32(tagsPtr->validMarker0);
> tagsPtr->chunkUsed = SWAP32(tagsPtr->chunkUsed);
> tagsPtr->objectId = SWAP32(tagsPtr->objectId);
> tagsPtr->chunkId = SWAP32(tagsPtr->chunkId);
> tagsPtr->byteCount = SWAP32(tagsPtr->byteCount);
>
> /* The following stuff only has meaning when we read */
> /* enum type is 32bit */
> tagsPtr->eccResult = SWAP32(tagsPtr->eccResult);
> tagsPtr->blockBad = SWAP32(tagsPtr->blockBad);
>
> /* YAFFS 1 stuff */
> tagsPtr->chunkDeleted = SWAP32(tagsPtr->chunkDeleted);
> tagsPtr->serialNumber = SWAP32(tagsPtr->chunkDeleted);
>
> /* YAFFS2 stuff */
> tagsPtr->sequenceNumber = SWAP32(tagsPtr->sequenceNumber);
>
> /* Extra info if this is an object header (YAFFS2 only) */
>
> tagsPtr->extraHeaderInfoAvailable = SWAP32(tagsPtr->extraHeaderInfoAvailable);
> tagsPtr->extraParentObjectId = SWAP32(tagsPtr->extraParentObjectId);
> tagsPtr->extraIsShrinkHeader = SWAP32(tagsPtr->extraIsShrinkHeader);
> tagsPtr->extraShadows = SWAP32(tagsPtr->extraShadows);
>
> /* enum type is 32bit */
> tagsPtr->extraObjectType = SWAP32(tagsPtr->extraObjectType);
>
> tagsPtr->extraFileLength = SWAP32(tagsPtr->extraFileLength);
> tagsPtr->extraEquivalentObjectId = SWAP32(tagsPtr->extraEquivalentObjectId);
>
> tagsPtr->validMarker1 = SWAP32(tagsPtr->validMarker1);
> }
>
>
> static void nandmtd2_pt2buf(unsigned char *buf, yaffs_PackedTags2 *pt)
> {
> int i, j = 0, k, n;
> unsigned char pt2_byte_buf[PT2_BYTES];
>
> *((unsigned int *) &pt2_byte_buf[0]) = pt->t.sequenceNumber;
> *((unsigned int *) &pt2_byte_buf[4]) = pt->t.objectId;
> *((unsigned int *) &pt2_byte_buf[8]) = pt->t.chunkId;
> *((unsigned int *) &pt2_byte_buf[12]) = pt->t.byteCount;
> pt2_byte_buf[16] = pt->ecc.colParity;
> pt2_byte_buf[17] = pt->ecc.lineParity & 0xff;
> pt2_byte_buf[18] = (pt->ecc.lineParity >> 8) & 0xff;
> pt2_byte_buf[19] = (pt->ecc.lineParity >> 16) & 0xff;
> pt2_byte_buf[20] = (pt->ecc.lineParity >> 24) & 0xff;
> pt2_byte_buf[21] = pt->ecc.lineParityPrime & 0xff;
> pt2_byte_buf[22] = (pt->ecc.lineParityPrime >> 8) & 0xff;
> pt2_byte_buf[23] = (pt->ecc.lineParityPrime >> 16) & 0xff;
> pt2_byte_buf[24] = (pt->ecc.lineParityPrime >> 24) & 0xff;
>
> 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 < 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--;
> }
160a307
> unsigned char spare_buf[spareSize];
187,190c334
< // return write(outFile,&pt,sizeof(yaffs_PackedTags2));
< return write(outFile,&pt,spareSize);
<
< }
---
> memset(spare_buf, 0xff, sizeof(spare_buf));
192,195c336,345
< #define SWAP32(x) ((((x) & 0x000000FF) << 24) | \
< (((x) & 0x0000FF00) << 8 ) | \
< (((x) & 0x00FF0000) >> 8 ) | \
< (((x) & 0xFF000000) >> 24))
---
> if (layout_no == 0) {
> memcpy(spare_buf, &pt, sizeof(yaffs_PackedTags2));
> } else {
> nandmtd2_pt2buf(spare_buf, &pt);
> }
>
> yaffs_PutDataECC(data, &spare_buf[0]);
>
> return write(outFile,spare_buf,spareSize);
> }
197,198d346
< #define SWAP16(x) ((((x) & 0x00FF) << 8) | \
< (((x) & 0xFF00) >> 8))
200d347
< // This one is easier, since the types are more standard. No funky shifts here.
257a405
>
305c453
<
---
>
403a552
> close(h);
408,410c557
< }
< close(h);
<
---
> }
448a596,602
> /* KSI:
> * Who is supposed to close those open directories in this
> * recursive function, lord Byron? Stock "ulimit -n" is 1024
> * and e.g. stock Fedora /etc directory has more that 1024
> * directories...
> */
> closedir(dir);
454a609,617
> static 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.\n");
> exit(1);
> }
459c622,623
<
---
> int i;
>
462c626
< if(argc < 3)
---
> if ((argc < 4) || (sscanf(argv[1], "%u", &layout_no) != 1))
464,468c628
< 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");
< exit(1);
---
> usage();
471,474c631,642
< if ((argc == 4) && (!strncmp(argv[3], "convert", strlen("convert"))))
< {
< convert_endian = 1;
< }
---
> i = 0;
>
> while (oob_layout[i].useecc != -1)
> i++;
>
> if (layout_no >= i)
> usage();
>
> if ((argc == 5) && (!strncmp(argv[4], "convert", strlen("convert"))))
> {
> convert_endian = 1;
> }
476c644
< if(stat(argv[1],&stats) < 0)
---
> if(stat(argv[2],&stats) < 0)
478c646
< printf("Could not stat %s\n",argv[1]);
---
> printf("Could not stat %s\n",argv[2]);
484c652
< printf(" %s is not a directory\n",argv[1]);
---
> printf(" %s is not a directory\n",argv[2]);
488c656
< outFile = open(argv[2],O_CREAT | O_TRUNC | O_WRONLY, S_IREAD | S_IWRITE);
---
> outFile = open(argv[3],O_CREAT | O_TRUNC | O_WRONLY, S_IREAD | S_IWRITE);
493c661
< printf("Could not open output file %s\n",argv[2]);
---
> printf("Could not open output file %s\n",argv[3]);
497c665
< printf("Processing directory %s into image file %s\n",argv[1],argv[2]);
---
> printf("Processing directory %s into image file %s\n",argv[2],argv[3]);
498a667
>
500c669
< error = process_directory(YAFFS_OBJECTID_ROOT,argv[1]);
---
> error = process_directory(YAFFS_OBJECTID_ROOT,argv[2]);
On Friday 21 September 2007 00:25, Andre Renaud wrote:
> Here is a patch, which is mostly a slightly modified version of KSI's patch to enable
> correct OOB placement in the yaffs2 image, but with the addition of calculating
> the ECC checksums and putting them in the correct place as well. This is useful if
> doing large production runs where the NAND chips will be programmed in advance. Or if
> trying to program the NAND chips using a JTAG programmer etc...
>
> It isn't a particularly clean patch, and it does #ifdef out the support for endian swapping,
> but it certainly appears to be working for me.