oslinuxtbl.c 36 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366
  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. 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 =
  715. (acpi_physical_address) gbl_rsdp_address;
  716. signature = ACPI_SIG_RSDP;
  717. }
  718. /* Now we can get the requested special table */
  719. status = osl_map_table(table_address, signature, &mapped_table);
  720. if (ACPI_FAILURE(status)) {
  721. return (status);
  722. }
  723. table_length = ap_get_table_length(mapped_table);
  724. } else { /* Case for a normal ACPI table */
  725. if (osl_can_use_xsdt()) {
  726. item_size = sizeof(u64);
  727. table_data =
  728. ACPI_CAST8(gbl_xsdt) +
  729. sizeof(struct acpi_table_header);
  730. number_of_tables =
  731. (u8)((gbl_xsdt->header.length -
  732. sizeof(struct acpi_table_header))
  733. / item_size);
  734. } else { /* Use RSDT if XSDT is not available */
  735. item_size = sizeof(u32);
  736. table_data =
  737. ACPI_CAST8(gbl_rsdt) +
  738. sizeof(struct acpi_table_header);
  739. number_of_tables =
  740. (u8)((gbl_rsdt->header.length -
  741. sizeof(struct acpi_table_header))
  742. / item_size);
  743. }
  744. /* Search RSDT/XSDT for the requested table */
  745. for (i = 0; i < number_of_tables; ++i, table_data += item_size) {
  746. if (osl_can_use_xsdt()) {
  747. table_address =
  748. (acpi_physical_address) (*ACPI_CAST64
  749. (table_data));
  750. } else {
  751. table_address =
  752. (acpi_physical_address) (*ACPI_CAST32
  753. (table_data));
  754. }
  755. /* Skip NULL entries in RSDT/XSDT */
  756. if (!table_address) {
  757. continue;
  758. }
  759. status =
  760. osl_map_table(table_address, NULL, &mapped_table);
  761. if (ACPI_FAILURE(status)) {
  762. return (status);
  763. }
  764. table_length = mapped_table->length;
  765. /* Does this table match the requested signature? */
  766. if (!ACPI_COMPARE_NAME
  767. (mapped_table->signature, signature)) {
  768. osl_unmap_table(mapped_table);
  769. mapped_table = NULL;
  770. continue;
  771. }
  772. /* Match table instance (for SSDT/UEFI tables) */
  773. if (current_instance != instance) {
  774. osl_unmap_table(mapped_table);
  775. mapped_table = NULL;
  776. current_instance++;
  777. continue;
  778. }
  779. break;
  780. }
  781. }
  782. if (!mapped_table) {
  783. return (AE_LIMIT);
  784. }
  785. if (table_length == 0) {
  786. status = AE_BAD_HEADER;
  787. goto exit;
  788. }
  789. /* Copy table to local buffer and return it */
  790. local_table = calloc(1, table_length);
  791. if (!local_table) {
  792. status = AE_NO_MEMORY;
  793. goto exit;
  794. }
  795. memcpy(local_table, mapped_table, table_length);
  796. *address = table_address;
  797. *table = local_table;
  798. exit:
  799. osl_unmap_table(mapped_table);
  800. return (status);
  801. }
  802. /******************************************************************************
  803. *
  804. * FUNCTION: osl_list_customized_tables
  805. *
  806. * PARAMETERS: directory - Directory that contains the tables
  807. *
  808. * RETURN: Status; Table list is initialized if AE_OK.
  809. *
  810. * DESCRIPTION: Add ACPI tables to the table list from a directory.
  811. *
  812. *****************************************************************************/
  813. static acpi_status osl_list_customized_tables(char *directory)
  814. {
  815. void *table_dir;
  816. u32 instance;
  817. char temp_name[ACPI_NAME_SIZE];
  818. char *filename;
  819. acpi_status status = AE_OK;
  820. /* Open the requested directory */
  821. table_dir = acpi_os_open_directory(directory, "*", REQUEST_FILE_ONLY);
  822. if (!table_dir) {
  823. return (osl_get_last_status(AE_NOT_FOUND));
  824. }
  825. /* Examine all entries in this directory */
  826. while ((filename = acpi_os_get_next_filename(table_dir))) {
  827. /* Extract table name and instance number */
  828. status =
  829. osl_table_name_from_file(filename, temp_name, &instance);
  830. /* Ignore meaningless files */
  831. if (ACPI_FAILURE(status)) {
  832. continue;
  833. }
  834. /* Add new info node to global table list */
  835. status = osl_add_table_to_list(temp_name, instance);
  836. if (ACPI_FAILURE(status)) {
  837. break;
  838. }
  839. }
  840. acpi_os_close_directory(table_dir);
  841. return (status);
  842. }
  843. /******************************************************************************
  844. *
  845. * FUNCTION: osl_map_table
  846. *
  847. * PARAMETERS: address - Address of the table in memory
  848. * signature - Optional ACPI Signature for desired table.
  849. * Null terminated 4-character string.
  850. * table - Where a pointer to the mapped table is
  851. * returned
  852. *
  853. * RETURN: Status; Mapped table is returned if AE_OK.
  854. * AE_NOT_FOUND: A valid table was not found at the address
  855. *
  856. * DESCRIPTION: Map entire ACPI table into caller's address space.
  857. *
  858. *****************************************************************************/
  859. static acpi_status
  860. osl_map_table(acpi_size address,
  861. char *signature, struct acpi_table_header **table)
  862. {
  863. struct acpi_table_header *mapped_table;
  864. u32 length;
  865. if (!address) {
  866. return (AE_BAD_ADDRESS);
  867. }
  868. /*
  869. * Map the header so we can get the table length.
  870. * Use sizeof (struct acpi_table_header) as:
  871. * 1. it is bigger than 24 to include RSDP->Length
  872. * 2. it is smaller than sizeof (struct acpi_table_rsdp)
  873. */
  874. mapped_table =
  875. acpi_os_map_memory(address, sizeof(struct acpi_table_header));
  876. if (!mapped_table) {
  877. fprintf(stderr, "Could not map table header at 0x%8.8X%8.8X\n",
  878. ACPI_FORMAT_UINT64(address));
  879. return (osl_get_last_status(AE_BAD_ADDRESS));
  880. }
  881. /* If specified, signature must match */
  882. if (signature) {
  883. if (ACPI_VALIDATE_RSDP_SIG(signature)) {
  884. if (!ACPI_VALIDATE_RSDP_SIG(mapped_table->signature)) {
  885. acpi_os_unmap_memory(mapped_table,
  886. sizeof(struct
  887. acpi_table_header));
  888. return (AE_BAD_SIGNATURE);
  889. }
  890. } else
  891. if (!ACPI_COMPARE_NAME(signature, mapped_table->signature))
  892. {
  893. acpi_os_unmap_memory(mapped_table,
  894. sizeof(struct acpi_table_header));
  895. return (AE_BAD_SIGNATURE);
  896. }
  897. }
  898. /* Map the entire table */
  899. length = ap_get_table_length(mapped_table);
  900. acpi_os_unmap_memory(mapped_table, sizeof(struct acpi_table_header));
  901. if (length == 0) {
  902. return (AE_BAD_HEADER);
  903. }
  904. mapped_table = acpi_os_map_memory(address, length);
  905. if (!mapped_table) {
  906. fprintf(stderr,
  907. "Could not map table at 0x%8.8X%8.8X length %8.8X\n",
  908. ACPI_FORMAT_UINT64(address), length);
  909. return (osl_get_last_status(AE_INVALID_TABLE_LENGTH));
  910. }
  911. (void)ap_is_valid_checksum(mapped_table);
  912. *table = mapped_table;
  913. return (AE_OK);
  914. }
  915. /******************************************************************************
  916. *
  917. * FUNCTION: osl_unmap_table
  918. *
  919. * PARAMETERS: table - A pointer to the mapped table
  920. *
  921. * RETURN: None
  922. *
  923. * DESCRIPTION: Unmap entire ACPI table.
  924. *
  925. *****************************************************************************/
  926. static void osl_unmap_table(struct acpi_table_header *table)
  927. {
  928. if (table) {
  929. acpi_os_unmap_memory(table, ap_get_table_length(table));
  930. }
  931. }
  932. /******************************************************************************
  933. *
  934. * FUNCTION: osl_table_name_from_file
  935. *
  936. * PARAMETERS: filename - File that contains the desired table
  937. * signature - Pointer to 4-character buffer to store
  938. * extracted table signature.
  939. * instance - Pointer to integer to store extracted
  940. * table instance number.
  941. *
  942. * RETURN: Status; Table name is extracted if AE_OK.
  943. *
  944. * DESCRIPTION: Extract table signature and instance number from a table file
  945. * name.
  946. *
  947. *****************************************************************************/
  948. static acpi_status
  949. osl_table_name_from_file(char *filename, char *signature, u32 *instance)
  950. {
  951. /* Ignore meaningless files */
  952. if (strlen(filename) < ACPI_NAME_SIZE) {
  953. return (AE_BAD_SIGNATURE);
  954. }
  955. /* Extract instance number */
  956. if (isdigit((int)filename[ACPI_NAME_SIZE])) {
  957. sscanf(&filename[ACPI_NAME_SIZE], "%u", instance);
  958. } else if (strlen(filename) != ACPI_NAME_SIZE) {
  959. return (AE_BAD_SIGNATURE);
  960. } else {
  961. *instance = 0;
  962. }
  963. /* Extract signature */
  964. ACPI_MOVE_NAME(signature, filename);
  965. return (AE_OK);
  966. }
  967. /******************************************************************************
  968. *
  969. * FUNCTION: osl_read_table_from_file
  970. *
  971. * PARAMETERS: filename - File that contains the desired table
  972. * file_offset - Offset of the table in file
  973. * signature - Optional ACPI Signature for desired table.
  974. * A null terminated 4-character string.
  975. * table - Where a pointer to the table is returned
  976. *
  977. * RETURN: Status; Table buffer is returned if AE_OK.
  978. *
  979. * DESCRIPTION: Read a ACPI table from a file.
  980. *
  981. *****************************************************************************/
  982. static acpi_status
  983. osl_read_table_from_file(char *filename,
  984. acpi_size file_offset,
  985. char *signature, struct acpi_table_header **table)
  986. {
  987. FILE *table_file;
  988. struct acpi_table_header header;
  989. struct acpi_table_header *local_table = NULL;
  990. u32 table_length;
  991. s32 count;
  992. acpi_status status = AE_OK;
  993. /* Open the file */
  994. table_file = fopen(filename, "rb");
  995. if (table_file == NULL) {
  996. fprintf(stderr, "Could not open table file: %s\n", filename);
  997. return (osl_get_last_status(AE_NOT_FOUND));
  998. }
  999. fseek(table_file, file_offset, SEEK_SET);
  1000. /* Read the Table header to get the table length */
  1001. count = fread(&header, 1, sizeof(struct acpi_table_header), table_file);
  1002. if (count != sizeof(struct acpi_table_header)) {
  1003. fprintf(stderr, "Could not read table header: %s\n", filename);
  1004. status = AE_BAD_HEADER;
  1005. goto exit;
  1006. }
  1007. /* If signature is specified, it must match the table */
  1008. if (signature) {
  1009. if (ACPI_VALIDATE_RSDP_SIG(signature)) {
  1010. if (!ACPI_VALIDATE_RSDP_SIG(header.signature)) {
  1011. fprintf(stderr,
  1012. "Incorrect RSDP signature: found %8.8s\n",
  1013. header.signature);
  1014. status = AE_BAD_SIGNATURE;
  1015. goto exit;
  1016. }
  1017. } else if (!ACPI_COMPARE_NAME(signature, header.signature)) {
  1018. fprintf(stderr,
  1019. "Incorrect signature: Expecting %4.4s, found %4.4s\n",
  1020. signature, header.signature);
  1021. status = AE_BAD_SIGNATURE;
  1022. goto exit;
  1023. }
  1024. }
  1025. table_length = ap_get_table_length(&header);
  1026. if (table_length == 0) {
  1027. status = AE_BAD_HEADER;
  1028. goto exit;
  1029. }
  1030. /* Read the entire table into a local buffer */
  1031. local_table = calloc(1, table_length);
  1032. if (!local_table) {
  1033. fprintf(stderr,
  1034. "%4.4s: Could not allocate buffer for table of length %X\n",
  1035. header.signature, table_length);
  1036. status = AE_NO_MEMORY;
  1037. goto exit;
  1038. }
  1039. fseek(table_file, file_offset, SEEK_SET);
  1040. count = fread(local_table, 1, table_length, table_file);
  1041. if (count != table_length) {
  1042. fprintf(stderr, "%4.4s: Could not read table content\n",
  1043. header.signature);
  1044. status = AE_INVALID_TABLE_LENGTH;
  1045. goto exit;
  1046. }
  1047. /* Validate checksum */
  1048. (void)ap_is_valid_checksum(local_table);
  1049. exit:
  1050. fclose(table_file);
  1051. *table = local_table;
  1052. return (status);
  1053. }
  1054. /******************************************************************************
  1055. *
  1056. * FUNCTION: osl_get_customized_table
  1057. *
  1058. * PARAMETERS: pathname - Directory to find Linux customized table
  1059. * signature - ACPI Signature for desired table. Must be
  1060. * a null terminated 4-character string.
  1061. * instance - Multiple table support for SSDT/UEFI (0...n)
  1062. * Must be 0 for other tables.
  1063. * table - Where a pointer to the table is returned
  1064. * address - Where the table physical address is returned
  1065. *
  1066. * RETURN: Status; Table buffer is returned if AE_OK.
  1067. * AE_LIMIT: Instance is beyond valid limit
  1068. * AE_NOT_FOUND: A table with the signature was not found
  1069. *
  1070. * DESCRIPTION: Get an OS customized table.
  1071. *
  1072. *****************************************************************************/
  1073. static acpi_status
  1074. osl_get_customized_table(char *pathname,
  1075. char *signature,
  1076. u32 instance,
  1077. struct acpi_table_header **table,
  1078. acpi_physical_address * address)
  1079. {
  1080. void *table_dir;
  1081. u32 current_instance = 0;
  1082. char temp_name[ACPI_NAME_SIZE];
  1083. char table_filename[PATH_MAX];
  1084. char *filename;
  1085. acpi_status status;
  1086. /* Open the directory for customized tables */
  1087. table_dir = acpi_os_open_directory(pathname, "*", REQUEST_FILE_ONLY);
  1088. if (!table_dir) {
  1089. return (osl_get_last_status(AE_NOT_FOUND));
  1090. }
  1091. /* Attempt to find the table in the directory */
  1092. while ((filename = acpi_os_get_next_filename(table_dir))) {
  1093. /* Ignore meaningless files */
  1094. if (!ACPI_COMPARE_NAME(filename, signature)) {
  1095. continue;
  1096. }
  1097. /* Extract table name and instance number */
  1098. status =
  1099. osl_table_name_from_file(filename, temp_name,
  1100. &current_instance);
  1101. /* Ignore meaningless files */
  1102. if (ACPI_FAILURE(status) || current_instance != instance) {
  1103. continue;
  1104. }
  1105. /* Create the table pathname */
  1106. if (instance != 0) {
  1107. sprintf(table_filename, "%s/%4.4s%d", pathname,
  1108. temp_name, instance);
  1109. } else {
  1110. sprintf(table_filename, "%s/%4.4s", pathname,
  1111. temp_name);
  1112. }
  1113. break;
  1114. }
  1115. acpi_os_close_directory(table_dir);
  1116. if (!filename) {
  1117. return (AE_LIMIT);
  1118. }
  1119. /* There is no physical address saved for customized tables, use zero */
  1120. *address = 0;
  1121. status = osl_read_table_from_file(table_filename, 0, NULL, table);
  1122. return (status);
  1123. }