wok-current view linld/stuff/src/TAZBOOT.CPP @ rev 19878

syslinux/iso2exe: fix floppyset (again)
author Pascal Bellard <pascal.bellard@slitaz.org>
date Fri Mar 31 21:07:45 2017 +0200 (2017-03-31)
parents f01ec3e34a08
children 8525be25baad
line source
1 // This file is distributed under GPL
2 //
3 // TAZBOOT main() lives here
5 #include "crtl.h"
6 #include "crtlx.h"
7 #include "common.h"
8 #include "iso9660.h"
10 struct initrd_info {
11 u32 ofs;
12 u32 size;
13 };
14 #define MAXINITRD 10
15 static struct initrd_state {
16 struct initrd_info info[MAXINITRD];
17 u16 cnt;
18 } initrd_state;
20 static void next_chunk(struct image_himem *m)
21 {
22 struct initrd_info *i = &initrd_state.info[m->state];
23 m->chunk_size = 0;
24 if (m->state >= initrd_state.cnt) return;
25 lseekset(m->fd,i->ofs);
26 m->chunk_size = i->size;
27 m->state++;
28 }
30 static u32 isofilesize4round()
31 {
32 return (isofilesize+3)&-4;
33 }
35 static void addinitrd()
36 {
37 u16 *pcnt = &initrd_state.cnt;
38 struct initrd_info *i = &initrd_state.info[*pcnt];
39 if (*pcnt >= MAXINITRD) return;
40 ++*pcnt;
41 i->size = isofilesize;
42 i->ofs = isofileofs;
43 initrd.size += isofilesize4round();
44 }
46 static void load_initrds()
47 {
48 struct image_himem *m = &initrd;
49 if (!m->size) return;
50 m->next_chunk = next_chunk;
51 m->fd = isofd;
52 m->state = 0;
53 next_chunk(m);
54 load_initrd();
55 }
57 static char *isokernel()
58 {
59 struct image_himem *m = &pm;
60 m->chunk_size = m->size = isofilesize4round();
61 m->fd = isofd;
62 return load_kernel();
63 }
65 char _cmdline[4096];
66 const char *cmdline = (const char *) _cmdline+1;
67 static void bootiso(char **iso)
68 {
69 const char *init = " rdinit=/init.exe", *mode="menu";
70 char c;
71 static char rootfs[16], fallback[16], isknoppix, noauto;
72 int restart;
73 unsigned long magic;
74 struct isostate *x=&isostate;
76 if (!*iso || isoreset(*iso) < 0) return;
77 skip_alloc++;
78 base_himem = memtop() /2;
79 if (base_himem > _64m) base_himem = _64m;
80 isoopen("boot") >= 0 ||
81 isoopen("live") >= 0 || // debian
82 isoopen("casper") >= 0; // ubuntu
83 if (iso[1] && !strcmp(mode = iso[1], "text"))
84 init = "";
85 do {
86 if ((isoopen(mode) >= 0 && ++noauto) || // custom
87 isoopen("bzImage") >= 0 || // SliTaz
88 isoopen("vmlinuz") >= 0 || // misc
89 (isoopen("linux") >= 0 && ++isknoppix)) {
90 magic = kver2ul(isokernel());
91 break;
92 }
93 } while (isoopen("isolinux") >= 0); // Knoppix
94 for (c = 0, restart = 1; isoreaddir(restart) == 0; restart = 0) {
95 if (strstr(x->filename, ".gz"))
96 strcpy(fallback, x->filename);
97 if (strhead(x->filename, "rootfs")
98 || c > x->filename[6]) continue;
99 strcpy(rootfs, x->filename);
100 c = x->filename[6];
101 }
103 strcatb(_cmdline," rw root=/dev/null autologin bootfrom=");
104 strcat(_cmdline,*iso);
105 if (magic < 0x20630)
106 init = ""; // Does not support multiple initramfs
108 if (noauto) {
109 char *s;
110 * (int *) _cmdline = 0;
111 iso++;
112 while ((s = *++iso) != NULL) {
113 if (strhead(s,"initrd="))
114 strcatb(_cmdline,s);
115 else if (isoopen(s+7) >= 0)
116 addinitrd();
117 }
118 }
119 else if (magic > 0) {
120 char *initrd = fallback;
121 static const unsigned long initrddesc = 18L;
123 if (rootfs[0]) {
124 initrd = rootfs;
125 if (rootfs[6] != '.' && isoopen("rootfs.gz") >= 0)
126 addinitrd(); // for loram
127 }
128 if (isoopen(initrd) >= 0) {
129 addinitrd();
130 }
131 if (*init && isolseek(&initrddesc) != -1) {
132 read(x->fd, &x->filemod, 10); // + x->fileofs & x->filesize
133 magic = x->filemod;
134 x->fileofs &= 0xFFFFL;
135 x->fileofs -= 0xC0L + (x->filesize &= 0xFFFF);
136 if (((short *) &x->fileofs)[1] == 0) addinitrd();
137 else init="";
138 }
139 strcat(_cmdline,init);
140 strcatb(_cmdline,"mode=");
141 strcat(_cmdline,mode);
142 strcatb(_cmdline,"magic=");
143 strcat(_cmdline,ultoa(magic));
144 }
145 load_initrds();
146 if (isknoppix) {
147 char *s = *iso;
148 if (s[1] == ':')
149 s += 2;
150 for (; *s; s++)
151 if (*s == '\\') *s = '/';
152 }
153 close(x->fd);
154 boot_kernel();
155 }
157 u16 root_dev;
158 u16 vid_mode;
159 const char* kernel_name = "bzImage";
160 const char* initrd_name;
161 int main(int argc, char *argv[])
162 {
163 char *iso = NULL;
164 argv[0] = progname();
165 bootiso(argv); // iso ? parsing is /init.exe stuff !
167 if (argc < 2) {
168 try_default_args();
169 dousage:
170 die("Usage: tazboot [[@commands]|[-f][-b base_himem][kernel=<bzimage>] \
171 [initrd=<rootfs>[,<rootfs2>...]] [bootfrom=<isofile>] ...]\r\n\n\
172 Defaults: tazboot kernel=bzImage auto\r\n\n\
173 Examples for tazboot.cmd:\r\n\n\
174 bootfrom=\\isos\\slitaz-4.0.iso\r\n\
175 kernel=boot/bzImage\r\n\
176 initrd=boot/rootfs4.gz,boot/rootfs3.gz,boot/rootfs2.gz,boot/rootfs1.gz,\\slitaz\\extrafs.gz\r\n\
177 rw root=/dev/null vga=normal autologin\r\n\n\
178 kernel=\\slitaz\\elks\r\n\
179 root=/dev/bda1 ro\r\n");
180 }
181 bootiso(argv + 1);
182 chdirname(*argv);
183 for (int i=0;;) {
184 char *s;
185 next: argv++;
186 s=*argv;
187 i++;
188 if (!s) break;
189 if (strhead(s,"kernel=") == 0) {
190 s++;
191 set_kernel:
192 s += 6;
193 kernel_name = s;
194 }
195 else if (strhead(s,"image=") == 0) {
196 goto set_kernel;
197 }
198 else if (strhead(s,"initrd=") == 0) {
199 s += 7;
200 initrd_name = s;
201 }
202 else if (strhead(s,"bootfrom=") == 0) {
203 s += 9;
204 set_iso:
205 iso = s;
206 }
207 else if (strhead(s,"iso=") == 0) {
208 s += 4;
209 goto set_iso;
210 }
211 else if(strhead(s,"vga=") == 0) {
212 s += 4;
213 vid_mode = strtol(s); // support normal, extended & ask
214 }
215 else switch (*(u16 *)s|0x2002) {
216 case 0x662F: // -F /f
217 skip_alloc++;
218 goto next;
219 case 0x652F: // -E /e
220 argv++;
221 topmem = strtol(*argv);
222 goto next;
223 case 0x622F: // -B /b
224 argv++;
225 base_himem = strtol(*argv);
226 goto next;
227 default:
228 if(i == 1 && fileattr(s) != -1) {
229 goto set_kernel;
230 }
231 else {
232 if(strhead(s,"root=") == 0) {
233 s += 5;
234 root_dev = strtol(s);
235 }
236 if(strhead(s,"mem=") == 0) {
237 s += 4;
238 topmem = strtol(s);
239 }
240 strcatb(_cmdline,*argv);
241 }}
242 }
243 if (iso && isoreset(iso) >= 0) {
244 char *s = (char *) initrd_name;
245 if (isoopen((char *) kernel_name) >= 0) {
246 isokernel();
247 }
248 if (s) {
249 while (*s) {
250 char *p, c;
251 for (p = s; *s && *s != ','; s++);
252 c = *s; *s = 0;
253 if (isoopen(p) >= 0) {
254 addinitrd();
255 }
256 *s = c;
257 if (c) s++;
258 }
259 load_initrds();
260 }
261 }
262 else {
263 load_kernel();
264 load_initrd();
265 }
266 boot_kernel();
267 return _AX;
268 }