git: 7e1a2e46aa9a - main - kboot: Create routines to read Linux tiny files
- Go to: [ bottom of page ] [ top of archives ] [ this month ]
Date: Fri, 02 Dec 2022 18:08:54 UTC
The branch main has been updated by imp: URL: https://cgit.FreeBSD.org/src/commit/?id=7e1a2e46aa9a2f834c33b5c50de0437ac19d7b41 commit 7e1a2e46aa9a2f834c33b5c50de0437ac19d7b41 Author: Warner Losh <imp@FreeBSD.org> AuthorDate: 2022-12-02 18:05:58 +0000 Commit: Warner Losh <imp@FreeBSD.org> CommitDate: 2022-12-02 18:08:11 +0000 kboot: Create routines to read Linux tiny files Most of the files in /sys/ and /proc/ are small with one value. Create two routines to help us read the file and decode that value. Sponsored by: Netflix --- stand/kboot/Makefile | 3 ++- stand/kboot/kboot.h | 4 ++++ stand/kboot/util.c | 46 ++++++++++++++++++++++++++++++++++++++++++++++ 3 files changed, 52 insertions(+), 1 deletion(-) diff --git a/stand/kboot/Makefile b/stand/kboot/Makefile index c8bd313d1d1d..42385c9caf76 100644 --- a/stand/kboot/Makefile +++ b/stand/kboot/Makefile @@ -30,7 +30,8 @@ SRCS= \ kbootfdt.c \ main.c \ termios.c \ - vers.c \ + util.c \ + vers.c CFLAGS.gfx_fb_stub.c += -I${SRCTOP}/contrib/pnglite -I${SRCTOP}/sys/teken diff --git a/stand/kboot/kboot.h b/stand/kboot/kboot.h index 5441c90eaecc..e3d169ee5ac0 100644 --- a/stand/kboot/kboot.h +++ b/stand/kboot/kboot.h @@ -19,4 +19,8 @@ void fdt_arch_fixups(void *fdtp); uint64_t kboot_get_phys_load_segment(void); uint8_t kboot_get_kernel_machine_bits(void); +/* util.c */ +bool file2str(const char *fn, char *buffer, size_t buflen); +bool file2u64(const char *fn, uint64_t *val); + #endif /* KBOOT_H */ diff --git a/stand/kboot/util.c b/stand/kboot/util.c new file mode 100644 index 000000000000..938aad599e19 --- /dev/null +++ b/stand/kboot/util.c @@ -0,0 +1,46 @@ +/*- + * Copyright 2022 Netflix, Inc + * + * SPDX-License-Identifier: BSD-2-Clause + */ + +#include "stand.h" +#include "host_syscall.h" +#include "kboot.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 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; +}