2020-04-15 14:21:44 +03:00
|
|
|
#include <stddef.h>
|
|
|
|
#include <stdint.h>
|
|
|
|
#include <lib/part.h>
|
2021-03-04 00:38:28 +03:00
|
|
|
#include <drivers/disk.h>
|
2022-09-02 03:29:12 +03:00
|
|
|
#if defined (BIOS)
|
2021-03-04 00:38:28 +03:00
|
|
|
# include <lib/real.h>
|
2021-03-02 12:23:43 +03:00
|
|
|
#endif
|
2020-04-15 14:21:44 +03:00
|
|
|
#include <lib/libc.h>
|
2022-08-27 00:44:47 +03:00
|
|
|
#include <lib/misc.h>
|
2020-10-18 07:23:39 +03:00
|
|
|
#include <lib/print.h>
|
|
|
|
#include <mm/pmm.h>
|
2020-11-02 11:20:34 +03:00
|
|
|
#include <fs/file.h>
|
2020-04-15 14:21:44 +03:00
|
|
|
|
2021-03-03 22:53:26 +03:00
|
|
|
enum {
|
|
|
|
CACHE_NOT_READY = 0,
|
|
|
|
CACHE_READY
|
|
|
|
};
|
|
|
|
|
|
|
|
static bool cache_block(struct volume *volume, uint64_t block) {
|
|
|
|
if (volume->cache_status == CACHE_READY && block == volume->cached_block)
|
|
|
|
return true;
|
|
|
|
|
|
|
|
volume->cache_status = CACHE_NOT_READY;
|
|
|
|
|
|
|
|
if (volume->cache == NULL)
|
|
|
|
volume->cache =
|
2021-08-22 17:27:06 +03:00
|
|
|
ext_mem_alloc(volume->fastest_xfer_size * volume->sector_size);
|
2021-03-02 12:23:43 +03:00
|
|
|
|
2021-09-02 00:01:03 +03:00
|
|
|
if (volume->first_sect % (volume->sector_size / 512)) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2022-07-06 04:17:11 +03:00
|
|
|
uint64_t first_sect = volume->first_sect / (volume->sector_size / 512);
|
2021-09-02 00:01:03 +03:00
|
|
|
|
2021-12-24 02:19:24 +03:00
|
|
|
uint64_t xfer_size = volume->fastest_xfer_size;
|
|
|
|
|
2022-01-03 20:58:37 +03:00
|
|
|
for (;;) {
|
|
|
|
int ret = disk_read_sectors(volume, volume->cache,
|
2021-09-02 00:01:03 +03:00
|
|
|
first_sect + block * volume->fastest_xfer_size,
|
2022-01-03 20:58:37 +03:00
|
|
|
xfer_size);
|
|
|
|
|
|
|
|
switch (ret) {
|
|
|
|
case DISK_NO_MEDIA:
|
|
|
|
return false;
|
|
|
|
case DISK_SUCCESS:
|
|
|
|
goto disk_success;
|
|
|
|
}
|
|
|
|
|
2021-12-24 02:19:24 +03:00
|
|
|
xfer_size--;
|
2021-12-29 03:18:41 +03:00
|
|
|
if (xfer_size == 0) {
|
|
|
|
return false;
|
|
|
|
}
|
2021-12-24 02:19:24 +03:00
|
|
|
}
|
2021-03-03 22:53:26 +03:00
|
|
|
|
2022-01-03 20:58:37 +03:00
|
|
|
disk_success:
|
2021-03-03 22:53:26 +03:00
|
|
|
volume->cache_status = CACHE_READY;
|
|
|
|
volume->cached_block = block;
|
|
|
|
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
bool volume_read(struct volume *volume, void *buffer, uint64_t loc, uint64_t count) {
|
2021-03-13 11:08:01 +03:00
|
|
|
if (volume->pxe) {
|
2021-12-11 21:58:00 +03:00
|
|
|
panic(false, "Attempted volume_read() on pxe");
|
2021-03-13 11:08:01 +03:00
|
|
|
}
|
|
|
|
|
2021-08-22 17:27:06 +03:00
|
|
|
uint64_t block_size = volume->fastest_xfer_size * volume->sector_size;
|
2021-03-03 22:53:26 +03:00
|
|
|
|
|
|
|
uint64_t progress = 0;
|
|
|
|
while (progress < count) {
|
|
|
|
uint64_t block = (loc + progress) / block_size;
|
|
|
|
|
|
|
|
if (!cache_block(volume, block))
|
|
|
|
return false;
|
|
|
|
|
|
|
|
uint64_t chunk = count - progress;
|
|
|
|
uint64_t offset = (loc + progress) % block_size;
|
|
|
|
if (chunk > block_size - offset)
|
|
|
|
chunk = block_size - offset;
|
|
|
|
|
|
|
|
memcpy(buffer + progress, &volume->cache[offset], chunk);
|
|
|
|
progress += chunk;
|
|
|
|
}
|
|
|
|
|
|
|
|
return true;
|
2021-03-02 12:23:43 +03:00
|
|
|
}
|
|
|
|
|
2020-04-15 14:21:44 +03:00
|
|
|
struct gpt_table_header {
|
|
|
|
// the head
|
|
|
|
char signature[8];
|
|
|
|
uint32_t revision;
|
|
|
|
uint32_t header_size;
|
|
|
|
uint32_t crc32;
|
|
|
|
uint32_t _reserved0;
|
|
|
|
|
|
|
|
// the partitioning info
|
|
|
|
uint64_t my_lba;
|
|
|
|
uint64_t alternate_lba;
|
|
|
|
uint64_t first_usable_lba;
|
|
|
|
uint64_t last_usable_lba;
|
|
|
|
|
|
|
|
// the guid
|
2020-10-17 12:08:02 +03:00
|
|
|
struct guid disk_guid;
|
2020-04-15 14:21:44 +03:00
|
|
|
|
|
|
|
// entries related
|
|
|
|
uint64_t partition_entry_lba;
|
|
|
|
uint32_t number_of_partition_entries;
|
|
|
|
uint32_t size_of_partition_entry;
|
|
|
|
uint32_t partition_entry_array_crc32;
|
|
|
|
} __attribute__((packed));
|
|
|
|
|
|
|
|
struct gpt_entry {
|
2020-10-17 12:08:02 +03:00
|
|
|
struct guid partition_type_guid;
|
2020-04-15 14:21:44 +03:00
|
|
|
|
2020-10-17 12:08:02 +03:00
|
|
|
struct guid unique_partition_guid;
|
2020-04-15 14:21:44 +03:00
|
|
|
|
|
|
|
uint64_t starting_lba;
|
|
|
|
uint64_t ending_lba;
|
|
|
|
|
|
|
|
uint64_t attributes;
|
|
|
|
|
|
|
|
uint16_t partition_name[36];
|
|
|
|
} __attribute__((packed));
|
|
|
|
|
2021-03-02 12:23:43 +03:00
|
|
|
bool gpt_get_guid(struct guid *guid, struct volume *volume) {
|
2020-04-15 14:21:44 +03:00
|
|
|
struct gpt_table_header header = {0};
|
|
|
|
|
2022-07-01 18:30:53 +03:00
|
|
|
int lb_guesses[] = {
|
|
|
|
512,
|
|
|
|
4096
|
|
|
|
};
|
|
|
|
int lb_size = -1;
|
|
|
|
|
|
|
|
for (size_t i = 0; i < SIZEOF_ARRAY(lb_guesses); i++) {
|
|
|
|
// read header, located after the first block
|
|
|
|
volume_read(volume, &header, lb_guesses[i] * 1, sizeof(header));
|
|
|
|
|
|
|
|
// check the header
|
|
|
|
// 'EFI PART'
|
|
|
|
if (strncmp(header.signature, "EFI PART", 8))
|
|
|
|
continue;
|
|
|
|
|
|
|
|
lb_size = lb_guesses[i];
|
|
|
|
break;
|
|
|
|
}
|
2021-02-06 16:40:55 +03:00
|
|
|
|
2022-07-01 18:30:53 +03:00
|
|
|
if (lb_size == -1) {
|
2021-02-06 16:40:55 +03:00
|
|
|
return false;
|
2022-07-01 18:30:53 +03:00
|
|
|
}
|
|
|
|
|
2021-02-06 16:40:55 +03:00
|
|
|
if (header.revision != 0x00010000)
|
|
|
|
return false;
|
|
|
|
|
|
|
|
*guid = header.disk_guid;
|
|
|
|
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
static int gpt_get_part(struct volume *ret, struct volume *volume, int partition) {
|
|
|
|
struct gpt_table_header header = {0};
|
|
|
|
|
2022-07-01 18:30:53 +03:00
|
|
|
int lb_guesses[] = {
|
|
|
|
512,
|
|
|
|
4096
|
|
|
|
};
|
|
|
|
int lb_size = -1;
|
|
|
|
|
|
|
|
for (size_t i = 0; i < SIZEOF_ARRAY(lb_guesses); i++) {
|
|
|
|
// read header, located after the first block
|
|
|
|
volume_read(volume, &header, lb_guesses[i] * 1, sizeof(header));
|
2021-02-06 16:40:55 +03:00
|
|
|
|
2022-07-01 18:30:53 +03:00
|
|
|
// check the header
|
|
|
|
// 'EFI PART'
|
|
|
|
if (strncmp(header.signature, "EFI PART", 8))
|
|
|
|
continue;
|
2020-04-15 14:21:44 +03:00
|
|
|
|
2022-07-01 18:30:53 +03:00
|
|
|
lb_size = lb_guesses[i];
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (lb_size == -1) {
|
2020-10-18 07:23:39 +03:00
|
|
|
return INVALID_TABLE;
|
2022-07-01 18:30:53 +03:00
|
|
|
}
|
|
|
|
|
2020-10-18 07:23:39 +03:00
|
|
|
if (header.revision != 0x00010000)
|
2021-02-06 16:40:55 +03:00
|
|
|
return INVALID_TABLE;
|
2020-04-15 14:21:44 +03:00
|
|
|
|
|
|
|
// parse the entries if reached here
|
2020-04-29 17:53:05 +03:00
|
|
|
if ((uint32_t)partition >= header.number_of_partition_entries)
|
2020-10-18 07:23:39 +03:00
|
|
|
return END_OF_TABLE;
|
2020-04-15 14:21:44 +03:00
|
|
|
|
|
|
|
struct gpt_entry entry = {0};
|
2021-02-06 16:40:55 +03:00
|
|
|
volume_read(volume, &entry,
|
2022-07-01 18:30:53 +03:00
|
|
|
(header.partition_entry_lba * lb_size) + (partition * sizeof(entry)),
|
2020-04-15 14:21:44 +03:00
|
|
|
sizeof(entry));
|
|
|
|
|
2020-10-17 12:08:02 +03:00
|
|
|
struct guid empty_guid = {0};
|
|
|
|
if (!memcmp(&entry.unique_partition_guid, &empty_guid, sizeof(struct guid)))
|
|
|
|
return NO_PARTITION;
|
2020-04-15 14:21:44 +03:00
|
|
|
|
2022-09-02 03:29:12 +03:00
|
|
|
#if defined (UEFI)
|
2021-03-04 11:15:10 +03:00
|
|
|
ret->efi_handle = volume->efi_handle;
|
2021-09-02 00:01:03 +03:00
|
|
|
ret->block_io = volume->block_io;
|
2022-09-02 03:29:12 +03:00
|
|
|
#elif defined (BIOS)
|
2021-02-06 16:40:55 +03:00
|
|
|
ret->drive = volume->drive;
|
2021-06-12 14:13:19 +03:00
|
|
|
#endif
|
2021-08-22 18:20:22 +03:00
|
|
|
ret->fastest_xfer_size = volume->fastest_xfer_size;
|
2021-06-12 14:13:19 +03:00
|
|
|
ret->index = volume->index;
|
|
|
|
ret->is_optical = volume->is_optical;
|
|
|
|
ret->partition = partition + 1;
|
2021-09-02 00:01:03 +03:00
|
|
|
ret->sector_size = volume->sector_size;
|
2022-07-01 19:32:18 +03:00
|
|
|
ret->first_sect = entry.starting_lba * (lb_size / 512);
|
|
|
|
ret->sect_count = ((entry.ending_lba - entry.starting_lba) + 1) * (lb_size / 512);
|
2021-03-04 11:15:10 +03:00
|
|
|
ret->backing_dev = volume;
|
2020-11-01 23:25:35 +03:00
|
|
|
|
|
|
|
struct guid guid;
|
|
|
|
if (!fs_get_guid(&guid, ret)) {
|
|
|
|
ret->guid_valid = false;
|
|
|
|
} else {
|
|
|
|
ret->guid_valid = true;
|
|
|
|
ret->guid = guid;
|
|
|
|
}
|
2020-04-15 14:21:44 +03:00
|
|
|
|
2022-07-02 11:43:10 +03:00
|
|
|
char *fslabel = fs_get_label(ret);
|
|
|
|
if (fslabel == NULL) {
|
|
|
|
ret->fslabel_valid = false;
|
|
|
|
} else {
|
|
|
|
ret->fslabel_valid = true;
|
|
|
|
ret->fslabel = fslabel;
|
|
|
|
}
|
|
|
|
|
2020-12-10 09:13:52 +03:00
|
|
|
ret->part_guid_valid = true;
|
|
|
|
ret->part_guid = entry.unique_partition_guid;
|
|
|
|
|
2020-04-15 14:21:44 +03:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
struct mbr_entry {
|
|
|
|
uint8_t status;
|
|
|
|
uint8_t chs_first_sect[3];
|
|
|
|
uint8_t type;
|
|
|
|
uint8_t chs_last_sect[3];
|
|
|
|
uint32_t first_sect;
|
|
|
|
uint32_t sect_count;
|
|
|
|
} __attribute__((packed));
|
|
|
|
|
2022-07-11 02:30:54 +03:00
|
|
|
bool is_valid_mbr(struct volume *volume) {
|
2022-03-16 08:49:41 +03:00
|
|
|
// Check if actually valid mbr
|
|
|
|
uint16_t hint = 0;
|
|
|
|
|
|
|
|
volume_read(volume, &hint, 446, sizeof(uint8_t));
|
|
|
|
if ((uint8_t)hint != 0x00 && (uint8_t)hint != 0x80)
|
|
|
|
return false;
|
|
|
|
volume_read(volume, &hint, 462, sizeof(uint8_t));
|
|
|
|
if ((uint8_t)hint != 0x00 && (uint8_t)hint != 0x80)
|
|
|
|
return false;
|
|
|
|
volume_read(volume, &hint, 478, sizeof(uint8_t));
|
|
|
|
if ((uint8_t)hint != 0x00 && (uint8_t)hint != 0x80)
|
|
|
|
return false;
|
|
|
|
volume_read(volume, &hint, 494, sizeof(uint8_t));
|
|
|
|
if ((uint8_t)hint != 0x00 && (uint8_t)hint != 0x80)
|
|
|
|
return false;
|
|
|
|
|
|
|
|
char hintc[64];
|
|
|
|
volume_read(volume, hintc, 4, 8);
|
|
|
|
if (memcmp(hintc, "_ECH_FS_", 8) == 0)
|
|
|
|
return false;
|
2022-07-10 23:46:08 +03:00
|
|
|
volume_read(volume, hintc, 3, 4);
|
|
|
|
if (memcmp(hintc, "NTFS", 4) == 0)
|
|
|
|
return false;
|
2022-03-16 08:49:41 +03:00
|
|
|
volume_read(volume, hintc, 54, 3);
|
2022-05-07 17:24:04 +03:00
|
|
|
if (memcmp(hintc, "FAT", 3) == 0)
|
|
|
|
return false;
|
|
|
|
volume_read(volume, hintc, 82, 3);
|
2022-03-16 08:49:41 +03:00
|
|
|
if (memcmp(hintc, "FAT", 3) == 0)
|
|
|
|
return false;
|
2022-07-10 23:46:08 +03:00
|
|
|
volume_read(volume, hintc, 3, 5);
|
|
|
|
if (memcmp(hintc, "FAT32", 5) == 0)
|
|
|
|
return false;
|
2022-03-16 08:49:41 +03:00
|
|
|
volume_read(volume, &hint, 1080, sizeof(uint16_t));
|
|
|
|
if (hint == 0xef53)
|
|
|
|
return false;
|
|
|
|
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
uint32_t mbr_get_id(struct volume *volume) {
|
|
|
|
if (!is_valid_mbr(volume)) {
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
uint32_t ret;
|
|
|
|
volume_read(volume, &ret, 0x1b8, sizeof(uint32_t));
|
|
|
|
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
2021-02-06 16:40:55 +03:00
|
|
|
static int mbr_get_logical_part(struct volume *ret, struct volume *extended_part,
|
|
|
|
int partition) {
|
2020-12-26 22:02:22 +03:00
|
|
|
struct mbr_entry entry;
|
|
|
|
|
2022-07-06 04:17:11 +03:00
|
|
|
uint64_t ebr_sector = 0;
|
2020-12-26 22:02:22 +03:00
|
|
|
|
|
|
|
for (int i = 0; i < partition; i++) {
|
2022-07-06 04:17:11 +03:00
|
|
|
uint64_t entry_offset = ebr_sector * 512 + 0x1ce;
|
2020-12-26 22:02:22 +03:00
|
|
|
|
2021-03-03 22:53:26 +03:00
|
|
|
volume_read(extended_part, &entry, entry_offset, sizeof(struct mbr_entry));
|
2020-12-26 22:02:22 +03:00
|
|
|
|
2022-07-06 04:17:11 +03:00
|
|
|
if (entry.type != 0x0f && entry.type != 0x05) {
|
2020-12-26 22:02:22 +03:00
|
|
|
return END_OF_TABLE;
|
2022-07-06 04:17:11 +03:00
|
|
|
}
|
2020-12-26 22:02:22 +03:00
|
|
|
|
|
|
|
ebr_sector = entry.first_sect;
|
|
|
|
}
|
|
|
|
|
2022-07-06 04:17:11 +03:00
|
|
|
uint64_t entry_offset = ebr_sector * 512 + 0x1be;
|
2020-12-26 22:02:22 +03:00
|
|
|
|
2021-03-03 22:53:26 +03:00
|
|
|
volume_read(extended_part, &entry, entry_offset, sizeof(struct mbr_entry));
|
2020-12-26 22:02:22 +03:00
|
|
|
|
|
|
|
if (entry.type == 0)
|
|
|
|
return NO_PARTITION;
|
|
|
|
|
2022-09-02 03:29:12 +03:00
|
|
|
#if defined (UEFI)
|
2021-03-04 11:15:10 +03:00
|
|
|
ret->efi_handle = extended_part->efi_handle;
|
2021-09-02 00:01:03 +03:00
|
|
|
ret->block_io = extended_part->block_io;
|
2022-09-02 03:29:12 +03:00
|
|
|
#elif defined (BIOS)
|
2021-02-06 16:40:55 +03:00
|
|
|
ret->drive = extended_part->drive;
|
2021-06-12 14:13:19 +03:00
|
|
|
#endif
|
2021-08-22 18:20:22 +03:00
|
|
|
ret->fastest_xfer_size = extended_part->fastest_xfer_size;
|
2021-06-12 14:13:19 +03:00
|
|
|
ret->index = extended_part->index;
|
|
|
|
ret->is_optical = extended_part->is_optical;
|
|
|
|
ret->partition = partition + 4 + 1;
|
2021-03-03 22:53:26 +03:00
|
|
|
ret->sector_size = extended_part->sector_size;
|
2020-12-26 22:02:22 +03:00
|
|
|
ret->first_sect = extended_part->first_sect + ebr_sector + entry.first_sect;
|
|
|
|
ret->sect_count = entry.sect_count;
|
2021-03-04 11:15:10 +03:00
|
|
|
ret->backing_dev = extended_part->backing_dev;
|
2020-12-26 22:02:22 +03:00
|
|
|
|
|
|
|
struct guid guid;
|
|
|
|
if (!fs_get_guid(&guid, ret)) {
|
|
|
|
ret->guid_valid = false;
|
|
|
|
} else {
|
|
|
|
ret->guid_valid = true;
|
|
|
|
ret->guid = guid;
|
|
|
|
}
|
|
|
|
|
2022-07-02 11:43:10 +03:00
|
|
|
char *fslabel = fs_get_label(ret);
|
|
|
|
if (fslabel == NULL) {
|
|
|
|
ret->fslabel_valid = false;
|
|
|
|
} else {
|
|
|
|
ret->fslabel_valid = true;
|
|
|
|
ret->fslabel = fslabel;
|
|
|
|
}
|
|
|
|
|
2020-12-26 22:02:22 +03:00
|
|
|
ret->part_guid_valid = false;
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2021-02-06 16:40:55 +03:00
|
|
|
static int mbr_get_part(struct volume *ret, struct volume *volume, int partition) {
|
2022-03-16 08:49:41 +03:00
|
|
|
if (!is_valid_mbr(volume)) {
|
2020-09-04 02:37:24 +03:00
|
|
|
return INVALID_TABLE;
|
2022-03-16 08:49:41 +03:00
|
|
|
}
|
2020-09-04 02:37:24 +03:00
|
|
|
|
2020-12-26 22:02:22 +03:00
|
|
|
struct mbr_entry entry;
|
2020-10-18 07:23:39 +03:00
|
|
|
|
2020-12-26 22:02:22 +03:00
|
|
|
if (partition > 3) {
|
|
|
|
for (int i = 0; i < 4; i++) {
|
2022-07-06 04:17:11 +03:00
|
|
|
uint64_t entry_offset = 0x1be + sizeof(struct mbr_entry) * i;
|
2020-12-26 22:02:22 +03:00
|
|
|
|
2021-03-03 22:53:26 +03:00
|
|
|
volume_read(volume, &entry, entry_offset, sizeof(struct mbr_entry));
|
2020-12-26 22:02:22 +03:00
|
|
|
|
2022-07-06 04:17:11 +03:00
|
|
|
if (entry.type != 0x0f && entry.type != 0x05)
|
2020-12-26 22:02:22 +03:00
|
|
|
continue;
|
|
|
|
|
2021-03-03 22:53:26 +03:00
|
|
|
struct volume extended_part = {0};
|
2020-12-26 22:02:22 +03:00
|
|
|
|
2022-09-02 03:29:12 +03:00
|
|
|
#if defined (UEFI)
|
2021-03-04 11:15:10 +03:00
|
|
|
extended_part.efi_handle = volume->efi_handle;
|
2021-09-02 00:01:03 +03:00
|
|
|
extended_part.block_io = volume->block_io;
|
2022-09-02 03:29:12 +03:00
|
|
|
#elif defined (BIOS)
|
2021-02-06 16:40:55 +03:00
|
|
|
extended_part.drive = volume->drive;
|
2021-06-12 14:13:19 +03:00
|
|
|
#endif
|
2021-08-22 18:20:22 +03:00
|
|
|
extended_part.fastest_xfer_size = volume->fastest_xfer_size;
|
2021-06-12 14:13:19 +03:00
|
|
|
extended_part.index = volume->index;
|
|
|
|
extended_part.is_optical = volume->is_optical;
|
|
|
|
extended_part.partition = i + 1;
|
2021-03-03 22:53:26 +03:00
|
|
|
extended_part.sector_size = volume->sector_size;
|
2020-12-26 22:02:22 +03:00
|
|
|
extended_part.first_sect = entry.first_sect;
|
|
|
|
extended_part.sect_count = entry.sect_count;
|
2021-03-04 11:15:10 +03:00
|
|
|
extended_part.backing_dev = volume;
|
2020-12-26 22:02:22 +03:00
|
|
|
|
2021-02-06 16:40:55 +03:00
|
|
|
return mbr_get_logical_part(ret, &extended_part, partition - 4);
|
2020-12-26 22:02:22 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
return END_OF_TABLE;
|
|
|
|
}
|
2020-10-18 07:23:39 +03:00
|
|
|
|
2022-07-06 04:17:11 +03:00
|
|
|
uint64_t entry_offset = 0x1be + sizeof(struct mbr_entry) * partition;
|
2020-04-15 14:21:44 +03:00
|
|
|
|
2021-03-03 22:53:26 +03:00
|
|
|
volume_read(volume, &entry, entry_offset, sizeof(struct mbr_entry));
|
2020-04-15 14:21:44 +03:00
|
|
|
|
2020-09-04 02:37:24 +03:00
|
|
|
if (entry.type == 0)
|
2020-04-15 14:21:44 +03:00
|
|
|
return NO_PARTITION;
|
|
|
|
|
2022-09-02 03:29:12 +03:00
|
|
|
#if defined (UEFI)
|
2021-03-04 11:15:10 +03:00
|
|
|
ret->efi_handle = volume->efi_handle;
|
2021-09-02 00:01:03 +03:00
|
|
|
ret->block_io = volume->block_io;
|
2022-09-02 03:29:12 +03:00
|
|
|
#elif defined (BIOS)
|
2021-02-06 16:40:55 +03:00
|
|
|
ret->drive = volume->drive;
|
2021-06-12 14:13:19 +03:00
|
|
|
#endif
|
2021-08-22 18:20:22 +03:00
|
|
|
ret->fastest_xfer_size = volume->fastest_xfer_size;
|
2021-06-12 14:13:19 +03:00
|
|
|
ret->index = volume->index;
|
|
|
|
ret->is_optical = volume->is_optical;
|
|
|
|
ret->partition = partition + 1;
|
2021-03-03 22:53:26 +03:00
|
|
|
ret->sector_size = volume->sector_size;
|
2020-11-05 19:18:45 +03:00
|
|
|
ret->first_sect = entry.first_sect;
|
|
|
|
ret->sect_count = entry.sect_count;
|
2021-03-04 11:15:10 +03:00
|
|
|
ret->backing_dev = volume;
|
2020-10-18 07:23:39 +03:00
|
|
|
|
2020-11-01 23:25:35 +03:00
|
|
|
struct guid guid;
|
|
|
|
if (!fs_get_guid(&guid, ret)) {
|
|
|
|
ret->guid_valid = false;
|
|
|
|
} else {
|
|
|
|
ret->guid_valid = true;
|
|
|
|
ret->guid = guid;
|
|
|
|
}
|
2020-10-18 07:23:39 +03:00
|
|
|
|
2022-07-02 11:43:10 +03:00
|
|
|
char *fslabel = fs_get_label(ret);
|
|
|
|
if (fslabel == NULL) {
|
|
|
|
ret->fslabel_valid = false;
|
|
|
|
} else {
|
|
|
|
ret->fslabel_valid = true;
|
|
|
|
ret->fslabel = fslabel;
|
|
|
|
}
|
|
|
|
|
2020-12-10 09:13:52 +03:00
|
|
|
ret->part_guid_valid = false;
|
|
|
|
|
2020-04-15 14:21:44 +03:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2021-02-06 16:40:55 +03:00
|
|
|
int part_get(struct volume *part, struct volume *volume, int partition) {
|
2020-04-15 14:21:44 +03:00
|
|
|
int ret;
|
|
|
|
|
2021-02-06 16:40:55 +03:00
|
|
|
ret = gpt_get_part(part, volume, partition);
|
2020-04-15 14:21:44 +03:00
|
|
|
if (ret != INVALID_TABLE)
|
|
|
|
return ret;
|
|
|
|
|
2021-02-06 16:40:55 +03:00
|
|
|
ret = mbr_get_part(part, volume, partition);
|
2020-04-15 14:21:44 +03:00
|
|
|
if (ret != INVALID_TABLE)
|
|
|
|
return ret;
|
|
|
|
|
2020-11-09 17:04:53 +03:00
|
|
|
return INVALID_TABLE;
|
2020-04-15 14:21:44 +03:00
|
|
|
}
|
2020-10-18 07:23:39 +03:00
|
|
|
|
2021-03-04 11:15:10 +03:00
|
|
|
struct volume **volume_index = NULL;
|
2021-03-04 03:42:25 +03:00
|
|
|
size_t volume_index_i = 0;
|
2021-02-06 16:40:55 +03:00
|
|
|
|
2021-03-04 11:15:10 +03:00
|
|
|
struct volume *volume_get_by_guid(struct guid *guid) {
|
|
|
|
for (size_t i = 0; i < volume_index_i; i++) {
|
|
|
|
if (volume_index[i]->guid_valid
|
|
|
|
&& memcmp(&volume_index[i]->guid, guid, 16) == 0) {
|
|
|
|
return volume_index[i];
|
2021-02-06 16:40:55 +03:00
|
|
|
}
|
2021-03-04 11:15:10 +03:00
|
|
|
if (volume_index[i]->part_guid_valid
|
|
|
|
&& memcmp(&volume_index[i]->part_guid, guid, 16) == 0) {
|
|
|
|
return volume_index[i];
|
2020-12-10 09:13:52 +03:00
|
|
|
}
|
2021-02-06 16:40:55 +03:00
|
|
|
}
|
2021-03-04 11:15:10 +03:00
|
|
|
|
|
|
|
return NULL;
|
2021-02-06 16:40:55 +03:00
|
|
|
}
|
|
|
|
|
2022-07-02 11:43:10 +03:00
|
|
|
struct volume *volume_get_by_fslabel(char *fslabel) {
|
|
|
|
for (size_t i = 0; i < volume_index_i; i++) {
|
|
|
|
if (volume_index[i]->fslabel_valid
|
|
|
|
&& strcmp(volume_index[i]->fslabel, fslabel) == 0) {
|
|
|
|
return volume_index[i];
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
2021-06-12 14:13:19 +03:00
|
|
|
struct volume *volume_get_by_coord(bool optical, int drive, int partition) {
|
2021-03-04 11:15:10 +03:00
|
|
|
for (size_t i = 0; i < volume_index_i; i++) {
|
2021-06-12 14:13:19 +03:00
|
|
|
if (volume_index[i]->index == drive
|
|
|
|
&& volume_index[i]->is_optical == optical
|
2021-03-04 11:15:10 +03:00
|
|
|
&& volume_index[i]->partition == partition) {
|
|
|
|
return volume_index[i];
|
2020-11-01 23:25:35 +03:00
|
|
|
}
|
|
|
|
}
|
2021-03-04 11:15:10 +03:00
|
|
|
|
|
|
|
return NULL;
|
2020-11-01 23:25:35 +03:00
|
|
|
}
|
2021-06-12 14:13:19 +03:00
|
|
|
|
2022-09-02 03:29:12 +03:00
|
|
|
#if defined (BIOS)
|
2021-06-12 14:13:19 +03:00
|
|
|
struct volume *volume_get_by_bios_drive(int drive) {
|
|
|
|
for (size_t i = 0; i < volume_index_i; i++) {
|
|
|
|
if (volume_index[i]->drive == drive) {
|
|
|
|
return volume_index[i];
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
#endif
|