*** 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         
178         t.chunkId = chunkId;
179         t.serialNumber = 0;
180         t.byteCount = nBytes;
181         t.objectId = objId;
182         
183         yaffs_CalcTagsECC(&t);
184         yaffs_LoadTagsIntoSpare(&s,&t);
185         yaffs_CalcECC(data,&s);
186         
187         nPages++;
188         
189         return write(outFile,&s,sizeof(yaffs_Spare));
190         
191 }
192
193 int write_object_header(int objId, yaffs_ObjectType t, struct stat *s, int parent, const char *name, int equivalentObj, const char * alias)
194 {
195         __u8 bytes[512];
196         
197         
198         yaffs_ObjectHeader *oh = (yaffs_ObjectHeader *)bytes;
199         
200         memset(bytes,0xff,512);
201         
202         oh->type = t;
203
204         oh->parentObjectId = parent;
205         
206         strncpy(oh->name,name,YAFFS_MAX_NAME_LENGTH);
207         
208         if(t != YAFFS_OBJECT_TYPE_HARDLINK)
209         {
210                 oh->st_mode = s->st_mode;
211                 oh->st_uid = s->st_uid;
212                 oh->st_gid = s->st_uid;
213                 oh->st_atime = s->st_atime;
214                 oh->st_mtime = s->st_mtime;
215                 oh->st_ctime = s->st_ctime;
216                 oh->st_rdev  = s->st_rdev;
217         }
218         
219         if(t == YAFFS_OBJECT_TYPE_FILE)
220         {
221                 oh->fileSize = s->st_size;
222         }
223         
224         if(t == YAFFS_OBJECT_TYPE_HARDLINK)
225         {
226                 oh->equivalentObjectId = equivalentObj;
227         }
228         
229         if(t == YAFFS_OBJECT_TYPE_SYMLINK)
230         {
231                 strncpy(oh->alias,alias,YAFFS_MAX_ALIAS_LENGTH);
232         }
233         
234         return write_chunk(bytes,objId,0,0xffff);
235         
236 }
237
238
239 int process_directory(int parent, const char *path)
240 {
241
242         DIR *dir;
243         struct dirent *entry;
244
245         nDirectories++; 
246         
247         dir = opendir(path);
248         
249         if(dir)
250         {
251                 while((entry = readdir(dir)) != NULL)
252                 {
253                 
254                         /* Ignore . and .. */
255                         if(strcmp(entry->d_name,".") &&
256                            strcmp(entry->d_name,".."))
257                         {
258                                 char full_name[500];
259                                 struct stat stats;
260                                 int equivalentObj;
261                                 int newObj;
262                                 
263                                 sprintf(full_name,"%s/%s",path,entry->d_name);
264                                 
265                                 lstat(full_name,&stats);
266                                 
267                                 if(S_ISLNK(stats.st_mode) ||
268                                     S_ISREG(stats.st_mode) ||
269                                     S_ISDIR(stats.st_mode) ||
270                                     S_ISFIFO(stats.st_mode) ||
271                                     S_ISBLK(stats.st_mode) ||
272                                     S_ISCHR(stats.st_mode) ||
273                                     S_ISSOCK(stats.st_mode))
274                                 {
275                                 
276                                         newObj = obj_id++;
277                                         nObjects++;
278                                         
279                                         printf("Object %d, %s is a ",newObj,full_name);
280                                         
281                                         /* We're going to create an object for it */
282                                         if((equivalentObj = find_obj_in_list(stats.st_dev, stats.st_ino)) > 0)
283                                         {
284                                                 /* we need to make a hard link */
285                                                 printf("hard link to object %d\n",equivalentObj);
286                                                 error =  write_object_header(newObj, YAFFS_OBJECT_TYPE_HARDLINK, &stats, parent, entry->d_name, equivalentObj, NULL);
287                                         }
288                                         else 
289                                         {
290                                                 
291                                                 add_obj_to_list(stats.st_dev,stats.st_ino,newObj);
292                                                 
293                                                 if(S_ISLNK(stats.st_mode))
294                                                 {
295                                         
296                                                         char symname[500];
297                                                 
298                                                         memset(symname,0,500);
299                                         
300                                                         readlink(full_name,symname,499);
301                                                 
302                                                         printf("symlink to \"%s\"\n",symname);
303                                                         error =  write_object_header(newObj, YAFFS_OBJECT_TYPE_SYMLINK, &stats, parent, entry->d_name, -1, symname);
304
305                                                 }
306                                                 else if(S_ISREG(stats.st_mode))
307                                                 {
308                                                         printf("file, ");
309                                                         error =  write_object_header(newObj, YAFFS_OBJECT_TYPE_FILE, &stats, parent, entry->d_name, -1, NULL);
310
311                                                         if(error >= 0)
312                                                         {
313                                                                 int h;
314                                                                 __u8 bytes[512];
315                                                                 int nBytes;
316                                                                 int chunk = 0;
317                                                                 
318                                                                 h = open(full_name,O_RDONLY);
319                                                                 if(h >= 0)
320                                                                 {
321                                                                         memset(bytes,0xff,512);
322                                                                         while((nBytes = read(h,bytes,512)) > 0)
323                                                                         {
324                                                                                 chunk++;
325                                                                                 write_chunk(bytes,newObj,chunk,nBytes);
326                                                                                 memset(bytes,0xff,512);
327                                                                         }
328                                                                         if(nBytes < 0) 
329                                                                            error = nBytes;
330                                                                            
331                                                                         printf("%d data chunks written\n",chunk);
332                                                                 }
333                                                                 else
334                                                                 {
335                                                                         perror("Error opening file");
336                                                                 }
337                                                                 close(h);
338                                                                 
339                                                         }                                                       
340                                                                                                                 
341                                                 }
342                                                 else if(S_ISSOCK(stats.st_mode))
343                                                 {
344                                                         printf("socket\n");
345                                                         error =  write_object_header(newObj, YAFFS_OBJECT_TYPE_SPECIAL, &stats, parent, entry->d_name, -1, NULL);
346                                                 }
347                                                 else if(S_ISFIFO(stats.st_mode))
348                                                 {
349                                                         printf("fifo\n");
350                                                         error =  write_object_header(newObj, YAFFS_OBJECT_TYPE_SPECIAL, &stats, parent, entry->d_name, -1, NULL);
351                                                 }
352                                                 else if(S_ISCHR(stats.st_mode))
353                                                 {
354                                                         printf("character device\n");
355                                                         error =  write_object_header(newObj, YAFFS_OBJECT_TYPE_SPECIAL, &stats, parent, entry->d_name, -1, NULL);
356                                                 }
357                                                 else if(S_ISBLK(stats.st_mode))
358                                                 {
359                                                         printf("block device\n");
360                                                         error =  write_object_header(newObj, YAFFS_OBJECT_TYPE_SPECIAL, &stats, parent, entry->d_name, -1, NULL);
361                                                 }
362                                                 else if(S_ISDIR(stats.st_mode))
363                                                 {
364                                                         printf("directory\n");
365                                                         error =  write_object_header(newObj, YAFFS_OBJECT_TYPE_DIRECTORY, &stats, parent, entry->d_name, -1, NULL);
366                                                         process_directory(1,full_name);
367                                                 }
368                                         }
369                                 }
370                                 else
371                                 {
372                                         printf(" we don't handle this type\n");
373                                 }
374                         }
375                 }
376         }
377         
378         return 0;
379
380 }
381
382
383 int main(int argc, char *argv[])
384 {
385         struct stat stats;
386         
387         printf("mkyaffsimage: image building tool for YAFFS built "__DATE__"\n");
388         
389         if(argc != 3)
390         {
391                 printf("usage: mkyaffsimage dir image_file\n");
392                 printf("           dir        the directory tree to be converted\n");
393                 printf("           image_file the ouput file to hold the image\n");
394                 exit(1);
395         }
396
397         if(stat(argv[1],&stats) < 0)
398         {
399                 printf("Could not stat %s\n",argv[1]);
400                 exit(1);
401         }
402         
403         if(!S_ISDIR(stats.st_mode))
404         {
405                 printf(" %s is not a directory\n",argv[1]);
406                 exit(1);
407         }
408         
409         outFile = open(argv[2],O_CREAT | O_TRUNC | O_WRONLY, S_IREAD | S_IWRITE);
410         
411         
412         if(outFile < 0)
413         {
414                 printf("Could not open output file %s\n",argv[2]);
415                 exit(1);
416         }
417         
418         printf("Processing directory %s into image file %s\n",argv[1],argv[2]);
419
420         error = process_directory(YAFFS_OBJECTID_ROOT,argv[1]);
421         
422         close(outFile);
423         
424         if(error < 0)
425         {
426                 perror("operation incomplete");
427                 exit(1);
428         }
429         else
430         {
431                 printf("Operation complete.\n"
432                        "%d objects in %d directories\n"
433                        "%d NAND pages\n",nObjects, nDirectories, nPages);
434         }
435         
436         close(outFile);
437         
438         exit(0);
439 }       
440