1 /*
2  * Copyright (c) 2021-2022 Huawei Device Co., Ltd.
3  * Licensed under the Apache License, Version 2.0 (the "License");
4  * you may not use this file except in compliance with the License.
5  * You may obtain a copy of the License at
6  *
7  *     http://www.apache.org/licenses/LICENSE-2.0
8  *
9  * Unless required by applicable law or agreed to in writing, software
10  * distributed under the License is distributed on an "AS IS" BASIS,
11  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12  * See the License for the specific language governing permissions and
13  * limitations under the License.
14  */
15 
16 #include "usb_descriptor_parser.h"
17 #include "hilog_wrapper.h"
18 #include "message_parcel.h"
19 #include "securec.h"
20 #include "usb_config.h"
21 #include "usb_endpoint.h"
22 #include "usb_errors.h"
23 #include "usb_interface.h"
24 #include "usbd_type.h"
25 
26 static constexpr uint8_t NORMAL_ENDPOINT_DESCRIPTOR = 7;
27 static constexpr uint8_t AUDIO_ENDPOINT_DESCRIPTOR = 9;
28 namespace OHOS {
29 namespace USB {
30 enum class DescriptorType {
31     DESCRIPTOR_TYPE_DEVICE = 1,
32     DESCRIPTOR_TYPE_CONFIG = 2,
33     DESCRIPTOR_TYPE_INTERFACE = 4,
34     DESCRIPTOR_TYPE_ENDPOINT = 5
35 };
36 
UsbDescriptorParser()37 UsbDescriptorParser::UsbDescriptorParser() {}
38 
~UsbDescriptorParser()39 UsbDescriptorParser::~UsbDescriptorParser() {}
40 
ParseDeviceDescriptor(const uint8_t * buffer,uint32_t length,UsbDevice & dev)41 int32_t UsbDescriptorParser::ParseDeviceDescriptor(const uint8_t *buffer, uint32_t length, UsbDevice &dev)
42 {
43     if (buffer == nullptr || length == 0) {
44         USB_HILOGE(MODULE_USB_SERVICE, "buffer is null");
45         return UEC_SERVICE_INVALID_VALUE;
46     }
47 
48     USB_HILOGD(MODULE_USB_SERVICE, "parse begin length=%{public}u", length);
49     uint32_t deviceDescriptorSize = sizeof(UsbdDeviceDescriptor);
50     if (length < deviceDescriptorSize) {
51         USB_HILOGE(MODULE_USB_SERVICE, "buffer size error");
52         return UEC_SERVICE_INVALID_VALUE;
53     }
54 
55     UsbdDeviceDescriptor deviceDescriptor = *(reinterpret_cast<const UsbdDeviceDescriptor *>(buffer));
56     if (deviceDescriptor.bLength != deviceDescriptorSize) {
57         USB_HILOGE(MODULE_USB_SERVICE, "UsbdDeviceDescriptor size error");
58         return UEC_SERVICE_INVALID_VALUE;
59     }
60 
61     dev.SetVendorId(deviceDescriptor.idVendor);
62     dev.SetProductId(deviceDescriptor.idProduct);
63     dev.SetClass(deviceDescriptor.bDeviceClass);
64     dev.SetSubclass(deviceDescriptor.bDeviceSubClass);
65     dev.SetDescConfigCount(deviceDescriptor.bNumConfigurations);
66 
67     dev.SetbMaxPacketSize0(deviceDescriptor.bMaxPacketSize0);
68     dev.SetbcdDevice(deviceDescriptor.bcdDevice);
69     dev.SetbcdUSB(deviceDescriptor.bcdUSB);
70     dev.SetiManufacturer(deviceDescriptor.iManufacturer);
71     dev.SetiProduct(deviceDescriptor.iProduct);
72     dev.SetiSerialNumber(deviceDescriptor.iSerialNumber);
73     return UEC_OK;
74 }
75 
AddConfig(std::vector<USBConfig> & configs,const UsbdConfigDescriptor * configDescriptor)76 static int32_t AddConfig(std::vector<USBConfig> &configs, const UsbdConfigDescriptor *configDescriptor)
77 {
78     if (configDescriptor == nullptr) {
79         USB_HILOGE(MODULE_USB_SERVICE, "configDescriptor is nullptr");
80         return UEC_SERVICE_INVALID_VALUE;
81     }
82 
83     USBConfig config;
84     config.SetId(configDescriptor->bConfigurationValue);
85     config.SetAttribute(configDescriptor->bmAttributes);
86     config.SetMaxPower(configDescriptor->bMaxPower);
87     config.SetiConfiguration(configDescriptor->iConfiguration);
88     configs.emplace_back(config);
89     USB_HILOGD(MODULE_USB_SERVICE, "add config, interfaces=%{public}u", configDescriptor->bNumInterfaces);
90     return UEC_OK;
91 }
92 
AddInterface(std::vector<USBConfig> & configs,const UsbdInterfaceDescriptor * interfaceDescriptor)93 static int32_t AddInterface(std::vector<USBConfig> &configs, const UsbdInterfaceDescriptor *interfaceDescriptor)
94 {
95     if (interfaceDescriptor == nullptr) {
96         USB_HILOGE(MODULE_USB_SERVICE, "interfaceDescriptor is nullptr");
97         return UEC_SERVICE_INVALID_VALUE;
98     }
99     if (configs.empty()) {
100         USB_HILOGE(MODULE_USB_SERVICE, "config descriptor not found");
101         return UEC_SERVICE_INVALID_VALUE;
102     }
103 
104     UsbInterface interface;
105     interface.SetId(interfaceDescriptor->bInterfaceNumber);
106     interface.SetProtocol(interfaceDescriptor->bInterfaceProtocol);
107     interface.SetAlternateSetting(interfaceDescriptor->bAlternateSetting);
108     interface.SetClass(interfaceDescriptor->bInterfaceClass);
109     interface.SetSubClass(interfaceDescriptor->bInterfaceSubClass);
110     interface.SetiInterface(interfaceDescriptor->iInterface);
111     configs.back().GetInterfaces().emplace_back(interface);
112     USB_HILOGD(MODULE_USB_SERVICE, "add interface, endpoints=%{public}u", interfaceDescriptor->bNumEndpoints);
113     return UEC_OK;
114 }
115 
AddEndpoint(std::vector<USBConfig> & configs,const UsbdEndpointDescriptor * endpointDescriptor)116 static int32_t AddEndpoint(std::vector<USBConfig> &configs, const UsbdEndpointDescriptor *endpointDescriptor)
117 {
118     if (endpointDescriptor == nullptr) {
119         USB_HILOGE(MODULE_USB_SERVICE, "endpointDescriptor is nullptr");
120         return UEC_SERVICE_INVALID_VALUE;
121     }
122     if (configs.empty() || configs.back().GetInterfaces().empty()) {
123         USB_HILOGE(MODULE_USB_SERVICE, "interface descriptor not found");
124         return UEC_SERVICE_INVALID_VALUE;
125     }
126 
127     USBEndpoint endpoint;
128     endpoint.SetAddr(endpointDescriptor->bEndpointAddress);
129     endpoint.SetAttr(endpointDescriptor->bmAttributes);
130     endpoint.SetInterval(endpointDescriptor->bInterval);
131     endpoint.SetMaxPacketSize(endpointDescriptor->wMaxPacketSize);
132     endpoint.SetInterfaceId(configs.back().GetInterfaces().back().GetId());
133     configs.back().GetInterfaces().back().GetEndpoints().emplace_back(endpoint);
134     USB_HILOGD(MODULE_USB_SERVICE, "add endpoint, address=%{public}u", endpointDescriptor->bEndpointAddress);
135     return UEC_OK;
136 }
137 
ParseConfigDescriptors(std::vector<uint8_t> & descriptor,uint32_t offset,std::vector<USBConfig> & configs)138 int32_t UsbDescriptorParser::ParseConfigDescriptors(std::vector<uint8_t> &descriptor, uint32_t offset,
139     std::vector<USBConfig> &configs)
140 {
141     uint8_t *buffer = descriptor.data();
142     uint32_t length = descriptor.size();
143     uint32_t cursor = offset;
144     int32_t ret = UEC_OK;
145 
146     while (cursor < length) {
147         if ((length - cursor) < sizeof(UsbdDescriptorHeader)) {
148             USB_HILOGW(MODULE_USB_SERVICE, "invalid desc data, length=%{public}u, cursor=%{public}u", length, cursor);
149             break;
150         }
151         UsbdDescriptorHeader descriptorHeader = *(reinterpret_cast<const UsbdDescriptorHeader *>(buffer + cursor));
152         if (descriptorHeader.bLength > (length - cursor)) {
153             USB_HILOGW(MODULE_USB_SERVICE, "invalid data length, length=%{public}u, cursor=%{public}u", length, cursor);
154             break;
155         }
156         switch (descriptorHeader.bDescriptorType) {
157             case static_cast<uint8_t>(DescriptorType::DESCRIPTOR_TYPE_CONFIG):
158                 if (descriptorHeader.bLength != sizeof(UsbdConfigDescriptor)) {
159                     USB_HILOGE(MODULE_USB_SERVICE, "invalid config, length=%{public}u", descriptorHeader.bLength);
160                     return UEC_SERVICE_INVALID_VALUE;
161                 }
162                 ret = AddConfig(configs, reinterpret_cast<const UsbdConfigDescriptor *>(buffer + cursor));
163                 break;
164             case static_cast<uint8_t>(DescriptorType::DESCRIPTOR_TYPE_INTERFACE):
165                 if (descriptorHeader.bLength != sizeof(UsbdInterfaceDescriptor)) {
166                     USB_HILOGE(MODULE_USB_SERVICE, "invalid interface, length=%{public}u", descriptorHeader.bLength);
167                     return UEC_SERVICE_INVALID_VALUE;
168                 }
169                 ret = AddInterface(configs, reinterpret_cast<const UsbdInterfaceDescriptor *>(buffer + cursor));
170                 break;
171             case static_cast<uint8_t>(DescriptorType::DESCRIPTOR_TYPE_ENDPOINT):
172                 if (descriptorHeader.bLength != NORMAL_ENDPOINT_DESCRIPTOR
173                     && descriptorHeader.bLength != AUDIO_ENDPOINT_DESCRIPTOR) {
174                     USB_HILOGE(MODULE_USB_SERVICE, "invalid endpoint, length=%{public}u", descriptorHeader.bLength);
175                     return UEC_SERVICE_INVALID_VALUE;
176                 }
177                 ret = AddEndpoint(configs, reinterpret_cast<const UsbdEndpointDescriptor *>(buffer + cursor));
178                 break;
179             default:
180                 USB_HILOGW(MODULE_USB_SERVICE, "unrecognized type=%{public}d", descriptorHeader.bDescriptorType);
181                 break;
182         }
183         if (ret != UEC_OK) {
184             return ret;
185         }
186         cursor += descriptorHeader.bLength;
187     }
188     return ret;
189 }
190 
ParseConfigDescriptor(const uint8_t * buffer,uint32_t length,uint32_t & cursor,USBConfig & config)191 int32_t UsbDescriptorParser::ParseConfigDescriptor(
192     const uint8_t *buffer, uint32_t length, uint32_t &cursor, USBConfig &config)
193 {
194     if (buffer == nullptr || length == 0) {
195         return UEC_SERVICE_INVALID_VALUE;
196     }
197 
198     USB_HILOGD(MODULE_USB_SERVICE, "parse begin length=%{public}u, cursor=%{public}u", length, cursor);
199     uint32_t configDescriptorSize = sizeof(UsbdConfigDescriptor);
200     UsbdConfigDescriptor configDescriptor = *(reinterpret_cast<const UsbdConfigDescriptor *>(buffer));
201     cursor += configDescriptorSize;
202     if (length < configDescriptorSize || configDescriptor.bLength != configDescriptorSize) {
203         USB_HILOGE(MODULE_USB_SERVICE, "size error length=%{public}u, configDescriptor.bLength=%{public}d",
204             length, configDescriptor.bLength);
205         return UEC_SERVICE_INVALID_VALUE;
206     }
207 
208     config.SetId(configDescriptor.bConfigurationValue);
209     config.SetAttribute(configDescriptor.bmAttributes);
210     config.SetMaxPower(configDescriptor.bMaxPower);
211     config.SetiConfiguration(configDescriptor.iConfiguration);
212 
213     std::vector<UsbInterface> interfaces;
214     for (int32_t i = 0; (i < configDescriptor.bNumInterfaces) && (cursor < length); ++i) {
215         uint32_t interfaceCursor = 0;
216         UsbInterface interface;
217         int32_t ret = ParseInterfaceDescriptor(
218             buffer + cursor + interfaceCursor, length - cursor - interfaceCursor, interfaceCursor, interface);
219         if (ret != UEC_OK) {
220             USB_HILOGE(MODULE_USB_SERVICE, "ParseInterfaceDescriptor failed, ret=%{public}d", ret);
221             return UEC_SERVICE_INVALID_VALUE;
222         }
223         bool isRepeat = false;
224         auto iter = interfaces.begin();
225         while (iter != interfaces.end()) {
226             if (iter->GetId() == interface.GetId()) {
227                 isRepeat = true;
228                 break;
229             }
230             iter++;
231         }
232         if (interface.GetEndpointCount() >= 0 && !isRepeat) {
233             interfaces.push_back(interface);
234         } else {
235             // retry
236             if (interface.GetEndpointCount() > 0 && iter != interfaces.end()) {
237                 USB_HILOGE(MODULE_USB_SERVICE, "get repeat interface id info, and has endpoints");
238                 *iter = interface;
239             }
240             --i;
241         }
242         cursor += interfaceCursor;
243     }
244     config.SetInterfaces(interfaces);
245     return UEC_OK;
246 }
247 
ParseInterfaceDescriptor(const uint8_t * buffer,uint32_t length,uint32_t & cursor,UsbInterface & interface)248 int32_t UsbDescriptorParser::ParseInterfaceDescriptor(
249     const uint8_t *buffer, uint32_t length, uint32_t &cursor, UsbInterface &interface)
250 {
251     if (buffer == nullptr || length == 0) {
252         return UEC_SERVICE_INVALID_VALUE;
253     }
254 
255     uint32_t descriptorHeaderSize = sizeof(UsbdDescriptorHeader);
256     while (static_cast<uint32_t>(cursor) < length) {
257         if (descriptorHeaderSize >= length) {
258             USB_HILOGE(MODULE_USB_SERVICE, "length error");
259             return UEC_SERVICE_INVALID_VALUE;
260         }
261         UsbdDescriptorHeader descriptorHeader = *(reinterpret_cast<const UsbdDescriptorHeader *>(buffer + cursor));
262         if (descriptorHeader.bLength > length) {
263             USB_HILOGE(MODULE_USB_SERVICE, "descriptor size error");
264             return UEC_SERVICE_INVALID_VALUE;
265         }
266         if (descriptorHeader.bDescriptorType == static_cast<uint8_t>(DescriptorType::DESCRIPTOR_TYPE_INTERFACE)) {
267             break;
268         }
269         cursor += descriptorHeader.bLength;
270         USB_HILOGD(MODULE_USB_SERVICE, "type = %{public}d, length=%{public}d", descriptorHeader.bDescriptorType,
271             descriptorHeader.bLength);
272     }
273 
274     UsbdInterfaceDescriptor interfaceDescriptor = *(reinterpret_cast<const UsbdInterfaceDescriptor *>(buffer + cursor));
275     if (interfaceDescriptor.bLength != sizeof(UsbdInterfaceDescriptor)) {
276         USB_HILOGE(MODULE_USB_SERVICE, "UsbdInterfaceDescriptor size error");
277         return UEC_SERVICE_INVALID_VALUE;
278     }
279     cursor += interfaceDescriptor.bLength;
280 
281     interface.SetId(interfaceDescriptor.bInterfaceNumber);
282     interface.SetProtocol(interfaceDescriptor.bInterfaceProtocol);
283     interface.SetAlternateSetting(interfaceDescriptor.bAlternateSetting);
284     interface.SetClass(interfaceDescriptor.bInterfaceClass);
285     interface.SetSubClass(interfaceDescriptor.bInterfaceSubClass);
286     interface.SetiInterface(interfaceDescriptor.iInterface);
287 
288     std::vector<USBEndpoint> eps;
289     for (int32_t j = 0; j < interfaceDescriptor.bNumEndpoints; ++j) {
290         uint32_t epCursor = 0;
291         USBEndpoint ep;
292         ParseEndpointDescriptor(buffer + cursor + epCursor, length - cursor - epCursor, epCursor, ep);
293         ep.SetInterfaceId(interfaceDescriptor.bInterfaceNumber);
294         eps.push_back(ep);
295         cursor += epCursor;
296     }
297     interface.SetEndpoints(eps);
298     USB_HILOGD(MODULE_USB_SERVICE, "interface to string : %{public}s", interface.ToString().c_str());
299     return UEC_OK;
300 }
301 
ParseEndpointDescriptor(const uint8_t * buffer,uint32_t length,uint32_t & cursor,USBEndpoint & ep)302 int32_t UsbDescriptorParser::ParseEndpointDescriptor(
303     const uint8_t *buffer, uint32_t length, uint32_t &cursor, USBEndpoint &ep)
304 {
305     USB_HILOGD(MODULE_USB_SERVICE, "parse begin, length=%{public}u, cursor=%{public}u", length, cursor);
306     if (buffer == nullptr || length == 0) {
307         return UEC_SERVICE_INVALID_VALUE;
308     }
309 
310     uint32_t descriptorHeaderSize = sizeof(UsbdDescriptorHeader);
311     while (static_cast<uint32_t>(cursor) < length) {
312         if (descriptorHeaderSize >= length) {
313             USB_HILOGE(MODULE_USB_SERVICE, "length error");
314             return UEC_SERVICE_INVALID_VALUE;
315         }
316         UsbdDescriptorHeader descriptorHeader = *(reinterpret_cast<const UsbdDescriptorHeader *>(buffer + cursor));
317         if (descriptorHeader.bLength > length) {
318             USB_HILOGE(MODULE_USB_SERVICE, "descriptor size error");
319             return UEC_SERVICE_INVALID_VALUE;
320         }
321         if (descriptorHeader.bDescriptorType == static_cast<uint8_t>(DescriptorType::DESCRIPTOR_TYPE_ENDPOINT)) {
322             break;
323         }
324         cursor += descriptorHeader.bLength;
325         USB_HILOGD(MODULE_USB_SERVICE, "error type = %{public}d, length=%{public}d", descriptorHeader.bDescriptorType,
326             descriptorHeader.bLength);
327     }
328 
329     UsbdEndpointDescriptor endpointDescriptor = *(reinterpret_cast<const UsbdEndpointDescriptor *>(buffer + cursor));
330     if (endpointDescriptor.bLength != NORMAL_ENDPOINT_DESCRIPTOR &&
331         endpointDescriptor.bLength != AUDIO_ENDPOINT_DESCRIPTOR) {
332         USB_HILOGE(MODULE_USB_SERVICE, "Endpoint descriptor size error, length=%{public}d", endpointDescriptor.bLength);
333         return UEC_SERVICE_INVALID_VALUE;
334     }
335     cursor += endpointDescriptor.bLength;
336 
337     ep.SetAddr(endpointDescriptor.bEndpointAddress);
338     ep.SetAttr(endpointDescriptor.bmAttributes);
339     ep.SetInterval(endpointDescriptor.bInterval);
340     ep.SetMaxPacketSize(endpointDescriptor.wMaxPacketSize);
341     return UEC_OK;
342 }
343 } // namespace USB
344 } // namespace OHOS
345