| 1 |
/* SPDX-License-Identifier: LGPL-2.1-or-later */ |
| 2 |
|
| 3 |
#pragma once |
| 4 |
|
| 5 |
#include "shared-forward.h" |
| 6 |
|
| 7 |
#include "../fundamental/bootspec.h" /* IWYU pragma: export */ |
| 8 |
|
| 9 |
typedef enum BootEntryType { |
| 10 |
BOOT_ENTRY_TYPE1, /* Boot Loader Specification Type #1 entries: *.conf files */ |
| 11 |
BOOT_ENTRY_TYPE2, /* Boot Loader Specification Type #2 entries: *.efi files (UKIs) */ |
| 12 |
BOOT_ENTRY_LOADER, /* Additional entries augmented from LoaderEntries EFI variable (regular entries) */ |
| 13 |
BOOT_ENTRY_AUTO, /* Additional entries augmented from LoaderEntries EFI variable (special "automatic" entries) */ |
| 14 |
_BOOT_ENTRY_TYPE_MAX, |
| 15 |
_BOOT_ENTRY_TYPE_INVALID = -EINVAL, |
| 16 |
} BootEntryType; |
| 17 |
|
| 18 |
typedef enum BootEntrySource { |
| 19 |
BOOT_ENTRY_ESP, |
| 20 |
BOOT_ENTRY_XBOOTLDR, |
| 21 |
_BOOT_ENTRY_SOURCE_MAX, |
| 22 |
_BOOT_ENTRY_SOURCE_INVALID = -EINVAL, |
| 23 |
} BootEntrySource; |
| 24 |
|
| 25 |
typedef enum BootEntryExtraType { |
| 26 |
BOOT_ENTRY_ADDON, |
| 27 |
BOOT_ENTRY_CONFEXT, |
| 28 |
BOOT_ENTRY_SYSEXT, |
| 29 |
BOOT_ENTRY_CREDENTIAL, |
| 30 |
_BOOT_ENTRY_EXTRA_TYPE_MAX, |
| 31 |
_BOOT_ENTRY_EXTRA_TYPE_INVALID = -EINVAL, |
| 32 |
} BootEntryExtraType; |
| 33 |
|
| 34 |
typedef struct BootEntryExtra { |
| 35 |
BootEntryExtraType type; |
| 36 |
char *location; |
| 37 |
char *cmdline; /* only for BOOT_ENTRY_ADDON */ |
| 38 |
} BootEntryExtra; |
| 39 |
|
| 40 |
typedef struct BootEntryExtras { |
| 41 |
BootEntryExtra *items; |
| 42 |
size_t n_items; |
| 43 |
} BootEntryExtras; |
| 44 |
|
| 45 |
typedef struct BootEntry { |
| 46 |
BootEntryType type; |
| 47 |
BootEntrySource source; |
| 48 |
bool reported_by_loader; |
| 49 |
char *id; /* This is the file basename (including extension, but with tries counters stripped) */ |
| 50 |
char *id_old; /* Old-style ID, for deduplication purposes (for type1: same as the regular id, but with extension stripped too). */ |
| 51 |
char *id_without_profile; /* ID without profile suffixed */ |
| 52 |
char *path; /* This is the full path to the drop-in file (i.e. prefixed with 'root' field below) */ |
| 53 |
char *root; /* The root path in which the drop-in was found, i.e. to which 'kernel', 'efi' and 'initrd' are relative */ |
| 54 |
char *title; |
| 55 |
char *show_title; |
| 56 |
char *sort_key; |
| 57 |
char *version; |
| 58 |
char *machine_id; |
| 59 |
char *architecture; |
| 60 |
char **options; |
| 61 |
BootEntryExtras local_extras; |
| 62 |
const BootEntryExtras *global_extras; /* Backpointer into the BootConfig; we don't own this here */ |
| 63 |
char *kernel; /* linux is #defined to 1, yikes! */ |
| 64 |
char *efi; |
| 65 |
char *uki; |
| 66 |
char *uki_url; |
| 67 |
char **initrd; |
| 68 |
char *device_tree; |
| 69 |
char **device_tree_overlay; |
| 70 |
unsigned tries_left; |
| 71 |
unsigned tries_done; |
| 72 |
unsigned profile; |
| 73 |
} BootEntry; |
| 74 |
|
| 75 |
#define BOOT_ENTRY_INIT(t, s) \ |
| 76 |
{ \ |
| 77 |
.type = (t), \ |
| 78 |
.source = (s), \ |
| 79 |
.tries_left = UINT_MAX, \ |
| 80 |
.tries_done = UINT_MAX, \ |
| 81 |
.profile = UINT_MAX, \ |
| 82 |
} |
| 83 |
|
| 84 |
typedef struct BootConfig { |
| 85 |
int loader_conf_status; /* 0 → before loading, 1 → loaded, negative → error. */ |
| 86 |
|
| 87 |
char *default_pattern; |
| 88 |
char *preferred_pattern; |
| 89 |
|
| 90 |
char *entry_oneshot; |
| 91 |
char *entry_preferred; |
| 92 |
char *entry_default; |
| 93 |
char *entry_selected; |
| 94 |
char *entry_sysfail; |
| 95 |
|
| 96 |
BootEntry *entries; |
| 97 |
size_t n_entries; |
| 98 |
|
| 99 |
BootEntryExtras global_extras[_BOOT_ENTRY_SOURCE_MAX]; |
| 100 |
|
| 101 |
ssize_t default_entry; |
| 102 |
ssize_t selected_entry; |
| 103 |
|
| 104 |
Set *inodes_seen; |
| 105 |
} BootConfig; |
| 106 |
|
| 107 |
#define BOOT_CONFIG_NULL \ |
| 108 |
{ \ |
| 109 |
.default_entry = -1, \ |
| 110 |
.selected_entry = -1, \ |
| 111 |
} |
| 112 |
|
| 113 |
DECLARE_STRING_TABLE_LOOKUP_TO_STRING(boot_entry_type_description, BootEntryType); |
| 114 |
DECLARE_STRING_TABLE_LOOKUP(boot_entry_type, BootEntryType); |
| 115 |
|
| 116 |
DECLARE_STRING_TABLE_LOOKUP_TO_STRING(boot_entry_source_description, BootEntrySource); |
| 117 |
DECLARE_STRING_TABLE_LOOKUP_TO_STRING(boot_entry_source, BootEntrySource); |
| 118 |
|
| 119 |
BootEntry* boot_config_find_entry(BootConfig *config, const char *id); |
| 120 |
|
| 121 |
static inline const BootEntry* boot_config_default_entry(const BootConfig *config) { |
| 122 |
assert(config); |
| 123 |
|
| 124 |
if (config->default_entry < 0) |
| 125 |
return NULL; |
| 126 |
|
| 127 |
assert((size_t) config->default_entry < config->n_entries); |
| 128 |
return config->entries + config->default_entry; |
| 129 |
} |
| 130 |
|
| 131 |
static inline const BootEntry* boot_config_selected_entry(const BootConfig *config) { |
| 132 |
assert(config); |
| 133 |
|
| 134 |
if (config->selected_entry < 0) |
| 135 |
return NULL; |
| 136 |
|
| 137 |
assert((size_t) config->selected_entry < config->n_entries); |
| 138 |
return config->entries + config->selected_entry; |
| 139 |
} |
| 140 |
|
| 141 |
void boot_config_free(BootConfig *config); |
| 142 |
|
| 143 |
int boot_loader_read_conf(BootConfig *config, FILE *file, const char *path); |
| 144 |
|
| 145 |
int boot_config_load_type1( |
| 146 |
BootConfig *config, |
| 147 |
FILE *f, |
| 148 |
const char *root, |
| 149 |
BootEntrySource source, |
| 150 |
const char *dir, |
| 151 |
const char *filename); |
| 152 |
|
| 153 |
int boot_config_finalize(BootConfig *config); |
| 154 |
int boot_config_load(BootConfig *config, const char *esp_path, const char *xbootldr_path); |
| 155 |
int boot_config_load_auto(BootConfig *config, const char *override_esp_path, const char *override_xbootldr_path); |
| 156 |
int boot_config_augment_from_loader(BootConfig *config, char **found_by_loader, bool auto_only); |
| 157 |
|
| 158 |
int boot_config_select_special_entries(BootConfig *config, bool skip_efivars); |
| 159 |
|
| 160 |
static inline const char* boot_entry_title(const BootEntry *entry) { |
| 161 |
assert(entry); |
| 162 |
return ASSERT_PTR(entry->show_title ?: entry->title ?: entry->id); |
| 163 |
} |
| 164 |
|
| 165 |
int show_boot_entry( |
| 166 |
const BootEntry *e, |
| 167 |
bool show_as_default, |
| 168 |
bool show_as_selected, |
| 169 |
bool show_reported); |
| 170 |
int show_boot_entries( |
| 171 |
const BootConfig *config, |
| 172 |
sd_json_format_flags_t json_format); |
| 173 |
|
| 174 |
int boot_filename_extract_tries(const char *fname, char **ret_stripped, unsigned *ret_tries_left, unsigned *ret_tries_done); |
| 175 |
|
| 176 |
int boot_entry_to_json(const BootConfig *c, size_t i, sd_json_variant **ret); |
| 177 |
|
| 178 |
int pe_find_uki_sections(int fd, const char *path, unsigned profile, char **ret_osrelease, char **ret_profile, char **ret_cmdline); |
| 179 |
|
| 180 |
int bootspec_extract_osrelease(const char *text, char **ret_good_name, char **ret_good_version, char **ret_good_sort_key, char **ret_os_id, char **ret_os_version_id, char **ret_image_id, char **ret_image_version); |