在Grub 2.02源代码中查找倒数计时器

问题描述 投票:5回答:2

将Grub的超时更改为1/10或1/100秒的间隔

在具有AMD64架构的UEFI系统上使用Grub 2.02。我想将grub的超时计数器从1秒间隔更改为1/10秒或1/100秒间隔。原因是使gfxmenu循环进度倒计时减少“波涛汹涌”。下面的引导GIF显示循环1秒“块”中的5秒倒计时:

Grub Boot

在成功更改源代码并重新编译之后,/etc/default/grub将更改如下:

  • 如果1/10秒间隔,2.5秒倒计时将是GRUB_TIMEOUT=25
  • 如果1/100秒间隔,2.5秒倒计时将是GRUB_TIMEOUT=250

Grub 2.02 Source是1/2万行

我已经下载了这里描述的源:how to build grub2 bootloader from it's source and test it with qemu emulator并花时间浏览源文件。但是有477k行搜索:

~/src/grub-2.02$ wc -l **/*

      20 asm-tests/arm.S
      18 asm-tests/i386-pc.S
       4 asm-tests/i386.S
      11 asm-tests/mips.S
       8 asm-tests/powerpc.S
            (... SNIP ...)
     115 util/spkmodem-recv.c
  477316 total

我在Ask Ubuntu中做了很多bash项目,但这将是我的第一个C / Assembler Linux项目。作为一个“新手”,我的想法是:

  • 哪个文件包含倒数计时器源代码?
  • 如何将间隔更改为1/10或1/100秒?
  • 将源代码放在我的主目录下常规方法?
  • 有关在Virtualbox中编译和测试的任何提示都会有所帮助。

请注意,只有第一个问题是相关的。其他问题是作者选择更详细的答案。

c assembly grub
2个回答
7
投票

变量GRUB_TIMEOUTutil/grub.d/00_header.in中进行评估。

if [ "x$GRUB_BUTTON_CMOS_ADDRESS" != "x" ]; then
    cat <<EOF
if cmostest $GRUB_BUTTON_CMOS_ADDRESS ; then
EOF
make_timeout "${GRUB_HIDDEN_TIMEOUT_BUTTON}" "${GRUB_TIMEOUT_BUTTON}" "${GRUB_TIMEOUT_STYLE_BUTTON}"
echo else
make_timeout "${GRUB_HIDDEN_TIMEOUT}" "${GRUB_TIMEOUT}" "${GRUB_TIMEOUT_STYLE}"
echo fi
else
make_timeout "${GRUB_HIDDEN_TIMEOUT}" "${GRUB_TIMEOUT}" "${GRUB_TIMEOUT_STYLE}"
fi

请注意,这是一个生成脚本的脚本,这就是为什么它看起来很奇怪。 make_timeout看起来像这样(同上):

make_timeout ()
{
    if [ "x${3}" != "x" ] ; then
        timeout="${2}"
        style="${3}"
    elif [ "x${1}" != "x" ] && [ "x${1}" != "x0" ] ; then
        # Handle the deprecated GRUB_HIDDEN_TIMEOUT scheme.
        timeout="${1}"
        if [ "x${2}" != "x0" ] ; then
            grub_warn "$(gettext "Setting GRUB_TIMEOUT to a non-zero value when GRUB_HIDDEN_TIMEOUT is set is no longer supported.")"
        fi
        if [ "x${GRUB_HIDDEN_TIMEOUT_QUIET}" = "xtrue" ] ; then
            style="hidden"
            verbose=
        else
            style="countdown"
            verbose=" --verbose"
        fi
    else
        # No hidden timeout, so treat as GRUB_TIMEOUT_STYLE=menu
        timeout="${2}"
        style="menu"
    fi
    cat << EOF
if [ x\$feature_timeout_style = xy ] ; then
  set timeout_style=${style}
  set timeout=${timeout}
EOF
    if [ "x${style}" = "xmenu" ] ; then
        cat << EOF
# Fallback normal timeout code in case the timeout_style feature is
# unavailable.
else
  set timeout=${timeout}
EOF
    else
        cat << EOF
# Fallback hidden-timeout code in case the timeout_style feature is
# unavailable.
elif sleep${verbose} --interruptible ${timeout} ; then
  set timeout=0
EOF
    fi
    cat << EOF
fi
EOF
}

正如你所看到的,它最后只是调用sleep的一些选项。此命令在grub-core/commands/sleep.c中定义。虽然sleep命令只能以整秒的增量睡眠,但潜在的函数grub_millisleep可以做得更好。

通过将所有grub_millisleep(1000)调用更改为grub_millisleep(100)来修补此函数应该很容易,但请记住,这会破坏sleep的所有用法。更清洁的选择是向sleep添加新选项,以便可以根据具体情况选择行为。


2
投票

感谢所接受答案的帮助,我能够使用不同的方法实现目标。在成功完成Grub 2.02源代码更改并重新编译之后,/etc/default/grubGRUB_TIMEOUT=35改为3.5秒倒计时。

注意循环进度现在是如何平滑的,没有“块”:

New Grub Boot


要改变的代码:

/grub-2.02/grub-core/normal/menu.c

546行:

/* Check whether a second has elapsed since the last tick.  If so, adjust
   the timer and return 1; otherwise, return 0.  */
