1.参考文档
https://github.com/littleosbook/
2.宿主机环境
:~/lab/myOS# cat /etc/issue
Ubuntu 14.10
l
3.编译&链接OS内核
apt-get install build-essential nasm genisoimage bochs bochs-sdl(下载工具)
nasm -f elf32 loader.s(编译loader.s生成loader.o)
global loader ; the entry symbol for ELF MAGIC_NUMBER equ 0x1BADB002 ; define the magic number constant FLAGS equ 0x0 ; multiboot flags CHECKSUM equ -MAGIC_NUMBER ; calculate the checksum ; (magic number + checksum + flags should equal 0) section .text: ; start of the text (code) section align 4 ; the code must be 4 byte aligned dd MAGIC_NUMBER ; write the magic number to the machine code, dd FLAGS ; the flags, dd CHECKSUM ; and the checksum loader: ; the loader label (defined as entry point in linker script) mov eax, 0xCAFEBABE ; place the number 0xCAFEBABE in the register eax .loop: jmp .loop ; loop forever
ld -T link.ld -melf_i386 loader.o -o kernel.elf(链接OS生成内核二进制文件)
ENTRY(loader) /* the name of the entry label */ SECTIONS { . = 0x00100000; /* the code should be loaded at 1 MB */ .text ALIGN (0x1000) : /* align at 4 KB */ { *(.text) /* all text sections from all files */ } .rodata ALIGN (0x1000) : /* align at 4 KB */ { *(.rodata*) /* all read-only data sections from all files */ } .data ALIGN (0x1000) : /* align at 4 KB */ { *(.data) /* all data sections from all files */ } .bss ALIGN (0x1000) : /* align at 4 KB */ { *(COMMON) /* all COMMON sections from all files */ *(.bss) /* all bss sections from all files */ } }
wget http://littleosbook.github.com/files/stage2_eltorito(下载适合于ubuntu的grub)
4.打包OS内核生成iso镜像
#!/bin/bash mkdir -p iso/boot/grub # create the folder structure cp stage2_eltorito iso/boot/grub/ # copy the bootloader cp kernel.elf iso/boot/ # copy the kernel cp menu.lst iso/boot/grub/ genisoimage -R -b boot/grub/stage2_eltorito -no-emul-boot -boot-load-size 4 -A os -input-charset utf8 -quiet -boot-info-table -o os.iso iso
其中menu.lst文件
default=0 timeout=0 title os kernel /boot/kernel.elf
5.运行OS
bochs -f bochsrc.txt -q(注意远程终端运行会报错,提示sdl库不支持videomode,使用宿主机可以正常运行)
bothsrc.txt
megs: 32 display_library: sdl romimage: file=/usr/share/bochs/BIOS-bochs-latest vgaromimage: file=/usr/share/bochs/VGABIOS-lgpl-latest ata0-master: type=cdrom, path=os.iso, status=inserted boot: cdrom log: bochslog.txt clock: sync=realtime, time0=local cpu: count=1, ips=1000000
6.验证OS正常运行
点击弹出的窗口右上角的power关机,可以看到bochslog.txt中显示
02604562000i[CPU0 ] | RAX=00000000cafebabe RBX=000000000002cd80