From 614c3f43a12cd6be6d79d7a8a63cfa603ff9d49f Mon Sep 17 00:00:00 2001 From: Michael Brown Date: Wed, 23 Mar 2022 14:57:53 +0000 Subject: [acpi] Add MAC address extraction self-tests Signed-off-by: Michael Brown --- src/tests/acpi_test.c | 238 ++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 238 insertions(+) create mode 100644 src/tests/acpi_test.c (limited to 'src/tests/acpi_test.c') diff --git a/src/tests/acpi_test.c b/src/tests/acpi_test.c new file mode 100644 index 000000000..972067ee2 --- /dev/null +++ b/src/tests/acpi_test.c @@ -0,0 +1,238 @@ +/* + * Copyright (C) 2022 Michael Brown . + * + * 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. + * + * You can also choose to distribute this program under the terms of + * the Unmodified Binary Distribution Licence (as given in the file + * COPYING.UBDL), provided that you have satisfied its requirements. + */ + +FILE_LICENCE ( GPL2_OR_LATER_OR_UBDL ); + +/** @file + * + * ACPI tests + * + */ + +/* Forcibly enable assertions */ +#undef NDEBUG + +#include +#include +#include +#include + +/** An ACPI test table signature */ +union acpi_test_signature { + /** String */ + char str[4]; + /** Raw value */ + uint32_t raw; +}; + +/** An ACPI test table */ +struct acpi_test_table { + /** Signature */ + union acpi_test_signature signature; + /** Table content */ + const void *data; +}; + +/** An ACPI test table set */ +struct acpi_test_tables { + /** Tables */ + struct acpi_test_table **table; + /** Number of tables */ + unsigned int count; +}; + +/** An ACPI MAC extraction test */ +struct acpi_mac_test { + /** ACPI test table set */ + struct acpi_test_tables *tables; + /** Expected MAC address */ + uint8_t expected[ETH_ALEN]; +}; + +/** Define inline data */ +#define DATA(...) { __VA_ARGS__ } + +/** Define an ACPI test table */ +#define ACPI_TABLE( name, SIGNATURE, DATA ) \ + static const uint8_t name ## _data[] = DATA; \ + static struct acpi_test_table name = { \ + .signature = { \ + .str = SIGNATURE, \ + }, \ + .data = name ## _data, \ + } + +/** Define an ACPI test table set */ +#define ACPI_TABLES( name, ... ) \ + static struct acpi_test_table * name ## _table[] = \ + { __VA_ARGS__ }; \ + static struct acpi_test_tables name = { \ + .table = name ## _table, \ + .count = ( sizeof ( name ## _table ) / \ + sizeof ( name ## _table[0] ) ), \ + } + +/** Define an ACPI MAC extraction test */ +#define ACPI_MAC( name, TABLES, EXPECTED ) \ + static struct acpi_mac_test name = { \ + .tables = TABLES, \ + .expected = EXPECTED, \ + } + +/** "AMAC" SSDT + * + * DefinitionBlock ("", "SSDT", 2, "", "", 0x0) { + * Scope (\_SB) { + * Method (HW00, 0, Serialized) { Return(0) } + * Method (AMAC, 0, Serialized) { ToString("_AUXMAC_#525400aabbcc#") } + * Method (HW42, 0, Serialized) { Return(42) } + * } + * } + */ +ACPI_TABLE ( amac_ssdt, "SSDT", + DATA ( 0x53, 0x53, 0x44, 0x54, 0x5d, 0x00, 0x00, 0x00, 0x02, + 0x89, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, + 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, + 0x00, 0x49, 0x4e, 0x54, 0x4c, 0x04, 0x06, 0x21, 0x20, + 0x10, 0x38, 0x5c, 0x5f, 0x53, 0x42, 0x5f, 0x14, 0x08, + 0x48, 0x57, 0x30, 0x30, 0x08, 0xa4, 0x00, 0x14, 0x1e, + 0x41, 0x4d, 0x41, 0x43, 0x08, 0x0d, 0x5f, 0x41, 0x55, + 0x58, 0x4d, 0x41, 0x43, 0x5f, 0x23, 0x35, 0x32, 0x35, + 0x34, 0x30, 0x30, 0x61, 0x61, 0x62, 0x62, 0x63, 0x63, + 0x23, 0x00, 0x14, 0x09, 0x48, 0x57, 0x34, 0x32, 0x08, + 0xa4, 0x0a, 0x2a ) ); + +/** "AMAC" test tables */ +ACPI_TABLES ( amac_tables, &amac_ssdt ); + +/** "AMAC" test */ +ACPI_MAC ( amac, &amac_tables, + DATA ( 0x52, 0x54, 0x00, 0xaa, 0xbb, 0xcc ) ); + +/** "MACA" SSDT1 (does not contain AUXMAC) */ +ACPI_TABLE ( maca_ssdt1, "SSDT", + DATA ( 0x53, 0x53, 0x44, 0x54, 0x3e, 0x00, 0x00, 0x00, 0x02, + 0x5f, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, + 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, + 0x00, 0x49, 0x4e, 0x54, 0x4c, 0x04, 0x06, 0x21, 0x20, + 0x10, 0x19, 0x5c, 0x5f, 0x53, 0x42, 0x5f, 0x14, 0x08, + 0x48, 0x57, 0x30, 0x30, 0x08, 0xa4, 0x00, 0x14, 0x09, + 0x48, 0x57, 0x34, 0x32, 0x08, 0xa4, 0x0a, 0x2a ) ); + +/** "MACA" SSDT2 (contains AUXMAC) */ +ACPI_TABLE ( maca_ssdt2, "SSDT", + DATA ( 0x53, 0x53, 0x44, 0x54, 0x54, 0x00, 0x00, 0x00, 0x02, + 0x3d, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, + 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, + 0x00, 0x49, 0x4e, 0x54, 0x4c, 0x04, 0x06, 0x21, 0x20, + 0x10, 0x2f, 0x5c, 0x5f, 0x53, 0x42, 0x5f, 0x14, 0x1e, + 0x4d, 0x41, 0x43, 0x41, 0x08, 0x0d, 0x5f, 0x41, 0x55, + 0x58, 0x4d, 0x41, 0x43, 0x5f, 0x23, 0x35, 0x32, 0x35, + 0x34, 0x30, 0x30, 0x31, 0x31, 0x32, 0x32, 0x33, 0x33, + 0x23, 0x00, 0x14, 0x09, 0x48, 0x57, 0x39, 0x39, 0x08, + 0xa4, 0x0a, 0x63 ) ); + +/** "MACA" test tables */ +ACPI_TABLES ( maca_tables, &maca_ssdt1, &maca_ssdt2 ); + +/** "MACA" test */ +ACPI_MAC ( maca, &maca_tables, + DATA ( 0x52, 0x54, 0x00, 0x11, 0x22, 0x33 ) ); + +/** Current ACPI test table set */ +static struct acpi_test_tables *acpi_test_tables; + +/** + * Locate ACPI test table + * + * @v signature Requested table signature + * @v index Requested index of table with this signature + * @ret table Table, or UNULL if not found + */ +static userptr_t acpi_test_find ( uint32_t signature, unsigned int index ) { + struct acpi_test_table *table; + unsigned int i; + + /* Fail if no test tables are installed */ + if ( ! acpi_test_tables ) + return UNULL; + + /* Scan through test tables */ + for ( i = 0 ; i < acpi_test_tables->count ; i++ ) { + table = acpi_test_tables->table[i]; + if ( ( signature == le32_to_cpu ( table->signature.raw ) ) && + ( index-- == 0 ) ) { + return virt_to_user ( table->data ); + } + } + + return UNULL; +} + +/** Override ACPI table finder */ +typeof ( acpi_find ) *acpi_finder = acpi_test_find; + +/** + * Report ACPI MAC extraction test result + * + * @v test ACPI MAC extraction test + * @v file Test code file + * @v line Test code line + */ +static void acpi_mac_okx ( struct acpi_mac_test *test, + const char *file, unsigned int line ) { + uint8_t mac[ETH_ALEN]; + int rc; + + /* Set test table set */ + acpi_test_tables = test->tables; + + /* Extract MAC address */ + rc = acpi_mac ( mac ); + okx ( rc == 0, file, line ); + + /* Check extracted MAC address */ + okx ( memcmp ( mac, test->expected, ETH_ALEN ) == 0, file, line ); + + /* Clear test table set */ + acpi_test_tables = NULL; +} +#define acpi_mac_ok( test ) \ + acpi_mac_okx ( test, __FILE__, __LINE__ ) + +/** + * Perform ACPI self-test + * + */ +static void acpi_test_exec ( void ) { + + /* MAC extraction tests */ + acpi_mac_ok ( &amac ); + acpi_mac_ok ( &maca ); +} + +/** ACPI self-test */ +struct self_test acpi_test __self_test = { + .name = "acpi", + .exec = acpi_test_exec, +}; -- cgit v1.2.3-55-g7522 From f58b5109f46088bdbb5345a9d94b636c54345bdf Mon Sep 17 00:00:00 2001 From: Michael Brown Date: Wed, 23 Mar 2022 15:02:17 +0000 Subject: [acpi] Support the "_RTXMAC_" format for ACPI-based MAC addresses Some newer HP products expose the host-based MAC (HBMAC) address using an ACPI method named "RTMA" returning a part-binary string of the form "_RTXMAC_##", where "" comprises the raw MAC address bytes. Extend the existing support to handle this format alongside the older "_AUXMAC_" format (which uses a base16-encoded MAC address). Reported-by: Andreas Hammarskjöld Tested-by: Andreas Hammarskjöld Signed-off-by: Michael Brown --- src/core/acpimac.c | 153 +++++++++++++++++++++++++++++++++++++++++--------- src/tests/acpi_test.c | 19 +++++++ 2 files changed, 144 insertions(+), 28 deletions(-) (limited to 'src/tests/acpi_test.c') diff --git a/src/core/acpimac.c b/src/core/acpimac.c index 1cc8220b1..5920480dd 100644 --- a/src/core/acpimac.c +++ b/src/core/acpimac.c @@ -46,11 +46,79 @@ FILE_LICENCE ( GPL2_OR_LATER_OR_UBDL ); /** MACA signature */ #define MACA_SIGNATURE ACPI_SIGNATURE ( 'M', 'A', 'C', 'A' ) -/** Maximum number of bytes to skip after AMAC/MACA signature +/** RTMA signature */ +#define RTMA_SIGNATURE ACPI_SIGNATURE ( 'R', 'T', 'M', 'A' ) + +/** Maximum number of bytes to skip after ACPI signature * * This is entirely empirical. */ -#define AUXMAC_MAX_SKIP 8 +#define ACPIMAC_MAX_SKIP 8 + +/** An ACPI MAC extraction mechanism */ +struct acpimac_extractor { + /** Prefix string */ + const char *prefix; + /** Encoded MAC length */ + size_t len; + /** Decode MAC + * + * @v mac Encoded MAC + * @v hw_addr MAC address to fill in + * @ret rc Return status code + */ + int ( * decode ) ( const char *mac, uint8_t *hw_addr ); +}; + +/** + * Decode Base16-encoded MAC address + * + * @v mac Encoded MAC + * @v hw_addr MAC address to fill in + * @ret rc Return status code + */ +static int acpimac_decode_base16 ( const char *mac, uint8_t *hw_addr ) { + int len; + int rc; + + /* Attempt to base16-decode MAC address */ + len = base16_decode ( mac, hw_addr, ETH_ALEN ); + if ( len < 0 ) { + rc = len; + DBGC ( colour, "ACPI could not decode base16 MAC \"%s\": %s\n", + mac, strerror ( rc ) ); + return rc; + } + + return 0; +} + +/** + * Decode raw MAC address + * + * @v mac Encoded MAC + * @v hw_addr MAC address to fill in + * @ret rc Return status code + */ +static int acpimac_decode_raw ( const char *mac, uint8_t *hw_addr ) { + + memcpy ( hw_addr, mac, ETH_ALEN ); + return 0; +} + +/** "_AUXMAC_" extraction mechanism */ +static struct acpimac_extractor acpimac_auxmac = { + .prefix = "_AUXMAC_#", + .len = ( ETH_ALEN * 2 ), + .decode = acpimac_decode_base16, +}; + +/** "_RTXMAC_" extraction mechanism */ +static struct acpimac_extractor acpimac_rtxmac = { + .prefix = "_RTXMAC_#", + .len = ETH_ALEN, + .decode = acpimac_decode_raw, +}; /** * Extract MAC address from DSDT/SSDT @@ -59,6 +127,7 @@ FILE_LICENCE ( GPL2_OR_LATER_OR_UBDL ); * @v len Length of DSDT/SSDT * @v offset Offset of signature within DSDT/SSDT * @v data Data buffer + * @v extractor ACPI MAC address extractor * @ret rc Return status code * * Some vendors provide a "system MAC address" within the DSDT/SSDT, @@ -72,51 +141,44 @@ FILE_LICENCE ( GPL2_OR_LATER_OR_UBDL ); * string that appears shortly after an "AMAC" or "MACA" signature. * This should work for most implementations encountered in practice. */ -static int acpi_extract_mac ( userptr_t zsdt, size_t len, size_t offset, - void *data ) { - static const char prefix[9] = "_AUXMAC_#"; +static int acpimac_extract ( userptr_t zsdt, size_t len, size_t offset, + void *data, struct acpimac_extractor *extractor ){ + size_t prefix_len = strlen ( extractor->prefix ); uint8_t *hw_addr = data; size_t skip = 0; - char auxmac[ sizeof ( prefix ) /* "_AUXMAC_#" */ + - ( ETH_ALEN * 2 ) /* MAC */ + 1 /* "#" */ + 1 /* NUL */ ]; - char *mac = &auxmac[ sizeof ( prefix ) ]; - int decoded_len; + char buf[ prefix_len + extractor->len + 1 /* "#" */ + 1 /* NUL */ ]; + char *mac = &buf[prefix_len]; int rc; /* Skip signature and at least one tag byte */ offset += ( 4 /* signature */ + 1 /* tag byte */ ); - /* Scan for "_AUXMAC_#.....#" close to signature */ + /* Scan for suitable string close to signature */ for ( skip = 0 ; - ( ( skip < AUXMAC_MAX_SKIP ) && - ( offset + skip + sizeof ( auxmac ) ) < len ) ; + ( ( skip < ACPIMAC_MAX_SKIP ) && + ( offset + skip + sizeof ( buf ) ) <= len ) ; skip++ ) { /* Read value */ - copy_from_user ( auxmac, zsdt, ( offset + skip ), - sizeof ( auxmac ) ); + copy_from_user ( buf, zsdt, ( offset + skip ), + sizeof ( buf ) ); /* Check for expected format */ - if ( memcmp ( auxmac, prefix, sizeof ( prefix ) ) != 0 ) + if ( memcmp ( buf, extractor->prefix, prefix_len ) != 0 ) continue; - if ( auxmac[ sizeof ( auxmac ) - 2 ] != '#' ) + if ( buf[ sizeof ( buf ) - 2 ] != '#' ) continue; - if ( auxmac[ sizeof ( auxmac ) - 1 ] != '\0' ) + if ( buf[ sizeof ( buf ) - 1 ] != '\0' ) continue; - DBGC ( colour, "ACPI found MAC string \"%s\"\n", auxmac ); + DBGC ( colour, "ACPI found MAC:\n" ); + DBGC_HDA ( colour, ( offset + skip ), buf, sizeof ( buf ) ); /* Terminate MAC address string */ - mac = &auxmac[ sizeof ( prefix ) ]; - mac[ ETH_ALEN * 2 ] = '\0'; + mac[extractor->len] = '\0'; /* Decode MAC address */ - decoded_len = base16_decode ( mac, hw_addr, ETH_ALEN ); - if ( decoded_len < 0 ) { - rc = decoded_len; - DBGC ( colour, "ACPI could not decode MAC \"%s\": %s\n", - mac, strerror ( rc ) ); + if ( ( rc = extractor->decode ( mac, hw_addr ) ) != 0 ) return rc; - } /* Check MAC address validity */ if ( ! is_valid_ether_addr ( hw_addr ) ) { @@ -131,6 +193,36 @@ static int acpi_extract_mac ( userptr_t zsdt, size_t len, size_t offset, return -ENOENT; } +/** + * Extract "_AUXMAC_" MAC address from DSDT/SSDT + * + * @v zsdt DSDT or SSDT + * @v len Length of DSDT/SSDT + * @v offset Offset of signature within DSDT/SSDT + * @v data Data buffer + * @ret rc Return status code + */ +static int acpimac_extract_auxmac ( userptr_t zsdt, size_t len, size_t offset, + void *data ) { + + return acpimac_extract ( zsdt, len, offset, data, &acpimac_auxmac ); +} + +/** + * Extract "_RTXMAC_" MAC address from DSDT/SSDT + * + * @v zsdt DSDT or SSDT + * @v len Length of DSDT/SSDT + * @v offset Offset of signature within DSDT/SSDT + * @v data Data buffer + * @ret rc Return status code + */ +static int acpimac_extract_rtxmac ( userptr_t zsdt, size_t len, size_t offset, + void *data ) { + + return acpimac_extract ( zsdt, len, offset, data, &acpimac_rtxmac ); +} + /** * Extract MAC address from DSDT/SSDT * @@ -142,12 +234,17 @@ int acpi_mac ( uint8_t *hw_addr ) { /* Look for an "AMAC" address */ if ( ( rc = acpi_extract ( AMAC_SIGNATURE, hw_addr, - acpi_extract_mac ) ) == 0 ) + acpimac_extract_auxmac ) ) == 0 ) return 0; /* Look for a "MACA" address */ if ( ( rc = acpi_extract ( MACA_SIGNATURE, hw_addr, - acpi_extract_mac ) ) == 0 ) + acpimac_extract_auxmac ) ) == 0 ) + return 0; + + /* Look for a "RTMA" address */ + if ( ( rc = acpi_extract ( RTMA_SIGNATURE, hw_addr, + acpimac_extract_rtxmac ) ) == 0 ) return 0; return -ENOENT; diff --git a/src/tests/acpi_test.c b/src/tests/acpi_test.c index 972067ee2..1ca5befaf 100644 --- a/src/tests/acpi_test.c +++ b/src/tests/acpi_test.c @@ -159,6 +159,24 @@ ACPI_TABLES ( maca_tables, &maca_ssdt1, &maca_ssdt2 ); ACPI_MAC ( maca, &maca_tables, DATA ( 0x52, 0x54, 0x00, 0x11, 0x22, 0x33 ) ); +/** "RTMA" SSDT */ +ACPI_TABLE ( rtma_ssdt, "SSDT", + DATA ( 0x53, 0x53, 0x44, 0x54, 0x44, 0x00, 0x00, 0x00, 0x02, + 0x6d, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, + 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, + 0x00, 0x49, 0x4e, 0x54, 0x4c, 0x04, 0x06, 0x21, 0x20, + 0x10, 0x1f, 0x5c, 0x5f, 0x53, 0x42, 0x5f, 0x14, 0x18, + 0x52, 0x54, 0x4d, 0x41, 0x08, 0x0d, 0x5f, 0x52, 0x54, + 0x58, 0x4d, 0x41, 0x43, 0x5f, 0x23, 0x52, 0x54, 0x30, + 0x30, 0x30, 0x31, 0x23, 0x00 ) ); + +/** "RTMA" test tables */ +ACPI_TABLES ( rtma_tables, &rtma_ssdt ); + +/** "RTMA" test */ +ACPI_MAC ( rtma, &rtma_tables, + DATA ( 0x52, 0x54, 0x30, 0x30, 0x30, 0x31 ) ); + /** Current ACPI test table set */ static struct acpi_test_tables *acpi_test_tables; @@ -229,6 +247,7 @@ static void acpi_test_exec ( void ) { /* MAC extraction tests */ acpi_mac_ok ( &amac ); acpi_mac_ok ( &maca ); + acpi_mac_ok ( &rtma ); } /** ACPI self-test */ -- cgit v1.2.3-55-g7522