GCC Code Coverage Report
Directory: ./ Exec Total Coverage
File: core/src/ux_utility_descriptor_parse.c Lines: 37 39 94.9 %
Date: 2024-12-12 17:16:36 Branches: 15 16 93.8 %

Line Branch Exec Source
1
/***************************************************************************
2
 * Copyright (c) 2024 Microsoft Corporation
3
 *
4
 * This program and the accompanying materials are made available under the
5
 * terms of the MIT License which is available at
6
 * https://opensource.org/licenses/MIT.
7
 *
8
 * SPDX-License-Identifier: MIT
9
 **************************************************************************/
10
11
12
/**************************************************************************/
13
/**************************************************************************/
14
/**                                                                       */
15
/** USBX Component                                                        */
16
/**                                                                       */
17
/**   Utility                                                             */
18
/**                                                                       */
19
/**************************************************************************/
20
/**************************************************************************/
21
22
23
/* Include necessary system files.  */
24
25
#define UX_SOURCE_CODE
26
27
#include "ux_api.h"
28
29
30
/**************************************************************************/
31
/*                                                                        */
32
/*  FUNCTION                                               RELEASE        */
33
/*                                                                        */
34
/*    _ux_utility_descriptor_parse                        PORTABLE C      */
35
/*                                                           6.3.0        */
36
/*  AUTHOR                                                                */
37
/*                                                                        */
38
/*    Chaoqiong Xiao, Microsoft Corporation                               */
39
/*                                                                        */
40
/*  DESCRIPTION                                                           */
41
/*                                                                        */
42
/*    This function will unpack a USB descriptor from the bus into a      */
43
/*    memory aligned structure.                                           */
44
/*                                                                        */
45
/*  INPUT                                                                 */
46
/*                                                                        */
47
/*    raw_descriptor                        Pointer to packed descriptor  */
48
/*    descriptor_structure                  Components of the descriptor  */
49
/*    descriptor_entries                    Number of entries in the      */
50
/*                                            descriptor                  */
51
/*    descriptor                            Pointer to the unpacked       */
52
/*                                            descriptor                  */
53
/*                                                                        */
54
/*  OUTPUT                                                                */
55
/*                                                                        */
56
/*    None                                                                */
57
/*                                                                        */
58
/*  CALLS                                                                 */
59
/*                                                                        */
60
/*    _ux_utility_long_get                  Get 32-bit value              */
61
/*    _ux_utility_short_get                 Get 16-bit value              */
62
/*                                                                        */
63
/*  CALLED BY                                                             */
64
/*                                                                        */
65
/*    USBX Components                                                     */
66
/*                                                                        */
67
/*  RELEASE HISTORY                                                       */
68
/*                                                                        */
69
/*    DATE              NAME                      DESCRIPTION             */
70
/*                                                                        */
71
/*  05-19-2020     Chaoqiong Xiao           Initial Version 6.0           */
72
/*  09-30-2020     Chaoqiong Xiao           Modified comment(s),          */
73
/*                                            resulting in version 6.1    */
74
/*  10-31-2023     Chaoqiong Xiao           Modified comment(s),          */
75
/*                                            optimized USB descriptors,  */
76
/*                                            resulting in version 6.3.0  */
77
/*                                                                        */
78
/**************************************************************************/
79
22052
VOID  _ux_utility_descriptor_parse(UCHAR * raw_descriptor, UCHAR * descriptor_structure,
80
                        UINT descriptor_entries, UCHAR * descriptor)
