| 1 |
/* SPDX-License-Identifier: LGPL-2.1-or-later */ |
| 2 |
|
| 3 |
#include <unistd.h> |
| 4 |
|
| 5 |
#include "sd-json.h" |
| 6 |
|
| 7 |
#include "alloc-util.h" |
| 8 |
#include "efivars.h" |
| 9 |
#include "env-util.h" |
| 10 |
#include "factory-reset.h" |
| 11 |
#include "log.h" |
| 12 |
#include "os-util.h" |
| 13 |
#include "proc-cmdline.h" |
| 14 |
#include "string-table.h" |
| 15 |
|
| 16 |
static bool factory_reset_supported(void) { |
| 17 |
int r; |
| 18 |
|
| 19 |
r = secure_getenv_bool("SYSTEMD_FACTORY_RESET_SUPPORTED"); |
| 20 |
if (r >= 0) |
| 21 |
return r; |
| 22 |
if (r != -ENXIO) |
| 23 |
log_debug_errno(r, "Unable to parse $SYSTEMD_FACTORY_RESET_SUPPORTED, ignoring: %m"); |
| 24 |
|
| 25 |
return true; |
| 26 |
} |
| 27 |
|
| 28 |
static FactoryResetMode factory_reset_mode_efi_variable(void) { |
| 29 |
int r; |
| 30 |
|
| 31 |
if (!is_efi_boot()) { |
| 32 |
log_debug("Not booted in EFI mode, not checking FactoryResetRequest variable."); |
| 33 |
return FACTORY_RESET_UNSPECIFIED; |
| 34 |
} |
| 35 |
|
| 36 |
_cleanup_free_ char *req_str = NULL; |
| 37 |
r = efi_get_variable_string(EFI_SYSTEMD_VARIABLE_STR("FactoryResetRequest"), &req_str); |
| 38 |
if (r == -ENOENT) { |
| 39 |
log_debug_errno(r, "EFI variable FactoryResetRequest is not set, skipping."); |
| 40 |
return FACTORY_RESET_UNSPECIFIED; |
| 41 |
} |
| 42 |
if (r < 0) |
| 43 |
return log_debug_errno(r, "Failed to get EFI variable FactoryResetRequest: %m"); |
| 44 |
|
| 45 |
_cleanup_(sd_json_variant_unrefp) sd_json_variant *v = NULL; |
| 46 |
r = sd_json_parse(req_str, /* flags= */ 0, &v, /* reterr_line= */ NULL, /* ret_column= */ NULL); |
| 47 |
if (r < 0) { |
| 48 |
log_debug_errno(r, "EFI variable FactoryResetRequest set to invalid JSON, ignoring: %m"); |
| 49 |
return FACTORY_RESET_UNSPECIFIED; |
| 50 |
} |
| 51 |
|
| 52 |
struct { |
| 53 |
const char *id; |
| 54 |
const char *image_id; |
| 55 |
sd_id128_t boot_id; |
| 56 |
} req = {}; |
| 57 |
static const sd_json_dispatch_field dispatch_table[] = { |
| 58 |
{ "osReleaseId", SD_JSON_VARIANT_STRING, sd_json_dispatch_const_string, voffsetof(req, id), SD_JSON_MANDATORY }, |
| 59 |
{ "osReleaseImageId", SD_JSON_VARIANT_STRING, sd_json_dispatch_const_string, voffsetof(req, image_id), 0 }, |
| 60 |
{ "bootId", SD_JSON_VARIANT_STRING, sd_json_dispatch_id128, voffsetof(req, boot_id), SD_JSON_MANDATORY }, |
| 61 |
{}, |
| 62 |
}; |
| 63 |
|
| 64 |
r = sd_json_dispatch(v, dispatch_table, SD_JSON_ALLOW_EXTENSIONS, &req); |
| 65 |
if (r < 0) { |
| 66 |
log_debug_errno(r, "Unable to dispatch EFI variable FactoryResetRequest, ignoring: %m"); |
| 67 |
return FACTORY_RESET_UNSPECIFIED; |
| 68 |
} |
| 69 |
|
| 70 |
_cleanup_free_ char *id = NULL, *image_id = NULL; |
| 71 |
r = parse_os_release( |
| 72 |
/* root= */ NULL, |
| 73 |
"ID", &id, |
| 74 |
"IMAGE_ID", &image_id); |
| 75 |
if (r < 0) |
| 76 |
return log_debug_errno(r, "Failed to parse os-release: %m"); |
| 77 |
|
| 78 |
if (!streq_ptr(req.id, id) || !streq_ptr(req.image_id, image_id)) { |
| 79 |
log_debug("FactoryResetRequest EFI variable set, but not for us, ignoring."); |
| 80 |
return FACTORY_RESET_UNSPECIFIED; |
| 81 |
} |
| 82 |
|
| 83 |
sd_id128_t boot_id; |
| 84 |
r = sd_id128_get_boot(&boot_id); |
| 85 |
if (r < 0) |
| 86 |
return log_debug_errno(r, "Failed to query boot ID: %m"); |
| 87 |
|
| 88 |
if (sd_id128_equal(req.boot_id, boot_id)) { |
| 89 |
/* NB: if the boot ID in the EFI variable matches our *current* one, then the request is not |
| 90 |
* intended for us, but for the *next* boot. */ |
| 91 |
log_debug("EFI variable FactoryResetRequest set for next boot."); |
| 92 |
return FACTORY_RESET_PENDING; |
| 93 |
} |
| 94 |
|
| 95 |
return FACTORY_RESET_ON; |
| 96 |
} |
| 97 |
|
| 98 |
FactoryResetMode factory_reset_mode(void) { |
| 99 |
int r; |
| 100 |
|
| 101 |
if (!factory_reset_supported()) |
| 102 |
return FACTORY_RESET_UNSUPPORTED; |
| 103 |
|
| 104 |
/* First check if we already completed a factory reset in this boot */ |
| 105 |
if (access("/run/systemd/factory-reset-complete", F_OK) >= 0) |
| 106 |
return FACTORY_RESET_COMPLETE; |
| 107 |
if (errno != ENOENT) |
| 108 |
return log_debug_errno(errno, "Can't determine if /run/systemd/factory-reset-complete exists: %m"); |
| 109 |
|
| 110 |
bool b; |
| 111 |
r = proc_cmdline_get_bool("systemd.factory_reset", /* flags= */ 0, &b); |
| 112 |
if (r < 0) |
| 113 |
return log_debug_errno(r, "Failed to parse systemd.factory_reset kernel command line argument: %m"); |
| 114 |
if (r == 0) /* Check EFI variable in case kernel cmdline switch is not specified */ |
| 115 |
return factory_reset_mode_efi_variable(); |
| 116 |
|
| 117 |
return b ? FACTORY_RESET_ON : FACTORY_RESET_OFF; /* Honour if explicitly turned off or on via kernel cmdline */ |
| 118 |
} |
| 119 |
|
| 120 |
static const char* const factory_reset_mode_table[_FACTORY_RESET_MODE_MAX] = { |
| 121 |
[FACTORY_RESET_UNSUPPORTED] = "unsupported", |
| 122 |
[FACTORY_RESET_UNSPECIFIED] = "unspecified", |
| 123 |
[FACTORY_RESET_OFF] = "off", |
| 124 |
[FACTORY_RESET_ON] = "on", |
| 125 |
[FACTORY_RESET_COMPLETE] = "complete", |
| 126 |
[FACTORY_RESET_PENDING] = "pending", |
| 127 |
}; |
| 128 |
|
| 129 |
DEFINE_STRING_TABLE_LOOKUP(factory_reset_mode, FactoryResetMode); |