2 * YAFFS: Yet Another Flash File System. A NAND-flash specific file system.
4 * Copyright (C) 2002-2007 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 - change if you need something different.
27 #include <sys/types.h>
32 #include "yaffs_ecc.h"
33 #include "yaffs_guts.h"
35 #include "yaffs_tagsvalidity.h"
36 #include "yaffs_packedtags2.h"
38 unsigned yaffs_traceMask=0;
40 #define MAX_OBJECTS 10000
42 #define chunkSize 2048
45 const char * mkyaffsimage_c_version = "$Id: mkyaffs2image.c,v 1.4 2007-02-14 01:09:06 wookey Exp $";
56 static objItem obj_list[MAX_OBJECTS];
58 static int obj_id = YAFFS_NOBJECT_BUCKETS + 1;
60 static int nObjects, nDirectories, nPages;
66 static int convert_endian = 0;
68 static int obj_compare(const void *a, const void * b)
75 if(oa->dev < ob->dev) return -1;
76 if(oa->dev > ob->dev) return 1;
77 if(oa->ino < ob->ino) return -1;
78 if(oa->ino > ob->ino) return 1;
84 static void add_obj_to_list(dev_t dev, ino_t ino, int obj)
86 if(n_obj < MAX_OBJECTS)
88 obj_list[n_obj].dev = dev;
89 obj_list[n_obj].ino = ino;
90 obj_list[n_obj].obj = obj;
92 qsort(obj_list,n_obj,sizeof(objItem),obj_compare);
97 // oops! not enough space in the object array
98 fprintf(stderr,"Not enough space in object array\n");
104 static int find_obj_in_list(dev_t dev, ino_t ino)
114 i = bsearch(&test,obj_list,n_obj,sizeof(objItem),obj_compare);
124 /* This little function converts a little endian tag to a big endian tag.
125 * NOTE: The tag is not usable after this other than calculating the CRC
128 static void little_to_big_endian(yaffs_Tags *tagsPtr)
131 yaffs_TagsUnion * tags = (yaffs_TagsUnion* )tagsPtr; // Work in bytes.
132 yaffs_TagsUnion temp;
134 memset(&temp, 0, sizeof(temp));
135 // Ick, I hate magic numbers.
136 temp.asBytes[0] = ((tags->asBytes[2] & 0x0F) << 4) | ((tags->asBytes[1] & 0xF0) >> 4);
137 temp.asBytes[1] = ((tags->asBytes[1] & 0x0F) << 4) | ((tags->asBytes[0] & 0xF0) >> 4);
138 temp.asBytes[2] = ((tags->asBytes[0] & 0x0F) << 4) | ((tags->asBytes[2] & 0x30) >> 2) | ((tags->asBytes[3] & 0xC0) >> 6);
139 temp.asBytes[3] = ((tags->asBytes[3] & 0x3F) << 2) | ((tags->asBytes[2] & 0xC0) >> 6);
140 temp.asBytes[4] = ((tags->asBytes[6] & 0x03) << 6) | ((tags->asBytes[5] & 0xFC) >> 2);
141 temp.asBytes[5] = ((tags->asBytes[5] & 0x03) << 6) | ((tags->asBytes[4] & 0xFC) >> 2);
142 temp.asBytes[6] = ((tags->asBytes[4] & 0x03) << 6) | (tags->asBytes[7] & 0x3F);
143 temp.asBytes[7] = (tags->asBytes[6] & 0xFC) | ((tags->asBytes[7] & 0xC0) >> 6);
146 tags->asBytes[0] = temp.asBytes[0];
147 tags->asBytes[1] = temp.asBytes[1];
148 tags->asBytes[2] = temp.asBytes[2];
149 tags->asBytes[3] = temp.asBytes[3];
150 tags->asBytes[4] = temp.asBytes[4];
151 tags->asBytes[5] = temp.asBytes[5];
152 tags->asBytes[6] = temp.asBytes[6];
153 tags->asBytes[7] = temp.asBytes[7];
157 static int write_chunk(__u8 *data, __u32 objId, __u32 chunkId, __u32 nBytes)
159 yaffs_ExtendedTags t;
160 yaffs_PackedTags2 pt;
162 error = write(outFile,data,chunkSize);
163 if(error < 0) return error;
165 yaffs_InitialiseTags(&t);
168 // t.serialNumber = 0;
169 t.serialNumber = 1; // **CHECK**
170 t.byteCount = nBytes;
173 t.sequenceNumber = YAFFS_LOWEST_SEQUENCE_NUMBER;
175 // added NCB **CHECK**
180 little_to_big_endian(&t);
185 yaffs_PackTags2(&pt,&t);
187 // return write(outFile,&pt,sizeof(yaffs_PackedTags2));
188 return write(outFile,&pt,spareSize);
192 #define SWAP32(x) ((((x) & 0x000000FF) << 24) | \
193 (((x) & 0x0000FF00) << 8 ) | \
194 (((x) & 0x00FF0000) >> 8 ) | \
195 (((x) & 0xFF000000) >> 24))
197 #define SWAP16(x) ((((x) & 0x00FF) << 8) | \
198 (((x) & 0xFF00) >> 8))
200 // This one is easier, since the types are more standard. No funky shifts here.
201 static void object_header_little_to_big_endian(yaffs_ObjectHeader* oh)
203 oh->type = SWAP32(oh->type); // GCC makes enums 32 bits.
204 oh->parentObjectId = SWAP32(oh->parentObjectId); // int
205 oh->sum__NoLongerUsed = SWAP16(oh->sum__NoLongerUsed); // __u16 - Not used, but done for completeness.
206 // name = skip. Char array. Not swapped.
207 oh->yst_mode = SWAP32(oh->yst_mode);
208 #ifdef CONFIG_YAFFS_WINCE // WinCE doesn't implement this, but we need to just in case.
209 // In fact, WinCE would be *THE* place where this would be an issue!
210 oh->notForWinCE[0] = SWAP32(oh->notForWinCE[0]);
211 oh->notForWinCE[1] = SWAP32(oh->notForWinCE[1]);
212 oh->notForWinCE[2] = SWAP32(oh->notForWinCE[2]);
213 oh->notForWinCE[3] = SWAP32(oh->notForWinCE[3]);
214 oh->notForWinCE[4] = SWAP32(oh->notForWinCE[4]);
217 oh->yst_uid = SWAP32(oh->yst_uid);
218 oh->yst_gid = SWAP32(oh->yst_gid);
219 oh->yst_atime = SWAP32(oh->yst_atime);
220 oh->yst_mtime = SWAP32(oh->yst_mtime);
221 oh->yst_ctime = SWAP32(oh->yst_ctime);
224 oh->fileSize = SWAP32(oh->fileSize); // Aiee. An int... signed, at that!
225 oh->equivalentObjectId = SWAP32(oh->equivalentObjectId);
226 // alias - char array.
227 oh->yst_rdev = SWAP32(oh->yst_rdev);
229 #ifdef CONFIG_YAFFS_WINCE
230 oh->win_ctime[0] = SWAP32(oh->win_ctime[0]);
231 oh->win_ctime[1] = SWAP32(oh->win_ctime[1]);
232 oh->win_atime[0] = SWAP32(oh->win_atime[0]);
233 oh->win_atime[1] = SWAP32(oh->win_atime[1]);
234 oh->win_mtime[0] = SWAP32(oh->win_mtime[0]);
235 oh->win_mtime[1] = SWAP32(oh->win_mtime[1]);
236 oh->roomToGrow[0] = SWAP32(oh->roomToGrow[0]);
237 oh->roomToGrow[1] = SWAP32(oh->roomToGrow[1]);
238 oh->roomToGrow[2] = SWAP32(oh->roomToGrow[2]);
239 oh->roomToGrow[3] = SWAP32(oh->roomToGrow[3]);
240 oh->roomToGrow[4] = SWAP32(oh->roomToGrow[4]);
241 oh->roomToGrow[5] = SWAP32(oh->roomToGrow[5]);
243 oh->roomToGrow[0] = SWAP32(oh->roomToGrow[0]);
244 oh->roomToGrow[1] = SWAP32(oh->roomToGrow[1]);
245 oh->roomToGrow[2] = SWAP32(oh->roomToGrow[2]);
246 oh->roomToGrow[3] = SWAP32(oh->roomToGrow[3]);
247 oh->roomToGrow[4] = SWAP32(oh->roomToGrow[4]);
248 oh->roomToGrow[5] = SWAP32(oh->roomToGrow[5]);
249 oh->roomToGrow[6] = SWAP32(oh->roomToGrow[6]);
250 oh->roomToGrow[7] = SWAP32(oh->roomToGrow[7]);
251 oh->roomToGrow[8] = SWAP32(oh->roomToGrow[8]);
252 oh->roomToGrow[9] = SWAP32(oh->roomToGrow[9]);
253 oh->roomToGrow[10] = SWAP32(oh->roomToGrow[10]);
254 oh->roomToGrow[11] = SWAP32(oh->roomToGrow[11]);
258 static int write_object_header(int objId, yaffs_ObjectType t, struct stat *s, int parent, const char *name, int equivalentObj, const char * alias)
260 __u8 bytes[chunkSize];
263 yaffs_ObjectHeader *oh = (yaffs_ObjectHeader *)bytes;
265 memset(bytes,0xff,sizeof(bytes));
269 oh->parentObjectId = parent;
271 strncpy(oh->name,name,YAFFS_MAX_NAME_LENGTH);
274 if(t != YAFFS_OBJECT_TYPE_HARDLINK)
276 oh->yst_mode = s->st_mode;
277 oh->yst_uid = s->st_uid;
278 // NCB 12/9/02 oh->yst_gid = s->yst_uid;
279 oh->yst_gid = s->st_gid;
280 oh->yst_atime = s->st_atime;
281 oh->yst_mtime = s->st_mtime;
282 oh->yst_ctime = s->st_ctime;
283 oh->yst_rdev = s->st_rdev;
286 if(t == YAFFS_OBJECT_TYPE_FILE)
288 oh->fileSize = s->st_size;
291 if(t == YAFFS_OBJECT_TYPE_HARDLINK)
293 oh->equivalentObjectId = equivalentObj;
296 if(t == YAFFS_OBJECT_TYPE_SYMLINK)
298 strncpy(oh->alias,alias,YAFFS_MAX_ALIAS_LENGTH);
303 object_header_little_to_big_endian(oh);
306 return write_chunk(bytes,objId,0,0xffff);
311 static int process_directory(int parent, const char *path)
315 struct dirent *entry;
323 while((entry = readdir(dir)) != NULL)
326 /* Ignore . and .. */
327 if(strcmp(entry->d_name,".") &&
328 strcmp(entry->d_name,".."))
335 sprintf(full_name,"%s/%s",path,entry->d_name);
337 lstat(full_name,&stats);
339 if(S_ISLNK(stats.st_mode) ||
340 S_ISREG(stats.st_mode) ||
341 S_ISDIR(stats.st_mode) ||
342 S_ISFIFO(stats.st_mode) ||
343 S_ISBLK(stats.st_mode) ||
344 S_ISCHR(stats.st_mode) ||
345 S_ISSOCK(stats.st_mode))
351 printf("Object %d, %s is a ",newObj,full_name);
353 /* We're going to create an object for it */
354 if((equivalentObj = find_obj_in_list(stats.st_dev, stats.st_ino)) > 0)
356 /* we need to make a hard link */
357 printf("hard link to object %d\n",equivalentObj);
358 error = write_object_header(newObj, YAFFS_OBJECT_TYPE_HARDLINK, &stats, parent, entry->d_name, equivalentObj, NULL);
363 add_obj_to_list(stats.st_dev,stats.st_ino,newObj);
365 if(S_ISLNK(stats.st_mode))
370 memset(symname,0, sizeof(symname));
372 readlink(full_name,symname,sizeof(symname) -1);
374 printf("symlink to \"%s\"\n",symname);
375 error = write_object_header(newObj, YAFFS_OBJECT_TYPE_SYMLINK, &stats, parent, entry->d_name, -1, symname);
378 else if(S_ISREG(stats.st_mode))
381 error = write_object_header(newObj, YAFFS_OBJECT_TYPE_FILE, &stats, parent, entry->d_name, -1, NULL);
386 __u8 bytes[chunkSize];
390 h = open(full_name,O_RDONLY);
393 memset(bytes,0xff,sizeof(bytes));
394 while((nBytes = read(h,bytes,sizeof(bytes))) > 0)
397 write_chunk(bytes,newObj,chunk,nBytes);
398 memset(bytes,0xff,sizeof(bytes));
403 printf("%d data chunks written\n",chunk);
407 perror("Error opening file");
414 else if(S_ISSOCK(stats.st_mode))
417 error = write_object_header(newObj, YAFFS_OBJECT_TYPE_SPECIAL, &stats, parent, entry->d_name, -1, NULL);
419 else if(S_ISFIFO(stats.st_mode))
422 error = write_object_header(newObj, YAFFS_OBJECT_TYPE_SPECIAL, &stats, parent, entry->d_name, -1, NULL);
424 else if(S_ISCHR(stats.st_mode))
426 printf("character device\n");
427 error = write_object_header(newObj, YAFFS_OBJECT_TYPE_SPECIAL, &stats, parent, entry->d_name, -1, NULL);
429 else if(S_ISBLK(stats.st_mode))
431 printf("block device\n");
432 error = write_object_header(newObj, YAFFS_OBJECT_TYPE_SPECIAL, &stats, parent, entry->d_name, -1, NULL);
434 else if(S_ISDIR(stats.st_mode))
436 printf("directory\n");
437 error = write_object_header(newObj, YAFFS_OBJECT_TYPE_DIRECTORY, &stats, parent, entry->d_name, -1, NULL);
438 // NCB modified 10/9/2001 process_directory(1,full_name);
439 process_directory(newObj,full_name);
445 printf(" we don't handle this type\n");
456 int main(int argc, char *argv[])
460 printf("mkyaffs2image: image building tool for YAFFS2 built "__DATE__"\n");
464 printf("usage: mkyaffs2image dir image_file [convert]\n");
465 printf(" dir the directory tree to be converted\n");
466 printf(" image_file the output file to hold the image\n");
467 printf(" 'convert' produce a big-endian image from a little-endian machine\n");
471 if ((argc == 4) && (!strncmp(argv[3], "convert", strlen("convert"))))
476 if(stat(argv[1],&stats) < 0)
478 printf("Could not stat %s\n",argv[1]);
482 if(!S_ISDIR(stats.st_mode))
484 printf(" %s is not a directory\n",argv[1]);
488 outFile = open(argv[2],O_CREAT | O_TRUNC | O_WRONLY, S_IREAD | S_IWRITE);
493 printf("Could not open output file %s\n",argv[2]);
497 printf("Processing directory %s into image file %s\n",argv[1],argv[2]);
498 error = write_object_header(1, YAFFS_OBJECT_TYPE_DIRECTORY, &stats, 1,"", -1, NULL);
500 error = process_directory(YAFFS_OBJECTID_ROOT,argv[1]);
506 perror("operation incomplete");
511 printf("Operation complete.\n"
512 "%d objects in %d directories\n"
513 "%d NAND pages\n",nObjects, nDirectories, nPages);