disk: UEFI: Add BLOCK_IO fallback for volume_from_efi_handle()

This commit is contained in:
mintsuki 2022-10-13 22:59:22 +02:00
parent da5896b764
commit b30d2dc978
1 changed files with 60 additions and 18 deletions

View File

@ -326,6 +326,8 @@ struct volume *disk_volume_from_efi_handle(EFI_HANDLE efi_handle) {
return NULL; return NULL;
} }
block_io->Media->WriteCaching = false;
for (size_t i = 0; i < volume_index_i; i++) { for (size_t i = 0; i < volume_index_i; i++) {
if (volume_index[i]->unique_sector_valid == false) { if (volume_index[i]->unique_sector_valid == false) {
continue; continue;
@ -359,20 +361,31 @@ struct volume *disk_volume_from_efi_handle(EFI_HANDLE efi_handle) {
EFI_DISK_IO *disk_io = NULL; EFI_DISK_IO *disk_io = NULL;
status = gBS->HandleProtocol(efi_handle, &disk_io_guid, (void **)&disk_io); status = gBS->HandleProtocol(efi_handle, &disk_io_guid, (void **)&disk_io);
if (status) if (status) {
return NULL; disk_io = NULL;
}
uint64_t signature = rand64(); uint64_t signature = rand64();
uint64_t new_signature; uint64_t new_signature;
do { new_signature = rand64(); } while (new_signature == signature); do { new_signature = rand64(); } while (new_signature == signature);
uint64_t orig; uint64_t orig;
if (disk_io != NULL) {
status = disk_io->ReadDisk(disk_io, block_io->Media->MediaId, 0, sizeof(uint64_t), &orig); status = disk_io->ReadDisk(disk_io, block_io->Media->MediaId, 0, sizeof(uint64_t), &orig);
} else {
status = block_io->ReadBlocks(block_io, block_io->Media->MediaId, 0, 4096, unique_sector_pool);
orig = *(uint64_t *)unique_sector_pool;
}
if (status) { if (status) {
return NULL; return NULL;
} }
if (disk_io != NULL) {
status = disk_io->WriteDisk(disk_io, block_io->Media->MediaId, 0, sizeof(uint64_t), &signature); status = disk_io->WriteDisk(disk_io, block_io->Media->MediaId, 0, sizeof(uint64_t), &signature);
} else {
*(uint64_t *)unique_sector_pool = signature;
status = block_io->WriteBlocks(block_io, block_io->Media->MediaId, 0, 4096, unique_sector_pool);
}
if (status) { if (status) {
return NULL; return NULL;
} }
@ -385,31 +398,50 @@ struct volume *disk_volume_from_efi_handle(EFI_HANDLE efi_handle) {
status = gBS->HandleProtocol(volume_index[i]->efi_handle, status = gBS->HandleProtocol(volume_index[i]->efi_handle,
&disk_io_guid, (void **)&cur_disk_io); &disk_io_guid, (void **)&cur_disk_io);
if (status) { if (status) {
continue; cur_disk_io = NULL;
} }
if (cur_disk_io != NULL) {
status = cur_disk_io->ReadDisk(cur_disk_io, status = cur_disk_io->ReadDisk(cur_disk_io,
volume_index[i]->block_io->Media->MediaId, volume_index[i]->block_io->Media->MediaId,
0 + volume_index[i]->first_sect * 512, volume_index[i]->first_sect * 512,
sizeof(uint64_t), &compare); sizeof(uint64_t), &compare);
} else {
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,
4096, unique_sector_pool);
compare = *(uint64_t *)unique_sector_pool;
}
if (status) { if (status) {
continue; continue;
} }
if (compare == signature) { if (compare == signature) {
// Double check // Double check
if (disk_io != NULL) {
status = disk_io->WriteDisk(disk_io, block_io->Media->MediaId, 0, sizeof(uint64_t), &new_signature); status = disk_io->WriteDisk(disk_io, block_io->Media->MediaId, 0, sizeof(uint64_t), &new_signature);
} else {
*(uint64_t *)unique_sector_pool = new_signature;
status = block_io->WriteBlocks(block_io, block_io->Media->MediaId, 0, 4096, unique_sector_pool);
}
if (status) { if (status) {
break; break;
} }
if (cur_disk_io != NULL) {
status = cur_disk_io->ReadDisk(cur_disk_io, status = cur_disk_io->ReadDisk(cur_disk_io,
volume_index[i]->block_io->Media->MediaId, volume_index[i]->block_io->Media->MediaId,
0 + volume_index[i]->first_sect * 512, volume_index[i]->first_sect * 512,
sizeof(uint64_t), &compare); sizeof(uint64_t), &compare);
} else {
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,
4096, unique_sector_pool);
compare = *(uint64_t *)unique_sector_pool;
}
if (status) { if (status) {
continue; continue;
} }
@ -419,14 +451,24 @@ struct volume *disk_volume_from_efi_handle(EFI_HANDLE efi_handle) {
break; break;
} }
if (disk_io != NULL) {
status = disk_io->WriteDisk(disk_io, block_io->Media->MediaId, 0, sizeof(uint64_t), &signature); status = disk_io->WriteDisk(disk_io, block_io->Media->MediaId, 0, sizeof(uint64_t), &signature);
} else {
*(uint64_t *)unique_sector_pool = signature;
status = block_io->WriteBlocks(block_io, block_io->Media->MediaId, 0, 4096, unique_sector_pool);
}
if (status) { if (status) {
break; break;
} }
} }
} }
if (disk_io != NULL) {
status = disk_io->WriteDisk(disk_io, block_io->Media->MediaId, 0, sizeof(uint64_t), &orig); status = disk_io->WriteDisk(disk_io, block_io->Media->MediaId, 0, sizeof(uint64_t), &orig);
} else {
*(uint64_t *)unique_sector_pool = orig;
status = block_io->WriteBlocks(block_io, block_io->Media->MediaId, 0, 4096, unique_sector_pool);
}
if (status) { if (status) {
return NULL; return NULL;
} }