incorporate Fedora patch for sector sizes > 512 bytes
[debian/efibootmgr] / src / lib / gpt.c
index e4985d1353bf401fa9a0f1557ca98717f2c9a855..83e7a94fe3eed3231a9b19d2763c29bb2f6818f3 100644 (file)
@@ -22,8 +22,6 @@
     Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
 */
 
-#define _FILE_OFFSET_BITS 64
-
 #include <stdio.h>
 #include <string.h>
 #include <stdlib.h>
@@ -43,7 +41,7 @@
 #define BLKGETLASTSECT  _IO(0x12,108) /* get last sector of block device */
 #define BLKGETSIZE _IO(0x12,96)        /* return device size */
 #define BLKSSZGET  _IO(0x12,104)       /* get block device sector size */
-#define BLKGETSIZE64 _IOR(0x12,114,sizeof(uint64_t))   /* return device size in bytes (u64 *arg) */
+#define BLKGETSIZE64 _IOR(0x12,114,uint64_t)   /* return device size in bytes (u64 *arg) */
 
 struct blkdev_ioctl_param {
         unsigned int block;
@@ -217,25 +215,24 @@ read_lastoddsector(int fd, uint64_t lba, void *buffer, size_t count)
 static ssize_t
 read_lba(int fd, uint64_t lba, void *buffer, size_t bytes)
 {
-       int sector_size = get_sector_size(fd);
-       off_t offset = lba * sector_size;
+        int sector_size = get_sector_size(fd);
+        off_t offset = lba * sector_size;
         ssize_t bytesread;
-        void *aligned;
-        void *unaligned;
-
-        if (bytes % sector_size)
-                return EINVAL;
+        void *iobuf;
+        size_t iobuf_size;
+        int rc;
 
-       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);
+        lseek(fd, offset, SEEK_SET);
+        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.
@@ -625,9 +622,8 @@ gpt_disk_get_partition_info(int fd,
                memcpy(signature, &p->unique_partition_guid,
                       sizeof (p->unique_partition_guid));
        } else {
-               *start = 0;
-               *size = last_lba(fd) + 1;
-               memcpy(signature, &gpt->disk_guid, sizeof (gpt->disk_guid));
+               fprintf (stderr,"partition %d is not valid\n", num);
+               return 1;
        }
        return 0;
 }