]> CyberLeo.Net >> Repos - FreeBSD/releng/9.0.git/blob - sys/boot/i386/boot2/boot2.c
Copy stable/9 to releng/9.0 as part of the FreeBSD 9.0-RELEASE release
[FreeBSD/releng/9.0.git] / sys / boot / i386 / boot2 / boot2.c
1 /*-
2  * Copyright (c) 1998 Robert Nordier
3  * All rights reserved.
4  *
5  * Redistribution and use in source and binary forms are freely
6  * permitted provided that the above copyright notice and this
7  * paragraph and the following disclaimer are duplicated in all
8  * such forms.
9  *
10  * This software is provided "AS IS" and without any express or
11  * implied warranties, including, without limitation, the implied
12  * warranties of merchantability and fitness for a particular
13  * purpose.
14  */
15
16 #include <sys/cdefs.h>
17 __FBSDID("$FreeBSD$");
18
19 #include <sys/param.h>
20 #include <sys/disklabel.h>
21 #include <sys/diskmbr.h>
22 #include <sys/dirent.h>
23 #include <sys/reboot.h>
24
25 #include <machine/bootinfo.h>
26 #include <machine/elf.h>
27 #include <machine/psl.h>
28
29 #include <stdarg.h>
30
31 #include <a.out.h>
32
33 #include <btxv86.h>
34
35 #include "boot2.h"
36 #include "lib.h"
37
38 #define IO_KEYBOARD     1
39 #define IO_SERIAL       2
40
41 #define SECOND          18      /* Circa that many ticks in a second. */
42
43 #define RBX_ASKNAME     0x0     /* -a */
44 #define RBX_SINGLE      0x1     /* -s */
45 /* 0x2 is reserved for log2(RB_NOSYNC). */
46 /* 0x3 is reserved for log2(RB_HALT). */
47 /* 0x4 is reserved for log2(RB_INITNAME). */
48 #define RBX_DFLTROOT    0x5     /* -r */
49 #define RBX_KDB         0x6     /* -d */
50 /* 0x7 is reserved for log2(RB_RDONLY). */
51 /* 0x8 is reserved for log2(RB_DUMP). */
52 /* 0x9 is reserved for log2(RB_MINIROOT). */
53 #define RBX_CONFIG      0xa     /* -c */
54 #define RBX_VERBOSE     0xb     /* -v */
55 #define RBX_SERIAL      0xc     /* -h */
56 #define RBX_CDROM       0xd     /* -C */
57 /* 0xe is reserved for log2(RB_POWEROFF). */
58 #define RBX_GDB         0xf     /* -g */
59 #define RBX_MUTE        0x10    /* -m */
60 /* 0x11 is reserved for log2(RB_SELFTEST). */
61 /* 0x12 is reserved for boot programs. */
62 /* 0x13 is reserved for boot programs. */
63 #define RBX_PAUSE       0x14    /* -p */
64 #define RBX_QUIET       0x15    /* -q */
65 #define RBX_NOINTR      0x1c    /* -n */
66 /* 0x1d is reserved for log2(RB_MULTIPLE) and is just misnamed here. */
67 #define RBX_DUAL        0x1d    /* -D */
68 /* 0x1f is reserved for log2(RB_BOOTINFO). */
69
70 /* pass: -a, -s, -r, -d, -c, -v, -h, -C, -g, -m, -p, -D */
71 #define RBX_MASK        (OPT_SET(RBX_ASKNAME) | OPT_SET(RBX_SINGLE) | \
72                         OPT_SET(RBX_DFLTROOT) | OPT_SET(RBX_KDB ) | \
73                         OPT_SET(RBX_CONFIG) | OPT_SET(RBX_VERBOSE) | \
74                         OPT_SET(RBX_SERIAL) | OPT_SET(RBX_CDROM) | \
75                         OPT_SET(RBX_GDB ) | OPT_SET(RBX_MUTE) | \
76                         OPT_SET(RBX_PAUSE) | OPT_SET(RBX_DUAL))
77
78 #define PATH_CONFIG     "/boot.config"
79 #define PATH_BOOT3      "/boot/loader"
80 #define PATH_KERNEL     "/boot/kernel/kernel"
81
82 #define ARGS            0x900
83 #define NOPT            14
84 #define NDEV            3
85 #define MEM_BASE        0x12
86 #define MEM_EXT         0x15
87 #define V86_CY(x)       ((x) & PSL_C)
88 #define V86_ZR(x)       ((x) & PSL_Z)
89
90 #define DRV_HARD        0x80
91 #define DRV_MASK        0x7f
92
93 #define TYPE_AD         0
94 #define TYPE_DA         1
95 #define TYPE_MAXHARD    TYPE_DA
96 #define TYPE_FD         2
97
98 #define OPT_SET(opt)    (1 << (opt))
99 #define OPT_CHECK(opt)  ((opts) & OPT_SET(opt))
100
101 extern uint32_t _end;
102
103 static const char optstr[NOPT] = "DhaCcdgmnpqrsv"; /* Also 'P', 'S' */
104 static const unsigned char flags[NOPT] = {
105     RBX_DUAL,
106     RBX_SERIAL,
107     RBX_ASKNAME,
108     RBX_CDROM,
109     RBX_CONFIG,
110     RBX_KDB,
111     RBX_GDB,
112     RBX_MUTE,
113     RBX_NOINTR,
114     RBX_PAUSE,
115     RBX_QUIET,
116     RBX_DFLTROOT,
117     RBX_SINGLE,
118     RBX_VERBOSE
119 };
120
121 static const char *const dev_nm[NDEV] = {"ad", "da", "fd"};
122 static const unsigned char dev_maj[NDEV] = {30, 4, 2};
123
124 static struct dsk {
125     unsigned drive;
126     unsigned type;
127     unsigned unit;
128     uint8_t slice;
129     uint8_t part;
130     unsigned start;
131     int init;
132 } dsk;
133 static char cmd[512], cmddup[512];
134 static const char *kname;
135 static uint32_t opts;
136 static int comspeed = SIOSPD;
137 static struct bootinfo bootinfo;
138 static uint8_t ioctrl = IO_KEYBOARD;
139
140 void exit(int);
141 static void load(void);
142 static int parse(void);
143 static int xfsread(ino_t, void *, size_t);
144 static int dskread(void *, unsigned, unsigned);
145 static void printf(const char *,...);
146 static void putchar(int);
147 static int drvread(void *, unsigned, unsigned);
148 static int keyhit(unsigned);
149 static int xputc(int);
150 static int xgetc(int);
151 static inline int getc(int);
152
153 static void memcpy(void *, const void *, int);
154 static void
155 memcpy(void *dst, const void *src, int len)
156 {
157     const char *s = src;
158     char *d = dst;
159
160     while (len--)
161         *d++ = *s++;
162 }
163
164 static inline int
165 strcmp(const char *s1, const char *s2)
166 {
167     for (; *s1 == *s2 && *s1; s1++, s2++);
168     return (unsigned char)*s1 - (unsigned char)*s2;
169 }
170
171 #define UFS_SMALL_CGBASE
172 #include "ufsread.c"
173
174 static inline int
175 xfsread(ino_t inode, void *buf, size_t nbyte)
176 {
177     if ((size_t)fsread(inode, buf, nbyte) != nbyte) {
178         printf("Invalid %s\n", "format");
179         return -1;
180     }
181     return 0;
182 }
183
184 static inline void
185 getstr(void)
186 {
187     char *s;
188     int c;
189
190     s = cmd;
191     for (;;) {
192         switch (c = xgetc(0)) {
193         case 0:
194             break;
195         case '\177':
196         case '\b':
197             if (s > cmd) {
198                 s--;
199                 printf("\b \b");
200             }
201             break;
202         case '\n':
203         case '\r':
204             *s = 0;
205             return;
206         default:
207             if (s - cmd < sizeof(cmd) - 1)
208                 *s++ = c;
209             putchar(c);
210         }
211     }
212 }
213
214 static inline void
215 putc(int c)
216 {
217     v86.addr = 0x10;
218     v86.eax = 0xe00 | (c & 0xff);
219     v86.ebx = 0x7;
220     v86int();
221 }
222
223 int
224 main(void)
225 {
226     uint8_t autoboot;
227     ino_t ino;
228
229     kname = NULL;
230     dmadat = (void *)(roundup2(__base + (int32_t)&_end, 0x10000) - __base);
231     v86.ctl = V86_FLAGS;
232     v86.efl = PSL_RESERVED_DEFAULT | PSL_I;
233     dsk.drive = *(uint8_t *)PTOV(ARGS);
234     dsk.type = dsk.drive & DRV_HARD ? TYPE_AD : TYPE_FD;
235     dsk.unit = dsk.drive & DRV_MASK;
236     dsk.slice = *(uint8_t *)PTOV(ARGS + 1) + 1;
237     bootinfo.bi_version = BOOTINFO_VERSION;
238     bootinfo.bi_size = sizeof(bootinfo);
239
240     /* Process configuration file */
241
242     autoboot = 1;
243
244     if ((ino = lookup(PATH_CONFIG)))
245         fsread(ino, cmd, sizeof(cmd));
246
247     if (*cmd) {
248         memcpy(cmddup, cmd, sizeof(cmd));
249         if (parse())
250             autoboot = 0;
251         if (!OPT_CHECK(RBX_QUIET))
252             printf("%s: %s", PATH_CONFIG, cmddup);
253         /* Do not process this command twice */
254         *cmd = 0;
255     }
256
257     /*
258      * Try to exec stage 3 boot loader. If interrupted by a keypress,
259      * or in case of failure, try to load a kernel directly instead.
260      */
261
262     if (autoboot && !kname) {
263         kname = PATH_BOOT3;
264         if (!keyhit(3*SECOND)) {
265             load();
266             kname = PATH_KERNEL;
267         }
268     }
269
270     /* Present the user with the boot2 prompt. */
271
272     for (;;) {
273         if (!autoboot || !OPT_CHECK(RBX_QUIET))
274             printf("\nFreeBSD/x86 boot\n"
275                    "Default: %u:%s(%u,%c)%s\n"
276                    "boot: ",
277                    dsk.drive & DRV_MASK, dev_nm[dsk.type], dsk.unit,
278                    'a' + dsk.part, kname);
279         if (ioctrl & IO_SERIAL)
280             sio_flush();
281         if (!autoboot || keyhit(3*SECOND))
282             getstr();
283         else if (!autoboot || !OPT_CHECK(RBX_QUIET))
284             putchar('\n');
285         autoboot = 0;
286         if (parse())
287             putchar('\a');
288         else
289             load();
290     }
291 }
292
293 /* XXX - Needed for btxld to link the boot2 binary; do not remove. */
294 void
295 exit(int x)
296 {
297 }
298
299 static void
300 load(void)
301 {
302     union {
303         struct exec ex;
304         Elf32_Ehdr eh;
305     } hdr;
306     static Elf32_Phdr ep[2];
307     static Elf32_Shdr es[2];
308     caddr_t p;
309     ino_t ino;
310     uint32_t addr;
311     int i, j;
312
313     if (!(ino = lookup(kname))) {
314         if (!ls)
315             printf("No %s\n", kname);
316         return;
317     }
318     if (xfsread(ino, &hdr, sizeof(hdr)))
319         return;
320
321     if (N_GETMAGIC(hdr.ex) == ZMAGIC) {
322         addr = hdr.ex.a_entry & 0xffffff;
323         p = PTOV(addr);
324         fs_off = PAGE_SIZE;
325         if (xfsread(ino, p, hdr.ex.a_text))
326             return;
327         p += roundup2(hdr.ex.a_text, PAGE_SIZE);
328         if (xfsread(ino, p, hdr.ex.a_data))
329             return;
330     } else if (IS_ELF(hdr.eh)) {
331         fs_off = hdr.eh.e_phoff;
332         for (j = i = 0; i < hdr.eh.e_phnum && j < 2; i++) {
333             if (xfsread(ino, ep + j, sizeof(ep[0])))
334                 return;
335             if (ep[j].p_type == PT_LOAD)
336                 j++;
337         }
338         for (i = 0; i < 2; i++) {
339             p = PTOV(ep[i].p_paddr & 0xffffff);
340             fs_off = ep[i].p_offset;
341             if (xfsread(ino, p, ep[i].p_filesz))
342                 return;
343         }
344         p += roundup2(ep[1].p_memsz, PAGE_SIZE);
345         bootinfo.bi_symtab = VTOP(p);
346         if (hdr.eh.e_shnum == hdr.eh.e_shstrndx + 3) {
347             fs_off = hdr.eh.e_shoff + sizeof(es[0]) *
348                 (hdr.eh.e_shstrndx + 1);
349             if (xfsread(ino, &es, sizeof(es)))
350                 return;
351             for (i = 0; i < 2; i++) {
352                 *(Elf32_Word *)p = es[i].sh_size;
353                 p += sizeof(es[i].sh_size);
354                 fs_off = es[i].sh_offset;
355                 if (xfsread(ino, p, es[i].sh_size))
356                     return;
357                 p += es[i].sh_size;
358             }
359         }
360         addr = hdr.eh.e_entry & 0xffffff;
361         bootinfo.bi_esymtab = VTOP(p);
362     } else {
363         printf("Invalid %s\n", "format");
364         return;
365     }
366
367     bootinfo.bi_kernelname = VTOP(kname);
368     bootinfo.bi_bios_dev = dsk.drive;
369     __exec((caddr_t)addr, RB_BOOTINFO | (opts & RBX_MASK),
370            MAKEBOOTDEV(dev_maj[dsk.type], dsk.slice, dsk.unit, dsk.part),
371            0, 0, 0, VTOP(&bootinfo));
372 }
373
374 static int
375 parse()
376 {
377     char *arg = cmd;
378     char *ep, *p, *q;
379     const char *cp;
380     unsigned int drv;
381     int c, i, j;
382
383     while ((c = *arg++)) {
384         if (c == ' ' || c == '\t' || c == '\n')
385             continue;
386         for (p = arg; *p && *p != '\n' && *p != ' ' && *p != '\t'; p++);
387         ep = p;
388         if (*p)
389             *p++ = 0;
390         if (c == '-') {
391             while ((c = *arg++)) {
392                 if (c == 'P') {
393                     if (*(uint8_t *)PTOV(0x496) & 0x10) {
394                         cp = "yes";
395                     } else {
396                         opts |= OPT_SET(RBX_DUAL) | OPT_SET(RBX_SERIAL);
397                         cp = "no";
398                     }
399                     printf("Keyboard: %s\n", cp);
400                     continue;
401                 } else if (c == 'S') {
402                     j = 0;
403                     while ((unsigned int)(i = *arg++ - '0') <= 9)
404                         j = j * 10 + i;
405                     if (j > 0 && i == -'0') {
406                         comspeed = j;
407                         break;
408                     }
409                     /* Fall through to error below ('S' not in optstr[]). */
410                 }
411                 for (i = 0; c != optstr[i]; i++)
412                     if (i == NOPT - 1)
413                         return -1;
414                 opts ^= OPT_SET(flags[i]);
415             }
416             ioctrl = OPT_CHECK(RBX_DUAL) ? (IO_SERIAL|IO_KEYBOARD) :
417                      OPT_CHECK(RBX_SERIAL) ? IO_SERIAL : IO_KEYBOARD;
418             if (ioctrl & IO_SERIAL)
419                 sio_init(115200 / comspeed);
420         } else {
421             for (q = arg--; *q && *q != '('; q++);
422             if (*q) {
423                 drv = -1;
424                 if (arg[1] == ':') {
425                     drv = *arg - '0';
426                     if (drv > 9)
427                         return (-1);
428                     arg += 2;
429                 }
430                 if (q - arg != 2)
431                     return -1;
432                 for (i = 0; arg[0] != dev_nm[i][0] ||
433                             arg[1] != dev_nm[i][1]; i++)
434                     if (i == NDEV - 1)
435                         return -1;
436                 dsk.type = i;
437                 arg += 3;
438                 dsk.unit = *arg - '0';
439                 if (arg[1] != ',' || dsk.unit > 9)
440                     return -1;
441                 arg += 2;
442                 dsk.slice = WHOLE_DISK_SLICE;
443                 if (arg[1] == ',') {
444                     dsk.slice = *arg - '0' + 1;
445                     if (dsk.slice > NDOSPART + 1)
446                         return -1;
447                     arg += 2;
448                 }
449                 if (arg[1] != ')')
450                     return -1;
451                 dsk.part = *arg - 'a';
452                 if (dsk.part > 7)
453                     return (-1);
454                 arg += 2;
455                 if (drv == -1)
456                     drv = dsk.unit;
457                 dsk.drive = (dsk.type <= TYPE_MAXHARD
458                              ? DRV_HARD : 0) + drv;
459                 dsk_meta = 0;
460             }
461             kname = arg;
462         }
463         arg = p;
464     }
465     return 0;
466 }
467
468 static int
469 dskread(void *buf, unsigned lba, unsigned nblk)
470 {
471     struct dos_partition *dp;
472     struct disklabel *d;
473     char *sec;
474     unsigned i;
475     uint8_t sl;
476
477     if (!dsk_meta) {
478         sec = dmadat->secbuf;
479         dsk.start = 0;
480         if (drvread(sec, DOSBBSECTOR, 1))
481             return -1;
482         dp = (void *)(sec + DOSPARTOFF);
483         sl = dsk.slice;
484         if (sl < BASE_SLICE) {
485             for (i = 0; i < NDOSPART; i++)
486                 if (dp[i].dp_typ == DOSPTYP_386BSD &&
487                     (dp[i].dp_flag & 0x80 || sl < BASE_SLICE)) {
488                     sl = BASE_SLICE + i;
489                     if (dp[i].dp_flag & 0x80 ||
490                         dsk.slice == COMPATIBILITY_SLICE)
491                         break;
492                 }
493             if (dsk.slice == WHOLE_DISK_SLICE)
494                 dsk.slice = sl;
495         }
496         if (sl != WHOLE_DISK_SLICE) {
497             if (sl != COMPATIBILITY_SLICE)
498                 dp += sl - BASE_SLICE;
499             if (dp->dp_typ != DOSPTYP_386BSD) {
500                 printf("Invalid %s\n", "slice");
501                 return -1;
502             }
503             dsk.start = dp->dp_start;
504         }
505         if (drvread(sec, dsk.start + LABELSECTOR, 1))
506                 return -1;
507         d = (void *)(sec + LABELOFFSET);
508         if (d->d_magic != DISKMAGIC || d->d_magic2 != DISKMAGIC) {
509             if (dsk.part != RAW_PART) {
510                 printf("Invalid %s\n", "label");
511                 return -1;
512             }
513         } else {
514             if (!dsk.init) {
515                 if (d->d_type == DTYPE_SCSI)
516                     dsk.type = TYPE_DA;
517                 dsk.init++;
518             }
519             if (dsk.part >= d->d_npartitions ||
520                 !d->d_partitions[dsk.part].p_size) {
521                 printf("Invalid %s\n", "partition");
522                 return -1;
523             }
524             dsk.start += d->d_partitions[dsk.part].p_offset;
525             dsk.start -= d->d_partitions[RAW_PART].p_offset;
526         }
527     }
528     return drvread(buf, dsk.start + lba, nblk);
529 }
530
531 static void
532 printf(const char *fmt,...)
533 {
534     va_list ap;
535     static char buf[10];
536     char *s;
537     unsigned u;
538     int c;
539
540     va_start(ap, fmt);
541     while ((c = *fmt++)) {
542         if (c == '%') {
543             c = *fmt++;
544             switch (c) {
545             case 'c':
546                 putchar(va_arg(ap, int));
547                 continue;
548             case 's':
549                 for (s = va_arg(ap, char *); *s; s++)
550                     putchar(*s);
551                 continue;
552             case 'u':
553                 u = va_arg(ap, unsigned);
554                 s = buf;
555                 do
556                     *s++ = '0' + u % 10U;
557                 while (u /= 10U);
558                 while (--s >= buf)
559                     putchar(*s);
560                 continue;
561             }
562         }
563         putchar(c);
564     }
565     va_end(ap);
566     return;
567 }
568
569 static void
570 putchar(int c)
571 {
572     if (c == '\n')
573         xputc('\r');
574     xputc(c);
575 }
576
577 static int
578 drvread(void *buf, unsigned lba, unsigned nblk)
579 {
580     static unsigned c = 0x2d5c7c2f;
581
582     if (!OPT_CHECK(RBX_QUIET))
583         printf("%c\b", c = c << 8 | c >> 24);
584     v86.ctl = V86_ADDR | V86_CALLF | V86_FLAGS;
585     v86.addr = XREADORG;                /* call to xread in boot1 */
586     v86.es = VTOPSEG(buf);
587     v86.eax = lba;
588     v86.ebx = VTOPOFF(buf);
589     v86.ecx = lba >> 16;
590     v86.edx = nblk << 8 | dsk.drive;
591     v86int();
592     v86.ctl = V86_FLAGS;
593     if (V86_CY(v86.efl)) {
594         printf("error %u lba %u\n", v86.eax >> 8 & 0xff, lba);
595         return -1;
596     }
597     return 0;
598 }
599
600 static int
601 keyhit(unsigned ticks)
602 {
603     uint32_t t0, t1;
604
605     if (OPT_CHECK(RBX_NOINTR))
606         return 0;
607     t0 = 0;
608     for (;;) {
609         if (xgetc(1))
610             return 1;
611         t1 = *(uint32_t *)PTOV(0x46c);
612         if (!t0)
613             t0 = t1;
614         if ((uint32_t)(t1 - t0) >= ticks)
615             return 0;
616     }
617 }
618
619 static int
620 xputc(int c)
621 {
622     if (ioctrl & IO_KEYBOARD)
623         putc(c);
624     if (ioctrl & IO_SERIAL)
625         sio_putc(c);
626     return c;
627 }
628
629 static int
630 getc(int fn)
631 {
632     v86.addr = 0x16;
633     v86.eax = fn << 8;
634     v86int();
635     return fn == 0 ? v86.eax & 0xff : !V86_ZR(v86.efl);
636 }
637
638 static int
639 xgetc(int fn)
640 {
641     if (OPT_CHECK(RBX_NOINTR))
642         return 0;
643     for (;;) {
644         if (ioctrl & IO_KEYBOARD && getc(1))
645             return fn ? 1 : getc(0);
646         if (ioctrl & IO_SERIAL && sio_ischar())
647             return fn ? 1 : sio_getc();
648         if (fn)
649             return 0;
650     }
651 }