List of Tips for UEFI Development

Last Updated on 04/11/18

This is the extraction of the UEFI development in form of an organized tips list. In this passage, we focus more on the implementation details than the general concepts involved in Tianocore.

(1) Pre-EFI Initialization

  • Debug information output in PEI pahse
// MdeModulePkg/Core/Pei/Image/Image.c
   if (Machine != EFI_IMAGE_MACHINE_IA64) {
      DEBUG ((EFI_D_INFO | EFI_D_LOAD, "Loading PEIM at 0x%11p EntryPoint=0x%11p ", (VOID *)(UINTN)ImageAddress, (VOID *)(UINTN)*EntryPoint));
    } else {
      // For IPF Image, the real entry point should be print.
      DEBUG ((EFI_D_INFO | EFI_D_LOAD, "Loading PEIM at 0x%11p EntryPoint=0x%11p ", (VOID *)(UINTN)ImageAddress, (VOID *)(UINTN)(*(UINT64 *)(UINTN)*EntryPoint)));
}
  • The dependency expression in PEI phase
  • The PcdLIb instances will read the Module INF to ensure whether the PCD exists, and replace the PCD Token used in Module C source file with the value defined in platform DEC according to the [Pcd] part of Module INF

(2) Debugging

  • Usage of QEMU serial port debugging:
# Ensure that the OVMF is built in DEBUG mode with DEBUG_ON_SERIAL_PORT enabled
# For common firmware on physical machine, ensure that the fundamental \
# debug library classes are included in the specific platform DSC
# and debug headers are included in the testing programmes.

../vtpm-support/qemu-tpm/x86_64-softmmu/qemu-system-x86_64 -display sdl \
-m 2048 -serial file:/home/hecmay/debug.log -global isa-debugcon.iobase=0x402 \
-net none -boot c -bios Build/Ovmf3264/DEBUG_GCC5/FV/OVMF.fd -boot menu=on \
-tpmdev cuse-tpm,id=tpm0,path=/dev/vtpm0 \
-device tpm-tis,tpmdev=tpm0 Build/test.img
  • Tesing UEFI Apps with virtual hard-disk
# create the test image file
 dd if=/dev/zero of=test.img bs=1M count=128

# format the file system of the image
mkfs -t vfat test.img

# Map the image to loop device && mount the formated image to /mnt/
sudo mount -o loop test.img /mnt/

# copy the compiled UEFI Apps to /mnt/ and run QEMU with it
qemu-system-x86_64 -bios Build/Ovmf3264/DEBUG_GCC5/FV/OVMF.fd test.img
  • Testing UEFI Application on VMware WorkStation

Simply build up a naked virtual machine without OS installed in VMware, and enable the EFI Support in VMware configuration, we are able to enter the EFI Shell stage in virtual machine (without using OVMF).

By inserting a USB stick with FAT compatible File System and dump the EFI Applications into it, the testing job will be much easier.
https://blog.fpmurphy.com/2014/07/using-vmware-workstation-to-experiment-with-uefi.html

  • Useful Hot-Keys of QEMU
    Ctrl + Alt: release the mouse
    Ctrl + Alt + 1: The main graphic console
    Ctrl + Alt + 2: The QEMU Command condole
    Ctrl + Alt + 3: Serial port debugging output

(3) Tricks for UEFI Aplication

  • The console output of UEFI Application
# If using the Print function defined in UEFI, ensure UefiLib.h is included
# For string of CHAR8 type, conversion to CHAR16 is needed like

static VOID
AsciiToUnicodeSize( CHAR8 *String, 
                   UINT8 length, 
                   CHAR16 *UniString)
{
   int len = length;

   while (*String != '\0' && len > 0) {
       *(UniString++) = (CHAR16) *(String++);
       len--;
   }
   *UniString = '\0';
}

CHAR16 Buffer[100];
AsciiToUnicodeSize(Str, length, Buffer);
Print(L"text here: %x, %d, %s", Addr, Status, Buffer);
  • The String type incompatibility error
