summaryrefslogtreecommitdiff
path: root/meta/packages/dosfstools/files/mkdosfs-dir.patch
blob: 8f753b052c900976dd9db662b58e3942c411da8c (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
diff -urN dosfstools-2.10.orig/mkdosfs/mkdosfs.c dosfstools-2.10/mkdosfs/mkdosfs.c
--- dosfstools-2.10.orig/mkdosfs/mkdosfs.c	2004-08-02 20:48:45.000000000 -0700
+++ dosfstools-2.10/mkdosfs/mkdosfs.c	2004-08-02 20:49:44.296953792 -0700
@@ -18,6 +18,10 @@
    as a rule), and not the block.  For example the boot block does not
    occupy a full cluster.
 
+   June 2004 - Jordan Crouse (info.linux@amd.com)
+   Added -d <directory> support to populate the image
+   Copyright (C) 2004, Advanced Micro Devices, All Rights Reserved
+   
    Fixes/additions May 1998 by Roman Hodek
    <Roman.Hodek@informatik.uni-erlangen.de>:
    - Atari format support
@@ -71,6 +75,8 @@
 #include <unistd.h>
 #include <time.h>
 #include <errno.h>
+#include <libgen.h>
+#include <dirent.h>
 
 #if __BYTE_ORDER == __BIG_ENDIAN
 
@@ -124,6 +130,8 @@
 }
 #endif
 
+#define ROUND_UP(value, divisor) (value + (divisor - (value % divisor))) / divisor
+
 /* Constant definitions */
 
 #define TRUE 1			/* Boolean constants */
@@ -163,7 +171,6 @@
 #define ATTR_VOLUME  8		/* volume label */
 #define ATTR_DIR     16		/* directory */
 #define ATTR_ARCH    32		/* archived */
-
 #define ATTR_NONE    0		/* no attribute bits */
 #define ATTR_UNUSED  (ATTR_VOLUME | ATTR_ARCH | ATTR_SYS | ATTR_HIDDEN)
 	/* attribute bits that are copied "as is" */
@@ -258,6 +265,19 @@
   __u32		reserved2[4];
 };
 
+/* This stores up to 13 chars of the name */
+
+struct msdos_dir_slot {
+        __u8    id;             /* sequence number for slot */
+        __u8    name0_4[10];    /* first 5 characters in name */
+        __u8    attr;           /* attribute byte */
+        __u8    reserved;       /* always 0 */
+        __u8    alias_checksum; /* checksum for 8.3 alias */
+        __u8    name5_10[12];   /* 6 more characters in name */
+        __u16   start;          /* starting cluster number, 0 in long slots */
+        __u8    name11_12[4];   /* last 2 characters in name */
+};
+
 struct msdos_dir_entry
   {
     char	name[8], ext[3];	/* name and extension */
@@ -306,6 +326,15 @@
 
 #define MESSAGE_OFFSET 29	/* Offset of message in above code */
 
+/* Special structure to keep track of directories as we add them for the -d option */
+
+struct dir_entry {
+  int root;                       /* Specifies if this is the root dir or not */
+  int count;                      /* Number of items in the table */
+  int entries;                    /* Number of entries in the table */
+  struct msdos_dir_entry *table;  /* Pointer to the entry table */
+};
+
 /* Global variables - the root of all evil :-) - see these and weep! */
 
 static char *template_boot_code;	/* Variable to store a full template boot sector in */
@@ -339,6 +368,9 @@
 static int size_root_dir;	/* Size of the root directory in bytes */
 static int sectors_per_cluster = 0;	/* Number of sectors per disk cluster */
 static int root_dir_entries = 0;	/* Number of root directory entries */
+static int root_dir_num_entries = 0;
+static int last_cluster_written = 0;
+
 static char *blank_sector;		/* Blank sector - all zeros */
 
 
@@ -411,7 +443,6 @@
   }
 }
 
-
 /* Mark a specified sector as having a particular value in it's FAT entry */
 
 static void
@@ -1262,6 +1293,9 @@
       die ("unable to allocate space for root directory in memory");
     }
 
+
+  last_cluster_written = 2;
+  
   memset(root_dir, 0, size_root_dir);
   if ( memcmp(volume_name, "           ", 11) )
     {
@@ -1310,11 +1344,11 @@
   }
   
   if (!(blank_sector = malloc( sector_size )))
-      die( "Out of memory" );
+    die( "Out of memory" );
+  
   memset(blank_sector, 0, sector_size);
 }
