! ! 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 ! 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. ;bootset 分三部分加载系统 ;1.先加载磁盘512个字节 ;2.再加载后4个扇区 (setup.S) ;3.再加载随后的240个字节到内存(sys) .globl begtext, begdata, begbss, endtext, enddata, endbss .text begtext: .data begdata: .bss begbss: .text SETUPLEN = 4 ! nr of setup-sectors ;BIOS会自动的把磁盘第一块加载到0x7c00处 BOOTSEG = 0x07c0 ! original address of boot-sector INITSEG = 0x9000 ! we move boot here - out of the way SETUPSEG = 0x9020 ! setup starts here setup.s加载到此内存区段 SYSSEG = 0x1000 ! system loaded at 0x10000 (65536). ENDSEG = SYSSEG + SYSSIZE ! where to stop loading 内核的末尾 ! ROOT_DEV: 0x000 - same type of floppy as boot. ! 0x301 - first partition on first drive etc ;当时Linus存放跟文件系统是第二块软盘 第一个分区 所以为0x306 ;计算方法 设备号=主设备号*256+次设备号 ;0x306=774=3(3代表硬盘)*256+6(因为此前有第一个磁盘的4个主分区) ROOT_DEV = 0x306 !ds:si es:di entry start start: ;从0x7c00拷贝数据到0x9000 mov ax,#BOOTSEG mov ds,ax mov ax,#INITSEG mov es,ax mov cx,#256 sub si,si sub di,di rep ; movw ;这两句是老汇编中的 不要深究 jmpi go,INITSEG ;设置ax,ds,es等相关寄存器的数值 go: mov ax,cs mov ds,ax mov es,ax ! put stack at 0x9ff00. mov ss,ax mov sp,#0xFF00 ! arbitrary value >>512 ! load the setup-sectors directly after the bootblock. ! Note that 'es' is already set up. ////////////////////////////////////第二步///////////////////////////////////////////// load_setup: ;功能02H ;功能描述:读扇区 ;入口参数:AH=02H ;AL=扇区数 ;CH=柱面 ;CL=扇区 ;DH=磁头 ;DL=驱动器,00H~7FH:软盘;80H~0FFH:硬盘 ;ES:BX=缓冲区的地址 ;出口参数:CF=0——操作成功,AH=00H,AL=传输的扇区数,否则,AH=状态代码 mov dx,#0x0000 ! drive 0, head 0 mov cx,#0x0002 ! sector 2, track 0 mov bx,#0x0200 ! address = 512, in INITSEG 在int 0x13中断中es:bx代表的是数据缓冲区 ;即setup要拷贝的内存段 mov ax,#0x0200+SETUPLEN ! service 2(读操作), nr of sectors int 0x13 ! read it jnc ok_load_setup ! ok - continue jump not CF ;如果CF位有置位那么出错 处理错误 mov dx,#0x0000 mov ax,#0x0000 ! reset the diskette int 0x13 j load_setup ;jump ok_load_setup: ! Get disk drive parameters, specifically nr of sectors/track ;功能08H ;功能描述:读取驱动器参数 ;入口参数:AH=08H ;出口参数:CF=1——操作失败,AH=状态代码,否则, ;BL=01H — 360K ; =02H — 1.2M ; =03H — 720K ; =04H — 1.44M ;CH=柱面数的低8位 ;CL的位7-6=柱面数的该2位 ;CL的位5-0=扇区数 ;DH=磁头数 ;DL=驱动器数 ;ES:DI=磁盘驱动器参数表地址 ;DL=驱动器,00H~7FH:软盘;80H~0FFH:硬盘 mov dl,#0x00 ;调用前先清0 mov ax,#0x0800 ! AH=8 is get drive parameters int 0x13 mov ch,#0x00 ;因为软盘的磁道不能超过256所以cl的6-7位为0 又将ch置为0 ;则cx中表示每道扇区数 seg cs ;只影响下一条指令 cs:[sectors] mov sectors,cx mov ax,#INITSEG mov es,ax ;因为int 0x13中断改变了所以改回原先的值 ! Print some inane message ;int 0x10 ;功能03H ;功能描述:在文本坐标下,读取光标各种信息 ;入口参数:AH=03H ;BH=显示页码 ;出口参数:CH=光标的起始行 ;CL=光标的终止行 ;DH=行(Y坐标) ;DL=列(X坐标) mov ah,#0x03 ! read cursor pos xor bh,bh int 0x10 ;功能描述:在Teletype模式下显示字符串 ;入口参数:AH=13H ;BH=页码 ;BL=属性(若AL=00H或01H) ;CX=显示字符串长度 ;(DH、DL)=坐标(行、列) ;ES:BP=显示字符串的地址 AL=显示输出方式 ;0——字符串中只含显示字符,其显示属性在BL中。显示后,光标位置不变 ;1——字符串中只含显示字符,其显示属性在BL中。显示后,光标位置改变 ;2——字符串中含显示字符和显示属性。显示后,光标位置不变 ;3——字符串中含显示字符和显示属性。显示后,光标位置改变 ;出口参数:无 mov cx,#24 ;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 ! we want to load the system (at 0x10000) /////////////////////////////////////第三步////////////////////////////////////////// mov ax,#SYSSEG mov es,ax ! segment of 0x010000 call read_it 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 je root_defined mov ax,#0x021c ! /dev/PS0 - 1.44Mb cmp bx,#18 je root_defined undef_root: jmp undef_root root_defined: seg cs mov ,ax ! after that (everyting loaded), we jump to ! the setup-routine loaded directly after ! the bootblock: jmpi 0,SETUPSEG ! 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 ;0x1000&0xfff=0 不会进入die循环 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计算当前需要读的扇区 存放入ax中 ok1_read: seg cs mov ax,sectors ;之前通过中断获取的扇区数(int 0x13 08号功能存放在此的数据) sub ax,sread mov cx,ax shl cx,#9 ;总共读取的字节数放入cx中 add cx,bx jnc ok2_read ;操作没有超过64KB je ok2_read xor ax,ax ;否则 反转 sub ax,bx shr ax,#9 ;ok2_read的功能是开始读磁盘 ok2_read: call read_track ;调用int 0x13中断开始读磁盘 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: ;功能02H ;功能描述:读扇区 ;入口参数:AH=02H ;AL=扇区数 ;CH=磁道号 ;CL=扇区 ;DH=磁头 ;DL=驱动器,00H~7FH:软盘;80H~0FFH:硬盘 ;ES:BX=缓冲区的地址 ;出口参数:CF=0——操作成功,AH=00H,AL=传输的扇区数, ;否则,AH=状态代码,参见功能号01H中的说明 push ax push bx push cx push dx mov dx,track ;当前的磁道号 mov cx,sread ;当前已读扇区 注意:已读 (刚开始的时候是4+1) 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: ;功能0 磁盘系统复位 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 mov al,#0 outb pop dx ret sectors: .word 0 ;在这里代表的值是一个到的扇区数目 msg1: .byte 13,10 .ascii "Loading system ..." .byte 13,10,13,10 .org 508 root_dev: .word ROOT_DEV boot_flag: .word 0xAA55 .text endtext: .data enddata: .bss endbss: