dbconvert.c 14 KB

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