utresrc.c 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600
  1. /*******************************************************************************
  2. *
  3. * Module Name: utresrc - Resource management utilities
  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 <acpi/acpi.h>
  43. #include "accommon.h"
  44. #include "acresrc.h"
  45. #define _COMPONENT ACPI_UTILITIES
  46. ACPI_MODULE_NAME("utresrc")
  47. /*
  48. * Base sizes of the raw AML resource descriptors, indexed by resource type.
  49. * Zero indicates a reserved (and therefore invalid) resource type.
  50. */
  51. const u8 acpi_gbl_resource_aml_sizes[] = {
  52. /* Small descriptors */
  53. 0,
  54. 0,
  55. 0,
  56. 0,
  57. ACPI_AML_SIZE_SMALL(struct aml_resource_irq),
  58. ACPI_AML_SIZE_SMALL(struct aml_resource_dma),
  59. ACPI_AML_SIZE_SMALL(struct aml_resource_start_dependent),
  60. ACPI_AML_SIZE_SMALL(struct aml_resource_end_dependent),
  61. ACPI_AML_SIZE_SMALL(struct aml_resource_io),
  62. ACPI_AML_SIZE_SMALL(struct aml_resource_fixed_io),
  63. ACPI_AML_SIZE_SMALL(struct aml_resource_fixed_dma),
  64. 0,
  65. 0,
  66. 0,
  67. ACPI_AML_SIZE_SMALL(struct aml_resource_vendor_small),
  68. ACPI_AML_SIZE_SMALL(struct aml_resource_end_tag),
  69. /* Large descriptors */
  70. 0,
  71. ACPI_AML_SIZE_LARGE(struct aml_resource_memory24),
  72. ACPI_AML_SIZE_LARGE(struct aml_resource_generic_register),
  73. 0,
  74. ACPI_AML_SIZE_LARGE(struct aml_resource_vendor_large),
  75. ACPI_AML_SIZE_LARGE(struct aml_resource_memory32),
  76. ACPI_AML_SIZE_LARGE(struct aml_resource_fixed_memory32),
  77. ACPI_AML_SIZE_LARGE(struct aml_resource_address32),
  78. ACPI_AML_SIZE_LARGE(struct aml_resource_address16),
  79. ACPI_AML_SIZE_LARGE(struct aml_resource_extended_irq),
  80. ACPI_AML_SIZE_LARGE(struct aml_resource_address64),
  81. ACPI_AML_SIZE_LARGE(struct aml_resource_extended_address64),
  82. ACPI_AML_SIZE_LARGE(struct aml_resource_gpio),
  83. ACPI_AML_SIZE_LARGE(struct aml_resource_pin_function),
  84. ACPI_AML_SIZE_LARGE(struct aml_resource_common_serialbus),
  85. ACPI_AML_SIZE_LARGE(struct aml_resource_pin_config),
  86. ACPI_AML_SIZE_LARGE(struct aml_resource_pin_group),
  87. ACPI_AML_SIZE_LARGE(struct aml_resource_pin_group_function),
  88. ACPI_AML_SIZE_LARGE(struct aml_resource_pin_group_config),
  89. };
  90. const u8 acpi_gbl_resource_aml_serial_bus_sizes[] = {
  91. 0,
  92. ACPI_AML_SIZE_LARGE(struct aml_resource_i2c_serialbus),
  93. ACPI_AML_SIZE_LARGE(struct aml_resource_spi_serialbus),
  94. ACPI_AML_SIZE_LARGE(struct aml_resource_uart_serialbus),
  95. };
  96. /*
  97. * Resource types, used to validate the resource length field.
  98. * The length of fixed-length types must match exactly, variable
  99. * lengths must meet the minimum required length, etc.
  100. * Zero indicates a reserved (and therefore invalid) resource type.
  101. */
  102. static const u8 acpi_gbl_resource_types[] = {
  103. /* Small descriptors */
  104. 0,
  105. 0,
  106. 0,
  107. 0,
  108. ACPI_SMALL_VARIABLE_LENGTH, /* 04 IRQ */
  109. ACPI_FIXED_LENGTH, /* 05 DMA */
  110. ACPI_SMALL_VARIABLE_LENGTH, /* 06 start_dependent_functions */
  111. ACPI_FIXED_LENGTH, /* 07 end_dependent_functions */
  112. ACPI_FIXED_LENGTH, /* 08 IO */
  113. ACPI_FIXED_LENGTH, /* 09 fixed_IO */
  114. ACPI_FIXED_LENGTH, /* 0A fixed_DMA */
  115. 0,
  116. 0,
  117. 0,
  118. ACPI_VARIABLE_LENGTH, /* 0E vendor_short */
  119. ACPI_FIXED_LENGTH, /* 0F end_tag */
  120. /* Large descriptors */
  121. 0,
  122. ACPI_FIXED_LENGTH, /* 01 Memory24 */
  123. ACPI_FIXED_LENGTH, /* 02 generic_register */
  124. 0,
  125. ACPI_VARIABLE_LENGTH, /* 04 vendor_long */
  126. ACPI_FIXED_LENGTH, /* 05 Memory32 */
  127. ACPI_FIXED_LENGTH, /* 06 memory32_fixed */
  128. ACPI_VARIABLE_LENGTH, /* 07 Dword* address */
  129. ACPI_VARIABLE_LENGTH, /* 08 Word* address */
  130. ACPI_VARIABLE_LENGTH, /* 09 extended_IRQ */
  131. ACPI_VARIABLE_LENGTH, /* 0A Qword* address */
  132. ACPI_FIXED_LENGTH, /* 0B Extended* address */
  133. ACPI_VARIABLE_LENGTH, /* 0C Gpio* */
  134. ACPI_VARIABLE_LENGTH, /* 0D pin_function */
  135. ACPI_VARIABLE_LENGTH, /* 0E *serial_bus */
  136. ACPI_VARIABLE_LENGTH, /* 0F pin_config */
  137. ACPI_VARIABLE_LENGTH, /* 10 pin_group */
  138. ACPI_VARIABLE_LENGTH, /* 11 pin_group_function */
  139. ACPI_VARIABLE_LENGTH, /* 12 pin_group_config */
  140. };
  141. /*******************************************************************************
  142. *
  143. * FUNCTION: acpi_ut_walk_aml_resources
  144. *
  145. * PARAMETERS: walk_state - Current walk info
  146. * PARAMETERS: aml - Pointer to the raw AML resource template
  147. * aml_length - Length of the entire template
  148. * user_function - Called once for each descriptor found. If
  149. * NULL, a pointer to the end_tag is returned
  150. * context - Passed to user_function
  151. *
  152. * RETURN: Status
  153. *
  154. * DESCRIPTION: Walk a raw AML resource list(buffer). User function called
  155. * once for each resource found.
  156. *
  157. ******************************************************************************/
  158. acpi_status
  159. acpi_ut_walk_aml_resources(struct acpi_walk_state *walk_state,
  160. u8 *aml,
  161. acpi_size aml_length,
  162. acpi_walk_aml_callback user_function, void **context)
  163. {
  164. acpi_status status;
  165. u8 *end_aml;
  166. u8 resource_index;
  167. u32 length;
  168. u32 offset = 0;
  169. u8 end_tag[2] = { 0x79, 0x00 };
  170. ACPI_FUNCTION_TRACE(ut_walk_aml_resources);
  171. /* The absolute minimum resource template is one end_tag descriptor */
  172. if (aml_length < sizeof(struct aml_resource_end_tag)) {
  173. return_ACPI_STATUS(AE_AML_NO_RESOURCE_END_TAG);
  174. }
  175. /* Point to the end of the resource template buffer */
  176. end_aml = aml + aml_length;
  177. /* Walk the byte list, abort on any invalid descriptor type or length */
  178. while (aml < end_aml) {
  179. /* Validate the Resource Type and Resource Length */
  180. status =
  181. acpi_ut_validate_resource(walk_state, aml, &resource_index);
  182. if (ACPI_FAILURE(status)) {
  183. /*
  184. * Exit on failure. Cannot continue because the descriptor
  185. * length may be bogus also.
  186. */
  187. return_ACPI_STATUS(status);
  188. }
  189. /* Get the length of this descriptor */
  190. length = acpi_ut_get_descriptor_length(aml);
  191. /* Invoke the user function */
  192. if (user_function) {
  193. status =
  194. user_function(aml, length, offset, resource_index,
  195. context);
  196. if (ACPI_FAILURE(status)) {
  197. return_ACPI_STATUS(status);
  198. }
  199. }
  200. /* An end_tag descriptor terminates this resource template */
  201. if (acpi_ut_get_resource_type(aml) ==
  202. ACPI_RESOURCE_NAME_END_TAG) {
  203. /*
  204. * There must be at least one more byte in the buffer for
  205. * the 2nd byte of the end_tag
  206. */
  207. if ((aml + 1) >= end_aml) {
  208. return_ACPI_STATUS(AE_AML_NO_RESOURCE_END_TAG);
  209. }
  210. /*
  211. * Don't attempt to perform any validation on the 2nd byte.
  212. * Although all known ASL compilers insert a zero for the 2nd
  213. * byte, it can also be a checksum (as per the ACPI spec),
  214. * and this is occasionally seen in the field. July 2017.
  215. */
  216. /* Return the pointer to the end_tag if requested */
  217. if (!user_function) {
  218. *context = aml;
  219. }
  220. /* Normal exit */
  221. return_ACPI_STATUS(AE_OK);
  222. }
  223. aml += length;
  224. offset += length;
  225. }
  226. /* Did not find an end_tag descriptor */
  227. if (user_function) {
  228. /* Insert an end_tag anyway. acpi_rs_get_list_length always leaves room */
  229. (void)acpi_ut_validate_resource(walk_state, end_tag,
  230. &resource_index);
  231. status =
  232. user_function(end_tag, 2, offset, resource_index, context);
  233. if (ACPI_FAILURE(status)) {
  234. return_ACPI_STATUS(status);
  235. }
  236. }
  237. return_ACPI_STATUS(AE_AML_NO_RESOURCE_END_TAG);
  238. }
  239. /*******************************************************************************
  240. *
  241. * FUNCTION: acpi_ut_validate_resource
  242. *
  243. * PARAMETERS: walk_state - Current walk info
  244. * aml - Pointer to the raw AML resource descriptor
  245. * return_index - Where the resource index is returned. NULL
  246. * if the index is not required.
  247. *
  248. * RETURN: Status, and optionally the Index into the global resource tables
  249. *
  250. * DESCRIPTION: Validate an AML resource descriptor by checking the Resource
  251. * Type and Resource Length. Returns an index into the global
  252. * resource information/dispatch tables for later use.
  253. *
  254. ******************************************************************************/
  255. acpi_status
  256. acpi_ut_validate_resource(struct acpi_walk_state *walk_state,
  257. void *aml, u8 *return_index)
  258. {
  259. union aml_resource *aml_resource;
  260. u8 resource_type;
  261. u8 resource_index;
  262. acpi_rs_length resource_length;
  263. acpi_rs_length minimum_resource_length;
  264. ACPI_FUNCTION_ENTRY();
  265. /*
  266. * 1) Validate the resource_type field (Byte 0)
  267. */
  268. resource_type = ACPI_GET8(aml);
  269. /*
  270. * Byte 0 contains the descriptor name (Resource Type)
  271. * Examine the large/small bit in the resource header
  272. */
  273. if (resource_type & ACPI_RESOURCE_NAME_LARGE) {
  274. /* Verify the large resource type (name) against the max */
  275. if (resource_type > ACPI_RESOURCE_NAME_LARGE_MAX) {
  276. goto invalid_resource;
  277. }
  278. /*
  279. * Large Resource Type -- bits 6:0 contain the name
  280. * Translate range 0x80-0x8B to index range 0x10-0x1B
  281. */
  282. resource_index = (u8) (resource_type - 0x70);
  283. } else {
  284. /*
  285. * Small Resource Type -- bits 6:3 contain the name
  286. * Shift range to index range 0x00-0x0F
  287. */
  288. resource_index = (u8)
  289. ((resource_type & ACPI_RESOURCE_NAME_SMALL_MASK) >> 3);
  290. }
  291. /*
  292. * Check validity of the resource type, via acpi_gbl_resource_types.
  293. * Zero indicates an invalid resource.
  294. */
  295. if (!acpi_gbl_resource_types[resource_index]) {
  296. goto invalid_resource;
  297. }
  298. /*
  299. * Validate the resource_length field. This ensures that the length
  300. * is at least reasonable, and guarantees that it is non-zero.
  301. */
  302. resource_length = acpi_ut_get_resource_length(aml);
  303. minimum_resource_length = acpi_gbl_resource_aml_sizes[resource_index];
  304. /* Validate based upon the type of resource - fixed length or variable */
  305. switch (acpi_gbl_resource_types[resource_index]) {
  306. case ACPI_FIXED_LENGTH:
  307. /* Fixed length resource, length must match exactly */
  308. if (resource_length != minimum_resource_length) {
  309. goto bad_resource_length;
  310. }
  311. break;
  312. case ACPI_VARIABLE_LENGTH:
  313. /* Variable length resource, length must be at least the minimum */
  314. if (resource_length < minimum_resource_length) {
  315. goto bad_resource_length;
  316. }
  317. break;
  318. case ACPI_SMALL_VARIABLE_LENGTH:
  319. /* Small variable length resource, length can be (Min) or (Min-1) */
  320. if ((resource_length > minimum_resource_length) ||
  321. (resource_length < (minimum_resource_length - 1))) {
  322. goto bad_resource_length;
  323. }
  324. break;
  325. default:
  326. /* Shouldn't happen (because of validation earlier), but be sure */
  327. goto invalid_resource;
  328. }
  329. aml_resource = ACPI_CAST_PTR(union aml_resource, aml);
  330. if (resource_type == ACPI_RESOURCE_NAME_SERIAL_BUS) {
  331. /* Validate the bus_type field */
  332. if ((aml_resource->common_serial_bus.type == 0) ||
  333. (aml_resource->common_serial_bus.type >
  334. AML_RESOURCE_MAX_SERIALBUSTYPE)) {
  335. if (walk_state) {
  336. ACPI_ERROR((AE_INFO,
  337. "Invalid/unsupported SerialBus resource descriptor: BusType 0x%2.2X",
  338. aml_resource->common_serial_bus.
  339. type));
  340. }
  341. return (AE_AML_INVALID_RESOURCE_TYPE);
  342. }
  343. }
  344. /* Optionally return the resource table index */
  345. if (return_index) {
  346. *return_index = resource_index;
  347. }
  348. return (AE_OK);
  349. invalid_resource:
  350. if (walk_state) {
  351. ACPI_ERROR((AE_INFO,
  352. "Invalid/unsupported resource descriptor: Type 0x%2.2X",
  353. resource_type));
  354. }
  355. return (AE_AML_INVALID_RESOURCE_TYPE);
  356. bad_resource_length:
  357. if (walk_state) {
  358. ACPI_ERROR((AE_INFO,
  359. "Invalid resource descriptor length: Type "
  360. "0x%2.2X, Length 0x%4.4X, MinLength 0x%4.4X",
  361. resource_type, resource_length,
  362. minimum_resource_length));
  363. }
  364. return (AE_AML_BAD_RESOURCE_LENGTH);
  365. }
  366. /*******************************************************************************
  367. *
  368. * FUNCTION: acpi_ut_get_resource_type
  369. *
  370. * PARAMETERS: aml - Pointer to the raw AML resource descriptor
  371. *
  372. * RETURN: The Resource Type with no extraneous bits (except the
  373. * Large/Small descriptor bit -- this is left alone)
  374. *
  375. * DESCRIPTION: Extract the Resource Type/Name from the first byte of
  376. * a resource descriptor.
  377. *
  378. ******************************************************************************/
  379. u8 acpi_ut_get_resource_type(void *aml)
  380. {
  381. ACPI_FUNCTION_ENTRY();
  382. /*
  383. * Byte 0 contains the descriptor name (Resource Type)
  384. * Examine the large/small bit in the resource header
  385. */
  386. if (ACPI_GET8(aml) & ACPI_RESOURCE_NAME_LARGE) {
  387. /* Large Resource Type -- bits 6:0 contain the name */
  388. return (ACPI_GET8(aml));
  389. } else {
  390. /* Small Resource Type -- bits 6:3 contain the name */
  391. return ((u8) (ACPI_GET8(aml) & ACPI_RESOURCE_NAME_SMALL_MASK));
  392. }
  393. }
  394. /*******************************************************************************
  395. *
  396. * FUNCTION: acpi_ut_get_resource_length
  397. *
  398. * PARAMETERS: aml - Pointer to the raw AML resource descriptor
  399. *
  400. * RETURN: Byte Length
  401. *
  402. * DESCRIPTION: Get the "Resource Length" of a raw AML descriptor. By
  403. * definition, this does not include the size of the descriptor
  404. * header or the length field itself.
  405. *
  406. ******************************************************************************/
  407. u16 acpi_ut_get_resource_length(void *aml)
  408. {
  409. acpi_rs_length resource_length;
  410. ACPI_FUNCTION_ENTRY();
  411. /*
  412. * Byte 0 contains the descriptor name (Resource Type)
  413. * Examine the large/small bit in the resource header
  414. */
  415. if (ACPI_GET8(aml) & ACPI_RESOURCE_NAME_LARGE) {
  416. /* Large Resource type -- bytes 1-2 contain the 16-bit length */
  417. ACPI_MOVE_16_TO_16(&resource_length, ACPI_ADD_PTR(u8, aml, 1));
  418. } else {
  419. /* Small Resource type -- bits 2:0 of byte 0 contain the length */
  420. resource_length = (u16) (ACPI_GET8(aml) &
  421. ACPI_RESOURCE_NAME_SMALL_LENGTH_MASK);
  422. }
  423. return (resource_length);
  424. }
  425. /*******************************************************************************
  426. *
  427. * FUNCTION: acpi_ut_get_resource_header_length
  428. *
  429. * PARAMETERS: aml - Pointer to the raw AML resource descriptor
  430. *
  431. * RETURN: Length of the AML header (depends on large/small descriptor)
  432. *
  433. * DESCRIPTION: Get the length of the header for this resource.
  434. *
  435. ******************************************************************************/
  436. u8 acpi_ut_get_resource_header_length(void *aml)
  437. {
  438. ACPI_FUNCTION_ENTRY();
  439. /* Examine the large/small bit in the resource header */
  440. if (ACPI_GET8(aml) & ACPI_RESOURCE_NAME_LARGE) {
  441. return (sizeof(struct aml_resource_large_header));
  442. } else {
  443. return (sizeof(struct aml_resource_small_header));
  444. }
  445. }
  446. /*******************************************************************************
  447. *
  448. * FUNCTION: acpi_ut_get_descriptor_length
  449. *
  450. * PARAMETERS: aml - Pointer to the raw AML resource descriptor
  451. *
  452. * RETURN: Byte length
  453. *
  454. * DESCRIPTION: Get the total byte length of a raw AML descriptor, including the
  455. * length of the descriptor header and the length field itself.
  456. * Used to walk descriptor lists.
  457. *
  458. ******************************************************************************/
  459. u32 acpi_ut_get_descriptor_length(void *aml)
  460. {
  461. ACPI_FUNCTION_ENTRY();
  462. /*
  463. * Get the Resource Length (does not include header length) and add
  464. * the header length (depends on if this is a small or large resource)
  465. */
  466. return (acpi_ut_get_resource_length(aml) +
  467. acpi_ut_get_resource_header_length(aml));
  468. }
  469. /*******************************************************************************
  470. *
  471. * FUNCTION: acpi_ut_get_resource_end_tag
  472. *
  473. * PARAMETERS: obj_desc - The resource template buffer object
  474. * end_tag - Where the pointer to the end_tag is returned
  475. *
  476. * RETURN: Status, pointer to the end tag
  477. *
  478. * DESCRIPTION: Find the end_tag resource descriptor in an AML resource template
  479. * Note: allows a buffer length of zero.
  480. *
  481. ******************************************************************************/
  482. acpi_status
  483. acpi_ut_get_resource_end_tag(union acpi_operand_object *obj_desc, u8 **end_tag)
  484. {
  485. acpi_status status;
  486. ACPI_FUNCTION_TRACE(ut_get_resource_end_tag);
  487. /* Allow a buffer length of zero */
  488. if (!obj_desc->buffer.length) {
  489. *end_tag = obj_desc->buffer.pointer;
  490. return_ACPI_STATUS(AE_OK);
  491. }
  492. /* Validate the template and get a pointer to the end_tag */
  493. status = acpi_ut_walk_aml_resources(NULL, obj_desc->buffer.pointer,
  494. obj_desc->buffer.length, NULL,
  495. (void **)end_tag);
  496. return_ACPI_STATUS(status);
  497. }