// When running UEFI App in UEFI Shell
>FS: xxx.efi
>Error Command Status : Not Found
  // edk2/ShellPkg/Application/Shell/Shell.c +2583
  //
  // Now print errors
  // 
  if (EFI_ERROR(Status)) {
    ConstScriptFile = ShellCommandGetCurrentScriptFile();
    if (ConstScriptFile == NULL || ConstScriptFile->CurrentCommand == NULL) {
      ShellPrintHiiEx(-1, -1, NULL, STRING_TOKEN (STR_SHELL_ERROR), ShellInfoObject.HiiHandle, (VOID*)(Status));
    } else {
      ShellPrintHiiEx(-1, -1, NULL, STRING_TOKEN (STR_SHELL_ERROR_SCRIPT), ShellInfoObject.HiiHandle, (VOID*)(Status), ConstScriptFile->CurrentCommand->Line);
    }
  }

//
// ......
//

// edk2/ShellPkg/Application/Shell/Shell.uni
#string STR_SHELL_ERROR     #language en-US  "%NCommand Error Status: %r\r\n"

Make sure the type selection and variable correspondent, otherwise the Shell.efi will not be able to read out the script from it.

>>> cat /proc/sys/kernel/random/uuid
... 968b810c-00ea-42a5-88dc-6f8fa952c9b9
  • The definition of headers and compulsory inclusion for specific situation
> ShellPkg/Include/Library/ShellCEntryLib.h
>>  This header includes the initial definition of function ShellAppMain(), which is compulsory for UEFI Shell App

> ShellPkg/Include/Library/ShellLib.h
>> Similar but includes function handles for Efi Shell.
>> And redefinition of ShellAppMain() Should return a INTN instead of EFI_STATUS

* A simple example of gRT
#include <Uefi.h>
#include <Library/UefiLib.h>
#include <Library/ShellCEntryLib.h>
#include <Library/ShellLib.h>
#include <Library/UefiBootServicesTableLib.h>
#include <Library/UefiRuntimeServicesTableLib.h>

#include <Protocol/EfiShell.h>
#include <Protocol/LoadedImage.h>

INTN
EFIAPI
ShellAppMain (
          IN UINTN    Argc,
          IN CHAR16   **Argv
          )
{
    EFI_STATUS  Status = EFI_SUCCESS;
    gRT->ResetSystem(EfiResetCold, EFI_SUCCESS, 0, NULL);
    return Status;
}
  • The definition of fundamental types in UEFI
// MdePkg/Include/Ipf/ProcesserBind.h

// Other frequently-used base type in UEFI please refer to
// MdePkg/Include/Uefi/UefiBaseType.h

  ///
  /// 1-byte Character.
  ///
  typedef char                CHAR8;
  ///
  /// 1-byte signed value.
  ///
  typedef signed char         INT8;
#else
  ///
  /// 8-byte unsigned value.
  ///
  typedef unsigned long long  UINT64;
  ///
  /// 8-byte signed value.
  ///
  typedef long long           INT64;
  ///
  /// 4-byte unsigned value.
  ///
  typedef unsigned int        UINT32;
  ///
  /// 4-byte signed value.
  ///
  typedef int                 INT32;
  • Mechanism of Library/PCD/GUID Usage in Pkg Description File

The Platform includes the "Include" Path, the Protocol GUID, Platform Configuration Database items and the path of headers of the library in this specific Pkg

The DEC file includes the "Include" Path, with which the compiler will search for if encountering phrase like #include <Library/xxx.h> in the pragma code. In order to tell the compiler which Pkg "Include" Path the module is going to use, you should also declare the Pkg's DEC file path in the module's INF file.

About the Library you want to use: Please include the Library's INF files in the [LibraryClass] Part of the platform DSC File

