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