Skip to content

Commit

Permalink
CapsuleSplashDxe/CapsuleSplashDxe.c: print some warnings
Browse files Browse the repository at this point in the history
Signed-off-by: Krystian Hebel <[email protected]>
  • Loading branch information
krystian-hebel authored and SergiiDmytruk committed Sep 27, 2024
1 parent 4bb9525 commit 985f143
Showing 1 changed file with 160 additions and 1 deletion.
161 changes: 160 additions & 1 deletion DasharoPayloadPkg/CapsuleSplashDxe/CapsuleSplashDxe.c
Original file line number Diff line number Diff line change
Expand Up @@ -19,10 +19,31 @@ SPDX-License-Identifier: BSD-2-Clause-Patent

#include <Library/BaseLib.h>
#include <Library/BaseMemoryLib.h>
#include <Library/DebugLib.h>
#include <Library/MemoryAllocationLib.h>
#include <Library/SafeIntLib.h>
#include <Library/UefiBootServicesTableLib.h>

/**
scalable-font2 memory management assumes existence of realloc() as defined by
C standard. The closest edk2 has to offer seems to be ReallocatePool(), but
it requires the caller to pass the old size as an argument. Instead of adding
bookkeeping code, just turn off dynamic memory allocation completely by
defining SSFN_MAXLINES. This has some serious limitations, but should be
enough to print few simple lines. It also makes context size significantly
bigger, so move it to .bss to reduce stack usage.
https://gitlab.com/bztsrc/scalable-font2/blob/master/docs/API.md#configuring-memory-management
**/

#define SSFN_MAXLINES 4096
#define SSFN_IMPLEMENTATION
#include "SSFN/ssfn.h"
#include "SSFN/VeraB.h"

ssfn_t mCtx;
ssfn_buf_t mFBuf;

/**
Dimensions used by DisplayUpdateProgressLibGraphics:
Expand Down Expand Up @@ -143,6 +164,102 @@ SetDummyLogo (
return Status;
}

// Note: this is horizontal baseline (aka ascender), not line height.
#define FONT_SIZE_PERCENT 5
#define FONT_SIZE(Mode) (Mode->VerticalResolution * FONT_SIZE_PERCENT / 100)

EFI_STATUS
InitSsfn (
EFI_PHYSICAL_ADDRESS FrameBufferBase,
EFI_GRAPHICS_OUTPUT_MODE_INFORMATION *Info
)
{
int Status;

//
// Context must be zeroed, done by having it in .bss.
//

//
// Load the font.
//
Status = ssfn_load(&mCtx, VeraB);
if (Status != SSFN_OK) {
DEBUG ((DEBUG_ERROR, "ssfn_load failed with %a!\n", ssfn_error(Status)));
return EFI_UNSUPPORTED;
}

//
// Select the face.
//
Status = ssfn_select(&mCtx, SSFN_FAMILY_ANY, NULL, SSFN_STYLE_BOLD, FONT_SIZE(Info));
if (Status != SSFN_OK) {
DEBUG ((DEBUG_ERROR, "ssfn_select failed with %a!\n", ssfn_error(Status)));
return EFI_UNSUPPORTED;
}

//
// Initialize fields of frame buffer context that won't change.
//
mFBuf.ptr = (uint8_t *)FrameBufferBase;
mFBuf.w = Info->HorizontalResolution;
mFBuf.h = Info->VerticalResolution;
mFBuf.p = Info->PixelsPerScanLine * sizeof (EFI_GRAPHICS_OUTPUT_BLT_PIXEL);
mFBuf.fg = 0xFFFFFFFF; // This uses alpha channel for anti-aliasing
mFBuf.bg = 0;

return EFI_SUCCESS;
}

EFI_STATUS
RenderTextCenteredAt (
const char *str,
UINTN x,
UINTN y
)
{
int w, h, left, top;
int Status;

//
// Get the size of rendered text.
//
Status = ssfn_bbox(&mCtx, str, &w, &h, &left, &top);
if (Status != SSFN_OK) {
DEBUG ((DEBUG_ERROR, "ssfn_bbox failed with %a!\n", ssfn_error(Status)));
return EFI_UNSUPPORTED;
}

//
// Calculate and bound leftmost baseline coordinates of text.
//
w /= 2;
h /= 2;
mFBuf.x = x > w ? x - w : 0;
mFBuf.y = y > h ? y - h : 0;

//
// Add baseline to get the final coordinates.
//
mFBuf.x += left;
mFBuf.y += top;

//
// Render the text. The function takes care to not write outside of buffer.
// It returns number of bytes consumed to render one glyph (UTF-8). Special
// characters like \n are not handled.
//
while((Status = ssfn_render(&mCtx, &mFBuf, str)) > 0)
str += Status;

if (Status < 0) {
DEBUG ((DEBUG_ERROR, "ssfn_render failed with %a!\n", ssfn_error(Status)));
return EFI_UNSUPPORTED;
}

return EFI_SUCCESS;
}

/**
The Entry Point for CapsuleSplash driver.
Expand Down Expand Up @@ -171,16 +288,58 @@ CapsuleSplashEntry (
&gEfiGraphicsOutputProtocolGuid,
(VOID **)&GraphicsOutput);
if (EFI_ERROR (Status)) {
return EFI_UNSUPPORTED;
DEBUG ((DEBUG_ERROR, "Couldn't find GOP\n"));
return Status;
}

ModeInfo = GraphicsOutput->Mode->Info;
if (ModeInfo->PixelFormat != PixelRedGreenBlueReserved8BitPerColor &&
ModeInfo->PixelFormat != PixelBlueGreenRedReserved8BitPerColor) {
DEBUG ((DEBUG_ERROR, "Wrong pixel format\n"));
return EFI_UNSUPPORTED;
}

//
// Set dummy logo for consistent progress bar
//
Status = SetDummyLogo (ModeInfo);
if (EFI_ERROR (Status)) {
DEBUG ((DEBUG_ERROR, "Couldn't set dummy logo\n"));
return Status;
}

//
// Initialize font renderer.
//
Status = InitSsfn (GraphicsOutput->Mode->FrameBufferBase, ModeInfo);
if (EFI_ERROR (Status)) {
DEBUG ((DEBUG_ERROR, "Couldn't init font renderer\n"));
return Status;
}

//
// Clear the background. We don't know what logo was shown there, if any.
//
SetMem ((VOID *)GraphicsOutput->Mode->FrameBufferBase,
GraphicsOutput->Mode->FrameBufferSize,
0x00);

//
// Print some warnings.
//
Status = RenderTextCenteredAt("Firmware update in progress",
ModeInfo->HorizontalResolution / 2,
ModeInfo->VerticalResolution * 20 / 100);
if (EFI_ERROR (Status)) {
return Status;
}

Status = RenderTextCenteredAt("Don't turn off your platform!",
ModeInfo->HorizontalResolution / 2,
ModeInfo->VerticalResolution * 90 / 100);
if (EFI_ERROR (Status)) {
return Status;
}

return Status;
}

0 comments on commit 985f143

Please sign in to comment.