]> CyberLeo.Net >> Repos - FreeBSD/stable/8.git/blob - sys/boot/i386/boot2/boot2.c
MFC r362623:
[FreeBSD/stable/8.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                 sio_init(115200 / comspeed);
421         } else {
422             for (q = arg--; *q && *q != '('; q++);
423             if (*q) {
424                 drv = -1;
425                 if (arg[1] == ':') {
426                     drv = *arg - '0';
427                     if (drv > 9)
428                         return (-1);
429                     arg += 2;
430                 }
431                 if (q - arg != 2)
432                     return -1;
433                 for (i = 0; arg[0] != dev_nm[i][0] ||
434                             arg[1] != dev_nm[i][1]; i++)
435                     if (i == NDEV - 1)
436                         return -1;
437                 dsk.type = i;
438                 arg += 3;
439                 dsk.unit = *arg - '0';
440                 if (arg[1] != ',' || dsk.unit > 9)
441                     return -1;
442                 arg += 2;
443                 dsk.slice = WHOLE_DISK_SLICE;
444                 if (arg[1] == ',') {
445                     dsk.slice = *arg - '0' + 1;
446                     if (dsk.slice > NDOSPART + 1)
447                         return -1;
448                     arg += 2;
449                 }
450                 if (arg[1] != ')')
451                     return -1;
452                 dsk.part = *arg - 'a';
453                 if (dsk.part > 7)
454                     return (-1);
455                 arg += 2;
456                 if (drv == -1)
457                     drv = dsk.unit;
458                 dsk.drive = (dsk.type <= TYPE_MAXHARD
459                              ? DRV_HARD : 0) + drv;
460                 dsk_meta = 0;
461             }
462             if ((i = ep - arg)) {
463                 if ((size_t)i >= sizeof(knamebuf))
464                     return -1;
465                 memcpy(knamebuf, arg, i + 1);
466                 kname = knamebuf;
467             }
468         }
469         arg = p;
470     }
471     return 0;
472 }
473
474 static int
475 dskread(void *buf, unsigned lba, unsigned nblk)
476 {
477     struct dos_partition *dp;
478     struct disklabel *d;
479     char *sec;
480     unsigned i;
481     uint8_t sl;
482
483     if (!dsk_meta) {
484         sec = dmadat->secbuf;
485         dsk.start = 0;
486         if (drvread(sec, DOSBBSECTOR, 1))
487             return -1;
488         dp = (void *)(sec + DOSPARTOFF);
489         sl = dsk.slice;
490         if (sl < BASE_SLICE) {
491             for (i = 0; i < NDOSPART; i++)
492                 if (dp[i].dp_typ == DOSPTYP_386BSD &&
493                     (dp[i].dp_flag & 0x80 || sl < BASE_SLICE)) {
494                     sl = BASE_SLICE + i;
495                     if (dp[i].dp_flag & 0x80 ||
496                         dsk.slice == COMPATIBILITY_SLICE)
497                         break;
498                 }
499             if (dsk.slice == WHOLE_DISK_SLICE)
500                 dsk.slice = sl;
501         }
502         if (sl != WHOLE_DISK_SLICE) {
503             if (sl != COMPATIBILITY_SLICE)
504                 dp += sl - BASE_SLICE;
505             if (dp->dp_typ != DOSPTYP_386BSD) {
506                 printf("Invalid %s\n", "slice");
507                 return -1;
508             }
509             dsk.start = dp->dp_start;
510         }
511         if (drvread(sec, dsk.start + LABELSECTOR, 1))
512                 return -1;
513         d = (void *)(sec + LABELOFFSET);
514         if (d->d_magic != DISKMAGIC || d->d_magic2 != DISKMAGIC) {
515             if (dsk.part != RAW_PART) {
516                 printf("Invalid %s\n", "label");
517                 return -1;
518             }
519         } else {
520             if (!dsk.init) {
521                 if (d->d_type == DTYPE_SCSI)
522                     dsk.type = TYPE_DA;
523                 dsk.init++;
524             }
525             if (dsk.part >= d->d_npartitions ||
526                 !d->d_partitions[dsk.part].p_size) {
527                 printf("Invalid %s\n", "partition");
528                 return -1;
529             }
530             dsk.start += d->d_partitions[dsk.part].p_offset;
531             dsk.start -= d->d_partitions[RAW_PART].p_offset;
532         }
533     }
534     return drvread(buf, dsk.start + lba, nblk);
535 }
536
537 static void
538 printf(const char *fmt,...)
539 {
540     va_list ap;
541     static char buf[10];
542     char *s;
543     unsigned u;
544     int c;
545
546     va_start(ap, fmt);
547     while ((c = *fmt++)) {
548         if (c == '%') {
549             c = *fmt++;
550             switch (c) {
551             case 'c':
552                 putchar(va_arg(ap, int));
553                 continue;
554             case 's':
555                 for (s = va_arg(ap, char *); *s; s++)
556                     putchar(*s);
557                 continue;
558             case 'u':
559                 u = va_arg(ap, unsigned);
560                 s = buf;
561                 do
562                     *s++ = '0' + u % 10U;
563                 while (u /= 10U);
564                 while (--s >= buf)
565                     putchar(*s);
566                 continue;
567             }
568         }
569         putchar(c);
570     }
571     va_end(ap);
572     return;
573 }
574
575 static void
576 putchar(int c)
577 {
578     if (c == '\n')
579         xputc('\r');
580     xputc(c);
581 }
582
583 static int
584 drvread(void *buf, unsigned lba, unsigned nblk)
585 {
586     static unsigned c = 0x2d5c7c2f;
587
588     if (!OPT_CHECK(RBX_QUIET))
589         printf("%c\b", c = c << 8 | c >> 24);
590     v86.ctl = V86_ADDR | V86_CALLF | V86_FLAGS;
591     v86.addr = XREADORG;                /* call to xread in boot1 */
592     v86.es = VTOPSEG(buf);
593     v86.eax = lba;
594     v86.ebx = VTOPOFF(buf);
595     v86.ecx = lba >> 16;
596     v86.edx = nblk << 8 | dsk.drive;
597     v86int();
598     v86.ctl = V86_FLAGS;
599     if (V86_CY(v86.efl)) {
600         printf("error %u lba %u\n", v86.eax >> 8 & 0xff, lba);
601         return -1;
602     }
603     return 0;
604 }
605
606 static int
607 keyhit(unsigned ticks)
608 {
609     uint32_t t0, t1;
610
611     if (OPT_CHECK(RBX_NOINTR))
612         return 0;
613     t0 = 0;
614     for (;;) {
615         if (xgetc(1))
616             return 1;
617         t1 = *(uint32_t *)PTOV(0x46c);
618         if (!t0)
619             t0 = t1;
620         if ((uint32_t)(t1 - t0) >= ticks)
621             return 0;
622     }
623 }
624
625 static int
626 xputc(int c)
627 {
628     if (ioctrl & IO_KEYBOARD)
629         putc(c);
630     if (ioctrl & IO_SERIAL)
631         sio_putc(c);
632     return c;
633 }
634
635 static int
636 getc(int fn)
637 {
638     v86.addr = 0x16;
639     v86.eax = fn << 8;
640     v86int();
641     return fn == 0 ? v86.eax & 0xff : !V86_ZR(v86.efl);
642 }
643
644 static int
645 xgetc(int fn)
646 {
647     if (OPT_CHECK(RBX_NOINTR))
648         return 0;
649     for (;;) {
650         if (ioctrl & IO_KEYBOARD && getc(1))
651             return fn ? 1 : getc(0);
652         if (ioctrl & IO_SERIAL && sio_ischar())
653             return fn ? 1 : sio_getc();
654         if (fn)
655             return 0;
656     }
657 }