1 /* SPDX-License-Identifier: LGPL-2.1-or-later */
2 
3 #pragma once
4 
5 #include <errno.h>
6 #include <inttypes.h>
7 #include <stdbool.h>
8 #include <sys/types.h>
9 
10 #include "json.h"
11 #include "set.h"
12 #include "string-util.h"
13 
14 typedef enum BootEntryType {
15         BOOT_ENTRY_CONF,        /* Boot Loader Specification Type #1 entries: *.conf files */
16         BOOT_ENTRY_UNIFIED,     /* Boot Loader Specification Type #2 entries: *.efi files */
17         BOOT_ENTRY_LOADER,      /* Additional entries augmented from LoaderEntries EFI variable (regular entries) */
18         BOOT_ENTRY_LOADER_AUTO, /* Additional entries augmented from LoaderEntries EFI variable (special "automatic" entries) */
19         _BOOT_ENTRY_TYPE_MAX,
20         _BOOT_ENTRY_TYPE_INVALID = -EINVAL,
21 } BootEntryType;
22 
23 typedef struct BootEntry {
24         BootEntryType type;
25         bool reported_by_loader;
26         char *id;       /* This is the file basename (including extension!) */
27         char *id_old;   /* Old-style ID, for deduplication purposes. */
28         char *path;     /* This is the full path to the drop-in file */
29         char *root;     /* The root path in which the drop-in was found, i.e. to which 'kernel', 'efi' and 'initrd' are relative */
30         char *title;
31         char *show_title;
32         char *sort_key;
33         char *version;
34         char *machine_id;
35         char *architecture;
36         char **options;
37         char *kernel;        /* linux is #defined to 1, yikes! */
38         char *efi;
39         char **initrd;
40         char *device_tree;
41         char **device_tree_overlay;
42 } BootEntry;
43 
44 typedef struct BootConfig {
45         char *default_pattern;
46         char *timeout;
47         char *editor;
48         char *auto_entries;
49         char *auto_firmware;
50         char *console_mode;
51         char *random_seed_mode;
52         char *beep;
53 
54         char *entry_oneshot;
55         char *entry_default;
56         char *entry_selected;
57 
58         BootEntry *entries;
59         size_t n_entries;
60 
61         ssize_t default_entry;
62         ssize_t selected_entry;
63 
64         Set *inodes_seen;
65 } BootConfig;
66 
67 #define BOOT_CONFIG_NULL              \
68         {                             \
69                 .default_entry = -1,  \
70                 .selected_entry = -1, \
71         }
72 
73 const char* boot_entry_type_to_string(BootEntryType);
74 
75 BootEntry* boot_config_find_entry(BootConfig *config, const char *id);
76 
boot_config_default_entry(const BootConfig * config)77 static inline const BootEntry* boot_config_default_entry(const BootConfig *config) {
78         assert(config);
79 
80         if (config->default_entry < 0)
81                 return NULL;
82 
83         assert((size_t) config->default_entry < config->n_entries);
84         return config->entries + config->default_entry;
85 }
86 
87 void boot_config_free(BootConfig *config);
88 
89 int boot_loader_read_conf(BootConfig *config, FILE *file, const char *path);
90 
91 int boot_config_load_type1(
92                 BootConfig *config,
93                 FILE *f,
94                 const char *root,
95                 const char *dir,
96                 const char *id);
97 
98 int boot_config_finalize(BootConfig *config);
99 int boot_config_load(BootConfig *config, const char *esp_path, const char *xbootldr_path);
100 int boot_config_load_auto(BootConfig *config, const char *override_esp_path, const char *override_xbootldr_path);
101 int boot_config_augment_from_loader(BootConfig *config, char **list, bool only_auto);
102 
103 int boot_config_select_special_entries(BootConfig *config);
104 
boot_entry_title(const BootEntry * entry)105 static inline const char* boot_entry_title(const BootEntry *entry) {
106         assert(entry);
107 
108         return ASSERT_PTR(entry->show_title ?: entry->title ?: entry->id);
109 }
110 
111 int show_boot_entry(
112                 const BootEntry *e,
113                 bool show_as_default,
114                 bool show_as_selected,
115                 bool show_reported);
116 int show_boot_entries(
117                 const BootConfig *config,
118                 JsonFormatFlags json_format);
119