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