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