Write Your Own Operating System Tutorial(6)

原创 2003年01月21日 09:06:00

Lesson 6: Boot Loader

Everything we’ve done so far has been placed entirely inside the boot sector.  We can’t make our operating system very big at all if it is to fit in one sector.  We need a way of expanding.  We will do this by making a boot program that simply loads an executable file off the disk and begins executing it.  This is called a boot loader.  This file loaded off the disk can be as big as we want, since it will not be constrained to one sector.

This is more difficult than anything else we’ve done so far.  It might be a good idea, now, to locate a reference on the FAT file system (or the file system of your choice, but I will be assuming the use of the FAT system).  I will give a brief overview of the boot loading process.

A floppy disk contains, in this order, the DOS Boot Record (the first sector we have been working with), the File Allocation Table (FAT), the Root Directory, and then the data contained in the files on the disk.  (A hard disk is more complicated.  It has a Master Boot Record and multiple partitions.)  Suppose we write an operating system, compile/assemble it to a file named LOADER.BIN, and place it on the disk.  The boot loader will load it as follows.

  1. The DOS Boot Record (DBR) is read to determine the size of the DBR, FAT, and Root Directory.  The location of each on the disk is then determined.
  2. The Root Directory is read in to memory.
  3. The Root Directory is searched for the file name LOADER.BIN.  If found, we can look in the directory entry to find out which is the file’s first cluster (file allocation unit).  If not found, we give an error message.
  4. The File Allocation Table is read off the disk in to memory.
  5. Starting with the file’s first cluster, we use the FAT to locate all the clusters belonging to the file.  We read them all off the disk into memory at a specific location.
  6. We jump to that location to begin execution of the operating system.

All of the reading from the disk will be done using calls to BIOS.  If you feel adventurous, use a reference of BIOS functions to learn how to read sectors from the disk and try writing your own boot loader.  Otherwise, I have provided a slightly modified version of John S. Fine’s FAT12 bootstrap loader.  If you can find a copy of his utility “partcopy,” then use his compiling and installing instructions (and let me know where to find it).  Otherwise, copy the boot loader to the floppy disk using the same method we have used in the previous lessons.

There are many user-adjustable settings in John Fine’s bootstrap loader.  His loader assumes the use of a FAT12 file system (the system that is used on floppy disks).  For another system, you will need to use a different loader.  Things you can adjust are the locations where the operating system and various FAT data structures will be loaded into memory.  You can also adjust the filename (of the operating system) that the loader loads.

By default, the loader loads a file named LOADER.BIN in the root directory (if one exists) into memory starting at address 0x1000:0000.  (This is adjustable by the %define IMAGE_SEG).  Thus you can compile/assemble an operating system and copy it to the floppy disk as a file named LOADER.BIN.

As an example, we will take the Hello, World operating system from Lesson 4 and run it with our boot loader.  We cannot use the exact same source file from Lesson 4, however.  We need to make a few changes.  First, we need to take into account that this file will now be loaded into a different location in memory (0x1000:0000 instead of 0000:7C00), and, secondly, we can get rid of the DOS Boot Record data.

We can start the code by setting up the data and stack segments and the stack pointer.  We will do this as shown below.  The current code segment is in the CS register, and the static data assembled into the executable is here, so we will use this as the data segment as well.  For now, we will use this as the stack segment, but we will likely want to change this in the future.

       mov    ax, cs        ; Get the current segment<?xml:namespace prefix = o ns = "urn:schemas-microsoft-com:office:office" />

       mov    ds, ax        ; The data is in this segment

       cli                  ; disable interrupts while changing stack

       mov    ss, ax        ; We'll use this segment for the stack too

       mov    sp, 0xfffe    ; Start the stack at the top of the segment

       sti                  ; Reenable interrupts


Finally, we can get rid of some lines at the bottom of the source code that adds the boot sector signature and the check to make sure the file is exactly 1 sector long.  All of the other code should look familiar.  The resulting file can be downloaded here: lesson6.asm.

Assemble this file and copy it to your disk using the following commands.

nasmw lesson6.asm –o lesson6.bin

copy lesson6.bin a:/LOADER.BIN


Then, assuming you have already installed the boot loader, you can go ahead and boot with the disk.  Once you have this working, feel free to modify any other programs you have written in previous lessons, so that you can try loading them with this boot loader.  Most of the following lessons will assume that you will be using this boot loader (or other boot loader of your choice) to load your operating system file(s).

Now we can make our operating system larger than a single sector.

Build Your Own Angularjs 读书笔记(AngularJS牛逼的地方在于它内嵌了一个表达式到Function对象的编译器。。。当然还有DI框架)

Build Your Own Angularjs 读书笔记 目录  [隐藏]  1 项目配置2 作用域3 表达式与过滤器4 模块与依赖注入5 辅助函数6 指令 项目配置[编辑] npm ...
  • cteng
  • cteng
  • 2017年05月31日 19:41
  • 1320

操作系统概念 第九版 Operating System Concepts, 9th Edition 中文译稿(不定时更新)

前文: 本译稿不定时更新,翻译自Operating System Concepts, 9th Edition. 欢迎您的阅读,欢迎随时交流,仅供学习,请勿用作商业用途. 自行翻译的,水平不够存在错漏在...
  • advanced_delighted
  • advanced_delighted
  • 2016年03月06日 15:20
  • 3475

Android 6.0 运行时权限检查分析

Android版本升级到6.0之后,为了一改往日安全受人诟病的形象,将权限授权的安装时授予的基础上,对于一部分危险的权限采用动态控制授权的方式。类似国内手机安全助手权限控制的功能。 本篇...
  • hehui1860
  • hehui1860
  • 2016年08月27日 16:06
  • 6716

Sensory-Developing Your Own 32 Bit Computer Operating System

  • 2007年10月04日 10:33
  • 2.64MB
  • 下载

Sensory-Developing Your Own 32 Bit Computer Operating System--开发自己的操作系统

  • 2009年02月18日 08:20
  • 1.88MB
  • 下载

bringing smp to your up operating system

Bringing SMP to Your UP Operating System Sidney Cammeresi Overview PrefaceTerminologyMP...
  • chrysanthemumcao
  • chrysanthemumcao
  • 2012年08月19日 18:18
  • 629

怎样写一个简单的操作系统?(原文标题:How to write a simple operating system)

  • magictong
  • magictong
  • 2011年01月26日 01:10
  • 14155

怎样写一个简单的操作系统?(原文标题:How to write a simple operating system) 分类: 翻译 2011-01-26 01:10 3175人阅读 评论(3) 收藏

怎样写一个简单的操作系统?(原文标题:How to write a simple operating system) 分类: 翻译2011-01-26 01:10 3175人阅读 评论(3) 收...
  • HuaiZuoWoZuo
  • HuaiZuoWoZuo
  • 2013年11月19日 21:30
  • 2083

XV6, a simple, Unix-like teaching operating system

  • 2017年11月30日 03:19
  • 1.04MB
  • 下载

Operating System Concepts 6th Edition 英文版

  • 2015年01月13日 00:01
  • 44.2MB
  • 下载
您举报文章:Write Your Own Operating System Tutorial(6)