這個章節描述的是EFI會經常使用到的Protocol. 不管是要建立Device Driver, EFI pre-OS應用程式, 或者是Platform Firmware都應該要知道. 在這裡會有一些例子說明Protocol, 從最耳熟能響的"Hello world"開始. 這個測試會列在這裡是因為它最簡單. 它並不會與其他EFI Library的function有任何相依性. 所以在執行時並不會產生連結到EFI Library. 這個測試程式會使用Entry Point傳進來的SystemTable, 去取得EFI console device的控制權. 要在Console Output Device(通常是螢幕)上顯示一段訊息, 可以透過SIMPLE_TEXT_OUTPUT_INTERFACE Protocol的OutputString()來顯示訊息. 而Console Input Device(通常是鍵盤)可以等待使用者按下鍵盤, 可以透過SIMPLE_INPUT_INTERFACE Protocol的WaitForEvent()以及WaitForKey Event來達成. 一旦按鍵按下, 程式就會離開.
#include "efi.h"
EFI_STATUS
InitializeHelloApplication (
IN EFI_HANDLE ImageHandle,
IN EFI_SYSTEM_TABLE *SystemTable
)
{
UINTN Index;
//
// Send a message to the ConsoleOut device.
//
SystemTable->ConOut->OutputString (
SystemTable->ConOut,
L"Hello application started\n\r");
//
// Wait for the user to press a key.
//
SystemTable->ConOut->OutputString (
SystemTable->ConOut,
L"\n\r\n\r\n\rHit any key to exit\n\r");
SystemTable->BootServices->WaitForEvent (
1,
&(SystemTable->ConIn->WaitForKey),
&Index);
SystemTable->ConOut->OutputString (
SystemTable->ConOut,L"\n\r\n\r");
//
// Exit the application.
//
return EFI_SUCCESS;
}
要執行EFI的程式, 需要在EFI Shell的command line打入程式的名稱, 下面的例子可以知道如何在EFI Shell中執行程式.
Example:
Shell> hello
Hello application started
Hit any key to exit this image
沒有留言:
張貼留言