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
11 * This program is free software; you can redistribute it and/or modify
12 * it under the terms of the GNU General Public License version 2 as
13 * published by the Free Software Foundation.
20 * Makes a YAFFS file system image that can be used to load up a file system.
26 #include <sys/types.h>
31 #include "yaffs_ecc.h"
32 #include "yaffs_guts.h"
35 #define MAX_OBJECTS 10000
37 const char * mkyaffsimage_c_version = "$Id: mkyaffsimage.c,v 1.7 2003/07/16 03:00:48 charles Exp $";
48 static objItem obj_list[MAX_OBJECTS];
50 static int obj_id = YAFFS_NOBJECT_BUCKETS + 1;
52 static int nObjects, nDirectories, nPages;
58 static int convert_endian = 0;
60 static int obj_compare(const void *a, const void * b)
67 if(oa->dev < ob->dev) return -1;
68 if(oa->dev > ob->dev) return 1;
69 if(oa->ino < ob->ino) return -1;
70 if(oa->ino > ob->ino) return 1;
76 static void add_obj_to_list(dev_t dev, ino_t ino, int obj)
78 if(n_obj < MAX_OBJECTS)
80 obj_list[n_obj].dev = dev;
81 obj_list[n_obj].ino = ino;
82 obj_list[n_obj].obj = obj;
84 qsort(obj_list,n_obj,sizeof(objItem),obj_compare);
89 // oops! not enough space in the object array
90 fprintf(stderr,"Not enough space in object array\n");
96 static int find_obj_in_list(dev_t dev, ino_t ino)
106 i = bsearch(&test,obj_list,n_obj,sizeof(objItem),obj_compare);
116 // NCB added 10/9/2002
117 static __u16 yaffs_CalcNameSum(const char *name)
122 __u8 *bname = (__u8 *)name;
134 static void yaffs_CalcECC(const __u8 *data, yaffs_Spare *spare)
136 yaffs_ECCCalculate(data , spare->ecc1);
137 yaffs_ECCCalculate(&data[256] , spare->ecc2);
140 static void yaffs_CalcTagsECC(yaffs_Tags *tags)
142 // Todo don't do anything yet. Need to calculate ecc
143 unsigned char *b = ((yaffs_TagsUnion *)tags)->asBytes;
155 // Because we're in "munged tag" mode, we have to clear it manually
160 for(i = 0; i < 8; i++)
162 // NCB modified 20-9-02 for(j = 1; j &0x7f; j<<=1)
163 for(j = 1; j &0xff; j<<=1)
180 // We have to munge the ECC again.
181 b[6] |= ((ecc >> 6) & 0x3F);
182 b[7] |= ((ecc & 0x3F) << 2);
185 static void yaffs_LoadTagsIntoSpare(yaffs_Spare *sparePtr, yaffs_Tags *tagsPtr)
187 yaffs_TagsUnion *tu = (yaffs_TagsUnion *)tagsPtr;
189 //yaffs_CalcTagsECC(tagsPtr);
191 sparePtr->tagByte0 = tu->asBytes[0];
192 sparePtr->tagByte1 = tu->asBytes[1];
193 sparePtr->tagByte2 = tu->asBytes[2];
194 sparePtr->tagByte3 = tu->asBytes[3];
195 sparePtr->tagByte4 = tu->asBytes[4];
196 sparePtr->tagByte5 = tu->asBytes[5];
197 sparePtr->tagByte6 = tu->asBytes[6];
198 sparePtr->tagByte7 = tu->asBytes[7];
201 /* This little function converts a little endian tag to a big endian tag.
202 * NOTE: The tag is not usable after this other than calculating the CRC
205 static void little_to_big_endian(yaffs_Tags *tagsPtr)
207 yaffs_TagsUnion * tags = (yaffs_TagsUnion* )tagsPtr; // Work in bytes.
208 yaffs_TagsUnion temp;
210 memset(&temp, 0, sizeof(temp));
211 // Ick, I hate magic numbers.
212 temp.asBytes[0] = ((tags->asBytes[2] & 0x0F) << 4) | ((tags->asBytes[1] & 0xF0) >> 4);
213 temp.asBytes[1] = ((tags->asBytes[1] & 0x0F) << 4) | ((tags->asBytes[0] & 0xF0) >> 4);
214 temp.asBytes[2] = ((tags->asBytes[0] & 0x0F) << 4) | ((tags->asBytes[2] & 0x30) >> 2) | ((tags->asBytes[3] & 0xC0) >> 6);
215 temp.asBytes[3] = ((tags->asBytes[3] & 0x3F) << 2) | ((tags->asBytes[2] & 0xC0) >> 6);
216 temp.asBytes[4] = ((tags->asBytes[6] & 0x03) << 6) | ((tags->asBytes[5] & 0xFC) >> 2);
217 temp.asBytes[5] = ((tags->asBytes[5] & 0x03) << 6) | ((tags->asBytes[4] & 0xFC) >> 2);
218 temp.asBytes[6] = ((tags->asBytes[4] & 0x03) << 6) | (tags->asBytes[7] & 0x3F);
219 temp.asBytes[7] = (tags->asBytes[6] & 0xFC) | ((tags->asBytes[7] & 0xC0) >> 6);
222 tags->asBytes[0] = temp.asBytes[0];
223 tags->asBytes[1] = temp.asBytes[1];
224 tags->asBytes[2] = temp.asBytes[2];
225 tags->asBytes[3] = temp.asBytes[3];
226 tags->asBytes[4] = temp.asBytes[4];
227 tags->asBytes[5] = temp.asBytes[5];
228 tags->asBytes[6] = temp.asBytes[6];
229 tags->asBytes[7] = temp.asBytes[7];
232 static int write_chunk(__u8 *data, __u32 objId, __u32 chunkId, __u32 nBytes)
237 error = write(outFile,data,512);
238 if(error < 0) return error;
240 memset(&t,0xff,sizeof (yaffs_Tags));
241 memset(&s,0xff,sizeof (yaffs_Spare));
245 t.byteCount = nBytes;
250 little_to_big_endian(&t);
253 yaffs_CalcTagsECC(&t);
254 yaffs_LoadTagsIntoSpare(&s,&t);
255 yaffs_CalcECC(data,&s);
259 return write(outFile,&s,sizeof(yaffs_Spare));
263 #define SWAP32(x) ((((x) & 0x000000FF) << 24) | \
264 (((x) & 0x0000FF00) << 8 ) | \
265 (((x) & 0x00FF0000) >> 8 ) | \
266 (((x) & 0xFF000000) >> 24))
268 #define SWAP16(x) ((((x) & 0x00FF) << 8) | \
269 (((x) & 0xFF00) >> 8))
271 // This one is easier, since the types are more standard. No funky shifts here.
272 static void object_header_little_to_big_endian(yaffs_ObjectHeader* oh)
274 oh->type = SWAP32(oh->type); // GCC makes enums 32 bits.
275 oh->parentObjectId = SWAP32(oh->parentObjectId); // int
276 oh->sum__NoLongerUsed = SWAP16(oh->sum__NoLongerUsed); // __u16 - Not used, but done for completeness.
277 // name = skip. Char array. Not swapped.
278 oh->yst_mode = SWAP32(oh->yst_mode);
279 #ifdef CONFIG_YAFFS_WINCE // WinCE doesn't implement this, but we need to just in case.
280 // In fact, WinCE would be *THE* place where this would be an issue!
281 oh->notForWinCE[0] = SWAP32(oh->notForWinCE[0]);
282 oh->notForWinCE[1] = SWAP32(oh->notForWinCE[1]);
283 oh->notForWinCE[2] = SWAP32(oh->notForWinCE[2]);
284 oh->notForWinCE[3] = SWAP32(oh->notForWinCE[3]);
285 oh->notForWinCE[4] = SWAP32(oh->notForWinCE[4]);
288 oh->yst_uid = SWAP32(oh->yst_uid);
289 oh->yst_gid = SWAP32(oh->yst_gid);
290 oh->yst_atime = SWAP32(oh->yst_atime);
291 oh->yst_mtime = SWAP32(oh->yst_mtime);
292 oh->yst_ctime = SWAP32(oh->yst_ctime);
295 oh->fileSize = SWAP32(oh->fileSize); // Aiee. An int... signed, at that!
296 oh->equivalentObjectId = SWAP32(oh->equivalentObjectId);
297 // alias - char array.
298 oh->yst_rdev = SWAP32(oh->yst_rdev);
300 #ifdef CONFIG_YAFFS_WINCE
301 oh->win_ctime[0] = SWAP32(oh->win_ctime[0]);
302 oh->win_ctime[1] = SWAP32(oh->win_ctime[1]);
303 oh->win_atime[0] = SWAP32(oh->win_atime[0]);
304 oh->win_atime[1] = SWAP32(oh->win_atime[1]);
305 oh->win_mtime[0] = SWAP32(oh->win_mtime[0]);
306 oh->win_mtime[1] = SWAP32(oh->win_mtime[1]);
307 oh->roomToGrow[0] = SWAP32(oh->roomToGrow[0]);
308 oh->roomToGrow[1] = SWAP32(oh->roomToGrow[1]);
309 oh->roomToGrow[2] = SWAP32(oh->roomToGrow[2]);
310 oh->roomToGrow[3] = SWAP32(oh->roomToGrow[3]);
311 oh->roomToGrow[4] = SWAP32(oh->roomToGrow[4]);
312 oh->roomToGrow[5] = SWAP32(oh->roomToGrow[5]);
314 oh->roomToGrow[0] = SWAP32(oh->roomToGrow[0]);
315 oh->roomToGrow[1] = SWAP32(oh->roomToGrow[1]);
316 oh->roomToGrow[2] = SWAP32(oh->roomToGrow[2]);
317 oh->roomToGrow[3] = SWAP32(oh->roomToGrow[3]);
318 oh->roomToGrow[4] = SWAP32(oh->roomToGrow[4]);
319 oh->roomToGrow[5] = SWAP32(oh->roomToGrow[5]);
320 oh->roomToGrow[6] = SWAP32(oh->roomToGrow[6]);
321 oh->roomToGrow[7] = SWAP32(oh->roomToGrow[7]);
322 oh->roomToGrow[8] = SWAP32(oh->roomToGrow[8]);
323 oh->roomToGrow[9] = SWAP32(oh->roomToGrow[9]);
324 oh->roomToGrow[10] = SWAP32(oh->roomToGrow[10]);
325 oh->roomToGrow[11] = SWAP32(oh->roomToGrow[11]);
329 static int write_object_header(int objId, yaffs_ObjectType t, struct stat *s, int parent, const char *name, int equivalentObj, const char * alias)
334 yaffs_ObjectHeader *oh = (yaffs_ObjectHeader *)bytes;
336 memset(bytes,0xff,512);
340 oh->parentObjectId = parent;
342 strncpy(oh->name,name,YAFFS_MAX_NAME_LENGTH);
345 if(t != YAFFS_OBJECT_TYPE_HARDLINK)
347 oh->yst_mode = s->st_mode;
348 oh->yst_uid = s->st_uid;
349 // NCB 12/9/02 oh->yst_gid = s->yst_uid;
350 oh->yst_gid = s->st_gid;
351 oh->yst_atime = s->st_atime;
352 oh->yst_mtime = s->st_mtime;
353 oh->yst_ctime = s->st_ctime;
354 oh->yst_rdev = s->st_rdev;
357 if(t == YAFFS_OBJECT_TYPE_FILE)
359 oh->fileSize = s->st_size;
362 if(t == YAFFS_OBJECT_TYPE_HARDLINK)
364 oh->equivalentObjectId = equivalentObj;
367 if(t == YAFFS_OBJECT_TYPE_SYMLINK)
369 strncpy(oh->alias,alias,YAFFS_MAX_ALIAS_LENGTH);
374 object_header_little_to_big_endian(oh);
377 return write_chunk(bytes,objId,0,0xffff);
382 static int process_directory(int parent, const char *path)
386 struct dirent *entry;
394 while((entry = readdir(dir)) != NULL)
397 /* Ignore . and .. */
398 if(strcmp(entry->d_name,".") &&
399 strcmp(entry->d_name,".."))
406 sprintf(full_name,"%s/%s",path,entry->d_name);
408 lstat(full_name,&stats);
410 if(S_ISLNK(stats.st_mode) ||
411 S_ISREG(stats.st_mode) ||
412 S_ISDIR(stats.st_mode) ||
413 S_ISFIFO(stats.st_mode) ||
414 S_ISBLK(stats.st_mode) ||
415 S_ISCHR(stats.st_mode) ||
416 S_ISSOCK(stats.st_mode))
422 printf("Object %d, %s is a ",newObj,full_name);
424 /* We're going to create an object for it */
425 if((equivalentObj = find_obj_in_list(stats.st_dev, stats.st_ino)) > 0)
427 /* we need to make a hard link */
428 printf("hard link to object %d\n",equivalentObj);
429 error = write_object_header(newObj, YAFFS_OBJECT_TYPE_HARDLINK, &stats, parent, entry->d_name, equivalentObj, NULL);
434 add_obj_to_list(stats.st_dev,stats.st_ino,newObj);
436 if(S_ISLNK(stats.st_mode))
441 memset(symname,0, sizeof(symname));
443 readlink(full_name,symname,sizeof(symname) -1);
445 printf("symlink to \"%s\"\n",symname);
446 error = write_object_header(newObj, YAFFS_OBJECT_TYPE_SYMLINK, &stats, parent, entry->d_name, -1, symname);
449 else if(S_ISREG(stats.st_mode))
452 error = write_object_header(newObj, YAFFS_OBJECT_TYPE_FILE, &stats, parent, entry->d_name, -1, NULL);
461 h = open(full_name,O_RDONLY);
464 memset(bytes,0xff,512);
465 while((nBytes = read(h,bytes,512)) > 0)
468 write_chunk(bytes,newObj,chunk,nBytes);
469 memset(bytes,0xff,512);
474 printf("%d data chunks written\n",chunk);
478 perror("Error opening file");
485 else if(S_ISSOCK(stats.st_mode))
488 error = write_object_header(newObj, YAFFS_OBJECT_TYPE_SPECIAL, &stats, parent, entry->d_name, -1, NULL);
490 else if(S_ISFIFO(stats.st_mode))
493 error = write_object_header(newObj, YAFFS_OBJECT_TYPE_SPECIAL, &stats, parent, entry->d_name, -1, NULL);
495 else if(S_ISCHR(stats.st_mode))
497 printf("character device\n");
498 error = write_object_header(newObj, YAFFS_OBJECT_TYPE_SPECIAL, &stats, parent, entry->d_name, -1, NULL);
500 else if(S_ISBLK(stats.st_mode))
502 printf("block device\n");
503 error = write_object_header(newObj, YAFFS_OBJECT_TYPE_SPECIAL, &stats, parent, entry->d_name, -1, NULL);
505 else if(S_ISDIR(stats.st_mode))
507 printf("directory\n");
508 error = write_object_header(newObj, YAFFS_OBJECT_TYPE_DIRECTORY, &stats, parent, entry->d_name, -1, NULL);
509 // NCB modified 10/9/2001 process_directory(1,full_name);
510 process_directory(newObj,full_name);
516 printf(" we don't handle this type\n");
527 int main(int argc, char *argv[])
531 printf("mkyaffsimage: image building tool for YAFFS built "__DATE__"\n");
535 printf("usage: mkyaffsimage dir image_file [convert]\n");
536 printf(" dir the directory tree to be converted\n");
537 printf(" image_file the output file to hold the image\n");
538 printf(" 'convert' produce a big-endian image from a little-endian machine\n");
542 if ((argc == 4) && (!strncmp(argv[3], "convert", strlen("convert"))))
547 if(stat(argv[1],&stats) < 0)
549 printf("Could not stat %s\n",argv[1]);
553 if(!S_ISDIR(stats.st_mode))
555 printf(" %s is not a directory\n",argv[1]);
559 outFile = open(argv[2],O_CREAT | O_TRUNC | O_WRONLY, S_IREAD | S_IWRITE);
564 printf("Could not open output file %s\n",argv[2]);
568 printf("Processing directory %s into image file %s\n",argv[1],argv[2]);
569 error = write_object_header(1, YAFFS_OBJECT_TYPE_DIRECTORY, &stats, 1,"", -1, NULL);
571 error = process_directory(YAFFS_OBJECTID_ROOT,argv[1]);
577 perror("operation incomplete");
582 printf("Operation complete.\n"
583 "%d objects in %d directories\n"
584 "%d NAND pages\n",nObjects, nDirectories, nPages);