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