*** empty log message ***
[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  
19 #include <stdlib.h>
20 #include <stdio.h>
21 #include <fcntl.h>
22 #include <sys/types.h>
23 #include <sys/stat.h>
24 #include <dirent.h>
25 #include <string.h>
26 #include <unistd.h>
27
28 #include "yaffs_guts.h"
29
30
31 #define MAX_OBJECTS 10000
32
33 // External functions for ECC on data
34 void nand_calculate_ecc (const u_char *dat, u_char *ecc_code);
35 int nand_correct_data (u_char *dat, u_char *read_ecc, u_char *calc_ecc);
36
37
38
39
40
41 typedef struct
42 {
43         dev_t dev;
44         ino_t ino;
45         int   obj;
46 } objItem;
47
48
49 static objItem obj_list[MAX_OBJECTS];
50 static int n_obj = 0;
51 static int obj_id = YAFFS_NOBJECT_BUCKETS + 1;
52
53 static int nObjects, nDirectories, nPages;
54
55 static int outFile;
56
57 static int error;
58
59
60 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 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 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
117
118 void yaffs_CalcECC(const __u8 *data, yaffs_Spare *spare)
119 {
120         nand_calculate_ecc (data , spare->ecc1);
121         nand_calculate_ecc (&data[256] , spare->ecc2);
122 }
123
124 void yaffs_CalcTagsECC(yaffs_Tags *tags)
125 {
126         // Todo don't do anything yet. Need to calculate ecc
127         unsigned char *b = ((yaffs_TagsUnion *)tags)->asBytes;
128         unsigned  i,j;
129         unsigned  ecc = 0;
130         unsigned bit = 0;
131
132         tags->ecc = 0;
133         
134         for(i = 0; i < 8; i++)
135         {
136                 for(j = 1; j &0x7f; j<<=1)
137                 {
138                         bit++;
139                         if(b[i] & j)
140                         {
141                                 ecc ^= bit;
142                         }
143                 }
144         }
145         
146         tags->ecc = ecc;
147         
148         
149 }
150 static void yaffs_LoadTagsIntoSpare(yaffs_Spare *sparePtr, yaffs_Tags *tagsPtr)
151 {
152         yaffs_TagsUnion *tu = (yaffs_TagsUnion *)tagsPtr;
153         
154         yaffs_CalcTagsECC(tagsPtr);
155         
156         sparePtr->tagByte0 = tu->asBytes[0];
157         sparePtr->tagByte1 = tu->asBytes[1];
158         sparePtr->tagByte2 = tu->asBytes[2];
159         sparePtr->tagByte3 = tu->asBytes[3];
160         sparePtr->tagByte4 = tu->asBytes[4];
161         sparePtr->tagByte5 = tu->asBytes[5];
162         sparePtr->tagByte6 = tu->asBytes[6];
163         sparePtr->tagByte7 = tu->asBytes[7];
164 }
165
166
167
168 int write_chunk(__u8 *data, __u32 objId, __u32 chunkId, __u32 nBytes)
169 {
170         yaffs_Tags t;
171         yaffs_Spare s;
172         
173         error = write(outFile,data,512);
174         if(error < 0) return error;
175         
176         memset(&t,0xff,sizeof (yaffs_Tags));
177         memset(&s,0xff,sizeof (yaffs_Spare));
178         
179         t.chunkId = chunkId;
180         t.serialNumber = 0;
181         t.byteCount = nBytes;
182         t.objectId = objId;
183         
184         yaffs_CalcTagsECC(&t);
185         yaffs_LoadTagsIntoSpare(&s,&t);
186         yaffs_CalcECC(data,&s);
187         
188         nPages++;
189         
190         return write(outFile,&s,sizeof(yaffs_Spare));
191         
192 }
193
194 int write_object_header(int objId, yaffs_ObjectType t, struct stat *s, int parent, const char *name, int equivalentObj, const char * alias)
195 {
196         __u8 bytes[512];
197         
198         
199         yaffs_ObjectHeader *oh = (yaffs_ObjectHeader *)bytes;
200         
201         memset(bytes,0xff,512);
202         
203         oh->type = t;
204
205         oh->parentObjectId = parent;
206         
207         strncpy(oh->name,name,YAFFS_MAX_NAME_LENGTH);
208         
209         if(t != YAFFS_OBJECT_TYPE_HARDLINK)
210         {
211                 oh->st_mode = s->st_mode;
212                 oh->st_uid = s->st_uid;
213                 oh->st_gid = s->st_uid;
214                 oh->st_atime = s->st_atime;
215                 oh->st_mtime = s->st_mtime;
216                 oh->st_ctime = s->st_ctime;
217                 oh->st_rdev  = s->st_rdev;
218         }
219         
220         if(t == YAFFS_OBJECT_TYPE_FILE)
221         {
222                 oh->fileSize = s->st_size;
223         }
224         
225         if(t == YAFFS_OBJECT_TYPE_HARDLINK)
226         {
227                 oh->equivalentObjectId = equivalentObj;
228         }
229         
230         if(t == YAFFS_OBJECT_TYPE_SYMLINK)
231         {
232                 strncpy(oh->alias,alias,YAFFS_MAX_ALIAS_LENGTH);
233         }
234         
235         return write_chunk(bytes,objId,0,0xffff);
236         
237 }
238
239
240 int process_directory(int parent, const char *path)
241 {
242
243         DIR *dir;
244         struct dirent *entry;
245
246         nDirectories++; 
247         
248         dir = opendir(path);
249         
250         if(dir)
251         {
252                 while((entry = readdir(dir)) != NULL)
253                 {
254                 
255                         /* Ignore . and .. */
256                         if(strcmp(entry->d_name,".") &&
257                            strcmp(entry->d_name,".."))
258                         {
259                                 char full_name[500];
260                                 struct stat stats;
261                                 int equivalentObj;
262                                 int newObj;
263                                 
264                                 sprintf(full_name,"%s/%s",path,entry->d_name);
265                                 
266                                 lstat(full_name,&stats);
267                                 
268                                 if(S_ISLNK(stats.st_mode) ||
269                                     S_ISREG(stats.st_mode) ||
270                                     S_ISDIR(stats.st_mode) ||
271                                     S_ISFIFO(stats.st_mode) ||
272                                     S_ISBLK(stats.st_mode) ||
273                                     S_ISCHR(stats.st_mode) ||
274                                     S_ISSOCK(stats.st_mode))
275                                 {
276                                 
277                                         newObj = obj_id++;
278                                         nObjects++;
279                                         
280                                         printf("Object %d, %s is a ",newObj,full_name);
281                                         
282                                         /* We're going to create an object for it */
283                                         if((equivalentObj = find_obj_in_list(stats.st_dev, stats.st_ino)) > 0)
284                                         {
285                                                 /* we need to make a hard link */
286                                                 printf("hard link to object %d\n",equivalentObj);
287                                                 error =  write_object_header(newObj, YAFFS_OBJECT_TYPE_HARDLINK, &stats, parent, entry->d_name, equivalentObj, NULL);
288                                         }
289                                         else 
290                                         {
291                                                 
292                                                 add_obj_to_list(stats.st_dev,stats.st_ino,newObj);
293                                                 
294                                                 if(S_ISLNK(stats.st_mode))
295                                                 {
296                                         
297                                                         char symname[500];
298                                                 
299                                                         memset(symname,0,500);
300                                         
301                                                         readlink(full_name,symname,499);
302                                                 
303                                                         printf("symlink to \"%s\"\n",symname);
304                                                         error =  write_object_header(newObj, YAFFS_OBJECT_TYPE_SYMLINK, &stats, parent, entry->d_name, -1, symname);
305
306                                                 }
307                                                 else if(S_ISREG(stats.st_mode))
308                                                 {
309                                                         printf("file, ");
310                                                         error =  write_object_header(newObj, YAFFS_OBJECT_TYPE_FILE, &stats, parent, entry->d_name, -1, NULL);
311
312                                                         if(error >= 0)
313                                                         {
314                                                                 int h;
315                                                                 __u8 bytes[512];
316                                                                 int nBytes;
317                                                                 int chunk = 0;
318                                                                 
319                                                                 h = open(full_name,O_RDONLY);
320                                                                 if(h >= 0)
321                                                                 {
322                                                                         memset(bytes,0xff,512);
323                                                                         while((nBytes = read(h,bytes,512)) > 0)
324                                                                         {
325                                                                                 chunk++;
326                                                                                 write_chunk(bytes,newObj,chunk,nBytes);
327                                                                                 memset(bytes,0xff,512);
328                                                                         }
329                                                                         if(nBytes < 0) 
330                                                                            error = nBytes;
331                                                                            
332                                                                         printf("%d data chunks written\n",chunk);
333                                                                 }
334                                                                 else
335                                                                 {
336                                                                         perror("Error opening file");
337                                                                 }
338                                                                 close(h);
339                                                                 
340                                                         }                                                       
341                                                                                                                 
342                                                 }
343                                                 else if(S_ISSOCK(stats.st_mode))
344                                                 {
345                                                         printf("socket\n");
346                                                         error =  write_object_header(newObj, YAFFS_OBJECT_TYPE_SPECIAL, &stats, parent, entry->d_name, -1, NULL);
347                                                 }
348                                                 else if(S_ISFIFO(stats.st_mode))
349                                                 {
350                                                         printf("fifo\n");
351                                                         error =  write_object_header(newObj, YAFFS_OBJECT_TYPE_SPECIAL, &stats, parent, entry->d_name, -1, NULL);
352                                                 }
353                                                 else if(S_ISCHR(stats.st_mode))
354                                                 {
355                                                         printf("character device\n");
356                                                         error =  write_object_header(newObj, YAFFS_OBJECT_TYPE_SPECIAL, &stats, parent, entry->d_name, -1, NULL);
357                                                 }
358                                                 else if(S_ISBLK(stats.st_mode))
359                                                 {
360                                                         printf("block device\n");
361                                                         error =  write_object_header(newObj, YAFFS_OBJECT_TYPE_SPECIAL, &stats, parent, entry->d_name, -1, NULL);
362                                                 }
363                                                 else if(S_ISDIR(stats.st_mode))
364                                                 {
365                                                         printf("directory\n");
366                                                         error =  write_object_header(newObj, YAFFS_OBJECT_TYPE_DIRECTORY, &stats, parent, entry->d_name, -1, NULL);
367                                                         process_directory(1,full_name);
368                                                 }
369                                         }
370                                 }
371                                 else
372                                 {
373                                         printf(" we don't handle this type\n");
374                                 }
375                         }
376                 }
377         }
378         
379         return 0;
380
381 }
382
383
384 int main(int argc, char *argv[])
385 {
386         struct stat stats;
387         
388         printf("mkyaffsimage: image building tool for YAFFS built "__DATE__"\n");
389         
390         if(argc != 3)
391         {
392                 printf("usage: mkyaffsimage dir image_file\n");
393                 printf("           dir        the directory tree to be converted\n");
394                 printf("           image_file the ouput file to hold the image\n");
395                 exit(1);
396         }
397
398         if(stat(argv[1],&stats) < 0)
399         {
400                 printf("Could not stat %s\n",argv[1]);
401                 exit(1);
402         }
403         
404         if(!S_ISDIR(stats.st_mode))
405         {
406                 printf(" %s is not a directory\n",argv[1]);
407                 exit(1);
408         }
409         
410         outFile = open(argv[2],O_CREAT | O_TRUNC | O_WRONLY, S_IREAD | S_IWRITE);
411         
412         
413         if(outFile < 0)
414         {
415                 printf("Could not open output file %s\n",argv[2]);
416                 exit(1);
417         }
418         
419         printf("Processing directory %s into image file %s\n",argv[1],argv[2]);
420
421         error = process_directory(YAFFS_OBJECTID_ROOT,argv[1]);
422         
423         close(outFile);
424         
425         if(error < 0)
426         {
427                 perror("operation incomplete");
428                 exit(1);
429         }
430         else
431         {
432                 printf("Operation complete.\n"
433                        "%d objects in %d directories\n"
434                        "%d NAND pages\n",nObjects, nDirectories, nPages);
435         }
436         
437         close(outFile);
438         
439         exit(0);
440 }       
441