5292b667ae7f1639baef227529dce953ef051509
[yaffs2.git] / utils / mkyaffs2image.c
1 /*
2  * YAFFS: Yet Another Flash File System. A NAND-flash specific file system.
3  *
4  * Copyright (C) 2002-2011 Aleph One Ltd.
5  *   for Toby Churchill Ltd and Brightstar Engineering
6  *
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
11  *
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.
15  */
16
17 /*
18  * makeyaffs2image.c 
19  *
20  * Makes a YAFFS2 file system image that can be used to load up a file system.
21  * Uses default Linux MTD layout - search for "NAND LAYOUT" to change.
22  */
23  
24 #include <stdlib.h>
25 #include <stdio.h>
26 #include <fcntl.h>
27 #include <sys/types.h>
28 #include <sys/stat.h>
29 #include <dirent.h>
30 #include <string.h>
31 #include <unistd.h>
32 #include <errno.h>
33 #include <assert.h>
34 #include "yaffs_ecc.h"
35 #include "yaffs_guts.h"
36
37 #include "yaffs_packedtags2.h"
38
39 unsigned yaffs_trace_mask=0;
40
41 #define MAX_OBJECTS 10000
42
43 // Adjust these to match your NAND LAYOUT:
44 #define chunkSize 2048
45 #define spareSize 64
46 #define pagesPerBlock 64
47
48
49
50 typedef struct
51 {
52         dev_t dev;
53         ino_t ino;
54         int   obj;
55 } objItem;
56
57
58 static objItem obj_list[MAX_OBJECTS];
59 static int obj_id = YAFFS_NOBJECT_BUCKETS + 1;
60
61 static int n_obj, nDirectories, nPages;
62
63 static int outFile;
64
65 static int error;
66 static int savedErrno;
67
68 static int convert_endian = 0;
69
70 static void fatal(const char *fn)
71 {
72         perror(fn);
73         error |= 1;
74         exit(error);
75 }
76
77 static int warn(const char *fn)
78 {
79         savedErrno = errno;
80         perror(fn);
81         error |= 2;
82         return error;
83 }
84
85 static int obj_compare(const void *a, const void * b)
86 {
87   objItem *oa, *ob;
88   
89   oa = (objItem *)a;
90   ob = (objItem *)b;
91   
92   if(oa->dev < ob->dev) return -1;
93   if(oa->dev > ob->dev) return 1;
94   if(oa->ino < ob->ino) return -1;
95   if(oa->ino > ob->ino) return 1;
96   
97   return 0;
98 }
99
100
101 static void add_obj_to_list(dev_t dev, ino_t ino, int obj)
102 {
103         if(n_obj < MAX_OBJECTS)
104         {
105                 obj_list[n_obj].dev = dev;
106                 obj_list[n_obj].ino = ino;
107                 obj_list[n_obj].obj = obj;
108                 n_obj++;
109                 qsort(obj_list,n_obj,sizeof(objItem),obj_compare);
110                 
111         }
112         else
113         {
114                 // oops! not enough space in the object array
115                 fprintf(stderr,"Not enough space in object array\n");
116                 exit(1);
117         }
118 }
119
120
121 static int find_obj_in_list(dev_t dev, ino_t ino)
122 {
123         objItem *i = NULL;
124         objItem test;
125
126         test.dev = dev;
127         test.ino = ino;
128         
129         if(n_obj > 0)
130         {
131                 i = bsearch(&test,obj_list,n_obj,sizeof(objItem),obj_compare);
132         }
133
134         if(i)
135         {
136                 return i->obj;
137         }
138         return -1;
139 }
140
141 /* This little function converts a little endian tag to a big endian tag.
142  * NOTE: The tag is not usable after this other than calculating the CRC
143  * with.
144  */
145 static void little_to_big_endian(struct yaffs_ext_tags *tagsPtr)
146 {
147 #if 0 // FIXME NCB
148     union yaffs_tags_union * tags = (union yaffs_tags_union* )tagsPtr; // Work in bytes.
149     union yaffs_tags_union   temp;
150
151     memset(&temp, 0, sizeof(temp));
152     // Ick, I hate magic numbers.
153     temp.as_bytes[0] = ((tags->as_bytes[2] & 0x0F) << 4) | ((tags->as_bytes[1] & 0xF0) >> 4);
154     temp.as_bytes[1] = ((tags->as_bytes[1] & 0x0F) << 4) | ((tags->as_bytes[0] & 0xF0) >> 4);
155     temp.as_bytes[2] = ((tags->as_bytes[0] & 0x0F) << 4) | ((tags->as_bytes[2] & 0x30) >> 2) | ((tags->as_bytes[3] & 0xC0) >> 6);
156     temp.as_bytes[3] = ((tags->as_bytes[3] & 0x3F) << 2) | ((tags->as_bytes[2] & 0xC0) >> 6);
157     temp.as_bytes[4] = ((tags->as_bytes[6] & 0x03) << 6) | ((tags->as_bytes[5] & 0xFC) >> 2);
158     temp.as_bytes[5] = ((tags->as_bytes[5] & 0x03) << 6) | ((tags->as_bytes[4] & 0xFC) >> 2);
159     temp.as_bytes[6] = ((tags->as_bytes[4] & 0x03) << 6) | (tags->as_bytes[7] & 0x3F);
160     temp.as_bytes[7] = (tags->as_bytes[6] & 0xFC) | ((tags->as_bytes[7] & 0xC0) >> 6);
161
162     // Now copy it back.
163     tags->as_bytes[0] = temp.as_bytes[0];
164     tags->as_bytes[1] = temp.as_bytes[1];
165     tags->as_bytes[2] = temp.as_bytes[2];
166     tags->as_bytes[3] = temp.as_bytes[3];
167     tags->as_bytes[4] = temp.as_bytes[4];
168     tags->as_bytes[5] = temp.as_bytes[5];
169     tags->as_bytes[6] = temp.as_bytes[6];
170     tags->as_bytes[7] = temp.as_bytes[7];
171 #endif
172 }
173
174 static void shuffle_oob(char *spareData, struct yaffs_packed_tags2 *pt)
175 {
176         assert(sizeof(*pt) <= spareSize);
177         // NAND LAYOUT: For non-trivial OOB orderings, here would be a good place to shuffle.
178         memcpy(spareData, pt, sizeof(*pt));
179 }
180
181 static int write_chunk(u8 *data, u32 id, u32 chunk_id, u32 n_bytes)
182 {
183         struct yaffs_ext_tags t;
184         struct yaffs_packed_tags2 pt;
185         char spareData[spareSize];
186
187         if (write(outFile,data,chunkSize) != chunkSize)
188                 fatal("write");
189
190         memset(&t, 0, sizeof(t));
191         
192         t.chunk_id = chunk_id;
193 //      t.serial_number = 0;
194         t.serial_number = 1;    // **CHECK**
195         t.n_bytes = n_bytes;
196         t.obj_id = id;
197         
198         t.seq_number = YAFFS_LOWEST_SEQUENCE_NUMBER;
199
200 // added NCB **CHECK**
201         t.chunk_used = 1;
202
203         if (convert_endian)
204         {
205             little_to_big_endian(&t);
206         }
207
208         nPages++;
209
210         memset(&pt, 0, sizeof(pt));
211         yaffs_pack_tags2(&pt,&t,1);
212
213         memset(spareData, 0xff, sizeof(spareData));
214         shuffle_oob(spareData, &pt);
215
216         if (write(outFile,spareData,sizeof(spareData)) != sizeof(spareData))
217                 fatal("write");
218         return 0;
219 }
220
221 #define SWAP32(x)   ((((x) & 0x000000FF) << 24) | \
222                      (((x) & 0x0000FF00) << 8 ) | \
223                      (((x) & 0x00FF0000) >> 8 ) | \
224                      (((x) & 0xFF000000) >> 24))
225
226 #define SWAP16(x)   ((((x) & 0x00FF) << 8) | \
227                      (((x) & 0xFF00) >> 8))
228         
229 // This one is easier, since the types are more standard. No funky shifts here.
230 static void object_header_little_to_big_endian(struct yaffs_obj_hdr* oh)
231 {
232     oh->type = SWAP32(oh->type); // GCC makes enums 32 bits.
233     oh->parent_obj_id = SWAP32(oh->parent_obj_id); // int
234     oh->sum_no_longer_used = SWAP16(oh->sum_no_longer_used); // u16 - Not used, but done for completeness.
235     // name = skip. Char array. Not swapped.
236     oh->yst_mode = SWAP32(oh->yst_mode);
237
238     // Regular POSIX.
239     oh->yst_uid = SWAP32(oh->yst_uid);
240     oh->yst_gid = SWAP32(oh->yst_gid);
241     oh->yst_atime = SWAP32(oh->yst_atime);
242     oh->yst_mtime = SWAP32(oh->yst_mtime);
243     oh->yst_ctime = SWAP32(oh->yst_ctime);
244
245     oh->file_size = SWAP32(oh->file_size); // Aiee. An int... signed, at that!
246     oh->equiv_id = SWAP32(oh->equiv_id);
247     // alias  - char array.
248     oh->yst_rdev = SWAP32(oh->yst_rdev);
249
250     oh->win_ctime[0] = SWAP32(oh->win_ctime[0]);
251     oh->win_ctime[1] = SWAP32(oh->win_ctime[1]);
252     oh->win_atime[0] = SWAP32(oh->win_atime[0]);
253     oh->win_atime[1] = SWAP32(oh->win_atime[1]);
254     oh->win_mtime[0] = SWAP32(oh->win_mtime[0]);
255     oh->win_mtime[1] = SWAP32(oh->win_mtime[1]);
256
257     oh->reserved[0] = SWAP32(oh->reserved[0]);
258     oh->reserved[1] = SWAP32(oh->reserved[1]);
259
260     oh->inband_shadowed_obj_id = SWAP32(oh->inband_shadowed_obj_id);
261     oh->inband_is_shrink = SWAP32(oh->inband_is_shrink);
262     oh->shadows_obj = SWAP32(oh->shadows_obj);
263     oh->is_shrink = SWAP32(oh->is_shrink);
264 }
265
266
267 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)
268 {
269         u8 bytes[chunkSize];
270         
271         
272         struct yaffs_obj_hdr *oh = (struct yaffs_obj_hdr *)bytes;
273         
274         memset(bytes,0xff,sizeof(bytes));
275         
276         oh->type = t;
277
278         oh->parent_obj_id = parent;
279         
280         if (strlen(name)+1 > sizeof(oh->name))
281         {
282                 errno = ENAMETOOLONG;
283                 return warn("object name");
284         }
285         memset(oh->name,0,sizeof(oh->name));
286         strcpy(oh->name,name);
287         
288         
289         if(t != YAFFS_OBJECT_TYPE_HARDLINK)
290         {
291                 oh->yst_mode = s->st_mode;
292                 oh->yst_uid = s->st_uid;
293 // NCB 12/9/02          oh->yst_gid = s->yst_uid;
294                 oh->yst_gid = s->st_gid;
295                 oh->yst_atime = s->st_atime;
296                 oh->yst_mtime = s->st_mtime;
297                 oh->yst_ctime = s->st_ctime;
298                 oh->yst_rdev  = s->st_rdev;
299         }
300         
301         if(t == YAFFS_OBJECT_TYPE_FILE)
302         {
303                 oh->file_size = s->st_size;
304         }
305         
306         if(t == YAFFS_OBJECT_TYPE_HARDLINK)
307         {
308                 oh->equiv_id = equivalentObj;
309         }
310         
311         if(t == YAFFS_OBJECT_TYPE_SYMLINK)
312         {
313                 if (strlen(alias)+1 > sizeof(oh->alias))
314                 {
315                         errno = ENAMETOOLONG;
316                         return warn("object alias");
317                 }
318                 memset(oh->alias,0,sizeof(oh->alias));
319                 strcpy(oh->alias,alias);
320         }
321
322         if (convert_endian)
323         {
324                 object_header_little_to_big_endian(oh);
325         }
326         
327         return write_chunk(bytes,id,0,0xffff);
328         
329 }
330
331 static void pad_image(void)
332 {
333         u8 data[chunkSize + spareSize];
334         int padPages = (nPages % pagesPerBlock);
335
336         if (padPages)
337         {
338                 memset(data, 0xff, sizeof(data));
339                 for (padPages = pagesPerBlock-padPages; padPages; padPages--)
340                 {
341                         if (write(outFile, data, sizeof(data)) != sizeof(data))
342                                 fatal("write");
343                 }
344         }
345 }
346
347 static int process_directory(int parent, const char *path)
348 {
349
350         DIR *dir;
351         struct dirent *entry;
352
353         nDirectories++;
354         
355         dir = opendir(path);
356         if(!dir)
357         {
358                 warn("opendir");
359         }
360         else
361         {
362                 while((entry = readdir(dir)) != NULL)
363                 {
364                 
365                         /* Ignore . and .. */
366                         if(strcmp(entry->d_name,".") &&
367                            strcmp(entry->d_name,".."))
368                         {
369                                 char full_name[500];
370                                 struct stat stats;
371                                 int equivalentObj;
372                                 int newObj;
373                                 
374                                 if (snprintf(full_name,sizeof(full_name),"%s/%s",path,entry->d_name) >= (int)sizeof(full_name))
375                                 {
376                                         error = -1;
377                                         continue;
378                                 }
379                                 
380                                 if (lstat(full_name,&stats) < 0)
381                                 {
382                                         warn("lstat");
383                                         continue;
384                                 }
385                                 
386                                 if(S_ISLNK(stats.st_mode) ||
387                                     S_ISREG(stats.st_mode) ||
388                                     S_ISDIR(stats.st_mode) ||
389                                     S_ISFIFO(stats.st_mode) ||
390                                     S_ISBLK(stats.st_mode) ||
391                                     S_ISCHR(stats.st_mode) ||
392                                     S_ISSOCK(stats.st_mode))
393                                 {
394                                 
395                                         newObj = obj_id++;
396                                         n_obj++;
397                                         
398                                         printf("Object %d, %s is a ",newObj,full_name);
399                                         
400                                         /* We're going to create an object for it */
401                                         if((equivalentObj = find_obj_in_list(stats.st_dev, stats.st_ino)) > 0)
402                                         {
403                                                 /* we need to make a hard link */
404                                                 printf("hard link to object %d\n",equivalentObj);
405                                                 write_object_header(newObj, YAFFS_OBJECT_TYPE_HARDLINK, &stats, parent, entry->d_name, equivalentObj, NULL);
406                                         }
407                                         else 
408                                         {
409                                                 
410                                                 add_obj_to_list(stats.st_dev,stats.st_ino,newObj);
411                                                 
412                                                 if(S_ISLNK(stats.st_mode))
413                                                 {
414                                         
415                                                         char symname[500];
416                                                 
417                                                         memset(symname,0, sizeof(symname));
418                                         
419                                                         if (readlink(full_name,symname,sizeof(symname) -1) < 0)
420                                                         {
421                                                                 warn("readlink");
422                                                         }
423                                                         else
424                                                         {
425                                                                 printf("symlink to \"%s\"\n",symname);
426                                                                 write_object_header(newObj, YAFFS_OBJECT_TYPE_SYMLINK, &stats, parent, entry->d_name, -1, symname);
427                                                         }
428                                                 }
429                                                 else if(S_ISREG(stats.st_mode))
430                                                 {
431                                                         printf("file, ");
432                                                         if(write_object_header(newObj, YAFFS_OBJECT_TYPE_FILE, &stats, parent, entry->d_name, -1, NULL) == 0)
433                                                         {
434                                                                 int h;
435                                                                 u8 bytes[chunkSize];
436                                                                 int n_bytes;
437                                                                 int chunk = 0;
438                                                                 
439                                                                 h = open(full_name,O_RDONLY);
440                                                                 if(h >= 0)
441                                                                 {
442                                                                         memset(bytes,0xff,sizeof(bytes));
443                                                                         while((n_bytes = read(h,bytes,sizeof(bytes))) > 0)
444                                                                         {
445                                                                                 chunk++;
446                                                                                 write_chunk(bytes,newObj,chunk,n_bytes);
447                                                                                 memset(bytes,0xff,sizeof(bytes));
448                                                                         }
449                                                                         if(n_bytes < 0) 
450                                                                            warn("read");
451                                                                            
452                                                                         printf("%d data chunks written\n",chunk);
453                                                                         close(h);
454                                                                 }
455                                                                 else
456                                                                 {
457                                                                         warn("open");
458                                                                 }
459                                                                 
460                                                         }                                                       
461                                                                                                                 
462                                                 }
463                                                 else if(S_ISSOCK(stats.st_mode))
464                                                 {
465                                                         printf("socket\n");
466                                                         write_object_header(newObj, YAFFS_OBJECT_TYPE_SPECIAL, &stats, parent, entry->d_name, -1, NULL);
467                                                 }
468                                                 else if(S_ISFIFO(stats.st_mode))
469                                                 {
470                                                         printf("fifo\n");
471                                                         write_object_header(newObj, YAFFS_OBJECT_TYPE_SPECIAL, &stats, parent, entry->d_name, -1, NULL);
472                                                 }
473                                                 else if(S_ISCHR(stats.st_mode))
474                                                 {
475                                                         printf("character device\n");
476                                                         write_object_header(newObj, YAFFS_OBJECT_TYPE_SPECIAL, &stats, parent, entry->d_name, -1, NULL);
477                                                 }
478                                                 else if(S_ISBLK(stats.st_mode))
479                                                 {
480                                                         printf("block device\n");
481                                                         write_object_header(newObj, YAFFS_OBJECT_TYPE_SPECIAL, &stats, parent, entry->d_name, -1, NULL);
482                                                 }
483                                                 else if(S_ISDIR(stats.st_mode))
484                                                 {
485                                                         printf("directory\n");
486                                                         if (write_object_header(newObj, YAFFS_OBJECT_TYPE_DIRECTORY, &stats, parent, entry->d_name, -1, NULL) == 0)
487                                                                 process_directory(newObj,full_name);
488                                                 }
489                                         }
490                                 }
491                                 else
492                                 {
493                                         fprintf(stderr, "%s: unhandled type\n", full_name);
494                                         error |= 2;
495                                         savedErrno = EINVAL;
496                                 }
497                         }
498                 }
499                 closedir(dir);
500         }
501         
502         return 0;
503
504 }
505
506
507 int main(int argc, char *argv[])
508 {
509         struct stat stats;
510         
511         printf("mkyaffs2image: image building tool for YAFFS2 built "__DATE__"\n");
512         
513         if(argc < 3)
514         {
515                 printf("usage: mkyaffs2image dir image_file [convert]\n");
516                 printf("           dir        the directory tree to be converted\n");
517                 printf("           image_file the output file to hold the image\n");
518         printf("           'convert'  produce a big-endian image from a little-endian machine\n");
519                 exit(1);
520         }
521
522     if ((argc == 4) && (!strncmp(argv[3], "convert", strlen("convert"))))
523     {
524         convert_endian = 1;
525     }
526     
527         if(stat(argv[1],&stats) < 0)
528         {
529                 printf("Could not stat %s\n",argv[1]);
530                 exit(1);
531         }
532         
533         if(!S_ISDIR(stats.st_mode))
534         {
535                 printf(" %s is not a directory\n",argv[1]);
536                 exit(1);
537         }
538         
539         outFile = open(argv[2],O_CREAT | O_TRUNC | O_WRONLY, S_IREAD | S_IWRITE);
540         
541         
542         if(outFile < 0)
543         {
544                 printf("Could not open output file %s\n",argv[2]);
545                 exit(1);
546         }
547         
548         printf("Processing directory %s into image file %s\n",argv[1],argv[2]);
549         process_directory(YAFFS_OBJECTID_ROOT,argv[1]);
550         
551         pad_image();
552
553         close(outFile);
554         
555         if(error)
556         {
557                 errno = savedErrno;
558                 perror("operation incomplete");
559         }
560         else
561         {
562                 printf("Operation complete.\n"
563                        "%d objects in %d directories\n"
564                        "%d NAND pages\n",n_obj, nDirectories, nPages);
565         }
566         
567         exit(error);
568 }       
569