#include "project.h" #include #include DISK * disk_open (char *fn) { DISK *ret = calloc (1, sizeof (DISK)); off_t o; ret->fd = open (fn, O_RDWR); if (ret->fd < 0) { free (ret); return NULL; } ret->name = strdup (fn); ret->lbas = lseek (ret->fd, (off_t) 0, SEEK_END); ret->lbas = ret->lbas / (uint64_t) SECTOR_SIZE; fprintf (stderr, "Opened %s with size of %lld lbas\n", ret->name, (long long) ret->lbas); return ret; } void disk_read (DISK * d, void *buf, uint64_t lba, int lbas) { lba *= SECTOR_SIZE; if (lseek (d->fd, lba, SEEK_SET) != lba) { perror ("lseek"); abort (); } lbas *= SECTOR_SIZE; if (read (d->fd, buf, lbas) != lbas) { perror ("read"); // abort (); } } void disk_write (DISK * d, void *buf, uint64_t lba, int lbas) { lba *= SECTOR_SIZE; if (lseek (d->fd, lba, SEEK_SET) != lba) { perror ("lseek"); abort (); } lbas *= SECTOR_SIZE; if (write (d->fd, buf, lbas) != lbas) { perror ("write"); abort (); } } uint64_t disk_lbas (DISK * d) { return d->lbas; } disk_reread_kernel_table (DISK * d) { if (ioctl (d->fd, BLKRRPART, NULL)) { perror ("Telling kernel to re-read partition table failed:"); } else { fprintf (stderr, "Kernel has re-read partition table\n"); } }