iPXE
pci_cmd.c
Go to the documentation of this file.
00001 /*
00002  * Copyright (C) 2013 Michael Brown <mbrown@fensystems.co.uk>.
00003  *
00004  * This program is free software; you can redistribute it and/or
00005  * modify it under the terms of the GNU General Public License as
00006  * published by the Free Software Foundation; either version 2 of the
00007  * License, or any later version.
00008  *
00009  * This program is distributed in the hope that it will be useful, but
00010  * WITHOUT ANY WARRANTY; without even the implied warranty of
00011  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
00012  * General Public License for more details.
00013  *
00014  * You should have received a copy of the GNU General Public License
00015  * along with this program; if not, write to the Free Software
00016  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
00017  * 02110-1301, USA.
00018  *
00019  * You can also choose to distribute this program under the terms of
00020  * the Unmodified Binary Distribution Licence (as given in the file
00021  * COPYING.UBDL), provided that you have satisfied its requirements.
00022  */
00023 
00024 #include <stdio.h>
00025 #include <getopt.h>
00026 #include <ipxe/pci.h>
00027 #include <ipxe/command.h>
00028 #include <ipxe/parseopt.h>
00029 
00030 FILE_LICENCE ( GPL2_OR_LATER_OR_UBDL );
00031 
00032 /** @file
00033  *
00034  * PCI commands
00035  *
00036  */
00037 
00038 /** "pciscan" options */
00039 struct pciscan_options {};
00040 
00041 /** "pciscan" option list */
00042 static struct option_descriptor pciscan_opts[] = {};
00043 
00044 /** "pciscan" command descriptor */
00045 static struct command_descriptor pciscan_cmd =
00046         COMMAND_DESC ( struct pciscan_options, pciscan_opts, 1, 1,
00047                        "<setting>" );
00048 
00049 /**
00050  * "pciscan" command
00051  *
00052  * @v argc              Argument count
00053  * @v argv              Argument list
00054  * @ret rc              Return status code
00055  */
00056 static int pciscan_exec ( int argc, char **argv ) {
00057         struct pciscan_options opts;
00058         struct named_setting setting;
00059         struct pci_device pci;
00060         unsigned long prev;
00061         int next;
00062         int len;
00063         int rc;
00064 
00065         /* Parse options */
00066         if ( ( rc = parse_options ( argc, argv, &pciscan_cmd, &opts ) ) != 0 )
00067                 goto err_parse_options;
00068 
00069         /* Parse setting name */
00070         if ( ( rc = parse_autovivified_setting ( argv[optind],
00071                                                  &setting ) ) != 0 )
00072                 goto err_parse_setting;
00073 
00074         /* Determine starting bus:dev.fn address */
00075         if ( ( len = fetchn_setting ( setting.settings, &setting.setting,
00076                                       NULL, &setting.setting, &prev ) ) < 0 ) {
00077                 /* Setting not yet defined: start searching from 00:00.0 */
00078                 prev = 0;
00079         } else {
00080                 /* Setting is defined: start searching from next location */
00081                 prev++;
00082         }
00083 
00084         /* Find next existent PCI device */
00085         if ( ( next = pci_find_next ( &pci, prev ) ) < 0 ) {
00086                 rc = next;
00087                 goto err_find_next;
00088         }
00089 
00090         /* Apply default type if necessary.  Use ":uint16" rather than
00091          * ":busdevfn" to allow for easy inclusion within a
00092          * "${pci/${location}.x.y}" constructed setting.
00093          */
00094         if ( ! setting.setting.type )
00095                 setting.setting.type = &setting_type_uint16;
00096 
00097         /* Store setting */
00098         if ( ( rc = storen_setting ( setting.settings, &setting.setting,
00099                                      next ) ) != 0 ) {
00100                 printf ( "Could not store \"%s\": %s\n",
00101                          setting.setting.name, strerror ( rc ) );
00102                 goto err_store;
00103         }
00104 
00105  err_store:
00106  err_find_next:
00107  err_parse_setting:
00108  err_parse_options:
00109         return rc;
00110 }
00111 
00112 /** PCI commands */
00113 struct command pci_commands[] __command = {
00114         {
00115                 .name = "pciscan",
00116                 .exec = pciscan_exec,
00117         },
00118 };