mirror of https://github.com/ipxe/ipxe.git
[linux] Use generic sysfs mechanism to read ACPI tables
Signed-off-by: Michael Brown <mcb30@ipxe.org>pull/310/head
parent
5c8a9905ce
commit
6816006808
|
@ -23,9 +23,11 @@ FILE_LICENCE ( GPL2_OR_LATER );
|
||||||
#include <stdio.h>
|
#include <stdio.h>
|
||||||
#include <errno.h>
|
#include <errno.h>
|
||||||
#include <ipxe/linux_api.h>
|
#include <ipxe/linux_api.h>
|
||||||
|
#include <ipxe/linux_sysfs.h>
|
||||||
#include <ipxe/linux.h>
|
#include <ipxe/linux.h>
|
||||||
#include <ipxe/list.h>
|
#include <ipxe/list.h>
|
||||||
#include <ipxe/init.h>
|
#include <ipxe/init.h>
|
||||||
|
#include <ipxe/umalloc.h>
|
||||||
#include <ipxe/acpi.h>
|
#include <ipxe/acpi.h>
|
||||||
|
|
||||||
/** ACPI sysfs directory */
|
/** ACPI sysfs directory */
|
||||||
|
@ -40,7 +42,7 @@ struct linux_acpi_table {
|
||||||
/** Index */
|
/** Index */
|
||||||
unsigned int index;
|
unsigned int index;
|
||||||
/** Cached data */
|
/** Cached data */
|
||||||
void *data;
|
userptr_t data;
|
||||||
};
|
};
|
||||||
|
|
||||||
/** List of cached ACPI tables */
|
/** List of cached ACPI tables */
|
||||||
|
@ -55,7 +57,6 @@ static LIST_HEAD ( linux_acpi_tables );
|
||||||
*/
|
*/
|
||||||
static userptr_t linux_acpi_find ( uint32_t signature, unsigned int index ) {
|
static userptr_t linux_acpi_find ( uint32_t signature, unsigned int index ) {
|
||||||
struct linux_acpi_table *table;
|
struct linux_acpi_table *table;
|
||||||
struct acpi_header header;
|
|
||||||
union {
|
union {
|
||||||
uint32_t signature;
|
uint32_t signature;
|
||||||
char filename[5];
|
char filename[5];
|
||||||
|
@ -63,16 +64,14 @@ static userptr_t linux_acpi_find ( uint32_t signature, unsigned int index ) {
|
||||||
static const char prefix[] = ACPI_SYSFS_PREFIX;
|
static const char prefix[] = ACPI_SYSFS_PREFIX;
|
||||||
char filename[ sizeof ( prefix ) - 1 /* NUL */ + 4 /* signature */
|
char filename[ sizeof ( prefix ) - 1 /* NUL */ + 4 /* signature */
|
||||||
+ 3 /* "999" */ + 1 /* NUL */ ];
|
+ 3 /* "999" */ + 1 /* NUL */ ];
|
||||||
ssize_t rlen;
|
int len;
|
||||||
size_t len;
|
int rc;
|
||||||
void *data;
|
|
||||||
int fd;
|
|
||||||
|
|
||||||
/* Check for existing table */
|
/* Check for existing table */
|
||||||
list_for_each_entry ( table, &linux_acpi_tables, list ) {
|
list_for_each_entry ( table, &linux_acpi_tables, list ) {
|
||||||
if ( ( table->signature == signature ) &&
|
if ( ( table->signature == signature ) &&
|
||||||
( table->index == index ) )
|
( table->index == index ) )
|
||||||
return virt_to_user ( table->data );
|
return table->data;
|
||||||
}
|
}
|
||||||
|
|
||||||
/* Allocate a new table */
|
/* Allocate a new table */
|
||||||
|
@ -82,90 +81,34 @@ static userptr_t linux_acpi_find ( uint32_t signature, unsigned int index ) {
|
||||||
table->signature = signature;
|
table->signature = signature;
|
||||||
table->index = index;
|
table->index = index;
|
||||||
|
|
||||||
/* Construct filename */
|
/* Construct filename (including numeric suffix) */
|
||||||
memset ( &u, 0, sizeof ( u ) );
|
memset ( &u, 0, sizeof ( u ) );
|
||||||
u.signature = le32_to_cpu ( signature );
|
u.signature = le32_to_cpu ( signature );
|
||||||
snprintf ( filename, sizeof ( filename ), "%s%s%d", prefix,
|
snprintf ( filename, sizeof ( filename ), "%s%s%d", prefix,
|
||||||
u.filename, ( index + 1 ) );
|
u.filename, ( index + 1 ) );
|
||||||
|
|
||||||
/* Open file */
|
/* Read file (with or without numeric suffix for index 0) */
|
||||||
fd = linux_open ( filename, O_RDONLY );
|
len = linux_sysfs_read ( filename, &table->data );
|
||||||
if ( ( fd < 0 ) && ( index == 0 ) ) {
|
if ( ( len < 0 ) && ( index == 0 ) ) {
|
||||||
filename[ sizeof ( prefix ) - 1 /* NUL */ +
|
filename[ sizeof ( prefix ) - 1 /* NUL */ +
|
||||||
4 /* signature */ ] = '\0';
|
4 /* signature */ ] = '\0';
|
||||||
fd = linux_open ( filename, O_RDONLY );
|
len = linux_sysfs_read ( filename, &table->data );
|
||||||
}
|
}
|
||||||
if ( fd < 0 ) {
|
if ( len < 0 ) {
|
||||||
DBGC ( &linux_acpi_tables, "ACPI could not open %s: %s\n",
|
rc = len;
|
||||||
filename, linux_strerror ( linux_errno ) );
|
DBGC ( &linux_acpi_tables, "ACPI could not read %s: %s\n",
|
||||||
goto err_open;
|
filename, strerror ( rc ) );
|
||||||
|
goto err_read;
|
||||||
}
|
}
|
||||||
|
|
||||||
/* Read header */
|
|
||||||
rlen = linux_read ( fd, &header, sizeof ( header ) );
|
|
||||||
if ( rlen < 0 ) {
|
|
||||||
DBGC ( &linux_acpi_tables, "ACPI could not read %s header: "
|
|
||||||
"%s\n", filename, linux_strerror ( linux_errno ) );
|
|
||||||
goto err_header;
|
|
||||||
}
|
|
||||||
if ( ( ( size_t ) rlen ) != sizeof ( header ) ) {
|
|
||||||
DBGC ( &linux_acpi_tables, "ACPI underlength %s header\n",
|
|
||||||
filename );
|
|
||||||
goto err_header_len;
|
|
||||||
}
|
|
||||||
|
|
||||||
/* Parse header */
|
|
||||||
len = le32_to_cpu ( header.length );
|
|
||||||
if ( len < sizeof ( header ) ) {
|
|
||||||
DBGC ( &linux_acpi_tables, "ACPI malformed %s header\n",
|
|
||||||
filename );
|
|
||||||
goto err_malformed;
|
|
||||||
}
|
|
||||||
|
|
||||||
/* Allocate data */
|
|
||||||
table->data = malloc ( len );
|
|
||||||
if ( ! table->data )
|
|
||||||
goto err_data;
|
|
||||||
|
|
||||||
/* Read table */
|
|
||||||
memcpy ( table->data, &header, sizeof ( header ) );
|
|
||||||
data = ( table->data + sizeof ( header ) );
|
|
||||||
len -= sizeof ( header );
|
|
||||||
while ( len ) {
|
|
||||||
rlen = linux_read ( fd, data, len );
|
|
||||||
if ( rlen < 0 ) {
|
|
||||||
DBGC ( &linux_acpi_tables, "ACPI could not read %s: "
|
|
||||||
"%s\n", filename,
|
|
||||||
linux_strerror ( linux_errno ) );
|
|
||||||
goto err_body;
|
|
||||||
}
|
|
||||||
if ( rlen == 0 ) {
|
|
||||||
DBGC ( &linux_acpi_tables, "ACPI underlength %s\n",
|
|
||||||
filename );
|
|
||||||
goto err_body_len;
|
|
||||||
}
|
|
||||||
data += rlen;
|
|
||||||
len -= rlen;
|
|
||||||
}
|
|
||||||
|
|
||||||
/* Close file */
|
|
||||||
linux_close ( fd );
|
|
||||||
|
|
||||||
/* Add to list of tables */
|
/* Add to list of tables */
|
||||||
list_add ( &table->list, &linux_acpi_tables );
|
list_add ( &table->list, &linux_acpi_tables );
|
||||||
DBGC ( &linux_acpi_tables, "ACPI cached %s\n", filename );
|
DBGC ( &linux_acpi_tables, "ACPI cached %s\n", filename );
|
||||||
|
|
||||||
return virt_to_user ( table->data );
|
return table->data;
|
||||||
|
|
||||||
err_body_len:
|
ufree ( table->data );
|
||||||
err_body:
|
err_read:
|
||||||
free ( table->data );
|
|
||||||
err_data:
|
|
||||||
err_malformed:
|
|
||||||
err_header_len:
|
|
||||||
err_header:
|
|
||||||
linux_close ( fd );
|
|
||||||
err_open:
|
|
||||||
free ( table );
|
free ( table );
|
||||||
err_alloc:
|
err_alloc:
|
||||||
return UNULL;
|
return UNULL;
|
||||||
|
@ -181,7 +124,7 @@ static void linux_acpi_shutdown ( int booting __unused ) {
|
||||||
|
|
||||||
list_for_each_entry_safe ( table, tmp, &linux_acpi_tables, list ) {
|
list_for_each_entry_safe ( table, tmp, &linux_acpi_tables, list ) {
|
||||||
list_del ( &table->list );
|
list_del ( &table->list );
|
||||||
free ( table->data );
|
ufree ( table->data );
|
||||||
free ( table );
|
free ( table );
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in New Issue