从0.4.5起,用户可以自行编写程序以在GRUB4DOS中运行。
该可执行程序文件必须以8字节grub4dos EXEC签名结尾。
0x05, 0x18, 0x05, 0x03, 0xBA, 0xA7, 0xBA, 0xBC
程序的入口点在文件头,和DOS的.com文件很像(但我们是32位的程序)。/*================ begin echo.c ================*/
/*
* 编译:
gcc -Wl,--build-id=none -m32 -mno-sse -nostdlib -fno-zero-initialized-in-bss -fno-function-cse -fno-jump-tables -Wl,-N -fPIE echo.c -o echo.o
* disassemble: objdump -d echo.o
* confirm no relocation: readelf -r echo.o
* generate executable: objcopy -O binary echo.o echo
* 经过这一步之后生成的echo文件就是可以在grub4dos中运行的程序。
* and then the resultant echo will be grub4dos executable.
*/
/*
* This is a simple ECHO command, running under grub4dos.
*/
#define sprintf ((int (*)(char *, const char *, ...))((*(int **)0x8300)[0]))
#define printf(...) sprintf(NULL, __VA_ARGS__)
int i = 0x66666666; /* 这是必要的,看下面的注释。*/
/* gcc treat the following as data only if a global initialization like the
* above line occurs.
*/
/* GRUB4DOS可执行程序结尾必须有以下8个字节(EXEC签名) */
asm(".long 0x03051805");
asm(".long 0xBCBAA7BA");
/* 感谢上帝, gcc 会把上面的8个字按兵不动放在最终程序的最后面。
* 不要在这里插入其它任何代码.
*/
int main(char *arg,int flags)
{
return printf("%s\n",arg);
}
/*================ end echo.c ================*/
0x8300 是 grub4dos 系统函数(API)的入口点. 你可以在 asm.S 源码中找到它的定义.