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