oslinuxtbl.c 35 KB

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