dbconvert.c 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453
  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. /* Skip all preceding white space */
  93. acpi_ut_remove_whitespace(&string);
  94. /* Generate the final buffer length */
  95. for (i = 0, length = 0; string[i];) {
  96. i += 2;
  97. length++;
  98. while (string[i] && ((string[i] == ',') || (string[i] == ' '))) {
  99. i++;
  100. }
  101. }
  102. buffer = ACPI_ALLOCATE(length);
  103. if (!buffer) {
  104. return (AE_NO_MEMORY);
  105. }
  106. /* Convert the command line bytes to the buffer */
  107. for (i = 0, j = 0; string[i];) {
  108. status = acpi_db_hex_byte_to_binary(&string[i], &buffer[j]);
  109. if (ACPI_FAILURE(status)) {
  110. ACPI_FREE(buffer);
  111. return (status);
  112. }
  113. j++;
  114. i += 2;
  115. while (string[i] && ((string[i] == ',') || (string[i] == ' '))) {
  116. i++;
  117. }
  118. }
  119. object->type = ACPI_TYPE_BUFFER;
  120. object->buffer.pointer = buffer;
  121. object->buffer.length = length;
  122. return (AE_OK);
  123. }
  124. /*******************************************************************************
  125. *
  126. * FUNCTION: acpi_db_convert_to_package
  127. *
  128. * PARAMETERS: string - Input string to be converted
  129. * object - Where the package object is returned
  130. *
  131. * RETURN: Status
  132. *
  133. * DESCRIPTION: Convert a string to a package object. Handles nested packages
  134. * via recursion with acpi_db_convert_to_object.
  135. *
  136. ******************************************************************************/
  137. acpi_status acpi_db_convert_to_package(char *string, union acpi_object *object)
  138. {
  139. char *this;
  140. char *next;
  141. u32 i;
  142. acpi_object_type type;
  143. union acpi_object *elements;
  144. acpi_status status;
  145. elements =
  146. ACPI_ALLOCATE_ZEROED(DB_DEFAULT_PKG_ELEMENTS *
  147. sizeof(union acpi_object));
  148. this = string;
  149. for (i = 0; i < (DB_DEFAULT_PKG_ELEMENTS - 1); i++) {
  150. this = acpi_db_get_next_token(this, &next, &type);
  151. if (!this) {
  152. break;
  153. }
  154. /* Recursive call to convert each package element */
  155. status = acpi_db_convert_to_object(type, this, &elements[i]);
  156. if (ACPI_FAILURE(status)) {
  157. acpi_db_delete_objects(i + 1, elements);
  158. ACPI_FREE(elements);
  159. return (status);
  160. }
  161. this = next;
  162. }
  163. object->type = ACPI_TYPE_PACKAGE;
  164. object->package.count = i;
  165. object->package.elements = elements;
  166. return (AE_OK);
  167. }
  168. /*******************************************************************************
  169. *
  170. * FUNCTION: acpi_db_convert_to_object
  171. *
  172. * PARAMETERS: type - Object type as determined by parser
  173. * string - Input string to be converted
  174. * object - Where the new object is returned
  175. *
  176. * RETURN: Status
  177. *
  178. * DESCRIPTION: Convert a typed and tokenized string to a union acpi_object. Typing:
  179. * 1) String objects were surrounded by quotes.
  180. * 2) Buffer objects were surrounded by parentheses.
  181. * 3) Package objects were surrounded by brackets "[]".
  182. * 4) All standalone tokens are treated as integers.
  183. *
  184. ******************************************************************************/
  185. acpi_status
  186. acpi_db_convert_to_object(acpi_object_type type,
  187. char *string, union acpi_object *object)
  188. {
  189. acpi_status status = AE_OK;
  190. switch (type) {
  191. case ACPI_TYPE_STRING:
  192. object->type = ACPI_TYPE_STRING;
  193. object->string.pointer = string;
  194. object->string.length = (u32)strlen(string);
  195. break;
  196. case ACPI_TYPE_BUFFER:
  197. status = acpi_db_convert_to_buffer(string, object);
  198. break;
  199. case ACPI_TYPE_PACKAGE:
  200. status = acpi_db_convert_to_package(string, object);
  201. break;
  202. default:
  203. object->type = ACPI_TYPE_INTEGER;
  204. status = acpi_ut_strtoul64(string, &object->integer.value);
  205. break;
  206. }
  207. return (status);
  208. }
  209. /*******************************************************************************
  210. *
  211. * FUNCTION: acpi_db_encode_pld_buffer
  212. *
  213. * PARAMETERS: pld_info - _PLD buffer struct (Using local struct)
  214. *
  215. * RETURN: Encode _PLD buffer suitable for return value from _PLD
  216. *
  217. * DESCRIPTION: Bit-packs a _PLD buffer struct. Used to test the _PLD macros
  218. *
  219. ******************************************************************************/
  220. u8 *acpi_db_encode_pld_buffer(struct acpi_pld_info *pld_info)
  221. {
  222. u32 *buffer;
  223. u32 dword;
  224. buffer = ACPI_ALLOCATE_ZEROED(ACPI_PLD_BUFFER_SIZE);
  225. if (!buffer) {
  226. return (NULL);
  227. }
  228. /* First 32 bits */
  229. dword = 0;
  230. ACPI_PLD_SET_REVISION(&dword, pld_info->revision);
  231. ACPI_PLD_SET_IGNORE_COLOR(&dword, pld_info->ignore_color);
  232. ACPI_PLD_SET_RED(&dword, pld_info->red);
  233. ACPI_PLD_SET_GREEN(&dword, pld_info->green);
  234. ACPI_PLD_SET_BLUE(&dword, pld_info->blue);
  235. ACPI_MOVE_32_TO_32(&buffer[0], &dword);
  236. /* Second 32 bits */
  237. dword = 0;
  238. ACPI_PLD_SET_WIDTH(&dword, pld_info->width);
  239. ACPI_PLD_SET_HEIGHT(&dword, pld_info->height);
  240. ACPI_MOVE_32_TO_32(&buffer[1], &dword);
  241. /* Third 32 bits */
  242. dword = 0;
  243. ACPI_PLD_SET_USER_VISIBLE(&dword, pld_info->user_visible);
  244. ACPI_PLD_SET_DOCK(&dword, pld_info->dock);
  245. ACPI_PLD_SET_LID(&dword, pld_info->lid);
  246. ACPI_PLD_SET_PANEL(&dword, pld_info->panel);
  247. ACPI_PLD_SET_VERTICAL(&dword, pld_info->vertical_position);
  248. ACPI_PLD_SET_HORIZONTAL(&dword, pld_info->horizontal_position);
  249. ACPI_PLD_SET_SHAPE(&dword, pld_info->shape);
  250. ACPI_PLD_SET_ORIENTATION(&dword, pld_info->group_orientation);
  251. ACPI_PLD_SET_TOKEN(&dword, pld_info->group_token);
  252. ACPI_PLD_SET_POSITION(&dword, pld_info->group_position);
  253. ACPI_PLD_SET_BAY(&dword, pld_info->bay);
  254. ACPI_MOVE_32_TO_32(&buffer[2], &dword);
  255. /* Fourth 32 bits */
  256. dword = 0;
  257. ACPI_PLD_SET_EJECTABLE(&dword, pld_info->ejectable);
  258. ACPI_PLD_SET_OSPM_EJECT(&dword, pld_info->ospm_eject_required);
  259. ACPI_PLD_SET_CABINET(&dword, pld_info->cabinet_number);
  260. ACPI_PLD_SET_CARD_CAGE(&dword, pld_info->card_cage_number);
  261. ACPI_PLD_SET_REFERENCE(&dword, pld_info->reference);
  262. ACPI_PLD_SET_ROTATION(&dword, pld_info->rotation);
  263. ACPI_PLD_SET_ORDER(&dword, pld_info->order);
  264. ACPI_MOVE_32_TO_32(&buffer[3], &dword);
  265. if (pld_info->revision >= 2) {
  266. /* Fifth 32 bits */
  267. dword = 0;
  268. ACPI_PLD_SET_VERT_OFFSET(&dword, pld_info->vertical_offset);
  269. ACPI_PLD_SET_HORIZ_OFFSET(&dword, pld_info->horizontal_offset);
  270. ACPI_MOVE_32_TO_32(&buffer[4], &dword);
  271. }
  272. return (ACPI_CAST_PTR(u8, buffer));
  273. }
  274. /*******************************************************************************
  275. *
  276. * FUNCTION: acpi_db_dump_pld_buffer
  277. *
  278. * PARAMETERS: obj_desc - Object returned from _PLD method
  279. *
  280. * RETURN: None.
  281. *
  282. * DESCRIPTION: Dumps formatted contents of a _PLD return buffer.
  283. *
  284. ******************************************************************************/
  285. #define ACPI_PLD_OUTPUT "%20s : %-6X\n"
  286. void acpi_db_dump_pld_buffer(union acpi_object *obj_desc)
  287. {
  288. union acpi_object *buffer_desc;
  289. struct acpi_pld_info *pld_info;
  290. u8 *new_buffer;
  291. acpi_status status;
  292. /* Object must be of type Package with at least one Buffer element */
  293. if (obj_desc->type != ACPI_TYPE_PACKAGE) {
  294. return;
  295. }
  296. buffer_desc = &obj_desc->package.elements[0];
  297. if (buffer_desc->type != ACPI_TYPE_BUFFER) {
  298. return;
  299. }
  300. /* Convert _PLD buffer to local _PLD struct */
  301. status = acpi_decode_pld_buffer(buffer_desc->buffer.pointer,
  302. buffer_desc->buffer.length, &pld_info);
  303. if (ACPI_FAILURE(status)) {
  304. return;
  305. }
  306. /* Encode local _PLD struct back to a _PLD buffer */
  307. new_buffer = acpi_db_encode_pld_buffer(pld_info);
  308. if (!new_buffer) {
  309. goto exit;
  310. }
  311. /* The two bit-packed buffers should match */
  312. if (memcmp(new_buffer, buffer_desc->buffer.pointer,
  313. buffer_desc->buffer.length)) {
  314. acpi_os_printf
  315. ("Converted _PLD buffer does not compare. New:\n");
  316. acpi_ut_dump_buffer(new_buffer,
  317. buffer_desc->buffer.length, DB_BYTE_DISPLAY,
  318. 0);
  319. }
  320. /* First 32-bit dword */
  321. acpi_os_printf(ACPI_PLD_OUTPUT, "PLD_Revision", pld_info->revision);
  322. acpi_os_printf(ACPI_PLD_OUTPUT, "PLD_IgnoreColor",
  323. pld_info->ignore_color);
  324. acpi_os_printf(ACPI_PLD_OUTPUT, "PLD_Red", pld_info->red);
  325. acpi_os_printf(ACPI_PLD_OUTPUT, "PLD_Green", pld_info->green);
  326. acpi_os_printf(ACPI_PLD_OUTPUT, "PLD_Blue", pld_info->blue);
  327. /* Second 32-bit dword */
  328. acpi_os_printf(ACPI_PLD_OUTPUT, "PLD_Width", pld_info->width);
  329. acpi_os_printf(ACPI_PLD_OUTPUT, "PLD_Height", pld_info->height);
  330. /* Third 32-bit dword */
  331. acpi_os_printf(ACPI_PLD_OUTPUT, "PLD_UserVisible",
  332. pld_info->user_visible);
  333. acpi_os_printf(ACPI_PLD_OUTPUT, "PLD_Dock", pld_info->dock);
  334. acpi_os_printf(ACPI_PLD_OUTPUT, "PLD_Lid", pld_info->lid);
  335. acpi_os_printf(ACPI_PLD_OUTPUT, "PLD_Panel", pld_info->panel);
  336. acpi_os_printf(ACPI_PLD_OUTPUT, "PLD_VerticalPosition",
  337. pld_info->vertical_position);
  338. acpi_os_printf(ACPI_PLD_OUTPUT, "PLD_HorizontalPosition",
  339. pld_info->horizontal_position);
  340. acpi_os_printf(ACPI_PLD_OUTPUT, "PLD_Shape", pld_info->shape);
  341. acpi_os_printf(ACPI_PLD_OUTPUT, "PLD_GroupOrientation",
  342. pld_info->group_orientation);
  343. acpi_os_printf(ACPI_PLD_OUTPUT, "PLD_GroupToken",
  344. pld_info->group_token);
  345. acpi_os_printf(ACPI_PLD_OUTPUT, "PLD_GroupPosition",
  346. pld_info->group_position);
  347. acpi_os_printf(ACPI_PLD_OUTPUT, "PLD_Bay", pld_info->bay);
  348. /* Fourth 32-bit dword */
  349. acpi_os_printf(ACPI_PLD_OUTPUT, "PLD_Ejectable", pld_info->ejectable);
  350. acpi_os_printf(ACPI_PLD_OUTPUT, "PLD_EjectRequired",
  351. pld_info->ospm_eject_required);
  352. acpi_os_printf(ACPI_PLD_OUTPUT, "PLD_CabinetNumber",
  353. pld_info->cabinet_number);
  354. acpi_os_printf(ACPI_PLD_OUTPUT, "PLD_CardCageNumber",
  355. pld_info->card_cage_number);
  356. acpi_os_printf(ACPI_PLD_OUTPUT, "PLD_Reference", pld_info->reference);
  357. acpi_os_printf(ACPI_PLD_OUTPUT, "PLD_Rotation", pld_info->rotation);
  358. acpi_os_printf(ACPI_PLD_OUTPUT, "PLD_Order", pld_info->order);
  359. /* Fifth 32-bit dword */
  360. if (buffer_desc->buffer.length > 16) {
  361. acpi_os_printf(ACPI_PLD_OUTPUT, "PLD_VerticalOffset",
  362. pld_info->vertical_offset);
  363. acpi_os_printf(ACPI_PLD_OUTPUT, "PLD_HorizontalOffset",
  364. pld_info->horizontal_offset);
  365. }
  366. ACPI_FREE(new_buffer);
  367. exit:
  368. ACPI_FREE(pld_info);
  369. }