iPXE
cpuid_cmd.c
Go to the documentation of this file.
1 /*
2  * Copyright (C) 2012 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 FILE_LICENCE ( GPL2_OR_LATER_OR_UBDL );
25 
26 #include <stdint.h>
27 #include <stdio.h>
28 #include <errno.h>
29 #include <getopt.h>
30 #include <ipxe/cpuid.h>
31 #include <ipxe/command.h>
32 #include <ipxe/parseopt.h>
33 
34 /** @file
35  *
36  * x86 CPU feature detection command
37  *
38  */
39 
40 /** "cpuid" options */
41 struct cpuid_options {
42  /** Check AMD-defined features (%eax=0x80000001) */
43  int amd;
44  /** Check features defined via %ecx */
45  int ecx;
46 };
47 
48 /** "cpuid" option list */
49 static struct option_descriptor cpuid_opts[] = {
50  OPTION_DESC ( "ext", 'e', no_argument,
51  struct cpuid_options, amd, parse_flag ),
52  /* "--amd" retained for backwards compatibility */
53  OPTION_DESC ( "amd", 'a', no_argument,
54  struct cpuid_options, amd, parse_flag ),
55  OPTION_DESC ( "ecx", 'c', no_argument,
56  struct cpuid_options, ecx, parse_flag ),
57 };
58 
59 /** "cpuid" command descriptor */
61  COMMAND_DESC ( struct cpuid_options, cpuid_opts, 1, 1, "<bit>" );
62 
63 /**
64  * The "cpuid" command
65  *
66  * @v argc Argument count
67  * @v argv Argument list
68  * @ret rc Return status code
69  */
70 static int cpuid_exec ( int argc, char **argv ) {
71  struct cpuid_options opts;
72  struct x86_features features;
73  struct x86_feature_registers *feature_regs;
74  uint32_t feature_reg;
75  unsigned int bit;
76  int rc;
77 
78  /* Parse options */
79  if ( ( rc = parse_options ( argc, argv, &cpuid_cmd, &opts ) ) != 0 )
80  return rc;
81 
82  /* Parse bit number */
83  if ( ( rc = parse_integer ( argv[optind], &bit ) ) != 0 )
84  return rc;
85 
86  /* Get CPU features */
88 
89  /* Extract relevant feature register */
90  feature_regs = ( opts.amd ? &features.amd : &features.intel );
91  feature_reg = ( opts.ecx ? feature_regs->ecx : feature_regs->edx );
92 
93  /* Check presence of specified feature */
94  return ( ( feature_reg & ( 1 << bit ) ) ? 0 : -ENOENT );
95 }
96 
97 /** x86 CPU feature detection command */
98 struct command cpuid_command __command = {
99  .name = "cpuid",
100  .exec = cpuid_exec,
101 };
int parse_integer(char *text, unsigned int *value)
Parse integer value.
Definition: parseopt.c:88
struct command cpuid_command __command
x86 CPU feature detection command
Definition: cpuid_cmd.c:98
struct arbelprm_rc_send_wqe rc
Definition: arbel.h:14
int ecx
Check features defined via ecx.
Definition: cpuid_cmd.c:45
int optind
Current option index.
Definition: getopt.c:51
FILE_LICENCE(GPL2_OR_LATER_OR_UBDL)
int amd
Check AMD-defined features (eax=0x80000001)
Definition: cpuid_cmd.c:43
static unsigned int unsigned int bit
Definition: bigint.h:196
Error codes.
A command-line command.
Definition: command.h:9
static union @424 opts
"cert<xxx>" option list
#define ENOENT
No such file or directory.
Definition: errno.h:514
void x86_features(struct x86_features *features)
Get x86 CPU features.
Definition: cpuid.c:163
int parse_options(int argc, char **argv, struct command_descriptor *cmd, void *opts)
Parse command-line options.
Definition: parseopt.c:447
x86 CPU features
Definition: cpuid.h:23
A command descriptor.
Definition: parseopt.h:76
static int cpuid_exec(int argc, char **argv)
The "cpuid" command.
Definition: cpuid_cmd.c:70
Parse command-line options.
int parse_flag(char *text __unused, int *flag)
Parse flag.
Definition: parseopt.c:200
uint32_t ecx
Features returned via ecx.
Definition: cpuid.h:17
static uint32_t uint32_t uint32_t uint32_t * ecx
Definition: cpuid.h:77
x86 CPU feature detection
uint32_t features
Supported features.
Definition: ena.h:16
Command line option parsing.
Option does not take an argument.
Definition: getopt.h:16
"cpuid" options
Definition: cpuid_cmd.c:41
unsigned int uint32_t
Definition: stdint.h:12
const char * name
Name of the command.
Definition: command.h:11
static struct option_descriptor cpuid_opts[]
"cpuid" option list
Definition: cpuid_cmd.c:49
#define OPTION_DESC(_longopt, _shortopt, _has_arg, _struct, _field, _parse)
Construct option descriptor.
Definition: parseopt.h:66
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
uint32_t edx
Features returned via edx.
Definition: cpuid.h:19
static struct command_descriptor cpuid_cmd
"cpuid" command descriptor
Definition: cpuid_cmd.c:60
An x86 CPU feature register set.
Definition: cpuid.h:15