第一个mod的诞生
grub-emu
这个东西可以直接模拟grub开机时的状态,只不过用的是你主机/boot/grub/目录下的grub.cfg文件,所以先把你本机的配置文件配好。
可以通过
./configure --with-platform=emu
命令来生成配置文件之后make生成。
创建c源码
先去到grub-core目录下,在你想创建的地方创建一个c语言文件。这里以在grub-core目录下创建demo/colortest.c为例子。当你添加好文件后,在grub-core下找到Makefile.core.def文件,在最后添加mod
格式为
module = {
name = colortest;
common = demo/colortest.c;
};
然后就没了。
/* hello.c - test module for dynamic loading */
/*
* GRUB -- GRand Unified Bootloader
* Copyright (C) 2003,2007 Free Software Foundation, Inc.
* Copyright (C) 2003 NIIBE Yutaka <gniibe@m17n.org>
*
* 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 <http://www.gnu.org/licenses/>.
*/
#include <grub/types.h>
#include <grub/misc.h>
#include <grub/mm.h>
#include <grub/dl.h>
#include <grub/err.h>
#include <grub/extcmd.h>
#include <grub/i18n.h>
#include "grub/term.h"
#include "grub/env.h"
#include "grub/misc.h"
GRUB_MOD_LICENSE ("GPLv3+");
static grub_err_t
grub_cmd_colortest(grub_extcmd_context_t ctxt __attribute__ ((unused)),
int argc __attribute__ ((unused)),
char **args __attribute__ ((unused)))
{
struct grub_arg_list * state = ctxt->state;
grub_err_t err = GRUB_ERR_NONE;
char color_normal[50];
const char *tmp;
grub_printf ("%s\n", _("colortest"));
tmp = grub_env_get("color_normal");
if(*tmp)
grub_strcpy(color_normal, tmp);
else
grub_strcpy(color_normal, "light-gray/black");
if(state[0].set)
grub_cls();
grub_setcolorstate(GRUB_TERM_COLOR_NORMAL);
/*把界面设置为蓝色*/
grub_env_set ("color_normal", "dark-gray/blue");
grub_printf (" dark-gray/blue ");
grub_env_set ("color_normal", "light-blue/blue");
grub_printf (" light-blue/blue ");
grub_env_set ("color_normal", "light-green/blue");
grub_printf (" light-green/blue ");
grub_env_set ("color_normal", "light-cyan/blue");
grub_printf (" light-cyan/blue \n");
grub_env_set ("color_normal", "light-red/blue");
grub_printf (" light-red/blue ");
grub_env_set ("color_normal", "light-magenta/blue");
grub_printf (" light-magenta/blue ");
grub_env_set ("color_normal", "yellow/blue");
grub_printf (" yellow/blue ");
grub_env_set ("color_normal", "white/blue");
grub_printf (" white/blue \n");
grub_printf ("\n");
return err;
}
static grub_extcmd_t cmd;
GRUB_MOD_INIT(colortest)
{
cmd = grub_register_extcmd ("colortest", grub_cmd_colortest, 0, 0,
N_("Test supported color combinations."), 0);
}
GRUB_MOD_FINI(colortest)
{
grub_unregister_extcmd (cmd);
}
我是拿他自己的hello.c改的,参考了https://blog.fpmurphy.com/2010/06/grub2-modules.html但技术不够里面的编译方法没看懂,就去参考了一个已经集成到grub的项目https://github.com/jesusdf/grub-msr
编译
直接在grub主目录在执行一遍编译命令autotool会自己添加你的mod文件并编译
./autogen.sh
./configure --with-platform=emu
make
编译好之后将mod文件cp到你的架构目录下就可以了。
在grub中使用mod
在/boot/grub你使用的架构的名称目录下会有一个common.list文件,在其中加入你模块就可以一了通常模块名字和目录名字是统一的
colortest: colortest
补充
原本想看看使用grub能不能制作一个毕业设计。但了解之后不知道能干嘛,有谁能给个想法吗?