oslinuxtbl.c 36 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365
  1. /******************************************************************************
  2. *
  3. * Module Name: oslinuxtbl - Linux OSL for obtaining ACPI tables
  4. *
  5. *****************************************************************************/
  6. /*
  7. * Copyright (C) 2000 - 2016, Intel Corp.
  8. * All rights reserved.
  9. *
  10. * Redistribution and use in source and binary forms, with or without
  11. * modification, are permitted provided that the following conditions
  12. * are met:
  13. * 1. Redistributions of source code must retain the above copyright
  14. * notice, this list of conditions, and the following disclaimer,
  15. * without modification.
  16. * 2. Redistributions in binary form must reproduce at minimum a disclaimer
  17. * substantially similar to the "NO WARRANTY" disclaimer below
  18. * ("Disclaimer") and any redistribution must be conditioned upon
  19. * including a substantially similar Disclaimer requirement for further
  20. * binary redistribution.
  21. * 3. Neither the names of the above-listed copyright holders nor the names
  22. * of any contributors may be used to endorse or promote products derived
  23. * from this software without specific prior written permission.
  24. *
  25. * Alternatively, this software may be distributed under the terms of the
  26. * GNU General Public License ("GPL") version 2 as published by the Free
  27. * Software Foundation.
  28. *
  29. * NO WARRANTY
  30. * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
  31. * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
  32. * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTIBILITY AND FITNESS FOR
  33. * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
  34. * HOLDERS OR CONTRIBUTORS BE LIABLE FOR SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
  35. * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
  36. * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
  37. * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
  38. * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING
  39. * IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
  40. * POSSIBILITY OF SUCH DAMAGES.
  41. */
  42. #include "acpidump.h"
  43. #define _COMPONENT ACPI_OS_SERVICES
  44. ACPI_MODULE_NAME("oslinuxtbl")
  45. #ifndef PATH_MAX
  46. #define PATH_MAX 256
  47. #endif
  48. /* List of information about obtained ACPI tables */
  49. typedef struct osl_table_info {
  50. struct osl_table_info *next;
  51. u32 instance;
  52. char signature[ACPI_NAME_SIZE];
  53. } osl_table_info;
  54. /* Local prototypes */
  55. static acpi_status osl_table_initialize(void);
  56. static acpi_status
  57. osl_table_name_from_file(char *filename, char *signature, u32 *instance);
  58. static acpi_status osl_add_table_to_list(char *signature, u32 instance);
  59. static acpi_status
  60. osl_read_table_from_file(char *filename,
  61. acpi_size file_offset,
  62. char *signature, struct acpi_table_header **table);
  63. static acpi_status
  64. osl_map_table(acpi_size address,
  65. char *signature, struct acpi_table_header **table);
  66. static void osl_unmap_table(struct acpi_table_header *table);
  67. static acpi_physical_address
  68. osl_find_rsdp_via_efi_by_keyword(FILE * file, const char *keyword);
  69. static acpi_physical_address osl_find_rsdp_via_efi(void);
  70. static acpi_status osl_load_rsdp(void);
  71. static acpi_status osl_list_customized_tables(char *directory);
  72. static acpi_status
  73. osl_get_customized_table(char *pathname,
  74. char *signature,
  75. u32 instance,
  76. struct acpi_table_header **table,
  77. acpi_physical_address *address);
  78. static acpi_status osl_list_bios_tables(void);
  79. static acpi_status
  80. osl_get_bios_table(char *signature,
  81. u32 instance,
  82. struct acpi_table_header **table,
  83. acpi_physical_address *address);
  84. static acpi_status osl_get_last_status(acpi_status default_status);
  85. /* File locations */
  86. #define DYNAMIC_TABLE_DIR "/sys/firmware/acpi/tables/dynamic"
  87. #define STATIC_TABLE_DIR "/sys/firmware/acpi/tables"
  88. #define EFI_SYSTAB "/sys/firmware/efi/systab"
  89. /* Should we get dynamically loaded SSDTs from DYNAMIC_TABLE_DIR? */
  90. u8 gbl_dump_dynamic_tables = TRUE;
  91. /* Initialization flags */
  92. u8 gbl_table_list_initialized = FALSE;
  93. /* Local copies of main ACPI tables */
  94. struct acpi_table_rsdp gbl_rsdp;
  95. struct acpi_table_fadt *gbl_fadt = NULL;
  96. struct acpi_table_rsdt *gbl_rsdt = NULL;
  97. struct acpi_table_xsdt *gbl_xsdt = NULL;
  98. /* Table addresses */
  99. acpi_physical_address gbl_fadt_address = 0;
  100. acpi_physical_address gbl_rsdp_address = 0;
  101. /* Revision of RSD PTR */
  102. u8 gbl_revision = 0;
  103. struct osl_table_info *gbl_table_list_head = NULL;
  104. u32 gbl_table_count = 0;
  105. /******************************************************************************
  106. *
  107. * FUNCTION: osl_get_last_status
  108. *
  109. * PARAMETERS: default_status - Default error status to return
  110. *
  111. * RETURN: Status; Converted from errno.
  112. *
  113. * DESCRIPTION: Get last errno and conver it to acpi_status.
  114. *
  115. *****************************************************************************/
  116. static acpi_status osl_get_last_status(acpi_status default_status)
  117. {
  118. switch (errno) {
  119. case EACCES:
  120. case EPERM:
  121. return (AE_ACCESS);
  122. case ENOENT:
  123. return (AE_NOT_FOUND);
  124. case ENOMEM:
  125. return (AE_NO_MEMORY);
  126. default:
  127. return (default_status);
  128. }
  129. }
  130. /******************************************************************************
  131. *
  132. * FUNCTION: acpi_os_get_table_by_address
  133. *
  134. * PARAMETERS: address - Physical address of the ACPI table
  135. * table - Where a pointer to the table is returned
  136. *
  137. * RETURN: Status; Table buffer is returned if AE_OK.
  138. * AE_NOT_FOUND: A valid table was not found at the address
  139. *
  140. * DESCRIPTION: Get an ACPI table via a physical memory address.
  141. *
  142. *****************************************************************************/
  143. acpi_status
  144. acpi_os_get_table_by_address(acpi_physical_address address,
  145. struct acpi_table_header **table)
  146. {
  147. u32 table_length;
  148. struct acpi_table_header *mapped_table;
  149. struct acpi_table_header *local_table = NULL;
  150. acpi_status status = AE_OK;
  151. /* Get main ACPI tables from memory on first invocation of this function */
  152. status = osl_table_initialize();
  153. if (ACPI_FAILURE(status)) {
  154. return (status);
  155. }
  156. /* Map the table and validate it */
  157. status = osl_map_table(address, NULL, &mapped_table);
  158. if (ACPI_FAILURE(status)) {
  159. return (status);
  160. }
  161. /* Copy table to local buffer and return it */
  162. table_length = ap_get_table_length(mapped_table);
  163. if (table_length == 0) {
  164. status = AE_BAD_HEADER;
  165. goto exit;
  166. }
  167. local_table = calloc(1, table_length);
  168. if (!local_table) {
  169. status = AE_NO_MEMORY;
  170. goto exit;
  171. }
  172. memcpy(local_table, mapped_table, table_length);
  173. exit:
  174. osl_unmap_table(mapped_table);
  175. *table = local_table;
  176. return (status);
  177. }
  178. /******************************************************************************
  179. *
  180. * FUNCTION: acpi_os_get_table_by_name
  181. *
  182. * PARAMETERS: signature - ACPI Signature for desired table. Must be
  183. * a null terminated 4-character string.
  184. * instance - Multiple table support for SSDT/UEFI (0...n)
  185. * Must be 0 for other tables.
  186. * table - Where a pointer to the table is returned
  187. * address - Where the table physical address is returned
  188. *
  189. * RETURN: Status; Table buffer and physical address returned if AE_OK.
  190. * AE_LIMIT: Instance is beyond valid limit
  191. * AE_NOT_FOUND: A table with the signature was not found
  192. *
  193. * NOTE: Assumes the input signature is uppercase.
  194. *
  195. *****************************************************************************/
  196. acpi_status
  197. acpi_os_get_table_by_name(char *signature,
  198. u32 instance,
  199. struct acpi_table_header **table,
  200. acpi_physical_address *address)
  201. {
  202. acpi_status status;
  203. /* Get main ACPI tables from memory on first invocation of this function */
  204. status = osl_table_initialize();
  205. if (ACPI_FAILURE(status)) {
  206. return (status);
  207. }
  208. /* Not a main ACPI table, attempt to extract it from the RSDT/XSDT */
  209. if (!gbl_dump_customized_tables) {
  210. /* Attempt to get the table from the memory */
  211. status =
  212. osl_get_bios_table(signature, instance, table, address);
  213. } else {
  214. /* Attempt to get the table from the static directory */
  215. status = osl_get_customized_table(STATIC_TABLE_DIR, signature,
  216. instance, table, address);
  217. }
  218. if (ACPI_FAILURE(status) && status == AE_LIMIT) {
  219. if (gbl_dump_dynamic_tables) {
  220. /* Attempt to get a dynamic table */
  221. status =
  222. osl_get_customized_table(DYNAMIC_TABLE_DIR,
  223. signature, instance, table,
  224. address);
  225. }
  226. }
  227. return (status);
  228. }
  229. /******************************************************************************
  230. *
  231. * FUNCTION: osl_add_table_to_list
  232. *
  233. * PARAMETERS: signature - Table signature
  234. * instance - Table instance
  235. *
  236. * RETURN: Status; Successfully added if AE_OK.
  237. * AE_NO_MEMORY: Memory allocation error
  238. *
  239. * DESCRIPTION: Insert a table structure into OSL table list.
  240. *
  241. *****************************************************************************/
  242. static acpi_status osl_add_table_to_list(char *signature, u32 instance)
  243. {
  244. struct osl_table_info *new_info;
  245. struct osl_table_info *next;
  246. u32 next_instance = 0;
  247. u8 found = FALSE;
  248. new_info = calloc(1, sizeof(struct osl_table_info));
  249. if (!new_info) {
  250. return (AE_NO_MEMORY);
  251. }
  252. ACPI_MOVE_NAME(new_info->signature, signature);
  253. if (!gbl_table_list_head) {
  254. gbl_table_list_head = new_info;
  255. } else {
  256. next = gbl_table_list_head;
  257. while (1) {
  258. if (ACPI_COMPARE_NAME(next->signature, signature)) {
  259. if (next->instance == instance) {
  260. found = TRUE;
  261. }
  262. if (next->instance >= next_instance) {
  263. next_instance = next->instance + 1;
  264. }
  265. }
  266. if (!next->next) {
  267. break;
  268. }
  269. next = next->next;
  270. }
  271. next->next = new_info;
  272. }
  273. if (found) {
  274. if (instance) {
  275. fprintf(stderr,
  276. "%4.4s: Warning unmatched table instance %d, expected %d\n",
  277. signature, instance, next_instance);
  278. }
  279. instance = next_instance;
  280. }
  281. new_info->instance = instance;
  282. gbl_table_count++;
  283. return (AE_OK);
  284. }
  285. /******************************************************************************
  286. *
  287. * FUNCTION: acpi_os_get_table_by_index
  288. *
  289. * PARAMETERS: index - Which table to get
  290. * table - Where a pointer to the table is returned
  291. * instance - Where a pointer to the table instance no. is
  292. * returned
  293. * address - Where the table physical address is returned
  294. *
  295. * RETURN: Status; Table buffer and physical address returned if AE_OK.
  296. * AE_LIMIT: Index is beyond valid limit
  297. *
  298. * DESCRIPTION: Get an ACPI table via an index value (0 through n). Returns
  299. * AE_LIMIT when an invalid index is reached. Index is not
  300. * necessarily an index into the RSDT/XSDT.
  301. *
  302. *****************************************************************************/
  303. acpi_status
  304. acpi_os_get_table_by_index(u32 index,
  305. struct acpi_table_header **table,
  306. u32 *instance, acpi_physical_address *address)
  307. {
  308. struct osl_table_info *info;
  309. acpi_status status;
  310. u32 i;
  311. /* Get main ACPI tables from memory on first invocation of this function */
  312. status = osl_table_initialize();
  313. if (ACPI_FAILURE(status)) {
  314. return (status);
  315. }
  316. /* Validate Index */
  317. if (index >= gbl_table_count) {
  318. return (AE_LIMIT);
  319. }
  320. /* Point to the table list entry specified by the Index argument */
  321. info = gbl_table_list_head;
  322. for (i = 0; i < index; i++) {
  323. info = info->next;
  324. }
  325. /* Now we can just get the table via the signature */
  326. status = acpi_os_get_table_by_name(info->signature, info->instance,
  327. table, address);
  328. if (ACPI_SUCCESS(status)) {
  329. *instance = info->instance;
  330. }
  331. return (status);
  332. }
  333. /******************************************************************************
  334. *
  335. * FUNCTION: osl_find_rsdp_via_efi_by_keyword
  336. *
  337. * PARAMETERS: keyword - Character string indicating ACPI GUID version
  338. * in the EFI table
  339. *
  340. * RETURN: RSDP address if found
  341. *
  342. * DESCRIPTION: Find RSDP address via EFI using keyword indicating the ACPI
  343. * GUID version.
  344. *
  345. *****************************************************************************/
  346. static acpi_physical_address
  347. osl_find_rsdp_via_efi_by_keyword(FILE * file, const char *keyword)
  348. {
  349. char buffer[80];
  350. unsigned long long address = 0;
  351. char format[32];
  352. snprintf(format, 32, "%s=%s", keyword, "%llx");
  353. fseek(file, 0, SEEK_SET);
  354. while (fgets(buffer, 80, file)) {
  355. if (sscanf(buffer, format, &address) == 1) {
  356. break;
  357. }
  358. }
  359. return ((acpi_physical_address)(address));
  360. }
  361. /******************************************************************************
  362. *
  363. * FUNCTION: osl_find_rsdp_via_efi
  364. *
  365. * PARAMETERS: None
  366. *
  367. * RETURN: RSDP address if found
  368. *
  369. * DESCRIPTION: Find RSDP address via EFI.
  370. *
  371. *****************************************************************************/
  372. static acpi_physical_address osl_find_rsdp_via_efi(void)
  373. {
  374. FILE *file;
  375. acpi_physical_address address = 0;
  376. file = fopen(EFI_SYSTAB, "r");
  377. if (file) {
  378. address = osl_find_rsdp_via_efi_by_keyword(file, "ACPI20");
  379. if (!address) {
  380. address =
  381. osl_find_rsdp_via_efi_by_keyword(file, "ACPI");
  382. }
  383. fclose(file);
  384. }
  385. return (address);
  386. }
  387. /******************************************************************************
  388. *
  389. * FUNCTION: osl_load_rsdp
  390. *
  391. * PARAMETERS: None
  392. *
  393. * RETURN: Status
  394. *
  395. * DESCRIPTION: Scan and load RSDP.
  396. *
  397. *****************************************************************************/
  398. static acpi_status osl_load_rsdp(void)
  399. {
  400. struct acpi_table_header *mapped_table;
  401. u8 *rsdp_address;
  402. acpi_physical_address rsdp_base;
  403. acpi_size rsdp_size;
  404. /* Get RSDP from memory */
  405. rsdp_size = sizeof(struct acpi_table_rsdp);
  406. if (gbl_rsdp_base) {
  407. rsdp_base = gbl_rsdp_base;
  408. } else {
  409. rsdp_base = osl_find_rsdp_via_efi();
  410. }
  411. if (!rsdp_base) {
  412. rsdp_base = ACPI_HI_RSDP_WINDOW_BASE;
  413. rsdp_size = ACPI_HI_RSDP_WINDOW_SIZE;
  414. }
  415. rsdp_address = acpi_os_map_memory(rsdp_base, rsdp_size);
  416. if (!rsdp_address) {
  417. return (osl_get_last_status(AE_BAD_ADDRESS));
  418. }
  419. /* Search low memory for the RSDP */
  420. mapped_table = ACPI_CAST_PTR(struct acpi_table_header,
  421. acpi_tb_scan_memory_for_rsdp(rsdp_address,
  422. rsdp_size));
  423. if (!mapped_table) {
  424. acpi_os_unmap_memory(rsdp_address, rsdp_size);
  425. return (AE_NOT_FOUND);
  426. }
  427. gbl_rsdp_address =
  428. rsdp_base + (ACPI_CAST8(mapped_table) - rsdp_address);
  429. memcpy(&gbl_rsdp, mapped_table, sizeof(struct acpi_table_rsdp));
  430. acpi_os_unmap_memory(rsdp_address, rsdp_size);
  431. return (AE_OK);
  432. }
  433. /******************************************************************************
  434. *
  435. * FUNCTION: osl_can_use_xsdt
  436. *
  437. * PARAMETERS: None
  438. *
  439. * RETURN: TRUE if XSDT is allowed to be used.
  440. *
  441. * DESCRIPTION: This function collects logic that can be used to determine if
  442. * XSDT should be used instead of RSDT.
  443. *
  444. *****************************************************************************/
  445. static u8 osl_can_use_xsdt(void)
  446. {
  447. if (gbl_revision && !acpi_gbl_do_not_use_xsdt) {
  448. return (TRUE);
  449. } else {
  450. return (FALSE);
  451. }
  452. }
  453. /******************************************************************************
  454. *
  455. * FUNCTION: osl_table_initialize
  456. *
  457. * PARAMETERS: None
  458. *
  459. * RETURN: Status
  460. *
  461. * DESCRIPTION: Initialize ACPI table data. Get and store main ACPI tables to
  462. * local variables. Main ACPI tables include RSDT, FADT, RSDT,
  463. * and/or XSDT.
  464. *
  465. *****************************************************************************/
  466. static acpi_status osl_table_initialize(void)
  467. {
  468. acpi_status status;
  469. acpi_physical_address address;
  470. if (gbl_table_list_initialized) {
  471. return (AE_OK);
  472. }
  473. if (!gbl_dump_customized_tables) {
  474. /* Get RSDP from memory */
  475. status = osl_load_rsdp();
  476. if (ACPI_FAILURE(status)) {
  477. return (status);
  478. }
  479. /* Get XSDT from memory */
  480. if (gbl_rsdp.revision && !gbl_do_not_dump_xsdt) {
  481. if (gbl_xsdt) {
  482. free(gbl_xsdt);
  483. gbl_xsdt = NULL;
  484. }
  485. gbl_revision = 2;
  486. status = osl_get_bios_table(ACPI_SIG_XSDT, 0,
  487. ACPI_CAST_PTR(struct
  488. acpi_table_header
  489. *, &gbl_xsdt),
  490. &address);
  491. if (ACPI_FAILURE(status)) {
  492. return (status);
  493. }
  494. }
  495. /* Get RSDT from memory */
  496. if (gbl_rsdp.rsdt_physical_address) {
  497. if (gbl_rsdt) {
  498. free(gbl_rsdt);
  499. gbl_rsdt = NULL;
  500. }
  501. status = osl_get_bios_table(ACPI_SIG_RSDT, 0,
  502. ACPI_CAST_PTR(struct
  503. acpi_table_header
  504. *, &gbl_rsdt),
  505. &address);
  506. if (ACPI_FAILURE(status)) {
  507. return (status);
  508. }
  509. }
  510. /* Get FADT from memory */
  511. if (gbl_fadt) {
  512. free(gbl_fadt);
  513. gbl_fadt = NULL;
  514. }
  515. status = osl_get_bios_table(ACPI_SIG_FADT, 0,
  516. ACPI_CAST_PTR(struct
  517. acpi_table_header *,
  518. &gbl_fadt),
  519. &gbl_fadt_address);
  520. if (ACPI_FAILURE(status)) {
  521. return (status);
  522. }
  523. /* Add mandatory tables to global table list first */
  524. status = osl_add_table_to_list(ACPI_RSDP_NAME, 0);
  525. if (ACPI_FAILURE(status)) {
  526. return (status);
  527. }
  528. status = osl_add_table_to_list(ACPI_SIG_RSDT, 0);
  529. if (ACPI_FAILURE(status)) {
  530. return (status);
  531. }
  532. if (gbl_revision == 2) {
  533. status = osl_add_table_to_list(ACPI_SIG_XSDT, 0);
  534. if (ACPI_FAILURE(status)) {
  535. return (status);
  536. }
  537. }
  538. status = osl_add_table_to_list(ACPI_SIG_DSDT, 0);
  539. if (ACPI_FAILURE(status)) {
  540. return (status);
  541. }
  542. status = osl_add_table_to_list(ACPI_SIG_FACS, 0);
  543. if (ACPI_FAILURE(status)) {
  544. return (status);
  545. }
  546. /* Add all tables found in the memory */
  547. status = osl_list_bios_tables();
  548. if (ACPI_FAILURE(status)) {
  549. return (status);
  550. }
  551. } else {
  552. /* Add all tables found in the static directory */
  553. status = osl_list_customized_tables(STATIC_TABLE_DIR);
  554. if (ACPI_FAILURE(status)) {
  555. return (status);
  556. }
  557. }
  558. if (gbl_dump_dynamic_tables) {
  559. /* Add all dynamically loaded tables in the dynamic directory */
  560. status = osl_list_customized_tables(DYNAMIC_TABLE_DIR);
  561. if (ACPI_FAILURE(status)) {
  562. return (status);
  563. }
  564. }
  565. gbl_table_list_initialized = TRUE;
  566. return (AE_OK);
  567. }
  568. /******************************************************************************
  569. *
  570. * FUNCTION: osl_list_bios_tables
  571. *
  572. * PARAMETERS: None
  573. *
  574. * RETURN: Status; Table list is initialized if AE_OK.
  575. *
  576. * DESCRIPTION: Add ACPI tables to the table list from memory.
  577. *
  578. * NOTE: This works on Linux as table customization does not modify the
  579. * addresses stored in RSDP/RSDT/XSDT/FADT.
  580. *
  581. *****************************************************************************/
  582. static acpi_status osl_list_bios_tables(void)
  583. {
  584. struct acpi_table_header *mapped_table = NULL;
  585. u8 *table_data;
  586. u8 number_of_tables;
  587. u8 item_size;
  588. acpi_physical_address table_address = 0;
  589. acpi_status status = AE_OK;
  590. u32 i;
  591. if (osl_can_use_xsdt()) {
  592. item_size = sizeof(u64);
  593. table_data =
  594. ACPI_CAST8(gbl_xsdt) + sizeof(struct acpi_table_header);
  595. number_of_tables =
  596. (u8)((gbl_xsdt->header.length -
  597. sizeof(struct acpi_table_header))
  598. / item_size);
  599. } else { /* Use RSDT if XSDT is not available */
  600. item_size = sizeof(u32);
  601. table_data =
  602. ACPI_CAST8(gbl_rsdt) + sizeof(struct acpi_table_header);
  603. number_of_tables =
  604. (u8)((gbl_rsdt->header.length -
  605. sizeof(struct acpi_table_header))
  606. / item_size);
  607. }
  608. /* Search RSDT/XSDT for the requested table */
  609. for (i = 0; i < number_of_tables; ++i, table_data += item_size) {
  610. if (osl_can_use_xsdt()) {
  611. table_address =
  612. (acpi_physical_address)(*ACPI_CAST64(table_data));
  613. } else {
  614. table_address =
  615. (acpi_physical_address)(*ACPI_CAST32(table_data));
  616. }
  617. /* Skip NULL entries in RSDT/XSDT */
  618. if (!table_address) {
  619. continue;
  620. }
  621. status = osl_map_table(table_address, NULL, &mapped_table);
  622. if (ACPI_FAILURE(status)) {
  623. return (status);
  624. }
  625. osl_add_table_to_list(mapped_table->signature, 0);
  626. osl_unmap_table(mapped_table);
  627. }
  628. return (AE_OK);
  629. }
  630. /******************************************************************************
  631. *
  632. * FUNCTION: osl_get_bios_table
  633. *
  634. * PARAMETERS: signature - ACPI Signature for common table. Must be
  635. * a null terminated 4-character string.
  636. * instance - Multiple table support for SSDT/UEFI (0...n)
  637. * Must be 0 for other tables.
  638. * table - Where a pointer to the table is returned
  639. * address - Where the table physical address is returned
  640. *
  641. * RETURN: Status; Table buffer and physical address returned if AE_OK.
  642. * AE_LIMIT: Instance is beyond valid limit
  643. * AE_NOT_FOUND: A table with the signature was not found
  644. *
  645. * DESCRIPTION: Get a BIOS provided ACPI table
  646. *
  647. * NOTE: Assumes the input signature is uppercase.
  648. *
  649. *****************************************************************************/
  650. static acpi_status
  651. osl_get_bios_table(char *signature,
  652. u32 instance,
  653. struct acpi_table_header **table,
  654. acpi_physical_address *address)
  655. {
  656. struct acpi_table_header *local_table = NULL;
  657. struct acpi_table_header *mapped_table = NULL;
  658. u8 *table_data;
  659. u8 number_of_tables;
  660. u8 item_size;
  661. u32 current_instance = 0;
  662. acpi_physical_address table_address = 0;
  663. u32 table_length = 0;
  664. acpi_status status = AE_OK;
  665. u32 i;
  666. /* Handle special tables whose addresses are not in RSDT/XSDT */
  667. if (ACPI_COMPARE_NAME(signature, ACPI_RSDP_NAME) ||
  668. ACPI_COMPARE_NAME(signature, ACPI_SIG_RSDT) ||
  669. ACPI_COMPARE_NAME(signature, ACPI_SIG_XSDT) ||
  670. ACPI_COMPARE_NAME(signature, ACPI_SIG_DSDT) ||
  671. ACPI_COMPARE_NAME(signature, ACPI_SIG_FACS)) {
  672. if (instance > 0) {
  673. return (AE_LIMIT);
  674. }
  675. /*
  676. * Get the appropriate address, either 32-bit or 64-bit. Be very
  677. * careful about the FADT length and validate table addresses.
  678. * Note: The 64-bit addresses have priority.
  679. */
  680. if (ACPI_COMPARE_NAME(signature, ACPI_SIG_DSDT)) {
  681. if ((gbl_fadt->header.length >= MIN_FADT_FOR_XDSDT) &&
  682. gbl_fadt->Xdsdt) {
  683. table_address =
  684. (acpi_physical_address)gbl_fadt->Xdsdt;
  685. } else
  686. if ((gbl_fadt->header.length >= MIN_FADT_FOR_DSDT)
  687. && gbl_fadt->dsdt) {
  688. table_address =
  689. (acpi_physical_address)gbl_fadt->dsdt;
  690. }
  691. } else if (ACPI_COMPARE_NAME(signature, ACPI_SIG_FACS)) {
  692. if ((gbl_fadt->header.length >= MIN_FADT_FOR_XFACS) &&
  693. gbl_fadt->Xfacs) {
  694. table_address =
  695. (acpi_physical_address)gbl_fadt->Xfacs;
  696. } else
  697. if ((gbl_fadt->header.length >= MIN_FADT_FOR_FACS)
  698. && gbl_fadt->facs) {
  699. table_address =
  700. (acpi_physical_address)gbl_fadt->facs;
  701. }
  702. } else if (ACPI_COMPARE_NAME(signature, ACPI_SIG_XSDT)) {
  703. if (!gbl_revision) {
  704. return (AE_BAD_SIGNATURE);
  705. }
  706. table_address =
  707. (acpi_physical_address)gbl_rsdp.
  708. xsdt_physical_address;
  709. } else if (ACPI_COMPARE_NAME(signature, ACPI_SIG_RSDT)) {
  710. table_address =
  711. (acpi_physical_address)gbl_rsdp.
  712. rsdt_physical_address;
  713. } else {
  714. table_address = (acpi_physical_address)gbl_rsdp_address;
  715. signature = ACPI_SIG_RSDP;
  716. }
  717. /* Now we can get the requested special table */
  718. status = osl_map_table(table_address, signature, &mapped_table);
  719. if (ACPI_FAILURE(status)) {
  720. return (status);
  721. }
  722. table_length = ap_get_table_length(mapped_table);
  723. } else { /* Case for a normal ACPI table */
  724. if (osl_can_use_xsdt()) {
  725. item_size = sizeof(u64);
  726. table_data =
  727. ACPI_CAST8(gbl_xsdt) +
  728. sizeof(struct acpi_table_header);
  729. number_of_tables =
  730. (u8)((gbl_xsdt->header.length -
  731. sizeof(struct acpi_table_header))
  732. / item_size);
  733. } else { /* Use RSDT if XSDT is not available */
  734. item_size = sizeof(u32);
  735. table_data =
  736. ACPI_CAST8(gbl_rsdt) +
  737. sizeof(struct acpi_table_header);
  738. number_of_tables =
  739. (u8)((gbl_rsdt->header.length -
  740. sizeof(struct acpi_table_header))
  741. / item_size);
  742. }
  743. /* Search RSDT/XSDT for the requested table */
  744. for (i = 0; i < number_of_tables; ++i, table_data += item_size) {
  745. if (osl_can_use_xsdt()) {
  746. table_address =
  747. (acpi_physical_address)(*ACPI_CAST64
  748. (table_data));
  749. } else {
  750. table_address =
  751. (acpi_physical_address)(*ACPI_CAST32
  752. (table_data));
  753. }
  754. /* Skip NULL entries in RSDT/XSDT */
  755. if (!table_address) {
  756. continue;
  757. }
  758. status =
  759. osl_map_table(table_address, NULL, &mapped_table);
  760. if (ACPI_FAILURE(status)) {
  761. return (status);
  762. }
  763. table_length = mapped_table->length;
  764. /* Does this table match the requested signature? */
  765. if (!ACPI_COMPARE_NAME
  766. (mapped_table->signature, signature)) {
  767. osl_unmap_table(mapped_table);
  768. mapped_table = NULL;
  769. continue;
  770. }
  771. /* Match table instance (for SSDT/UEFI tables) */
  772. if (current_instance != instance) {
  773. osl_unmap_table(mapped_table);
  774. mapped_table = NULL;
  775. current_instance++;
  776. continue;
  777. }
  778. break;
  779. }
  780. }
  781. if (!mapped_table) {
  782. return (AE_LIMIT);
  783. }
  784. if (table_length == 0) {
  785. status = AE_BAD_HEADER;
  786. goto exit;
  787. }
  788. /* Copy table to local buffer and return it */
  789. local_table = calloc(1, table_length);
  790. if (!local_table) {
  791. status = AE_NO_MEMORY;
  792. goto exit;
  793. }
  794. memcpy(local_table, mapped_table, table_length);
  795. *address = table_address;
  796. *table = local_table;
  797. exit:
  798. osl_unmap_table(mapped_table);
  799. return (status);
  800. }
  801. /******************************************************************************
  802. *
  803. * FUNCTION: osl_list_customized_tables
  804. *
  805. * PARAMETERS: directory - Directory that contains the tables
  806. *
  807. * RETURN: Status; Table list is initialized if AE_OK.
  808. *
  809. * DESCRIPTION: Add ACPI tables to the table list from a directory.
  810. *
  811. *****************************************************************************/
  812. static acpi_status osl_list_customized_tables(char *directory)
  813. {
  814. void *table_dir;
  815. u32 instance;
  816. char temp_name[ACPI_NAME_SIZE];
  817. char *filename;
  818. acpi_status status = AE_OK;
  819. /* Open the requested directory */
  820. table_dir = acpi_os_open_directory(directory, "*", REQUEST_FILE_ONLY);
  821. if (!table_dir) {
  822. return (osl_get_last_status(AE_NOT_FOUND));
  823. }
  824. /* Examine all entries in this directory */
  825. while ((filename = acpi_os_get_next_filename(table_dir))) {
  826. /* Extract table name and instance number */
  827. status =
  828. osl_table_name_from_file(filename, temp_name, &instance);
  829. /* Ignore meaningless files */
  830. if (ACPI_FAILURE(status)) {
  831. continue;
  832. }
  833. /* Add new info node to global table list */
  834. status = osl_add_table_to_list(temp_name, instance);
  835. if (ACPI_FAILURE(status)) {
  836. break;
  837. }
  838. }
  839. acpi_os_close_directory(table_dir);
  840. return (status);
  841. }
  842. /******************************************************************************
  843. *
  844. * FUNCTION: osl_map_table
  845. *
  846. * PARAMETERS: address - Address of the table in memory
  847. * signature - Optional ACPI Signature for desired table.
  848. * Null terminated 4-character string.
  849. * table - Where a pointer to the mapped table is
  850. * returned
  851. *
  852. * RETURN: Status; Mapped table is returned if AE_OK.
  853. * AE_NOT_FOUND: A valid table was not found at the address
  854. *
  855. * DESCRIPTION: Map entire ACPI table into caller's address space.
  856. *
  857. *****************************************************************************/
  858. static acpi_status
  859. osl_map_table(acpi_size address,
  860. char *signature, struct acpi_table_header **table)
  861. {
  862. struct acpi_table_header *mapped_table;
  863. u32 length;
  864. if (!address) {
  865. return (AE_BAD_ADDRESS);
  866. }
  867. /*
  868. * Map the header so we can get the table length.
  869. * Use sizeof (struct acpi_table_header) as:
  870. * 1. it is bigger than 24 to include RSDP->Length
  871. * 2. it is smaller than sizeof (struct acpi_table_rsdp)
  872. */
  873. mapped_table =
  874. acpi_os_map_memory(address, sizeof(struct acpi_table_header));
  875. if (!mapped_table) {
  876. fprintf(stderr, "Could not map table header at 0x%8.8X%8.8X\n",
  877. ACPI_FORMAT_UINT64(address));
  878. return (osl_get_last_status(AE_BAD_ADDRESS));
  879. }
  880. /* If specified, signature must match */
  881. if (signature) {
  882. if (ACPI_VALIDATE_RSDP_SIG(signature)) {
  883. if (!ACPI_VALIDATE_RSDP_SIG(mapped_table->signature)) {
  884. acpi_os_unmap_memory(mapped_table,
  885. sizeof(struct
  886. acpi_table_header));
  887. return (AE_BAD_SIGNATURE);
  888. }
  889. } else
  890. if (!ACPI_COMPARE_NAME(signature, mapped_table->signature))
  891. {
  892. acpi_os_unmap_memory(mapped_table,
  893. sizeof(struct acpi_table_header));
  894. return (AE_BAD_SIGNATURE);
  895. }
  896. }
  897. /* Map the entire table */
  898. length = ap_get_table_length(mapped_table);
  899. acpi_os_unmap_memory(mapped_table, sizeof(struct acpi_table_header));
  900. if (length == 0) {
  901. return (AE_BAD_HEADER);
  902. }
  903. mapped_table = acpi_os_map_memory(address, length);
  904. if (!mapped_table) {
  905. fprintf(stderr,
  906. "Could not map table at 0x%8.8X%8.8X length %8.8X\n",
  907. ACPI_FORMAT_UINT64(address), length);
  908. return (osl_get_last_status(AE_INVALID_TABLE_LENGTH));
  909. }
  910. (void)ap_is_valid_checksum(mapped_table);
  911. *table = mapped_table;
  912. return (AE_OK);
  913. }
  914. /******************************************************************************
  915. *
  916. * FUNCTION: osl_unmap_table
  917. *
  918. * PARAMETERS: table - A pointer to the mapped table
  919. *
  920. * RETURN: None
  921. *
  922. * DESCRIPTION: Unmap entire ACPI table.
  923. *
  924. *****************************************************************************/
  925. static void osl_unmap_table(struct acpi_table_header *table)
  926. {
  927. if (table) {
  928. acpi_os_unmap_memory(table, ap_get_table_length(table));
  929. }
  930. }
  931. /******************************************************************************
  932. *
  933. * FUNCTION: osl_table_name_from_file
  934. *
  935. * PARAMETERS: filename - File that contains the desired table
  936. * signature - Pointer to 4-character buffer to store
  937. * extracted table signature.
  938. * instance - Pointer to integer to store extracted
  939. * table instance number.
  940. *
  941. * RETURN: Status; Table name is extracted if AE_OK.
  942. *
  943. * DESCRIPTION: Extract table signature and instance number from a table file
  944. * name.
  945. *
  946. *****************************************************************************/
  947. static acpi_status
  948. osl_table_name_from_file(char *filename, char *signature, u32 *instance)
  949. {
  950. /* Ignore meaningless files */
  951. if (strlen(filename) < ACPI_NAME_SIZE) {
  952. return (AE_BAD_SIGNATURE);
  953. }
  954. /* Extract instance number */
  955. if (isdigit((int)filename[ACPI_NAME_SIZE])) {
  956. sscanf(&filename[ACPI_NAME_SIZE], "%u", instance);
  957. } else if (strlen(filename) != ACPI_NAME_SIZE) {
  958. return (AE_BAD_SIGNATURE);
  959. } else {
  960. *instance = 0;
  961. }
  962. /* Extract signature */
  963. ACPI_MOVE_NAME(signature, filename);
  964. return (AE_OK);
  965. }
  966. /******************************************************************************
  967. *
  968. * FUNCTION: osl_read_table_from_file
  969. *
  970. * PARAMETERS: filename - File that contains the desired table
  971. * file_offset - Offset of the table in file
  972. * signature - Optional ACPI Signature for desired table.
  973. * A null terminated 4-character string.
  974. * table - Where a pointer to the table is returned
  975. *
  976. * RETURN: Status; Table buffer is returned if AE_OK.
  977. *
  978. * DESCRIPTION: Read a ACPI table from a file.
  979. *
  980. *****************************************************************************/
  981. static acpi_status
  982. osl_read_table_from_file(char *filename,
  983. acpi_size file_offset,
  984. char *signature, struct acpi_table_header **table)
  985. {
  986. FILE *table_file;
  987. struct acpi_table_header header;
  988. struct acpi_table_header *local_table = NULL;
  989. u32 table_length;
  990. s32 count;
  991. acpi_status status = AE_OK;
  992. /* Open the file */
  993. table_file = fopen(filename, "rb");
  994. if (table_file == NULL) {
  995. fprintf(stderr, "Could not open table file: %s\n", filename);
  996. return (osl_get_last_status(AE_NOT_FOUND));
  997. }
  998. fseek(table_file, file_offset, SEEK_SET);
  999. /* Read the Table header to get the table length */
  1000. count = fread(&header, 1, sizeof(struct acpi_table_header), table_file);
  1001. if (count != sizeof(struct acpi_table_header)) {
  1002. fprintf(stderr, "Could not read table header: %s\n", filename);
  1003. status = AE_BAD_HEADER;
  1004. goto exit;
  1005. }
  1006. /* If signature is specified, it must match the table */
  1007. if (signature) {
  1008. if (ACPI_VALIDATE_RSDP_SIG(signature)) {
  1009. if (!ACPI_VALIDATE_RSDP_SIG(header.signature)) {
  1010. fprintf(stderr,
  1011. "Incorrect RSDP signature: found %8.8s\n",
  1012. header.signature);
  1013. status = AE_BAD_SIGNATURE;
  1014. goto exit;
  1015. }
  1016. } else if (!ACPI_COMPARE_NAME(signature, header.signature)) {
  1017. fprintf(stderr,
  1018. "Incorrect signature: Expecting %4.4s, found %4.4s\n",
  1019. signature, header.signature);
  1020. status = AE_BAD_SIGNATURE;
  1021. goto exit;
  1022. }
  1023. }
  1024. table_length = ap_get_table_length(&header);
  1025. if (table_length == 0) {
  1026. status = AE_BAD_HEADER;
  1027. goto exit;
  1028. }
  1029. /* Read the entire table into a local buffer */
  1030. local_table = calloc(1, table_length);
  1031. if (!local_table) {
  1032. fprintf(stderr,
  1033. "%4.4s: Could not allocate buffer for table of length %X\n",
  1034. header.signature, table_length);
  1035. status = AE_NO_MEMORY;
  1036. goto exit;
  1037. }
  1038. fseek(table_file, file_offset, SEEK_SET);
  1039. count = fread(local_table, 1, table_length, table_file);
  1040. if (count != table_length) {
  1041. fprintf(stderr, "%4.4s: Could not read table content\n",
  1042. header.signature);
  1043. status = AE_INVALID_TABLE_LENGTH;
  1044. goto exit;
  1045. }
  1046. /* Validate checksum */
  1047. (void)ap_is_valid_checksum(local_table);
  1048. exit:
  1049. fclose(table_file);
  1050. *table = local_table;
  1051. return (status);
  1052. }
  1053. /******************************************************************************
  1054. *
  1055. * FUNCTION: osl_get_customized_table
  1056. *
  1057. * PARAMETERS: pathname - Directory to find Linux customized table
  1058. * signature - ACPI Signature for desired table. Must be
  1059. * a null terminated 4-character string.
  1060. * instance - Multiple table support for SSDT/UEFI (0...n)
  1061. * Must be 0 for other tables.
  1062. * table - Where a pointer to the table is returned
  1063. * address - Where the table physical address is returned
  1064. *
  1065. * RETURN: Status; Table buffer is returned if AE_OK.
  1066. * AE_LIMIT: Instance is beyond valid limit
  1067. * AE_NOT_FOUND: A table with the signature was not found
  1068. *
  1069. * DESCRIPTION: Get an OS customized table.
  1070. *
  1071. *****************************************************************************/
  1072. static acpi_status
  1073. osl_get_customized_table(char *pathname,
  1074. char *signature,
  1075. u32 instance,
  1076. struct acpi_table_header **table,
  1077. acpi_physical_address *address)
  1078. {
  1079. void *table_dir;
  1080. u32 current_instance = 0;
  1081. char temp_name[ACPI_NAME_SIZE];
  1082. char table_filename[PATH_MAX];
  1083. char *filename;
  1084. acpi_status status;
  1085. /* Open the directory for customized tables */
  1086. table_dir = acpi_os_open_directory(pathname, "*", REQUEST_FILE_ONLY);
  1087. if (!table_dir) {
  1088. return (osl_get_last_status(AE_NOT_FOUND));
  1089. }
  1090. /* Attempt to find the table in the directory */
  1091. while ((filename = acpi_os_get_next_filename(table_dir))) {
  1092. /* Ignore meaningless files */
  1093. if (!ACPI_COMPARE_NAME(filename, signature)) {
  1094. continue;
  1095. }
  1096. /* Extract table name and instance number */
  1097. status =
  1098. osl_table_name_from_file(filename, temp_name,
  1099. &current_instance);
  1100. /* Ignore meaningless files */
  1101. if (ACPI_FAILURE(status) || current_instance != instance) {
  1102. continue;
  1103. }
  1104. /* Create the table pathname */
  1105. if (instance != 0) {
  1106. sprintf(table_filename, "%s/%4.4s%d", pathname,
  1107. temp_name, instance);
  1108. } else {
  1109. sprintf(table_filename, "%s/%4.4s", pathname,
  1110. temp_name);
  1111. }
  1112. break;
  1113. }
  1114. acpi_os_close_directory(table_dir);
  1115. if (!filename) {
  1116. return (AE_LIMIT);
  1117. }
  1118. /* There is no physical address saved for customized tables, use zero */
  1119. *address = 0;
  1120. status = osl_read_table_from_file(table_filename, 0, NULL, table);
  1121. return (status);
  1122. }