iPXE
acpi_test.c
Go to the documentation of this file.
1 /*
2  * Copyright (C) 2022 Michael Brown <mbrown@fensystems.co.uk>.
3  *
4  * This program is free software; you can redistribute it and/or
5  * modify it under the terms of the GNU General Public License as
6  * published by the Free Software Foundation; either version 2 of the
7  * License, or any later version.
8  *
9  * This program is distributed in the hope that it will be useful, but
10  * WITHOUT ANY WARRANTY; without even the implied warranty of
11  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
12  * General Public License for more details.
13  *
14  * You should have received a copy of the GNU General Public License
15  * along with this program; if not, write to the Free Software
16  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
17  * 02110-1301, USA.
18  *
19  * You can also choose to distribute this program under the terms of
20  * the Unmodified Binary Distribution Licence (as given in the file
21  * COPYING.UBDL), provided that you have satisfied its requirements.
22  */
23 
24 FILE_LICENCE ( GPL2_OR_LATER_OR_UBDL );
25 
26 /** @file
27  *
28  * ACPI tests
29  *
30  */
31 
32 /* Forcibly enable assertions */
33 #undef NDEBUG
34 
35 #include <string.h>
36 #include <ipxe/acpi.h>
37 #include <ipxe/acpimac.h>
38 #include <ipxe/if_ether.h>
39 #include <ipxe/test.h>
40 
41 /** An ACPI test table signature */
43  /** String */
44  char str[4];
45  /** Raw value */
47 };
48 
49 /** An ACPI test table */
51  /** Signature */
53  /** Table content */
54  const void *data;
55 };
56 
57 /** An ACPI test table set */
59  /** Tables */
61  /** Number of tables */
62  unsigned int count;
63 };
64 
65 /** An ACPI MAC extraction test */
66 struct acpi_mac_test {
67  /** ACPI test table set */
69  /** Expected MAC address */
71 };
72 
73 /** Define inline data */
74 #define DATA(...) { __VA_ARGS__ }
75 
76 /** Define an ACPI test table */
77 #define ACPI_TABLE( name, SIGNATURE, DATA ) \
78  static const uint8_t name ## _data[] = DATA; \
79  static struct acpi_test_table name = { \
80  .signature = { \
81  .str = SIGNATURE, \
82  }, \
83  .data = name ## _data, \
84  }
85 
86 /** Define an ACPI test table set */
87 #define ACPI_TABLES( name, ... ) \
88  static struct acpi_test_table * name ## _table[] = \
89  { __VA_ARGS__ }; \
90  static struct acpi_test_tables name = { \
91  .table = name ## _table, \
92  .count = ( sizeof ( name ## _table ) / \
93  sizeof ( name ## _table[0] ) ), \
94  }
95 
96 /** Define an ACPI MAC extraction test */
97 #define ACPI_MAC( name, TABLES, EXPECTED ) \
98  static struct acpi_mac_test name = { \
99  .tables = TABLES, \
100  .expected = EXPECTED, \
101  }
102 
103 /** "AMAC" SSDT
104  *
105  * DefinitionBlock ("", "SSDT", 2, "", "", 0x0) {
106  * Scope (\_SB) {
107  * Method (HW00, 0, Serialized) { Return(0) }
108  * Method (AMAC, 0, Serialized) { ToString("_AUXMAC_#525400aabbcc#") }
109  * Method (HW42, 0, Serialized) { Return(42) }
110  * }
111  * }
112  */
113 ACPI_TABLE ( amac_ssdt, "SSDT",
114  DATA ( 0x53, 0x53, 0x44, 0x54, 0x5d, 0x00, 0x00, 0x00, 0x02,
115  0x89, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
116  0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
117  0x00, 0x49, 0x4e, 0x54, 0x4c, 0x04, 0x06, 0x21, 0x20,
118  0x10, 0x38, 0x5c, 0x5f, 0x53, 0x42, 0x5f, 0x14, 0x08,
119  0x48, 0x57, 0x30, 0x30, 0x08, 0xa4, 0x00, 0x14, 0x1e,
120  0x41, 0x4d, 0x41, 0x43, 0x08, 0x0d, 0x5f, 0x41, 0x55,
121  0x58, 0x4d, 0x41, 0x43, 0x5f, 0x23, 0x35, 0x32, 0x35,
122  0x34, 0x30, 0x30, 0x61, 0x61, 0x62, 0x62, 0x63, 0x63,
123  0x23, 0x00, 0x14, 0x09, 0x48, 0x57, 0x34, 0x32, 0x08,
124  0xa4, 0x0a, 0x2a ) );
125 
126 /** "AMAC" test tables */
127 ACPI_TABLES ( amac_tables, &amac_ssdt );
128 
129 /** "AMAC" test */
130 ACPI_MAC ( amac, &amac_tables,
131  DATA ( 0x52, 0x54, 0x00, 0xaa, 0xbb, 0xcc ) );
132 
133 /** "MACA" SSDT1 (does not contain AUXMAC) */
134 ACPI_TABLE ( maca_ssdt1, "SSDT",
135  DATA ( 0x53, 0x53, 0x44, 0x54, 0x3e, 0x00, 0x00, 0x00, 0x02,
136  0x5f, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
137  0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
138  0x00, 0x49, 0x4e, 0x54, 0x4c, 0x04, 0x06, 0x21, 0x20,
139  0x10, 0x19, 0x5c, 0x5f, 0x53, 0x42, 0x5f, 0x14, 0x08,
140  0x48, 0x57, 0x30, 0x30, 0x08, 0xa4, 0x00, 0x14, 0x09,
141  0x48, 0x57, 0x34, 0x32, 0x08, 0xa4, 0x0a, 0x2a ) );
142 
143 /** "MACA" SSDT2 (contains AUXMAC) */
144 ACPI_TABLE ( maca_ssdt2, "SSDT",
145  DATA ( 0x53, 0x53, 0x44, 0x54, 0x54, 0x00, 0x00, 0x00, 0x02,
146  0x3d, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
147  0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
148  0x00, 0x49, 0x4e, 0x54, 0x4c, 0x04, 0x06, 0x21, 0x20,
149  0x10, 0x2f, 0x5c, 0x5f, 0x53, 0x42, 0x5f, 0x14, 0x1e,
150  0x4d, 0x41, 0x43, 0x41, 0x08, 0x0d, 0x5f, 0x41, 0x55,
151  0x58, 0x4d, 0x41, 0x43, 0x5f, 0x23, 0x35, 0x32, 0x35,
152  0x34, 0x30, 0x30, 0x31, 0x31, 0x32, 0x32, 0x33, 0x33,
153  0x23, 0x00, 0x14, 0x09, 0x48, 0x57, 0x39, 0x39, 0x08,
154  0xa4, 0x0a, 0x63 ) );
155 
156 /** "MACA" test tables */
157 ACPI_TABLES ( maca_tables, &maca_ssdt1, &maca_ssdt2 );
158 
159 /** "MACA" test */
160 ACPI_MAC ( maca, &maca_tables,
161  DATA ( 0x52, 0x54, 0x00, 0x11, 0x22, 0x33 ) );
162 
163 /** "RTMA" SSDT */
164 ACPI_TABLE ( rtma_ssdt, "SSDT",
165  DATA ( 0x53, 0x53, 0x44, 0x54, 0x44, 0x00, 0x00, 0x00, 0x02,
166  0x6d, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
167  0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
168  0x00, 0x49, 0x4e, 0x54, 0x4c, 0x04, 0x06, 0x21, 0x20,
169  0x10, 0x1f, 0x5c, 0x5f, 0x53, 0x42, 0x5f, 0x14, 0x18,
170  0x52, 0x54, 0x4d, 0x41, 0x08, 0x0d, 0x5f, 0x52, 0x54,
171  0x58, 0x4d, 0x41, 0x43, 0x5f, 0x23, 0x52, 0x54, 0x30,
172  0x30, 0x30, 0x31, 0x23, 0x00 ) );
173 
174 /** "RTMA" test tables */
175 ACPI_TABLES ( rtma_tables, &rtma_ssdt );
176 
177 /** "RTMA" test */
178 ACPI_MAC ( rtma, &rtma_tables,
179  DATA ( 0x52, 0x54, 0x30, 0x30, 0x30, 0x31 ) );
180 
181 /** Current ACPI test table set */
183 
184 /**
185  * Locate ACPI test table
186  *
187  * @v signature Requested table signature
188  * @v index Requested index of table with this signature
189  * @ret table Table, or NULL if not found
190  */
192  unsigned int index ) {
193  struct acpi_test_table *table;
194  unsigned int i;
195 
196  /* Fail if no test tables are installed */
197  if ( ! acpi_test_tables )
198  return NULL;
199 
200  /* Scan through test tables */
201  for ( i = 0 ; i < acpi_test_tables->count ; i++ ) {
202  table = acpi_test_tables->table[i];
203  if ( ( signature == le32_to_cpu ( table->signature.raw ) ) &&
204  ( index-- == 0 ) ) {
205  return table->data;
206  }
207  }
208 
209  return NULL;
210 }
211 
212 /** Override ACPI table finder */
214 
215 /**
216  * Report ACPI MAC extraction test result
217  *
218  * @v test ACPI MAC extraction test
219  * @v file Test code file
220  * @v line Test code line
221  */
222 static void acpi_mac_okx ( struct acpi_mac_test *test,
223  const char *file, unsigned int line ) {
225  int rc;
226 
227  /* Set test table set */
228  acpi_test_tables = test->tables;
229 
230  /* Extract MAC address */
231  rc = acpi_mac ( mac );
232  okx ( rc == 0, file, line );
233 
234  /* Check extracted MAC address */
235  okx ( memcmp ( mac, test->expected, ETH_ALEN ) == 0, file, line );
236 
237  /* Clear test table set */
239 }
240 #define acpi_mac_ok( test ) \
241  acpi_mac_okx ( test, __FILE__, __LINE__ )
242 
243 /**
244  * Perform ACPI self-test
245  *
246  */
247 static void acpi_test_exec ( void ) {
248 
249  /* MAC extraction tests */
250  acpi_mac_ok ( &amac );
251  acpi_mac_ok ( &maca );
252  acpi_mac_ok ( &rtma );
253 }
254 
255 /** ACPI self-test */
256 struct self_test acpi_test __self_test = {
257  .name = "acpi",
258  .exec = acpi_test_exec,
259 };
struct arbelprm_rc_send_wqe rc
Definition: arbel.h:14
static const struct acpi_header * acpi_test_find(uint32_t signature, unsigned int index)
Locate ACPI test table.
Definition: acpi_test.c:191
#define le32_to_cpu(value)
Definition: byteswap.h:113
FILE_LICENCE(GPL2_OR_LATER_OR_UBDL)
const struct acpi_header *(* acpi_finder)(uint32_t signature, unsigned int index)
struct acpi_test_table ** table
Tables.
Definition: acpi_test.c:60
An ACPI MAC extraction test.
Definition: acpi_test.c:66
#define ACPI_MAC(name, TABLES, EXPECTED)
Define an ACPI MAC extraction test.
Definition: acpi_test.c:97
typeof(acpi_find)
Override ACPI table finder.
Definition: acpi_test.c:213
struct self_test acpi_test __self_test
ACPI self-test.
Definition: acpi_test.c:256
long index
Definition: bigint.h:62
Self-test infrastructure.
const char * name
Test set name.
Definition: test.h:17
static acpi_find(uint32_t signature, unsigned int index)
Locate ACPI table.
Definition: rsdp.h:26
uint8_t mac[ETH_ALEN]
MAC address.
Definition: ena.h:24
A self-test set.
Definition: test.h:15
uint8_t expected[ETH_ALEN]
Expected MAC address.
Definition: acpi_test.c:70
An ACPI test table signature.
Definition: acpi_test.c:42
An ACPI test table set.
Definition: acpi_test.c:58
uint32_t raw
Raw value.
Definition: acpi_test.c:46
int acpi_mac(uint8_t *hw_addr)
Extract MAC address from DSDT/SSDT.
Definition: acpimac.c:234
#define okx(success, file, line)
Report test result.
Definition: test.h:44
unsigned int count
Number of tables.
Definition: acpi_test.c:62
#define ACPI_TABLES(name,...)
Define an ACPI test table set.
Definition: acpi_test.c:87
static struct acpi_test_tables * acpi_test_tables
Current ACPI test table set.
Definition: acpi_test.c:182
union acpi_test_signature signature
Signature.
Definition: acpi_test.c:52
ACPI data structures.
struct acpi_test_tables * tables
ACPI test table set.
Definition: acpi_test.c:68
unsigned char uint8_t
Definition: stdint.h:10
#define acpi_mac_ok(test)
Definition: acpi_test.c:240
#define ETH_ALEN
Definition: if_ether.h:8
An ACPI description header.
Definition: acpi.h:179
unsigned int uint32_t
Definition: stdint.h:12
char str[4]
String.
Definition: acpi_test.c:44
ACPI MAC address.
An ACPI test table.
Definition: acpi_test.c:50
#define DATA(...)
Define inline data.
Definition: acpi_test.c:74
u8 signature
CPU signature.
Definition: CIB_PRM.h:35
#define ACPI_TABLE(name, SIGNATURE, DATA)
Define an ACPI test table.
Definition: acpi_test.c:77
int memcmp(const void *first, const void *second, size_t len)
Compare memory regions.
Definition: string.c:114
#define NULL
NULL pointer (VOID *)
Definition: Base.h:321
String functions.
static void acpi_test_exec(void)
Perform ACPI self-test.
Definition: acpi_test.c:247
static int test
Definition: epic100.c:73
const void * data
Table content.
Definition: acpi_test.c:54