polishing a bit

This commit is contained in:
PoliEcho 2025-08-16 13:20:03 +02:00
parent f2ad30047a
commit 887e393d92
12 changed files with 104 additions and 35 deletions

View File

@ -30,6 +30,8 @@ LocalApicLib|edk2/UefiCpuPkg/Library/BaseXApicLib/BaseXApicLib.inf
CpuLib|edk2/MdePkg/Library/BaseCpuLib/BaseCpuLib.inf CpuLib|edk2/MdePkg/Library/BaseCpuLib/BaseCpuLib.inf
UefiRuntimeServicesTableLib|edk2/MdePkg/Library/UefiRuntimeServicesTableLib/UefiRuntimeServicesTableLib.inf UefiRuntimeServicesTableLib|edk2/MdePkg/Library/UefiRuntimeServicesTableLib/UefiRuntimeServicesTableLib.inf
TimerLib|edk2/MdePkg/Library/BaseTimerLibNullTemplate/BaseTimerLibNullTemplate.inf TimerLib|edk2/MdePkg/Library/BaseTimerLibNullTemplate/BaseTimerLibNullTemplate.inf
BmpSupportLib|edk2/MdeModulePkg/Library/BaseBmpSupportLib/BaseBmpSupportLib.inf
SafeIntLib|edk2/MdePkg/Library/BaseSafeIntLib/BaseSafeIntLib.inf
[Components] [Components]
src/UEFI_fireworks/UEFI_fireworks.inf src/UEFI_fireworks/UEFI_fireworks.inf

BIN
assets/rocket.bmp Normal file

Binary file not shown.

After

Width:  |  Height:  |  Size: 1.3 KiB

BIN
assets/rocket.xcf Normal file

Binary file not shown.

View File

@ -1,5 +1,6 @@
#!/bin/bash #!/bin/bash
export PACKAGES_PATH=$PWD:$PWD/edk2 export PACKAGES_PATH=$PWD:$PWD/edk2
xxd -i assets/rocket.bmp > src/UEFI_fireworks/rocket.c
if [ "$1" == "debug" ]; then if [ "$1" == "debug" ]; then
build -a X64 -t GCC5 -p UEFI_fireworks.dsc -b DEBUG build -a X64 -t GCC5 -p UEFI_fireworks.dsc -b DEBUG
else else

View File

@ -11,6 +11,7 @@
drawing.c drawing.c
rng.c rng.c
time.c time.c
rocket.c
[Packages] [Packages]
edk2/MdePkg/MdePkg.dec edk2/MdePkg/MdePkg.dec
@ -30,6 +31,7 @@
RngLib RngLib
SerialPortLib SerialPortLib
UefiRuntimeServicesTableLib UefiRuntimeServicesTableLib
BmpSupportLib
[Protocols] [Protocols]
gEfiRngProtocolGuid gEfiRngProtocolGuid

View File

