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.
22 * mkyaffs2image hacks by NCB
29 #include <sys/types.h>
34 #include "yaffs_ecc.h"
35 #include "yaffs_guts.h"
37 #include "yaffs_tagsvalidity.h"
38 #include "yaffs_packedtags2.h"
40 unsigned yaffs_traceMask=0;
42 #define MAX_OBJECTS 10000
44 #define chunkSize 2048
47 const char * mkyaffsimage_c_version = "$Id: mkyaffs2image.c,v 1.2 2005-12-13 00:34:58 tpoynor Exp $";
58 static objItem obj_list[MAX_OBJECTS];
60 static int obj_id = YAFFS_NOBJECT_BUCKETS + 1;
62 static int nObjects, nDirectories, nPages;
68 static int convert_endian = 0;
70 static int obj_compare(const void *a, const void * b)
77 if(oa->dev < ob->dev) return -1;
78 if(oa->dev > ob->dev) return 1;
79 if(oa->ino < ob->ino) return -1;
80 if(oa->ino > ob->ino) return 1;
86 static void add_obj_to_list(dev_t dev, ino_t ino, int obj)
88 if(n_obj < MAX_OBJECTS)
90 obj_list[n_obj].dev = dev;
91 obj_list[n_obj].ino = ino;
92 obj_list[n_obj].obj = obj;
94 qsort(obj_list,n_obj,sizeof(objItem),obj_compare);
99 // oops! not enough space in the object array
100 fprintf(stderr,"Not enough space in object array\n");
106 static int find_obj_in_list(dev_t dev, ino_t ino)
116 i = bsearch(&test,obj_list,n_obj,sizeof(objItem),obj_compare);
126 /* This little function converts a little endian tag to a big endian tag.
127 * NOTE: The tag is not usable after this other than calculating the CRC
130 static void little_to_big_endian(yaffs_Tags *tagsPtr)
133 yaffs_TagsUnion * tags = (yaffs_TagsUnion* )tagsPtr; // Work in bytes.
134 yaffs_TagsUnion temp;
136 memset(&temp, 0, sizeof(temp));
137 // Ick, I hate magic numbers.
138 temp.asBytes[0] = ((tags->asBytes[2] & 0x0F) << 4) | ((tags->asBytes[1] & 0xF0) >> 4);
139 temp.asBytes[1] = ((tags->asBytes[1] & 0x0F) << 4) | ((tags->asBytes[0] & 0xF0) >> 4);
140 temp.asBytes[2] = ((tags->asBytes[0] & 0x0F) << 4) | ((tags->asBytes[2] & 0x30) >> 2) | ((tags->asBytes[3] & 0xC0) >> 6);
141 temp.asBytes[3] = ((tags->asBytes[3] & 0x3F) << 2) | ((tags->asBytes[2] & 0xC0) >> 6);
142 temp.asBytes[4] = ((tags->asBytes[6] & 0x03) << 6) | ((tags->asBytes[5] & 0xFC) >> 2);
143 temp.asBytes[5] = ((tags->asBytes[5] & 0x03) << 6) | ((tags->asBytes[4] & 0xFC) >> 2);
144 temp.asBytes[6] = ((tags->asBytes[4] & 0x03) << 6) | (tags->asBytes[7] & 0x3F);
145 temp.asBytes[7] = (tags->asBytes[6] & 0xFC) | ((tags->asBytes[7] & 0xC0) >> 6);
148 tags->asBytes[0] = temp.asBytes[0];
149 tags->asBytes[1] = temp.asBytes[1];
150 tags->asBytes[2] = temp.asBytes[2];
151 tags->asBytes[3] = temp.asBytes[3];
152 tags->asBytes[4] = temp.asBytes[4];
153 tags->asBytes[5] = temp.asBytes[5];
154 tags->asBytes[6] = temp.asBytes[6];
155 tags->asBytes[7] = temp.asBytes[7];
159 static int write_chunk(__u8 *data, __u32 objId, __u32 chunkId, __u32 nBytes)
161 yaffs_ExtendedTags t;
162 yaffs_PackedTags2 pt;
164 error = write(outFile,data,chunkSize);
165 if(error < 0) return error;
167 yaffs_InitialiseTags(&t);
170 // t.serialNumber = 0;
171 t.serialNumber = 1; // **CHECK**
172 t.byteCount = nBytes;
175 t.sequenceNumber = YAFFS_LOWEST_SEQUENCE_NUMBER;
177 // added NCB **CHECK**
182 little_to_big_endian(&t);
187 yaffs_PackTags2(&pt,&t);
189 // return write(outFile,&pt,sizeof(yaffs_PackedTags2));
190 return write(outFile,&pt,spareSize);
194 #define SWAP32(x) ((((x) & 0x000000FF) << 24) | \
195 (((x) & 0x0000FF00) << 8 ) | \
196 (((x) & 0x00FF0000) >> 8 ) | \
197 (((x) & 0xFF000000) >> 24))
199 #define SWAP16(x) ((((x) & 0x00FF) << 8) | \
200 (((x) & 0xFF00) >> 8))
202 // This one is easier, since the types are more standard. No funky shifts here.
203 static void object_header_little_to_big_endian(yaffs_ObjectHeader* oh)
205 oh->type = SWAP32(oh->type); // GCC makes enums 32 bits.
206 oh->parentObjectId = SWAP32(oh->parentObjectId); // int
207 oh->sum__NoLongerUsed = SWAP16(oh->sum__NoLongerUsed); // __u16 - Not used, but done for completeness.
208 // name = skip. Char array. Not swapped.
209 oh->yst_mode = SWAP32(oh->yst_mode);
210 #ifdef CONFIG_YAFFS_WINCE // WinCE doesn't implement this, but we need to just in case.
211 // In fact, WinCE would be *THE* place where this would be an issue!
212 oh->notForWinCE[0] = SWAP32(oh->notForWinCE[0]);
213 oh->notForWinCE[1] = SWAP32(oh->notForWinCE[1]);
214 oh->notForWinCE[2] = SWAP32(oh->notForWinCE[2]);
215 oh->notForWinCE[3] = SWAP32(oh->notForWinCE[3]);
216 oh->notForWinCE[4] = SWAP32(oh->notForWinCE[4]);
219 oh->yst_uid = SWAP32(oh->yst_uid);
220 oh->yst_gid = SWAP32(oh->yst_gid);
221 oh->yst_atime = SWAP32(oh->yst_atime);
222 oh->yst_mtime = SWAP32(oh->yst_mtime);
223 oh->yst_ctime = SWAP32(oh->yst_ctime);
226 oh->fileSize = SWAP32(oh->fileSize); // Aiee. An int... signed, at that!
227 oh->equivalentObjectId = SWAP32(oh->equivalentObjectId);
228 // alias - char array.
229 oh->yst_rdev = SWAP32(oh->yst_rdev);
231 #ifdef CONFIG_YAFFS_WINCE
232 oh->win_ctime[0] = SWAP32(oh->win_ctime[0]);
233 oh->win_ctime[1] = SWAP32(oh->win_ctime[1]);
234 oh->win_atime[0] = SWAP32(oh->win_atime[0]);
235 oh->win_atime[1] = SWAP32(oh->win_atime[1]);
236 oh->win_mtime[0] = SWAP32(oh->win_mtime[0]);
237 oh->win_mtime[1] = SWAP32(oh->win_mtime[1]);
238 oh->roomToGrow[0] = SWAP32(oh->roomToGrow[0]);
239 oh->roomToGrow[1] = SWAP32(oh->roomToGrow[1]);
240 oh->roomToGrow[2] = SWAP32(oh->roomToGrow[2]);
241 oh->roomToGrow[3] = SWAP32(oh->roomToGrow[3]);
242 oh->roomToGrow[4] = SWAP32(oh->roomToGrow[4]);
243 oh->roomToGrow[5] = SWAP32(oh->roomToGrow[5]);
245 oh->roomToGrow[0] = SWAP32(oh->roomToGrow[0]);
246 oh->roomToGrow[1] = SWAP32(oh->roomToGrow[1]);
247 oh->roomToGrow[2] = SWAP32(oh->roomToGrow[2]);
248 oh->roomToGrow[3] = SWAP32(oh->roomToGrow[3]);
249 oh->roomToGrow[4] = SWAP32(oh->roomToGrow[4]);
250 oh->roomToGrow[5] = SWAP32(oh->roomToGrow[5]);
251 oh->roomToGrow[6] = SWAP32(oh->roomToGrow[6]);
252 oh->roomToGrow[7] = SWAP32(oh->roomToGrow[7]);
253 oh->roomToGrow[8] = SWAP32(oh->roomToGrow[8]);
254 oh->roomToGrow[9] = SWAP32(oh->roomToGrow[9]);
255 oh->roomToGrow[10] = SWAP32(oh->roomToGrow[10]);
256 oh->roomToGrow[11] = SWAP32(oh->roomToGrow[11]);
260 static int write_object_header(int objId, yaffs_ObjectType t, struct stat *s, int parent, const char *name, int equivalentObj, const char * alias)
262 __u8 bytes[chunkSize];
265 yaffs_ObjectHeader *oh = (yaffs_ObjectHeader *)bytes;
267 memset(bytes,0xff,sizeof(bytes));
271 oh->parentObjectId = parent;
273 strncpy(oh->name,name,YAFFS_MAX_NAME_LENGTH);
276 if(t != YAFFS_OBJECT_TYPE_HARDLINK)
278 oh->yst_mode = s->st_mode;
279 oh->yst_uid = s->st_uid;
280 // NCB 12/9/02 oh->yst_gid = s->yst_uid;
281 oh->yst_gid = s->st_gid;
282 oh->yst_atime = s->st_atime;
283 oh->yst_mtime = s->st_mtime;
284 oh->yst_ctime = s->st_ctime;
285 oh->yst_rdev = s->st_rdev;
288 if(t == YAFFS_OBJECT_TYPE_FILE)
290 oh->fileSize = s->st_size;
293 if(t == YAFFS_OBJECT_TYPE_HARDLINK)
295 oh->equivalentObjectId = equivalentObj;
298 if(t == YAFFS_OBJECT_TYPE_SYMLINK)
300 strncpy(oh->alias,alias,YAFFS_MAX_ALIAS_LENGTH);
305 object_header_little_to_big_endian(oh);
308 return write_chunk(bytes,objId,0,0xffff);
313 static int process_directory(int parent, const char *path)
317 struct dirent *entry;
325 while((entry = readdir(dir)) != NULL)
328 /* Ignore . and .. */
329 if(strcmp(entry->d_name,".") &&
330 strcmp(entry->d_name,".."))
337 sprintf(full_name,"%s/%s",path,entry->d_name);
339 lstat(full_name,&stats);
341 if(S_ISLNK(stats.st_mode) ||
342 S_ISREG(stats.st_mode) ||
343 S_ISDIR(stats.st_mode) ||
344 S_ISFIFO(stats.st_mode) ||
345 S_ISBLK(stats.st_mode) ||
346 S_ISCHR(stats.st_mode) ||
347 S_ISSOCK(stats.st_mode))
353 printf("Object %d, %s is a ",newObj,full_name);
355 /* We're going to create an object for it */
356 if((equivalentObj = find_obj_in_list(stats.st_dev, stats.st_ino)) > 0)
358 /* we need to make a hard link */
359 printf("hard link to object %d\n",equivalentObj);
360 error = write_object_header(newObj, YAFFS_OBJECT_TYPE_HARDLINK, &stats, parent, entry->d_name, equivalentObj, NULL);
365 add_obj_to_list(stats.st_dev,stats.st_ino,newObj);
367 if(S_ISLNK(stats.st_mode))
372 memset(symname,0, sizeof(symname));
374 readlink(full_name,symname,sizeof(symname) -1);
376 printf("symlink to \"%s\"\n",symname);
377 error = write_object_header(newObj, YAFFS_OBJECT_TYPE_SYMLINK, &stats, parent, entry->d_name, -1, symname);
380 else if(S_ISREG(stats.st_mode))
383 error = write_object_header(newObj, YAFFS_OBJECT_TYPE_FILE, &stats, parent, entry->d_name, -1, NULL);
388 __u8 bytes[chunkSize];
392 h = open(full_name,O_RDONLY);
395 memset(bytes,0xff,sizeof(bytes));
396 while((nBytes = read(h,bytes,sizeof(bytes))) > 0)
399 write_chunk(bytes,newObj,chunk,nBytes);
400 memset(bytes,0xff,sizeof(bytes));
405 printf("%d data chunks written\n",chunk);
409 perror("Error opening file");
416 else if(S_ISSOCK(stats.st_mode))
419 error = write_object_header(newObj, YAFFS_OBJECT_TYPE_SPECIAL, &stats, parent, entry->d_name, -1, NULL);
421 else if(S_ISFIFO(stats.st_mode))
424 error = write_object_header(newObj, YAFFS_OBJECT_TYPE_SPECIAL, &stats, parent, entry->d_name, -1, NULL);
426 else if(S_ISCHR(stats.st_mode))
428 printf("character device\n");
429 error = write_object_header(newObj, YAFFS_OBJECT_TYPE_SPECIAL, &stats, parent, entry->d_name, -1, NULL);
431 else if(S_ISBLK(stats.st_mode))
433 printf("block device\n");
434 error = write_object_header(newObj, YAFFS_OBJECT_TYPE_SPECIAL, &stats, parent, entry->d_name, -1, NULL);
436 else if(S_ISDIR(stats.st_mode))
438 printf("directory\n");
439 error = write_object_header(newObj, YAFFS_OBJECT_TYPE_DIRECTORY, &stats, parent, entry->d_name, -1, NULL);
440 // NCB modified 10/9/2001 process_directory(1,full_name);
441 process_directory(newObj,full_name);
447 printf(" we don't handle this type\n");
458 int main(int argc, char *argv[])
462 printf("mkyaffs2image: image building tool for YAFFS2 built "__DATE__"\n");
466 printf("usage: mkyaffs2image dir image_file [convert]\n");
467 printf(" dir the directory tree to be converted\n");
468 printf(" image_file the output file to hold the image\n");
469 printf(" 'convert' produce a big-endian image from a little-endian machine\n");
473 if ((argc == 4) && (!strncmp(argv[3], "convert", strlen("convert"))))
478 if(stat(argv[1],&stats) < 0)
480 printf("Could not stat %s\n",argv[1]);
484 if(!S_ISDIR(stats.st_mode))
486 printf(" %s is not a directory\n",argv[1]);
490 outFile = open(argv[2],O_CREAT | O_TRUNC | O_WRONLY, S_IREAD | S_IWRITE);
495 printf("Could not open output file %s\n",argv[2]);
499 printf("Processing directory %s into image file %s\n",argv[1],argv[2]);
500 error = write_object_header(1, YAFFS_OBJECT_TYPE_DIRECTORY, &stats, 1,"", -1, NULL);
502 error = process_directory(YAFFS_OBJECTID_ROOT,argv[1]);
508 perror("operation incomplete");
513 printf("Operation complete.\n"
514 "%d objects in %d directories\n"
515 "%d NAND pages\n",nObjects, nDirectories, nPages);