UEFI代码中执行UEFI shell命令

在UEFI代码中执行UEFI shell命令,类似于linux下C代码执行system("command")系统调用。

MyUefiShell.inf内容如下:

[Defines]                                                                          
 INF_VERSION = 1.25                                                                
 BASE_NAME = MyUefiShell                                                              
 FILE_GUID = 6987936E-ED34-44db-AE97-1FA5E4ED2116                                  
 MODULE_TYPE = UEFI_APPLICATION                                                    
 VERSION_STRING = 1.0                                                              
 ENTRY_POINT = UefiEntry                                                           
                                                                                   
[Sources]                                                                          
 MyUefiShell.c                                                                        
                                                                                   
[Packages]                                                                         
 MdePkg/MdePkg.dec                                                                 
                                                                                   
[LibraryClasses]                                                                   
 UefiApplicationEntryPoint                                                         
 UefiLib                                                                           
                                                                                   
[Guids]                                                                            
                                                                                   
[Ppis]                                                                             
                                                                                   
[Protocols]                                                                        
 gEfiShellProtocolGuid                                                             
                                                                                   
[FeaturePcd]                                                                       
                                                                                   
[Pcd] 

 

MyUefiShell.c内容如下, 代码中执行UEFI shell的map命令:

#include <Uefi.h>                                                                  
#include <Library/UefiLib.h>                                                       
#include <Library/UefiBootServicesTableLib.h>                                      
#include <Protocol/Shell.h>                                                        
                                                                                                                                
                                                                               
EFI_STATUS EFIAPI UefiEntry(IN EFI_HANDLE imgHandle, IN EFI_SYSTEM_TABLE *sysTable)
{                                                                                  
    EFI_SHELL_PROTOCOL    *EfiShellProtocol;                                       
    EFI_STATUS            Status;                                                  
    gBS = sysTable->BootServices;                                                  
                                                                                   
    gBS->SetWatchdogTimer(0, 0, 0, NULL);                                          
    Print(L"Execute UEFI shell command!\r\n");                                
    Status = gBS->LocateProtocol(&gEfiShellProtocolGuid,                           
                                NULL,                                              
                                (VOID **)&EfiShellProtocol);                       
    if (EFI_ERROR (Status)) {                                                   
        return Status;                                                          
    }                                                                           
                                                                                
    EfiShellProtocol->Execute(&imgHandle,                                       
                            L"map",                                            
                            NULL,                                               
                            &Status);                                           
                                                             
    return Status;                                                              
}                            

在edk2/MdeModulePkg/MdeModulePkg.dsc 里[Components]  下添加:

MyEfiApp/MyUefiShell.inf

如何搭建EDK2开发环境和新建项目,以及编译UEFI程序,请参考:ubuntu20.04下搭建EDK2开发环境 。

 

posted @ 2023-12-07 19:13  闹闹爸爸  阅读(110)  评论(0编辑  收藏  举报