在 UEFI 下读取扇区:遍历磁盘与加载原始扇区
来源:OSDev Wiki· 阅读原文 ↗
文章目录
请注意,你不一定需要访问原始块设备,因为 UEFI 通过 EFI_SIMPLE_FILE_SYSTEM_PROTOCOL 提供了文件抽象(通常仅限于 FAT 分区)。
遍历磁盘
如果你已经拥有一个块设备的句柄,那么你的工作就很简单了。然而更多时候你并没有,这意味着你必须首先查询系统中的块设备列表。因为你事先不知道有多少设备,所以你必须定位 BLOCK_IO_PROTOCOL 两次。
EFI_GUID bioGuid = BLOCK_IO_PROTOCOL; EFI_BLOCK_IO *bio; EFI_HANDLE *handles = NULL; UINTN handle_size = 0; /* first, locate the protocol with empty buffer */ status = uefi_call_wrapper(BS->LocateHandle, 5, ByProtocol, &bioGuid, NULL, &handle_size, handles); if (status != EFI_BUFFER_TOO_SMALL || handle_size == 0) { /* AUCH, error we don't have BLOCK_IO */ } /* get a buffer for the handles now that we know the buffer size */ status = uefi_call_wrapper(BS->AllocatePages, 4, 0, 2, (handle_size + PAGESIZE-1) / PAGESIZE, (EFI_PHYSICAL_ADDRESS*)&handles); if (EFI_ERROR(status) || handles == NULL) { /* AUCH, error memory allocation failed */ } /* second round, locate the protocol with big enough buffer */ status = uefi_call_wrapper(BS->LocateHandle, 5, ByProtocol, &bioGuid, NULL, &handle_size, handles);
现在 "handles" 是一个数组,其中为系统中的每个块设备都有一个句柄。
我们应该遍历该数组,并使用第 i 个句柄来获取某个设备的块 IO 接口。
for (i = 0; i < handle_size / sizeof(EFI_HANDLE); i++) { status = uefi_call_wrapper(BS->HandleProtocol, 3, handles[i], &bioGuid, (void **) &bio); /* if unsuccessful, skip and go over to the next device */ if (EFI_ERROR(status) || bio == NULL || bio->Media->BlockSize==0) continue; /* TODO: do something with that device. */ }
加载原始扇区
上述迭代中的 "bio" 接口指的是一个特定的设备。你可以使用该接口相当容易地从该设备加载扇区:
UINT8 mbr[512]; status = uefi_call_wrapper(bio->ReadBlocks, 5, bio, bio->Media->MediaId, 1, 512, &mbr); if (EFI_ERROR(status)) { /* AUCH, error reading sector */ }
另请参阅
文章
外部链接
来源与翻译说明
本文根据原文快照翻译,保留原有代码、数值、技术限定及历史背景。原文中的命令与运行结果不代表本站已实际执行验证。
原文:Reading sectors under UEFI;离线修订号:25441。对应原文版本

评论
登录后参与技术讨论,分享你的经验与见解。
登录 / 注册暂无评论