grub-devel
[Top][All Lists]
Advanced

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

[PATCH/RFC] cpuid.c: Provide CPU model information


From: David Michael
Subject: [PATCH/RFC] cpuid.c: Provide CPU model information
Date: Wed, 30 Jan 2013 15:36:29 -0500

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



reply via email to

[Prev in Thread] Current Thread [Next in Thread]