Indentation fix, cleanup.
[AROS.git] / arch / all-pc / boot / grub2-aros / grub-core / commands / i386 / cpuid.c
blob42b98415451abc412f52dcfedd4f66bf3420ce16
1 /* cpuid.c - test for CPU features */
2 /*
3 * GRUB -- GRand Unified Bootloader
4 * Copyright (C) 2006, 2007, 2009 Free Software Foundation, Inc.
5 * Based on gcc/gcc/config/i386/driver-i386.c
7 * GRUB is free software: you can redistribute it and/or modify
8 * it under the terms of the GNU General Public License as published by
9 * the Free Software Foundation, either version 3 of the License, or
10 * (at your option) any later version.
12 * GRUB is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 * GNU General Public License for more details.
17 * You should have received a copy of the GNU General Public License
18 * along with GRUB. If not, see <http://www.gnu.org/licenses/>.
21 #include <grub/dl.h>
22 #include <grub/misc.h>
23 #include <grub/mm.h>
24 #include <grub/env.h>
25 #include <grub/command.h>
26 #include <grub/extcmd.h>
27 #include <grub/i386/cpuid.h>
28 #include <grub/i18n.h>
30 GRUB_MOD_LICENSE ("GPLv3+");
32 static const struct grub_arg_option options[] =
34 /* TRANSLATORS: "(default)" at the end means that this option is used if
35 no argument is specified. */
36 {"long-mode", 'l', 0, N_("Check if CPU supports 64-bit (long) mode (default)."), 0, 0},
37 {"pae", 'p', 0, N_("Check if CPU supports Physical Address Extension."), 0, 0},
38 {0, 0, 0, 0, 0, 0}
41 enum
43 MODE_LM = 0,
44 MODE_PAE = 1
47 enum
49 bit_PAE = (1 << 6),
51 enum
53 bit_LM = (1 << 29)
56 unsigned char grub_cpuid_has_longmode = 0, grub_cpuid_has_pae = 0;
58 static grub_err_t
59 grub_cmd_cpuid (grub_extcmd_context_t ctxt,
60 int argc __attribute__ ((unused)),
61 char **args __attribute__ ((unused)))
63 int val = 0;
64 if (ctxt->state[MODE_PAE].set)
65 val = grub_cpuid_has_pae;
66 else
67 val = grub_cpuid_has_longmode;
68 return val ? GRUB_ERR_NONE
69 /* TRANSLATORS: it's a standalone boolean value,
70 opposite of "true". */
71 : grub_error (GRUB_ERR_TEST_FAILURE, N_("false"));
74 static grub_extcmd_t cmd;
76 GRUB_MOD_INIT(cpuid)
78 #ifdef __x86_64__
79 /* grub-emu */
80 grub_cpuid_has_longmode = 1;
81 grub_cpuid_has_pae = 1;
82 #else
83 unsigned int eax, ebx, ecx, edx;
84 unsigned int max_level;
85 unsigned int ext_level;
87 /* See if we can use cpuid. */
88 asm volatile ("pushfl; pushfl; popl %0; movl %0,%1; xorl %2,%0;"
89 "pushl %0; popfl; pushfl; popl %0; popfl"
90 : "=&r" (eax), "=&r" (ebx)
91 : "i" (0x00200000));
92 if (((eax ^ ebx) & 0x00200000) == 0)
93 goto done;
95 /* Check the highest input value for eax. */
96 grub_cpuid (0, eax, ebx, ecx, edx);
97 /* We only look at the first four characters. */
98 max_level = eax;
99 if (max_level == 0)
100 goto done;
102 if (max_level >= 1)
104 grub_cpuid (1, eax, ebx, ecx, edx);
105 grub_cpuid_has_pae = !!(edx & bit_PAE);
108 grub_cpuid (0x80000000, eax, ebx, ecx, edx);
109 ext_level = eax;
110 if (ext_level < 0x80000000)
111 goto done;
113 grub_cpuid (0x80000001, eax, ebx, ecx, edx);
114 grub_cpuid_has_longmode = !!(edx & bit_LM);
115 done:
116 #endif
118 cmd = grub_register_extcmd ("cpuid", grub_cmd_cpuid, 0,
119 "[-l]", N_("Check for CPU features."), options);
122 GRUB_MOD_FINI(cpuid)
124 grub_unregister_extcmd (cmd);