static int
has_second_elapsed (grub_uint64_t *saved_time)
{
  grub_uint64_t current_time;

  current_time = grub_get_time_ms ();
  /* July 14, 2018 Use deciseconds - change 1000 to 100 */
  if (current_time - *saved_time >= 100)
    {
      *saved_time = current_time;
      return 1;
    }
  else
    return 0;
}

换行:

if (current_time - *saved_time >= 1000)

至:

if (current_time - *saved_time >= 100)

瞧!一行代码要改变。另外还添加了两条评论专线。

如何编译grub 2.02

在按照关于Grub的website的说明之前:

sudo apt install bison
sudo apt install flex

然后按照grub的网站说明操作:

cd grub-2.02
./configure

在Grub的网站上运行下一个命令:

make install

文件在/usr/local/bin(惊喜!!!)和.../grub-2.02目录中创建,这是预期的。


编译grub的杂项问题

我最终将源代码克隆到VM(Lubuntu 16.04)并在那里重新编译。使用新编译的grub-install搞砸了,我不得不使用sudo apt install grub2进行全新安装。然后手动将新编译的文件复制到/boot/grub/i386-pc

我的终端盒全部倾斜了所以我将不得不创建一个新的grub背景图像。在下图中,我改变了GRUB_TIMEOUT=35 3.5秒倒计时。


2018年7月16日更新

想出一个参数来使用以获得X86,EFI支持:

./configure –with-platform=efi

*******************************************************
GRUB2 will be compiled with following components:
Platform: x86_64-efi
With devmapper support: No (need libdevmapper header)
With memory debugging: No
With disk cache statistics: No
With boot time statistics: No
efiemu runtime: No (not available on efi)
grub-mkfont: No (need freetype2 library)
grub-mount: No (need FUSE library)
starfield theme: No (No build-time grub-mkfont)
With libzfs support: No (need zfs library)
Build-time grub-mkfont: No (need freetype2 library)
Without unifont (no build-time grub-mkfont)
Without liblzma (no support for XZ-compressed mips images) (need lzma library)
*******************************************************

但是,在make install之后出现错误:

Making install in grub-core
make[2]: Entering directory '/home/rick/src/grub-2.02/grub-core'
gcc -E -DHAVE_CONFIG_H  -Wall -W  -DGRUB_MACHINE_EFI=1 -DGRUB_MACHINE=X86_64_EFI -m64 -nostdinc -isystem /usr/lib/gcc/x86_64-linux-gnu/5/include -I../include -I../include -DGRUB_FILE=\"symlist.h\" -I. -I. -I.. -I.. -I../include -I../include -I../grub-core/lib/libgcrypt-grub/src/   -DGRUB_KERNEL=1 -D_FILE_OFFSET_BITS=64 -DGRUB_SYMBOL_GENERATOR=1 symlist.h > symlist.p || (rm -f symlist.p; exit 1)
symlist.h:25:44: fatal error: ../include/grub/machine/kernel.h: No such file or directory
compilation terminated.
Makefile:42544: recipe for target 'symlist.c' failed
make[2]: *** [symlist.c] Error 1
make[2]: Leaving directory '/home/rick/src/grub-2.02/grub-core'
Makefile:10904: recipe for target 'install-recursive' failed
make[1]: *** [install-recursive] Error 1
make[1]: Leaving directory '/home/rick/src/grub-2.02'
Makefile:11927: recipe for target 'install' failed
make: *** [install] Error 2

我向Grub人提交了一份错误报告(2018年7月),但没有回复过。 EFI系统的下一步是使用Ubuntu的存储库而不是Grub的网站指令在全新安装上下载源代码。


© www.soinside.com 2019 - 2024. All rights reserved.