1. 程式人生 > >Linux內核0.11 bootsect文件說明

Linux內核0.11 bootsect文件說明

現在 RoCE 語句 數據 ant tty click there AR

一、總體功能介紹

這是關於Linux-kernel-0.11中boot文件夾下bootsect.s源文件的說明,其中涉及到了一些基礎知識可以參考這兩篇文章。

  1. 操作系統啟動過程
  2. 軟盤相關知識和通過BIOS中斷訪問軟盤

bootsect.s 代碼是磁盤引導塊程序,存儲在磁盤的第一個扇區中(0面0道1扇區),在計算機上電BIOS自檢後,BIOS 會吧引導扇區代碼bootsect加載到內存0x90000處開並運行。

bootsect代碼主要完成以下幾項工作:

  • 加載從磁盤第二個扇區開始的4個扇區的內容(由setup.s編譯而成)到內存緊接著bootsect後面的0x90200處。

  • 利用BIOS中斷 int 13h 獲取磁盤參數表中當前啟動引導盤的參數。

  • 在屏幕上顯示"Loading system..."字符串。

  • 把磁盤上setup模塊後面的system模塊加載到內存0x10000開始的地方。

  • 確定根文件系統的設備號,若沒有指定,則根據所保存的引導盤的每磁道扇區數判別出磁盤的類型和種類(1.2MB 或 1.44MB 軟盤),並將其設備號保存在root_dev(引導扇區的508地址出,即第一個扇區的第509個字節處)。

  • 長跳轉到setup程序開始處(0x90200)執行setup程序。

在磁盤上,引導塊bootsect、setup模塊和system模塊的扇區位置和大小如下圖所示:

技術分享圖片

二、代碼註釋

