Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Can I write on my local filesystem using EFI

I am working on this project to write files to local filesystem as soon as the OS starts through an EFI application. I need to know if it is possible. And if yes then kindly guide me a little. Thanks

like image 852
Asak Avatar asked Sep 01 '15 05:09

Asak


1 Answers

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;
like image 160
Alex D Avatar answered Nov 14 '22 06:11

Alex D