How to Make a Computer Operating System (4)

Multiboot头结构:

struct multiboot_info { u32 flags; u32 low_mem; u32 high_mem; u32 boot_device; u32 cmdline; u32 mods_count; u32 mods_addr; struct { u32 num; u32 size; u32 addr; u32 shndx; } elf_sec; unsigned long mmap_length; unsigned long mmap_addr; unsigned long drives_length; unsigned long drives_addr; unsigned long config_table; unsigned long boot_loader_name; unsigned long apm_table; unsigned long vbe_control_info; unsigned long vbe_mode_info; unsigned long vbe_mode; unsigned long vbe_interface_seg; unsigned long vbe_interface_off; unsigned long vbe_interface_len; };

You can use the command mbchk kernel.elf to validate your kernel.elf file against the multiboot standard. You can also use the command nm -n kernel.elf to validate the offset of the different objects in the ELF binary.

您可以使用命令 mbchk kernel.elf来根据多引导标准验证kernel.elf文件。您还可以使用nm -n kernel.elf验证elf二进制文件中不同对象的偏移量。

Create a disk image for our kernel and grub 为内核和grub创建一个磁盘映像

The script diskimage.sh will generate a hard disk image that can be used by QEMU.

脚本diskimage.sh将生成一个可以使用QEMU启动的硬盘映像。

The first step is to create a hard-disk image (c.img) using qemu-img:

第一步是使用qemu-img创建硬盘映像(c.img):

qemu-img create c.img 2M

We need now to partition the disk using fdisk:

我们现在需要使用fdisk来分区磁盘:

fdisk ./c.img # Switch to Expert commands 切换到专家命令 > x # Change number of cylinders (1-1048576) 修改柱面数量(1-1048576) > c > 4 # Change number of heads (1-256, default 16) 改变磁头数量(1-256,默认16) > h > 16 # Change number of sectors/track (1-63, default 63) 更改磁道数目(1-63,默认63) > s > 63 # Return to main menu 返回主菜单 > r # Add a new partition 添加一个新分区 > n # Choose primary partition 选择主分区 > p # Choose partition number 选择分区号 > 1 # Choose first sector (1-4, default 1) 选择第一个扇区(1-4,默认1) > 1 # Choose last sector, +cylinders or +size{K,M,G} (1-4, default 4) 选择最后一个扇区,+柱面或+大小{K,M,G}(1-4,默认4) > 4 # Toggle bootable flag 切换启动的标志 > a # Choose first partition for bootable flag 为可引导标志选择第一个分区 > 1 # Write table to disk and exit 将表写到磁盘并退出 > w

We need now to attach the created partition to the loop-device using losetup. This allows a file to be access like a block device. The offset of the partition is passed as an argument and calculated using: offset= start_sector * bytes_by_sector.

现在我们需要使用losetup命令将创建的分区附加到循环设备上。这允许像块设备一样访问文件。分区的偏移量作为参数传递并使用:offset= start_扇区* bytes_by_扇区计算。

Using fdisk -l -u c.img, you get: 63 * 512 = 32256.

使用 fdisk -l -u c.img, 你将得到: 63 * 512 = 32256.

losetup -o 32256 /dev/loop1 ./c.img

We create a EXT2 filesystem on this new device using:

我们使用以下命令在这个新设备上创建EXT2文件系统:

mke2fs /dev/loop1

We copy our files on a mounted disk:

我们复制我们的文件在一个挂载磁盘:

mount /dev/loop1 /mnt/ cp -R bootdisk/* /mnt/ umount /mnt/

Install GRUB on the disk:

在磁盘上安装GRUB:

grub --device-map=http://www.likecs.com/dev/null << EOF device (hd0) ./c.img geometry (hd0) 4 16 63 root (hd0,0) setup (hd0) quit EOF

And finally we detach the loop device:

最后我们分离驱动循环:

losetup -d /dev/loop1 See Also 参考

GNU GRUB on Wikipedia

Multiboot specification

内容版权声明:除非注明,否则皆为本站原创文章。

转载注明出处:https://www.heiqu.com/wsfsxx.html