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

syslinux/iso2exe: access both local and isofs namespaces
author Pascal Bellard <pascal.bellard@slitaz.org>
date Tue Mar 04 21:00:29 2014 +0000 (2014-03-04)
parents 647e4ebbbcae
children 592369f78d12
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>...]] [iso=<isofile>] ...]\n\n\
14 Defaults: %s @tazboot.cmd or %s kernel=bzImage auto\n\n\
15 Examples for tazboot.cmd:\n\n\
16 iso=\\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";
28 char c, rootfs[16], cmdline[256];
29 int restart;
30 unsigned long magic;
32 if (isoreset(*iso) || isoopen("boot")) return;
33 if (iso[1] && !strcmp(mode = iso[1], "text"))
34 init = "";
35 for (c = 0, restart = 1; isoreaddir(restart) == 0; restart = 0) {
36 if (strncmp(isofilename, "rootfs", 6)
37 || c > isofilename[6]) continue;
38 strcpy(rootfs, isofilename);
39 c = isofilename[6];
40 }
41 if (isoopen(mode))
42 isoopen("bzImage");
43 if (loadkernel() < 0x20630)
44 init = ""; // Does not support multiple initramfs
45 isoopen(rootfs);
46 loadinitrd();
47 lseek(isofd, 24, SEEK_SET);
48 read(isofd, &magic, 4);
49 isofilesize = magic & 0xFFFF;
50 isofileofs = 0x7EE0 - isofilesize;
51 loadinitrd();
52 close(isofd);
53 sprintf(cmdline,"rw root=/dev/null %s iso=%s magic=%lu mode=%s",
54 init, *iso, magic, mode);
55 bootlinux(cmdline);
56 }
58 static int stricmp(char *ref, char *s)
59 {
60 char c;
61 while (*ref) {
62 c = *s++;
63 if (c >= 'A' && c <= 'Z') c += 'a' - 'A';
64 c -= *ref++;
65 if (c) return c;
66 }
67 return 0;
68 }
70 static char *iso;
71 static int fakeopen(char *file)
72 {
73 if (file) {
74 char *s = file;
75 while (*s && *s != '\r' && *s != '\n') s++;
76 *s = 0;
77 }
78 if (*file == '\\') {
79 static fd = -1;
80 if (fd >= 0) close(fd);
81 return open(file, O_RDONLY);
82 }
83 if (iso) {
84 isoreset(iso);
85 return isoopen(file);
86 }
87 close(isofd);
88 isofd = open(file, O_RDONLY);
89 if (isofd != -1) {
90 isofileofs = 0;
91 isofilesize = LONG_MAX;
92 }
93 return isofd;
94 }
96 static char args[2048];
97 int main(int argc, char *argv[])
98 {
99 char *kernel, *initrd, *cmdline, *cmdfile, *s;
101 argv[0] = progname();
102 bootiso(argv); // iso ? parsing is /init.exe stuff !
103 if (argc == 2)
104 bootiso(argv + 1);
106 chdirname(*argv);
107 cmdfile = "tazboot.cmd";
108 kernel = "bzImage";
109 initrd = NULL;
110 cmdline = "auto";
111 if (argc > 1) {
112 if (argv[1][0] == '@')
113 cmdfile = argv[1] + 1;
114 else {
115 cmdfile = NULL;
116 #asm
117 push ds
118 pop es
119 mov si, #0x82
120 mov di, #_args
121 mov cx, #0x7E/2
122 rep
123 seg cs
124 movsw
125 #endasm
126 }
127 }
128 if (cmdfile) {
129 int fd;
130 fd = open(cmdfile, O_RDONLY);;
131 if (fd != -1) {
132 read(fd, args, sizeof(args));
133 close(fd);
134 for (s = args; s < args + sizeof(args) -1; s++) {
135 if (*s == '\r') *s++ = ' ';
136 if (*s == '\n') *s = ' ';
137 }
138 }
139 }
140 for (s = args; s < args + sizeof(args); s++) {
141 if (*s == ' ') continue;
142 if (stricmp("kernel=", s) == 0)
143 kernel = s + 7;
144 else if (stricmp("initrd=", s) == 0)
145 initrd = s + 7;
146 else if (stricmp("iso=", s) == 0)
147 iso = s + 4;
148 else {
149 cmdline = s;
150 break;
151 }
152 while (*s && *s != ' ') s++;
153 *s = 0;
154 }
155 if (cmdline) {
156 char *last;
157 for (s = cmdline; *s && *s != '\r' && *s != '\n'; s++)
158 if (*s != ' ') last = s;
159 *++last = 0;
160 }
161 if (fakeopen(kernel) == -1)
162 usage(argv[0]);
163 loadkernel();
164 if (initrd) {
165 char *p, *q = initrd;
166 while (1) {
167 char c;
168 for (p = q; *p && *p != ','; p++);
169 c = *p;
170 *p = 0;
171 if (fakeopen(q) != -1)
172 loadinitrd();
173 if (c == 0)
174 break;
175 q = ++p;
176 }
177 }
178 bootlinux(cmdline);
179 }