最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
  • 序言:七十年代末涂佃,一起剝皮案震驚了整個濱河市蔚晨,隨后出現(xiàn)的幾起案子滥壕,更是在濱河造成了極大的恐慌建炫,老刑警劉巖惭蟋,帶你破解...
    沈念sama閱讀 216,402評論 6 499
  • 序言:濱河連續(xù)發(fā)生了三起死亡事件诅炉,死亡現(xiàn)場離奇詭異驻襟,居然都是意外死亡,警方通過查閱死者的電腦和手機叔锐,發(fā)現(xiàn)死者居然都...
    沈念sama閱讀 92,377評論 3 392
  • 文/潘曉璐 我一進店門挪鹏,熙熙樓的掌柜王于貴愁眉苦臉地迎上來见秽,“玉大人,你說我怎么就攤上這事讨盒〗馊。” “怎么了?”我有些...
    開封第一講書人閱讀 162,483評論 0 353
  • 文/不壞的土叔 我叫張陵返顺,是天一觀的道長禀苦。 經(jīng)常有香客問我,道長遂鹊,這世上最難降的妖魔是什么振乏? 我笑而不...
    開封第一講書人閱讀 58,165評論 1 292
  • 正文 為了忘掉前任,我火速辦了婚禮秉扑,結(jié)果婚禮上慧邮,老公的妹妹穿的比我還像新娘。我一直安慰自己舟陆,他們只是感情好误澳,可當我...
    茶點故事閱讀 67,176評論 6 388
  • 文/花漫 我一把揭開白布。 她就那樣靜靜地躺著秦躯,像睡著了一般忆谓。 火紅的嫁衣襯著肌膚如雪。 梳的紋絲不亂的頭發(fā)上踱承,一...
    開封第一講書人閱讀 51,146評論 1 297
  • 那天倡缠,我揣著相機與錄音,去河邊找鬼茎活。 笑死毡琉,一個胖子當著我的面吹牛妙色,可吹牛的內(nèi)容都是我干的桅滋。 我是一名探鬼主播身辨,決...
    沈念sama閱讀 40,032評論 3 417
  • 文/蒼蘭香墨 我猛地睜開眼,長吁一口氣:“原來是場噩夢啊……” “哼煌珊!你這毒婦竟也來了号俐?” 一聲冷哼從身側(cè)響起,我...
    開封第一講書人閱讀 38,896評論 0 274
  • 序言:老撾萬榮一對情侶失蹤定庵,失蹤者是張志新(化名)和其女友劉穎踪危,沒想到半個月后,有當?shù)厝嗽跇淞掷锇l(fā)現(xiàn)了一具尸體贞远,經(jīng)...
    沈念sama閱讀 45,311評論 1 310
  • 正文 獨居荒郊野嶺守林人離奇死亡,尸身上長有42處帶血的膿包…… 初始之章·張勛 以下內(nèi)容為張勛視角 年9月15日...
    茶點故事閱讀 37,536評論 2 332
  • 正文 我和宋清朗相戀三年笨忌,在試婚紗的時候發(fā)現(xiàn)自己被綠了蓝仲。 大學時的朋友給我發(fā)了我未婚夫和他白月光在一起吃飯的照片。...
    茶點故事閱讀 39,696評論 1 348
  • 序言:一個原本活蹦亂跳的男人離奇死亡官疲,死狀恐怖袱结,靈堂內(nèi)的尸體忽然破棺而出途凫,到底是詐尸還是另有隱情,我是刑警寧澤维费,帶...
    沈念sama閱讀 35,413評論 5 343
  • 正文 年R本政府宣布,位于F島的核電站掩完,受9級特大地震影響,放射性物質(zhì)發(fā)生泄漏且蓬。R本人自食惡果不足惜,卻給世界環(huán)境...
    茶點故事閱讀 41,008評論 3 325
  • 文/蒙蒙 一恶阴、第九天 我趴在偏房一處隱蔽的房頂上張望。 院中可真熱鬧冯事,春花似錦、人聲如沸昵仅。這莊子的主人今日做“春日...
    開封第一講書人閱讀 31,659評論 0 22
  • 文/蒼蘭香墨 我抬頭看了看天上的太陽。三九已至吕世,卻和暖如春,著一層夾襖步出監(jiān)牢的瞬間命辖,已是汗流浹背分蓖。 一陣腳步聲響...
    開封第一講書人閱讀 32,815評論 1 269
  • 我被黑心中介騙來泰國打工尔许, 沒想到剛下飛機就差點兒被人妖公主榨干…… 1. 我叫王不留么鹤,地道東北人母债。 一個月前我還...
    沈念sama閱讀 47,698評論 2 368
  • 正文 我出身青樓尝抖,卻偏偏與公主長得像,于是被迫代替她去往敵國和親昧辽。 傳聞我的和親對象是個殘疾皇子衙熔,可洞房花燭夜當晚...
    茶點故事閱讀 44,592評論 2 353