make includes fix from trunk
[minix.git] / boot / bootimage.c
blob3638199b10007eb096726b5ef8a6df204ff249ee
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 DEBUG_PRINT(params, level) do { \
34 if (verboseboot >= (level)) printf params; } while (0)
35 #define DEBUGBASIC(params) DEBUG_PRINT(params, VERBOSEBOOT_BASIC)
36 #define DEBUGEXTRA(params) DEBUG_PRINT(params, VERBOSEBOOT_EXTRA)
37 #define DEBUGMAX(params) DEBUG_PRINT(params, VERBOSEBOOT_MAX)
39 extern int serial_line;
40 extern u16_t vid_port; /* Video i/o port. */
41 extern u32_t vid_mem_base; /* Video memory base address. */
42 extern u32_t vid_mem_size; /* Video memory size. */
44 #define click_shift clck_shft /* 7 char clash with click_size. */
46 /* Some kernels have extra features: */
47 #define K_I386 0x0001 /* Make the 386 transition before you call me. */
48 #define K_CLAIM 0x0002 /* I will acquire my own bss pages, thank you. */
49 #define K_CHMEM 0x0004 /* This kernel listens to chmem for its stack size. */
50 #define K_HIGH 0x0008 /* Load mm, fs, etc. in extended memory. */
51 #define K_HDR 0x0010 /* No need to patch sizes, kernel uses the headers. */
52 #define K_RET 0x0020 /* Returns to the monitor on reboot. */
53 #define K_INT86 0x0040 /* Requires generic INT support. */
54 #define K_MEML 0x0080 /* Pass a list of free memory. */
55 #define K_BRET 0x0100 /* New monitor code on shutdown in boot parameters. */
56 #define K_KHIGH 0x0200 /* Load kernel in extended memory. */
57 #define K_ALL 0x03FF /* All feature bits this monitor supports. */
60 /* Data about the different processes. */
62 #define PROCESS_MAX 16 /* Must match the space in kernel/mpx.x */
63 #define KERNEL_IDX 0 /* The first process is the kernel. */
64 #define FS 2 /* The third must be fs. */
66 struct process { /* Per-process memory adresses. */
67 u32_t entry; /* Entry point. */
68 u32_t cs; /* Code segment. */
69 u32_t ds; /* Data segment. */
70 u32_t data; /* To access the data segment. */
71 u32_t end; /* End of this process, size = (end - cs). */
72 } process[PROCESS_MAX];
73 int n_procs; /* Number of processes. */
75 /* Magic numbers in process' data space. */
76 #define MAGIC_OFF 0 /* Offset of magic # in data seg. */
77 #define CLICK_OFF 2 /* Offset in kernel text to click_shift. */
78 #define FLAGS_OFF 4 /* Offset in kernel text to flags. */
79 #define KERNEL_D_MAGIC 0x526F /* Kernel magic number. */
81 /* Offsets of sizes to be patched into kernel and fs. */
82 #define P_SIZ_OFF 0 /* Process' sizes into kernel data. */
83 #define P_INIT_OFF 4 /* Init cs & sizes into fs data. */
86 #define between(a, c, z) ((unsigned) ((c) - (a)) <= ((z) - (a)))
88 void pretty_image(const char *image)
89 /* Pretty print the name of the image to load. Translate '/' and '_' to
90 * space, first letter goes uppercase. An 'r' before a digit prints as
91 * 'revision'. E.g. 'minix/1.6.16r10' -> 'Minix 1.6.16 revision 10'.
92 * The idea is that the part before the 'r' is the official Minix release
93 * and after the 'r' you can put version numbers for your own changes.
96 int up= 0, c;
98 while ((c= *image++) != 0) {
99 if (c == '/' || c == '_') c= ' ';
101 if (c == 'r' && between('0', *image, '9')) {
102 printf(" revision ");
103 continue;
105 if (!up && between('a', c, 'z')) c= c - 'a' + 'A';
107 if (between('A', c, 'Z')) up= 1;
109 putch(c);
113 #define RAW_ALIGN 16
114 #define BUFSIZE_ZEROS 128
116 void raw_clear(u32_t addr, u32_t count)
117 /* Clear "count" bytes at absolute address "addr". */
119 static char zerosdata[BUFSIZE_ZEROS + RAW_ALIGN];
120 char *zeros = zerosdata + RAW_ALIGN - (unsigned) &zerosdata % RAW_ALIGN;
121 u32_t dst;
122 u32_t zct;
124 zct= BUFSIZE_ZEROS;
125 if (zct > count) zct= count;
126 raw_copy(addr, mon2abs(zeros), zct);
127 count-= zct;
129 while (count > 0) {
130 dst= addr + zct;
131 if (zct > count) zct= count;
132 raw_copy(dst, addr, zct);
133 count-= zct;
134 zct*= 2;
138 /* Align a to a multiple of n (a power of 2): */
139 #define align(a, n) (((u32_t)(a) + ((u32_t)(n) - 1)) & ~((u32_t)(n) - 1))
140 unsigned click_shift;
141 unsigned click_size; /* click_size = Smallest kernel memory object. */
142 unsigned k_flags; /* Not all kernels are created equal. */
143 u32_t reboot_code; /* Obsolete reboot code return pointer. */
145 int params2params(char *params, size_t psize)
146 /* Repackage the environment settings for the kernel. */
148 size_t i, n;
149 environment *e;
150 char *name, *value;
151 dev_t dev;
153 i= 0;
154 for (e= env; e != nil; e= e->next) {
155 name= e->name;
156 value= e->value;
158 if (!(e->flags & E_VAR)) continue;
160 if (e->flags & E_DEV) {
161 if ((dev= name2dev(value)) == -1) return 0;
162 value= ul2a10((u16_t) dev);
165 n= i + strlen(name) + 1 + strlen(value) + 1;
166 if (n < psize) {
167 strcpy(params + i, name);
168 strcat(params + i, "=");
169 strcat(params + i, value);
171 i= n;
174 if (!(k_flags & K_MEML)) {
175 /* Require old memory size variables. */
177 value= ul2a10((mem[0].base + mem[0].size) / 1024);
178 n= i + 7 + 1 + strlen(value) + 1;
179 if (n < psize) {
180 strcpy(params + i, "memsize=");
181 strcat(params + i, value);
183 i= n;
184 value= ul2a10(mem[1].size / 1024);
185 n= i + 7 + 1 + strlen(value) + 1;
186 if (n < psize) {
187 strcpy(params + i, "emssize=");
188 strcat(params + i, value);
190 i= n;
193 if (i >= psize) {
194 printf("Too many boot parameters\n");
195 return 0;
197 params[i]= 0; /* End marked with empty string. */
198 return 1;
201 void patch_sizes(void)
202 /* Patch sizes of each process into kernel data space, kernel ds into kernel
203 * text space, and sizes of init into data space of fs. All the patched
204 * numbers are based on the kernel click size, not hardware segments.
207 u16_t text_size, data_size;
208 int i;
209 struct process *procp, *initp;
210 u32_t doff;
212 if (k_flags & K_HDR) return; /* Uses the headers. */
214 /* Patch text and data sizes of the processes into kernel data space.
216 doff= process[KERNEL_IDX].data + P_SIZ_OFF;
218 for (i= 0; i < n_procs; i++) {
219 procp= &process[i];
220 text_size= (procp->ds - procp->cs) >> click_shift;
221 data_size= (procp->end - procp->ds) >> click_shift;
223 /* Two words per process, the text and data size: */
224 put_word(doff, text_size); doff+= 2;
225 put_word(doff, data_size); doff+= 2;
227 initp= procp; /* The last process must be init. */
230 if (k_flags & (K_HIGH|K_MEML)) return; /* Doesn't need FS patching. */
232 /* Patch cs and sizes of init into fs data. */
233 put_word(process[FS].data + P_INIT_OFF+0, initp->cs >> click_shift);
234 put_word(process[FS].data + P_INIT_OFF+2, text_size);
235 put_word(process[FS].data + P_INIT_OFF+4, data_size);
238 int selected(const char *name)
239 /* True iff name has no label or the proper label. */
241 char *colon, *label;
242 int cmp;
244 if ((colon= strchr(name, ':')) == nil) return 1;
245 if ((label= b_value("label")) == nil) return 1;
247 *colon= 0;
248 cmp= strcmp(label, name);
249 *colon= ':';
250 return cmp == 0;
253 static u32_t proc_size(const struct image_header *hdr)
254 /* Return the size of a process in sectors as found in an image. */
256 u32_t len= hdr->process.a_text;
258 if (hdr->process.a_flags & A_PAL) len+= hdr->process.a_hdrlen;
259 if (hdr->process.a_flags & A_SEP) len= align(len, SECTOR_SIZE);
260 len= align(len + hdr->process.a_data, SECTOR_SIZE);
262 return len >> SECTOR_SHIFT;
265 off_t image_off, image_size;
266 u32_t (*vir2sec)(u32_t vsec); /* Where is a sector on disk? */
268 u32_t file_vir2sec(u32_t vsec)
269 /* Translate a virtual sector number to an absolute disk sector. */
271 off_t blk;
273 if(!block_size) { errno = 0; return -1; }
275 if ((blk= r_vir2abs(vsec / RATIO(block_size))) == -1) {
276 errno= EIO;
277 return -1;
279 return blk == 0 ? 0 : lowsec + blk * RATIO(block_size) + vsec % RATIO(block_size);
282 u32_t flat_vir2sec(u32_t vsec)
283 /* Simply add an absolute sector offset to vsec. */
285 return lowsec + image_off + vsec;
288 char *get_sector(u32_t vsec)
289 /* Read a sector "vsec" from the image into memory and return its address.
290 * Return nil on error. (This routine tries to read an entire track, so
291 * the next request is usually satisfied from the track buffer.)
294 u32_t sec;
295 int r;
296 #define SECBUFS 16
297 static char bufdata[SECBUFS * SECTOR_SIZE + RAW_ALIGN];
298 static size_t count; /* Number of sectors in the buffer. */
299 static u32_t bufsec; /* First Sector now in the buffer. */
300 char *buf = bufdata + RAW_ALIGN - (unsigned) &bufdata % RAW_ALIGN;
302 if (vsec == 0) count= 0; /* First sector; initialize. */
304 if ((sec= (*vir2sec)(vsec)) == -1) return nil;
306 if (sec == 0) {
307 /* A hole. */
308 count= 0;
309 memset(buf, 0, SECTOR_SIZE);
310 return buf;
313 /* Can we return a sector from the buffer? */
314 if ((sec - bufsec) < count) {
315 return buf + ((size_t) (sec - bufsec) << SECTOR_SHIFT);
318 /* Not in the buffer. */
319 count= 0;
320 bufsec= sec;
322 /* Read a whole track if possible. */
323 while (++count < SECBUFS && !dev_boundary(bufsec + count)) {
324 vsec++;
325 if ((sec= (*vir2sec)(vsec)) == -1) break;
327 /* Consecutive? */
328 if (sec != bufsec + count) break;
331 /* Actually read the sectors. */
332 if ((r= readsectors(mon2abs(buf), bufsec, count)) != 0) {
333 readerr(bufsec, r);
334 count= 0;
335 errno= 0;
336 return nil;
338 return buf;
341 int get_clickshift(u32_t ksec, struct image_header *hdr)
342 /* Get the click shift and special flags from kernel text. */
344 char *textp;
346 if ((textp= get_sector(ksec)) == nil) return 0;
348 if (hdr->process.a_flags & A_PAL) textp+= hdr->process.a_hdrlen;
349 click_shift= * (u16_t *) (textp + CLICK_OFF);
350 k_flags= * (u16_t *) (textp + FLAGS_OFF);
352 if ((k_flags & ~K_ALL) != 0) {
353 printf("%s requires features this monitor doesn't offer\n",
354 hdr->name);
355 return 0;
358 if (click_shift < HCLICK_SHIFT || click_shift > 16) {
359 printf("%s click size is bad\n", hdr->name);
360 errno= 0;
361 return 0;
364 click_size= 1 << click_shift;
366 return 1;
369 int get_segment(u32_t *vsec, long *size, u32_t *addr, u32_t limit)
370 /* Read *size bytes starting at virtual sector *vsec to memory at *addr. */
372 char *buf;
373 size_t cnt, n;
375 cnt= 0;
376 while (*size > 0) {
377 if (cnt == 0) {
378 if ((buf= get_sector((*vsec)++)) == nil) return 0;
379 cnt= SECTOR_SIZE;
381 if (*addr + click_size > limit)
383 DEBUGEXTRA(("get_segment: out of memory; "
384 "addr=0x%lx; limit=0x%lx; size=%lx\n",
385 *addr, limit, size));
386 errno= ENOMEM;
387 return 0;
389 n= click_size;
390 if (n > cnt) n= cnt;
391 DEBUGMAX(("raw_copy(0x%lx, 0x%lx/0x%x, 0x%lx)... ",
392 *addr, mon2abs(buf), buf, n));
393 raw_copy(*addr, mon2abs(buf), n);
394 DEBUGMAX(("done\n"));
395 *addr+= n;
396 *size-= n;
397 buf+= n;
398 cnt-= n;
401 /* Zero extend to a click. */
402 n= align(*addr, click_size) - *addr;
403 DEBUGMAX(("raw_clear(0x%lx, 0x%lx)... ", *addr, n));
404 raw_clear(*addr, n);
405 DEBUGMAX(("done\n"));
406 *addr+= n;
407 *size-= n;
408 return 1;
411 static void restore_screen(void)
413 struct boot_tty_info boot_tty_info;
414 u32_t info_location;
415 #define LINES 25
416 #define CHARS 80
417 static u16_t consolescreen[LINES][CHARS];
419 /* Try and find out what the main console was displaying
420 * by looking into video memory.
423 info_location = vid_mem_base+vid_mem_size-sizeof(boot_tty_info);
424 raw_copy(mon2abs(&boot_tty_info), info_location,
425 sizeof(boot_tty_info));
427 if(boot_tty_info.magic == TTYMAGIC) {
428 if((boot_tty_info.flags & (BTIF_CONSORIGIN|BTIF_CONSCURSOR)) ==
429 (BTIF_CONSORIGIN|BTIF_CONSCURSOR)) {
430 int line;
431 raw_copy(mon2abs(consolescreen),
432 vid_mem_base + boot_tty_info.consorigin,
433 sizeof(consolescreen));
434 clear_screen();
435 for(line = 0; line < LINES; line++) {
436 int ch;
437 for(ch = 0; ch < CHARS; ch++) {
438 u16_t newch = consolescreen[line][ch] & BYTE;
439 if(newch < ' ') newch = ' ';
440 putch(newch);
447 void exec_image(char *image)
448 /* Get a Minix image into core, patch it up and execute. */
450 int i;
451 struct image_header hdr;
452 char *buf;
453 u32_t vsec, addr, limit, aout, n, totalmem = 0;
454 struct process *procp; /* Process under construction. */
455 long a_text, a_data, a_bss, a_stack;
456 int banner= 0;
457 long processor= a2l(b_value("processor"));
458 u16_t kmagic, mode;
459 char *console;
460 char params[SECTOR_SIZE];
461 extern char *sbrk(int);
462 char *verb;
464 /* The stack is pretty deep here, so check if heap and stack collide. */
465 (void) sbrk(0);
467 if ((verb= b_value(VERBOSEBOOTVARNAME)) != nil)
468 verboseboot = a2l(verb);
470 printf("\nLoading ");
471 pretty_image(image);
472 printf(".\n");
474 vsec= 0; /* Load this sector from image next. */
475 addr= mem[0].base; /* Into this memory block. */
476 limit= mem[0].base + mem[0].size;
477 if (limit > caddr) limit= caddr;
479 /* Allocate and clear the area where the headers will be placed. */
480 aout = (limit -= PROCESS_MAX * A_MINHDR);
482 /* Clear the area where the headers will be placed. */
483 raw_clear(aout, PROCESS_MAX * A_MINHDR);
485 /* Read the many different processes: */
486 for (i= 0; vsec < image_size; i++) {
487 u32_t startaddr;
488 startaddr = addr;
489 if (i == PROCESS_MAX) {
490 printf("There are more then %d programs in %s\n",
491 PROCESS_MAX, image);
492 errno= 0;
493 return;
495 procp= &process[i];
497 /* Read header. */
498 DEBUGEXTRA(("Reading header... "));
499 for (;;) {
500 if ((buf= get_sector(vsec++)) == nil) return;
502 memcpy(&hdr, buf, sizeof(hdr));
504 if (BADMAG(hdr.process)) { errno= ENOEXEC; return; }
506 /* Check the optional label on the process. */
507 if (selected(hdr.name)) break;
509 /* Bad label, skip this process. */
510 vsec+= proc_size(&hdr);
512 DEBUGEXTRA(("done\n"));
514 /* Sanity check: an 8086 can't run a 386 kernel. */
515 if (hdr.process.a_cpu == A_I80386 && processor < 386) {
516 printf("You can't run a 386 kernel on this 80%ld\n",
517 processor);
518 errno= 0;
519 return;
522 /* Get the click shift from the kernel text segment. */
523 if (i == KERNEL_IDX) {
524 if (!get_clickshift(vsec, &hdr)) return;
525 addr= align(addr, click_size);
527 /* big kernels must be loaded into extended memory */
528 if (k_flags & K_KHIGH) {
529 addr= mem[1].base;
530 limit= mem[1].base + mem[1].size;
534 /* Save a copy of the header for the kernel, with a_syms
535 * misused as the address where the process is loaded at.
537 DEBUGEXTRA(("raw_copy(0x%x, 0x%lx, 0x%x)... ",
538 aout + i * A_MINHDR, mon2abs(&hdr.process), A_MINHDR));
539 hdr.process.a_syms= addr;
540 raw_copy(aout + i * A_MINHDR, mon2abs(&hdr.process), A_MINHDR);
541 DEBUGEXTRA(("done\n"));
543 if (!banner) {
544 DEBUGBASIC((" cs ds text data bss"));
545 if (k_flags & K_CHMEM) DEBUGBASIC((" stack"));
546 DEBUGBASIC(("\n"));
547 banner= 1;
550 /* Segment sizes. */
551 DEBUGEXTRA(("a_text=0x%lx; a_data=0x%lx; a_bss=0x%lx; a_flags=0x%x)\n",
552 hdr.process.a_text, hdr.process.a_data,
553 hdr.process.a_bss, hdr.process.a_flags));
555 a_text= hdr.process.a_text;
556 a_data= hdr.process.a_data;
557 a_bss= hdr.process.a_bss;
558 if (k_flags & K_CHMEM) {
559 a_stack= hdr.process.a_total - a_data - a_bss;
560 if (!(hdr.process.a_flags & A_SEP)) a_stack-= a_text;
561 } else {
562 a_stack= 0;
565 /* Collect info about the process to be. */
566 procp->cs= addr;
568 /* Process may be page aligned so that the text segment contains
569 * the header, or have an unmapped zero page against vaxisms.
571 procp->entry= hdr.process.a_entry;
572 if (hdr.process.a_flags & A_PAL) a_text+= hdr.process.a_hdrlen;
573 if (hdr.process.a_flags & A_UZP) procp->cs-= click_size;
575 /* Separate I&D: two segments. Common I&D: only one. */
576 if (hdr.process.a_flags & A_SEP) {
577 /* Read the text segment. */
578 DEBUGEXTRA(("get_segment(0x%lx, 0x%lx, 0x%lx, 0x%lx)\n",
579 vsec, a_text, addr, limit));
580 if (!get_segment(&vsec, &a_text, &addr, limit)) return;
581 DEBUGEXTRA(("get_segment done vsec=0x%lx a_text=0x%lx "
582 "addr=0x%lx\n",
583 vsec, a_text, addr));
585 /* The data segment follows. */
586 procp->ds= addr;
587 if (hdr.process.a_flags & A_UZP) procp->ds-= click_size;
588 procp->data= addr;
589 } else {
590 /* Add text to data to form one segment. */
591 procp->data= addr + a_text;
592 procp->ds= procp->cs;
593 a_data+= a_text;
596 /* Read the data segment. */
597 DEBUGEXTRA(("get_segment(0x%lx, 0x%lx, 0x%lx, 0x%lx)\n",
598 vsec, a_data, addr, limit));
599 if (!get_segment(&vsec, &a_data, &addr, limit)) return;
600 DEBUGEXTRA(("get_segment done vsec=0x%lx a_data=0x%lx "
601 "addr=0x%lx\n",
602 vsec, a_data, addr));
604 /* Make space for bss and stack unless... */
605 if (i != KERNEL_IDX && (k_flags & K_CLAIM)) a_bss= a_stack= 0;
607 DEBUGBASIC(("%07lx %07lx %8ld %8ld %8ld",
608 procp->cs, procp->ds, hdr.process.a_text,
609 hdr.process.a_data, hdr.process.a_bss));
610 if (k_flags & K_CHMEM) DEBUGBASIC((" %8ld", a_stack));
612 /* Note that a_data may be negative now, but we can look at it
613 * as -a_data bss bytes.
616 /* Compute the number of bss clicks left. */
617 a_bss+= a_data;
618 n= align(a_bss, click_size);
619 a_bss-= n;
621 /* Zero out bss. */
622 DEBUGEXTRA(("\nraw_clear(0x%lx, 0x%lx); limit=0x%lx... ", addr, n, limit));
623 if (addr + n > limit) { errno= ENOMEM; return; }
624 raw_clear(addr, n);
625 DEBUGEXTRA(("done\n"));
626 addr+= n;
628 /* And the number of stack clicks. */
629 a_stack+= a_bss;
630 n= align(a_stack, click_size);
631 a_stack-= n;
633 /* Add space for the stack. */
634 addr+= n;
636 /* Process endpoint. */
637 procp->end= addr;
639 if (verboseboot >= VERBOSEBOOT_BASIC)
640 printf(" %s\n", hdr.name);
641 else {
642 u32_t mem;
643 mem = addr-startaddr;
644 printf("%s ", hdr.name);
645 totalmem += mem;
648 if (i == 0 && (k_flags & (K_HIGH | K_KHIGH)) == K_HIGH) {
649 /* Load the rest in extended memory. */
650 addr= mem[1].base;
651 limit= mem[1].base + mem[1].size;
655 if (verboseboot < VERBOSEBOOT_BASIC)
656 printf("(%dk)\n", totalmem/1024);
658 if ((n_procs= i) == 0) {
659 printf("There are no programs in %s\n", image);
660 errno= 0;
661 return;
664 /* Check the kernel magic number. */
665 raw_copy(mon2abs(&kmagic),
666 process[KERNEL_IDX].data + MAGIC_OFF, sizeof(kmagic));
667 if (kmagic != KERNEL_D_MAGIC) {
668 printf("Kernel magic number is incorrect (0x%x@0x%lx)\n",
669 kmagic, process[KERNEL_IDX].data + MAGIC_OFF);
670 errno= 0;
671 return;
674 /* Patch sizes, etc. into kernel data. */
675 DEBUGEXTRA(("patch_sizes()... "));
676 patch_sizes();
677 DEBUGEXTRA(("done\n"));
679 #if !DOS
680 if (!(k_flags & K_MEML)) {
681 /* Copy the a.out headers to the old place. */
682 raw_copy(HEADERPOS, aout, PROCESS_MAX * A_MINHDR);
684 #endif
686 /* Run the trailer function just before starting Minix. */
687 DEBUGEXTRA(("run_trailer()... "));
688 if (!run_trailer()) { errno= 0; return; }
689 DEBUGEXTRA(("done\n"));
691 /* Translate the boot parameters to what Minix likes best. */
692 DEBUGEXTRA(("params2params(0x%x, 0x%x)... ", params, sizeof(params)));
693 if (!params2params(params, sizeof(params))) { errno= 0; return; }
694 DEBUGEXTRA(("done\n"));
696 /* Set the video to the required mode. */
697 if ((console= b_value("console")) == nil || (mode= a2x(console)) == 0) {
698 mode= strcmp(b_value("chrome"), "color") == 0 ? COLOR_MODE :
699 MONO_MODE;
701 DEBUGEXTRA(("set_mode(%d)... ", mode));
702 set_mode(mode);
703 DEBUGEXTRA(("done\n"));
705 /* Close the disk. */
706 DEBUGEXTRA(("dev_close()... "));
707 (void) dev_close();
708 DEBUGEXTRA(("done\n"));
710 /* Minix. */
711 DEBUGEXTRA(("minix(0x%lx, 0x%lx, 0x%lx, 0x%x, 0x%x, 0x%lx)\n",
712 process[KERNEL_IDX].entry, process[KERNEL_IDX].cs,
713 process[KERNEL_IDX].ds, params, sizeof(params), aout));
714 minix(process[KERNEL_IDX].entry, process[KERNEL_IDX].cs,
715 process[KERNEL_IDX].ds, params, sizeof(params), aout);
717 if (!(k_flags & K_BRET)) {
718 extern u32_t reboot_code;
719 raw_copy(mon2abs(params), reboot_code, sizeof(params));
721 parse_code(params);
723 /* Return from Minix. Things may have changed, so assume nothing. */
724 fsok= -1;
725 errno= 0;
727 /* Read leftover character, if any. */
728 scan_keyboard();
730 /* Restore screen contents. */
731 restore_screen();
736 ino_t latest_version(char *version, struct stat *stp)
737 /* Recursively read the current directory, selecting the newest image on
738 * the way up. (One can't use r_stat while reading a directory.)
741 char name[NAME_MAX + 1];
742 ino_t ino, newest;
743 time_t mtime;
745 if ((ino= r_readdir(name)) == 0) { stp->st_mtime= 0; return 0; }
747 newest= latest_version(version, stp);
748 mtime= stp->st_mtime;
749 r_stat(ino, stp);
751 if (S_ISREG(stp->st_mode) && stp->st_mtime > mtime) {
752 newest= ino;
753 strcpy(version, name);
754 } else {
755 stp->st_mtime= mtime;
757 return newest;
760 char *select_image(char *image)
761 /* Look image up on the filesystem, if it is a file then we're done, but
762 * if its a directory then we want the newest file in that directory. If
763 * it doesn't exist at all, then see if it is 'number:number' and get the
764 * image from that absolute offset off the disk.
767 ino_t image_ino;
768 struct stat st;
770 image= strcpy(malloc((strlen(image) + 1 + NAME_MAX + 1)
771 * sizeof(char)), image);
773 fsok= r_super(&block_size) != 0;
774 if (!fsok || (image_ino= r_lookup(ROOT_INO, image)) == 0) {
775 char *size;
777 if (numprefix(image, &size) && *size++ == ':'
778 && numeric(size)) {
779 vir2sec= flat_vir2sec;
780 image_off= a2l(image);
781 image_size= a2l(size);
782 strcpy(image, "Minix");
783 return image;
785 if (!fsok)
786 printf("No image selected\n");
787 else
788 printf("Can't load %s: %s\n", image, unix_err(errno));
789 goto bail_out;
792 r_stat(image_ino, &st);
793 if (!S_ISREG(st.st_mode)) {
794 char *version= image + strlen(image);
795 char dots[NAME_MAX + 1];
797 if (!S_ISDIR(st.st_mode)) {
798 printf("%s: %s\n", image, unix_err(ENOTDIR));
799 goto bail_out;
801 (void) r_readdir(dots);
802 (void) r_readdir(dots); /* "." & ".." */
803 *version++= '/';
804 *version= 0;
805 if ((image_ino= latest_version(version, &st)) == 0) {
806 printf("There are no images in %s\n", image);
807 goto bail_out;
809 r_stat(image_ino, &st);
811 vir2sec= file_vir2sec;
812 image_size= (st.st_size + SECTOR_SIZE - 1) >> SECTOR_SHIFT;
813 return image;
814 bail_out:
815 free(image);
816 return nil;
819 void bootminix(void)
820 /* Load Minix and run it. (Given the size of this program it is surprising
821 * that it ever gets to that.)
824 char *image;
826 if ((image= select_image(b_value("image"))) == nil) return;
828 if(serial_line >= 0) {
829 char linename[2];
830 linename[0] = serial_line + '0';
831 linename[1] = '\0';
832 b_setvar(E_VAR, SERVARNAME, linename);
835 exec_image(image);
837 switch (errno) {
838 case ENOEXEC:
839 printf("%s contains a bad program header\n", image);
840 break;
841 case ENOMEM:
842 printf("Not enough memory to load %s\n", image);
843 break;
844 case EIO:
845 printf("Unsuspected EOF on %s\n", image);
846 case 0:
847 /* No error or error already reported. */;
849 free(image);
851 if(serial_line >= 0)
852 b_unset(SERVARNAME);
856 * $PchId: bootimage.c,v 1.10 2002/02/27 19:39:09 philip Exp $