Back to home page

OSCL-LXR

 
 

    


0001 // SPDX-License-Identifier: BSD-3-Clause OR GPL-2.0
0002 /*******************************************************************************
0003  *
0004  * Module Name: dbconvert - debugger miscellaneous conversion routines
0005  *
0006  ******************************************************************************/
0007 
0008 #include <acpi/acpi.h>
0009 #include "accommon.h"
0010 #include "acdebug.h"
0011 
0012 #define _COMPONENT          ACPI_CA_DEBUGGER
0013 ACPI_MODULE_NAME("dbconvert")
0014 
0015 #define DB_DEFAULT_PKG_ELEMENTS     33
0016 /*******************************************************************************
0017  *
0018  * FUNCTION:    acpi_db_hex_char_to_value
0019  *
0020  * PARAMETERS:  hex_char            - Ascii Hex digit, 0-9|a-f|A-F
0021  *              return_value        - Where the converted value is returned
0022  *
0023  * RETURN:      Status
0024  *
0025  * DESCRIPTION: Convert a single hex character to a 4-bit number (0-16).
0026  *
0027  ******************************************************************************/
0028 acpi_status acpi_db_hex_char_to_value(int hex_char, u8 *return_value)
0029 {
0030     u8 value;
0031 
0032     /* Digit must be ascii [0-9a-fA-F] */
0033 
0034     if (!isxdigit(hex_char)) {
0035         return (AE_BAD_HEX_CONSTANT);
0036     }
0037 
0038     if (hex_char <= 0x39) {
0039         value = (u8)(hex_char - 0x30);
0040     } else {
0041         value = (u8)(toupper(hex_char) - 0x37);
0042     }
0043 
0044     *return_value = value;
0045     return (AE_OK);
0046 }
0047 
0048 /*******************************************************************************
0049  *
0050  * FUNCTION:    acpi_db_hex_byte_to_binary
0051  *
0052  * PARAMETERS:  hex_byte            - Double hex digit (0x00 - 0xFF) in format:
0053  *                                    hi_byte then lo_byte.
0054  *              return_value        - Where the converted value is returned
0055  *
0056  * RETURN:      Status
0057  *
0058  * DESCRIPTION: Convert two hex characters to an 8 bit number (0 - 255).
0059  *
0060  ******************************************************************************/
0061 
0062 static acpi_status acpi_db_hex_byte_to_binary(char *hex_byte, u8 *return_value)
0063 {
0064     u8 local0;
0065     u8 local1;
0066     acpi_status status;
0067 
0068     /* High byte */
0069 
0070     status = acpi_db_hex_char_to_value(hex_byte[0], &local0);
0071     if (ACPI_FAILURE(status)) {
0072         return (status);
0073     }
0074 
0075     /* Low byte */
0076 
0077     status = acpi_db_hex_char_to_value(hex_byte[1], &local1);
0078     if (ACPI_FAILURE(status)) {
0079         return (status);
0080     }
0081 
0082     *return_value = (u8)((local0 << 4) | local1);
0083     return (AE_OK);
0084 }
0085 
0086 /*******************************************************************************
0087  *
0088  * FUNCTION:    acpi_db_convert_to_buffer
0089  *
0090  * PARAMETERS:  string              - Input string to be converted
0091  *              object              - Where the buffer object is returned
0092  *
0093  * RETURN:      Status
0094  *
0095  * DESCRIPTION: Convert a string to a buffer object. String is treated a list
0096  *              of buffer elements, each separated by a space or comma.
0097  *
0098  ******************************************************************************/
0099 
0100 static acpi_status
0101 acpi_db_convert_to_buffer(char *string, union acpi_object *object)
0102 {
0103     u32 i;
0104     u32 j;
0105     u32 length;
0106     u8 *buffer;
0107     acpi_status status;
0108 
0109     /* Skip all preceding white space */
0110 
0111     acpi_ut_remove_whitespace(&string);
0112 
0113     /* Generate the final buffer length */
0114 
0115     for (i = 0, length = 0; string[i];) {
0116         i += 2;
0117         length++;
0118 
0119         while (string[i] && ((string[i] == ',') || (string[i] == ' '))) {
0120             i++;
0121         }
0122     }
0123 
0124     buffer = ACPI_ALLOCATE(length);
0125     if (!buffer) {
0126         return (AE_NO_MEMORY);
0127     }
0128 
0129     /* Convert the command line bytes to the buffer */
0130 
0131     for (i = 0, j = 0; string[i];) {
0132         status = acpi_db_hex_byte_to_binary(&string[i], &buffer[j]);
0133         if (ACPI_FAILURE(status)) {
0134             ACPI_FREE(buffer);
0135             return (status);
0136         }
0137 
0138         j++;
0139         i += 2;
0140         while (string[i] && ((string[i] == ',') || (string[i] == ' '))) {
0141             i++;
0142         }
0143     }
0144 
0145     object->type = ACPI_TYPE_BUFFER;
0146     object->buffer.pointer = buffer;
0147     object->buffer.length = length;
0148     return (AE_OK);
0149 }
0150 
0151 /*******************************************************************************
0152  *
0153  * FUNCTION:    acpi_db_convert_to_package
0154  *
0155  * PARAMETERS:  string              - Input string to be converted
0156  *              object              - Where the package object is returned
0157  *
0158  * RETURN:      Status
0159  *
0160  * DESCRIPTION: Convert a string to a package object. Handles nested packages
0161  *              via recursion with acpi_db_convert_to_object.
0162  *
0163  ******************************************************************************/
0164 
0165 acpi_status acpi_db_convert_to_package(char *string, union acpi_object *object)
0166 {
0167     char *this;
0168     char *next;
0169     u32 i;
0170     acpi_object_type type;
0171     union acpi_object *elements;
0172     acpi_status status;
0173 
0174     elements =
0175         ACPI_ALLOCATE_ZEROED(DB_DEFAULT_PKG_ELEMENTS *
0176                  sizeof(union acpi_object));
0177 
0178     this = string;
0179     for (i = 0; i < (DB_DEFAULT_PKG_ELEMENTS - 1); i++) {
0180         this = acpi_db_get_next_token(this, &next, &type);
0181         if (!this) {
0182             break;
0183         }
0184 
0185         /* Recursive call to convert each package element */
0186 
0187         status = acpi_db_convert_to_object(type, this, &elements[i]);
0188         if (ACPI_FAILURE(status)) {
0189             acpi_db_delete_objects(i + 1, elements);
0190             ACPI_FREE(elements);
0191             return (status);
0192         }
0193 
0194         this = next;
0195     }
0196 
0197     object->type = ACPI_TYPE_PACKAGE;
0198     object->package.count = i;
0199     object->package.elements = elements;
0200     return (AE_OK);
0201 }
0202 
0203 /*******************************************************************************
0204  *
0205  * FUNCTION:    acpi_db_convert_to_object
0206  *
0207  * PARAMETERS:  type                - Object type as determined by parser
0208  *              string              - Input string to be converted
0209  *              object              - Where the new object is returned
0210  *
0211  * RETURN:      Status
0212  *
0213  * DESCRIPTION: Convert a typed and tokenized string to a union acpi_object. Typing:
0214  *              1) String objects were surrounded by quotes.
0215  *              2) Buffer objects were surrounded by parentheses.
0216  *              3) Package objects were surrounded by brackets "[]".
0217  *              4) All standalone tokens are treated as integers.
0218  *
0219  ******************************************************************************/
0220 
0221 acpi_status
0222 acpi_db_convert_to_object(acpi_object_type type,
0223               char *string, union acpi_object *object)
0224 {
0225     acpi_status status = AE_OK;
0226 
0227     switch (type) {
0228     case ACPI_TYPE_STRING:
0229 
0230         object->type = ACPI_TYPE_STRING;
0231         object->string.pointer = string;
0232         object->string.length = (u32)strlen(string);
0233         break;
0234 
0235     case ACPI_TYPE_BUFFER:
0236 
0237         status = acpi_db_convert_to_buffer(string, object);
0238         break;
0239 
0240     case ACPI_TYPE_PACKAGE:
0241 
0242         status = acpi_db_convert_to_package(string, object);
0243         break;
0244 
0245     default:
0246 
0247         object->type = ACPI_TYPE_INTEGER;
0248         status = acpi_ut_strtoul64(string, &object->integer.value);
0249         break;
0250     }
0251 
0252     return (status);
0253 }
0254 
0255 /*******************************************************************************
0256  *
0257  * FUNCTION:    acpi_db_encode_pld_buffer
0258  *
0259  * PARAMETERS:  pld_info            - _PLD buffer struct (Using local struct)
0260  *
0261  * RETURN:      Encode _PLD buffer suitable for return value from _PLD
0262  *
0263  * DESCRIPTION: Bit-packs a _PLD buffer struct. Used to test the _PLD macros
0264  *
0265  ******************************************************************************/
0266 
0267 u8 *acpi_db_encode_pld_buffer(struct acpi_pld_info *pld_info)
0268 {
0269     u32 *buffer;
0270     u32 dword;
0271 
0272     buffer = ACPI_ALLOCATE_ZEROED(ACPI_PLD_BUFFER_SIZE);
0273     if (!buffer) {
0274         return (NULL);
0275     }
0276 
0277     /* First 32 bits */
0278 
0279     dword = 0;
0280     ACPI_PLD_SET_REVISION(&dword, pld_info->revision);
0281     ACPI_PLD_SET_IGNORE_COLOR(&dword, pld_info->ignore_color);
0282     ACPI_PLD_SET_RED(&dword, pld_info->red);
0283     ACPI_PLD_SET_GREEN(&dword, pld_info->green);
0284     ACPI_PLD_SET_BLUE(&dword, pld_info->blue);
0285     ACPI_MOVE_32_TO_32(&buffer[0], &dword);
0286 
0287     /* Second 32 bits */
0288 
0289     dword = 0;
0290     ACPI_PLD_SET_WIDTH(&dword, pld_info->width);
0291     ACPI_PLD_SET_HEIGHT(&dword, pld_info->height);
0292     ACPI_MOVE_32_TO_32(&buffer[1], &dword);
0293 
0294     /* Third 32 bits */
0295 
0296     dword = 0;
0297     ACPI_PLD_SET_USER_VISIBLE(&dword, pld_info->user_visible);
0298     ACPI_PLD_SET_DOCK(&dword, pld_info->dock);
0299     ACPI_PLD_SET_LID(&dword, pld_info->lid);
0300     ACPI_PLD_SET_PANEL(&dword, pld_info->panel);
0301     ACPI_PLD_SET_VERTICAL(&dword, pld_info->vertical_position);
0302     ACPI_PLD_SET_HORIZONTAL(&dword, pld_info->horizontal_position);
0303     ACPI_PLD_SET_SHAPE(&dword, pld_info->shape);
0304     ACPI_PLD_SET_ORIENTATION(&dword, pld_info->group_orientation);
0305     ACPI_PLD_SET_TOKEN(&dword, pld_info->group_token);
0306     ACPI_PLD_SET_POSITION(&dword, pld_info->group_position);
0307     ACPI_PLD_SET_BAY(&dword, pld_info->bay);
0308     ACPI_MOVE_32_TO_32(&buffer[2], &dword);
0309 
0310     /* Fourth 32 bits */
0311 
0312     dword = 0;
0313     ACPI_PLD_SET_EJECTABLE(&dword, pld_info->ejectable);
0314     ACPI_PLD_SET_OSPM_EJECT(&dword, pld_info->ospm_eject_required);
0315     ACPI_PLD_SET_CABINET(&dword, pld_info->cabinet_number);
0316     ACPI_PLD_SET_CARD_CAGE(&dword, pld_info->card_cage_number);
0317     ACPI_PLD_SET_REFERENCE(&dword, pld_info->reference);
0318     ACPI_PLD_SET_ROTATION(&dword, pld_info->rotation);
0319     ACPI_PLD_SET_ORDER(&dword, pld_info->order);
0320     ACPI_MOVE_32_TO_32(&buffer[3], &dword);
0321 
0322     if (pld_info->revision >= 2) {
0323 
0324         /* Fifth 32 bits */
0325 
0326         dword = 0;
0327         ACPI_PLD_SET_VERT_OFFSET(&dword, pld_info->vertical_offset);
0328         ACPI_PLD_SET_HORIZ_OFFSET(&dword, pld_info->horizontal_offset);
0329         ACPI_MOVE_32_TO_32(&buffer[4], &dword);
0330     }
0331 
0332     return (ACPI_CAST_PTR(u8, buffer));
0333 }
0334 
0335 /*******************************************************************************
0336  *
0337  * FUNCTION:    acpi_db_dump_pld_buffer
0338  *
0339  * PARAMETERS:  obj_desc            - Object returned from _PLD method
0340  *
0341  * RETURN:      None.
0342  *
0343  * DESCRIPTION: Dumps formatted contents of a _PLD return buffer.
0344  *
0345  ******************************************************************************/
0346 
0347 #define ACPI_PLD_OUTPUT     "%20s : %-6X\n"
0348 
0349 void acpi_db_dump_pld_buffer(union acpi_object *obj_desc)
0350 {
0351     union acpi_object *buffer_desc;
0352     struct acpi_pld_info *pld_info;
0353     u8 *new_buffer;
0354     acpi_status status;
0355 
0356     /* Object must be of type Package with at least one Buffer element */
0357 
0358     if (obj_desc->type != ACPI_TYPE_PACKAGE) {
0359         return;
0360     }
0361 
0362     buffer_desc = &obj_desc->package.elements[0];
0363     if (buffer_desc->type != ACPI_TYPE_BUFFER) {
0364         return;
0365     }
0366 
0367     /* Convert _PLD buffer to local _PLD struct */
0368 
0369     status = acpi_decode_pld_buffer(buffer_desc->buffer.pointer,
0370                     buffer_desc->buffer.length, &pld_info);
0371     if (ACPI_FAILURE(status)) {
0372         return;
0373     }
0374 
0375     /* Encode local _PLD struct back to a _PLD buffer */
0376 
0377     new_buffer = acpi_db_encode_pld_buffer(pld_info);
0378     if (!new_buffer) {
0379         goto exit;
0380     }
0381 
0382     /* The two bit-packed buffers should match */
0383 
0384     if (memcmp(new_buffer, buffer_desc->buffer.pointer,
0385            buffer_desc->buffer.length)) {
0386         acpi_os_printf
0387             ("Converted _PLD buffer does not compare. New:\n");
0388 
0389         acpi_ut_dump_buffer(new_buffer,
0390                     buffer_desc->buffer.length, DB_BYTE_DISPLAY,
0391                     0);
0392     }
0393 
0394     /* First 32-bit dword */
0395 
0396     acpi_os_printf(ACPI_PLD_OUTPUT, "PLD_Revision", pld_info->revision);
0397     acpi_os_printf(ACPI_PLD_OUTPUT, "PLD_IgnoreColor",
0398                pld_info->ignore_color);
0399     acpi_os_printf(ACPI_PLD_OUTPUT, "PLD_Red", pld_info->red);
0400     acpi_os_printf(ACPI_PLD_OUTPUT, "PLD_Green", pld_info->green);
0401     acpi_os_printf(ACPI_PLD_OUTPUT, "PLD_Blue", pld_info->blue);
0402 
0403     /* Second 32-bit dword */
0404 
0405     acpi_os_printf(ACPI_PLD_OUTPUT, "PLD_Width", pld_info->width);
0406     acpi_os_printf(ACPI_PLD_OUTPUT, "PLD_Height", pld_info->height);
0407 
0408     /* Third 32-bit dword */
0409 
0410     acpi_os_printf(ACPI_PLD_OUTPUT, "PLD_UserVisible",
0411                pld_info->user_visible);
0412     acpi_os_printf(ACPI_PLD_OUTPUT, "PLD_Dock", pld_info->dock);
0413     acpi_os_printf(ACPI_PLD_OUTPUT, "PLD_Lid", pld_info->lid);
0414     acpi_os_printf(ACPI_PLD_OUTPUT, "PLD_Panel", pld_info->panel);
0415     acpi_os_printf(ACPI_PLD_OUTPUT, "PLD_VerticalPosition",
0416                pld_info->vertical_position);
0417     acpi_os_printf(ACPI_PLD_OUTPUT, "PLD_HorizontalPosition",
0418                pld_info->horizontal_position);
0419     acpi_os_printf(ACPI_PLD_OUTPUT, "PLD_Shape", pld_info->shape);
0420     acpi_os_printf(ACPI_PLD_OUTPUT, "PLD_GroupOrientation",
0421                pld_info->group_orientation);
0422     acpi_os_printf(ACPI_PLD_OUTPUT, "PLD_GroupToken",
0423                pld_info->group_token);
0424     acpi_os_printf(ACPI_PLD_OUTPUT, "PLD_GroupPosition",
0425                pld_info->group_position);
0426     acpi_os_printf(ACPI_PLD_OUTPUT, "PLD_Bay", pld_info->bay);
0427 
0428     /* Fourth 32-bit dword */
0429 
0430     acpi_os_printf(ACPI_PLD_OUTPUT, "PLD_Ejectable", pld_info->ejectable);
0431     acpi_os_printf(ACPI_PLD_OUTPUT, "PLD_EjectRequired",
0432                pld_info->ospm_eject_required);
0433     acpi_os_printf(ACPI_PLD_OUTPUT, "PLD_CabinetNumber",
0434                pld_info->cabinet_number);
0435     acpi_os_printf(ACPI_PLD_OUTPUT, "PLD_CardCageNumber",
0436                pld_info->card_cage_number);
0437     acpi_os_printf(ACPI_PLD_OUTPUT, "PLD_Reference", pld_info->reference);
0438     acpi_os_printf(ACPI_PLD_OUTPUT, "PLD_Rotation", pld_info->rotation);
0439     acpi_os_printf(ACPI_PLD_OUTPUT, "PLD_Order", pld_info->order);
0440 
0441     /* Fifth 32-bit dword */
0442 
0443     if (buffer_desc->buffer.length > 16) {
0444         acpi_os_printf(ACPI_PLD_OUTPUT, "PLD_VerticalOffset",
0445                    pld_info->vertical_offset);
0446         acpi_os_printf(ACPI_PLD_OUTPUT, "PLD_HorizontalOffset",
0447                    pld_info->horizontal_offset);
0448     }
0449 
0450     ACPI_FREE(new_buffer);
0451 exit:
0452     ACPI_FREE(pld_info);
0453 }