]> git.ipfire.org Git - thirdparty/systemd.git/blob - src/udev/udev-builtin-usb_id.c
udev: fix codesonar warnings
[thirdparty/systemd.git] / src / udev / udev-builtin-usb_id.c
1 /* SPDX-License-Identifier: GPL-2.0+ */
2 /*
3 * USB device properties and persistent device path
4 *
5 * Copyright (c) 2005 SUSE Linux Products GmbH, Germany
6 * Author: Hannes Reinecke <hare@suse.de>
7 */
8
9 #include <ctype.h>
10 #include <errno.h>
11 #include <fcntl.h>
12 #include <stdarg.h>
13 #include <stdio.h>
14 #include <stdlib.h>
15 #include <unistd.h>
16
17 #include "alloc-util.h"
18 #include "device-util.h"
19 #include "fd-util.h"
20 #include "libudev-util.h"
21 #include "string-util.h"
22 #include "strxcpyx.h"
23 #include "udev-builtin.h"
24
25 static void set_usb_iftype(char *to, int if_class_num, size_t len) {
26 const char *type = "generic";
27
28 switch (if_class_num) {
29 case 1:
30 type = "audio";
31 break;
32 case 2: /* CDC-Control */
33 break;
34 case 3:
35 type = "hid";
36 break;
37 case 5: /* Physical */
38 break;
39 case 6:
40 type = "media";
41 break;
42 case 7:
43 type = "printer";
44 break;
45 case 8:
46 type = "storage";
47 break;
48 case 9:
49 type = "hub";
50 break;
51 case 0x0a: /* CDC-Data */
52 break;
53 case 0x0b: /* Chip/Smart Card */
54 break;
55 case 0x0d: /* Content Security */
56 break;
57 case 0x0e:
58 type = "video";
59 break;
60 case 0xdc: /* Diagnostic Device */
61 break;
62 case 0xe0: /* Wireless Controller */
63 break;
64 case 0xfe: /* Application-specific */
65 break;
66 case 0xff: /* Vendor-specific */
67 break;
68 default:
69 break;
70 }
71 strncpy(to, type, len);
72 to[len-1] = '\0';
73 }
74
75 static int set_usb_mass_storage_ifsubtype(char *to, const char *from, size_t len) {
76 int type_num = 0;
77 char *eptr;
78 const char *type = "generic";
79
80 type_num = strtoul(from, &eptr, 0);
81 if (eptr != from) {
82 switch (type_num) {
83 case 1: /* RBC devices */
84 type = "rbc";
85 break;
86 case 2:
87 type = "atapi";
88 break;
89 case 3:
90 type = "tape";
91 break;
92 case 4: /* UFI */
93 type = "floppy";
94 break;
95 case 6: /* Transparent SPC-2 devices */
96 type = "scsi";
97 break;
98 default:
99 break;
100 }
101 }
102 strscpy(to, len, type);
103 return type_num;
104 }
105
106 static void set_scsi_type(char *to, const char *from, size_t len) {
107 int type_num;
108 char *eptr;
109 const char *type = "generic";
110
111 type_num = strtoul(from, &eptr, 0);
112 if (eptr != from) {
113 switch (type_num) {
114 case 0:
115 case 0xe:
116 type = "disk";
117 break;
118 case 1:
119 type = "tape";
120 break;
121 case 4:
122 case 7:
123 case 0xf:
124 type = "optical";
125 break;
126 case 5:
127 type = "cd";
128 break;
129 default:
130 break;
131 }
132 }
133 strscpy(to, len, type);
134 }
135
136 #define USB_DT_DEVICE 0x01
137 #define USB_DT_INTERFACE 0x04
138
139 static int dev_if_packed_info(sd_device *dev, char *ifs_str, size_t len) {
140 _cleanup_close_ int fd = -1;
141 ssize_t size;
142 unsigned char buf[18 + 65535];
143 size_t pos = 0;
144 unsigned strpos = 0;
145 const char *filename, *syspath;
146 int r;
147 struct usb_interface_descriptor {
148 uint8_t bLength;
149 uint8_t bDescriptorType;
150 uint8_t bInterfaceNumber;
151 uint8_t bAlternateSetting;
152 uint8_t bNumEndpoints;
153 uint8_t bInterfaceClass;
154 uint8_t bInterfaceSubClass;
155 uint8_t bInterfaceProtocol;
156 uint8_t iInterface;
157 } _packed_;
158
159 r = sd_device_get_syspath(dev, &syspath);
160 if (r < 0)
161 return r;
162
163 filename = strjoina(syspath, "/descriptors");
164 fd = open(filename, O_RDONLY|O_CLOEXEC);
165 if (fd < 0)
166 return log_device_debug_errno(dev, errno, "Failed to open \"%s\": %m", filename);
167
168 size = read(fd, buf, sizeof(buf));
169 if (size < 18)
170 return log_device_warning_errno(dev, SYNTHETIC_ERRNO(EIO),
171 "Short read from \"%s\"", filename);
172 assert((size_t) size <= sizeof buf);
173
174 ifs_str[0] = '\0';
175 while (pos + sizeof(struct usb_interface_descriptor) < (size_t) size &&
176 strpos + 7 < len - 2) {
177
178 struct usb_interface_descriptor *desc;
179 char if_str[8];
180
181 desc = (struct usb_interface_descriptor *) (buf + pos);
182 if (desc->bLength < 3)
183 break;
184 if (desc->bLength > size - sizeof(struct usb_interface_descriptor))
185 return log_device_debug_errno(dev, SYNTHETIC_ERRNO(EIO),
186 "Corrupt data read from \"%s\"", filename);
187 pos += desc->bLength;
188
189 if (desc->bDescriptorType != USB_DT_INTERFACE)
190 continue;
191
192 if (snprintf(if_str, 8, ":%02x%02x%02x",
193 desc->bInterfaceClass,
194 desc->bInterfaceSubClass,
195 desc->bInterfaceProtocol) != 7)
196 continue;
197
198 if (strstr(ifs_str, if_str))
199 continue;
200
201 memcpy(&ifs_str[strpos], if_str, 8),
202 strpos += 7;
203 }
204
205 if (strpos > 0) {
206 ifs_str[strpos++] = ':';
207 ifs_str[strpos++] = '\0';
208 }
209
210 return 0;
211 }
212
213 /*
214 * A unique USB identification is generated like this:
215 *
216 * 1.) Get the USB device type from InterfaceClass and InterfaceSubClass
217 * 2.) If the device type is 'Mass-Storage/SPC-2' or 'Mass-Storage/RBC',
218 * use the SCSI vendor and model as USB-Vendor and USB-model.
219 * 3.) Otherwise, use the USB manufacturer and product as
220 * USB-Vendor and USB-model. Any non-printable characters
221 * in those strings will be skipped; a slash '/' will be converted
222 * into a full stop '.'.
223 * 4.) If that fails, too, we will use idVendor and idProduct
224 * as USB-Vendor and USB-model.
225 * 5.) The USB identification is the USB-vendor and USB-model
226 * string concatenated with an underscore '_'.
227 * 6.) If the device supplies a serial number, this number
228 * is concatenated with the identification with an underscore '_'.
229 */
230 static int builtin_usb_id(sd_device *dev, int argc, char *argv[], bool test) {
231 char vendor_str[64] = "";
232 char vendor_str_enc[256];
233 const char *vendor_id;
234 char model_str[64] = "";
235 char model_str_enc[256];
236 const char *product_id;
237 char serial_str[UTIL_NAME_SIZE] = "";
238 char packed_if_str[UTIL_NAME_SIZE] = "";
239 char revision_str[64] = "";
240 char type_str[64] = "";
241 char instance_str[64] = "";
242 const char *ifnum = NULL;
243 const char *driver = NULL;
244 char serial[256];
245
246 sd_device *dev_interface, *dev_usb;
247 const char *if_class, *if_subclass;
248 int if_class_num;
249 int protocol = 0;
250 size_t l;
251 char *s;
252
253 const char *syspath, *sysname, *devtype, *interface_syspath;
254 int r;
255
256 assert(dev);
257
258 r = sd_device_get_syspath(dev, &syspath);
259 if (r < 0)
260 return r;
261
262 r = sd_device_get_sysname(dev, &sysname);
263 if (r < 0)
264 return r;
265
266 /* shortcut, if we are called directly for a "usb_device" type */
267 if (sd_device_get_devtype(dev, &devtype) >= 0 && streq(devtype, "usb_device")) {
268 dev_if_packed_info(dev, packed_if_str, sizeof(packed_if_str));
269 dev_usb = dev;
270 goto fallback;
271 }
272
273 /* usb interface directory */
274 r = sd_device_get_parent_with_subsystem_devtype(dev, "usb", "usb_interface", &dev_interface);
275 if (r < 0)
276 return log_device_debug_errno(dev, r, "Failed to access usb_interface: %m");
277
278 r = sd_device_get_syspath(dev_interface, &interface_syspath);
279 if (r < 0)
280 return log_device_debug_errno(dev_interface, r, "Failed to get syspath: %m");
281 (void) sd_device_get_sysattr_value(dev_interface, "bInterfaceNumber", &ifnum);
282 (void) sd_device_get_sysattr_value(dev_interface, "driver", &driver);
283
284 r = sd_device_get_sysattr_value(dev_interface, "bInterfaceClass", &if_class);
285 if (r < 0)
286 return log_device_debug_errno(dev_interface, r, "Failed to get bInterfaceClass attribute: %m");
287
288 if_class_num = strtoul(if_class, NULL, 16);
289 if (if_class_num == 8) {
290 /* mass storage */
291 if (sd_device_get_sysattr_value(dev_interface, "bInterfaceSubClass", &if_subclass) >= 0)
292 protocol = set_usb_mass_storage_ifsubtype(type_str, if_subclass, sizeof(type_str)-1);
293 } else
294 set_usb_iftype(type_str, if_class_num, sizeof(type_str)-1);
295
296 log_device_debug(dev_interface, "if_class:%d protocol:%d", if_class_num, protocol);
297
298 /* usb device directory */
299 r = sd_device_get_parent_with_subsystem_devtype(dev_interface, "usb", "usb_device", &dev_usb);
300 if (r < 0)
301 return log_device_debug_errno(dev_interface, r, "Failed to find parent 'usb' device");
302
303 /* all interfaces of the device in a single string */
304 dev_if_packed_info(dev_usb, packed_if_str, sizeof(packed_if_str));
305
306 /* mass storage : SCSI or ATAPI */
307 if (IN_SET(protocol, 6, 2)) {
308 sd_device *dev_scsi;
309 const char *scsi_sysname, *scsi_model, *scsi_vendor, *scsi_type, *scsi_rev;
310 int host, bus, target, lun;
311
312 /* get scsi device */
313 r = sd_device_get_parent_with_subsystem_devtype(dev, "scsi", "scsi_device", &dev_scsi);
314 if (r < 0) {
315 log_device_debug_errno(dev, r, "Unable to find parent SCSI device");
316 goto fallback;
317 }
318 if (sd_device_get_sysname(dev_scsi, &scsi_sysname) < 0)
319 goto fallback;
320 if (sscanf(scsi_sysname, "%d:%d:%d:%d", &host, &bus, &target, &lun) != 4) {
321 log_device_debug(dev_scsi, "Invalid SCSI device");
322 goto fallback;
323 }
324
325 /* Generic SPC-2 device */
326 r = sd_device_get_sysattr_value(dev_scsi, "vendor", &scsi_vendor);
327 if (r < 0) {
328 log_device_debug_errno(dev_scsi, r, "Failed to get SCSI vendor attribute: %m");
329 goto fallback;
330 }
331 udev_util_encode_string(scsi_vendor, vendor_str_enc, sizeof(vendor_str_enc));
332 util_replace_whitespace(scsi_vendor, vendor_str, sizeof(vendor_str)-1);
333 util_replace_chars(vendor_str, NULL);
334
335 r = sd_device_get_sysattr_value(dev_scsi, "model", &scsi_model);
336 if (r < 0) {
337 log_device_debug_errno(dev_scsi, r, "Failed to get SCSI model attribute: %m");
338 goto fallback;
339 }
340 udev_util_encode_string(scsi_model, model_str_enc, sizeof(model_str_enc));
341 util_replace_whitespace(scsi_model, model_str, sizeof(model_str)-1);
342 util_replace_chars(model_str, NULL);
343
344 r = sd_device_get_sysattr_value(dev_scsi, "type", &scsi_type);
345 if (r < 0) {
346 log_device_debug_errno(dev_scsi, r, "Failed to get SCSI type attribute: %m");
347 goto fallback;
348 }
349 set_scsi_type(type_str, scsi_type, sizeof(type_str)-1);
350
351 r = sd_device_get_sysattr_value(dev_scsi, "rev", &scsi_rev);
352 if (r < 0) {
353 log_device_debug_errno(dev_scsi, r, "Failed to get SCSI revision attribute: %m");
354 goto fallback;
355 }
356 util_replace_whitespace(scsi_rev, revision_str, sizeof(revision_str)-1);
357 util_replace_chars(revision_str, NULL);
358
359 /*
360 * some broken devices have the same identifiers
361 * for all luns, export the target:lun number
362 */
363 sprintf(instance_str, "%d:%d", target, lun);
364 }
365
366 fallback:
367 r = sd_device_get_sysattr_value(dev_usb, "idVendor", &vendor_id);
368 if (r < 0)
369 return log_device_debug_errno(dev_usb, r, "Failed to get idVendor attribute: %m");
370
371 r = sd_device_get_sysattr_value(dev_usb, "idProduct", &product_id);
372 if (r < 0)
373 return log_device_debug_errno(dev_usb, r, "Failed to get idProduct attribute: %m");
374
375 /* fallback to USB vendor & device */
376 if (vendor_str[0] == '\0') {
377 const char *usb_vendor;
378
379 if (sd_device_get_sysattr_value(dev_usb, "manufacturer", &usb_vendor) < 0)
380 usb_vendor = vendor_id;
381 udev_util_encode_string(usb_vendor, vendor_str_enc, sizeof(vendor_str_enc));
382 util_replace_whitespace(usb_vendor, vendor_str, sizeof(vendor_str)-1);
383 util_replace_chars(vendor_str, NULL);
384 }
385
386 if (model_str[0] == '\0') {
387 const char *usb_model;
388
389 if (sd_device_get_sysattr_value(dev_usb, "product", &usb_model) < 0)
390 usb_model = product_id;
391 udev_util_encode_string(usb_model, model_str_enc, sizeof(model_str_enc));
392 util_replace_whitespace(usb_model, model_str, sizeof(model_str)-1);
393 util_replace_chars(model_str, NULL);
394 }
395
396 if (revision_str[0] == '\0') {
397 const char *usb_rev;
398
399 if (sd_device_get_sysattr_value(dev_usb, "bcdDevice", &usb_rev) >= 0) {
400 util_replace_whitespace(usb_rev, revision_str, sizeof(revision_str)-1);
401 util_replace_chars(revision_str, NULL);
402 }
403 }
404
405 if (serial_str[0] == '\0') {
406 const char *usb_serial;
407
408 if (sd_device_get_sysattr_value(dev_usb, "serial", &usb_serial) >= 0) {
409 const unsigned char *p;
410
411 /* http://msdn.microsoft.com/en-us/library/windows/hardware/gg487321.aspx */
412 for (p = (unsigned char *) usb_serial; *p != '\0'; p++)
413 if (*p < 0x20 || *p > 0x7f || *p == ',') {
414 usb_serial = NULL;
415 break;
416 }
417
418 if (usb_serial) {
419 util_replace_whitespace(usb_serial, serial_str, sizeof(serial_str)-1);
420 util_replace_chars(serial_str, NULL);
421 }
422 }
423 }
424
425 s = serial;
426 l = strpcpyl(&s, sizeof(serial), vendor_str, "_", model_str, NULL);
427 if (!isempty(serial_str))
428 l = strpcpyl(&s, l, "_", serial_str, NULL);
429
430 if (!isempty(instance_str))
431 strpcpyl(&s, l, "-", instance_str, NULL);
432
433 udev_builtin_add_property(dev, test, "ID_VENDOR", vendor_str);
434 udev_builtin_add_property(dev, test, "ID_VENDOR_ENC", vendor_str_enc);
435 udev_builtin_add_property(dev, test, "ID_VENDOR_ID", vendor_id);
436 udev_builtin_add_property(dev, test, "ID_MODEL", model_str);
437 udev_builtin_add_property(dev, test, "ID_MODEL_ENC", model_str_enc);
438 udev_builtin_add_property(dev, test, "ID_MODEL_ID", product_id);
439 udev_builtin_add_property(dev, test, "ID_REVISION", revision_str);
440 udev_builtin_add_property(dev, test, "ID_SERIAL", serial);
441 if (!isempty(serial_str))
442 udev_builtin_add_property(dev, test, "ID_SERIAL_SHORT", serial_str);
443 if (!isempty(type_str))
444 udev_builtin_add_property(dev, test, "ID_TYPE", type_str);
445 if (!isempty(instance_str))
446 udev_builtin_add_property(dev, test, "ID_INSTANCE", instance_str);
447 udev_builtin_add_property(dev, test, "ID_BUS", "usb");
448 if (!isempty(packed_if_str))
449 udev_builtin_add_property(dev, test, "ID_USB_INTERFACES", packed_if_str);
450 if (ifnum)
451 udev_builtin_add_property(dev, test, "ID_USB_INTERFACE_NUM", ifnum);
452 if (driver)
453 udev_builtin_add_property(dev, test, "ID_USB_DRIVER", driver);
454 return 0;
455 }
456
457 const UdevBuiltin udev_builtin_usb_id = {
458 .name = "usb_id",
459 .cmd = builtin_usb_id,
460 .help = "USB device properties",
461 .run_once = true,
462 };