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>
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 $";
41 // External functions for ECC on data
42 void nand_calculate_ecc (const u_char *dat, u_char *ecc_code);
43 int nand_correct_data (u_char *dat, u_char *read_ecc, u_char *calc_ecc);
54 static objItem obj_list[MAX_OBJECTS];
56 static int obj_id = YAFFS_NOBJECT_BUCKETS + 1;
58 static int nObjects, nDirectories, nPages;
64 static int convert_endian = 0;
66 static int obj_compare(const void *a, const void * b)
73 if(oa->dev < ob->dev) return -1;
74 if(oa->dev > ob->dev) return 1;
75 if(oa->ino < ob->ino) return -1;
76 if(oa->ino > ob->ino) return 1;
82 static void add_obj_to_list(dev_t dev, ino_t ino, int obj)
84 if(n_obj < MAX_OBJECTS)
86 obj_list[n_obj].dev = dev;
87 obj_list[n_obj].ino = ino;
88 obj_list[n_obj].obj = obj;
90 qsort(obj_list,n_obj,sizeof(objItem),obj_compare);
95 // oops! not enough space in the object array
96 fprintf(stderr,"Not enough space in object array\n");
102 static int find_obj_in_list(dev_t dev, ino_t ino)
112 i = bsearch(&test,obj_list,n_obj,sizeof(objItem),obj_compare);
122 // NCB added 10/9/2002
123 static __u16 yaffs_CalcNameSum(const char *name)
128 __u8 *bname = (__u8 *)name;
140 static void yaffs_CalcECC(const __u8 *data, yaffs_Spare *spare)
142 nand_calculate_ecc (data , spare->ecc1);
143 nand_calculate_ecc (&data[256] , spare->ecc2);
146 static void yaffs_CalcTagsECC(yaffs_Tags *tags)
148 // Todo don't do anything yet. Need to calculate ecc
149 unsigned char *b = ((yaffs_TagsUnion *)tags)->asBytes;
161 // Because we're in "munged tag" mode, we have to clear it manually
166 for(i = 0; i < 8; i++)
168 // NCB modified 20-9-02 for(j = 1; j &0x7f; j<<=1)
169 for(j = 1; j &0xff; j<<=1)
186 // We have to munge the ECC again.
187 b[6] |= ((ecc >> 6) & 0x3F);
188 b[7] |= ((ecc & 0x3F) << 2);
191 static void yaffs_LoadTagsIntoSpare(yaffs_Spare *sparePtr, yaffs_Tags *tagsPtr)
193 yaffs_TagsUnion *tu = (yaffs_TagsUnion *)tagsPtr;
195 //yaffs_CalcTagsECC(tagsPtr);
197 sparePtr->tagByte0 = tu->asBytes[0];
198 sparePtr->tagByte1 = tu->asBytes[1];
199 sparePtr->tagByte2 = tu->asBytes[2];
200 sparePtr->tagByte3 = tu->asBytes[3];
201 sparePtr->tagByte4 = tu->asBytes[4];
202 sparePtr->tagByte5 = tu->asBytes[5];
203 sparePtr->tagByte6 = tu->asBytes[6];
204 sparePtr->tagByte7 = tu->asBytes[7];
207 /* This little function converts a little endian tag to a big endian tag.
208 * NOTE: The tag is not usable after this other than calculating the CRC
211 static void little_to_big_endian(yaffs_Tags *tagsPtr)
213 yaffs_TagsUnion * tags = (yaffs_TagsUnion* )tagsPtr; // Work in bytes.
214 yaffs_TagsUnion temp;
216 memset(&temp, 0, sizeof(temp));
217 // Ick, I hate magic numbers.
218 temp.asBytes[0] = ((tags->asBytes[2] & 0x0F) << 4) | ((tags->asBytes[1] & 0xF0) >> 4);
219 temp.asBytes[1] = ((tags->asBytes[1] & 0x0F) << 4) | ((tags->asBytes[0] & 0xF0) >> 4);
220 temp.asBytes[2] = ((tags->asBytes[0] & 0x0F) << 4) | ((tags->asBytes[2] & 0x30) >> 2) | ((tags->asBytes[3] & 0xC0) >> 6);
221 temp.asBytes[3] = ((tags->asBytes[3] & 0x3F) << 2) | ((tags->asBytes[2] & 0xC0) >> 6);
222 temp.asBytes[4] = ((tags->asBytes[6] & 0x03) << 6) | ((tags->asBytes[5] & 0xFC) >> 2);
223 temp.asBytes[5] = ((tags->asBytes[5] & 0x03) << 6) | ((tags->asBytes[4] & 0xFC) >> 2);
224 temp.asBytes[6] = ((tags->asBytes[4] & 0x03) << 6) | (tags->asBytes[7] & 0x3F);
225 temp.asBytes[7] = (tags->asBytes[6] & 0xFC) | ((tags->asBytes[7] & 0xC0) >> 6);
228 tags->asBytes[0] = temp.asBytes[0];
229 tags->asBytes[1] = temp.asBytes[1];
230 tags->asBytes[2] = temp.asBytes[2];
231 tags->asBytes[3] = temp.asBytes[3];
232 tags->asBytes[4] = temp.asBytes[4];
233 tags->asBytes[5] = temp.asBytes[5];
234 tags->asBytes[6] = temp.asBytes[6];
235 tags->asBytes[7] = temp.asBytes[7];
238 static int write_chunk(__u8 *data, __u32 objId, __u32 chunkId, __u32 nBytes)
243 error = write(outFile,data,512);
244 if(error < 0) return error;
246 memset(&t,0xff,sizeof (yaffs_Tags));
247 memset(&s,0xff,sizeof (yaffs_Spare));
251 t.byteCount = nBytes;
256 little_to_big_endian(&t);
259 yaffs_CalcTagsECC(&t);
260 yaffs_LoadTagsIntoSpare(&s,&t);
261 yaffs_CalcECC(data,&s);
265 return write(outFile,&s,sizeof(yaffs_Spare));
269 #define SWAP32(x) ((((x) & 0x000000FF) << 24) | \
270 (((x) & 0x0000FF00) << 8 ) | \
271 (((x) & 0x00FF0000) >> 8 ) | \
272 (((x) & 0xFF000000) >> 24))
274 #define SWAP16(x) ((((x) & 0x00FF) << 8) | \
275 (((x) & 0xFF00) >> 8))
277 // This one is easier, since the types are more standard. No funky shifts here.
278 static void object_header_little_to_big_endian(yaffs_ObjectHeader* oh)
280 oh->type = SWAP32(oh->type); // GCC makes enums 32 bits.
281 oh->parentObjectId = SWAP32(oh->parentObjectId); // int
282 oh->sum__NoLongerUsed = SWAP16(oh->sum__NoLongerUsed); // __u16 - Not used, but done for completeness.
283 // name = skip. Char array. Not swapped.
284 oh->st_mode = SWAP32(oh->st_mode);
285 #ifdef CONFIG_YAFFS_WINCE // WinCE doesn't implement this, but we need to just in case.
286 // In fact, WinCE would be *THE* place where this would be an issue!
287 oh->notForWinCE[0] = SWAP32(oh->notForWinCE[0]);
288 oh->notForWinCE[1] = SWAP32(oh->notForWinCE[1]);
289 oh->notForWinCE[2] = SWAP32(oh->notForWinCE[2]);
290 oh->notForWinCE[3] = SWAP32(oh->notForWinCE[3]);
291 oh->notForWinCE[4] = SWAP32(oh->notForWinCE[4]);
294 oh->st_uid = SWAP32(oh->st_uid);
295 oh->st_gid = SWAP32(oh->st_gid);
296 oh->st_atime = SWAP32(oh->st_atime);
297 oh->st_mtime = SWAP32(oh->st_mtime);
298 oh->st_ctime = SWAP32(oh->st_ctime);
301 oh->fileSize = SWAP32(oh->fileSize); // Aiee. An int... signed, at that!
302 oh->equivalentObjectId = SWAP32(oh->equivalentObjectId);
303 // alias - char array.
304 oh->st_rdev = SWAP32(oh->st_rdev);
306 #ifdef CONFIG_YAFFS_WINCE
307 oh->win_ctime[0] = SWAP32(oh->win_ctime[0]);
308 oh->win_ctime[1] = SWAP32(oh->win_ctime[1]);
309 oh->win_atime[0] = SWAP32(oh->win_atime[0]);
310 oh->win_atime[1] = SWAP32(oh->win_atime[1]);
311 oh->win_mtime[0] = SWAP32(oh->win_mtime[0]);
312 oh->win_mtime[1] = SWAP32(oh->win_mtime[1]);
313 oh->roomToGrow[0] = SWAP32(oh->roomToGrow[0]);
314 oh->roomToGrow[1] = SWAP32(oh->roomToGrow[1]);
315 oh->roomToGrow[2] = SWAP32(oh->roomToGrow[2]);
316 oh->roomToGrow[3] = SWAP32(oh->roomToGrow[3]);
317 oh->roomToGrow[4] = SWAP32(oh->roomToGrow[4]);
318 oh->roomToGrow[5] = SWAP32(oh->roomToGrow[5]);
320 oh->roomToGrow[0] = SWAP32(oh->roomToGrow[0]);
321 oh->roomToGrow[1] = SWAP32(oh->roomToGrow[1]);
322 oh->roomToGrow[2] = SWAP32(oh->roomToGrow[2]);
323 oh->roomToGrow[3] = SWAP32(oh->roomToGrow[3]);
324 oh->roomToGrow[4] = SWAP32(oh->roomToGrow[4]);
325 oh->roomToGrow[5] = SWAP32(oh->roomToGrow[5]);
326 oh->roomToGrow[6] = SWAP32(oh->roomToGrow[6]);
327 oh->roomToGrow[7] = SWAP32(oh->roomToGrow[7]);
328 oh->roomToGrow[8] = SWAP32(oh->roomToGrow[8]);
329 oh->roomToGrow[9] = SWAP32(oh->roomToGrow[9]);
330 oh->roomToGrow[10] = SWAP32(oh->roomToGrow[10]);
331 oh->roomToGrow[11] = SWAP32(oh->roomToGrow[11]);
335 static int write_object_header(int objId, yaffs_ObjectType t, struct stat *s, int parent, const char *name, int equivalentObj, const char * alias)
340 yaffs_ObjectHeader *oh = (yaffs_ObjectHeader *)bytes;
342 memset(bytes,0xff,512);
346 oh->parentObjectId = parent;
348 strncpy(oh->name,name,YAFFS_MAX_NAME_LENGTH);
351 if(t != YAFFS_OBJECT_TYPE_HARDLINK)
353 oh->st_mode = s->st_mode;
354 oh->st_uid = s->st_uid;
355 // NCB 12/9/02 oh->st_gid = s->st_uid;
356 oh->st_gid = s->st_gid;
357 oh->st_atime = s->st_atime;
358 oh->st_mtime = s->st_mtime;
359 oh->st_ctime = s->st_ctime;
360 oh->st_rdev = s->st_rdev;
363 if(t == YAFFS_OBJECT_TYPE_FILE)
365 oh->fileSize = s->st_size;
368 if(t == YAFFS_OBJECT_TYPE_HARDLINK)
370 oh->equivalentObjectId = equivalentObj;
373 if(t == YAFFS_OBJECT_TYPE_SYMLINK)
375 strncpy(oh->alias,alias,YAFFS_MAX_ALIAS_LENGTH);
380 object_header_little_to_big_endian(oh);
383 return write_chunk(bytes,objId,0,0xffff);
388 static int process_directory(int parent, const char *path)
392 struct dirent *entry;
400 while((entry = readdir(dir)) != NULL)
403 /* Ignore . and .. */
404 if(strcmp(entry->d_name,".") &&
405 strcmp(entry->d_name,".."))
412 sprintf(full_name,"%s/%s",path,entry->d_name);
414 lstat(full_name,&stats);
416 if(S_ISLNK(stats.st_mode) ||
417 S_ISREG(stats.st_mode) ||
418 S_ISDIR(stats.st_mode) ||
419 S_ISFIFO(stats.st_mode) ||
420 S_ISBLK(stats.st_mode) ||
421 S_ISCHR(stats.st_mode) ||
422 S_ISSOCK(stats.st_mode))
428 printf("Object %d, %s is a ",newObj,full_name);
430 /* We're going to create an object for it */
431 if((equivalentObj = find_obj_in_list(stats.st_dev, stats.st_ino)) > 0)
433 /* we need to make a hard link */
434 printf("hard link to object %d\n",equivalentObj);
435 error = write_object_header(newObj, YAFFS_OBJECT_TYPE_HARDLINK, &stats, parent, entry->d_name, equivalentObj, NULL);
440 add_obj_to_list(stats.st_dev,stats.st_ino,newObj);
442 if(S_ISLNK(stats.st_mode))
447 memset(symname,0, sizeof(symname));
449 readlink(full_name,symname,sizeof(symname) -1);
451 printf("symlink to \"%s\"\n",symname);
452 error = write_object_header(newObj, YAFFS_OBJECT_TYPE_SYMLINK, &stats, parent, entry->d_name, -1, symname);
455 else if(S_ISREG(stats.st_mode))
458 error = write_object_header(newObj, YAFFS_OBJECT_TYPE_FILE, &stats, parent, entry->d_name, -1, NULL);
467 h = open(full_name,O_RDONLY);
470 memset(bytes,0xff,512);
471 while((nBytes = read(h,bytes,512)) > 0)
474 write_chunk(bytes,newObj,chunk,nBytes);
475 memset(bytes,0xff,512);
480 printf("%d data chunks written\n",chunk);
484 perror("Error opening file");
491 else if(S_ISSOCK(stats.st_mode))
494 error = write_object_header(newObj, YAFFS_OBJECT_TYPE_SPECIAL, &stats, parent, entry->d_name, -1, NULL);
496 else if(S_ISFIFO(stats.st_mode))
499 error = write_object_header(newObj, YAFFS_OBJECT_TYPE_SPECIAL, &stats, parent, entry->d_name, -1, NULL);
501 else if(S_ISCHR(stats.st_mode))
503 printf("character device\n");
504 error = write_object_header(newObj, YAFFS_OBJECT_TYPE_SPECIAL, &stats, parent, entry->d_name, -1, NULL);
506 else if(S_ISBLK(stats.st_mode))
508 printf("block device\n");
509 error = write_object_header(newObj, YAFFS_OBJECT_TYPE_SPECIAL, &stats, parent, entry->d_name, -1, NULL);
511 else if(S_ISDIR(stats.st_mode))
513 printf("directory\n");
514 error = write_object_header(newObj, YAFFS_OBJECT_TYPE_DIRECTORY, &stats, parent, entry->d_name, -1, NULL);
515 // NCB modified 10/9/2001 process_directory(1,full_name);
516 process_directory(newObj,full_name);
522 printf(" we don't handle this type\n");
533 int main(int argc, char *argv[])
537 printf("mkyaffsimage: image building tool for YAFFS built "__DATE__"\n");
541 printf("usage: mkyaffsimage dir image_file [convert]\n");
542 printf(" dir the directory tree to be converted\n");
543 printf(" image_file the output file to hold the image\n");
544 printf(" 'convert' produce a big-endian image from a little-endian machine\n");
548 if ((argc == 4) && (!strncmp(argv[3], "convert", strlen("convert"))))
553 if(stat(argv[1],&stats) < 0)
555 printf("Could not stat %s\n",argv[1]);
559 if(!S_ISDIR(stats.st_mode))
561 printf(" %s is not a directory\n",argv[1]);
565 outFile = open(argv[2],O_CREAT | O_TRUNC | O_WRONLY, S_IREAD | S_IWRITE);
570 printf("Could not open output file %s\n",argv[2]);
574 printf("Processing directory %s into image file %s\n",argv[1],argv[2]);
575 error = write_object_header(1, YAFFS_OBJECT_TYPE_DIRECTORY, &stats, 1,"", -1, NULL);
577 error = process_directory(YAFFS_OBJECTID_ROOT,argv[1]);
583 perror("operation incomplete");
588 printf("Operation complete.\n"
589 "%d objects in %d directories\n"
590 "%d NAND pages\n",nObjects, nDirectories, nPages);