Writing Your Own Toy OS (Part I)

作者: Krishnakumar R.
整理:penghuster
編者按:偶然在 http://oldlinux.org/Linux.old/docs/WritingOS.pdf 發(fā)現(xiàn)此文,覺得對于理解 Linux 內(nèi)核構(gòu)建有幫助见剩,而沒有看見網(wǎng)頁版常侣,故整理于此搏恤。另外我將此文也進行了翻譯 http://www.reibang.com/p/497701ffc060梢莽。


This article is a hands-on tutorial for building a small boot sector. The first section provides the theory behind what happens at the time the computer is switched on. It also explains our plan. The second section tells all the things you should have on hand before proceeding further, and the third section deals with the programs. Our little startup program won't actually boot Linux, but it will display something on the screen.


1. Background

1.1 The Fancy Dress

The microprocessor controls the computer. At startup, every microprocessor is just another 8086. Even though you may have a brand new Pentium, it will only have the capabilities of an 8086. From this point, we can use some software and switch processor to the infamous protected mode . Only then can we utilize the processor's full power.

1.2 Our Role

Initially, control is in the hands of the BIOS . This is nothing but a collection of programs that are stored in ROM. BIOS performs the POST (Power On Self Test). This checks the integrity of the computer (whether the peripherals are working properly, whether the keyboard is connected, etc.). This is when you hear those beeps from the computer. If everything is okay, BIOS selects a boot device. It copies the first sector (boot sector) from the device, to address location 0x7C00 . The control is then transferred to this location. The boot device may be a floppy disk, CD-ROM, hard disk or some device of your choice. Here we will take the
boot device to be a floppy disk. If we had written some code into the boot sector of the floppy, our code would be executed now. Our role is clear: just write some programs to the boot sector of the floppy.

1.3 The Plan

