gptboot.c (ec82884e87b9ad05278081e33749133247063f59) gptboot.c (c1418270b28edf9b99044181e9f48dfc0a7dc160)
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.

--- 99 unchanged lines hidden (view full) ---

108#ifdef LOADER_GELI_SUPPORT
109static int vdev_read(void *vdev __unused, void *priv, off_t off, void *buf,
110 size_t bytes);
111#endif
112
113#include "ufsread.c"
114#include "gpt.c"
115#ifdef LOADER_GELI_SUPPORT
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.

--- 99 unchanged lines hidden (view full) ---

108#ifdef LOADER_GELI_SUPPORT
109static int vdev_read(void *vdev __unused, void *priv, off_t off, void *buf,
110 size_t bytes);
111#endif
112
113#include "ufsread.c"
114#include "gpt.c"
115#ifdef LOADER_GELI_SUPPORT
116#include "geliboot.c"
116#include "geliboot.h"
117static char gelipw[GELI_PW_MAXLEN];
118static struct keybuf *gelibuf;
119#endif
120
117static char gelipw[GELI_PW_MAXLEN];
118static struct keybuf *gelibuf;
119#endif
120
121struct gptdsk {
122 struct dsk dsk;
123#ifdef LOADER_GELI_SUPPORT
124 struct geli_dev *gdev;
125#endif
126};
127
128static struct gptdsk gdsk;
129
121static inline int
122xfsread(ufs_ino_t inode, void *buf, size_t nbyte)
123{
124
125 if ((size_t)fsread(inode, buf, nbyte) != nbyte) {
126 printf("Invalid %s\n", "format");
127 return (-1);
128 }

--- 91 unchanged lines hidden (view full) ---

220 high_heap_base = bios_extmem + 0x100000 - HEAP_MIN;
221 }
222}
223
224static int
225gptinit(void)
226{
227
130static inline int
131xfsread(ufs_ino_t inode, void *buf, size_t nbyte)
132{
133
134 if ((size_t)fsread(inode, buf, nbyte) != nbyte) {
135 printf("Invalid %s\n", "format");
136 return (-1);
137 }

--- 91 unchanged lines hidden (view full) ---

229 high_heap_base = bios_extmem + 0x100000 - HEAP_MIN;
230 }
231}
232
233static int
234gptinit(void)
235{
236
228 if (gptread(&freebsd_ufs_uuid, &dsk, dmadat->secbuf) == -1) {
237 if (gptread(&freebsd_ufs_uuid, &gdsk.dsk, dmadat->secbuf) == -1) {
229 printf("%s: unable to load GPT\n", BOOTPROG);
230 return (-1);
231 }
238 printf("%s: unable to load GPT\n", BOOTPROG);
239 return (-1);
240 }
232 if (gptfind(&freebsd_ufs_uuid, &dsk, dsk.part) == -1) {
241 if (gptfind(&freebsd_ufs_uuid, &gdsk.dsk, gdsk.dsk.part) == -1) {
233 printf("%s: no UFS partition was found\n", BOOTPROG);
234 return (-1);
235 }
236#ifdef LOADER_GELI_SUPPORT
242 printf("%s: no UFS partition was found\n", BOOTPROG);
243 return (-1);
244 }
245#ifdef LOADER_GELI_SUPPORT
237 if (geli_taste(vdev_read, &dsk, (gpttable[curent].ent_lba_end -
238 gpttable[curent].ent_lba_start)) == 0) {
239 if (geli_havekey(&dsk) != 0 && geli_passphrase(gelipw,
240 dsk.unit, 'p', curent + 1, &dsk) != 0) {
246 gdsk.gdev = geli_taste(vdev_read, &gdsk.dsk,
247 (gpttable[curent].ent_lba_end - gpttable[curent].ent_lba_start),
248 "disk%up%u:", gdsk.dsk.unit, curent + 1);
249 if (gdsk.gdev != NULL) {
250 if (geli_havekey(gdsk.gdev) != 0 &&
251 geli_passphrase(gdsk.gdev, gelipw) != 0) {
241 printf("%s: unable to decrypt GELI key\n", BOOTPROG);
242 return (-1);
243 }
244 }
245#endif
246
247 dsk_meta = 0;
248 return (0);

--- 19 unchanged lines hidden (view full) ---

268 } else {
269 heap_next = (char *)dmadat + sizeof(*dmadat);
270 heap_end = (char *)PTOV(bios_basemem);
271 }
272 setheap(heap_next, heap_end);
273
274 v86.ctl = V86_FLAGS;
275 v86.efl = PSL_RESERVED_DEFAULT | PSL_I;
252 printf("%s: unable to decrypt GELI key\n", BOOTPROG);
253 return (-1);
254 }
255 }
256#endif
257
258 dsk_meta = 0;
259 return (0);

