Gujin:面向 PC 的 GPL 引导加载程序

来源:OSDev Wiki· 阅读原文 ↗

本条目或章节的事实准确性存在争议 存在争议。
请参阅 讨论页。

警告: Gujin 似乎是弃件(abandonware),自 2013 年以来未再更新。

Gujin 是一个采用 GPL 许可的 引导加载程序,适用于 PC。

Gujin 能做什么?

它可以读取 FAT12、FAT16、FAT32、ext2、ext3、ext4(inode 大小固定)以及 ISO 9660 文件系统。

它具有带鼠标支持的图形用户界面,可安装在任何介质上:软盘、硬盘/USB 磁盘分区、硬盘/USB 磁盘 MBR、CD/DVD-ROM、DVD-RAM(即每扇区 2048 字节的 FAT)。它还可以使用串口代替屏幕和键盘作为输入/输出。

Gujin 可以链式加载其他引导加载程序、加载 Linux 内核,还有一个未完成的 Multiboot 规范加载器,但就我们当前关注的重点而言,更重要的是它可以加载标准 ELF 文件(更确切地说是 GZIP 压缩的 ELF 文件)。

PC 上主要有两种 ELF 变体:ELF32(加载地址、大小和入口点为 32 位)和 ELF64(字段为 64 位),Gujin 可以加载其中任意一种,并将处理器切换到保护模式后跳转到 ELF 入口点。

Gujin 完全不会尝试设置内存分页,选择何种分页方式是内核的工作——因此即使是 64 位的 ELF 文件也必须自行处理从 32 位到 64 位的转换(因为进入 64 位模式需要分页)。

中断方面同理,Gujin 切换到保护模式但不会重新启用中断,被引导的内核需要自行设置中断描述符表并处理各个中断,因为 BIOS 不会再为内核提供帮助。

如果 ELF 文件包含重定位信息,Gujin 还可以对其进行重定位,参见 --emit-relocs"Binutils" 工具链的 "ld" 链接器的选项。

由于大多数应用程序希望在切换到保护模式之前收集 BIOS 信息,Gujin 可以在实模式下调用 ELF 文件中的一个函数,如果该函数返回零——则继续加载过程——否则显示错误消息(例如:"trying to execute a 64 bits application on a processor without this feature!")。

此外,在大多数情况下,内核可以决定返回到 Gujin 引导加载程序(如果它没有将其从内存中擦除),而无需强制重启。

Gujin 引导加载程序使用标准 Linux 工具构建,运行时不需要 Linux(只需要一个不太有缺陷的 BIOS)——但我们必须假定你运行的是 Linux,才能使用 GNU 工具链:GCC、Binutils、GZIP... 来生成内核的 ELF 文件。 此外,Gujin 安装程序需要 Linux 32 位或 Linux 64 位才能运行并将 Gujin 引导加载程序安装到设备上。

由于 Windows 使用另一种可执行文件格式,你不能在 Windows 上安装 Cygwin/MinGW 并直接使用编译工具链,在这种情况下你必须生成一个交叉编译工具链来产生 ELF 文件,这超出了本说明的范围(但并非那么困难)。


本页面或章节使用以下人称指代读者或编辑者:I、我、我们 或 我们。应加以 编辑,改用百科全书式的语气。

文字说明够多了,让我们试试吧!

软盘已经过时,假设我们想用 U 盘作为测试介质。在某些情况下,我们可能想用 SD 卡,基本上流程是相同的。

我们首先需要确认目标 PC 在安装了 Gujin 之后能够从该 U 盘或 SD 卡引导。

为了提高成功率,我们将让 Gujin 安装程序完全重新格式化这个 USB 磁盘或 SD 卡,所以请先将你拥有的任何重要文件备份到其他地方。

然后,下载这个 gujin 可执行文件 (如果你使用的是 32 位 Linux),以及 gujin64 可执行文件 (如果你使用的是 64 位 Linux):

如果你不信任任何人,下载源文件 gujin-*.tar.gz,将其解压到一个目录中并输入 "make"——这将生成一个名为 "gujin" 的可执行文件(即使在 64 位 Linux 上也是如此)。

记得访问 http://gujin.org 以检查是否有更新版本的 Gujin,并增加 Gujin 作者的计数器,让他开心。

然后,我们将那个专用的 U 盘重新格式化为可引导的 FAT 文件系统,擦除其全部内容:首先通过输入 "su" 或 "sudo"(取决于发行版)进入 "root",然后获取你的 U 盘的设备名(假设它是 /dev/sdg)(SD 卡有时是 /dev/mmcblk0),并输入:

./gujin /dev/sdg --disk=BIOS:0x00,auto

根据 U 盘的大小,这将创建 FAT16 或 FAT32(甚至 FAT12)文件系统,但这一点并不太重要。

