📄 mkyaffs2image.c
字号:
/* * YAFFS: Yet another FFS. A NAND-flash specific file system. * * makeyaffsimage.c * * Makes a YAFFS file system image that can be used to load up a file system. * * Copyright (C) 2002 Aleph One Ltd. * for Toby Churchill Ltd and Brightstar Engineering * * Created by Charles Manning <charles@aleph1.co.uk> * * 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. * * * Nick Bane modifications flagged NCB * * Endian handling patches by James Ng. * * mkyaffs2image hacks by NCB * * 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? */ #include <stdlib.h>#include <stdio.h>#include <fcntl.h>#include <sys/types.h>#include <sys/stat.h>#include <dirent.h>#include <string.h>#include <unistd.h>#include <mtd/mtd-user.h>#include "yaffs_ecc.h"#include "yaffs_guts.h"#include "yaffs_packedtags2.h"unsigned yaffs_traceMask=0;#define MAX_OBJECTS 10000#define chunkSize 2048#define spareSize 64#define PT2_BYTES 25const char * mkyaffsimage_c_version = "$Id: mkyaffs2image.c,v 1.1.1.1 2008/03/28 04:29:21 jlwei Exp $";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, }};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->dev < ob->dev) return -1; if(oa->dev > ob->dev) return 1; if(oa->ino < ob->ino) return -1; if(oa->ino > ob->ino) return 1; return 0;}static void add_obj_to_list(dev_t dev, ino_t ino, int obj){ if(n_obj < 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 > 0) { i = bsearch(&test,obj_list,n_obj,sizeof(objItem),obj_compare); } if(i) { return i->obj; } return -1;}/* 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->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}#endifvoid 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--; }}static int write_chunk(__u8 *data, __u32 objId, __u32 chunkId, __u32 nBytes){ yaffs_ExtendedTags t; yaffs_PackedTags2 pt; unsigned char spare_buf[spareSize]; error = write(outFile,data,chunkSize); if(error < 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;/* KSI: Broken anyway -- e.g. &t is pointer to a wrong type... */#if 0 if (convert_endian) { little_to_big_endian(&t); }#endif nPages++; yaffs_PackTags2(&pt,&t); memset(spare_buf, 0xff, sizeof(spare_buf)); if (layout_no == 0) { memcpy(spare_buf, &pt, sizeof(yaffs_PackedTags2)); } else { nandmtd2_pt2buf(spare_buf, &pt); } return write(outFile,spare_buf,spareSize);}#define SWAP32(x) ((((x) & 0x000000FF) << 24) | \ (((x) & 0x0000FF00) << 8 ) | \ (((x) & 0x00FF0000) >> 8 ) | \ (((x) & 0xFF000000) >> 24))#define SWAP16(x) ((((x) & 0x00FF) << 8) | \ (((x) & 0xFF00) >> 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->type = SWAP32(oh->type); // GCC makes enums 32 bits. oh->parentObjectId = SWAP32(oh->parentObjectId); // int oh->sum__NoLongerUsed = SWAP16(oh->sum__NoLongerUsed); // __u16 - Not used, but done for completeness. // name = skip. Char array. Not swapped. oh->yst_mode = SWAP32(oh->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->notForWinCE[0] = SWAP32(oh->notForWinCE[0]); oh->notForWinCE[1] = SWAP32(oh->notForWinCE[1]); oh->notForWinCE[2] = SWAP32(oh->notForWinCE[2]); oh->notForWinCE[3] = SWAP32(oh->notForWinCE[3]); oh->notForWinCE[4] = SWAP32(oh->notForWinCE[4]);#else // Regular POSIX. oh->yst_uid = SWAP32(oh->yst_uid);
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -