wok annotate syslinux/stuff/iso2exe/boot.c @ rev 16002

syslinux/iso2exe: fix 16M hole
author Pascal Bellard <pascal.bellard@slitaz.org>
date Tue Mar 04 09:23:30 2014 +0000 (2014-03-04)
parents 19258b949c1b
children 3bb29e042c1c
rev   line source
pascal@13691 1 #include <asm/limits.h>
pascal@13691 2 #include <sys/types.h>
pascal@13691 3 #include <unistd.h>
pascal@13691 4 #include <fcntl.h>
pascal@13691 5 #include <stdio.h>
pascal@13691 6 #include "iso9660.h"
pascal@13691 7 #include "bootlinux.h"
pascal@13691 8 #include "libdos.h"
pascal@13691 9
pascal@13691 10 static void usage(char *iso)
pascal@13691 11 {
pascal@13691 12 printf("Usage: %s [[@commands]|[kernel=<bzimage>] \
pascal@13691 13 [initrd=<rootfs>[,<rootfs2>...]] [iso=<isofile>] ...]\n\n\
pascal@13691 14 Defaults: %s @tazboot.cmd or %s kernel=bzImage auto\n\n\
pascal@13691 15 Examples for tazboot.cmd:\n\n\
pascal@13691 16 iso=\\isos\\slitaz-4.0.iso\n\
pascal@13691 17 kernel=boot/bzImage\n\
pascal@13691 18 initrd=boot/rootfs4.gz,boot/rootfs3.gz,boot/rootfs2.gz,boot/rootfs1.gz\n\
pascal@13691 19 rw root=/dev/null vga=normal autologin\n\n\
pascal@13691 20 kernel=\\slitaz\\vmlinuz\n\
pascal@13691 21 root=/dev/sda5 ro\n",iso,iso,iso);
pascal@13691 22 exit(1);
pascal@13691 23 }
pascal@13691 24
pascal@13691 25 static void bootiso(char **iso)
pascal@13691 26 {
pascal@13691 27 char *init = "rdinit=/init.exe", *mode="menu";
pascal@16002 28 char c, rootfs[16], cmdline[256];
pascal@13691 29 int fd, restart;
pascal@13691 30 unsigned long magic;
pascal@13691 31
pascal@13691 32 if (isoreset(*iso) || isoopen("boot")) return;
pascal@13691 33 if (iso[1] && !strcmp(mode = iso[1], "text"))
pascal@13691 34 init = "";
pascal@13691 35 for (c = 0, restart = 1; isoreaddir(restart) == 0; restart = 0) {
pascal@16002 36 if (strncmp(isofilename, "rootfs", 6)
pascal@16002 37 || c > isofilename[6]) continue;
pascal@13691 38 strcpy(rootfs, isofilename);
pascal@16002 39 c = isofilename[6];
pascal@13691 40 }
pascal@13691 41 if (isoopen(mode))
pascal@13691 42 isoopen("bzImage");
pascal@14257 43 if (loadkernel() < 0x20630)
pascal@14257 44 init = ""; // Does not support multiple initramfs
pascal@13691 45 isoopen(rootfs);
pascal@13691 46 loadinitrd();
pascal@13972 47 lseek(isofd, 24, SEEK_SET);
pascal@13691 48 read(isofd, &magic, 4);
pascal@13691 49 isofilesize = magic & 0xFFFF;
pascal@14264 50 isofileofs = 0x7EE0 - isofilesize;
pascal@13691 51 loadinitrd();
pascal@13691 52 close(isofd);
pascal@13691 53 sprintf(cmdline,"rw root=/dev/null %s iso=%s magic=%lu mode=%s",
pascal@13691 54 init, *iso, magic, mode);
pascal@13691 55 bootlinux(cmdline);
pascal@13691 56 }
pascal@13691 57
pascal@13691 58 static int stricmp(char *ref, char *s)
pascal@13691 59 {
pascal@13691 60 char c;
pascal@13691 61 while (*ref) {
pascal@13691 62 c = *s++;
pascal@13691 63 if (c >= 'A' && c <= 'Z') c += 'a' - 'A';
pascal@13691 64 c -= *ref++;
pascal@13691 65 if (c) return c;
pascal@13691 66 }
pascal@13691 67 return 0;
pascal@13691 68 }
pascal@13691 69
pascal@13691 70 static char *iso;
pascal@13691 71 static int fakeopen(char *file)
pascal@13691 72 {
pascal@16002 73 if (file) {
pascal@16002 74 char *s = file;
pascal@16002 75 while (*s && *s != '\r' && *s != '\n') s++;
pascal@16002 76 *s = 0;
pascal@16002 77 }
pascal@13691 78 if (iso) {
pascal@13691 79 isoreset(iso);
pascal@13691 80 return isoopen(file);
pascal@13691 81 }
pascal@13691 82 close(isofd);
pascal@13691 83 isofd = open(file, O_RDONLY);
pascal@13691 84 if (isofd != -1) {
pascal@13691 85 isofileofs = 0;
pascal@13691 86 isofilesize = LONG_MAX;
pascal@13691 87 }
pascal@13691 88 return isofd;
pascal@13691 89 }
pascal@13691 90
pascal@13691 91 static char args[2048];
pascal@13691 92 int main(int argc, char *argv[])
pascal@13691 93 {
pascal@13691 94 char *kernel, *initrd, *cmdline, *cmdfile, *s;
pascal@13691 95
pascal@13691 96 argv[0] = progname();
pascal@14268 97 bootiso(argv); // iso ? parsing is /init.exe stuff !
pascal@14268 98 if (argc == 2)
pascal@14268 99 bootiso(argv + 1);
pascal@13691 100
pascal@13691 101 chdirname(*argv);
pascal@13691 102 cmdfile = "tazboot.cmd";
pascal@13691 103 kernel = "bzImage";
pascal@13691 104 initrd = NULL;
pascal@13691 105 cmdline = "auto";
pascal@13691 106 if (argc > 1) {
pascal@13691 107 if (argv[1][0] == '@')
pascal@13691 108 cmdfile = argv[1] + 1;
pascal@13691 109 else {
pascal@13691 110 cmdfile = NULL;
pascal@13691 111 #asm
pascal@13691 112 push ds
pascal@13691 113 pop es
pascal@13691 114 mov si, #0x82
pascal@13691 115 mov di, #_args
pascal@13691 116 mov cx, #0x7E/2
pascal@13691 117 rep
pascal@13697 118 seg cs
pascal@13691 119 movsw
pascal@13691 120 #endasm
pascal@13691 121 }
pascal@13691 122 }
pascal@13691 123 if (cmdfile) {
pascal@13691 124 int fd;
pascal@13691 125 fd = open(cmdfile, O_RDONLY);;
pascal@13691 126 if (fd != -1) {
pascal@13691 127 read(fd, args, sizeof(args));
pascal@13691 128 close(fd);
pascal@13691 129 for (s = args; s < args + sizeof(args) -1; s++) {
pascal@13691 130 if (*s == '\r') *s++ = ' ';
pascal@13691 131 if (*s == '\n') *s = ' ';
pascal@13691 132 }
pascal@13691 133 }
pascal@13691 134 }
pascal@13691 135 for (s = args; s < args + sizeof(args); s++) {
pascal@13691 136 if (*s == ' ') continue;
pascal@13691 137 if (stricmp("kernel=", s) == 0)
pascal@13691 138 kernel = s + 7;
pascal@13691 139 else if (stricmp("initrd=", s) == 0)
pascal@13691 140 initrd = s + 7;
pascal@13691 141 else if (stricmp("iso=", s) == 0)
pascal@13691 142 iso = s + 4;
pascal@13691 143 else {
pascal@13691 144 cmdline = s;
pascal@13691 145 break;
pascal@13691 146 }
pascal@13691 147 while (*s && *s != ' ') s++;
pascal@13691 148 *s = 0;
pascal@13691 149 }
pascal@16002 150 if (cmdline) {
pascal@16002 151 char *last;
pascal@16002 152 for (s = cmdline; *s && *s != '\r' && *s != '\n'; s++)
pascal@16002 153 if (*s != ' ') last = s;
pascal@16002 154 *++last = 0;
pascal@16002 155 }
pascal@13691 156 if (fakeopen(kernel) == -1)
pascal@13691 157 usage(argv[0]);
pascal@13691 158 loadkernel();
pascal@13691 159 if (initrd) {
pascal@13691 160 char *p, *q = initrd;
pascal@13691 161 while (1) {
pascal@13691 162 char c;
pascal@13691 163 for (p = q; *p && *p != ','; p++);
pascal@13691 164 c = *p;
pascal@13691 165 *p = 0;
pascal@13691 166 if (fakeopen(q) != -1)
pascal@13691 167 loadinitrd();
pascal@13691 168 if (c == 0)
pascal@13691 169 break;
pascal@13691 170 q = ++p;
pascal@13691 171 }
pascal@13691 172 }
pascal@13691 173 bootlinux(cmdline);
pascal@13691 174 }