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