Received: from list by lists.gnu.org with archive (Exim 4.90_1) id 1n21LG-0001Zo-Kp for mharc-grub-devel@gnu.org; Mon, 27 Dec 2021 20:31:54 -0500 Received: from eggs.gnu.org ([209.51.188.92]:52490) by lists.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.90_1) (envelope-from ) id 1n21LE-0001ZQ-3c for grub-devel@gnu.org; Mon, 27 Dec 2021 20:31:52 -0500 Received: from smtpbgau2.qq.com ([54.206.34.216]:47502) by eggs.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.90_1) (envelope-from ) id 1n21L5-0007Xd-Td for grub-devel@gnu.org; Mon, 27 Dec 2021 20:31:49 -0500 X-QQ-mid: bizesmtp49t1640655080t6q0rp0e Received: from localhost.localdomain (unknown [113.57.152.160]) by esmtp6.qq.com (ESMTP) with id ; Tue, 28 Dec 2021 09:31:06 +0800 (CST) X-QQ-SSF: 01400000000000B0C000B00A0000000 X-QQ-FEAT: jfdGVjI73+RVhML8WDkoThfg9LmblmURR85EqqSNGPxJpyt2PofMlikv/k3Rv triwvC/1saNiP/e8QHldwUbbwjEMhjkDbQKsUQqHfUfsJnE7Q/yX+X2E2dQbkB9BlPuBoFe uL88cxRLgBxAK6t38FO/f77ZG4Y/u5/RUXZYnX8aVj+f5PgR8eeqLiuXLPjyEpkiY4F3nxU OpkfKL0QfvZgKaSG2KQy7g1ph01v/jwV9ud4X7Uhhc/YAX4rTV6RhA8X2/xYxpe/hEuuX1W 310Mil2k2D0j+xYO5aWFSnMhLvEDvSKw2pLdV8JyQWP4LufFCIJo1Fd6C1V1rxWdJiP7F0a WjJB06GkBg8njVH+tsO3K3h4Qtf9A== X-QQ-GoodBg: 2 From: wangyouwan To: grub-devel@gnu.org Cc: dkiper@net-space.pl, ut000745 Subject: [PATCH] add cpumodelname mod to kernel 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, 28 Dec 2021 09:31:00 +0800 Message-Id: <20211228013100.14500-1-wangyouwan@uniontech.com> X-Mailer: git-send-email 2.20.1 MIME-Version: 1.0 Content-Transfer-Encoding: 8bit X-QQ-SENDSIZE: 520 Feedback-ID: bizesmtp:uniontech.com:qybgforeign:qybgforeign5 X-QQ-Bgrelay: 1 Received-SPF: pass client-ip=54.206.34.216; envelope-from=wangyouwan@uniontech.com; helo=smtpbgau2.qq.com X-Spam_score_int: -18 X-Spam_score: -1.9 X-Spam_bar: - X-Spam_report: (-1.9 / 5.0 requ) BAYES_00=-1.9, RCVD_IN_DNSWL_NONE=-0.0001, RCVD_IN_MSPIKE_H3=0.001, RCVD_IN_MSPIKE_WL=0.001, SPF_HELO_PASS=-0.001, SPF_PASS=-0.001 autolearn=ham autolearn_force=no X-Spam_action: no action X-BeenThere: grub-devel@gnu.org X-Mailman-Version: 2.1.29 Precedence: list List-Id: The development of GNU GRUB List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , X-List-Received-Date: Tue, 28 Dec 2021 01:31:52 -0000 From: ut000745 Signed-off-by: wangyouwan --- grub-core/Makefile.core.def | 1 + grub-core/kern/i386/coreboot/init.c | 5 ++ grub-core/kern/i386/cpumodelname.c | 73 +++++++++++++++++++++++++++++ grub-core/kern/i386/efi/init.c | 3 ++ grub-core/kern/i386/pc/init.c | 3 ++ include/grub/i386/cpumodelname.h | 24 ++++++++++ 6 files changed, 109 insertions(+) create mode 100644 grub-core/kern/i386/cpumodelname.c create mode 100644 include/grub/i386/cpumodelname.h diff --git a/grub-core/Makefile.core.def b/grub-core/Makefile.core.def index 8022e1c0a..2d46b945a 100644 --- a/grub-core/Makefile.core.def +++ b/grub-core/Makefile.core.def @@ -214,6 +214,7 @@ kernel = { x86 = kern/i386/tsc.c; x86 = kern/i386/tsc_pit.c; + x86 = kern/i386/cpumodelname.c; i386_efi = kern/i386/efi/tsc.c; x86_64_efi = kern/i386/efi/tsc.c; i386_efi = kern/i386/tsc_pmtimer.c; diff --git a/grub-core/kern/i386/coreboot/init.c b/grub-core/kern/i386/coreboot/init.c index 3314f027f..68675ac66 100644 --- a/grub-core/kern/i386/coreboot/init.c +++ b/grub-core/kern/i386/coreboot/init.c @@ -35,6 +35,7 @@ #include #include #include +#include extern grub_uint8_t _start[]; extern grub_uint8_t _end[]; @@ -109,6 +110,8 @@ grub_machine_init (void) grub_gfxterm_init (); grub_tsc_init (); + + grub_cpu_model_name_init (); } #else @@ -124,6 +127,8 @@ grub_machine_init (void) grub_machine_mmap_iterate (heap_init, NULL); grub_tsc_init (); + + grub_cpu_model_name_init (); } #endif diff --git a/grub-core/kern/i386/cpumodelname.c b/grub-core/kern/i386/cpumodelname.c new file mode 100644 index 000000000..49adbcf14 --- /dev/null +++ b/grub-core/kern/i386/cpumodelname.c @@ -0,0 +1,73 @@ +/* kern/i386/cpumodelname.c - x86 get cpu model name + * + * GRUB -- GRand Unified Bootloader + * Copyright (C) 2008 Free Software Foundation, Inc. + * + * GRUB is free software: you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation, either version 3 of the License, or + * (at your option) any later version. + * + * GRUB is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with GRUB. If not, see . + */ +#include +#include +#include +#include +#include + +GRUB_MOD_LICENSE ("GPLv3+"); + +static void +grub_get_model_name (void) +{ + char *ret = NULL; + grub_uint32_t x86_model_id[12]; + + char cpumodelname[6] = {0}; + const char *list[] = {"Intel","AMD","zhaoxin"}; + unsigned i; + + grub_cpuid (0x80000002, x86_model_id[0], x86_model_id[1], x86_model_id[2], x86_model_id[3]); + grub_cpuid (0x80000003, x86_model_id[4], x86_model_id[5], x86_model_id[6], x86_model_id[7]); + grub_cpuid (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"); + +} + +void +grub_cpu_model_name_init (void) +{ + unsigned int ext_level; + unsigned int eax, ebx, ecx, edx; + + if (!grub_cpu_is_cpuid_supported ()) + return; + + grub_cpuid (0x80000000, eax, ebx, ecx, edx); + ext_level = eax; + + if (ext_level < 0x80000004) + return; + + grub_get_model_name (); + +} diff --git a/grub-core/kern/i386/efi/init.c b/grub-core/kern/i386/efi/init.c index 46476e27e..099265140 100644 --- a/grub-core/kern/i386/efi/init.c +++ b/grub-core/kern/i386/efi/init.c @@ -27,12 +27,15 @@ #include #include #include +#include void grub_machine_init (void) { grub_efi_init (); grub_tsc_init (); + + grub_cpu_model_name_init (); } void diff --git a/grub-core/kern/i386/pc/init.c b/grub-core/kern/i386/pc/init.c index 27bc68b8a..b8251be8a 100644 --- a/grub-core/kern/i386/pc/init.c +++ b/grub-core/kern/i386/pc/init.c @@ -35,6 +35,7 @@ #include #include #include +#include struct mem_region { @@ -260,6 +261,8 @@ grub_machine_init (void) } grub_tsc_init (); + + grub_cpu_model_name_init (); } void diff --git a/include/grub/i386/cpumodelname.h b/include/grub/i386/cpumodelname.h new file mode 100644 index 000000000..838fe1603 --- /dev/null +++ b/include/grub/i386/cpumodelname.h @@ -0,0 +1,24 @@ +/* + * GRUB -- GRand Unified Bootloader + * Copyright (C) 2019 Free Software Foundation, Inc. + * + * GRUB is free software: you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation, either version 3 of the License, or + * (at your option) any later version. + * + * GRUB is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with GRUB. If not, see . + */ + +#ifndef GRUB_CPUMODENAME_H +#define GRUB_CPUMODENAME_H 1 + +void grub_cpu_model_name_init (void); + +#endif /* GRUB_CPUMODENAME_H */ -- 2.20.1