[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]
Re: [PATCH] add cpumodelname mod for in grub.cfg to get cpu_mode_name, l
From: |
Daniel Kiper |
Subject: |
Re: [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: |
Tue, 21 Dec 2021 00:12:46 +0100 |
User-agent: |
NeoMutt/20170113 (1.7.2) |
On Mon, Dec 13, 2021 at 10:04:40AM +0800, ut000745 wrote:
> 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));
> +}
You can find grub_cpuid() macro in the include/grub/i386/cpuid.h file.
Please use it.
> +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]);
Missing spaces before "(". Same below. You can use
grub-core/kern/efi/sb.c as a reference for GRUB coding
convention. Please take a look at [1] too.
> + 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;
> + }
> + }
In general I think this implementation is lacking at least some minimal
cpuid checks. Please take a look at the arch/x86/kernel/cpu/common.c
file in the Linux kernel. I think this is good reference point.
> + 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");
We do not need a command for this. The variable should be defined
unconditionally.
> +}
> +
> +GRUB_MOD_FINI(cpu_model_name) {
> + grub_unregister_command(cmd_cpu_model_name);
> +}
This whole cpu_model_name init code should go into the GRUB kernel.
Please take a look how grub_tsc_init() is defined and used. I think
you should do something similar.
Daniel
[1] https://www.gnu.org/software/grub/manual/grub-dev/grub-dev.html#Coding-style