iPXE
cpuid_cmd.c
Go to the documentation of this file.
00001 /*
00002  * Copyright (C) 2012 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 FILE_LICENCE ( GPL2_OR_LATER_OR_UBDL );
00025 
00026 #include <stdint.h>
00027 #include <stdio.h>
00028 #include <errno.h>
00029 #include <getopt.h>
00030 #include <ipxe/cpuid.h>
00031 #include <ipxe/command.h>
00032 #include <ipxe/parseopt.h>
00033 
00034 /** @file
00035  *
00036  * x86 CPU feature detection command
00037  *
00038  */
00039 
00040 /** "cpuid" options */
00041 struct cpuid_options {
00042         /** Check AMD-defined features (%eax=0x80000001) */
00043         int amd;
00044         /** Check features defined via %ecx */
00045         int ecx;
00046 };
00047 
00048 /** "cpuid" option list */
00049 static struct option_descriptor cpuid_opts[] = {
00050         OPTION_DESC ( "ext", 'e', no_argument,
00051                       struct cpuid_options, amd, parse_flag ),
00052         /* "--amd" retained for backwards compatibility */
00053         OPTION_DESC ( "amd", 'a', no_argument,
00054                       struct cpuid_options, amd, parse_flag ),
00055         OPTION_DESC ( "ecx", 'c', no_argument,
00056                       struct cpuid_options, ecx, parse_flag ),
00057 };
00058 
00059 /** "cpuid" command descriptor */
00060 static struct command_descriptor cpuid_cmd =
00061         COMMAND_DESC ( struct cpuid_options, cpuid_opts, 1, 1, "<bit>" );
00062 
00063 /**
00064  * The "cpuid" command
00065  *
00066  * @v argc              Argument count
00067  * @v argv              Argument list
00068  * @ret rc              Return status code
00069  */
00070 static int cpuid_exec ( int argc, char **argv ) {
00071         struct cpuid_options opts;
00072         struct x86_features features;
00073         struct x86_feature_registers *feature_regs;
00074         uint32_t feature_reg;
00075         unsigned int bit;
00076         int rc;
00077 
00078         /* Parse options */
00079         if ( ( rc = parse_options ( argc, argv, &cpuid_cmd, &opts ) ) != 0 )
00080                 return rc;
00081 
00082         /* Parse bit number */
00083         if ( ( rc = parse_integer ( argv[optind], &bit ) ) != 0 )
00084                 return rc;
00085 
00086         /* Get CPU features */
00087         x86_features ( &features );
00088 
00089         /* Extract relevant feature register */
00090         feature_regs = ( opts.amd ? &features.amd : &features.intel );
00091         feature_reg = ( opts.ecx ? feature_regs->ecx : feature_regs->edx );
00092 
00093         /* Check presence of specified feature */
00094         return ( ( feature_reg & ( 1 << bit ) ) ? 0 : -ENOENT );
00095 }
00096 
00097 /** x86 CPU feature detection command */
00098 struct command cpuid_command __command = {
00099         .name = "cpuid",
00100         .exec = cpuid_exec,
00101 };