• 自己写一个简单的操作系统


    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

  • 相关阅读:
    Crowdsec:一款面向Linux的现代化协作式大型多人防火墙
    Linux文件操作:利用C语言删除某个目录下的文件
    GitHub 长期被中国人“霸榜”?看完这些榜单,这又是什么新姿势?
    英语不好学不好编程?程序员记忆单词专属诀窍,效果简直要逆天
    谁说女生不适合当程序员?世界第一位程序员,就是女性!
    C语言丨一篇文章带你认识递归与迭代
    C语言进阶丨(七)数组的基本概念和初始化【1】
    赔97.6万元!腾讯一程序员违反竞业协议,三年白干了!
    雪夜思绪
    算法复习(2)递归
  • 原文地址:https://www.cnblogs.com/dodng/p/6277131.html
Copyright © 2020-2023  润新知