Source-Changes-HG archive

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index][Old Index]

[src/trunk]: src Support booting from GPT-partioned disks on PC-BIOS-compatib...



details:   https://anonhg.NetBSD.org/src/rev/9e23d110128f
branches:  trunk
changeset: 760486:9e23d110128f
user:      jakllsch <jakllsch%NetBSD.org@localhost>
date:      Thu Jan 06 01:08:48 2011 +0000

description:
Support booting from GPT-partioned disks on PC-BIOS-compatible systems.

Much of the work in this commit was done by Mike Volokhov during GSoC 2009.

diffstat:

 distrib/sets/lists/base/md.amd64              |    4 +-
 distrib/sets/lists/base/md.i386               |    4 +-
 sbin/gpt/Makefile                             |    6 +-
 sbin/gpt/biosboot.c                           |  326 +++++++++++++++
 sbin/gpt/gpt.c                                |    7 +-
 sbin/gpt/gpt.h                                |    3 +-
 sys/arch/i386/stand/bootxx/boot1.c            |   10 +-
 sys/arch/i386/stand/bootxx/bootxx.S           |   13 +-
 sys/arch/i386/stand/bootxx/pbr.S              |   33 +-
 sys/arch/i386/stand/mbr/Makefile              |    3 +-
 sys/arch/i386/stand/mbr/gpt.S                 |  552 ++++++++++++++++++++++++++
 sys/arch/i386/stand/mbr/mbr_gpt/Makefile      |    6 +
 sys/arch/i386/stand/mbr/mbr_gpt_com0/Makefile |    7 +
 sys/sys/bootblock.h                           |    8 +-
 14 files changed, 956 insertions(+), 26 deletions(-)

diffs (truncated from 1250 to 300 lines):

diff -r 358ca469eb5f -r 9e23d110128f distrib/sets/lists/base/md.amd64
--- a/distrib/sets/lists/base/md.amd64  Wed Jan 05 23:13:01 2011 +0000
+++ b/distrib/sets/lists/base/md.amd64  Thu Jan 06 01:08:48 2011 +0000
@@ -1,4 +1,4 @@
-# $NetBSD: md.amd64,v 1.106 2010/12/16 18:42:49 christos Exp $
+# $NetBSD: md.amd64,v 1.107 2011/01/06 01:08:48 jakllsch Exp $
 ./dev/lms0                                     base-obsolete           obsolete
 ./dev/mms0                                     base-obsolete           obsolete
 ./libexec/ld.elf_so-i386                       base-sys-shlib          compat,pic
@@ -298,6 +298,8 @@
 ./usr/mdec/mbr_com0                            base-sysutil-bin
 ./usr/mdec/mbr_com0_9600                       base-sysutil-bin
 ./usr/mdec/mbr_ext                             base-sysutil-bin
+./usr/mdec/mbr_gpt                             base-sysutil-bin
+./usr/mdec/mbr_gpt_com0                                base-sysutil-bin
 ./usr/mdec/netboot_3c509.rom                   base-sysutil-bin
 ./usr/mdec/netboot_3c590.rom                   base-sysutil-bin
 ./usr/mdec/netboot_3c90xb.rom                  base-sysutil-bin
diff -r 358ca469eb5f -r 9e23d110128f distrib/sets/lists/base/md.i386
--- a/distrib/sets/lists/base/md.i386   Wed Jan 05 23:13:01 2011 +0000
+++ b/distrib/sets/lists/base/md.i386   Thu Jan 06 01:08:48 2011 +0000
@@ -1,4 +1,4 @@
-# $NetBSD: md.i386,v 1.137 2010/09/11 13:06:36 tsutsui Exp $
+# $NetBSD: md.i386,v 1.138 2011/01/06 01:08:48 jakllsch Exp $
 ./dev/lms0                                     base-obsolete           obsolete
 ./dev/mms0                                     base-obsolete           obsolete
 ./dev/pms0                                     base-obsolete           obsolete
@@ -79,6 +79,8 @@
 ./usr/mdec/mbr_com0                            base-sysutil-bin
 ./usr/mdec/mbr_com0_9600                       base-sysutil-bin
 ./usr/mdec/mbr_ext                             base-sysutil-bin
+./usr/mdec/mbr_gpt                             base-sysutil-bin
+./usr/mdec/mbr_gpt_com0                                base-sysutil-bin
 ./usr/mdec/netboot_3c509.rom                   base-sysutil-bin
 ./usr/mdec/netboot_3c590.rom                   base-sysutil-bin
 ./usr/mdec/netboot_3c90xb.rom                  base-sysutil-bin
