oslinuxtbl.c 36 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363
  1. /******************************************************************************
  2. *
  3. * Module Name: oslinuxtbl - Linux OSL for obtaining ACPI tables
  4. *
  5. *****************************************************************************/
  6. /*
  7. * Copyright (C) 2000 - 2015, 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. ACPI_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. ACPI_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. /* Get RSDP from memory */
  474. status = osl_load_rsdp();
  475. if (ACPI_FAILURE(status)) {
  476. return (status);
  477. }
  478. /* Get XSDT from memory */
  479. if (gbl_rsdp.revision && !gbl_do_not_dump_xsdt) {
  480. if (gbl_xsdt) {
  481. free(gbl_xsdt);
  482. gbl_xsdt = NULL;
  483. }
  484. gbl_revision = 2;
  485. status = osl_get_bios_table(ACPI_SIG_XSDT, 0,
  486. ACPI_CAST_PTR(struct
  487. acpi_table_header *,
  488. &gbl_xsdt), &address);
  489. if (ACPI_FAILURE(status)) {
  490. return (status);
  491. }
  492. }
  493. /* Get RSDT from memory */
  494. if (gbl_rsdp.rsdt_physical_address) {
  495. if (gbl_rsdt) {
  496. free(gbl_rsdt);
  497. gbl_rsdt = NULL;
  498. }
  499. status = osl_get_bios_table(ACPI_SIG_RSDT, 0,
  500. ACPI_CAST_PTR(struct
  501. acpi_table_header *,
  502. &gbl_rsdt), &address);
  503. if (ACPI_FAILURE(status)) {
  504. return (status);
  505. }
  506. }
  507. /* Get FADT from memory */
  508. if (gbl_fadt) {
  509. free(gbl_fadt);
  510. gbl_fadt = NULL;
  511. }
  512. status = osl_get_bios_table(ACPI_SIG_FADT, 0,
  513. ACPI_CAST_PTR(struct acpi_table_header *,
  514. &gbl_fadt),
  515. &gbl_fadt_address);
  516. if (ACPI_FAILURE(status)) {
  517. return (status);
  518. }
  519. if (!gbl_dump_customized_tables) {
  520. /* Add mandatory tables to global table list first */
  521. status = osl_add_table_to_list(ACPI_RSDP_NAME, 0);
  522. if (ACPI_FAILURE(status)) {
  523. return (status);
  524. }
  525. status = osl_add_table_to_list(ACPI_SIG_RSDT, 0);
  526. if (ACPI_FAILURE(status)) {
  527. return (status);
  528. }
  529. if (gbl_revision == 2) {
  530. status = osl_add_table_to_list(ACPI_SIG_XSDT, 0);
  531. if (ACPI_FAILURE(status)) {
  532. return (status);
  533. }
  534. }
  535. status = osl_add_table_to_list(ACPI_SIG_DSDT, 0);
  536. if (ACPI_FAILURE(status)) {
  537. return (status);
  538. }
  539. status = osl_add_table_to_list(ACPI_SIG_FACS, 0);
  540. if (ACPI_FAILURE(status)) {
  541. return (status);
  542. }
  543. /* Add all tables found in the memory */
  544. status = osl_list_bios_tables();
  545. if (ACPI_FAILURE(status)) {
  546. return (status);
  547. }
  548. } else {
  549. /* Add all tables found in the static directory */
  550. status = osl_list_customized_tables(STATIC_TABLE_DIR);
  551. if (ACPI_FAILURE(status)) {
  552. return (status);
  553. }
  554. }
  555. if (gbl_dump_dynamic_tables) {
  556. /* Add all dynamically loaded tables in the dynamic directory */
  557. status = osl_list_customized_tables(DYNAMIC_TABLE_DIR);
  558. if (ACPI_FAILURE(status)) {
  559. return (status);
  560. }
  561. }
  562. gbl_table_list_initialized = TRUE;
  563. return (AE_OK);
  564. }
  565. /******************************************************************************
  566. *
  567. * FUNCTION: osl_list_bios_tables
  568. *
  569. * PARAMETERS: None
  570. *
  571. * RETURN: Status; Table list is initialized if AE_OK.
  572. *
  573. * DESCRIPTION: Add ACPI tables to the table list from memory.
  574. *
  575. * NOTE: This works on Linux as table customization does not modify the
  576. * addresses stored in RSDP/RSDT/XSDT/FADT.
  577. *
  578. *****************************************************************************/
  579. static acpi_status osl_list_bios_tables(void)
  580. {
  581. struct acpi_table_header *mapped_table = NULL;
  582. u8 *table_data;
  583. u8 number_of_tables;
  584. u8 item_size;
  585. acpi_physical_address table_address = 0;
  586. acpi_status status = AE_OK;
  587. u32 i;
  588. if (osl_can_use_xsdt()) {
  589. item_size = sizeof(u64);
  590. table_data =
  591. ACPI_CAST8(gbl_xsdt) + sizeof(struct acpi_table_header);
  592. number_of_tables =
  593. (u8)((gbl_xsdt->header.length -
  594. sizeof(struct acpi_table_header))
  595. / item_size);
  596. } else { /* Use RSDT if XSDT is not available */
  597. item_size = sizeof(u32);
  598. table_data =
  599. ACPI_CAST8(gbl_rsdt) + sizeof(struct acpi_table_header);
  600. number_of_tables =
  601. (u8)((gbl_rsdt->header.length -
  602. sizeof(struct acpi_table_header))
  603. / item_size);
  604. }
  605. /* Search RSDT/XSDT for the requested table */
  606. for (i = 0; i < number_of_tables; ++i, table_data += item_size) {
  607. if (osl_can_use_xsdt()) {
  608. table_address =
  609. (acpi_physical_address) (*ACPI_CAST64(table_data));
  610. } else {
  611. table_address =
  612. (acpi_physical_address) (*ACPI_CAST32(table_data));
  613. }
  614. /* Skip NULL entries in RSDT/XSDT */
  615. if (!table_address) {
  616. continue;
  617. }
  618. status = osl_map_table(table_address, NULL, &mapped_table);
  619. if (ACPI_FAILURE(status)) {
  620. return (status);
  621. }
  622. osl_add_table_to_list(mapped_table->signature, 0);
  623. osl_unmap_table(mapped_table);
  624. }
  625. return (AE_OK);
  626. }
  627. /******************************************************************************
  628. *
  629. * FUNCTION: osl_get_bios_table
  630. *
  631. * PARAMETERS: signature - ACPI Signature for common table. Must be
  632. * a null terminated 4-character string.
  633. * instance - Multiple table support for SSDT/UEFI (0...n)
  634. * Must be 0 for other tables.
  635. * table - Where a pointer to the table is returned
  636. * address - Where the table physical address is returned
  637. *
  638. * RETURN: Status; Table buffer and physical address returned if AE_OK.
  639. * AE_LIMIT: Instance is beyond valid limit
  640. * AE_NOT_FOUND: A table with the signature was not found
  641. *
  642. * DESCRIPTION: Get a BIOS provided ACPI table
  643. *
  644. * NOTE: Assumes the input signature is uppercase.
  645. *
  646. *****************************************************************************/
  647. static acpi_status
  648. osl_get_bios_table(char *signature,
  649. u32 instance,
  650. struct acpi_table_header **table,
  651. acpi_physical_address * address)
  652. {
  653. struct acpi_table_header *local_table = NULL;
  654. struct acpi_table_header *mapped_table = NULL;
  655. u8 *table_data;
  656. u8 number_of_tables;
  657. u8 item_size;
  658. u32 current_instance = 0;
  659. acpi_physical_address table_address = 0;
  660. u32 table_length = 0;
  661. acpi_status status = AE_OK;
  662. u32 i;
  663. /* Handle special tables whose addresses are not in RSDT/XSDT */
  664. if (ACPI_COMPARE_NAME(signature, ACPI_RSDP_NAME) ||
  665. ACPI_COMPARE_NAME(signature, ACPI_SIG_RSDT) ||
  666. ACPI_COMPARE_NAME(signature, ACPI_SIG_XSDT) ||
  667. ACPI_COMPARE_NAME(signature, ACPI_SIG_DSDT) ||
  668. ACPI_COMPARE_NAME(signature, ACPI_SIG_FACS)) {
  669. if (instance > 0) {
  670. return (AE_LIMIT);
  671. }
  672. /*
  673. * Get the appropriate address, either 32-bit or 64-bit. Be very
  674. * careful about the FADT length and validate table addresses.
  675. * Note: The 64-bit addresses have priority.
  676. */
  677. if (ACPI_COMPARE_NAME(signature, ACPI_SIG_DSDT)) {
  678. if ((gbl_fadt->header.length >= MIN_FADT_FOR_XDSDT) &&
  679. gbl_fadt->Xdsdt) {
  680. table_address =
  681. (acpi_physical_address) gbl_fadt->Xdsdt;
  682. } else
  683. if ((gbl_fadt->header.length >= MIN_FADT_FOR_DSDT)
  684. && gbl_fadt->dsdt) {
  685. table_address =
  686. (acpi_physical_address) gbl_fadt->dsdt;
  687. }
  688. } else if (ACPI_COMPARE_NAME(signature, ACPI_SIG_FACS)) {
  689. if ((gbl_fadt->header.length >= MIN_FADT_FOR_XFACS) &&
  690. gbl_fadt->Xfacs) {
  691. table_address =
  692. (acpi_physical_address) gbl_fadt->Xfacs;
  693. } else
  694. if ((gbl_fadt->header.length >= MIN_FADT_FOR_FACS)
  695. && gbl_fadt->facs) {
  696. table_address =
  697. (acpi_physical_address) gbl_fadt->facs;
  698. }
  699. } else if (ACPI_COMPARE_NAME(signature, ACPI_SIG_XSDT)) {
  700. if (!gbl_revision) {
  701. return (AE_BAD_SIGNATURE);
  702. }
  703. table_address =
  704. (acpi_physical_address) gbl_rsdp.
  705. xsdt_physical_address;
  706. } else if (ACPI_COMPARE_NAME(signature, ACPI_SIG_RSDT)) {
  707. table_address =
  708. (acpi_physical_address) gbl_rsdp.
  709. rsdt_physical_address;
  710. } else {
  711. table_address =
  712. (acpi_physical_address) gbl_rsdp_address;
  713. signature = ACPI_SIG_RSDP;
  714. }
  715. /* Now we can get the requested special table */
  716. status = osl_map_table(table_address, signature, &mapped_table);
  717. if (ACPI_FAILURE(status)) {
  718. return (status);
  719. }
  720. table_length = ap_get_table_length(mapped_table);
  721. } else { /* Case for a normal ACPI table */
  722. if (osl_can_use_xsdt()) {
  723. item_size = sizeof(u64);
  724. table_data =
  725. ACPI_CAST8(gbl_xsdt) +
  726. sizeof(struct acpi_table_header);
  727. number_of_tables =
  728. (u8)((gbl_xsdt->header.length -
  729. sizeof(struct acpi_table_header))
  730. / item_size);
  731. } else { /* Use RSDT if XSDT is not available */
  732. item_size = sizeof(u32);
  733. table_data =
  734. ACPI_CAST8(gbl_rsdt) +
  735. sizeof(struct acpi_table_header);
  736. number_of_tables =
  737. (u8)((gbl_rsdt->header.length -
  738. sizeof(struct acpi_table_header))
  739. / item_size);
  740. }
  741. /* Search RSDT/XSDT for the requested table */
  742. for (i = 0; i < number_of_tables; ++i, table_data += item_size) {
  743. if (osl_can_use_xsdt()) {
  744. table_address =
  745. (acpi_physical_address) (*ACPI_CAST64
  746. (table_data));
  747. } else {
  748. table_address =
  749. (acpi_physical_address) (*ACPI_CAST32
  750. (table_data));
  751. }
  752. /* Skip NULL entries in RSDT/XSDT */
  753. if (!table_address) {
  754. continue;
  755. }
  756. status =
  757. osl_map_table(table_address, NULL, &mapped_table);
  758. if (ACPI_FAILURE(status)) {
  759. return (status);
  760. }
  761. table_length = mapped_table->length;
  762. /* Does this table match the requested signature? */
  763. if (!ACPI_COMPARE_NAME
  764. (mapped_table->signature, signature)) {
  765. osl_unmap_table(mapped_table);
  766. mapped_table = NULL;
  767. continue;
  768. }
  769. /* Match table instance (for SSDT/UEFI tables) */
  770. if (current_instance != instance) {
  771. osl_unmap_table(mapped_table);
  772. mapped_table = NULL;
  773. current_instance++;
  774. continue;
  775. }
  776. break;
  777. }
  778. }
  779. if (!mapped_table) {
  780. return (AE_LIMIT);
  781. }
  782. if (table_length == 0) {
  783. status = AE_BAD_HEADER;
  784. goto exit;
  785. }
  786. /* Copy table to local buffer and return it */
  787. local_table = calloc(1, table_length);
  788. if (!local_table) {
  789. status = AE_NO_MEMORY;
  790. goto exit;
  791. }
  792. ACPI_MEMCPY(local_table, mapped_table, table_length);
  793. *address = table_address;
  794. *table = local_table;
  795. exit:
  796. osl_unmap_table(mapped_table);
  797. return (status);
  798. }
  799. /******************************************************************************
  800. *
  801. * FUNCTION: osl_list_customized_tables
  802. *
  803. * PARAMETERS: directory - Directory that contains the tables
  804. *
  805. * RETURN: Status; Table list is initialized if AE_OK.
  806. *
  807. * DESCRIPTION: Add ACPI tables to the table list from a directory.
  808. *
  809. *****************************************************************************/
  810. static acpi_status osl_list_customized_tables(char *directory)
  811. {
  812. void *table_dir;
  813. u32 instance;
  814. char temp_name[ACPI_NAME_SIZE];
  815. char *filename;
  816. acpi_status status = AE_OK;
  817. /* Open the requested directory */
  818. table_dir = acpi_os_open_directory(directory, "*", REQUEST_FILE_ONLY);
  819. if (!table_dir) {
  820. return (osl_get_last_status(AE_NOT_FOUND));
  821. }
  822. /* Examine all entries in this directory */
  823. while ((filename = acpi_os_get_next_filename(table_dir))) {
  824. /* Extract table name and instance number */
  825. status =
  826. osl_table_name_from_file(filename, temp_name, &instance);
  827. /* Ignore meaningless files */
  828. if (ACPI_FAILURE(status)) {
  829. continue;
  830. }
  831. /* Add new info node to global table list */
  832. status = osl_add_table_to_list(temp_name, instance);
  833. if (ACPI_FAILURE(status)) {
  834. break;
  835. }
  836. }
  837. acpi_os_close_directory(table_dir);
  838. return (status);
  839. }
  840. /******************************************************************************
  841. *
  842. * FUNCTION: osl_map_table
  843. *
  844. * PARAMETERS: address - Address of the table in memory
  845. * signature - Optional ACPI Signature for desired table.
  846. * Null terminated 4-character string.
  847. * table - Where a pointer to the mapped table is
  848. * returned
  849. *
  850. * RETURN: Status; Mapped table is returned if AE_OK.
  851. * AE_NOT_FOUND: A valid table was not found at the address
  852. *
  853. * DESCRIPTION: Map entire ACPI table into caller's address space.
  854. *
  855. *****************************************************************************/
  856. static acpi_status
  857. osl_map_table(acpi_size address,
  858. char *signature, struct acpi_table_header **table)
  859. {
  860. struct acpi_table_header *mapped_table;
  861. u32 length;
  862. if (!address) {
  863. return (AE_BAD_ADDRESS);
  864. }
  865. /*
  866. * Map the header so we can get the table length.
  867. * Use sizeof (struct acpi_table_header) as:
  868. * 1. it is bigger than 24 to include RSDP->Length
  869. * 2. it is smaller than sizeof (struct acpi_table_rsdp)
  870. */
  871. mapped_table =
  872. acpi_os_map_memory(address, sizeof(struct acpi_table_header));
  873. if (!mapped_table) {
  874. fprintf(stderr, "Could not map table header at 0x%8.8X%8.8X\n",
  875. ACPI_FORMAT_UINT64(address));
  876. return (osl_get_last_status(AE_BAD_ADDRESS));
  877. }
  878. /* If specified, signature must match */
  879. if (signature) {
  880. if (ACPI_VALIDATE_RSDP_SIG(signature)) {
  881. if (!ACPI_VALIDATE_RSDP_SIG(mapped_table->signature)) {
  882. acpi_os_unmap_memory(mapped_table,
  883. sizeof(struct
  884. acpi_table_header));
  885. return (AE_BAD_SIGNATURE);
  886. }
  887. } else
  888. if (!ACPI_COMPARE_NAME(signature, mapped_table->signature))
  889. {
  890. acpi_os_unmap_memory(mapped_table,
  891. sizeof(struct acpi_table_header));
  892. return (AE_BAD_SIGNATURE);
  893. }
  894. }
  895. /* Map the entire table */
  896. length = ap_get_table_length(mapped_table);
  897. acpi_os_unmap_memory(mapped_table, sizeof(struct acpi_table_header));
  898. if (length == 0) {
  899. return (AE_BAD_HEADER);
  900. }
  901. mapped_table = acpi_os_map_memory(address, length);
  902. if (!mapped_table) {
  903. fprintf(stderr,
  904. "Could not map table at 0x%8.8X%8.8X length %8.8X\n",
  905. ACPI_FORMAT_UINT64(address), length);
  906. return (osl_get_last_status(AE_INVALID_TABLE_LENGTH));
  907. }
  908. (void)ap_is_valid_checksum(mapped_table);
  909. *table = mapped_table;
  910. return (AE_OK);
  911. }
  912. /******************************************************************************
  913. *
  914. * FUNCTION: osl_unmap_table
  915. *
  916. * PARAMETERS: table - A pointer to the mapped table
  917. *
  918. * RETURN: None
  919. *
  920. * DESCRIPTION: Unmap entire ACPI table.
  921. *
  922. *****************************************************************************/
  923. static void osl_unmap_table(struct acpi_table_header *table)
  924. {
  925. if (table) {
  926. acpi_os_unmap_memory(table, ap_get_table_length(table));
  927. }
  928. }
  929. /******************************************************************************
  930. *
  931. * FUNCTION: osl_table_name_from_file
  932. *
  933. * PARAMETERS: filename - File that contains the desired table
  934. * signature - Pointer to 4-character buffer to store
  935. * extracted table signature.
  936. * instance - Pointer to integer to store extracted
  937. * table instance number.
  938. *
  939. * RETURN: Status; Table name is extracted if AE_OK.
  940. *
  941. * DESCRIPTION: Extract table signature and instance number from a table file
  942. * name.
  943. *
  944. *****************************************************************************/
  945. static acpi_status
  946. osl_table_name_from_file(char *filename, char *signature, u32 *instance)
  947. {
  948. /* Ignore meaningless files */
  949. if (strlen(filename) < ACPI_NAME_SIZE) {
  950. return (AE_BAD_SIGNATURE);
  951. }
  952. /* Extract instance number */
  953. if (isdigit((int)filename[ACPI_NAME_SIZE])) {
  954. sscanf(&filename[ACPI_NAME_SIZE], "%u", instance);
  955. } else if (strlen(filename) != ACPI_NAME_SIZE) {
  956. return (AE_BAD_SIGNATURE);
  957. } else {
  958. *instance = 0;
  959. }
  960. /* Extract signature */
  961. ACPI_MOVE_NAME(signature, filename);
  962. return (AE_OK);
  963. }
  964. /******************************************************************************
  965. *
  966. * FUNCTION: osl_read_table_from_file
  967. *
  968. * PARAMETERS: filename - File that contains the desired table
  969. * file_offset - Offset of the table in file
  970. * signature - Optional ACPI Signature for desired table.
  971. * A null terminated 4-character string.
  972. * table - Where a pointer to the table is returned
  973. *
  974. * RETURN: Status; Table buffer is returned if AE_OK.
  975. *
  976. * DESCRIPTION: Read a ACPI table from a file.
  977. *
  978. *****************************************************************************/
  979. static acpi_status
  980. osl_read_table_from_file(char *filename,
  981. acpi_size file_offset,
  982. char *signature, struct acpi_table_header **table)
  983. {
  984. FILE *table_file;
  985. struct acpi_table_header header;
  986. struct acpi_table_header *local_table = NULL;
  987. u32 table_length;
  988. s32 count;
  989. acpi_status status = AE_OK;
  990. /* Open the file */
  991. table_file = fopen(filename, "rb");
  992. if (table_file == NULL) {
  993. fprintf(stderr, "Could not open table file: %s\n", filename);
  994. return (osl_get_last_status(AE_NOT_FOUND));
  995. }
  996. fseek(table_file, file_offset, SEEK_SET);
  997. /* Read the Table header to get the table length */
  998. count = fread(&header, 1, sizeof(struct acpi_table_header), table_file);
  999. if (count != sizeof(struct acpi_table_header)) {
  1000. fprintf(stderr, "Could not read table header: %s\n", filename);
  1001. status = AE_BAD_HEADER;
  1002. goto exit;
  1003. }
  1004. /* If signature is specified, it must match the table */
  1005. if (signature) {
  1006. if (ACPI_VALIDATE_RSDP_SIG(signature)) {
  1007. if (!ACPI_VALIDATE_RSDP_SIG(header.signature)) {
  1008. fprintf(stderr,
  1009. "Incorrect RSDP signature: found %8.8s\n",
  1010. header.signature);
  1011. status = AE_BAD_SIGNATURE;
  1012. goto exit;
  1013. }
  1014. } else if (!ACPI_COMPARE_NAME(signature, header.signature)) {
  1015. fprintf(stderr,
  1016. "Incorrect signature: Expecting %4.4s, found %4.4s\n",
  1017. signature, header.signature);
  1018. status = AE_BAD_SIGNATURE;
  1019. goto exit;
  1020. }
  1021. }
  1022. table_length = ap_get_table_length(&header);
  1023. if (table_length == 0) {
  1024. status = AE_BAD_HEADER;
  1025. goto exit;
  1026. }
  1027. /* Read the entire table into a local buffer */
  1028. local_table = calloc(1, table_length);
  1029. if (!local_table) {
  1030. fprintf(stderr,
  1031. "%4.4s: Could not allocate buffer for table of length %X\n",
  1032. header.signature, table_length);
  1033. status = AE_NO_MEMORY;
  1034. goto exit;
  1035. }
  1036. fseek(table_file, file_offset, SEEK_SET);
  1037. count = fread(local_table, 1, table_length, table_file);
  1038. if (count != table_length) {
  1039. fprintf(stderr, "%4.4s: Could not read table content\n",
  1040. header.signature);
  1041. status = AE_INVALID_TABLE_LENGTH;
  1042. goto exit;
  1043. }
  1044. /* Validate checksum */
  1045. (void)ap_is_valid_checksum(local_table);
  1046. exit:
  1047. fclose(table_file);
  1048. *table = local_table;
  1049. return (status);
  1050. }
  1051. /******************************************************************************
  1052. *
  1053. * FUNCTION: osl_get_customized_table
  1054. *
  1055. * PARAMETERS: pathname - Directory to find Linux customized table
  1056. * signature - ACPI Signature for desired table. Must be
  1057. * a null terminated 4-character string.
  1058. * instance - Multiple table support for SSDT/UEFI (0...n)
  1059. * Must be 0 for other tables.
  1060. * table - Where a pointer to the table is returned
  1061. * address - Where the table physical address is returned
  1062. *
  1063. * RETURN: Status; Table buffer is returned if AE_OK.
  1064. * AE_LIMIT: Instance is beyond valid limit
  1065. * AE_NOT_FOUND: A table with the signature was not found
  1066. *
  1067. * DESCRIPTION: Get an OS customized table.
  1068. *
  1069. *****************************************************************************/
  1070. static acpi_status
  1071. osl_get_customized_table(char *pathname,
  1072. char *signature,
  1073. u32 instance,
  1074. struct acpi_table_header **table,
  1075. acpi_physical_address * address)
  1076. {
  1077. void *table_dir;
  1078. u32 current_instance = 0;
  1079. char temp_name[ACPI_NAME_SIZE];
  1080. char table_filename[PATH_MAX];
  1081. char *filename;
  1082. acpi_status status;
  1083. /* Open the directory for customized tables */
  1084. table_dir = acpi_os_open_directory(pathname, "*", REQUEST_FILE_ONLY);
  1085. if (!table_dir) {
  1086. return (osl_get_last_status(AE_NOT_FOUND));
  1087. }
  1088. /* Attempt to find the table in the directory */
  1089. while ((filename = acpi_os_get_next_filename(table_dir))) {
  1090. /* Ignore meaningless files */
  1091. if (!ACPI_COMPARE_NAME(filename, signature)) {
  1092. continue;
  1093. }
  1094. /* Extract table name and instance number */
  1095. status =
  1096. osl_table_name_from_file(filename, temp_name,
  1097. &current_instance);
  1098. /* Ignore meaningless files */
  1099. if (ACPI_FAILURE(status) || current_instance != instance) {
  1100. continue;
  1101. }
  1102. /* Create the table pathname */
  1103. if (instance != 0) {
  1104. sprintf(table_filename, "%s/%4.4s%d", pathname,
  1105. temp_name, instance);
  1106. } else {
  1107. sprintf(table_filename, "%s/%4.4s", pathname,
  1108. temp_name);
  1109. }
  1110. break;
  1111. }
  1112. acpi_os_close_directory(table_dir);
  1113. if (!filename) {
  1114. return (AE_LIMIT);
  1115. }
  1116. /* There is no physical address saved for customized tables, use zero */
  1117. *address = 0;
  1118. status = osl_read_table_from_file(table_filename, 0, NULL, table);
  1119. return (status);
  1120. }