dbconvert.c 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484
  1. /*******************************************************************************
  2. *
  3. * Module Name: dbconvert - debugger miscellaneous conversion routines
  4. *
  5. ******************************************************************************/
  6. /*
  7. * Copyright (C) 2000 - 2015, 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, 16, &object->integer.value);
  238. break;
  239. }
  240. return (status);
  241. }
  242. /*******************************************************************************
  243. *
  244. * FUNCTION: acpi_db_encode_pld_buffer
  245. *
  246. * PARAMETERS: pld_info - _PLD buffer struct (Using local struct)
  247. *
  248. * RETURN: Encode _PLD buffer suitable for return value from _PLD
  249. *
  250. * DESCRIPTION: Bit-packs a _PLD buffer struct. Used to test the _PLD macros
  251. *
  252. ******************************************************************************/
  253. u8 *acpi_db_encode_pld_buffer(struct acpi_pld_info *pld_info)
  254. {
  255. u32 *buffer;
  256. u32 dword;
  257. buffer = ACPI_ALLOCATE_ZEROED(ACPI_PLD_BUFFER_SIZE);
  258. if (!buffer) {
  259. return (NULL);
  260. }
  261. /* First 32 bits */
  262. dword = 0;
  263. ACPI_PLD_SET_REVISION(&dword, pld_info->revision);
  264. ACPI_PLD_SET_IGNORE_COLOR(&dword, pld_info->ignore_color);
  265. ACPI_PLD_SET_RED(&dword, pld_info->red);
  266. ACPI_PLD_SET_GREEN(&dword, pld_info->green);
  267. ACPI_PLD_SET_BLUE(&dword, pld_info->blue);
  268. ACPI_MOVE_32_TO_32(&buffer[0], &dword);
  269. /* Second 32 bits */
  270. dword = 0;
  271. ACPI_PLD_SET_WIDTH(&dword, pld_info->width);
  272. ACPI_PLD_SET_HEIGHT(&dword, pld_info->height);
  273. ACPI_MOVE_32_TO_32(&buffer[1], &dword);
  274. /* Third 32 bits */
  275. dword = 0;
  276. ACPI_PLD_SET_USER_VISIBLE(&dword, pld_info->user_visible);
  277. ACPI_PLD_SET_DOCK(&dword, pld_info->dock);
  278. ACPI_PLD_SET_LID(&dword, pld_info->lid);
  279. ACPI_PLD_SET_PANEL(&dword, pld_info->panel);
  280. ACPI_PLD_SET_VERTICAL(&dword, pld_info->vertical_position);
  281. ACPI_PLD_SET_HORIZONTAL(&dword, pld_info->horizontal_position);
  282. ACPI_PLD_SET_SHAPE(&dword, pld_info->shape);
  283. ACPI_PLD_SET_ORIENTATION(&dword, pld_info->group_orientation);
  284. ACPI_PLD_SET_TOKEN(&dword, pld_info->group_token);
  285. ACPI_PLD_SET_POSITION(&dword, pld_info->group_position);
  286. ACPI_PLD_SET_BAY(&dword, pld_info->bay);
  287. ACPI_MOVE_32_TO_32(&buffer[2], &dword);
  288. /* Fourth 32 bits */
  289. dword = 0;
  290. ACPI_PLD_SET_EJECTABLE(&dword, pld_info->ejectable);
  291. ACPI_PLD_SET_OSPM_EJECT(&dword, pld_info->ospm_eject_required);
  292. ACPI_PLD_SET_CABINET(&dword, pld_info->cabinet_number);
  293. ACPI_PLD_SET_CARD_CAGE(&dword, pld_info->card_cage_number);
  294. ACPI_PLD_SET_REFERENCE(&dword, pld_info->reference);
  295. ACPI_PLD_SET_ROTATION(&dword, pld_info->rotation);
  296. ACPI_PLD_SET_ORDER(&dword, pld_info->order);
  297. ACPI_MOVE_32_TO_32(&buffer[3], &dword);
  298. if (pld_info->revision >= 2) {
  299. /* Fifth 32 bits */
  300. dword = 0;
  301. ACPI_PLD_SET_VERT_OFFSET(&dword, pld_info->vertical_offset);
  302. ACPI_PLD_SET_HORIZ_OFFSET(&dword, pld_info->horizontal_offset);
  303. ACPI_MOVE_32_TO_32(&buffer[4], &dword);
  304. }
  305. return (ACPI_CAST_PTR(u8, buffer));
  306. }
  307. /*******************************************************************************
  308. *
  309. * FUNCTION: acpi_db_dump_pld_buffer
  310. *
  311. * PARAMETERS: obj_desc - Object returned from _PLD method
  312. *
  313. * RETURN: None.
  314. *
  315. * DESCRIPTION: Dumps formatted contents of a _PLD return buffer.
  316. *
  317. ******************************************************************************/
  318. #define ACPI_PLD_OUTPUT "%20s : %-6X\n"
  319. void acpi_db_dump_pld_buffer(union acpi_object *obj_desc)
  320. {
  321. union acpi_object *buffer_desc;
  322. struct acpi_pld_info *pld_info;
  323. u8 *new_buffer;
  324. acpi_status status;
  325. /* Object must be of type Package with at least one Buffer element */
  326. if (obj_desc->type != ACPI_TYPE_PACKAGE) {
  327. return;
  328. }
  329. buffer_desc = &obj_desc->package.elements[0];
  330. if (buffer_desc->type != ACPI_TYPE_BUFFER) {
  331. return;
  332. }
  333. /* Convert _PLD buffer to local _PLD struct */
  334. status = acpi_decode_pld_buffer(buffer_desc->buffer.pointer,
  335. buffer_desc->buffer.length, &pld_info);
  336. if (ACPI_FAILURE(status)) {
  337. return;
  338. }
  339. /* Encode local _PLD struct back to a _PLD buffer */
  340. new_buffer = acpi_db_encode_pld_buffer(pld_info);
  341. if (!new_buffer) {
  342. return;
  343. }
  344. /* The two bit-packed buffers should match */
  345. if (memcmp(new_buffer, buffer_desc->buffer.pointer,
  346. buffer_desc->buffer.length)) {
  347. acpi_os_printf
  348. ("Converted _PLD buffer does not compare. New:\n");
  349. acpi_ut_dump_buffer(new_buffer,
  350. buffer_desc->buffer.length, DB_BYTE_DISPLAY,
  351. 0);
  352. }
  353. /* First 32-bit dword */
  354. acpi_os_printf(ACPI_PLD_OUTPUT, "PLD_Revision", pld_info->revision);
  355. acpi_os_printf(ACPI_PLD_OUTPUT, "PLD_IgnoreColor",
  356. pld_info->ignore_color);
  357. acpi_os_printf(ACPI_PLD_OUTPUT, "PLD_Red", pld_info->red);
  358. acpi_os_printf(ACPI_PLD_OUTPUT, "PLD_Green", pld_info->green);
  359. acpi_os_printf(ACPI_PLD_OUTPUT, "PLD_Blue", pld_info->blue);
  360. /* Second 32-bit dword */
  361. acpi_os_printf(ACPI_PLD_OUTPUT, "PLD_Width", pld_info->width);
  362. acpi_os_printf(ACPI_PLD_OUTPUT, "PLD_Height", pld_info->height);
  363. /* Third 32-bit dword */
  364. acpi_os_printf(ACPI_PLD_OUTPUT, "PLD_UserVisible",
  365. pld_info->user_visible);
  366. acpi_os_printf(ACPI_PLD_OUTPUT, "PLD_Dock", pld_info->dock);
  367. acpi_os_printf(ACPI_PLD_OUTPUT, "PLD_Lid", pld_info->lid);
  368. acpi_os_printf(ACPI_PLD_OUTPUT, "PLD_Panel", pld_info->panel);
  369. acpi_os_printf(ACPI_PLD_OUTPUT, "PLD_VerticalPosition",
  370. pld_info->vertical_position);
  371. acpi_os_printf(ACPI_PLD_OUTPUT, "PLD_HorizontalPosition",
  372. pld_info->horizontal_position);
  373. acpi_os_printf(ACPI_PLD_OUTPUT, "PLD_Shape", pld_info->shape);
  374. acpi_os_printf(ACPI_PLD_OUTPUT, "PLD_GroupOrientation",
  375. pld_info->group_orientation);
  376. acpi_os_printf(ACPI_PLD_OUTPUT, "PLD_GroupToken",
  377. pld_info->group_token);
  378. acpi_os_printf(ACPI_PLD_OUTPUT, "PLD_GroupPosition",
  379. pld_info->group_position);
  380. acpi_os_printf(ACPI_PLD_OUTPUT, "PLD_Bay", pld_info->bay);
  381. /* Fourth 32-bit dword */
  382. acpi_os_printf(ACPI_PLD_OUTPUT, "PLD_Ejectable", pld_info->ejectable);
  383. acpi_os_printf(ACPI_PLD_OUTPUT, "PLD_EjectRequired",
  384. pld_info->ospm_eject_required);
  385. acpi_os_printf(ACPI_PLD_OUTPUT, "PLD_CabinetNumber",
  386. pld_info->cabinet_number);
  387. acpi_os_printf(ACPI_PLD_OUTPUT, "PLD_CardCageNumber",
  388. pld_info->card_cage_number);
  389. acpi_os_printf(ACPI_PLD_OUTPUT, "PLD_Reference", pld_info->reference);
  390. acpi_os_printf(ACPI_PLD_OUTPUT, "PLD_Rotation", pld_info->rotation);
  391. acpi_os_printf(ACPI_PLD_OUTPUT, "PLD_Order", pld_info->order);
  392. /* Fifth 32-bit dword */
  393. if (buffer_desc->buffer.length > 16) {
  394. acpi_os_printf(ACPI_PLD_OUTPUT, "PLD_VerticalOffset",
  395. pld_info->vertical_offset);
  396. acpi_os_printf(ACPI_PLD_OUTPUT, "PLD_HorizontalOffset",
  397. pld_info->horizontal_offset);
  398. }
  399. ACPI_FREE(pld_info);
  400. ACPI_FREE(new_buffer);
  401. }