Slightly more flexible packman.
[minix3.git] / boot / boot.c
blobeae2ae75c85b18dffff8ab21d3caafc33b49a07c
1 /* boot.c - Load and start Minix. Author: Kees J. Bot
2 * 27 Dec 1991
3 */
5 char version[]= "2.20";
7 #define BIOS (!UNIX) /* Either uses BIOS or UNIX syscalls. */
9 #define nil 0
10 #define _POSIX_SOURCE 1
11 #define _MINIX 1
12 #include <stddef.h>
13 #include <sys/types.h>
14 #include <sys/stat.h>
15 #include <stdlib.h>
16 #include <stdio.h>
17 #include <limits.h>
18 #include <string.h>
19 #include <errno.h>
20 #include <ibm/partition.h>
21 #include <minix/config.h>
22 #include <minix/type.h>
23 #include <minix/com.h>
24 #include <minix/dmap.h>
25 #include <minix/const.h>
26 #include <minix/minlib.h>
27 #include <minix/syslib.h>
28 #if BIOS
29 #include <kernel/const.h>
30 #include <kernel/type.h>
31 #endif
32 #if UNIX
33 #include <stdio.h>
34 #include <time.h>
35 #include <unistd.h>
36 #include <fcntl.h>
37 #include <signal.h>
38 #include <termios.h>
39 #endif
40 #include "rawfs.h"
41 #undef EXTERN
42 #define EXTERN /* Empty */
43 #include "boot.h"
45 #define arraysize(a) (sizeof(a) / sizeof((a)[0]))
46 #define arraylimit(a) ((a) + arraysize(a))
47 #define between(a, c, z) ((unsigned) ((c) - (a)) <= ((z) - (a)))
49 int fsok= -1; /* File system state. Initially unknown. */
51 static int block_size;
53 #if BIOS
55 /* this data is reserved for BIOS int 0x13 to put the 'specification packet'
56 * in. It has a structure of course, but we don't define a struct because
57 * of compiler padding. We fiddle out the bytes ourselves later.
59 unsigned char boot_spec[24];
61 char *bios_err(int err)
62 /* Translate BIOS error code to a readable string. (This is a rare trait
63 * known as error checking and reporting. Take a good look at it, you won't
64 * see it often.)
67 static struct errlist {
68 int err;
69 char *what;
70 } errlist[] = {
71 #if !DOS
72 { 0x00, "No error" },
73 { 0x01, "Invalid command" },
74 { 0x02, "Address mark not found" },
75 { 0x03, "Disk write-protected" },
76 { 0x04, "Sector not found" },
77 { 0x05, "Reset failed" },
78 { 0x06, "Floppy disk removed" },
79 { 0x07, "Bad parameter table" },
80 { 0x08, "DMA overrun" },
81 { 0x09, "DMA crossed 64 KB boundary" },
82 { 0x0A, "Bad sector flag" },
83 { 0x0B, "Bad track flag" },
84 { 0x0C, "Media type not found" },
85 { 0x0D, "Invalid number of sectors on format" },
86 { 0x0E, "Control data address mark detected" },
87 { 0x0F, "DMA arbitration level out of range" },
88 { 0x10, "Uncorrectable CRC or ECC data error" },
89 { 0x11, "ECC corrected data error" },
90 { 0x20, "Controller failed" },
91 { 0x40, "Seek failed" },
92 { 0x80, "Disk timed-out" },
93 { 0xAA, "Drive not ready" },
94 { 0xBB, "Undefined error" },
95 { 0xCC, "Write fault" },
96 { 0xE0, "Status register error" },
97 { 0xFF, "Sense operation failed" }
98 #else /* DOS */
99 { 0x00, "No error" },
100 { 0x01, "Function number invalid" },
101 { 0x02, "File not found" },
102 { 0x03, "Path not found" },
103 { 0x04, "Too many open files" },
104 { 0x05, "Access denied" },
105 { 0x06, "Invalid handle" },
106 { 0x0C, "Access code invalid" },
107 #endif /* DOS */
109 struct errlist *errp;
111 for (errp= errlist; errp < arraylimit(errlist); errp++) {
112 if (errp->err == err) return errp->what;
114 return "Unknown error";
117 char *unix_err(int err)
118 /* Translate the few errors rawfs can give. */
120 switch (err) {
121 case ENOENT: return "No such file or directory";
122 case ENOTDIR: return "Not a directory";
123 default: return "Unknown error";
127 void rwerr(char *rw, off_t sec, int err)
129 printf("\n%s error 0x%02x (%s) at sector %ld absolute\n",
130 rw, err, bios_err(err), sec);
133 void readerr(off_t sec, int err) { rwerr("Read", sec, err); }
134 void writerr(off_t sec, int err) { rwerr("Write", sec, err); }
136 void readblock(off_t blk, char *buf, int block_size)
137 /* Read blocks for the rawfs package. */
139 int r;
140 u32_t sec= lowsec + blk * RATIO(block_size);
142 if(!block_size) {
143 printf("block_size 0\n");
144 exit(1);
147 if ((r= readsectors(mon2abs(buf), sec, 1 * RATIO(block_size))) != 0) {
148 readerr(sec, r); exit(1);
152 #define istty (1)
153 #define alarm(n) (0)
155 #endif /* BIOS */
157 #if UNIX
159 /* The Minix boot block must start with these bytes: */
160 char boot_magic[] = { 0x31, 0xC0, 0x8E, 0xD8, 0xFA, 0x8E, 0xD0, 0xBC };
162 struct biosdev {
163 char *name; /* Name of device. */
164 int device; /* Device to edit parameters. */
165 } bootdev;
167 struct termios termbuf;
168 int istty;
170 void quit(int status)
172 if (istty) (void) tcsetattr(0, TCSANOW, &termbuf);
173 exit(status);
176 #define exit(s) quit(s)
178 void report(char *label)
179 /* edparams: label: No such file or directory */
181 fprintf(stderr, "edparams: %s: %s\n", label, strerror(errno));
184 void fatal(char *label)
186 report(label);
187 exit(1);
190 void *alloc(void *m, size_t n)
192 m= m == nil ? malloc(n) : realloc(m, n);
193 if (m == nil) fatal("");
194 return m;
197 #define malloc(n) alloc(nil, n)
198 #define realloc(m, n) alloc(m, n)
200 #define mon2abs(addr) ((void *) (addr))
202 int rwsectors(int rw, void *addr, u32_t sec, int nsec)
204 ssize_t r;
205 size_t len= nsec * SECTOR_SIZE;
207 if (lseek(bootdev.device, sec * SECTOR_SIZE, SEEK_SET) == -1)
208 return errno;
210 if (rw == 0) {
211 r= read(bootdev.device, (char *) addr, len);
212 } else {
213 r= write(bootdev.device, (char *) addr, len);
215 if (r == -1) return errno;
216 if (r != len) return EIO;
217 return 0;
220 #define readsectors(a, s, n) rwsectors(0, (a), (s), (n))
221 #define writesectors(a, s, n) rwsectors(1, (a), (s), (n))
222 #define readerr(sec, err) (errno= (err), report(bootdev.name))
223 #define writerr(sec, err) (errno= (err), report(bootdev.name))
224 #define putch(c) putchar(c)
225 #define unix_err(err) strerror(err)
227 void readblock(off_t blk, char *buf, int block_size)
228 /* Read blocks for the rawfs package. */
230 if(!block_size) fatal("block_size 0");
231 errno= EIO;
232 if (lseek(bootdev.device, blk * block_size, SEEK_SET) == -1
233 || read(bootdev.device, buf, block_size) != block_size)
235 fatal(bootdev.name);
239 sig_atomic_t trapsig;
241 void trap(int sig)
243 trapsig= sig;
244 signal(sig, trap);
247 int escape(void)
249 if (trapsig == SIGINT) {
250 trapsig= 0;
251 return 1;
253 return 0;
256 static unsigned char unchar;
258 int getch(void)
260 unsigned char c;
262 fflush(stdout);
264 if (unchar != 0) {
265 c= unchar;
266 unchar= 0;
267 return c;
270 switch (read(0, &c, 1)) {
271 case -1:
272 if (errno != EINTR) fatal("");
273 return(ESC);
274 case 0:
275 if (istty) putch('\n');
276 exit(0);
277 default:
278 if (istty && c == termbuf.c_cc[VEOF]) {
279 putch('\n');
280 exit(0);
282 return c;
286 #define ungetch(c) ((void) (unchar = (c)))
288 #define get_tick() ((u32_t) time(nil))
289 #define clear_screen() printf("[clear]")
290 #define boot_device(device) printf("[boot %s]\n", device)
291 #define ctty(line) printf("[ctty %s]\n", line)
292 #define bootminix() (run_trailer() && printf("[boot]\n"))
293 #define off() printf("[off]")
295 #endif /* UNIX */
297 char *readline(void)
298 /* Read a line including a newline with echoing. */
300 char *line;
301 size_t i, z;
302 int c;
304 i= 0;
305 z= 20;
306 line= malloc(z * sizeof(char));
308 do {
309 c= getch();
311 if (strchr("\b\177\25\30", c) != nil) {
312 /* Backspace, DEL, ctrl-U, or ctrl-X. */
313 do {
314 if (i == 0) break;
315 printf("\b \b");
316 i--;
317 } while (c == '\25' || c == '\30');
318 } else
319 if (c < ' ' && c != '\n') {
320 putch('\7');
321 } else {
322 putch(c);
323 line[i++]= c;
324 if (i == z) {
325 z*= 2;
326 line= realloc(line, z * sizeof(char));
329 } while (c != '\n');
330 line[i]= 0;
331 return line;
334 int sugar(char *tok)
335 /* Recognize special tokens. */
337 return strchr("=(){};\n", tok[0]) != nil;
340 char *onetoken(char **aline)
341 /* Returns a string with one token for tokenize. */
343 char *line= *aline;
344 size_t n;
345 char *tok;
347 /* Skip spaces and runs of newlines. */
348 while (*line == ' ' || (*line == '\n' && line[1] == '\n')) line++;
350 *aline= line;
352 /* Don't do odd junk (nor the terminating 0!). */
353 if ((unsigned) *line < ' ' && *line != '\n') return nil;
355 if (*line == '(') {
356 /* Function argument, anything goes but () must match. */
357 int depth= 0;
359 while ((unsigned) *line >= ' ') {
360 if (*line == '(') depth++;
361 if (*line++ == ')' && --depth == 0) break;
363 } else
364 if (sugar(line)) {
365 /* Single character token. */
366 line++;
367 } else {
368 /* Multicharacter token. */
369 do line++; while ((unsigned) *line > ' ' && !sugar(line));
371 n= line - *aline;
372 tok= malloc((n + 1) * sizeof(char));
373 memcpy(tok, *aline, n);
374 tok[n]= 0;
375 if (tok[0] == '\n') tok[0]= ';'; /* ';' same as '\n' */
377 *aline= line;
378 return tok;
381 /* Typed commands form strings of tokens. */
383 typedef struct token {
384 struct token *next; /* Next in a command chain. */
385 char *token;
386 } token;
388 token **tokenize(token **acmds, char *line)
389 /* Takes a line apart to form tokens. The tokens are inserted into a command
390 * chain at *acmds. Tokenize returns a reference to where another line could
391 * be added. Tokenize looks at spaces as token separators, and recognizes only
392 * ';', '=', '{', '}', and '\n' as single character tokens. One token is
393 * formed from '(' and ')' with anything in between as long as more () match.
396 char *tok;
397 token *newcmd;
399 while ((tok= onetoken(&line)) != nil) {
400 newcmd= malloc(sizeof(*newcmd));
401 newcmd->token= tok;
402 newcmd->next= *acmds;
403 *acmds= newcmd;
404 acmds= &newcmd->next;
406 return acmds;
409 token *cmds; /* String of commands to execute. */
410 int err; /* Set on an error. */
412 char *poptoken(void)
413 /* Pop one token off the command chain. */
415 token *cmd= cmds;
416 char *tok= cmd->token;
418 cmds= cmd->next;
419 free(cmd);
421 return tok;
424 void voidtoken(void)
425 /* Remove one token from the command chain. */
427 free(poptoken());
430 void parse_code(char *code)
431 /* Tokenize a string of monitor code, making sure there is a delimiter. It is
432 * to be executed next. (Prepended to the current input.)
435 if (cmds != nil && cmds->token[0] != ';') (void) tokenize(&cmds, ";");
436 (void) tokenize(&cmds, code);
439 int interrupt(void)
440 /* Clean up after an ESC has been typed. */
442 if (escape()) {
443 printf("[ESC]\n");
444 err= 1;
445 return 1;
447 return 0;
450 #if BIOS
452 int activate;
454 struct biosdev {
455 char name[8];
456 int device, primary, secondary;
457 } bootdev, tmpdev;
459 int get_master(char *master, struct part_entry **table, u32_t pos)
460 /* Read a master boot sector and its partition table. */
462 int r, n;
463 struct part_entry *pe, **pt;
465 if ((r= readsectors(mon2abs(master), pos, 1)) != 0) return r;
467 pe= (struct part_entry *) (master + PART_TABLE_OFF);
468 for (pt= table; pt < table + NR_PARTITIONS; pt++) *pt= pe++;
470 /* DOS has the misguided idea that partition tables must be sorted. */
471 if (pos != 0) return 0; /* But only the primary. */
473 n= NR_PARTITIONS;
474 do {
475 for (pt= table; pt < table + NR_PARTITIONS-1; pt++) {
476 if (pt[0]->sysind == NO_PART
477 || pt[0]->lowsec > pt[1]->lowsec) {
478 pe= pt[0]; pt[0]= pt[1]; pt[1]= pe;
481 } while (--n > 0);
482 return 0;
485 void initialize(void)
487 char master[SECTOR_SIZE];
488 struct part_entry *table[NR_PARTITIONS];
489 int r, p;
490 u32_t masterpos;
491 char *argp;
493 /* Copy the boot program to the far end of low memory, this must be
494 * done to get out of the way of Minix, and to put the data area
495 * cleanly inside a 64K chunk if using BIOS I/O (no DMA problems).
497 u32_t oldaddr= caddr;
498 u32_t memend= mem[0].base + mem[0].size;
499 u32_t newaddr= (memend - runsize) & ~0x0000FL;
500 #if !DOS
501 u32_t dma64k= (memend - 1) & ~0x0FFFFL;
504 /* Check if data segment crosses a 64K boundary. */
505 if (newaddr + (daddr - caddr) < dma64k) {
506 newaddr= (dma64k - runsize) & ~0x0000FL;
508 #endif
510 /* Set the new caddr for relocate. */
511 caddr= newaddr;
513 /* Copy code and data. */
514 raw_copy(newaddr, oldaddr, runsize);
516 /* Make the copy running. */
517 relocate();
519 #if !DOS
521 /* Take the monitor out of the memory map if we have memory to spare,
522 * and also keep the BIOS data area safe (1.5K), plus a bit extra for
523 * where we may have to put a.out headers for older kernels.
525 if (mon_return = (mem[1].size > 512*1024L)) mem[0].size = newaddr;
526 mem[0].base += 2048;
527 mem[0].size -= 2048;
529 /* Find out what the boot device and partition was. */
530 bootdev.name[0]= 0;
531 bootdev.device= device;
532 bootdev.primary= -1;
533 bootdev.secondary= -1;
535 if (device < 0x80) {
536 /* Floppy. */
537 strcpy(bootdev.name, "fd0");
538 bootdev.name[2] += bootdev.device;
539 return;
542 /* Disk: Get the partition table from the very first sector, and
543 * determine the partition we booted from using the information from
544 * the booted partition entry as passed on by the bootstrap (rem_part).
545 * All we need from it is the partition offset.
547 raw_copy(mon2abs(&lowsec),
548 vec2abs(&rem_part) + offsetof(struct part_entry, lowsec),
549 sizeof(lowsec));
551 masterpos= 0; /* Master bootsector position. */
553 for (;;) {
554 /* Extract the partition table from the master boot sector. */
555 if ((r= get_master(master, table, masterpos)) != 0) {
556 readerr(masterpos, r); exit(1);
559 /* See if you can find "lowsec" back. */
560 for (p= 0; p < NR_PARTITIONS; p++) {
561 if (lowsec - table[p]->lowsec < table[p]->size) break;
564 if (lowsec == table[p]->lowsec) { /* Found! */
565 if (bootdev.primary < 0)
566 bootdev.primary= p;
567 else
568 bootdev.secondary= p;
569 break;
572 if (p == NR_PARTITIONS || bootdev.primary >= 0
573 || table[p]->sysind != MINIX_PART) {
574 /* The boot partition cannot be named, this only means
575 * that "bootdev" doesn't work.
577 bootdev.device= -1;
578 return;
581 /* See if the primary partition is subpartitioned. */
582 bootdev.primary= p;
583 masterpos= table[p]->lowsec;
585 strcpy(bootdev.name, "d0p0");
586 bootdev.name[1] += (device - 0x80);
587 bootdev.name[3] += bootdev.primary;
588 if (bootdev.secondary >= 0) {
589 strcat(bootdev.name, "s0");
590 bootdev.name[5] += bootdev.secondary;
593 #else /* DOS */
594 /* Take the monitor out of the memory map if we have memory to spare,
595 * note that only half our PSP is needed at the new place, the first
596 * half is to be kept in its place.
598 if (mem[1].size > 0) mem[0].size = newaddr + 0x80 - mem[0].base;
600 /* Parse the command line. */
601 argp= PSP + 0x81;
602 argp[PSP[0x80]]= 0;
603 while (between('\1', *argp, ' ')) argp++;
604 vdisk= argp;
605 while (!between('\0', *argp, ' ')) argp++;
606 while (between('\1', *argp, ' ')) *argp++= 0;
607 if (*vdisk == 0) {
608 printf("\nUsage: boot <vdisk> [commands ...]\n");
609 exit(1);
611 drun= *argp == 0 ? "main" : argp;
613 if ((r= dev_open()) != 0) {
614 printf("\n%s: Error %02x (%s)\n", vdisk, r, bios_err(r));
615 exit(1);
618 /* Find the active partition on the virtual disk. */
619 if ((r= get_master(master, table, 0)) != 0) {
620 readerr(0, r); exit(1);
623 strcpy(bootdev.name, "d0");
624 bootdev.primary= -1;
625 for (p= 0; p < NR_PARTITIONS; p++) {
626 if (table[p]->bootind != 0 && table[p]->sysind == MINIX_PART) {
627 bootdev.primary= p;
628 strcat(bootdev.name, "p0");
629 bootdev.name[3] += p;
630 lowsec= table[p]->lowsec;
631 break;
634 #endif /* DOS */
637 #endif /* BIOS */
639 /* Reserved names: */
640 enum resnames {
641 R_NULL, R_BOOT, R_CTTY, R_DELAY, R_ECHO, R_EXIT, R_HELP,
642 R_LS, R_MENU, R_OFF, R_SAVE, R_SET, R_TRAP, R_UNSET
645 char resnames[][6] = {
646 "", "boot", "ctty", "delay", "echo", "exit", "help",
647 "ls", "menu", "off", "save", "set", "trap", "unset",
650 /* Using this for all null strings saves a lot of memory. */
651 #define null (resnames[0])
653 int reserved(char *s)
654 /* Recognize reserved strings. */
656 int r;
658 for (r= R_BOOT; r <= R_UNSET; r++) {
659 if (strcmp(s, resnames[r]) == 0) return r;
661 return R_NULL;
664 void sfree(char *s)
665 /* Free a non-null string. */
667 if (s != nil && s != null) free(s);
670 char *copystr(char *s)
671 /* Copy a non-null string using malloc. */
673 char *c;
675 if (*s == 0) return null;
676 c= malloc((strlen(s) + 1) * sizeof(char));
677 strcpy(c, s);
678 return c;
681 int is_default(environment *e)
683 return (e->flags & E_SPECIAL) && e->defval == nil;
686 environment **searchenv(char *name)
688 environment **aenv= &env;
690 while (*aenv != nil && strcmp((*aenv)->name, name) != 0) {
691 aenv= &(*aenv)->next;
694 return aenv;
697 #define b_getenv(name) (*searchenv(name))
698 /* Return the environment *structure* belonging to name, or nil if not found. */
700 char *b_value(char *name)
701 /* The value of a variable. */
703 environment *e= b_getenv(name);
705 return e == nil || !(e->flags & E_VAR) ? nil : e->value;
708 char *b_body(char *name)
709 /* The value of a function. */
711 environment *e= b_getenv(name);
713 return e == nil || !(e->flags & E_FUNCTION) ? nil : e->value;
716 int b_setenv(int flags, char *name, char *arg, char *value)
717 /* Change the value of an environment variable. Returns the flags of the
718 * variable if you are not allowed to change it, 0 otherwise.
721 environment **aenv, *e;
723 if (*(aenv= searchenv(name)) == nil) {
724 if (reserved(name)) return E_RESERVED;
725 e= malloc(sizeof(*e));
726 e->name= copystr(name);
727 e->flags= flags;
728 e->defval= nil;
729 e->next= nil;
730 *aenv= e;
731 } else {
732 e= *aenv;
734 /* Don't change special variables to functions or vv. */
735 if (e->flags & E_SPECIAL
736 && (e->flags & E_FUNCTION) != (flags & E_FUNCTION)
737 ) return e->flags;
739 e->flags= (e->flags & E_STICKY) | flags;
740 if (is_default(e)) {
741 e->defval= e->value;
742 } else {
743 sfree(e->value);
745 sfree(e->arg);
747 e->arg= copystr(arg);
748 e->value= copystr(value);
750 return 0;
753 int b_setvar(int flags, char *name, char *value)
754 /* Set variable or simple function. */
756 int r;
758 if((r=b_setenv(flags, name, null, value))) {
759 return r;
762 return r;
765 void b_unset(char *name)
766 /* Remove a variable from the environment. A special variable is reset to
767 * its default value.
770 environment **aenv, *e;
772 if ((e= *(aenv= searchenv(name))) == nil) return;
774 if (e->flags & E_SPECIAL) {
775 if (e->defval != nil) {
776 sfree(e->arg);
777 e->arg= null;
778 sfree(e->value);
779 e->value= e->defval;
780 e->defval= nil;
782 } else {
783 sfree(e->name);
784 sfree(e->arg);
785 sfree(e->value);
786 *aenv= e->next;
787 free(e);
791 long a2l(char *a)
792 /* Cheap atol(). */
794 int sign= 1;
795 long n= 0;
797 if (*a == '-') { sign= -1; a++; }
799 while (between('0', *a, '9')) n= n * 10 + (*a++ - '0');
801 return sign * n;
804 char *ul2a(u32_t n, unsigned b)
805 /* Transform a long number to ascii at base b, (b >= 8). */
807 static char num[(CHAR_BIT * sizeof(n) + 2) / 3 + 1];
808 char *a= arraylimit(num) - 1;
809 static char hex[16] = "0123456789ABCDEF";
811 do *--a = hex[(int) (n % b)]; while ((n/= b) > 0);
812 return a;
815 char *ul2a10(u32_t n)
816 /* Transform a long number to ascii at base 10. */
818 return ul2a(n, 10);
821 unsigned a2x(char *a)
822 /* Ascii to hex. */
824 unsigned n= 0;
825 int c;
827 for (;;) {
828 c= *a;
829 if (between('0', c, '9')) c= c - '0' + 0x0;
830 else
831 if (between('A', c, 'F')) c= c - 'A' + 0xA;
832 else
833 if (between('a', c, 'f')) c= c - 'a' + 0xa;
834 else
835 break;
836 n= (n<<4) | c;
837 a++;
839 return n;
842 void get_parameters(void)
844 char params[SECTOR_SIZE + 1];
845 token **acmds;
846 int r, bus, processor;
847 memory *mp;
848 static char bus_type[][4] = {
849 "xt", "at", "mca"
851 static char vid_type[][4] = {
852 "mda", "cga", "ega", "ega", "vga", "vga"
854 static char vid_chrome[][6] = {
855 "mono", "color"
858 /* Variables that Minix needs: */
859 b_setvar(E_SPECIAL|E_VAR|E_DEV, "rootdev", "ram");
860 b_setvar(E_SPECIAL|E_VAR|E_DEV, "ramimagedev", "bootdev");
861 b_setvar(E_SPECIAL|E_VAR, "ramsize", "0");
862 #if BIOS
863 processor = getprocessor();
864 if(processor == 1586) processor = 686;
865 b_setvar(E_SPECIAL|E_VAR, "processor", ul2a10(processor));
866 b_setvar(E_SPECIAL|E_VAR, "bus", bus_type[get_bus()]);
867 b_setvar(E_SPECIAL|E_VAR, "video", vid_type[get_video()]);
868 b_setvar(E_SPECIAL|E_VAR, "chrome", vid_chrome[get_video() & 1]);
869 params[0]= 0;
870 for (mp= mem; mp < arraylimit(mem); mp++) {
871 if (mp->size == 0) continue;
872 if (params[0] != 0) strcat(params, ",");
873 strcat(params, ul2a(mp->base, 0x10));
874 strcat(params, ":");
875 strcat(params, ul2a(mp->size, 0x10));
877 b_setvar(E_SPECIAL|E_VAR, "memory", params);
879 #if DOS
880 b_setvar(E_SPECIAL|E_VAR, "dosfile-d0", vdisk);
881 #endif
883 #endif
884 #if UNIX
885 b_setvar(E_SPECIAL|E_VAR, "processor", "?");
886 b_setvar(E_SPECIAL|E_VAR, "bus", "?");
887 b_setvar(E_SPECIAL|E_VAR, "video", "?");
888 b_setvar(E_SPECIAL|E_VAR, "chrome", "?");
889 b_setvar(E_SPECIAL|E_VAR, "memory", "?");
890 b_setvar(E_SPECIAL|E_VAR, "c0", "?");
891 #endif
893 /* Variables boot needs: */
894 b_setvar(E_SPECIAL|E_VAR, "image", "boot/image");
895 b_setvar(E_SPECIAL|E_FUNCTION, "leader",
896 "echo --- Welcome to MINIX 3. This is the boot monitor. ---\\n");
897 b_setvar(E_SPECIAL|E_FUNCTION, "main", "menu");
898 b_setvar(E_SPECIAL|E_FUNCTION, "trailer", "");
900 /* Default hidden menu function: */
901 b_setenv(E_RESERVED|E_FUNCTION, null, "=,Start MINIX", "boot");
903 /* Tokenize bootparams sector. */
904 if ((r= readsectors(mon2abs(params), lowsec+PARAMSEC, 1)) != 0) {
905 readerr(lowsec+PARAMSEC, r);
906 exit(1);
908 params[SECTOR_SIZE]= 0;
909 acmds= tokenize(&cmds, params);
911 /* Stuff the default action into the command chain. */
912 #if UNIX
913 (void) tokenize(acmds, ":;");
914 #elif DOS
915 (void) tokenize(tokenize(acmds, ":;leader;"), drun);
916 #else /* BIOS */
917 (void) tokenize(acmds, ":;leader;main");
918 #endif
921 char *addptr;
923 void addparm(char *n)
925 while (*n != 0 && *addptr != 0) *addptr++ = *n++;
928 void save_parameters(void)
929 /* Save nondefault environment variables to the bootparams sector. */
931 environment *e;
932 char params[SECTOR_SIZE + 1];
933 int r;
935 /* Default filling: */
936 memset(params, '\n', SECTOR_SIZE);
938 /* Don't touch the 0! */
939 params[SECTOR_SIZE]= 0;
940 addptr= params;
942 for (e= env; e != nil; e= e->next) {
943 if (e->flags & E_RESERVED || is_default(e)) continue;
945 addparm(e->name);
946 if (e->flags & E_FUNCTION) {
947 addparm("(");
948 addparm(e->arg);
949 addparm(")");
950 } else {
951 addparm((e->flags & (E_DEV|E_SPECIAL)) != E_DEV
952 ? "=" : "=d ");
954 addparm(e->value);
955 if (*addptr == 0) {
956 printf("The environment is too big\n");
957 return;
959 *addptr++= '\n';
962 /* Save the parameters on disk. */
963 if ((r= writesectors(mon2abs(params), lowsec+PARAMSEC, 1)) != 0) {
964 writerr(lowsec+PARAMSEC, r);
965 printf("Can't save environment\n");
969 void show_env(void)
970 /* Show the environment settings. */
972 environment *e;
973 unsigned more= 0;
974 int c;
976 for (e= env; e != nil; e= e->next) {
977 if (e->flags & E_RESERVED) continue;
978 if (!istty && is_default(e)) continue;
980 if (e->flags & E_FUNCTION) {
981 printf("%s(%s) %s\n", e->name, e->arg, e->value);
982 } else {
983 printf(is_default(e) ? "%s = (%s)\n" : "%s = %s\n",
984 e->name, e->value);
987 if (e->next != nil && istty && ++more % 20 == 0) {
988 printf("More? ");
989 c= getch();
990 if (c == ESC || c > ' ') {
991 putch('\n');
992 if (c > ' ') ungetch(c);
993 break;
995 printf("\b\b\b\b\b\b");
1000 int numprefix(char *s, char **ps)
1001 /* True iff s is a string of digits. *ps will be set to the first nondigit
1002 * if non-nil, otherwise the string should end.
1005 char *n= s;
1007 while (between('0', *n, '9')) n++;
1009 if (n == s) return 0;
1011 if (ps == nil) return *n == 0;
1013 *ps= n;
1014 return 1;
1017 int numeric(char *s)
1019 return numprefix(s, (char **) nil);
1022 #if BIOS
1024 /* Device numbers of standard MINIX devices. */
1025 #define DEV_FD0 0x0200
1026 static dev_t dev_cNd0[] = { 0x0300, 0x0800, 0x0A00, 0x0C00, 0x1000 };
1027 #define minor_p0s0 128
1029 static int block_size;
1031 dev_t name2dev(char *name)
1032 /* Translate, say, /dev/c0d0p2 to a device number. If the name can't be
1033 * found on the boot device, then do some guesswork. The global structure
1034 * "tmpdev" will be filled in based on the name, so that "boot d1p0" knows
1035 * what device to boot without interpreting device numbers.
1038 dev_t dev;
1039 ino_t ino;
1040 int drive;
1041 struct stat st;
1042 char *n, *s;
1044 /* "boot *d0p2" means: make partition 2 active before you boot it. */
1045 if ((activate= (name[0] == '*'))) name++;
1047 /* The special name "bootdev" must be translated to the boot device. */
1048 if (strcmp(name, "bootdev") == 0) {
1049 if (bootdev.device == -1) {
1050 printf("The boot device could not be named\n");
1051 errno= 0;
1052 return -1;
1054 name= bootdev.name;
1057 /* If our boot device doesn't have a file system, or we want to know
1058 * what a name means for the BIOS, then we need to interpret the
1059 * device name ourselves: "fd" = floppy, "c0d0" = hard disk, etc.
1061 tmpdev.device= tmpdev.primary= tmpdev.secondary= -1;
1062 dev= -1;
1063 n= name;
1064 if (strncmp(n, "/dev/", 5) == 0) n+= 5;
1066 if (strcmp(n, "ram") == 0) {
1067 dev= DEV_RAM;
1068 } else
1069 if (strcmp(n, "boot") == 0) {
1070 dev= DEV_BOOT;
1071 } else
1072 if (n[0] == 'f' && n[1] == 'd' && numeric(n+2)) {
1073 /* Floppy. */
1074 tmpdev.device= a2l(n+2);
1075 dev= DEV_FD0 + tmpdev.device;
1076 } else
1077 if ((n[0] == 'h' || n[0] == 's') && n[1] == 'd' && numprefix(n+2, &s)
1078 && (*s == 0 || (between('a', *s, 'd') && s[1] == 0))
1080 /* Old style hard disk (backwards compatibility.) */
1081 dev= a2l(n+2);
1082 tmpdev.device= dev / (1 + NR_PARTITIONS);
1083 tmpdev.primary= (dev % (1 + NR_PARTITIONS)) - 1;
1084 if (*s != 0) {
1085 /* Subpartition. */
1086 tmpdev.secondary= *s - 'a';
1087 dev= minor_p0s0
1088 + (tmpdev.device * NR_PARTITIONS
1089 + tmpdev.primary) * NR_PARTITIONS
1090 + tmpdev.secondary;
1092 tmpdev.device+= 0x80;
1093 dev+= n[0] == 'h' ? dev_cNd0[0] : dev_cNd0[2];
1094 } else {
1095 /* Hard disk. */
1096 int ctrlr= 0;
1098 if (n[0] == 'c' && between('0', n[1], '4')) {
1099 ctrlr= (n[1] - '0');
1100 tmpdev.device= 0;
1101 n+= 2;
1103 if (n[0] == 'd' && between('0', n[1], '7')) {
1104 tmpdev.device= (n[1] - '0');
1105 n+= 2;
1106 if (n[0] == 'p' && between('0', n[1], '3')) {
1107 tmpdev.primary= (n[1] - '0');
1108 n+= 2;
1109 if (n[0] == 's' && between('0', n[1], '3')) {
1110 tmpdev.secondary= (n[1] - '0');
1111 n+= 2;
1115 if (*n == 0) {
1116 dev= dev_cNd0[ctrlr];
1117 if (tmpdev.secondary < 0) {
1118 dev += tmpdev.device * (NR_PARTITIONS+1)
1119 + (tmpdev.primary + 1);
1120 } else {
1121 dev += minor_p0s0
1122 + (tmpdev.device * NR_PARTITIONS
1123 + tmpdev.primary) * NR_PARTITIONS
1124 + tmpdev.secondary;
1126 tmpdev.device+= 0x80;
1130 /* Look the name up on the boot device for the UNIX device number. */
1131 if (fsok == -1) fsok= r_super(&block_size) != 0;
1132 if (fsok) {
1133 /* The current working directory is "/dev". */
1134 ino= r_lookup(r_lookup(ROOT_INO, "dev"), name);
1136 if (ino != 0) {
1137 /* Name has been found, extract the device number. */
1138 r_stat(ino, &st);
1139 if (!S_ISBLK(st.st_mode)) {
1140 printf("%s is not a block device\n", name);
1141 errno= 0;
1142 return (dev_t) -1;
1144 dev= st.st_rdev;
1148 if (tmpdev.primary < 0) activate= 0; /* Careful now! */
1150 if (dev == -1) {
1151 printf("Can't recognize '%s' as a device\n", name);
1152 errno= 0;
1154 return dev;
1157 #if DEBUG
1158 static void apm_perror(char *label, u16_t ax)
1160 unsigned ah;
1161 char *str;
1163 ah= (ax >> 8);
1164 switch(ah)
1166 case 0x01: str= "APM functionality disabled"; break;
1167 case 0x03: str= "interface not connected"; break;
1168 case 0x09: str= "unrecognized device ID"; break;
1169 case 0x0A: str= "parameter value out of range"; break;
1170 case 0x0B: str= "interface not engaged"; break;
1171 case 0x60: str= "unable to enter requested state"; break;
1172 case 0x86: str= "APM not present"; break;
1173 default: printf("%s: error 0x%02x\n", label, ah); return;
1175 printf("%s: %s\n", label, str);
1178 #define apm_printf printf
1179 #else
1180 #define apm_perror(label, ax) ((void)0)
1181 #define apm_printf
1182 #endif
1184 static void off(void)
1186 bios_env_t be;
1187 unsigned al, ah;
1189 /* Try to switch off the system. Print diagnostic information
1190 * that can be useful if the operation fails.
1193 be.ax= 0x5300; /* APM, Installation check */
1194 be.bx= 0; /* Device, APM BIOS */
1195 int15(&be);
1196 if (be.flags & FL_CARRY)
1198 apm_perror("APM installation check failed", be.ax);
1199 return;
1201 if (be.bx != (('P' << 8) | 'M'))
1203 apm_printf("APM signature not found (got 0x%04x)\n", be.bx);
1204 return;
1207 ah= be.ax >> 8;
1208 if (ah > 9)
1209 ah= (ah >> 4)*10 + (ah & 0xf);
1210 al= be.ax & 0xff;
1211 if (al > 9)
1212 al= (al >> 4)*10 + (al & 0xf);
1213 apm_printf("APM version %u.%u%s%s%s%s%s\n",
1214 ah, al,
1215 (be.cx & 0x1) ? ", 16-bit PM" : "",
1216 (be.cx & 0x2) ? ", 32-bit PM" : "",
1217 (be.cx & 0x4) ? ", CPU-Idle" : "",
1218 (be.cx & 0x8) ? ", APM-disabled" : "",
1219 (be.cx & 0x10) ? ", APM-disengaged" : "");
1221 /* Connect */
1222 be.ax= 0x5301; /* APM, Real mode interface connect */
1223 be.bx= 0x0000; /* APM BIOS */
1224 int15(&be);
1225 if (be.flags & FL_CARRY)
1227 apm_perror("APM real mode connect failed", be.ax);
1228 return;
1231 /* Ask for a seat upgrade */
1232 be.ax= 0x530e; /* APM, Driver Version */
1233 be.bx= 0x0000; /* BIOS */
1234 be.cx= 0x0102; /* version 1.2 */
1235 int15(&be);
1236 if (be.flags & FL_CARRY)
1238 apm_perror("Set driver version failed", be.ax);
1239 goto disco;
1242 /* Is this version really worth reporting. Well, if the system
1243 * does switch off, you won't see it anyway.
1245 ah= be.ax >> 8;
1246 if (ah > 9)
1247 ah= (ah >> 4)*10 + (ah & 0xf);
1248 al= be.ax & 0xff;
1249 if (al > 9)
1250 al= (al >> 4)*10 + (al & 0xf);
1251 apm_printf("Got APM connection version %u.%u\n", ah, al);
1253 /* Enable */
1254 be.ax= 0x5308; /* APM, Enable/disable power management */
1255 be.bx= 0x0001; /* All device managed by APM BIOS */
1256 #if 0
1257 /* For old APM 1.0 systems, we need 0xffff. Assume that those
1258 * systems do not exist.
1260 be.bx= 0xffff; /* All device managed by APM BIOS (compat) */
1261 #endif
1262 be.cx= 0x0001; /* Enable power management */
1263 int15(&be);
1264 if (be.flags & FL_CARRY)
1266 apm_perror("Enable power management failed", be.ax);
1267 goto disco;
1270 /* Off */
1271 be.ax= 0x5307; /* APM, Set Power State */
1272 be.bx= 0x0001; /* All devices managed by APM */
1273 be.cx= 0x0003; /* Off */
1274 int15(&be);
1275 if (be.flags & FL_CARRY)
1277 apm_perror("Set power state failed", be.ax);
1278 goto disco;
1281 apm_printf("Power off sequence successfully completed.\n\n");
1282 apm_printf("Ha, ha, just kidding!\n");
1284 disco:
1285 /* Disconnect */
1286 be.ax= 0x5304; /* APM, interface disconnect */
1287 be.bx= 0x0000; /* APM BIOS */
1288 int15(&be);
1289 if (be.flags & FL_CARRY)
1291 apm_perror("APM interface disconnect failed", be.ax);
1292 return;
1296 #if !DOS
1297 #define B_NOSIG -1 /* "No signature" error code. */
1299 int exec_bootstrap(void)
1300 /* Load boot sector from the disk or floppy described by tmpdev and execute it.
1303 int r, n, dirty= 0;
1304 char master[SECTOR_SIZE];
1305 struct part_entry *table[NR_PARTITIONS], dummy, *active= &dummy;
1306 u32_t masterpos;
1308 active->lowsec= 0;
1310 /* Select a partition table entry. */
1311 while (tmpdev.primary >= 0) {
1312 masterpos= active->lowsec;
1314 if ((r= get_master(master, table, masterpos)) != 0) return r;
1316 active= table[tmpdev.primary];
1318 /* How does one check a partition table entry? */
1319 if (active->sysind == NO_PART) return B_NOSIG;
1321 tmpdev.primary= tmpdev.secondary;
1322 tmpdev.secondary= -1;
1325 if (activate && !active->bootind) {
1326 for (n= 0; n < NR_PARTITIONS; n++) table[n]->bootind= 0;
1327 active->bootind= ACTIVE_FLAG;
1328 dirty= 1;
1331 /* Read the boot sector. */
1332 if ((r= readsectors(BOOTPOS, active->lowsec, 1)) != 0) return r;
1334 /* Check signature word. */
1335 if (get_word(BOOTPOS+SIGNATOFF) != SIGNATURE) return B_NOSIG;
1337 /* Write the partition table if a member must be made active. */
1338 if (dirty && (r= writesectors(mon2abs(master), masterpos, 1)) != 0)
1339 return r;
1341 bootstrap(device, active);
1344 void boot_device(char *devname)
1345 /* Boot the device named by devname. */
1347 dev_t dev= name2dev(devname);
1348 int save_dev= device;
1349 int r;
1350 char *err;
1352 if (tmpdev.device < 0) {
1353 if (dev != -1) printf("Can't boot from %s\n", devname);
1354 return;
1357 /* Change current device and try to load and execute its bootstrap. */
1358 device= tmpdev.device;
1360 if ((r= dev_open()) == 0) r= exec_bootstrap();
1362 err= r == B_NOSIG ? "Not bootable" : bios_err(r);
1363 printf("Can't boot %s: %s\n", devname, err);
1365 /* Restore boot device setting. */
1366 device= save_dev;
1367 (void) dev_open();
1370 void ctty(char *line)
1372 if (line == nil) {
1373 serial_init(-1);
1374 } else
1375 if (between('0', line[0], '3') && line[1] == 0) {
1376 serial_init(line[0] - '0');
1377 } else {
1378 printf("Bad serial line number: %s\n", line);
1382 #else /* DOS */
1384 void boot_device(char *devname)
1385 /* No booting of other devices under DOS. */
1387 printf("Can't boot devices under DOS\n");
1390 void ctty(char *line)
1391 /* Don't know how to handle serial lines under DOS. */
1393 printf("No serial line support under DOS\n");
1396 #endif /* DOS */
1397 #endif /* BIOS */
1399 void ls(char *dir)
1400 /* List the contents of a directory. */
1402 ino_t ino;
1403 struct stat st;
1404 char name[NAME_MAX+1];
1406 if (fsok == -1) fsok= r_super(&block_size) != 0;
1407 if (!fsok) return;
1409 /* (,) construct because r_stat returns void */
1410 if ((ino= r_lookup(ROOT_INO, dir)) == 0 ||
1411 (r_stat(ino, &st), r_readdir(name)) == -1)
1413 printf("ls: %s: %s\n", dir, unix_err(errno));
1414 return;
1416 (void) r_readdir(name); /* Skip ".." too. */
1418 while ((ino= r_readdir(name)) != 0) printf("%s/%s\n", dir, name);
1421 u32_t milli_time(void)
1423 return get_tick() * MSEC_PER_TICK;
1426 u32_t milli_since(u32_t base)
1428 return (milli_time() + (TICKS_PER_DAY*MSEC_PER_TICK) - base)
1429 % (TICKS_PER_DAY*MSEC_PER_TICK);
1432 char *Thandler;
1433 u32_t Tbase, Tcount;
1435 void unschedule(void)
1436 /* Invalidate a waiting command. */
1438 alarm(0);
1440 if (Thandler != nil) {
1441 free(Thandler);
1442 Thandler= nil;
1446 void schedule(long msec, char *cmd)
1447 /* Schedule command at a certain time from now. */
1449 unschedule();
1450 Thandler= cmd;
1451 Tbase= milli_time();
1452 Tcount= msec;
1453 alarm(1);
1456 int expired(void)
1457 /* Check if the timer expired for getch(). */
1459 return (Thandler != nil && milli_since(Tbase) >= Tcount);
1462 void delay(char *msec)
1463 /* Delay for a given time. */
1465 u32_t base, count;
1467 if ((count= a2l(msec)) == 0) return;
1468 base= milli_time();
1470 alarm(1);
1472 do {
1473 pause();
1474 } while (!interrupt() && !expired() && milli_since(base) < count);
1477 enum whatfun { NOFUN, SELECT, DEFFUN, USERFUN } menufun(environment *e)
1479 if (!(e->flags & E_FUNCTION) || e->arg[0] == 0) return NOFUN;
1480 if (e->arg[1] != ',') return SELECT;
1481 return e->flags & E_RESERVED ? DEFFUN : USERFUN;
1484 void menu(void)
1485 /* By default: Show a simple menu.
1486 * Multiple kernels/images: Show extra selection options.
1487 * User defined function: Kill the defaults and show these.
1488 * Wait for a keypress and execute the given function.
1491 int c, def= 1;
1492 char *choice= nil;
1493 environment *e;
1495 /* Just a default menu? */
1496 for (e= env; e != nil; e= e->next) if (menufun(e) == USERFUN) def= 0;
1498 printf("\nHit a key as follows:\n\n");
1500 /* Show the choices. */
1501 for (e= env; e != nil; e= e->next) {
1502 switch (menufun(e)) {
1503 case DEFFUN:
1504 if (!def) break;
1505 /*FALL THROUGH*/
1506 case USERFUN:
1507 printf(" %c %s\n", e->arg[0], e->arg+2);
1508 break;
1509 case SELECT:
1510 printf(" %c Select %s kernel\n", e->arg[0],e->name);
1511 break;
1512 default:;
1516 /* Wait for a keypress. */
1517 do {
1518 c= getch();
1519 if (interrupt() || expired()) return;
1521 unschedule();
1523 for (e= env; e != nil; e= e->next) {
1524 switch (menufun(e)) {
1525 case DEFFUN:
1526 if (!def) break;
1527 case USERFUN:
1528 case SELECT:
1529 if (c == e->arg[0]) choice= e->value;
1532 } while (choice == nil);
1534 /* Execute the chosen function. */
1535 printf("%c\n", c);
1536 (void) tokenize(&cmds, choice);
1539 void help(void)
1540 /* Not everyone is a rocket scientist. */
1542 struct help {
1543 char *thing;
1544 char *help;
1545 } *pi;
1546 static struct help info[] = {
1547 { nil, "Names:" },
1548 { "rootdev", "Root device" },
1549 { "ramimagedev", "Device to use as RAM disk image " },
1550 { "ramsize", "RAM disk size (if no image device) " },
1551 { "bootdev", "Special name for the boot device" },
1552 { "fd0, d0p2, c0d0p1s0", "Devices (as in /dev)" },
1553 { "image", "Name of the boot image to use" },
1554 { "main", "Startup function" },
1555 { "bootdelay", "Delay in msec after loading image" },
1556 { nil, "Commands:" },
1557 { "name = [device] value", "Set environment variable" },
1558 { "name() { ... }", "Define function" },
1559 { "name(key,text) { ... }",
1560 "A menu option like: minix(=,Start MINIX) {boot}" },
1561 { "name", "Call function" },
1562 { "boot [device]", "Boot Minix or another O.S." },
1563 { "ctty [line]", "Duplicate to serial line" },
1564 { "delay [msec]", "Delay (500 msec default)" },
1565 { "echo word ...", "Display the words" },
1566 { "ls [directory]", "List contents of directory" },
1567 { "menu", "Show menu and choose menu option" },
1568 { "save / set", "Save or show environment" },
1569 { "trap msec command", "Schedule command " },
1570 { "unset name ...", "Unset variable or set to default" },
1571 { "exit / off", "Exit the Monitor / Power off" },
1574 for (pi= info; pi < arraylimit(info); pi++) {
1575 if (pi->thing != nil) printf(" %-24s- ", pi->thing);
1576 printf("%s\n", pi->help);
1580 void execute(void)
1581 /* Get one command from the command chain and execute it. */
1583 token *second, *third, *fourth, *sep;
1584 char *name;
1585 int res;
1586 size_t n= 0;
1588 if (err) {
1589 /* An error occured, stop interpreting. */
1590 while (cmds != nil) voidtoken();
1591 return;
1594 if (expired()) { /* Timer expired? */
1595 parse_code(Thandler);
1596 unschedule();
1599 /* There must be a separator lurking somewhere. */
1600 for (sep= cmds; sep != nil && sep->token[0] != ';'; sep= sep->next) n++;
1602 name= cmds->token;
1603 res= reserved(name);
1604 if ((second= cmds->next) != nil
1605 && (third= second->next) != nil)
1606 fourth= third->next;
1608 /* Null command? */
1609 if (n == 0) {
1610 voidtoken();
1611 return;
1612 } else
1613 /* name = [device] value? */
1614 if ((n == 3 || n == 4)
1615 && !sugar(name)
1616 && second->token[0] == '='
1617 && !sugar(third->token)
1618 && (n == 3 || (n == 4 && third->token[0] == 'd'
1619 && !sugar(fourth->token)
1620 ))) {
1621 char *value= third->token;
1622 int flags= E_VAR;
1624 if (n == 4) { value= fourth->token; flags|= E_DEV; }
1626 if ((flags= b_setvar(flags, name, value)) != 0) {
1627 printf("%s is a %s\n", name,
1628 flags & E_RESERVED ? "reserved word" :
1629 "special function");
1630 err= 1;
1632 while (cmds != sep) voidtoken();
1633 return;
1634 } else
1635 /* name '(arg)' ... ? */
1636 if (n >= 3
1637 && !sugar(name)
1638 && second->token[0] == '('
1640 token *fun;
1641 int c, flags, depth;
1642 char *body;
1643 size_t len;
1645 sep= fun= third;
1646 depth= 0;
1647 len= 1;
1648 while (sep != nil) {
1649 if ((c= sep->token[0]) == ';' && depth == 0) break;
1650 len+= strlen(sep->token) + 1;
1651 sep= sep->next;
1652 if (c == '{') depth++;
1653 if (c == '}' && --depth == 0) break;
1656 body= malloc(len * sizeof(char));
1657 *body= 0;
1659 while (fun != sep) {
1660 strcat(body, fun->token);
1661 if (!sugar(fun->token)
1662 && !sugar(fun->next->token)
1663 ) strcat(body, " ");
1664 fun= fun->next;
1666 second->token[strlen(second->token)-1]= 0;
1668 if (depth != 0) {
1669 printf("Missing '}'\n");
1670 err= 1;
1671 } else
1672 if ((flags= b_setenv(E_FUNCTION, name,
1673 second->token+1, body)) != 0) {
1674 printf("%s is a %s\n", name,
1675 flags & E_RESERVED ? "reserved word" :
1676 "special variable");
1677 err= 1;
1679 while (cmds != sep) voidtoken();
1680 free(body);
1681 return;
1682 } else
1683 /* Grouping? */
1684 if (name[0] == '{') {
1685 token **acmds= &cmds->next;
1686 char *t;
1687 int depth= 1;
1689 /* Find and remove matching '}' */
1690 depth= 1;
1691 while (*acmds != nil) {
1692 t= (*acmds)->token;
1693 if (t[0] == '{') depth++;
1694 if (t[0] == '}' && --depth == 0) { t[0]= ';'; break; }
1695 acmds= &(*acmds)->next;
1697 voidtoken();
1698 return;
1699 } else
1700 /* Command coming up, check if ESC typed. */
1701 if (interrupt()) {
1702 return;
1703 } else
1704 /* unset name ..., echo word ...? */
1705 if (n >= 1 && (res == R_UNSET || res == R_ECHO)) {
1706 char *arg= poptoken(), *p;
1708 for (;;) {
1709 free(arg);
1710 if (cmds == sep) break;
1711 arg= poptoken();
1712 if (res == R_UNSET) { /* unset arg */
1713 b_unset(arg);
1714 } else { /* echo arg */
1715 p= arg;
1716 while (*p != 0) {
1717 if (*p != '\\') {
1718 putch(*p);
1719 } else
1720 switch (*++p) {
1721 case 0:
1722 if (cmds == sep) return;
1723 continue;
1724 case 'n':
1725 putch('\n');
1726 break;
1727 case 'v':
1728 printf(version);
1729 break;
1730 case 'c':
1731 clear_screen();
1732 break;
1733 case 'w':
1734 for (;;) {
1735 if (interrupt())
1736 return;
1737 if (getch() == '\n')
1738 break;
1740 break;
1741 default:
1742 putch(*p);
1744 p++;
1746 putch(cmds != sep ? ' ' : '\n');
1749 return;
1750 } else
1751 /* boot -opts? */
1752 if (n == 2 && res == R_BOOT && second->token[0] == '-') {
1753 static char optsvar[]= "bootopts";
1754 (void) b_setvar(E_VAR, optsvar, second->token);
1755 voidtoken();
1756 voidtoken();
1757 bootminix();
1758 b_unset(optsvar);
1759 return;
1760 } else
1761 /* boot device, ls dir, delay msec? */
1762 if (n == 2 && (res == R_BOOT || res == R_CTTY
1763 || res == R_DELAY || res == R_LS)
1765 if (res == R_BOOT) boot_device(second->token);
1766 if (res == R_CTTY) ctty(second->token);
1767 if (res == R_DELAY) delay(second->token);
1768 if (res == R_LS) ls(second->token);
1769 voidtoken();
1770 voidtoken();
1771 return;
1772 } else
1773 /* trap msec command? */
1774 if (n == 3 && res == R_TRAP && numeric(second->token)) {
1775 long msec= a2l(second->token);
1777 voidtoken();
1778 voidtoken();
1779 schedule(msec, poptoken());
1780 return;
1781 } else
1782 /* Simple command. */
1783 if (n == 1) {
1784 char *body;
1785 int ok= 0;
1787 name= poptoken();
1789 switch (res) {
1790 case R_BOOT: bootminix(); ok= 1; break;
1791 case R_DELAY: delay("500"); ok= 1; break;
1792 case R_LS: ls(null); ok= 1; break;
1793 case R_MENU: menu(); ok= 1; break;
1794 case R_SAVE: save_parameters(); ok= 1;break;
1795 case R_SET: show_env(); ok= 1; break;
1796 case R_HELP: help(); ok= 1; break;
1797 case R_EXIT: exit(0);
1798 case R_OFF: off(); ok= 1; break;
1799 case R_CTTY: ctty(nil); ok= 1; break;
1802 /* Command to check bootparams: */
1803 if (strcmp(name, ":") == 0) ok= 1;
1805 /* User defined function. */
1806 if (!ok && (body= b_body(name)) != nil) {
1807 (void) tokenize(&cmds, body);
1808 ok= 1;
1810 if (!ok) printf("%s: unknown function", name);
1811 free(name);
1812 if (ok) return;
1813 } else {
1814 /* Syntax error. */
1815 printf("Can't parse:");
1816 while (cmds != sep) {
1817 printf(" %s", cmds->token); voidtoken();
1821 /* Getting here means that the command is not understood. */
1822 printf("\nTry 'help'\n");
1823 err= 1;
1826 int run_trailer(void)
1827 /* Run the trailer function between loading Minix and handing control to it.
1828 * Return true iff there was no error.
1831 token *save_cmds= cmds;
1833 cmds= nil;
1834 (void) tokenize(&cmds, "trailer");
1835 while (cmds != nil) execute();
1836 cmds= save_cmds;
1837 return !err;
1840 void monitor(void)
1841 /* Read a line and tokenize it. */
1843 char *line;
1845 unschedule(); /* Kill a trap. */
1846 err= 0; /* Clear error state. */
1848 if (istty) printf("%s>", bootdev.name);
1849 line= readline();
1850 (void) tokenize(&cmds, line);
1851 free(line);
1852 (void) escape(); /* Forget if ESC typed. */
1855 #if BIOS
1857 unsigned char cdspec[25];
1858 void bootcdinfo(u32_t, int *, int drive);
1860 void boot(void)
1861 /* Load Minix and start it, among other things. */
1864 /* Initialize tables. */
1865 initialize();
1867 /* Get environment variables from the parameter sector. */
1868 get_parameters();
1870 while (1) {
1871 /* While there are commands, execute them! */
1873 while (cmds != nil) execute();
1875 /* The "monitor" is just a "read one command" thing. */
1876 monitor();
1879 #endif /* BIOS */
1881 #if UNIX
1883 void main(int argc, char **argv)
1884 /* Do not load or start anything, just edit parameters. */
1886 int i;
1887 char bootcode[SECTOR_SIZE];
1888 struct termios rawterm;
1890 istty= (argc <= 2 && tcgetattr(0, &termbuf) == 0);
1892 if (argc < 2) {
1893 fprintf(stderr, "Usage: edparams device [command ...]\n");
1894 exit(1);
1897 /* Go over the arguments, changing control characters to spaces. */
1898 for (i= 2; i < argc; i++) {
1899 char *p;
1901 for (p= argv[i]; *p != 0; p++) {
1902 if ((unsigned) *p < ' ' && *p != '\n') *p= ' ';
1906 bootdev.name= argv[1];
1907 if (strncmp(bootdev.name, "/dev/", 5) == 0) bootdev.name+= 5;
1908 if ((bootdev.device= open(argv[1], O_RDWR, 0666)) < 0)
1909 fatal(bootdev.name);
1911 /* Check if it is a bootable Minix device. */
1912 if (readsectors(mon2abs(bootcode), lowsec, 1) != 0
1913 || memcmp(bootcode, boot_magic, sizeof(boot_magic)) != 0) {
1914 fprintf(stderr, "edparams: %s: not a bootable Minix device\n",
1915 bootdev.name);
1916 exit(1);
1919 /* Print greeting message. */
1920 if (istty) printf("Boot parameters editor.\n");
1922 signal(SIGINT, trap);
1923 signal(SIGALRM, trap);
1925 if (istty) {
1926 rawterm= termbuf;
1927 rawterm.c_lflag&= ~(ICANON|ECHO|IEXTEN);
1928 rawterm.c_cc[VINTR]= ESC;
1929 if (tcsetattr(0, TCSANOW, &rawterm) < 0) fatal("");
1932 /* Get environment variables from the parameter sector. */
1933 get_parameters();
1935 i= 2;
1936 for (;;) {
1937 /* While there are commands, execute them! */
1938 while (cmds != nil || i < argc) {
1939 if (cmds == nil) {
1940 /* A command line command. */
1941 parse_code(argv[i++]);
1943 execute();
1945 /* Bail out on errors if not interactive. */
1946 if (err && !istty) exit(1);
1949 /* Commands on the command line? */
1950 if (argc > 2) break;
1952 /* The "monitor" is just a "read one command" thing. */
1953 monitor();
1955 exit(0);
1957 #endif /* UNIX */
1960 * $PchId: boot.c,v 1.14 2002/02/27 19:46:14 philip Exp $