You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

cpuid_cmd.c 2.7KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798
  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. FILE_LICENCE ( GPL2_OR_LATER );
  20. #include <stdint.h>
  21. #include <stdio.h>
  22. #include <errno.h>
  23. #include <getopt.h>
  24. #include <ipxe/cpuid.h>
  25. #include <ipxe/command.h>
  26. #include <ipxe/parseopt.h>
  27. /** @file
  28. *
  29. * x86 CPU feature detection command
  30. *
  31. */
  32. /** "cpuid" options */
  33. struct cpuid_options {
  34. /** Check AMD-defined features (%eax=0x80000001) */
  35. int amd;
  36. /** Check features defined via %ecx */
  37. int ecx;
  38. };
  39. /** "cpuid" option list */
  40. static struct option_descriptor cpuid_opts[] = {
  41. OPTION_DESC ( "ext", 'e', no_argument,
  42. struct cpuid_options, amd, parse_flag ),
  43. /* "--amd" retained for backwards compatibility */
  44. OPTION_DESC ( "amd", 'a', no_argument,
  45. struct cpuid_options, amd, parse_flag ),
  46. OPTION_DESC ( "ecx", 'c', no_argument,
  47. struct cpuid_options, ecx, parse_flag ),
  48. };
  49. /** "cpuid" command descriptor */
  50. static struct command_descriptor cpuid_cmd =
  51. COMMAND_DESC ( struct cpuid_options, cpuid_opts, 1, 1,
  52. "[--ext] [--ecx] <bit>" );
  53. /**
  54. * The "cpuid" command
  55. *
  56. * @v argc Argument count
  57. * @v argv Argument list
  58. * @ret rc Return status code
  59. */
  60. static int cpuid_exec ( int argc, char **argv ) {
  61. struct cpuid_options opts;
  62. struct x86_features features;
  63. struct x86_feature_registers *feature_regs;
  64. uint32_t feature_reg;
  65. unsigned int bit;
  66. int rc;
  67. /* Parse options */
  68. if ( ( rc = parse_options ( argc, argv, &cpuid_cmd, &opts ) ) != 0 )
  69. return rc;
  70. /* Parse bit number */
  71. if ( ( rc = parse_integer ( argv[optind], &bit ) ) != 0 )
  72. return rc;
  73. /* Get CPU features */
  74. x86_features ( &features );
  75. /* Extract relevant feature register */
  76. feature_regs = ( opts.amd ? &features.amd : &features.intel );
  77. feature_reg = ( opts.ecx ? feature_regs->ecx : feature_regs->edx );
  78. /* Check presence of specified feature */
  79. return ( ( feature_reg & ( 1 << bit ) ) ? 0 : -ENOENT );
  80. }
  81. /** x86 CPU feature detection command */
  82. struct command cpuid_command __command = {
  83. .name = "cpuid",
  84. .exec = cpuid_exec,
  85. };