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