wok-next view syslinux/stuff/iso2exe/boot.c @ rev 18672
syslinux/iso2exe: undo custom config too
author | Pascal Bellard <pascal.bellard@slitaz.org> |
---|---|
date | Sat Dec 05 12:23:18 2015 +0100 (2015-12-05) |
parents | aa46cf9cc35a |
children | fd3dadf90ba9 |
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, 20L, SEEK_SET);
75 read(isofd, &isofileofs, 4);
76 isofileofs &= 0xFFFFL;
77 read(isofd, &magic, 4);
78 isofilesize = magic & 0xFFFFL;
79 isofileofs -= 0xC0L + isofilesize;
80 if (isofilesize) loadinitrd();
81 else init="";
82 }
83 }
84 if (isknoppix) {
85 if (iso[0][1] == ':')
86 *iso += 2;
87 for (s = *iso; *s; s++)
88 if (*s == '\\') *s = '/';
89 }
90 sprintf(cmdline, fmt, *iso, init, magic, mode);
91 close(isofd);
92 bootlinux(cmdline);
93 }
95 static int stricmp(char *ref, char *s)
96 {
97 char c;
98 while (*ref) {
99 c = *s++;
100 if (c >= 'A' && c <= 'Z') c += 'a' - 'A';
101 c -= *ref++;
102 if (c) return c;
103 }
104 return 0;
105 }
107 static int chkstatus(int status, char *name)
108 {
109 if (status == -1)
110 printf("%s not found.\n",name);
111 return status;
112 }
114 static char *iso;
115 static int fakeopen(char *file)
116 {
117 if (file) {
118 char *s = file;
119 while (*s && *s != '\r' && *s != '\n') s++;
120 *s = 0;
121 }
122 if (*file == '\\') {
123 static fd = -1;
124 if (fd >= 0) close(fd);
125 fd = chkstatus(open(file, O_RDONLY), file);
126 return fd;
127 }
128 if (iso) {
129 chkstatus(isoreset(iso), iso);
130 return chkstatus(isoopen(file), file);
131 }
132 close(isofd);
133 isofd = chkstatus(open(file, O_RDONLY), file);
134 if (isofd != -1) {
135 isofileofs = 0;
136 isofilesize = LONG_MAX;
137 }
138 return isofd;
139 }
141 static char args[2048];
142 int main(int argc, char *argv[])
143 {
144 char *kernel, *initrd, *cmdline, *cmdfile, *s;
146 argv[0] = progname();
147 bootiso(argv); // iso ? parsing is /init.exe stuff !
148 if (argc >= 2)
149 bootiso(argv + 1);
151 chdirname(*argv);
152 cmdfile = "tazboot.cmd";
153 kernel = "bzImage";
154 initrd = NULL;
155 cmdline = "auto";
156 if (argc > 1) {
157 if (argv[1][0] == '@')
158 cmdfile = argv[1] + 1;
159 else {
160 cmdfile = NULL;
161 copycmdline(args);
162 }
163 }
164 if (cmdfile) {
165 int fd;
166 fd = chkstatus(open(cmdfile, O_RDONLY), cmdfile);
167 if (fd != -1) {
168 read(fd, args, sizeof(args));
169 close(fd);
170 for (s = args; s < args + sizeof(args) -1; s++) {
171 if (*s == '\r') *s++ = ' ';
172 if (*s == '\n') *s = ' ';
173 }
174 }
175 }
176 for (s = args; s < args + sizeof(args); s++) {
177 if (*s == ' ') continue;
178 if (stricmp("kernel=", s) == 0)
179 kernel = s + 7;
180 else if (stricmp("initrd=", s) == 0)
181 initrd = s + 7;
182 else if (stricmp("bootfrom=", s) == 0)
183 iso = s + 4;
184 else {
185 cmdline = s;
186 break;
187 }
188 while (*s && *s != ' ') s++;
189 *s = 0;
190 }
191 if (cmdline) {
192 char *last;
193 for (s = cmdline; *s && *s != '\r' && *s != '\n'; s++)
194 if (*s != ' ') last = s;
195 *++last = 0;
196 }
197 if (fakeopen(kernel) == -1)
198 usage(argv[0]);
199 loadkernel();
200 if (initrd) {
201 char *p, *q = initrd;
202 while (1) {
203 char c;
204 for (p = q; *p && *p != ','; p++);
205 c = *p;
206 *p = 0;
207 if (fakeopen(q) != -1)
208 loadinitrd();
209 if (c == 0)
210 break;
211 q = ++p;
212 }
213 }
214 bootlinux(cmdline);
215 }