-
-
+  
 /* Write the new filesystem's data tables to wherever they're going to end up! */
 
 #define error(str)				\
@@ -1336,7 +1370,7 @@
   do {							\
     int __size = (size);				\
     if (write (dev, buf, __size) != __size)		\
-	error ("failed whilst writing " errstr);	\
+       error ("failed whilst writing " errstr);	\
   } while(0)
 
 
@@ -1407,6 +1441,452 @@
   free (fat);  /* Free up the fat table space reserved during setup_tables */
 }
 
+/* Add a file to the specified directory entry, and also write it into the image */
+
+static void copy_filename(char *filename, char *base, char *ext) {
+  
+  char *ch = filename;
+  int i, len;
+
+  memset(base, 0x20, 8);
+  memset(ext, 0x20, 3);
+  
+  for(len = 0 ; *ch && *ch != '.'; ch++) {
+    base[len++] = toupper(*ch);
+    if (len == 8) break;
+  }
+  
+  for ( ; *ch && *ch != '.'; ch++);
+  if (*ch) ch++;
+  
+  for(len = 0 ; *ch; ch++) {
+    ext[len++] = toupper(*ch);
+    if (len == 3) break;
+  }
+}
+
+/* Check for an .attrib.<filename> file, and read the attributes therein */
+
+/* We are going to be pretty pedantic about this.  The file needs 3
+   bytes at the beginning, the attributes are listed in this order:
+   
+   (H)idden|(S)ystem|(A)rchived
+   
+   A capital HSA means to enable it, anything else will disable it
+   (I recommend a '-') The unix user attributes will still be used 
+   for write access.
+
+   For example, to enable system file access for ldlinux.sys, write 
+   the following to .attrib.ldlinux.sys: -S-
+*/
+
+unsigned char check_attrib_file(char *dir, char *filename) {
+
+  char attrib[4] = { '-', '-', '-' };
+  unsigned char *buffer = 0;
+  int ret = ATTR_NONE;
+  int fd = -1;
+
+  buffer = (char *) calloc(1, strlen(dir) + strlen(filename) + 10);
+  if (!buffer) return ATTR_NONE;
+
+  sprintf(buffer, "%s/.attrib.%s", dir, filename);
+
+  if (access(buffer, R_OK)) 
+    goto exit_attrib;
+
+  if ((fd = open(buffer, O_RDONLY, 0)) < 0)
+    goto exit_attrib;
+
+  if (read(fd, attrib, 3) < 0)
+    goto exit_attrib;
+  
+  if (attrib[0] == 'H') ret |= ATTR_HIDDEN;
+  if (attrib[1] == 'S') ret |= ATTR_SYS;
+  if (attrib[2] == 'A') ret |= ATTR_ARCH;
+
+  printf("%s: Setting atrribute %x\n", filename, ret);
+
+ exit_attrib:
+  if (fd >= 0) close(fd);
+  if (buffer) free(buffer);
+
+  return ret;
+}
+
+static void copy_name(char *buffer, int size, char **pointer) {
+  int i;
+
+  for(i = 0; i < size; i += 2) {
+    if (*pointer) {
+      buffer[i] = **pointer;
+      buffer[i + 1] = 0x00;
+      *pointer = **pointer ? *pointer + 1 : 0;
+    }
+    else {
+      buffer[i] = 0xFF;
+      buffer[i + 1] = 0xFF;
+    }
+  }  
+}
+
+static int add_file(char *filename, struct dir_entry *dir, unsigned char attr) 
+{
+  struct stat stat;
+  struct msdos_dir_entry *entry;
+  int infile = 0;
+  int sectors, clusters;
+  struct tm *ctime;
+  int c, s;
+  int ptr;
+  char *buffer, *base;
+  int start;
+  int usedsec, totalsec;
+
+  char name83[8], ext83[3];
+
+  struct msdos_dir_slot *slot;
+  int i;
+  char *p;
+
+  /* The root directory is static, everything else grows as needed */
+
+  if (dir->root) {
+    if (dir->count == dir->entries) {
+      printf("Error - too many directory entries\n");
+    }
+  }
+  else {
+    if (dir->count == dir->entries) {
+      if (!dir->table) 
+	dir->table = 
+	  (struct msdos_dir_entry *) malloc(sizeof(struct msdos_dir_entry));
+      else {
+	dir->table = 
+	  (struct msdos_dir_entry *) realloc(dir->table, (dir->entries + 1) * 
+					     sizeof(struct msdos_dir_entry));
+
+	memset(&dir->table[dir->entries], 0, sizeof(struct msdos_dir_entry));
+      }
+
+      dir->entries++;
+    }
+  }
+
+  infile = open(filename, O_RDONLY, 0);
+  if (!infile) return;
+  
+  if (fstat(infile, &stat))
+    goto exit_add;
+ 
+  if (S_ISCHR(stat.st_mode) ||S_ISBLK(stat.st_mode) ||
+      S_ISFIFO(stat.st_mode) || S_ISLNK(stat.st_mode)) {
+    printf("Error - cannot create a special file in a FATFS\n");
+    goto exit_add;
+  }
+
+  /* FIXME: This isn't very pretty */
+
+  usedsec = start_data_sector + (size_root_dir / sector_size) +
+    (last_cluster_written * bs.cluster_size);
+
+  totalsec = blocks * BLOCK_SIZE / sector_size;
+  
+  /* Figure out how many sectors / clustors the file requires */
+
+  sectors = ROUND_UP(stat.st_size, sector_size);
+  clusters = ROUND_UP(sectors, (int) bs.cluster_size);
+
+  if (usedsec + sectors > totalsec) {
+    printf("Error - %s is too big (%d vs %d)\n", filename, sectors, totalsec - usedsec);
+    close(infile);
+    return -1;
+  }
+
+  printf("ADD %s\n", filename);
+
+  /* Grab the basename of the file */
+  base = basename(filename);
+  
+  /* Extract out the 8.3 name */
+  copy_filename(base, name83, ext83);
+
+  /* Make an extended name slot */
+
+  slot = (struct msdos_dir_slot *) &dir->table[dir->count++];
+  slot->id = 'A';  
+  slot->attr = 0x0F;
+  slot->reserved = 0;
+  slot->start = 0;
+  
+  slot->alias_checksum = 0;
+  
+  for(i = 0; i < 8; i++) 
+    slot->alias_checksum = (((slot->alias_checksum&1)<<7)|((slot->alias_checksum&0xfe)>>1)) + name83[i];
+  
+  for(i = 0; i < 3; i++) 
+    slot->alias_checksum = (((slot->alias_checksum&1)<<7)|((slot->alias_checksum&0xfe)>>1)) + ext83[i];
+
+  p = base;
+
+  copy_name(slot->name0_4, 10, &p);
+  copy_name(slot->name5_10, 12, &p);
+  copy_name(slot->name11_12, 4, &p);
+
+
+  /* Get the entry from the root filesytem */
+  entry = &dir->table[dir->count++];
+
+  strncpy(entry->name, name83, 8);
+  strncpy(entry->ext, ext83, 3);
+
+
+  /* If the user has it read only, then add read only to the incoming
+     attribute settings */
+
+  if (!(stat.st_mode & S_IWUSR)) attr |= ATTR_RO;
+  entry->attr = attr;
+
+  /* Set the access time on the file */
+  ctime = localtime(&create_time);
+
+  entry->time = CT_LE_W((unsigned short)((ctime->tm_sec >> 1) +
+					  (ctime->tm_min << 5) + (ctime->tm_hour << 11)));
+
+  entry->date = CT_LE_W((unsigned short)(ctime->tm_mday +
+				      ((ctime->tm_mon+1) << 5) +
+				      ((ctime->tm_year-80) << 9)));
+
+  entry->ctime_ms = 0;
+  entry->ctime = entry->time;
+  entry->cdate = entry->date;
+  entry->adate = entry->date;
+  entry->size = stat.st_size;
+
+  start = last_cluster_written;
+
+  entry->start = CT_LE_W(start);  /* start sector */
+  entry->starthi = CT_LE_W((start & 0xFFFF0000) >> 16); /* High start sector (for FAT32) */
+
+  /* We mark all of the clusters we use in the FAT */
+  
+  for(c = 0; c < clusters; c++ ) {
+    int free;
+    int next = c == (clusters - 1) ? FAT_EOF : start + c + 1;
+    mark_FAT_cluster(start + c, next);  
+    last_cluster_written++;
+  }
+  
+  /* This confused me too - cluster 2 starts after the
+     root directory data - search me as to why */
+
+  ptr = (start_data_sector * sector_size) + size_root_dir;
+  ptr += (start - 2) * bs.cluster_size * sector_size;
+    
+  buffer = (char *) malloc(sector_size);
+
+  if (!buffer) {
+    printf("Error - couldn't allocate memory\n");
+    goto exit_add;
+  }
+
+  /* Write the file into the file block */
+
+  seekto(ptr, "datafile");
+
+  while(1) {
+    int size = read(infile, buffer, sector_size);    
+    if (size <= 0) break;
+    
+    writebuf(buffer, size, "data");    
+  }
+
+ exit_add:
+  if (infile) close(infile);
+}
+
+/* Add a new directory to the specified directory entry, and in turn populate 
+   it with its own files */
+
+/* FIXME:  This should check to make sure there is enough size to add itself */
+
+static void add_directory(char *filename, struct dir_entry *dir) {
+
+  struct dir_entry *newdir = 0;
+  struct msdos_dir_entry *entry;
+  struct tm *ctime;
+  DIR *rddir = opendir(filename);
+  struct dirent *dentry = 0;
+  int remain;
+  char *data;
+
+  /* If the directory doesn't exist */
+  if (!rddir) return;
+  
+  if (dir->root) {
+    if (dir->count == dir->entries) {
+      printf("Error - too many directory entries\n");
+      goto exit_add_dir;
+    }
+  }
+  else {
+    if (dir->count == dir->entries) {
+      if (!dir->table) 
+	dir->table = (struct msdos_dir_entry *) malloc(sizeof(struct msdos_dir_entry));
+      else {
+	dir->table = (struct msdos_dir_entry *) realloc(dir->table, (dir->entries + 1) * 
+							sizeof(struct msdos_dir_entry));
+
+	/* Zero it out to avoid issues */
+	memset(&dir->table[dir->entries], 0, sizeof(struct msdos_dir_entry));
+      }
+	dir->entries++;
+    }
+  }
+
+  /* Now, create a new directory entry for the new directory */
+  newdir = (struct dir_entry *) calloc(1, sizeof(struct dir_entry));
+  if (!newdir) goto exit_add_dir;
+
+  entry = &dir->table[dir->count++];
+
+  strncpy(entry->name, basename(filename), sizeof(entry->name));
+  
+  entry->attr = ATTR_DIR;
+  ctime = localtime(&create_time);
+
+  entry->time = CT_LE_W((unsigned short)((ctime->tm_sec >> 1) +
+					  (ctime->tm_min << 5) + (ctime->tm_hour << 11)));
+
+  entry->date = CT_LE_W((unsigned short)(ctime->tm_mday +
+				      ((ctime->tm_mon+1) << 5) +
+				      ((ctime->tm_year-80) << 9)));
+
+  entry->ctime_ms = 0;
+  entry->ctime = entry->time;
+  entry->cdate = entry->date;
+  entry->adate = entry->date;
+
+  /* Now, read the directory */
+
+  while((dentry = readdir(rddir))) {
+    struct stat st;
+    char *buffer;
+    
+    if (!strcmp(dentry->d_name, ".") || !strcmp(dentry->d_name, ".."))
+      continue;
+
+    /* DOS wouldn't like a typical unix . (dot) file, so we skip those too */
+    if (dentry->d_name[0] == '.') continue;
+
+    buffer = malloc(strlen(filename) + strlen(dentry->d_name) + 3);
+    if (!buffer) continue;
+    
+    sprintf(buffer, "%s/%s", filename, dentry->d_name);
+    if (!stat(buffer, &st)) {
+      if (S_ISDIR(st.st_mode)) 
+	add_directory(buffer, newdir);
+      else if (S_ISREG(st.st_mode)) {
+	unsigned char attrib = check_attrib_file(filename, dentry->d_name);
+	add_file(buffer, newdir, attrib);
+      }
+    }
+ 
+    free(buffer);
+  }
+  
+  /* Now that the entire directory has been written, go ahead and write the directory
+     entry as well */
+
+  entry->start = CT_LE_W(last_cluster_written);
+  entry->starthi = CT_LE_W((last_cluster_written & 0xFFFF0000) >> 16); 
+  entry->size = newdir->count * sizeof(struct msdos_dir_entry);
+  
+  remain = entry->size;
+  data = (char *) newdir->table;
+
+  while(remain) {
+    int size = 
+      remain > bs.cluster_size * sector_size ? bs.cluster_size * sector_size : remain;
+
+    int pos = (start_data_sector * sector_size) + size_root_dir;
+    pos += (last_cluster_written - 2) * bs.cluster_size * sector_size;
+    
+    seekto(pos, "add_dir");
+    writebuf(data, size, "add_dir");
+    
+    remain -= size;
+    data += size;
+
+    mark_FAT_cluster(last_cluster_written, remain ? last_cluster_written + 1 : FAT_EOF);      
+    last_cluster_written++;
+  }
+  
+ exit_add_dir:
+  if (rddir) closedir(rddir);
+  if (newdir->table) free(newdir->table);
+  if (newdir) free(newdir);
+}  
+  
+/* Given a directory, add all the files and directories to the root directory of the
+   image.  
+*/
+
+static void add_root_directory(char *dirname) 
+{
+  DIR *dir = opendir(dirname);
+  struct dirent *entry = 0;
+  struct dir_entry *newdir = 0;
+
+  if (!dir) {
+    printf("Error - directory %s does not exist\n", dirname);
+    return;
+  }
+ 
+  /* Create the root directory structure - this is a bit different then
+     above, because the table already exists, we just refer to it. */
+
+  newdir = (struct dir_entry *) calloc(1,sizeof(struct dir_entry));
+
+  if (!newdir) {
+    closedir(dir);
+    return;
+  }
+
+  newdir->entries = root_dir_entries;
+  newdir->root = 1;
+  newdir->count = 0;
+  newdir->table = root_dir;
+
+  while((entry = readdir(dir))) {
+    struct stat st;
+    char *buffer;
+    
+    if (!strcmp(entry->d_name, ".") || !strcmp(entry->d_name, ".."))
+      continue;
+    
+    /* DOS wouldn't like a typical unix . (dot) file, so we skip those too */
+    if (entry->d_name[0] == '.') continue;
+ 
+    buffer = malloc(strlen(dirname) + strlen(entry->d_name) + 3);
+    if (!buffer) continue;
+
+    sprintf(buffer, "%s/%s", dirname, entry->d_name);
+    if (!stat(buffer, &st)) {
+      if (S_ISDIR(st.st_mode)) 
+	add_directory(buffer, newdir);
+      else if (S_ISREG(st.st_mode)) {
+	unsigned char attrib = check_attrib_file(dirname, entry->d_name);
+	add_file(buffer, newdir, attrib);
+      }
+    }
+
+    free(buffer);
+  }
+
+  closedir(dir);
+  if (newdir) free(newdir);
+}
 
 /* Report the command usage and return a failure error code */
 
