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.3 2002-09-27 20:50:50 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 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 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 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 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 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 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 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);
228 // NCB added 10/9/2001
229 oh->sum = yaffs_CalcNameSum(oh->name);
231 if(t != YAFFS_OBJECT_TYPE_HARDLINK)
233 oh->st_mode = s->st_mode;
234 oh->st_uid = s->st_uid;
235 // NCB 12/9/02 oh->st_gid = s->st_uid;
236 oh->st_gid = s->st_gid;
237 oh->st_atime = s->st_atime;
238 oh->st_mtime = s->st_mtime;
239 oh->st_ctime = s->st_ctime;
240 oh->st_rdev = s->st_rdev;
243 if(t == YAFFS_OBJECT_TYPE_FILE)
245 oh->fileSize = s->st_size;
248 if(t == YAFFS_OBJECT_TYPE_HARDLINK)
250 oh->equivalentObjectId = equivalentObj;
253 if(t == YAFFS_OBJECT_TYPE_SYMLINK)
255 strncpy(oh->alias,alias,YAFFS_MAX_ALIAS_LENGTH);
258 return write_chunk(bytes,objId,0,0xffff);
263 int process_directory(int parent, const char *path)
267 struct dirent *entry;
275 while((entry = readdir(dir)) != NULL)
278 /* Ignore . and .. */
279 if(strcmp(entry->d_name,".") &&
280 strcmp(entry->d_name,".."))
287 sprintf(full_name,"%s/%s",path,entry->d_name);
289 lstat(full_name,&stats);
291 if(S_ISLNK(stats.st_mode) ||
292 S_ISREG(stats.st_mode) ||
293 S_ISDIR(stats.st_mode) ||
294 S_ISFIFO(stats.st_mode) ||
295 S_ISBLK(stats.st_mode) ||
296 S_ISCHR(stats.st_mode) ||
297 S_ISSOCK(stats.st_mode))
303 printf("Object %d, %s is a ",newObj,full_name);
305 /* We're going to create an object for it */
306 if((equivalentObj = find_obj_in_list(stats.st_dev, stats.st_ino)) > 0)
308 /* we need to make a hard link */
309 printf("hard link to object %d\n",equivalentObj);
310 error = write_object_header(newObj, YAFFS_OBJECT_TYPE_HARDLINK, &stats, parent, entry->d_name, equivalentObj, NULL);
315 add_obj_to_list(stats.st_dev,stats.st_ino,newObj);
317 if(S_ISLNK(stats.st_mode))
322 memset(symname,0,500);
324 readlink(full_name,symname,499);
326 printf("symlink to \"%s\"\n",symname);
327 error = write_object_header(newObj, YAFFS_OBJECT_TYPE_SYMLINK, &stats, parent, entry->d_name, -1, symname);
330 else if(S_ISREG(stats.st_mode))
333 error = write_object_header(newObj, YAFFS_OBJECT_TYPE_FILE, &stats, parent, entry->d_name, -1, NULL);
342 h = open(full_name,O_RDONLY);
345 memset(bytes,0xff,512);
346 while((nBytes = read(h,bytes,512)) > 0)
349 write_chunk(bytes,newObj,chunk,nBytes);
350 memset(bytes,0xff,512);
355 printf("%d data chunks written\n",chunk);
359 perror("Error opening file");
366 else if(S_ISSOCK(stats.st_mode))
369 error = write_object_header(newObj, YAFFS_OBJECT_TYPE_SPECIAL, &stats, parent, entry->d_name, -1, NULL);
371 else if(S_ISFIFO(stats.st_mode))
374 error = write_object_header(newObj, YAFFS_OBJECT_TYPE_SPECIAL, &stats, parent, entry->d_name, -1, NULL);
376 else if(S_ISCHR(stats.st_mode))
378 printf("character device\n");
379 error = write_object_header(newObj, YAFFS_OBJECT_TYPE_SPECIAL, &stats, parent, entry->d_name, -1, NULL);
381 else if(S_ISBLK(stats.st_mode))
383 printf("block device\n");
384 error = write_object_header(newObj, YAFFS_OBJECT_TYPE_SPECIAL, &stats, parent, entry->d_name, -1, NULL);
386 else if(S_ISDIR(stats.st_mode))
388 printf("directory\n");
389 error = write_object_header(newObj, YAFFS_OBJECT_TYPE_DIRECTORY, &stats, parent, entry->d_name, -1, NULL);
390 // NCB modified 10/9/2001 process_directory(1,full_name);
391 process_directory(newObj,full_name);
397 printf(" we don't handle this type\n");
408 int main(int argc, char *argv[])
412 printf("mkyaffsimage: image building tool for YAFFS built "__DATE__"\n");
416 printf("usage: mkyaffsimage dir image_file\n");
417 printf(" dir the directory tree to be converted\n");
418 printf(" image_file the ouput file to hold the image\n");
422 if(stat(argv[1],&stats) < 0)
424 printf("Could not stat %s\n",argv[1]);
428 if(!S_ISDIR(stats.st_mode))
430 printf(" %s is not a directory\n",argv[1]);
434 outFile = open(argv[2],O_CREAT | O_TRUNC | O_WRONLY, S_IREAD | S_IWRITE);
439 printf("Could not open output file %s\n",argv[2]);
443 printf("Processing directory %s into image file %s\n",argv[1],argv[2]);
445 error = process_directory(YAFFS_OBJECTID_ROOT,argv[1]);
451 perror("operation incomplete");
456 printf("Operation complete.\n"
457 "%d objects in %d directories\n"
458 "%d NAND pages\n",nObjects, nDirectories, nPages);