2019-06-02 00:34:24 +03:00
|
|
|
#include <stdint.h>
|
|
|
|
#include <stddef.h>
|
2022-01-01 15:15:53 +03:00
|
|
|
#include <stdalign.h>
|
2019-06-02 00:48:33 +03:00
|
|
|
#include <drivers/disk.h>
|
2020-11-02 12:17:20 +03:00
|
|
|
#include <lib/libc.h>
|
2022-09-02 03:29:12 +03:00
|
|
|
#if defined (BIOS)
|
2021-03-04 00:38:28 +03:00
|
|
|
# include <lib/real.h>
|
2022-09-02 03:29:12 +03:00
|
|
|
#elif defined (UEFI)
|
2021-03-04 00:38:28 +03:00
|
|
|
# include <efi.h>
|
2022-09-15 13:53:47 +03:00
|
|
|
# include <crypt/blake2b.h>
|
2021-03-04 00:38:28 +03:00
|
|
|
#endif
|
2022-08-27 00:44:47 +03:00
|
|
|
#include <lib/misc.h>
|
2021-08-22 17:27:06 +03:00
|
|
|
#include <lib/print.h>
|
2021-09-06 09:52:41 +03:00
|
|
|
#include <lib/rand.h>
|
2020-09-20 13:03:44 +03:00
|
|
|
#include <mm/pmm.h>
|
2021-08-22 17:27:06 +03:00
|
|
|
#include <sys/cpu.h>
|
2023-01-17 19:40:43 +03:00
|
|
|
#include <pxe/pxe.h>
|
2019-06-02 00:34:24 +03:00
|
|
|
|
2022-01-06 13:03:12 +03:00
|
|
|
#define DEFAULT_FASTEST_XFER_SIZE 64
|
|
|
|
#define MAX_FASTEST_XFER_SIZE 512
|
|
|
|
|
2021-08-22 19:05:21 +03:00
|
|
|
#define MAX_VOLUMES 64
|
|
|
|
|
2022-09-02 03:29:12 +03:00
|
|
|
#if defined (BIOS)
|
2021-03-04 00:38:28 +03:00
|
|
|
|
|
|
|
struct bios_drive_params {
|
|
|
|
uint16_t buf_size;
|
|
|
|
uint16_t info_flags;
|
|
|
|
uint32_t cyl;
|
|
|
|
uint32_t heads;
|
|
|
|
uint32_t sects;
|
|
|
|
uint64_t lba_count;
|
|
|
|
uint16_t bytes_per_sect;
|
|
|
|
uint32_t edd;
|
|
|
|
} __attribute__((packed));
|
|
|
|
|
2020-09-25 23:36:26 +03:00
|
|
|
struct dap {
|
2019-06-02 00:48:33 +03:00
|
|
|
uint16_t size;
|
|
|
|
uint16_t count;
|
|
|
|
uint16_t offset;
|
|
|
|
uint16_t segment;
|
|
|
|
uint64_t lba;
|
2020-09-25 23:36:26 +03:00
|
|
|
};
|
|
|
|
|
2021-08-22 23:07:48 +03:00
|
|
|
#define XFER_BUF_SIZE (xfer_sizes[SIZEOF_ARRAY(xfer_sizes) - 1] * 512)
|
|
|
|
static const size_t xfer_sizes[] = { 1, 2, 4, 8, 16, 24, 32, 48, 64 };
|
2022-09-01 05:22:30 +03:00
|
|
|
static uint8_t *xfer_buf = NULL;
|
2021-03-03 22:53:26 +03:00
|
|
|
|
2021-08-22 17:27:06 +03:00
|
|
|
static size_t fastest_xfer_size(struct volume *volume) {
|
2021-08-22 23:07:48 +03:00
|
|
|
struct dap dap = {0};
|
|
|
|
|
2021-08-22 17:27:06 +03:00
|
|
|
if (xfer_buf == NULL)
|
|
|
|
xfer_buf = conv_mem_alloc(XFER_BUF_SIZE);
|
|
|
|
|
|
|
|
size_t fastest_size = 1;
|
|
|
|
uint64_t last_speed = (uint64_t)-1;
|
|
|
|
|
|
|
|
for (size_t i = 0; i < SIZEOF_ARRAY(xfer_sizes); i++) {
|
|
|
|
if (xfer_sizes[i] * volume->sector_size > XFER_BUF_SIZE) {
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
dap.size = 16;
|
|
|
|
dap.count = xfer_sizes[i];
|
|
|
|
dap.segment = rm_seg(xfer_buf);
|
|
|
|
dap.offset = rm_off(xfer_buf);
|
|
|
|
dap.lba = 0;
|
|
|
|
|
|
|
|
uint64_t start_timestamp = rdtsc();
|
2021-08-26 02:33:43 +03:00
|
|
|
for (size_t j = 0; j < XFER_BUF_SIZE / 512; j += xfer_sizes[i]) {
|
|
|
|
struct rm_regs r = {0};
|
|
|
|
r.eax = 0x4200;
|
|
|
|
r.edx = volume->drive;
|
|
|
|
r.esi = (uint32_t)rm_off(&dap);
|
|
|
|
r.ds = rm_seg(&dap);
|
|
|
|
rm_int(0x13, &r, &r);
|
|
|
|
if (r.eflags & EFLAGS_CF) {
|
|
|
|
int ah = (r.eax >> 8) & 0xff;
|
2021-09-06 09:52:41 +03:00
|
|
|
print("Disk error %x. Drive %x", ah, volume->drive);
|
2021-08-26 02:33:43 +03:00
|
|
|
return 8;
|
|
|
|
}
|
|
|
|
dap.lba += xfer_sizes[i];
|
2021-08-22 17:27:06 +03:00
|
|
|
}
|
2021-08-26 02:33:43 +03:00
|
|
|
uint64_t end_timestamp = rdtsc();
|
2021-08-22 17:27:06 +03:00
|
|
|
|
2021-08-26 02:33:43 +03:00
|
|
|
uint64_t speed = end_timestamp - start_timestamp;
|
2021-08-22 17:27:06 +03:00
|
|
|
|
|
|
|
if (speed < last_speed) {
|
|
|
|
last_speed = speed;
|
|
|
|
fastest_size = xfer_sizes[i];
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return fastest_size;
|
|
|
|
}
|
|
|
|
|
2022-01-03 20:58:37 +03:00
|
|
|
int disk_read_sectors(struct volume *volume, void *buf, uint64_t block, size_t count) {
|
2021-08-22 23:07:48 +03:00
|
|
|
struct dap dap = {0};
|
|
|
|
|
2021-03-03 22:53:26 +03:00
|
|
|
if (count * volume->sector_size > XFER_BUF_SIZE)
|
2021-12-11 21:58:00 +03:00
|
|
|
panic(false, "XFER");
|
2019-06-03 01:14:32 +03:00
|
|
|
|
2021-03-03 22:53:26 +03:00
|
|
|
if (xfer_buf == NULL)
|
2021-04-15 03:21:38 +03:00
|
|
|
xfer_buf = conv_mem_alloc(XFER_BUF_SIZE);
|
2021-03-03 22:53:26 +03:00
|
|
|
|
2021-04-15 03:21:38 +03:00
|
|
|
dap.size = 16;
|
|
|
|
dap.count = count;
|
|
|
|
dap.segment = rm_seg(xfer_buf);
|
|
|
|
dap.offset = rm_off(xfer_buf);
|
|
|
|
dap.lba = block;
|
2019-06-03 01:14:32 +03:00
|
|
|
|
|
|
|
struct rm_regs r = {0};
|
|
|
|
r.eax = 0x4200;
|
2021-03-03 22:53:26 +03:00
|
|
|
r.edx = volume->drive;
|
2021-04-15 03:21:38 +03:00
|
|
|
r.esi = (uint32_t)rm_off(&dap);
|
|
|
|
r.ds = rm_seg(&dap);
|
2019-06-03 01:14:32 +03:00
|
|
|
|
|
|
|
rm_int(0x13, &r, &r);
|
|
|
|
|
|
|
|
if (r.eflags & EFLAGS_CF) {
|
2022-01-03 20:58:37 +03:00
|
|
|
return DISK_FAILURE;
|
2019-06-03 01:14:32 +03:00
|
|
|
}
|
|
|
|
|
2021-03-03 23:40:55 +03:00
|
|
|
if (buf != NULL)
|
|
|
|
memcpy(buf, xfer_buf, count * volume->sector_size);
|
2019-06-02 12:17:49 +03:00
|
|
|
|
2022-01-03 20:58:37 +03:00
|
|
|
return DISK_SUCCESS;
|
2019-06-02 12:17:49 +03:00
|
|
|
}
|
2021-03-02 12:23:43 +03:00
|
|
|
|
2022-10-09 10:02:32 +03:00
|
|
|
static int disk_write_sectors(struct volume *volume, void *buf, uint64_t block, size_t count) {
|
|
|
|
struct dap dap = {0};
|
|
|
|
|
|
|
|
if (count * volume->sector_size > XFER_BUF_SIZE)
|
|
|
|
panic(false, "XFER");
|
|
|
|
|
|
|
|
if (xfer_buf == NULL)
|
|
|
|
xfer_buf = conv_mem_alloc(XFER_BUF_SIZE);
|
|
|
|
|
|
|
|
dap.size = 16;
|
|
|
|
dap.count = count;
|
|
|
|
dap.segment = rm_seg(xfer_buf);
|
|
|
|
dap.offset = rm_off(xfer_buf);
|
|
|
|
dap.lba = block;
|
|
|
|
|
|
|
|
struct rm_regs r = {0};
|
|
|
|
r.eax = 0x4301;
|
|
|
|
r.edx = volume->drive;
|
|
|
|
r.esi = (uint32_t)rm_off(&dap);
|
|
|
|
r.ds = rm_seg(&dap);
|
|
|
|
|
|
|
|
if (buf != NULL)
|
|
|
|
memcpy(xfer_buf, buf, count * volume->sector_size);
|
|
|
|
|
|
|
|
rm_int(0x13, &r, &r);
|
|
|
|
|
|
|
|
if (r.eflags & EFLAGS_CF) {
|
|
|
|
return DISK_FAILURE;
|
|
|
|
}
|
|
|
|
|
|
|
|
return DISK_SUCCESS;
|
|
|
|
}
|
|
|
|
|
2022-09-01 05:22:30 +03:00
|
|
|
static bool detect_sector_size(struct volume *volume) {
|
|
|
|
struct dap dap = {0};
|
|
|
|
|
|
|
|
if (xfer_buf == NULL)
|
|
|
|
xfer_buf = conv_mem_alloc(XFER_BUF_SIZE);
|
|
|
|
|
|
|
|
dap.size = 16;
|
|
|
|
dap.count = 1;
|
|
|
|
dap.segment = rm_seg(xfer_buf);
|
|
|
|
dap.offset = rm_off(xfer_buf);
|
|
|
|
dap.lba = 0;
|
|
|
|
|
|
|
|
struct rm_regs r = {0};
|
|
|
|
r.eax = 0x4200;
|
|
|
|
r.edx = volume->drive;
|
|
|
|
r.esi = (uint32_t)rm_off(&dap);
|
|
|
|
r.ds = rm_seg(&dap);
|
|
|
|
|
|
|
|
struct rm_regs r_copy = r;
|
|
|
|
struct dap dap_copy = dap;
|
|
|
|
|
|
|
|
memset(xfer_buf, 0, XFER_BUF_SIZE);
|
|
|
|
|
|
|
|
rm_int(0x13, &r, &r);
|
|
|
|
|
|
|
|
if (r.eflags & EFLAGS_CF) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
size_t sector_size_a = 0;
|
2022-09-01 05:42:49 +03:00
|
|
|
for (long i = XFER_BUF_SIZE - 1; i >= 0; i--) {
|
2022-09-01 05:22:30 +03:00
|
|
|
if (xfer_buf[i] != 0) {
|
|
|
|
sector_size_a = i + 1;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
r = r_copy;
|
|
|
|
dap = dap_copy;
|
|
|
|
|
|
|
|
memset(xfer_buf, 0xff, XFER_BUF_SIZE);
|
|
|
|
|
|
|
|
rm_int(0x13, &r, &r);
|
|
|
|
|
|
|
|
if (r.eflags & EFLAGS_CF) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
size_t sector_size_b = 0;
|
2022-09-01 05:42:49 +03:00
|
|
|
for (long i = XFER_BUF_SIZE - 1; i >= 0; i--) {
|
2022-09-01 05:22:30 +03:00
|
|
|
if (xfer_buf[i] != 0xff) {
|
|
|
|
sector_size_b = i + 1;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
volume->sector_size = sector_size_a > sector_size_b ? sector_size_a : sector_size_b;
|
|
|
|
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2021-03-04 11:15:10 +03:00
|
|
|
void disk_create_index(void) {
|
2021-08-22 19:05:21 +03:00
|
|
|
volume_index = ext_mem_alloc(sizeof(struct volume) * MAX_VOLUMES);
|
2021-03-02 12:23:43 +03:00
|
|
|
|
2021-06-12 14:13:19 +03:00
|
|
|
int optical_indices = 1, hdd_indices = 1;
|
2021-04-24 23:59:03 +03:00
|
|
|
|
2021-06-12 14:13:19 +03:00
|
|
|
for (uint8_t drive = 0x80; drive < 0xf0; drive++) {
|
2021-08-22 19:05:21 +03:00
|
|
|
if (volume_index_i == MAX_VOLUMES) {
|
|
|
|
print("WARNING: TOO MANY VOLUMES!");
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
2021-03-02 12:23:43 +03:00
|
|
|
struct rm_regs r = {0};
|
|
|
|
struct bios_drive_params drive_params;
|
|
|
|
|
|
|
|
r.eax = 0x4800;
|
|
|
|
r.edx = drive;
|
|
|
|
r.ds = rm_seg(&drive_params);
|
|
|
|
r.esi = rm_off(&drive_params);
|
|
|
|
|
|
|
|
drive_params.buf_size = sizeof(struct bios_drive_params);
|
|
|
|
|
|
|
|
rm_int(0x13, &r, &r);
|
|
|
|
|
2022-09-01 05:22:30 +03:00
|
|
|
if (r.eflags & EFLAGS_CF) {
|
2021-03-28 21:30:13 +03:00
|
|
|
continue;
|
2022-09-01 05:22:30 +03:00
|
|
|
}
|
2021-03-28 21:30:13 +03:00
|
|
|
|
2021-03-04 11:15:10 +03:00
|
|
|
struct volume *block = ext_mem_alloc(sizeof(struct volume));
|
2021-03-02 12:23:43 +03:00
|
|
|
|
|
|
|
block->drive = drive;
|
2021-06-12 14:13:19 +03:00
|
|
|
block->partition = 0;
|
2021-03-02 12:23:43 +03:00
|
|
|
block->first_sect = 0;
|
|
|
|
block->sect_count = drive_params.lba_count;
|
2021-03-15 04:22:55 +03:00
|
|
|
block->max_partition = -1;
|
2021-03-02 12:23:43 +03:00
|
|
|
|
2022-09-01 05:22:30 +03:00
|
|
|
if (!detect_sector_size(block)) {
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
2022-10-09 10:02:32 +03:00
|
|
|
if (disk_read_sectors(block, xfer_buf, 0, 1) != DISK_SUCCESS) {
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
|
|
|
block->is_optical = disk_write_sectors(block, xfer_buf, 0, 1) != DISK_SUCCESS;
|
|
|
|
|
|
|
|
if (block->is_optical) {
|
2021-06-12 14:13:19 +03:00
|
|
|
block->index = optical_indices++;
|
|
|
|
} else {
|
|
|
|
block->index = hdd_indices++;
|
2021-03-03 23:40:55 +03:00
|
|
|
}
|
|
|
|
|
2021-08-22 17:27:06 +03:00
|
|
|
block->fastest_xfer_size = fastest_xfer_size(block);
|
|
|
|
|
2021-03-02 12:23:43 +03:00
|
|
|
if (gpt_get_guid(&block->guid, block)) {
|
|
|
|
block->guid_valid = true;
|
|
|
|
}
|
|
|
|
|
2021-05-05 20:59:21 +03:00
|
|
|
volume_index[volume_index_i++] = block;
|
|
|
|
|
2021-03-02 12:23:43 +03:00
|
|
|
for (int part = 0; ; part++) {
|
2021-03-04 11:15:10 +03:00
|
|
|
struct volume *p = ext_mem_alloc(sizeof(struct volume));
|
|
|
|
int ret = part_get(p, block, part);
|
2021-03-04 00:38:28 +03:00
|
|
|
|
|
|
|
if (ret == END_OF_TABLE || ret == INVALID_TABLE)
|
|
|
|
break;
|
|
|
|
if (ret == NO_PARTITION)
|
|
|
|
continue;
|
|
|
|
|
|
|
|
volume_index[volume_index_i++] = p;
|
2021-03-15 04:22:55 +03:00
|
|
|
|
|
|
|
block->max_partition++;
|
2021-03-04 00:38:28 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
#endif
|
|
|
|
|
2022-09-02 03:29:12 +03:00
|
|
|
#if defined (UEFI)
|
2021-03-04 00:38:28 +03:00
|
|
|
|
2022-01-03 20:58:37 +03:00
|
|
|
int disk_read_sectors(struct volume *volume, void *buf, uint64_t block, size_t count) {
|
2021-03-13 17:53:16 +03:00
|
|
|
EFI_STATUS status;
|
|
|
|
|
2021-09-02 00:01:03 +03:00
|
|
|
status = volume->block_io->ReadBlocks(volume->block_io,
|
|
|
|
volume->block_io->Media->MediaId,
|
|
|
|
block, count * volume->sector_size, buf);
|
2022-01-06 13:03:12 +03:00
|
|
|
|
2022-01-03 20:58:37 +03:00
|
|
|
switch (status) {
|
|
|
|
case EFI_SUCCESS: return DISK_SUCCESS;
|
|
|
|
case EFI_NO_MEDIA: return DISK_NO_MEDIA;
|
|
|
|
default: return DISK_FAILURE;
|
2021-09-02 00:01:03 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-01-17 19:40:43 +03:00
|
|
|
static struct volume *pxe_from_efi_handle(EFI_HANDLE efi_handle) {
|
|
|
|
static struct volume *vol = NULL;
|
|
|
|
|
|
|
|
// There's only one PXE volume
|
|
|
|
if (vol) {
|
|
|
|
return vol;
|
|
|
|
}
|
|
|
|
|
|
|
|
EFI_STATUS status;
|
|
|
|
|
|
|
|
EFI_GUID pxe_base_code_guid = EFI_PXE_BASE_CODE_PROTOCOL_GUID;
|
|
|
|
EFI_PXE_BASE_CODE *pxe_base_code = NULL;
|
|
|
|
|
|
|
|
status = gBS->HandleProtocol(efi_handle, &pxe_base_code_guid, (void **)&pxe_base_code);
|
|
|
|
if (status) {
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!pxe_base_code->Mode->DhcpDiscoverValid) {
|
|
|
|
print("PXE somehow didn't use DHCP?\n");
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (pxe_base_code->Mode->UsingIpv6) {
|
|
|
|
print("Sorry, unsupported: PXE IPv6\n");
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
|
|
|
vol = pxe_bind_volume(efi_handle, pxe_base_code);
|
|
|
|
return vol;
|
|
|
|
}
|
|
|
|
|
2022-01-14 04:04:22 +03:00
|
|
|
static alignas(4096) uint8_t unique_sector_pool[4096];
|
2021-09-02 00:01:03 +03:00
|
|
|
|
|
|
|
struct volume *disk_volume_from_efi_handle(EFI_HANDLE efi_handle) {
|
|
|
|
EFI_STATUS status;
|
2021-03-04 03:42:25 +03:00
|
|
|
|
|
|
|
EFI_GUID block_io_guid = BLOCK_IO_PROTOCOL;
|
|
|
|
EFI_BLOCK_IO *block_io = NULL;
|
|
|
|
|
2021-08-25 23:03:00 +03:00
|
|
|
status = gBS->HandleProtocol(efi_handle, &block_io_guid, (void **)&block_io);
|
2021-09-02 00:01:03 +03:00
|
|
|
if (status) {
|
2023-01-17 19:40:43 +03:00
|
|
|
return pxe_from_efi_handle(efi_handle);
|
2021-09-02 00:01:03 +03:00
|
|
|
}
|
2021-03-04 03:42:25 +03:00
|
|
|
|
2022-10-13 23:59:22 +03:00
|
|
|
block_io->Media->WriteCaching = false;
|
|
|
|
|
2021-09-02 00:01:03 +03:00
|
|
|
for (size_t i = 0; i < volume_index_i; i++) {
|
|
|
|
if (volume_index[i]->unique_sector_valid == false) {
|
|
|
|
continue;
|
|
|
|
}
|
2021-03-13 17:53:16 +03:00
|
|
|
|
2022-01-14 04:04:22 +03:00
|
|
|
if (volume_index[i]->unique_sector % block_io->Media->BlockSize) {
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
|
|
|
size_t unique_sector = volume_index[i]->unique_sector / block_io->Media->BlockSize;
|
2021-03-13 17:53:16 +03:00
|
|
|
|
2021-09-02 00:01:03 +03:00
|
|
|
status = block_io->ReadBlocks(block_io, block_io->Media->MediaId,
|
|
|
|
unique_sector,
|
2022-01-14 04:04:22 +03:00
|
|
|
4096,
|
2021-09-02 00:01:03 +03:00
|
|
|
unique_sector_pool);
|
|
|
|
if (status != 0) {
|
|
|
|
continue;
|
2021-03-13 17:53:16 +03:00
|
|
|
}
|
2021-05-06 05:31:05 +03:00
|
|
|
|
2022-09-15 13:53:47 +03:00
|
|
|
uint8_t b2b[BLAKE2B_OUT_BYTES];
|
|
|
|
blake2b(b2b, unique_sector_pool, 4096);
|
2021-03-04 03:42:25 +03:00
|
|
|
|
2022-09-15 13:53:47 +03:00
|
|
|
if (memcmp(b2b, volume_index[i]->unique_sector_b2b, BLAKE2B_OUT_BYTES) == 0) {
|
2021-09-02 00:01:03 +03:00
|
|
|
return volume_index[i];
|
|
|
|
}
|
|
|
|
}
|
2021-03-04 03:42:25 +03:00
|
|
|
|
2021-09-02 02:31:36 +03:00
|
|
|
// Fallback to read-back method
|
2022-01-14 04:04:22 +03:00
|
|
|
|
2021-09-06 09:52:41 +03:00
|
|
|
uint64_t signature = rand64();
|
|
|
|
uint64_t new_signature;
|
|
|
|
do { new_signature = rand64(); } while (new_signature == signature);
|
2021-09-02 02:31:36 +03:00
|
|
|
uint64_t orig;
|
|
|
|
|
2023-05-30 22:36:09 +03:00
|
|
|
status = block_io->ReadBlocks(block_io, block_io->Media->MediaId, 0, 4096, unique_sector_pool);
|
|
|
|
orig = *(uint64_t *)unique_sector_pool;
|
2021-09-02 02:31:36 +03:00
|
|
|
if (status) {
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
2023-05-30 22:36:09 +03:00
|
|
|
*(uint64_t *)unique_sector_pool = signature;
|
|
|
|
status = block_io->WriteBlocks(block_io, block_io->Media->MediaId, 0, 4096, unique_sector_pool);
|
2021-09-02 02:31:36 +03:00
|
|
|
if (status) {
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
|
|
|
struct volume *ret = NULL;
|
|
|
|
for (size_t i = 0; i < volume_index_i; i++) {
|
|
|
|
uint64_t compare;
|
|
|
|
|
2023-05-30 22:36:09 +03:00
|
|
|
status = volume_index[i]->block_io->ReadBlocks(volume_index[i]->block_io,
|
|
|
|
volume_index[i]->block_io->Media->MediaId,
|
|
|
|
(volume_index[i]->first_sect * 512) / volume_index[i]->sector_size,
|
|
|
|
4096, unique_sector_pool);
|
|
|
|
compare = *(uint64_t *)unique_sector_pool;
|
2022-09-19 21:48:42 +03:00
|
|
|
if (status) {
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
2021-09-02 02:31:36 +03:00
|
|
|
if (compare == signature) {
|
2021-09-06 09:52:41 +03:00
|
|
|
// Double check
|
2023-05-30 22:36:09 +03:00
|
|
|
status = block_io->ReadBlocks(block_io, block_io->Media->MediaId, 0, 4096, unique_sector_pool);
|
|
|
|
if (status) {
|
|
|
|
break;
|
2022-10-13 23:59:22 +03:00
|
|
|
}
|
2023-05-30 22:36:09 +03:00
|
|
|
*(uint64_t *)unique_sector_pool = new_signature;
|
|
|
|
status = block_io->WriteBlocks(block_io, block_io->Media->MediaId, 0, 4096, unique_sector_pool);
|
2022-09-19 21:48:42 +03:00
|
|
|
if (status) {
|
|
|
|
break;
|
|
|
|
}
|
2021-09-06 09:52:41 +03:00
|
|
|
|
2023-05-30 22:36:09 +03:00
|
|
|
status = volume_index[i]->block_io->ReadBlocks(volume_index[i]->block_io,
|
|
|
|
volume_index[i]->block_io->Media->MediaId,
|
|
|
|
(volume_index[i]->first_sect * 512) / volume_index[i]->sector_size,
|
|
|
|
4096, unique_sector_pool);
|
|
|
|
compare = *(uint64_t *)unique_sector_pool;
|
2022-09-19 21:48:42 +03:00
|
|
|
if (status) {
|
|
|
|
continue;
|
|
|
|
}
|
2021-09-06 09:52:41 +03:00
|
|
|
|
|
|
|
if (compare == new_signature) {
|
|
|
|
ret = volume_index[i];
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
2023-05-30 22:36:09 +03:00
|
|
|
status = block_io->ReadBlocks(block_io, block_io->Media->MediaId, 0, 4096, unique_sector_pool);
|
|
|
|
if (status) {
|
|
|
|
break;
|
2022-10-13 23:59:22 +03:00
|
|
|
}
|
2023-05-30 22:36:09 +03:00
|
|
|
*(uint64_t *)unique_sector_pool = signature;
|
|
|
|
status = block_io->WriteBlocks(block_io, block_io->Media->MediaId, 0, 4096, unique_sector_pool);
|
2022-09-19 21:48:42 +03:00
|
|
|
if (status) {
|
|
|
|
break;
|
|
|
|
}
|
2021-09-02 02:31:36 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-05-30 22:36:09 +03:00
|
|
|
status = block_io->ReadBlocks(block_io, block_io->Media->MediaId, 0, 4096, unique_sector_pool);
|
|
|
|
if (status) {
|
|
|
|
return NULL;
|
2022-10-13 23:59:22 +03:00
|
|
|
}
|
2023-05-30 22:36:09 +03:00
|
|
|
*(uint64_t *)unique_sector_pool = orig;
|
|
|
|
status = block_io->WriteBlocks(block_io, block_io->Media->MediaId, 0, 4096, unique_sector_pool);
|
2021-09-02 02:31:36 +03:00
|
|
|
if (status) {
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (ret != NULL) {
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
2021-09-02 00:01:03 +03:00
|
|
|
return NULL;
|
|
|
|
}
|
2021-03-04 03:42:25 +03:00
|
|
|
|
2022-09-15 13:53:47 +03:00
|
|
|
static struct volume *volume_by_unique_sector(uint64_t sect, void *b2b) {
|
2021-09-02 00:01:03 +03:00
|
|
|
for (size_t i = 0; i < volume_index_i; i++) {
|
|
|
|
if (volume_index[i]->unique_sector_valid == false) {
|
|
|
|
continue;
|
|
|
|
}
|
2021-03-04 03:42:25 +03:00
|
|
|
|
2022-01-14 04:04:22 +03:00
|
|
|
if (volume_index[i]->unique_sector == sect
|
2022-09-15 13:53:47 +03:00
|
|
|
&& memcmp(volume_index[i]->unique_sector_b2b, b2b, BLAKE2B_OUT_BYTES) == 0) {
|
2021-09-02 00:01:03 +03:00
|
|
|
return volume_index[i];
|
2021-03-04 03:42:25 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-09-02 00:01:03 +03:00
|
|
|
return NULL;
|
2021-03-04 03:42:25 +03:00
|
|
|
}
|
|
|
|
|
2021-09-02 00:01:03 +03:00
|
|
|
#define UNIQUE_SECT_MAX_SEARCH_RANGE 0x1000
|
|
|
|
|
|
|
|
static void find_unique_sectors(void) {
|
2021-03-04 00:38:28 +03:00
|
|
|
EFI_STATUS status;
|
|
|
|
|
2022-01-14 04:04:22 +03:00
|
|
|
for (size_t i = 0; i < volume_index_i; i++) {
|
|
|
|
for (size_t j = 0; j < UNIQUE_SECT_MAX_SEARCH_RANGE; j++) {
|
2022-01-14 04:04:22 +03:00
|
|
|
if ((volume_index[i]->first_sect * 512) % volume_index[i]->block_io->Media->BlockSize) {
|
2022-01-14 04:04:22 +03:00
|
|
|
break;
|
2021-09-02 00:01:03 +03:00
|
|
|
}
|
2021-03-04 03:42:25 +03:00
|
|
|
|
2022-01-14 04:04:22 +03:00
|
|
|
size_t first_sect = (volume_index[i]->first_sect * 512) / volume_index[i]->block_io->Media->BlockSize;
|
2021-03-04 03:42:25 +03:00
|
|
|
|
2021-09-02 00:01:03 +03:00
|
|
|
status = volume_index[i]->block_io->ReadBlocks(
|
|
|
|
volume_index[i]->block_io,
|
|
|
|
volume_index[i]->block_io->Media->MediaId,
|
|
|
|
first_sect + j,
|
2022-01-14 04:04:22 +03:00
|
|
|
4096,
|
2021-09-02 00:01:03 +03:00
|
|
|
unique_sector_pool);
|
|
|
|
if (status != 0) {
|
2022-01-14 04:04:22 +03:00
|
|
|
break;
|
2021-09-02 00:01:03 +03:00
|
|
|
}
|
2021-03-04 00:38:28 +03:00
|
|
|
|
2022-09-15 13:53:47 +03:00
|
|
|
uint8_t b2b[BLAKE2B_OUT_BYTES];
|
|
|
|
blake2b(b2b, unique_sector_pool, 4096);
|
2022-01-14 04:04:22 +03:00
|
|
|
|
|
|
|
uint64_t uniq = (uint64_t)j * volume_index[i]->block_io->Media->BlockSize;
|
2021-03-04 00:38:28 +03:00
|
|
|
|
2022-09-15 13:53:47 +03:00
|
|
|
if (volume_by_unique_sector(uniq, b2b) == NULL) {
|
2021-09-02 00:01:03 +03:00
|
|
|
volume_index[i]->unique_sector_valid = true;
|
2022-01-14 04:04:22 +03:00
|
|
|
volume_index[i]->unique_sector = uniq;
|
2022-09-15 13:53:47 +03:00
|
|
|
memcpy(volume_index[i]->unique_sector_b2b, b2b, BLAKE2B_OUT_BYTES);
|
2022-01-14 04:04:22 +03:00
|
|
|
break;
|
2021-09-02 00:01:03 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2021-03-04 00:38:28 +03:00
|
|
|
}
|
|
|
|
|
2021-09-27 05:04:15 +03:00
|
|
|
static void find_part_handles(EFI_HANDLE *handles, size_t handle_count) {
|
|
|
|
for (size_t i = 0; i < handle_count; i++) {
|
|
|
|
struct volume *vol = disk_volume_from_efi_handle(handles[i]);
|
|
|
|
if (vol == NULL) {
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
vol->efi_part_handle = handles[i];
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-03-04 11:15:10 +03:00
|
|
|
void disk_create_index(void) {
|
2021-03-04 00:38:28 +03:00
|
|
|
EFI_STATUS status;
|
|
|
|
|
2022-07-01 09:03:21 +03:00
|
|
|
EFI_HANDLE tmp_handles[1];
|
|
|
|
|
2021-03-04 00:38:28 +03:00
|
|
|
EFI_GUID block_io_guid = BLOCK_IO_PROTOCOL;
|
2022-07-01 09:03:21 +03:00
|
|
|
EFI_HANDLE *handles = tmp_handles;
|
2023-06-01 08:18:26 +03:00
|
|
|
UINTN handles_size = sizeof(tmp_handles);
|
2022-07-01 09:03:21 +03:00
|
|
|
|
|
|
|
status = gBS->LocateHandle(ByProtocol, &block_io_guid, NULL, &handles_size, handles);
|
2021-03-04 00:38:28 +03:00
|
|
|
|
2023-06-01 08:18:26 +03:00
|
|
|
// we only care about the first handle, so ignore if we get EFI_BUFFER_TOO_SMALL
|
2022-07-01 09:03:21 +03:00
|
|
|
if (status != EFI_BUFFER_TOO_SMALL && status != EFI_SUCCESS) {
|
2023-06-01 08:18:26 +03:00
|
|
|
EFI_GUID pxe_guid = EFI_PXE_BASE_CODE_PROTOCOL_GUID;
|
|
|
|
status = gBS->LocateHandle(ByProtocol, &pxe_guid, NULL, &handles_size, handles);
|
|
|
|
// likewise, all that matters is that the protocol is present
|
|
|
|
if (status == EFI_BUFFER_TOO_SMALL || status == EFI_SUCCESS) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2022-07-01 09:03:21 +03:00
|
|
|
goto fail;
|
|
|
|
}
|
2021-03-04 00:38:28 +03:00
|
|
|
|
|
|
|
handles = ext_mem_alloc(handles_size);
|
|
|
|
|
2022-07-01 09:03:21 +03:00
|
|
|
status = gBS->LocateHandle(ByProtocol, &block_io_guid, NULL, &handles_size, handles);
|
|
|
|
|
|
|
|
if (status != EFI_SUCCESS) {
|
|
|
|
fail:
|
|
|
|
panic(false, "LocateHandle for BLOCK_IO_PROTOCOL failed. Machine not supported by Limine UEFI.");
|
|
|
|
}
|
2021-03-04 00:38:28 +03:00
|
|
|
|
2021-08-22 19:05:21 +03:00
|
|
|
volume_index = ext_mem_alloc(sizeof(struct volume) * MAX_VOLUMES);
|
2021-03-04 00:38:28 +03:00
|
|
|
|
2021-06-12 14:13:19 +03:00
|
|
|
int optical_indices = 1, hdd_indices = 1;
|
2021-03-04 11:15:10 +03:00
|
|
|
|
2021-09-27 05:04:15 +03:00
|
|
|
size_t handle_count = handles_size / sizeof(EFI_HANDLE);
|
|
|
|
|
|
|
|
for (size_t i = 0; i < handle_count; i++) {
|
2021-08-22 19:05:21 +03:00
|
|
|
if (volume_index_i == MAX_VOLUMES) {
|
|
|
|
print("WARNING: TOO MANY VOLUMES!");
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
2021-03-04 03:42:25 +03:00
|
|
|
EFI_BLOCK_IO *drive = NULL;
|
2021-03-04 00:38:28 +03:00
|
|
|
|
2021-08-25 23:03:00 +03:00
|
|
|
status = gBS->HandleProtocol(handles[i], &block_io_guid, (void **)&drive);
|
2021-03-04 00:38:28 +03:00
|
|
|
|
|
|
|
if (status != 0 || drive == NULL || drive->Media->LastBlock == 0)
|
|
|
|
continue;
|
|
|
|
|
|
|
|
if (drive->Media->LogicalPartition)
|
|
|
|
continue;
|
|
|
|
|
2022-10-13 23:20:23 +03:00
|
|
|
drive->Media->WriteCaching = false;
|
|
|
|
|
2023-05-30 22:36:09 +03:00
|
|
|
status = drive->ReadBlocks(drive, drive->Media->MediaId, 0, 4096, unique_sector_pool);
|
2022-09-19 21:48:42 +03:00
|
|
|
if (status) {
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
2023-05-30 22:36:09 +03:00
|
|
|
status = drive->WriteBlocks(drive, drive->Media->MediaId, 0, 4096, unique_sector_pool);
|
2021-03-13 17:53:16 +03:00
|
|
|
|
2021-03-04 11:15:10 +03:00
|
|
|
struct volume *block = ext_mem_alloc(sizeof(struct volume));
|
2021-03-04 00:38:28 +03:00
|
|
|
|
2022-10-13 23:20:23 +03:00
|
|
|
if (status || drive->Media->ReadOnly) {
|
2021-06-12 14:13:19 +03:00
|
|
|
block->index = optical_indices++;
|
|
|
|
block->is_optical = true;
|
|
|
|
} else {
|
|
|
|
block->index = hdd_indices++;
|
|
|
|
}
|
|
|
|
|
2021-03-04 11:15:10 +03:00
|
|
|
block->efi_handle = handles[i];
|
2021-09-02 00:01:03 +03:00
|
|
|
block->block_io = drive;
|
2021-06-12 14:13:19 +03:00
|
|
|
block->partition = 0;
|
2021-03-04 00:38:28 +03:00
|
|
|
block->sector_size = drive->Media->BlockSize;
|
|
|
|
block->first_sect = 0;
|
|
|
|
block->sect_count = drive->Media->LastBlock + 1;
|
2021-03-15 04:22:55 +03:00
|
|
|
block->max_partition = -1;
|
2021-03-04 00:38:28 +03:00
|
|
|
|
2021-11-02 07:16:42 +03:00
|
|
|
if (drive->Revision >= EFI_BLOCK_IO_PROTOCOL_REVISION3) {
|
|
|
|
block->fastest_xfer_size = drive->Media->OptimalTransferLengthGranularity;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (block->fastest_xfer_size == 0) {
|
2022-01-06 13:03:12 +03:00
|
|
|
block->fastest_xfer_size = DEFAULT_FASTEST_XFER_SIZE;
|
|
|
|
} else if (block->fastest_xfer_size >= MAX_FASTEST_XFER_SIZE) {
|
|
|
|
block->fastest_xfer_size = MAX_FASTEST_XFER_SIZE;
|
2021-11-02 07:16:42 +03:00
|
|
|
}
|
2021-08-22 18:20:22 +03:00
|
|
|
|
2021-05-05 20:59:21 +03:00
|
|
|
if (gpt_get_guid(&block->guid, block)) {
|
|
|
|
block->guid_valid = true;
|
|
|
|
}
|
|
|
|
|
2021-03-04 11:15:10 +03:00
|
|
|
volume_index[volume_index_i++] = block;
|
|
|
|
|
2021-03-04 00:38:28 +03:00
|
|
|
for (int part = 0; ; part++) {
|
2022-11-19 08:35:41 +03:00
|
|
|
struct volume _p = {0};
|
2022-11-19 01:28:28 +03:00
|
|
|
|
|
|
|
int ret = part_get(&_p, block, part);
|
2021-03-02 12:23:43 +03:00
|
|
|
|
|
|
|
if (ret == END_OF_TABLE || ret == INVALID_TABLE)
|
|
|
|
break;
|
|
|
|
if (ret == NO_PARTITION)
|
|
|
|
continue;
|
|
|
|
|
2022-11-19 01:28:28 +03:00
|
|
|
struct volume *p = ext_mem_alloc(sizeof(struct volume));
|
|
|
|
memcpy(p, &_p, sizeof(struct volume));
|
2021-03-02 12:23:43 +03:00
|
|
|
volume_index[volume_index_i++] = p;
|
2021-03-15 04:22:55 +03:00
|
|
|
|
|
|
|
block->max_partition++;
|
2021-03-02 12:23:43 +03:00
|
|
|
}
|
|
|
|
}
|
2021-09-02 00:01:03 +03:00
|
|
|
|
|
|
|
find_unique_sectors();
|
2021-09-27 05:04:15 +03:00
|
|
|
find_part_handles(handles, handle_count);
|
2021-10-21 03:37:05 +03:00
|
|
|
|
|
|
|
pmm_free(handles, handles_size);
|
2021-03-02 12:23:43 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
#endif
|