First write a small program in 8086 assembly (don't be frightened; I will teach you how to write it), and copy it to the boot sector of the floppy. To copy, we will code a C program. Boot the computer with that floppy, and then enjoy.

2. Things You Should Have

as86
This is an assembler. The assembly code we write is converted to an object file by this tool.
ld86
This is the linker. The object code generated by as86 is converted to actual machine language code by this tool. Machine language will be in a form that 8086 understands.
gcc
The C compiler. For now we need to write a C program to transfer our OS to the floppy.
A free floppy
A floppy will be used to store our operating system. This also is our boot
device.
Good Old Linux box
You know what this is for.

as86 and ld86 will be in most of the standard distributions. If not, you can
always get them from the site http://www.cix.co.uk/~mayday/. Both of them are included in single package, bin86. Good documentation is available at www.linux.org/docs/ldp/howto/Assembly-HOWTO/as86.html.

3. Start!

3.1 The Boot Sector

Grab your favourite editor and type in these few lines.

entry start
start:
mov ax,#0xb800
mov es,ax
seg es
mov [0],#0x41
seg es
mov [1],#0x1f
loop1: jmp loop1

This is an assembly language that as86 will understand. The first statement specifies the entry point where the control should enter the program. We are stating that control should initially go to label start . The 2nd line depicts the location of the label start (don't forget to put ":" after the start). The first statement that will be executed in this program is the statement just after start .

0xb800 is the address of the video memory. The # is for representing an immediate value. After the execution of mov ax,#0xb800 register ax will contain the value 0xb800, that is, the address of the video memory. Now we move this value to the es register. es stands for the extra segment register. Remember that 8086 has a segmented architecture. It has segments like code segments, data segments, extra segments, etc.--hence the segment registers cs, ds, es. Actually, we have made the video memory our extra segment, so anything written to extra segment would go to video memory.

To display any character on the screen, you need to write two bytes to the video memory. The first is the ascii value you are going to display. The second is the attribute of the character. Attribute has to do with which colour should be used as the foreground, which for the background, should the char blink and so on. seg es is actually a prefix that tells which instruction is to be executed next with reference to es segment. So, we move value 0x41, which is the ascii value of character A, into the first byte of the video memory. Next we need to move the attribute of the character to the next byte. Here we enter 0x1f, which is the value for representing a white character on a blue background. So if we execute this program, we get a white A on a blue background. Finally, there is the loop. We need to stop the execution after the display of the character, or we have a loop that loops forever. Save the file as boot.s .

The idea of video memory may not be very clear, so let me explain further. Suppose we assume the screen consists of 80 columns and 25 rows. So for each line we need 160 bytes, one for each character and one for each character's attribute. If we need to write some character to column 3 then we need to skip bytes 0 and 1 as they is for the 1st column; 2 and 3 as they are for the 2nd column; and then write our ascii value to the 4th byte and its attribute to the 5th location in the video memory.

3.2 Writing Boot Sector to Floppy

We have to write a C program that copies our code (OS code) to first sector of the floppy disk. Here it is:

#include <sys/types.h> /* unistd.h needs this */
#include <unistd.h> /* contains read/write */
#include <fcntl.h>
int main()
{
char boot_buf[512];
int floppy_desc, file_desc;
file_desc = open("./boot", O_RDONLY);
read(file_desc, boot_buf, 510);
close(file_desc);
boot_buf[510] = 0x55;
boot_buf[511] = 0xaa;
floppy_desc = open("/dev/fd0", O_RDWR);
lseek(floppy_desc, 0, SEEK_CUR);
write(floppy_desc, boot_buf, 512);
close(floppy_desc);
}

First, we open the file boot in read-only mode, and copy the file descripter of the opened file to variable file_desc . Read from the file 510 characters or until the file ends. Here the code is small, so the latter case occurs. Be decent; close the file.

The last four lines of code open the floppy disk device (which mostly would be /dev/fd0). It brings the head to the beginning of the file using lseek , then writes the 512 bytes from the buffer to floppy.

The man pages of read, write, open and lseek (refer to man 2) would give you enough information on what the other parameters of those functions are and how to use them. There are two lines in between, which may be slightly mysterious.
The lines:

boot_buf[510] = 0x55;
boot_buf[511] = 0xaa;

This information is for BIOS. If BIOS is to recognize a device as a bootable device, then the device should have the values 0x55 and 0xaa at the 510th and 511th location. Now we are done. The program reads the file boot to a buffer named boot_buf. It makes the required changes to 510th and 511th bytes and then writes boot_buf to floppy disk. If we execute the code, the first 512 bytes of the floppy disk will contain our boot code. Save the file as write.c .

3.3 Let's Do It All

To make executables out of this file you need to type the following at the Linux bash prompt.

as86 boot.s -o boot.o
ld86 -d boot.o -o boot
cc write.c -o write

First, we assemble the boot.s to form an object file boot.o . Then we link this file to get the final file boot . The -d for ld86 is for removing all headers and producing pure binary. Reading man pages for as86 and ld86 will clear any doubts. We then compile the C program to form an executable named write .

Insert a blank floppy into the floppy drive and type
./write
Reset the machine. Enter the BIOS setup and make floppy the first boot device. Put the floppy in the drive and watch the computer boot from your boot floppy.

Then you will see an 'A' (with white foreground color on a blue background). That means that the system has booted from the boot floppy we have made and then executed the boot sector program we wrote. It is now in the infinite loop we had written at the end of our boot sector. We must now reboot the computer and remove the our boot floppy to boot into Linux.

From here, we'll want to insert more code into our boot sector program, to make it do more complex things (like using BIOS interrupts, protected-mode switching, etc). The later parts (PART II, PART III etc. ) of this article will guide you on further improvements. Till then GOOD BYE !


Krishnakumar R.
Krishnakumar is a final year B.Tech student at Govt. Engg. College Thrissur, Kerala, India. His journey into the land of Operating systems started with module programming in linux . He has built a routing operating system by name GROS.(Details available at his home page: www.askus.way.to ) His other interests include Networking, Device drivers, Compiler Porting and Embedded systems.


版權(quán)聲明:自由轉(zhuǎn)載-非商用-非衍生-保持署名創(chuàng)意共享3.0許可證

最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
  • 序言:七十年代末嗓袱,一起剝皮案震驚了整個濱河市食听,隨后出現(xiàn)的幾起案子胸蛛,更是在濱河造成了極大的恐慌,老刑警劉巖樱报,帶你破解...
    沈念sama閱讀 212,816評論 6 492
  • 序言:濱河連續(xù)發(fā)生了三起死亡事件葬项,死亡現(xiàn)場離奇詭異,居然都是意外死亡迹蛤,警方通過查閱死者的電腦和手機民珍,發(fā)現(xiàn)死者居然都...
    沈念sama閱讀 90,729評論 3 385
  • 文/潘曉璐 我一進店門,熙熙樓的掌柜王于貴愁眉苦臉地迎上來盗飒,“玉大人嚷量,你說我怎么就攤上這事∧嫒ぃ” “怎么了蝶溶?”我有些...
    開封第一講書人閱讀 158,300評論 0 348
  • 文/不壞的土叔 我叫張陵,是天一觀的道長宣渗。 經(jīng)常有香客問我抖所,道長,這世上最難降的妖魔是什么落包? 我笑而不...
    開封第一講書人閱讀 56,780評論 1 285
  • 正文 為了忘掉前任部蛇,我火速辦了婚禮,結(jié)果婚禮上咐蝇,老公的妹妹穿的比我還像新娘涯鲁。我一直安慰自己,他們只是感情好有序,可當我...
    茶點故事閱讀 65,890評論 6 385
  • 文/花漫 我一把揭開白布抹腿。 她就那樣靜靜地躺著,像睡著了一般旭寿。 火紅的嫁衣襯著肌膚如雪警绩。 梳的紋絲不亂的頭發(fā)上,一...
    開封第一講書人閱讀 50,084評論 1 291
  • 那天盅称,我揣著相機與錄音肩祥,去河邊找鬼后室。 笑死,一個胖子當著我的面吹牛混狠,可吹牛的內(nèi)容都是我干的岸霹。 我是一名探鬼主播,決...
    沈念sama閱讀 39,151評論 3 410
  • 文/蒼蘭香墨 我猛地睜開眼将饺,長吁一口氣:“原來是場噩夢啊……” “哼贡避!你這毒婦竟也來了?” 一聲冷哼從身側(cè)響起予弧,我...
    開封第一講書人閱讀 37,912評論 0 268
  • 序言:老撾萬榮一對情侶失蹤刮吧,失蹤者是張志新(化名)和其女友劉穎,沒想到半個月后掖蛤,有當?shù)厝嗽跇淞掷锇l(fā)現(xiàn)了一具尸體杀捻,經(jīng)...
    沈念sama閱讀 44,355評論 1 303
  • 正文 獨居荒郊野嶺守林人離奇死亡,尸身上長有42處帶血的膿包…… 初始之章·張勛 以下內(nèi)容為張勛視角 年9月15日...
    茶點故事閱讀 36,666評論 2 327
  • 正文 我和宋清朗相戀三年坠七,在試婚紗的時候發(fā)現(xiàn)自己被綠了水醋。 大學時的朋友給我發(fā)了我未婚夫和他白月光在一起吃飯的照片旗笔。...
    茶點故事閱讀 38,809評論 1 341
  • 序言:一個原本活蹦亂跳的男人離奇死亡彪置,死狀恐怖,靈堂內(nèi)的尸體忽然破棺而出蝇恶,到底是詐尸還是另有隱情拳魁,我是刑警寧澤,帶...
    沈念sama閱讀 34,504評論 4 334
  • 正文 年R本政府宣布撮弧,位于F島的核電站潘懊,受9級特大地震影響,放射性物質(zhì)發(fā)生泄漏贿衍。R本人自食惡果不足惜授舟,卻給世界環(huán)境...
    茶點故事閱讀 40,150評論 3 317
  • 文/蒙蒙 一、第九天 我趴在偏房一處隱蔽的房頂上張望贸辈。 院中可真熱鬧释树,春花似錦、人聲如沸擎淤。這莊子的主人今日做“春日...
    開封第一講書人閱讀 30,882評論 0 21
  • 文/蒼蘭香墨 我抬頭看了看天上的太陽嘴拢。三九已至桩盲,卻和暖如春,著一層夾襖步出監(jiān)牢的瞬間席吴,已是汗流浹背赌结。 一陣腳步聲響...
    開封第一講書人閱讀 32,121評論 1 267
  • 我被黑心中介騙來泰國打工捞蛋, 沒想到剛下飛機就差點兒被人妖公主榨干…… 1. 我叫王不留,地道東北人柬姚。 一個月前我還...
    沈念sama閱讀 46,628評論 2 362
  • 正文 我出身青樓襟交,卻偏偏與公主長得像,于是被迫代替她去往敵國和親伤靠。 傳聞我的和親對象是個殘疾皇子捣域,可洞房花燭夜當晚...
    茶點故事閱讀 43,724評論 2 351

推薦閱讀更多精彩內(nèi)容

  • **2014真題Directions:Read the following text. Choose the be...
    又是夜半驚坐起閱讀 9,442評論 0 23
  • 遇到張妍是在一個夏天的傍晚,我拎了大概百十張宣傳單在大街上招搖撞騙宴合,這些傳單經(jīng)過不少人之手之后而且還都發(fā)出去了...
    4b82fd06921b閱讀 163評論 0 0
  • 非是我要起一個夸張的標題卦洽,而是船底頂最廣為流傳的就是這句話了贞言。 船底頂位于廣東省清遠市轄市英德和韶關市曲江區(qū)交接處...
    堅韌的天狼星閱讀 1,578評論 13 11
  • 為了追求所謂的安全感,所以買房成了獲得安全感的最佳途徑阀蒂。 但現(xiàn)實是有多少人能毫不費力的買下一套自己...
    茹此懶羊羊閱讀 235評論 0 0
  • 我聽說该窗,太陽喜歡深秋的早晨;我聽說蚤霞,細雨總是伴著微風酗失;我聽說,四葉草只有三片葉子昧绣;我聽說规肴,樹葉掉了大樹會很冷;我聽...
    流波上的白鳥閱讀 538評論 1 7