--- 19 unchanged lines hidden (view full) ---

279 } else {
280 heap_next = (char *)dmadat + sizeof(*dmadat);
281 heap_end = (char *)PTOV(bios_basemem);
282 }
283 setheap(heap_next, heap_end);
284
285 v86.ctl = V86_FLAGS;
286 v86.efl = PSL_RESERVED_DEFAULT | PSL_I;
276 dsk.drive = *(uint8_t *)PTOV(ARGS);
277 dsk.type = dsk.drive & DRV_HARD ? TYPE_AD : TYPE_FD;
278 dsk.unit = dsk.drive & DRV_MASK;
279 dsk.part = -1;
280 dsk.start = 0;
287 gdsk.dsk.drive = *(uint8_t *)PTOV(ARGS);
288 gdsk.dsk.type = gdsk.dsk.drive & DRV_HARD ? TYPE_AD : TYPE_FD;
289 gdsk.dsk.unit = gdsk.dsk.drive & DRV_MASK;
290 gdsk.dsk.part = -1;
291 gdsk.dsk.start = 0;
281 bootinfo.bi_version = BOOTINFO_VERSION;
282 bootinfo.bi_size = sizeof(bootinfo);
283 bootinfo.bi_basemem = bios_basemem / 1024;
284 bootinfo.bi_extmem = bios_extmem / 1024;
285 bootinfo.bi_memsizes_valid++;
292 bootinfo.bi_version = BOOTINFO_VERSION;
293 bootinfo.bi_size = sizeof(bootinfo);
294 bootinfo.bi_basemem = bios_basemem / 1024;
295 bootinfo.bi_extmem = bios_extmem / 1024;
296 bootinfo.bi_memsizes_valid++;
286 bootinfo.bi_bios_dev = dsk.drive;
297 bootinfo.bi_bios_dev = gdsk.dsk.drive;
287
298
288#ifdef LOADER_GELI_SUPPORT
289 geli_init();
290#endif
291 /* Process configuration file */
292
293 if (gptinit() != 0)
294 return (-1);
295
296 autoboot = 1;
297 *cmd = '\0';
298

--- 28 unchanged lines hidden (view full) ---

327 * directly instead.
328 */
329 if (*kname != '\0')
330 load();
331 memcpy(kname, PATH_LOADER, sizeof(PATH_LOADER));
332 load();
333 memcpy(kname, PATH_KERNEL, sizeof(PATH_KERNEL));
334 load();
299 /* Process configuration file */
300
301 if (gptinit() != 0)
302 return (-1);
303
304 autoboot = 1;
305 *cmd = '\0';
306

--- 28 unchanged lines hidden (view full) ---

