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