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