psargs.c 23 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955
  1. // SPDX-License-Identifier: BSD-3-Clause OR GPL-2.0
  2. /******************************************************************************
  3. *
  4. * Module Name: psargs - Parse AML opcode arguments
  5. *
  6. * Copyright (C) 2000 - 2025, Intel Corp.
  7. *
  8. *****************************************************************************/
  9. #include <acpi/acpi.h>
  10. #include "accommon.h"
  11. #include "acparser.h"
  12. #include "amlcode.h"
  13. #include "acnamesp.h"
  14. #include "acdispat.h"
  15. #include "acconvert.h"
  16. #define _COMPONENT ACPI_PARSER
  17. ACPI_MODULE_NAME("psargs")
  18. /* Local prototypes */
  19. static u32
  20. acpi_ps_get_next_package_length(struct acpi_parse_state *parser_state);
  21. static union acpi_parse_object *acpi_ps_get_next_field(struct acpi_parse_state
  22. *parser_state);
  23. static void acpi_ps_free_field_list(union acpi_parse_object *start);
  24. /*******************************************************************************
  25. *
  26. * FUNCTION: acpi_ps_get_next_package_length
  27. *
  28. * PARAMETERS: parser_state - Current parser state object
  29. *
  30. * RETURN: Decoded package length. On completion, the AML pointer points
  31. * past the length byte or bytes.
  32. *
  33. * DESCRIPTION: Decode and return a package length field.
  34. * Note: Largest package length is 28 bits, from ACPI specification
  35. *
  36. ******************************************************************************/
  37. static u32
  38. acpi_ps_get_next_package_length(struct acpi_parse_state *parser_state)
  39. {
  40. u8 *aml = parser_state->aml;
  41. u32 package_length = 0;
  42. u32 byte_count;
  43. u8 byte_zero_mask = 0x3F; /* Default [0:5] */
  44. ACPI_FUNCTION_TRACE(ps_get_next_package_length);
  45. /*
  46. * Byte 0 bits [6:7] contain the number of additional bytes
  47. * used to encode the package length, either 0,1,2, or 3
  48. */
  49. byte_count = (aml[0] >> 6);
  50. parser_state->aml += ((acpi_size)byte_count + 1);
  51. /* Get bytes 3, 2, 1 as needed */
  52. while (byte_count) {
  53. /*
  54. * Final bit positions for the package length bytes:
  55. * Byte3->[20:27]
  56. * Byte2->[12:19]
  57. * Byte1->[04:11]
  58. * Byte0->[00:03]
  59. */
  60. package_length |= (aml[byte_count] << ((byte_count << 3) - 4));
  61. byte_zero_mask = 0x0F; /* Use bits [0:3] of byte 0 */
  62. byte_count--;
  63. }
  64. /* Byte 0 is a special case, either bits [0:3] or [0:5] are used */
  65. package_length |= (aml[0] & byte_zero_mask);
  66. return_UINT32(package_length);
  67. }
  68. /*******************************************************************************
  69. *
  70. * FUNCTION: acpi_ps_get_next_package_end
  71. *
  72. * PARAMETERS: parser_state - Current parser state object
  73. *
  74. * RETURN: Pointer to end-of-package +1
  75. *
  76. * DESCRIPTION: Get next package length and return a pointer past the end of
  77. * the package. Consumes the package length field
  78. *
  79. ******************************************************************************/
  80. u8 *acpi_ps_get_next_package_end(struct acpi_parse_state *parser_state)
  81. {
  82. u8 *start = parser_state->aml;
  83. u32 package_length;
  84. ACPI_FUNCTION_TRACE(ps_get_next_package_end);
  85. /* Function below updates parser_state->Aml */
  86. package_length = acpi_ps_get_next_package_length(parser_state);
  87. return_PTR(start + package_length); /* end of package */
  88. }
  89. /*******************************************************************************
  90. *
  91. * FUNCTION: acpi_ps_get_next_namestring
  92. *
  93. * PARAMETERS: parser_state - Current parser state object
  94. *
  95. * RETURN: Pointer to the start of the name string (pointer points into
  96. * the AML.
  97. *
  98. * DESCRIPTION: Get next raw namestring within the AML stream. Handles all name
  99. * prefix characters. Set parser state to point past the string.
  100. * (Name is consumed from the AML.)
  101. *
  102. ******************************************************************************/
  103. char *acpi_ps_get_next_namestring(struct acpi_parse_state *parser_state)
  104. {
  105. u8 *start = parser_state->aml;
  106. u8 *end = parser_state->aml;
  107. ACPI_FUNCTION_TRACE(ps_get_next_namestring);
  108. /* Point past any namestring prefix characters (backslash or carat) */
  109. while (ACPI_IS_ROOT_PREFIX(*end) || ACPI_IS_PARENT_PREFIX(*end)) {
  110. end++;
  111. }
  112. /* Decode the path prefix character */
  113. switch (*end) {
  114. case 0:
  115. /* null_name */
  116. if (end == start) {
  117. start = NULL;
  118. }
  119. end++;
  120. break;
  121. case AML_DUAL_NAME_PREFIX:
  122. /* Two name segments */
  123. end += 1 + (2 * ACPI_NAMESEG_SIZE);
  124. break;
  125. case AML_MULTI_NAME_PREFIX:
  126. /* Multiple name segments, 4 chars each, count in next byte */
  127. end += 2 + (*(end + 1) * ACPI_NAMESEG_SIZE);
  128. break;
  129. default:
  130. /* Single name segment */
  131. end += ACPI_NAMESEG_SIZE;
  132. break;
  133. }
  134. parser_state->aml = end;
  135. return_PTR((char *)start);
  136. }
  137. /*******************************************************************************
  138. *
  139. * FUNCTION: acpi_ps_get_next_namepath
  140. *
  141. * PARAMETERS: parser_state - Current parser state object
  142. * arg - Where the namepath will be stored
  143. * arg_count - If the namepath points to a control method
  144. * the method's argument is returned here.
  145. * possible_method_call - Whether the namepath can possibly be the
  146. * start of a method call
  147. *
  148. * RETURN: Status
  149. *
  150. * DESCRIPTION: Get next name (if method call, return # of required args).
  151. * Names are looked up in the internal namespace to determine
  152. * if the name represents a control method. If a method
  153. * is found, the number of arguments to the method is returned.
  154. * This information is critical for parsing to continue correctly.
  155. *
  156. ******************************************************************************/
  157. acpi_status
  158. acpi_ps_get_next_namepath(struct acpi_walk_state *walk_state,
  159. struct acpi_parse_state *parser_state,
  160. union acpi_parse_object *arg, u8 possible_method_call)
  161. {
  162. acpi_status status;
  163. char *path;
  164. union acpi_parse_object *name_op;
  165. union acpi_operand_object *method_desc;
  166. struct acpi_namespace_node *node;
  167. u8 *start = parser_state->aml;
  168. ACPI_FUNCTION_TRACE(ps_get_next_namepath);
  169. path = acpi_ps_get_next_namestring(parser_state);
  170. acpi_ps_init_op(arg, AML_INT_NAMEPATH_OP);
  171. /* Null path case is allowed, just exit */
  172. if (!path) {
  173. arg->common.value.name = path;
  174. return_ACPI_STATUS(AE_OK);
  175. }
  176. /*
  177. * Lookup the name in the internal namespace, starting with the current
  178. * scope. We don't want to add anything new to the namespace here,
  179. * however, so we use MODE_EXECUTE.
  180. * Allow searching of the parent tree, but don't open a new scope -
  181. * we just want to lookup the object (must be mode EXECUTE to perform
  182. * the upsearch)
  183. */
  184. status = acpi_ns_lookup(walk_state->scope_info, path,
  185. ACPI_TYPE_ANY, ACPI_IMODE_EXECUTE,
  186. ACPI_NS_SEARCH_PARENT | ACPI_NS_DONT_OPEN_SCOPE,
  187. NULL, &node);
  188. /*
  189. * If this name is a control method invocation, we must
  190. * setup the method call
  191. */
  192. if (ACPI_SUCCESS(status) &&
  193. possible_method_call && (node->type == ACPI_TYPE_METHOD)) {
  194. if ((GET_CURRENT_ARG_TYPE(walk_state->arg_types) ==
  195. ARGP_SUPERNAME)
  196. || (GET_CURRENT_ARG_TYPE(walk_state->arg_types) ==
  197. ARGP_TARGET)) {
  198. /*
  199. * acpi_ps_get_next_namestring has increased the AML pointer past
  200. * the method invocation namestring, so we need to restore the
  201. * saved AML pointer back to the original method invocation
  202. * namestring.
  203. */
  204. walk_state->parser_state.aml = start;
  205. walk_state->arg_count = 1;
  206. acpi_ps_init_op(arg, AML_INT_METHODCALL_OP);
  207. }
  208. /* This name is actually a control method invocation */
  209. method_desc = acpi_ns_get_attached_object(node);
  210. ACPI_DEBUG_PRINT((ACPI_DB_PARSE,
  211. "Control Method invocation %4.4s - %p Desc %p Path=%p\n",
  212. node->name.ascii, node, method_desc, path));
  213. name_op = acpi_ps_alloc_op(AML_INT_NAMEPATH_OP, start);
  214. if (!name_op) {
  215. return_ACPI_STATUS(AE_NO_MEMORY);
  216. }
  217. /* Change Arg into a METHOD CALL and attach name to it */
  218. acpi_ps_init_op(arg, AML_INT_METHODCALL_OP);
  219. name_op->common.value.name = path;
  220. /* Point METHODCALL/NAME to the METHOD Node */
  221. name_op->common.node = node;
  222. acpi_ps_append_arg(arg, name_op);
  223. if (!method_desc) {
  224. ACPI_ERROR((AE_INFO,
  225. "Control Method %p has no attached object",
  226. node));
  227. return_ACPI_STATUS(AE_AML_INTERNAL);
  228. }
  229. ACPI_DEBUG_PRINT((ACPI_DB_PARSE,
  230. "Control Method - %p Args %X\n",
  231. node, method_desc->method.param_count));
  232. /* Get the number of arguments to expect */
  233. walk_state->arg_count = method_desc->method.param_count;
  234. return_ACPI_STATUS(AE_OK);
  235. }
  236. /*
  237. * Special handling if the name was not found during the lookup -
  238. * some not_found cases are allowed
  239. */
  240. if (status == AE_NOT_FOUND) {
  241. /* 1) not_found is ok during load pass 1/2 (allow forward references) */
  242. if ((walk_state->parse_flags & ACPI_PARSE_MODE_MASK) !=
  243. ACPI_PARSE_EXECUTE) {
  244. status = AE_OK;
  245. }
  246. /* 2) not_found during a cond_ref_of(x) is ok by definition */
  247. else if (walk_state->op->common.aml_opcode ==
  248. AML_CONDITIONAL_REF_OF_OP) {
  249. status = AE_OK;
  250. }
  251. /*
  252. * 3) not_found while building a Package is ok at this point, we
  253. * may flag as an error later if slack mode is not enabled.
  254. * (Some ASL code depends on allowing this behavior)
  255. */
  256. else if ((arg->common.parent) &&
  257. ((arg->common.parent->common.aml_opcode ==
  258. AML_PACKAGE_OP)
  259. || (arg->common.parent->common.aml_opcode ==
  260. AML_VARIABLE_PACKAGE_OP))) {
  261. status = AE_OK;
  262. }
  263. }
  264. /* Final exception check (may have been changed from code above) */
  265. if (ACPI_FAILURE(status)) {
  266. ACPI_ERROR_NAMESPACE(walk_state->scope_info, path, status);
  267. if ((walk_state->parse_flags & ACPI_PARSE_MODE_MASK) ==
  268. ACPI_PARSE_EXECUTE) {
  269. /* Report a control method execution error */
  270. status = acpi_ds_method_error(status, walk_state);
  271. }
  272. }
  273. /* Save the namepath */
  274. arg->common.value.name = path;
  275. return_ACPI_STATUS(status);
  276. }
  277. /*******************************************************************************
  278. *
  279. * FUNCTION: acpi_ps_get_next_simple_arg
  280. *
  281. * PARAMETERS: parser_state - Current parser state object
  282. * arg_type - The argument type (AML_*_ARG)
  283. * arg - Where the argument is returned
  284. *
  285. * RETURN: None
  286. *
  287. * DESCRIPTION: Get the next simple argument (constant, string, or namestring)
  288. *
  289. ******************************************************************************/
  290. void
  291. acpi_ps_get_next_simple_arg(struct acpi_parse_state *parser_state,
  292. u32 arg_type, union acpi_parse_object *arg)
  293. {
  294. u32 length;
  295. u16 opcode;
  296. u8 *aml = parser_state->aml;
  297. ACPI_FUNCTION_TRACE_U32(ps_get_next_simple_arg, arg_type);
  298. switch (arg_type) {
  299. case ARGP_BYTEDATA:
  300. /* Get 1 byte from the AML stream */
  301. opcode = AML_BYTE_OP;
  302. arg->common.value.integer = (u64) *aml;
  303. length = 1;
  304. break;
  305. case ARGP_WORDDATA:
  306. /* Get 2 bytes from the AML stream */
  307. opcode = AML_WORD_OP;
  308. ACPI_MOVE_16_TO_64(&arg->common.value.integer, aml);
  309. length = 2;
  310. break;
  311. case ARGP_DWORDDATA:
  312. /* Get 4 bytes from the AML stream */
  313. opcode = AML_DWORD_OP;
  314. ACPI_MOVE_32_TO_64(&arg->common.value.integer, aml);
  315. length = 4;
  316. break;
  317. case ARGP_QWORDDATA:
  318. /* Get 8 bytes from the AML stream */
  319. opcode = AML_QWORD_OP;
  320. ACPI_MOVE_64_TO_64(&arg->common.value.integer, aml);
  321. length = 8;
  322. break;
  323. case ARGP_CHARLIST:
  324. /* Get a pointer to the string, point past the string */
  325. opcode = AML_STRING_OP;
  326. arg->common.value.string = ACPI_CAST_PTR(char, aml);
  327. /* Find the null terminator */
  328. length = 0;
  329. while (aml[length]) {
  330. length++;
  331. }
  332. length++;
  333. break;
  334. case ARGP_NAME:
  335. case ARGP_NAMESTRING:
  336. acpi_ps_init_op(arg, AML_INT_NAMEPATH_OP);
  337. arg->common.value.name =
  338. acpi_ps_get_next_namestring(parser_state);
  339. return_VOID;
  340. default:
  341. ACPI_ERROR((AE_INFO, "Invalid ArgType 0x%X", arg_type));
  342. return_VOID;
  343. }
  344. acpi_ps_init_op(arg, opcode);
  345. parser_state->aml += length;
  346. return_VOID;
  347. }
  348. /*******************************************************************************
  349. *
  350. * FUNCTION: acpi_ps_get_next_field
  351. *
  352. * PARAMETERS: parser_state - Current parser state object
  353. *
  354. * RETURN: A newly allocated FIELD op
  355. *
  356. * DESCRIPTION: Get next field (named_field, reserved_field, or access_field)
  357. *
  358. ******************************************************************************/
  359. static union acpi_parse_object *acpi_ps_get_next_field(struct acpi_parse_state
  360. *parser_state)
  361. {
  362. u8 *aml;
  363. union acpi_parse_object *field;
  364. union acpi_parse_object *arg = NULL;
  365. u16 opcode;
  366. u32 name;
  367. u8 access_type;
  368. u8 access_attribute;
  369. u8 access_length;
  370. u32 pkg_length;
  371. u8 *pkg_end;
  372. u32 buffer_length;
  373. ACPI_FUNCTION_TRACE(ps_get_next_field);
  374. ASL_CV_CAPTURE_COMMENTS_ONLY(parser_state);
  375. aml = parser_state->aml;
  376. /* Determine field type */
  377. switch (ACPI_GET8(parser_state->aml)) {
  378. case AML_FIELD_OFFSET_OP:
  379. opcode = AML_INT_RESERVEDFIELD_OP;
  380. parser_state->aml++;
  381. break;
  382. case AML_FIELD_ACCESS_OP:
  383. opcode = AML_INT_ACCESSFIELD_OP;
  384. parser_state->aml++;
  385. break;
  386. case AML_FIELD_CONNECTION_OP:
  387. opcode = AML_INT_CONNECTION_OP;
  388. parser_state->aml++;
  389. break;
  390. case AML_FIELD_EXT_ACCESS_OP:
  391. opcode = AML_INT_EXTACCESSFIELD_OP;
  392. parser_state->aml++;
  393. break;
  394. default:
  395. opcode = AML_INT_NAMEDFIELD_OP;
  396. break;
  397. }
  398. /* Allocate a new field op */
  399. field = acpi_ps_alloc_op(opcode, aml);
  400. if (!field) {
  401. return_PTR(NULL);
  402. }
  403. /* Decode the field type */
  404. ASL_CV_CAPTURE_COMMENTS_ONLY(parser_state);
  405. switch (opcode) {
  406. case AML_INT_NAMEDFIELD_OP:
  407. /* Get the 4-character name */
  408. ACPI_MOVE_32_TO_32(&name, parser_state->aml);
  409. acpi_ps_set_name(field, name);
  410. parser_state->aml += ACPI_NAMESEG_SIZE;
  411. ASL_CV_CAPTURE_COMMENTS_ONLY(parser_state);
  412. #ifdef ACPI_ASL_COMPILER
  413. /*
  414. * Because the package length isn't represented as a parse tree object,
  415. * take comments surrounding this and add to the previously created
  416. * parse node.
  417. */
  418. if (field->common.inline_comment) {
  419. field->common.name_comment =
  420. field->common.inline_comment;
  421. }
  422. field->common.inline_comment = acpi_gbl_current_inline_comment;
  423. acpi_gbl_current_inline_comment = NULL;
  424. #endif
  425. /* Get the length which is encoded as a package length */
  426. field->common.value.size =
  427. acpi_ps_get_next_package_length(parser_state);
  428. break;
  429. case AML_INT_RESERVEDFIELD_OP:
  430. /* Get the length which is encoded as a package length */
  431. field->common.value.size =
  432. acpi_ps_get_next_package_length(parser_state);
  433. break;
  434. case AML_INT_ACCESSFIELD_OP:
  435. case AML_INT_EXTACCESSFIELD_OP:
  436. /*
  437. * Get access_type and access_attrib and merge into the field Op
  438. * access_type is first operand, access_attribute is second. stuff
  439. * these bytes into the node integer value for convenience.
  440. */
  441. /* Get the two bytes (Type/Attribute) */
  442. access_type = ACPI_GET8(parser_state->aml);
  443. parser_state->aml++;
  444. access_attribute = ACPI_GET8(parser_state->aml);
  445. parser_state->aml++;
  446. field->common.value.integer = (u8)access_type;
  447. field->common.value.integer |= (u16)(access_attribute << 8);
  448. /* This opcode has a third byte, access_length */
  449. if (opcode == AML_INT_EXTACCESSFIELD_OP) {
  450. access_length = ACPI_GET8(parser_state->aml);
  451. parser_state->aml++;
  452. field->common.value.integer |=
  453. (u32)(access_length << 16);
  454. }
  455. break;
  456. case AML_INT_CONNECTION_OP:
  457. /*
  458. * Argument for Connection operator can be either a Buffer
  459. * (resource descriptor), or a name_string.
  460. */
  461. aml = parser_state->aml;
  462. if (ACPI_GET8(parser_state->aml) == AML_BUFFER_OP) {
  463. parser_state->aml++;
  464. ASL_CV_CAPTURE_COMMENTS_ONLY(parser_state);
  465. pkg_end = parser_state->aml;
  466. pkg_length =
  467. acpi_ps_get_next_package_length(parser_state);
  468. pkg_end += pkg_length;
  469. ASL_CV_CAPTURE_COMMENTS_ONLY(parser_state);
  470. if (parser_state->aml < pkg_end) {
  471. /* Non-empty list */
  472. arg =
  473. acpi_ps_alloc_op(AML_INT_BYTELIST_OP, aml);
  474. if (!arg) {
  475. acpi_ps_free_op(field);
  476. return_PTR(NULL);
  477. }
  478. /* Get the actual buffer length argument */
  479. opcode = ACPI_GET8(parser_state->aml);
  480. parser_state->aml++;
  481. ASL_CV_CAPTURE_COMMENTS_ONLY(parser_state);
  482. switch (opcode) {
  483. case AML_BYTE_OP: /* AML_BYTEDATA_ARG */
  484. buffer_length =
  485. ACPI_GET8(parser_state->aml);
  486. parser_state->aml += 1;
  487. break;
  488. case AML_WORD_OP: /* AML_WORDDATA_ARG */
  489. buffer_length =
  490. ACPI_GET16(parser_state->aml);
  491. parser_state->aml += 2;
  492. break;
  493. case AML_DWORD_OP: /* AML_DWORDATA_ARG */
  494. buffer_length =
  495. ACPI_GET32(parser_state->aml);
  496. parser_state->aml += 4;
  497. break;
  498. default:
  499. buffer_length = 0;
  500. break;
  501. }
  502. /* Fill in bytelist data */
  503. ASL_CV_CAPTURE_COMMENTS_ONLY(parser_state);
  504. arg->named.value.size = buffer_length;
  505. arg->named.data = parser_state->aml;
  506. }
  507. /* Skip to End of byte data */
  508. parser_state->aml = pkg_end;
  509. } else {
  510. arg = acpi_ps_alloc_op(AML_INT_NAMEPATH_OP, aml);
  511. if (!arg) {
  512. acpi_ps_free_op(field);
  513. return_PTR(NULL);
  514. }
  515. /* Get the Namestring argument */
  516. arg->common.value.name =
  517. acpi_ps_get_next_namestring(parser_state);
  518. }
  519. /* Link the buffer/namestring to parent (CONNECTION_OP) */
  520. acpi_ps_append_arg(field, arg);
  521. break;
  522. default:
  523. /* Opcode was set in previous switch */
  524. break;
  525. }
  526. return_PTR(field);
  527. }
  528. /*******************************************************************************
  529. *
  530. * FUNCTION: acpi_ps_free_field_list
  531. *
  532. * PARAMETERS: start - First Op in field list
  533. *
  534. * RETURN: None.
  535. *
  536. * DESCRIPTION: Free all Op objects inside a field list.
  537. *
  538. ******************************************************************************/
  539. static void acpi_ps_free_field_list(union acpi_parse_object *start)
  540. {
  541. union acpi_parse_object *cur = start;
  542. union acpi_parse_object *next;
  543. union acpi_parse_object *arg;
  544. while (cur) {
  545. next = cur->common.next;
  546. /* AML_INT_CONNECTION_OP can have a single argument */
  547. arg = acpi_ps_get_arg(cur, 0);
  548. if (arg) {
  549. acpi_ps_free_op(arg);
  550. }
  551. acpi_ps_free_op(cur);
  552. cur = next;
  553. }
  554. }
  555. /*******************************************************************************
  556. *
  557. * FUNCTION: acpi_ps_get_next_arg
  558. *
  559. * PARAMETERS: walk_state - Current state
  560. * parser_state - Current parser state object
  561. * arg_type - The argument type (AML_*_ARG)
  562. * return_arg - Where the next arg is returned
  563. *
  564. * RETURN: Status, and an op object containing the next argument.
  565. *
  566. * DESCRIPTION: Get next argument (including complex list arguments that require
  567. * pushing the parser stack)
  568. *
  569. ******************************************************************************/
  570. acpi_status
  571. acpi_ps_get_next_arg(struct acpi_walk_state *walk_state,
  572. struct acpi_parse_state *parser_state,
  573. u32 arg_type, union acpi_parse_object **return_arg)
  574. {
  575. union acpi_parse_object *arg = NULL;
  576. union acpi_parse_object *prev = NULL;
  577. union acpi_parse_object *field;
  578. u32 subop;
  579. acpi_status status = AE_OK;
  580. ACPI_FUNCTION_TRACE_PTR(ps_get_next_arg, parser_state);
  581. ACPI_DEBUG_PRINT((ACPI_DB_PARSE,
  582. "Expected argument type ARGP: %s (%2.2X)\n",
  583. acpi_ut_get_argument_type_name(arg_type), arg_type));
  584. switch (arg_type) {
  585. case ARGP_BYTEDATA:
  586. case ARGP_WORDDATA:
  587. case ARGP_DWORDDATA:
  588. case ARGP_CHARLIST:
  589. case ARGP_NAME:
  590. case ARGP_NAMESTRING:
  591. /* Constants, strings, and namestrings are all the same size */
  592. arg = acpi_ps_alloc_op(AML_BYTE_OP, parser_state->aml);
  593. if (!arg) {
  594. return_ACPI_STATUS(AE_NO_MEMORY);
  595. }
  596. acpi_ps_get_next_simple_arg(parser_state, arg_type, arg);
  597. break;
  598. case ARGP_PKGLENGTH:
  599. /* Package length, nothing returned */
  600. parser_state->pkg_end =
  601. acpi_ps_get_next_package_end(parser_state);
  602. break;
  603. case ARGP_FIELDLIST:
  604. if (parser_state->aml < parser_state->pkg_end) {
  605. /* Non-empty list */
  606. while (parser_state->aml < parser_state->pkg_end) {
  607. field = acpi_ps_get_next_field(parser_state);
  608. if (!field) {
  609. if (arg) {
  610. acpi_ps_free_field_list(arg);
  611. }
  612. return_ACPI_STATUS(AE_NO_MEMORY);
  613. }
  614. if (prev) {
  615. prev->common.next = field;
  616. } else {
  617. arg = field;
  618. }
  619. prev = field;
  620. }
  621. /* Skip to End of byte data */
  622. parser_state->aml = parser_state->pkg_end;
  623. }
  624. break;
  625. case ARGP_BYTELIST:
  626. if (parser_state->aml < parser_state->pkg_end) {
  627. /* Non-empty list */
  628. arg = acpi_ps_alloc_op(AML_INT_BYTELIST_OP,
  629. parser_state->aml);
  630. if (!arg) {
  631. return_ACPI_STATUS(AE_NO_MEMORY);
  632. }
  633. /* Fill in bytelist data */
  634. arg->common.value.size = (u32)
  635. ACPI_PTR_DIFF(parser_state->pkg_end,
  636. parser_state->aml);
  637. arg->named.data = parser_state->aml;
  638. /* Skip to End of byte data */
  639. parser_state->aml = parser_state->pkg_end;
  640. }
  641. break;
  642. case ARGP_SIMPLENAME:
  643. case ARGP_NAME_OR_REF:
  644. ACPI_DEBUG_PRINT((ACPI_DB_PARSE,
  645. "**** SimpleName/NameOrRef: %s (%2.2X)\n",
  646. acpi_ut_get_argument_type_name(arg_type),
  647. arg_type));
  648. subop = acpi_ps_peek_opcode(parser_state);
  649. if (subop == 0 ||
  650. acpi_ps_is_leading_char(subop) ||
  651. ACPI_IS_ROOT_PREFIX(subop) ||
  652. ACPI_IS_PARENT_PREFIX(subop)) {
  653. /* null_name or name_string */
  654. arg =
  655. acpi_ps_alloc_op(AML_INT_NAMEPATH_OP,
  656. parser_state->aml);
  657. if (!arg) {
  658. return_ACPI_STATUS(AE_NO_MEMORY);
  659. }
  660. status =
  661. acpi_ps_get_next_namepath(walk_state, parser_state,
  662. arg,
  663. ACPI_NOT_METHOD_CALL);
  664. if (ACPI_FAILURE(status)) {
  665. acpi_ps_free_op(arg);
  666. return_ACPI_STATUS(status);
  667. }
  668. } else {
  669. /* Single complex argument, nothing returned */
  670. walk_state->arg_count = 1;
  671. }
  672. break;
  673. case ARGP_TARGET:
  674. case ARGP_SUPERNAME:
  675. ACPI_DEBUG_PRINT((ACPI_DB_PARSE,
  676. "**** Target/Supername: %s (%2.2X)\n",
  677. acpi_ut_get_argument_type_name(arg_type),
  678. arg_type));
  679. subop = acpi_ps_peek_opcode(parser_state);
  680. if (subop == 0 ||
  681. acpi_ps_is_leading_char(subop) ||
  682. ACPI_IS_ROOT_PREFIX(subop) ||
  683. ACPI_IS_PARENT_PREFIX(subop)) {
  684. /* NULL target (zero). Convert to a NULL namepath */
  685. arg =
  686. acpi_ps_alloc_op(AML_INT_NAMEPATH_OP,
  687. parser_state->aml);
  688. if (!arg) {
  689. return_ACPI_STATUS(AE_NO_MEMORY);
  690. }
  691. status =
  692. acpi_ps_get_next_namepath(walk_state, parser_state,
  693. arg,
  694. ACPI_POSSIBLE_METHOD_CALL);
  695. if (ACPI_FAILURE(status)) {
  696. acpi_ps_free_op(arg);
  697. return_ACPI_STATUS(status);
  698. }
  699. if (arg->common.aml_opcode == AML_INT_METHODCALL_OP) {
  700. /* Free method call op and corresponding namestring sub-ob */
  701. acpi_ps_free_op(arg->common.value.arg);
  702. acpi_ps_free_op(arg);
  703. arg = NULL;
  704. walk_state->arg_count = 1;
  705. }
  706. } else {
  707. /* Single complex argument, nothing returned */
  708. walk_state->arg_count = 1;
  709. }
  710. break;
  711. case ARGP_DATAOBJ:
  712. case ARGP_TERMARG:
  713. ACPI_DEBUG_PRINT((ACPI_DB_PARSE,
  714. "**** TermArg/DataObj: %s (%2.2X)\n",
  715. acpi_ut_get_argument_type_name(arg_type),
  716. arg_type));
  717. /* Single complex argument, nothing returned */
  718. walk_state->arg_count = 1;
  719. break;
  720. case ARGP_DATAOBJLIST:
  721. case ARGP_TERMLIST:
  722. case ARGP_OBJLIST:
  723. if (parser_state->aml < parser_state->pkg_end) {
  724. /* Non-empty list of variable arguments, nothing returned */
  725. walk_state->arg_count = ACPI_VAR_ARGS;
  726. }
  727. break;
  728. default:
  729. ACPI_ERROR((AE_INFO, "Invalid ArgType: 0x%X", arg_type));
  730. status = AE_AML_OPERAND_TYPE;
  731. break;
  732. }
  733. *return_arg = arg;
  734. return_ACPI_STATUS(status);
  735. }