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

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