Skip site navigation (1)Skip section navigation (2)
Date:      Wed, 3 Jun 2009 16:28:29 +0000 (UTC)
From:      Rafal Jaworowski <raj@FreeBSD.org>
To:        src-committers@freebsd.org, svn-src-all@freebsd.org, svn-src-head@freebsd.org
Subject:   svn commit: r193387 - head/sys/boot/uboot/lib
Message-ID:  <200906031628.n53GSTWV059888@svn.freebsd.org>

next in thread | raw e-mail | index | archive | help
Author: raj
Date: Wed Jun  3 16:28:29 2009
New Revision: 193387
URL: http://svn.freebsd.org/changeset/base/193387

Log:
  Make GPT style partitiong endian-safe in U-Boot support library.
  
  Submitted by:	Piotr Ziecik
  Obtained from:	Semihalf

Modified:
  head/sys/boot/uboot/lib/disk.c

Modified: head/sys/boot/uboot/lib/disk.c
==============================================================================
--- head/sys/boot/uboot/lib/disk.c	Wed Jun  3 16:11:27 2009	(r193386)
+++ head/sys/boot/uboot/lib/disk.c	Wed Jun  3 16:28:29 2009	(r193387)
@@ -34,6 +34,7 @@
 __FBSDID("$FreeBSD$");
 
 #include <sys/param.h>
+#include <sys/endian.h>
 #include <sys/queue.h>
 #include <netinet/in.h>
 #include <machine/stdarg.h>
@@ -122,6 +123,15 @@ struct devsw uboot_storage = {
 	stor_print
 };
 
+static void
+uuid_letoh(uuid_t *uuid)
+{
+
+	uuid->time_low = le32toh(uuid->time_low);
+	uuid->time_mid = le16toh(uuid->time_mid);
+	uuid->time_hi_and_version = le16toh(uuid->time_hi_and_version);
+}
+
 static int
 stor_init(void)
 {
@@ -251,7 +261,7 @@ stor_open_gpt(struct open_dev *od, struc
 	}
 
 	/* Check the slice table magic. */
-	if (*((uint16_t *)(buf + DOSMAGICOFFSET)) != DOSMAGIC) {
+	if (le16toh(*((uint16_t *)(buf + DOSMAGICOFFSET))) != DOSMAGIC) {
 		err = ENXIO;
 		goto out;
 	}
@@ -286,9 +296,10 @@ stor_open_gpt(struct open_dev *od, struc
 
 	/* Check GPT header */
 	if (bcmp(hdr->hdr_sig, GPT_HDR_SIG, sizeof(hdr->hdr_sig)) != 0 ||
-	    hdr->hdr_lba_self != 1 || hdr->hdr_revision < 0x00010000 ||
-	    hdr->hdr_entsz < sizeof(*ent) ||
-	    od->od_bsize % hdr->hdr_entsz != 0) {
+	    le64toh(hdr->hdr_lba_self) != 1 ||
+	    le32toh(hdr->hdr_revision) < 0x00010000 ||
+	    le32toh(hdr->hdr_entsz) < sizeof(*ent) ||
+	    od->od_bsize % le32toh(hdr->hdr_entsz) != 0) {
 		debugf("Invalid GPT header!\n");
 		err = EINVAL;
 		goto out;
@@ -296,9 +307,9 @@ stor_open_gpt(struct open_dev *od, struc
 
 	/* Count number of valid partitions */
 	part = 0;
-	eps = od->od_bsize / hdr->hdr_entsz;
-	slba = hdr->hdr_lba_table;
-	elba = slba + hdr->hdr_entries / eps;
+	eps = od->od_bsize / le32toh(hdr->hdr_entsz);
+	slba = le64toh(hdr->hdr_lba_table);
+	elba = slba + le32toh(hdr->hdr_entries) / eps;
 
 	for (lba = slba; lba < elba; lba++) {
 		err = stor_readdev(dev, lba, 1, buf);
@@ -312,8 +323,9 @@ stor_open_gpt(struct open_dev *od, struc
 
 		for (i = 0; i < eps; i++) {
 			if (uuid_is_nil(&ent[i].ent_type, NULL) ||
-			    ent[i].ent_lba_start == 0 ||
-			    ent[i].ent_lba_end < ent[i].ent_lba_start)
+			    le64toh(ent[i].ent_lba_start) == 0 ||
+			    le64toh(ent[i].ent_lba_end) <
+			    le64toh(ent[i].ent_lba_start))
 				continue;
 
 			part += 1;
@@ -343,8 +355,9 @@ stor_open_gpt(struct open_dev *od, struc
 
 			for (i = 0; i < eps; i++) {
 				if (uuid_is_nil(&ent[i].ent_type, NULL) ||
-				    ent[i].ent_lba_start == 0 ||
-				    ent[i].ent_lba_end < ent[i].ent_lba_start)
+				    le64toh(ent[i].ent_lba_start) == 0 ||
+				    le64toh(ent[i].ent_lba_end) <
+				    le64toh(ent[i].ent_lba_start))
 					continue;
 
 				od->od_partitions[part].gp_index = (lba - slba)
@@ -352,9 +365,11 @@ stor_open_gpt(struct open_dev *od, struc
 				od->od_partitions[part].gp_type =
 				    ent[i].ent_type;
 				od->od_partitions[part].gp_start =
-				    ent[i].ent_lba_start;
+				    le64toh(ent[i].ent_lba_start);
 				od->od_partitions[part].gp_end =
-				    ent[i].ent_lba_end;
+				    le64toh(ent[i].ent_lba_end);
+
+				uuid_letoh(&od->od_partitions[part].gp_type);
 				part += 1;
 			}
 		}



Want to link to this message? Use this URL: <https://mail-archive.FreeBSD.org/cgi/mid.cgi?200906031628.n53GSTWV059888>