X-Git-Url: https://git.gag.com/?a=blobdiff_plain;f=src%2Flib%2Fgpt.c;h=83e7a94fe3eed3231a9b19d2763c29bb2f6818f3;hb=457fe3ea877fb535743a540f634bc406037b7be6;hp=d90ddaf235c072ffe3626c0f358345f1042a59d9;hpb=53e41690e9d8c650b4cda79ee2dbadc08e5d5ab4;p=debian%2Fefibootmgr diff --git a/src/lib/gpt.c b/src/lib/gpt.c index d90ddaf..83e7a94 100644 --- a/src/lib/gpt.c +++ b/src/lib/gpt.c @@ -215,26 +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); - free(unaligned); + 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.