master ohmu/fastfetch-openbsd / src / modules / bootmgr / bootmgr.c
ohmu  ·  2026-10-15
  1#include "common/printing.h"
  2#include "common/jsonconfig.h"
  3#include "common/strutil.h"
  4#include "detection/bootmgr/bootmgr.h"
  5#include "modules/bootmgr/bootmgr.h"
  6
  7bool ffPrintBootmgr(FFBootmgrOptions* options) {
  8    bool success = false;
  9    FFBootmgrResult bootmgr = {
 10        .name = ffStrbufCreate(),
 11        .firmware = ffStrbufCreate(),
 12    };
 13
 14    const char* error = ffDetectBootmgr(&bootmgr);
 15
 16    if (error) {
 17        ffPrintError(FF_MODULE_GET_DISPLAY_NAME(Bootmgr), 0, &options->moduleArgs, FF_PRINT_TYPE_DEFAULT, "%s", error);
 18        goto exit;
 19    } else {
 20        FF_STRBUF_AUTO_DESTROY firmwareName = ffStrbufCreateCopy(&bootmgr.firmware);
 21#ifndef __APPLE__
 22        ffStrbufSubstrAfterLastC(&firmwareName, '\\');
 23#else
 24        ffStrbufSubstrAfterLastC(&firmwareName, '/');
 25#endif
 26
 27        if (options->moduleArgs.outputFormat.length == 0) {
 28            ffPrintLogoAndKey(FF_MODULE_GET_DISPLAY_NAME(Bootmgr), 0, &options->moduleArgs, FF_PRINT_TYPE_DEFAULT);
 29            ffStrbufWriteTo(&bootmgr.name, stdout);
 30            if (firmwareName.length > 0) {
 31                printf(" - %s\n", firmwareName.chars);
 32            } else {
 33                putchar('\n');
 34            }
 35        } else {
 36            FF_PRINT_FORMAT_CHECKED(FF_MODULE_GET_DISPLAY_NAME(Bootmgr), 0, &options->moduleArgs, FF_PRINT_TYPE_DEFAULT, ((FFformatarg[]) {
 37                                                                                                                FF_ARG(bootmgr.name, "name"),
 38                                                                                                                FF_ARG(bootmgr.firmware, "firmware-path"),
 39                                                                                                                FF_ARG(firmwareName, "firmware-name"),
 40                                                                                                                FF_ARG(bootmgr.secureBoot, "secure-boot"),
 41                                                                                                                FF_ARG(bootmgr.order, "order"),
 42                                                                                                            }));
 43        }
 44    }
 45    success = true;
 46
 47exit:
 48    ffStrbufDestroy(&bootmgr.name);
 49    ffStrbufDestroy(&bootmgr.firmware);
 50
 51    return success;
 52}
 53
 54void ffParseBootmgrJsonObject(FFBootmgrOptions* options, yyjson_val* module) {
 55    yyjson_val *key, *val;
 56    size_t idx, max;
 57    yyjson_obj_foreach (module, idx, max, key, val) {
 58        if (ffJsonConfigParseModuleArgs(key, val, &options->moduleArgs)) {
 59            continue;
 60        }
 61
 62        ffPrintError(FF_MODULE_GET_DISPLAY_NAME(Bootmgr), 0, &options->moduleArgs, FF_PRINT_TYPE_DEFAULT, "Unknown JSON key %s", unsafe_yyjson_get_str(key));
 63    }
 64}
 65
 66[[gnu::cold]]
 67void ffGenerateBootmgrJsonConfig(FFBootmgrOptions* options, yyjson_mut_doc* doc, yyjson_mut_val* module) {
 68    ffJsonConfigGenerateModuleArgsConfig(doc, module, &options->moduleArgs);
 69}
 70
 71bool ffGenerateBootmgrJsonResult([[maybe_unused]] FFBootmgrOptions* options, yyjson_mut_doc* doc, yyjson_mut_val* module) {
 72    bool success = false;
 73    FFBootmgrResult bootmgr = {
 74        .name = ffStrbufCreate(),
 75        .firmware = ffStrbufCreate(),
 76    };
 77
 78    const char* error = ffDetectBootmgr(&bootmgr);
 79
 80    if (error) {
 81        yyjson_mut_obj_add_str(doc, module, "error", error);
 82        goto exit;
 83    }
 84
 85    yyjson_mut_val* obj = yyjson_mut_obj_add_obj(doc, module, "result");
 86    yyjson_mut_obj_add_strbuf(doc, obj, "name", &bootmgr.name);
 87    yyjson_mut_obj_add_strbuf(doc, obj, "firmware", &bootmgr.firmware);
 88    yyjson_mut_obj_add_uint(doc, obj, "order", bootmgr.order);
 89    yyjson_mut_obj_add_bool(doc, obj, "secureBoot", bootmgr.secureBoot);
 90    success = true;
 91
 92exit:
 93    ffStrbufDestroy(&bootmgr.name);
 94    ffStrbufDestroy(&bootmgr.firmware);
 95    return success;
 96}
 97
 98void ffInitBootmgrOptions(FFBootmgrOptions* options) {
 99    ffOptionInitModuleArg(&options->moduleArgs, "");
100}
101
102void ffDestroyBootmgrOptions(FFBootmgrOptions* options) {
103    ffOptionDestroyModuleArg(&options->moduleArgs);
104}
105
106FFModuleBaseInfo ffBootmgrModuleInfo = {
107    .name = "Bootmgr",
108    .description = "Print second-stage bootloader information (name, firmware, etc.)",
109    .displayName = {
110        .en = "Boot Manager",
111        .ar = "مدير الإقلاع",
112        .cs = "Správce spouštění",
113        .de = "Boot-Manager",
114        .es = "Gestor de arranque",
115        .fi = "Käynnistyslataaja",
116        .fr = "Gestionnaire de démarrage",
117        .gl = "Xestor de arranque",
118        .he = "מנהל אתחול",
119        .id = "Manajer Boot",
120        .it = "Gestore di avvio",
121        .ja = "ブートマネージャー",
122        .ko = "부트 매니저",
123        .pl = "Menedżer rozruchu",
124        .pt = "Gerenciador de inicialização",
125        .ru = "Менеджер загрузки",
126        .tr = "Önyükleme Yöneticisi",
127        .uk = "Менеджер завантаження",
128        .vi = "Trình quản lý khởi động",
129        .zh_CN = "启动管理器",
130        .zh_TW = "啟動管理器",
131    },
132    .initOptions = (void*) ffInitBootmgrOptions,
133    .destroyOptions = (void*) ffDestroyBootmgrOptions,
134    .parseJsonObject = (void*) ffParseBootmgrJsonObject,
135    .printModule = (void*) ffPrintBootmgr,
136    .generateJsonResult = (void*) ffGenerateBootmgrJsonResult,
137    .generateJsonConfig = (void*) ffGenerateBootmgrJsonConfig,
138    .formatArgs = FF_FORMAT_ARG_LIST(((FFModuleFormatArg[]) {
139        { "Name / description", "name" },
140        { "Firmware file path", "firmware-path" },
141        { "Firmware file name", "firmware-name" },
142        { "Is secure boot enabled", "secure-boot" },
143        { "Boot order", "order" },
144    })),
145    .defaultOrder = 6,
146};