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

openssh: add ssh-copy-id
author Pascal Bellard <pascal.bellard@slitaz.org>
date Sat Feb 23 23:00:18 2019 +0100 (2019-02-23)
parents cbcb33ee9044
children 87b6697bb350
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 lseekset2(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 unsigned long magic;
73 struct isostate *x=&isostate;
75 if (!*iso || isoreset(*iso) < 0) return;
76 skip_alloc++;
77 base_himem = memtop() /2;
78 //if (base_himem >= _64m) base_himem = _64m;
79 if (* ((char *) &base_himem +3) >= 4) base_himem = _64m;
80 isoopen("boot") >= 0 ||
81 isoopen("live") >= 0 || // debian
82 isoopen("casper") >= 0; // ubuntu
83 if (iso[1] && !strcmp(mode = iso[1], "text"))
84 init = "";
85 do {
86 if ((isoopen(mode) >= 0 && ++noauto != 0) || // custom
87 isoopen("bzImage") >= 0 || // SliTaz
88 isoopen("vmlinuz") >= 0 || // misc
89 (isoopen("linux") >= 0 && ++isknoppix != 0)) {
90 magic = kver2ul(isokernel());
91 break;
92 }
93 } while (isoopen("isolinux") >= 0); // Knoppix
94 for (c = 0, x->curdirsize = 0xFFFF; isoreaddir() == 0;) {
95 if (strstr(x->filename, ".gz"))
96 strcpy(fallback, x->filename);
97 if (strhead(x->filename, "rootfs")
98 || c > x->filename[6]) continue;
99 strcpy(rootfs, x->filename);
100 c = x->filename[6];
101 }
103 strcatb(buf_cmdline,"rw root=/dev/null autologin bootfrom=");
104 strcat(buf_cmdline,*iso);
105 if (magic < 0x20630)
106 init = ""; // Does not support multiple initramfs
108 if (noauto) {
109 char *s;
110 * (int *) buf_cmdline = 0;
111 iso++;
112 while ((s = *++iso) != NULL) {
113 if (strhead(s,"initrd="))
114 strcatb(buf_cmdline,s);
115 else if (isoopen(s+7) >= 0)
116 addinitrd();
117 }
118 }
119 else if (magic > 0) {
120 char *initrdfilename = fallback;
121 static const unsigned long initrddesc = 18L;
123 if (rootfs[0]) {
124 initrdfilename = rootfs;
125 if (rootfs[6] != '.' && isoopen("rootfs.gz") >= 0)
126 addinitrd(); // for loram
127 }
128 if (isoopen(initrdfilename) >= 0) {
129 addinitrd();
130 }
131 if (*init && isolseek(&initrddesc) != 0) {
132 read(x->fd, &x->filemod, 10); // + x->fileofs & x->filesize
133 magic = x->filemod;
134 x->fileofs = 0x7FF0 - (x->filesize &= 0xFFFF);
135 if (((short *) &x->fileofs)[1] == 0) addinitrd();
136 else init="";
137 }
138 strcat(buf_cmdline,init);
139 strcatb(buf_cmdline,"mode=");
140 strcat(buf_cmdline,mode);
141 strcatb(buf_cmdline,"magic=");
142 strcat(buf_cmdline,(char *)ultoa(magic));
143 }
144 load_initrds();
145 if (isknoppix) {
146 char *s = *iso;
147 if (s[1] == ':')
148 s += 2;
149 for (; *s; s++)
150 if (*s == '\\') *s = '/';
151 }
152 close(x->fd);
153 boot_kernel();
154 }
156 u32 root_dev;
157 u32 vid_mode;
158 u32 topmem;
159 const char* kernel_name = "bzImage";
160 const char* initrd_name;
161 char* iso;
163 int main(int argc, char *argv[])
164 {
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 #ifdef USE_ARGSTR
191 if ((*(u16 *)s|0x2002) == 0x662F) { // -F /f
192 skip_alloc++;
193 goto next;
194 }
195 if (argstr(s,"kernel/image|initrd|bootfrom/iso",&kernel_name) == 0);
196 else if (argnum(s,"root|vga|mem/-e|-b",&root_dev) == 0);
197 else if(i == 1 && fileexist(s) != -1) {
198 kernel_name = s;
199 }
200 else strcatb(buf_cmdline,*argv);
201 }
202 #else
203 if (strhead(s,"kernel=") == 0) {
204 s++;
205 set_kernel:
206 s += 6;
207 set_kernelz:
208 kernel_name = s;
209 }
210 else if (strhead(s,"image=") == 0) {
211 goto set_kernel;
212 }
213 else if (strhead(s,"initrd=") == 0) {
214 s += 7;
215 initrd_name = s;
216 }
217 else if (strhead(s,"bootfrom=") == 0) {
218 s += 9;
219 set_iso:
220 iso = s;
221 }
222 else if (strhead(s,"iso=") == 0) {
223 s += 4;
224 goto set_iso;
225 }
226 else if(strhead(s,"vga=") == 0) {
227 s += 4;
228 vid_mode = strtol(s); // support normal, extended & ask
229 }
230 else switch (*(u16 *)s|0x2002) {
231 case 0x662F: // -F /f
232 skip_alloc++;
233 goto next;
234 case 0x652F: // -E /e
235 argv++;
236 topmem = strtol(*argv);
237 goto next;
238 case 0x622F: // -B /b
239 argv++;
240 base_himem = strtol(*argv);
241 goto next;
242 default:
243 if(i == 1 && fileexist(s) != -1) {
244 goto set_kernelz;
245 }
246 else {
247 if(strhead(s,"root=") == 0) {
248 s += 5;
249 root_dev = strtol(s);
250 }
251 if(strhead(s,"mem=") == 0) {
252 s += 4;
253 topmem = strtol(s);
254 }
255 strcatb(buf_cmdline,*argv);
256 }}
257 }
258 #endif
259 if (iso && isoreset(iso) >= 0) {
260 char *s = (char *) initrd_name;
261 if (isoopen((char *) kernel_name) >= 0) {
262 isokernel();
263 }
264 if (s) {
265 while (*s) {
266 char *p, c;
267 for (p = s; *s && *s != ','; s++);
268 c = *s; *s = 0;
269 if (isoopen(p) >= 0) {
270 addinitrd();
271 }
272 *s = c;
273 if (c) s++;
274 }
275 load_initrds();
276 }
277 }
278 else {
279 load_kernel();
280 load_initrd();
281 }
282 boot_kernel();
283 return _AX;
284 }