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 <getopt.h>
26 #include <ipxe/pci.h>
27 #include <ipxe/command.h>
28 #include <ipxe/parseopt.h>
29 
30 FILE_LICENCE ( GPL2_OR_LATER_OR_UBDL );
31 
32 /** @file
33  *
34  * PCI commands
35  *
36  */
37 
38 /** "pciscan" options */
39 struct pciscan_options {};
40 
41 /** "pciscan" option list */
42 static struct option_descriptor pciscan_opts[] = {};
43 
44 /** "pciscan" command descriptor */
47  "<setting>" );
48 
49 /**
50  * "pciscan" command
51  *
52  * @v argc Argument count
53  * @v argv Argument list
54  * @ret rc Return status code
55  */
56 static int pciscan_exec ( int argc, char **argv ) {
57  struct pciscan_options opts;
58  struct named_setting setting;
59  struct pci_device pci;
60  unsigned long prev;
61  int next;
62  int len;
63  int rc;
64 
65  /* Parse options */
66  if ( ( rc = parse_options ( argc, argv, &pciscan_cmd, &opts ) ) != 0 )
67  goto err_parse_options;
68 
69  /* Parse setting name */
70  if ( ( rc = parse_autovivified_setting ( argv[optind],
71  &setting ) ) != 0 )
72  goto err_parse_setting;
73 
74  /* Determine starting bus:dev.fn address */
75  if ( ( len = fetchn_setting ( setting.settings, &setting.setting,
76  NULL, &setting.setting, &prev ) ) < 0 ) {
77  /* Setting not yet defined: start searching from 00:00.0 */
78  prev = 0;
79  } else {
80  /* Setting is defined: start searching from next location */
81  prev++;
82  }
83 
84  /* Find next existent PCI device */
85  if ( ( next = pci_find_next ( &pci, prev ) ) < 0 ) {
86  rc = next;
87  goto err_find_next;
88  }
89 
90  /* Apply default type if necessary. Use ":uint16" rather than
91  * ":busdevfn" to allow for easy inclusion within a
92  * "${pci/${location}.x.y}" constructed setting.
93  */
94  if ( ! setting.setting.type )
95  setting.setting.type = &setting_type_uint16;
96 
97  /* Store setting */
98  if ( ( rc = storen_setting ( setting.settings, &setting.setting,
99  next ) ) != 0 ) {
100  printf ( "Could not store \"%s\": %s\n",
101  setting.setting.name, strerror ( rc ) );
102  goto err_store;
103  }
104 
105  err_store:
106  err_find_next:
107  err_parse_setting:
108  err_parse_options:
109  return rc;
110 }
111 
112 /** PCI commands */
113 struct command pci_commands[] __command = {
114  {
115  .name = "pciscan",
116  .exec = pciscan_exec,
117  },
118 };
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:121
int optind
Current option index.
Definition: getopt.c:51
uint32_t next
Next descriptor address.
Definition: myson.h:18
A command-line command.
Definition: command.h:9
int storen_setting(struct settings *settings, const struct setting *setting, unsigned long value)
Store numeric value of setting.
Definition: settings.c:1418
int parse_autovivified_setting(char *text, struct named_setting *setting)
Parse and autovivify setting name.
Definition: parseopt.c:299
int parse_options(int argc, char **argv, struct command_descriptor *cmd, void *opts)
Parse command-line options.
Definition: parseopt.c:447
A command descriptor.
Definition: parseopt.h:76
"pciscan" options
Definition: pci_cmd.c:39
const char * name
Name.
Definition: settings.h:28
Parse command-line options.
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:1376
static union @423 opts
"cert<xxx>" option list
char * strerror(int errno)
Retrieve string representation of error number.
Definition: strerror.c:78
struct command pci_commands [] __command
PCI commands.
Definition: pci_cmd.c:113
PCI bus.
A PCI device.
Definition: pci.h:187
Command line option parsing.
int pci_find_next(struct pci_device *pci, unsigned int busdevfn)
Find next device on PCI bus.
Definition: pci.c:229
A setting.
Definition: settings.h:23
const char * name
Name of the command.
Definition: command.h:11
uint32_t len
Length.
Definition: ena.h:14
static int pciscan_exec(int argc, char **argv)
"pciscan" command
Definition: pci_cmd.c:56
A command-line option descriptor.
Definition: parseopt.h:22
#define COMMAND_DESC(_struct, _options, _min_args, _max_args, _usage)
Construct command descriptor.
Definition: parseopt.h:107
#define NULL
NULL pointer (VOID *)
Definition: Base.h:362
static struct command_descriptor pciscan_cmd
"pciscan" command descriptor
Definition: pci_cmd.c:45
static struct option_descriptor pciscan_opts[]
"pciscan" option list
Definition: pci_cmd.c:42