Advertisement
Guest User

2-Stage x86 bootloader with boot loop

a guest
Nov 7th, 2012
369
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. =====[stage1.asm]==================================================
  2.  
  3. [BITS 16]    ;16-bit real mode
  4. [ORG 0x7C00] ;program origin
  5.  
  6. JMP start
  7.  
  8. msg_halt  DB "HALT0",0x00
  9. msg_ok    DB "OK",0x0D,0x0A,0x00
  10. msg_fail  DB "FAIL",0x0D,0x0A,0x00
  11. msg_reset DB "Resetting Disk...",0x00
  12. msg_read  DB "Reading Sector...",0x00
  13. msg_st1   DB "Welcome to stage 1",0x0D,0x0A,0x00
  14. msg_exit  DB "Now leaving stage 1",0x0D,0x0A,0x0D,0x0A,0x00
  15.  
  16. currentDrive DB 0x00 ;Drive number
  17. ;---------------
  18. ;Halt the system
  19. ;---------------
  20. fn_halt:
  21.     MOV SI,msg_halt
  22.     CALL fn_print
  23.   _halt:
  24.     CLI
  25.     HLT
  26.     JMP _halt
  27.  
  28. ;---------------------------------------------------------
  29. ;Print null-terminated string with address in SI to screen
  30. ;---------------------------------------------------------
  31. fn_print:
  32.     PUSH AX
  33.     PUSH BX
  34.   _print:
  35.     LODSB
  36.     OR AL,AL
  37.     JZ _printDone
  38.     MOV AH,0x0E
  39.     MOV BH,0x00
  40.     MOV BL,0x0F
  41.     INT 0x10
  42.     JMP _print
  43.   _printDone:
  44.     POP BX
  45.     POP AX
  46.     RET
  47.  
  48. ;------------------------------
  49. ;Reset hdd back to first sector
  50. ;------------------------------
  51. fn_reset_disk:
  52.     PUSH AX
  53.     PUSH DX
  54.  
  55.     MOV SI,msg_reset
  56.     CALL fn_print
  57.  
  58.   _reset_disk:
  59.     MOV AH,0x00               ;Reset drive
  60.     MOV DL,BYTE[currentDrive] ;Drive number
  61.     INT 0x13                  ;Video BIOS interrupt
  62.     JC _reset_disk            ;Retry on fail
  63.  
  64.     MOV SI,msg_ok
  65.     CALL fn_print
  66.  
  67.     POP DX
  68.     POP AX
  69.     RET
  70.  
  71. ;--------------------------------------------
  72. ;Read a sector from the disk to 0x0500:0x0000
  73. ;--------------------------------------------
  74. fn_read_sector:
  75.     MOV DI,0x05
  76.     MOV AX,0x0500             ;Location of stage 2
  77.     MOV ES,AX                 ;Populate buffer secment
  78.     XOR BX,BX                 ;Clear buffer offset
  79.  
  80.     MOV SI,msg_read
  81.     CALL fn_print
  82.  
  83.   _try_read:
  84.     MOV AH,0x02               ;Read from drive
  85.     MOV AL,1                  ;How many sectors to read
  86.     MOV CH,0                  ;Track/Cylinder number
  87.     MOV CL,2                  ;Sector to read
  88.     MOV DH,0                  ;Head number
  89.     MOV DL,BYTE[currentDrive] ;Drive number
  90.     INT 0x13
  91.     JNC _read_done
  92.     DEC DI
  93.     CMP DI,0
  94.     JZ  _read_error
  95.     JMP _try_read
  96.   _read_error:
  97.     MOV SI,msg_fail
  98.     CALL fn_print
  99.     CALL fn_halt
  100.     CLI
  101.     HLT
  102.   _read_done:
  103.     MOV SI,msg_ok
  104.     CALL fn_print
  105.     RET
  106.  
  107. start:
  108.     MOV [currentDrive],dl ;Save drive number
  109.     CLI                   ;Disable interrupts while setting up the stack
  110.     XOR AX,AX             ;Real mode flat memory model
  111.     MOV DS,AX
  112.     MOV ES,AX
  113.     MOV FS,AX
  114.     MOV GS,AX
  115.     MOV SS,AX
  116.     MOV SP,0xFFFF
  117.     STI                   ;Enable interrupts
  118.  
  119.     MOV AH,0x00           ;Set video mode
  120.     MOV AL,0x12           ;640x480x8 @ 80x30
  121.     INT 0x10              ;Video BIOS interrupt
  122.  
  123.     MOV SI,msg_st1
  124.     CALL fn_print
  125.  
  126.     CALL fn_reset_disk
  127.     CALL fn_read_sector
  128.    
  129.     MOV SI,msg_exit
  130.     CALL fn_print
  131.     JMP  0x0500:0x0000
  132.    
  133.     CALL fn_halt
  134.  
  135.     CLI
  136.     HLT
  137.  
  138. TIMES 510-($-$$) HLT         ;Padding bytes
  139. DB    0x55, 0xAA             ;Mark bootable
  140.  
  141.  
  142.  
  143. =====[stage2.asm]==================================================
  144.  
  145. [BITS 16]    ;16-bit real mode
  146. [ORG 0x5000] ;program origin
  147.  
  148. JMP main
  149.  
  150. %include "gdt.inc" ;Global Descriptor Table definition
  151.  
  152. msg_halt  DB "HALT1",0x00
  153. msg_ok    DB "OK",0x0D,0x0A,0x00
  154. msg_st2   DB "Welcome to Stage 2",0x0D,0x0A,0x00
  155. msg_gdt   DB "Setting up GDT...",0x00
  156. msg_a20   DB "Enabling A20 Gate...",0x00
  157. msg_pm    DB "Entering Protected Mode...",0x00
  158.  
  159. ;---------------
  160. ;Halt the system
  161. ;---------------
  162. fn_halt:
  163.     MOV SI,msg_halt
  164.     CALL fn_print
  165.   _halt:
  166.     CLI
  167.     HLT
  168.     JMP _halt
  169.  
  170. ;---------------------------------------------------------
  171. ;Print null-terminated string with address in SI to screen
  172. ;---------------------------------------------------------
  173. fn_print:
  174.     PUSH AX
  175.     PUSH BX
  176.   _print:
  177.     LODSB
  178.     OR AL,AL
  179.     JZ _printDone
  180.     MOV AH,0x0E
  181.     MOV BH,0x00
  182.     MOV BL,0x0F
  183.     INT 0x10
  184.     JMP _print
  185.   _printDone:
  186.     POP BX
  187.     POP AX
  188.     RET
  189.    
  190. fn_enable_A20:
  191.   MOV AX,0x2401
  192.   INT 0x15
  193.   RET
  194.  
  195. main:
  196.     CLI                   ;Disable interrupts while setting up the stack
  197.     XOR AX,AX             ;Real mode flat memory model
  198.     MOV DS,AX
  199.     MOV ES,AX
  200.     MOV FS,AX
  201.     MOV GS,AX
  202.     MOV SS,AX
  203.     MOV SP,0xFFFF
  204.     STI                   ;Enable interrupts
  205.    
  206.     MOV SI,msg_st2
  207.     CALL fn_print
  208.  
  209.     MOV SI,msg_gdt
  210.     CALL fn_print
  211.     CALL fn_load_gdt
  212.     MOV SI,msg_ok
  213.     CALL fn_print
  214.  
  215.     MOV SI,msg_a20
  216.     CALL fn_print
  217.     CALL fn_enable_A20
  218.     MOV SI,msg_ok
  219.     CALL fn_print
  220.  
  221.     MOV SI,msg_pm
  222.     CALL fn_print
  223.     MOV EAX,CR0
  224.     OR  EAX,1
  225.     MOV CR0,EAX
  226.     MOV SI,msg_ok
  227.     CALL fn_print
  228.  
  229.     CALL fn_halt
  230.  
  231. TIMES 510-($-$$) HLT         ;Padding bytes
  232.  
  233.  
  234. =====[gdt.inc]==================================================
  235. fn_load_gdt:
  236.                 cli                             ;disable interrupts
  237.                 pusha                           ;save registers
  238.                 lgdt    [gdt_ptr]               ;load the gdt
  239.                 sti                             ;enable interrupts
  240.                 popa                            ;restore registers
  241.                 ret                             ;return
  242.  
  243. descriptor_table:
  244.         ;null descriptor
  245.                 db      0                       ;fill 8 bytes with zeros
  246.                 db      0
  247.                
  248.         ;code descriptor
  249.                 dw      0FFFFH                  ;limit low
  250.                 dw      0                       ;base low
  251.                 db      0                       ;base middle
  252.                 db      10011010b               ;access
  253.                 db      11001111b               ;granularity
  254.                 db      0                       ;base high
  255.                
  256.         ;data descriptor
  257.                 dw      0FFFFH                  ;limit low
  258.                 dw      0                       ;base low
  259.                 db      0                       ;base middle
  260.                 db      10010010b               ;access
  261.                 db      11001111b               ;granularity
  262.                 db      0                       ;base high
  263. end_table:
  264. gdt_ptr:
  265.                 dw      end_table - descriptor_table - 1                ;the sise of the descriptor table - 1
  266.                 dd      descriptor_table                                ;base of the GDT
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement