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