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
25 #include <sys/types.h>
31 #include "yaffs_guts.h"
34 #define MAX_OBJECTS 10000
36 const char * mkyaffsimage_c_version = "$Id: mkyaffsimage.c,v 1.6 2002-12-13 00:13:06 charles Exp $";
38 // External functions for ECC on data
39 void nand_calculate_ecc (const u_char *dat, u_char *ecc_code);
40 int nand_correct_data (u_char *dat, u_char *read_ecc, u_char *calc_ecc);
51 static objItem obj_list[MAX_OBJECTS];
53 static int obj_id = YAFFS_NOBJECT_BUCKETS + 1;
55 static int nObjects, nDirectories, nPages;
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 nand_calculate_ecc (data , spare->ecc1);
139 nand_calculate_ecc (&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;
152 for(i = 0; i < 8; i++)
154 // NCB modified 20-9-02 for(j = 1; j &0x7f; j<<=1)
155 for(j = 1; j &0xff; j<<=1)
169 static void yaffs_LoadTagsIntoSpare(yaffs_Spare *sparePtr, yaffs_Tags *tagsPtr)
171 yaffs_TagsUnion *tu = (yaffs_TagsUnion *)tagsPtr;
173 //yaffs_CalcTagsECC(tagsPtr);
175 sparePtr->tagByte0 = tu->asBytes[0];
176 sparePtr->tagByte1 = tu->asBytes[1];
177 sparePtr->tagByte2 = tu->asBytes[2];
178 sparePtr->tagByte3 = tu->asBytes[3];
179 sparePtr->tagByte4 = tu->asBytes[4];
180 sparePtr->tagByte5 = tu->asBytes[5];
181 sparePtr->tagByte6 = tu->asBytes[6];
182 sparePtr->tagByte7 = tu->asBytes[7];
187 static int write_chunk(__u8 *data, __u32 objId, __u32 chunkId, __u32 nBytes)
192 error = write(outFile,data,512);
193 if(error < 0) return error;
195 memset(&t,0xff,sizeof (yaffs_Tags));
196 memset(&s,0xff,sizeof (yaffs_Spare));
200 t.byteCount = nBytes;
203 yaffs_CalcTagsECC(&t);
204 yaffs_LoadTagsIntoSpare(&s,&t);
205 yaffs_CalcECC(data,&s);
209 return write(outFile,&s,sizeof(yaffs_Spare));
213 static int write_object_header(int objId, yaffs_ObjectType t, struct stat *s, int parent, const char *name, int equivalentObj, const char * alias)
218 yaffs_ObjectHeader *oh = (yaffs_ObjectHeader *)bytes;
220 memset(bytes,0xff,512);
224 oh->parentObjectId = parent;
226 strncpy(oh->name,name,YAFFS_MAX_NAME_LENGTH);
229 if(t != YAFFS_OBJECT_TYPE_HARDLINK)
231 oh->st_mode = s->st_mode;
232 oh->st_uid = s->st_uid;
233 // NCB 12/9/02 oh->st_gid = s->st_uid;
234 oh->st_gid = s->st_gid;
235 oh->st_atime = s->st_atime;
236 oh->st_mtime = s->st_mtime;
237 oh->st_ctime = s->st_ctime;
238 oh->st_rdev = s->st_rdev;
241 if(t == YAFFS_OBJECT_TYPE_FILE)
243 oh->fileSize = s->st_size;
246 if(t == YAFFS_OBJECT_TYPE_HARDLINK)
248 oh->equivalentObjectId = equivalentObj;
251 if(t == YAFFS_OBJECT_TYPE_SYMLINK)
253 strncpy(oh->alias,alias,YAFFS_MAX_ALIAS_LENGTH);
256 return write_chunk(bytes,objId,0,0xffff);
261 static int process_directory(int parent, const char *path)
265 struct dirent *entry;
273 while((entry = readdir(dir)) != NULL)
276 /* Ignore . and .. */
277 if(strcmp(entry->d_name,".") &&
278 strcmp(entry->d_name,".."))
285 sprintf(full_name,"%s/%s",path,entry->d_name);
287 lstat(full_name,&stats);
289 if(S_ISLNK(stats.st_mode) ||
290 S_ISREG(stats.st_mode) ||
291 S_ISDIR(stats.st_mode) ||
292 S_ISFIFO(stats.st_mode) ||
293 S_ISBLK(stats.st_mode) ||
294 S_ISCHR(stats.st_mode) ||
295 S_ISSOCK(stats.st_mode))
301 printf("Object %d, %s is a ",newObj,full_name);
303 /* We're going to create an object for it */
304 if((equivalentObj = find_obj_in_list(stats.st_dev, stats.st_ino)) > 0)
306 /* we need to make a hard link */
307 printf("hard link to object %d\n",equivalentObj);
308 error = write_object_header(newObj, YAFFS_OBJECT_TYPE_HARDLINK, &stats, parent, entry->d_name, equivalentObj, NULL);
313 add_obj_to_list(stats.st_dev,stats.st_ino,newObj);
315 if(S_ISLNK(stats.st_mode))
320 memset(symname,0, sizeof(symname));
322 readlink(full_name,symname,sizeof(symname) -1);
324 printf("symlink to \"%s\"\n",symname);
325 error = write_object_header(newObj, YAFFS_OBJECT_TYPE_SYMLINK, &stats, parent, entry->d_name, -1, symname);
328 else if(S_ISREG(stats.st_mode))
331 error = write_object_header(newObj, YAFFS_OBJECT_TYPE_FILE, &stats, parent, entry->d_name, -1, NULL);
340 h = open(full_name,O_RDONLY);
343 memset(bytes,0xff,512);
344 while((nBytes = read(h,bytes,512)) > 0)
347 write_chunk(bytes,newObj,chunk,nBytes);
348 memset(bytes,0xff,512);
353 printf("%d data chunks written\n",chunk);
357 perror("Error opening file");
364 else if(S_ISSOCK(stats.st_mode))
367 error = write_object_header(newObj, YAFFS_OBJECT_TYPE_SPECIAL, &stats, parent, entry->d_name, -1, NULL);
369 else if(S_ISFIFO(stats.st_mode))
372 error = write_object_header(newObj, YAFFS_OBJECT_TYPE_SPECIAL, &stats, parent, entry->d_name, -1, NULL);
374 else if(S_ISCHR(stats.st_mode))
376 printf("character device\n");
377 error = write_object_header(newObj, YAFFS_OBJECT_TYPE_SPECIAL, &stats, parent, entry->d_name, -1, NULL);
379 else if(S_ISBLK(stats.st_mode))
381 printf("block device\n");
382 error = write_object_header(newObj, YAFFS_OBJECT_TYPE_SPECIAL, &stats, parent, entry->d_name, -1, NULL);
384 else if(S_ISDIR(stats.st_mode))
386 printf("directory\n");
387 error = write_object_header(newObj, YAFFS_OBJECT_TYPE_DIRECTORY, &stats, parent, entry->d_name, -1, NULL);
388 // NCB modified 10/9/2001 process_directory(1,full_name);
389 process_directory(newObj,full_name);
395 printf(" we don't handle this type\n");
406 int main(int argc, char *argv[])
410 printf("mkyaffsimage: image building tool for YAFFS built "__DATE__"\n");
414 printf("usage: mkyaffsimage dir image_file\n");
415 printf(" dir the directory tree to be converted\n");
416 printf(" image_file the output file to hold the image\n");
420 if(stat(argv[1],&stats) < 0)
422 printf("Could not stat %s\n",argv[1]);
426 if(!S_ISDIR(stats.st_mode))
428 printf(" %s is not a directory\n",argv[1]);
432 outFile = open(argv[2],O_CREAT | O_TRUNC | O_WRONLY, S_IREAD | S_IWRITE);
437 printf("Could not open output file %s\n",argv[2]);
441 printf("Processing directory %s into image file %s\n",argv[1],argv[2]);
442 error = write_object_header(1, YAFFS_OBJECT_TYPE_DIRECTORY, &stats, 1,"", -1, NULL);
444 error = process_directory(YAFFS_OBJECTID_ROOT,argv[1]);
450 perror("operation incomplete");
455 printf("Operation complete.\n"
456 "%d objects in %d directories\n"
457 "%d NAND pages\n",nObjects, nDirectories, nPages);