grub-devel.gnu.org archive mirror
 help / color / mirror / Atom feed
* [PATCH/RFC] cpuid.c: Provide CPU model information
@ 2013-01-30 20:36 David Michael
  2013-04-03  9:19 ` Vladimir 'φ-coder/phcoder' Serbinenko
  0 siblings, 1 reply; 3+ messages in thread
From: David Michael @ 2013-01-30 20:36 UTC (permalink / raw)
  To: grub-devel

Hi,

I have some disks that can be booted both physically and virtually,
and it is helpful to have different options depending on the type of
platform being used.  It's fine having different menu entries to
select at boot time, but I wanted to automate this in the
configuration.

I didn't find a solution through searching, so I modified a module to
supply sufficient information.  The CPU model name was unique enough
for my purpose of detecting whether it is a virtual machine (and I can
imagine additional use cases when GRUB2 is on a portable drive).  The
following patch can be used with configuration like this:

    insmod cpuid
    if [ "$cpuid_model_name" = "QEMU Virtual CPU ...

Would something like this be useful to have upstream?  Alternatively,
if there is a better way to get at various platform identifiers with
config scripts, can someone point me in the right direction?

Thanks.

David

=== modified file 'grub-core/commands/i386/cpuid.c'
--- grub-core/commands/i386/cpuid.c    2012-03-03 12:09:14 +0000
+++ grub-core/commands/i386/cpuid.c    2013-01-30 19:08:48 +0000
@@ -68,6 +68,8 @@
   unsigned int eax, ebx, ecx, edx;
   unsigned int max_level;
   unsigned int ext_level;
+  unsigned int vendor[4] = { 0 };
+  unsigned int model[12];

   /* See if we can use cpuid.  */
   asm volatile ("pushfl; pushfl; popl %0; movl %0,%1; xorl %2,%0;"
@@ -78,7 +80,10 @@
     goto done;

   /* Check the highest input value for eax.  */
-  cpuid (0, eax, ebx, ecx, edx);
+  cpuid (0, eax, vendor[0], vendor[2], vendor[1]);
+  /* Save the CPU vendor ID string. */
+  grub_env_set ("cpuid_vendor_id", (const char *)vendor);
+  grub_env_export ("cpuid_vendor_id");
   /* We only look at the first four characters.  */
   max_level = eax;
   if (max_level == 0)
@@ -91,6 +96,16 @@

   cpuid (0x80000001, eax, ebx, ecx, edx);
   grub_cpuid_has_longmode = !!(edx & bit_LM);
+
+  /* Assemble the CPU model name. */
+  if (ext_level >= 0x80000004)
+    {
+      cpuid(0x80000002, model[0], model[1], model[ 2], model[ 3]);
+      cpuid(0x80000003, model[4], model[5], model[ 6], model[ 7]);
+      cpuid(0x80000004, model[8], model[9], model[10], model[11]);
+      grub_env_set ("cpuid_model_name", (const char *)model);
+      grub_env_export ("cpuid_model_name");
+    }
 done:
 #endif


^ permalink raw reply	[flat|nested] 3+ messages in thread

end of thread, other threads:[~2013-04-15 15:07 UTC | newest]

Thread overview: 3+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2013-01-30 20:36 [PATCH/RFC] cpuid.c: Provide CPU model information David Michael
2013-04-03  9:19 ` Vladimir 'φ-coder/phcoder' Serbinenko
2013-04-15 15:07   ` David Michael

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).