!
! SYS_SIZE is the number of clicks (16
bytes) to be loaded. ! 0x3000 is 0x30000 bytes = 196kB, more than enough for current ! versions of linux ! SYS_SIZE 是要加載的系統模塊的長度,單位是節 1節 = 16字節, 0x3000 字節 = 196kB。 !這裏定義了system模塊長度,所以makefile中的規則就失效了 ! SYSSIZE = 0x3000 ! ! bootsect.s (C) 1991 Linus Torvalds ! ! bootsect.s is loaded at 0x7c00 by the bios-startup routines, and
moves ! iself out of the way to address 0x90000, and jumps there. ! ! It then loads ‘setup‘ directly after itself (0x90200), and the system ! at 0x10000, using BIOS interrupts. ! ! NOTE! currently system is at most 8*65536 bytes long. This should be no ! problem, even in the future. I want to keep it simple. This 512 kB ! kernel size should be enough, especially as this doesn‘t contain the ! buffer cache as in minix ! ! The loader has been made as simple as possible, and continuos ! read errors will result in a unbreakable loop. Reboot by hand. It ! loads pretty fast by getting whole sectors at a time whenever possible. ! .globl 或 .global 用於定義隨後的標識符是外部的或全局的,並且即使不使用也強制引入。 ! .text .data .bss 分別定義當前代碼段、數據段和未初始化數據段。 .globl begtext, begdata, begbss, endtext, enddata, endbss !定義了6個全局標識符 .text !文本段 begtext: .data !數據段 begdata: .bss !未初始化數據段 begbss: .text SETUPLEN = 4 ! nr of setup-sectors setup程序的扇區(setup-sectors)值 BOOTSEG = 0x07c0 ! original address of boot-sector bootsect的原始值(是段地址) INITSEG = 0x9000 ! we move boot here - out of the way 將bootsect移到這裏 SETUPSEG = 0x9020 ! setup starts here setup程序從這裏開始 SYSSEG = 0x1000 ! system loaded at 0x10000 (65536). system模塊加載到0x10000(64KB)處 ENDSEG = SYSSEG + SYSSIZE ! where to stop loading 停止加載的段地址 ! ROOT_DEV: 0x000 - same type of floppy as boot. 根文件系統設備與引導使用同樣的軟驅設備 ! 0x301 - first partition on first drive etc 根文件系統設備在第一個硬盤的第一個分區上。 ROOT_DEV = 0x306 entry start !告訴鏈接程序,程序從標號start開始執行 start: mov ax,#BOOTSEG !設置ds為0x07c0(段地址) mov ds,ax mov ax,#INITSEG !設置es為0x9000(段地址) mov es,ax mov cx,#256 !移動計數值256個字 sub si,si !源地址 ds:si = 0x07c0:0x0000 sub di,di !目的地址 es:di = 0x9000:0x0000 rep !重復執行並cx的值,直到cx = 0 movw !串傳送指令,從[si]移動cx個字到[di]處。從start開始到這裏,這段程序將bootsect從0x07c0復制到0x9000處 jmpi go,INITSEG !段間跳轉,INITSEG 指跳轉到的段地址, 標號go是段內偏移地址。 go: mov ax,cs !設置ds,ss和es為復制代碼後代碼所在的段。 mov ds,ax mov es,ax ! put stack at 0x9ff00. mov ss,ax mov sp,#0xFF00 ! arbitrary value >>512 設置棧頂指針,遠大於512個字節偏移。 ! load the setup-sectors directly after the bootblock. ! Note that ‘es‘ is already set up. !加載 setup 模塊代碼數據,註意 es已經指向了 0x9000處,不用再設置。 !將setup從磁盤第二個扇區讀到0x90200開始處,共讀4個扇區。如果讀出錯,則復位驅動器並重讀。 load_setup: mov dx,#0x0000 ! drive 0, head 0 對驅動器0進行操作 mov cx,#0x0002 ! sector 2, track 0 mov bx,#0x0200 ! address = 512, in INITSEG mov ax,#0x0200+SETUPLEN ! service 2, nr of sectors int 0x13 ! read it jnc ok_load_setup ! ok - continue mov dx,#0x0000 mov ax,#0x0000 ! reset the diskette int 0x13 j load_setup ! j 即jmp指令 ok_load_setup: ! Get disk drive parameters, specifically nr of sectors/track ! 獲取磁盤參數,尤其是每道的扇區數量 mov dl,#0x00 mov ax,#0x0800 ! AH=8 is get drive parameters int 0x13 mov ch,#0x00 seg cs mov sectors,cx mov ax,#INITSEG mov es,ax ! Print some inane message 在屏幕上顯示msg1指向的字符串,"Loading system ..." mov ah,#0x03 ! read cursor pos xor bh,bh int 0x10 mov cx,#24 mov bx,#0x0007 ! page 0, attribute 7 (normal) mov bp,#msg1 mov ax,#0x1301 ! write string, move cursor int 0x10 ! 寫字符串並將光標移動到字符串結尾處。 ! ok, we‘ve written the message, now 現在開始將system模塊加載到 0x10000處(64KB處) ! we want to load the system (at 0x10000) mov ax,#SYSSEG mov es,ax ! segment of 0x010000 call read_it ! 讀磁盤上的system模塊,es為輸入參數 call kill_motor ! 關閉驅動馬達,這樣就可以知道驅動器的狀態了。 ! After that we check which root-device to use. If the device is ! defined (!= 0), nothing is done and the given device is used. ! Otherwise, either /dev/PS0 (2,28) or /dev/at0 (2,8), depending ! on the number of sectors that the BIOS reports currently. ! 確定選用哪個根文件系統 seg cs mov ax,root_dev cmp ax,#0 jne root_defined seg cs mov bx,sectors mov ax,#0x0208 ! /dev/ps0 - 1.2Mb cmp bx,#15 ! 判斷每磁道扇區數是否為15 je root_defined mov ax,#0x021c ! /dev/PS0 - 1.44Mb cmp bx,#18 ! 判斷每磁道扇區數是否為18 je root_defined undef_root: jmp undef_root 如果根文件系統設備都不對,則死循環。 root_defined: seg cs mov root_dev,ax ! 將檢查過的設備號保存到 root_dev 中 ! after that (everyting loaded), we jump to ! the setup-routine loaded directly after ! the bootblock: jmpi 0,SETUPSEG ! 到這裏所有的文件都已經加載完畢,程序跳轉到setup文件中去執行。 !!!!! bootsect.s 執行到這裏就結束了。 下面是兩個子程序。 ! This routine loads the system at address 0x10000, making sure ! no 64kB boundaries are crossed. We try to load it as fast as ! possible, loading whole tracks whenever we can. ! ! in: es - starting address segment (normally 0x1000) ! sread: .word 1+SETUPLEN ! sectors read of current track 當前磁道中已讀扇區數 head: .word 0 ! current head 當前磁頭號 track: .word 0 ! current track 當前磁道號 read_it: mov ax,es test ax,#0x0fff die: jne die ! es must be at 64kB boundary xor bx,bx ! bx is starting address within segment rp_read: mov ax,es cmp ax,#ENDSEG ! have we loaded all yet? jb ok1_read ret ok1_read: seg cs mov ax,sectors sub ax,sread mov cx,ax shl cx,#9 add cx,bx jnc ok2_read je ok2_read xor ax,ax sub ax,bx shr ax,#9 ok2_read: call read_track mov cx,ax add ax,sread seg cs cmp ax,sectors jne ok3_read mov ax,#1 sub ax,head jne ok4_read inc track ok4_read: mov head,ax xor ax,ax ok3_read: mov sread,ax shl cx,#9 add bx,cx jnc rp_read mov ax,es add ax,#0x1000 mov es,ax xor bx,bx jmp rp_read read_track: push ax push bx push cx push dx mov dx,track mov cx,sread inc cx mov ch,dl mov dx,head mov dh,dl mov dl,#0 and dx,#0x0100 mov ah,#2 int 0x13 jc bad_rt pop dx pop cx pop bx pop ax ret bad_rt: mov ax,#0 mov dx,#0 int 0x13 pop dx pop cx pop bx pop ax jmp read_track /* * This procedure turns off the floppy drive motor, so * that we enter the kernel in a known state, and * don‘t have to worry about it later. */ kill_motor: push dx mov dx,#0x3f2 !軟驅控制卡的數字輸出寄存器(DOR)端口,只寫 mov al,#0 !A驅動器,關閉FDC,禁止DMA和中斷請求,關閉馬達。 outb !將al中的內容輸出到dx指定的端口。 pop dx ret sectors: .word 0 !存放當前啟動軟盤每磁道的存儲扇區數。 msg1: !調用BIOS中斷顯示的信息 .byte 13,10 !回車,換行的ASCⅡ碼 .ascii "Loading system ..." .byte 13,10,13,10 !共24個ASCⅡ碼字符 .org 508 !表示語句從地址508(0x01fc)開始,所以root_dev在啟動扇區的第508開始的2個字節中。 root_dev: .word ROOT_DEV !這裏存放根文件系統所在設備號(init/main.c 中會用到) boot_flag: .word 0xAA55 !啟動盤具有有效引導扇區的標誌,僅供BIOS中程序加載引導扇區時識別使用。 !它必須位於引導扇區的最後兩個字節中。 .text endtext: .data enddata: .bss endbss:

Linux內核0.11 bootsect文件說明