]> Zhao Yanbai Git Server - minix.git/commitdiff
writeisofs: also boots plain binaries with -B
authorAntoine Leca <Antoine.Leca.1@gmail.com>
Tue, 17 Jan 2012 15:08:40 +0000 (16:08 +0100)
committerBen Gras <ben@minix3.org>
Thu, 9 Feb 2012 17:54:49 +0000 (18:54 +0100)
With -n -b file, a.out boot images can be used for CD booting;
with the new -n -B file option, plain binary (like bootxx_cd9660)
can be used instead.
Restore working the -h and -f options while there.
And add a new -F option for 2.8MB floppy image.

commands/writeisofs/writeisofs.c

index 06c5c06c10543d4617b5d6f1aff991c3b030a5a3..39c810dc89ad2a7010743111b84c1d45fd481795 100644 (file)
@@ -115,9 +115,9 @@ struct bc_validation {
 
 #define BOOTMEDIA_UNSPECIFIED  -1
 #define BOOTMEDIA_NONE         0
-#define BOOTMEDIA_120M         1
-#define BOOTMEDIA_144M         2
-#define BOOTMEDIA_288M         3
+#define BOOTMEDIA_1200K                1
+#define BOOTMEDIA_1440K                2
+#define BOOTMEDIA_2880K                3
 #define BOOTMEDIA_HARDDISK     4
 
 struct bc_initial {
@@ -170,6 +170,7 @@ int n_reserved_pathtableentries = 0, n_used_pathtableentries = 0;
 int bootmedia = BOOTMEDIA_UNSPECIFIED;
 unsigned long bootseg = 0;
 int system_type = 0;
+int bootimage_is_aout = 0;
 
 int get_system_type(int fd);
 
@@ -785,13 +786,21 @@ writebootimage(char *bootimage, int bootfd, int fd, int *currentsector,
        ssize_t written = 0, rest;
        int virtuals, rem;
        struct exec hdr;
+       struct stat sb;
        struct bap {
                off_t sector;
                int length;
        } bap[2];
 
+       bap[0].length = bap[0].sector = 0;
        bap[1].length = bap[1].sector = 0;
 
+       if (fstat(bootfd, &sb) < 0) {
+               perror("stat boot image");
+               exit(1);
+       }
+
+      if (bootimage_is_aout) {
        Read(bootfd, &hdr, A_MINHDR);
 
        if(hdr.a_magic[0] != A_MAGIC0) {
@@ -809,11 +818,26 @@ writebootimage(char *bootimage, int bootfd, int fd, int *currentsector,
 
        /* copy text+data */
        rem = hdr.a_text + hdr.a_data;
+      } else
+               rem = sb.st_size;
 
        while(rem > 0) {
                int want;
                want = rem < sizeof(buf) ? rem : sizeof(buf);
                Read(bootfd, buf, want);
+               if (written == 0) {
+                       /* check some properties at beginning. */
+                       if (!bootimage_is_aout && buf[0] == 1 && buf[1] == 3) {
+                               fprintf(stderr, "boot image %s is an a.out executable\n",
+                                               bootimage);
+                               exit(1);
+                       }
+                       if (rem >= VIRTUAL_SECTOR
+                         && (buf[510] != 0x55 || buf[511] != 0xaa) ) {
+                               fprintf(stderr, "invalid boot sector (bad magic.)\n");
+                               exit(1);
+                       }
+               }
                written += Write(fd, buf, want);
                rem -= want;
        }
@@ -833,7 +857,7 @@ writebootimage(char *bootimage, int bootfd, int fd, int *currentsector,
                        exit(1);
                }
 
-               fprintf(stderr, " * appended sector info: 0x%lx len 0x%lx\n",
+               fprintf(stderr, " * appended sector info: 0x%lx len 0x%x\n",
                        bap[0].sector, bap[0].length);
 
                addr = buf;
@@ -931,7 +955,7 @@ main(int argc, char *argv[])
                return 1;
        }
 
-       while ((ch = getopt(argc, argv, "a:b:s:Rb:hl:nf")) != -1) {
+       while ((ch = getopt(argc, argv, "a:b:B:s:Rb:hl:nfF")) != -1) {
                switch(ch) {
                        case 's':
                                if(optarg[0] != '0' || optarg[1] != 'x') {
@@ -948,7 +972,10 @@ main(int argc, char *argv[])
                                bootmedia= BOOTMEDIA_NONE;
                                break;
                        case 'f':
-                               bootmedia= BOOTMEDIA_144M;
+                               bootmedia= BOOTMEDIA_1440K;
+                               break;
+                       case 'F':
+                               bootmedia= BOOTMEDIA_2880K;
                                break;
                        case 'a':
                                if(!(appendsectorinfo = strdup(optarg)))
@@ -957,10 +984,13 @@ main(int argc, char *argv[])
                        case 'l':
                                label = optarg;
                                break;
-                       case 'r':
+                       case 'R':
                                remove_after = 1;
                                break;
                        case 'b':
+                               bootimage_is_aout= 1;
+                               /*FALLTHROUGH*/
+                       case 'B':
                                bootimage = optarg;
                                if((bootfd = open(bootimage, O_RDONLY)) < 0) {
                                        perror(bootimage);
@@ -976,7 +1006,7 @@ main(int argc, char *argv[])
        /* Args check */
 
        if(argc != 2) {
-               fprintf(stderr, "usage: %s [-l <label>] [-b <bootimage> [-n] [-f] [-h] [-s <bootsegment>] [ -a <appendfile> ] <dir> <isofile>\n",
+               fprintf(stderr, "usage: %s [-l <label>] [-(b|B) <bootimage> [-n|-f|-F|-h] [-s <bootsegment>] [ -a <appendfile> ] <dir> <isofile>\n",
                        prog);
                return 1;
        }
@@ -1175,8 +1205,8 @@ main(int argc, char *argv[])
                        writebootimage(bootimage, bootfd,
                                fd, &currentsector, appendsectorinfo, &root);
 
-                       close(bootfd);
                }
+               close(bootfd);
        }
 
        fprintf(stderr, " * all ok\n");