@@ -1418,9 +1898,9 @@
        [-m boot-msg-file] [-n volume-name] [-i volume-id] [-B bootcode]\n\
        [-s sectors-per-cluster] [-S logical-sector-size] [-f number-of-FATs]\n\
        [-F fat-size] [-r root-dir-entries] [-R reserved-sectors]\n\
-       /dev/name [blocks]\n");
+       [-d directory] /dev/name [blocks]\n");
 }
-
+	
 /*
  * ++roman: On m68k, check if this is an Atari; if yes, turn on Atari variant
  * of MS-DOS filesystem by default.
@@ -1458,6 +1938,8 @@
   int c;
   char *tmp;
   char *listfile = NULL;
+  char *dirname = NULL;
+
   FILE *msgfile;
   struct stat statbuf;
   int i = 0, pos, ch;
@@ -1477,7 +1959,7 @@
   printf ("%s " VERSION " (" VERSION_DATE ")\n",
 	   program_name);
 
-  while ((c = getopt (argc, argv, "AcCf:F:Ii:l:m:n:r:R:s:S:v:B:b")) != EOF)
+  while ((c = getopt (argc, argv, "AcCd:f:F:Ii:l:m:n:r:R:s:S:v:B:b")) != EOF)
     /* Scan the command line for options */
     switch (c)
       {
@@ -1502,6 +1984,10 @@
 	create = TRUE;
 	break;
 
+      case 'd':
+	dirname = optarg;
+	break;
+
       case 'f':		/* f : Choose number of FATs */
 	nr_fats = (int) strtol (optarg, &tmp, 0);
 	if (*tmp || nr_fats < 1 || nr_fats > 4)
@@ -1796,8 +2282,10 @@
   else if (listfile)
     get_list_blocks (listfile);
 
-  write_tables ();		/* Write the file system tables away! */
 
+  if (dirname) add_root_directory(dirname);
+
+  write_tables ();		/* Write the file system tables away! */  
   exit (0);			/* Terminate with no errors! */
 }