oslinuxtbl.c 35 KB

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