From owner-svn-src-user@FreeBSD.ORG  Wed Jun 20 07:56:39 2012
Return-Path: <owner-svn-src-user@FreeBSD.ORG>
Delivered-To: svn-src-user@freebsd.org
Received: from mx1.freebsd.org (mx1.freebsd.org [IPv6:2001:4f8:fff6::34])
	by hub.freebsd.org (Postfix) with ESMTP id 4E5931065670;
	Wed, 20 Jun 2012 07:56:39 +0000 (UTC) (envelope-from ae@FreeBSD.org)
Received: from svn.freebsd.org (svn.freebsd.org [IPv6:2001:4f8:fff6::2c])
	by mx1.freebsd.org (Postfix) with ESMTP id 39D918FC08;
	Wed, 20 Jun 2012 07:56:39 +0000 (UTC)
Received: from svn.freebsd.org (localhost [127.0.0.1])
	by svn.freebsd.org (8.14.4/8.14.4) with ESMTP id q5K7udVW027517;
	Wed, 20 Jun 2012 07:56:39 GMT (envelope-from ae@svn.freebsd.org)
Received: (from ae@localhost)
	by svn.freebsd.org (8.14.4/8.14.4/Submit) id q5K7ud7j027515;
	Wed, 20 Jun 2012 07:56:39 GMT (envelope-from ae@svn.freebsd.org)
Message-Id: <201206200756.q5K7ud7j027515@svn.freebsd.org>
From: "Andrey V. Elsukov" <ae@FreeBSD.org>
Date: Wed, 20 Jun 2012 07:56:39 +0000 (UTC)
To: src-committers@freebsd.org, svn-src-user@freebsd.org
X-SVN-Group: user
MIME-Version: 1.0
Content-Type: text/plain; charset=UTF-8
Content-Transfer-Encoding: 8bit
Cc: 
Subject: svn commit: r237292 - user/ae/bootcode/sys/boot/common
X-BeenThere: svn-src-user@freebsd.org
X-Mailman-Version: 2.1.5
Precedence: list
List-Id: "SVN commit messages for the experimental &quot; user&quot;
	src tree" <svn-src-user.freebsd.org>
List-Unsubscribe: <http://lists.freebsd.org/mailman/listinfo/svn-src-user>,
	<mailto:svn-src-user-request@freebsd.org?subject=unsubscribe>
List-Archive: <http://lists.freebsd.org/pipermail/svn-src-user>
List-Post: <mailto:svn-src-user@freebsd.org>
List-Help: <mailto:svn-src-user-request@freebsd.org?subject=help>
List-Subscribe: <http://lists.freebsd.org/mailman/listinfo/svn-src-user>,
	<mailto:svn-src-user-request@freebsd.org?subject=subscribe>
X-List-Received-Date: Wed, 20 Jun 2012 07:56:39 -0000

Author: ae
Date: Wed Jun 20 07:56:38 2012
New Revision: 237292
URL: http://svn.freebsd.org/changeset/base/237292

Log:
  When primary GPT header is damaged and we are trying to find backup
  GPT header at the last LBA, check for GEOM metadata and skip them if
  found. This allows use GPT atop of the some GEOM providers and boot
  from.
  
  Submitted by:	hrs

Modified:
  user/ae/bootcode/sys/boot/common/gpt.c

Modified: user/ae/bootcode/sys/boot/common/gpt.c
==============================================================================
--- user/ae/bootcode/sys/boot/common/gpt.c	Wed Jun 20 07:38:52 2012	(r237291)
+++ user/ae/bootcode/sys/boot/common/gpt.c	Wed Jun 20 07:56:38 2012	(r237292)
@@ -40,6 +40,7 @@ __FBSDID("$FreeBSD$");
 #include "gpt.h"
 
 #define	MAXTBLENTS	128
+#define	GEOM_MAGIC	"GEOM::"
 
 static struct gpt_hdr hdr_primary, hdr_backup, *gpthdr;
 static uint64_t hdr_primary_lba, hdr_backup_lba;
@@ -345,8 +346,18 @@ gptread(const uuid_t *uuid, struct dsk *
 		altlba = hdr_primary.hdr_lba_alt;
 	} else {
 		altlba = drvsize(dskp);
-		if (altlba > 0)
-			altlba--;
+		if (altlba > 0) {
+			do {
+				altlba--;
+				/*
+				 * Check GEOM metadata and decrement
+				 * the altlba if found.
+				 */
+				if (drvread(dskp, secbuf, altlba, 1) != 0)
+					break;
+			} while (memcmp(secbuf, GEOM_MAGIC,
+			    sizeof(GEOM_MAGIC) - 1) == 0);
+		}
 	}
 	if (altlba == 0)
 		printf("%s: unable to locate backup GPT header\n", BOOTPROG);