]> CyberLeo.Net >> Repos - FreeBSD/FreeBSD.git/blob - sys/boot/i386/boot2/boot2.c
This commit was generated by cvs2svn to compensate for changes in r177128,
[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], cmddup[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         memcpy(cmddup, cmd, sizeof(cmd));
259         if (parse())
260             autoboot = 0;
261         if (!OPT_CHECK(RBX_QUIET))
262             printf("%s: %s", PATH_CONFIG, cmddup);
263         /* Do not process this command twice */
264         *cmd = 0;
265     }
266
267     /*
268      * Try to exec stage 3 boot loader. If interrupted by a keypress,
269      * or in case of failure, try to load a kernel directly instead.
270      */
271
272     if (autoboot && !*kname) {
273         memcpy(kname, PATH_BOOT3, sizeof(PATH_BOOT3));
274         if (!keyhit(3*SECOND)) {
275             load();
276             memcpy(kname, PATH_KERNEL, sizeof(PATH_KERNEL));
277         }
278     }
279
280     /* Present the user with the boot2 prompt. */
281
282     for (;;) {
283         if (!autoboot || !OPT_CHECK(RBX_QUIET))
284             printf("\nFreeBSD/i386 boot\n"
285                    "Default: %u:%s(%u,%c)%s\n"
286                    "boot: ",
287                    dsk.drive & DRV_MASK, dev_nm[dsk.type], dsk.unit,
288                    'a' + dsk.part, kname);
289         if (ioctrl & IO_SERIAL)
290             sio_flush();
291         if (!autoboot || keyhit(5*SECOND))
292             getstr();
293         else if (!autoboot || !OPT_CHECK(RBX_QUIET))
294             putchar('\n');
295         autoboot = 0;
296         if (parse())
297             putchar('\a');
298         else
299             load();
300     }
301 }
302
303 /* XXX - Needed for btxld to link the boot2 binary; do not remove. */
304 void
305 exit(int x)
306 {
307 }
308
309 static void
310 load(void)
311 {
312     union {
313         struct exec ex;
314         Elf32_Ehdr eh;
315     } hdr;
316     static Elf32_Phdr ep[2];
317     static Elf32_Shdr es[2];
318     caddr_t p;
319     ino_t ino;
320     uint32_t addr, x;
321     int fmt, i, j;
322
323     if (!(ino = lookup(kname))) {
324         if (!ls)
325             printf("No %s\n", kname);
326         return;
327     }
328     if (xfsread(ino, &hdr, sizeof(hdr)))
329         return;
330     if (N_GETMAGIC(hdr.ex) == ZMAGIC)
331         fmt = 0;
332     else if (IS_ELF(hdr.eh))
333         fmt = 1;
334     else {
335         printf("Invalid %s\n", "format");
336         return;
337     }
338     if (fmt == 0) {
339         addr = hdr.ex.a_entry & 0xffffff;
340         p = PTOV(addr);
341         fs_off = PAGE_SIZE;
342         if (xfsread(ino, p, hdr.ex.a_text))
343             return;
344         p += roundup2(hdr.ex.a_text, PAGE_SIZE);
345         if (xfsread(ino, p, hdr.ex.a_data))
346             return;
347         p += hdr.ex.a_data + roundup2(hdr.ex.a_bss, PAGE_SIZE);
348         bootinfo.bi_symtab = VTOP(p);
349         memcpy(p, &hdr.ex.a_syms, sizeof(hdr.ex.a_syms));
350         p += sizeof(hdr.ex.a_syms);
351         if (hdr.ex.a_syms) {
352             if (xfsread(ino, p, hdr.ex.a_syms))
353                 return;
354             p += hdr.ex.a_syms;
355             if (xfsread(ino, p, sizeof(int)))
356                 return;
357             x = *(uint32_t *)p;
358             p += sizeof(int);
359             x -= sizeof(int);
360             if (xfsread(ino, p, x))
361                 return;
362             p += x;
363         }
364     } else {
365         fs_off = hdr.eh.e_phoff;
366         for (j = i = 0; i < hdr.eh.e_phnum && j < 2; i++) {
367             if (xfsread(ino, ep + j, sizeof(ep[0])))
368                 return;
369             if (ep[j].p_type == PT_LOAD)
370                 j++;
371         }
372         for (i = 0; i < 2; i++) {
373             p = PTOV(ep[i].p_paddr & 0xffffff);
374             fs_off = ep[i].p_offset;
375             if (xfsread(ino, p, ep[i].p_filesz))
376                 return;
377         }
378         p += roundup2(ep[1].p_memsz, PAGE_SIZE);
379         bootinfo.bi_symtab = VTOP(p);
380         if (hdr.eh.e_shnum == hdr.eh.e_shstrndx + 3) {
381             fs_off = hdr.eh.e_shoff + sizeof(es[0]) *
382                 (hdr.eh.e_shstrndx + 1);
383             if (xfsread(ino, &es, sizeof(es)))
384                 return;
385             for (i = 0; i < 2; i++) {
386                 memcpy(p, &es[i].sh_size, sizeof(es[i].sh_size));
387                 p += sizeof(es[i].sh_size);
388                 fs_off = es[i].sh_offset;
389                 if (xfsread(ino, p, es[i].sh_size))
390                     return;
391                 p += es[i].sh_size;
392             }
393         }
394         addr = hdr.eh.e_entry & 0xffffff;
395     }
396     bootinfo.bi_esymtab = VTOP(p);
397     bootinfo.bi_kernelname = VTOP(kname);
398     bootinfo.bi_bios_dev = dsk.drive;
399     __exec((caddr_t)addr, RB_BOOTINFO | (opts & RBX_MASK),
400            MAKEBOOTDEV(dev_maj[dsk.type], dsk.slice, dsk.unit, dsk.part),
401            0, 0, 0, VTOP(&bootinfo));
402 }
403
404 static int
405 parse()
406 {
407     char *arg = cmd;
408     char *ep, *p, *q;
409     const char *cp;
410     unsigned int drv;
411     int c, i, j;
412
413     while ((c = *arg++)) {
414         if (c == ' ' || c == '\t' || c == '\n')
415             continue;
416         for (p = arg; *p && *p != '\n' && *p != ' ' && *p != '\t'; p++);
417         ep = p;
418         if (*p)
419             *p++ = 0;
420         if (c == '-') {
421             while ((c = *arg++)) {
422                 if (c == 'P') {
423                     if (*(uint8_t *)PTOV(0x496) & 0x10) {
424                         cp = "yes";
425                     } else {
426                         opts |= OPT_SET(RBX_DUAL) | OPT_SET(RBX_SERIAL);
427                         cp = "no";
428                     }
429                     printf("Keyboard: %s\n", cp);
430                     continue;
431                 } else if (c == 'S') {
432                     j = 0;
433                     while ((unsigned int)(i = *arg++ - '0') <= 9)
434                         j = j * 10 + i;
435                     if (j > 0 && i == -'0') {
436                         comspeed = j;
437                         break;
438                     }
439                     /* Fall through to error below ('S' not in optstr[]). */
440                 }
441                 for (i = 0; c != optstr[i]; i++)
442                     if (i == NOPT - 1)
443                         return -1;
444                 opts ^= OPT_SET(flags[i]);
445             }
446             ioctrl = OPT_CHECK(RBX_DUAL) ? (IO_SERIAL|IO_KEYBOARD) :
447                      OPT_CHECK(RBX_SERIAL) ? IO_SERIAL : IO_KEYBOARD;
448             if (ioctrl & IO_SERIAL)
449                 sio_init(115200 / comspeed);
450         } else {
451             for (q = arg--; *q && *q != '('; q++);
452             if (*q) {
453                 drv = -1;
454                 if (arg[1] == ':') {
455                     drv = *arg - '0';
456                     if (drv > 9)
457                         return (-1);
458                     arg += 2;
459                 }
460                 if (q - arg != 2)
461                     return -1;
462                 for (i = 0; arg[0] != dev_nm[i][0] ||
463                             arg[1] != dev_nm[i][1]; i++)
464                     if (i == NDEV - 1)
465                         return -1;
466                 dsk.type = i;
467                 arg += 3;
468                 dsk.unit = *arg - '0';
469                 if (arg[1] != ',' || dsk.unit > 9)
470                     return -1;
471                 arg += 2;
472                 dsk.slice = WHOLE_DISK_SLICE;
473                 if (arg[1] == ',') {
474                     dsk.slice = *arg - '0' + 1;
475                     if (dsk.slice > NDOSPART)
476                         return -1;
477                     arg += 2;
478                 }
479                 if (arg[1] != ')')
480                     return -1;
481                 dsk.part = *arg - 'a';
482                 if (dsk.part > 7)
483                     return (-1);
484                 arg += 2;
485                 if (drv == -1)
486                     drv = dsk.unit;
487                 dsk.drive = (dsk.type <= TYPE_MAXHARD
488                              ? DRV_HARD : 0) + drv;
489                 dsk_meta = 0;
490             }
491             if ((i = ep - arg)) {
492                 if ((size_t)i >= sizeof(kname))
493                     return -1;
494                 memcpy(kname, arg, i + 1);
495             }
496         }
497         arg = p;
498     }
499     return 0;
500 }
501
502 static int
503 dskread(void *buf, unsigned lba, unsigned nblk)
504 {
505     struct dos_partition *dp;
506     struct disklabel *d;
507     char *sec;
508     unsigned sl, i;
509
510     if (!dsk_meta) {
511         sec = dmadat->secbuf;
512         dsk.start = 0;
513         if (drvread(sec, DOSBBSECTOR, 1))
514             return -1;
515         dp = (void *)(sec + DOSPARTOFF);
516         sl = dsk.slice;
517         if (sl < BASE_SLICE) {
518             for (i = 0; i < NDOSPART; i++)
519                 if (dp[i].dp_typ == DOSPTYP_386BSD &&
520                     (dp[i].dp_flag & 0x80 || sl < BASE_SLICE)) {
521                     sl = BASE_SLICE + i;
522                     if (dp[i].dp_flag & 0x80 ||
523                         dsk.slice == COMPATIBILITY_SLICE)
524                         break;
525                 }
526             if (dsk.slice == WHOLE_DISK_SLICE)
527                 dsk.slice = sl;
528         }
529         if (sl != WHOLE_DISK_SLICE) {
530             if (sl != COMPATIBILITY_SLICE)
531                 dp += sl - BASE_SLICE;
532             if (dp->dp_typ != DOSPTYP_386BSD) {
533                 printf("Invalid %s\n", "slice");
534                 return -1;
535             }
536             dsk.start = dp->dp_start;
537         }
538         if (drvread(sec, dsk.start + LABELSECTOR, 1))
539                 return -1;
540         d = (void *)(sec + LABELOFFSET);
541         if (d->d_magic != DISKMAGIC || d->d_magic2 != DISKMAGIC) {
542             if (dsk.part != RAW_PART) {
543                 printf("Invalid %s\n", "label");
544                 return -1;
545             }
546         } else {
547             if (!dsk.init) {
548                 if (d->d_type == DTYPE_SCSI)
549                     dsk.type = TYPE_DA;
550                 dsk.init++;
551             }
552             if (dsk.part >= d->d_npartitions ||
553                 !d->d_partitions[dsk.part].p_size) {
554                 printf("Invalid %s\n", "partition");
555                 return -1;
556             }
557             dsk.start += d->d_partitions[dsk.part].p_offset;
558             dsk.start -= d->d_partitions[RAW_PART].p_offset;
559         }
560     }
561     return drvread(buf, dsk.start + lba, nblk);
562 }
563
564 static void
565 printf(const char *fmt,...)
566 {
567     va_list ap;
568     char buf[10];
569     char *s;
570     unsigned u;
571     int c;
572
573     va_start(ap, fmt);
574     while ((c = *fmt++)) {
575         if (c == '%') {
576             c = *fmt++;
577             switch (c) {
578             case 'c':
579                 putchar(va_arg(ap, int));
580                 continue;
581             case 's':
582                 for (s = va_arg(ap, char *); *s; s++)
583                     putchar(*s);
584                 continue;
585             case 'u':
586                 u = va_arg(ap, unsigned);
587                 s = buf;
588                 do
589                     *s++ = '0' + u % 10U;
590                 while (u /= 10U);
591                 while (--s >= buf)
592                     putchar(*s);
593                 continue;
594             }
595         }
596         putchar(c);
597     }
598     va_end(ap);
599     return;
600 }
601
602 static void
603 putchar(int c)
604 {
605     if (c == '\n')
606         xputc('\r');
607     xputc(c);
608 }
609
610 static int
611 drvread(void *buf, unsigned lba, unsigned nblk)
612 {
613     static unsigned c = 0x2d5c7c2f;
614
615     if (!OPT_CHECK(RBX_QUIET))
616         printf("%c\b", c = c << 8 | c >> 24);
617     v86.ctl = V86_ADDR | V86_CALLF | V86_FLAGS;
618     v86.addr = XREADORG;                /* call to xread in boot1 */
619     v86.es = VTOPSEG(buf);
620     v86.eax = lba;
621     v86.ebx = VTOPOFF(buf);
622     v86.ecx = lba >> 16;
623     v86.edx = nblk << 8 | dsk.drive;
624     v86int();
625     v86.ctl = V86_FLAGS;
626     if (V86_CY(v86.efl)) {
627         printf("error %u lba %u\n", v86.eax >> 8 & 0xff, lba);
628         return -1;
629     }
630     return 0;
631 }
632
633 static int
634 keyhit(unsigned ticks)
635 {
636     uint32_t t0, t1;
637
638     if (OPT_CHECK(RBX_NOINTR))
639         return 0;
640     t0 = 0;
641     for (;;) {
642         if (xgetc(1))
643             return 1;
644         t1 = *(uint32_t *)PTOV(0x46c);
645         if (!t0)
646             t0 = t1;
647         if (t1 < t0 || t1 >= t0 + ticks)
648             return 0;
649     }
650 }
651
652 static int
653 xputc(int c)
654 {
655     if (ioctrl & IO_KEYBOARD)
656         putc(c);
657     if (ioctrl & IO_SERIAL)
658         sio_putc(c);
659     return c;
660 }
661
662 static int
663 xgetc(int fn)
664 {
665     if (OPT_CHECK(RBX_NOINTR))
666         return 0;
667     for (;;) {
668         if (ioctrl & IO_KEYBOARD && getc(1))
669             return fn ? 1 : getc(0);
670         if (ioctrl & IO_SERIAL && sio_ischar())
671             return fn ? 1 : sio_getc();
672         if (fn)
673             return 0;
674     }
675 }
676
677 static int
678 getc(int fn)
679 {
680     v86.addr = 0x16;
681     v86.eax = fn << 8;
682     v86int();
683     return fn == 0 ? v86.eax & 0xff : !V86_ZR(v86.efl);
684 }