File efibootmgr-0.5.4-sector-size.diff of Package efibootmgr

From b8af2c229031e274a2c61293a1a634e515a56d27 Mon Sep 17 00:00:00 2001
In-Reply-To: <1279121617-17961-1-git-send-email-pjones@redhat.com>
References: <1279121617-17961-1-git-send-email-pjones@redhat.com>
From: Peter Jones <pjones@redhat.com>
Date: Wed, 14 Jul 2010 14:55:17 -0400
Subject: [efibootmgr patch] Handle sector_size != 512.

Disks can have 4kB sectors now, so don't just bail out when that's the
case.
---
 src/include/disk.h |    3 +++
 src/lib/disk.c     |   42 ++++++++++++++++++++++++++++++++----------
 src/lib/gpt.c      |   24 +++++++++++-------------
 3 files changed, 46 insertions(+), 23 deletions(-)

--- a/src/include/disk.h
+++ b/src/include/disk.h
@@ -65,6 +65,9 @@ enum _interface_type {interface_type_unk
 		      ata, atapi, scsi, usb,
 		      i1394, fibre, i2o, md};
 
+
+unsigned int lcm(unsigned int x, unsigned int y);
+
 int disk_get_pci(int fd,
 		 unsigned char *bus,
 		 unsigned char *device,
--- a/src/lib/disk.c
+++ b/src/lib/disk.c
@@ -420,6 +420,27 @@ get_sector_size(int filedes)
 	return sector_size;
 }
 
+/************************************************************
+ * lcm
+ * Requires:
+ * - numbers of which to find the lowest common multiple
+ * Modifies: nothing
+ * Returns:
+ *  lowest common multiple of x and y
+ ************************************************************/
+unsigned int
+lcm(unsigned int x, unsigned int y)
+{
+	unsigned int m = x, n = y, o;
+
+	while ((o = m % n)) {
+		m = n;
+		n = o;
+	}
+
+	return (x / n) * y;
+}
+
 /**
  * disk_get_partition_info()
  *  @fd - open file descriptor to disk
@@ -442,26 +463,26 @@ disk_get_partition_info (int fd,
 			 uint8_t *mbr_type, uint8_t *signature_type)
 {
 	legacy_mbr *mbr;
-	void *mbr_unaligned;
+	void *mbr_sector;
+	size_t mbr_size;
 	off_t offset;
 	int this_bytes_read = 0;
 	int gpt_invalid=0, mbr_invalid=0;
 	int rc=0;
 	int sector_size = get_sector_size(fd);
 
-	if (sizeof(*mbr) != sector_size)
-		return 1;
-	mbr_unaligned = malloc(sizeof(*mbr)+sector_size-1);
-	mbr = (legacy_mbr *)
-		(((unsigned long)mbr_unaligned + sector_size - 1) &
-		 ~(unsigned long)(sector_size-1));
-	memset(mbr, 0, sizeof(*mbr));
+	mbr_size = lcm(sizeof(*mbr), sector_size);
+	if ((rc = posix_memalign(&mbr_sector, sector_size, mbr_size)) != 0)
+		goto error;
+	memset(mbr_sector, '\0', mbr_size);
+
 	offset = lseek(fd, 0, SEEK_SET);
-	this_bytes_read = read(fd, mbr, sizeof(*mbr));
+	this_bytes_read = read(fd, mbr_sector, mbr_size);
 	if (this_bytes_read < sizeof(*mbr)) {
 		rc=1;
 		goto error_free_mbr;
 	}
+	mbr = (legacy_mbr *)mbr_sector;
 	gpt_invalid = gpt_disk_get_partition_info(fd, num,
 						  start, size,
 						  signature,
@@ -479,7 +500,8 @@ disk_get_partition_info (int fd,
 		}
 	}
  error_free_mbr:
-	free(mbr_unaligned);
+	free(mbr_sector);
+ error:
 	return rc;
 }
 
--- a/src/lib/gpt.c
+++ b/src/lib/gpt.c
@@ -218,23 +218,21 @@ read_lba(int fd, uint64_t lba, void *buf
 	int sector_size = get_sector_size(fd);
 	off_t offset = lba * sector_size;
         ssize_t bytesread;
-        void *aligned;
-        void *unaligned;
+        void *iobuf;
+        size_t iobuf_size;
+        int rc;
 
-        if (bytes % sector_size)
-                return EINVAL;
-
-	unaligned = malloc(bytes+sector_size-1);
-	aligned = (void *)
-		(((unsigned long)unaligned + sector_size - 1) &
-		 ~(unsigned long)(sector_size-1));
-	memset(aligned, 0, bytes);
+        iobuf_size = lcm(bytes, sector_size);
+        rc = posix_memalign(&iobuf, sector_size, iobuf_size);
+        if (rc)
+                return rc;
+        memset(iobuf, 0, bytes);
 
 
 	lseek(fd, offset, SEEK_SET);
-	bytesread = read(fd, aligned, bytes);
-        memcpy(buffer, aligned, bytesread);
-        free(unaligned);
+        bytesread = read(fd, iobuf, iobuf_size);
+        memcpy(buffer, iobuf, bytes);
+        free(iobuf);
 
         /* Kludge.  This is necessary to read/write the last
            block of an odd-sized disk, until Linux 2.5.x kernel fixes.
openSUSE Build Service is sponsored by