[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]
[PATCH] add cpumodelname mod for in grub.cfg to get cpu_mode_name, load
From: |
ut000745 |
Subject: |
[PATCH] add cpumodelname mod for in grub.cfg to get cpu_mode_name, load different versions of the kernel through the path composed of names. like this: linux /cpu_mode_name/vmlinuz-5.7.7-amd64-desktop |
Date: |
Mon, 13 Dec 2021 10:04:40 +0800 |
Signed-off-by: ut000745 <wangyouwan@uniontech.com>
---
grub-core/Makefile.core.def | 5 +++
grub-core/commands/i386/cpumodelname.c | 60 ++++++++++++++++++++++++++
2 files changed, 65 insertions(+)
create mode 100644 grub-core/commands/i386/cpumodelname.c
diff --git a/grub-core/Makefile.core.def b/grub-core/Makefile.core.def
index 8022e1c0a..063a59063 100644
--- a/grub-core/Makefile.core.def
+++ b/grub-core/Makefile.core.def
@@ -2527,3 +2527,8 @@ module = {
common = commands/i386/wrmsr.c;
enable = x86;
};
+module = {
+ name = cpu_model_name;
+ common = commands/i386/cpumodelname.c;
+ enable = x86;
+};
\ No newline at end of file
diff --git a/grub-core/commands/i386/cpumodelname.c
b/grub-core/commands/i386/cpumodelname.c
new file mode 100644
index 000000000..dc63be6d0
--- /dev/null
+++ b/grub-core/commands/i386/cpumodelname.c
@@ -0,0 +1,60 @@
+#include <grub/mm.h>
+#include <grub/dl.h>
+#include <grub/env.h>
+#include <grub/command.h>
+
+GRUB_MOD_LICENSE ("GPLv3+");
+
+static void
+grub_cpuid_model(unsigned int op, grub_uint32_t *a, grub_uint32_t *b,
grub_uint32_t *c, grub_uint32_t *d)
+{
+ __asm__ __volatile__ (".byte 0x53\n\tcpuid\n\t"
+ "movl %%ebx, %%esi\n\t.byte 0x5b"
+ : "=a" (*a),
+ "=S" (*b),
+ "=c" (*c),
+ "=d" (*d)
+ : "a" (op));
+}
+
+static grub_err_t grub_get_model_name(struct grub_command *cmd
__attribute__((unused)),
+ int argc __attribute__((unused)),
+ char *argv[] __attribute__((unused)))
+{
+ char *ret = NULL;
+ grub_uint32_t x86_model_id[12];
+
+ char cpumodelname[6] = {0};
+ const char *list[] = {"Intel","AMD","zhaoxin"};
+ unsigned i;
+
+ grub_cpuid_model(0x80000002, &x86_model_id[0], &x86_model_id[1],
&x86_model_id[2], &x86_model_id[3]);
+ grub_cpuid_model(0x80000003, &x86_model_id[4], &x86_model_id[5],
&x86_model_id[6], &x86_model_id[7]);
+ grub_cpuid_model(0x80000004, &x86_model_id[8], &x86_model_id[9],
&x86_model_id[10], &x86_model_id[11]);
+
+ for (i = 0; i < sizeof(list)/sizeof(list[0]); i++)
+ {
+ ret = grub_strstr ((char *)x86_model_id, list[i]);
+ if (ret != NULL)
+ {
+ grub_strncpy(cpumodelname, list[i], grub_strlen(list[i]) + 1);
+ break;
+ }
+ }
+
+ grub_env_set ("cpu_model_name", cpumodelname);
+ grub_env_export("cpu_model_name");
+
+ return 0;
+}
+
+static grub_command_t cmd_cpu_model_name;
+
+GRUB_MOD_INIT(cpu_model_name) {
+ cmd_cpu_model_name = grub_register_command("cpu_model_name",
grub_get_model_name,
+ 0, "cpu model name");
+}
+
+GRUB_MOD_FINI(cpu_model_name) {
+ grub_unregister_command(cmd_cpu_model_name);
+}
\ No newline at end of file
--
2.20.1
- [PATCH] add cpumodelname mod for in grub.cfg to get cpu_mode_name, load different versions of the kernel through the path composed of names. like this: linux /cpu_mode_name/vmlinuz-5.7.7-amd64-desktop,
ut000745 <=