diff --git a/stand/kboot/include/util.h b/stand/kboot/include/util.h index ca71277bc66a..682ab8830bfa 100644 --- a/stand/kboot/include/util.h +++ b/stand/kboot/include/util.h @@ -1,10 +1,11 @@ /*- * Copyright (c) 2022, Netflix, Inc. * * SPDX-License-Identifier: BSD-2-Clause */ #pragma once bool file2str(const char *fn, char *buffer, size_t buflen); +bool file2u32(const char *fn, uint32_t *val); bool file2u64(const char *fn, uint64_t *val); diff --git a/stand/kboot/libkboot/util.c b/stand/kboot/libkboot/util.c index 0100a7cc5d8a..c7fe8b542643 100644 --- a/stand/kboot/libkboot/util.c +++ b/stand/kboot/libkboot/util.c @@ -1,46 +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; +}