Add -d <directory> support to populate the image.

Upstream-Status: Inappropriate [licensing]
We're tracking an old release of dosfstools due to licensing issues.

Signed-off-by: Scott Garman <scott.a.garman@intel.com>

Index: dosfstools-2.11/mkdosfs/mkdosfs.c
===================================================================
--- dosfstools-2.11.orig/mkdosfs/mkdosfs.c	2011-12-06 12:27:55.000000000 +0000
+++ dosfstools-2.11/mkdosfs/mkdosfs.c	2011-12-06 12:37:13.445950703 +0000
@@ -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>
 
 #include <linux/version.h>
 #if LINUX_VERSION_CODE >= KERNEL_VERSION(2, 6, 0)
@@ -110,6 +116,8 @@
  * sufficient (or even better :) for 64 bit offsets in the meantime */
 #define llseek lseek
 
+#define ROUND_UP(value, divisor) (value + (divisor - (value % divisor))) / divisor
+
 /* Constant definitions */
 
 #define TRUE 1			/* Boolean constants */
@@ -149,7 +157,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" */
@@ -245,6 +252,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 */
@@ -293,6 +313,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 */
@@ -326,6 +355,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 */
 static int hidden_sectors = 0;		/* Number of hidden sectors */
 
@@ -399,7 +431,6 @@
   }
 }
 
-
 /* Mark a specified sector as having a particular value in it's FAT entry */
 
 static void
@@ -1266,6 +1297,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) )
     {
@@ -1314,11 +1348,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)				\
@@ -1340,7 +1374,7 @@
   do {							\
     int __size = (size);				\
     if (write (dev, buf, __size) != __size)		\
-	error ("failed whilst writing " errstr);	\
+       error ("failed whilst writing " errstr);	\
   } while(0)
 
 
@@ -1412,6 +1446,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 */
 
@@ -1423,7 +1903,7 @@
        [-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\
        [-h hidden-sectors] [-F fat-size] [-r root-dir-entries] [-R reserved-sectors]\n\
-       /dev/name [blocks]\n");
+       [-d directory] /dev/name [blocks]\n");
 }
 
 /*
@@ -1463,6 +1943,8 @@
   int c;
   char *tmp;
   char *listfile = NULL;
+  char *dirname = NULL;
+
   FILE *msgfile;
   struct stat statbuf;
   int i = 0, pos, ch;
@@ -1483,7 +1965,7 @@
   printf ("%s " VERSION " (" VERSION_DATE ")\n",
 	   program_name);
 
-  while ((c = getopt (argc, argv, "AbcCf:F:Ii:l:m:n:r:R:s:S:v:B:")) != EOF)
+  while ((c = getopt (argc, argv, "AbcCd:f:F:Ii:l:m:n:r:R:s:S:v:B:")) != EOF)
     /* Scan the command line for options */
     switch (c)
       {
@@ -1508,6 +1990,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)
@@ -1811,8 +2297,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! */
 }
 
