[5ccb15c] | 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 |
|
---|
[160b75e] | 29 | /** @addtogroup libusbdev
|
---|
[5ccb15c] | 30 | * @{
|
---|
| 31 | */
|
---|
| 32 | /**
|
---|
| 33 | * @file
|
---|
[a6add7a] | 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.
|
---|
[5ccb15c] | 42 | */
|
---|
[7d521e24] | 43 | #include <usb/dev/dp.h>
|
---|
[745d2ad] | 44 | #include <usb/descriptor.h>
|
---|
| 45 |
|
---|
[c01987c] | 46 | #include <assert.h>
|
---|
| 47 | #include <errno.h>
|
---|
| 48 | #include <stdlib.h>
|
---|
| 49 | #include <stdbool.h>
|
---|
[582a0b8] | 50 | #include <stddef.h>
|
---|
[8d2dd7f2] | 51 | #include <stdint.h>
|
---|
[c01987c] | 52 |
|
---|
[745d2ad] | 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. */
|
---|
[207acc4e] | 61 | const usb_dp_descriptor_nesting_t usb_dp_standard_descriptor_nesting[] = {
|
---|
[745d2ad] | 62 | NESTING(CONFIGURATION, INTERFACE),
|
---|
| 63 | NESTING(INTERFACE, ENDPOINT),
|
---|
[7f70d1c] | 64 | NESTING(ENDPOINT, SSPEED_EP_COMPANION),
|
---|
[745d2ad] | 65 | NESTING(INTERFACE, HUB),
|
---|
| 66 | NESTING(INTERFACE, HID),
|
---|
| 67 | NESTING(HID, HID_REPORT),
|
---|
| 68 | LAST_NESTING
|
---|
| 69 | };
|
---|
| 70 |
|
---|
| 71 | #undef NESTING
|
---|
| 72 | #undef LAST_NESTING
|
---|
[5ccb15c] | 73 |
|
---|
| 74 | /** Tells whether pointer points inside descriptor data.
|
---|
| 75 | *
|
---|
| 76 | * @param data Parser data.
|
---|
| 77 | * @param ptr Pointer to be verified.
|
---|
[3b77628] | 78 | * @return Whether @p ptr points inside <code>data->data</code> field.
|
---|
[5ccb15c] | 79 | */
|
---|
[8a121b1] | 80 | static bool is_valid_descriptor_pointer(const usb_dp_parser_data_t *data,
|
---|
| 81 | const uint8_t *ptr)
|
---|
[5ccb15c] | 82 | {
|
---|
| 83 | if (ptr == NULL) {
|
---|
| 84 | return false;
|
---|
| 85 | }
|
---|
| 86 |
|
---|
| 87 | if (ptr < data->data) {
|
---|
| 88 | return false;
|
---|
| 89 | }
|
---|
| 90 |
|
---|
| 91 | if ((size_t)(ptr - data->data) >= data->size) {
|
---|
| 92 | return false;
|
---|
| 93 | }
|
---|
| 94 |
|
---|
| 95 | return true;
|
---|
| 96 | }
|
---|
| 97 |
|
---|
| 98 | /** Get next descriptor regardless of the nesting.
|
---|
| 99 | *
|
---|
| 100 | * @param data Parser data.
|
---|
| 101 | * @param current Pointer to current descriptor.
|
---|
| 102 | * @return Pointer to start of next descriptor.
|
---|
| 103 | * @retval NULL Invalid input or no next descriptor.
|
---|
| 104 | */
|
---|
[8a121b1] | 105 | static const uint8_t *get_next_descriptor(const usb_dp_parser_data_t *data,
|
---|
| 106 | const uint8_t *current)
|
---|
[5ccb15c] | 107 | {
|
---|
| 108 | assert(is_valid_descriptor_pointer(data, current));
|
---|
| 109 |
|
---|
[8a121b1] | 110 | const uint8_t current_length = *current;
|
---|
| 111 | const uint8_t *next = current + current_length;
|
---|
[5ccb15c] | 112 |
|
---|
| 113 | if (!is_valid_descriptor_pointer(data, next)) {
|
---|
| 114 | return NULL;
|
---|
| 115 | }
|
---|
| 116 |
|
---|
| 117 | return next;
|
---|
| 118 | }
|
---|
| 119 |
|
---|
| 120 | /** Get descriptor type.
|
---|
| 121 | *
|
---|
| 122 | * @see usb_descriptor_type_t
|
---|
| 123 | *
|
---|
| 124 | * @param data Parser data.
|
---|
| 125 | * @param start Pointer to start of the descriptor.
|
---|
| 126 | * @return Descriptor type.
|
---|
| 127 | * @retval -1 Invalid input.
|
---|
| 128 | */
|
---|
[8a121b1] | 129 | static int get_descriptor_type(const usb_dp_parser_data_t *data, const uint8_t *start)
|
---|
[5ccb15c] | 130 | {
|
---|
| 131 | if (start == NULL) {
|
---|
| 132 | return -1;
|
---|
| 133 | }
|
---|
| 134 |
|
---|
| 135 | start++;
|
---|
| 136 | if (!is_valid_descriptor_pointer(data, start)) {
|
---|
| 137 | return -1;
|
---|
| 138 | } else {
|
---|
| 139 | return (int) (*start);
|
---|
| 140 | }
|
---|
| 141 | }
|
---|
| 142 |
|
---|
| 143 | /** Tells whether descriptors could be nested.
|
---|
| 144 | *
|
---|
| 145 | * @param parser Parser.
|
---|
| 146 | * @param child Child descriptor type.
|
---|
| 147 | * @param parent Parent descriptor type.
|
---|
| 148 | * @return Whether @p child could be child of @p parent.
|
---|
| 149 | */
|
---|
[8a121b1] | 150 | static bool is_nested_descriptor_type(const usb_dp_parser_t *parser,
|
---|
[5ccb15c] | 151 | int child, int parent)
|
---|
| 152 | {
|
---|
[8a121b1] | 153 | const usb_dp_descriptor_nesting_t *nesting = parser->nesting;
|
---|
[5ccb15c] | 154 | while ((nesting->child > 0) && (nesting->parent > 0)) {
|
---|
| 155 | if ((nesting->child == child) && (nesting->parent == parent)) {
|
---|
| 156 | return true;
|
---|
| 157 | }
|
---|
| 158 | nesting++;
|
---|
| 159 | }
|
---|
| 160 | return false;
|
---|
| 161 | }
|
---|
| 162 |
|
---|
| 163 | /** Tells whether descriptors could be nested.
|
---|
| 164 | *
|
---|
| 165 | * @param parser Parser.
|
---|
| 166 | * @param data Parser data.
|
---|
| 167 | * @param child Pointer to child descriptor.
|
---|
| 168 | * @param parent Pointer to parent descriptor.
|
---|
| 169 | * @return Whether @p child could be child of @p parent.
|
---|
| 170 | */
|
---|
[8a121b1] | 171 | static bool is_nested_descriptor(const usb_dp_parser_t *parser,
|
---|
| 172 | const usb_dp_parser_data_t *data, const uint8_t *child, const uint8_t *parent)
|
---|
[5ccb15c] | 173 | {
|
---|
| 174 | return is_nested_descriptor_type(parser,
|
---|
| 175 | get_descriptor_type(data, child),
|
---|
| 176 | get_descriptor_type(data, parent));
|
---|
| 177 | }
|
---|
| 178 |
|
---|
| 179 | /** Find first nested descriptor of given parent.
|
---|
| 180 | *
|
---|
| 181 | * @param parser Parser.
|
---|
| 182 | * @param data Parser data.
|
---|
| 183 | * @param parent Pointer to the beginning of parent descriptor.
|
---|
| 184 | * @return Pointer to the beginning of the first nested (child) descriptor.
|
---|
| 185 | * @retval NULL No child descriptor found.
|
---|
| 186 | * @retval NULL Invalid input.
|
---|
| 187 | */
|
---|
[8a121b1] | 188 | const uint8_t *usb_dp_get_nested_descriptor(const usb_dp_parser_t *parser,
|
---|
| 189 | const usb_dp_parser_data_t *data, const uint8_t *parent)
|
---|
[5ccb15c] | 190 | {
|
---|
| 191 | if (!is_valid_descriptor_pointer(data, parent)) {
|
---|
| 192 | return NULL;
|
---|
| 193 | }
|
---|
| 194 |
|
---|
[8a121b1] | 195 | const uint8_t *next = get_next_descriptor(data, parent);
|
---|
[5ccb15c] | 196 | if (next == NULL) {
|
---|
| 197 | return NULL;
|
---|
| 198 | }
|
---|
| 199 |
|
---|
| 200 | if (is_nested_descriptor(parser, data, next, parent)) {
|
---|
| 201 | return next;
|
---|
| 202 | } else {
|
---|
| 203 | return NULL;
|
---|
| 204 | }
|
---|
| 205 | }
|
---|
| 206 |
|
---|
| 207 | /** Skip all nested descriptors.
|
---|
| 208 | *
|
---|
| 209 | * @param parser Parser.
|
---|
| 210 | * @param data Parser data.
|
---|
| 211 | * @param parent Pointer to the beginning of parent descriptor.
|
---|
| 212 | * @return Pointer to first non-child descriptor.
|
---|
| 213 | * @retval NULL No next descriptor.
|
---|
| 214 | * @retval NULL Invalid input.
|
---|
| 215 | */
|
---|
[8a121b1] | 216 | static const uint8_t *skip_nested_descriptors(const usb_dp_parser_t *parser,
|
---|
| 217 | const usb_dp_parser_data_t *data, const uint8_t *parent)
|
---|
[5ccb15c] | 218 | {
|
---|
[8a121b1] | 219 | const uint8_t *child =
|
---|
| 220 | usb_dp_get_nested_descriptor(parser, data, parent);
|
---|
[5ccb15c] | 221 | if (child == NULL) {
|
---|
| 222 | return get_next_descriptor(data, parent);
|
---|
| 223 | }
|
---|
[8a121b1] | 224 | const uint8_t *next_child =
|
---|
| 225 | skip_nested_descriptors(parser, data, child);
|
---|
[5ccb15c] | 226 | while (is_nested_descriptor(parser, data, next_child, parent)) {
|
---|
| 227 | next_child = skip_nested_descriptors(parser, data, next_child);
|
---|
| 228 | }
|
---|
| 229 |
|
---|
| 230 | return next_child;
|
---|
| 231 | }
|
---|
| 232 |
|
---|
| 233 | /** Get sibling descriptor.
|
---|
| 234 | *
|
---|
| 235 | * @param parser Parser.
|
---|
| 236 | * @param data Parser data.
|
---|
| 237 | * @param parent Pointer to common parent descriptor.
|
---|
| 238 | * @param sibling Left sibling.
|
---|
| 239 | * @return Pointer to first right sibling of @p sibling.
|
---|
| 240 | * @retval NULL No sibling exist.
|
---|
| 241 | * @retval NULL Invalid input.
|
---|
| 242 | */
|
---|
[8a121b1] | 243 | const uint8_t *usb_dp_get_sibling_descriptor(
|
---|
| 244 | const usb_dp_parser_t *parser, const usb_dp_parser_data_t *data,
|
---|
| 245 | const uint8_t *parent, const uint8_t *sibling)
|
---|
[5ccb15c] | 246 | {
|
---|
| 247 | if (!is_valid_descriptor_pointer(data, parent)
|
---|
| 248 | || !is_valid_descriptor_pointer(data, sibling)) {
|
---|
| 249 | return NULL;
|
---|
| 250 | }
|
---|
| 251 |
|
---|
[8a121b1] | 252 | const uint8_t *possible_sibling =
|
---|
| 253 | skip_nested_descriptors(parser, data, sibling);
|
---|
[5ccb15c] | 254 | if (possible_sibling == NULL) {
|
---|
| 255 | return NULL;
|
---|
| 256 | }
|
---|
| 257 |
|
---|
| 258 | int parent_type = get_descriptor_type(data, parent);
|
---|
| 259 | int possible_sibling_type = get_descriptor_type(data, possible_sibling);
|
---|
| 260 | if (is_nested_descriptor_type(parser, possible_sibling_type, parent_type)) {
|
---|
| 261 | return possible_sibling;
|
---|
| 262 | } else {
|
---|
| 263 | return NULL;
|
---|
| 264 | }
|
---|
| 265 | }
|
---|
| 266 |
|
---|
[da77278] | 267 | /** Browser of the descriptor tree.
|
---|
| 268 | *
|
---|
| 269 | * @see usb_dp_walk_simple
|
---|
| 270 | *
|
---|
| 271 | * @param parser Descriptor parser.
|
---|
| 272 | * @param data Data for descriptor parser.
|
---|
| 273 | * @param root Pointer to current root of the tree.
|
---|
| 274 | * @param depth Current nesting depth.
|
---|
| 275 | * @param callback Callback for each found descriptor.
|
---|
| 276 | * @param arg Custom (user) argument.
|
---|
| 277 | */
|
---|
[8a121b1] | 278 | static void usb_dp_browse_simple_internal(const usb_dp_parser_t *parser,
|
---|
| 279 | const usb_dp_parser_data_t *data, const uint8_t *root, size_t depth,
|
---|
| 280 | void (*callback)(const uint8_t *, size_t, void *), void *arg)
|
---|
[da77278] | 281 | {
|
---|
| 282 | if (root == NULL) {
|
---|
| 283 | return;
|
---|
| 284 | }
|
---|
| 285 | callback(root, depth, arg);
|
---|
[8a121b1] | 286 | const uint8_t *child = usb_dp_get_nested_descriptor(parser, data, root);
|
---|
[da77278] | 287 | do {
|
---|
| 288 | usb_dp_browse_simple_internal(parser, data, child, depth + 1,
|
---|
| 289 | callback, arg);
|
---|
| 290 | child = usb_dp_get_sibling_descriptor(parser, data,
|
---|
| 291 | root, child);
|
---|
| 292 | } while (child != NULL);
|
---|
| 293 | }
|
---|
| 294 |
|
---|
| 295 | /** Browse flatten descriptor tree.
|
---|
| 296 | *
|
---|
| 297 | * The callback is called with following arguments: pointer to the start
|
---|
| 298 | * of the descriptor (somewhere inside @p descriptors), depth of the nesting
|
---|
| 299 | * (starting from 0 for the first descriptor) and the custom argument.
|
---|
| 300 | * Note that the size of the descriptor is not passed because it can
|
---|
| 301 | * be read from the first byte of the descriptor.
|
---|
| 302 | *
|
---|
| 303 | * @param descriptors Descriptor data.
|
---|
| 304 | * @param descriptors_size Size of descriptor data (in bytes).
|
---|
| 305 | * @param descriptor_nesting Possible descriptor nesting.
|
---|
| 306 | * @param callback Callback for each found descriptor.
|
---|
| 307 | * @param arg Custom (user) argument.
|
---|
| 308 | */
|
---|
[25effe2] | 309 | void usb_dp_walk_simple(const uint8_t *descriptors, size_t descriptors_size,
|
---|
[8a121b1] | 310 | const usb_dp_descriptor_nesting_t *descriptor_nesting,
|
---|
| 311 | walk_callback_t callback, void *arg)
|
---|
[da77278] | 312 | {
|
---|
| 313 | if ((descriptors == NULL) || (descriptors_size == 0)
|
---|
| 314 | || (descriptor_nesting == NULL) || (callback == NULL)) {
|
---|
| 315 | return;
|
---|
| 316 | }
|
---|
| 317 |
|
---|
[8a121b1] | 318 | const usb_dp_parser_data_t data = {
|
---|
[da77278] | 319 | .data = descriptors,
|
---|
| 320 | .size = descriptors_size,
|
---|
| 321 | .arg = NULL
|
---|
| 322 | };
|
---|
| 323 |
|
---|
[8a121b1] | 324 | const usb_dp_parser_t parser = {
|
---|
[da77278] | 325 | .nesting = descriptor_nesting
|
---|
| 326 | };
|
---|
| 327 |
|
---|
| 328 | usb_dp_browse_simple_internal(&parser, &data, descriptors,
|
---|
| 329 | 0, callback, arg);
|
---|
| 330 | }
|
---|
[5ccb15c] | 331 |
|
---|
| 332 | /** @}
|
---|
| 333 | */
|
---|