]> git.ipfire.org Git - thirdparty/ipxe.git/commitdiff
[usb] Expose USB device descriptor and strings via settings
authorMichael Brown <mcb30@ipxe.org>
Fri, 18 Oct 2024 12:13:28 +0000 (13:13 +0100)
committerMichael Brown <mcb30@ipxe.org>
Fri, 18 Oct 2024 12:13:51 +0000 (13:13 +0100)
Allow scripts to read basic information from USB device descriptors
via the settings mechanism.  For example:

  echo USB vendor ID: ${usb/${busloc}.8.2}
  echo USB device ID: ${usb/${busloc}.10.2}
  echo USB manufacturer name: ${usb/${busloc}.14.0}

The general syntax is

  usb/<bus:dev>.<offset>.<length>

where bus:dev is the USB bus:device address (as obtained via the
"usbscan" command, or from e.g. ${net0/busloc} for a USB network
device), and <offset> and <length> select the required portion of the
USB device descriptor.

Following the usage of SMBIOS settings tags, a <length> of zero may be
used to indicate that the byte at <offset> contains a USB string
descriptor index, and an <offset> of zero may be used to indicate that
the <length> contains a literal USB string descriptor index.

Since the byte at offset zero can never contain a string index, and a
literal string index can never be zero, the combination of both
<length> and <offset> being zero may be used to indicate that the
entire device descriptor is to be read as a raw hex dump.

Signed-off-by: Michael Brown <mcb30@ipxe.org>
src/config/config_usb.c
src/config/settings.h
src/drivers/bus/usb.c
src/drivers/bus/usb_settings.c [new file with mode: 0644]
src/drivers/net/ecm.c
src/include/ipxe/errfile.h

index b679aeb2743ccb3272c27e03d9d915212d2d5d78..10dec221a226d095d263c1c0cf5f24c17b6b073f 100644 (file)
@@ -22,6 +22,7 @@
 FILE_LICENCE ( GPL2_OR_LATER_OR_UBDL );
 
 #include <config/usb.h>
+#include <config/settings.h>
 
 /** @file
  *
@@ -63,3 +64,10 @@ REQUIRE_OBJECT ( usbblk );
 #ifdef USB_EFI
 REQUIRE_OBJECT ( efi_usb );
 #endif
+
+/*
+ * Drag in USB settings mechanism
+ */
+#ifdef USB_SETTINGS
+REQUIRE_OBJECT ( usb_settings );
+#endif
index d7f787d38e762a387b57239f6bfab0e1da54ae6f..7b4af4fdf7206518b00e2b338fd349e0dc1fc59d 100644 (file)
@@ -12,6 +12,7 @@ FILE_LICENCE ( GPL2_OR_LATER_OR_UBDL );
 #include <config/defaults.h>
 
 #define        PCI_SETTINGS    /* PCI device settings */
+#define        USB_SETTINGS    /* USB device settings */
 //#define      CPUID_SETTINGS  /* CPUID settings */
 //#define      MEMMAP_SETTINGS /* Memory map settings */
 //#define      VMWARE_SETTINGS /* VMware GuestInfo settings */
