Building UEFI driver using Visual Studio

6.8k views Asked by At

I'm looking for advice on how to build UEFI drivers with the EDK2 SDK using a Visual Studio 2012 project. I'm trying to statically link UefiLib.lib but failing miserably. I've added the lib to the additional dependencies under linker.

#include  <Uefi.h>
#include  <Library/UefiLib.h>

EFI_STATUS
EFIAPI
UefiMain (
  IN EFI_HANDLE        ImageHandle,
  IN EFI_SYSTEM_TABLE  *SystemTable
  )
{
 Print((CHAR16 *)L"Welcome to the world of EDK II.\n");
 return EFI_SUCCESS;
}

Error is

test.obj : error LNK2001: unresolved external symbol "unsigned __int64 __cdecl Print(unsigned short const *,...)" (?Print@@YA_KPEBGZZ)

Now i've used DUMPBIN to make sure that Print exists in the lib, however it exists not as an export or import but as an archivemember. I'm not sure if thats the issue.

1

There are 1 answers

4
sun2sirius On

You cannot build a UEFI driver or application separately, it must be done within the EDK2 source tree, which has a lot of header files and libraries necessary to compile and link an EDK2 component. EDK2 components must be built with EDK2 build tools, the C compiler/linker is only a step in the EDK2 build process. You should start from building using command line to make sure you have the environment setup properly, then you can try bringing the components you want to develop into Visual Studio.

In Visual Studio you can start with creating a Makefile Project - this is necessary to specify all the custom build steps. For example, go to your project's Property Pages, under Configuration Properties -> NMake -> General -> Build Command Line. Put the following code in the Build Command Line; it will allow you to build the entire AppPkg package, which includes the Hello app you are trying to build.

cd C:\src\edk2
call "C:\Program Files (x86)\Microsoft Visual Studio 11.0\VC\vcvarsall.bat" x86
call edksetup.bat
build.exe -p c:\src\edk2\AppPkg\AppPkg.dsc -t VS2012x86  -b DEBUG

If you want to only build Hello, you would have to customize the AppPkg.dsc file, which is used to build the AppPkg package. There are ways to further tweak the build process by using more advanced command line switches with the build.exe utility - you would have to study the EDK2 manuals available at TianoCore.org.

The above-mentioned steps should help you to get started with building a UEFI app/driver within Visual Studio, but you probably want to use Visual Studio for its powerful IntelliSense and Debugging features. To do this you should start with building the Nt32Pkg package, which creates an EFI emulated environment in Windows and allows you to step through the code and debug a UEFI driver/app as a regular Windows application. Setting up a Visual Studio project to do this is a bit tricky, unless you use a specialized VS extension for UEFI development that automates all this work for you, such as Phoenix Core Architect. However, it is possible to do manually, you just need to bring a part or all the of file/folder structure of EDK2 source into the VS Solution Explorer by hand. The result of Nt32Pkg build is the Windows application C:\src\edk2\Build\NT32IA32\DEBUG_VS2012x86\IA32\SecMain.exe - that should get you started in Windows environment and introduce you to UEFI.