| 1 |
/* SPDX-License-Identifier: LGPL-2.1-or-later */ |
| 2 |
|
| 3 |
#include "sd-device.h" |
| 4 |
|
| 5 |
#include "alloc-util.h" |
| 6 |
#include "analyze.h" |
| 7 |
#include "analyze-chid.h" |
| 8 |
#include "ansi-color.h" |
| 9 |
#include "chid.h" |
| 10 |
#include "device-util.h" |
| 11 |
#include "edid.h" |
| 12 |
#include "efi-api.h" |
| 13 |
#include "errno-util.h" |
| 14 |
#include "escape.h" |
| 15 |
#include "fd-util.h" |
| 16 |
#include "fileio.h" |
| 17 |
#include "format-table.h" |
| 18 |
#include "glyph-util.h" |
| 19 |
#include "parse-util.h" |
| 20 |
#include "string-util.h" |
| 21 |
#include "strv.h" |
| 22 |
#include "utf8.h" |
| 23 |
#include "virt.h" |
| 24 |
|
| 25 |
static int parse_chid_type(const char *s, size_t *ret) { |
| 26 |
const char *e; |
| 27 |
unsigned u; |
| 28 |
int r; |
| 29 |
|
| 30 |
assert(s); |
| 31 |
|
| 32 |
if ((e = startswith(s, "ext"))) { |
| 33 |
r = safe_atou(e, &u); |
| 34 |
if (r < 0) |
| 35 |
return r; |
| 36 |
if (u >= CHID_TYPES_MAX - EXTRA_CHID_BASE) |
| 37 |
return -ERANGE; |
| 38 |
u += EXTRA_CHID_BASE; |
| 39 |
} else { |
| 40 |
r = safe_atou(s, &u); |
| 41 |
if (r < 0) |
| 42 |
return r; |
| 43 |
if (u >= EXTRA_CHID_BASE) |
| 44 |
return -ERANGE; |
| 45 |
} |
| 46 |
|
| 47 |
|
| 48 |
if (ret) |
| 49 |
*ret = u; |
| 50 |
|
| 51 |
return 0; |
| 52 |
} |
| 53 |
|
| 54 |
static const char *const chid_smbios_friendly[_CHID_SMBIOS_FIELDS_MAX] = { |
| 55 |
[CHID_SMBIOS_MANUFACTURER] = "manufacturer", |
| 56 |
[CHID_SMBIOS_FAMILY] = "family", |
| 57 |
[CHID_SMBIOS_PRODUCT_NAME] = "product-name", |
| 58 |
[CHID_SMBIOS_PRODUCT_SKU] = "product-sku", |
| 59 |
[CHID_SMBIOS_BASEBOARD_MANUFACTURER] = "baseboard-manufacturer", |
| 60 |
[CHID_SMBIOS_BASEBOARD_PRODUCT] = "baseboard-product", |
| 61 |
[CHID_SMBIOS_BIOS_VENDOR] = "bios-vendor", |
| 62 |
[CHID_SMBIOS_BIOS_VERSION] = "bios-version", |
| 63 |
[CHID_SMBIOS_BIOS_MAJOR] = "bios-major", |
| 64 |
[CHID_SMBIOS_BIOS_MINOR] = "bios-minor", |
| 65 |
[CHID_SMBIOS_ENCLOSURE_TYPE] = "enclosure-type", |
| 66 |
[CHID_EDID_PANEL] = "edid-panel", |
| 67 |
}; |
| 68 |
|
| 69 |
static const char chid_smbios_fields_char[_CHID_SMBIOS_FIELDS_MAX] = { |
| 70 |
[CHID_SMBIOS_MANUFACTURER] = 'M', |
| 71 |
[CHID_SMBIOS_FAMILY] = 'F', |
| 72 |
[CHID_SMBIOS_PRODUCT_NAME] = 'P', |
| 73 |
[CHID_SMBIOS_PRODUCT_SKU] = 'S', |
| 74 |
[CHID_SMBIOS_BASEBOARD_MANUFACTURER] = 'm', |
| 75 |
[CHID_SMBIOS_BASEBOARD_PRODUCT] = 'p', |
| 76 |
[CHID_SMBIOS_BIOS_VENDOR] = 'B', |
| 77 |
[CHID_SMBIOS_BIOS_VERSION] = 'v', |
| 78 |
[CHID_SMBIOS_BIOS_MAJOR] = 'R', |
| 79 |
[CHID_SMBIOS_BIOS_MINOR] = 'r', |
| 80 |
[CHID_SMBIOS_ENCLOSURE_TYPE] = 'e', |
| 81 |
[CHID_EDID_PANEL] = 'E', |
| 82 |
}; |
| 83 |
|
| 84 |
static char *chid_smbios_fields_string(uint32_t combination) { |
| 85 |
_cleanup_free_ char *s = NULL; |
| 86 |
|
| 87 |
for (ChidSmbiosFields f = 0; f < _CHID_SMBIOS_FIELDS_MAX; f++) { |
| 88 |
char c; |
| 89 |
|
| 90 |
c = (combination & (UINT32_C(1) << f)) ? chid_smbios_fields_char[f] : '-'; |
| 91 |
|
| 92 |
if (!strextend(&s, CHAR_TO_STR(c))) |
| 93 |
return NULL; |
| 94 |
} |
| 95 |
|
| 96 |
return TAKE_PTR(s); |
| 97 |
} |
| 98 |
|
| 99 |
static int add_chid(Table *table, const EFI_GUID guids[static CHID_TYPES_MAX], size_t t) { |
| 100 |
int r; |
| 101 |
|
| 102 |
assert(table); |
| 103 |
assert(guids); |
| 104 |
assert(t < CHID_TYPES_MAX); |
| 105 |
|
| 106 |
sd_id128_t id = efi_guid_to_id128(guids + t); |
| 107 |
|
| 108 |
if (sd_id128_is_null(id)) |
| 109 |
return 0; |
| 110 |
|
| 111 |
_cleanup_free_ char *flags = chid_smbios_fields_string(chid_smbios_table[t]); |
| 112 |
if (!flags) |
| 113 |
return log_oom(); |
| 114 |
|
| 115 |
if (t < EXTRA_CHID_BASE) |
| 116 |
r = table_add_many(table, TABLE_UINT, (unsigned) t); |
| 117 |
else |
| 118 |
r = table_add_cell_stringf(table, NULL, "ext%zu", t - EXTRA_CHID_BASE); |
| 119 |
if (r < 0) |
| 120 |
return table_log_add_error(r); |
| 121 |
|
| 122 |
r = table_add_many(table, |
| 123 |
TABLE_STRING, flags, |
| 124 |
TABLE_UUID, id); |
| 125 |
if (r < 0) |
| 126 |
return table_log_add_error(r); |
| 127 |
|
| 128 |
return 0; |
| 129 |
} |
| 130 |
|
| 131 |
static void smbios_fields_free(char16_t *(*fields)[_CHID_SMBIOS_FIELDS_MAX]) { |
| 132 |
assert(fields); |
| 133 |
|
| 134 |
FOREACH_ARRAY(i, *fields, _CHID_SMBIOS_FIELDS_MAX) |
| 135 |
free(*i); |
| 136 |
} |
| 137 |
|
| 138 |
static int smbios_fields_acquire(char16_t *fields[static _CHID_SMBIOS_FIELDS_MAX]) { |
| 139 |
|
| 140 |
static const char *const smbios_files[_CHID_SMBIOS_FIELDS_MAX] = { |
| 141 |
[CHID_SMBIOS_MANUFACTURER] = "sys_vendor", |
| 142 |
[CHID_SMBIOS_FAMILY] = "product_family", |
| 143 |
[CHID_SMBIOS_PRODUCT_NAME] = "product_name", |
| 144 |
[CHID_SMBIOS_PRODUCT_SKU] = "product_sku", |
| 145 |
[CHID_SMBIOS_BASEBOARD_MANUFACTURER] = "board_vendor", |
| 146 |
[CHID_SMBIOS_BASEBOARD_PRODUCT] = "board_name", |
| 147 |
[CHID_SMBIOS_BIOS_VENDOR] = "bios_vendor", |
| 148 |
[CHID_SMBIOS_BIOS_VERSION] = "bios_version", |
| 149 |
[CHID_SMBIOS_BIOS_MAJOR] = "bios_release", |
| 150 |
[CHID_SMBIOS_BIOS_MINOR] = "bios_release", |
| 151 |
[CHID_SMBIOS_ENCLOSURE_TYPE] = "chassis_type", |
| 152 |
}; |
| 153 |
|
| 154 |
int r; |
| 155 |
|
| 156 |
_cleanup_close_ int smbios_fd = open("/sys/class/dmi/id", O_RDONLY|O_DIRECTORY|O_CLOEXEC); |
| 157 |
if (smbios_fd < 0) |
| 158 |
return log_error_errno(errno, "Failed to open SMBIOS sysfs object: %m"); |
| 159 |
|
| 160 |
for (ChidSmbiosFields f = 0; f < _CHID_SMBIOS_FIELDS_MAX; f++) { |
| 161 |
_cleanup_free_ char *buf = NULL; |
| 162 |
size_t size; |
| 163 |
|
| 164 |
/* According to the CHID spec we should not generate CHIDs for SMBIOS fields that aren't set |
| 165 |
* or are set to an empty string. Hence leave them NULL here. */ |
| 166 |
|
| 167 |
if (!smbios_files[f]) |
| 168 |
continue; |
| 169 |
|
| 170 |
r = read_virtual_file_at(smbios_fd, smbios_files[f], SIZE_MAX, &buf, &size); |
| 171 |
if (r == -ENOENT) { |
| 172 |
log_debug_errno(r, "SMBIOS field '%s' not set, skipping.", smbios_files[f]); |
| 173 |
continue; |
| 174 |
} |
| 175 |
if (r < 0) |
| 176 |
return log_error_errno(r, "Failed to read SMBIOS field '%s': %m", smbios_files[f]); |
| 177 |
|
| 178 |
if (size == 0 || (size == 1 && buf[0] == '\n')) { |
| 179 |
log_debug("SMBIOS field '%s' is empty, skipping.", smbios_files[f]); |
| 180 |
continue; |
| 181 |
} |
| 182 |
|
| 183 |
if (buf[size-1] != '\n') |
| 184 |
return log_error_errno(SYNTHETIC_ERRNO(EINVAL), "Expected SMBIOS field '%s' to end in newline, but it doesn't, refusing.", smbios_files[f]); |
| 185 |
|
| 186 |
buf[size-1] = 0; |
| 187 |
size--; |
| 188 |
|
| 189 |
switch (f) { |
| 190 |
|
| 191 |
case CHID_SMBIOS_BIOS_MAJOR: |
| 192 |
case CHID_SMBIOS_BIOS_MINOR: { |
| 193 |
/* The kernel exposes this a string <major>.<minor>, split them apart again. */ |
| 194 |
char *dot = memchr(buf, '.', size); |
| 195 |
if (!dot) |
| 196 |
return log_error_errno(SYNTHETIC_ERRNO(EINVAL), "BIOS release field '%s' contains no dot?", smbios_files[f]); |
| 197 |
|
| 198 |
const char *p; |
| 199 |
if (f == CHID_SMBIOS_BIOS_MAJOR) { |
| 200 |
*dot = 0; |
| 201 |
p = buf; |
| 202 |
} else { |
| 203 |
assert(f == CHID_SMBIOS_BIOS_MINOR); |
| 204 |
p = dot + 1; |
| 205 |
} |
| 206 |
|
| 207 |
/* The kernel exports the enclosure in decimal, we need it in hex (zero left-padded) */ |
| 208 |
|
| 209 |
uint8_t u; |
| 210 |
r = safe_atou8(p, &u); |
| 211 |
if (r < 0) |
| 212 |
return log_error_errno(r, "Failed to parse BIOS release: %s", p); |
| 213 |
|
| 214 |
buf = mfree(buf); |
| 215 |
if (asprintf(&buf, "%02x", u) < 0) |
| 216 |
return log_oom(); |
| 217 |
|
| 218 |
size = strlen(buf); |
| 219 |
break; |
| 220 |
} |
| 221 |
|
| 222 |
case CHID_SMBIOS_ENCLOSURE_TYPE: { |
| 223 |
/* The kernel exports the enclosure in decimal, we need it in hex (no padding!) */ |
| 224 |
|
| 225 |
uint8_t u; |
| 226 |
r = safe_atou8(buf, &u); |
| 227 |
if (r < 0) |
| 228 |
return log_error_errno(r, "Failed to parse enclosure type: %s", buf); |
| 229 |
|
| 230 |
buf = mfree(buf); |
| 231 |
if (u == 0) |
| 232 |
buf = strdup(""); /* zero is mapped to empty string */ |
| 233 |
else |
| 234 |
(void) asprintf(&buf, "%x", u); |
| 235 |
if (!buf) |
| 236 |
return log_oom(); |
| 237 |
|
| 238 |
size = strlen(buf); |
| 239 |
break; |
| 240 |
} |
| 241 |
|
| 242 |
default: |
| 243 |
; |
| 244 |
} |
| 245 |
|
| 246 |
fields[f] = utf8_to_utf16(buf, size); |
| 247 |
if (!fields[f]) |
| 248 |
return log_oom(); |
| 249 |
} |
| 250 |
|
| 251 |
return 0; |
| 252 |
} |
| 253 |
|
| 254 |
static int edid_parse(sd_device *drm_dev, char16_t **ret_panel) { |
| 255 |
const char *edid_content; |
| 256 |
size_t edid_size; |
| 257 |
int r; |
| 258 |
|
| 259 |
assert(drm_dev); |
| 260 |
assert(ret_panel); |
| 261 |
|
| 262 |
r = sd_device_get_sysattr_value_with_size(drm_dev, "edid", &edid_content, &edid_size); |
| 263 |
if (r < 0) |
| 264 |
return r; |
| 265 |
if (edid_size == 0) |
| 266 |
return -ENXIO; |
| 267 |
|
| 268 |
EdidHeader header; |
| 269 |
if (edid_parse_blob(edid_content, edid_size, &header) < 0) |
| 270 |
return -EBADMSG; |
| 271 |
|
| 272 |
_cleanup_free_ char16_t *panel_id = new0(char16_t, 8); |
| 273 |
if (!panel_id) |
| 274 |
return -ENOMEM; |
| 275 |
|
| 276 |
if (edid_get_panel_id(&header, panel_id) < 0) |
| 277 |
return -EBADMSG; |
| 278 |
|
| 279 |
*ret_panel = TAKE_PTR(panel_id); |
| 280 |
return 0; |
| 281 |
} |
| 282 |
|
| 283 |
static int edid_search(char16_t **ret_panel) { |
| 284 |
_cleanup_(sd_device_enumerator_unrefp) sd_device_enumerator *e = NULL; |
| 285 |
_cleanup_strv_free_ char **drm_paths = NULL; |
| 286 |
_cleanup_free_ char16_t *unique_panel = NULL; |
| 287 |
size_t n = 0; |
| 288 |
int r; |
| 289 |
|
| 290 |
assert(ret_panel); |
| 291 |
|
| 292 |
r = sd_device_enumerator_new(&e); |
| 293 |
if (r < 0) |
| 294 |
return log_error_errno(r, "Failed to create device enumerator: %m"); |
| 295 |
|
| 296 |
r = sd_device_enumerator_allow_uninitialized(e); |
| 297 |
if (r < 0) |
| 298 |
return log_error_errno(r, "Failed to allow uninitialized device enumerator: %m"); |
| 299 |
|
| 300 |
r = sd_device_enumerator_add_match_subsystem(e, "drm", true); |
| 301 |
if (r < 0) |
| 302 |
return log_error_errno(r, "Failed to add drm match subsystem to device enumerator: %m"); |
| 303 |
|
| 304 |
FOREACH_DEVICE(e, d) { |
| 305 |
_cleanup_free_ char16_t *panel = NULL; |
| 306 |
const char *drm_path; |
| 307 |
|
| 308 |
r = sd_device_get_syspath(d, &drm_path); |
| 309 |
if (r < 0) |
| 310 |
return log_device_error_errno(d, r, "Failed to get syspath from device: %m"); |
| 311 |
|
| 312 |
r = edid_parse(d, &panel); |
| 313 |
if (ERRNO_IS_DEVICE_ABSENT(r)) |
| 314 |
continue; |
| 315 |
if (r < 0) { |
| 316 |
log_device_debug_errno(d, r, "Failed to parse EDID from DRM device, skipping: %m"); |
| 317 |
continue; |
| 318 |
} |
| 319 |
|
| 320 |
if (!unique_panel) |
| 321 |
unique_panel = TAKE_PTR(panel); |
| 322 |
|
| 323 |
if (strv_extend_with_size(&drm_paths, &n, drm_path) < 0) |
| 324 |
return log_oom(); |
| 325 |
} |
| 326 |
|
| 327 |
if (n == 1) { |
| 328 |
*ret_panel = TAKE_PTR(unique_panel); |
| 329 |
return 0; |
| 330 |
} |
| 331 |
if (n == 0) |
| 332 |
return log_error_errno(SYNTHETIC_ERRNO(ENODEV), "No monitors detected, skipping EDID CHID extensions."); |
| 333 |
|
| 334 |
log_notice("Multiple monitors detected, skipping EDID CHID extensions."); |
| 335 |
STRV_FOREACH(s, drm_paths) |
| 336 |
log_info("Hint: use --drm-device=%s", *s); |
| 337 |
|
| 338 |
return -ENOTUNIQ; |
| 339 |
} |
| 340 |
|
| 341 |
int verb_chid(int argc, char *argv[], uintptr_t _data, void *userdata) { |
| 342 |
|
| 343 |
_cleanup_(table_unrefp) Table *table = NULL; |
| 344 |
int r; |
| 345 |
|
| 346 |
if (detect_container() > 0) |
| 347 |
return log_error_errno(SYNTHETIC_ERRNO(ENOTRECOVERABLE), "Container environments do not have SMBIOS."); |
| 348 |
|
| 349 |
table = table_new("type", "input", "chid"); |
| 350 |
if (!table) |
| 351 |
return log_oom(); |
| 352 |
|
| 353 |
(void) table_set_align_percent(table, table_get_cell(table, 0, 0), 100); |
| 354 |
(void) table_set_align_percent(table, table_get_cell(table, 0, 1), 50); |
| 355 |
|
| 356 |
_cleanup_(smbios_fields_free) char16_t* smbios_fields[_CHID_SMBIOS_FIELDS_MAX] = {}; |
| 357 |
r = smbios_fields_acquire(smbios_fields); |
| 358 |
if (r < 0) |
| 359 |
return r; |
| 360 |
|
| 361 |
if (arg_drm_device_path) { |
| 362 |
_cleanup_(sd_device_unrefp) sd_device *drm_dev = NULL; |
| 363 |
r = sd_device_new_from_path(&drm_dev, arg_drm_device_path); |
| 364 |
if (r < 0) |
| 365 |
return log_error_errno(r, "Failed to open device %s: %m", arg_drm_device_path); |
| 366 |
|
| 367 |
r = device_in_subsystem(drm_dev, "drm"); |
| 368 |
if (r < 0) |
| 369 |
return log_error_errno(r, "Failed to check if the device is a DRM device: %m"); |
| 370 |
if (r == 0) |
| 371 |
return log_error_errno(SYNTHETIC_ERRNO(EINVAL), "Cannot read EDID from a non-DRM device '%s'", arg_drm_device_path); |
| 372 |
|
| 373 |
r = edid_parse(drm_dev, &smbios_fields[CHID_EDID_PANEL]); |
| 374 |
if (r < 0) |
| 375 |
return log_error_errno(r, "Failed to parse EDID for device %s: %m", arg_drm_device_path); |
| 376 |
} else { |
| 377 |
r = edid_search(&smbios_fields[CHID_EDID_PANEL]); |
| 378 |
if (r < 0 && !IN_SET(r, -ENOTUNIQ, -ENODEV)) |
| 379 |
return r; |
| 380 |
} |
| 381 |
|
| 382 |
EFI_GUID chids[CHID_TYPES_MAX] = {}; |
| 383 |
chid_calculate((const char16_t* const*) smbios_fields, chids); |
| 384 |
|
| 385 |
if (strv_isempty(strv_skip(argv, 1))) |
| 386 |
for (size_t t = 0; t < CHID_TYPES_MAX; t++) { |
| 387 |
r = add_chid(table, chids, t); |
| 388 |
if (r < 0) |
| 389 |
return r; |
| 390 |
} |
| 391 |
else { |
| 392 |
STRV_FOREACH(as, strv_skip(argv, 1)) { |
| 393 |
size_t t; |
| 394 |
r = parse_chid_type(*as, &t); |
| 395 |
if (r < 0) |
| 396 |
return log_error_errno(r, "Failed to parse CHID type: %s", *as); |
| 397 |
|
| 398 |
r = add_chid(table, chids, t); |
| 399 |
if (r < 0) |
| 400 |
return r; |
| 401 |
} |
| 402 |
|
| 403 |
(void) table_set_sort(table, (size_t) 0); |
| 404 |
} |
| 405 |
|
| 406 |
r = table_print_with_pager(table, arg_json_format_flags, arg_pager_flags, arg_legend); |
| 407 |
if (r < 0) |
| 408 |
return r; |
| 409 |
|
| 410 |
if (!sd_json_format_enabled(arg_json_format_flags)) { |
| 411 |
_cleanup_free_ char *legend = NULL; |
| 412 |
bool separator = false; |
| 413 |
size_t w = 0; |
| 414 |
|
| 415 |
legend = strjoin(ansi_grey(), "LEGEND: ", ansi_normal()); |
| 416 |
if (!legend) |
| 417 |
return log_oom(); |
| 418 |
|
| 419 |
for (ChidSmbiosFields f = 0; f < _CHID_SMBIOS_FIELDS_MAX; f++) { |
| 420 |
_cleanup_free_ char *c = NULL; |
| 421 |
|
| 422 |
if (smbios_fields[f]) { |
| 423 |
_cleanup_free_ char *u = NULL; |
| 424 |
|
| 425 |
u = utf16_to_utf8(smbios_fields[f], SIZE_MAX); |
| 426 |
if (!u) |
| 427 |
return log_oom(); |
| 428 |
|
| 429 |
c = cescape(u); |
| 430 |
if (!c) |
| 431 |
return log_oom(); |
| 432 |
} |
| 433 |
|
| 434 |
if (!strextend(&legend, |
| 435 |
ansi_grey(), |
| 436 |
separator ? " " : "", |
| 437 |
separator ? glyph(GLYPH_HORIZONTAL_DOTTED) : "", |
| 438 |
separator ? " " : "", |
| 439 |
ansi_normal(), |
| 440 |
CHAR_TO_STR(chid_smbios_fields_char[f]), |
| 441 |
ansi_grey(), |
| 442 |
" ", |
| 443 |
glyph(GLYPH_ARROW_RIGHT), |
| 444 |
" ", |
| 445 |
ansi_normal(), |
| 446 |
chid_smbios_friendly[f], |
| 447 |
ansi_grey(), |
| 448 |
" (", |
| 449 |
c ? ansi_highlight() : ansi_grey(), |
| 450 |
strna(c), |
| 451 |
ansi_grey(), |
| 452 |
")", |
| 453 |
ansi_normal())) |
| 454 |
return log_oom(); |
| 455 |
|
| 456 |
w += separator * 3 + |
| 457 |
4 + |
| 458 |
utf8_console_width(chid_smbios_friendly[f]) + |
| 459 |
2 + |
| 460 |
utf8_console_width(strna(c)) + |
| 461 |
1; |
| 462 |
|
| 463 |
if (w > 79) { |
| 464 |
if (!strextend(&legend, "\n ")) |
| 465 |
return log_oom(); |
| 466 |
|
| 467 |
separator = false; |
| 468 |
w = 8; |
| 469 |
} else |
| 470 |
separator = true; |
| 471 |
|
| 472 |
} |
| 473 |
|
| 474 |
putchar('\n'); |
| 475 |
puts(legend); |
| 476 |
} |
| 477 |
|
| 478 |
return EXIT_SUCCESS; |
| 479 |
} |