wok view linld/stuff/src/TAZBOOT.CPP @ rev 20454

Up zstd (1.3.6)
author Pascal Bellard <pascal.bellard@slitaz.org>
date Sat Oct 06 15:17:20 2018 +0200 (2018-10-06)
parents 46b511e941a7
children 77cbb661144f
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 if (* ((char *) &base_himem +3) >= 4) base_himem = _64m;
81 isoopen("boot") >= 0 ||
82 isoopen("live") >= 0 || // debian
83 isoopen("casper") >= 0; // ubuntu
84 if (iso[1] && !strcmp(mode = iso[1], "text"))
85 init = "";
86 do {
87 if ((isoopen(mode) >= 0 && ++noauto != 0) || // custom
88 isoopen("bzImage") >= 0 || // SliTaz
89 isoopen("vmlinuz") >= 0 || // misc
90 (isoopen("linux") >= 0 && ++isknoppix != 0)) {
91 magic = kver2ul(isokernel());
92 break;
93 }
94 } while (isoopen("isolinux") >= 0); // Knoppix
95 for (c = 0, restart = 1; isoreaddir(restart) == 0; restart = 0) {
96 if (strstr(x->filename, ".gz"))
97 strcpy(fallback, x->filename);
98 if (strhead(x->filename, "rootfs")
99 || c > x->filename[6]) continue;
100 strcpy(rootfs, x->filename);
101 c = x->filename[6];
102 }
104 strcatb(_cmdline," rw root=/dev/null autologin bootfrom=");
105 strcat(_cmdline,*iso);
106 if (magic < 0x20630)
107 init = ""; // Does not support multiple initramfs
109 if (noauto) {
110 char *s;
111 * (int *) _cmdline = 0;
112 iso++;
113 while ((s = *++iso) != NULL) {
114 if (strhead(s,"initrd="))
115 strcatb(_cmdline,s);
116 else if (isoopen(s+7) >= 0)
117 addinitrd();
118 }
119 }
120 else if (magic > 0) {
121 char *initrdfilename = fallback;
122 static const unsigned long initrddesc = 18L;
124 if (rootfs[0]) {
125 initrdfilename = rootfs;
126 if (rootfs[6] != '.' && isoopen("rootfs.gz") >= 0)
127 addinitrd(); // for loram
128 }
129 if (isoopen(initrdfilename) >= 0) {
130 addinitrd();
131 }
132 if (*init && isolseek(&initrddesc) != -1) {
133 read(x->fd, &x->filemod, 10); // + x->fileofs & x->filesize
134 magic = x->filemod;
135 //x->fileofs &= 0xFFFFL;
136 * ((short *) &x->fileofs + 1) &= 0;
137 //x->fileofs -= 0xC0L + (x->filesize &= 0xFFFF);
138 * ((short *) &x->filesize + 1) &= 0;
139 x->fileofs -= 0xC0L + x->filesize;
140 if (((short *) &x->fileofs)[1] == 0) addinitrd();
141 else init="";
142 }
143 strcat(_cmdline,init);
144 strcatb(_cmdline,"mode=");
145 strcat(_cmdline,mode);
146 strcatb(_cmdline,"magic=");
147 strcat(_cmdline,ultoa(magic));
148 }
149 load_initrds();
150 if (isknoppix) {
151 char *s = *iso;
152 if (s[1] == ':')
153 s += 2;
154 for (; *s; s++)
155 if (*s == '\\') *s = '/';
156 }
157 close(x->fd);
158 boot_kernel();
159 }
161 u16 root_dev;
162 u16 vid_mode;
163 const char* kernel_name = "bzImage";
164 const char* initrd_name;
165 int main(int argc, char *argv[])
166 {
167 char *iso = NULL;
168 argv[0] = progname();
169 bootiso(argv); // iso ? parsing is /init.exe stuff !
171 if (argc < 2) {
172 try_default_args();
173 dousage:
174 die("Usage: tazboot [[@commands]|[-f][-b base_himem][kernel=<bzImage>] \
175 [initrd=<rootfs>[,<rootfs2>...]] [bootfrom=<isofile>] ...]\r\n\n\
176 Defaults: tazboot kernel=bzImage auto\r\n\n\
177 Examples for tazboot.cmd:\r\n\n\
178 bootfrom=\\isos\\slitaz-4.0.iso\r\n\
179 kernel=boot/bzImage\r\n\
180 initrd=boot/rootfs4.gz,boot/rootfs3.gz,boot/rootfs2.gz,boot/rootfs1.gz,\\slitaz\\extrafs.gz\r\n\
181 rw root=/dev/null vga=normal autologin\r\n\n\
182 kernel=\\slitaz\\elks\r\n\
183 root=/dev/bda1 ro\r\n");
184 }
185 bootiso(argv + 1);
186 chdirname(*argv);
187 for (int i=0;;) {
188 char *s;
189 next: argv++;
190 s=*argv;
191 i++;
192 if (!s) break;
193 if (strhead(s,"kernel=") == 0) {
194 s++;
195 set_kernel:
196 s += 6;
197 set_kernelz:
198 kernel_name = s;
199 }
200 else if (strhead(s,"image=") == 0) {
201 goto set_kernel;
202 }
203 else if (strhead(s,"initrd=") == 0) {
204 s += 7;
205 initrd_name = s;
206 }
207 else if (strhead(s,"bootfrom=") == 0) {
208 s += 9;
209 set_iso:
210 iso = s;
211 }
212 else if (strhead(s,"iso=") == 0) {
213 s += 4;
214 goto set_iso;
215 }
216 else if(strhead(s,"vga=") == 0) {
217 s += 4;
218 vid_mode = strtol(s); // support normal, extended & ask
219 }
220 else switch (*(u16 *)s|0x2002) {
221 case 0x662F: // -F /f
222 skip_alloc++;
223 goto next;
224 case 0x652F: // -E /e
225 argv++;
226 topmem = strtol(*argv);
227 goto next;
228 case 0x622F: // -B /b
229 argv++;
230 base_himem = strtol(*argv);
231 goto next;
232 default:
233 if(i == 1 && fileattr(s) != -1) {
234 goto set_kernelz;
235 }
236 else {
237 if(strhead(s,"root=") == 0) {
238 s += 5;
239 root_dev = strtol(s);
240 }
241 if(strhead(s,"mem=") == 0) {
242 s += 4;
243 topmem = strtol(s);
244 }
245 strcatb(_cmdline,*argv);
246 }}
247 }
248 if (iso && isoreset(iso) >= 0) {
249 char *s = (char *) initrd_name;
250 if (isoopen((char *) kernel_name) >= 0) {
251 isokernel();
252 }
253 if (s) {
254 while (*s) {
255 char *p, c;
256 for (p = s; *s && *s != ','; s++);
257 c = *s; *s = 0;
258 if (isoopen(p) >= 0) {
259 addinitrd();
260 }
261 *s = c;
262 if (c) s++;
263 }
264 load_initrds();
265 }
266 }
267 else {
268 load_kernel();
269 load_initrd();
270 }
271 boot_kernel();
272 return _AX;
273 }