wok view syslinux/stuff/iso2exe/boot.c @ rev 17489
syslinux/iso2exe: add knoppix support
author | Pascal Bellard <pascal.bellard@slitaz.org> |
---|---|
date | Thu Jan 01 22:09:19 2015 +0100 (2015-01-01) |
parents | 8cf93f4aedd1 |
children | 2c2a6dd5ba40 |
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>...]] [bootfrom=<isofile>] ...]\n\n\
14 Defaults: %s @tazboot.cmd or %s kernel=bzImage auto\n\n\
15 Examples for tazboot.cmd:\n\n\
16 bootfrom=\\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,\\slitaz\\extrafs.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", *fmt="";
28 char *s, c, rootfs[16], fallback[16], cmdline[256];
29 int restart, isknoppix = 0;
30 unsigned long magic;
32 if (isoreset(*iso)) return;
33 !isoopen("boot") ||
34 !isoopen("live") || // debian
35 !isoopen("casper") || // ubuntu
36 !isoopen("isolinux"); // zeroshell
37 if (iso[1] && !strcmp(mode = iso[1], "text"))
38 init = "";
39 do {
40 if (!isoopen(mode) || // custom
41 !isoopen("bzImage") || // SliTaz
42 !isoopen("linux24") || // dsl
43 !isoopen("vmlinuz") || // misc
44 (!isoopen("linux") && ++isknoppix)) {
45 magic = loadkernel();
46 break;
47 }
48 } while (!isoopen("isolinux")); // Knoppix
49 fallback[0] = 0;
50 for (c = 0, restart = 1; isoreaddir(restart) == 0; restart = 0) {
51 if (strstr(isofilename, ".gz"))
52 strcpy(fallback, isofilename);
53 if (strncmp(isofilename, "rootfs", 6)
54 || c > isofilename[6]) continue;
55 strcpy(rootfs, isofilename);
56 c = isofilename[6];
57 }
59 if (magic < 0x20630)
60 init = ""; // Does not support multiple initramfs
62 if (magic > 0) {
63 char *initrd = fallback;
65 fmt = "rw root=/dev/null bootfrom=%s%s magic=%lu mode=%s autologin";
66 if (rootfs[0]) {
67 initrd = rootfs;
68 if (rootfs[6] != '.' && !isoopen("rootfs.gz"))
69 loadinitrd(); // for loram
70 }
71 if (!isoopen(initrd)) {
72 loadinitrd();
73 }
74 if (*init) {
75 lseek(isofd, 24L, SEEK_SET);
76 read(isofd, &magic, 4);
77 isofilesize = magic & 0xFFFFL;
78 isofileofs = 0x7EE0L - isofilesize;
79 if (isofilesize) loadinitrd();
80 else init="";
81 }
82 }
83 if (isknoppix) {
84 if (iso[0][1] == ':')
85 *iso += 2;
86 for (s = *iso; *s; s++)
87 if (*s == '\\') *s = '/';
88 }
89 sprintf(cmdline, fmt, *iso, init, magic, mode);
90 close(isofd);
91 bootlinux(cmdline);
92 }
94 static int stricmp(char *ref, char *s)
95 {
96 char c;
97 while (*ref) {
98 c = *s++;
99 if (c >= 'A' && c <= 'Z') c += 'a' - 'A';
100 c -= *ref++;
101 if (c) return c;
102 }
103 return 0;
104 }
106 static int chkstatus(int status, char *name)
107 {
108 if (status == -1)
109 printf("%s not found.\n",name);
110 return status;
111 }
113 static char *iso;
114 static int fakeopen(char *file)
115 {
116 if (file) {
117 char *s = file;
118 while (*s && *s != '\r' && *s != '\n') s++;
119 *s = 0;
120 }
121 if (*file == '\\') {
122 static fd = -1;
123 if (fd >= 0) close(fd);
124 fd = chkstatus(open(file, O_RDONLY), file);
125 return fd;
126 }
127 if (iso) {
128 chkstatus(isoreset(iso), iso);
129 return chkstatus(isoopen(file), file);
130 }
131 close(isofd);
132 isofd = chkstatus(open(file, O_RDONLY), file);
133 if (isofd != -1) {
134 isofileofs = 0;
135 isofilesize = LONG_MAX;
136 }
137 return isofd;
138 }
140 static char args[2048];
141 int main(int argc, char *argv[])
142 {
143 char *kernel, *initrd, *cmdline, *cmdfile, *s;
145 argv[0] = progname();
146 bootiso(argv); // iso ? parsing is /init.exe stuff !
147 if (argc >= 2)
148 bootiso(argv + 1);
150 chdirname(*argv);
151 cmdfile = "tazboot.cmd";
152 kernel = "bzImage";
153 initrd = NULL;
154 cmdline = "auto";
155 if (argc > 1) {
156 if (argv[1][0] == '@')
157 cmdfile = argv[1] + 1;
158 else {
159 cmdfile = NULL;
160 #asm
161 push ds
162 pop es
163 mov si, #0x82
164 mov di, #_args
165 mov cx, #0x7E/2
166 rep
167 seg cs
168 movsw
169 #endasm
170 }
171 }
172 if (cmdfile) {
173 int fd;
174 fd = chkstatus(open(cmdfile, O_RDONLY), cmdfile);
175 if (fd != -1) {
176 read(fd, args, sizeof(args));
177 close(fd);
178 for (s = args; s < args + sizeof(args) -1; s++) {
179 if (*s == '\r') *s++ = ' ';
180 if (*s == '\n') *s = ' ';
181 }
182 }
183 }
184 for (s = args; s < args + sizeof(args); s++) {
185 if (*s == ' ') continue;
186 if (stricmp("kernel=", s) == 0)
187 kernel = s + 7;
188 else if (stricmp("initrd=", s) == 0)
189 initrd = s + 7;
190 else if (stricmp("bootfrom=", s) == 0)
191 iso = s + 4;
192 else {
193 cmdline = s;
194 break;
195 }
196 while (*s && *s != ' ') s++;
197 *s = 0;
198 }
199 if (cmdline) {
200 char *last;
201 for (s = cmdline; *s && *s != '\r' && *s != '\n'; s++)
202 if (*s != ' ') last = s;
203 *++last = 0;
204 }
205 if (fakeopen(kernel) == -1)
206 usage(argv[0]);
207 loadkernel();
208 if (initrd) {
209 char *p, *q = initrd;
210 while (1) {
211 char c;
212 for (p = q; *p && *p != ','; p++);
213 c = *p;
214 *p = 0;
215 if (fakeopen(q) != -1)
216 loadinitrd();
217 if (c == 0)
218 break;
219 q = ++p;
220 }
221 }
222 bootlinux(cmdline);
223 }