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