tools: add tool to decode AML files and build an ACPI namespace
This commit is contained in:
72
tools/amldecode/table.c
Normal file
72
tools/amldecode/table.c
Normal file
@@ -0,0 +1,72 @@
|
||||
#include <stdio.h>
|
||||
#include <stdlib.h>
|
||||
#include "table.h"
|
||||
|
||||
enum loader_status acpi_table_load(const char *path, struct acpi_table **out)
|
||||
{
|
||||
FILE *fp = fopen(path, "rb");
|
||||
if (!fp) {
|
||||
return LOADER_IOERR;
|
||||
}
|
||||
|
||||
fseek(fp, 0, SEEK_END);
|
||||
size_t len = ftell(fp);
|
||||
fseek(fp, 0, SEEK_SET);
|
||||
|
||||
unsigned char *buf = malloc(len);
|
||||
if (!buf) {
|
||||
fclose(fp);
|
||||
return LOADER_NOMEM;
|
||||
}
|
||||
|
||||
size_t r = fread(buf, 1, len, fp);
|
||||
if (r != len) {
|
||||
free(buf);
|
||||
fclose(fp);
|
||||
return LOADER_IOERR;
|
||||
}
|
||||
|
||||
fclose(fp);
|
||||
|
||||
size_t sum = 0;
|
||||
for (size_t i = 0; i < len; i++) {
|
||||
sum += buf[i];
|
||||
}
|
||||
|
||||
if ((sum % 0x100) != 0) {
|
||||
free(buf);
|
||||
return LOADER_BADSUM;
|
||||
}
|
||||
|
||||
struct acpi_table *table = (struct acpi_table *)buf;
|
||||
*out = table;
|
||||
|
||||
return LOADER_OK;
|
||||
}
|
||||
|
||||
void acpi_table_destroy(struct acpi_table *table)
|
||||
{
|
||||
free(table);
|
||||
}
|
||||
|
||||
void acpi_table_get_payload(struct acpi_table *table, void **p, size_t *len)
|
||||
{
|
||||
*p = (unsigned char *)table + sizeof *table;
|
||||
*len = table->length;
|
||||
}
|
||||
|
||||
#define STATUS_STRING(code) \
|
||||
case code: \
|
||||
return #code;
|
||||
|
||||
const char *loader_status_string(enum loader_status status)
|
||||
{
|
||||
switch (status) {
|
||||
STATUS_STRING(LOADER_OK)
|
||||
STATUS_STRING(LOADER_IOERR)
|
||||
STATUS_STRING(LOADER_BADSUM)
|
||||
STATUS_STRING(LOADER_NOMEM)
|
||||
default:
|
||||
return "<unknown>";
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user