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