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));
180 t.byteCount = nBytes;
183 yaffs_CalcTagsECC(&t);
184 yaffs_LoadTagsIntoSpare(&s,&t);
185 yaffs_CalcECC(data,&s);
189 return write(outFile,&s,sizeof(yaffs_Spare));
193 int write_object_header(int objId, yaffs_ObjectType t, struct stat *s, int parent, const char *name, int equivalentObj, const char * alias)
198 yaffs_ObjectHeader *oh = (yaffs_ObjectHeader *)bytes;
200 memset(bytes,0xff,512);
204 oh->parentObjectId = parent;
206 strncpy(oh->name,name,YAFFS_MAX_NAME_LENGTH);
208 if(t != YAFFS_OBJECT_TYPE_HARDLINK)
210 oh->st_mode = s->st_mode;
211 oh->st_uid = s->st_uid;
212 oh->st_gid = s->st_uid;
213 oh->st_atime = s->st_atime;
214 oh->st_mtime = s->st_mtime;
215 oh->st_ctime = s->st_ctime;
216 oh->st_rdev = s->st_rdev;
219 if(t == YAFFS_OBJECT_TYPE_FILE)
221 oh->fileSize = s->st_size;
224 if(t == YAFFS_OBJECT_TYPE_HARDLINK)
226 oh->equivalentObjectId = equivalentObj;
229 if(t == YAFFS_OBJECT_TYPE_SYMLINK)
231 strncpy(oh->alias,alias,YAFFS_MAX_ALIAS_LENGTH);
234 return write_chunk(bytes,objId,0,0xffff);
239 int process_directory(int parent, const char *path)
243 struct dirent *entry;
251 while((entry = readdir(dir)) != NULL)
254 /* Ignore . and .. */
255 if(strcmp(entry->d_name,".") &&
256 strcmp(entry->d_name,".."))
263 sprintf(full_name,"%s/%s",path,entry->d_name);
265 lstat(full_name,&stats);
267 if(S_ISLNK(stats.st_mode) ||
268 S_ISREG(stats.st_mode) ||
269 S_ISDIR(stats.st_mode) ||
270 S_ISFIFO(stats.st_mode) ||
271 S_ISBLK(stats.st_mode) ||
272 S_ISCHR(stats.st_mode) ||
273 S_ISSOCK(stats.st_mode))
279 printf("Object %d, %s is a ",newObj,full_name);
281 /* We're going to create an object for it */
282 if((equivalentObj = find_obj_in_list(stats.st_dev, stats.st_ino)) > 0)
284 /* we need to make a hard link */
285 printf("hard link to object %d\n",equivalentObj);
286 error = write_object_header(newObj, YAFFS_OBJECT_TYPE_HARDLINK, &stats, parent, entry->d_name, equivalentObj, NULL);
291 add_obj_to_list(stats.st_dev,stats.st_ino,newObj);
293 if(S_ISLNK(stats.st_mode))
298 memset(symname,0,500);
300 readlink(full_name,symname,499);
302 printf("symlink to \"%s\"\n",symname);
303 error = write_object_header(newObj, YAFFS_OBJECT_TYPE_SYMLINK, &stats, parent, entry->d_name, -1, symname);
306 else if(S_ISREG(stats.st_mode))
309 error = write_object_header(newObj, YAFFS_OBJECT_TYPE_FILE, &stats, parent, entry->d_name, -1, NULL);
318 h = open(full_name,O_RDONLY);
321 memset(bytes,0xff,512);
322 while((nBytes = read(h,bytes,512)) > 0)
325 write_chunk(bytes,newObj,chunk,nBytes);
326 memset(bytes,0xff,512);
331 printf("%d data chunks written\n",chunk);
335 perror("Error opening file");
342 else if(S_ISSOCK(stats.st_mode))
345 error = write_object_header(newObj, YAFFS_OBJECT_TYPE_SPECIAL, &stats, parent, entry->d_name, -1, NULL);
347 else if(S_ISFIFO(stats.st_mode))
350 error = write_object_header(newObj, YAFFS_OBJECT_TYPE_SPECIAL, &stats, parent, entry->d_name, -1, NULL);
352 else if(S_ISCHR(stats.st_mode))
354 printf("character device\n");
355 error = write_object_header(newObj, YAFFS_OBJECT_TYPE_SPECIAL, &stats, parent, entry->d_name, -1, NULL);
357 else if(S_ISBLK(stats.st_mode))
359 printf("block device\n");
360 error = write_object_header(newObj, YAFFS_OBJECT_TYPE_SPECIAL, &stats, parent, entry->d_name, -1, NULL);
362 else if(S_ISDIR(stats.st_mode))
364 printf("directory\n");
365 error = write_object_header(newObj, YAFFS_OBJECT_TYPE_DIRECTORY, &stats, parent, entry->d_name, -1, NULL);
366 process_directory(1,full_name);
372 printf(" we don't handle this type\n");
383 int main(int argc, char *argv[])
387 printf("mkyaffsimage: image building tool for YAFFS built "__DATE__"\n");
391 printf("usage: mkyaffsimage dir image_file\n");
392 printf(" dir the directory tree to be converted\n");
393 printf(" image_file the ouput file to hold the image\n");
397 if(stat(argv[1],&stats) < 0)
399 printf("Could not stat %s\n",argv[1]);
403 if(!S_ISDIR(stats.st_mode))
405 printf(" %s is not a directory\n",argv[1]);
409 outFile = open(argv[2],O_CREAT | O_TRUNC | O_WRONLY, S_IREAD | S_IWRITE);
414 printf("Could not open output file %s\n",argv[2]);
418 printf("Processing directory %s into image file %s\n",argv[1],argv[2]);
420 error = process_directory(YAFFS_OBJECTID_ROOT,argv[1]);
426 perror("operation incomplete");
431 printf("Operation complete.\n"
432 "%d objects in %d directories\n"
433 "%d NAND pages\n",nObjects, nDirectories, nPages);