wok-current view linld/stuff/src/TAZBOOT.CPP @ rev 21984
linld: x86 support
author | Pascal Bellard <pascal.bellard@slitaz.org> |
---|---|
date | Mon Oct 14 11:20:06 2019 +0200 (2019-10-14) |
parents | 1c40a6bea05c |
children | fcb1de9af8f7 |
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 m->chunk_size = 0;
23 if (m->state >= initrd_state.cnt) return;
24 struct initrd_info *i = &initrd_state.info[m->state];
25 m->chunk_size = i->size;
26 m->state++;
27 lseekset2(m->fd,&i->ofs);
28 }
30 static u32 isofilesize4round()
31 {
32 return (isofilesize+3)&-4;
33 }
35 static void addinitrd()
36 {
37 u16 *pcnt = &initrd_state.cnt;
38 if (*pcnt >= MAXINITRD) return;
39 struct initrd_info *i = &initrd_state.info[(*pcnt)++];
40 i->size = isofilesize;
41 i->ofs = isofileofs;
42 initrd.size += isofilesize4round();
43 }
45 static void load_initrds()
46 {
47 struct image_himem *m = &initrd;
48 if (!m->size) return;
49 m->next_chunk = next_chunk;
50 m->fd = isofd;
51 m->state = 0;
52 next_chunk(m);
53 load_initrd();
54 }
56 static char *isokernel()
57 {
58 struct image_himem *m = ±
59 m->chunk_size = m->size = isofilesize4round();
60 m->fd = isofd;
61 return load_kernel();
62 }
64 static char buf_cmdline[4096];
65 const char *cmdline = (const char *) buf_cmdline+1;
66 static void bootiso(char **iso)
67 {
68 const char *init = " rdinit=/init.exe", *mode="menu";
69 char c;
70 static char rootfs[16], fallback[16], noauto;
71 unsigned long magic;
72 struct isostate *x=&isostate;
74 if (isoreset(*iso) == -1) return;
75 skip_alloc++;
76 base_himem = memtop() /2;
77 //if (base_himem >= _64m) base_himem = _64m;
78 if (* ((char *) &base_himem +3) >= 4) ((short *)&base_himem)[1] = _64m/_64k;
79 isoopen("boot");
80 if (iso[1] && !strcmp(mode = iso[1], "text"))
81 init = "";
82 if (isoopen(mode) == -1 || ++noauto == 0) // custom
83 isoopen("bzImage"); // SliTaz
84 magic = kver2ul(isokernel());
85 for (c = 0, x->curdirsize = 0xFFFF; isoreaddir() != -1;) {
86 if (strstr(x->filename, ".gz"))
87 strcpy(fallback, x->filename);
88 if (strhead(x->filename, "rootfs") == -1
89 || c > x->filename[6]) continue;
90 c = x->filename[6];
91 strcpy(rootfs, x->filename);
92 }
94 strcatb(buf_cmdline,"autologin bootfrom=");
95 strcat(buf_cmdline,*iso);
96 if (magic < 0x20630)
97 init = ""; // Does not support multiple initramfs
99 if (noauto) {
100 char *s;
101 * (int *) buf_cmdline = 0;
102 iso++;
103 while ((s = *++iso) != NULL) {
104 if (strhead(s,"initrd=") == -1)
105 strcatb(buf_cmdline,s);
106 else if (isoopen(s+7) != -1)
107 addinitrd();
108 }
109 }
110 else if (magic != 0) {
111 static const unsigned long initrddesc = 18L;
113 if (isoopen("rootfs.gz") != -1 ||
114 isoopen(rootfs[0] ? rootfs : fallback) != -1) {
115 addinitrd();
116 }
117 if (*init && isolseek(&initrddesc) != 0) {
118 read(x->fd, &x->filemod, 10); // + x->fileofs & x->filesize
119 magic = x->filemod;
120 x->fileofs = 0x7FF0 - (x->filesize &= 0xFFFF);
121 if (((short *) &x->fileofs)[1] == 0) addinitrd();
122 else init="";
123 }
124 strcat(buf_cmdline,init);
125 strcatb(buf_cmdline,"mode=");
126 strcat(buf_cmdline,mode);
127 strcatb(buf_cmdline,"magic=");
128 strcat(buf_cmdline,(char *)ultoa(magic));
129 }
130 load_initrds();
131 close(x->fd);
132 boot_kernel();
133 }
135 u32 root_dev;
136 u32 vid_mode;
137 u32 topmem;
138 u32 base_himem;
139 const char* kernel_name = "bzImage";
140 const char* initrd_name;
141 char* iso;
143 int main(int argc, char *argv[])
144 {
145 ((u16*) &base_himem)[1] |= (_1m+_64k)>>16; // base_himem = _1m+_64k
147 if (argc < 2) {
148 try_default_args();
149 dousage:
150 die("Usage: tazboot [[@commands]|[-f][-b base_himem][kernel=<bzImage>] \
151 [initrd=<rootfs>[,<rootfs2>...]] [bootfrom=<isofile>] ...]\n\n\
152 Defaults: tazboot kernel=bzImage auto\n\n\
153 Examples for tazboot.cmd:\n\n\
154 bootfrom=\\isos\\slitaz-4.0.iso\n\
155 kernel=boot/bzImage\n\
156 initrd=boot/rootfs4.gz,boot/rootfs3.gz,boot/rootfs2.gz,boot/rootfs1.gz,\\slitaz\\extrafs.gz\n\
157 vga=normal autologin\n\n\
158 kernel=\\slitaz\\elks\n\
159 root=/dev/bda1 ro\n");
160 }
161 bootiso(argv + 1); // iso ? parsing is /init.exe stuff !
162 for (int i=0;;) {
163 char *s;
164 next: argv++;
165 s=*argv;
166 i++;
167 if (!s) {
168 if (isoreset(iso) != -1) {
169 s = (char *) initrd_name;
170 if (isoopen((char *) kernel_name) != -1) {
171 isokernel();
172 }
173 if (s) {
174 do {
175 char *p, c;
176 for (p = s; *s && *s != ','; s++);
177 c = *s; *s = 0;
178 if (isoopen(p) != -1) {
179 addinitrd();
180 }
181 *s = c;
182 if (c) s++;
183 } while (*s);
184 load_initrds();
185 }
186 }
187 else {
188 load_kernel();
189 load_initrd();
190 }
191 boot_kernel();
192 }
193 #ifdef USE_ARGSTR
194 if ((*(u16 *)s|0x2002) == 0x662F) { // -F /f
195 skip_alloc++;
196 goto next;
197 }
198 if (argstr(s,"kernel/image|initrd|bootfrom/iso",&kernel_name) != -1);
199 else if (argnum(s,"root|vga|mem/-e|-b",&root_dev) != -1);
200 else if(i == 1 && fileexist(s) != -1) {
201 kernel_name = s;
202 }
203 else strcatb(buf_cmdline,*argv); // FIXME mem ?
204 }
205 #else
206 if (strhead(s,"initrd=") != -1) {
207 s += 7;
208 initrd_name = s;
209 }
210 else if (strhead(s,"bootfrom=") != -1) {
211 s += 9;
212 goto set_iso;
213 }
214 else if (strhead(s,"iso=") != -1) {
215 s += 4;
216 set_iso:
217 iso = s;
218 }
219 else if (strhead(s,"image=") != -1) {
220 goto set_kernel;
221 }
222 else if(strhead(s,"vga=") != -1) {
223 *(u16*)&vid_mode = (u16)strtol(s+4); // support normal, extended & ask
224 }
225 else if (strhead(s,"kernel=") != -1) {
226 s++;
227 set_kernel:
228 s += 6;
229 set_kernelz:
230 kernel_name = s;
231 }
232 else switch (*(u16 *)s|0x2002) {
233 case 0x662F: // -F /f
234 skip_alloc++;
235 goto next;
236 case 0x652F: // -E /e
237 s=*++argv;
238 goto set_topmem;
239 case 0x622F: // -B /b
240 argv++;
241 ((u16 *)&base_himem)[1] = (u16)(strtol(*argv)>>16);
242 goto next;
243 default:
244 if(i == 1 && fileexist(s) != -1) {
245 goto set_kernelz;
246 }
247 else {
248 strcatb(buf_cmdline,*argv);
249 if(strhead(s,"root=") != -1) {
250 *(u16*)&root_dev = (u16)strtol(s+5);
251 }
252 if(strhead(s,"mem=") != -1) {
253 s += 4;
254 set_topmem:
255 ((u16 *)&topmem)[1] = (u16)(strtol(s)>>16);
256 }
257 }}
258 }
259 #endif
260 }