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