]>
git.ipfire.org Git - thirdparty/systemd.git/blob - src/shared/bootspec.c
2 This file is part of systemd.
4 Copyright 2017 Zbigniew Jędrzejewski-Szmek
6 systemd is free software; you can redistribute it and/or modify it
7 under the terms of the GNU Lesser General Public License as published by
8 the Free Software Foundation; either version 2.1 of the License, or
9 (at your option) any later version.
11 systemd is distributed in the hope that it will be useful, but
12 WITHOUT ANY WARRANTY; without even the implied warranty of
13 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 Lesser General Public License for more details.
16 You should have received a copy of the GNU Lesser General Public License
17 along with systemd; If not, see <http://www.gnu.org/licenses/>.
22 #include "alloc-util.h"
24 #include "conf-files.h"
29 #include "string-util.h"
32 void boot_entry_free(BootEntry
*entry
) {
33 free(entry
->filename
);
36 free(entry
->show_title
);
38 free(entry
->machine_id
);
39 free(entry
->architecture
);
40 strv_free(entry
->options
);
43 strv_free(entry
->initrd
);
44 free(entry
->device_tree
);
47 int boot_entry_load(const char *path
, BootEntry
*entry
) {
48 _cleanup_fclose_
FILE *f
= NULL
;
50 _cleanup_(boot_entry_free
) BootEntry tmp
= {};
53 f
= fopen(path
, "re");
55 return log_error_errno(errno
, "Failed to open \"%s\": %m", path
);
57 tmp
.filename
= strdup(basename(path
));
62 _cleanup_free_
char *buf
= NULL
;
65 r
= read_line(f
, LONG_LINE_MAX
, &buf
);
69 return log_error_errno(r
, "%s:%u: Line too long", path
, line
);
71 return log_error_errno(r
, "%s:%u: Error while reading: %m", path
, line
);
75 if (IN_SET(*strstrip(buf
), '#', '\0'))
80 log_warning("%s:%u: Bad syntax", path
, line
);
86 if (streq(buf
, "title"))
87 r
= free_and_strdup(&tmp
.title
, p
);
88 else if (streq(buf
, "version"))
89 r
= free_and_strdup(&tmp
.version
, p
);
90 else if (streq(buf
, "machine-id"))
91 r
= free_and_strdup(&tmp
.machine_id
, p
);
92 else if (streq(buf
, "architecture"))
93 r
= free_and_strdup(&tmp
.architecture
, p
);
94 else if (streq(buf
, "options"))
95 r
= strv_extend(&tmp
.options
, p
);
96 else if (streq(buf
, "linux"))
97 r
= free_and_strdup(&tmp
.kernel
, p
);
98 else if (streq(buf
, "efi"))
99 r
= free_and_strdup(&tmp
.efi
, p
);
100 else if (streq(buf
, "initrd"))
101 r
= strv_extend(&tmp
.initrd
, p
);
102 else if (streq(buf
, "devicetree"))
103 r
= free_and_strdup(&tmp
.device_tree
, p
);
105 log_notice("%s:%u: Unknown line \"%s\"", path
, line
, buf
);
109 return log_error_errno(r
, "%s:%u: Error while reading: %m", path
, line
);
113 tmp
= (BootEntry
) {};
117 void boot_config_free(BootConfig
*config
) {
120 free(config
->default_pattern
);
121 free(config
->timeout
);
122 free(config
->editor
);
124 free(config
->entry_oneshot
);
125 free(config
->entry_default
);
127 for (i
= 0; i
< config
->n_entries
; i
++)
128 boot_entry_free(config
->entries
+ i
);
129 free(config
->entries
);
132 int boot_loader_read_conf(const char *path
, BootConfig
*config
) {
133 _cleanup_fclose_
FILE *f
= NULL
;
137 f
= fopen(path
, "re");
139 return log_error_errno(errno
, "Failed to open \"%s\": %m", path
);
142 _cleanup_free_
char *buf
= NULL
;
145 r
= read_line(f
, LONG_LINE_MAX
, &buf
);
149 return log_error_errno(r
, "%s:%u: Line too long", path
, line
);
151 return log_error_errno(r
, "%s:%u: Error while reading: %m", path
, line
);
155 if (IN_SET(*strstrip(buf
), '#', '\0'))
158 p
= strchr(buf
, ' ');
160 log_warning("%s:%u: Bad syntax", path
, line
);
166 if (streq(buf
, "default"))
167 r
= free_and_strdup(&config
->default_pattern
, p
);
168 else if (streq(buf
, "timeout"))
169 r
= free_and_strdup(&config
->timeout
, p
);
170 else if (streq(buf
, "editor"))
171 r
= free_and_strdup(&config
->editor
, p
);
173 log_notice("%s:%u: Unknown line \"%s\"", path
, line
, buf
);
177 return log_error_errno(r
, "%s:%u: Error while reading: %m", path
, line
);
183 /* This is a direct translation of str_verscmp from boot.c */
184 static bool is_digit(int c
) {
185 return c
>= '0' && c
<= '9';
188 static int c_order(int c
) {
193 else if ((c
>= 'a') && (c
<= 'z'))
199 static int str_verscmp(const char *s1
, const char *s2
) {
200 const char *os1
= s1
;
201 const char *os2
= s2
;
206 while ((*s1
&& !is_digit(*s1
)) || (*s2
&& !is_digit(*s2
))) {
209 order
= c_order(*s1
) - c_order(*s2
);
222 while (is_digit(*s1
) && is_digit(*s2
)) {
238 return strcmp(os1
, os2
);
241 static int boot_entry_compare(const void *a
, const void *b
) {
242 const BootEntry
*aa
= a
;
243 const BootEntry
*bb
= b
;
245 return str_verscmp(aa
->filename
, bb
->filename
);
248 int boot_entries_find(const char *dir
, BootEntry
**entries
, size_t *n_entries
) {
249 _cleanup_strv_free_
char **files
= NULL
;
253 BootEntry
*array
= NULL
;
254 size_t n_allocated
= 0, n
= 0;
256 r
= conf_files_list(&files
, ".conf", NULL
, 0, dir
, NULL
);
258 return log_error_errno(r
, "Failed to list files in \"%s\": %m", dir
);
260 STRV_FOREACH(f
, files
) {
261 if (!GREEDY_REALLOC0(array
, n_allocated
, n
+ 1))
264 r
= boot_entry_load(*f
, array
+ n
);
271 qsort_safe(array
, n
, sizeof(BootEntry
), boot_entry_compare
);
278 static bool find_nonunique(BootEntry
*entries
, size_t n_entries
, bool *arr
) {
280 bool non_unique
= false;
282 for (i
= 0; i
< n_entries
; i
++)
285 for (i
= 0; i
< n_entries
; i
++)
286 for (j
= 0; j
< n_entries
; j
++)
287 if (i
!= j
&& streq(boot_entry_title(entries
+ i
),
288 boot_entry_title(entries
+ j
)))
289 non_unique
= arr
[i
] = arr
[j
] = true;
294 static int boot_entries_uniquify(BootEntry
*entries
, size_t n_entries
) {
300 /* Find _all_ non-unique titles */
301 if (!find_nonunique(entries
, n_entries
, arr
))
304 /* Add version to non-unique titles */
305 for (i
= 0; i
< n_entries
; i
++)
306 if (arr
[i
] && entries
[i
].version
) {
307 r
= asprintf(&s
, "%s (%s)", boot_entry_title(entries
+ i
), entries
[i
].version
);
311 free_and_replace(entries
[i
].show_title
, s
);
314 if (!find_nonunique(entries
, n_entries
, arr
))
317 /* Add machine-id to non-unique titles */
318 for (i
= 0; i
< n_entries
; i
++)
319 if (arr
[i
] && entries
[i
].machine_id
) {
320 r
= asprintf(&s
, "%s (%s)", boot_entry_title(entries
+ i
), entries
[i
].machine_id
);
324 free_and_replace(entries
[i
].show_title
, s
);
327 if (!find_nonunique(entries
, n_entries
, arr
))
330 /* Add file name to non-unique titles */
331 for (i
= 0; i
< n_entries
; i
++)
333 r
= asprintf(&s
, "%s (%s)", boot_entry_title(entries
+ i
), entries
[i
].filename
);
337 free_and_replace(entries
[i
].show_title
, s
);
343 int boot_entries_select_default(const BootConfig
*config
) {
346 if (config
->entry_oneshot
)
347 for (i
= config
->n_entries
- 1; i
>= 0; i
--)
348 if (streq(config
->entry_oneshot
, config
->entries
[i
].filename
)) {
349 log_debug("Found default: filename \"%s\" is matched by LoaderEntryOneShot",
350 config
->entries
[i
].filename
);
354 if (config
->entry_default
)
355 for (i
= config
->n_entries
- 1; i
>= 0; i
--)
356 if (streq(config
->entry_default
, config
->entries
[i
].filename
)) {
357 log_debug("Found default: filename \"%s\" is matched by LoaderEntryDefault",
358 config
->entries
[i
].filename
);
362 if (config
->default_pattern
)
363 for (i
= config
->n_entries
- 1; i
>= 0; i
--)
364 if (fnmatch(config
->default_pattern
, config
->entries
[i
].filename
, FNM_CASEFOLD
) == 0) {
365 log_debug("Found default: filename \"%s\" is matched by pattern \"%s\"",
366 config
->entries
[i
].filename
, config
->default_pattern
);
370 if (config
->n_entries
> 0)
371 log_debug("Found default: last entry \"%s\"", config
->entries
[i
].filename
);
373 log_debug("Found no default boot entry :(");
374 return config
->n_entries
- 1; /* -1 means "no default" */
377 int boot_entries_load_config(const char *esp_path
, BootConfig
*config
) {
381 p
= strjoina(esp_path
, "/loader/loader.conf");
382 r
= boot_loader_read_conf(p
, config
);
384 return log_error_errno(r
, "Failed to read boot config from \"%s\": %m", p
);
386 p
= strjoina(esp_path
, "/loader/entries");
387 r
= boot_entries_find(p
, &config
->entries
, &config
->n_entries
);
389 return log_error_errno(r
, "Failed to read boot entries from \"%s\": %m", p
);
391 r
= boot_entries_uniquify(config
->entries
, config
->n_entries
);
393 return log_error_errno(r
, "Failed to uniquify boot entries: %m");
395 r
= efi_get_variable_string(EFI_VENDOR_LOADER
, "LoaderEntryOneShot", &config
->entry_oneshot
);
396 if (r
< 0 && r
!= -ENOENT
)
397 return log_error_errno(r
, "Failed to read EFI var \"LoaderEntryOneShot\": %m");
399 r
= efi_get_variable_string(EFI_VENDOR_LOADER
, "LoaderEntryDefault", &config
->entry_default
);
400 if (r
< 0 && r
!= -ENOENT
)
401 return log_error_errno(r
, "Failed to read EFI var \"LoaderEntryDefault\": %m");
403 config
->default_entry
= boot_entries_select_default(config
);