:: commit 82a715142a59315981e1ea92385a964a51403ded

mintsuki <mintsuki@protonmail.com> — 2020-11-02 09:17

parents: c542ff6845

part: Some general cleanup

diff --git a/limine.bin b/limine.bin
index 72b6abbc..211982d2 100644
Binary files a/limine.bin and b/limine.bin differ
diff --git a/stage2/drivers/disk.c b/stage2/drivers/disk.c
index 693a3363..2df218ee 100644
--- a/stage2/drivers/disk.c
+++ b/stage2/drivers/disk.c
@@ -1,10 +1,9 @@
 #include <stdint.h>
 #include <stddef.h>
-#include <lib/libc.h>
 #include <drivers/disk.h>
+#include <lib/libc.h>
 #include <lib/real.h>
 #include <lib/blib.h>
-#include <lib/part.h>
 #include <lib/print.h>
 #include <mm/pmm.h>
 
@@ -61,7 +60,7 @@ static int cache_block(int drive, uint64_t block, int sector_size) {
     return 0;
 }
 
-int read(int drive, void *buffer, uint64_t loc, uint64_t count) {
+int disk_read(int drive, void *buffer, uint64_t loc, uint64_t count) {
     struct rm_regs r = {0};
     struct bios_drive_params drive_params;
 
@@ -100,7 +99,3 @@ int read(int drive, void *buffer, uint64_t loc, uint64_t count) {
 
     return 0;
 }
-
-int read_partition(int drive, struct part *part, void *buffer, uint64_t loc, uint64_t count) {
-    return read(drive, buffer, loc + (part->first_sect * 512), count);
-}
diff --git a/stage2/drivers/disk.h b/stage2/drivers/disk.h
index 843d0fac..166264b2 100644
--- a/stage2/drivers/disk.h
+++ b/stage2/drivers/disk.h
@@ -1,9 +1,7 @@
 #ifndef __DRIVERS__DISK_H__
 #define __DRIVERS__DISK_H__
 
-#include <stddef.h>
 #include <stdint.h>
-#include <lib/part.h>
 
 struct bios_drive_params {
     uint16_t buf_size;
@@ -16,7 +14,6 @@ struct bios_drive_params {
     uint32_t edd;
 } __attribute__((packed));
 
-int read(int drive, void *buffer, uint64_t loc, uint64_t count);
-int read_partition(int drive, struct part *part, void *buffer, uint64_t loc, uint64_t count);
+int disk_read(int drive, void *buffer, uint64_t loc, uint64_t count);
 
 #endif
diff --git a/stage2/fs/echfs.c b/stage2/fs/echfs.c
index 806f6370..ab9cadd5 100644
--- a/stage2/fs/echfs.c
+++ b/stage2/fs/echfs.c
@@ -23,7 +23,7 @@ struct echfs_identity_table {
 #define DIR_TYPE     1
 
 static int read_block(struct echfs_file_handle *file, void *buf, uint64_t block, uint64_t offset, uint64_t count) {
-    return read_partition(file->disk, &file->part, buf, (file->alloc_map[block] * file->block_size) + offset, count);
+    return part_read(&file->part, buf, (file->alloc_map[block] * file->block_size) + offset, count);
 }
 
 int echfs_read(struct echfs_file_handle *file, void *buf, uint64_t loc, uint64_t count) {
@@ -44,7 +44,7 @@ int echfs_read(struct echfs_file_handle *file, void *buf, uint64_t loc, uint64_t
 
 int echfs_check_signature(struct part *part) {
     struct echfs_identity_table id_table;
-    read_partition(part->drive, part, &id_table, 0, sizeof(struct echfs_identity_table));
+    part_read(part, &id_table, 0, sizeof(struct echfs_identity_table));
 
     if (strncmp(id_table.signature, "_ECH_FS_", 8)) {
         return 0;
@@ -55,7 +55,7 @@ int echfs_check_signature(struct part *part) {
 
 bool echfs_get_guid(struct guid *guid, struct part *part) {
     struct echfs_identity_table id_table;
-    read_partition(part->drive, part, &id_table, 0, sizeof(struct echfs_identity_table));
+    part_read(part, &id_table, 0, sizeof(struct echfs_identity_table));
 
     if (strncmp(id_table.signature, "_ECH_FS_", 8)) {
         return false;
@@ -66,17 +66,14 @@ bool echfs_get_guid(struct guid *guid, struct part *part) {
     return true;
 }
 
-int echfs_open(struct echfs_file_handle *ret, int disk, int partition, const char *path) {
+int echfs_open(struct echfs_file_handle *ret, struct part *part, const char *path) {
     const char *fullpath = path;
 
-    ret->disk = disk;
-
-    if (get_part(&ret->part, disk, partition)) {
-        panic("Invalid partition");
-    }
+    ret->disk = part->drive;
+    ret->part = *part;
 
     struct echfs_identity_table id_table;
-    read_partition(disk, &ret->part, &id_table, 0, sizeof(struct echfs_identity_table));
+    part_read(&ret->part, &id_table, 0, sizeof(struct echfs_identity_table));
 
     if (strncmp(id_table.signature, "_ECH_FS_", 8)) {
         print("echfs: signature invalid\n");
@@ -109,7 +106,7 @@ next:;
     }
 
     for (uint64_t i = 0; i < ret->dir_length; i += sizeof(struct echfs_dir_entry)) {
-        read_partition(disk, &ret->part, &ret->dir_entry, i + ret->dir_offset, sizeof(struct echfs_dir_entry));
+        part_read(&ret->part, &ret->dir_entry, i + ret->dir_offset, sizeof(struct echfs_dir_entry));
 
         if (!ret->dir_entry.parent_id) {
             break;
@@ -139,7 +136,7 @@ found:;
     ret->alloc_map[0] = ret->dir_entry.payload;
     for (uint64_t i = 1; i < file_block_count; i++) {
         // Read the next block.
-        read_partition(ret->disk, &ret->part,
+        part_read(&ret->part,
             &ret->alloc_map[i],
             ret->alloc_table_offset + ret->alloc_map[i-1] * sizeof(uint64_t),
             sizeof(uint64_t));
diff --git a/stage2/fs/echfs.h b/stage2/fs/echfs.h
index a3eb7b3e..4aacad2e 100644
--- a/stage2/fs/echfs.h
+++ b/stage2/fs/echfs.h
@@ -36,7 +36,7 @@ struct echfs_file_handle {
 int echfs_check_signature(struct part *part);
 bool echfs_get_guid(struct guid *guid, struct part *part);
 
-int echfs_open(struct echfs_file_handle *ret, int disk, int partition, const char *filename);
+int echfs_open(struct echfs_file_handle *ret, struct part *part, const char *filename);
 int echfs_read(struct echfs_file_handle *file, void *buf, uint64_t loc, uint64_t count);
 
 #endif
diff --git a/stage2/fs/ext2.c b/stage2/fs/ext2.c
index ef29d1da..e1d20a77 100644
--- a/stage2/fs/ext2.c
+++ b/stage2/fs/ext2.c
@@ -157,10 +157,10 @@ struct ext4_extent_idx {
 

 static int inode_read(void *buf, uint64_t loc, uint64_t count,

                       uint64_t block_size, struct ext2_inode *inode,

-                      uint64_t drive, struct part *part);

+                      struct part *part);

 

 // parse an inode given the partition base and inode number

-static int ext2_get_inode(struct ext2_inode *ret, uint64_t drive, struct part *part,

+static int ext2_get_inode(struct ext2_inode *ret, struct part *part,

                           uint64_t inode, struct ext2_superblock *sb) {

     if (inode == 0)

         return -1;

@@ -188,7 +188,7 @@ static int ext2_get_inode(struct ext2_inode *ret, uint64_t drive, struct part *p
         struct ext2_bgd target_descriptor;

         const uint64_t bgd_offset = bgd_start_offset + (sizeof(struct ext2_bgd) * ino_blk_grp);

 

-        read_partition(drive, part, &target_descriptor, bgd_offset, sizeof(struct ext2_bgd));

+        part_read(part, &target_descriptor, bgd_offset, sizeof(struct ext2_bgd));

 

         ino_offset = ((target_descriptor.bg_inode_table) * block_size) +

                                     (ino_size * ino_tbl_idx);

@@ -196,13 +196,13 @@ static int ext2_get_inode(struct ext2_inode *ret, uint64_t drive, struct part *p
         struct ext4_bgd target_descriptor;

         const uint64_t bgd_offset = bgd_start_offset + (sizeof(struct ext4_bgd) * ino_blk_grp);

 

-        read_partition(drive, part, &target_descriptor, bgd_offset, sizeof(struct ext4_bgd));

+        part_read(part, &target_descriptor, bgd_offset, sizeof(struct ext4_bgd));

 

         ino_offset = ((target_descriptor.bg_inode_table | (bit64 ? ((uint64_t)target_descriptor.inode_id_hi << 32) : 0)) * block_size) +

                                     (ino_size * ino_tbl_idx);

     }

 

-    read_partition(drive, part, ret, ino_offset, sizeof(struct ext2_inode));

+    part_read(part, ret, ino_offset, sizeof(struct ext2_inode));

 

     return 0;

 }

@@ -230,14 +230,14 @@ next:
         // preliminary read

         inode_read(dir, i, sizeof(struct ext2_dir_entry),

                    fd->block_size, &current_inode,

-                   fd->drive, &fd->part);

+                   &fd->part);

 

         // name read

         char name[dir->name_len + 1];

 

         memset(name, 0, dir->name_len + 1);

         inode_read(name, i + sizeof(struct ext2_dir_entry), dir->name_len,

-                   fd->block_size, &current_inode, fd->drive, &fd->part);

+                   fd->block_size, &current_inode, &fd->part);

 

         int r = strcmp(token, name);

 

@@ -246,7 +246,7 @@ next:
                 return 0;

             } else {

                 // update the current inode

-                ext2_get_inode(&current_inode, fd->drive, &fd->part, dir->inode, sb);

+                ext2_get_inode(&current_inode, &fd->part, dir->inode, sb);

                 goto next;

             }

         }

@@ -257,21 +257,19 @@ next:
     return -1;

 }

 

-int ext2_open(struct ext2_file_handle *ret, int drive, int partition, const char *path) {

-    if (get_part(&ret->part, drive, partition))

-        panic("Invalid partition");

-

-    ret->drive = drive;

+int ext2_open(struct ext2_file_handle *ret, struct part *part, const char *path) {

+    ret->part  = *part;

+    ret->drive = part->drive;

 

     struct ext2_superblock sb;

-    read_partition(drive, &ret->part, &sb, 1024, sizeof(struct ext2_superblock));

+    part_read(&ret->part, &sb, 1024, sizeof(struct ext2_superblock));

 

     if (sb.s_state == EXT2_FS_UNRECOVERABLE_ERRORS)

         panic("EXT2: unrecoverable errors found\n");

 

     ret->block_size = ((uint64_t)1024 << sb.s_log_block_size);

 

-    ext2_get_inode(&ret->root_inode, drive, &ret->part, 2, &sb);

+    ext2_get_inode(&ret->root_inode, &ret->part, 2, &sb);

 

     struct ext2_dir_entry entry;

     int r = ext2_parse_dirent(&entry, ret, &sb, path);

@@ -279,7 +277,7 @@ int ext2_open(struct ext2_file_handle *ret, int drive, int partition, const char
     if (r)

         return r;

 

-    ext2_get_inode(&ret->inode, drive, &ret->part, entry.inode, &sb);

+    ext2_get_inode(&ret->inode, &ret->part, entry.inode, &sb);

     ret->size = ret->inode.i_size;

 

     if ((ret->inode.i_mode & 0xf000) == EXT2_INO_DIRECTORY)

@@ -290,10 +288,10 @@ int ext2_open(struct ext2_file_handle *ret, int drive, int partition, const char
 

 int ext2_read(struct ext2_file_handle *file, void *buf, uint64_t loc, uint64_t count) {

     return inode_read(buf, loc, count, file->block_size, &file->inode,

-                      file->drive, &file->part);

+                      &file->part);

 }

 

-static struct ext4_extent_header* ext4_find_leaf(struct ext4_extent_header* ext_block, uint32_t read_block, uint64_t block_size, uint64_t drive, struct part *part) {

+static struct ext4_extent_header* ext4_find_leaf(struct ext4_extent_header* ext_block, uint32_t read_block, uint64_t block_size, struct part *part) {

     struct ext4_extent_idx* index;

     void* buf = NULL;

 

@@ -320,14 +318,14 @@ static struct ext4_extent_header* ext4_find_leaf(struct ext4_extent_header* ext_
         uint64_t block = ((uint64_t)index[i].leaf_hi << 32) | index[i].leaf;

         if(!buf)

             buf = conv_mem_alloc(block_size);

-        read_partition(drive, part, buf, (block * block_size), block_size);

+        part_read(part, buf, (block * block_size), block_size);

         ext_block = buf;

     }

 }

 

 static int inode_read(void *buf, uint64_t loc, uint64_t count,

                       uint64_t block_size, struct ext2_inode *inode,

-                      uint64_t drive, struct part *part) {

+                      struct part *part) {

     for (uint64_t progress = 0; progress < count;) {

         uint64_t block = (loc + progress) / block_size;

 

@@ -343,7 +341,7 @@ static int inode_read(void *buf, uint64_t loc, uint64_t count,
             struct ext4_extent *ext;

             int i;

 

-            leaf = ext4_find_leaf((struct ext4_extent_header*)inode->i_blocks, block, block_size, drive, part);

+            leaf = ext4_find_leaf((struct ext4_extent_header*)inode->i_blocks, block, block_size, part);

 

             if (!leaf)

                 panic("invalid extent");

@@ -382,26 +380,26 @@ static int inode_read(void *buf, uint64_t loc, uint64_t count,
                 }

                 uint32_t offset = block % (block_size / sizeof(uint32_t));

                 uint32_t indirect_block;

-                read_partition(

-                    drive, part, &indirect_block,

+                part_read(

+                    part, &indirect_block,

                     inode->i_blocks[13] * block_size + index * sizeof(uint32_t),

                     sizeof(uint32_t)

                 );

-                read_partition(

-                    drive, part, &block_index,

+                part_read(

+                    part, &block_index,

                     indirect_block * block_size + offset * sizeof(uint32_t),

                     sizeof(uint32_t)

                 );

             } else {

-                read_partition(

-                    drive, part, &block_index,

+                part_read(

+                    part, &block_index,

                     inode->i_blocks[12] * block_size + block * sizeof(uint32_t),

                     sizeof(uint32_t)

                 );

             }

         }

 

-        read_partition(drive, part, buf + progress, (block_index * block_size) + offset, chunk);

+        part_read(part, buf + progress, (block_index * block_size) + offset, chunk);

 

         progress += chunk;

     }

@@ -413,7 +411,7 @@ static int inode_read(void *buf, uint64_t loc, uint64_t count,
 // and checks if all features are supported

 int ext2_check_signature(struct part *part) {

     struct ext2_superblock sb;

-    read_partition(part->drive, part, &sb, 1024, sizeof(struct ext2_superblock));

+    part_read(part, &sb, 1024, sizeof(struct ext2_superblock));

 

     if (sb.s_magic != EXT2_S_MAGIC)

         return 0;

@@ -433,7 +431,7 @@ int ext2_check_signature(struct part *part) {
 

 bool ext2_get_guid(struct guid *guid, struct part *part) {

     struct ext2_superblock sb;

-    read_partition(part->drive, part, &sb, 1024, sizeof(struct ext2_superblock));

+    part_read(part, &sb, 1024, sizeof(struct ext2_superblock));

 

     if (sb.s_magic != EXT2_S_MAGIC)

         return false;

diff --git a/stage2/fs/ext2.h b/stage2/fs/ext2.h
index c59b8c10..f69e4ad6 100644
--- a/stage2/fs/ext2.h
+++ b/stage2/fs/ext2.h
@@ -59,7 +59,7 @@ struct ext2_file_handle {
 int ext2_check_signature(struct part *part);

 bool ext2_get_guid(struct guid *guid, struct part *part);

 

-int ext2_open(struct ext2_file_handle *ret, int drive, int partition, const char *path);

+int ext2_open(struct ext2_file_handle *ret, struct part *part, const char *path);

 int ext2_read(struct ext2_file_handle *file, void *buf, uint64_t loc, uint64_t count);

 

 #endif

diff --git a/stage2/fs/fat32.c b/stage2/fs/fat32.c
index 7361d50e..062ebd1e 100644
--- a/stage2/fs/fat32.c
+++ b/stage2/fs/fat32.c
@@ -71,7 +71,7 @@ static int fat32_init_context(struct fat32_context* context, struct part *part)
     context->drive = part->drive;
 
     struct fat32_bpb bpb;
-    read_partition(part->drive, &context->part, &bpb, 0, sizeof(struct fat32_bpb));
+    part_read(&context->part, &bpb, 0, sizeof(struct fat32_bpb));
 
     if (bpb.signature != FAT32_VALID_SIGNATURE_1 && bpb.signature != FAT32_VALID_SIGNATURE_2) {
         return 1;
@@ -98,7 +98,7 @@ static int fat32_read_cluster_from_map(struct fat32_context* context, uint32_t c
     const uint32_t offset = cluster % (FAT32_SECTOR_SIZE / 4);
 
     uint32_t clusters[FAT32_SECTOR_SIZE / sizeof(uint32_t)];
-    int r = read_partition(context->drive, &context->part, &clusters[0], (context->fat_start_lba + sector) * FAT32_SECTOR_SIZE, sizeof(clusters));
+    int r = part_read(&context->part, &clusters[0], (context->fat_start_lba + sector) * FAT32_SECTOR_SIZE, sizeof(clusters));
 
     if (r) {
         return r;
@@ -110,7 +110,7 @@ static int fat32_read_cluster_from_map(struct fat32_context* context, uint32_t c
 
 static int fat32_load_fat_cluster_to_memory(struct fat32_context* context, uint32_t cluster_number, void* buffer, uint32_t offset, uint32_t limit) {
     const uint32_t sector = context->data_start_lba + (cluster_number - 2) * context->sectors_per_cluster;
-    return read_partition(context->drive, &context->part, buffer, ((uint64_t) sector) * FAT32_SECTOR_SIZE + offset, limit);
+    return part_read(&context->part, buffer, ((uint64_t) sector) * FAT32_SECTOR_SIZE + offset, limit);
 }
 
 // Copy ucs-2 characters to char*
@@ -226,14 +226,9 @@ int fat32_check_signature(struct part *part) {
     return fat32_init_context(&context, part) == 0;
 }
 
-int fat32_open(struct fat32_file_handle* ret, int disk, int partition, const char* path) {
-    struct part part;
-    if (get_part(&part, disk, partition)) {
-        panic("Invalid partition");
-    }
-
+int fat32_open(struct fat32_file_handle* ret, struct part *part, const char* path) {
     struct fat32_context context;
-    int r = fat32_init_context(&context, &part);
+    int r = fat32_init_context(&context, part);
 
     if (r) {
         print("fat32: context init failure (%d)\n", r);
diff --git a/stage2/fs/fat32.h b/stage2/fs/fat32.h
index 8697a40f..20990c3c 100644
--- a/stage2/fs/fat32.h
+++ b/stage2/fs/fat32.h
@@ -26,7 +26,7 @@ struct fat32_file_handle {
 
 int fat32_check_signature(struct part *part);
 
-int fat32_open(struct fat32_file_handle *ret, int disk, int partition, const char *path);
+int fat32_open(struct fat32_file_handle *ret, struct part *part, const char *path);
 int fat32_read(struct fat32_file_handle *file, void *buf, uint64_t loc, uint64_t count);
 
 #endif
diff --git a/stage2/fs/file.c b/stage2/fs/file.c
index 4dec2d9e..cfd04e7c 100644
--- a/stage2/fs/file.c
+++ b/stage2/fs/file.c
@@ -19,57 +19,49 @@ bool fs_get_guid(struct guid *guid, struct part *part) {
     return false;
 }
 
-int fopen(struct file_handle *ret, int disk, int partition, const char *filename) {
-    struct part part;
-    if (get_part(&part, disk, partition)) {
-        panic("Invalid partition");
-    }
-
-    if (echfs_check_signature(&part)) {
+int fopen(struct file_handle *ret, struct part *part, const char *filename) {
+    if (echfs_check_signature(part)) {
         struct echfs_file_handle *fd = conv_mem_alloc(sizeof(struct echfs_file_handle));
 
-        int r = echfs_open(fd, disk, partition, filename);
+        int r = echfs_open(fd, part, filename);
         if (r)
             return r;
 
-        ret->fd        = (void *)fd;
-        ret->read      = (void *)echfs_read;
-        ret->disk      = disk;
-        ret->partition = partition;
-        ret->size      = fd->dir_entry.size;
+        ret->fd   = (void *)fd;
+        ret->read = (void *)echfs_read;
+        ret->part = *part;
+        ret->size = fd->dir_entry.size;
 
         return 0;
     }
 
-    if (ext2_check_signature(&part)) {
+    if (ext2_check_signature(part)) {
         struct ext2_file_handle *fd = conv_mem_alloc(sizeof(struct ext2_file_handle));
 
-        int r = ext2_open(fd, disk, partition, filename);
+        int r = ext2_open(fd, part, filename);
         if (r)
             return r;
 
-        ret->fd        = (void *)fd;
-        ret->read      = (void *)ext2_read;
-        ret->disk      = disk;
-        ret->partition = partition;
-        ret->size      = fd->size;
+        ret->fd   = (void *)fd;
+        ret->read = (void *)ext2_read;
+        ret->part = *part;
+        ret->size = fd->size;
 
         return 0;
     }
 
-    if (fat32_check_signature(&part)) {
+    if (fat32_check_signature(part)) {
         struct fat32_file_handle *fd = conv_mem_alloc(sizeof(struct fat32_file_handle));
 
-        int r = fat32_open(fd, disk, partition, filename);
+        int r = fat32_open(fd, part, filename);
 
         if (r)
             return r;
 
-        ret->fd        = (void *)fd;
-        ret->read      = (void *)fat32_read;
-        ret->disk      = disk;
-        ret->partition = partition;
-        ret->size      = fd->size_bytes;
+        ret->fd   = (void *)fd;
+        ret->read = (void *)fat32_read;
+        ret->part = *part;
+        ret->size = fd->size_bytes;
 
         return 0;
     }
diff --git a/stage2/fs/file.h b/stage2/fs/file.h
index dfea68b3..c91d906b 100644
--- a/stage2/fs/file.h
+++ b/stage2/fs/file.h
@@ -3,21 +3,18 @@
 
 #include <stdint.h>
 #include <stdbool.h>
-
-struct part;
-struct guid;
+#include <lib/part.h>
 
 bool fs_get_guid(struct guid *guid, struct part *part);
 
 struct file_handle {
-    int        disk;
-    int        partition;
+    struct part part;
     void      *fd;
     int      (*read)(void *fd, void *buf, uint64_t loc, uint64_t count);
     uint64_t   size;
 };
 
-int fopen(struct file_handle *ret, int disk, int partition, const char *filename);
+int fopen(struct file_handle *ret, struct part *part, const char *filename);
 int fread(struct file_handle *fd, void *buf, uint64_t loc, uint64_t count);
 
 #endif
diff --git a/stage2/lib/config.c b/stage2/lib/config.c
index 28723455..051a473c 100644
--- a/stage2/lib/config.c
+++ b/stage2/lib/config.c
@@ -12,11 +12,11 @@ bool config_ready = false;
 
 static char *config_addr;
 
-int init_config(int drive, int part) {
+int init_config(struct part *part) {
     struct file_handle f;
 
-    if (fopen(&f, drive, part, "/limine.cfg")) {
-        if (fopen(&f, drive, part, "/boot/limine.cfg")) {
+    if (fopen(&f, part, "/limine.cfg")) {
+        if (fopen(&f, part, "/boot/limine.cfg")) {
             return -1;
         }
     }
diff --git a/stage2/lib/config.h b/stage2/lib/config.h
index f5c2adc3..959d0a67 100644
--- a/stage2/lib/config.h
+++ b/stage2/lib/config.h
@@ -3,10 +3,11 @@
 
 #include <stddef.h>
 #include <stdbool.h>
+#include <lib/part.h>
 
 extern bool config_ready;
 
-int init_config(int drive, int part);
+int init_config(struct part *part);
 int config_get_entry_name(char *ret, size_t index, size_t limit);
 int config_set_entry(size_t index);
 char *config_get_value(char *buf, size_t index, size_t limit, const char *key);
diff --git a/stage2/lib/part.c b/stage2/lib/part.c
index 2e1a84db..ef4083fb 100644
--- a/stage2/lib/part.c
+++ b/stage2/lib/part.c
@@ -54,7 +54,7 @@ static int gpt_get_part(struct part *ret, int drive, int partition) {
     struct gpt_table_header header = {0};
 
     // read header, located after the first block
-    read(drive, &header, 512, sizeof(header));
+    disk_read(drive, &header, 512, sizeof(header));
 
     // check the header
     // 'EFI PART'
@@ -68,7 +68,7 @@ static int gpt_get_part(struct part *ret, int drive, int partition) {
         return END_OF_TABLE;
 
     struct gpt_entry entry = {0};
-    read(drive, &entry,
+    disk_read(drive, &entry,
          (header.partition_entry_lba * 512) + (partition * sizeof(entry)),
          sizeof(entry));
 
@@ -104,7 +104,7 @@ struct mbr_entry {
 static int mbr_get_part(struct part *ret, int drive, int partition) {
     // Check if actually valid mbr
     uint16_t hint;
-    read(drive, &hint, 444, sizeof(uint16_t));
+    disk_read(drive, &hint, 444, sizeof(uint16_t));
     if (hint && hint != 0x5a5a)
         return INVALID_TABLE;
 
@@ -112,12 +112,12 @@ static int mbr_get_part(struct part *ret, int drive, int partition) {
         return END_OF_TABLE;
 
     uint32_t disk_signature;
-    read(drive, &disk_signature, 440, sizeof(uint32_t));
+    disk_read(drive, &disk_signature, 440, sizeof(uint32_t));
 
     struct mbr_entry entry;
     size_t entry_offset = 0x1be + sizeof(struct mbr_entry) * partition;
 
-    int r = read(drive, &entry, entry_offset, sizeof(struct mbr_entry));
+    int r = disk_read(drive, &entry, entry_offset, sizeof(struct mbr_entry));
     if (r)
         return r;
 
@@ -140,7 +140,7 @@ static int mbr_get_part(struct part *ret, int drive, int partition) {
     return 0;
 }
 
-int get_part(struct part *part, int drive, int partition) {
+int part_get(struct part *part, int drive, int partition) {
     int ret;
 
     ret = gpt_get_part(part, drive, partition);
@@ -183,7 +183,7 @@ void part_create_index(void) {
 load_up:
         for (int part = 0; ; part++) {
             struct part p;
-            int ret = get_part(&p, drive, part);
+            int ret = part_get(&p, drive, part);
 
             if (ret == END_OF_TABLE)
                 break;
@@ -204,15 +204,18 @@ load_up:
     }
 }
 
-bool part_get_by_guid(int *drive, int *part, struct guid *guid) {
+bool part_get_by_guid(struct part *part, struct guid *guid) {
     for (size_t i = 0; i < part_index_i; i++) {
         if (!part_index[i].guid_valid)
             continue;
         if (!memcmp(&part_index[i].guid, guid, 16)) {
-            *drive = part_index[i].drive;
-            *part  = part_index[i].partition;
+            *part = part_index[i];
             return true;
         }
     }
     return false;
 }
+
+int part_read(struct part *part, void *buffer, uint64_t loc, uint64_t count) {
+    return disk_read(part->drive, buffer, loc + (part->first_sect * 512), count);
+}
diff --git a/stage2/lib/part.h b/stage2/lib/part.h
index e718577f..cd31a7fc 100644
--- a/stage2/lib/part.h
+++ b/stage2/lib/part.h
@@ -14,9 +14,11 @@ struct part {
     struct guid guid;
 };
 
-int get_part(struct part *part, int drive, int partition);
-bool part_get_by_guid(int *drive, int *part, struct guid *guid);
-
 void part_create_index(void);
 
+int part_get(struct part *part, int drive, int partition);
+bool part_get_by_guid(struct part *part, struct guid *guid);
+
+int part_read(struct part *part, void *buffer, uint64_t loc, uint64_t count);
+
 #endif
diff --git a/stage2/lib/uri.c b/stage2/lib/uri.c
index eafe13c3..439f44ac 100644
--- a/stage2/lib/uri.c
+++ b/stage2/lib/uri.c
@@ -84,7 +84,11 @@ static bool uri_bios_dispatch(struct file_handle *fd, char *loc, char *path) {
     if (!parse_bios_partition(loc, &drive, &partition))
         return false;
 
-    if (fopen(fd, drive, partition, path))
+    struct part part;
+    if (part_get(&part, drive, partition))
+        return false;
+
+    if (fopen(fd, &part, path))
         return false;
 
     return true;
@@ -95,11 +99,11 @@ static bool uri_guid_dispatch(struct file_handle *fd, char *guid_str, char *path
     if (!string_to_guid(&guid, guid_str))
         return false;
 
-    int drive, partition;
-    if (!part_get_by_guid(&drive, &partition, &guid))
+    struct part part;
+    if (!part_get_by_guid(&part, &guid))
         return false;
 
-    if (fopen(fd, drive, partition, path))
+    if (fopen(fd, &part, path))
         return false;
 
     return true;
diff --git a/stage2/main.c b/stage2/main.c
index 1200e4fd..34f33bbc 100644
--- a/stage2/main.c
+++ b/stage2/main.c
@@ -42,12 +42,12 @@ void entry(uint8_t _boot_drive) {
             panic("Config file not found.");
         }
         print("Checking partition %d...\n", i);
-        int ret = get_part(&parts[i], boot_drive, i);
+        int ret = part_get(&parts[i], boot_drive, i);
         if (ret) {
             print("Partition not found.\n");
         } else {
             print("Partition found.\n");
-            if (!init_config(boot_drive, i)) {
+            if (!init_config(&parts[i])) {
                 print("Config file found and loaded.\n");
                 break;
             }
diff --git a/stage2/protos/chainload.c b/stage2/protos/chainload.c
index 14346456..cc33ccc8 100644
--- a/stage2/protos/chainload.c
+++ b/stage2/protos/chainload.c
@@ -73,11 +73,11 @@ void chainload(void) {
 
     if (part != -1) {
         struct part p;
-        get_part(&p, drive, part);
+        part_get(&p, drive, part);
 
-        read_partition(drive, &p, (void *)0x7c00, 0, 512);
+        part_read(&p, (void *)0x7c00, 0, 512);
     } else {
-        read(drive, (void *)0x7c00, 0, 512);
+        disk_read(drive, (void *)0x7c00, 0, 512);
     }
 
     mtrr_restore();
tab: 248 wrap: offon