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.
22 #include <sys/types.h>
28 #include "yaffs_guts.h"
31 #define MAX_OBJECTS 10000
33 // External functions for ECC on data
34 void nand_calculate_ecc (const u_char *dat, u_char *ecc_code);
35 int nand_correct_data (u_char *dat, u_char *read_ecc, u_char *calc_ecc);
49 static objItem obj_list[MAX_OBJECTS];
51 static int obj_id = YAFFS_NOBJECT_BUCKETS + 1;
53 static int nObjects, nDirectories, nPages;
60 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 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 int find_obj_in_list(dev_t dev, ino_t ino)
106 i = bsearch(&test,obj_list,n_obj,sizeof(objItem),obj_compare);
118 void yaffs_CalcECC(const __u8 *data, yaffs_Spare *spare)
120 nand_calculate_ecc (data , spare->ecc1);
121 nand_calculate_ecc (&data[256] , spare->ecc2);
124 void yaffs_CalcTagsECC(yaffs_Tags *tags)
126 // Todo don't do anything yet. Need to calculate ecc
127 unsigned char *b = ((yaffs_TagsUnion *)tags)->asBytes;
134 for(i = 0; i < 8; i++)
136 for(j = 1; j &0x7f; j<<=1)
150 static void yaffs_LoadTagsIntoSpare(yaffs_Spare *sparePtr, yaffs_Tags *tagsPtr)
152 yaffs_TagsUnion *tu = (yaffs_TagsUnion *)tagsPtr;
154 yaffs_CalcTagsECC(tagsPtr);
156 sparePtr->tagByte0 = tu->asBytes[0];
157 sparePtr->tagByte1 = tu->asBytes[1];
158 sparePtr->tagByte2 = tu->asBytes[2];
159 sparePtr->tagByte3 = tu->asBytes[3];
160 sparePtr->tagByte4 = tu->asBytes[4];
161 sparePtr->tagByte5 = tu->asBytes[5];
162 sparePtr->tagByte6 = tu->asBytes[6];
163 sparePtr->tagByte7 = tu->asBytes[7];
168 int write_chunk(__u8 *data, __u32 objId, __u32 chunkId, __u32 nBytes)
173 error = write(outFile,data,512);
174 if(error < 0) return error;
176 memset(&t,0xff,sizeof (yaffs_Tags));
177 memset(&s,0xff,sizeof (yaffs_Spare));
181 t.byteCount = nBytes;
184 yaffs_CalcTagsECC(&t);
185 yaffs_LoadTagsIntoSpare(&s,&t);
186 yaffs_CalcECC(data,&s);
190 return write(outFile,&s,sizeof(yaffs_Spare));
194 int write_object_header(int objId, yaffs_ObjectType t, struct stat *s, int parent, const char *name, int equivalentObj, const char * alias)
199 yaffs_ObjectHeader *oh = (yaffs_ObjectHeader *)bytes;
201 memset(bytes,0xff,512);
205 oh->parentObjectId = parent;
207 strncpy(oh->name,name,YAFFS_MAX_NAME_LENGTH);
209 if(t != YAFFS_OBJECT_TYPE_HARDLINK)
211 oh->st_mode = s->st_mode;
212 oh->st_uid = s->st_uid;
213 oh->st_gid = s->st_uid;
214 oh->st_atime = s->st_atime;
215 oh->st_mtime = s->st_mtime;
216 oh->st_ctime = s->st_ctime;
217 oh->st_rdev = s->st_rdev;
220 if(t == YAFFS_OBJECT_TYPE_FILE)
222 oh->fileSize = s->st_size;
225 if(t == YAFFS_OBJECT_TYPE_HARDLINK)
227 oh->equivalentObjectId = equivalentObj;
230 if(t == YAFFS_OBJECT_TYPE_SYMLINK)
232 strncpy(oh->alias,alias,YAFFS_MAX_ALIAS_LENGTH);
235 return write_chunk(bytes,objId,0,0xffff);
240 int process_directory(int parent, const char *path)
244 struct dirent *entry;
252 while((entry = readdir(dir)) != NULL)
255 /* Ignore . and .. */
256 if(strcmp(entry->d_name,".") &&
257 strcmp(entry->d_name,".."))
264 sprintf(full_name,"%s/%s",path,entry->d_name);
266 lstat(full_name,&stats);
268 if(S_ISLNK(stats.st_mode) ||
269 S_ISREG(stats.st_mode) ||
270 S_ISDIR(stats.st_mode) ||
271 S_ISFIFO(stats.st_mode) ||
272 S_ISBLK(stats.st_mode) ||
273 S_ISCHR(stats.st_mode) ||
274 S_ISSOCK(stats.st_mode))
280 printf("Object %d, %s is a ",newObj,full_name);
282 /* We're going to create an object for it */
283 if((equivalentObj = find_obj_in_list(stats.st_dev, stats.st_ino)) > 0)
285 /* we need to make a hard link */
286 printf("hard link to object %d\n",equivalentObj);
287 error = write_object_header(newObj, YAFFS_OBJECT_TYPE_HARDLINK, &stats, parent, entry->d_name, equivalentObj, NULL);
292 add_obj_to_list(stats.st_dev,stats.st_ino,newObj);
294 if(S_ISLNK(stats.st_mode))
299 memset(symname,0,500);
301 readlink(full_name,symname,499);
303 printf("symlink to \"%s\"\n",symname);
304 error = write_object_header(newObj, YAFFS_OBJECT_TYPE_SYMLINK, &stats, parent, entry->d_name, -1, symname);
307 else if(S_ISREG(stats.st_mode))
310 error = write_object_header(newObj, YAFFS_OBJECT_TYPE_FILE, &stats, parent, entry->d_name, -1, NULL);
319 h = open(full_name,O_RDONLY);
322 memset(bytes,0xff,512);
323 while((nBytes = read(h,bytes,512)) > 0)
326 write_chunk(bytes,newObj,chunk,nBytes);
327 memset(bytes,0xff,512);
332 printf("%d data chunks written\n",chunk);
336 perror("Error opening file");
343 else if(S_ISSOCK(stats.st_mode))
346 error = write_object_header(newObj, YAFFS_OBJECT_TYPE_SPECIAL, &stats, parent, entry->d_name, -1, NULL);
348 else if(S_ISFIFO(stats.st_mode))
351 error = write_object_header(newObj, YAFFS_OBJECT_TYPE_SPECIAL, &stats, parent, entry->d_name, -1, NULL);
353 else if(S_ISCHR(stats.st_mode))
355 printf("character device\n");
356 error = write_object_header(newObj, YAFFS_OBJECT_TYPE_SPECIAL, &stats, parent, entry->d_name, -1, NULL);
358 else if(S_ISBLK(stats.st_mode))
360 printf("block device\n");
361 error = write_object_header(newObj, YAFFS_OBJECT_TYPE_SPECIAL, &stats, parent, entry->d_name, -1, NULL);
363 else if(S_ISDIR(stats.st_mode))
365 printf("directory\n");
366 error = write_object_header(newObj, YAFFS_OBJECT_TYPE_DIRECTORY, &stats, parent, entry->d_name, -1, NULL);
367 process_directory(1,full_name);
373 printf(" we don't handle this type\n");
384 int main(int argc, char *argv[])
388 printf("mkyaffsimage: image building tool for YAFFS built "__DATE__"\n");
392 printf("usage: mkyaffsimage dir image_file\n");
393 printf(" dir the directory tree to be converted\n");
394 printf(" image_file the ouput file to hold the image\n");
398 if(stat(argv[1],&stats) < 0)
400 printf("Could not stat %s\n",argv[1]);
404 if(!S_ISDIR(stats.st_mode))
406 printf(" %s is not a directory\n",argv[1]);
410 outFile = open(argv[2],O_CREAT | O_TRUNC | O_WRONLY, S_IREAD | S_IWRITE);
415 printf("Could not open output file %s\n",argv[2]);
419 printf("Processing directory %s into image file %s\n",argv[1],argv[2]);
421 error = process_directory(YAFFS_OBJECTID_ROOT,argv[1]);
427 perror("operation incomplete");
432 printf("Operation complete.\n"
433 "%d objects in %d directories\n"
434 "%d NAND pages\n",nObjects, nDirectories, nPages);