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

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