oslinuxtbl.c 36 KB

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