@ -1,11 +1,12 @@
#include "Library/UefiApplicationEntryPoint.h"
#include "ProcessorBind.h" #include "ProcessorBind.h"
#include "const.h"
#include "drawing.h" #include "drawing.h"
#include "global.h" #include "global.h"
#include "macros.h" #include "macros.h"
#include "rng.h" #include "rng.h"
#include "time.h" #include "time.h"
#include "types.h" #include "types.h"
#include <../MdeModulePkg/Include/Library/BmpSupportLib.h>
#include <Base.h> #include <Base.h>
#include <Library/BaseLib.h> #include <Library/BaseLib.h>
#include <Library/DebugLib.h> #include <Library/DebugLib.h>
@ -18,6 +19,8 @@
#include <Protocol/GraphicsOutput.h> #include <Protocol/GraphicsOutput.h>
#include <Uefi.h> #include <Uefi.h>
#include <stdint.h> #include <stdint.h>
EFI_GRAPHICS_OUTPUT_BLT_PIXEL night_sky =
COLOR_FROM_HEX(0x11095e); // this cannot be const becose EDK2 said so
firework_instance create_firework(); firework_instance create_firework();
EFI_GRAPHICS_OUTPUT_PROTOCOL *GraphicsOutput = NULL; EFI_GRAPHICS_OUTPUT_PROTOCOL *GraphicsOutput = NULL;
@ -67,14 +70,14 @@ EFI_STATUS EFIAPI UefiMain(IN EFI_HANDLE imgHandle,
0); // make all pointers null 0); // make all pointers null
SERIAL_PRINT("DOES it work?\n"); SERIAL_PRINT("DOES it work?\n");
Print(L"If you see this message timer does not work\n"); Print(L"If you see this message for long time, timer does not work\n");
milisleep(100); milisleep(1);
clear_screen(); clear_screen();
while (TRUE) { while (TRUE) {
UINT8 random; UINT8 random;
fill_random_bytes(&random, sizeof(random)); fill_random_bytes(&random, sizeof(random));
if (random % 6 == 0) { if (random % 30 == 0) {
// spawn new firework // spawn new firework
firework_instance *new_firework_instence = firework_instance *new_firework_instence =
AllocateZeroPool(sizeof(firework_instance)); AllocateZeroPool(sizeof(firework_instance));
@ -85,7 +88,7 @@ EFI_STATUS EFIAPI UefiMain(IN EFI_HANDLE imgHandle,
*new_firework_instence = create_firework(); *new_firework_instence = create_firework();
for (UINT8 i = 0; i < ARRAY_SIZE(firework_array); i++) { for (UINT8 i = 0; i < ARRAY_SIZE(firework_array); i++) {
if (firework_array[i] == NULL || firework_array[i]->active != TRUE) { if (firework_array[i] == NULL || firework_array[i]->status != TRUE) {
if (firework_array[i] != NULL) { if (firework_array[i] != NULL) {
FreePool(firework_array[i]); // free firework FreePool(firework_array[i]); // free firework
firework_array[i] = NULL; firework_array[i] = NULL;
@ -99,9 +102,9 @@ EFI_STATUS EFIAPI UefiMain(IN EFI_HANDLE imgHandle,
assgned: assgned:
for (UINT8 i = 0; i < ARRAY_SIZE(firework_array); i++) { for (UINT8 i = 0; i < ARRAY_SIZE(firework_array); i++) {
if (firework_array[i] != NULL) { if (firework_array[i] != NULL) {
if (firework_array[i]->active == TRUE) { if (firework_array[i]->status == ACTIVE) {
if (!step_firework(firework_array[i])) { if (!step_firework(firework_array[i])) {
firework_array[i]->active = FALSE; firework_array[i]->status = INACTIVE;
} }
} else { } else {
FreePool(firework_array[i]); // free firework FreePool(firework_array[i]); // free firework
@ -132,8 +135,9 @@ firework_instance create_firework() {
fill_random_bytes(&random, sizeof(random)); fill_random_bytes(&random, sizeof(random));
firework.x = random % GraphicsOutput->Mode->Info->HorizontalResolution; firework.x = random % GraphicsOutput->Mode->Info->HorizontalResolution;
fill_random_bytes(&random, sizeof(random)); fill_random_bytes(&random, sizeof(random));
firework.y = random % GraphicsOutput->Mode->Info->VerticalResolution; firework.y = random % GraphicsOutput->Mode->Info->VerticalResolution /
2; // spawn only on upper half of the screen
firework.active = TRUE; firework.status = ACTIVE; // TODO set to LAUNCHING when implemented
return firework; return firework;
} }

3
src/UEFI_fireworks/bmp.h Normal file
View File

@ -0,0 +1,3 @@
#pragma once
extern unsigned char assets_rocket_bmp[];
extern unsigned int assets_rocket_bmp_len;

View File

@ -2,5 +2,6 @@
#include "macros.h" #include "macros.h"
#include <Protocol/GraphicsOutput.h> #include <Protocol/GraphicsOutput.h>
EFI_GRAPHICS_OUTPUT_BLT_PIXEL night_sky = extern EFI_GRAPHICS_OUTPUT_BLT_PIXEL night_sky;
COLOR_FROM_HEX(0x11095e); // this cannot be const becose EDK2 said so
enum firework_status { INACTIVE, ACTIVE, LAUNCHING };

View File

@ -1,5 +1,4 @@
#include "Base.h" #include "Base.h"
#include "Library/UefiLib.h"
#include "ProcessorBind.h" #include "ProcessorBind.h"
#include "const.h" #include "const.h"
#include "global.h" #include "global.h"
@ -105,3 +104,5 @@ BOOLEAN step_firework(firework_instance *firework) {
} }
return TRUE; return TRUE;
} }
BOOLEAN step_rocket(rocket_instance *rocket) {}

View File

@ -2,12 +2,14 @@
#include "Library/UefiBootServicesTableLib.h" #include "Library/UefiBootServicesTableLib.h"
#include "Library/UefiLib.h" #include "Library/UefiLib.h"
#include "ProcessorBind.h" #include "ProcessorBind.h"
#include "Uefi/UefiBaseType.h"
#include "time.h" #include "time.h"
#include <Library/BaseLib.h> #include <Library/BaseLib.h>
#include <Library/RngLib.h> #include <Library/RngLib.h>
#include <Library/TimerLib.h> #include <Library/TimerLib.h>
#include <Library/UefiRuntimeServicesTableLib.h> #include <Library/UefiRuntimeServicesTableLib.h>
#include <Uefi.h> #include <Uefi.h>
#include <stdint.h>
void (*fill_random_bytes)(void *dst, UINTN n); void (*fill_random_bytes)(void *dst, UINTN n);
UINT16 shift_LFSR(UINT16 LFSR) { UINT16 shift_LFSR(UINT16 LFSR) {
@ -19,22 +21,6 @@ UINT16 shift_LFSR(UINT16 LFSR) {
return LFSR; return LFSR;
} }
void fill_random_bytes_LFSR(void *dst, UINTN n) {
static UINT16 LFSR = 0xf5d7;
if (n % 2 != 0) {
LFSR = shift_LFSR(LFSR);
*(UINT8 *)dst = (UINT8)LFSR;
dst++;
n--;
}
while (n != 0) {
LFSR = shift_LFSR(LFSR);
*(UINT16 *)dst = LFSR;
dst += 2;
n -= 2;
}
}
void fill_random_bytes_TRUE(void *dst, UINTN n) { void fill_random_bytes_TRUE(void *dst, UINTN n) {
UINT16 rand; UINT16 rand;
if (n % 2 != 0) { if (n % 2 != 0) {
@ -50,6 +36,21 @@ void fill_random_bytes_TRUE(void *dst, UINTN n) {
n -= 2; n -= 2;
} }
} }
UINT16 LFSR;
void fill_random_bytes_LFSR(void *dst, UINTN n) {
if (n % 2 != 0) {
LFSR = shift_LFSR(LFSR);
*(UINT8 *)dst = (UINT8)LFSR;
dst++;
n--;
}
while (n != 0) {
LFSR = shift_LFSR(LFSR);
*(UINT16 *)dst = LFSR;
dst += 2;
n -= 2;
}
}
void init_rng() { void init_rng() {
UINT16 test_var; UINT16 test_var;
@ -59,12 +60,58 @@ void init_rng() {
gST->ConOut->SetAttribute(gST->ConOut, EFI_YELLOW); gST->ConOut->SetAttribute(gST->ConOut, EFI_YELLOW);
Print(L"[WARNING]"); Print(L"[WARNING]");
gST->ConOut->SetAttribute(gST->ConOut, EFI_WHITE); gST->ConOut->SetAttribute(gST->ConOut, EFI_WHITE);
Print(L" RNG device not available falling back to LFSR\r\n"); Print(L" RNG device not available falling back to LFSR!\r\n");
Print(L"Continuing in "); EFI_TIME Time;
for (UINT8 i = 5; i > 0; i--) { EFI_STATUS Status;
Print(L"\b%d", i); UINT32 starting;
milisleep(1000); Status = gRT->GetTime(&Time, NULL);
if (EFI_ERROR(Status)) {
Print(L"failed to get time\r\n");
milisleep(2000);
starting = 1;
} else {
starting = Time.Second;
}
Print(L"Press any key to seed the LFSR\r\n");
EFI_INPUT_KEY Key;
UINTN Index;
Status = gBS->WaitForEvent(1, &(gST->ConIn->WaitForKey), &Index);
if (EFI_ERROR(Status)) {
Print(L"failed to wait key\r\n");
Key.ScanCode = 1;
Key.UnicodeChar = 1;
} else {
Status = gST->ConIn->ReadKeyStroke(gST->ConIn, &Key);
if (EFI_ERROR(Status)) {
Print(L"failed to read key\r\n");
Key.ScanCode = 1;
Key.UnicodeChar = 1;
}
}
if (Key.ScanCode == 0) {
Key.ScanCode = 1;
}
Status = gRT->GetTime(&Time, NULL);
if (EFI_ERROR(Status)) {
Print(L"failed to get time\r\n");
Time.Second = 1;
}
UINT8 time_diff = (Time.Second - starting);
if (time_diff == 0) {
time_diff = 1;
}
LFSR = (typeof(LFSR))(((UINTN)Key.ScanCode * (UINTN)Key.UnicodeChar *
(UINTN)(time_diff)) %
UINT16_MAX);
if (LFSR <= 1) {
LFSR = 0xf5d7;
} }
fill_random_bytes = fill_random_bytes_LFSR; fill_random_bytes = fill_random_bytes_LFSR;

View File

@ -1,2 +1,2 @@
#pragma once #pragma once
void milisleep(UINTN Milliseconds); void milisleep(UINTN n);

View File

@ -1,13 +1,21 @@
#pragma once #pragma once
#include "ProcessorBind.h" #include "ProcessorBind.h"
#include "Protocol/GraphicsOutput.h" #include "Protocol/GraphicsOutput.h"
#include "const.h"
#include <Library/BaseLib.h> #include <Library/BaseLib.h>
typedef struct { typedef struct {
BOOLEAN active; // IF FALSE can be overwriten UINT32 x;
UINT32 y;
} rocket_instance;
typedef struct {
enum firework_status status; // IF INACTIVE can be overwriten
UINT32 x; UINT32 x;
UINT32 y; UINT32 y;
UINT16 max_r; UINT16 max_r;
UINT16 r[3]; UINT16 r[3];
EFI_GRAPHICS_OUTPUT_BLT_PIXEL color[3]; EFI_GRAPHICS_OUTPUT_BLT_PIXEL color[3];
UINT16 cleanup_r; UINT16 cleanup_r;
rocket_instance rocket;
} firework_instance; } firework_instance;