2 * YAFFS: Yet another FFS. A NAND-flash specific file system.
6 * Makes a YAFFS file system image that can be used to load up a file system.
8 * Copyright (C) 2002 Aleph One Ltd.
9 * for Toby Churchill Ltd and Brightstar Engineering
11 * Created by Charles Manning <charles@aleph1.co.uk>
13 * This program is free software; you can redistribute it and/or modify
14 * it under the terms of the GNU General Public License version 2 as
15 * published by the Free Software Foundation.
18 * Nick Bane modifications flagged NCB
20 * Endian handling patches by James Ng.
28 #include <sys/types.h>
33 #include "yaffs_ecc.h"
34 #include "yaffs_guts.h"
37 #define MAX_OBJECTS 10000
39 const char * mkyaffsimage_c_version = "$Id: mkyaffsimage.c,v 1.7 2003/07/16 03:00:48 charles Exp $";
50 static objItem obj_list[MAX_OBJECTS];
52 static int obj_id = YAFFS_NOBJECT_BUCKETS + 1;
54 static int nObjects, nDirectories, nPages;
60 static int convert_endian = 0;
62 static int obj_compare(const void *a, const void * b)
69 if(oa->dev < ob->dev) return -1;
70 if(oa->dev > ob->dev) return 1;
71 if(oa->ino < ob->ino) return -1;
72 if(oa->ino > ob->ino) return 1;
78 static void add_obj_to_list(dev_t dev, ino_t ino, int obj)
80 if(n_obj < MAX_OBJECTS)
82 obj_list[n_obj].dev = dev;
83 obj_list[n_obj].ino = ino;
84 obj_list[n_obj].obj = obj;
86 qsort(obj_list,n_obj,sizeof(objItem),obj_compare);
91 // oops! not enough space in the object array
92 fprintf(stderr,"Not enough space in object array\n");
98 static int find_obj_in_list(dev_t dev, ino_t ino)
108 i = bsearch(&test,obj_list,n_obj,sizeof(objItem),obj_compare);
118 // NCB added 10/9/2002
119 static __u16 yaffs_CalcNameSum(const char *name)
124 __u8 *bname = (__u8 *)name;
136 static void yaffs_CalcECC(const __u8 *data, yaffs_Spare *spare)
138 yaffs_ECCCalculate(data , spare->ecc1);
139 yaffs_ECCCalculate(&data[256] , spare->ecc2);
142 static void yaffs_CalcTagsECC(yaffs_Tags *tags)
144 // Todo don't do anything yet. Need to calculate ecc
145 unsigned char *b = ((yaffs_TagsUnion *)tags)->asBytes;
157 // Because we're in "munged tag" mode, we have to clear it manually
162 for(i = 0; i < 8; i++)
164 // NCB modified 20-9-02 for(j = 1; j &0x7f; j<<=1)
165 for(j = 1; j &0xff; j<<=1)
182 // We have to munge the ECC again.
183 b[6] |= ((ecc >> 6) & 0x3F);
184 b[7] |= ((ecc & 0x3F) << 2);
187 static void yaffs_LoadTagsIntoSpare(yaffs_Spare *sparePtr, yaffs_Tags *tagsPtr)
189 yaffs_TagsUnion *tu = (yaffs_TagsUnion *)tagsPtr;
191 //yaffs_CalcTagsECC(tagsPtr);
193 sparePtr->tagByte0 = tu->asBytes[0];
194 sparePtr->tagByte1 = tu->asBytes[1];
195 sparePtr->tagByte2 = tu->asBytes[2];
196 sparePtr->tagByte3 = tu->asBytes[3];
197 sparePtr->tagByte4 = tu->asBytes[4];
198 sparePtr->tagByte5 = tu->asBytes[5];
199 sparePtr->tagByte6 = tu->asBytes[6];
200 sparePtr->tagByte7 = tu->asBytes[7];
203 /* This little function converts a little endian tag to a big endian tag.
204 * NOTE: The tag is not usable after this other than calculating the CRC
207 static void little_to_big_endian(yaffs_Tags *tagsPtr)
209 yaffs_TagsUnion * tags = (yaffs_TagsUnion* )tagsPtr; // Work in bytes.
210 yaffs_TagsUnion temp;
212 memset(&temp, 0, sizeof(temp));
213 // Ick, I hate magic numbers.
214 temp.asBytes[0] = ((tags->asBytes[2] & 0x0F) << 4) | ((tags->asBytes[1] & 0xF0) >> 4);
215 temp.asBytes[1] = ((tags->asBytes[1] & 0x0F) << 4) | ((tags->asBytes[0] & 0xF0) >> 4);
216 temp.asBytes[2] = ((tags->asBytes[0] & 0x0F) << 4) | ((tags->asBytes[2] & 0x30) >> 2) | ((tags->asBytes[3] & 0xC0) >> 6);
217 temp.asBytes[3] = ((tags->asBytes[3] & 0x3F) << 2) | ((tags->asBytes[2] & 0xC0) >> 6);
218 temp.asBytes[4] = ((tags->asBytes[6] & 0x03) << 6) | ((tags->asBytes[5] & 0xFC) >> 2);
219 temp.asBytes[5] = ((tags->asBytes[5] & 0x03) << 6) | ((tags->asBytes[4] & 0xFC) >> 2);
220 temp.asBytes[6] = ((tags->asBytes[4] & 0x03) << 6) | (tags->asBytes[7] & 0x3F);
221 temp.asBytes[7] = (tags->asBytes[6] & 0xFC) | ((tags->asBytes[7] & 0xC0) >> 6);
224 tags->asBytes[0] = temp.asBytes[0];
225 tags->asBytes[1] = temp.asBytes[1];
226 tags->asBytes[2] = temp.asBytes[2];
227 tags->asBytes[3] = temp.asBytes[3];
228 tags->asBytes[4] = temp.asBytes[4];
229 tags->asBytes[5] = temp.asBytes[5];
230 tags->asBytes[6] = temp.asBytes[6];
231 tags->asBytes[7] = temp.asBytes[7];
234 static int write_chunk(__u8 *data, __u32 objId, __u32 chunkId, __u32 nBytes)
239 error = write(outFile,data,512);
240 if(error < 0) return error;
242 memset(&t,0xff,sizeof (yaffs_Tags));
243 memset(&s,0xff,sizeof (yaffs_Spare));
247 t.byteCount = nBytes;
252 little_to_big_endian(&t);
255 yaffs_CalcTagsECC(&t);
256 yaffs_LoadTagsIntoSpare(&s,&t);
257 yaffs_CalcECC(data,&s);
261 return write(outFile,&s,sizeof(yaffs_Spare));
265 #define SWAP32(x) ((((x) & 0x000000FF) << 24) | \
266 (((x) & 0x0000FF00) << 8 ) | \
267 (((x) & 0x00FF0000) >> 8 ) | \
268 (((x) & 0xFF000000) >> 24))
270 #define SWAP16(x) ((((x) & 0x00FF) << 8) | \
271 (((x) & 0xFF00) >> 8))
273 // This one is easier, since the types are more standard. No funky shifts here.
274 static void object_header_little_to_big_endian(yaffs_ObjectHeader* oh)
276 oh->type = SWAP32(oh->type); // GCC makes enums 32 bits.
277 oh->parentObjectId = SWAP32(oh->parentObjectId); // int
278 oh->sum__NoLongerUsed = SWAP16(oh->sum__NoLongerUsed); // __u16 - Not used, but done for completeness.
279 // name = skip. Char array. Not swapped.
280 oh->yst_mode = SWAP32(oh->yst_mode);
281 #ifdef CONFIG_YAFFS_WINCE // WinCE doesn't implement this, but we need to just in case.
282 // In fact, WinCE would be *THE* place where this would be an issue!
283 oh->notForWinCE[0] = SWAP32(oh->notForWinCE[0]);
284 oh->notForWinCE[1] = SWAP32(oh->notForWinCE[1]);
285 oh->notForWinCE[2] = SWAP32(oh->notForWinCE[2]);
286 oh->notForWinCE[3] = SWAP32(oh->notForWinCE[3]);
287 oh->notForWinCE[4] = SWAP32(oh->notForWinCE[4]);
290 oh->yst_uid = SWAP32(oh->yst_uid);
291 oh->yst_gid = SWAP32(oh->yst_gid);
292 oh->yst_atime = SWAP32(oh->yst_atime);
293 oh->yst_mtime = SWAP32(oh->yst_mtime);
294 oh->yst_ctime = SWAP32(oh->yst_ctime);
297 oh->fileSize = SWAP32(oh->fileSize); // Aiee. An int... signed, at that!
298 oh->equivalentObjectId = SWAP32(oh->equivalentObjectId);
299 // alias - char array.
300 oh->yst_rdev = SWAP32(oh->yst_rdev);
302 #ifdef CONFIG_YAFFS_WINCE
303 oh->win_ctime[0] = SWAP32(oh->win_ctime[0]);
304 oh->win_ctime[1] = SWAP32(oh->win_ctime[1]);
305 oh->win_atime[0] = SWAP32(oh->win_atime[0]);
306 oh->win_atime[1] = SWAP32(oh->win_atime[1]);
307 oh->win_mtime[0] = SWAP32(oh->win_mtime[0]);
308 oh->win_mtime[1] = SWAP32(oh->win_mtime[1]);
309 oh->roomToGrow[0] = SWAP32(oh->roomToGrow[0]);
310 oh->roomToGrow[1] = SWAP32(oh->roomToGrow[1]);
311 oh->roomToGrow[2] = SWAP32(oh->roomToGrow[2]);
312 oh->roomToGrow[3] = SWAP32(oh->roomToGrow[3]);
313 oh->roomToGrow[4] = SWAP32(oh->roomToGrow[4]);
314 oh->roomToGrow[5] = SWAP32(oh->roomToGrow[5]);
316 oh->roomToGrow[0] = SWAP32(oh->roomToGrow[0]);
317 oh->roomToGrow[1] = SWAP32(oh->roomToGrow[1]);
318 oh->roomToGrow[2] = SWAP32(oh->roomToGrow[2]);
319 oh->roomToGrow[3] = SWAP32(oh->roomToGrow[3]);
320 oh->roomToGrow[4] = SWAP32(oh->roomToGrow[4]);
321 oh->roomToGrow[5] = SWAP32(oh->roomToGrow[5]);
322 oh->roomToGrow[6] = SWAP32(oh->roomToGrow[6]);
323 oh->roomToGrow[7] = SWAP32(oh->roomToGrow[7]);
324 oh->roomToGrow[8] = SWAP32(oh->roomToGrow[8]);
325 oh->roomToGrow[9] = SWAP32(oh->roomToGrow[9]);
326 oh->roomToGrow[10] = SWAP32(oh->roomToGrow[10]);
327 oh->roomToGrow[11] = SWAP32(oh->roomToGrow[11]);
331 static int write_object_header(int objId, yaffs_ObjectType t, struct stat *s, int parent, const char *name, int equivalentObj, const char * alias)
336 yaffs_ObjectHeader *oh = (yaffs_ObjectHeader *)bytes;
338 memset(bytes,0xff,512);
342 oh->parentObjectId = parent;
344 strncpy(oh->name,name,YAFFS_MAX_NAME_LENGTH);
347 if(t != YAFFS_OBJECT_TYPE_HARDLINK)
349 oh->yst_mode = s->st_mode;
350 oh->yst_uid = s->st_uid;
351 // NCB 12/9/02 oh->yst_gid = s->yst_uid;
352 oh->yst_gid = s->st_gid;
353 oh->yst_atime = s->st_atime;
354 oh->yst_mtime = s->st_mtime;
355 oh->yst_ctime = s->st_ctime;
356 oh->yst_rdev = s->st_rdev;
359 if(t == YAFFS_OBJECT_TYPE_FILE)
361 oh->fileSize = s->st_size;
364 if(t == YAFFS_OBJECT_TYPE_HARDLINK)
366 oh->equivalentObjectId = equivalentObj;
369 if(t == YAFFS_OBJECT_TYPE_SYMLINK)
371 strncpy(oh->alias,alias,YAFFS_MAX_ALIAS_LENGTH);
376 object_header_little_to_big_endian(oh);
379 return write_chunk(bytes,objId,0,0xffff);
384 static int process_directory(int parent, const char *path)
388 struct dirent *entry;
396 while((entry = readdir(dir)) != NULL)
399 /* Ignore . and .. */
400 if(strcmp(entry->d_name,".") &&
401 strcmp(entry->d_name,".."))
408 sprintf(full_name,"%s/%s",path,entry->d_name);
410 lstat(full_name,&stats);
412 if(S_ISLNK(stats.st_mode) ||
413 S_ISREG(stats.st_mode) ||
414 S_ISDIR(stats.st_mode) ||
415 S_ISFIFO(stats.st_mode) ||
416 S_ISBLK(stats.st_mode) ||
417 S_ISCHR(stats.st_mode) ||
418 S_ISSOCK(stats.st_mode))
424 printf("Object %d, %s is a ",newObj,full_name);
426 /* We're going to create an object for it */
427 if((equivalentObj = find_obj_in_list(stats.st_dev, stats.st_ino)) > 0)
429 /* we need to make a hard link */
430 printf("hard link to object %d\n",equivalentObj);
431 error = write_object_header(newObj, YAFFS_OBJECT_TYPE_HARDLINK, &stats, parent, entry->d_name, equivalentObj, NULL);
436 add_obj_to_list(stats.st_dev,stats.st_ino,newObj);
438 if(S_ISLNK(stats.st_mode))
443 memset(symname,0, sizeof(symname));
445 readlink(full_name,symname,sizeof(symname) -1);
447 printf("symlink to \"%s\"\n",symname);
448 error = write_object_header(newObj, YAFFS_OBJECT_TYPE_SYMLINK, &stats, parent, entry->d_name, -1, symname);
451 else if(S_ISREG(stats.st_mode))
454 error = write_object_header(newObj, YAFFS_OBJECT_TYPE_FILE, &stats, parent, entry->d_name, -1, NULL);
463 h = open(full_name,O_RDONLY);
466 memset(bytes,0xff,512);
467 while((nBytes = read(h,bytes,512)) > 0)
470 write_chunk(bytes,newObj,chunk,nBytes);
471 memset(bytes,0xff,512);
476 printf("%d data chunks written\n",chunk);
480 perror("Error opening file");
487 else if(S_ISSOCK(stats.st_mode))
490 error = write_object_header(newObj, YAFFS_OBJECT_TYPE_SPECIAL, &stats, parent, entry->d_name, -1, NULL);
492 else if(S_ISFIFO(stats.st_mode))
495 error = write_object_header(newObj, YAFFS_OBJECT_TYPE_SPECIAL, &stats, parent, entry->d_name, -1, NULL);
497 else if(S_ISCHR(stats.st_mode))
499 printf("character device\n");
500 error = write_object_header(newObj, YAFFS_OBJECT_TYPE_SPECIAL, &stats, parent, entry->d_name, -1, NULL);
502 else if(S_ISBLK(stats.st_mode))
504 printf("block device\n");
505 error = write_object_header(newObj, YAFFS_OBJECT_TYPE_SPECIAL, &stats, parent, entry->d_name, -1, NULL);
507 else if(S_ISDIR(stats.st_mode))
509 printf("directory\n");
510 error = write_object_header(newObj, YAFFS_OBJECT_TYPE_DIRECTORY, &stats, parent, entry->d_name, -1, NULL);
511 // NCB modified 10/9/2001 process_directory(1,full_name);
512 process_directory(newObj,full_name);
518 printf(" we don't handle this type\n");
529 int main(int argc, char *argv[])
533 printf("mkyaffsimage: image building tool for YAFFS built "__DATE__"\n");
537 printf("usage: mkyaffsimage dir image_file [convert]\n");
538 printf(" dir the directory tree to be converted\n");
539 printf(" image_file the output file to hold the image\n");
540 printf(" 'convert' produce a big-endian image from a little-endian machine\n");
544 if ((argc == 4) && (!strncmp(argv[3], "convert", strlen("convert"))))
549 if(stat(argv[1],&stats) < 0)
551 printf("Could not stat %s\n",argv[1]);
555 if(!S_ISDIR(stats.st_mode))
557 printf(" %s is not a directory\n",argv[1]);
561 outFile = open(argv[2],O_CREAT | O_TRUNC | O_WRONLY, S_IREAD | S_IWRITE);
566 printf("Could not open output file %s\n",argv[2]);
570 printf("Processing directory %s into image file %s\n",argv[1],argv[2]);
571 error = write_object_header(1, YAFFS_OBJECT_TYPE_DIRECTORY, &stats, 1,"", -1, NULL);
573 error = process_directory(YAFFS_OBJECTID_ROOT,argv[1]);
579 perror("operation incomplete");
584 printf("Operation complete.\n"
585 "%d objects in %d directories\n"
586 "%d NAND pages\n",nObjects, nDirectories, nPages);