iPXE
usb_settings.c
Go to the documentation of this file.
1 /*
2  * Copyright (C) 2024 Michael Brown <mbrown@fensystems.co.uk>.
3  *
4  * This program is free software; you can redistribute it and/or
5  * modify it under the terms of the GNU General Public License as
6  * published by the Free Software Foundation; either version 2 of the
7  * License, or (at your option) any later version.
8  *
9  * This program is distributed in the hope that it will be useful, but
10  * WITHOUT ANY WARRANTY; without even the implied warranty of
11  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
12  * General Public License for more details.
13  *
14  * You should have received a copy of the GNU General Public License
15  * along with this program; if not, write to the Free Software
16  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
17  * 02110-1301, USA.
18  *
19  * You can also choose to distribute this program under the terms of
20  * the Unmodified Binary Distribution Licence (as given in the file
21  * COPYING.UBDL), provided that you have satisfied its requirements.
22  */
23 
24 FILE_LICENCE ( GPL2_OR_LATER_OR_UBDL );
25 
26 #include <stdio.h>
27 #include <errno.h>
28 #include <ipxe/usb.h>
29 #include <ipxe/settings.h>
30 #include <ipxe/init.h>
31 
32 /** @file
33  *
34  * USB device settings
35  *
36  */
37 
38 /** USB device settings scope */
39 static const struct settings_scope usb_settings_scope;
40 
41 /**
42  * Check applicability of USB device setting
43  *
44  * @v settings Settings block
45  * @v setting Setting
46  * @ret applies Setting applies within this settings block
47  */
49  const struct setting *setting ) {
50 
51  return ( setting->scope == &usb_settings_scope );
52 }
53 
54 /**
55  * Fetch value of USB device setting
56  *
57  * @v settings Settings block
58  * @v setting Setting to fetch
59  * @v data Buffer to fill with setting data
60  * @v len Length of buffer
61  * @ret len Length of setting data, or negative error
62  */
64  struct setting *setting,
65  void *data, size_t len ) {
66  uint8_t *dst = data;
67  const uint8_t *src;
68  const uint8_t *desc;
69  struct usb_bus *bus;
70  struct usb_device *usb;
71  int tag_direction;
72  unsigned int tag_busdev;
73  unsigned int tag_offset;
74  unsigned int tag_len;
75  unsigned int index;
76  int rc;
77 
78  /* Extract parameters from tag */
79  tag_direction = ( ( setting->tag & ( 1 << 31 ) ) ? +1 : -1 );
80  tag_busdev = ( ( setting->tag >> 16 ) & 0x7fff );
81  tag_offset = ( ( setting->tag >> 8 ) & 0xff );
82  tag_len = ( ( setting->tag >> 0 ) & 0xff );
83 
84  /* Locate USB device */
85  bus = find_usb_bus ( USB_BUS ( tag_busdev ) );
86  if ( ! bus )
87  return -ENODEV;
88  usb = find_usb ( bus, USB_DEV ( tag_busdev ) );
89  if ( ! usb )
90  return -ENODEV;
91  desc = ( ( const uint8_t * ) &usb->device );
92 
93  /* Following the usage of SMBIOS settings tags, a <length> of
94  * zero indicates that the byte at <offset> contains a string
95  * index. An <offset> of zero indicates that the <length>
96  * contains a literal string index.
97  *
98  * Since the byte at offset zero can never contain a string
99  * index, and a literal string index can never be zero, the
100  * combination of both <length> and <offset> being zero
101  * indicates that the entire structure is to be read.
102  *
103  * By default we reverse the byte direction since USB values
104  * are little-endian and iPXE settings are big-endian. We
105  * invert this default when reading the entire structure.
106  */
107  if ( ( tag_len == 0 ) && ( tag_offset == 0 ) ) {
108  tag_len = sizeof ( usb->device );
109  tag_direction = -tag_direction;
110  } else if ( ( tag_len == 0 ) || ( tag_offset == 0 ) ) {
111  index = tag_len;
112  if ( ( ! index ) && ( tag_offset < sizeof ( usb->device ) ) )
113  index = desc[tag_offset];
114  if ( ( rc = usb_get_string_descriptor ( usb, index, 0, data,
115  len ) ) < 0 ) {
116  return rc;
117  }
118  if ( ! setting->type )
119  setting->type = &setting_type_string;
120  return rc;
121  }
122 
123  /* Limit length */
124  if ( tag_offset > sizeof ( usb->device ) ) {
125  tag_len = 0;
126  } else if ( ( tag_offset + tag_len ) > sizeof ( usb->device ) ) {
127  tag_len = ( sizeof ( usb->device ) - tag_offset );
128  }
129 
130  /* Copy data, reversing endianness if applicable */
131  dst = data;
132  src = ( desc + tag_offset );
133  if ( tag_direction < 0 )
134  src += ( tag_len - 1 );
135  if ( len > tag_len )
136  len = tag_len;
137  for ( ; len-- ; src += tag_direction, dst++ )
138  *dst = *src;
139 
140  /* Set type to ":hexraw" if not already specified */
141  if ( ! setting->type )
142  setting->type = &setting_type_hexraw;
143 
144  return tag_len;
145 }
146 
147 /** USB device settings operations */
150  .fetch = usb_settings_fetch,
151 };
152 
153 /** USB device settings */
154 static struct settings usb_settings = {
155  .refcnt = NULL,
156  .siblings = LIST_HEAD_INIT ( usb_settings.siblings ),
157  .children = LIST_HEAD_INIT ( usb_settings.children ),
159  .default_scope = &usb_settings_scope,
160 };
161 
162 /** Initialise USB device settings */
163 static void usb_settings_init ( void ) {
164  int rc;
165 
166  if ( ( rc = register_settings ( &usb_settings, NULL, "usb" ) ) != 0 ) {
167  DBG ( "USB could not register settings: %s\n",
168  strerror ( rc ) );
169  return;
170  }
171 }
172 
173 /** USB device settings initialiser */
174 struct init_fn usb_settings_init_fn __init_fn ( INIT_NORMAL ) = {
176 };
#define USB_BUS(busdev)
Extract USB bus address.
Definition: usb.h:66
static const void * src
Definition: string.h:47
struct arbelprm_rc_send_wqe rc
Definition: arbel.h:14
void(* initialise)(void)
Definition: init.h:15
struct usb_bus * find_usb_bus(unsigned int address)
Find USB bus by address.
Definition: usb.c:2219
Error codes.
long index
Definition: bigint.h:61
uint64_t desc
Microcode descriptor list physical address.
Definition: ucode.h:12
struct usb_device * find_usb(struct usb_bus *bus, unsigned int address)
Find USB device by address.
Definition: usb.c:1734
FILE_LICENCE(GPL2_OR_LATER_OR_UBDL)
uint64_t tag
Setting tag, if applicable.
Definition: settings.h:43
#define INIT_NORMAL
Normal initialisation.
Definition: init.h:30
#define __unused
Declare a variable or data structure as unused.
Definition: compiler.h:573
An initialisation function.
Definition: init.h:14
struct init_fn usb_settings_init_fn __init_fn(INIT_NORMAL)
USB device settings initialiser.
const struct setting_type * type
Setting type.
Definition: settings.h:36
A USB device.
Definition: usb.h:722
Configuration settings.
char * strerror(int errno)
Retrieve string representation of error number.
Definition: strerror.c:78
#define ENODEV
No such device.
Definition: errno.h:509
Settings block operations.
Definition: settings.h:85
A settings block.
Definition: settings.h:132
unsigned char uint8_t
Definition: stdint.h:10
A setting scope.
Definition: settings.h:176
struct usb_device_descriptor device
Device descriptor.
Definition: usb.h:734
struct list_head siblings
Sibling settings blocks.
Definition: settings.h:140
A setting.
Definition: settings.h:23
static void usb_settings_init(void)
Initialise USB device settings.
Definition: usb_settings.c:163
Universal Serial Bus (USB)
static int usb_settings_applies(struct settings *settings __unused, const struct setting *setting)
Check applicability of USB device setting.
Definition: usb_settings.c:48
static int usb_settings_fetch(struct settings *settings __unused, struct setting *setting, void *data, size_t len)
Fetch value of USB device setting.
Definition: usb_settings.c:63
uint8_t data[48]
Additional event data.
Definition: ena.h:22
static struct settings_operations usb_settings_operations
USB device settings operations.
Definition: usb_settings.c:148
int(* applies)(struct settings *settings, const struct setting *setting)
Check applicability of setting.
Definition: settings.h:98
struct list_head children
Child settings blocks.
Definition: settings.h:142
int register_settings(struct settings *settings, struct settings *parent, const char *name)
Register settings block.
Definition: settings.c:475
#define DBG(...)
Print a debugging message.
Definition: compiler.h:498
#define LIST_HEAD_INIT(list)
Initialise a static list head.
Definition: list.h:30
const struct settings_scope * scope
Setting scope (or NULL)
Definition: settings.h:49
static const struct settings_scope usb_settings_scope
USB device settings scope.
Definition: usb_settings.c:39
uint32_t len
Length.
Definition: ena.h:14
#define NULL
NULL pointer (VOID *)
Definition: Base.h:321
A USB bus.
Definition: usb.h:965
static struct settings usb_settings
USB device settings.
Definition: usb_settings.c:154
uint8_t bus
Bus.
Definition: edd.h:14
struct refcnt * refcnt
Reference counter.
Definition: settings.h:134
int usb_get_string_descriptor(struct usb_device *usb, unsigned int index, unsigned int language, char *buf, size_t len)
Get USB string descriptor.
Definition: usb.c:915
#define USB_DEV(busdev)
Extract USB device address.
Definition: usb.h:69