iPXE
pci_cmd.c
Go to the documentation of this file.
1 /*
2  * Copyright (C) 2013 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/pci.h>
29 #include <ipxe/command.h>
30 #include <ipxe/parseopt.h>
31 
32 FILE_LICENCE ( GPL2_OR_LATER_OR_UBDL );
33 
34 /** @file
35  *
36  * PCI commands
37  *
38  */
39 
40 /** "pciscan" options */
41 struct pciscan_options {};
42 
43 /** "pciscan" option list */
44 static struct option_descriptor pciscan_opts[] = {};
45 
46 /** "pciscan" command descriptor */
49  "<setting>" );
50 
51 /**
52  * "pciscan" command
53  *
54  * @v argc Argument count
55  * @v argv Argument list
56  * @ret rc Return status code
57  */
58 static int pciscan_exec ( int argc, char **argv ) {
59  struct pciscan_options opts;
60  struct named_setting setting;
61  struct pci_device pci;
62  unsigned long prev;
64  int len;
65  int rc;
66 
67  /* Parse options */
68  if ( ( rc = parse_options ( argc, argv, &pciscan_cmd, &opts ) ) != 0 )
69  goto err_parse_options;
70 
71  /* Parse setting name */
72  if ( ( rc = parse_autovivified_setting ( argv[optind],
73  &setting ) ) != 0 )
74  goto err_parse_setting;
75 
76  /* Determine starting bus:dev.fn address */
77  if ( ( len = fetchn_setting ( setting.settings, &setting.setting,
78  NULL, &setting.setting, &prev ) ) < 0 ) {
79  /* Setting not yet defined: start searching from 00:00.0 */
80  busdevfn = 0;
81  } else {
82  /* Setting is defined: start searching from next location */
83  busdevfn = ( prev + 1 );
84  if ( ! busdevfn ) {
85  rc = -ENOENT;
86  goto err_end;
87  }
88  }
89 
90  /* Find next existent PCI device */
91  if ( ( rc = pci_find_next ( &pci, &busdevfn ) ) != 0 )
92  goto err_find_next;
93 
94  /* Apply default type if necessary. Use ":uint16" rather than
95  * ":busdevfn" to allow for easy inclusion within a
96  * "${pci/${location}.x.y}" constructed setting.
97  */
98  if ( ! setting.setting.type )
99  setting.setting.type = &setting_type_uint16;
100 
101  /* Store setting */
102  if ( ( rc = storen_setting ( setting.settings, &setting.setting,
103  busdevfn ) ) != 0 ) {
104  printf ( "Could not store \"%s\": %s\n",
105  setting.setting.name, strerror ( rc ) );
106  goto err_store;
107  }
108 
109  err_store:
110  err_end:
111  err_find_next:
112  err_parse_setting:
113  err_parse_options:
114  return rc;
115 }
116 
117 /** PCI commands */
118 COMMAND ( pciscan, pciscan_exec );
struct arbelprm_rc_send_wqe rc
Definition: arbel.h:14
FILE_LICENCE(GPL2_OR_LATER_OR_UBDL)
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
Error codes.
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
"pciscan" options
Definition: pci_cmd.c:41
const char * name
Name.
Definition: settings.h:28
uint16_t busdevfn
PCI bus:dev.fn address.
Definition: ena.h:28
int pci_find_next(struct pci_device *pci, uint32_t *busdevfn)
Find next device on PCI bus.
Definition: pci.c:322
Parse command-line options.
COMMAND(pciscan, pciscan_exec)
PCI commands.
ring len
Length.
Definition: dwmac.h:231
const struct setting_type * type
Setting type.
Definition: settings.h:36
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
PCI bus.
A PCI device.
Definition: pci.h:210
Command line option parsing.
unsigned int uint32_t
Definition: stdint.h:12
A setting.
Definition: settings.h:23
static int pciscan_exec(int argc, char **argv)
"pciscan" command
Definition: pci_cmd.c:58
A command-line option descriptor.
Definition: parseopt.h:23
static union @447 opts
"cert<xxx>" option list
#define COMMAND_DESC(_struct, _options, _min_args, _max_args, _usage)
Construct command descriptor.
Definition: parseopt.h:108
#define NULL
NULL pointer (VOID *)
Definition: Base.h:321
String functions.
static struct command_descriptor pciscan_cmd
"pciscan" command descriptor
Definition: pci_cmd.c:47
static struct option_descriptor pciscan_opts[]
"pciscan" option list
Definition: pci_cmd.c:44