Use libfat to set the MS-DOS attributes when using the unix installer.
[profile/ivi/syslinux.git] / unix / syslinux.c
index 60d114f..276990d 100644 (file)
@@ -1,7 +1,7 @@
 #ident "$Id$"
 /* ----------------------------------------------------------------------- *
  *   
- *   Copyright 1998-2004 H. Peter Anvin - All Rights Reserved
+ *   Copyright 1998-2005 H. Peter Anvin - All Rights Reserved
  *
  *   This program is free software; you can redistribute it and/or modify
  *   it under the terms of the GNU General Public License as published by
  * mtools, but requires root privilege.
  */
 
+/*
+ * If DO_DIRECT_MOUNT is 0, call mount(8)
+ * If DO_DIRECT_MOUNT is 1, call mount(2)
+ */
 #ifdef __KLIBC__
-# define DO_DIRECT_MOUNT 1     /* Call mount(2) directly */
+# define DO_DIRECT_MOUNT 1
 #else
-# define DO_DIRECT_MOUNT 0     /* Call /bin/mount instead */
+# define DO_DIRECT_MOUNT 0     /* glibc has broken losetup ioctls */
 #endif
 
 #define _GNU_SOURCE
@@ -66,6 +70,7 @@ const char *program;          /* Name of program */
 const char *device;            /* Device to install to */
 pid_t mypid;
 char *mntpath = NULL;          /* Path on which to mount */
+off_t filesystem_offset = 0;   /* Filesystem offset */
 #if DO_DIRECT_MOUNT
 int loop_fd = -1;              /* Loop device */
 #endif
@@ -99,11 +104,12 @@ void __attribute__((noreturn)) die(const char *msg)
  */
 ssize_t xpread(int fd, void *buf, size_t count, off_t offset)
 {
+  char *p = buf;
   ssize_t rv;
   ssize_t done = 0;
 
   while ( count ) {
-    rv = pread(fd, buf, count, offset);
+    rv = pread(fd, p, count, offset);
     if ( rv == 0 ) {
       die("short read");
     } else if ( rv == -1 ) {
@@ -116,6 +122,7 @@ ssize_t xpread(int fd, void *buf, size_t count, off_t offset)
     } else {
       offset += rv;
       done += rv;
+      p += rv;
       count -= rv;
     }
   }
@@ -123,13 +130,14 @@ ssize_t xpread(int fd, void *buf, size_t count, off_t offset)
   return done;
 }
 
-ssize_t xpwrite(int fd, void *buf, size_t count, off_t offset)
+ssize_t xpwrite(int fd, const void *buf, size_t count, off_t offset)
 {
+  const char *p = buf;
   ssize_t rv;
   ssize_t done = 0;
 
   while ( count ) {
-    rv = pwrite(fd, buf, count, offset);
+    rv = pwrite(fd, p, count, offset);
     if ( rv == 0 ) {
       die("short write");
     } else if ( rv == -1 ) {
@@ -142,6 +150,7 @@ ssize_t xpwrite(int fd, void *buf, size_t count, off_t offset)
     } else {
       offset += rv;
       done += rv;
+      p += rv;
       count -= rv;
     }
   }
@@ -154,7 +163,7 @@ ssize_t xpwrite(int fd, void *buf, size_t count, off_t offset)
  */
 int libfat_xpread(intptr_t pp, void *buf, size_t secsize, libfat_sector_t sector)
 {
-  off_t offset = (off_t)sector * secsize;
+  off_t offset = (off_t)sector * secsize + filesystem_offset;
   return xpread(pp, buf, secsize, offset);
 }
 
@@ -172,8 +181,8 @@ int main(int argc, char *argv[])
   char mntname[64], devfdname[64];
   char *ldlinux_name, **argp, *opt;
   int force = 0;               /* -f (force) option */
-  off_t offset = 0;            /* -o (offset) option */
   struct libfat_filesystem *fs;
+  struct libfat_direntry dentry;
   libfat_sector_t s, *secp, sectors[65]; /* 65 is maximum possible */
   int32_t ldlinux_cluster;
   int nsectors;
@@ -200,7 +209,7 @@ int main(int argc, char *argv[])
        } else if ( *opt == 'f' ) {
          force = 1;            /* Force install */
        } else if ( *opt == 'o' && argp[1] ) {
-         offset = (off_t)strtoull(*++argp, NULL, 0); /* Byte offset */
+         filesystem_offset = (off_t)strtoull(*++argp, NULL, 0); /* Byte offset */
        } else {
          usage();
        }