然后你拔下该设备并重新插入:大多数发行版会自动挂载该文件系统并显示其内容窗口:只有一个文件,即引导加载程序本身。

要检查该文件系统是否正确创建,你可以输入:

/sbin/fsck.vfat /dev/sdg

使用我们所用的 Gujin 安装程序参数,这将在我们的 U 盘上创建 "superfloppy" 格式,这是目前大多数 PC 能够理解并从中引导的格式。

这并不意味着你自己的 PC 一定会 100% 引导它,由于 BIOS 缺陷——所以你现在需要测试这个 U 盘是否能被你的 PC 引导:卸载 U 盘,将其插入测试 PC 并开机,看看 Gujin 是否启动(你会很容易注意到)。

如果没有启动,请尝试检查:

 - that the boot order in the BIOS is set to boot USB devices first
 - try the different USB devices if your BIOS has switchable items.
 - try to tell the Gujin installer to use the Extended BIOS instead by typing (--disk=EBIOS:0x00,auto is the default):
 ./gujin /dev/sdg
 - try to tell the Gujin installer to generate a real disk and not a superfloppy by:
 ./gujin --mbr /dev/sdg --disk=BIOS:0x00,auto
 - try the two previous options together:
 ./gujin --mbr /dev/sdg --disk=EBIOS:0x00,auto
 - try to use another (smaller) USB stick, some BIOS will only accept to boot from a FAT16 superfloppy

到现在你应该对自己的 BIOS 有了更多了解,并拥有一个可引导的 U 盘。


警告: 本节不使用 交叉编译器 ,很可能无法工作或在之后引发隐蔽的重大问题。

然后,我们想生成这个 ELF 内核——让我们尝试生成一个 "hello world": 创建一个包含以下内容的文件:

const char msg1[] = "Hello protected-mode text world! please reboot ..."; 
#define STACKSIZE 64 * 1024 
static unsigned stack[STACKSIZE / 4] __attribute__ ((aligned(32))); 

void _start (void) 
{ 
	/* We are flat non-paged memory and interrupt disabled */ 
	asm (" mov %0,%%esp " : : "i" (&stack[STACKSIZE / 4])); 
	volatile unsigned short *video_array = (volatile unsigned short *)0xB8000; 
	unsigned cpt1; 

	video_array += 10 * 80; /* few empty lines */ 
	/* We want blue background color and lightgray foreground, so 0x1700: */ 
	for (cpt1 = 0; cpt1 < sizeof(msg1) - 1; cpt1++) 
		video_array[cpt1] = 0x1700 + msg1[cpt1]; 

	while (1) 
		continue; 
}

然后像这样编译它(你可能还需要添加 "-fno-stack-protector",取决于发行版):

gcc -m32 -Wall -O2 -s -static -nostartfiles -nodefaultlibs -Wl,-Ttext=0x110000 hello.c -o hello.elf

并用以下方式压缩它:

gzip -9 hello.elf -c > hello.kgz

你只需将该 hello.kgz 文件复制到 U 盘中,然后用这个 U 盘重启,你将得到一个显示 "hello.kgz" 的菜单。

如果你点击这个文件名,你将看到 "Hello protected-mode text world! please reboot ..."(因为这个 hello-world 不管理图形模式,你需要在 Gujin 配置中 "force start kernel in text mode")。

如果你想要稍微复杂一点的 "hello world" 应用程序,你应该下载 Gujin 的 install*.tar.gz 包。

你会在那里找到几个 KGZ 文件,与前面的例子类似,但会显示一些地址以展示应用程序在内存中的确切位置。

你会看到 hello_32bits.kgz 和 hello_32bits_r.kgz 以展示重定位的效果,以及 hello_64bits.kgz 和 hello_64bits_r.kgz 以展示 64 位应用程序。

要获取源代码,只需下载文件 gujin*.tar.gz,并解压相应的 c 文件。

要查看用于编译它们的命令,只需在文件 Makefile 中搜索字符串 "hello_32bits.kgz:"(带冒号)。

如果你只想要一个实模式的 "hello world",你也可以用一个代码段加载地址为 null 的 ELF 文件来实现。

实模式 ELF 文件有 64 Kbytes 的限制。

只需创建一个包含以下内容的文件 包含:

/* hello_bios.c - public domain - no support whatsoever */