diff -r 358ca469eb5f -r 9e23d110128f sbin/gpt/Makefile
--- a/sbin/gpt/Makefile Wed Jan 05 23:13:01 2011 +0000
+++ b/sbin/gpt/Makefile Thu Jan 06 01:08:48 2011 +0000
@@ -1,9 +1,9 @@
-# $NetBSD: Makefile,v 1.3 2009/04/11 07:58:12 lukem Exp $
+# $NetBSD: Makefile,v 1.4 2011/01/06 01:08:48 jakllsch Exp $
 # $FreeBSD: src/sbin/gpt/Makefile,v 1.7 2005/09/01 02:49:20 marcel Exp $
 
 PROG=  gpt
-SRCS=  add.c create.c destroy.c gpt.c label.c map.c migrate.c recover.c \
-       remove.c show.c
+SRCS=  add.c biosboot.c create.c destroy.c gpt.c label.c map.c migrate.c \
+       recover.c remove.c show.c
 MAN=   gpt.8
 
 LDADD+=        -lprop -lutil
diff -r 358ca469eb5f -r 9e23d110128f sbin/gpt/biosboot.c
--- /dev/null   Thu Jan 01 00:00:00 1970 +0000
+++ b/sbin/gpt/biosboot.c       Thu Jan 06 01:08:48 2011 +0000
@@ -0,0 +1,326 @@
+/*     $NetBSD: biosboot.c,v 1.1 2011/01/06 01:08:48 jakllsch Exp $ */
+
+/*
+ * Copyright (c) 2009 The NetBSD Foundation, Inc. 
+ * All rights reserved.
+ *     
+ * This code is derived from software contributed to the NetBSD Foundation
+ * by Mike M. Volokhov. Development of this software was supported by the
+ * Google Summer of Code program.
+ * The GSoC project was mentored by Allen Briggs and Joerg Sonnenberger.
+ *
+ * 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.
+ *
+ * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. 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 FOUNDATION 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.
+ */
+
+#include <sys/cdefs.h>
+#ifdef __RCSID
+__RCSID("$NetBSD: biosboot.c,v 1.1 2011/01/06 01:08:48 jakllsch Exp $");
+#endif
+
+#include <sys/stat.h>
+#include <sys/types.h>
+#include <sys/ioctl.h>
+#include <sys/disk.h>
+#include <sys/param.h>
+#include <sys/bootblock.h>
+
+#include <err.h>
+#include <fcntl.h>
+#include <inttypes.h>
+#include <paths.h>
+#include <stddef.h>
+#include <stdio.h>
+#include <stdlib.h>
+#include <string.h>
+#include <unistd.h>
+#include <util.h>
+
+#include "map.h"
+#include "gpt.h"
+
+#define DEFAULT_BOOTDIR                "/usr/mdec"
+#define DEFAULT_BOOTCODE       "mbr_gpt"
+
+static daddr_t start;
+static uint64_t size;
+
+static char *bootpath;
+static unsigned int entry;
+
+const uuid_t uuid_mbr_guid_default = MBR_GPT_GUID_DEFAULT;
+
+const char biosbootmsg[] = "biosboot [-c bootcode] [-i index] device ...";
+
+static void
+usage_biosboot(void)
+{
+       fprintf(stderr, "usage: %s %s\n", getprogname(), biosbootmsg);
+       exit(1);
+}
+
+static struct mbr*
+read_boot(void)
+{
+       int bfd, ret = 0;
+       struct mbr *buf;
+       struct stat st;
+       uuid_t uuid_patch_magic;
+
+       /* XXX how to do the following better? */
+       if (bootpath == NULL) {
+               bootpath = strdup(DEFAULT_BOOTDIR "/" DEFAULT_BOOTCODE);
+       } else {
+               if (strchr(bootpath, '/') == 0) {
+                       char *p;
+                       if ((p = strdup(bootpath)) == NULL)
+                               err(1, "Malloc failed");
+                       free(bootpath);
+                       (void)asprintf(&bootpath, "%s/%s", DEFAULT_BOOTDIR, p);
+                       free(p);
+               }
+       }
+       if (bootpath == NULL)
+               err(1, "Malloc failed");
+
+       if ((buf = malloc((size_t)secsz)) == NULL)
+               err(1, "Malloc failed");
+
+       if ((bfd = open(bootpath, O_RDONLY)) < 0 || fstat(bfd, &st) == -1) {
+               warn("%s", bootpath);
+               goto fail;
+       }
+
+       if (st.st_size != secsz) {
+               warnx("%s: the bootcode does not match '%s' sector size",
+                       bootpath, device_name);
+               goto fail;
+       }
+
+       if (read(bfd, buf, secsz) != st.st_size) {
+               warn("%s", bootpath);
+               goto fail;
+       }
+
+       if (le32toh(buf->mbr_sig) != MBR_SIG) {
+               warnx("%s: invalid MBR magic", bootpath);
+               goto fail;
+       }
+
+       uuid_dec_le(&buf->mbr_code[MBR_GPT_GUID_OFFSET], &uuid_patch_magic);
+
+       /*
+        * The loader have to contain some magic in patchable area,
+        * such we can be sure that we won't trash something important
+        */
+       if (!uuid_equal(&uuid_patch_magic, &uuid_mbr_guid_default, NULL)) {
+               warnx("%s: the bootcode does not support required options",
+                       bootpath);
+               goto fail;
+       }
+
+       ret++;
+
+    fail:
+       if (bfd >= 0)
+               close(bfd);
+       if (ret == 0) {
+               free(buf);
+               buf = NULL;
+       }
+       return buf;
+}
+
+static void
+biosboot(int fd)
+{
+       map_t *gpt, *tpg;
+       map_t *tbl, *lbt;
+       map_t *mbrmap, *m;
+       struct mbr *mbr, *bootcode;
+       struct gpt_ent *pp;
+       uuid_t buuid;
+
+       /*
+        * Parse and validate partition maps
+        */
+       gpt = map_find(MAP_TYPE_PRI_GPT_HDR);
+       if (gpt == NULL) {
+               warnx("%s: error: no primary GPT header; run create or recover",
+                   device_name);
+               return;
+       }
+
+       tpg = map_find(MAP_TYPE_SEC_GPT_HDR);
+       if (tpg == NULL) {
+               warnx("%s: error: no secondary GPT header; run recover",
+                   device_name);
+               return;
+       }
+
+       tbl = map_find(MAP_TYPE_PRI_GPT_TBL);
+       lbt = map_find(MAP_TYPE_SEC_GPT_TBL);
+       if (tbl == NULL || lbt == NULL) {
+               warnx("%s: error: run recover -- trust me", device_name);
+               return;
+       }
+
+       mbrmap = map_find(MAP_TYPE_PMBR);
+       if (mbrmap == NULL || mbrmap->map_start != 0) {
+               warnx("%s: error: no valid Protective MBR found", device_name);
+               return;
+       }
+
+       mbr = mbrmap->map_data;
+
+       /*
+        * Update the boot code
+        */
+       if ((bootcode = read_boot()) == NULL) {
+               warnx("error reading bootcode");
+               return;
+       }
+       (void)memcpy(&mbr->mbr_code, &bootcode->mbr_code,
+               sizeof(mbr->mbr_code));
+       free(bootcode);
+
+       /*
+        * Walk through the GPT and see where we can boot from
+        */
+       for (m = map_first(); m != NULL; m = m->map_next) {
+               if (m->map_type != MAP_TYPE_GPT_PART || m->map_index < 1)
+                       continue;
+
+               pp = m->map_data;
+
+               /* first, prefer user selection */
+               if (entry > 0 && m->map_index == entry)
+                       break;
+
+               /* next, partition as could be specified by wedge */
+               if (entry < 1 && size > 0 &&
+                   m->map_start == start && m->map_size == (off_t)size)
+                       break;
+       }
+
+       if (m == NULL) {
+               warnx("error: no bootable partition");
+               return;
+       } else
+               uuid_dec_le(pp->ent_guid, &buuid);
+
+#if 1
+       {
+               char *p;
+
+               uuid_to_string(&buuid, &p, NULL);
+               printf("Boot device:     %s\n", device_name);
+               printf("Partition GUID:  %s\n", p);
+               printf("Boot code:       %s\n", bootpath);
+
+               free(p);
+       }
+#endif
+
+       uuid_enc_le(&mbr->mbr_code[MBR_GPT_GUID_OFFSET], &buuid);
+       if (gpt_write(fd, mbrmap) == -1) {



Home | Main Index | Thread Index | Old Index