HALF ECHO LEARN / REFERENCE COLLECTION
v262.2src/shared/bootspec.c
Git blob: 4615a8755ea1a36c83f44848960af185f13bd0ae
Lines 2001–2354 of 2,354
if (indent_embedded_newlines(options, &t) < 0)
return log_oom();
printf(" options: %s\n", t);
t2 = strdup(options);
if (!t2)
return log_oom();
}
FOREACH_ARRAY(extra, e->global_extras->items, e->global_extras->n_items) {
print_extra(e, extra, "extra", status);
if (extra->cmdline)
if (!strextend(&t2, " ", extra->cmdline))
return log_oom();
}
FOREACH_ARRAY(extra, e->local_extras.items, e->local_extras.n_items) {
print_extra(e, extra, "extra", status);
if (extra->cmdline)
if (!strextend(&t2, " ", extra->cmdline))
return log_oom();
}
/* Don't print the combined cmdline if it's same as options. */
if (streq_ptr(t2, options))
return 0;
if (indent_embedded_newlines(t2, &combined_cmdline) < 0)
return log_oom();
if (combined_cmdline)
printf(" cmdline: %s\n", combined_cmdline);
return 0;
}
static int json_addon(
const BootEntryExtra *extra,
const char *extra_str,
sd_json_variant **array) {
int r;
assert(extra);
assert(extra_str);
r = sd_json_variant_append_arraybo(
array,
SD_JSON_BUILD_PAIR_STRING(extra_str, extra->location),
JSON_BUILD_PAIR_STRING_NON_EMPTY("options", extra->cmdline));
if (r < 0)
return log_oom();
return 0;
}
static int json_cmdline(
const BootEntry *e,
const char *def_cmdline,
sd_json_variant **v) {
_cleanup_free_ char *combined_cmdline = NULL;
_cleanup_(sd_json_variant_unrefp) sd_json_variant *addons_array = NULL;
int r;
assert(e);
if (def_cmdline) {
combined_cmdline = strdup(def_cmdline);
if (!combined_cmdline)
return log_oom();
}
/* NB: these JSON fields are kinda obsolete, we want the more generic 'extra' ones to be used. */
FOREACH_ARRAY(extra, e->global_extras->items, e->global_extras->n_items) {
if (extra->type != BOOT_ENTRY_ADDON)
continue;
r = json_addon(extra, "globalAddon", &addons_array);
if (r < 0)
return r;
if (extra->cmdline)
if (!strextend(&combined_cmdline, " ", extra->cmdline))
return log_oom();
}
FOREACH_ARRAY(extra, e->local_extras.items, e->local_extras.n_items) {
if (extra->type != BOOT_ENTRY_ADDON)
continue;
r = json_addon(extra, "localAddon", &addons_array);
if (r < 0)
return r;
if (extra->cmdline)
if (!strextend(&combined_cmdline, " ", extra->cmdline))
return log_oom();
}
r = sd_json_variant_merge_objectbo(
v,
SD_JSON_BUILD_PAIR_VARIANT("addons", addons_array),
SD_JSON_BUILD_PAIR_CONDITION(!!combined_cmdline, "cmdline", SD_JSON_BUILD_STRING(combined_cmdline)));
if (r < 0)
return log_oom();
return 0;
}
int show_boot_entry(
const BootEntry *e,
bool show_as_default,
bool show_as_selected,
bool show_reported) {
int status = 0, r = 0;
/* Returns 0 on success, negative on processing error, and positive if something is wrong with the
boot entry itself. */
assert(e);
printf(" type: %s\n",
boot_entry_type_description_to_string(e->type));
printf(" title: %s%s%s",
ansi_highlight(), boot_entry_title(e), ansi_normal());
if (show_as_default)
printf(" %s(default)%s",
ansi_highlight_green(), ansi_normal());
if (show_as_selected)
printf(" %s(selected)%s",
ansi_highlight_magenta(), ansi_normal());
if (show_reported) {
if (e->type == BOOT_ENTRY_LOADER)
printf(" %s(reported/absent)%s",
ansi_highlight_red(), ansi_normal());
else if (!e->reported_by_loader && e->type != BOOT_ENTRY_AUTO)
printf(" %s(not reported/new)%s",
ansi_highlight_green(), ansi_normal());
}
putchar('\n');
if (e->id) {
printf(" id: %s", e->id);
if (e->id_without_profile && !streq_ptr(e->id, e->id_without_profile))
printf(" (without profile: %s)\n", e->id_without_profile);
else
putchar('\n');
}
if (e->path) {
_cleanup_free_ char *text = NULL, *link = NULL;
const char *p = e->root ? path_startswith(e->path, e->root) : NULL;
if (p) {
text = strjoin(ansi_grey(), e->root, "/", ansi_normal(), "/", p);
if (!text)
return log_oom();
}
/* Let's urlify the link to make it easy to view in an editor, but only if it is a text
* file. Unified images are binary ELFs, and EFI variables are not pure text either. */
if (e->type == BOOT_ENTRY_TYPE1)
(void) terminal_urlify_path(e->path, text, &link);
printf(" source: %s (on the %s)\n",
link ?: text ?: e->path,
boot_entry_source_description_to_string(e->source));
}
if (e->tries_left != UINT_MAX) {
printf(" tries: %u left", e->tries_left);
if (e->tries_done != UINT_MAX)
printf("; %u done\n", e->tries_done);
else
putchar('\n');
}
if (e->sort_key)
printf(" sort-key: %s\n", e->sort_key);
if (e->version)
printf(" version: %s\n", e->version);
if (e->uname)
printf(" uname: %s\n", e->uname);
if (e->machine_id)
printf(" machine-id: %s\n", e->machine_id);
if (e->architecture)
printf(" architecture: %s\n", e->architecture);
if (e->kernel)
boot_entry_file_list("linux", e->root, e->kernel, &status);
if (e->efi)
boot_entry_file_list("efi", e->root, e->efi, &status);
if (e->uki)
boot_entry_file_list("uki", e->root, e->uki, &status);
if (e->uki_url)
printf(" uki-url: %s\n", e->uki_url);
if (e->profile != UINT_MAX)
printf(" profile: %u\n", e->profile);
STRV_FOREACH(s, e->initrd)
boot_entry_file_list(s == e->initrd ? "initrd" : NULL,
e->root,
*s,
&status);
r = print_cmdline(e, &status);
if (r < 0)
return r;
if (e->device_tree)
boot_entry_file_list("devicetree", e->root, e->device_tree, &status);
STRV_FOREACH(s, e->device_tree_overlay)
boot_entry_file_list(s == e->device_tree_overlay ? "devicetree-overlay" : NULL,
e->root,
*s,
&status);
return -status;
}
int boot_entry_to_json(const BootConfig *c, size_t i, sd_json_variant **ret) {
_cleanup_(sd_json_variant_unrefp) sd_json_variant *v = NULL;
_cleanup_free_ char *opts = NULL;
const BootEntry *e;
int r;
assert(c);
assert(ret);
if (i >= c->n_entries) {
*ret = NULL;
return 0;
}
e = c->entries + i;
if (!strv_isempty(e->options)) {
opts = strv_join(e->options, " ");
if (!opts)
return log_oom();
}
r = sd_json_variant_merge_objectbo(
&v,
SD_JSON_BUILD_PAIR_STRING("type", boot_entry_type_to_string(e->type)),
SD_JSON_BUILD_PAIR_STRING("source", boot_entry_source_to_string(e->source)),
SD_JSON_BUILD_PAIR_CONDITION(!!e->id, "id", SD_JSON_BUILD_STRING(e->id)),
SD_JSON_BUILD_PAIR_CONDITION(!!e->path, "path", SD_JSON_BUILD_STRING(e->path)),
SD_JSON_BUILD_PAIR_CONDITION(!!e->root, "root", SD_JSON_BUILD_STRING(e->root)),
SD_JSON_BUILD_PAIR_CONDITION(!!e->title, "title", SD_JSON_BUILD_STRING(e->title)),
SD_JSON_BUILD_PAIR_CONDITION(!!boot_entry_title(e), "showTitle", SD_JSON_BUILD_STRING(boot_entry_title(e))),
SD_JSON_BUILD_PAIR_CONDITION(!!e->sort_key, "sortKey", SD_JSON_BUILD_STRING(e->sort_key)),
SD_JSON_BUILD_PAIR_CONDITION(!!e->version, "version", SD_JSON_BUILD_STRING(e->version)),
SD_JSON_BUILD_PAIR_CONDITION(!!e->uname, "uname", SD_JSON_BUILD_STRING(e->uname)),
SD_JSON_BUILD_PAIR_CONDITION(!!e->machine_id, "machineId", SD_JSON_BUILD_STRING(e->machine_id)),
SD_JSON_BUILD_PAIR_CONDITION(!!e->architecture, "architecture", SD_JSON_BUILD_STRING(e->architecture)),
SD_JSON_BUILD_PAIR_CONDITION(!!opts, "options", SD_JSON_BUILD_STRING(opts)),
SD_JSON_BUILD_PAIR_CONDITION(!!e->kernel, "linux", SD_JSON_BUILD_STRING(e->kernel)),
SD_JSON_BUILD_PAIR_CONDITION(!!e->efi, "efi", SD_JSON_BUILD_STRING(e->efi)),
SD_JSON_BUILD_PAIR_CONDITION(!!e->uki, "uki", SD_JSON_BUILD_STRING(e->uki)),
SD_JSON_BUILD_PAIR_CONDITION(!!e->uki_url, "ukiUrl", SD_JSON_BUILD_STRING(e->uki_url)),
SD_JSON_BUILD_PAIR_CONDITION(e->profile != UINT_MAX, "profile", SD_JSON_BUILD_UNSIGNED(e->profile)),
SD_JSON_BUILD_PAIR_CONDITION(!strv_isempty(e->initrd), "initrd", SD_JSON_BUILD_STRV(e->initrd)));
if (r < 0)
return log_oom();
/* Sanitizers (only memory sanitizer?) do not like function call with too many
* arguments and trigger false positive warnings. Let's not add too many json objects
* at once. */
r = sd_json_variant_merge_objectbo(
&v,
SD_JSON_BUILD_PAIR_CONDITION(!!e->device_tree, "devicetree", SD_JSON_BUILD_STRING(e->device_tree)),
SD_JSON_BUILD_PAIR_CONDITION(!strv_isempty(e->device_tree_overlay), "devicetreeOverlay", SD_JSON_BUILD_STRV(e->device_tree_overlay)),
SD_JSON_BUILD_PAIR_BOOLEAN("isReported", e->reported_by_loader),
SD_JSON_BUILD_PAIR_CONDITION(e->tries_left != UINT_MAX, "triesLeft", SD_JSON_BUILD_UNSIGNED(e->tries_left)),
SD_JSON_BUILD_PAIR_CONDITION(e->tries_done != UINT_MAX, "triesDone", SD_JSON_BUILD_UNSIGNED(e->tries_done)),
SD_JSON_BUILD_PAIR_CONDITION(c->default_entry >= 0, "isDefault", SD_JSON_BUILD_BOOLEAN(i == (size_t) c->default_entry)),
SD_JSON_BUILD_PAIR_CONDITION(c->selected_entry >= 0, "isSelected", SD_JSON_BUILD_BOOLEAN(i == (size_t) c->selected_entry)));
if (r < 0)
return log_oom();
r = json_cmdline(e, opts, &v);
if (r < 0)
return log_oom();
_cleanup_(sd_json_variant_unrefp) sd_json_variant *jextras = NULL;
FOREACH_ARRAY(extra, e->global_extras->items, e->global_extras->n_items) {
r = sd_json_variant_append_arrayb(&jextras, SD_JSON_BUILD_STRING(extra->location));
if (r < 0)
return log_oom();
}
FOREACH_ARRAY(extra, e->local_extras.items, e->local_extras.n_items) {
r = sd_json_variant_append_arrayb(&jextras, SD_JSON_BUILD_STRING(extra->location));
if (r < 0)
return log_oom();
}
r = sd_json_variant_merge_objectbo(
&v,
SD_JSON_BUILD_PAIR_CONDITION(!!jextras, "extras", SD_JSON_BUILD_VARIANT(jextras)));
if (r < 0)
return log_oom();
*ret = TAKE_PTR(v);
return 1;
}
int show_boot_entries(const BootConfig *config, sd_json_format_flags_t json_format) {
int r;
assert(config);
if (sd_json_format_enabled(json_format)) {
_cleanup_(sd_json_variant_unrefp) sd_json_variant *array = NULL;
for (size_t i = 0; i < config->n_entries; i++) {
_cleanup_(sd_json_variant_unrefp) sd_json_variant *v = NULL;
r = boot_entry_to_json(config, i, &v);
if (r < 0)
return log_oom();
r = sd_json_variant_append_array(&array, v);
if (r < 0)
return log_oom();
}
return sd_json_variant_dump(array, json_format | SD_JSON_FORMAT_EMPTY_ARRAY, NULL, NULL);
} else
for (size_t n = 0; n < config->n_entries; n++) {
r = show_boot_entry(
config->entries + n,
/* show_as_default= */ n == (size_t) config->default_entry,
/* show_as_selected= */ n == (size_t) config->selected_entry,
/* show_reported= */ true);
if (r < 0)
return r;
if (n+1 < config->n_entries)
putchar('\n');
}
return 0;
}