How to load second stage boot loader from first stage?

前端 未结 3 1541
青春惊慌失措
青春惊慌失措 2020-11-30 09:24

I have written simple first stage bootloader which displays \"Hello world\" using interrupt to bios. Now as a next obvious step to write a second stage, but where code for t

3条回答
  •  执笔经年
    2020-11-30 09:51

    Look at the GRUB implementation here (stage 1):

    http://src.illumos.org/source/xref/illumos-gate/usr/src/grub/grub-0.97/stage1/stage1.S

    First noticed the starting point at 0x7c00 and the end signature of 0xaa55 for this first sector. From within the disassembly, u can see this:

    349 copy_buffer:
    350   movw    ABS(stage2_segment), %es
    351 
    352   /*
    353    * We need to save %cx and %si because the startup code in
    354    * stage2 uses them without initializing them.
    355    */
    356   pusha
    357   pushw   %ds
    358 
    359   movw    $0x100, %cx
    360   movw    %bx, %ds
    361   xorw    %si, %si
    362   xorw    %di, %di
    363 
    364   cld
    365 
    366   rep
    367   movsw
    368 
    369   popw    %ds
    370   popa
    371 
    372   /* boot stage2 */
    373   jmp *(stage2_address)
    374 
    375 /* END OF MAIN LOOP */
    376
    

    Essentially the logic is to copy the stage 2 code into another part of memory, and after that jump directly there, and that is "boot stage2". In other words, "boot stage1" is effectively triggered from BIOS after it has loaded the sector into memory, whereas stage2 is where you jump there - it can be anywhere.

提交回复
热议问题