8

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

Asak
  • 109
  • 1
  • 3
  • 1
    It's hard to understand what do you need exactly, who writes where. When OS starts the UEFI is already gone. If you need to write from UEFI to your FS then the answer is "Yes" with 1 limitation: the FS has to be FAT32 which is natively supported by UEFI; If you have NTFS for example, as for open source, there is read-only NTFS drivers only for UEFI, as far as I know. As for guide, start for EFI_FILE_PROTOCOL – Alex D Sep 01 '15 at 17:45
  • ok I understand what you are saying. My point is I want to write an application(EFI) that copies a text file or an executable to the filesystem. How should i go about it? – Asak Sep 02 '15 at 06:32
  • I don't see any problem. Use the protocol I said in the first comment. – Alex D Sep 02 '15 at 06:38
  • ok thanks. and thing more I would like to ask is if I am using edk2 on my system and build programs using NT32, would I be able to read the files on my filesystem? because when I list directories on efi shell, it only lists the files with .efi extension. I am sorry I am asking really basic questions. – Asak Sep 03 '15 at 08:45
  • Hi @Asak , Can you please help me with this issue? My question: http://stackoverflow.com/questions/39719771/how-to-open-a-file-by-its-full-path-in-uefi – Keshava GN Sep 27 '16 at 08:34

1 Answers1

15

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;
Alex D
  • 942
  • 1
  • 6
  • 17
  • 1
    ok this is the best explanation of the procedure I have come across so far. Thank you so much. it worked. – Asak Sep 04 '15 at 12:02
  • Good explanation, @Alex , for a beginner this is useful, not only for SFSP, but also for understanding how to handle UEFI FS. I think you can write about UEFI in [documentation](http://stackoverflow.com/documentation) :) – Keshava GN Sep 16 '16 at 10:39
  • Hi @Alex , Can you please help me with this issue? My question: http://stackoverflow.com/questions/39719771/how-to-open-a-file-by-its-full-path-in-uefi – Keshava GN Sep 27 '16 at 08:31
  • Hi @Alex , can you explain how you are doing DP Parsing? Im trying as: `DevicePathFromHandle (handles[Index]);` in for loop, but im getting Type as 0x2 (ACPI, not as Media device path?) . what could be the reason? Can you please let me know how you are parsing? thanks – Keshava GN Oct 12 '16 at 07:25
  • It's a totally different question and it's impossible to fit it into comment here. You have to create a new post but before that search for anthers yourself. Traversing DP in EFI is a primitive task and I believe this forum has a few post on that. And another thing, refer to EDK, it has all the implementation you will ever need! EDK is a huge source of info how-to in EFI! – Alex D Oct 12 '16 at 17:55
  • @Alex, Can you please provide complete code? I literally confused and nothing for me works fine! – Ali Sepehri-Amin Jul 21 '20 at 08:13
  • 1
    @AliSepehri-Amin this is actually a complete code. At least it gives complete direction on how to do it. I am not sure what else do you need. And you know that a correct question might have include at least half of an answer. Nothing works is not very informative, there are at least half a dozen steps. What steps you don't understand? Or what steps don't work for you? – Alex D Jul 22 '20 at 21:35
  • @Alex Thank you for response, the code worked fine but I have another problem as I described in this question, can you please gimme an guide? https://stackoverflow.com/questions/63029203/efi-application-erorr-write-protected – Ali Sepehri-Amin Jul 24 '20 at 12:13
  • @AliSepehri-Amin I looked into your question. It's really hard to figure out what's going on, man. – Alex D Jul 26 '20 at 05:12