2 * YAFFS: Yet Another Flash File System. A NAND-flash specific file system.
4 * Copyright (C) 2002-2010 Aleph One Ltd.
5 * for Toby Churchill Ltd and Brightstar Engineering
7 * Created by Charles Manning <charles@aleph1.co.uk>
8 * Nick Bane modifications flagged NCB
9 * Endian handling patches by James Ng.
10 * mkyaffs2image hacks by NCB
12 * This program is free software; you can redistribute it and/or modify
13 * it under the terms of the GNU General Public License version 2 as
14 * published by the Free Software Foundation.
20 * Makes a YAFFS2 file system image that can be used to load up a file system.
21 * Uses default Linux MTD layout - search for "NAND LAYOUT" to change.
27 #include <sys/types.h>
34 #include "yaffs_ecc.h"
35 #include "yaffs_guts.h"
37 #include "yaffs_tagsvalidity.h"
38 #include "yaffs_packedtags2.h"
40 unsigned yaffs_traceMask=0;
42 #define MAX_OBJECTS 10000
44 // Adjust these to match your NAND LAYOUT:
45 #define chunkSize 2048
47 #define pagesPerBlock 64
49 const char * mkyaffsimage_c_version = "$Id: mkyaffs2image.c,v 1.5 2010-01-11 21:43:18 charles Exp $";
60 static objItem obj_list[MAX_OBJECTS];
62 static int obj_id = YAFFS_NOBJECT_BUCKETS + 1;
64 static int nObjects, nDirectories, nPages;
69 static int savedErrno;
71 static int convert_endian = 0;
73 static void fatal(const char *fn)
80 static int warn(const char *fn)
88 static int obj_compare(const void *a, const void * b)
95 if(oa->dev < ob->dev) return -1;
96 if(oa->dev > ob->dev) return 1;
97 if(oa->ino < ob->ino) return -1;
98 if(oa->ino > ob->ino) return 1;
104 static void add_obj_to_list(dev_t dev, ino_t ino, int obj)
106 if(n_obj < MAX_OBJECTS)
108 obj_list[n_obj].dev = dev;
109 obj_list[n_obj].ino = ino;
110 obj_list[n_obj].obj = obj;
112 qsort(obj_list,n_obj,sizeof(objItem),obj_compare);
117 // oops! not enough space in the object array
118 fprintf(stderr,"Not enough space in object array\n");
124 static int find_obj_in_list(dev_t dev, ino_t ino)
134 i = bsearch(&test,obj_list,n_obj,sizeof(objItem),obj_compare);
144 /* This little function converts a little endian tag to a big endian tag.
145 * NOTE: The tag is not usable after this other than calculating the CRC
148 static void little_to_big_endian(yaffs_Tags *tagsPtr)
151 yaffs_TagsUnion * tags = (yaffs_TagsUnion* )tagsPtr; // Work in bytes.
152 yaffs_TagsUnion temp;
154 memset(&temp, 0, sizeof(temp));
155 // Ick, I hate magic numbers.
156 temp.asBytes[0] = ((tags->asBytes[2] & 0x0F) << 4) | ((tags->asBytes[1] & 0xF0) >> 4);
157 temp.asBytes[1] = ((tags->asBytes[1] & 0x0F) << 4) | ((tags->asBytes[0] & 0xF0) >> 4);
158 temp.asBytes[2] = ((tags->asBytes[0] & 0x0F) << 4) | ((tags->asBytes[2] & 0x30) >> 2) | ((tags->asBytes[3] & 0xC0) >> 6);
159 temp.asBytes[3] = ((tags->asBytes[3] & 0x3F) << 2) | ((tags->asBytes[2] & 0xC0) >> 6);
160 temp.asBytes[4] = ((tags->asBytes[6] & 0x03) << 6) | ((tags->asBytes[5] & 0xFC) >> 2);
161 temp.asBytes[5] = ((tags->asBytes[5] & 0x03) << 6) | ((tags->asBytes[4] & 0xFC) >> 2);
162 temp.asBytes[6] = ((tags->asBytes[4] & 0x03) << 6) | (tags->asBytes[7] & 0x3F);
163 temp.asBytes[7] = (tags->asBytes[6] & 0xFC) | ((tags->asBytes[7] & 0xC0) >> 6);
166 tags->asBytes[0] = temp.asBytes[0];
167 tags->asBytes[1] = temp.asBytes[1];
168 tags->asBytes[2] = temp.asBytes[2];
169 tags->asBytes[3] = temp.asBytes[3];
170 tags->asBytes[4] = temp.asBytes[4];
171 tags->asBytes[5] = temp.asBytes[5];
172 tags->asBytes[6] = temp.asBytes[6];
173 tags->asBytes[7] = temp.asBytes[7];
177 static void shuffle_oob(char *spareData, yaffs_PackedTags2 *pt)
179 assert(sizeof(*pt) <= spareSize);
180 // NAND LAYOUT: For non-trivial OOB orderings, here would be a good place to shuffle.
181 memcpy(spareData, pt, sizeof(*pt));
184 static int write_chunk(__u8 *data, __u32 objId, __u32 chunkId, __u32 nBytes)
186 yaffs_ExtendedTags t;
187 yaffs_PackedTags2 pt;
188 char spareData[spareSize];
190 if (write(outFile,data,chunkSize) != chunkSize)
193 yaffs_InitialiseTags(&t);
196 // t.serialNumber = 0;
197 t.serialNumber = 1; // **CHECK**
198 t.byteCount = nBytes;
201 t.sequenceNumber = YAFFS_LOWEST_SEQUENCE_NUMBER;
203 // added NCB **CHECK**
208 little_to_big_endian(&t);
213 memset(&pt, 0, sizeof(pt));
214 yaffs_PackTags2(&pt,&t,1);
216 memset(spareData, 0xff, sizeof(spareData));
217 shuffle_oob(spareData, &pt);
219 if (write(outFile,spareData,sizeof(spareData)) != sizeof(spareData))
224 #define SWAP32(x) ((((x) & 0x000000FF) << 24) | \
225 (((x) & 0x0000FF00) << 8 ) | \
226 (((x) & 0x00FF0000) >> 8 ) | \
227 (((x) & 0xFF000000) >> 24))
229 #define SWAP16(x) ((((x) & 0x00FF) << 8) | \
230 (((x) & 0xFF00) >> 8))
232 // This one is easier, since the types are more standard. No funky shifts here.
233 static void object_header_little_to_big_endian(yaffs_ObjectHeader* oh)
236 oh->type = SWAP32(oh->type); // GCC makes enums 32 bits.
237 oh->parentObjectId = SWAP32(oh->parentObjectId); // int
238 oh->sum__NoLongerUsed = SWAP16(oh->sum__NoLongerUsed); // __u16 - Not used, but done for completeness.
239 // name = skip. Char array. Not swapped.
240 oh->yst_mode = SWAP32(oh->yst_mode);
241 #ifdef CONFIG_YAFFS_WINCE // WinCE doesn't implement this, but we need to just in case.
242 // In fact, WinCE would be *THE* place where this would be an issue.
243 // Why? WINCE is little-endian only.
246 int n = sizeof(oh->notForWinCE)/sizeof(oh->notForWinCE[0]);
247 for(i = 0; i < n; i++)
248 oh->notForWinCE[i] = SWAP32(oh->notForWinCE[i]);
252 oh->yst_uid = SWAP32(oh->yst_uid);
253 oh->yst_gid = SWAP32(oh->yst_gid);
254 oh->yst_atime = SWAP32(oh->yst_atime);
255 oh->yst_mtime = SWAP32(oh->yst_mtime);
256 oh->yst_ctime = SWAP32(oh->yst_ctime);
259 oh->fileSize = SWAP32(oh->fileSize); // Aiee. An int... signed, at that!
260 oh->equivalentObjectId = SWAP32(oh->equivalentObjectId);
261 // alias - char array.
262 oh->yst_rdev = SWAP32(oh->yst_rdev);
264 #ifdef CONFIG_YAFFS_WINCE
265 oh->win_ctime[0] = SWAP32(oh->win_ctime[0]);
266 oh->win_ctime[1] = SWAP32(oh->win_ctime[1]);
267 oh->win_atime[0] = SWAP32(oh->win_atime[0]);
268 oh->win_atime[1] = SWAP32(oh->win_atime[1]);
269 oh->win_mtime[0] = SWAP32(oh->win_mtime[0]);
270 oh->win_mtime[1] = SWAP32(oh->win_mtime[1]);
274 int n = sizeof(oh->roomToGrow)/sizeof(oh->roomToGrow[0]);
276 oh->roomToGrow[i] = SWAP32(oh->roomToGrow[i]);
281 static int write_object_header(int objId, yaffs_ObjectType t, struct stat *s, int parent, const char *name, int equivalentObj, const char * alias)
283 __u8 bytes[chunkSize];
286 yaffs_ObjectHeader *oh = (yaffs_ObjectHeader *)bytes;
288 memset(bytes,0xff,sizeof(bytes));
292 oh->parentObjectId = parent;
294 if (strlen(name)+1 > sizeof(oh->name))
296 errno = ENAMETOOLONG;
297 return warn("object name");
299 memset(oh->name,0,sizeof(oh->name));
300 strcpy(oh->name,name);
303 if(t != YAFFS_OBJECT_TYPE_HARDLINK)
305 oh->yst_mode = s->st_mode;
306 oh->yst_uid = s->st_uid;
307 // NCB 12/9/02 oh->yst_gid = s->yst_uid;
308 oh->yst_gid = s->st_gid;
309 oh->yst_atime = s->st_atime;
310 oh->yst_mtime = s->st_mtime;
311 oh->yst_ctime = s->st_ctime;
312 oh->yst_rdev = s->st_rdev;
315 if(t == YAFFS_OBJECT_TYPE_FILE)
317 oh->fileSize = s->st_size;
320 if(t == YAFFS_OBJECT_TYPE_HARDLINK)
322 oh->equivalentObjectId = equivalentObj;
325 if(t == YAFFS_OBJECT_TYPE_SYMLINK)
327 if (strlen(alias)+1 > sizeof(oh->alias))
329 errno = ENAMETOOLONG;
330 return warn("object alias");
332 memset(oh->alias,0,sizeof(oh->alias));
333 strcpy(oh->alias,alias);
338 object_header_little_to_big_endian(oh);
341 return write_chunk(bytes,objId,0,0xffff);
345 static void pad_image()
347 __u8 data[chunkSize + spareSize];
348 int padPages = (nPages % pagesPerBlock);
352 memset(data, 0xff, sizeof(data));
353 for (padPages = pagesPerBlock-padPages; padPages; padPages--)
355 if (write(outFile, data, sizeof(data)) != sizeof(data))
361 static int process_directory(int parent, const char *path)
365 struct dirent *entry;
376 while((entry = readdir(dir)) != NULL)
379 /* Ignore . and .. */
380 if(strcmp(entry->d_name,".") &&
381 strcmp(entry->d_name,".."))
388 if (snprintf(full_name,sizeof(full_name),"%s/%s",path,entry->d_name) >= (int)sizeof(full_name))
394 if (lstat(full_name,&stats) < 0)
400 if(S_ISLNK(stats.st_mode) ||
401 S_ISREG(stats.st_mode) ||
402 S_ISDIR(stats.st_mode) ||
403 S_ISFIFO(stats.st_mode) ||
404 S_ISBLK(stats.st_mode) ||
405 S_ISCHR(stats.st_mode) ||
406 S_ISSOCK(stats.st_mode))
412 printf("Object %d, %s is a ",newObj,full_name);
414 /* We're going to create an object for it */
415 if((equivalentObj = find_obj_in_list(stats.st_dev, stats.st_ino)) > 0)
417 /* we need to make a hard link */
418 printf("hard link to object %d\n",equivalentObj);
419 write_object_header(newObj, YAFFS_OBJECT_TYPE_HARDLINK, &stats, parent, entry->d_name, equivalentObj, NULL);
424 add_obj_to_list(stats.st_dev,stats.st_ino,newObj);
426 if(S_ISLNK(stats.st_mode))
431 memset(symname,0, sizeof(symname));
433 if (readlink(full_name,symname,sizeof(symname) -1) < 0)
439 printf("symlink to \"%s\"\n",symname);
440 write_object_header(newObj, YAFFS_OBJECT_TYPE_SYMLINK, &stats, parent, entry->d_name, -1, symname);
443 else if(S_ISREG(stats.st_mode))
446 if(write_object_header(newObj, YAFFS_OBJECT_TYPE_FILE, &stats, parent, entry->d_name, -1, NULL) == 0)
449 __u8 bytes[chunkSize];
453 h = open(full_name,O_RDONLY);
456 memset(bytes,0xff,sizeof(bytes));
457 while((nBytes = read(h,bytes,sizeof(bytes))) > 0)
460 write_chunk(bytes,newObj,chunk,nBytes);
461 memset(bytes,0xff,sizeof(bytes));
466 printf("%d data chunks written\n",chunk);
477 else if(S_ISSOCK(stats.st_mode))
480 write_object_header(newObj, YAFFS_OBJECT_TYPE_SPECIAL, &stats, parent, entry->d_name, -1, NULL);
482 else if(S_ISFIFO(stats.st_mode))
485 write_object_header(newObj, YAFFS_OBJECT_TYPE_SPECIAL, &stats, parent, entry->d_name, -1, NULL);
487 else if(S_ISCHR(stats.st_mode))
489 printf("character device\n");
490 write_object_header(newObj, YAFFS_OBJECT_TYPE_SPECIAL, &stats, parent, entry->d_name, -1, NULL);
492 else if(S_ISBLK(stats.st_mode))
494 printf("block device\n");
495 write_object_header(newObj, YAFFS_OBJECT_TYPE_SPECIAL, &stats, parent, entry->d_name, -1, NULL);
497 else if(S_ISDIR(stats.st_mode))
499 printf("directory\n");
500 if (write_object_header(newObj, YAFFS_OBJECT_TYPE_DIRECTORY, &stats, parent, entry->d_name, -1, NULL) == 0)
501 process_directory(newObj,full_name);
507 fprintf(stderr, "%s: unhandled type\n", full_name);
521 int main(int argc, char *argv[])
525 printf("mkyaffs2image: image building tool for YAFFS2 built "__DATE__"\n");
529 printf("usage: mkyaffs2image dir image_file [convert]\n");
530 printf(" dir the directory tree to be converted\n");
531 printf(" image_file the output file to hold the image\n");
532 printf(" 'convert' produce a big-endian image from a little-endian machine\n");
536 if ((argc == 4) && (!strncmp(argv[3], "convert", strlen("convert"))))
541 if(stat(argv[1],&stats) < 0)
543 printf("Could not stat %s\n",argv[1]);
547 if(!S_ISDIR(stats.st_mode))
549 printf(" %s is not a directory\n",argv[1]);
553 outFile = open(argv[2],O_CREAT | O_TRUNC | O_WRONLY, S_IREAD | S_IWRITE);
558 printf("Could not open output file %s\n",argv[2]);
562 printf("Processing directory %s into image file %s\n",argv[1],argv[2]);
563 process_directory(YAFFS_OBJECTID_ROOT,argv[1]);
572 perror("operation incomplete");
576 printf("Operation complete.\n"
577 "%d objects in %d directories\n"
578 "%d NAND pages\n",nObjects, nDirectories, nPages);