天天看点

grub-0.97源码分析(1)

grub-0.97源码分析(1)

#include <stage1.h>

#define ABS(x) (x-_start+0x7c00)    //计算物理地址,不依赖编译器  

#define MSG(x) movw $ABS(x), %si; call message  

#define MOV_MEM_TO_AL(x) .byte 0xa0;  .word x //这个不用管它,只是为了实现兼容性用了机器码

 .file "stage1.S"  .text  .code16 .globl _start; _start:

 jmp after_BPB

 nop   . = _start + 4            //跳过上面两条指令  

mode:

 .byte 0

disk_address_packet: 

sectors:

 .long 0

heads:

 .long 0

cylinders:

 .word 0

sector_start:

 .byte 0

head_start:

 .byte 0

cylinder_start:

 .word 0

   . = _start + STAGE1_BPBEND   stage1_version: 

 .byte COMPAT_VERSION_MAJOR, COMPAT_VERSION_MINOR

boot_drive: 

 .byte GRUB_INVALID_DRIVE 

force_lba:

 .byte 0

stage2_address:

 .word 0x8000

stage2_sector:

 .long 1

stage2_segment:

 .word 0x800 after_BPB:

 cli     boot_drive_check:   

 jmp 1f

 testb $0x80, %dl

 jnz 1f

 movb $0x80, %dl

1:   

 ljmp $0, $ABS(real_start)            real_start:   

 xorw %ax, %ax

 movw %ax, %ds

 movw %ax, %ss  

 movw $STAGE1_STACKSEG, %sp                        sti    

 MOV_MEM_TO_AL(ABS(boot_drive))    

 cmpb $GRUB_INVALID_DRIVE, %al    

 je 1f

 movb %al, %dl

1:

 pushw %dx                                      

 MSG(notification_string)  

 testb $STAGE1_BIOS_HD_FLAG, %dl    

 jz chs_mode

 movb $0x41, %ah         

 movw $0x55aa, %bx             

 int $0x13         

 popw %dx

 pushw %dx  

 jc chs_mode

 cmpw $0xaa55, %bx      

 jne chs_mode  

 MOV_MEM_TO_AL(ABS(force_lba)) 

 testb %al, %al

 jnz lba_mode

 andw $1, %cx                

 jz chs_mode

lba_mode:

 movl 0x10(%si), %ecx