| 1 |
/* SPDX-License-Identifier: LGPL-2.1-or-later */ |
| 2 |
|
| 3 |
#include "alloc-util.h" |
| 4 |
#include "boot-entry.h" |
| 5 |
#include "bootspec-util.h" |
| 6 |
#include "devnum-util.h" |
| 7 |
#include "efi-loader.h" |
| 8 |
#include "errno-util.h" |
| 9 |
#include "log.h" |
| 10 |
#include "parse-util.h" |
| 11 |
#include "path-util.h" |
| 12 |
#include "stdio-util.h" |
| 13 |
#include "string-util.h" |
| 14 |
#include "strv.h" |
| 15 |
#include "utf8.h" |
| 16 |
|
| 17 |
int boot_config_load_and_select( |
| 18 |
BootConfig *config, |
| 19 |
const char *root, |
| 20 |
const char *esp_path, |
| 21 |
dev_t esp_devid, |
| 22 |
const char *xbootldr_path, |
| 23 |
dev_t xbootldr_devid) { |
| 24 |
|
| 25 |
int r; |
| 26 |
|
| 27 |
/* If XBOOTLDR and ESP actually refer to the same block device, suppress XBOOTLDR, since it would |
| 28 |
* find the same entries twice. */ |
| 29 |
bool same = esp_path && xbootldr_path && devnum_set_and_equal(esp_devid, xbootldr_devid); |
| 30 |
|
| 31 |
r = boot_config_load(config, esp_path, same ? NULL : xbootldr_path); |
| 32 |
if (r < 0) |
| 33 |
return r; |
| 34 |
|
| 35 |
if (!root) { |
| 36 |
_cleanup_strv_free_ char **efi_entries = NULL; |
| 37 |
|
| 38 |
r = efi_loader_get_entries(&efi_entries); |
| 39 |
if (r == -ENOENT || ERRNO_IS_NEG_NOT_SUPPORTED(r)) |
| 40 |
log_debug_errno(r, "Boot loader reported no entries."); |
| 41 |
else if (r < 0) |
| 42 |
log_warning_errno(r, "Failed to determine entries reported by boot loader, ignoring: %m"); |
| 43 |
else |
| 44 |
(void) boot_config_augment_from_loader(config, efi_entries, /* auto_only= */ false); |
| 45 |
} |
| 46 |
|
| 47 |
return boot_config_select_special_entries(config, /* skip_efivars= */ !!root); |
| 48 |
} |
| 49 |
|
| 50 |
int boot_entry_make_commit_filename( |
| 51 |
const char *entry_token, |
| 52 |
uint64_t entry_commit, |
| 53 |
const char *version, |
| 54 |
unsigned profile_nr, |
| 55 |
unsigned tries_left, |
| 56 |
char **ret) { |
| 57 |
|
| 58 |
assert(entry_token); |
| 59 |
assert(ret); |
| 60 |
|
| 61 |
/* Generate a new entry filename from the entry token, the commit number, and (optionally) the |
| 62 |
* image/OS version, (if non-zero) the profile number, and (unless UINT_MAX) the number of tries |
| 63 |
* left. */ |
| 64 |
|
| 65 |
if (!boot_entry_token_valid(entry_token)) |
| 66 |
return -EINVAL; |
| 67 |
if (!entry_commit_valid(entry_commit)) |
| 68 |
return -EINVAL; |
| 69 |
|
| 70 |
_cleanup_free_ char *filename = asprintf_safe("%s-commit_%" PRIu64, entry_token, entry_commit); |
| 71 |
if (!filename) |
| 72 |
return -ENOMEM; |
| 73 |
if (version && !strextend(&filename, ".", version)) |
| 74 |
return -ENOMEM; |
| 75 |
if (profile_nr > 0 && strextendf(&filename, "@%u", profile_nr) < 0) |
| 76 |
return -ENOMEM; |
| 77 |
if (tries_left != UINT_MAX && strextendf(&filename, "+%u", tries_left) < 0) |
| 78 |
return -ENOMEM; |
| 79 |
if (!strextend(&filename, ".conf")) |
| 80 |
return -ENOMEM; |
| 81 |
|
| 82 |
if (!filename_is_valid(filename) || string_has_cc(filename, /* ok= */ NULL) || !utf8_is_valid(filename)) |
| 83 |
return -EINVAL; |
| 84 |
|
| 85 |
*ret = TAKE_PTR(filename); |
| 86 |
return 0; |
| 87 |
} |
| 88 |
|
| 89 |
int boot_entry_parse_commit_filename( |
| 90 |
const char *filename, |
| 91 |
char **ret_entry_token, |
| 92 |
uint64_t *ret_entry_commit) { |
| 93 |
|
| 94 |
int r; |
| 95 |
|
| 96 |
assert(filename); |
| 97 |
|
| 98 |
if (!filename_is_valid(filename)) |
| 99 |
return -EINVAL; |
| 100 |
|
| 101 |
_cleanup_free_ char *stripped = NULL; |
| 102 |
r = boot_filename_extract_tries(filename, &stripped, /* ret_tries_left= */ NULL, /* ret_tries_done= */ NULL); |
| 103 |
if (r < 0) |
| 104 |
return r; |
| 105 |
|
| 106 |
const char *a = strrstr_no_case(stripped, "-commit_"); |
| 107 |
if (!a) |
| 108 |
return -EBADMSG; |
| 109 |
|
| 110 |
const char *c = endswith_no_case(stripped, ".conf"); |
| 111 |
if (!c) |
| 112 |
return -EBADMSG; |
| 113 |
|
| 114 |
assert(a < c); |
| 115 |
|
| 116 |
_cleanup_free_ char *entry_token = strndup(stripped, a - stripped); |
| 117 |
if (!entry_token) |
| 118 |
return -ENOMEM; |
| 119 |
|
| 120 |
if (!boot_entry_token_valid(entry_token)) |
| 121 |
return -EBADMSG; |
| 122 |
|
| 123 |
const char *b = a + STRLEN("-commit_"); |
| 124 |
size_t n = strspn(b, DIGITS); |
| 125 |
if (n <= 0 || !IN_SET(b[n], '+', '.', '@')) |
| 126 |
return -EBADMSG; |
| 127 |
|
| 128 |
_cleanup_free_ char *entry_commit_string = strndup(b, n); |
| 129 |
if (!entry_commit_string) |
| 130 |
return -ENOMEM; |
| 131 |
|
| 132 |
uint64_t entry_commit; |
| 133 |
r = safe_atou64_full(entry_commit_string, 10, &entry_commit); |
| 134 |
if (r < 0) |
| 135 |
return r; |
| 136 |
if (!entry_commit_valid(entry_commit)) |
| 137 |
return -EBADMSG; |
| 138 |
|
| 139 |
if (ret_entry_token) |
| 140 |
*ret_entry_token = TAKE_PTR(entry_token); |
| 141 |
if (ret_entry_commit) |
| 142 |
*ret_entry_commit = entry_commit; |
| 143 |
|
| 144 |
return 0; |
| 145 |
} |
| 146 |
|
| 147 |
int boot_entry_parse_commit( |
| 148 |
BootEntry *entry, |
| 149 |
char **ret_entry_token, |
| 150 |
uint64_t *ret_entry_commit) { |
| 151 |
|
| 152 |
int r; |
| 153 |
|
| 154 |
assert(entry); |
| 155 |
|
| 156 |
if (entry->type != BOOT_ENTRY_TYPE1) |
| 157 |
return -EADDRNOTAVAIL; |
| 158 |
|
| 159 |
_cleanup_free_ char *fn = NULL; |
| 160 |
r = path_extract_filename(entry->path, &fn); |
| 161 |
if (r < 0) |
| 162 |
return r; |
| 163 |
|
| 164 |
return boot_entry_parse_commit_filename(fn, ret_entry_token, ret_entry_commit); |
| 165 |
} |
| 166 |
|
| 167 |
int boot_config_find_oldest_commit( |
| 168 |
BootConfig *config, |
| 169 |
const char *entry_token, |
| 170 |
char ***ret_ids) { |
| 171 |
|
| 172 |
int r; |
| 173 |
|
| 174 |
assert(config); |
| 175 |
assert(entry_token); |
| 176 |
assert(ret_ids); |
| 177 |
|
| 178 |
uint64_t commit_oldest = UINT64_MAX, commit_2nd_oldest = UINT64_MAX, commit_blocked = UINT64_MAX; |
| 179 |
|
| 180 |
/* First, determine which commit is the oldest (that isn't the current one), and hence the candidate |
| 181 |
* to be removed */ |
| 182 |
FOREACH_ARRAY(b, config->entries, config->n_entries) { |
| 183 |
_cleanup_free_ char *et = NULL; |
| 184 |
uint64_t ec; |
| 185 |
|
| 186 |
r = boot_entry_parse_commit(b, &et, &ec); |
| 187 |
if (r == -EADDRNOTAVAIL) |
| 188 |
continue; |
| 189 |
if (r < 0) { |
| 190 |
log_debug_errno(r, "Failed to parse entry filename of '%s', ignoring: %m", strna(b->id)); |
| 191 |
continue; |
| 192 |
} |
| 193 |
|
| 194 |
if (!streq(et, entry_token)) /* Not ours? */ |
| 195 |
continue; |
| 196 |
|
| 197 |
if (ec < commit_oldest) { |
| 198 |
commit_2nd_oldest = commit_oldest; |
| 199 |
commit_oldest = ec; |
| 200 |
} else if (ec > commit_oldest && ec < commit_2nd_oldest) |
| 201 |
commit_2nd_oldest = ec; |
| 202 |
|
| 203 |
if (boot_config_selected_entry(config) == b) { |
| 204 |
assert(commit_blocked == UINT64_MAX); |
| 205 |
commit_blocked = ec; |
| 206 |
} |
| 207 |
} |
| 208 |
|
| 209 |
uint64_t commit_picked; |
| 210 |
if (commit_oldest == UINT64_MAX) |
| 211 |
return log_debug_errno(SYNTHETIC_ERRNO(ENXIO), "No matching entry found while determining oldest entry."); |
| 212 |
if (commit_oldest != commit_blocked) |
| 213 |
commit_picked = commit_oldest; |
| 214 |
else { |
| 215 |
if (commit_2nd_oldest == UINT64_MAX) |
| 216 |
return log_debug_errno(SYNTHETIC_ERRNO(EBUSY), "Only matching entry found while determining oldest entry is current one, skipping it."); |
| 217 |
|
| 218 |
assert(commit_2nd_oldest != commit_blocked); |
| 219 |
commit_picked = commit_2nd_oldest; |
| 220 |
} |
| 221 |
|
| 222 |
log_debug("Determined commit %" PRIu64 " to be oldest.", commit_picked); |
| 223 |
|
| 224 |
/* Second loop: actually remove all entries matching this commit (which can be multiple, since UKIs |
| 225 |
* have profiles) */ |
| 226 |
_cleanup_(strv_freep) char **l = NULL; |
| 227 |
FOREACH_ARRAY(b, config->entries, config->n_entries) { |
| 228 |
_cleanup_free_ char *et = NULL; |
| 229 |
uint64_t ec; |
| 230 |
|
| 231 |
r = boot_entry_parse_commit(b, &et, &ec); |
| 232 |
if (r == -EADDRNOTAVAIL) |
| 233 |
continue; |
| 234 |
if (r < 0) { |
| 235 |
log_debug_errno(r, "Failed to parse entry filename of '%s', ignoring: %m", strna(b->id)); |
| 236 |
continue; |
| 237 |
} |
| 238 |
|
| 239 |
if (!streq(et, entry_token)) /* Not ours? */ |
| 240 |
continue; |
| 241 |
|
| 242 |
if (ec != commit_picked) |
| 243 |
continue; |
| 244 |
|
| 245 |
r = strv_extend(&l, b->id); |
| 246 |
if (r < 0) |
| 247 |
return r; |
| 248 |
} |
| 249 |
|
| 250 |
/* The list cannot be empty, the first loop above and the 2nd loop must have found the same matching |
| 251 |
* entries, and if the first loop didn't find any we'd not come this far. */ |
| 252 |
assert(!strv_isempty(l)); |
| 253 |
*ret_ids = TAKE_PTR(l); |
| 254 |
return 0; |
| 255 |
} |