81
{
82
83
    /* Loop on all the entries in this descriptor.  */
84
217199
    while(descriptor_entries--)
85
    {
86
87
        /* Get the length of that component.  */
88
195147
        switch(*descriptor_structure++)
89
        {
90
91
        /* Check the size then build the component from the source and
92
           insert it into the target descriptor.  */
93
527
        case 4:
94
95
            /* Padding zeros so address is aligned.  */
96
697
            while((ALIGN_TYPE) descriptor & 3u)
97
170
                *descriptor++ =  0;
98
99
            /* Save the DW.  */
100
527
            *((ULONG *) descriptor) =  _ux_utility_long_get(raw_descriptor);
101
527
            raw_descriptor +=  4;
102
527
            descriptor += 4;
103
527
            break;
104
105
26723
        case 2:
106
107
            /* Padding zeros so address is aligned.  */
108
27275
            while((ALIGN_TYPE) descriptor & 1u)
109
552
                *descriptor++ =  0;
110
111
            /* Save the word.  */
112
26723
            *((USHORT *) descriptor) = (USHORT) _ux_utility_short_get(raw_descriptor);
113
26723
            raw_descriptor += 2;
114
26723
            descriptor += 2;
115
26723
            break;
116
117
167897
        default:
118
119
            /* Save the byte.  */
120
167897
            *((UCHAR *) descriptor) =  (UCHAR) *raw_descriptor;
121
167897
            raw_descriptor++;
122
167897
            descriptor ++;
123
        }
124
    }
125
126
    /* Return to caller.  */
127
22052
    return;
128
}
129
130
/**************************************************************************/
131
/*                                                                        */
132
/*  FUNCTION                                               RELEASE        */
133
/*                                                                        */
134
/*    _ux_utility_descriptor_parse_size                   PORTABLE C      */
135
/*                                                           6.3.0        */
136
/*  AUTHOR                                                                */
137
/*                                                                        */
138
/*    Chaoqiong Xiao, Microsoft Corporation                               */
139
/*                                                                        */
140
/*  DESCRIPTION                                                           */
141
/*                                                                        */
142
/*    This function will calculate the size of a parsed USB descriptor.   */
143
/*                                                                        */
144
/*  INPUT                                                                 */
145
/*                                                                        */
146
/*    descriptor_structure                  Components of the descriptor  */
147
/*    descriptor_entries                    Number of entries in the      */
148
/*                                            descriptor                  */
149
/*    size_align_mask                       Size alignment mask           */
150
/*                                                                        */
151
/*  OUTPUT                                                                */
152
/*                                                                        */
153
/*    size                                  Size of the parsed descriptor */
154
/*                                                                        */
155
/*  CALLS                                                                 */
156
/*                                                                        */
157
/*    None                                                                */
158
/*                                                                        */
159
/*  CALLED BY                                                             */
160
/*                                                                        */
161
/*    USBX Components                                                     */
162
/*                                                                        */
163
/*  RELEASE HISTORY                                                       */
164
/*                                                                        */
165
/*    DATE              NAME                      DESCRIPTION             */
166
/*                                                                        */
167
/*  10-31-2023     Chaoqiong Xiao           Initial Version 6.3.0         */
168
/*                                                                        */
169
/**************************************************************************/
170
25
ULONG _ux_utility_descriptor_parse_size(UCHAR * descriptor_structure, UINT descriptor_entries, UINT size_align_mask)
171
{
172
173
25
ULONG           size = 0;
174
ULONG           entry_size;
175
176
    /* Loop on all the entries in this descriptor.  */
177
223
    while(descriptor_entries--)
178
    {
179
180
        /* Get entry size.  */
181
198
        entry_size = (ULONG)*descriptor_structure ++;
182
183
        /* Check the size then build the component from the source and
184
           insert it into the target descriptor.  */
185
198
        switch(entry_size)
186
        {
187
188
54
        case 4: /* Fall through.  */
189
        case 2:
190
191
            /* Padding zeros so address is aligned.  */
192
64
            while(size & (entry_size - 1))
193
10
                size++;
194
195
            /* Add to the size.  */
196
54
            size += entry_size;
197
54
            break;
198
199
144
        case 1:
200
201
            /* Add to the size.  */
202
144
            size += 1;
203
144
            break;
204
205
        default:
206
207
            /* Invalid entry size.  */
208
            return(0);
209
        }
210
    }
211
212
    /* Align the size.  */
213
25
    size = (size + size_align_mask) & (~size_align_mask);
214
215
    /* Return the size.  */
216
25
    return(size);
217
}