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