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