2 * YAFFS: Yet Another Flash File System. A NAND-flash specific file system.
4 * Copyright (C) 2002-2018 Aleph One Ltd.
6 * Created by Charles Manning <charles@aleph1.co.uk>
7 * Nick Bane modifications flagged NCBw
8 * Endian handling patches by James Ng.
9 * mkyaffs2image hacks by NCB
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 YAFFS2 file system image that can be used to load up a file system.
20 * Uses default Linux MTD layout - search for "NAND LAYOUT" to change.
26 #include <sys/types.h>
33 #include "yaffs_ecc.h"
34 #include "yaffs_guts.h"
35 #include "yaffs_endian.h"
37 #include "yaffs_packedtags2.h"
39 unsigned yaffs_trace_mask=0;
41 #define MAX_OBJECTS 10000
43 // Adjust these to match your NAND LAYOUT:
44 #define chunkSize 2048
46 #define pagesPerBlock 64
57 struct yaffs_dev dummy_dev = {.swap_endian = 0};
60 static objItem obj_list[MAX_OBJECTS];
61 static int obj_id = YAFFS_NOBJECT_BUCKETS + 1;
63 static int n_obj, nDirectories, nPages;
68 static int savedErrno;
70 static int convert_endian = 0;
72 /* Provide our own endian swap that does nothing. */
73 void yaffs_do_endian_packed_tags2(struct yaffs_dev *dev,
74 struct yaffs_packed_tags2_tags_only *ptt)
77 if (!dev->swap_endian)
79 ptt->seq_number = swap_u32(ptt->seq_number);
80 ptt->obj_id = swap_u32(ptt->obj_id);
81 ptt->chunk_id = swap_u32(ptt->chunk_id);
82 ptt->n_bytes = swap_u32(ptt->n_bytes);
86 static void fatal(const char *fn)
93 static int warn(const char *fn)
101 static int obj_compare(const void *a, const void * b)
108 if(oa->dev < ob->dev) return -1;
109 if(oa->dev > ob->dev) return 1;
110 if(oa->ino < ob->ino) return -1;
111 if(oa->ino > ob->ino) return 1;
117 static void add_obj_to_list(dev_t dev, ino_t ino, int obj)
119 if(n_obj < MAX_OBJECTS)
121 obj_list[n_obj].dev = dev;
122 obj_list[n_obj].ino = ino;
123 obj_list[n_obj].obj = obj;
125 qsort(obj_list,n_obj,sizeof(objItem),obj_compare);
130 // oops! not enough space in the object array
131 fprintf(stderr,"Not enough space in object array\n");
137 static int find_obj_in_list(dev_t dev, ino_t ino)
147 i = bsearch(&test,obj_list,n_obj,sizeof(objItem),obj_compare);
157 /* This little function converts a little endian tag to a big endian tag.
158 * NOTE: The tag is not usable after this other than calculating the CRC
161 static void little_to_big_endian(struct yaffs_ext_tags *tagsPtr)
164 union yaffs_tags_union * tags = (union yaffs_tags_union* )tagsPtr; // Work in bytes.
165 union yaffs_tags_union temp;
167 memset(&temp, 0, sizeof(temp));
168 // Ick, I hate magic numbers.
169 temp.as_bytes[0] = ((tags->as_bytes[2] & 0x0F) << 4) | ((tags->as_bytes[1] & 0xF0) >> 4);
170 temp.as_bytes[1] = ((tags->as_bytes[1] & 0x0F) << 4) | ((tags->as_bytes[0] & 0xF0) >> 4);
171 temp.as_bytes[2] = ((tags->as_bytes[0] & 0x0F) << 4) | ((tags->as_bytes[2] & 0x30) >> 2) | ((tags->as_bytes[3] & 0xC0) >> 6);
172 temp.as_bytes[3] = ((tags->as_bytes[3] & 0x3F) << 2) | ((tags->as_bytes[2] & 0xC0) >> 6);
173 temp.as_bytes[4] = ((tags->as_bytes[6] & 0x03) << 6) | ((tags->as_bytes[5] & 0xFC) >> 2);
174 temp.as_bytes[5] = ((tags->as_bytes[5] & 0x03) << 6) | ((tags->as_bytes[4] & 0xFC) >> 2);
175 temp.as_bytes[6] = ((tags->as_bytes[4] & 0x03) << 6) | (tags->as_bytes[7] & 0x3F);
176 temp.as_bytes[7] = (tags->as_bytes[6] & 0xFC) | ((tags->as_bytes[7] & 0xC0) >> 6);
179 tags->as_bytes[0] = temp.as_bytes[0];
180 tags->as_bytes[1] = temp.as_bytes[1];
181 tags->as_bytes[2] = temp.as_bytes[2];
182 tags->as_bytes[3] = temp.as_bytes[3];
183 tags->as_bytes[4] = temp.as_bytes[4];
184 tags->as_bytes[5] = temp.as_bytes[5];
185 tags->as_bytes[6] = temp.as_bytes[6];
186 tags->as_bytes[7] = temp.as_bytes[7];
190 static void shuffle_oob(char *spareData, struct yaffs_packed_tags2 *pt)
192 assert(sizeof(*pt) <= spareSize);
193 // NAND LAYOUT: For non-trivial OOB orderings, here would be a good place to shuffle.
194 memcpy(spareData, pt, sizeof(*pt));
197 static int write_chunk(u8 *data, u32 id, u32 chunk_id, u32 n_bytes)
199 struct yaffs_ext_tags t;
200 struct yaffs_packed_tags2 pt;
201 char spareData[spareSize];
203 if (write(outFile,data,chunkSize) != chunkSize)
206 memset(&t, 0, sizeof(t));
208 t.chunk_id = chunk_id;
209 // t.serial_number = 0;
210 t.serial_number = 1; // **CHECK**
214 t.seq_number = YAFFS_LOWEST_SEQUENCE_NUMBER;
216 // added NCB **CHECK**
221 little_to_big_endian(&t);
226 memset(&pt, 0, sizeof(pt));
227 yaffs_pack_tags2(&dummy_dev, &pt,&t,1);
229 memset(spareData, 0xff, sizeof(spareData));
230 shuffle_oob(spareData, &pt);
232 if (write(outFile,spareData,sizeof(spareData)) != sizeof(spareData))
237 #define SWAP32(x) ((((x) & 0x000000FF) << 24) | \
238 (((x) & 0x0000FF00) << 8 ) | \
239 (((x) & 0x00FF0000) >> 8 ) | \
240 (((x) & 0xFF000000) >> 24))
242 #define SWAP16(x) ((((x) & 0x00FF) << 8) | \
243 (((x) & 0xFF00) >> 8))
245 // This one is easier, since the types are more standard. No funky shifts here.
246 static void object_header_little_to_big_endian(struct yaffs_obj_hdr* oh)
248 oh->type = SWAP32(oh->type); // GCC makes enums 32 bits.
249 oh->parent_obj_id = SWAP32(oh->parent_obj_id); // int
250 oh->sum_no_longer_used = SWAP16(oh->sum_no_longer_used); // u16 - Not used, but done for completeness.
251 // name = skip. Char array. Not swapped.
252 oh->yst_mode = SWAP32(oh->yst_mode);
255 oh->yst_uid = SWAP32(oh->yst_uid);
256 oh->yst_gid = SWAP32(oh->yst_gid);
257 oh->yst_atime = SWAP32(oh->yst_atime);
258 oh->yst_mtime = SWAP32(oh->yst_mtime);
259 oh->yst_ctime = SWAP32(oh->yst_ctime);
261 oh->file_size_low = SWAP32(oh->file_size_low); // Aiee. An int... signed, at that!
262 oh->file_size_high = SWAP32(oh->file_size_high); // Aiee. An int... signed, at that!
263 oh->equiv_id = SWAP32(oh->equiv_id);
264 // alias - char array.
265 oh->yst_rdev = SWAP32(oh->yst_rdev);
267 oh->win_ctime[0] = SWAP32(oh->win_ctime[0]);
268 oh->win_ctime[1] = SWAP32(oh->win_ctime[1]);
269 oh->win_atime[0] = SWAP32(oh->win_atime[0]);
270 oh->win_atime[1] = SWAP32(oh->win_atime[1]);
271 oh->win_mtime[0] = SWAP32(oh->win_mtime[0]);
272 oh->win_mtime[1] = SWAP32(oh->win_mtime[1]);
274 oh->reserved[0] = SWAP32(oh->reserved[0]);
276 oh->reserved[1] = SWAP32(oh->reserved[1]);
278 oh->inband_shadowed_obj_id = SWAP32(oh->inband_shadowed_obj_id);
279 oh->inband_is_shrink = SWAP32(oh->inband_is_shrink);
280 oh->shadows_obj = SWAP32(oh->shadows_obj);
281 oh->is_shrink = SWAP32(oh->is_shrink);
285 static int write_object_header(int id, enum yaffs_obj_type t, struct stat *s, int parent, const char *name, int equivalentObj, const char * alias)
290 struct yaffs_obj_hdr *oh = (struct yaffs_obj_hdr *)bytes;
292 memset(bytes,0xff,sizeof(bytes));
296 oh->parent_obj_id = parent;
298 if (strlen(name)+1 > sizeof(oh->name))
300 errno = ENAMETOOLONG;
301 return warn("object name");
303 memset(oh->name,0,sizeof(oh->name));
304 strcpy(oh->name,name);
307 if(t != YAFFS_OBJECT_TYPE_HARDLINK)
309 oh->yst_mode = s->st_mode;
310 oh->yst_uid = s->st_uid;
311 // NCB 12/9/02 oh->yst_gid = s->yst_uid;
312 oh->yst_gid = s->st_gid;
313 oh->yst_atime = s->st_atime;
314 oh->yst_mtime = s->st_mtime;
315 oh->yst_ctime = s->st_ctime;
316 oh->yst_rdev = s->st_rdev;
319 if(t == YAFFS_OBJECT_TYPE_FILE)
321 oh->file_size_low = s->st_size;
322 oh->file_size_high = (s->st_size >> 32);
325 if(t == YAFFS_OBJECT_TYPE_HARDLINK)
327 oh->equiv_id = equivalentObj;
330 if(t == YAFFS_OBJECT_TYPE_SYMLINK)
332 if (strlen(alias)+1 > sizeof(oh->alias))
334 errno = ENAMETOOLONG;
335 return warn("object alias");
337 memset(oh->alias,0,sizeof(oh->alias));
338 strcpy(oh->alias,alias);
343 object_header_little_to_big_endian(oh);
346 return write_chunk(bytes,id,0,0xffff);
350 static void pad_image(void)
352 u8 data[chunkSize + spareSize];
353 int padPages = (nPages % pagesPerBlock);
357 memset(data, 0xff, sizeof(data));
358 for (padPages = pagesPerBlock-padPages; padPages; padPages--)
360 if (write(outFile, data, sizeof(data)) != sizeof(data))
366 static int process_directory(int parent, const char *path)
370 struct dirent *entry;
381 while((entry = readdir(dir)) != NULL)
384 /* Ignore . and .. */
385 if(strcmp(entry->d_name,".") &&
386 strcmp(entry->d_name,".."))
393 if (snprintf(full_name,sizeof(full_name),"%s/%s",path,entry->d_name) >= (int)sizeof(full_name))
399 if (lstat(full_name,&stats) < 0)
405 if(S_ISLNK(stats.st_mode) ||
406 S_ISREG(stats.st_mode) ||
407 S_ISDIR(stats.st_mode) ||
408 S_ISFIFO(stats.st_mode) ||
409 S_ISBLK(stats.st_mode) ||
410 S_ISCHR(stats.st_mode) ||
411 S_ISSOCK(stats.st_mode))
417 printf("Object %d, %s is a ",newObj,full_name);
419 /* We're going to create an object for it */
420 if((equivalentObj = find_obj_in_list(stats.st_dev, stats.st_ino)) > 0)
422 /* we need to make a hard link */
423 printf("hard link to object %d\n",equivalentObj);
424 write_object_header(newObj, YAFFS_OBJECT_TYPE_HARDLINK, &stats, parent, entry->d_name, equivalentObj, NULL);
429 add_obj_to_list(stats.st_dev,stats.st_ino,newObj);
431 if(S_ISLNK(stats.st_mode))
436 memset(symname,0, sizeof(symname));
438 if (readlink(full_name,symname,sizeof(symname) -1) < 0)
444 printf("symlink to \"%s\"\n",symname);
445 write_object_header(newObj, YAFFS_OBJECT_TYPE_SYMLINK, &stats, parent, entry->d_name, -1, symname);
448 else if(S_ISREG(stats.st_mode))
451 if(write_object_header(newObj, YAFFS_OBJECT_TYPE_FILE, &stats, parent, entry->d_name, -1, NULL) == 0)
458 h = open(full_name,O_RDONLY);
461 memset(bytes,0xff,sizeof(bytes));
462 while((n_bytes = read(h,bytes,sizeof(bytes))) > 0)
465 write_chunk(bytes,newObj,chunk,n_bytes);
466 memset(bytes,0xff,sizeof(bytes));
471 printf("%d data chunks written\n",chunk);
482 else if(S_ISSOCK(stats.st_mode))
485 write_object_header(newObj, YAFFS_OBJECT_TYPE_SPECIAL, &stats, parent, entry->d_name, -1, NULL);
487 else if(S_ISFIFO(stats.st_mode))
490 write_object_header(newObj, YAFFS_OBJECT_TYPE_SPECIAL, &stats, parent, entry->d_name, -1, NULL);
492 else if(S_ISCHR(stats.st_mode))
494 printf("character device\n");
495 write_object_header(newObj, YAFFS_OBJECT_TYPE_SPECIAL, &stats, parent, entry->d_name, -1, NULL);
497 else if(S_ISBLK(stats.st_mode))
499 printf("block device\n");
500 write_object_header(newObj, YAFFS_OBJECT_TYPE_SPECIAL, &stats, parent, entry->d_name, -1, NULL);
502 else if(S_ISDIR(stats.st_mode))
504 printf("directory\n");
505 if (write_object_header(newObj, YAFFS_OBJECT_TYPE_DIRECTORY, &stats, parent, entry->d_name, -1, NULL) == 0)
506 process_directory(newObj,full_name);
512 fprintf(stderr, "%s: unhandled type\n", full_name);
526 int main(int argc, char *argv[])
530 printf("mkyaffs2image: image building tool for YAFFS2 built "__DATE__"\n");
534 printf("usage: mkyaffs2image 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 process_directory(YAFFS_OBJECTID_ROOT,argv[1]);
577 perror("operation incomplete");
581 printf("Operation complete.\n"
582 "%d objects in %d directories\n"
583 "%d NAND pages\n",n_obj, nDirectories, nPages);