1 /* bootimage.c - Load an image and start it. Author: Kees J. Bot
4 #define BIOS 1 /* Can only be used under the BIOS. */
6 #define _POSIX_SOURCE 1
17 #include <minix/config.h>
18 #include <minix/const.h>
19 #include <minix/type.h>
20 #include <minix/syslib.h>
21 #include <kernel/const.h>
22 #include <kernel/type.h>
23 #include <ibm/partition.h>
28 static int block_size
= 0;
30 #define click_shift clck_shft /* 7 char clash with click_size. */
32 /* Some kernels have extra features: */
33 #define K_I386 0x0001 /* Make the 386 transition before you call me. */
34 #define K_CLAIM 0x0002 /* I will acquire my own bss pages, thank you. */
35 #define K_CHMEM 0x0004 /* This kernel listens to chmem for its stack size. */
36 #define K_HIGH 0x0008 /* Load mm, fs, etc. in extended memory. */
37 #define K_HDR 0x0010 /* No need to patch sizes, kernel uses the headers. */
38 #define K_RET 0x0020 /* Returns to the monitor on reboot. */
39 #define K_INT86 0x0040 /* Requires generic INT support. */
40 #define K_MEML 0x0080 /* Pass a list of free memory. */
41 #define K_BRET 0x0100 /* New monitor code on shutdown in boot parameters. */
42 #define K_ALL 0x01FF /* All feature bits this monitor supports. */
45 /* Data about the different processes. */
47 #define PROCESS_MAX 16 /* Must match the space in kernel/mpx.x */
48 #define KERNEL_IDX 0 /* The first process is the kernel. */
49 #define FS 2 /* The third must be fs. */
51 struct process
{ /* Per-process memory adresses. */
52 u32_t entry
; /* Entry point. */
53 u32_t cs
; /* Code segment. */
54 u32_t ds
; /* Data segment. */
55 u32_t data
; /* To access the data segment. */
56 u32_t end
; /* End of this process, size = (end - cs). */
57 } process
[PROCESS_MAX
];
58 int n_procs
; /* Number of processes. */
60 /* Magic numbers in process' data space. */
61 #define MAGIC_OFF 0 /* Offset of magic # in data seg. */
62 #define CLICK_OFF 2 /* Offset in kernel text to click_shift. */
63 #define FLAGS_OFF 4 /* Offset in kernel text to flags. */
64 #define KERNEL_D_MAGIC 0x526F /* Kernel magic number. */
66 /* Offsets of sizes to be patched into kernel and fs. */
67 #define P_SIZ_OFF 0 /* Process' sizes into kernel data. */
68 #define P_INIT_OFF 4 /* Init cs & sizes into fs data. */
71 #define between(a, c, z) ((unsigned) ((c) - (a)) <= ((z) - (a)))
73 void pretty_image(char *image
)
74 /* Pretty print the name of the image to load. Translate '/' and '_' to
75 * space, first letter goes uppercase. An 'r' before a digit prints as
76 * 'revision'. E.g. 'minix/1.6.16r10' -> 'Minix 1.6.16 revision 10'.
77 * The idea is that the part before the 'r' is the official Minix release
78 * and after the 'r' you can put version numbers for your own changes.
83 while ((c
= *image
++) != 0) {
84 if (c
== '/' || c
== '_') c
= ' ';
86 if (c
== 'r' && between('0', *image
, '9')) {
90 if (!up
&& between('a', c
, 'z')) c
= c
- 'a' + 'A';
92 if (between('A', c
, 'Z')) up
= 1;
98 void raw_clear(u32_t addr
, u32_t count
)
99 /* Clear "count" bytes at absolute address "addr". */
101 static char zeros
[128];
106 if (zct
> count
) zct
= count
;
107 raw_copy(addr
, mon2abs(&zeros
), zct
);
112 if (zct
> count
) zct
= count
;
113 raw_copy(dst
, addr
, zct
);
119 /* Align a to a multiple of n (a power of 2): */
120 #define align(a, n) (((u32_t)(a) + ((u32_t)(n) - 1)) & ~((u32_t)(n) - 1))
121 unsigned click_shift
;
122 unsigned click_size
; /* click_size = Smallest kernel memory object. */
123 unsigned k_flags
; /* Not all kernels are created equal. */
124 u32_t reboot_code
; /* Obsolete reboot code return pointer. */
126 int params2params(char *params
, size_t psize
)
127 /* Repackage the environment settings for the kernel. */
135 for (e
= env
; e
!= nil
; e
= e
->next
) {
139 if (!(e
->flags
& E_VAR
)) continue;
141 if (e
->flags
& E_DEV
) {
142 if ((dev
= name2dev(value
)) == -1) return 0;
143 value
= ul2a10((u16_t
) dev
);
146 n
= i
+ strlen(name
) + 1 + strlen(value
) + 1;
148 strcpy(params
+ i
, name
);
149 strcat(params
+ i
, "=");
150 strcat(params
+ i
, value
);
155 if (!(k_flags
& K_MEML
)) {
156 /* Require old memory size variables. */
158 value
= ul2a10((mem
[0].base
+ mem
[0].size
) / 1024);
159 n
= i
+ 7 + 1 + strlen(value
) + 1;
161 strcpy(params
+ i
, "memsize=");
162 strcat(params
+ i
, value
);
165 value
= ul2a10(mem
[1].size
/ 1024);
166 n
= i
+ 7 + 1 + strlen(value
) + 1;
168 strcpy(params
+ i
, "emssize=");
169 strcat(params
+ i
, value
);
175 printf("Too many boot parameters\n");
178 params
[i
]= 0; /* End marked with empty string. */
182 void patch_sizes(void)
183 /* Patch sizes of each process into kernel data space, kernel ds into kernel
184 * text space, and sizes of init into data space of fs. All the patched
185 * numbers are based on the kernel click size, not hardware segments.
188 u16_t text_size
, data_size
;
190 struct process
*procp
, *initp
;
193 if (k_flags
& K_HDR
) return; /* Uses the headers. */
195 /* Patch text and data sizes of the processes into kernel data space.
197 doff
= process
[KERNEL_IDX
].data
+ P_SIZ_OFF
;
199 for (i
= 0; i
< n_procs
; i
++) {
201 text_size
= (procp
->ds
- procp
->cs
) >> click_shift
;
202 data_size
= (procp
->end
- procp
->ds
) >> click_shift
;
204 /* Two words per process, the text and data size: */
205 put_word(doff
, text_size
); doff
+= 2;
206 put_word(doff
, data_size
); doff
+= 2;
208 initp
= procp
; /* The last process must be init. */
211 if (k_flags
& (K_HIGH
|K_MEML
)) return; /* Doesn't need FS patching. */
213 /* Patch cs and sizes of init into fs data. */
214 put_word(process
[FS
].data
+ P_INIT_OFF
+0, initp
->cs
>> click_shift
);
215 put_word(process
[FS
].data
+ P_INIT_OFF
+2, text_size
);
216 put_word(process
[FS
].data
+ P_INIT_OFF
+4, data_size
);
219 int selected(char *name
)
220 /* True iff name has no label or the proper label. */
225 if ((colon
= strchr(name
, ':')) == nil
) return 1;
226 if ((label
= b_value("label")) == nil
) return 1;
229 cmp
= strcmp(label
, name
);
234 u32_t
proc_size(struct image_header
*hdr
)
235 /* Return the size of a process in sectors as found in an image. */
237 u32_t len
= hdr
->process
.a_text
;
239 if (hdr
->process
.a_flags
& A_PAL
) len
+= hdr
->process
.a_hdrlen
;
240 if (hdr
->process
.a_flags
& A_SEP
) len
= align(len
, SECTOR_SIZE
);
241 len
= align(len
+ hdr
->process
.a_data
, SECTOR_SIZE
);
243 return len
>> SECTOR_SHIFT
;
246 off_t image_off
, image_size
;
247 u32_t (*vir2sec
)(u32_t vsec
); /* Where is a sector on disk? */
249 u32_t
file_vir2sec(u32_t vsec
)
250 /* Translate a virtual sector number to an absolute disk sector. */
254 if(!block_size
) { errno
= 0; return -1; }
256 if ((blk
= r_vir2abs(vsec
/ RATIO(block_size
))) == -1) {
260 return blk
== 0 ? 0 : lowsec
+ blk
* RATIO(block_size
) + vsec
% RATIO(block_size
);
263 u32_t
flat_vir2sec(u32_t vsec
)
264 /* Simply add an absolute sector offset to vsec. */
266 return lowsec
+ image_off
+ vsec
;
269 char *get_sector(u32_t vsec
)
270 /* Read a sector "vsec" from the image into memory and return its address.
271 * Return nil on error. (This routine tries to read an entire track, so
272 * the next request is usually satisfied from the track buffer.)
278 static char buf
[SECBUFS
* SECTOR_SIZE
];
279 static size_t count
; /* Number of sectors in the buffer. */
280 static u32_t bufsec
; /* First Sector now in the buffer. */
282 if (vsec
== 0) count
= 0; /* First sector; initialize. */
284 if ((sec
= (*vir2sec
)(vsec
)) == -1) return nil
;
289 memset(buf
, 0, SECTOR_SIZE
);
293 /* Can we return a sector from the buffer? */
294 if ((sec
- bufsec
) < count
) {
295 return buf
+ ((size_t) (sec
- bufsec
) << SECTOR_SHIFT
);
298 /* Not in the buffer. */
302 /* Read a whole track if possible. */
303 while (++count
< SECBUFS
&& !dev_boundary(bufsec
+ count
)) {
305 if ((sec
= (*vir2sec
)(vsec
)) == -1) break;
308 if (sec
!= bufsec
+ count
) break;
311 /* Actually read the sectors. */
312 if ((r
= readsectors(mon2abs(buf
), bufsec
, count
)) != 0) {
321 int get_clickshift(u32_t ksec
, struct image_header
*hdr
)
322 /* Get the click shift and special flags from kernel text. */
326 if ((textp
= get_sector(ksec
)) == nil
) return 0;
328 if (hdr
->process
.a_flags
& A_PAL
) textp
+= hdr
->process
.a_hdrlen
;
329 click_shift
= * (u16_t
*) (textp
+ CLICK_OFF
);
330 k_flags
= * (u16_t
*) (textp
+ FLAGS_OFF
);
332 if ((k_flags
& ~K_ALL
) != 0) {
333 printf("%s requires features this monitor doesn't offer\n",
338 if (click_shift
< HCLICK_SHIFT
|| click_shift
> 16) {
339 printf("%s click size is bad\n", hdr
->name
);
344 click_size
= 1 << click_shift
;
349 int get_segment(u32_t
*vsec
, long *size
, u32_t
*addr
, u32_t limit
)
350 /* Read *size bytes starting at virtual sector *vsec to memory at *addr. */
358 if ((buf
= get_sector((*vsec
)++)) == nil
) return 0;
361 if (*addr
+ click_size
> limit
) { errno
= ENOMEM
; return 0; }
364 raw_copy(*addr
, mon2abs(buf
), n
);
371 /* Zero extend to a click. */
372 n
= align(*addr
, click_size
) - *addr
;
379 void exec_image(char *image
)
380 /* Get a Minix image into core, patch it up and execute. */
384 struct image_header hdr
;
386 u32_t vsec
, addr
, limit
, aout
, n
;
387 struct process
*procp
; /* Process under construction. */
388 long a_text
, a_data
, a_bss
, a_stack
;
390 long processor
= a2l(b_value("processor"));
393 char params
[SECTOR_SIZE
];
394 extern char *sbrk(int);
396 /* The stack is pretty deep here, so check if heap and stack collide. */
399 printf("\nLoading ");
403 vsec
= 0; /* Load this sector from image next. */
404 addr
= mem
[0].base
; /* Into this memory block. */
405 limit
= mem
[0].base
+ mem
[0].size
;
406 if (limit
> caddr
) limit
= caddr
;
408 /* Allocate and clear the area where the headers will be placed. */
409 aout
= (limit
-= PROCESS_MAX
* A_MINHDR
);
411 /* Clear the area where the headers will be placed. */
412 raw_clear(aout
, PROCESS_MAX
* A_MINHDR
);
414 /* Read the many different processes: */
415 for (i
= 0; vsec
< image_size
; i
++) {
416 if (i
== PROCESS_MAX
) {
417 printf("There are more then %d programs in %s\n",
426 if ((buf
= get_sector(vsec
++)) == nil
) return;
428 memcpy(&hdr
, buf
, sizeof(hdr
));
430 if (BADMAG(hdr
.process
)) { errno
= ENOEXEC
; return; }
432 /* Check the optional label on the process. */
433 if (selected(hdr
.name
)) break;
435 /* Bad label, skip this process. */
436 vsec
+= proc_size(&hdr
);
439 /* Sanity check: an 8086 can't run a 386 kernel. */
440 if (hdr
.process
.a_cpu
== A_I80386
&& processor
< 386) {
441 printf("You can't run a 386 kernel on this 80%ld\n",
447 /* Get the click shift from the kernel text segment. */
448 if (i
== KERNEL_IDX
) {
449 if (!get_clickshift(vsec
, &hdr
)) return;
450 addr
= align(addr
, click_size
);
453 /* Save a copy of the header for the kernel, with a_syms
454 * misused as the address where the process is loaded at.
456 hdr
.process
.a_syms
= addr
;
457 raw_copy(aout
+ i
* A_MINHDR
, mon2abs(&hdr
.process
), A_MINHDR
);
460 printf(" cs ds text data bss");
461 if (k_flags
& K_CHMEM
) printf(" stack");
467 a_text
= hdr
.process
.a_text
;
468 a_data
= hdr
.process
.a_data
;
469 a_bss
= hdr
.process
.a_bss
;
470 if (k_flags
& K_CHMEM
) {
471 a_stack
= hdr
.process
.a_total
- a_data
- a_bss
;
472 if (!(hdr
.process
.a_flags
& A_SEP
)) a_stack
-= a_text
;
477 /* Collect info about the process to be. */
480 /* Process may be page aligned so that the text segment contains
481 * the header, or have an unmapped zero page against vaxisms.
483 procp
->entry
= hdr
.process
.a_entry
;
484 if (hdr
.process
.a_flags
& A_PAL
) a_text
+= hdr
.process
.a_hdrlen
;
485 if (hdr
.process
.a_flags
& A_UZP
) procp
->cs
-= click_size
;
487 /* Separate I&D: two segments. Common I&D: only one. */
488 if (hdr
.process
.a_flags
& A_SEP
) {
489 /* Read the text segment. */
490 if (!get_segment(&vsec
, &a_text
, &addr
, limit
)) return;
492 /* The data segment follows. */
494 if (hdr
.process
.a_flags
& A_UZP
) procp
->ds
-= click_size
;
497 /* Add text to data to form one segment. */
498 procp
->data
= addr
+ a_text
;
499 procp
->ds
= procp
->cs
;
503 /* Read the data segment. */
504 if (!get_segment(&vsec
, &a_data
, &addr
, limit
)) return;
506 /* Make space for bss and stack unless... */
507 if (i
!= KERNEL_IDX
&& (k_flags
& K_CLAIM
)) a_bss
= a_stack
= 0;
509 printf("%07lx %07lx %8ld %8ld %8ld",
510 procp
->cs
, procp
->ds
,
511 hdr
.process
.a_text
, hdr
.process
.a_data
,
514 if (k_flags
& K_CHMEM
) printf(" %8ld", a_stack
);
516 printf(" %s\n", hdr
.name
);
518 /* Note that a_data may be negative now, but we can look at it
519 * as -a_data bss bytes.
522 /* Compute the number of bss clicks left. */
524 n
= align(a_bss
, click_size
);
528 if (addr
+ n
> limit
) { errno
= ENOMEM
; return; }
532 /* And the number of stack clicks. */
534 n
= align(a_stack
, click_size
);
537 /* Add space for the stack. */
540 /* Process endpoint. */
543 if (i
== 0 && (k_flags
& K_HIGH
)) {
544 /* Load the rest in extended memory. */
546 limit
= mem
[1].base
+ mem
[1].size
;
550 if ((n_procs
= i
) == 0) {
551 printf("There are no programs in %s\n", image
);
556 /* Check the kernel magic number. */
557 if (get_word(process
[KERNEL_IDX
].data
+ MAGIC_OFF
) != KERNEL_D_MAGIC
) {
558 printf("Kernel magic number is incorrect\n");
563 /* Patch sizes, etc. into kernel data. */
567 if (!(k_flags
& K_MEML
)) {
568 /* Copy the a.out headers to the old place. */
569 raw_copy(HEADERPOS
, aout
, PROCESS_MAX
* A_MINHDR
);
573 /* Do delay if wanted. */
574 if((delayvalue
= b_value("bootdelay")) != nil
> 0) {
578 /* Run the trailer function just before starting Minix. */
579 if (!run_trailer()) { errno
= 0; return; }
581 /* Translate the boot parameters to what Minix likes best. */
582 if (!params2params(params
, sizeof(params
))) { errno
= 0; return; }
584 /* Set the video to the required mode. */
585 if ((console
= b_value("console")) == nil
|| (mode
= a2x(console
)) == 0) {
586 mode
= strcmp(b_value("chrome"), "color") == 0 ? COLOR_MODE
:
591 /* Close the disk. */
595 minix(process
[KERNEL_IDX
].entry
, process
[KERNEL_IDX
].cs
,
596 process
[KERNEL_IDX
].ds
, params
, sizeof(params
), aout
);
598 if (!(k_flags
& K_BRET
)) {
599 extern u32_t reboot_code
;
600 raw_copy(mon2abs(params
), reboot_code
, sizeof(params
));
604 /* Return from Minix. Things may have changed, so assume nothing. */
608 /* Read leftover character, if any. */
612 ino_t
latest_version(char *version
, struct stat
*stp
)
613 /* Recursively read the current directory, selecting the newest image on
614 * the way up. (One can't use r_stat while reading a directory.)
617 char name
[NAME_MAX
+ 1];
621 if ((ino
= r_readdir(name
)) == 0) { stp
->st_mtime
= 0; return 0; }
623 newest
= latest_version(version
, stp
);
624 mtime
= stp
->st_mtime
;
627 if (S_ISREG(stp
->st_mode
) && stp
->st_mtime
> mtime
) {
629 strcpy(version
, name
);
631 stp
->st_mtime
= mtime
;
636 char *select_image(char *image
)
637 /* Look image up on the filesystem, if it is a file then we're done, but
638 * if its a directory then we want the newest file in that directory. If
639 * it doesn't exist at all, then see if it is 'number:number' and get the
640 * image from that absolute offset off the disk.
646 image
= strcpy(malloc((strlen(image
) + 1 + NAME_MAX
+ 1)
647 * sizeof(char)), image
);
649 fsok
= r_super(&block_size
) != 0;
650 if (!fsok
|| (image_ino
= r_lookup(ROOT_INO
, image
)) == 0) {
653 if (numprefix(image
, &size
) && *size
++ == ':'
655 vir2sec
= flat_vir2sec
;
656 image_off
= a2l(image
);
657 image_size
= a2l(size
);
658 strcpy(image
, "Minix");
662 printf("No image selected\n");
664 printf("Can't load %s: %s\n", image
, unix_err(errno
));
668 r_stat(image_ino
, &st
);
669 if (!S_ISREG(st
.st_mode
)) {
670 char *version
= image
+ strlen(image
);
671 char dots
[NAME_MAX
+ 1];
673 if (!S_ISDIR(st
.st_mode
)) {
674 printf("%s: %s\n", image
, unix_err(ENOTDIR
));
677 (void) r_readdir(dots
);
678 (void) r_readdir(dots
); /* "." & ".." */
681 if ((image_ino
= latest_version(version
, &st
)) == 0) {
682 printf("There are no images in %s\n", image
);
685 r_stat(image_ino
, &st
);
687 vir2sec
= file_vir2sec
;
688 image_size
= (st
.st_size
+ SECTOR_SIZE
- 1) >> SECTOR_SHIFT
;
696 /* Load Minix and run it. (Given the size of this program it is surprising
697 * that it ever gets to that.)
702 if ((image
= select_image(b_value("image"))) == nil
) return;
708 printf("%s contains a bad program header\n", image
);
711 printf("Not enough memory to load %s\n", image
);
714 printf("Unsuspected EOF on %s\n", image
);
716 /* No error or error already reported. */;
722 * $PchId: bootimage.c,v 1.10 2002/02/27 19:39:09 philip Exp $