335 * directly instead.
336 */
337 if (*kname != '\0')
338 load();
339 memcpy(kname, PATH_LOADER, sizeof(PATH_LOADER));
340 load();
341 memcpy(kname, PATH_KERNEL, sizeof(PATH_KERNEL));
342 load();
335 gptbootfailed(&dsk);
336 if (gptfind(&freebsd_ufs_uuid, &dsk, -1) == -1)
343 gptbootfailed(&gdsk.dsk);
344 if (gptfind(&freebsd_ufs_uuid, &gdsk.dsk, -1) == -1)
337 break;
338 dsk_meta = 0;
339 }
340
341 /* Present the user with the boot2 prompt. */
342
343 for (;;) {
344 if (!OPT_CHECK(RBX_QUIET)) {
345 printf("\nFreeBSD/x86 boot\n"
346 "Default: %u:%s(%up%u)%s\n"
347 "boot: ",
345 break;
346 dsk_meta = 0;
347 }
348
349 /* Present the user with the boot2 prompt. */
350
351 for (;;) {
352 if (!OPT_CHECK(RBX_QUIET)) {
353 printf("\nFreeBSD/x86 boot\n"
354 "Default: %u:%s(%up%u)%s\n"
355 "boot: ",
348 dsk.drive & DRV_MASK, dev_nm[dsk.type], dsk.unit,
349 dsk.part, kname);
356 gdsk.dsk.drive & DRV_MASK, dev_nm[gdsk.dsk.type],
357 gdsk.dsk.unit, gdsk.dsk.part, kname);
350 }
351 if (ioctrl & IO_SERIAL)
352 sio_flush();
353 *cmd = '\0';
354 if (keyhit(0))
355 getstr(cmd, sizeof(cmd));
356 else if (!OPT_CHECK(RBX_QUIET))
357 putchar('\n');

--- 29 unchanged lines hidden (view full) ---

387 caddr_t p;
388 ufs_ino_t ino;
389 uint32_t addr, x;
390 int fmt, i, j;
391
392 if (!(ino = lookup(kname))) {
393 if (!ls) {
394 printf("%s: No %s on %u:%s(%up%u)\n", BOOTPROG,
358 }
359 if (ioctrl & IO_SERIAL)
360 sio_flush();
361 *cmd = '\0';
362 if (keyhit(0))
363 getstr(cmd, sizeof(cmd));
364 else if (!OPT_CHECK(RBX_QUIET))
365 putchar('\n');

--- 29 unchanged lines hidden (view full) ---

395 caddr_t p;
396 ufs_ino_t ino;
397 uint32_t addr, x;
398 int fmt, i, j;
399
400 if (!(ino = lookup(kname))) {
401 if (!ls) {
402 printf("%s: No %s on %u:%s(%up%u)\n", BOOTPROG,
395 kname, dsk.drive & DRV_MASK, dev_nm[dsk.type],
396 dsk.unit,
397 dsk.part);
398 }
403 kname, gdsk.dsk.drive & DRV_MASK, dev_nm[gdsk.dsk.type],
404 gdsk.dsk.unit, gdsk.dsk.part);
405 }
399 return;
400 }
401 if (xfsread(ino, &hdr, sizeof(hdr)))
402 return;
403 if (N_GETMAGIC(hdr.ex) == ZMAGIC)
404 fmt = 0;
405 else if (IS_ELF(hdr.eh))
406 fmt = 1;

--- 57 unchanged lines hidden (view full) ---

464 return;
465 p += es[i].sh_size;
466 }
467 }
468 addr = hdr.eh.e_entry & 0xffffff;
469 }
470 bootinfo.bi_esymtab = VTOP(p);
471 bootinfo.bi_kernelname = VTOP(kname);
406 return;
407 }
408 if (xfsread(ino, &hdr, sizeof(hdr)))
409 return;
410 if (N_GETMAGIC(hdr.ex) == ZMAGIC)
411 fmt = 0;
412 else if (IS_ELF(hdr.eh))
413 fmt = 1;

--- 57 unchanged lines hidden (view full) ---

471 return;
472 p += es[i].sh_size;
473 }
474 }
475 addr = hdr.eh.e_entry & 0xffffff;
476 }
477 bootinfo.bi_esymtab = VTOP(p);
478 bootinfo.bi_kernelname = VTOP(kname);
472 bootinfo.bi_bios_dev = dsk.drive;
479 bootinfo.bi_bios_dev = gdsk.dsk.drive;
473#ifdef LOADER_GELI_SUPPORT
474 geliargs.size = sizeof(geliargs);
475 explicit_bzero(gelipw, sizeof(gelipw));
476 gelibuf = malloc(sizeof(struct keybuf) +
477 (GELI_MAX_KEYS * sizeof(struct keybuf_ent)));
480#ifdef LOADER_GELI_SUPPORT
481 geliargs.size = sizeof(geliargs);
482 explicit_bzero(gelipw, sizeof(gelipw));
483 gelibuf = malloc(sizeof(struct keybuf) +
484 (GELI_MAX_KEYS * sizeof(struct keybuf_ent)));
478 geli_fill_keybuf(gelibuf);
485 geli_export_key_buffer(gelibuf);
479 geliargs.notapw = '\0';
480 geliargs.keybuf_sentinel = KEYBUF_SENTINEL;
481 geliargs.keybuf = gelibuf;
482#endif
483 __exec((caddr_t)addr, RB_BOOTINFO | (opts & RBX_MASK),
486 geliargs.notapw = '\0';
487 geliargs.keybuf_sentinel = KEYBUF_SENTINEL;
488 geliargs.keybuf = gelibuf;
489#endif
490 __exec((caddr_t)addr, RB_BOOTINFO | (opts & RBX_MASK),
484 MAKEBOOTDEV(dev_maj[dsk.type], dsk.part + 1, dsk.unit, 0xff),
491 MAKEBOOTDEV(dev_maj[gdsk.dsk.type], gdsk.dsk.part + 1, gdsk.dsk.unit, 0xff),
485 KARGS_FLAGS_EXTARG, 0, 0, VTOP(&bootinfo)
486#ifdef LOADER_GELI_SUPPORT
487 , geliargs
488#endif
489 );
490}
491
492static int

