[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),
|
---|
| 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
|
---|
[5ccb15c] | 72 |
|
---|
| 73 | /** Tells whether pointer points inside descriptor data.
|
---|
| 74 | *
|
---|
| 75 | * @param data Parser data.
|
---|
| 76 | * @param ptr Pointer to be verified.
|
---|
[3b77628] | 77 | * @return Whether @p ptr points inside <code>data->data</code> field.
|
---|
[5ccb15c] | 78 | */
|
---|
[8a121b1] | 79 | static bool is_valid_descriptor_pointer(const usb_dp_parser_data_t *data,
|
---|
| 80 | const uint8_t *ptr)
|
---|
[5ccb15c] | 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 | */
|
---|
[8a121b1] | 104 | static const uint8_t *get_next_descriptor(const usb_dp_parser_data_t *data,
|
---|
| 105 | const uint8_t *current)
|
---|
[5ccb15c] | 106 | {
|
---|
| 107 | assert(is_valid_descriptor_pointer(data, current));
|
---|
| 108 |
|
---|
[8a121b1] | 109 | const uint8_t current_length = *current;
|
---|
| 110 | const uint8_t *next = current + current_length;
|
---|
[5ccb15c] | 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 | */
|
---|
[8a121b1] | 128 | static int get_descriptor_type(const usb_dp_parser_data_t *data, const uint8_t *start)
|
---|
[5ccb15c] | 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 | */
|
---|
[8a121b1] | 149 | static bool is_nested_descriptor_type(const usb_dp_parser_t *parser,
|
---|
[5ccb15c] | 150 | int child, int parent)
|
---|
| 151 | {
|
---|
[8a121b1] | 152 | const usb_dp_descriptor_nesting_t *nesting = parser->nesting;
|
---|
[5ccb15c] | 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 | */
|
---|
[8a121b1] | 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)
|
---|
[5ccb15c] | 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 | */
|
---|
[8a121b1] | 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)
|
---|
[5ccb15c] | 189 | {
|
---|
| 190 | if (!is_valid_descriptor_pointer(data, parent)) {
|
---|
| 191 | return NULL;
|
---|
| 192 | }
|
---|
| 193 |
|
---|
[8a121b1] | 194 | const uint8_t *next = get_next_descriptor(data, parent);
|
---|
[5ccb15c] | 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 | */
|
---|
[8a121b1] | 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)
|
---|
[5ccb15c] | 217 | {
|
---|
[8a121b1] | 218 | const uint8_t *child =
|
---|
| 219 | usb_dp_get_nested_descriptor(parser, data, parent);
|
---|
[5ccb15c] | 220 | if (child == NULL) {
|
---|
| 221 | return get_next_descriptor(data, parent);
|
---|
| 222 | }
|
---|
[8a121b1] | 223 | const uint8_t *next_child =
|
---|
| 224 | skip_nested_descriptors(parser, data, child);
|
---|
[5ccb15c] | 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 | */
|
---|
[8a121b1] | 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)
|
---|
[5ccb15c] | 245 | {
|
---|
| 246 | if (!is_valid_descriptor_pointer(data, parent)
|
---|
| 247 | || !is_valid_descriptor_pointer(data, sibling)) {
|
---|
| 248 | return NULL;
|
---|
| 249 | }
|
---|
| 250 |
|
---|
[8a121b1] | 251 | const uint8_t *possible_sibling =
|
---|
| 252 | skip_nested_descriptors(parser, data, sibling);
|
---|
[5ccb15c] | 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 |
|
---|
[da77278] | 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 | */
|
---|
[8a121b1] | 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)
|
---|
[da77278] | 280 | {
|
---|
| 281 | if (root == NULL) {
|
---|
| 282 | return;
|
---|
| 283 | }
|
---|
| 284 | callback(root, depth, arg);
|
---|
[8a121b1] | 285 | const uint8_t *child = usb_dp_get_nested_descriptor(parser, data, root);
|
---|
[da77278] | 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 | */
|
---|
[25effe2] | 308 | void usb_dp_walk_simple(const uint8_t *descriptors, size_t descriptors_size,
|
---|
[8a121b1] | 309 | const usb_dp_descriptor_nesting_t *descriptor_nesting,
|
---|
| 310 | walk_callback_t callback, void *arg)
|
---|
[da77278] | 311 | {
|
---|
| 312 | if ((descriptors == NULL) || (descriptors_size == 0)
|
---|
| 313 | || (descriptor_nesting == NULL) || (callback == NULL)) {
|
---|
| 314 | return;
|
---|
| 315 | }
|
---|
| 316 |
|
---|
[8a121b1] | 317 | const usb_dp_parser_data_t data = {
|
---|
[da77278] | 318 | .data = descriptors,
|
---|
| 319 | .size = descriptors_size,
|
---|
| 320 | .arg = NULL
|
---|
| 321 | };
|
---|
| 322 |
|
---|
[8a121b1] | 323 | const usb_dp_parser_t parser = {
|
---|
[da77278] | 324 | .nesting = descriptor_nesting
|
---|
| 325 | };
|
---|
| 326 |
|
---|
| 327 | usb_dp_browse_simple_internal(&parser, &data, descriptors,
|
---|
| 328 | 0, callback, arg);
|
---|
| 329 | }
|
---|
[5ccb15c] | 330 |
|
---|
| 331 | /** @}
|
---|
| 332 | */
|
---|