| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
|
| 6 |
#include <errno.h> |
| 7 |
#include <limits.h> |
| 8 |
#include <stdbool.h> |
| 9 |
#include <stdint.h> |
| 10 |
#include <stdio.h> |
| 11 |
#include <stdlib.h> |
| 12 |
#include <string.h> |
| 13 |
|
| 14 |
#include "2common.h" |
| 15 |
#include "2return_codes.h" |
| 16 |
#include "chromeos_config.h" |
| 17 |
#include "host_misc.h" |
| 18 |
|
| 19 |
#define CHROMEOS_CONFIG_BASE "/run/chromeos-config/v1" |
| 20 |
|
| 21 |
vb2_error_t chromeos_config_get_string(const char *path, const char *property, |
| 22 |
char **val_out) |
| 23 |
{ |
| 24 |
vb2_error_t rv; |
| 25 |
uint32_t size; |
| 26 |
char filepath[PATH_MAX]; |
| 27 |
|
| 28 |
*val_out = NULL; |
| 29 |
|
| 30 |
if (!path || path[0] != '/') { |
| 31 |
VB2_DEBUG("Path parameter must begin with /"); |
| 32 |
return VB2_ERROR_INVALID_PARAMETER; |
| 33 |
} |
| 34 |
|
| 35 |
if (strstr(path, "//")) { |
| 36 |
VB2_DEBUG("Path cannot contain //"); |
| 37 |
return VB2_ERROR_INVALID_PARAMETER; |
| 38 |
} |
| 39 |
|
| 40 |
if (strchr(property, '/')) { |
| 41 |
VB2_DEBUG("Property cannot contain /"); |
| 42 |
return VB2_ERROR_INVALID_PARAMETER; |
| 43 |
} |
| 44 |
|
| 45 |
snprintf(filepath, sizeof(filepath), CHROMEOS_CONFIG_BASE "%s/%s", path, |
| 46 |
property); |
| 47 |
rv = vb2_read_file(filepath, (uint8_t **)val_out, &size); |
| 48 |
|
| 49 |
if (rv == VB2_SUCCESS) { |
| 50 |
*val_out = realloc(*val_out, size + 1); |
| 51 |
(*val_out)[size] = '\0'; |
| 52 |
} |
| 53 |
|
| 54 |
return rv; |
| 55 |
} |
| 56 |
|
| 57 |
vb2_error_t chromeos_config_get_boolean(const char *path, const char *property, |
| 58 |
bool *val_out) |
| 59 |
{ |
| 60 |
char *val_string; |
| 61 |
vb2_error_t rv; |
| 62 |
|
| 63 |
*val_out = false; |
| 64 |
if ((rv = chromeos_config_get_string(path, property, &val_string)) != |
| 65 |
VB2_SUCCESS) |
| 66 |
return rv; |
| 67 |
|
| 68 |
if (!strcmp(val_string, "false")) |
| 69 |
goto exit; |
| 70 |
|
| 71 |
if (!strcmp(val_string, "true")) { |
| 72 |
*val_out = true; |
| 73 |
goto exit; |
| 74 |
} |
| 75 |
|
| 76 |
VB2_DEBUG("Config entry is not a boolean: %s:%s", path, property); |
| 77 |
rv = VB2_ERROR_INVALID_PARAMETER; |
| 78 |
|
| 79 |
exit: |
| 80 |
free(val_string); |
| 81 |
return rv; |
| 82 |
} |
| 83 |
|
| 84 |
vb2_error_t chromeos_config_get_integer(const char *path, const char *property, |
| 85 |
int *val_out) |
| 86 |
{ |
| 87 |
char *endptr; |
| 88 |
char *val_string; |
| 89 |
vb2_error_t rv; |
| 90 |
|
| 91 |
*val_out = -1; |
| 92 |
if ((rv = chromeos_config_get_string(path, property, &val_string)) != |
| 93 |
VB2_SUCCESS) |
| 94 |
goto exit; |
| 95 |
|
| 96 |
errno = 0; |
| 97 |
*val_out = strtol(val_string, &endptr, 10); |
| 98 |
if (errno || endptr) { |
| 99 |
VB2_DEBUG("Config entry is not an integer: %s:%s", path, |
| 100 |
property); |
| 101 |
rv = VB2_ERROR_INVALID_PARAMETER; |
| 102 |
goto exit; |
| 103 |
} |
| 104 |
|
| 105 |
exit: |
| 106 |
free(val_string); |
| 107 |
return rv; |
| 108 |
} |