@@ -230,11 +239,11 @@ int main(int argc, char *argv[])
     die("not a block device or regular file (use -f to override)");
   }
 
-  if ( !force && offset != 0 && !S_ISREG(st.st_mode) ) {
+  if ( !force && filesystem_offset && !S_ISREG(st.st_mode) ) {
     die("not a regular file and an offset specified (use -f to override)");
   }
 
-  xpread(dev_fd, sectbuf, 512, offset);
+  xpread(dev_fd, sectbuf, 512, filesystem_offset);
   fsync(dev_fd);
 
   /*
@@ -316,7 +325,7 @@ int main(int argc, char *argv[])
        }
        
        if ( ioctl(loop_fd, LOOP_GET_STATUS64, &loopinfo) ||
-            (loopinfo.lo_offset = offset,
+            (loopinfo.lo_offset = filesystem_offset,
              ioctl(loop_fd, LOOP_SET_STATUS64, &loopinfo)) )
          die("cannot set up loopback device");
       }
@@ -343,7 +352,7 @@ int main(int argc, char *argv[])
       char mnt_opts[128];
       if ( S_ISREG(st.st_mode) ) {
        snprintf(mnt_opts, sizeof mnt_opts, "rw,nodev,noexec,loop,offset=%llu,umask=077,quiet",
-                (unsigned long long)offset);
+                (unsigned long long)filesystem_offset);
       } else {
        snprintf(mnt_opts, sizeof mnt_opts, "rw,nodev,noexec,umask=077,quiet");
       }
@@ -445,7 +454,7 @@ umount:
    * of the installer.
    */
   fs = libfat_open(libfat_xpread, dev_fd);
-  ldlinux_cluster = libfat_searchdir(fs, 0, "LDLINUX SYS", NULL);
+  ldlinux_cluster = libfat_searchdir(fs, 0, "LDLINUX SYS", &dentry);
   secp = sectors;
   nsectors = 0;
   s = libfat_clustertosector(fs, ldlinux_cluster);
@@ -464,20 +473,29 @@ umount:
   /*
    * Write the now-patched first sector of ldlinux.sys
    */
-  xpwrite(dev_fd, syslinux_ldlinux, 512, offset + ((off_t)sectors[0] << 9));
+  xpwrite(dev_fd, syslinux_ldlinux, 512, filesystem_offset + ((off_t)sectors[0] << 9));
+
+  /*
+   * Patch the root directory to set attributes to
+   * HIDDEN|SYSTEM|READONLY
+   */
+  {
+    const unsigned char attrib = 0x07;
+    xpwrite(dev_fd, &attrib, 1, ((off_t)dentry.sector << 9)+dentry.offset+11);
+  }
 
   /*
    * To finish up, write the boot sector
    */
 
   /* Read the superblock again since it might have changed while mounted */
-  xpread(dev_fd, sectbuf, 512, offset);
+  xpread(dev_fd, sectbuf, 512, filesystem_offset);
 
   /* Copy the syslinux code into the boot sector */
   syslinux_make_bootsect(sectbuf);
 
   /* Write new boot sector */
-  xpwrite(dev_fd, sectbuf, 512, offset);
+  xpwrite(dev_fd, sectbuf, 512, filesystem_offset);
 
   close(dev_fd);
   sync();