如何在EFI图形模式下使用文本?

时间:2019-05-07 18:33:42

标签: c macos uefi

我对创建efi应用程序是超级新手。我的目标是在efi中创建一个小型应用程序,在背景上显示一些文本。但是我一直坚持尝试在显示器上显示文本(伟大的是拥有一种自定义字体,但这在现阶段不是必须的)。我希望该应用程序(也)可以在苹果系统上运行(从USB引导)

  1. 如何找到有关EFI功能的优质文档?看起来很难找到好的例子,等等。

  2. 如何使用EFI在背景上显示文本?

这是我到目前为止所得到的。我使用图形协议将背景更改为颜色。如何在上面显示文本。输出字符串似乎不起作用。

#include "efibind.h"
#include "efidef.h"
#include "efidevp.h"
#include "eficon.h"
#include "efiapi.h"
#include "efierr.h"
#include "efiprot.h"

static EFI_GUID GraphicsOutputProtocolGUID = EFI_GRAPHICS_OUTPUT_PROTOCOL_GUID;

/**
 * efi_main - The entry point for the EFI application
 * @image: firmware-allocated handle that identifies the image
 * @SystemTable: EFI system table
 */
EFI_STATUS efi_main(EFI_HANDLE image, EFI_SYSTEM_TABLE *systemTable) {
        EFI_BOOT_SERVICES *bs = systemTable->BootServices;
        EFI_STATUS status;
        EFI_GRAPHICS_OUTPUT_PROTOCOL *graphicsProtocol;
        SIMPLE_TEXT_OUTPUT_INTERFACE *conOut = systemTable->ConOut;
        EFI_GRAPHICS_OUTPUT_MODE_INFORMATION *info;
        UINTN SizeOfInfo, sWidth, sHeight;

        status = bs->LocateProtocol(&GraphicsOutputProtocolGUID, NULL, 
                (void**)&graphicsProtocol);

        if (EFI_ERROR(status) || graphicsProtocol == NULL) {
                conOut->OutputString(conOut, L"Failed to init gfx!\r\n");
                return status;
        }

        conOut->ClearScreen(conOut);

        //Switch to current mode so gfx is started.
        status = graphicsProtocol->SetMode(graphicsProtocol, graphicsProtocol->Mode->Mode);
        if (EFI_ERROR(status)) {
                conOut->OutputString(conOut, L"Failed to set default mode!\r\n");
                return status;
        }

        EFI_GRAPHICS_OUTPUT_BLT_PIXEL p;
        p.Red = 200;
        p.Green = 77;
        p.Blue = 13;
        graphicsProtocol->QueryMode(graphicsProtocol, graphicsProtocol->Mode->Mode, &SizeOfInfo, &info);
        sWidth = info->HorizontalResolution;
        sHeight = info->VerticalResolution;
        status = graphicsProtocol->Blt(graphicsProtocol, &p, EfiBltVideoFill, 0, 0, 0, 0, sWidth, sHeight, 0);


while (1) {
conOut->OutputString(conOut, L"Some text that I want to display\r\n");
bs->Stall(500000);
}



        return EFI_SUCCESS;
}

3 个答案:

答案 0 :(得分:0)

如果您专门针对MacEFI,则需要额外的协议调用以将控制台强制进入文本模式like this

答案 1 :(得分:0)

UEFI支持图形输出。它还支持文本输出(这可能意味着输出到串行控制台,或文本呈现到图形控制台,或两者)。但是,没有一种可控的方式在它们之间进行交互。

为图形环境提供文本元素(BIOS配置菜单,GRUB)的应用程序通常使用自己的框架通过GRAPHICS_OUTPUT_PROTOCOL在图形控制台上绘制文本。

答案 2 :(得分:0)

