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.

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