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