wok view syslinux/stuff/iso2exe/boot.c @ rev 19435

zerobin: add download button
author Pascal Bellard <pascal.bellard@slitaz.org>
date Tue Oct 04 15:33:49 2016 +0200 (2016-10-04)
parents fd3dadf90ba9
children e93e6b4d565f
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 switch (argv[1][0]) {
158 case '-' : case '/' : case '?' :
159 usage(argv[0]); break;
160 case '@' :
161 cmdfile = argv[1] + 1; break;
162 default :
163 cmdfile = NULL;
164 copycmdline(args);
165 }
166 }
167 if (cmdfile) {
168 int fd;
169 fd = chkstatus(open(cmdfile, O_RDONLY), cmdfile);
170 if (fd != -1) {
171 read(fd, args, sizeof(args));
172 close(fd);
173 for (s = args; s < args + sizeof(args) -1; s++) {
174 if (*s == '\r') *s++ = ' ';
175 if (*s == '\n') *s = ' ';
176 }
177 }
178 }
179 for (s = args; s < args + sizeof(args); s++) {
180 if (*s == ' ') continue;
181 if (stricmp("kernel=", s) == 0)
182 kernel = s + 7;
183 else if (stricmp("initrd=", s) == 0)
184 initrd = s + 7;
185 else if (stricmp("bootfrom=", s) == 0)
186 iso = s + 4;
187 else {
188 cmdline = s;
189 break;
190 }
191 while (*s && *s != ' ') s++;
192 *s = 0;
193 }
194 if (cmdline) {
195 char *last;
196 for (s = last = cmdline; *s && *s != '\r' && *s != '\n'; s++)
197 if (*s != ' ') last = s;
198 *++last = 0;
199 }
200 if (fakeopen(kernel) == -1)
201 usage(argv[0]);
202 loadkernel();
203 if (initrd) {
204 char *p, *q = initrd;
205 while (1) {
206 char c;
207 for (p = q; *p && *p != ','; p++);
208 c = *p;
209 *p = 0;
210 if (fakeopen(q) != -1)
211 loadinitrd();
212 if (c == 0)
213 break;
214 q = ++p;
215 }
216 }
217 bootlinux(cmdline);
218 }