disk: Fix logic issue with UEFI volume detection

This commit is contained in:
mintsuki 2022-01-09 20:43:51 +01:00
parent 93ab7a2ad3
commit 19bd781661
2 changed files with 27 additions and 15 deletions

View File

@ -217,6 +217,7 @@ int disk_read_sectors(struct volume *volume, void *buf, uint64_t block, size_t c
}
}
static uint64_t unique_sector_column;
static alignas(4096) uint8_t unique_sector_pool[8192];
struct volume *disk_volume_from_efi_handle(EFI_HANDLE efi_handle) {
@ -230,12 +231,16 @@ struct volume *disk_volume_from_efi_handle(EFI_HANDLE efi_handle) {
return NULL;
}
if (unique_sector_column == (uint64_t)-1) {
goto fallback;
}
for (size_t i = 0; i < volume_index_i; i++) {
if (volume_index[i]->unique_sector_valid == false) {
continue;
}
size_t unique_sector = (volume_index[i]->unique_sector * volume_index[i]->sector_size) / block_io->Media->BlockSize;
size_t unique_sector = (unique_sector_column * volume_index[i]->sector_size) / block_io->Media->BlockSize;
status = block_io->ReadBlocks(block_io, block_io->Media->MediaId,
unique_sector,
@ -253,7 +258,7 @@ struct volume *disk_volume_from_efi_handle(EFI_HANDLE efi_handle) {
}
// Fallback to read-back method
fallback:;
EFI_GUID disk_io_guid = DISK_IO_PROTOCOL;
EFI_DISK_IO *disk_io = NULL;
@ -320,14 +325,13 @@ struct volume *disk_volume_from_efi_handle(EFI_HANDLE efi_handle) {
return NULL;
}
static struct volume *volume_by_unique_sector(size_t sect, uint32_t crc32) {
static struct volume *volume_by_unique_sector(uint32_t crc32) {
for (size_t i = 0; i < volume_index_i; i++) {
if (volume_index[i]->unique_sector_valid == false) {
continue;
}
if (volume_index[i]->unique_sector == sect
&& volume_index[i]->unique_sector_crc32 == crc32) {
if (volume_index[i]->unique_sector_crc32 == crc32) {
return volume_index[i];
}
}
@ -340,10 +344,13 @@ static struct volume *volume_by_unique_sector(size_t sect, uint32_t crc32) {
static void find_unique_sectors(void) {
EFI_STATUS status;
for (size_t i = 0; i < volume_index_i; i++) {
for (size_t j = 0; j < UNIQUE_SECT_MAX_SEARCH_RANGE; j++) {
for (size_t j = 0; j < UNIQUE_SECT_MAX_SEARCH_RANGE; j++) {
unique_sector_column = j;
for (size_t i = 0; i < volume_index_i; i++) {
if (volume_index[i]->first_sect % (volume_index[i]->sector_size / 512)) {
break;
volume_index[i]->unique_sector_valid = false;
continue;
}
size_t first_sect = volume_index[i]->first_sect / (volume_index[i]->sector_size / 512);
@ -355,19 +362,26 @@ static void find_unique_sectors(void) {
volume_index[i]->sector_size,
unique_sector_pool);
if (status != 0) {
break;
volume_index[i]->unique_sector_valid = false;
continue;
}
uint32_t crc32 = get_crc32(unique_sector_pool, volume_index[i]->sector_size);
if (volume_by_unique_sector(j, crc32) == NULL) {
if (volume_by_unique_sector(crc32) == NULL) {
volume_index[i]->unique_sector_valid = true;
volume_index[i]->unique_sector = j;
volume_index[i]->unique_sector_crc32 = crc32;
break;
} else {
// next column
goto next_column;
}
}
return;
next_column:;
}
unique_sector_column = (uint64_t)-1;
}
static void find_part_handles(EFI_HANDLE *handles, size_t handle_count) {

View File

@ -20,7 +20,6 @@ struct volume {
EFI_BLOCK_IO *block_io;
bool unique_sector_valid;
size_t unique_sector;
uint32_t unique_sector_crc32;
#elif bios == 1
int drive;
@ -86,10 +85,9 @@ bool volume_read(struct volume *part, void *buffer, uint64_t loc, uint64_t count
\
struct volume *_PART = volume_get_by_coord(_VOLUME->is_optical, \
_VOLUME->index, _PARTNO); \
_PART_CNT++; \
if (_PART == NULL) \
continue; \
\
_PART_CNT++; \
\
_BODY_ \
} \