dbconvert.c 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449
  1. // SPDX-License-Identifier: BSD-3-Clause OR GPL-2.0
  2. /*******************************************************************************
  3. *
  4. * Module Name: dbconvert - debugger miscellaneous conversion routines
  5. *
  6. ******************************************************************************/
  7. #include <acpi/acpi.h>
  8. #include "accommon.h"
  9. #include "acdebug.h"
  10. #define _COMPONENT ACPI_CA_DEBUGGER
  11. ACPI_MODULE_NAME("dbconvert")
  12. #define DB_DEFAULT_PKG_ELEMENTS 33
  13. /*******************************************************************************
  14. *
  15. * FUNCTION: acpi_db_hex_char_to_value
  16. *
  17. * PARAMETERS: hex_char - Ascii Hex digit, 0-9|a-f|A-F
  18. * return_value - Where the converted value is returned
  19. *
  20. * RETURN: Status
  21. *
  22. * DESCRIPTION: Convert a single hex character to a 4-bit number (0-16).
  23. *
  24. ******************************************************************************/
  25. acpi_status acpi_db_hex_char_to_value(int hex_char, u8 *return_value)
  26. {
  27. u8 value;
  28. /* Digit must be ascii [0-9a-fA-F] */
  29. if (!isxdigit(hex_char)) {
  30. return (AE_BAD_HEX_CONSTANT);
  31. }
  32. if (hex_char <= 0x39) {
  33. value = (u8)(hex_char - 0x30);
  34. } else {
  35. value = (u8)(toupper(hex_char) - 0x37);
  36. }
  37. *return_value = value;
  38. return (AE_OK);
  39. }
  40. /*******************************************************************************
  41. *
  42. * FUNCTION: acpi_db_hex_byte_to_binary
  43. *
  44. * PARAMETERS: hex_byte - Double hex digit (0x00 - 0xFF) in format:
  45. * hi_byte then lo_byte.
  46. * return_value - Where the converted value is returned
  47. *
  48. * RETURN: Status
  49. *
  50. * DESCRIPTION: Convert two hex characters to an 8 bit number (0 - 255).
  51. *
  52. ******************************************************************************/
  53. static acpi_status acpi_db_hex_byte_to_binary(char *hex_byte, u8 *return_value)
  54. {
  55. u8 local0;
  56. u8 local1;
  57. acpi_status status;
  58. /* High byte */
  59. status = acpi_db_hex_char_to_value(hex_byte[0], &local0);
  60. if (ACPI_FAILURE(status)) {
  61. return (status);
  62. }
  63. /* Low byte */
  64. status = acpi_db_hex_char_to_value(hex_byte[1], &local1);
  65. if (ACPI_FAILURE(status)) {
  66. return (status);
  67. }
  68. *return_value = (u8)((local0 << 4) | local1);
  69. return (AE_OK);
  70. }
  71. /*******************************************************************************
  72. *
  73. * FUNCTION: acpi_db_convert_to_buffer
  74. *
  75. * PARAMETERS: string - Input string to be converted
  76. * object - Where the buffer object is returned
  77. *
  78. * RETURN: Status
  79. *
  80. * DESCRIPTION: Convert a string to a buffer object. String is treated a list
  81. * of buffer elements, each separated by a space or comma.
  82. *
  83. ******************************************************************************/
  84. static acpi_status
  85. acpi_db_convert_to_buffer(char *string, union acpi_object *object)
  86. {
  87. u32 i;
  88. u32 j;
  89. u32 length;
  90. u8 *buffer;
  91. acpi_status status;
  92. /* Generate the final buffer length */
  93. for (i = 0, length = 0; string[i];) {
  94. i += 2;
  95. length++;
  96. while (string[i] && ((string[i] == ',') || (string[i] == ' '))) {
  97. i++;
  98. }
  99. }
  100. buffer = ACPI_ALLOCATE(length);
  101. if (!buffer) {
  102. return (AE_NO_MEMORY);
  103. }
  104. /* Convert the command line bytes to the buffer */
  105. for (i = 0, j = 0; string[i];) {
  106. status = acpi_db_hex_byte_to_binary(&string[i], &buffer[j]);
  107. if (ACPI_FAILURE(status)) {
  108. ACPI_FREE(buffer);
  109. return (status);
  110. }
  111. j++;
  112. i += 2;
  113. while (string[i] && ((string[i] == ',') || (string[i] == ' '))) {
  114. i++;
  115. }
  116. }
  117. object->type = ACPI_TYPE_BUFFER;
  118. object->buffer.pointer = buffer;
  119. object->buffer.length = length;
  120. return (AE_OK);
  121. }
  122. /*******************************************************************************
  123. *
  124. * FUNCTION: acpi_db_convert_to_package
  125. *
  126. * PARAMETERS: string - Input string to be converted
  127. * object - Where the package object is returned
  128. *
  129. * RETURN: Status
  130. *
  131. * DESCRIPTION: Convert a string to a package object. Handles nested packages
  132. * via recursion with acpi_db_convert_to_object.
  133. *
  134. ******************************************************************************/
  135. acpi_status acpi_db_convert_to_package(char *string, union acpi_object *object)
  136. {
  137. char *this;
  138. char *next;
  139. u32 i;
  140. acpi_object_type type;
  141. union acpi_object *elements;
  142. acpi_status status;
  143. elements =
  144. ACPI_ALLOCATE_ZEROED(DB_DEFAULT_PKG_ELEMENTS *
  145. sizeof(union acpi_object));
  146. this = string;
  147. for (i = 0; i < (DB_DEFAULT_PKG_ELEMENTS - 1); i++) {
  148. this = acpi_db_get_next_token(this, &next, &type);
  149. if (!this) {
  150. break;
  151. }
  152. /* Recursive call to convert each package element */
  153. status = acpi_db_convert_to_object(type, this, &elements[i]);
  154. if (ACPI_FAILURE(status)) {
  155. acpi_db_delete_objects(i + 1, elements);
  156. ACPI_FREE(elements);
  157. return (status);
  158. }
  159. this = next;
  160. }
  161. object->type = ACPI_TYPE_PACKAGE;
  162. object->package.count = i;
  163. object->package.elements = elements;
  164. return (AE_OK);
  165. }
  166. /*******************************************************************************
  167. *
  168. * FUNCTION: acpi_db_convert_to_object
  169. *
  170. * PARAMETERS: type - Object type as determined by parser
  171. * string - Input string to be converted
  172. * object - Where the new object is returned
  173. *
  174. * RETURN: Status
  175. *
  176. * DESCRIPTION: Convert a typed and tokenized string to a union acpi_object. Typing:
  177. * 1) String objects were surrounded by quotes.
  178. * 2) Buffer objects were surrounded by parentheses.
  179. * 3) Package objects were surrounded by brackets "[]".
  180. * 4) All standalone tokens are treated as integers.
  181. *
  182. ******************************************************************************/
  183. acpi_status
  184. acpi_db_convert_to_object(acpi_object_type type,
  185. char *string, union acpi_object *object)
  186. {
  187. acpi_status status = AE_OK;
  188. switch (type) {
  189. case ACPI_TYPE_STRING:
  190. object->type = ACPI_TYPE_STRING;
  191. object->string.pointer = string;
  192. object->string.length = (u32)strlen(string);
  193. break;
  194. case ACPI_TYPE_BUFFER:
  195. status = acpi_db_convert_to_buffer(string, object);
  196. break;
  197. case ACPI_TYPE_PACKAGE:
  198. status = acpi_db_convert_to_package(string, object);
  199. break;
  200. default:
  201. object->type = ACPI_TYPE_INTEGER;
  202. status = acpi_ut_strtoul64(string, &object->integer.value);
  203. break;
  204. }
  205. return (status);
  206. }
  207. /*******************************************************************************
  208. *
  209. * FUNCTION: acpi_db_encode_pld_buffer
  210. *
  211. * PARAMETERS: pld_info - _PLD buffer struct (Using local struct)
  212. *
  213. * RETURN: Encode _PLD buffer suitable for return value from _PLD
  214. *
  215. * DESCRIPTION: Bit-packs a _PLD buffer struct. Used to test the _PLD macros
  216. *
  217. ******************************************************************************/
  218. u8 *acpi_db_encode_pld_buffer(struct acpi_pld_info *pld_info)
  219. {
  220. u32 *buffer;
  221. u32 dword;
  222. buffer = ACPI_ALLOCATE_ZEROED(ACPI_PLD_BUFFER_SIZE);
  223. if (!buffer) {
  224. return (NULL);
  225. }
  226. /* First 32 bits */
  227. dword = 0;
  228. ACPI_PLD_SET_REVISION(&dword, pld_info->revision);
  229. ACPI_PLD_SET_IGNORE_COLOR(&dword, pld_info->ignore_color);
  230. ACPI_PLD_SET_RED(&dword, pld_info->red);
  231. ACPI_PLD_SET_GREEN(&dword, pld_info->green);
  232. ACPI_PLD_SET_BLUE(&dword, pld_info->blue);
  233. ACPI_MOVE_32_TO_32(&buffer[0], &dword);
  234. /* Second 32 bits */
  235. dword = 0;
  236. ACPI_PLD_SET_WIDTH(&dword, pld_info->width);
  237. ACPI_PLD_SET_HEIGHT(&dword, pld_info->height);
  238. ACPI_MOVE_32_TO_32(&buffer[1], &dword);
  239. /* Third 32 bits */
  240. dword = 0;
  241. ACPI_PLD_SET_USER_VISIBLE(&dword, pld_info->user_visible);
  242. ACPI_PLD_SET_DOCK(&dword, pld_info->dock);
  243. ACPI_PLD_SET_LID(&dword, pld_info->lid);
  244. ACPI_PLD_SET_PANEL(&dword, pld_info->panel);
  245. ACPI_PLD_SET_VERTICAL(&dword, pld_info->vertical_position);
  246. ACPI_PLD_SET_HORIZONTAL(&dword, pld_info->horizontal_position);
  247. ACPI_PLD_SET_SHAPE(&dword, pld_info->shape);
  248. ACPI_PLD_SET_ORIENTATION(&dword, pld_info->group_orientation);
  249. ACPI_PLD_SET_TOKEN(&dword, pld_info->group_token);
  250. ACPI_PLD_SET_POSITION(&dword, pld_info->group_position);
  251. ACPI_PLD_SET_BAY(&dword, pld_info->bay);
  252. ACPI_MOVE_32_TO_32(&buffer[2], &dword);
  253. /* Fourth 32 bits */
  254. dword = 0;
  255. ACPI_PLD_SET_EJECTABLE(&dword, pld_info->ejectable);
  256. ACPI_PLD_SET_OSPM_EJECT(&dword, pld_info->ospm_eject_required);
  257. ACPI_PLD_SET_CABINET(&dword, pld_info->cabinet_number);
  258. ACPI_PLD_SET_CARD_CAGE(&dword, pld_info->card_cage_number);
  259. ACPI_PLD_SET_REFERENCE(&dword, pld_info->reference);
  260. ACPI_PLD_SET_ROTATION(&dword, pld_info->rotation);
  261. ACPI_PLD_SET_ORDER(&dword, pld_info->order);
  262. ACPI_MOVE_32_TO_32(&buffer[3], &dword);
  263. if (pld_info->revision >= 2) {
  264. /* Fifth 32 bits */
  265. dword = 0;
  266. ACPI_PLD_SET_VERT_OFFSET(&dword, pld_info->vertical_offset);
  267. ACPI_PLD_SET_HORIZ_OFFSET(&dword, pld_info->horizontal_offset);
  268. ACPI_MOVE_32_TO_32(&buffer[4], &dword);
  269. }
  270. return (ACPI_CAST_PTR(u8, buffer));
  271. }
  272. /*******************************************************************************
  273. *
  274. * FUNCTION: acpi_db_dump_pld_buffer
  275. *
  276. * PARAMETERS: obj_desc - Object returned from _PLD method
  277. *
  278. * RETURN: None.
  279. *
  280. * DESCRIPTION: Dumps formatted contents of a _PLD return buffer.
  281. *
  282. ******************************************************************************/
  283. #define ACPI_PLD_OUTPUT "%20s : %-6X\n"
  284. void acpi_db_dump_pld_buffer(union acpi_object *obj_desc)
  285. {
  286. union acpi_object *buffer_desc;
  287. struct acpi_pld_info *pld_info;
  288. u8 *new_buffer;
  289. acpi_status status;
  290. /* Object must be of type Package with at least one Buffer element */
  291. if (obj_desc->type != ACPI_TYPE_PACKAGE) {
  292. return;
  293. }
  294. buffer_desc = &obj_desc->package.elements[0];
  295. if (buffer_desc->type != ACPI_TYPE_BUFFER) {
  296. return;
  297. }
  298. /* Convert _PLD buffer to local _PLD struct */
  299. status = acpi_decode_pld_buffer(buffer_desc->buffer.pointer,
  300. buffer_desc->buffer.length, &pld_info);
  301. if (ACPI_FAILURE(status)) {
  302. return;
  303. }
  304. /* Encode local _PLD struct back to a _PLD buffer */
  305. new_buffer = acpi_db_encode_pld_buffer(pld_info);
  306. if (!new_buffer) {
  307. goto exit;
  308. }
  309. /* The two bit-packed buffers should match */
  310. if (memcmp(new_buffer, buffer_desc->buffer.pointer,
  311. buffer_desc->buffer.length)) {
  312. acpi_os_printf
  313. ("Converted _PLD buffer does not compare. New:\n");
  314. acpi_ut_dump_buffer(new_buffer,
  315. buffer_desc->buffer.length, DB_BYTE_DISPLAY,
  316. 0);
  317. }
  318. /* First 32-bit dword */
  319. acpi_os_printf(ACPI_PLD_OUTPUT, "PLD_Revision", pld_info->revision);
  320. acpi_os_printf(ACPI_PLD_OUTPUT, "PLD_IgnoreColor",
  321. pld_info->ignore_color);
  322. acpi_os_printf(ACPI_PLD_OUTPUT, "PLD_Red", pld_info->red);
  323. acpi_os_printf(ACPI_PLD_OUTPUT, "PLD_Green", pld_info->green);
  324. acpi_os_printf(ACPI_PLD_OUTPUT, "PLD_Blue", pld_info->blue);
  325. /* Second 32-bit dword */
  326. acpi_os_printf(ACPI_PLD_OUTPUT, "PLD_Width", pld_info->width);
  327. acpi_os_printf(ACPI_PLD_OUTPUT, "PLD_Height", pld_info->height);
  328. /* Third 32-bit dword */
  329. acpi_os_printf(ACPI_PLD_OUTPUT, "PLD_UserVisible",
  330. pld_info->user_visible);
  331. acpi_os_printf(ACPI_PLD_OUTPUT, "PLD_Dock", pld_info->dock);
  332. acpi_os_printf(ACPI_PLD_OUTPUT, "PLD_Lid", pld_info->lid);
  333. acpi_os_printf(ACPI_PLD_OUTPUT, "PLD_Panel", pld_info->panel);
  334. acpi_os_printf(ACPI_PLD_OUTPUT, "PLD_VerticalPosition",
  335. pld_info->vertical_position);
  336. acpi_os_printf(ACPI_PLD_OUTPUT, "PLD_HorizontalPosition",
  337. pld_info->horizontal_position);
  338. acpi_os_printf(ACPI_PLD_OUTPUT, "PLD_Shape", pld_info->shape);
  339. acpi_os_printf(ACPI_PLD_OUTPUT, "PLD_GroupOrientation",
  340. pld_info->group_orientation);
  341. acpi_os_printf(ACPI_PLD_OUTPUT, "PLD_GroupToken",
  342. pld_info->group_token);
  343. acpi_os_printf(ACPI_PLD_OUTPUT, "PLD_GroupPosition",
  344. pld_info->group_position);
  345. acpi_os_printf(ACPI_PLD_OUTPUT, "PLD_Bay", pld_info->bay);
  346. /* Fourth 32-bit dword */
  347. acpi_os_printf(ACPI_PLD_OUTPUT, "PLD_Ejectable", pld_info->ejectable);
  348. acpi_os_printf(ACPI_PLD_OUTPUT, "PLD_EjectRequired",
  349. pld_info->ospm_eject_required);
  350. acpi_os_printf(ACPI_PLD_OUTPUT, "PLD_CabinetNumber",
  351. pld_info->cabinet_number);
  352. acpi_os_printf(ACPI_PLD_OUTPUT, "PLD_CardCageNumber",
  353. pld_info->card_cage_number);
  354. acpi_os_printf(ACPI_PLD_OUTPUT, "PLD_Reference", pld_info->reference);
  355. acpi_os_printf(ACPI_PLD_OUTPUT, "PLD_Rotation", pld_info->rotation);
  356. acpi_os_printf(ACPI_PLD_OUTPUT, "PLD_Order", pld_info->order);
  357. /* Fifth 32-bit dword */
  358. if (buffer_desc->buffer.length > 16) {
  359. acpi_os_printf(ACPI_PLD_OUTPUT, "PLD_VerticalOffset",
  360. pld_info->vertical_offset);
  361. acpi_os_printf(ACPI_PLD_OUTPUT, "PLD_HorizontalOffset",
  362. pld_info->horizontal_offset);
  363. }
  364. ACPI_FREE(new_buffer);
  365. exit:
  366. ACPI_FREE(pld_info);
  367. }