wok-6.x view linld/stuff/src/TAZBOOT.CPP @ rev 20453
Up lz4 (1.8.3)
author | Pascal Bellard <pascal.bellard@slitaz.org> |
---|---|
date | Fri Oct 05 23:02:32 2018 +0200 (2018-10-05) |
parents | bcdfc23ee041 |
children | 7b15fbf27388 |
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 = ±
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 x->fileofs -= 0xC0L + (x->filesize &= 0xFFFF);
137 if (((short *) &x->fileofs)[1] == 0) addinitrd();
138 else init="";
139 }
140 strcat(_cmdline,init);
141 strcatb(_cmdline,"mode=");
142 strcat(_cmdline,mode);
143 strcatb(_cmdline,"magic=");
144 strcat(_cmdline,ultoa(magic));
145 }
146 load_initrds();
147 if (isknoppix) {
148 char *s = *iso;
149 if (s[1] == ':')
150 s += 2;
151 for (; *s; s++)
152 if (*s == '\\') *s = '/';
153 }
154 close(x->fd);
155 boot_kernel();
156 }
158 u16 root_dev;
159 u16 vid_mode;
160 const char* kernel_name = "bzImage";
161 const char* initrd_name;
162 int main(int argc, char *argv[])
163 {
164 char *iso = NULL;
165 argv[0] = progname();
166 bootiso(argv); // iso ? parsing is /init.exe stuff !
168 if (argc < 2) {
169 try_default_args();
170 dousage:
171 die("Usage: tazboot [[@commands]|[-f][-b base_himem][kernel=<bzImage>] \
172 [initrd=<rootfs>[,<rootfs2>...]] [bootfrom=<isofile>] ...]\r\n\n\
173 Defaults: tazboot kernel=bzImage auto\r\n\n\
174 Examples for tazboot.cmd:\r\n\n\
175 bootfrom=\\isos\\slitaz-4.0.iso\r\n\
176 kernel=boot/bzImage\r\n\
177 initrd=boot/rootfs4.gz,boot/rootfs3.gz,boot/rootfs2.gz,boot/rootfs1.gz,\\slitaz\\extrafs.gz\r\n\
178 rw root=/dev/null vga=normal autologin\r\n\n\
179 kernel=\\slitaz\\elks\r\n\
180 root=/dev/bda1 ro\r\n");
181 }
182 bootiso(argv + 1);
183 chdirname(*argv);
184 for (int i=0;;) {
185 char *s;
186 next: argv++;
187 s=*argv;
188 i++;
189 if (!s) break;
190 if (strhead(s,"kernel=") == 0) {
191 s++;
192 set_kernel:
193 s += 6;
194 set_kernelz:
195 kernel_name = s;
196 }
197 else if (strhead(s,"image=") == 0) {
198 goto set_kernel;
199 }
200 else if (strhead(s,"initrd=") == 0) {
201 s += 7;
202 initrd_name = s;
203 }
204 else if (strhead(s,"bootfrom=") == 0) {
205 s += 9;
206 set_iso:
207 iso = s;
208 }
209 else if (strhead(s,"iso=") == 0) {
210 s += 4;
211 goto set_iso;
212 }
213 else if(strhead(s,"vga=") == 0) {
214 s += 4;
215 vid_mode = strtol(s); // support normal, extended & ask
216 }
217 else switch (*(u16 *)s|0x2002) {
218 case 0x662F: // -F /f
219 skip_alloc++;
220 goto next;
221 case 0x652F: // -E /e
222 argv++;
223 topmem = strtol(*argv);
224 goto next;
225 case 0x622F: // -B /b
226 argv++;
227 base_himem = strtol(*argv);
228 goto next;
229 default:
230 if(i == 1 && fileattr(s) != -1) {
231 goto set_kernelz;
232 }
233 else {
234 if(strhead(s,"root=") == 0) {
235 s += 5;
236 root_dev = strtol(s);
237 }
238 if(strhead(s,"mem=") == 0) {
239 s += 4;
240 topmem = strtol(s);
241 }
242 strcatb(_cmdline,*argv);
243 }}
244 }
245 if (iso && isoreset(iso) >= 0) {
246 char *s = (char *) initrd_name;
247 if (isoopen((char *) kernel_name) >= 0) {
248 isokernel();
249 }
250 if (s) {
251 while (*s) {
252 char *p, c;
253 for (p = s; *s && *s != ','; s++);
254 c = *s; *s = 0;
255 if (isoopen(p) >= 0) {
256 addinitrd();
257 }
258 *s = c;
259 if (c) s++;
260 }
261 load_initrds();
262 }
263 }
264 else {
265 load_kernel();
266 load_initrd();
267 }
268 boot_kernel();
269 return _AX;
270 }