--- 97 unchanged lines hidden (view full) ---

590 return (0);
591}
592
593static int
594dskread(void *buf, daddr_t lba, unsigned nblk)
595{
596 int err;
597
492 KARGS_FLAGS_EXTARG, 0, 0, VTOP(&bootinfo)
493#ifdef LOADER_GELI_SUPPORT
494 , geliargs
495#endif
496 );
497}
498
499static int

--- 97 unchanged lines hidden (view full) ---

597 return (0);
598}
599
600static int
601dskread(void *buf, daddr_t lba, unsigned nblk)
602{
603 int err;
604
598 err = drvread(&dsk, buf, lba + dsk.start, nblk);
605 err = drvread(&gdsk.dsk, buf, lba + gdsk.dsk.start, nblk);
599
600#ifdef LOADER_GELI_SUPPORT
606
607#ifdef LOADER_GELI_SUPPORT
601 if (err == 0 && is_geli(&dsk) == 0) {
608 if (err == 0 && gdsk.gdev != NULL) {
602 /* Decrypt */
609 /* Decrypt */
603 if (geli_read(&dsk, lba * DEV_BSIZE, buf, nblk * DEV_BSIZE))
610 if (geli_read(gdsk.gdev, lba * DEV_BSIZE, buf,
611 nblk * DEV_BSIZE))
604 return (err);
605 }
606#endif
607
608 return (err);
609}
610
611#ifdef LOADER_GELI_SUPPORT
612/*
612 return (err);
613 }
614#endif
615
616 return (err);
617}
618
619#ifdef LOADER_GELI_SUPPORT
620/*
613 * Read function compartible with the ZFS callback, required to keep the GELI
614 * Implementation the same for both UFS and ZFS
621 * Read function compatible with the ZFS callback, required to keep the GELI
622 * implementation the same for both UFS and ZFS.
615 */
616static int
617vdev_read(void *vdev __unused, void *priv, off_t off, void *buf, size_t bytes)
618{
619 char *p;
620 daddr_t lba;
621 unsigned int nb;
623 */
624static int
625vdev_read(void *vdev __unused, void *priv, off_t off, void *buf, size_t bytes)
626{
627 char *p;
628 daddr_t lba;
629 unsigned int nb;
622 struct dsk *dskp;
630 struct gptdsk *dskp;
623
631
624 dskp = (struct dsk *)priv;
632 dskp = (struct gptdsk *)priv;
625
626 if ((off & (DEV_BSIZE - 1)) || (bytes & (DEV_BSIZE - 1)))
627 return (-1);
628
629 p = buf;
630 lba = off / DEV_BSIZE;
633
634 if ((off & (DEV_BSIZE - 1)) || (bytes & (DEV_BSIZE - 1)))
635 return (-1);
636
637 p = buf;
638 lba = off / DEV_BSIZE;
631 lba += dskp->start;
639 lba += dskp->dsk.start;
632
633 while (bytes > 0) {
634 nb = bytes / DEV_BSIZE;
635 if (nb > VBLKSIZE / DEV_BSIZE)
636 nb = VBLKSIZE / DEV_BSIZE;
640
641 while (bytes > 0) {
642 nb = bytes / DEV_BSIZE;
643 if (nb > VBLKSIZE / DEV_BSIZE)
644 nb = VBLKSIZE / DEV_BSIZE;
637 if (drvread(dskp, dmadat->blkbuf, lba, nb))
645 if (drvread(&dskp->dsk, dmadat->blkbuf, lba, nb))
638 return (-1);
639 memcpy(p, dmadat->blkbuf, nb * DEV_BSIZE);
640 p += nb * DEV_BSIZE;
641 lba += nb;
642 bytes -= nb * DEV_BSIZE;
643 }
644
645 return (0);
646}
647#endif /* LOADER_GELI_SUPPORT */
646 return (-1);
647 memcpy(p, dmadat->blkbuf, nb * DEV_BSIZE);
648 p += nb * DEV_BSIZE;
649 lba += nb;
650 bytes -= nb * DEV_BSIZE;
651 }
652
653 return (0);
654}
655#endif /* LOADER_GELI_SUPPORT */