diff options
Diffstat (limited to 'stand/kboot/libkboot/util.c')
-rw-r--r-- | stand/kboot/libkboot/util.c | 59 |
1 files changed, 59 insertions, 0 deletions
diff --git a/stand/kboot/libkboot/util.c b/stand/kboot/libkboot/util.c new file mode 100644 index 000000000000..c7fe8b542643 --- /dev/null +++ b/stand/kboot/libkboot/util.c @@ -0,0 +1,59 @@ +/*- + * Copyright 2022 Netflix, Inc + * + * SPDX-License-Identifier: BSD-2-Clause + */ + +#include "stand.h" +#include "host_syscall.h" +#include "util.h" + +bool +file2str(const char *fn, char *buffer, size_t buflen) +{ + int fd; + ssize_t len; + + fd = host_open(fn, HOST_O_RDONLY, 0); + if (fd == -1) + return false; + len = host_read(fd, buffer, buflen - 1); + if (len < 0) { + host_close(fd); + return false; + } + buffer[len] = '\0'; + /* + * Trim trailing white space + */ + while (isspace(buffer[len - 1])) + buffer[--len] = '\0'; + host_close(fd); + return true; +} + +bool +file2u64(const char *fn, uint64_t *val) +{ + unsigned long long v; + char buffer[80]; + + if (!file2str(fn, buffer, sizeof(buffer))) + return false; + v = strtoull(buffer, NULL, 0); /* XXX check return values? */ + *val = v; + return true; +} + +bool +file2u32(const char *fn, uint32_t *val) +{ + unsigned long v; + char buffer[80]; + + if (!file2str(fn, buffer, sizeof(buffer))) + return false; + v = strtoul(buffer, NULL, 0); /* XXX check return values? */ + *val = v; + return true; +} |