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

syslinux/iso2exe: fix cmdline
author Pascal Bellard <pascal.bellard@slitaz.org>
date Fri Mar 07 10:50:10 2014 +0000 (2014-03-07)
parents 592369f78d12
children 8cf93f4aedd1
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", *fmt="";
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 magic = loadkernel();
44 if (magic < 0x20630)
45 init = ""; // Does not support multiple initramfs
46 if (magic > 0) {
47 fmt = "rw root=/dev/null%s iso=%s magic=%lu mode=%s";
48 isoopen(rootfs);
49 loadinitrd();
50 if (*init) {
51 lseek(isofd, 24L, SEEK_SET);
52 read(isofd, &magic, 4);
53 isofilesize = magic & 0xFFFFL;
54 isofileofs = 0x7EE0L - isofilesize;
55 if (isofilesize) loadinitrd();
56 else init="";
57 }
58 }
59 sprintf(cmdline, fmt, init, *iso, magic, mode);
60 close(isofd);
61 bootlinux(cmdline);
62 }
64 static int stricmp(char *ref, char *s)
65 {
66 char c;
67 while (*ref) {
68 c = *s++;
69 if (c >= 'A' && c <= 'Z') c += 'a' - 'A';
70 c -= *ref++;
71 if (c) return c;
72 }
73 return 0;
74 }
76 static int chkstatus(int status, char *name)
77 {
78 if (status == -1)
79 printf("%s not found.\n",name);
80 return status;
81 }
83 static char *iso;
84 static int fakeopen(char *file)
85 {
86 if (file) {
87 char *s = file;
88 while (*s && *s != '\r' && *s != '\n') s++;
89 *s = 0;
90 }
91 if (*file == '\\') {
92 static fd = -1;
93 if (fd >= 0) close(fd);
94 fd = chkstatus(open(file, O_RDONLY), file);
95 return fd;
96 }
97 if (iso) {
98 chkstatus(isoreset(iso), iso);
99 return chkstatus(isoopen(file), file);
100 }
101 close(isofd);
102 isofd = chkstatus(open(file, O_RDONLY), file);
103 if (isofd != -1) {
104 isofileofs = 0;
105 isofilesize = LONG_MAX;
106 }
107 return isofd;
108 }
110 static char args[2048];
111 int main(int argc, char *argv[])
112 {
113 char *kernel, *initrd, *cmdline, *cmdfile, *s;
115 argv[0] = progname();
116 bootiso(argv); // iso ? parsing is /init.exe stuff !
117 if (argc >= 2)
118 bootiso(argv + 1);
120 chdirname(*argv);
121 cmdfile = "tazboot.cmd";
122 kernel = "bzImage";
123 initrd = NULL;
124 cmdline = "auto";
125 if (argc > 1) {
126 if (argv[1][0] == '@')
127 cmdfile = argv[1] + 1;
128 else {
129 cmdfile = NULL;
130 #asm
131 push ds
132 pop es
133 mov si, #0x82
134 mov di, #_args
135 mov cx, #0x7E/2
136 rep
137 seg cs
138 movsw
139 #endasm
140 }
141 }
142 if (cmdfile) {
143 int fd;
144 fd = chkstatus(open(cmdfile, O_RDONLY), cmdfile);
145 if (fd != -1) {
146 read(fd, args, sizeof(args));
147 close(fd);
148 for (s = args; s < args + sizeof(args) -1; s++) {
149 if (*s == '\r') *s++ = ' ';
150 if (*s == '\n') *s = ' ';
151 }
152 }
153 }
154 for (s = args; s < args + sizeof(args); s++) {
155 if (*s == ' ') continue;
156 if (stricmp("kernel=", s) == 0)
157 kernel = s + 7;
158 else if (stricmp("initrd=", s) == 0)
159 initrd = s + 7;
160 else if (stricmp("iso=", s) == 0)
161 iso = s + 4;
162 else {
163 cmdline = s;
164 break;
165 }
166 while (*s && *s != ' ') s++;
167 *s = 0;
168 }
169 if (cmdline) {
170 char *last;
171 for (s = cmdline; *s && *s != '\r' && *s != '\n'; s++)
172 if (*s != ' ') last = s;
173 *++last = 0;
174 }
175 if (fakeopen(kernel) == -1)
176 usage(argv[0]);
177 loadkernel();
178 if (initrd) {
179 char *p, *q = initrd;
180 while (1) {
181 char c;
182 for (p = q; *p && *p != ','; p++);
183 c = *p;
184 *p = 0;
185 if (fakeopen(q) != -1)
186 loadinitrd();
187 if (c == 0)
188 break;
189 q = ++p;
190 }
191 }
192 bootlinux(cmdline);
193 }