| Commit | Line | Data |
|---|---|---|
| cacaceec MD |
1 | /* |
| 2 | * Copyright (c) 2003,2004 The DragonFly Project. All rights reserved. | |
| 3 | * | |
| 4 | * This code is derived from software contributed to The DragonFly Project | |
| 5 | * by Matthew Dillon <dillon@backplane.com> | |
| 6 | * | |
| 7 | * Redistribution and use in source and binary forms, with or without | |
| 8 | * modification, are permitted provided that the following conditions | |
| 9 | * are met: | |
| 10 | * | |
| 11 | * 1. Redistributions of source code must retain the above copyright | |
| 12 | * notice, this list of conditions and the following disclaimer. | |
| 13 | * 2. Redistributions in binary form must reproduce the above copyright | |
| 14 | * notice, this list of conditions and the following disclaimer in | |
| 15 | * the documentation and/or other materials provided with the | |
| 16 | * distribution. | |
| 17 | * 3. Neither the name of The DragonFly Project nor the names of its | |
| 18 | * contributors may be used to endorse or promote products derived | |
| 19 | * from this software without specific, prior written permission. | |
| 20 | * | |
| 21 | * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS | |
| 22 | * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT | |
| 23 | * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS | |
| 24 | * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE | |
| 25 | * COPYRIGHT HOLDERS OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, | |
| 26 | * INCIDENTAL, SPECIAL, EXEMPLARY OR CONSEQUENTIAL DAMAGES (INCLUDING, | |
| 27 | * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; | |
| 28 | * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED | |
| 29 | * AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, | |
| 30 | * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT | |
| 31 | * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF | |
| 32 | * SUCH DAMAGE. | |
| 33 | * | |
| 984263bc MD |
34 | * Copyright (c) 1998 Robert Nordier |
| 35 | * All rights reserved. | |
| 36 | * | |
| 37 | * Redistribution and use in source and binary forms are freely | |
| 38 | * permitted provided that the above copyright notice and this | |
| 39 | * paragraph and the following disclaimer are duplicated in all | |
| 40 | * such forms. | |
| 41 | * | |
| 42 | * This software is provided "AS IS" and without any express or | |
| 43 | * implied warranties, including, without limitation, the implied | |
| 44 | * warranties of merchantability and fitness for a particular | |
| 45 | * purpose. | |
| 5ee58eed MD |
46 | * |
| 47 | * $FreeBSD: src/sys/boot/i386/boot2/boot2.c,v 1.64 2003/08/25 23:28:31 obrien Exp $ | |
| 7580e818 | 48 | * $DragonFly: src/sys/boot/pc32/boot2/boot2.c,v 1.18 2008/09/13 11:45:45 corecode Exp $ |
| 984263bc | 49 | */ |
| 7580e818 | 50 | |
| 79b4bbd6 | 51 | #define AOUT_H_FORCE32 |
| 984263bc | 52 | #include <sys/param.h> |
| 6080181b SS |
53 | #ifdef DISKLABEL64 |
| 54 | #include <sys/disklabel64.h> | |
| 55 | #else | |
| 2b961883 | 56 | #include <sys/disklabel32.h> |
| 6080181b | 57 | #endif |
| 5ee58eed MD |
58 | #include <sys/diskslice.h> |
| 59 | #include <sys/diskmbr.h> | |
| ba0cc1ab | 60 | #include <sys/dtype.h> |
| 984263bc MD |
61 | #include <sys/dirent.h> |
| 62 | #include <machine/bootinfo.h> | |
| 63 | #include <machine/elf.h> | |
| 64 | ||
| 984263bc MD |
65 | #include <stdarg.h> |
| 66 | ||
| 67 | #include <a.out.h> | |
| 68 | ||
| 69 | #include <btxv86.h> | |
| 70 | ||
| 6080181b SS |
71 | #ifdef DISKLABEL64 |
| 72 | #include "boot2_64.h" | |
| 73 | #else | |
| 74 | #include "boot2_32.h" | |
| 75 | #endif | |
| d64b2e33 | 76 | #include "boot2.h" |
| 984263bc | 77 | #include "lib.h" |
| 409cbc03 | 78 | #include "../bootasm.h" |
| 984263bc | 79 | |
| 5ee58eed MD |
80 | #define SECOND 18 /* Circa that many ticks in a second. */ |
| 81 | ||
| 984263bc MD |
82 | #define RBX_ASKNAME 0x0 /* -a */ |
| 83 | #define RBX_SINGLE 0x1 /* -s */ | |
| 84 | #define RBX_DFLTROOT 0x5 /* -r */ | |
| 85 | #define RBX_KDB 0x6 /* -d */ | |
| 86 | #define RBX_CONFIG 0xa /* -c */ | |
| 87 | #define RBX_VERBOSE 0xb /* -v */ | |
| 88 | #define RBX_SERIAL 0xc /* -h */ | |
| 89 | #define RBX_CDROM 0xd /* -C */ | |
| 90 | #define RBX_GDB 0xf /* -g */ | |
| 5ee58eed MD |
91 | #define RBX_MUTE 0x10 /* -m */ |
| 92 | #define RBX_PAUSE 0x12 /* -p */ | |
| 93 | #define RBX_NOINTR 0x1c /* -n */ | |
| badbfe61 | 94 | #define RBX_VIDEO 0x1d /* -V */ |
| 984263bc | 95 | #define RBX_PROBEKBD 0x1e /* -P */ |
| 5ee58eed | 96 | /* 0x1f is reserved for the historical RB_BOOTINFO option */ |
| 984263bc | 97 | |
| 38e10dff | 98 | #define RBF_MUTE (1 << RBX_MUTE) |
| badbfe61 MD |
99 | #define RBF_SERIAL (1 << RBX_SERIAL) |
| 100 | #define RBF_VIDEO (1 << RBX_VIDEO) | |
| 101 | ||
| 50a1695f | 102 | /* pass: -a, -s, -r, -d, -c, -v, -h, -C, -g, -m, -p, -V */ |
| 5ee58eed | 103 | #define RBX_MASK 0x2005ffff |
| 984263bc MD |
104 | |
| 105 | #define PATH_CONFIG "/boot.config" | |
| 79dc5fc6 MD |
106 | #define PATH_BOOT3 "/boot/loader" /* /boot in root */ |
| 107 | #define PATH_BOOT3_ALT "/loader" /* /boot partition */ | |
| 984263bc MD |
108 | #define PATH_KERNEL "/kernel" |
| 109 | ||
| 5ee58eed | 110 | #define NDEV 3 |
| 984263bc MD |
111 | #define MEM_BASE 0x12 |
| 112 | #define MEM_EXT 0x15 | |
| 113 | #define V86_CY(x) ((x) & 1) | |
| 114 | #define V86_ZR(x) ((x) & 0x40) | |
| 115 | ||
| 116 | #define DRV_HARD 0x80 | |
| 117 | #define DRV_MASK 0x7f | |
| 118 | ||
| 119 | #define TYPE_AD 0 | |
| 5ee58eed MD |
120 | #define TYPE_DA 1 |
| 121 | #define TYPE_MAXHARD TYPE_DA | |
| 122 | #define TYPE_FD 2 | |
| 984263bc | 123 | |
| badbfe61 MD |
124 | #define NOPT 12 |
| 125 | ||
| 4e06dda7 MD |
126 | #define INVALID_S "Bad %s\n" |
| 127 | ||
| 984263bc MD |
128 | extern uint32_t _end; |
| 129 | ||
| badbfe61 | 130 | static const char optstr[NOPT] = { "VhaCgmnPprsv" }; |
| 984263bc | 131 | static const unsigned char flags[NOPT] = { |
| badbfe61 | 132 | RBX_VIDEO, |
| 984263bc MD |
133 | RBX_SERIAL, |
| 134 | RBX_ASKNAME, | |
| 135 | RBX_CDROM, | |
| 984263bc | 136 | RBX_GDB, |
| 5ee58eed | 137 | RBX_MUTE, |
| 984263bc MD |
138 | RBX_NOINTR, |
| 139 | RBX_PROBEKBD, | |
| 5ee58eed | 140 | RBX_PAUSE, |
| 984263bc MD |
141 | RBX_DFLTROOT, |
| 142 | RBX_SINGLE, | |
| 143 | RBX_VERBOSE | |
| 144 | }; | |
| 145 | ||
| 5ee58eed MD |
146 | static const char *const dev_nm[NDEV] = {"ad", "da", "fd"}; |
| 147 | static const unsigned char dev_maj[NDEV] = {30, 4, 2}; | |
| 984263bc MD |
148 | |
| 149 | static struct dsk { | |
| 150 | unsigned drive; | |
| 151 | unsigned type; | |
| 152 | unsigned unit; | |
| 153 | unsigned slice; | |
| 154 | unsigned part; | |
| 155 | unsigned start; | |
| 156 | int init; | |
| 984263bc | 157 | } dsk; |
| d64b2e33 | 158 | |
| 984263bc MD |
159 | static char cmd[512]; |
| 160 | static char kname[1024]; | |
| d64b2e33 | 161 | static uint32_t opts = RBF_VIDEO; |
| 984263bc | 162 | static struct bootinfo bootinfo; |
| 984263bc | 163 | |
| d64b2e33 MD |
164 | /* |
| 165 | * boot2 encapsulated ABI elements provided to *fsread.c | |
| 166 | * | |
| 167 | * NOTE: boot2_dmadat is extended by per-filesystem APIs | |
| 168 | */ | |
| 169 | uint32_t fs_off; | |
| 170 | int ls; | |
| 171 | struct boot2_dmadat *boot2_dmadat; | |
| 7580e818 | 172 | |
| 984263bc | 173 | void exit(int); |
| 5ee58eed MD |
174 | static void load(void); |
| 175 | static int parse(void); | |
| d64b2e33 MD |
176 | static int dskprobe(void); |
| 177 | static int xfsread(boot2_ino_t, void *, size_t); | |
| 5ee58eed | 178 | static uint32_t memsize(void); |
| 984263bc MD |
179 | static int drvread(void *, unsigned, unsigned); |
| 180 | static int keyhit(unsigned); | |
| 50a1695f | 181 | static void xputc(int); |
| 984263bc MD |
182 | static int xgetc(int); |
| 183 | static int getc(int); | |
| 184 | ||
| d64b2e33 | 185 | void |
| 5ee58eed | 186 | memcpy(void *d, const void *s, int len) |
| 984263bc | 187 | { |
| 5ee58eed MD |
188 | char *dd = d; |
| 189 | const char *ss = s; | |
| 984263bc | 190 | |
| 5ee58eed MD |
191 | #if 0 |
| 192 | if (dd < ss) { | |
| 193 | while (--len >= 0) | |
| 194 | *dd++ = *ss++; | |
| 195 | } else { | |
| 196 | #endif | |
| 197 | while (--len >= 0) | |
| 198 | dd[len] = ss[len]; | |
| 199 | #if 0 | |
| 200 | } | |
| 201 | #endif | |
| 984263bc MD |
202 | } |
| 203 | ||
| d64b2e33 | 204 | int |
| 984263bc MD |
205 | strcmp(const char *s1, const char *s2) |
| 206 | { | |
| d64b2e33 MD |
207 | for (; *s1 == *s2 && *s1; s1++, s2++) |
| 208 | ; | |
| 209 | return ((int)((unsigned char)*s1 - (unsigned char)*s2)); | |
| 984263bc MD |
210 | } |
| 211 | ||
| d64b2e33 MD |
212 | #if defined(UFS) && defined(HAMMERFS) |
| 213 | ||
| 214 | const struct boot2_fsapi *fsapi; | |
| 215 | ||
| 216 | #elif defined(UFS) | |
| 217 | ||
| 218 | #define fsapi (&boot2_ufs_api) | |
| 219 | ||
| 220 | #elif defined(HAMMERFS) | |
| 221 | ||
| 222 | #define fsapi (&boot2_hammer_api) | |
| 223 | ||
| 7580e818 | 224 | #endif |
| 5ee58eed MD |
225 | |
| 226 | static int | |
| d64b2e33 | 227 | xfsread(boot2_ino_t inode, void *buf, size_t nbyte) |
| 984263bc | 228 | { |
| d64b2e33 | 229 | if ((size_t)fsapi->fsread(inode, buf, nbyte) != nbyte) { |
| 4e06dda7 | 230 | printf(INVALID_S, "format"); |
| 5ee58eed MD |
231 | return -1; |
| 232 | } | |
| 984263bc MD |
233 | return 0; |
| 234 | } | |
| 235 | ||
| 5ee58eed MD |
236 | static inline uint32_t |
| 237 | memsize(void) | |
| 984263bc | 238 | { |
| 5ee58eed MD |
239 | v86.addr = MEM_EXT; |
| 240 | v86.eax = 0x8800; | |
| 241 | v86int(); | |
| 242 | return v86.eax; | |
| 984263bc MD |
243 | } |
| 244 | ||
| 245 | static inline void | |
| 5ee58eed | 246 | getstr(void) |
| 984263bc MD |
247 | { |
| 248 | char *s; | |
| 249 | int c; | |
| 250 | ||
| 5ee58eed MD |
251 | s = cmd; |
| 252 | for (;;) { | |
| 253 | switch (c = xgetc(0)) { | |
| 984263bc MD |
254 | case 0: |
| 255 | break; | |
| 984263bc | 256 | case '\177': |
| 5ee58eed MD |
257 | case '\b': |
| 258 | if (s > cmd) { | |
| 984263bc | 259 | s--; |
| 5ee58eed MD |
260 | printf("\b \b"); |
| 261 | } | |
| 984263bc MD |
262 | break; |
| 263 | case '\n': | |
| 5ee58eed | 264 | case '\r': |
| 984263bc | 265 | *s = 0; |
| 5ee58eed | 266 | return; |
| 984263bc | 267 | default: |
| 5ee58eed | 268 | if (s - cmd < sizeof(cmd) - 1) |
| 984263bc | 269 | *s++ = c; |
| 984263bc | 270 | putchar(c); |
| 5ee58eed MD |
271 | } |
| 272 | } | |
| 984263bc MD |
273 | } |
| 274 | ||
| 275 | static inline void | |
| 276 | putc(int c) | |
| 277 | { | |
| 278 | v86.addr = 0x10; | |
| 279 | v86.eax = 0xe00 | (c & 0xff); | |
| 280 | v86.ebx = 0x7; | |
| 281 | v86int(); | |
| 282 | } | |
| 283 | ||
| 284 | int | |
| 285 | main(void) | |
| 286 | { | |
| 5ee58eed | 287 | int autoboot; |
| d64b2e33 | 288 | boot2_ino_t ino; |
| 984263bc | 289 | |
| d64b2e33 MD |
290 | boot2_dmadat = |
| 291 | (void *)(roundup2(__base + (int32_t)&_end, 0x10000) - __base); | |
| 984263bc | 292 | v86.ctl = V86_FLAGS; |
| cacaceec | 293 | dsk.drive = *(uint8_t *)PTOV(MEM_BTX_USR_ARG); |
| 984263bc MD |
294 | dsk.type = dsk.drive & DRV_HARD ? TYPE_AD : TYPE_FD; |
| 295 | dsk.unit = dsk.drive & DRV_MASK; | |
| cacaceec | 296 | dsk.slice = *(uint8_t *)PTOV(MEM_BTX_USR_ARG + 1) + 1; |
| 984263bc MD |
297 | bootinfo.bi_version = BOOTINFO_VERSION; |
| 298 | bootinfo.bi_size = sizeof(bootinfo); | |
| 299 | bootinfo.bi_basemem = 0; /* XXX will be filled by loader or kernel */ | |
| 5ee58eed | 300 | bootinfo.bi_extmem = memsize(); |
| 984263bc | 301 | bootinfo.bi_memsizes_valid++; |
| 5ee58eed | 302 | |
| 5ee58eed MD |
303 | autoboot = 1; |
| 304 | ||
| d64b2e33 MD |
305 | /* |
| 306 | * Probe the default disk and process the configuration file if | |
| 307 | * successful. | |
| 308 | */ | |
| 309 | if (dskprobe() == 0) { | |
| 310 | if ((ino = fsapi->fslookup(PATH_CONFIG))) | |
| 311 | fsapi->fsread(ino, cmd, sizeof(cmd)); | |
| 312 | } | |
| 5ee58eed | 313 | |
| d64b2e33 MD |
314 | /* |
| 315 | * Parse config file if present. parse() will re-probe if necessary. | |
| 316 | */ | |
| badbfe61 | 317 | if (cmd[0]) { |
| 984263bc | 318 | printf("%s: %s", PATH_CONFIG, cmd); |
| 5ee58eed | 319 | if (parse()) |
| 984263bc | 320 | autoboot = 0; |
| 5ee58eed | 321 | /* Do not process this command twice */ |
| 984263bc MD |
322 | *cmd = 0; |
| 323 | } | |
| 5ee58eed MD |
324 | |
| 325 | /* | |
| 38e10dff MD |
326 | * Setup our (serial) console after processing the config file. If |
| 327 | * the initialization fails, don't try to use the serial port. This | |
| 328 | * can happen if the serial port is unmaped (happens on new laptops a lot). | |
| badbfe61 | 329 | */ |
| 38e10dff | 330 | if ((opts & (RBF_MUTE|RBF_SERIAL|RBF_VIDEO)) == 0) |
| badbfe61 | 331 | opts |= RBF_SERIAL|RBF_VIDEO; |
| 38e10dff MD |
332 | if (opts & RBF_SERIAL) { |
| 333 | if (sio_init()) | |
| 334 | opts = RBF_VIDEO; | |
| 335 | } | |
| badbfe61 MD |
336 | |
| 337 | ||
| 338 | /* | |
| 5ee58eed MD |
339 | * Try to exec stage 3 boot loader. If interrupted by a keypress, |
| 340 | * or in case of failure, try to load a kernel directly instead. | |
| 79dc5fc6 MD |
341 | * |
| 342 | * We have to try boot /boot/loader and /loader to support booting | |
| 343 | * from a /boot partition instead of a root partition. | |
| 5ee58eed | 344 | */ |
| 984263bc | 345 | if (autoboot && !*kname) { |
| 5ee58eed MD |
346 | memcpy(kname, PATH_BOOT3, sizeof(PATH_BOOT3)); |
| 347 | if (!keyhit(3*SECOND)) { | |
| 348 | load(); | |
| 79dc5fc6 MD |
349 | memcpy(kname, PATH_BOOT3_ALT, sizeof(PATH_BOOT3_ALT)); |
| 350 | load(); | |
| 984263bc | 351 | memcpy(kname, PATH_KERNEL, sizeof(PATH_KERNEL)); |
| 5ee58eed | 352 | } |
| 984263bc | 353 | } |
| 5ee58eed MD |
354 | |
| 355 | /* Present the user with the boot2 prompt. */ | |
| 356 | ||
| 984263bc | 357 | for (;;) { |
| 38e10dff MD |
358 | printf("\nDragonFly boot\n" |
| 359 | "%u:%s(%u,%c)%s: ", | |
| 984263bc MD |
360 | dsk.drive & DRV_MASK, dev_nm[dsk.type], dsk.unit, |
| 361 | 'a' + dsk.part, kname); | |
| 5ee58eed MD |
362 | if (!autoboot || keyhit(5*SECOND)) |
| 363 | getstr(); | |
| 984263bc MD |
364 | else |
| 365 | putchar('\n'); | |
| 366 | autoboot = 0; | |
| d561b492 | 367 | if (parse()) |
| 5ee58eed | 368 | putchar('\a'); |
| 984263bc | 369 | else |
| 5ee58eed | 370 | load(); |
| 984263bc MD |
371 | } |
| 372 | } | |
| 373 | ||
| 374 | /* XXX - Needed for btxld to link the boot2 binary; do not remove. */ | |
| 375 | void | |
| 376 | exit(int x) | |
| 377 | { | |
| 378 | } | |
| 379 | ||
| 380 | static void | |
| 5ee58eed | 381 | load(void) |
| 984263bc MD |
382 | { |
| 383 | union { | |
| 384 | struct exec ex; | |
| 385 | Elf32_Ehdr eh; | |
| 386 | } hdr; | |
| 387 | Elf32_Phdr ep[2]; | |
| 388 | Elf32_Shdr es[2]; | |
| 389 | caddr_t p; | |
| d64b2e33 | 390 | boot2_ino_t ino; |
| 984263bc MD |
391 | uint32_t addr, x; |
| 392 | int fmt, i, j; | |
| 393 | ||
| d64b2e33 | 394 | if (!(ino = fsapi->fslookup(kname))) { |
| 984263bc | 395 | if (!ls) |
| 5ee58eed | 396 | printf("No %s\n", kname); |
| 984263bc MD |
397 | return; |
| 398 | } | |
| 399 | if (xfsread(ino, &hdr, sizeof(hdr))) | |
| 400 | return; | |
| 401 | if (N_GETMAGIC(hdr.ex) == ZMAGIC) | |
| 402 | fmt = 0; | |
| 403 | else if (IS_ELF(hdr.eh)) | |
| 404 | fmt = 1; | |
| 405 | else { | |
| 4e06dda7 | 406 | printf(INVALID_S, "format"); |
| 984263bc MD |
407 | return; |
| 408 | } | |
| 409 | if (fmt == 0) { | |
| 410 | addr = hdr.ex.a_entry & 0xffffff; | |
| 411 | p = PTOV(addr); | |
| 412 | fs_off = PAGE_SIZE; | |
| 413 | if (xfsread(ino, p, hdr.ex.a_text)) | |
| 414 | return; | |
| 415 | p += roundup2(hdr.ex.a_text, PAGE_SIZE); | |
| 416 | if (xfsread(ino, p, hdr.ex.a_data)) | |
| 417 | return; | |
| 418 | p += hdr.ex.a_data + roundup2(hdr.ex.a_bss, PAGE_SIZE); | |
| 419 | bootinfo.bi_symtab = VTOP(p); | |
| 420 | memcpy(p, &hdr.ex.a_syms, sizeof(hdr.ex.a_syms)); | |
| 421 | p += sizeof(hdr.ex.a_syms); | |
| 422 | if (hdr.ex.a_syms) { | |
| 423 | if (xfsread(ino, p, hdr.ex.a_syms)) | |
| 424 | return; | |
| 425 | p += hdr.ex.a_syms; | |
| 426 | if (xfsread(ino, p, sizeof(int))) | |
| 427 | return; | |
| 428 | x = *(uint32_t *)p; | |
| 429 | p += sizeof(int); | |
| 430 | x -= sizeof(int); | |
| 431 | if (xfsread(ino, p, x)) | |
| 432 | return; | |
| 433 | p += x; | |
| 434 | } | |
| 435 | } else { | |
| 436 | fs_off = hdr.eh.e_phoff; | |
| 437 | for (j = i = 0; i < hdr.eh.e_phnum && j < 2; i++) { | |
| 438 | if (xfsread(ino, ep + j, sizeof(ep[0]))) | |
| 439 | return; | |
| 440 | if (ep[j].p_type == PT_LOAD) | |
| 441 | j++; | |
| 442 | } | |
| 443 | for (i = 0; i < 2; i++) { | |
| 444 | p = PTOV(ep[i].p_paddr & 0xffffff); | |
| 445 | fs_off = ep[i].p_offset; | |
| 446 | if (xfsread(ino, p, ep[i].p_filesz)) | |
| 447 | return; | |
| 448 | } | |
| 449 | p += roundup2(ep[1].p_memsz, PAGE_SIZE); | |
| 450 | bootinfo.bi_symtab = VTOP(p); | |
| 451 | if (hdr.eh.e_shnum == hdr.eh.e_shstrndx + 3) { | |
| 452 | fs_off = hdr.eh.e_shoff + sizeof(es[0]) * | |
| 453 | (hdr.eh.e_shstrndx + 1); | |
| 454 | if (xfsread(ino, &es, sizeof(es))) | |
| 455 | return; | |
| 456 | for (i = 0; i < 2; i++) { | |
| 457 | memcpy(p, &es[i].sh_size, sizeof(es[i].sh_size)); | |
| 458 | p += sizeof(es[i].sh_size); | |
| 459 | fs_off = es[i].sh_offset; | |
| 460 | if (xfsread(ino, p, es[i].sh_size)) | |
| 461 | return; | |
| 462 | p += es[i].sh_size; | |
| 463 | } | |
| 464 | } | |
| 465 | addr = hdr.eh.e_entry & 0xffffff; | |
| 466 | } | |
| 467 | bootinfo.bi_esymtab = VTOP(p); | |
| 5ee58eed | 468 | bootinfo.bi_kernelname = VTOP(kname); |
| 984263bc | 469 | bootinfo.bi_bios_dev = dsk.drive; |
| 5ee58eed | 470 | __exec((caddr_t)addr, opts & RBX_MASK, |
| 984263bc MD |
471 | MAKEBOOTDEV(dev_maj[dsk.type], 0, dsk.slice, dsk.unit, dsk.part), |
| 472 | 0, 0, 0, VTOP(&bootinfo)); | |
| 473 | } | |
| 474 | ||
| 475 | static int | |
| d64b2e33 | 476 | parse(void) |
| 984263bc | 477 | { |
| 5ee58eed | 478 | char *arg = cmd; |
| 984263bc | 479 | char *p, *q; |
| 5ee58eed MD |
480 | unsigned int drv; |
| 481 | int c, i; | |
| 984263bc MD |
482 | |
| 483 | while ((c = *arg++)) { | |
| 484 | if (c == ' ' || c == '\t' || c == '\n') | |
| 485 | continue; | |
| badbfe61 MD |
486 | for (p = arg; *p && *p != '\n' && *p != ' ' && *p != '\t'; p++) |
| 487 | ; | |
| 984263bc MD |
488 | if (*p) |
| 489 | *p++ = 0; | |
| 490 | if (c == '-') { | |
| 491 | while ((c = *arg++)) { | |
| badbfe61 MD |
492 | for (i = NOPT - 1; i >= 0; --i) { |
| 493 | if (optstr[i] == c) { | |
| 494 | opts ^= 1 << flags[i]; | |
| 495 | goto ok; | |
| 496 | } | |
| 497 | } | |
| 498 | return(-1); | |
| 499 | ok: ; /* ugly but save space */ | |
| 984263bc | 500 | } |
| badbfe61 | 501 | if (opts & (1 << RBX_PROBEKBD)) { |
| 984263bc | 502 | i = *(uint8_t *)PTOV(0x496) & 0x10; |
| 2d7f6790 MD |
503 | if (!i) { |
| 504 | printf("NO KB\n"); | |
| badbfe61 | 505 | opts |= RBF_VIDEO | RBF_SERIAL; |
| 2d7f6790 | 506 | } |
| 984263bc MD |
507 | opts &= ~(1 << RBX_PROBEKBD); |
| 508 | } | |
| 984263bc MD |
509 | } else { |
| 510 | for (q = arg--; *q && *q != '('; q++); | |
| 511 | if (*q) { | |
| 512 | drv = -1; | |
| 513 | if (arg[1] == ':') { | |
| 984263bc | 514 | drv = *arg - '0'; |
| 5ee58eed MD |
515 | if (drv > 9) |
| 516 | return (-1); | |
| 984263bc MD |
517 | arg += 2; |
| 518 | } | |
| 519 | if (q - arg != 2) | |
| 520 | return -1; | |
| 521 | for (i = 0; arg[0] != dev_nm[i][0] || | |
| 522 | arg[1] != dev_nm[i][1]; i++) | |
| 523 | if (i == NDEV - 1) | |
| 524 | return -1; | |
| 525 | dsk.type = i; | |
| 526 | arg += 3; | |
| 984263bc | 527 | dsk.unit = *arg - '0'; |
| 5ee58eed MD |
528 | if (arg[1] != ',' || dsk.unit > 9) |
| 529 | return -1; | |
| 984263bc MD |
530 | arg += 2; |
| 531 | dsk.slice = WHOLE_DISK_SLICE; | |
| 532 | if (arg[1] == ',') { | |
| 5ee58eed MD |
533 | dsk.slice = *arg - '0' + 1; |
| 534 | if (dsk.slice > NDOSPART) | |
| 984263bc | 535 | return -1; |
| 984263bc MD |
536 | arg += 2; |
| 537 | } | |
| 5ee58eed | 538 | if (arg[1] != ')') |
| 984263bc MD |
539 | return -1; |
| 540 | dsk.part = *arg - 'a'; | |
| 5ee58eed MD |
541 | if (dsk.part > 7) |
| 542 | return (-1); | |
| 984263bc MD |
543 | arg += 2; |
| 544 | if (drv == -1) | |
| 545 | drv = dsk.unit; | |
| 5ee58eed MD |
546 | dsk.drive = (dsk.type <= TYPE_MAXHARD |
| 547 | ? DRV_HARD : 0) + drv; | |
| 984263bc MD |
548 | } |
| 549 | if ((i = p - arg - !*(p - 1))) { | |
| 5ee58eed | 550 | if ((size_t)i >= sizeof(kname)) |
| 984263bc MD |
551 | return -1; |
| 552 | memcpy(kname, arg, i + 1); | |
| 553 | } | |
| 554 | } | |
| 555 | arg = p; | |
| 556 | } | |
| d64b2e33 | 557 | return dskprobe(); |
| 984263bc MD |
558 | } |
| 559 | ||
| 984263bc | 560 | static int |
| d64b2e33 | 561 | dskprobe(void) |
| 984263bc | 562 | { |
| 984263bc | 563 | struct dos_partition *dp; |
| 6080181b SS |
564 | #ifdef DISKLABEL64 |
| 565 | struct disklabel64 *d; | |
| 566 | #else | |
| 2b961883 | 567 | struct disklabel32 *d; |
| 6080181b | 568 | #endif |
| 5ee58eed | 569 | char *sec; |
| 984263bc MD |
570 | unsigned sl, i; |
| 571 | ||
| d64b2e33 MD |
572 | /* |
| 573 | * Probe slice table | |
| 574 | */ | |
| 575 | sec = boot2_dmadat->secbuf; | |
| 576 | dsk.start = 0; | |
| 577 | if (drvread(sec, DOSBBSECTOR, 1)) | |
| 578 | return -1; | |
| 579 | dp = (void *)(sec + DOSPARTOFF); | |
| 580 | sl = dsk.slice; | |
| 581 | if (sl < BASE_SLICE) { | |
| 582 | for (i = 0; i < NDOSPART; i++) | |
| 583 | if (dp[i].dp_typ == DOSPTYP_386BSD && | |
| 584 | (dp[i].dp_flag & 0x80 || sl < BASE_SLICE)) { | |
| 585 | sl = BASE_SLICE + i; | |
| 586 | if (dp[i].dp_flag & 0x80 || | |
| 587 | dsk.slice == COMPATIBILITY_SLICE) | |
| 588 | break; | |
| 984263bc | 589 | } |
| d64b2e33 MD |
590 | if (dsk.slice == WHOLE_DISK_SLICE) |
| 591 | dsk.slice = sl; | |
| 592 | } | |
| 593 | if (sl != WHOLE_DISK_SLICE) { | |
| 594 | if (sl != COMPATIBILITY_SLICE) | |
| 595 | dp += sl - BASE_SLICE; | |
| 596 | if (dp->dp_typ != DOSPTYP_386BSD) { | |
| 597 | printf(INVALID_S, "slice"); | |
| 598 | return -1; | |
| 984263bc | 599 | } |
| d64b2e33 MD |
600 | dsk.start = dp->dp_start; |
| 601 | } | |
| 602 | ||
| 603 | /* | |
| 604 | * Probe label and partition table | |
| 605 | */ | |
| 6080181b | 606 | #ifdef DISKLABEL64 |
| d64b2e33 MD |
607 | if (drvread(sec, dsk.start, (sizeof(struct disklabel64) + 511) / 512)) |
| 608 | return -1; | |
| 609 | d = (void *)sec; | |
| 610 | if (d->d_magic != DISKMAGIC64) { | |
| 611 | printf(INVALID_S, "label"); | |
| 612 | return -1; | |
| 613 | } else { | |
| 614 | if (dsk.part >= d->d_npartitions || d->d_partitions[dsk.part].p_bsize == 0) { | |
| 615 | printf(INVALID_S, "partition"); | |
| 6080181b | 616 | return -1; |
| 6080181b | 617 | } |
| d64b2e33 MD |
618 | dsk.start += d->d_partitions[dsk.part].p_boffset / 512; |
| 619 | } | |
| 6080181b | 620 | #else |
| d64b2e33 MD |
621 | if (drvread(sec, dsk.start + LABELSECTOR32, 1)) |
| 622 | return -1; | |
| 623 | d = (void *)(sec + LABELOFFSET32); | |
| 624 | if (d->d_magic != DISKMAGIC32 || d->d_magic2 != DISKMAGIC32) { | |
| 625 | if (dsk.part != RAW_PART) { | |
| 626 | printf(INVALID_S, "label"); | |
| 627 | return -1; | |
| 628 | } | |
| 629 | } else { | |
| 630 | if (!dsk.init) { | |
| 631 | if (d->d_type == DTYPE_SCSI) | |
| 632 | dsk.type = TYPE_DA; | |
| 633 | dsk.init++; | |
| 984263bc | 634 | } |
| d64b2e33 MD |
635 | if (dsk.part >= d->d_npartitions || |
| 636 | !d->d_partitions[dsk.part].p_size) { | |
| 637 | printf(INVALID_S, "partition"); | |
| 638 | return -1; | |
| 639 | } | |
| 640 | dsk.start += d->d_partitions[dsk.part].p_offset; | |
| 641 | dsk.start -= d->d_partitions[RAW_PART].p_offset; | |
| 642 | } | |
| 6080181b | 643 | #endif |
| d64b2e33 MD |
644 | /* |
| 645 | * Probe filesystem | |
| 646 | */ | |
| 647 | #if defined(UFS) && defined(HAMMERFS) | |
| 648 | if (boot2_hammer_api.fsinit() == 0) { | |
| 649 | fsapi = &boot2_hammer_api; | |
| 650 | } else if (boot2_ufs_api.fsinit() == 0) { | |
| 651 | fsapi = &boot2_ufs_api; | |
| 652 | } else { | |
| 653 | printf("fs probe failed\n"); | |
| 654 | fsapi = &boot2_ufs_api; | |
| 655 | return -1; | |
| 984263bc | 656 | } |
| d64b2e33 MD |
657 | return 0; |
| 658 | #else | |
| 659 | return fsapi->fsinit(); | |
| 660 | #endif | |
| 661 | } | |
| 662 | ||
| 663 | ||
| 664 | /* | |
| 665 | * Read from the probed disk. We have established the slice and partition | |
| 666 | * base sector. | |
| 667 | */ | |
| 668 | int | |
| 669 | dskread(void *buf, unsigned lba, unsigned nblk) | |
| 670 | { | |
| 984263bc MD |
671 | return drvread(buf, dsk.start + lba, nblk); |
| 672 | } | |
| 673 | ||
| d64b2e33 MD |
674 | /* |
| 675 | * boot encapsulated ABI | |
| 676 | */ | |
| 677 | void | |
| 984263bc MD |
678 | printf(const char *fmt,...) |
| 679 | { | |
| 5ee58eed | 680 | va_list ap; |
| 984263bc MD |
681 | char buf[10]; |
| 682 | char *s; | |
| 5ee58eed | 683 | unsigned u; |
| 984263bc MD |
684 | int c; |
| 685 | ||
| 5ee58eed | 686 | va_start(ap, fmt); |
| 984263bc MD |
687 | while ((c = *fmt++)) { |
| 688 | if (c == '%') { | |
| 689 | c = *fmt++; | |
| 690 | switch (c) { | |
| 691 | case 'c': | |
| 5ee58eed | 692 | putchar(va_arg(ap, int)); |
| 984263bc MD |
693 | continue; |
| 694 | case 's': | |
| 5ee58eed | 695 | for (s = va_arg(ap, char *); *s; s++) |
| 984263bc MD |
696 | putchar(*s); |
| 697 | continue; | |
| 698 | case 'u': | |
| 5ee58eed | 699 | u = va_arg(ap, unsigned); |
| 984263bc MD |
700 | s = buf; |
| 701 | do | |
| 5ee58eed MD |
702 | *s++ = '0' + u % 10U; |
| 703 | while (u /= 10U); | |
| 984263bc MD |
704 | while (--s >= buf) |
| 705 | putchar(*s); | |
| 706 | continue; | |
| 707 | } | |
| 708 | } | |
| 709 | putchar(c); | |
| 710 | } | |
| 5ee58eed | 711 | va_end(ap); |
| 984263bc MD |
712 | } |
| 713 | ||
| d64b2e33 MD |
714 | /* |
| 715 | * boot encapsulated ABI | |
| 716 | */ | |
| 717 | void | |
| 984263bc MD |
718 | putchar(int c) |
| 719 | { | |
| 720 | if (c == '\n') | |
| 721 | xputc('\r'); | |
| 5ee58eed | 722 | xputc(c); |
| 984263bc MD |
723 | } |
| 724 | ||
| d64b2e33 MD |
725 | /* |
| 726 | * boot encapsulated ABI | |
| 727 | */ | |
| 728 | int | |
| 984263bc MD |
729 | drvread(void *buf, unsigned lba, unsigned nblk) |
| 730 | { | |
| 50a1695f | 731 | static unsigned c = 0x2d5c7c2f; /* twiddle */ |
| 984263bc | 732 | |
| 50a1695f MD |
733 | c = (c << 8) | (c >> 24); |
| 734 | xputc(c); | |
| 735 | xputc('\b'); | |
| 984263bc MD |
736 | v86.ctl = V86_ADDR | V86_CALLF | V86_FLAGS; |
| 737 | v86.addr = XREADORG; /* call to xread in boot1 */ | |
| 738 | v86.es = VTOPSEG(buf); | |
| 739 | v86.eax = lba; | |
| 740 | v86.ebx = VTOPOFF(buf); | |
| 741 | v86.ecx = lba >> 16; | |
| 742 | v86.edx = nblk << 8 | dsk.drive; | |
| 743 | v86int(); | |
| 744 | v86.ctl = V86_FLAGS; | |
| 745 | if (V86_CY(v86.efl)) { | |
| 5ee58eed | 746 | printf("error %u lba %u\n", v86.eax >> 8 & 0xff, lba); |
| 984263bc MD |
747 | return -1; |
| 748 | } | |
| 749 | return 0; | |
| 750 | } | |
| 751 | ||
| 752 | static int | |
| 753 | keyhit(unsigned ticks) | |
| 754 | { | |
| 755 | uint32_t t0, t1; | |
| 756 | ||
| 757 | if (opts & 1 << RBX_NOINTR) | |
| 758 | return 0; | |
| 759 | t0 = 0; | |
| 760 | for (;;) { | |
| 761 | if (xgetc(1)) | |
| 762 | return 1; | |
| 763 | t1 = *(uint32_t *)PTOV(0x46c); | |
| 764 | if (!t0) | |
| 765 | t0 = t1; | |
| 766 | if (t1 < t0 || t1 >= t0 + ticks) | |
| 767 | return 0; | |
| 768 | } | |
| 769 | } | |
| 770 | ||
| 50a1695f | 771 | static void |
| 984263bc MD |
772 | xputc(int c) |
| 773 | { | |
| badbfe61 | 774 | if (opts & RBF_VIDEO) |
| 984263bc | 775 | putc(c); |
| badbfe61 | 776 | if (opts & RBF_SERIAL) |
| 984263bc | 777 | sio_putc(c); |
| 984263bc MD |
778 | } |
| 779 | ||
| 780 | static int | |
| 781 | xgetc(int fn) | |
| 782 | { | |
| 783 | if (opts & 1 << RBX_NOINTR) | |
| 784 | return 0; | |
| 785 | for (;;) { | |
| badbfe61 | 786 | if ((opts & RBF_VIDEO) && getc(1)) |
| 984263bc | 787 | return fn ? 1 : getc(0); |
| badbfe61 | 788 | if ((opts & RBF_SERIAL) && sio_ischar()) |
| 984263bc MD |
789 | return fn ? 1 : sio_getc(); |
| 790 | if (fn) | |
| 791 | return 0; | |
| 792 | } | |
| 793 | } | |
| 794 | ||
| 795 | static int | |
| 796 | getc(int fn) | |
| 797 | { | |
| 798 | v86.addr = 0x16; | |
| 799 | v86.eax = fn << 8; | |
| 800 | v86int(); | |
| 801 | return fn == 0 ? v86.eax & 0xff : !V86_ZR(v86.efl); | |
| 802 | } |