From 17aaeace515d09255effbf9be93e323ce62879ce Mon Sep 17 00:00:00 2001 From: Jack Rosenthal Date: Wed, 11 Mar 2020 16:03:49 -0600 Subject: host: add host library for accessing chromeos-config Host side library for accessing chromeos-config. Initially, this will be used by futility to access the /firmware:image-name property during a firmware update. More background: go/mosys-firmware-name (note: despite the name "mosys" in the design doc, this is an effort to *not* rely on mosys during the firmware update) BUG=chromium:1061192 BRANCH=none TEST=provided unit tests Signed-off-by: Jack Rosenthal Change-Id: Ib8e5f8f836a93695e3b30731ae227501f37c4633 Reviewed-on: https://chromium-review.googlesource.com/c/chromiumos/platform/vboot_reference/+/2099449 Reviewed-by: Julius Werner --- host/lib/chromeos_config.c | 108 +++++++++++++++++++++++++++++++++++++ host/lib/include/chromeos_config.h | 56 +++++++++++++++++++ 2 files changed, 164 insertions(+) create mode 100644 host/lib/chromeos_config.c create mode 100644 host/lib/include/chromeos_config.h (limited to 'host') diff --git a/host/lib/chromeos_config.c b/host/lib/chromeos_config.c new file mode 100644 index 00000000..6307318c --- /dev/null +++ b/host/lib/chromeos_config.c @@ -0,0 +1,108 @@ +/* Copyright 2020 The Chromium OS Authors. All rights reserved. + * Use of this source code is governed by a BSD-style license that can be + * found in the LICENSE file. + */ + +#include +#include +#include +#include +#include +#include +#include + +#include "2common.h" +#include "2return_codes.h" +#include "chromeos_config.h" +#include "host_misc.h" + +#define CHROMEOS_CONFIG_BASE "/run/chromeos-config/v1" + +vb2_error_t chromeos_config_get_string(const char *path, const char *property, + char **val_out) +{ + vb2_error_t rv; + uint32_t size; + char filepath[PATH_MAX]; + + *val_out = NULL; + + if (!path || path[0] != '/') { + VB2_DEBUG("Path parameter must begin with /"); + return VB2_ERROR_INVALID_PARAMETER; + } + + if (strstr(path, "//")) { + VB2_DEBUG("Path cannot contain //"); + return VB2_ERROR_INVALID_PARAMETER; + } + + if (strchr(property, '/')) { + VB2_DEBUG("Property cannot contain /"); + return VB2_ERROR_INVALID_PARAMETER; + } + + snprintf(filepath, sizeof(filepath), CHROMEOS_CONFIG_BASE "%s/%s", path, + property); + rv = vb2_read_file(filepath, (uint8_t **)val_out, &size); + + if (rv == VB2_SUCCESS) { + *val_out = realloc(*val_out, size + 1); + (*val_out)[size] = '\0'; + } + + return rv; +} + +vb2_error_t chromeos_config_get_boolean(const char *path, const char *property, + bool *val_out) +{ + char *val_string; + vb2_error_t rv; + + *val_out = false; + if ((rv = chromeos_config_get_string(path, property, &val_string)) != + VB2_SUCCESS) + return rv; + + if (!strcmp(val_string, "false")) + goto exit; + + if (!strcmp(val_string, "true")) { + *val_out = true; + goto exit; + } + + VB2_DEBUG("Config entry is not a boolean: %s:%s", path, property); + rv = VB2_ERROR_INVALID_PARAMETER; + + exit: + free(val_string); + return rv; +} + +vb2_error_t chromeos_config_get_integer(const char *path, const char *property, + int *val_out) +{ + char *endptr; + char *val_string; + vb2_error_t rv; + + *val_out = -1; + if ((rv = chromeos_config_get_string(path, property, &val_string)) != + VB2_SUCCESS) + goto exit; + + errno = 0; + *val_out = strtol(val_string, &endptr, 10); + if (errno || endptr) { + VB2_DEBUG("Config entry is not an integer: %s:%s", path, + property); + rv = VB2_ERROR_INVALID_PARAMETER; + goto exit; + } + + exit: + free(val_string); + return rv; +} diff --git a/host/lib/include/chromeos_config.h b/host/lib/include/chromeos_config.h new file mode 100644 index 00000000..e83570e5 --- /dev/null +++ b/host/lib/include/chromeos_config.h @@ -0,0 +1,56 @@ +/* Copyright 2020 The Chromium OS Authors. All rights reserved. + * Use of this source code is governed by a BSD-style license that can be + * found in the LICENSE file. + */ + +#ifndef VBOOT_REFERENCE_CHROMEOS_CONFIG_H_ +#define VBOOT_REFERENCE_CHROMEOS_CONFIG_H_ + +#include +#include + +#include "2common.h" +#include "2return_codes.h" + +/** + * Get a value from the model configuration on the device as a string. + * + * Note: this function allocates memory by its use of vb2_read_file, and relies + * on the caller to free the allocated memory. The output parameter will be set + * to NULL upon failure, so free can be safely called on this parameter. + * + * @param path The path in the config schema to the object containing + * the requested property. + * @param property The name of the requested property. + * @param val_out Output parameter which gets assigned to a + * null-terminated string. + * @return VB2_SUCCESS on success, or a relevant error upon error. + */ +vb2_error_t chromeos_config_get_string(const char *path, const char *property, + char **val_out); + +/** + * Get a value from the model configuration on the device as a boolean. + * + * @param path The path in the config schema to the object containing + * the requested property. + * @param property The name of the requested property. + * @param val_out Output parameter which gets assigned to a boolean. + * @return VB2_SUCCESS on success, or a relevant error upon error. + */ +vb2_error_t chromeos_config_get_boolean(const char *path, const char *property, + bool *val_out); + +/** + * Get a value from the model configuration on the device as an integer. + * + * @param path The path in the config schema to the object containing + * the requested property. + * @param property The name of the requested property. + * @param val_out Output parameter which gets assigned to an integer. + * @return VB2_SUCCESS on success, or a relevant error upon error. + */ +vb2_error_t chromeos_config_get_integer(const char *path, const char *property, + int *val_out); + +#endif /* VBOOT_REFERENCE_CHROMEOS_CONFIG_H_ */ -- cgit v1.2.1