/* Constant entry block, max 64 KB code+data+stack, do not touch: */
asm (
"	.code16gcc				\n"
"	.section .init,\"ax\",@progbits		\n"
"	.global _start				\n"
"_start:					\n"
//  "int $3 # if debugging with borland td.exe, started by tiny.exe	\n"
"	pushw	%ds				\n"
"	pushw	%es				\n"
"	pushw	%fs				\n"
"	pushw	%gs				\n"
"	pushfl					\n"
"	pushal					\n"
"	movw	%ss,%cs:1f+1			\n"
"	movw	%sp,%cs:2f+2			\n"
"	movw	%cs,%ax				\n"
"	movw	%ax,%ds				\n"
"	movw	%ax,%es				\n"
"	movw	%ax,%fs				\n"
"	movw	%ax,%gs				\n"
"	movw	%ax,%ss				\n"
"	movl	$0,%esp				\n"
"	cld					\n"
"	calll	main				\n"
"	movl	%eax,%cs:3f+2			\n"
"1:	movw	$0,%ax				\n"
"	movw	%ax,%ss				\n"
"2:	movl	$0,%esp				\n"
"	popal					\n"
"	popfl					\n"
"	popw	%gs				\n"
"	popw	%fs				\n"
"	popw	%es				\n"
"	popw	%ds				\n"
"3:	movl	$0,%eax				\n"
"	lretw					\n"
"	.previous				\n"
);

/* For information about asm() construct, see GCC manual:
[http://gcc.gnu.org/onlinedocs/gcc-4.5.0/gcc/Extended-Asm.html http://gcc.gnu.org/onlinedocs/gcc-4.5.0/gcc/Extended-Asm.html]
For information about BIOS services, see
[http://stanislavs.org/helppc/ http://stanislavs.org/helppc/]
*/
enum standard_color {
black,		blue,		green,		cyan,
red,		magenta,	brown,		lightgray,
darkgray,	lightblue,	lightgreen,	lightcyan,
lightred,	lightmagenta,	yellow,		white
};

/* This assumes 16 colors modes, text or graphic - i.e. not 256 colors: */
static inline void
VGA_writestring (const char *string, unsigned char row, unsigned char col)
{
unsigned short page_attribute = 16 * blue + lightgray, strlen_string;
const char *endstring = string;

/* We cannot rely on libc to provide strlen(): */
while (*endstring)
	endstring++;
strlen_string = endstring - string;

asm (
"	xchgl	%%ebp,%4		\n"
"	int	$0x10 # writestring	\n"
"	xchgl	%%ebp,%4		\n"
	: : "a" (0x1301), "b" (page_attribute),
	"c" (strlen_string), "d" (((unsigned short)row << 8) | col),
	"r" (string)	/* in fact %es:%bp, but "B" (string) doesn't work */
	);
}

static inline void BIOS_wait (unsigned nb_microsecond)
{
unsigned short status;

/* Use volatile when there is at least one result, but even if the result
	is not used, the code has to be inserted (status is the result): */
asm volatile ("	int	$0x15	"
	: "=a" (status)
	:  "a" (0x8600), "d" (nb_microsecond & 0xFFFF), "c" (nb_microsecond >> 16)
	);
}

int main (void)
{
VGA_writestring ("Hello BIOS world! Please wait...\r\n", 0, 0);
BIOS_wait (3 * 1000 * 1000); /* 3 seconds before returning to Gujin*/
//	return 0x80000000; /* to return to Gujin menu */
return 0xA0000000; /* to return to Gujin menu, do not display "press a key to continue" */
}

然后用以下方式编译它:

gcc -m32 -Wall -Os -s -static -nostartfiles -nodefaultlibs -Wa,-R -Wl,--section-start,.init=0 hello_bios.c -o hello_bios.elf

并压缩它:

gzip -9 hello_bios.elf -c > hello_bios.kgz

你可以将这个 hello_bios.kgz 复制到 U 盘并运行它,我希望源文件中各个函数的功能是显而易见的。

Gujin 引导加载程序愿意让实模式和保护模式内核访问引导加载程序的内部数据(比如当前视频模式是什么、如何显示字符串……),只要内核采用 GPL 许可证——即当 GZIP 内核包含描述其许可证的 GZIP 注释时。这一点由 cleandisk.kgz 加以说明,它由这个 cleandisk.c 源代码编译而成,并与这个 链接器文件 链接。

关于 GPL 许可证的主要一点是,无法在 Gujin 可能的缺陷和闭源应用程序之间划清界限:不允许在实模式下修改 Gujin 的数据——但没有任何硬件强制执行。

显然,Gujin 将使你能够运行同时包含实模式部分和保护模式部分的 ELF 文件,只需看看示例 hello_gpl.kgz 及其源代码 hello_gpl.c。

另请参阅

文章

线程

外部链接

  • Gujin 在 sourceforge.net

来源与翻译说明

本文根据原文快照翻译,保留原有代码、数值、技术限定及历史背景。原文中的命令与运行结果不代表本站已实际执行验证。

原文:Gujin;离线修订号:30357。

原作者:OSDev Wiki 社区贡献者。原文及引用资料的版权与许可声明继续适用:版权声明 许可说明

评论

暂无评论