*** 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  * 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.5 2002-11-28 07:24:32 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         
229         if(t != YAFFS_OBJECT_TYPE_HARDLINK)
230         {
231                 oh->st_mode = s->st_mode;
232                 oh->st_uid = s->st_uid;
233 // NCB 12/9/02          oh->st_gid = s->st_uid;
234                 oh->st_gid = s->st_gid;
235                 oh->st_atime = s->st_atime;
236                 oh->st_mtime = s->st_mtime;
237                 oh->st_ctime = s->st_ctime;
238                 oh->st_rdev  = s->st_rdev;
239         }
240         
241         if(t == YAFFS_OBJECT_TYPE_FILE)
242         {
243                 oh->fileSize = s->st_size;
244         }
245         
246         if(t == YAFFS_OBJECT_TYPE_HARDLINK)
247         {
248                 oh->equivalentObjectId = equivalentObj;
249         }
250         
251         if(t == YAFFS_OBJECT_TYPE_SYMLINK)
252         {
253                 strncpy(oh->alias,alias,YAFFS_MAX_ALIAS_LENGTH);
254         }
255         
256         return write_chunk(bytes,objId,0,0xffff);
257         
258 }
259
260
261 int process_directory(int parent, const char *path)
262 {
263
264         DIR *dir;
265         struct dirent *entry;
266
267         nDirectories++; 
268         
269         dir = opendir(path);
270         
271         if(dir)
272         {
273                 while((entry = readdir(dir)) != NULL)
274                 {
275                 
276                         /* Ignore . and .. */
277                         if(strcmp(entry->d_name,".") &&
278                            strcmp(entry->d_name,".."))
279                         {
280                                 char full_name[500];
281                                 struct stat stats;
282                                 int equivalentObj;
283                                 int newObj;
284                                 
285                                 sprintf(full_name,"%s/%s",path,entry->d_name);
286                                 
287                                 lstat(full_name,&stats);
288                                 
289                                 if(S_ISLNK(stats.st_mode) ||
290                                     S_ISREG(stats.st_mode) ||
291                                     S_ISDIR(stats.st_mode) ||
292                                     S_ISFIFO(stats.st_mode) ||
293                                     S_ISBLK(stats.st_mode) ||
294                                     S_ISCHR(stats.st_mode) ||
295                                     S_ISSOCK(stats.st_mode))
296                                 {
297                                 
298                                         newObj = obj_id++;
299                                         nObjects++;
300                                         
301                                         printf("Object %d, %s is a ",newObj,full_name);
302                                         
303                                         /* We're going to create an object for it */
304                                         if((equivalentObj = find_obj_in_list(stats.st_dev, stats.st_ino)) > 0)
305                                         {
306                                                 /* we need to make a hard link */
307                                                 printf("hard link to object %d\n",equivalentObj);
308                                                 error =  write_object_header(newObj, YAFFS_OBJECT_TYPE_HARDLINK, &stats, parent, entry->d_name, equivalentObj, NULL);
309                                         }
310                                         else 
311                                         {
312                                                 
313                                                 add_obj_to_list(stats.st_dev,stats.st_ino,newObj);
314                                                 
315                                                 if(S_ISLNK(stats.st_mode))
316                                                 {
317                                         
318                                                         char symname[500];
319                                                 
320                                                         memset(symname,0,500);
321                                         
322                                                         readlink(full_name,symname,499);
323                                                 
324                                                         printf("symlink to \"%s\"\n",symname);
325                                                         error =  write_object_header(newObj, YAFFS_OBJECT_TYPE_SYMLINK, &stats, parent, entry->d_name, -1, symname);
326
327                                                 }
328                                                 else if(S_ISREG(stats.st_mode))
329                                                 {
330                                                         printf("file, ");
331                                                         error =  write_object_header(newObj, YAFFS_OBJECT_TYPE_FILE, &stats, parent, entry->d_name, -1, NULL);
332
333                                                         if(error >= 0)
334                                                         {
335                                                                 int h;
336                                                                 __u8 bytes[512];
337                                                                 int nBytes;
338                                                                 int chunk = 0;
339                                                                 
340                                                                 h = open(full_name,O_RDONLY);
341                                                                 if(h >= 0)
342                                                                 {
343                                                                         memset(bytes,0xff,512);
344                                                                         while((nBytes = read(h,bytes,512)) > 0)
345                                                                         {
346                                                                                 chunk++;
347                                                                                 write_chunk(bytes,newObj,chunk,nBytes);
348                                                                                 memset(bytes,0xff,512);
349                                                                         }
350                                                                         if(nBytes < 0) 
351                                                                            error = nBytes;
352                                                                            
353                                                                         printf("%d data chunks written\n",chunk);
354                                                                 }
355                                                                 else
356                                                                 {
357                                                                         perror("Error opening file");
358                                                                 }
359                                                                 close(h);
360                                                                 
361                                                         }                                                       
362                                                                                                                 
363                                                 }
364                                                 else if(S_ISSOCK(stats.st_mode))
365                                                 {
366                                                         printf("socket\n");
367                                                         error =  write_object_header(newObj, YAFFS_OBJECT_TYPE_SPECIAL, &stats, parent, entry->d_name, -1, NULL);
368                                                 }
369                                                 else if(S_ISFIFO(stats.st_mode))
370                                                 {
371                                                         printf("fifo\n");
372                                                         error =  write_object_header(newObj, YAFFS_OBJECT_TYPE_SPECIAL, &stats, parent, entry->d_name, -1, NULL);
373                                                 }
374                                                 else if(S_ISCHR(stats.st_mode))
375                                                 {
376                                                         printf("character device\n");
377                                                         error =  write_object_header(newObj, YAFFS_OBJECT_TYPE_SPECIAL, &stats, parent, entry->d_name, -1, NULL);
378                                                 }
379                                                 else if(S_ISBLK(stats.st_mode))
380                                                 {
381                                                         printf("block device\n");
382                                                         error =  write_object_header(newObj, YAFFS_OBJECT_TYPE_SPECIAL, &stats, parent, entry->d_name, -1, NULL);
383                                                 }
384                                                 else if(S_ISDIR(stats.st_mode))
385                                                 {
386                                                         printf("directory\n");
387                                                         error =  write_object_header(newObj, YAFFS_OBJECT_TYPE_DIRECTORY, &stats, parent, entry->d_name, -1, NULL);
388 // NCB modified 10/9/2001                               process_directory(1,full_name);
389                                                         process_directory(newObj,full_name);
390                                                 }
391                                         }
392                                 }
393                                 else
394                                 {
395                                         printf(" we don't handle this type\n");
396                                 }
397                         }
398                 }
399         }
400         
401         return 0;
402
403 }
404
405
406 int main(int argc, char *argv[])
407 {
408         struct stat stats;
409         
410         printf("mkyaffsimage: image building tool for YAFFS built "__DATE__"\n");
411         
412         if(argc != 3)
413         {
414                 printf("usage: mkyaffsimage dir image_file\n");
415                 printf("           dir        the directory tree to be converted\n");
416                 printf("           image_file the ouput file to hold the image\n");
417                 exit(1);
418         }
419
420         if(stat(argv[1],&stats) < 0)
421         {
422                 printf("Could not stat %s\n",argv[1]);
423                 exit(1);
424         }
425         
426         if(!S_ISDIR(stats.st_mode))
427         {
428                 printf(" %s is not a directory\n",argv[1]);
429                 exit(1);
430         }
431         
432         outFile = open(argv[2],O_CREAT | O_TRUNC | O_WRONLY, S_IREAD | S_IWRITE);
433         
434         
435         if(outFile < 0)
436         {
437                 printf("Could not open output file %s\n",argv[2]);
438                 exit(1);
439         }
440         
441         printf("Processing directory %s into image file %s\n",argv[1],argv[2]);
442         error =  write_object_header(1, YAFFS_OBJECT_TYPE_DIRECTORY, &stats, 1,"", -1, NULL);
443         if(error)
444         error = process_directory(YAFFS_OBJECTID_ROOT,argv[1]);
445         
446         close(outFile);
447         
448         if(error < 0)
449         {
450                 perror("operation incomplete");
451                 exit(1);
452         }
453         else
454         {
455                 printf("Operation complete.\n"
456                        "%d objects in %d directories\n"
457                        "%d NAND pages\n",nObjects, nDirectories, nPages);
458         }
459         
460         close(outFile);
461         
462         exit(0);
463 }       
464