aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorMichael Brown <mcb30@ipxe.org>2021-03-02 23:37:41 +0000
committerMichael Brown <mcb30@ipxe.org>2021-03-02 23:59:30 +0000
commit5c8a9905ce3b04a4317d356d5481552fd17b63cb (patch)
tree5b9c13b219632bb6171fe6acc5dcfd505ae1c27d
parent976839ae4c01530a6a931be7b2386a3abee328b9 (diff)
downloadipxe-5c8a9905ce3b04a4317d356d5481552fd17b63cb.zip
ipxe-5c8a9905ce3b04a4317d356d5481552fd17b63cb.tar.gz
ipxe-5c8a9905ce3b04a4317d356d5481552fd17b63cb.tar.bz2
[linux] Add a generic function for reading files from sysfs
Signed-off-by: Michael Brown <mcb30@ipxe.org>
-rw-r--r--src/include/ipxe/errfile.h1
-rw-r--r--src/include/ipxe/linux_api.h3
-rw-r--r--src/include/ipxe/linux_sysfs.h16
-rw-r--r--src/interface/linux/linux_api.c16
-rw-r--r--src/interface/linux/linux_sysfs.c107
5 files changed, 143 insertions, 0 deletions
diff --git a/src/include/ipxe/errfile.h b/src/include/ipxe/errfile.h
index b5c5d18..3daf7bd 100644
--- a/src/include/ipxe/errfile.h
+++ b/src/include/ipxe/errfile.h
@@ -388,6 +388,7 @@ FILE_LICENCE ( GPL2_OR_LATER_OR_UBDL );
#define ERRFILE_efi_autoboot ( ERRFILE_OTHER | 0x00530000 )
#define ERRFILE_efi_autoexec ( ERRFILE_OTHER | 0x00540000 )
#define ERRFILE_efi_cachedhcp ( ERRFILE_OTHER | 0x00550000 )
+#define ERRFILE_linux_sysfs ( ERRFILE_OTHER | 0x00560000 )
/** @} */
diff --git a/src/include/ipxe/linux_api.h b/src/include/ipxe/linux_api.h
index 040b52f..ab2e801 100644
--- a/src/include/ipxe/linux_api.h
+++ b/src/include/ipxe/linux_api.h
@@ -46,6 +46,7 @@ FILE_LICENCE ( GPL2_OR_LATER );
#include <linux/ioctl.h>
#include <linux/poll.h>
#include <linux/fs.h>
+#include <linux/stat.h>
#define MAP_FAILED ( ( void * ) -1 )
#endif
@@ -65,6 +66,8 @@ extern ssize_t __asmcall linux_read ( int fd, void *buf, size_t count );
extern ssize_t __asmcall linux_write ( int fd, const void *buf, size_t count );
extern int __asmcall linux_fcntl ( int fd, int cmd, ... );
extern int __asmcall linux_ioctl ( int fd, unsigned long request, ... );
+extern int __asmcall linux_statx ( int dirfd, const char *pathname, int flags,
+ unsigned int mask, struct statx *statxbuf );
extern int __asmcall linux_poll ( struct pollfd *fds, unsigned int nfds,
int timeout );
extern int __asmcall linux_nanosleep ( const struct timespec *req,
diff --git a/src/include/ipxe/linux_sysfs.h b/src/include/ipxe/linux_sysfs.h
new file mode 100644
index 0000000..d97b649
--- /dev/null
+++ b/src/include/ipxe/linux_sysfs.h
@@ -0,0 +1,16 @@
+#ifndef _IPXE_LINUX_SYSFS_H
+#define _IPXE_LINUX_SYSFS_H
+
+/** @file
+ *
+ * Linux sysfs files
+ *
+ */
+
+FILE_LICENCE ( GPL2_OR_LATER );
+
+#include <ipxe/uaccess.h>
+
+extern int linux_sysfs_read ( const char *filename, userptr_t *data );
+
+#endif /* _IPXE_LINUX_SYSFS_H */
diff --git a/src/interface/linux/linux_api.c b/src/interface/linux/linux_api.c
index fa69433..6fa2b0e 100644
--- a/src/interface/linux/linux_api.c
+++ b/src/interface/linux/linux_api.c
@@ -32,6 +32,7 @@
#include <sys/mman.h>
#include <sys/socket.h>
#include <sys/time.h>
+#include <sys/stat.h>
#include <netinet/in.h>
#include <ipxe/linux_api.h>
#include <ipxe/slirp.h>
@@ -199,6 +200,20 @@ int __asmcall linux_ioctl ( int fd, unsigned long request, ... ) {
}
/**
+ * Wrap statx()
+ *
+ */
+int __asmcall linux_statx ( int dirfd, const char *pathname, int flags,
+ unsigned int mask, struct statx *statxbuf ) {
+ int ret;
+
+ ret = statx ( dirfd, pathname, flags, mask, statxbuf );
+ if ( ret == -1 )
+ linux_errno = errno;
+ return ret;
+}
+
+/**
* Wrap poll()
*
*/
@@ -516,6 +531,7 @@ PROVIDE_IPXE_SYM ( linux_read );
PROVIDE_IPXE_SYM ( linux_write );
PROVIDE_IPXE_SYM ( linux_fcntl );
PROVIDE_IPXE_SYM ( linux_ioctl );
+PROVIDE_IPXE_SYM ( linux_statx );
PROVIDE_IPXE_SYM ( linux_poll );
PROVIDE_IPXE_SYM ( linux_nanosleep );
PROVIDE_IPXE_SYM ( linux_usleep );
diff --git a/src/interface/linux/linux_sysfs.c b/src/interface/linux/linux_sysfs.c
new file mode 100644
index 0000000..0b9f1f5
--- /dev/null
+++ b/src/interface/linux/linux_sysfs.c
@@ -0,0 +1,107 @@
+/*
+ * Copyright (C) 2021 Michael Brown <mbrown@fensystems.co.uk>.
+ *
+ * This program is free software; you can redistribute it and/or
+ * modify it under the terms of the GNU General Public License as
+ * published by the Free Software Foundation; either version 2 of the
+ * License, or any later version.
+ *
+ * This program is distributed in the hope that it will be useful, but
+ * WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
+ * General Public License for more details.
+ *
+ * You should have received a copy of the GNU General Public License
+ * along with this program; if not, write to the Free Software
+ * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
+ * 02110-1301, USA.
+ */
+
+FILE_LICENCE ( GPL2_OR_LATER );
+
+#include <string.h>
+#include <errno.h>
+#include <ipxe/umalloc.h>
+#include <ipxe/linux_api.h>
+#include <ipxe/linux.h>
+#include <ipxe/linux_sysfs.h>
+
+/** @file
+ *
+ * Linux sysfs files
+ *
+ */
+
+/**
+ * Read file from sysfs
+ *
+ * @v filename Filename
+ * @v data Data to fill in
+ * @ret len Length read, or negative error
+ */
+int linux_sysfs_read ( const char *filename, userptr_t *data ) {
+ struct statx statx;
+ size_t offset;
+ size_t len;
+ ssize_t read;
+ int fd;
+ int rc;
+
+ /* Open file */
+ fd = linux_open ( filename, O_RDONLY );
+ if ( fd < 0 ) {
+ rc = -ELINUX ( linux_errno );
+ DBGC ( filename, "LINUX could not open %s: %s\n",
+ filename, linux_strerror ( linux_errno ) );
+ goto err_open;
+ }
+
+ /* Get file length */
+ if ( linux_statx ( fd, "", AT_EMPTY_PATH, STATX_SIZE, &statx ) == -1 ) {
+ rc = -ELINUX ( linux_errno );
+ DBGC ( filename, "LINUX could not stat %s: %s\n",
+ filename, linux_strerror ( linux_errno ) );
+ goto err_stat;
+ }
+ len = statx.stx_size;
+
+ /* Allocate buffer */
+ *data = umalloc ( len );
+ if ( ! *data ) {
+ rc = -ENOMEM;
+ DBGC ( filename, "LINUX could not allocate %zd bytes for %s\n",
+ len, filename );
+ goto err_alloc;
+ }
+
+ /* Read file */
+ for ( offset = 0 ; offset < len ; offset += read ) {
+ read = linux_read ( fd, user_to_virt ( *data, offset ), len );
+ if ( read < 0 ) {
+ DBGC ( filename, "LINUX could not read %s: %s\n",
+ filename, linux_strerror ( linux_errno ) );
+ goto err_read;
+ }
+ if ( read == 0 ) {
+ rc = -EIO;
+ DBGC ( filename, "LINUX read underlength %s\n",
+ filename );
+ goto err_eof;
+ }
+ }
+
+ /* Close file */
+ linux_close ( fd );
+
+ DBGC ( filename, "LINUX read %s\n", filename );
+ return len;
+
+ err_eof:
+ err_read:
+ ufree ( *data );
+ err_alloc:
+ err_stat:
+ linux_close ( fd );
+ err_open:
+ return rc;
+}