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