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

syslinux/iso2exe: check ISO md5
author Pascal Bellard <pascal.bellard@slitaz.org>
date Sat Jan 03 21:41:48 2015 +0100 (2015-01-03)
parents 2c2a6dd5ba40
children 00bb1e4137c6
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 #asm
162 push ds
163 pop es
164 mov si, #0x82
165 mov di, #_args
166 mov cx, #0x7E/2
167 rep
168 seg cs
169 movsw
170 #endasm
171 }
172 }
173 if (cmdfile) {
174 int fd;
175 fd = chkstatus(open(cmdfile, O_RDONLY), cmdfile);
176 if (fd != -1) {
177 read(fd, args, sizeof(args));
178 close(fd);
179 for (s = args; s < args + sizeof(args) -1; s++) {
180 if (*s == '\r') *s++ = ' ';
181 if (*s == '\n') *s = ' ';
182 }
183 }
184 }
185 for (s = args; s < args + sizeof(args); s++) {
186 if (*s == ' ') continue;
187 if (stricmp("kernel=", s) == 0)
188 kernel = s + 7;
189 else if (stricmp("initrd=", s) == 0)
190 initrd = s + 7;
191 else if (stricmp("bootfrom=", s) == 0)
192 iso = s + 4;
193 else {
194 cmdline = s;
195 break;
196 }
197 while (*s && *s != ' ') s++;
198 *s = 0;
199 }
200 if (cmdline) {
201 char *last;
202 for (s = cmdline; *s && *s != '\r' && *s != '\n'; s++)
203 if (*s != ' ') last = s;
204 *++last = 0;
205 }
206 if (fakeopen(kernel) == -1)
207 usage(argv[0]);
208 loadkernel();
209 if (initrd) {
210 char *p, *q = initrd;
211 while (1) {
212 char c;
213 for (p = q; *p && *p != ','; p++);
214 c = *p;
215 *p = 0;
216 if (fakeopen(q) != -1)
217 loadinitrd();
218 if (c == 0)
219 break;
220 q = ++p;
221 }
222 }
223 bootlinux(cmdline);
224 }