Fix copyright
[yaffs2.git] / utils / mkyaffs2image.c
1 /*
2  * YAFFS: Yet Another Flash File System. A NAND-flash specific file system.
3  *
4  * Copyright (C) 2002-2018 Aleph One Ltd.
5  *
6  * Created by Charles Manning <charles@aleph1.co.uk>
7  * Nick Bane modifications flagged NCB
8  * Endian handling patches by James Ng.
9  * mkyaffs2image hacks by NCB
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  * makeyaffs2image.c 
18  *
19  * Makes a YAFFS2 file system image that can be used to load up a file system.
20  * Uses default Linux MTD layout - search for "NAND LAYOUT" to change.
21  */
22  
23 #include <stdlib.h>
24 #include <stdio.h>
25 #include <fcntl.h>
26 #include <sys/types.h>
27 #include <sys/stat.h>
28 #include <dirent.h>
29 #include <string.h>
30 #include <unistd.h>
31 #include <errno.h>
32 #include <assert.h>
33 #include "yaffs_ecc.h"
34 #include "yaffs_guts.h"
35
36 #include "yaffs_packedtags2.h"
37
38 unsigned yaffs_trace_mask=0;
39
40 #define MAX_OBJECTS 10000
41
42 // Adjust these to match your NAND LAYOUT:
43 #define chunkSize 2048
44 #define spareSize 64
45 #define pagesPerBlock 64
46
47
48
49 typedef struct
50 {
51         dev_t dev;
52         ino_t ino;
53         int   obj;
54 } objItem;
55
56
57 static objItem obj_list[MAX_OBJECTS];
58 static int obj_id = YAFFS_NOBJECT_BUCKETS + 1;
59
60 static int n_obj, nDirectories, nPages;
61
62 static int outFile;
63
64 static int error;
65 static int savedErrno;
66
67 static int convert_endian = 0;
68
69 static void fatal(const char *fn)
70 {
71         perror(fn);
72         error |= 1;
73         exit(error);
74 }
75
76 static int warn(const char *fn)
77 {
78         savedErrno = errno;
79         perror(fn);
80         error |= 2;
81         return error;
82 }
83
84 static int obj_compare(const void *a, const void * b)
85 {
86   objItem *oa, *ob;
87   
88   oa = (objItem *)a;
89   ob = (objItem *)b;
90   
91   if(oa->dev < ob->dev) return -1;
92   if(oa->dev > ob->dev) return 1;
93   if(oa->ino < ob->ino) return -1;
94   if(oa->ino > ob->ino) return 1;
95   
96   return 0;
97 }
98
99
100 static void add_obj_to_list(dev_t dev, ino_t ino, int obj)
101 {
102         if(n_obj < MAX_OBJECTS)
103         {
104                 obj_list[n_obj].dev = dev;
105                 obj_list[n_obj].ino = ino;
106                 obj_list[n_obj].obj = obj;
107                 n_obj++;
108                 qsort(obj_list,n_obj,sizeof(objItem),obj_compare);
109                 
110         }
111         else
112         {
113                 // oops! not enough space in the object array
114                 fprintf(stderr,"Not enough space in object array\n");
115                 exit(1);
116         }
117 }
118
119
120 static int find_obj_in_list(dev_t dev, ino_t ino)
121 {
122         objItem *i = NULL;
123         objItem test;
124
125         test.dev = dev;
126         test.ino = ino;
127         
128         if(n_obj > 0)
129         {
130                 i = bsearch(&test,obj_list,n_obj,sizeof(objItem),obj_compare);
131         }
132
133         if(i)
134         {
135                 return i->obj;
136         }
137         return -1;
138 }
139
140 /* This little function converts a little endian tag to a big endian tag.
141  * NOTE: The tag is not usable after this other than calculating the CRC
142  * with.
143  */
144 static void little_to_big_endian(struct yaffs_ext_tags *tagsPtr)
145 {
146 #if 0 // FIXME NCB
147     union yaffs_tags_union * tags = (union yaffs_tags_union* )tagsPtr; // Work in bytes.
148     union yaffs_tags_union   temp;
149
150     memset(&temp, 0, sizeof(temp));
151     // Ick, I hate magic numbers.
152     temp.as_bytes[0] = ((tags->as_bytes[2] & 0x0F) << 4) | ((tags->as_bytes[1] & 0xF0) >> 4);
153     temp.as_bytes[1] = ((tags->as_bytes[1] & 0x0F) << 4) | ((tags->as_bytes[0] & 0xF0) >> 4);
154     temp.as_bytes[2] = ((tags->as_bytes[0] & 0x0F) << 4) | ((tags->as_bytes[2] & 0x30) >> 2) | ((tags->as_bytes[3] & 0xC0) >> 6);
155     temp.as_bytes[3] = ((tags->as_bytes[3] & 0x3F) << 2) | ((tags->as_bytes[2] & 0xC0) >> 6);
156     temp.as_bytes[4] = ((tags->as_bytes[6] & 0x03) << 6) | ((tags->as_bytes[5] & 0xFC) >> 2);
157     temp.as_bytes[5] = ((tags->as_bytes[5] & 0x03) << 6) | ((tags->as_bytes[4] & 0xFC) >> 2);
158     temp.as_bytes[6] = ((tags->as_bytes[4] & 0x03) << 6) | (tags->as_bytes[7] & 0x3F);
159     temp.as_bytes[7] = (tags->as_bytes[6] & 0xFC) | ((tags->as_bytes[7] & 0xC0) >> 6);
160
161     // Now copy it back.
162     tags->as_bytes[0] = temp.as_bytes[0];
163     tags->as_bytes[1] = temp.as_bytes[1];
164     tags->as_bytes[2] = temp.as_bytes[2];
165     tags->as_bytes[3] = temp.as_bytes[3];
166     tags->as_bytes[4] = temp.as_bytes[4];
167     tags->as_bytes[5] = temp.as_bytes[5];
168     tags->as_bytes[6] = temp.as_bytes[6];
169     tags->as_bytes[7] = temp.as_bytes[7];
170 #endif
171 }
172
173 static void shuffle_oob(char *spareData, struct yaffs_packed_tags2 *pt)
174 {
175         assert(sizeof(*pt) <= spareSize);
176         // NAND LAYOUT: For non-trivial OOB orderings, here would be a good place to shuffle.
177         memcpy(spareData, pt, sizeof(*pt));
178 }
179
180 static int write_chunk(u8 *data, u32 id, u32 chunk_id, u32 n_bytes)
181 {
182         struct yaffs_ext_tags t;
183         struct yaffs_packed_tags2 pt;
184         char spareData[spareSize];
185
186         if (write(outFile,data,chunkSize) != chunkSize)
187                 fatal("write");
188
189         memset(&t, 0, sizeof(t));
190         
191         t.chunk_id = chunk_id;
192 //      t.serial_number = 0;
193         t.serial_number = 1;    // **CHECK**
194         t.n_bytes = n_bytes;
195         t.obj_id = id;
196         
197         t.seq_number = YAFFS_LOWEST_SEQUENCE_NUMBER;
198
199 // added NCB **CHECK**
200         t.chunk_used = 1;
201
202         if (convert_endian)
203         {
204             little_to_big_endian(&t);
205         }
206
207         nPages++;
208
209         memset(&pt, 0, sizeof(pt));
210         yaffs_pack_tags2(&pt,&t,1);
211
212         memset(spareData, 0xff, sizeof(spareData));
213         shuffle_oob(spareData, &pt);
214
215         if (write(outFile,spareData,sizeof(spareData)) != sizeof(spareData))
216                 fatal("write");
217         return 0;
218 }
219
220 #define SWAP32(x)   ((((x) & 0x000000FF) << 24) | \
221                      (((x) & 0x0000FF00) << 8 ) | \
222                      (((x) & 0x00FF0000) >> 8 ) | \
223                      (((x) & 0xFF000000) >> 24))
224
225 #define SWAP16(x)   ((((x) & 0x00FF) << 8) | \
226                      (((x) & 0xFF00) >> 8))
227         
228 // This one is easier, since the types are more standard. No funky shifts here.
229 static void object_header_little_to_big_endian(struct yaffs_obj_hdr* oh)
230 {
231     oh->type = SWAP32(oh->type); // GCC makes enums 32 bits.
232     oh->parent_obj_id = SWAP32(oh->parent_obj_id); // int
233     oh->sum_no_longer_used = SWAP16(oh->sum_no_longer_used); // u16 - Not used, but done for completeness.
234     // name = skip. Char array. Not swapped.
235     oh->yst_mode = SWAP32(oh->yst_mode);
236
237     // Regular POSIX.
238     oh->yst_uid = SWAP32(oh->yst_uid);
239     oh->yst_gid = SWAP32(oh->yst_gid);
240     oh->yst_atime = SWAP32(oh->yst_atime);
241     oh->yst_mtime = SWAP32(oh->yst_mtime);
242     oh->yst_ctime = SWAP32(oh->yst_ctime);
243
244     oh->file_size_low = SWAP32(oh->file_size_low); // Aiee. An int... signed, at that!
245     oh->file_size_high = SWAP32(oh->file_size_high); // Aiee. An int... signed, at that!
246     oh->equiv_id = SWAP32(oh->equiv_id);
247     // alias  - char array.
248     oh->yst_rdev = SWAP32(oh->yst_rdev);
249
250     oh->win_ctime[0] = SWAP32(oh->win_ctime[0]);
251     oh->win_ctime[1] = SWAP32(oh->win_ctime[1]);
252     oh->win_atime[0] = SWAP32(oh->win_atime[0]);
253     oh->win_atime[1] = SWAP32(oh->win_atime[1]);
254     oh->win_mtime[0] = SWAP32(oh->win_mtime[0]);
255     oh->win_mtime[1] = SWAP32(oh->win_mtime[1]);
256
257     oh->reserved[0] = SWAP32(oh->reserved[0]);
258     oh->reserved[1] = SWAP32(oh->reserved[1]);
259
260     oh->inband_shadowed_obj_id = SWAP32(oh->inband_shadowed_obj_id);
261     oh->inband_is_shrink = SWAP32(oh->inband_is_shrink);
262     oh->shadows_obj = SWAP32(oh->shadows_obj);
263     oh->is_shrink = SWAP32(oh->is_shrink);
264 }
265
266
267 static int write_object_header(int id, enum yaffs_obj_type t, struct stat *s, int parent, const char *name, int equivalentObj, const char * alias)
268 {
269         u8 bytes[chunkSize];
270         
271         
272         struct yaffs_obj_hdr *oh = (struct yaffs_obj_hdr *)bytes;
273         
274         memset(bytes,0xff,sizeof(bytes));
275         
276         oh->type = t;
277
278         oh->parent_obj_id = parent;
279         
280         if (strlen(name)+1 > sizeof(oh->name))
281         {
282                 errno = ENAMETOOLONG;
283                 return warn("object name");
284         }
285         memset(oh->name,0,sizeof(oh->name));
286         strcpy(oh->name,name);
287         
288         
289         if(t != YAFFS_OBJECT_TYPE_HARDLINK)
290         {
291                 oh->yst_mode = s->st_mode;
292                 oh->yst_uid = s->st_uid;
293 // NCB 12/9/02          oh->yst_gid = s->yst_uid;
294                 oh->yst_gid = s->st_gid;
295                 oh->yst_atime = s->st_atime;
296                 oh->yst_mtime = s->st_mtime;
297                 oh->yst_ctime = s->st_ctime;
298                 oh->yst_rdev  = s->st_rdev;
299         }
300         
301         if(t == YAFFS_OBJECT_TYPE_FILE)
302         {
303                 oh->file_size_low = s->st_size;
304                 oh->file_size_high = (s->st_size >> 32);
305         }
306         
307         if(t == YAFFS_OBJECT_TYPE_HARDLINK)
308         {
309                 oh->equiv_id = equivalentObj;
310         }
311         
312         if(t == YAFFS_OBJECT_TYPE_SYMLINK)
313         {
314                 if (strlen(alias)+1 > sizeof(oh->alias))
315                 {
316                         errno = ENAMETOOLONG;
317                         return warn("object alias");
318                 }
319                 memset(oh->alias,0,sizeof(oh->alias));
320                 strcpy(oh->alias,alias);
321         }
322
323         if (convert_endian)
324         {
325                 object_header_little_to_big_endian(oh);
326         }
327         
328         return write_chunk(bytes,id,0,0xffff);
329         
330 }
331
332 static void pad_image(void)
333 {
334         u8 data[chunkSize + spareSize];
335         int padPages = (nPages % pagesPerBlock);
336
337         if (padPages)
338         {
339                 memset(data, 0xff, sizeof(data));
340                 for (padPages = pagesPerBlock-padPages; padPages; padPages--)
341                 {
342                         if (write(outFile, data, sizeof(data)) != sizeof(data))
343                                 fatal("write");
344                 }
345         }
346 }
347
348 static int process_directory(int parent, const char *path)
349 {
350
351         DIR *dir;
352         struct dirent *entry;
353
354         nDirectories++;
355         
356         dir = opendir(path);
357         if(!dir)
358         {
359                 warn("opendir");
360         }
361         else
362         {
363                 while((entry = readdir(dir)) != NULL)
364                 {
365                 
366                         /* Ignore . and .. */
367                         if(strcmp(entry->d_name,".") &&
368                            strcmp(entry->d_name,".."))
369                         {
370                                 char full_name[500];
371                                 struct stat stats;
372                                 int equivalentObj;
373                                 int newObj;
374                                 
375                                 if (snprintf(full_name,sizeof(full_name),"%s/%s",path,entry->d_name) >= (int)sizeof(full_name))
376                                 {
377                                         error = -1;
378                                         continue;
379                                 }
380                                 
381                                 if (lstat(full_name,&stats) < 0)
382                                 {
383                                         warn("lstat");
384                                         continue;
385                                 }
386                                 
387                                 if(S_ISLNK(stats.st_mode) ||
388                                     S_ISREG(stats.st_mode) ||
389                                     S_ISDIR(stats.st_mode) ||
390                                     S_ISFIFO(stats.st_mode) ||
391                                     S_ISBLK(stats.st_mode) ||
392                                     S_ISCHR(stats.st_mode) ||
393                                     S_ISSOCK(stats.st_mode))
394                                 {
395                                 
396                                         newObj = obj_id++;
397                                         n_obj++;
398                                         
399                                         printf("Object %d, %s is a ",newObj,full_name);
400                                         
401                                         /* We're going to create an object for it */
402                                         if((equivalentObj = find_obj_in_list(stats.st_dev, stats.st_ino)) > 0)
403                                         {
404                                                 /* we need to make a hard link */
405                                                 printf("hard link to object %d\n",equivalentObj);
406                                                 write_object_header(newObj, YAFFS_OBJECT_TYPE_HARDLINK, &stats, parent, entry->d_name, equivalentObj, NULL);
407                                         }
408                                         else 
409                                         {
410                                                 
411                                                 add_obj_to_list(stats.st_dev,stats.st_ino,newObj);
412                                                 
413                                                 if(S_ISLNK(stats.st_mode))
414                                                 {
415                                         
416                                                         char symname[500];
417                                                 
418                                                         memset(symname,0, sizeof(symname));
419                                         
420                                                         if (readlink(full_name,symname,sizeof(symname) -1) < 0)
421                                                         {
422                                                                 warn("readlink");
423                                                         }
424                                                         else
425                                                         {
426                                                                 printf("symlink to \"%s\"\n",symname);
427                                                                 write_object_header(newObj, YAFFS_OBJECT_TYPE_SYMLINK, &stats, parent, entry->d_name, -1, symname);
428                                                         }
429                                                 }
430                                                 else if(S_ISREG(stats.st_mode))
431                                                 {
432                                                         printf("file, ");
433                                                         if(write_object_header(newObj, YAFFS_OBJECT_TYPE_FILE, &stats, parent, entry->d_name, -1, NULL) == 0)
434                                                         {
435                                                                 int h;
436                                                                 u8 bytes[chunkSize];
437                                                                 int n_bytes;
438                                                                 int chunk = 0;
439                                                                 
440                                                                 h = open(full_name,O_RDONLY);
441                                                                 if(h >= 0)
442                                                                 {
443                                                                         memset(bytes,0xff,sizeof(bytes));
444                                                                         while((n_bytes = read(h,bytes,sizeof(bytes))) > 0)
445                                                                         {
446                                                                                 chunk++;
447                                                                                 write_chunk(bytes,newObj,chunk,n_bytes);
448                                                                                 memset(bytes,0xff,sizeof(bytes));
449                                                                         }
450                                                                         if(n_bytes < 0) 
451                                                                            warn("read");
452                                                                            
453                                                                         printf("%d data chunks written\n",chunk);
454                                                                         close(h);
455                                                                 }
456                                                                 else
457                                                                 {
458                                                                         warn("open");
459                                                                 }
460                                                                 
461                                                         }                                                       
462                                                                                                                 
463                                                 }
464                                                 else if(S_ISSOCK(stats.st_mode))
465                                                 {
466                                                         printf("socket\n");
467                                                         write_object_header(newObj, YAFFS_OBJECT_TYPE_SPECIAL, &stats, parent, entry->d_name, -1, NULL);
468                                                 }
469                                                 else if(S_ISFIFO(stats.st_mode))
470                                                 {
471                                                         printf("fifo\n");
472                                                         write_object_header(newObj, YAFFS_OBJECT_TYPE_SPECIAL, &stats, parent, entry->d_name, -1, NULL);
473                                                 }
474                                                 else if(S_ISCHR(stats.st_mode))
475                                                 {
476                                                         printf("character device\n");
477                                                         write_object_header(newObj, YAFFS_OBJECT_TYPE_SPECIAL, &stats, parent, entry->d_name, -1, NULL);
478                                                 }
479                                                 else if(S_ISBLK(stats.st_mode))
480                                                 {
481                                                         printf("block device\n");
482                                                         write_object_header(newObj, YAFFS_OBJECT_TYPE_SPECIAL, &stats, parent, entry->d_name, -1, NULL);
483                                                 }
484                                                 else if(S_ISDIR(stats.st_mode))
485                                                 {
486                                                         printf("directory\n");
487                                                         if (write_object_header(newObj, YAFFS_OBJECT_TYPE_DIRECTORY, &stats, parent, entry->d_name, -1, NULL) == 0)
488                                                                 process_directory(newObj,full_name);
489                                                 }
490                                         }
491                                 }
492                                 else
493                                 {
494                                         fprintf(stderr, "%s: unhandled type\n", full_name);
495                                         error |= 2;
496                                         savedErrno = EINVAL;
497                                 }
498                         }
499                 }
500                 closedir(dir);
501         }
502         
503         return 0;
504
505 }
506
507
508 int main(int argc, char *argv[])
509 {
510         struct stat stats;
511         
512         printf("mkyaffs2image: image building tool for YAFFS2 built "__DATE__"\n");
513         
514         if(argc < 3)
515         {
516                 printf("usage: mkyaffs2image dir image_file [convert]\n");
517                 printf("           dir        the directory tree to be converted\n");
518                 printf("           image_file the output file to hold the image\n");
519         printf("           'convert'  produce a big-endian image from a little-endian machine\n");
520                 exit(1);
521         }
522
523     if ((argc == 4) && (!strncmp(argv[3], "convert", strlen("convert"))))
524     {
525         convert_endian = 1;
526     }
527     
528         if(stat(argv[1],&stats) < 0)
529         {
530                 printf("Could not stat %s\n",argv[1]);
531                 exit(1);
532         }
533         
534         if(!S_ISDIR(stats.st_mode))
535         {
536                 printf(" %s is not a directory\n",argv[1]);
537                 exit(1);
538         }
539         
540         outFile = open(argv[2],O_CREAT | O_TRUNC | O_WRONLY, S_IREAD | S_IWRITE);
541         
542         
543         if(outFile < 0)
544         {
545                 printf("Could not open output file %s\n",argv[2]);
546                 exit(1);
547         }
548         
549         printf("Processing directory %s into image file %s\n",argv[1],argv[2]);
550         process_directory(YAFFS_OBJECTID_ROOT,argv[1]);
551         
552         pad_image();
553
554         close(outFile);
555         
556         if(error)
557         {
558                 errno = savedErrno;
559                 perror("operation incomplete");
560         }
561         else
562         {
563                 printf("Operation complete.\n"
564                        "%d objects in %d directories\n"
565                        "%d NAND pages\n",n_obj, nDirectories, nPages);
566         }
567         
568         exit(error);
569 }       
570