diff options
author | Jack Rosenthal <jrosenth@chromium.org> | 2020-03-11 16:03:49 -0600 |
---|---|---|
committer | Commit Bot <commit-bot@chromium.org> | 2020-03-13 03:51:49 +0000 |
commit | 17aaeace515d09255effbf9be93e323ce62879ce (patch) | |
tree | e293f3de1cbca7692635816e683857e84aeb05c5 /host/lib | |
parent | 46ff62c31c1d815196fa990e613e1e6e229f8ce2 (diff) | |
download | vboot-release-R82-12974.B.tar.gz |
host: add host library for accessing chromeos-configrelease-R82-12974.B
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 <jrosenth@chromium.org>
Change-Id: Ib8e5f8f836a93695e3b30731ae227501f37c4633
Reviewed-on: https://chromium-review.googlesource.com/c/chromiumos/platform/vboot_reference/+/2099449
Reviewed-by: Julius Werner <jwerner@chromium.org>
Diffstat (limited to 'host/lib')
-rw-r--r-- | host/lib/chromeos_config.c | 108 | ||||
-rw-r--r-- | host/lib/include/chromeos_config.h | 56 |
2 files changed, 164 insertions, 0 deletions
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 <errno.h> +#include <limits.h> +#include <stdbool.h> +#include <stdint.h> +#include <stdio.h> +#include <stdlib.h> +#include <string.h> + +#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 <stdbool.h> +#include <stdint.h> + +#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_ */ |