这是使用LVGL中的字体模块的文本渲染器的简短示例(可以单独使用,替换 #include“ ../../ lv_conf.h” 在带有 #define USE_LV_FONT_DEJAVU_20 8 的lv_font.h文件中)和来自GRAPHICS_OUTPUT_PROTOCOL

的Blt方法
#include <Uefi.h>
#include <Library\UefiLib.h>
#include <Protocol\GraphicsOutput.h>
#include "lv_font.h"

#define LETTER_SPACE 2
#define WAIT_SECONDS 10
#define FONT &lv_font_dejavu_20

static EFI_BOOT_SERVICES *gBS;
static EFI_RUNTIME_SERVICES *gRT;
static EFI_GRAPHICS_OUTPUT_PROTOCOL *gGOP = (EFI_GRAPHICS_OUTPUT_PROTOCOL *)NULL;
static EFI_GRAPHICS_OUTPUT_BLT_PIXEL gWhite = { 255,255,255,0 };

static void _util_render_glyph(UINT32 x, UINT32 y, CHAR8 letter)
{
    UINT32        height;
    UINT32        width;
    UINT32        pm_x;
    UINT32        pm_y;
    UINT32        index;
    const UINT8*  bitmap;
    EFI_GRAPHICS_OUTPUT_BLT_PIXEL *pixelmap;

    if (gGOP == NULL) {
        return;
    }

    height = lv_font_get_height(FONT);
    width = lv_font_get_width(FONT, letter);

    // glyph is not defined in this font
    if (width == 0) {
        return;
    }

    bitmap = lv_font_get_bitmap(FONT, letter);

    // using 8 bpp for simplicity
    if (EFI_ERROR(gBS->AllocatePool(EfiLoaderData, height * width * sizeof(*pixelmap), (VOID**)&pixelmap))) {
        return;
    }

    gBS->SetMem((VOID*)pixelmap, height * width * sizeof(*pixelmap), 0);

    // get the current content of the framebuffer to allow 'transparent' blt operations
    gGOP->Blt(gGOP, pixelmap, EfiBltVideoToBltBuffer, x, y, 0, 0, width, height, 0);

    for (pm_y = 0; pm_y < height; pm_y++) {
        for (pm_x = 0; pm_x < width; pm_x++) {
            index = width * pm_y + pm_x;

            if (bitmap[index] > 200) {
                pixelmap[index].Red = 0;
                pixelmap[index].Blue = 0;
                pixelmap[index].Green = 0;
                pixelmap[index].Reserved = 0;
            }
            else if (bitmap[index] > 100) {
                pixelmap[index].Red = 105;
                pixelmap[index].Blue = 105;
                pixelmap[index].Green = 105;
                pixelmap[index].Reserved = 0;
            }
        }
    }

    gGOP->Blt(gGOP, pixelmap, EfiBltBufferToVideo, 0, 0, x, y, width, height, 0);
    gBS->FreePool(pixelmap);
}

static void _util_render_text(UINT32 x, UINT32 y, const CHAR8 *string)
{
    UINT32 index;
    UINTN length;
    UINT32 scr_w;
    UINT32 scr_h;

    UINT32 str_x;

    UINT32 gly_w;
    UINT32 gly_h;

    if (string == NULL) {
        return;
    }

    if (gGOP == NULL) {
        return;
    }

    scr_w = gGOP->Mode->Info->HorizontalResolution;
    scr_h = gGOP->Mode->Info->VerticalResolution;

    length = AsciiStrnLenS(string, 32);

    gly_h = lv_font_get_height(FONT);

    // check if the string can be printed 
    if ((y + gly_h) > scr_h) {
        return;
    }

    if (x > scr_w) {
        return;
    }

    // print the string glyph by glyph
    str_x = x;
    for (index = 0; index < length; index++) {
        // check if the glyph can be printed
        gly_w = lv_font_get_width(FONT, string[index]);
        if ((str_x + gly_w) > scr_w) {
            break;
        }

        // print the glyph
        _util_render_glyph(str_x, y, string[index]);

        // calculate the position of the next glyph
        str_x += gly_w + LETTER_SPACE;
    }
}

static void _util_fill_screen(EFI_GRAPHICS_OUTPUT_BLT_PIXEL *color)
{
    if (gGOP == NULL) {
        return;
    }

    gGOP->Blt(gGOP, color, EfiBltVideoFill, 0, 0, 0, 0, gGOP->Mode->Info->HorizontalResolution, gGOP->Mode->Info->VerticalResolution, 0);
}


static void _util_wait(UINT32 seconds)
{
    EFI_TIME    time;
    UINT8       current_second = 255;
    UINT32      elapsed_seconds = 0;

    //wait for some seconds
    while (elapsed_seconds <= WAIT_SECONDS) {
        if (!EFI_ERROR(gRT->GetTime(&time, (EFI_TIME_CAPABILITIES*)NULL))) {
            if (current_second != time.Second) {
                elapsed_seconds++;
                current_second = time.Second;
            }
        }
        else {
            break;
        }
        CpuPause();
    }
}

EFI_STATUS
EFIAPI
UefiMain(
    IN EFI_HANDLE        ImageHandle,
    IN EFI_SYSTEM_TABLE  *SystemTable)
{
    EFI_STATUS  eRc;

    gBS = SystemTable->BootServices;
    gRT = SystemTable->RuntimeServices;

    eRc = gBS->LocateProtocol(
        &gEfiGraphicsOutputProtocolGuid,
        NULL,
        (VOID**)&gGOP);

    if (EFI_ERROR(eRc) || gGOP == NULL) {
        return EFI_SUCCESS;
    }

    _util_fill_screen(&gWhite);
    _util_render_text(0, 0, "HELLO WORLD!");

    _util_wait(WAIT_SECONDS);

    return EFI_SUCCESS;
}

我在PC和Mac上都对其进行了测试。使用LVGL网站上提供的工具,您可以使用所需的任何字体。