From: Matthew Dillon Date: Wed, 30 Jul 2008 00:45:26 +0000 (+0000) Subject: Add a terrible hack to GPT which allows non-EFI BIOSes to boot from it. X-Git-Tag: v2.1.1~750 X-Git-Url: https://gitweb.dragonflybsd.org/dragonfly.git/commitdiff_plain/210143e4adb94adc21693f94a9ed6a57110ffdcd Add a terrible hack to GPT which allows non-EFI BIOSes to boot from it. With a freshly made gpt on a disk you can run 'gpt boot da0' (specifying the correct whole-disk name) and GPT will do the following: * It will create a 100MB partition #0 in the GPT * It will fake up a slice 1 in the PMBR for the BIOS and the loader that aliases partition #0 in the GPT. The BIOS and loader will see a bootable slice 1 while the kernel, once booted, will see a GPT. This can cause confusion because the kernel will NOT see the fake slice 1 any more, and the partition number in the GPT of the boot partition 0 (aka da0s0). The boot partition (da0s0 from the point of view of the kernel) needs to have a 32 bit disklabel and a UFS filesystem in da0s0a which contains the kernel image and boot sub-directory. In the boot sub-directory the loader.conf file must contain a line that points to the root mount, which say you may have created with gpt add as partition #1. Older kernels may require the root mount to also be in a disklabel, so you might have to put a 32 or 64 bit disklabel in partition #1 as well. This requirement will be removed soon relative to this commit (since the idea with GPT is to not have to use disklabels). vfs.root.mountfrom="ufs:ad0s1a" vfs.root.mountfrom="ufs:ad0s1" --- diff --git a/sbin/gpt/Makefile b/sbin/gpt/Makefile index d367eda296..6dbd0bf032 100644 --- a/sbin/gpt/Makefile +++ b/sbin/gpt/Makefile @@ -1,9 +1,9 @@ # $FreeBSD: src/sbin/gpt/Makefile,v 1.7 2005/09/01 02:49:20 marcel Exp $ -# $DragonFly: src/sbin/gpt/Makefile,v 1.1 2007/06/16 22:29:27 dillon Exp $ +# $DragonFly: src/sbin/gpt/Makefile,v 1.2 2008/07/30 00:45:26 dillon Exp $ PROG= gpt SRCS= add.c create.c destroy.c gpt.c label.c map.c migrate.c recover.c \ - remove.c show.c + remove.c show.c boot.c WARNS?= 4 MAN= gpt.8 diff --git a/sbin/gpt/boot.c b/sbin/gpt/boot.c new file mode 100644 index 0000000000..8369a95c9e --- /dev/null +++ b/sbin/gpt/boot.c @@ -0,0 +1,209 @@ +/* + * Copyright (c) 2008 The DragonFly Project. All rights reserved. + * + * This code is derived from software contributed to The DragonFly Project + * by Matthew Dillon + * + * Redistribution and use in source and binary forms, with or without + * modification, are permitted provided that the following conditions + * are met: + * + * 1. Redistributions of source code must retain the above copyright + * notice, this list of conditions and the following disclaimer. + * 2. Redistributions in binary form must reproduce the above copyright + * notice, this list of conditions and the following disclaimer in + * the documentation and/or other materials provided with the + * distribution. + * 3. Neither the name of The DragonFly Project nor the names of its + * contributors may be used to endorse or promote products derived + * from this software without specific, prior written permission. + * + * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS + * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT + * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS + * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE + * COPYRIGHT HOLDERS OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, + * INCIDENTAL, SPECIAL, EXEMPLARY OR CONSEQUENTIAL DAMAGES (INCLUDING, + * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; + * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED + * AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, + * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT + * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF + * SUCH DAMAGE. + * + * $DragonFly: src/sbin/gpt/boot.c,v 1.1 2008/07/30 00:45:26 dillon Exp $ + */ + +#include + +#include +#include +#include +#include +#include +#include +#include + +#include "map.h" +#include "gpt.h" + +static void +usage_boot(void) +{ + fprintf(stderr, "usage: %s device\n", getprogname()); + exit(1); +} + +static void +bootset(int fd) +{ + uuid_t uuid; + off_t block; + off_t size; + unsigned int entry; + map_t *gpt, *tpg; + map_t *tbl, *lbt; + map_t *map; + u_int32_t status; + struct gpt_hdr *hdr; + struct gpt_ent *ent; + struct mbr *mbr; + int bfd; + + /* + * Paramters for boot partition + */ + uuid_name_lookup(&uuid, "DragonFly Label32", &status); + if (status != uuid_s_ok) + err(1, "unable to find uuid for 'DragonFly Label32'"); + entry = 0; + block = 0; + size = 128 * 1024 * 1024 / 512; + + gpt = map_find(MAP_TYPE_PRI_GPT_HDR); + if (gpt == NULL) + errx(1, "%s: error: no primary GPT header", device_name); + tpg = map_find(MAP_TYPE_SEC_GPT_HDR); + if (tpg == NULL) + errx(1, "%s: error: no secondary GPT header", device_name); + tbl = map_find(MAP_TYPE_PRI_GPT_TBL); + lbt = map_find(MAP_TYPE_SEC_GPT_TBL); + if (tbl == NULL || lbt == NULL) { + errx(1, "%s: error: no primary or secondary gpt table", + device_name); + } + + hdr = gpt->map_data; + if (entry > le32toh(hdr->hdr_entries)) { + errx(1, "%s: error: index %u out of range (%u max)", + device_name, entry, le32toh(hdr->hdr_entries)); + } + + ent = (void *)((char *)tbl->map_data + entry * + le32toh(hdr->hdr_entsz)); + if (!uuid_is_nil(&ent->ent_type, NULL)) { + errx(1, "%s: error: entry at index %d is not free", + device_name, entry); + } + map = map_alloc(block, size); + if (map == NULL) + errx(1, "%s: error: no space available on device", device_name); + block = map->map_start; + size = map->map_size; + + le_uuid_enc(&ent->ent_type, &uuid); + ent->ent_lba_start = htole64(map->map_start); + ent->ent_lba_end = htole64(map->map_start + map->map_size - 1LL); + + hdr->hdr_crc_table = htole32(crc32(tbl->map_data, + le32toh(hdr->hdr_entries) * + le32toh(hdr->hdr_entsz))); + hdr->hdr_crc_self = 0; + hdr->hdr_crc_self = htole32(crc32(hdr, le32toh(hdr->hdr_size))); + + gpt_write(fd, gpt); + gpt_write(fd, tbl); + + hdr = tpg->map_data; + ent = (void*)((char*)lbt->map_data + entry * le32toh(hdr->hdr_entsz)); + le_uuid_enc(&ent->ent_type, &uuid); + ent->ent_lba_start = htole64(map->map_start); + ent->ent_lba_end = htole64(map->map_start + map->map_size - 1LL); + + hdr->hdr_crc_table = htole32(crc32(lbt->map_data, + le32toh(hdr->hdr_entries) * + le32toh(hdr->hdr_entsz))); + hdr->hdr_crc_self = 0; + hdr->hdr_crc_self = htole32(crc32(hdr, le32toh(hdr->hdr_size))); + + gpt_write(fd, lbt); + gpt_write(fd, tpg); + + /* + * Create a dummy partition + */ + map = map_find(MAP_TYPE_PMBR); + if (map == NULL) + errx(1, "I can't find the PMBR!"); + mbr = map->map_data; + if (mbr == NULL) + errx(1, "I can't find the PMBR's data!"); + + /* + * Copy in real boot code + */ + bfd = open("/boot/boot0", O_RDONLY); + if (bfd < 0 || + read(bfd, mbr->mbr_code, sizeof(mbr->mbr_code)) != + sizeof(mbr->mbr_code)) { + errx(1, "Cannot read /boot/boot0"); + } + close(bfd); + + /* + * Generate partition #1 + */ + mbr->mbr_part[1].part_shd = 0xff; + mbr->mbr_part[1].part_ssect = 0xff; + mbr->mbr_part[1].part_scyl = 0xff; + mbr->mbr_part[1].part_ehd = 0xff; + mbr->mbr_part[1].part_esect = 0xff; + mbr->mbr_part[1].part_ecyl = 0xff; + mbr->mbr_part[1].part_start_lo = htole16(block); + mbr->mbr_part[1].part_start_hi = htole16((block) >> 16); + mbr->mbr_part[1].part_size_lo = htole16(size); + mbr->mbr_part[1].part_size_hi = htole16(size >> 16); + + mbr->mbr_part[1].part_typ = 165; + mbr->mbr_part[1].part_flag = 0x80; + + gpt_write(fd, map); +} + +int +cmd_boot(int argc, char *argv[]) +{ + int ch, fd; + + while ((ch = getopt(argc, argv, "")) != -1) { + switch(ch) { + default: + usage_boot(); + } + } + + if (argc == optind) + usage_boot(); + + while (optind < argc) { + fd = gpt_open(argv[optind++]); + if (fd == -1) { + warn("unable to open device '%s'", device_name); + continue; + } + bootset(fd); + gpt_close(fd); + } + return (0); +} + diff --git a/sbin/gpt/gpt.c b/sbin/gpt/gpt.c index 26fb20ddff..7cb21abdc3 100644 --- a/sbin/gpt/gpt.c +++ b/sbin/gpt/gpt.c @@ -26,7 +26,7 @@ * CRC32 code derived from work by Gary S. Brown. * * $FreeBSD: src/sbin/gpt/gpt.c,v 1.16 2006/07/07 02:44:23 marcel Exp $ - * $DragonFly: src/sbin/gpt/gpt.c,v 1.4 2007/06/17 23:50:15 dillon Exp $ + * $DragonFly: src/sbin/gpt/gpt.c,v 1.5 2008/07/30 00:45:26 dillon Exp $ */ #include @@ -397,7 +397,7 @@ gpt_mbr(int fd, off_t lba) else break; } - if (pmbr && i == 4 && lba == 0) { + if (pmbr && (i == 1 || i == 4) && lba == 0) { if (pmbr != 1) warnx("%s: Suspicious PMBR at sector %llu", device_name, (long long)lba); @@ -627,6 +627,7 @@ static struct { const char *name; } cmdsw[] = { { cmd_add, "add" }, + { cmd_boot, "boot" }, { cmd_create, "create" }, { cmd_destroy, "destroy" }, { NULL, "help" }, diff --git a/sbin/gpt/gpt.h b/sbin/gpt/gpt.h index 2cc6f43176..97a6d48392 100644 --- a/sbin/gpt/gpt.h +++ b/sbin/gpt/gpt.h @@ -24,7 +24,7 @@ * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. * * $FreeBSD: src/sbin/gpt/gpt.h,v 1.11 2006/06/22 22:05:28 marcel Exp $ - * $DragonFly: src/sbin/gpt/gpt.h,v 1.1 2007/06/16 22:29:27 dillon Exp $ + * $DragonFly: src/sbin/gpt/gpt.h,v 1.2 2008/07/30 00:45:26 dillon Exp $ */ #ifndef _GPT_H_ @@ -77,6 +77,7 @@ uint8_t *utf16_to_utf8(uint16_t *); void utf8_to_utf16(const uint8_t *, uint16_t *, size_t); int cmd_add(int, char *[]); +int cmd_boot(int, char *[]); int cmd_create(int, char *[]); int cmd_destroy(int, char *[]); int cmd_label(int, char *[]);