光盘启动 (Boot from CDROM) Part 1- SakiProject

来源:互联网 发布:淘宝pc端是在哪里 编辑:程序博客网 时间:2024/06/05 07:35

嘛,毕竟打算走留学的路线,那么得渐渐习惯起学术英语了,今后的博客就用双语发吧。好久没写博客,也好久没写操作系统了,今天又想干一把了。

Well, since I already planned to study in United States, I need to practice my English in academic environment, so I decided to post my future blogs in both Chinese and English. I haven't write blogs for almost a year, and I haven't develop my OS project in a long time, but today I want to try that again.

这次的项目名叫SakiProject,Saki是日语花朵盛开的意思。

The project this time is called SakiProject, Saki means the the bloom of blossom in Japanese.

于是--花了一天时间, 先写了个引导扇区练练手。

As a result -- I spent a day to write a bootsect as a warmup.

这次的设计与之前的Norlit OS不太一样,这次不打算依靠grub,而且只支持64位的CPU,并且依赖其他高级功能(基本上现在电脑都具有,比如APIC,ACPI,SMP等等)。我一开始就把目标定在支持光盘/U盘/硬盘启动,于是直接抛弃掉以前的软盘启动。然后U盘启动因为可以模拟成USB-HDD和USB-CDROM,于是也就先不管了。在权衡之下我认为应该先支持光盘启动,因为毕竟安装操作系统就要用到光盘嘛(笑)。

The design this time is different from the previous one, Norlit OS, and this time I decide not to rely on grub, and I plan to support 64 bit CPU only, and rely on many other advanced features (Well, though they're advanced, the computer we are using now have almost all these features, ex. APIC, ACPI, SMP, etc.) I aim at supporting booting from cdrom, usb and harddisk, so I just abandon the old floppy boot codes. And since usb disks can emulate harddisk(USB-HDD) and cdrom(USB-CDROM), I just forget about it in a short time (>_<). After consideration, I decide to support boot from cdrom first, because we need it to install our system(lol, maybe I consider too much).

我们首先使用xorriso(或者mkisofs)建立一个iso文件,然后尝试用一个引导扇区去读取里面的一个文本文件。(好吧,其实不一定是要扇区,如果用EI Torito启动的话貌似没大小限制,不过我们加上限制也无妨)。

We first use xorriso (or mkisofs) to build a iso file, then try to use a bootsect to read a text file inside it. To be honest, the bootsect is not necessarily a sector, because if you use EI Torito as boot spec, the size of bootsect is unlimited. But it is good to restrict our code to prevent the boom of its size)

我们先建立一个mount文件夹放iso里的东西,然后在mount里面建立saki,boot两个文件夹,saki文件夹里放bootmgr,bootmgr文件夹里放一个文本文件,我叫它boot.txt。如果我们的引导扇区的源文件是boot.asm,我们可以用一下命令来建立这个iso并运行。(需要安装nasm和qemu)

We first create a folder called mount to place files to be added to the iso file, then we create saki and boot two folders in mount, create a bootmgr folder in saki and create a boot.txt file in bootmgr. If our source code for the bootsect is boot.asm, we can use the following code to build the iso file and run it. (You need to install nasm and qemu first)

nasm boot.asm -o mount/boot.bin
xorriso -as mkisofs -R -J -c boot/bootcat -b boot.bin -no-emul-boot -boot-load-size 4 -o boot.iso mount
qemu-system-x86_64 -boot d -cdrom boot.iso -s


第一步,我们首先得让我们的boot.asm运行起来试试。

First step, we need to run our boot.asm first to say "hello, world" (Well, a cliche)

[org 7c00h][bits   16]jmp     0:main   ; nullify cs if the bootsect is loaded at 7c0h:0; entrance pointmain:    mov     ax,cs    mov     ds,ax           ; Fix ds and es, set them equal to cs    mov     es,ax    xor     ax, ax    mov     ss, ax    mov     sp, 0x7c00      ; setup stack    mov     [driveNum], dl  ; The bios pass us the drive number in dl, we need to save it first    ; Print information    mov     esi, Welcome    call    print    cli    hlt;end main; print given string; @param si     offset of the string; @destroy ax, bx, siprint:    mov     bx, 0000Fh    mov     ah, 0Eh.loop:    mov     al, [si]    inc     si    or      al, al    jz      .end    int     10h    jmp     .loop.end:    ret;end print; driveNum is the initial dl value passed to us by biosdriveNum    db  0Welcome db "Hello world is too common so I don't want to say it. "times 510-($-$$) db 0dw   0xAA55  ; End mark

这段代码没有什么难度,刚开始先跳转到0:main,因为有些bios会加载到把程序加载到7c0h:0,很蛋疼,所以先fix。然后就是一如既往的设置段寄存器。下面一步比较特殊,因为dl是bios传给你的参数,指示了引导设备的驱动器号码。所以用这个号码读肯定不会读错驱动器,我们先保存,防止被破坏。print函数是学过汇编的人都能看的懂得输出语句,就略过了。

This code segment has no difficulty. We long jump to 0:main first, because some non-standard bios may load our program to 7c0h:0, which is terrible, so we fix it first. Then we setup the segment registers as every assembly program do. The next step is very special, because dl is the argument passed to us by bios, indicating the drive number of the boot drive. So, if we use this number in bios calls, we will certainly read the drive that contains our files. Thus, we save the number first to prevent potential overrides. Print function is a simple function calling bios to display texts, and I just ignore it because it's the very basic of assembly programming.

我们来运行一下:

Let's run it first:


嗯,看起来不错,我们有了很好的开始。对了,硬盘启动的代码也是一样的,因为我们还没有设计到光驱Only的代码。

Um, it looks good, and we surely have a great start. By the way, the code is the same for harddisk because we didn't introduce cdrom-specific codes.


有点晚了,我先去睡一觉然后继续把这篇文章的其他Part更了。

It's too late, so I will go sleep first and then finish other parts in this series.

0 0
原创粉丝点击