Writing multiboot PE kernels using Visual C++

本文介绍如何利用Grub引导PE格式的操作系统内核,包括设置多启动规范头的具体步骤,以及如何针对Windows背景下的开发者进行代码适配。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

http://ksrenevasan.blogspot.com/2005/10/writing-multiboot-pe-kernels-using.html

Aspiring operating system developers who target x86 often don’t get beyondwriting a boot sector (seldom do they even complete it) because of theinordinate amount of time needed to understand the “tricks” required to get theprocessor into a “sane” mode of operation before the kernel can start executing.That’s why newbie kernel developers are always advised to use an alternativelike Grub to bootstrap theirkernel so that they can concentrate on implementing the kernel itself ratherthan the plumbing. Why Grub? Because it is one of the bootloaders thatimplements the “Multiboot”specification (correctly?).


This specification details the steps thatOS / bootloader developers need to follow in order to be compatible with (andusable by) each other. In very simple terms, multiboot compliant operatingsystems need to have a 48 byte structure called the Multiboot header (in itsentirety), somewhere within the first 8192 bytes of the kernel image, longwordaligned. on a 4K boundary. [Updated: 10/6/2005, 12:23 PM].Actual details about the fields are documented in the Multiboot specification here.

Everyother “roll your own OS” tutorial invariably talks about how you can make yourkernel bootable by Grub. But all of these assume that you are using the GCCtoolset. If you are from a windows background you are out of luck. The GCCtoolset itself is not very difficult to learn, but I’m sure you’d feel more athome using the tools you’re familiar with for a long time. At least I do andthat’s why I set out writing this post about how you can make grub boot yourVC++ kernel.

Making a boot loader like Grub boot a custom kernel is easy(at least compared to the effort it takes to create a new boot loader). Thekernel itself is only a binary in some file format (AOUT, ELF, PE etc.). Forexample the Windows kernel (%WINDRIVE%\Windows\System32\NTOSKRNL.EXE) uses thePE file format (Try dumpbin /ALL %WINDRIVE%\Windows\System32\NTOSKRNL.EXE) thatis also used by user mode programs under windows. Similarly, the Linux kernelprobably gets compiled into the ELF file format. Now, expecting a bootloader to“know” all executable file formats is probably not a good idea. The multibootspecification takes a different approach to load a kernel image onto RAM. Ituses fields in the multiboot header to denote the parts of the kernel image thatneeds to be loaded. Grub “knows” how to load an ELF binary, not a PE. So we aregoing to give it “hints” in our multiboot header that will help it load thekernel image properly. Time for some code…

/* kernel.h */
#ifndef__kernel_h__
#define__kernel_h__

#definedd(x)                            \
        __asm _emit (x)      & 0xff     \
        __asm _emit     (x)>> 8  & 0xff \
        __asm _emit     (x)>> 16 & 0xff \
        __asm _emit     (x)>> 24 & 0xff

#defineKERNEL_STACK               0x00103fff
#defineKERNEL_START               0x00101000
#defineKERNEL_LENGTH               0x0000200F

void main(unsigned long,unsigned long);

#endif

/* kernel.c */
#include"kernel.h"

__declspec(naked) void__multiboot_entry__(void)
{
          __asm{
          
          multiboot_header:
                    dd(0x1BADB002)               ;magic
                    dd(1<< 16)                    ; flags
                    dd(-(0x1BADB002+ (1 << 16)))     ; checksum
                    dd(0x00101000)               ;header_addr
                    dd(0x00101000)               ;load_addr
                    dd(0x0010200F)               ;load_end_addr
                    dd(0x0010200F)               ;bss_end_addr
                    dd(0x00101030)               ;entry_addr
                    dd(0x00000000)               ;mode_type
                    dd(0x00000000)               ;width
                    dd(0x00000000)               ;height
                    dd(0x00000000)               ;depth

          kernel_entry:
                    mov     esp,     KERNEL_STACK

                    xor     ecx,     ecx
                    push     ecx
                    popf

                    push     eax
                    push     ebx
                    call     main

                    jmp     $
          }
}

void main(unsigned long magic,unsigned long addr)
{
          char *string = "HelloWorld!", *ch;
          unsigned short*vidmem = (unsigned short *) 0xB8000;
          inti;

          for(ch = string, i =0; *ch; ch++, i++)
                    vidmem[i] =(unsigned char) *ch | 0x0700;
}

The first fieldin the header is a magic number that the bootloader will use to locate the startof the multiboot header in the image. The second field denotes the features thatthe OS expects from the boot loader. To keep the code simple, I’ve ignored bits0-15 (about which you can read in the multiboot specification). I’ve set bit 16of this field. This means that the fields at offsets 8-24 in the Multibootheader are valid, and the boot loader should use them instead of the fields inthe actual executable header to calculate where to load the OS image. Thismechanism enables the bootloader load kernel images whose format is notunderstood “natively”.

Before examining what the fields at offsets 8-24mean, let’s take a look at the PE file format.

A PE image starts with a couple of standard headers (DOS / PE/ File / Optional). Following these is a set of headers called the sectionheaders that contain information about the different sections in the image. (Fora more verbose explanation of the PE file format read MattPietrek’s article) A section typically contains either code or data. Theabove kernel if compiled with the following switches

cl      /Gd
/Fokernel.obj
/Fm
/TC
/c
kernel.c

link     /safeseh:no
/filealign:0x1000
/BASE:0x100000
/MAP: kernel.map
/ENTRY:__multiboot_entry__kernel.obj
/NODEFAULTLIB:LIBC
/SUBSYSTEM:CONSOLE
/OUT:kernel.exe

