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