Perform some basic cleanups. Change some types over to C99 standard
[dragonfly.git] / sys / boot / pc32 / boot2 / boot2.c
CommitLineData
5ee58eed 1/*-
984263bc
MD
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.
5ee58eed
MD
14 *
15 * $FreeBSD: src/sys/boot/i386/boot2/boot2.c,v 1.64 2003/08/25 23:28:31 obrien Exp $
38e10dff 16 * $DragonFly: src/sys/boot/pc32/boot2/boot2.c,v 1.10 2004/06/27 08:00:46 dillon Exp $
984263bc 17 */
984263bc 18#include <sys/param.h>
984263bc 19#include <sys/disklabel.h>
5ee58eed
MD
20#include <sys/diskslice.h>
21#include <sys/diskmbr.h>
984263bc
MD
22#include <sys/dirent.h>
23#include <machine/bootinfo.h>
24#include <machine/elf.h>
25
984263bc
MD
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
5ee58eed
MD
35#define SECOND 18 /* Circa that many ticks in a second. */
36
984263bc
MD
37#define RBX_ASKNAME 0x0 /* -a */
38#define RBX_SINGLE 0x1 /* -s */
39#define RBX_DFLTROOT 0x5 /* -r */
40#define RBX_KDB 0x6 /* -d */
41#define RBX_CONFIG 0xa /* -c */
42#define RBX_VERBOSE 0xb /* -v */
43#define RBX_SERIAL 0xc /* -h */
44#define RBX_CDROM 0xd /* -C */
45#define RBX_GDB 0xf /* -g */
5ee58eed
MD
46#define RBX_MUTE 0x10 /* -m */
47#define RBX_PAUSE 0x12 /* -p */
48#define RBX_NOINTR 0x1c /* -n */
badbfe61 49#define RBX_VIDEO 0x1d /* -V */
984263bc 50#define RBX_PROBEKBD 0x1e /* -P */
5ee58eed 51/* 0x1f is reserved for the historical RB_BOOTINFO option */
984263bc 52
38e10dff 53#define RBF_MUTE (1 << RBX_MUTE)
badbfe61
MD
54#define RBF_SERIAL (1 << RBX_SERIAL)
55#define RBF_VIDEO (1 << RBX_VIDEO)
56
50a1695f 57/* pass: -a, -s, -r, -d, -c, -v, -h, -C, -g, -m, -p, -V */
5ee58eed 58#define RBX_MASK 0x2005ffff
984263bc
MD
59
60#define PATH_CONFIG "/boot.config"
61#define PATH_BOOT3 "/boot/loader"
62#define PATH_KERNEL "/kernel"
63
64#define ARGS 0x900
5ee58eed 65#define NDEV 3
984263bc
MD
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
5ee58eed
MD
75#define TYPE_DA 1
76#define TYPE_MAXHARD TYPE_DA
77#define TYPE_FD 2
984263bc 78
badbfe61
MD
79#define NOPT 12
80
984263bc
MD
81extern uint32_t _end;
82
badbfe61 83static const char optstr[NOPT] = { "VhaCgmnPprsv" };
984263bc 84static const unsigned char flags[NOPT] = {
badbfe61 85 RBX_VIDEO,
984263bc
MD
86 RBX_SERIAL,
87 RBX_ASKNAME,
88 RBX_CDROM,
984263bc 89 RBX_GDB,
5ee58eed 90 RBX_MUTE,
984263bc
MD
91 RBX_NOINTR,
92 RBX_PROBEKBD,
5ee58eed 93 RBX_PAUSE,
984263bc
MD
94 RBX_DFLTROOT,
95 RBX_SINGLE,
96 RBX_VERBOSE
97};
98
5ee58eed
MD
99static const char *const dev_nm[NDEV] = {"ad", "da", "fd"};
100static const unsigned char dev_maj[NDEV] = {30, 4, 2};
984263bc
MD
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;
984263bc
MD
110} dsk;
111static char cmd[512];
112static char kname[1024];
113static uint32_t opts;
114static struct bootinfo bootinfo;
984263bc
MD
115
116void exit(int);
5ee58eed
MD
117static void load(void);
118static int parse(void);
984263bc 119static int xfsread(ino_t, void *, size_t);
984263bc 120static int dskread(void *, unsigned, unsigned);
5ee58eed
MD
121static void printf(const char *,...);
122static void putchar(int);
123static uint32_t memsize(void);
984263bc
MD
124static int drvread(void *, unsigned, unsigned);
125static int keyhit(unsigned);
50a1695f 126static void xputc(int);
984263bc
MD
127static int xgetc(int);
128static int getc(int);
129
5ee58eed
MD
130static void
131memcpy(void *d, const void *s, int len)
984263bc 132{
5ee58eed
MD
133 char *dd = d;
134 const char *ss = s;
984263bc 135
5ee58eed
MD
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
984263bc
MD
147}
148
149static inline int
150strcmp(const char *s1, const char *s2)
151{
152 for (; *s1 == *s2 && *s1; s1++, s2++);
5ee58eed 153 return (unsigned char)*s1 - (unsigned char)*s2;
984263bc
MD
154}
155
5ee58eed
MD
156#include "ufsread.c"
157
158static int
159xfsread(ino_t inode, void *buf, size_t nbyte)
984263bc 160{
5ee58eed
MD
161 if ((size_t)fsread(inode, buf, nbyte) != nbyte) {
162 printf("Invalid %s\n", "format");
163 return -1;
164 }
984263bc
MD
165 return 0;
166}
167
5ee58eed
MD
168static inline uint32_t
169memsize(void)
984263bc 170{
5ee58eed
MD
171 v86.addr = MEM_EXT;
172 v86.eax = 0x8800;
173 v86int();
174 return v86.eax;
984263bc
MD
175}
176
177static inline void
5ee58eed 178getstr(void)
984263bc
MD
179{
180 char *s;
181 int c;
182
5ee58eed
MD
183 s = cmd;
184 for (;;) {
185 switch (c = xgetc(0)) {
984263bc
MD
186 case 0:
187 break;
984263bc 188 case '\177':
5ee58eed
MD
189 case '\b':
190 if (s > cmd) {
984263bc 191 s--;
5ee58eed
MD
192 printf("\b \b");
193 }
984263bc
MD
194 break;
195 case '\n':
5ee58eed 196 case '\r':
984263bc 197 *s = 0;
5ee58eed 198 return;
984263bc 199 default:
5ee58eed 200 if (s - cmd < sizeof(cmd) - 1)
984263bc 201 *s++ = c;
984263bc 202 putchar(c);
5ee58eed
MD
203 }
204 }
984263bc
MD
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{
5ee58eed
MD
219 int autoboot;
220 ino_t ino;
984263bc 221
5ee58eed 222 dmadat = (void *)(roundup2(__base + (int32_t)&_end, 0x10000) - __base);
984263bc
MD
223 v86.ctl = V86_FLAGS;
224 dsk.drive = *(uint8_t *)PTOV(ARGS);
225 dsk.type = dsk.drive & DRV_HARD ? TYPE_AD : TYPE_FD;
226 dsk.unit = dsk.drive & DRV_MASK;
227 dsk.slice = *(uint8_t *)PTOV(ARGS + 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 */
5ee58eed 231 bootinfo.bi_extmem = memsize();
984263bc 232 bootinfo.bi_memsizes_valid++;
5ee58eed
MD
233
234 /* Process configuration file */
235
236 autoboot = 1;
237
238 if ((ino = lookup(PATH_CONFIG)))
239 fsread(ino, cmd, sizeof(cmd));
240
badbfe61 241 if (cmd[0]) {
984263bc 242 printf("%s: %s", PATH_CONFIG, cmd);
5ee58eed 243 if (parse())
984263bc 244 autoboot = 0;
5ee58eed 245 /* Do not process this command twice */
984263bc
MD
246 *cmd = 0;
247 }
5ee58eed 248
badbfe61 249 /*
38e10dff
MD
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).
badbfe61 253 */
38e10dff 254 if ((opts & (RBF_MUTE|RBF_SERIAL|RBF_VIDEO)) == 0)
badbfe61 255 opts |= RBF_SERIAL|RBF_VIDEO;
38e10dff
MD
256 if (opts & RBF_SERIAL) {
257 if (sio_init())
258 opts = RBF_VIDEO;
259 }
badbfe61
MD
260
261
5ee58eed
MD
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
984263bc 267 if (autoboot && !*kname) {
5ee58eed
MD
268 memcpy(kname, PATH_BOOT3, sizeof(PATH_BOOT3));
269 if (!keyhit(3*SECOND)) {
270 load();
984263bc 271 memcpy(kname, PATH_KERNEL, sizeof(PATH_KERNEL));
5ee58eed 272 }
984263bc 273 }
5ee58eed
MD
274
275 /* Present the user with the boot2 prompt. */
276
984263bc 277 for (;;) {
38e10dff
MD
278 printf("\nDragonFly boot\n"
279 "%u:%s(%u,%c)%s: ",
984263bc
MD
280 dsk.drive & DRV_MASK, dev_nm[dsk.type], dsk.unit,
281 'a' + dsk.part, kname);
5ee58eed
MD
282 if (!autoboot || keyhit(5*SECOND))
283 getstr();
984263bc
MD
284 else
285 putchar('\n');
286 autoboot = 0;
badbfe61 287 if (cmd[0] == 0 || parse())
5ee58eed 288 putchar('\a');
984263bc 289 else
5ee58eed 290 load();
984263bc
MD
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
5ee58eed 301load(void)
984263bc
MD
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
5ee58eed 314 if (!(ino = lookup(kname))) {
984263bc 315 if (!ls)
5ee58eed 316 printf("No %s\n", kname);
984263bc
MD
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);
5ee58eed 388 bootinfo.bi_kernelname = VTOP(kname);
984263bc 389 bootinfo.bi_bios_dev = dsk.drive;
5ee58eed 390 __exec((caddr_t)addr, opts & RBX_MASK,
984263bc
MD
391 MAKEBOOTDEV(dev_maj[dsk.type], 0, dsk.slice, dsk.unit, dsk.part),
392 0, 0, 0, VTOP(&bootinfo));
393}
394
395static int
5ee58eed 396parse()
984263bc 397{
5ee58eed 398 char *arg = cmd;
984263bc 399 char *p, *q;
5ee58eed
MD
400 unsigned int drv;
401 int c, i;
984263bc
MD
402
403 while ((c = *arg++)) {
404 if (c == ' ' || c == '\t' || c == '\n')
405 continue;
badbfe61
MD
406 for (p = arg; *p && *p != '\n' && *p != ' ' && *p != '\t'; p++)
407 ;
984263bc
MD
408 if (*p)
409 *p++ = 0;
410 if (c == '-') {
411 while ((c = *arg++)) {
badbfe61
MD
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 */
984263bc 420 }
badbfe61 421 if (opts & (1 << RBX_PROBEKBD)) {
984263bc 422 i = *(uint8_t *)PTOV(0x496) & 0x10;
2d7f6790
MD
423 if (!i) {
424 printf("NO KB\n");
badbfe61 425 opts |= RBF_VIDEO | RBF_SERIAL;
2d7f6790 426 }
984263bc
MD
427 opts &= ~(1 << RBX_PROBEKBD);
428 }
984263bc
MD
429 } else {
430 for (q = arg--; *q && *q != '('; q++);
431 if (*q) {
432 drv = -1;
433 if (arg[1] == ':') {
984263bc 434 drv = *arg - '0';
5ee58eed
MD
435 if (drv > 9)
436 return (-1);
984263bc
MD
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;
984263bc 447 dsk.unit = *arg - '0';
5ee58eed
MD
448 if (arg[1] != ',' || dsk.unit > 9)
449 return -1;
984263bc
MD
450 arg += 2;
451 dsk.slice = WHOLE_DISK_SLICE;
452 if (arg[1] == ',') {
5ee58eed
MD
453 dsk.slice = *arg - '0' + 1;
454 if (dsk.slice > NDOSPART)
984263bc 455 return -1;
984263bc
MD
456 arg += 2;
457 }
5ee58eed 458 if (arg[1] != ')')
984263bc
MD
459 return -1;
460 dsk.part = *arg - 'a';
5ee58eed
MD
461 if (dsk.part > 7)
462 return (-1);
984263bc
MD
463 arg += 2;
464 if (drv == -1)
465 drv = dsk.unit;
5ee58eed
MD
466 dsk.drive = (dsk.type <= TYPE_MAXHARD
467 ? DRV_HARD : 0) + drv;
468 dsk_meta = 0;
984263bc
MD
469 }
470 if ((i = p - arg - !*(p - 1))) {
5ee58eed 471 if ((size_t)i >= sizeof(kname))
984263bc
MD
472 return -1;
473 memcpy(kname, arg, i + 1);
474 }
475 }
476 arg = p;
477 }
478 return 0;
479}
480
984263bc
MD
481static int
482dskread(void *buf, unsigned lba, unsigned nblk)
483{
984263bc
MD
484 struct dos_partition *dp;
485 struct disklabel *d;
5ee58eed 486 char *sec;
984263bc
MD
487 unsigned sl, i;
488
5ee58eed
MD
489 if (!dsk_meta) {
490 sec = dmadat->secbuf;
984263bc
MD
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 }
5ee58eed
MD
536 dsk.start += d->d_partitions[dsk.part].p_offset;
537 dsk.start -= d->d_partitions[RAW_PART].p_offset;
984263bc
MD
538 }
539 }
540 return drvread(buf, dsk.start + lba, nblk);
541}
542
5ee58eed 543static void
984263bc
MD
544printf(const char *fmt,...)
545{
5ee58eed 546 va_list ap;
984263bc
MD
547 char buf[10];
548 char *s;
5ee58eed 549 unsigned u;
984263bc
MD
550 int c;
551
5ee58eed 552 va_start(ap, fmt);
984263bc
MD
553 while ((c = *fmt++)) {
554 if (c == '%') {
555 c = *fmt++;
556 switch (c) {
557 case 'c':
5ee58eed 558 putchar(va_arg(ap, int));
984263bc
MD
559 continue;
560 case 's':
5ee58eed 561 for (s = va_arg(ap, char *); *s; s++)
984263bc
MD
562 putchar(*s);
563 continue;
564 case 'u':
5ee58eed 565 u = va_arg(ap, unsigned);
984263bc
MD
566 s = buf;
567 do
5ee58eed
MD
568 *s++ = '0' + u % 10U;
569 while (u /= 10U);
984263bc
MD
570 while (--s >= buf)
571 putchar(*s);
572 continue;
573 }
574 }
575 putchar(c);
576 }
5ee58eed
MD
577 va_end(ap);
578 return;
984263bc
MD
579}
580
5ee58eed 581static void
984263bc
MD
582putchar(int c)
583{
584 if (c == '\n')
585 xputc('\r');
5ee58eed 586 xputc(c);
984263bc
MD
587}
588
589static int
590drvread(void *buf, unsigned lba, unsigned nblk)
591{
50a1695f 592 static unsigned c = 0x2d5c7c2f; /* twiddle */
984263bc 593
50a1695f
MD
594 c = (c << 8) | (c >> 24);
595 xputc(c);
596 xputc('\b');
984263bc
MD
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)) {
5ee58eed 607 printf("error %u lba %u\n", v86.eax >> 8 & 0xff, lba);
984263bc
MD
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
50a1695f 632static void
984263bc
MD
633xputc(int c)
634{
badbfe61 635 if (opts & RBF_VIDEO)
984263bc 636 putc(c);
badbfe61 637 if (opts & RBF_SERIAL)
984263bc 638 sio_putc(c);
984263bc
MD
639}
640
641static int
642xgetc(int fn)
643{
644 if (opts & 1 << RBX_NOINTR)
645 return 0;
646 for (;;) {
badbfe61 647 if ((opts & RBF_VIDEO) && getc(1))
984263bc 648 return fn ? 1 : getc(0);
badbfe61 649 if ((opts & RBF_SERIAL) && sio_ischar())
984263bc
MD
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}