Can I write on my local filesystem using EFI

倾然丶 夕夏残阳落幕 提交于 2019-11-30 14:17:41

Ok, I'll give you a good heads up...

  1. First you enumerate all FS protocols in the system.

    EFI_BOOT_SERVICES* bs = ...;
    EFI_GUID sfspGuid = EFI_SIMPLE_FILE_SYSTEM_PROTOCOL_GUID;
    EFI_HANDLE* handles = NULL;   
    UINTN handleCount = 0;
    
    efiStatus = bs->LocateHandleBuffer(ByProtocol, 
                                       &sfspGuid, 
                                       NULL, 
                                       &handleCount, 
                                       &handles);
    
  2. Then you go through all of them and open the EFI_SIMPLE_FILE_SYSTEM_PROTOCOL for each handle you found, then you can grab a device path from a handle and figure out what device it is, what partition ect. and if the drive/partition is not what you are looking for skip it and go to the next handle. Or if you don't want to mess with DP parsing it you can simply try to open your file on each partition (handle) until the operation is successful.

    for (index = 0; index < (int)handleCount; ++ index)
    {
        EFI_SIMPLE_FILE_SYSTEM_PROTOCOL* fs = NULL;
    
        efiStatus = bs->HandleProtocol(
            handles[index],
            &sfspGuid,
            (void**)&fs);
    
  3. You found the handle for a partition you need. Then you open the volume.

    EFI_FILE_PROTOCOL* root = NULL;
    ...
    efiStatus = fs->OpenVolume(fs, &root);
    
  4. There is some functions to enumerate files and folders ect... But if you know the correct file path you can open it right away.

    EFI_FILE_PROTOCOL* token = NULL;
    
    efiStatus = root->Open(
            root, 
            &token,
            L"myfolder\\token.bin",
            EFI_FILE_MODE_READ,
            EFI_FILE_READ_ONLY | EFI_FILE_HIDDEN | EFI_FILE_SYSTEM);
    

Under the EFI_FILE_PROTOCOL you have a whole bunch of functions to operate on files:

  EFI_FILE_OPEN         Open;
  EFI_FILE_CLOSE        Close;
  EFI_FILE_DELETE       Delete;
  EFI_FILE_READ         Read;
  EFI_FILE_WRITE        Write;
  EFI_FILE_GET_POSITION GetPosition;
  EFI_FILE_SET_POSITION SetPosition;
  EFI_FILE_GET_INFO     GetInfo;
  EFI_FILE_SET_INFO     SetInfo;
  EFI_FILE_FLUSH        Flush;
标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!