comparison packages/fs/yaffs/current/src/utils/mkyaffsimage.c @ 298:07c18ca96388

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