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
This commit is contained in:
Warner Losh 2022-12-02 11:05:58 -07:00
parent 858a73f14c
commit 7e1a2e46aa
3 changed files with 52 additions and 1 deletions

View file

@ -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

View file

@ -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 */

46
stand/kboot/util.c Normal file
View file

@ -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;
}