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