Produces a .EXE with two sections named .text and .data.Sections are aligned on a 4K boundary using the undocumented linker switch/filealign:0x1000.

Armed with this information about the PE file format,lets examine the fields at offset 8-24 in the multiboot header.

dd(0x1BADB002)               ;magic
dd(1<< 16)                    ; flags
dd(-(0x1BADB002 + (1 <<16)))     ; checksum
dd(0x00101000)               ;header_addr
dd(0x00101000)               ;load_addr
dd(0x0010200F)               ;load_end_addr
dd(0x0010200F)               ;bss_end_addr

dd(0x00101030)               ;entry_addr
dd(0x00000000)               ;mode_type
dd(0x00000000)               ;width
dd(0x00000000)               ;height
dd(0x00000000)               ;depth

The field at offset 8, Checksum, needs to be set to – (magic+ flags). Grub loads the .text section of the kernel into physical address0x100000 (1 MB) + Offset by default. The offset is specified indirectly usingthe header_addr and load_addr fields. According to the specification header_addr“Contains the address corresponding to the beginning of the Multiboot header”.IMHO, this is a bit confusing. What it really means is, if the image file isloaded at 0x100000, the physical address of the starting of the multiboot headeris header_addr. The next field load_addr contains the physical address of thebeginning of the .text section. (In our case both are the same because themultiboot header is the first 48 bytes of the .text section). The next fieldload_end_addr is used to determine how many bytes of the image file actuallyneeds to be loaded. (Note that the .text and .data sections need to besuccessive in the image for this to work). In our case 0x102000 is where datasection starts and it has a size of 0xF bytes and hence the value 0x10200F forload_end_addr. Grub, now knows it needs to load 0x10200F – 0x101000 bytes. Thenext field according to the multiboot specification, needs to be set to 0 if abss section doesn’t exist. (As in our case). However Grub refuses to load theimage if bss_end_addr is set to 0, so I set it to 0x10200F (same as theprevious). The rest of the code is perhaps obvious and hence doesn’t deserve anexplanation.

Our multiboot compliant PE kernel is now ready :)
JFM7VX690T型SRAM型现场可编程门阵列技术手册主要介绍的是上海复旦微电子集团股份有限公司(简称复旦微电子)生产的高性能FPGA产品JFM7VX690T。该产品属于JFM7系列,具有现场可编程特性,集成了功能强大且可以灵活配置组合的可编程资源,适用于实现多种功能,如输入输出接口、通用数字逻辑、存储器、数字信号处理和时钟管理等。JFM7VX690T型FPGA适用于复杂、高速的数字逻辑电路,广泛应用于通讯、信息处理、工业控制、数据中心、仪表测量、医疗仪器、人工智能、自动驾驶等领域。 产品特点包括: 1. 可配置逻辑资源(CLB),使用LUT6结构。 2. 包含CLB模块,可用于实现常规数字逻辑和分布式RAM。 3. 含有I/O、BlockRAM、DSP、MMCM、GTH等可编程模块。 4. 提供不同的封装规格和工作温度范围的产品,便于满足不同的使用环境。 JFM7VX690T产品系列中,有多种型号可供选择。例如: - JFM7VX690T80采用FCBGA1927封装,尺寸为45x45mm,使用锡银焊球,工作温度范围为-40°C到+100°C。 - JFM7VX690T80-AS同样采用FCBGA1927封装,但工作温度范围更广,为-55°C到+125°C,同样使用锡银焊球。 - JFM7VX690T80-N采用FCBGA1927封装和铅锡焊球,工作温度范围与JFM7VX690T80-AS相同。 - JFM7VX690T36的封装规格为FCBGA1761,尺寸为42.5x42.5mm,使用锡银焊球,工作温度范围为-40°C到+100°C。 - JFM7VX690T36-AS使用锡银焊球,工作温度范围为-55°C到+125°C。 - JFM7VX690T36-N使用铅锡焊球,工作温度范围与JFM7VX690T36-AS相同。 技术手册中还包含了一系列详细的技术参数,包括极限参数、推荐工作条件、电特性参数、ESD等级、MSL等级、重量等。在产品参数章节中,还特别强调了封装类型,包括外形图和尺寸、引出端定义等。引出端定义是指对FPGA芯片上的各个引脚的功能和接线规则进行说明,这对于FPGA的正确应用和电路设计至关重要。 应用指南章节涉及了FPGA在不同应用场景下的推荐使用方法。其中差异说明部分可能涉及产品之间的性能差异;关键性能对比可能包括功耗与速度对比、上电浪涌电流测试情况说明、GTH Channel Loss性能差异说明、GTH电源性能差异说明等。此外,手册可能还提供了其他推荐应用方案,例如不使用的BANK接法推荐、CCLK信号PCB布线推荐、JTAG级联PCB布线推荐、系统工作的复位方案推荐等,这些内容对于提高系统性能和稳定性有着重要作用。 焊接及注意事项章节则针对产品的焊接过程提供了指导,强调焊接过程中的注意事项,以确保产品在组装过程中的稳定性和可靠性。手册还明确指出,未经复旦微电子的许可,不得翻印或者复制全部或部分本资料的内容,且不承担采购方选择与使用本文描述的产品和服务的责任。 上海复旦微电子集团股份有限公司拥有相关的商标和知识产权。该公司在中国发布的技术手册,版权为上海复旦微电子集团股份有限公司所有,未经许可不得进行复制或传播。 技术手册提供了上海复旦微电子集团股份有限公司销售及服务网点的信息,方便用户在需要时能够联系到相应的服务机构,获取最新信息和必要的支持。同时,用户可以访问复旦微电子的官方网站(***以获取更多产品信息和公司动态。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值