index e1e7d51f71e4587ae0febf081c1f9f48e49e8024..b3b361b0d7a908c3254cf5157037191b22c992e6 100644 (file)
@@ -914,10 +914,9 @@ static unsigned int usb_get_default_language ( struct usb_device *usb ) {
  */
 int usb_get_string_descriptor ( struct usb_device *usb, unsigned int index,
                                unsigned int language, char *buf, size_t len ) {
-       size_t max = ( len ? ( len - 1 /* NUL */ ) : 0 );
        struct {
                struct usb_descriptor_header header;
-               uint16_t character[max];
+               uint16_t character[len];
        } __attribute__ (( packed )) *desc;
        unsigned int actual;
        unsigned int i;
@@ -952,10 +951,9 @@ int usb_get_string_descriptor ( struct usb_device *usb, unsigned int index,
                   sizeof ( desc->character[0] ) );
 
        /* Copy to buffer */
-       for ( i = 0 ; ( ( i < actual ) && ( i < max ) ) ; i++ )
+       memset ( buf, 0, len );
+       for ( i = 0 ; ( ( i < actual ) && ( i < len ) ) ; i++ )
                buf[i] = le16_to_cpu ( desc->character[i] );
-       if ( len )
-               buf[i] = '\0';
 
        /* Free buffer */
        free ( desc );
diff --git a/src/drivers/bus/usb_settings.c b/src/drivers/bus/usb_settings.c
new file mode 100644 (file)
index 0000000..db6f94d
--- /dev/null
@@ -0,0 +1,176 @@
+/*
+ * Copyright (C) 2024 Michael Brown <mbrown@fensystems.co.uk>.
+ *
+ * This program is free software; you can redistribute it and/or
+ * modify it under the terms of the GNU General Public License as
+ * published by the Free Software Foundation; either version 2 of the
+ * License, or (at your option) any later version.
+ *
+ * This program is distributed in the hope that it will be useful, but
+ * WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
+ * General Public License for more details.
+ *
+ * You should have received a copy of the GNU General Public License
+ * along with this program; if not, write to the Free Software
+ * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
+ * 02110-1301, USA.
+ *
+ * You can also choose to distribute this program under the terms of
+ * the Unmodified Binary Distribution Licence (as given in the file
+ * COPYING.UBDL), provided that you have satisfied its requirements.
+ */
+
+FILE_LICENCE ( GPL2_OR_LATER_OR_UBDL );
+
+#include <stdio.h>
+#include <errno.h>
+#include <ipxe/usb.h>
+#include <ipxe/settings.h>
+#include <ipxe/init.h>
+
+/** @file
+ *
+ * USB device settings
+ *
+ */
+
+/** USB device settings scope */
+static const struct settings_scope usb_settings_scope;
+
+/**
+ * Check applicability of USB device setting
+ *
+ * @v settings         Settings block
+ * @v setting          Setting
+ * @ret applies                Setting applies within this settings block
+ */
+static int usb_settings_applies ( struct settings *settings __unused,
+                                 const struct setting *setting ) {
+
+       return ( setting->scope == &usb_settings_scope );
+}
+
+/**
+ * Fetch value of USB device setting
+ *
+ * @v settings         Settings block
+ * @v setting          Setting to fetch
+ * @v data             Buffer to fill with setting data
+ * @v len              Length of buffer
+ * @ret len            Length of setting data, or negative error
+ */
+static int usb_settings_fetch ( struct settings *settings __unused,
+                               struct setting *setting,
+                               void *data, size_t len ) {
+       uint8_t *dst = data;
+       const uint8_t *src;
+       const uint8_t *desc;
+       struct usb_bus *bus;
+       struct usb_device *usb;
+       int tag_direction;
+       unsigned int tag_busdev;
+       unsigned int tag_offset;
+       unsigned int tag_len;
+       unsigned int index;
+       int rc;
+
+       /* Extract parameters from tag */
+       tag_direction = ( ( setting->tag & ( 1 << 31 ) ) ? +1 : -1 );
+       tag_busdev = ( ( setting->tag >> 16 ) & 0x7fff );
+       tag_offset = ( ( setting->tag >> 8 ) & 0xff );
+       tag_len = ( ( setting->tag >> 0 ) & 0xff );
+
+       /* Locate USB device */
+       bus = find_usb_bus ( USB_BUS ( tag_busdev ) );
+       if ( ! bus )
+               return -ENODEV;
+       usb = find_usb ( bus, USB_DEV ( tag_busdev ) );
+       if ( ! usb )
+               return -ENODEV;
+       desc = ( ( const uint8_t * ) &usb->device );
+
+       /* Following the usage of SMBIOS settings tags, a <length> of
+        * zero indicates that the byte at <offset> contains a string
+        * index.  An <offset> of zero indicates that the <length>
+        * contains a literal string index.
+        *
+        * Since the byte at offset zero can never contain a string
+        * index, and a literal string index can never be zero, the
+        * combination of both <length> and <offset> being zero
+        * indicates that the entire structure is to be read.
+        *
+        * By default we reverse the byte direction since USB values
+        * are little-endian and iPXE settings are big-endian.  We
+        * invert this default when reading the entire structure.
+        */
+       if ( ( tag_len == 0 ) && ( tag_offset == 0 ) ) {
+               tag_len = sizeof ( usb->device );
+               tag_direction = -tag_direction;
+       } else if ( ( tag_len == 0 ) || ( tag_offset == 0 ) ) {
+               index = tag_len;
+               if ( ( ! index ) && ( tag_offset < sizeof ( usb->device ) ) )
+                       index = desc[tag_offset];
+               if ( ( rc = usb_get_string_descriptor ( usb, index, 0, data,
+                                                       len ) ) < 0 ) {
+                       return rc;
+               }
+               if ( ! setting->type )
+                       setting->type = &setting_type_string;
+               return rc;
+       }
+
+       /* Limit length */
+       if ( tag_offset > sizeof ( usb->device ) ) {
+               tag_len = 0;
+       } else if ( ( tag_offset + tag_len ) > sizeof ( usb->device ) ) {
+               tag_len = ( sizeof ( usb->device ) - tag_offset );
+       }
+
+       /* Copy data, reversing endianness if applicable */
+       dst = data;
+       src = ( desc + tag_offset );
+       if ( tag_direction < 0 )
+               src += ( tag_len - 1 );
+       if ( len > tag_len )
+               len = tag_len;
+       for ( ; len-- ; src += tag_direction, dst++ )
+               *dst = *src;
+
+       /* Set type to ":hexraw" if not already specified */
+       if ( ! setting->type )
+               setting->type = &setting_type_hexraw;
+
+       return tag_len;
+}
+
+/** USB device settings operations */
+static struct settings_operations usb_settings_operations = {
+       .applies = usb_settings_applies,
+       .fetch = usb_settings_fetch,
+};
+
+/** USB device settings */
+static struct settings usb_settings = {
+       .refcnt = NULL,
+       .siblings = LIST_HEAD_INIT ( usb_settings.siblings ),
+       .children = LIST_HEAD_INIT ( usb_settings.children ),
+       .op = &usb_settings_operations,
+       .default_scope = &usb_settings_scope,
+};
+
+/** Initialise USB device settings */
+static void usb_settings_init ( void ) {
+       int rc;
+
+       if ( ( rc = register_settings ( &usb_settings, NULL, "usb" ) ) != 0 ) {
+               DBG ( "USB could not register settings: %s\n",
+                     strerror ( rc ) );
+               return;
+       }
+}
+
+/** USB device settings initialiser */
+struct init_fn usb_settings_init_fn __init_fn ( INIT_NORMAL ) = {
+       .initialise = usb_settings_init,
+};
index ab1f9837017346a8a7664dd3d71244da4b0e6db5..7b3e92b9b2d740d5ce3a0cad470e5a43568ab6c9 100644 (file)
@@ -97,8 +97,9 @@ int ecm_fetch_mac ( struct usb_function *func,
        int rc;
 
        /* Fetch MAC address string */
+       buf[ sizeof ( buf ) - 1 ] = '\0';
        len = usb_get_string_descriptor ( usb, desc->mac, 0, buf,
-                                         sizeof ( buf ) );
+                                         ( sizeof ( buf ) - 1 ) );
        if ( len < 0 ) {
                rc = len;
                return rc;
index 01a2be6543706fc7e3d06c10c69f91ce98c6096d..6183e5b7e076394ff0d2b86cb0d5e1e294f22919 100644 (file)
@@ -424,6 +424,7 @@ FILE_LICENCE ( GPL2_OR_LATER_OR_UBDL );
 #define ERRFILE_widget_ui            ( ERRFILE_OTHER | 0x00620000 )
 #define ERRFILE_form_ui                      ( ERRFILE_OTHER | 0x00630000 )
 #define ERRFILE_usb_cmd                      ( ERRFILE_OTHER | 0x00640000 )
+#define ERRFILE_usb_settings         ( ERRFILE_OTHER | 0x00650000 )
 
 /** @} */