more stack for boot
[minix.git] / boot / bootimage.c
blob2e3754cfe3a782c3a2005ebfd89108c3a9d3e48d
1 /* bootimage.c - Load an image and start it. Author: Kees J. Bot
2 * 19 Jan 1992
3 */
4 #define BIOS 1 /* Can only be used under the BIOS. */
5 #define nil 0
6 #define _POSIX_SOURCE 1
7 #define _MINIX 1
8 #include <stddef.h>
9 #include <sys/types.h>
10 #include <sys/stat.h>
11 #include <stdlib.h>
12 #include <stdio.h>
13 #include <limits.h>
14 #include <string.h>
15 #include <errno.h>
16 #include <a.out.h>
17 #include <minix/config.h>
18 #include <minix/const.h>
19 #include <minix/type.h>
20 #include <minix/syslib.h>
21 #include <minix/tty.h>
22 #include <sys/video.h>
23 #include <kernel/const.h>
24 #include <kernel/type.h>
25 #include <machine/partition.h>
26 #include "rawfs.h"
27 #include "image.h"
28 #include "boot.h"
30 static int block_size = 0;
31 static int verboseboot = VERBOSEBOOT_QUIET;
33 #define DEBUGBASIC(params) do { \
34 if (verboseboot >= VERBOSEBOOT_BASIC) printf params; } while (0)
35 #define DEBUGMAX(params) do { \
36 if (verboseboot >= VERBOSEBOOT_MAX) printf params; } while (0)
38 extern int serial_line;
39 extern u16_t vid_port; /* Video i/o port. */
40 extern u32_t vid_mem_base; /* Video memory base address. */
41 extern u32_t vid_mem_size; /* Video memory size. */
43 #define click_shift clck_shft /* 7 char clash with click_size. */
45 /* Some kernels have extra features: */
46 #define K_I386 0x0001 /* Make the 386 transition before you call me. */
47 #define K_CLAIM 0x0002 /* I will acquire my own bss pages, thank you. */
48 #define K_CHMEM 0x0004 /* This kernel listens to chmem for its stack size. */
49 #define K_HIGH 0x0008 /* Load mm, fs, etc. in extended memory. */
50 #define K_HDR 0x0010 /* No need to patch sizes, kernel uses the headers. */
51 #define K_RET 0x0020 /* Returns to the monitor on reboot. */
52 #define K_INT86 0x0040 /* Requires generic INT support. */
53 #define K_MEML 0x0080 /* Pass a list of free memory. */
54 #define K_BRET 0x0100 /* New monitor code on shutdown in boot parameters. */
55 #define K_KHIGH 0x0200 /* Load kernel in extended memory. */
56 #define K_ALL 0x03FF /* All feature bits this monitor supports. */
59 /* Data about the different processes. */
61 #define PROCESS_MAX 16 /* Must match the space in kernel/mpx.x */
62 #define KERNEL_IDX 0 /* The first process is the kernel. */
63 #define FS 2 /* The third must be fs. */
65 struct process { /* Per-process memory adresses. */
66 u32_t entry; /* Entry point. */
67 u32_t cs; /* Code segment. */
68 u32_t ds; /* Data segment. */
69 u32_t data; /* To access the data segment. */
70 u32_t end; /* End of this process, size = (end - cs). */
71 } process[PROCESS_MAX];
72 int n_procs; /* Number of processes. */
74 /* Magic numbers in process' data space. */
75 #define MAGIC_OFF 0 /* Offset of magic # in data seg. */
76 #define CLICK_OFF 2 /* Offset in kernel text to click_shift. */
77 #define FLAGS_OFF 4 /* Offset in kernel text to flags. */
78 #define KERNEL_D_MAGIC 0x526F /* Kernel magic number. */
80 /* Offsets of sizes to be patched into kernel and fs. */
81 #define P_SIZ_OFF 0 /* Process' sizes into kernel data. */
82 #define P_INIT_OFF 4 /* Init cs & sizes into fs data. */
85 #define between(a, c, z) ((unsigned) ((c) - (a)) <= ((z) - (a)))
87 void pretty_image(const char *image)
88 /* Pretty print the name of the image to load. Translate '/' and '_' to
89 * space, first letter goes uppercase. An 'r' before a digit prints as
90 * 'revision'. E.g. 'minix/1.6.16r10' -> 'Minix 1.6.16 revision 10'.
91 * The idea is that the part before the 'r' is the official Minix release
92 * and after the 'r' you can put version numbers for your own changes.
95 int up= 0, c;
97 while ((c= *image++) != 0) {
98 if (c == '/' || c == '_') c= ' ';
100 if (c == 'r' && between('0', *image, '9')) {
101 printf(" revision ");
102 continue;
104 if (!up && between('a', c, 'z')) c= c - 'a' + 'A';
106 if (between('A', c, 'Z')) up= 1;
108 putch(c);
112 #define RAW_ALIGN 16
113 #define BUFSIZE_ZEROS 128
115 void raw_clear(u32_t addr, u32_t count)
116 /* Clear "count" bytes at absolute address "addr". */
118 static char zerosdata[BUFSIZE_ZEROS + RAW_ALIGN];
119 char *zeros = zerosdata + RAW_ALIGN - (unsigned) &zerosdata % RAW_ALIGN;
120 u32_t dst;
121 u32_t zct;
123 zct= BUFSIZE_ZEROS;
124 if (zct > count) zct= count;
125 raw_copy(addr, mon2abs(zeros), zct);
126 count-= zct;
128 while (count > 0) {
129 dst= addr + zct;
130 if (zct > count) zct= count;
131 raw_copy(dst, addr, zct);
132 count-= zct;
133 zct*= 2;
137 /* Align a to a multiple of n (a power of 2): */
138 #define align(a, n) (((u32_t)(a) + ((u32_t)(n) - 1)) & ~((u32_t)(n) - 1))
139 unsigned click_shift;
140 unsigned click_size; /* click_size = Smallest kernel memory object. */
141 unsigned k_flags; /* Not all kernels are created equal. */
142 u32_t reboot_code; /* Obsolete reboot code return pointer. */
144 int params2params(char *params, size_t psize)
145 /* Repackage the environment settings for the kernel. */
147 size_t i, n;
148 environment *e;
149 char *name, *value;
150 dev_t dev;
152 i= 0;
153 for (e= env; e != nil; e= e->next) {
154 name= e->name;
155 value= e->value;
157 if (!(e->flags & E_VAR)) continue;
159 if (e->flags & E_DEV) {
160 if ((dev= name2dev(value)) == -1) return 0;
161 value= ul2a10((u16_t) dev);
164 n= i + strlen(name) + 1 + strlen(value) + 1;
165 if (n < psize) {
166 strcpy(params + i, name);
167 strcat(params + i, "=");
168 strcat(params + i, value);
170 i= n;
173 if (!(k_flags & K_MEML)) {
174 /* Require old memory size variables. */
176 value= ul2a10((mem[0].base + mem[0].size) / 1024);
177 n= i + 7 + 1 + strlen(value) + 1;
178 if (n < psize) {
179 strcpy(params + i, "memsize=");
180 strcat(params + i, value);
182 i= n;
183 value= ul2a10(mem[1].size / 1024);
184 n= i + 7 + 1 + strlen(value) + 1;
185 if (n < psize) {
186 strcpy(params + i, "emssize=");
187 strcat(params + i, value);
189 i= n;
192 if (i >= psize) {
193 printf("Too many boot parameters\n");
194 return 0;
196 params[i]= 0; /* End marked with empty string. */
197 return 1;
200 void patch_sizes(void)
201 /* Patch sizes of each process into kernel data space, kernel ds into kernel
202 * text space, and sizes of init into data space of fs. All the patched
203 * numbers are based on the kernel click size, not hardware segments.
206 u16_t text_size, data_size;
207 int i;
208 struct process *procp, *initp;
209 u32_t doff;
211 if (k_flags & K_HDR) return; /* Uses the headers. */
213 /* Patch text and data sizes of the processes into kernel data space.
215 doff= process[KERNEL_IDX].data + P_SIZ_OFF;
217 for (i= 0; i < n_procs; i++) {
218 procp= &process[i];
219 text_size= (procp->ds - procp->cs) >> click_shift;
220 data_size= (procp->end - procp->ds) >> click_shift;
222 /* Two words per process, the text and data size: */
223 put_word(doff, text_size); doff+= 2;
224 put_word(doff, data_size); doff+= 2;
226 initp= procp; /* The last process must be init. */
229 if (k_flags & (K_HIGH|K_MEML)) return; /* Doesn't need FS patching. */
231 /* Patch cs and sizes of init into fs data. */
232 put_word(process[FS].data + P_INIT_OFF+0, initp->cs >> click_shift);
233 put_word(process[FS].data + P_INIT_OFF+2, text_size);
234 put_word(process[FS].data + P_INIT_OFF+4, data_size);
237 int selected(const char *name)
238 /* True iff name has no label or the proper label. */
240 char *colon, *label;
241 int cmp;
243 if ((colon= strchr(name, ':')) == nil) return 1;
244 if ((label= b_value("label")) == nil) return 1;
246 *colon= 0;
247 cmp= strcmp(label, name);
248 *colon= ':';
249 return cmp == 0;
252 static u32_t proc_size(const struct image_header *hdr)
253 /* Return the size of a process in sectors as found in an image. */
255 u32_t len= hdr->process.a_text;
257 if (hdr->process.a_flags & A_PAL) len+= hdr->process.a_hdrlen;
258 if (hdr->process.a_flags & A_SEP) len= align(len, SECTOR_SIZE);
259 len= align(len + hdr->process.a_data, SECTOR_SIZE);
261 return len >> SECTOR_SHIFT;
264 off_t image_off, image_size;
265 u32_t (*vir2sec)(u32_t vsec); /* Where is a sector on disk? */
267 u32_t file_vir2sec(u32_t vsec)
268 /* Translate a virtual sector number to an absolute disk sector. */
270 off_t blk;
272 if(!block_size) { errno = 0; return -1; }
274 if ((blk= r_vir2abs(vsec / RATIO(block_size))) == -1) {
275 errno= EIO;
276 return -1;
278 return blk == 0 ? 0 : lowsec + blk * RATIO(block_size) + vsec % RATIO(block_size);
281 u32_t flat_vir2sec(u32_t vsec)
282 /* Simply add an absolute sector offset to vsec. */
284 return lowsec + image_off + vsec;
287 char *get_sector(u32_t vsec)
288 /* Read a sector "vsec" from the image into memory and return its address.
289 * Return nil on error. (This routine tries to read an entire track, so
290 * the next request is usually satisfied from the track buffer.)
293 u32_t sec;
294 int r;
295 #define SECBUFS 16
296 static char bufdata[SECBUFS * SECTOR_SIZE + RAW_ALIGN];
297 static size_t count; /* Number of sectors in the buffer. */
298 static u32_t bufsec; /* First Sector now in the buffer. */
299 char *buf = bufdata + RAW_ALIGN - (unsigned) &bufdata % RAW_ALIGN;
301 if (vsec == 0) count= 0; /* First sector; initialize. */
303 if ((sec= (*vir2sec)(vsec)) == -1) return nil;
305 if (sec == 0) {
306 /* A hole. */
307 count= 0;
308 memset(buf, 0, SECTOR_SIZE);
309 return buf;
312 /* Can we return a sector from the buffer? */
313 if ((sec - bufsec) < count) {
314 return buf + ((size_t) (sec - bufsec) << SECTOR_SHIFT);
317 /* Not in the buffer. */
318 count= 0;
319 bufsec= sec;
321 /* Read a whole track if possible. */
322 while (++count < SECBUFS && !dev_boundary(bufsec + count)) {
323 vsec++;
324 if ((sec= (*vir2sec)(vsec)) == -1) break;
326 /* Consecutive? */
327 if (sec != bufsec + count) break;
330 /* Actually read the sectors. */
331 if ((r= readsectors(mon2abs(buf), bufsec, count)) != 0) {
332 readerr(bufsec, r);
333 count= 0;
334 errno= 0;
335 return nil;
337 return buf;
340 int get_clickshift(u32_t ksec, struct image_header *hdr)
341 /* Get the click shift and special flags from kernel text. */
343 char *textp;
345 if ((textp= get_sector(ksec)) == nil) return 0;
347 if (hdr->process.a_flags & A_PAL) textp+= hdr->process.a_hdrlen;
348 click_shift= * (u16_t *) (textp + CLICK_OFF);
349 k_flags= * (u16_t *) (textp + FLAGS_OFF);
351 if ((k_flags & ~K_ALL) != 0) {
352 printf("%s requires features this monitor doesn't offer\n",
353 hdr->name);
354 return 0;
357 if (click_shift < HCLICK_SHIFT || click_shift > 16) {
358 printf("%s click size is bad\n", hdr->name);
359 errno= 0;
360 return 0;
363 click_size= 1 << click_shift;
365 return 1;
368 int get_segment(u32_t *vsec, long *size, u32_t *addr, u32_t limit)
369 /* Read *size bytes starting at virtual sector *vsec to memory at *addr. */
371 char *buf;
372 size_t cnt, n;
374 cnt= 0;
375 while (*size > 0) {
376 if (cnt == 0) {
377 if ((buf= get_sector((*vsec)++)) == nil) return 0;
378 cnt= SECTOR_SIZE;
380 if (*addr + click_size > limit)
382 DEBUGMAX(("get_segment: out of memory; "
383 "addr=0x%lx; limit=0x%lx; size=%lx\n",
384 *addr, limit, size));
385 errno= ENOMEM;
386 return 0;
388 n= click_size;
389 if (n > cnt) n= cnt;
390 DEBUGMAX(("raw_copy(0x%lx, 0x%lx/0x%x, 0x%lx)... ",
391 *addr, mon2abs(buf), buf, n));
392 raw_copy(*addr, mon2abs(buf), n);
393 DEBUGMAX(("done\n"));
394 *addr+= n;
395 *size-= n;
396 buf+= n;
397 cnt-= n;
400 /* Zero extend to a click. */
401 n= align(*addr, click_size) - *addr;
402 DEBUGMAX(("raw_clear(0x%lx, 0x%lx)... ", *addr, n));
403 raw_clear(*addr, n);
404 DEBUGMAX(("done\n"));
405 *addr+= n;
406 *size-= n;
407 return 1;
410 static void restore_screen(void)
412 struct boot_tty_info boot_tty_info;
413 u32_t info_location;
414 #define LINES 25
415 #define CHARS 80
416 static u16_t consolescreen[LINES][CHARS];
418 /* Try and find out what the main console was displaying
419 * by looking into video memory.
422 info_location = vid_mem_base+vid_mem_size-sizeof(boot_tty_info);
423 raw_copy(mon2abs(&boot_tty_info), info_location,
424 sizeof(boot_tty_info));
426 if(boot_tty_info.magic == TTYMAGIC) {
427 if((boot_tty_info.flags & (BTIF_CONSORIGIN|BTIF_CONSCURSOR)) ==
428 (BTIF_CONSORIGIN|BTIF_CONSCURSOR)) {
429 int line;
430 raw_copy(mon2abs(consolescreen),
431 vid_mem_base + boot_tty_info.consorigin,
432 sizeof(consolescreen));
433 clear_screen();
434 for(line = 0; line < LINES; line++) {
435 int ch;
436 for(ch = 0; ch < CHARS; ch++) {
437 u16_t newch = consolescreen[line][ch] & BYTE;
438 if(newch < ' ') newch = ' ';
439 putch(newch);
446 void exec_image(char *image)
447 /* Get a Minix image into core, patch it up and execute. */
449 int i;
450 struct image_header hdr;
451 char *buf;
452 u32_t vsec, addr, limit, aout, n, totalmem = 0;
453 struct process *procp; /* Process under construction. */
454 long a_text, a_data, a_bss, a_stack;
455 int banner= 0;
456 long processor= a2l(b_value("processor"));
457 u16_t kmagic, mode;
458 char *console;
459 char params[SECTOR_SIZE];
460 extern char *sbrk(int);
461 char *verb;
463 /* The stack is pretty deep here, so check if heap and stack collide. */
464 (void) sbrk(0);
466 if ((verb= b_value(VERBOSEBOOTVARNAME)) != nil)
467 verboseboot = a2l(verb);
469 printf("\nLoading ");
470 pretty_image(image);
471 printf(".\n");
473 vsec= 0; /* Load this sector from image next. */
474 addr= mem[0].base; /* Into this memory block. */
475 limit= mem[0].base + mem[0].size;
476 if (limit > caddr) limit= caddr;
478 /* Allocate and clear the area where the headers will be placed. */
479 aout = (limit -= PROCESS_MAX * A_MINHDR);
481 /* Clear the area where the headers will be placed. */
482 raw_clear(aout, PROCESS_MAX * A_MINHDR);
484 /* Read the many different processes: */
485 for (i= 0; vsec < image_size; i++) {
486 u32_t startaddr;
487 startaddr = addr;
488 if (i == PROCESS_MAX) {
489 printf("There are more then %d programs in %s\n",
490 PROCESS_MAX, image);
491 errno= 0;
492 return;
494 procp= &process[i];
496 /* Read header. */
497 DEBUGMAX(("Reading header... "));
498 for (;;) {
499 if ((buf= get_sector(vsec++)) == nil) return;
501 memcpy(&hdr, buf, sizeof(hdr));
503 if (BADMAG(hdr.process)) { errno= ENOEXEC; return; }
505 /* Check the optional label on the process. */
506 if (selected(hdr.name)) break;
508 /* Bad label, skip this process. */
509 vsec+= proc_size(&hdr);
511 DEBUGMAX(("done\n"));
513 /* Sanity check: an 8086 can't run a 386 kernel. */
514 if (hdr.process.a_cpu == A_I80386 && processor < 386) {
515 printf("You can't run a 386 kernel on this 80%ld\n",
516 processor);
517 errno= 0;
518 return;
521 /* Get the click shift from the kernel text segment. */
522 if (i == KERNEL_IDX) {
523 if (!get_clickshift(vsec, &hdr)) return;
524 addr= align(addr, click_size);
526 /* big kernels must be loaded into extended memory */
527 if (k_flags & K_KHIGH) {
528 addr= mem[1].base;
529 limit= mem[1].base + mem[1].size;
533 /* Save a copy of the header for the kernel, with a_syms
534 * misused as the address where the process is loaded at.
536 DEBUGMAX(("raw_copy(0x%x, 0x%lx, 0x%x)... ",
537 aout + i * A_MINHDR, mon2abs(&hdr.process), A_MINHDR));
538 hdr.process.a_syms= addr;
539 raw_copy(aout + i * A_MINHDR, mon2abs(&hdr.process), A_MINHDR);
540 DEBUGMAX(("done\n"));
542 if (!banner) {
543 DEBUGBASIC((" cs ds text data bss"));
544 if (k_flags & K_CHMEM) DEBUGBASIC((" stack"));
545 DEBUGBASIC(("\n"));
546 banner= 1;
549 /* Segment sizes. */
550 DEBUGMAX(("a_text=0x%lx; a_data=0x%lx; a_bss=0x%lx; a_flags=0x%x)\n",
551 hdr.process.a_text, hdr.process.a_data,
552 hdr.process.a_bss, hdr.process.a_flags));
554 a_text= hdr.process.a_text;
555 a_data= hdr.process.a_data;
556 a_bss= hdr.process.a_bss;
557 if (k_flags & K_CHMEM) {
558 a_stack= hdr.process.a_total - a_data - a_bss;
559 if (!(hdr.process.a_flags & A_SEP)) a_stack-= a_text;
560 } else {
561 a_stack= 0;
564 /* Collect info about the process to be. */
565 procp->cs= addr;
567 /* Process may be page aligned so that the text segment contains
568 * the header, or have an unmapped zero page against vaxisms.
570 procp->entry= hdr.process.a_entry;
571 if (hdr.process.a_flags & A_PAL) a_text+= hdr.process.a_hdrlen;
572 if (hdr.process.a_flags & A_UZP) procp->cs-= click_size;
574 /* Separate I&D: two segments. Common I&D: only one. */
575 if (hdr.process.a_flags & A_SEP) {
576 /* Read the text segment. */
577 DEBUGMAX(("get_segment(0x%lx, 0x%lx, 0x%lx, 0x%lx)\n",
578 vsec, a_text, addr, limit));
579 if (!get_segment(&vsec, &a_text, &addr, limit)) return;
580 DEBUGMAX(("get_segment done vsec=0x%lx a_text=0x%lx "
581 "addr=0x%lx\n",
582 vsec, a_text, addr));
584 /* The data segment follows. */
585 procp->ds= addr;
586 if (hdr.process.a_flags & A_UZP) procp->ds-= click_size;
587 procp->data= addr;
588 } else {
589 /* Add text to data to form one segment. */
590 procp->data= addr + a_text;
591 procp->ds= procp->cs;
592 a_data+= a_text;
595 /* Read the data segment. */
596 DEBUGMAX(("get_segment(0x%lx, 0x%lx, 0x%lx, 0x%lx)\n",
597 vsec, a_data, addr, limit));
598 if (!get_segment(&vsec, &a_data, &addr, limit)) return;
599 DEBUGMAX(("get_segment done vsec=0x%lx a_data=0x%lx "
600 "addr=0x%lx\n",
601 vsec, a_data, addr));
603 /* Make space for bss and stack unless... */
604 if (i != KERNEL_IDX && (k_flags & K_CLAIM)) a_bss= a_stack= 0;
606 DEBUGBASIC(("%07lx %07lx %8ld %8ld %8ld",
607 procp->cs, procp->ds, hdr.process.a_text,
608 hdr.process.a_data, hdr.process.a_bss));
609 if (k_flags & K_CHMEM) DEBUGBASIC((" %8ld", a_stack));
611 /* Note that a_data may be negative now, but we can look at it
612 * as -a_data bss bytes.
615 /* Compute the number of bss clicks left. */
616 a_bss+= a_data;
617 n= align(a_bss, click_size);
618 a_bss-= n;
620 /* Zero out bss. */
621 DEBUGMAX(("\nraw_clear(0x%lx, 0x%lx); limit=0x%lx... ", addr, n, limit));
622 if (addr + n > limit) { errno= ENOMEM; return; }
623 raw_clear(addr, n);
624 DEBUGMAX(("done\n"));
625 addr+= n;
627 /* And the number of stack clicks. */
628 a_stack+= a_bss;
629 n= align(a_stack, click_size);
630 a_stack-= n;
632 /* Add space for the stack. */
633 addr+= n;
635 /* Process endpoint. */
636 procp->end= addr;
638 if (verboseboot >= VERBOSEBOOT_BASIC)
639 printf(" %s\n", hdr.name);
640 else {
641 u32_t mem;
642 mem = addr-startaddr;
643 printf("%s ", hdr.name);
644 totalmem += mem;
647 if (i == 0 && (k_flags & (K_HIGH | K_KHIGH)) == K_HIGH) {
648 /* Load the rest in extended memory. */
649 addr= mem[1].base;
650 limit= mem[1].base + mem[1].size;
654 if (verboseboot < VERBOSEBOOT_BASIC)
655 printf("(%dk)\n", totalmem/1024);
657 if ((n_procs= i) == 0) {
658 printf("There are no programs in %s\n", image);
659 errno= 0;
660 return;
663 /* Check the kernel magic number. */
664 raw_copy(mon2abs(&kmagic),
665 process[KERNEL_IDX].data + MAGIC_OFF, sizeof(kmagic));
666 if (kmagic != KERNEL_D_MAGIC) {
667 printf("Kernel magic number is incorrect (0x%x@0x%lx)\n",
668 kmagic, process[KERNEL_IDX].data + MAGIC_OFF);
669 errno= 0;
670 return;
673 /* Patch sizes, etc. into kernel data. */
674 DEBUGMAX(("patch_sizes()... "));
675 patch_sizes();
676 DEBUGMAX(("done\n"));
678 #if !DOS
679 if (!(k_flags & K_MEML)) {
680 /* Copy the a.out headers to the old place. */
681 raw_copy(HEADERPOS, aout, PROCESS_MAX * A_MINHDR);
683 #endif
685 /* Run the trailer function just before starting Minix. */
686 DEBUGMAX(("run_trailer()... "));
687 if (!run_trailer()) { errno= 0; return; }
688 DEBUGMAX(("done\n"));
690 /* Translate the boot parameters to what Minix likes best. */
691 DEBUGMAX(("params2params(0x%x, 0x%x)... ", params, sizeof(params)));
692 if (!params2params(params, sizeof(params))) { errno= 0; return; }
693 DEBUGMAX(("done\n"));
695 /* Set the video to the required mode. */
696 if ((console= b_value("console")) == nil || (mode= a2x(console)) == 0) {
697 mode= strcmp(b_value("chrome"), "color") == 0 ? COLOR_MODE :
698 MONO_MODE;
700 DEBUGMAX(("set_mode(%d)... ", mode));
701 set_mode(mode);
702 DEBUGMAX(("done\n"));
704 /* Close the disk. */
705 DEBUGMAX(("dev_close()... "));
706 (void) dev_close();
707 DEBUGMAX(("done\n"));
709 /* Minix. */
710 DEBUGMAX(("minix(0x%lx, 0x%lx, 0x%lx, 0x%x, 0x%x, 0x%lx)\n",
711 process[KERNEL_IDX].entry, process[KERNEL_IDX].cs,
712 process[KERNEL_IDX].ds, params, sizeof(params), aout));
713 minix(process[KERNEL_IDX].entry, process[KERNEL_IDX].cs,
714 process[KERNEL_IDX].ds, params, sizeof(params), aout);
716 if (!(k_flags & K_BRET)) {
717 extern u32_t reboot_code;
718 raw_copy(mon2abs(params), reboot_code, sizeof(params));
720 parse_code(params);
722 /* Return from Minix. Things may have changed, so assume nothing. */
723 fsok= -1;
724 errno= 0;
726 /* Read leftover character, if any. */
727 scan_keyboard();
729 /* Restore screen contents. */
730 restore_screen();
735 ino_t latest_version(char *version, struct stat *stp)
736 /* Recursively read the current directory, selecting the newest image on
737 * the way up. (One can't use r_stat while reading a directory.)
740 char name[NAME_MAX + 1];
741 ino_t ino, newest;
742 time_t mtime;
744 if ((ino= r_readdir(name)) == 0) { stp->st_mtime= 0; return 0; }
746 newest= latest_version(version, stp);
747 mtime= stp->st_mtime;
748 r_stat(ino, stp);
750 if (S_ISREG(stp->st_mode) && stp->st_mtime > mtime) {
751 newest= ino;
752 strcpy(version, name);
753 } else {
754 stp->st_mtime= mtime;
756 return newest;
759 char *select_image(char *image)
760 /* Look image up on the filesystem, if it is a file then we're done, but
761 * if its a directory then we want the newest file in that directory. If
762 * it doesn't exist at all, then see if it is 'number:number' and get the
763 * image from that absolute offset off the disk.
766 ino_t image_ino;
767 struct stat st;
769 image= strcpy(malloc((strlen(image) + 1 + NAME_MAX + 1)
770 * sizeof(char)), image);
772 fsok= r_super(&block_size) != 0;
773 if (!fsok || (image_ino= r_lookup(ROOT_INO, image)) == 0) {
774 char *size;
776 if (numprefix(image, &size) && *size++ == ':'
777 && numeric(size)) {
778 vir2sec= flat_vir2sec;
779 image_off= a2l(image);
780 image_size= a2l(size);
781 strcpy(image, "Minix");
782 return image;
784 if (!fsok)
785 printf("No image selected\n");
786 else
787 printf("Can't load %s: %s\n", image, unix_err(errno));
788 goto bail_out;
791 r_stat(image_ino, &st);
792 if (!S_ISREG(st.st_mode)) {
793 char *version= image + strlen(image);
794 char dots[NAME_MAX + 1];
796 if (!S_ISDIR(st.st_mode)) {
797 printf("%s: %s\n", image, unix_err(ENOTDIR));
798 goto bail_out;
800 (void) r_readdir(dots);
801 (void) r_readdir(dots); /* "." & ".." */
802 *version++= '/';
803 *version= 0;
804 if ((image_ino= latest_version(version, &st)) == 0) {
805 printf("There are no images in %s\n", image);
806 goto bail_out;
808 r_stat(image_ino, &st);
810 vir2sec= file_vir2sec;
811 image_size= (st.st_size + SECTOR_SIZE - 1) >> SECTOR_SHIFT;
812 return image;
813 bail_out:
814 free(image);
815 return nil;
818 void bootminix(void)
819 /* Load Minix and run it. (Given the size of this program it is surprising
820 * that it ever gets to that.)
823 char *image;
825 if ((image= select_image(b_value("image"))) == nil) return;
827 if(serial_line >= 0) {
828 char linename[2];
829 linename[0] = serial_line + '0';
830 linename[1] = '\0';
831 b_setvar(E_VAR, SERVARNAME, linename);
834 exec_image(image);
836 switch (errno) {
837 case ENOEXEC:
838 printf("%s contains a bad program header\n", image);
839 break;
840 case ENOMEM:
841 printf("Not enough memory to load %s\n", image);
842 break;
843 case EIO:
844 printf("Unsuspected EOF on %s\n", image);
845 case 0:
846 /* No error or error already reported. */;
848 free(image);
850 if(serial_line >= 0)
851 b_unset(SERVARNAME);
855 * $PchId: bootimage.c,v 1.10 2002/02/27 19:39:09 philip Exp $