1 | /*
|
---|
2 | * Copyright (c) 2011 Vojtech Horky
|
---|
3 | * All rights reserved.
|
---|
4 | *
|
---|
5 | * Redistribution and use in source and binary forms, with or without
|
---|
6 | * modification, are permitted provided that the following conditions
|
---|
7 | * are met:
|
---|
8 | *
|
---|
9 | * - Redistributions of source code must retain the above copyright
|
---|
10 | * notice, this list of conditions and the following disclaimer.
|
---|
11 | * - Redistributions in binary form must reproduce the above copyright
|
---|
12 | * notice, this list of conditions and the following disclaimer in the
|
---|
13 | * documentation and/or other materials provided with the distribution.
|
---|
14 | * - The name of the author may not be used to endorse or promote products
|
---|
15 | * derived from this software without specific prior written permission.
|
---|
16 | *
|
---|
17 | * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
|
---|
18 | * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
|
---|
19 | * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
|
---|
20 | * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
|
---|
21 | * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
|
---|
22 | * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
|
---|
23 | * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
|
---|
24 | * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
|
---|
25 | * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
|
---|
26 | * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
|
---|
27 | */
|
---|
28 |
|
---|
29 | /** @addtogroup libusbdev
|
---|
30 | * @{
|
---|
31 | */
|
---|
32 | /**
|
---|
33 | * @file
|
---|
34 | * USB descriptor parser (implementation).
|
---|
35 | *
|
---|
36 | * The descriptor parser is a generic parser for structure, where individual
|
---|
37 | * items are stored in single buffer and each item begins with length followed
|
---|
38 | * by type. These types are organized into tree hierarchy.
|
---|
39 | *
|
---|
40 | * The parser is able of only two actions: find first child and find next
|
---|
41 | * sibling.
|
---|
42 | */
|
---|
43 | #include <usb/dev/dp.h>
|
---|
44 | #include <usb/descriptor.h>
|
---|
45 |
|
---|
46 | #include <assert.h>
|
---|
47 | #include <errno.h>
|
---|
48 | #include <stdlib.h>
|
---|
49 | #include <stdbool.h>
|
---|
50 | #include <stddef.h>
|
---|
51 | #include <stdint.h>
|
---|
52 |
|
---|
53 | #define NESTING(parentname, childname) \
|
---|
54 | { \
|
---|
55 | .child = USB_DESCTYPE_##childname, \
|
---|
56 | .parent = USB_DESCTYPE_##parentname, \
|
---|
57 | }
|
---|
58 | #define LAST_NESTING { -1, -1 }
|
---|
59 |
|
---|
60 | /** Nesting of standard USB descriptors. */
|
---|
61 | const usb_dp_descriptor_nesting_t usb_dp_standard_descriptor_nesting[] = {
|
---|
62 | NESTING(CONFIGURATION, INTERFACE),
|
---|
63 | NESTING(INTERFACE, ENDPOINT),
|
---|
64 | NESTING(INTERFACE, HUB),
|
---|
65 | NESTING(INTERFACE, HID),
|
---|
66 | NESTING(HID, HID_REPORT),
|
---|
67 | LAST_NESTING
|
---|
68 | };
|
---|
69 |
|
---|
70 | #undef NESTING
|
---|
71 | #undef LAST_NESTING
|
---|
72 |
|
---|
73 | /** Tells whether pointer points inside descriptor data.
|
---|
74 | *
|
---|
75 | * @param data Parser data.
|
---|
76 | * @param ptr Pointer to be verified.
|
---|
77 | * @return Whether @p ptr points inside <code>data->data</code> field.
|
---|
78 | */
|
---|
79 | static bool is_valid_descriptor_pointer(const usb_dp_parser_data_t *data,
|
---|
80 | const uint8_t *ptr)
|
---|
81 | {
|
---|
82 | if (ptr == NULL) {
|
---|
83 | return false;
|
---|
84 | }
|
---|
85 |
|
---|
86 | if (ptr < data->data) {
|
---|
87 | return false;
|
---|
88 | }
|
---|
89 |
|
---|
90 | if ((size_t)(ptr - data->data) >= data->size) {
|
---|
91 | return false;
|
---|
92 | }
|
---|
93 |
|
---|
94 | return true;
|
---|
95 | }
|
---|
96 |
|
---|
97 | /** Get next descriptor regardless of the nesting.
|
---|
98 | *
|
---|
99 | * @param data Parser data.
|
---|
100 | * @param current Pointer to current descriptor.
|
---|
101 | * @return Pointer to start of next descriptor.
|
---|
102 | * @retval NULL Invalid input or no next descriptor.
|
---|
103 | */
|
---|
104 | static const uint8_t *get_next_descriptor(const usb_dp_parser_data_t *data,
|
---|
105 | const uint8_t *current)
|
---|
106 | {
|
---|
107 | assert(is_valid_descriptor_pointer(data, current));
|
---|
108 |
|
---|
109 | const uint8_t current_length = *current;
|
---|
110 | const uint8_t *next = current + current_length;
|
---|
111 |
|
---|
112 | if (!is_valid_descriptor_pointer(data, next)) {
|
---|
113 | return NULL;
|
---|
114 | }
|
---|
115 |
|
---|
116 | return next;
|
---|
117 | }
|
---|
118 |
|
---|
119 | /** Get descriptor type.
|
---|
120 | *
|
---|
121 | * @see usb_descriptor_type_t
|
---|
122 | *
|
---|
123 | * @param data Parser data.
|
---|
124 | * @param start Pointer to start of the descriptor.
|
---|
125 | * @return Descriptor type.
|
---|
126 | * @retval -1 Invalid input.
|
---|
127 | */
|
---|
128 | static int get_descriptor_type(const usb_dp_parser_data_t *data, const uint8_t *start)
|
---|
129 | {
|
---|
130 | if (start == NULL) {
|
---|
131 | return -1;
|
---|
132 | }
|
---|
133 |
|
---|
134 | start++;
|
---|
135 | if (!is_valid_descriptor_pointer(data, start)) {
|
---|
136 | return -1;
|
---|
137 | } else {
|
---|
138 | return (int) (*start);
|
---|
139 | }
|
---|
140 | }
|
---|
141 |
|
---|
142 | /** Tells whether descriptors could be nested.
|
---|
143 | *
|
---|
144 | * @param parser Parser.
|
---|
145 | * @param child Child descriptor type.
|
---|
146 | * @param parent Parent descriptor type.
|
---|
147 | * @return Whether @p child could be child of @p parent.
|
---|
148 | */
|
---|
149 | static bool is_nested_descriptor_type(const usb_dp_parser_t *parser,
|
---|
150 | int child, int parent)
|
---|
151 | {
|
---|
152 | const usb_dp_descriptor_nesting_t *nesting = parser->nesting;
|
---|
153 | while ((nesting->child > 0) && (nesting->parent > 0)) {
|
---|
154 | if ((nesting->child == child) && (nesting->parent == parent)) {
|
---|
155 | return true;
|
---|
156 | }
|
---|
157 | nesting++;
|
---|
158 | }
|
---|
159 | return false;
|
---|
160 | }
|
---|
161 |
|
---|
162 | /** Tells whether descriptors could be nested.
|
---|
163 | *
|
---|
164 | * @param parser Parser.
|
---|
165 | * @param data Parser data.
|
---|
166 | * @param child Pointer to child descriptor.
|
---|
167 | * @param parent Pointer to parent descriptor.
|
---|
168 | * @return Whether @p child could be child of @p parent.
|
---|
169 | */
|
---|
170 | static bool is_nested_descriptor(const usb_dp_parser_t *parser,
|
---|
171 | const usb_dp_parser_data_t *data, const uint8_t *child, const uint8_t *parent)
|
---|
172 | {
|
---|
173 | return is_nested_descriptor_type(parser,
|
---|
174 | get_descriptor_type(data, child),
|
---|
175 | get_descriptor_type(data, parent));
|
---|
176 | }
|
---|
177 |
|
---|
178 | /** Find first nested descriptor of given parent.
|
---|
179 | *
|
---|
180 | * @param parser Parser.
|
---|
181 | * @param data Parser data.
|
---|
182 | * @param parent Pointer to the beginning of parent descriptor.
|
---|
183 | * @return Pointer to the beginning of the first nested (child) descriptor.
|
---|
184 | * @retval NULL No child descriptor found.
|
---|
185 | * @retval NULL Invalid input.
|
---|
186 | */
|
---|
187 | const uint8_t *usb_dp_get_nested_descriptor(const usb_dp_parser_t *parser,
|
---|
188 | const usb_dp_parser_data_t *data, const uint8_t *parent)
|
---|
189 | {
|
---|
190 | if (!is_valid_descriptor_pointer(data, parent)) {
|
---|
191 | return NULL;
|
---|
192 | }
|
---|
193 |
|
---|
194 | const uint8_t *next = get_next_descriptor(data, parent);
|
---|
195 | if (next == NULL) {
|
---|
196 | return NULL;
|
---|
197 | }
|
---|
198 |
|
---|
199 | if (is_nested_descriptor(parser, data, next, parent)) {
|
---|
200 | return next;
|
---|
201 | } else {
|
---|
202 | return NULL;
|
---|
203 | }
|
---|
204 | }
|
---|
205 |
|
---|
206 | /** Skip all nested descriptors.
|
---|
207 | *
|
---|
208 | * @param parser Parser.
|
---|
209 | * @param data Parser data.
|
---|
210 | * @param parent Pointer to the beginning of parent descriptor.
|
---|
211 | * @return Pointer to first non-child descriptor.
|
---|
212 | * @retval NULL No next descriptor.
|
---|
213 | * @retval NULL Invalid input.
|
---|
214 | */
|
---|
215 | static const uint8_t *skip_nested_descriptors(const usb_dp_parser_t *parser,
|
---|
216 | const usb_dp_parser_data_t *data, const uint8_t *parent)
|
---|
217 | {
|
---|
218 | const uint8_t *child =
|
---|
219 | usb_dp_get_nested_descriptor(parser, data, parent);
|
---|
220 | if (child == NULL) {
|
---|
221 | return get_next_descriptor(data, parent);
|
---|
222 | }
|
---|
223 | const uint8_t *next_child =
|
---|
224 | skip_nested_descriptors(parser, data, child);
|
---|
225 | while (is_nested_descriptor(parser, data, next_child, parent)) {
|
---|
226 | next_child = skip_nested_descriptors(parser, data, next_child);
|
---|
227 | }
|
---|
228 |
|
---|
229 | return next_child;
|
---|
230 | }
|
---|
231 |
|
---|
232 | /** Get sibling descriptor.
|
---|
233 | *
|
---|
234 | * @param parser Parser.
|
---|
235 | * @param data Parser data.
|
---|
236 | * @param parent Pointer to common parent descriptor.
|
---|
237 | * @param sibling Left sibling.
|
---|
238 | * @return Pointer to first right sibling of @p sibling.
|
---|
239 | * @retval NULL No sibling exist.
|
---|
240 | * @retval NULL Invalid input.
|
---|
241 | */
|
---|
242 | const uint8_t *usb_dp_get_sibling_descriptor(
|
---|
243 | const usb_dp_parser_t *parser, const usb_dp_parser_data_t *data,
|
---|
244 | const uint8_t *parent, const uint8_t *sibling)
|
---|
245 | {
|
---|
246 | if (!is_valid_descriptor_pointer(data, parent)
|
---|
247 | || !is_valid_descriptor_pointer(data, sibling)) {
|
---|
248 | return NULL;
|
---|
249 | }
|
---|
250 |
|
---|
251 | const uint8_t *possible_sibling =
|
---|
252 | skip_nested_descriptors(parser, data, sibling);
|
---|
253 | if (possible_sibling == NULL) {
|
---|
254 | return NULL;
|
---|
255 | }
|
---|
256 |
|
---|
257 | int parent_type = get_descriptor_type(data, parent);
|
---|
258 | int possible_sibling_type = get_descriptor_type(data, possible_sibling);
|
---|
259 | if (is_nested_descriptor_type(parser, possible_sibling_type, parent_type)) {
|
---|
260 | return possible_sibling;
|
---|
261 | } else {
|
---|
262 | return NULL;
|
---|
263 | }
|
---|
264 | }
|
---|
265 |
|
---|
266 | /** Browser of the descriptor tree.
|
---|
267 | *
|
---|
268 | * @see usb_dp_walk_simple
|
---|
269 | *
|
---|
270 | * @param parser Descriptor parser.
|
---|
271 | * @param data Data for descriptor parser.
|
---|
272 | * @param root Pointer to current root of the tree.
|
---|
273 | * @param depth Current nesting depth.
|
---|
274 | * @param callback Callback for each found descriptor.
|
---|
275 | * @param arg Custom (user) argument.
|
---|
276 | */
|
---|
277 | static void usb_dp_browse_simple_internal(const usb_dp_parser_t *parser,
|
---|
278 | const usb_dp_parser_data_t *data, const uint8_t *root, size_t depth,
|
---|
279 | void (*callback)(const uint8_t *, size_t, void *), void *arg)
|
---|
280 | {
|
---|
281 | if (root == NULL) {
|
---|
282 | return;
|
---|
283 | }
|
---|
284 | callback(root, depth, arg);
|
---|
285 | const uint8_t *child = usb_dp_get_nested_descriptor(parser, data, root);
|
---|
286 | do {
|
---|
287 | usb_dp_browse_simple_internal(parser, data, child, depth + 1,
|
---|
288 | callback, arg);
|
---|
289 | child = usb_dp_get_sibling_descriptor(parser, data,
|
---|
290 | root, child);
|
---|
291 | } while (child != NULL);
|
---|
292 | }
|
---|
293 |
|
---|
294 | /** Browse flatten descriptor tree.
|
---|
295 | *
|
---|
296 | * The callback is called with following arguments: pointer to the start
|
---|
297 | * of the descriptor (somewhere inside @p descriptors), depth of the nesting
|
---|
298 | * (starting from 0 for the first descriptor) and the custom argument.
|
---|
299 | * Note that the size of the descriptor is not passed because it can
|
---|
300 | * be read from the first byte of the descriptor.
|
---|
301 | *
|
---|
302 | * @param descriptors Descriptor data.
|
---|
303 | * @param descriptors_size Size of descriptor data (in bytes).
|
---|
304 | * @param descriptor_nesting Possible descriptor nesting.
|
---|
305 | * @param callback Callback for each found descriptor.
|
---|
306 | * @param arg Custom (user) argument.
|
---|
307 | */
|
---|
308 | void usb_dp_walk_simple(const uint8_t *descriptors, size_t descriptors_size,
|
---|
309 | const usb_dp_descriptor_nesting_t *descriptor_nesting,
|
---|
310 | walk_callback_t callback, void *arg)
|
---|
311 | {
|
---|
312 | if ((descriptors == NULL) || (descriptors_size == 0)
|
---|
313 | || (descriptor_nesting == NULL) || (callback == NULL)) {
|
---|
314 | return;
|
---|
315 | }
|
---|
316 |
|
---|
317 | const usb_dp_parser_data_t data = {
|
---|
318 | .data = descriptors,
|
---|
319 | .size = descriptors_size,
|
---|
320 | .arg = NULL
|
---|
321 | };
|
---|
322 |
|
---|
323 | const usb_dp_parser_t parser = {
|
---|
324 | .nesting = descriptor_nesting
|
---|
325 | };
|
---|
326 |
|
---|
327 | usb_dp_browse_simple_internal(&parser, &data, descriptors,
|
---|
328 | 0, callback, arg);
|
---|
329 | }
|
---|
330 |
|
---|
331 | /** @}
|
---|
332 | */
|
---|