1 // SPDX-License-Identifier: BSD-3-Clause OR GPL-2.0
2 /*******************************************************************************
4 * Module Name: dbconvert - debugger miscellaneous conversion routines
6 ******************************************************************************/
12 #define _COMPONENT ACPI_CA_DEBUGGER
13 ACPI_MODULE_NAME("dbconvert")
15 #define DB_DEFAULT_PKG_ELEMENTS 33
16 /*******************************************************************************
18 * FUNCTION: acpi_db_hex_char_to_value
20 * PARAMETERS: hex_char - Ascii Hex digit, 0-9|a-f|A-F
21 * return_value - Where the converted value is returned
25 * DESCRIPTION: Convert a single hex character to a 4-bit number (0-16).
27 ******************************************************************************/
28 acpi_status acpi_db_hex_char_to_value(int hex_char, u8 *return_value)
32 /* Digit must be ascii [0-9a-fA-F] */
34 if (!isxdigit(hex_char)) {
35 return (AE_BAD_HEX_CONSTANT);
38 if (hex_char <= 0x39) {
39 value = (u8)(hex_char - 0x30);
41 value = (u8)(toupper(hex_char) - 0x37);
44 *return_value = value;
48 /*******************************************************************************
50 * FUNCTION: acpi_db_hex_byte_to_binary
52 * PARAMETERS: hex_byte - Double hex digit (0x00 - 0xFF) in format:
53 * hi_byte then lo_byte.
54 * return_value - Where the converted value is returned
58 * DESCRIPTION: Convert two hex characters to an 8 bit number (0 - 255).
60 ******************************************************************************/
62 static acpi_status acpi_db_hex_byte_to_binary(char *hex_byte, u8 *return_value)
70 status = acpi_db_hex_char_to_value(hex_byte[0], &local0);
71 if (ACPI_FAILURE(status)) {
77 status = acpi_db_hex_char_to_value(hex_byte[1], &local1);
78 if (ACPI_FAILURE(status)) {
82 *return_value = (u8)((local0 << 4) | local1);
86 /*******************************************************************************
88 * FUNCTION: acpi_db_convert_to_buffer
90 * PARAMETERS: string - Input string to be converted
91 * object - Where the buffer object is returned
95 * DESCRIPTION: Convert a string to a buffer object. String is treated a list
96 * of buffer elements, each separated by a space or comma.
98 ******************************************************************************/
101 acpi_db_convert_to_buffer(char *string, union acpi_object *object)
109 /* Skip all preceding white space */
111 acpi_ut_remove_whitespace(&string);
113 /* Generate the final buffer length */
115 for (i = 0, length = 0; string[i];) {
119 while (string[i] && ((string[i] == ',') || (string[i] == ' '))) {
124 buffer = ACPI_ALLOCATE(length);
126 return (AE_NO_MEMORY);
129 /* Convert the command line bytes to the buffer */
131 for (i = 0, j = 0; string[i];) {
132 status = acpi_db_hex_byte_to_binary(&string[i], &buffer[j]);
133 if (ACPI_FAILURE(status)) {
140 while (string[i] && ((string[i] == ',') || (string[i] == ' '))) {
145 object->type = ACPI_TYPE_BUFFER;
146 object->buffer.pointer = buffer;
147 object->buffer.length = length;
151 /*******************************************************************************
153 * FUNCTION: acpi_db_convert_to_package
155 * PARAMETERS: string - Input string to be converted
156 * object - Where the package object is returned
160 * DESCRIPTION: Convert a string to a package object. Handles nested packages
161 * via recursion with acpi_db_convert_to_object.
163 ******************************************************************************/
165 acpi_status acpi_db_convert_to_package(char *string, union acpi_object *object)
170 acpi_object_type type;
171 union acpi_object *elements;
175 ACPI_ALLOCATE_ZEROED(DB_DEFAULT_PKG_ELEMENTS *
176 sizeof(union acpi_object));
178 return (AE_NO_MEMORY);
181 for (i = 0; i < (DB_DEFAULT_PKG_ELEMENTS - 1); i++) {
182 this = acpi_db_get_next_token(this, &next, &type);
187 /* Recursive call to convert each package element */
189 status = acpi_db_convert_to_object(type, this, &elements[i]);
190 if (ACPI_FAILURE(status)) {
191 acpi_db_delete_objects(i + 1, elements);
199 object->type = ACPI_TYPE_PACKAGE;
200 object->package.count = i;
201 object->package.elements = elements;
205 /*******************************************************************************
207 * FUNCTION: acpi_db_convert_to_object
209 * PARAMETERS: type - Object type as determined by parser
210 * string - Input string to be converted
211 * object - Where the new object is returned
215 * DESCRIPTION: Convert a typed and tokenized string to a union acpi_object. Typing:
216 * 1) String objects were surrounded by quotes.
217 * 2) Buffer objects were surrounded by parentheses.
218 * 3) Package objects were surrounded by brackets "[]".
219 * 4) All standalone tokens are treated as integers.
221 ******************************************************************************/
224 acpi_db_convert_to_object(acpi_object_type type,
225 char *string, union acpi_object *object)
227 acpi_status status = AE_OK;
230 case ACPI_TYPE_STRING:
232 object->type = ACPI_TYPE_STRING;
233 object->string.pointer = string;
234 object->string.length = (u32)strlen(string);
237 case ACPI_TYPE_BUFFER:
239 status = acpi_db_convert_to_buffer(string, object);
242 case ACPI_TYPE_PACKAGE:
244 status = acpi_db_convert_to_package(string, object);
249 object->type = ACPI_TYPE_INTEGER;
250 status = acpi_ut_strtoul64(string, &object->integer.value);
257 /*******************************************************************************
259 * FUNCTION: acpi_db_encode_pld_buffer
261 * PARAMETERS: pld_info - _PLD buffer struct (Using local struct)
263 * RETURN: Encode _PLD buffer suitable for return value from _PLD
265 * DESCRIPTION: Bit-packs a _PLD buffer struct. Used to test the _PLD macros
267 ******************************************************************************/
269 u8 *acpi_db_encode_pld_buffer(struct acpi_pld_info *pld_info)
274 buffer = ACPI_ALLOCATE_ZEROED(ACPI_PLD_BUFFER_SIZE);
282 ACPI_PLD_SET_REVISION(&dword, pld_info->revision);
283 ACPI_PLD_SET_IGNORE_COLOR(&dword, pld_info->ignore_color);
284 ACPI_PLD_SET_RED(&dword, pld_info->red);
285 ACPI_PLD_SET_GREEN(&dword, pld_info->green);
286 ACPI_PLD_SET_BLUE(&dword, pld_info->blue);
287 ACPI_MOVE_32_TO_32(&buffer[0], &dword);
292 ACPI_PLD_SET_WIDTH(&dword, pld_info->width);
293 ACPI_PLD_SET_HEIGHT(&dword, pld_info->height);
294 ACPI_MOVE_32_TO_32(&buffer[1], &dword);
299 ACPI_PLD_SET_USER_VISIBLE(&dword, pld_info->user_visible);
300 ACPI_PLD_SET_DOCK(&dword, pld_info->dock);
301 ACPI_PLD_SET_LID(&dword, pld_info->lid);
302 ACPI_PLD_SET_PANEL(&dword, pld_info->panel);
303 ACPI_PLD_SET_VERTICAL(&dword, pld_info->vertical_position);
304 ACPI_PLD_SET_HORIZONTAL(&dword, pld_info->horizontal_position);
305 ACPI_PLD_SET_SHAPE(&dword, pld_info->shape);
306 ACPI_PLD_SET_ORIENTATION(&dword, pld_info->group_orientation);
307 ACPI_PLD_SET_TOKEN(&dword, pld_info->group_token);
308 ACPI_PLD_SET_POSITION(&dword, pld_info->group_position);
309 ACPI_PLD_SET_BAY(&dword, pld_info->bay);
310 ACPI_MOVE_32_TO_32(&buffer[2], &dword);
315 ACPI_PLD_SET_EJECTABLE(&dword, pld_info->ejectable);
316 ACPI_PLD_SET_OSPM_EJECT(&dword, pld_info->ospm_eject_required);
317 ACPI_PLD_SET_CABINET(&dword, pld_info->cabinet_number);
318 ACPI_PLD_SET_CARD_CAGE(&dword, pld_info->card_cage_number);
319 ACPI_PLD_SET_REFERENCE(&dword, pld_info->reference);
320 ACPI_PLD_SET_ROTATION(&dword, pld_info->rotation);
321 ACPI_PLD_SET_ORDER(&dword, pld_info->order);
322 ACPI_MOVE_32_TO_32(&buffer[3], &dword);
324 if (pld_info->revision >= 2) {
329 ACPI_PLD_SET_VERT_OFFSET(&dword, pld_info->vertical_offset);
330 ACPI_PLD_SET_HORIZ_OFFSET(&dword, pld_info->horizontal_offset);
331 ACPI_MOVE_32_TO_32(&buffer[4], &dword);
334 return (ACPI_CAST_PTR(u8, buffer));
337 /*******************************************************************************
339 * FUNCTION: acpi_db_dump_pld_buffer
341 * PARAMETERS: obj_desc - Object returned from _PLD method
345 * DESCRIPTION: Dumps formatted contents of a _PLD return buffer.
347 ******************************************************************************/
349 #define ACPI_PLD_OUTPUT "%20s : %-6X\n"
351 void acpi_db_dump_pld_buffer(union acpi_object *obj_desc)
353 union acpi_object *buffer_desc;
354 struct acpi_pld_info *pld_info;
358 /* Object must be of type Package with at least one Buffer element */
360 if (obj_desc->type != ACPI_TYPE_PACKAGE) {
364 buffer_desc = &obj_desc->package.elements[0];
365 if (buffer_desc->type != ACPI_TYPE_BUFFER) {
369 /* Convert _PLD buffer to local _PLD struct */
371 status = acpi_decode_pld_buffer(buffer_desc->buffer.pointer,
372 buffer_desc->buffer.length, &pld_info);
373 if (ACPI_FAILURE(status)) {
377 /* Encode local _PLD struct back to a _PLD buffer */
379 new_buffer = acpi_db_encode_pld_buffer(pld_info);
384 /* The two bit-packed buffers should match */
386 if (memcmp(new_buffer, buffer_desc->buffer.pointer,
387 buffer_desc->buffer.length)) {
389 ("Converted _PLD buffer does not compare. New:\n");
391 acpi_ut_dump_buffer(new_buffer,
392 buffer_desc->buffer.length, DB_BYTE_DISPLAY,
396 /* First 32-bit dword */
398 acpi_os_printf(ACPI_PLD_OUTPUT, "PLD_Revision", pld_info->revision);
399 acpi_os_printf(ACPI_PLD_OUTPUT, "PLD_IgnoreColor",
400 pld_info->ignore_color);
401 acpi_os_printf(ACPI_PLD_OUTPUT, "PLD_Red", pld_info->red);
402 acpi_os_printf(ACPI_PLD_OUTPUT, "PLD_Green", pld_info->green);
403 acpi_os_printf(ACPI_PLD_OUTPUT, "PLD_Blue", pld_info->blue);
405 /* Second 32-bit dword */
407 acpi_os_printf(ACPI_PLD_OUTPUT, "PLD_Width", pld_info->width);
408 acpi_os_printf(ACPI_PLD_OUTPUT, "PLD_Height", pld_info->height);
410 /* Third 32-bit dword */
412 acpi_os_printf(ACPI_PLD_OUTPUT, "PLD_UserVisible",
413 pld_info->user_visible);
414 acpi_os_printf(ACPI_PLD_OUTPUT, "PLD_Dock", pld_info->dock);
415 acpi_os_printf(ACPI_PLD_OUTPUT, "PLD_Lid", pld_info->lid);
416 acpi_os_printf(ACPI_PLD_OUTPUT, "PLD_Panel", pld_info->panel);
417 acpi_os_printf(ACPI_PLD_OUTPUT, "PLD_VerticalPosition",
418 pld_info->vertical_position);
419 acpi_os_printf(ACPI_PLD_OUTPUT, "PLD_HorizontalPosition",
420 pld_info->horizontal_position);
421 acpi_os_printf(ACPI_PLD_OUTPUT, "PLD_Shape", pld_info->shape);
422 acpi_os_printf(ACPI_PLD_OUTPUT, "PLD_GroupOrientation",
423 pld_info->group_orientation);
424 acpi_os_printf(ACPI_PLD_OUTPUT, "PLD_GroupToken",
425 pld_info->group_token);
426 acpi_os_printf(ACPI_PLD_OUTPUT, "PLD_GroupPosition",
427 pld_info->group_position);
428 acpi_os_printf(ACPI_PLD_OUTPUT, "PLD_Bay", pld_info->bay);
430 /* Fourth 32-bit dword */
432 acpi_os_printf(ACPI_PLD_OUTPUT, "PLD_Ejectable", pld_info->ejectable);
433 acpi_os_printf(ACPI_PLD_OUTPUT, "PLD_EjectRequired",
434 pld_info->ospm_eject_required);
435 acpi_os_printf(ACPI_PLD_OUTPUT, "PLD_CabinetNumber",
436 pld_info->cabinet_number);
437 acpi_os_printf(ACPI_PLD_OUTPUT, "PLD_CardCageNumber",
438 pld_info->card_cage_number);
439 acpi_os_printf(ACPI_PLD_OUTPUT, "PLD_Reference", pld_info->reference);
440 acpi_os_printf(ACPI_PLD_OUTPUT, "PLD_Rotation", pld_info->rotation);
441 acpi_os_printf(ACPI_PLD_OUTPUT, "PLD_Order", pld_info->order);
443 /* Fifth 32-bit dword */
445 if (buffer_desc->buffer.length > 16) {
446 acpi_os_printf(ACPI_PLD_OUTPUT, "PLD_VerticalOffset",
447 pld_info->vertical_offset);
448 acpi_os_printf(ACPI_PLD_OUTPUT, "PLD_HorizontalOffset",
449 pld_info->horizontal_offset);
452 ACPI_FREE(new_buffer);