2 * YAFFS: Yet Another Flash File System. A NAND-flash specific file system.
4 * Copyright (C) 2002-2010 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
11 * This program is free software; you can redistribute it and/or modify
12 * it under the terms of the GNU General Public License version 2 as
13 * published by the Free Software Foundation.
19 * Makes a YAFFS file system image that can be used to load up a file system.
25 #include <sys/types.h>
30 #include "yaffs_ecc.h"
31 #include "yaffs_guts.h"
34 #define MAX_OBJECTS 10000
36 const char * mkyaffsimage_c_version = "$Id: mkyaffsimage.c,v 1.7 2003/07/16 03:00:48 charles Exp $";
47 static objItem obj_list[MAX_OBJECTS];
49 static int obj_id = YAFFS_NOBJECT_BUCKETS + 1;
51 static int n_obj, nDirectories, nPages;
57 static int convert_endian = 0;
59 static int obj_compare(const void *a, const void * b)
66 if(oa->dev < ob->dev) return -1;
67 if(oa->dev > ob->dev) return 1;
68 if(oa->ino < ob->ino) return -1;
69 if(oa->ino > ob->ino) return 1;
75 static void add_obj_to_list(dev_t dev, ino_t ino, int obj)
77 if(n_obj < MAX_OBJECTS)
79 obj_list[n_obj].dev = dev;
80 obj_list[n_obj].ino = ino;
81 obj_list[n_obj].obj = obj;
83 qsort(obj_list,n_obj,sizeof(objItem),obj_compare);
88 // oops! not enough space in the object array
89 fprintf(stderr,"Not enough space in object array\n");
95 static int find_obj_in_list(dev_t dev, ino_t ino)
105 i = bsearch(&test,obj_list,n_obj,sizeof(objItem),obj_compare);
115 // NCB added 10/9/2002
116 static u16 yaffs_calc_name_sum(const char *name)
121 u8 *bname = (u8 *)name;
133 static void yaffs_calc_ecc(const u8 *data, struct yaffs_spare *spare)
135 yaffs_ecc_cacl(data , spare->ecc1);
136 yaffs_ecc_cacl(&data[256] , spare->ecc2);
139 static void yaffs_calc_tags_ecc(struct yaffs_tags *tags)
141 // Todo don't do anything yet. Need to calculate ecc
142 unsigned char *b = ((union yaffs_tags_union *)tags)->as_bytes;
154 // Because we're in "munged tag" mode, we have to clear it manually
159 for(i = 0; i < 8; i++)
161 // NCB modified 20-9-02 for(j = 1; j &0x7f; j<<=1)
162 for(j = 1; j &0xff; j<<=1)
179 // We have to munge the ECC again.
180 b[6] |= ((ecc >> 6) & 0x3F);
181 b[7] |= ((ecc & 0x3F) << 2);
184 static void yaffs_load_tags_to_spare(struct yaffs_spare *sparePtr, struct yaffs_tags *tagsPtr)
186 union yaffs_tags_union *tu = (union yaffs_tags_union *)tagsPtr;
188 //yaffs_calc_tags_ecc(tagsPtr);
190 sparePtr->tb0 = tu->as_bytes[0];
191 sparePtr->tb1 = tu->as_bytes[1];
192 sparePtr->tb2 = tu->as_bytes[2];
193 sparePtr->tb3 = tu->as_bytes[3];
194 sparePtr->tb4 = tu->as_bytes[4];
195 sparePtr->tb5 = tu->as_bytes[5];
196 sparePtr->tb6 = tu->as_bytes[6];
197 sparePtr->tb7 = tu->as_bytes[7];
200 /* This little function converts a little endian tag to a big endian tag.
201 * NOTE: The tag is not usable after this other than calculating the CRC
204 static void little_to_big_endian(struct yaffs_tags *tagsPtr)
206 union yaffs_tags_union * tags = (union yaffs_tags_union* )tagsPtr; // Work in bytes.
207 union yaffs_tags_union temp;
209 memset(&temp, 0, sizeof(temp));
210 // Ick, I hate magic numbers.
211 temp.as_bytes[0] = ((tags->as_bytes[2] & 0x0F) << 4) | ((tags->as_bytes[1] & 0xF0) >> 4);
212 temp.as_bytes[1] = ((tags->as_bytes[1] & 0x0F) << 4) | ((tags->as_bytes[0] & 0xF0) >> 4);
213 temp.as_bytes[2] = ((tags->as_bytes[0] & 0x0F) << 4) | ((tags->as_bytes[2] & 0x30) >> 2) | ((tags->as_bytes[3] & 0xC0) >> 6);
214 temp.as_bytes[3] = ((tags->as_bytes[3] & 0x3F) << 2) | ((tags->as_bytes[2] & 0xC0) >> 6);
215 temp.as_bytes[4] = ((tags->as_bytes[6] & 0x03) << 6) | ((tags->as_bytes[5] & 0xFC) >> 2);
216 temp.as_bytes[5] = ((tags->as_bytes[5] & 0x03) << 6) | ((tags->as_bytes[4] & 0xFC) >> 2);
217 temp.as_bytes[6] = ((tags->as_bytes[4] & 0x03) << 6) | (tags->as_bytes[7] & 0x3F);
218 temp.as_bytes[7] = (tags->as_bytes[6] & 0xFC) | ((tags->as_bytes[7] & 0xC0) >> 6);
221 tags->as_bytes[0] = temp.as_bytes[0];
222 tags->as_bytes[1] = temp.as_bytes[1];
223 tags->as_bytes[2] = temp.as_bytes[2];
224 tags->as_bytes[3] = temp.as_bytes[3];
225 tags->as_bytes[4] = temp.as_bytes[4];
226 tags->as_bytes[5] = temp.as_bytes[5];
227 tags->as_bytes[6] = temp.as_bytes[6];
228 tags->as_bytes[7] = temp.as_bytes[7];
231 static int write_chunk(u8 *data, u32 obj_id, u32 chunk_id, u32 n_bytes)
234 struct yaffs_spare s;
236 error = write(outFile,data,512);
237 if(error < 0) return error;
239 memset(&t,0xff,sizeof (struct yaffs_tags));
240 memset(&s,0xff,sizeof (struct yaffs_spare));
242 t.chunk_id = chunk_id;
244 t.n_bytes_lsb = n_bytes;
249 little_to_big_endian(&t);
252 yaffs_calc_tags_ecc(&t);
253 yaffs_load_tags_to_spare(&s,&t);
254 yaffs_calc_ecc(data,&s);
258 return write(outFile,&s,sizeof(struct yaffs_spare));
262 #define SWAP32(x) ((((x) & 0x000000FF) << 24) | \
263 (((x) & 0x0000FF00) << 8 ) | \
264 (((x) & 0x00FF0000) >> 8 ) | \
265 (((x) & 0xFF000000) >> 24))
267 #define SWAP16(x) ((((x) & 0x00FF) << 8) | \
268 (((x) & 0xFF00) >> 8))
270 // This one is easier, since the types are more standard. No funky shifts here.
271 static void object_header_little_to_big_endian(struct yaffs_obj_hdr* oh)
273 oh->type = SWAP32(oh->type); // GCC makes enums 32 bits.
274 oh->parent_obj_id = SWAP32(oh->parent_obj_id); // int
275 oh->sum_no_longer_used = SWAP16(oh->sum_no_longer_used); // u16 - Not used, but done for completeness.
276 // name = skip. Char array. Not swapped.
277 oh->yst_mode = SWAP32(oh->yst_mode);
278 #ifdef CONFIG_YAFFS_WINCE // WinCE doesn't implement this, but we need to just in case.
279 // In fact, WinCE would be *THE* place where this would be an issue!
280 oh->not_for_wince[0] = SWAP32(oh->not_for_wince[0]);
281 oh->not_for_wince[1] = SWAP32(oh->not_for_wince[1]);
282 oh->not_for_wince[2] = SWAP32(oh->not_for_wince[2]);
283 oh->not_for_wince[3] = SWAP32(oh->not_for_wince[3]);
284 oh->not_for_wince[4] = SWAP32(oh->not_for_wince[4]);
287 oh->yst_uid = SWAP32(oh->yst_uid);
288 oh->yst_gid = SWAP32(oh->yst_gid);
289 oh->yst_atime = SWAP32(oh->yst_atime);
290 oh->yst_mtime = SWAP32(oh->yst_mtime);
291 oh->yst_ctime = SWAP32(oh->yst_ctime);
294 oh->file_size = SWAP32(oh->file_size); // Aiee. An int... signed, at that!
295 oh->equiv_id = SWAP32(oh->equiv_id);
296 // alias - char array.
297 oh->yst_rdev = SWAP32(oh->yst_rdev);
299 #ifdef CONFIG_YAFFS_WINCE
300 oh->win_ctime[0] = SWAP32(oh->win_ctime[0]);
301 oh->win_ctime[1] = SWAP32(oh->win_ctime[1]);
302 oh->win_atime[0] = SWAP32(oh->win_atime[0]);
303 oh->win_atime[1] = SWAP32(oh->win_atime[1]);
304 oh->win_mtime[0] = SWAP32(oh->win_mtime[0]);
305 oh->win_mtime[1] = SWAP32(oh->win_mtime[1]);
306 oh->room_to_grow[0] = SWAP32(oh->room_to_grow[0]);
307 oh->room_to_grow[1] = SWAP32(oh->room_to_grow[1]);
308 oh->room_to_grow[2] = SWAP32(oh->room_to_grow[2]);
309 oh->room_to_grow[3] = SWAP32(oh->room_to_grow[3]);
310 oh->room_to_grow[4] = SWAP32(oh->room_to_grow[4]);
311 oh->room_to_grow[5] = SWAP32(oh->room_to_grow[5]);
313 oh->room_to_grow[0] = SWAP32(oh->room_to_grow[0]);
314 oh->room_to_grow[1] = SWAP32(oh->room_to_grow[1]);
315 oh->room_to_grow[2] = SWAP32(oh->room_to_grow[2]);
316 oh->room_to_grow[3] = SWAP32(oh->room_to_grow[3]);
317 oh->room_to_grow[4] = SWAP32(oh->room_to_grow[4]);
318 oh->room_to_grow[5] = SWAP32(oh->room_to_grow[5]);
319 oh->room_to_grow[6] = SWAP32(oh->room_to_grow[6]);
320 oh->room_to_grow[7] = SWAP32(oh->room_to_grow[7]);
321 oh->room_to_grow[8] = SWAP32(oh->room_to_grow[8]);
322 oh->room_to_grow[9] = SWAP32(oh->room_to_grow[9]);
323 oh->room_to_grow[10] = SWAP32(oh->room_to_grow[10]);
324 oh->room_to_grow[11] = SWAP32(oh->room_to_grow[11]);
328 static int write_object_header(int obj_id, enum yaffs_obj_type t, struct stat *s, int parent, const char *name, int equivalentObj, const char * alias)
333 struct yaffs_obj_hdr *oh = (struct yaffs_obj_hdr *)bytes;
335 memset(bytes,0xff,512);
339 oh->parent_obj_id = parent;
341 strncpy(oh->name,name,YAFFS_MAX_NAME_LENGTH);
344 if(t != YAFFS_OBJECT_TYPE_HARDLINK)
346 oh->yst_mode = s->st_mode;
347 oh->yst_uid = s->st_uid;
348 // NCB 12/9/02 oh->yst_gid = s->yst_uid;
349 oh->yst_gid = s->st_gid;
350 oh->yst_atime = s->st_atime;
351 oh->yst_mtime = s->st_mtime;
352 oh->yst_ctime = s->st_ctime;
353 oh->yst_rdev = s->st_rdev;
356 if(t == YAFFS_OBJECT_TYPE_FILE)
358 oh->file_size = s->st_size;
361 if(t == YAFFS_OBJECT_TYPE_HARDLINK)
363 oh->equiv_id = equivalentObj;
366 if(t == YAFFS_OBJECT_TYPE_SYMLINK)
368 strncpy(oh->alias,alias,YAFFS_MAX_ALIAS_LENGTH);
373 object_header_little_to_big_endian(oh);
376 return write_chunk(bytes,obj_id,0,0xffff);
381 static int process_directory(int parent, const char *path)
385 struct dirent *entry;
393 while((entry = readdir(dir)) != NULL)
396 /* Ignore . and .. */
397 if(strcmp(entry->d_name,".") &&
398 strcmp(entry->d_name,".."))
405 sprintf(full_name,"%s/%s",path,entry->d_name);
407 lstat(full_name,&stats);
409 if(S_ISLNK(stats.st_mode) ||
410 S_ISREG(stats.st_mode) ||
411 S_ISDIR(stats.st_mode) ||
412 S_ISFIFO(stats.st_mode) ||
413 S_ISBLK(stats.st_mode) ||
414 S_ISCHR(stats.st_mode) ||
415 S_ISSOCK(stats.st_mode))
421 printf("Object %d, %s is a ",newObj,full_name);
423 /* We're going to create an object for it */
424 if((equivalentObj = find_obj_in_list(stats.st_dev, stats.st_ino)) > 0)
426 /* we need to make a hard link */
427 printf("hard link to object %d\n",equivalentObj);
428 error = write_object_header(newObj, YAFFS_OBJECT_TYPE_HARDLINK, &stats, parent, entry->d_name, equivalentObj, NULL);
433 add_obj_to_list(stats.st_dev,stats.st_ino,newObj);
435 if(S_ISLNK(stats.st_mode))
440 memset(symname,0, sizeof(symname));
442 readlink(full_name,symname,sizeof(symname) -1);
444 printf("symlink to \"%s\"\n",symname);
445 error = write_object_header(newObj, YAFFS_OBJECT_TYPE_SYMLINK, &stats, parent, entry->d_name, -1, symname);
448 else if(S_ISREG(stats.st_mode))
451 error = write_object_header(newObj, YAFFS_OBJECT_TYPE_FILE, &stats, parent, entry->d_name, -1, NULL);
460 h = open(full_name,O_RDONLY);
463 memset(bytes,0xff,512);
464 while((n_bytes = read(h,bytes,512)) > 0)
467 write_chunk(bytes,newObj,chunk,n_bytes);
468 memset(bytes,0xff,512);
473 printf("%d data chunks written\n",chunk);
477 perror("Error opening file");
484 else if(S_ISSOCK(stats.st_mode))
487 error = write_object_header(newObj, YAFFS_OBJECT_TYPE_SPECIAL, &stats, parent, entry->d_name, -1, NULL);
489 else if(S_ISFIFO(stats.st_mode))
492 error = write_object_header(newObj, YAFFS_OBJECT_TYPE_SPECIAL, &stats, parent, entry->d_name, -1, NULL);
494 else if(S_ISCHR(stats.st_mode))
496 printf("character device\n");
497 error = write_object_header(newObj, YAFFS_OBJECT_TYPE_SPECIAL, &stats, parent, entry->d_name, -1, NULL);
499 else if(S_ISBLK(stats.st_mode))
501 printf("block device\n");
502 error = write_object_header(newObj, YAFFS_OBJECT_TYPE_SPECIAL, &stats, parent, entry->d_name, -1, NULL);
504 else if(S_ISDIR(stats.st_mode))
506 printf("directory\n");
507 error = write_object_header(newObj, YAFFS_OBJECT_TYPE_DIRECTORY, &stats, parent, entry->d_name, -1, NULL);
508 // NCB modified 10/9/2001 process_directory(1,full_name);
509 process_directory(newObj,full_name);
515 printf(" we don't handle this type\n");
526 int main(int argc, char *argv[])
530 printf("mkyaffsimage: image building tool for YAFFS built "__DATE__"\n");
534 printf("usage: mkyaffsimage dir image_file [convert]\n");
535 printf(" dir the directory tree to be converted\n");
536 printf(" image_file the output file to hold the image\n");
537 printf(" 'convert' produce a big-endian image from a little-endian machine\n");
541 if ((argc == 4) && (!strncmp(argv[3], "convert", strlen("convert"))))
546 if(stat(argv[1],&stats) < 0)
548 printf("Could not stat %s\n",argv[1]);
552 if(!S_ISDIR(stats.st_mode))
554 printf(" %s is not a directory\n",argv[1]);
558 outFile = open(argv[2],O_CREAT | O_TRUNC | O_WRONLY, S_IREAD | S_IWRITE);
563 printf("Could not open output file %s\n",argv[2]);
567 printf("Processing directory %s into image file %s\n",argv[1],argv[2]);
568 error = write_object_header(1, YAFFS_OBJECT_TYPE_DIRECTORY, &stats, 1,"", -1, NULL);
570 error = process_directory(YAFFS_OBJECTID_ROOT,argv[1]);
576 perror("operation incomplete");
581 printf("Operation complete.\n"
582 "%d objects in %d directories\n"
583 "%d NAND pages\n",n_obj, nDirectories, nPages);