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

最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
  • 序言:七十年代末,一起剥皮案震惊了整个滨河市,随后出现的几起案子,更是在滨河造成了极大的恐慌,老刑警刘岩,带你破解...
    沈念sama阅读 202,529评论 5 475
  • 序言:滨河连续发生了三起死亡事件,死亡现场离奇诡异,居然都是意外死亡,警方通过查阅死者的电脑和手机,发现死者居然都...
    沈念sama阅读 85,015评论 2 379
  • 文/潘晓璐 我一进店门,熙熙楼的掌柜王于贵愁眉苦脸地迎上来,“玉大人,你说我怎么就摊上这事。” “怎么了?”我有些...
    开封第一讲书人阅读 149,409评论 0 335
  • 文/不坏的土叔 我叫张陵,是天一观的道长。 经常有香客问我,道长,这世上最难降的妖魔是什么? 我笑而不...
    开封第一讲书人阅读 54,385评论 1 273
  • 正文 为了忘掉前任,我火速办了婚礼,结果婚礼上,老公的妹妹穿的比我还像新娘。我一直安慰自己,他们只是感情好,可当我...
    茶点故事阅读 63,387评论 5 364
  • 文/花漫 我一把揭开白布。 她就那样静静地躺着,像睡着了一般。 火红的嫁衣衬着肌肤如雪。 梳的纹丝不乱的头发上,一...
    开封第一讲书人阅读 48,466评论 1 281
  • 那天,我揣着相机与录音,去河边找鬼。 笑死,一个胖子当着我的面吹牛,可吹牛的内容都是我干的。 我是一名探鬼主播,决...
    沈念sama阅读 37,880评论 3 395
  • 文/苍兰香墨 我猛地睁开眼,长吁一口气:“原来是场噩梦啊……” “哼!你这毒妇竟也来了?” 一声冷哼从身侧响起,我...
    开封第一讲书人阅读 36,528评论 0 256
  • 序言:老挝万荣一对情侣失踪,失踪者是张志新(化名)和其女友刘颖,没想到半个月后,有当地人在树林里发现了一具尸体,经...
    沈念sama阅读 40,727评论 1 295
  • 正文 独居荒郊野岭守林人离奇死亡,尸身上长有42处带血的脓包…… 初始之章·张勋 以下内容为张勋视角 年9月15日...
    茶点故事阅读 35,528评论 2 319
  • 正文 我和宋清朗相恋三年,在试婚纱的时候发现自己被绿了。 大学时的朋友给我发了我未婚夫和他白月光在一起吃饭的照片。...
    茶点故事阅读 37,602评论 1 329
  • 序言:一个原本活蹦乱跳的男人离奇死亡,死状恐怖,灵堂内的尸体忽然破棺而出,到底是诈尸还是另有隐情,我是刑警宁泽,带...
    沈念sama阅读 33,302评论 4 318
  • 正文 年R本政府宣布,位于F岛的核电站,受9级特大地震影响,放射性物质发生泄漏。R本人自食恶果不足惜,却给世界环境...
    茶点故事阅读 38,873评论 3 306
  • 文/蒙蒙 一、第九天 我趴在偏房一处隐蔽的房顶上张望。 院中可真热闹,春花似锦、人声如沸。这庄子的主人今日做“春日...
    开封第一讲书人阅读 29,890评论 0 19
  • 文/苍兰香墨 我抬头看了看天上的太阳。三九已至,却和暖如春,着一层夹袄步出监牢的瞬间,已是汗流浃背。 一阵脚步声响...
    开封第一讲书人阅读 31,132评论 1 259
  • 我被黑心中介骗来泰国打工, 没想到刚下飞机就差点儿被人妖公主榨干…… 1. 我叫王不留,地道东北人。 一个月前我还...
    沈念sama阅读 42,777评论 2 349
  • 正文 我出身青楼,却偏偏与公主长得像,于是被迫代替她去往敌国和亲。 传闻我的和亲对象是个残疾皇子,可洞房花烛夜当晚...
    茶点故事阅读 42,310评论 2 342