iPXE
usb_cmd.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 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 #include <stdio.h>
25 #include <errno.h>
26 #include <getopt.h>
27 #include <ipxe/usb.h>
28 #include <ipxe/command.h>
29 #include <ipxe/parseopt.h>
30 
31 FILE_LICENCE ( GPL2_OR_LATER_OR_UBDL );
32 
33 /** @file
34  *
35  * USB commands
36  *
37  */
38 
39 /** "usbscan" options */
40 struct usbscan_options {};
41 
42 /** "usbscan" option list */
43 static struct option_descriptor usbscan_opts[] = {};
44 
45 /** "usbscan" command descriptor */
48  "<setting>" );
49 
50 /**
51  * "usbscan" command
52  *
53  * @v argc Argument count
54  * @v argv Argument list
55  * @ret rc Return status code
56  */
57 static int usbscan_exec ( int argc, char **argv ) {
58  struct usbscan_options opts;
59  struct named_setting setting;
60  struct usb_device *usb;
61  unsigned long prev;
62  uint16_t busdev;
63  int len;
64  int rc;
65 
66  /* Parse options */
67  if ( ( rc = parse_options ( argc, argv, &usbscan_cmd, &opts ) ) != 0 )
68  goto err_parse_options;
69 
70  /* Parse setting name */
71  if ( ( rc = parse_autovivified_setting ( argv[optind],
72  &setting ) ) != 0 )
73  goto err_parse_setting;
74 
75  /* Determine starting bus:dev.fn address */
76  if ( ( len = fetchn_setting ( setting.settings, &setting.setting,
77  NULL, &setting.setting, &prev ) ) < 0 ) {
78  /* Setting not yet defined: start searching from 00:00 */
79  busdev = 0;
80  } else {
81  /* Setting is defined: start searching from next location */
82  busdev = ( prev + 1 );
83  if ( ! busdev ) {
84  rc = -ENOENT;
85  goto err_end;
86  }
87  }
88 
89  /* Find next existent USB device */
90  if ( ( rc = usb_find_next ( &usb, &busdev ) ) != 0 )
91  goto err_find_next;
92 
93  /* Apply default type if necessary. Use ":uint16" rather than
94  * ":hex" to allow for easy inclusion within a
95  * "${usb/${location}....}" constructed setting.
96  */
97  if ( ! setting.setting.type )
98  setting.setting.type = &setting_type_uint16;
99 
100  /* Store setting */
101  if ( ( rc = storen_setting ( setting.settings, &setting.setting,
102  busdev ) ) != 0 ) {
103  printf ( "Could not store \"%s\": %s\n",
104  setting.setting.name, strerror ( rc ) );
105  goto err_store;
106  }
107 
108  err_store:
109  err_end:
110  err_find_next:
111  err_parse_setting:
112  err_parse_options:
113  return rc;
114 }
115 
116 /** USB commands */
117 struct command usb_commands[] __command = {
118  {
119  .name = "usbscan",
120  .exec = usbscan_exec,
121  },
122 };
struct arbelprm_rc_send_wqe rc
Definition: arbel.h:14
unsigned short uint16_t
Definition: stdint.h:11
int printf(const char *fmt,...)
Write a formatted string to the console.
Definition: vsprintf.c:464
A parsed named setting.
Definition: parseopt.h:122
int optind
Current option index.
Definition: getopt.c:51
"usbscan" options
Definition: usb_cmd.c:40
Error codes.
A command-line command.
Definition: command.h:9
static struct command_descriptor usbscan_cmd
"usbscan" command descriptor
Definition: usb_cmd.c:46
int storen_setting(struct settings *settings, const struct setting *setting, unsigned long value)
Store numeric value of setting.
Definition: settings.c:1414
int parse_autovivified_setting(char *text, struct named_setting *setting)
Parse and autovivify setting name.
Definition: parseopt.c:336
#define ENOENT
No such file or directory.
Definition: errno.h:514
int parse_options(int argc, char **argv, struct command_descriptor *cmd, void *opts)
Parse command-line options.
Definition: parseopt.c:484
A command descriptor.
Definition: parseopt.h:77
const char * name
Name.
Definition: settings.h:28
int usb_find_next(struct usb_device **usb, uint16_t *busdev)
Find next USB device.
Definition: usb.c:2301
Parse command-line options.
const struct setting_type * type
Setting type.
Definition: settings.h:36
A USB device.
Definition: usb.h:722
int fetchn_setting(struct settings *settings, const struct setting *setting, struct settings **origin, struct setting *fetched, unsigned long *value)
Fetch numeric value of setting.
Definition: settings.c:1372
char * strerror(int errno)
Retrieve string representation of error number.
Definition: strerror.c:78
Command line option parsing.
struct command usb_commands [] __command
USB commands.
Definition: usb_cmd.c:117
A setting.
Definition: settings.h:23
const char * name
Name of the command.
Definition: command.h:11
FILE_LICENCE(GPL2_OR_LATER_OR_UBDL)
static struct option_descriptor usbscan_opts[]
"usbscan" option list
Definition: usb_cmd.c:43
Universal Serial Bus (USB)
A command-line option descriptor.
Definition: parseopt.h:23
#define COMMAND_DESC(_struct, _options, _min_args, _max_args, _usage)
Construct command descriptor.
Definition: parseopt.h:108
static union @438 opts
"cert<xxx>" option list
uint32_t len
Length.
Definition: ena.h:14
#define NULL
NULL pointer (VOID *)
Definition: Base.h:321
static int usbscan_exec(int argc, char **argv)
"usbscan" command
Definition: usb_cmd.c:57