master ohmu/fastfetch-openbsd / src / detection / bootmgr / bootmgr.c
ohmu  ·  2026-10-15
 1#include "efi_helper.h"
 2
 3static inline uint8_t evBits(uint16_t val, uint8_t mask, uint8_t shift) {
 4    return (uint8_t) ((val & (mask << shift)) >> shift);
 5}
 6
 7static void ffEfiUcs2ToUtf8(const uint16_t* const chars, FFstrbuf* result) {
 8    for (uint32_t i = 0; chars[i]; i++) {
 9        if (chars[i] <= 0x007f) {
10            ffStrbufAppendC(result, (char) chars[i]);
11        } else if (chars[i] > 0x007f && chars[i] <= 0x07ff) {
12            ffStrbufAppendC(result, (char) (0xc0 | evBits(chars[i], 0x1f, 6)));
13            ffStrbufAppendC(result, (char) (0x80 | evBits(chars[i], 0x3f, 0)));
14        } else {
15            ffStrbufAppendC(result, (char) (0xe0 | evBits(chars[i], 0xf, 12)));
16            ffStrbufAppendC(result, (char) (0x80 | evBits(chars[i], 0x3f, 6)));
17            ffStrbufAppendC(result, (char) (0x80 | evBits(chars[i], 0x3f, 0)));
18        }
19    }
20}
21
22bool ffEfiFillLoadOption(const FFEfiLoadOption* efiOption, FFBootmgrResult* result) {
23    uint32_t descLen = 0;
24    while (efiOption->Description[descLen]) {
25        ++descLen;
26    }
27
28    if (descLen) {
29        ffEfiUcs2ToUtf8(efiOption->Description, &result->name);
30    }
31
32    for (
33        ffEfiDevicePathProtocol* filePathList = (ffEfiDevicePathProtocol*) &efiOption->Description[descLen + 1];
34        filePathList->Type != 0x7F; // End of Hardware Device Path
35        filePathList = (ffEfiDevicePathProtocol*) ((uint8_t*) filePathList + filePathList->Length)) {
36        if (filePathList->Type == 4 && filePathList->SubType == 4) {
37            // https://uefi.org/specs/UEFI/2.10/10_Protocols_Device_Path_Protocol.html#file-path-media-device-path
38            ffEfiUcs2ToUtf8((uint16_t*) filePathList->SpecificDevicePathData, &result->firmware);
39            return true;
40        }
41    }
42
43    return false;
44}