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 */
60 static struct command_descriptor cpuid_cmd =
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  */
cpuid_exec(int argc,char ** argv)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 */
87 	x86_features ( &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 };
102