2 * YAFFS: Yet Another Flash File System. A NAND-flash specific file system.
4 * Copyright (C) 2002-2007 Aleph One Ltd.
5 * for Toby Churchill Ltd and Brightstar Engineering
7 * Created by Charles Manning <charles@aleph1.co.uk>
8 * Nick Bane modifications flagged NCB
9 * Endian handling patches by James Ng.
10 * mkyaffs2image hacks by NCB
12 * This program is free software; you can redistribute it and/or modify
13 * it under the terms of the GNU General Public License version 2 as
14 * published by the Free Software Foundation.
21 * Makes a YAFFS2 file system image that can be used to load up a file system.
22 * Uses default Linux MTD layout - change if you need something different.
28 #include <sys/types.h>
33 #include "yaffs_ecc.h"
34 #include "yaffs_guts.h"
36 #include "yaffs_tagsvalidity.h"
37 #include "yaffs_packedtags2.h"
39 unsigned yaffs_traceMask=0;
41 #define MAX_OBJECTS 10000
43 #define chunkSize 2048
46 const char * mkyaffsimage_c_version = "$Id: mkyaffs2image.c,v 1.3 2007-02-12 16:55:27 wookey Exp $";
57 static objItem obj_list[MAX_OBJECTS];
59 static int obj_id = YAFFS_NOBJECT_BUCKETS + 1;
61 static int nObjects, nDirectories, nPages;
67 static int convert_endian = 0;
69 static int obj_compare(const void *a, const void * b)
76 if(oa->dev < ob->dev) return -1;
77 if(oa->dev > ob->dev) return 1;
78 if(oa->ino < ob->ino) return -1;
79 if(oa->ino > ob->ino) return 1;
85 static void add_obj_to_list(dev_t dev, ino_t ino, int obj)
87 if(n_obj < MAX_OBJECTS)
89 obj_list[n_obj].dev = dev;
90 obj_list[n_obj].ino = ino;
91 obj_list[n_obj].obj = obj;
93 qsort(obj_list,n_obj,sizeof(objItem),obj_compare);
98 // oops! not enough space in the object array
99 fprintf(stderr,"Not enough space in object array\n");
105 static int find_obj_in_list(dev_t dev, ino_t ino)
115 i = bsearch(&test,obj_list,n_obj,sizeof(objItem),obj_compare);
125 /* This little function converts a little endian tag to a big endian tag.
126 * NOTE: The tag is not usable after this other than calculating the CRC
129 static void little_to_big_endian(yaffs_Tags *tagsPtr)
132 yaffs_TagsUnion * tags = (yaffs_TagsUnion* )tagsPtr; // Work in bytes.
133 yaffs_TagsUnion temp;
135 memset(&temp, 0, sizeof(temp));
136 // Ick, I hate magic numbers.
137 temp.asBytes[0] = ((tags->asBytes[2] & 0x0F) << 4) | ((tags->asBytes[1] & 0xF0) >> 4);
138 temp.asBytes[1] = ((tags->asBytes[1] & 0x0F) << 4) | ((tags->asBytes[0] & 0xF0) >> 4);
139 temp.asBytes[2] = ((tags->asBytes[0] & 0x0F) << 4) | ((tags->asBytes[2] & 0x30) >> 2) | ((tags->asBytes[3] & 0xC0) >> 6);
140 temp.asBytes[3] = ((tags->asBytes[3] & 0x3F) << 2) | ((tags->asBytes[2] & 0xC0) >> 6);
141 temp.asBytes[4] = ((tags->asBytes[6] & 0x03) << 6) | ((tags->asBytes[5] & 0xFC) >> 2);
142 temp.asBytes[5] = ((tags->asBytes[5] & 0x03) << 6) | ((tags->asBytes[4] & 0xFC) >> 2);
143 temp.asBytes[6] = ((tags->asBytes[4] & 0x03) << 6) | (tags->asBytes[7] & 0x3F);
144 temp.asBytes[7] = (tags->asBytes[6] & 0xFC) | ((tags->asBytes[7] & 0xC0) >> 6);
147 tags->asBytes[0] = temp.asBytes[0];
148 tags->asBytes[1] = temp.asBytes[1];
149 tags->asBytes[2] = temp.asBytes[2];
150 tags->asBytes[3] = temp.asBytes[3];
151 tags->asBytes[4] = temp.asBytes[4];
152 tags->asBytes[5] = temp.asBytes[5];
153 tags->asBytes[6] = temp.asBytes[6];
154 tags->asBytes[7] = temp.asBytes[7];
158 static int write_chunk(__u8 *data, __u32 objId, __u32 chunkId, __u32 nBytes)
160 yaffs_ExtendedTags t;
161 yaffs_PackedTags2 pt;
163 error = write(outFile,data,chunkSize);
164 if(error < 0) return error;
166 yaffs_InitialiseTags(&t);
169 // t.serialNumber = 0;
170 t.serialNumber = 1; // **CHECK**
171 t.byteCount = nBytes;
174 t.sequenceNumber = YAFFS_LOWEST_SEQUENCE_NUMBER;
176 // added NCB **CHECK**
181 little_to_big_endian(&t);
186 yaffs_PackTags2(&pt,&t);
188 // return write(outFile,&pt,sizeof(yaffs_PackedTags2));
189 return write(outFile,&pt,spareSize);
193 #define SWAP32(x) ((((x) & 0x000000FF) << 24) | \
194 (((x) & 0x0000FF00) << 8 ) | \
195 (((x) & 0x00FF0000) >> 8 ) | \
196 (((x) & 0xFF000000) >> 24))
198 #define SWAP16(x) ((((x) & 0x00FF) << 8) | \
199 (((x) & 0xFF00) >> 8))
201 // This one is easier, since the types are more standard. No funky shifts here.
202 static void object_header_little_to_big_endian(yaffs_ObjectHeader* oh)
204 oh->type = SWAP32(oh->type); // GCC makes enums 32 bits.
205 oh->parentObjectId = SWAP32(oh->parentObjectId); // int
206 oh->sum__NoLongerUsed = SWAP16(oh->sum__NoLongerUsed); // __u16 - Not used, but done for completeness.
207 // name = skip. Char array. Not swapped.
208 oh->yst_mode = SWAP32(oh->yst_mode);
209 #ifdef CONFIG_YAFFS_WINCE // WinCE doesn't implement this, but we need to just in case.
210 // In fact, WinCE would be *THE* place where this would be an issue!
211 oh->notForWinCE[0] = SWAP32(oh->notForWinCE[0]);
212 oh->notForWinCE[1] = SWAP32(oh->notForWinCE[1]);
213 oh->notForWinCE[2] = SWAP32(oh->notForWinCE[2]);
214 oh->notForWinCE[3] = SWAP32(oh->notForWinCE[3]);
215 oh->notForWinCE[4] = SWAP32(oh->notForWinCE[4]);
218 oh->yst_uid = SWAP32(oh->yst_uid);
219 oh->yst_gid = SWAP32(oh->yst_gid);
220 oh->yst_atime = SWAP32(oh->yst_atime);
221 oh->yst_mtime = SWAP32(oh->yst_mtime);
222 oh->yst_ctime = SWAP32(oh->yst_ctime);
225 oh->fileSize = SWAP32(oh->fileSize); // Aiee. An int... signed, at that!
226 oh->equivalentObjectId = SWAP32(oh->equivalentObjectId);
227 // alias - char array.
228 oh->yst_rdev = SWAP32(oh->yst_rdev);
230 #ifdef CONFIG_YAFFS_WINCE
231 oh->win_ctime[0] = SWAP32(oh->win_ctime[0]);
232 oh->win_ctime[1] = SWAP32(oh->win_ctime[1]);
233 oh->win_atime[0] = SWAP32(oh->win_atime[0]);
234 oh->win_atime[1] = SWAP32(oh->win_atime[1]);
235 oh->win_mtime[0] = SWAP32(oh->win_mtime[0]);
236 oh->win_mtime[1] = SWAP32(oh->win_mtime[1]);
237 oh->roomToGrow[0] = SWAP32(oh->roomToGrow[0]);
238 oh->roomToGrow[1] = SWAP32(oh->roomToGrow[1]);
239 oh->roomToGrow[2] = SWAP32(oh->roomToGrow[2]);
240 oh->roomToGrow[3] = SWAP32(oh->roomToGrow[3]);
241 oh->roomToGrow[4] = SWAP32(oh->roomToGrow[4]);
242 oh->roomToGrow[5] = SWAP32(oh->roomToGrow[5]);
244 oh->roomToGrow[0] = SWAP32(oh->roomToGrow[0]);
245 oh->roomToGrow[1] = SWAP32(oh->roomToGrow[1]);
246 oh->roomToGrow[2] = SWAP32(oh->roomToGrow[2]);
247 oh->roomToGrow[3] = SWAP32(oh->roomToGrow[3]);
248 oh->roomToGrow[4] = SWAP32(oh->roomToGrow[4]);
249 oh->roomToGrow[5] = SWAP32(oh->roomToGrow[5]);
250 oh->roomToGrow[6] = SWAP32(oh->roomToGrow[6]);
251 oh->roomToGrow[7] = SWAP32(oh->roomToGrow[7]);
252 oh->roomToGrow[8] = SWAP32(oh->roomToGrow[8]);
253 oh->roomToGrow[9] = SWAP32(oh->roomToGrow[9]);
254 oh->roomToGrow[10] = SWAP32(oh->roomToGrow[10]);
255 oh->roomToGrow[11] = SWAP32(oh->roomToGrow[11]);
259 static int write_object_header(int objId, yaffs_ObjectType t, struct stat *s, int parent, const char *name, int equivalentObj, const char * alias)
261 __u8 bytes[chunkSize];
264 yaffs_ObjectHeader *oh = (yaffs_ObjectHeader *)bytes;
266 memset(bytes,0xff,sizeof(bytes));
270 oh->parentObjectId = parent;
272 strncpy(oh->name,name,YAFFS_MAX_NAME_LENGTH);
275 if(t != YAFFS_OBJECT_TYPE_HARDLINK)
277 oh->yst_mode = s->st_mode;
278 oh->yst_uid = s->st_uid;
279 // NCB 12/9/02 oh->yst_gid = s->yst_uid;
280 oh->yst_gid = s->st_gid;
281 oh->yst_atime = s->st_atime;
282 oh->yst_mtime = s->st_mtime;
283 oh->yst_ctime = s->st_ctime;
284 oh->yst_rdev = s->st_rdev;
287 if(t == YAFFS_OBJECT_TYPE_FILE)
289 oh->fileSize = s->st_size;
292 if(t == YAFFS_OBJECT_TYPE_HARDLINK)
294 oh->equivalentObjectId = equivalentObj;
297 if(t == YAFFS_OBJECT_TYPE_SYMLINK)
299 strncpy(oh->alias,alias,YAFFS_MAX_ALIAS_LENGTH);
304 object_header_little_to_big_endian(oh);
307 return write_chunk(bytes,objId,0,0xffff);
312 static int process_directory(int parent, const char *path)
316 struct dirent *entry;
324 while((entry = readdir(dir)) != NULL)
327 /* Ignore . and .. */
328 if(strcmp(entry->d_name,".") &&
329 strcmp(entry->d_name,".."))
336 sprintf(full_name,"%s/%s",path,entry->d_name);
338 lstat(full_name,&stats);
340 if(S_ISLNK(stats.st_mode) ||
341 S_ISREG(stats.st_mode) ||
342 S_ISDIR(stats.st_mode) ||
343 S_ISFIFO(stats.st_mode) ||
344 S_ISBLK(stats.st_mode) ||
345 S_ISCHR(stats.st_mode) ||
346 S_ISSOCK(stats.st_mode))
352 printf("Object %d, %s is a ",newObj,full_name);
354 /* We're going to create an object for it */
355 if((equivalentObj = find_obj_in_list(stats.st_dev, stats.st_ino)) > 0)
357 /* we need to make a hard link */
358 printf("hard link to object %d\n",equivalentObj);
359 error = write_object_header(newObj, YAFFS_OBJECT_TYPE_HARDLINK, &stats, parent, entry->d_name, equivalentObj, NULL);
364 add_obj_to_list(stats.st_dev,stats.st_ino,newObj);
366 if(S_ISLNK(stats.st_mode))
371 memset(symname,0, sizeof(symname));
373 readlink(full_name,symname,sizeof(symname) -1);
375 printf("symlink to \"%s\"\n",symname);
376 error = write_object_header(newObj, YAFFS_OBJECT_TYPE_SYMLINK, &stats, parent, entry->d_name, -1, symname);
379 else if(S_ISREG(stats.st_mode))
382 error = write_object_header(newObj, YAFFS_OBJECT_TYPE_FILE, &stats, parent, entry->d_name, -1, NULL);
387 __u8 bytes[chunkSize];
391 h = open(full_name,O_RDONLY);
394 memset(bytes,0xff,sizeof(bytes));
395 while((nBytes = read(h,bytes,sizeof(bytes))) > 0)
398 write_chunk(bytes,newObj,chunk,nBytes);
399 memset(bytes,0xff,sizeof(bytes));
404 printf("%d data chunks written\n",chunk);
408 perror("Error opening file");
415 else if(S_ISSOCK(stats.st_mode))
418 error = write_object_header(newObj, YAFFS_OBJECT_TYPE_SPECIAL, &stats, parent, entry->d_name, -1, NULL);
420 else if(S_ISFIFO(stats.st_mode))
423 error = write_object_header(newObj, YAFFS_OBJECT_TYPE_SPECIAL, &stats, parent, entry->d_name, -1, NULL);
425 else if(S_ISCHR(stats.st_mode))
427 printf("character device\n");
428 error = write_object_header(newObj, YAFFS_OBJECT_TYPE_SPECIAL, &stats, parent, entry->d_name, -1, NULL);
430 else if(S_ISBLK(stats.st_mode))
432 printf("block device\n");
433 error = write_object_header(newObj, YAFFS_OBJECT_TYPE_SPECIAL, &stats, parent, entry->d_name, -1, NULL);
435 else if(S_ISDIR(stats.st_mode))
437 printf("directory\n");
438 error = write_object_header(newObj, YAFFS_OBJECT_TYPE_DIRECTORY, &stats, parent, entry->d_name, -1, NULL);
439 // NCB modified 10/9/2001 process_directory(1,full_name);
440 process_directory(newObj,full_name);
446 printf(" we don't handle this type\n");
457 int main(int argc, char *argv[])
461 printf("mkyaffs2image: image building tool for YAFFS2 built "__DATE__"\n");
465 printf("usage: mkyaffs2image dir image_file [convert]\n");
466 printf(" dir the directory tree to be converted\n");
467 printf(" image_file the output file to hold the image\n");
468 printf(" 'convert' produce a big-endian image from a little-endian machine\n");
472 if ((argc == 4) && (!strncmp(argv[3], "convert", strlen("convert"))))
477 if(stat(argv[1],&stats) < 0)
479 printf("Could not stat %s\n",argv[1]);
483 if(!S_ISDIR(stats.st_mode))
485 printf(" %s is not a directory\n",argv[1]);
489 outFile = open(argv[2],O_CREAT | O_TRUNC | O_WRONLY, S_IREAD | S_IWRITE);
494 printf("Could not open output file %s\n",argv[2]);
498 printf("Processing directory %s into image file %s\n",argv[1],argv[2]);
499 error = write_object_header(1, YAFFS_OBJECT_TYPE_DIRECTORY, &stats, 1,"", -1, NULL);
501 error = process_directory(YAFFS_OBJECTID_ROOT,argv[1]);
507 perror("operation incomplete");
512 printf("Operation complete.\n"
513 "%d objects in %d directories\n"
514 "%d NAND pages\n",nObjects, nDirectories, nPages);