Guest User

Untitled

a guest
Nov 24th, 2012
287
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. [BITS 16]
  2. [ORG 0x7C00]
  3.  
  4. ;BIOS loads code to 0x0000:0x7C00, but CS is not necessarily 0x7C00/16 = 0x7C0
  5. ;So, jump to a known address: CS=0x0000 and IP=0x7C00 and set up a new stack immediately
  6. jmp  0x0000:main
  7.  
  8. %include "../asm_helpers/print.asm"
  9. %include "print_level1.asm"
  10.  
  11. ; PRINT 3 is a macro that prints given a string, text color, and background color
  12. %macro PRINT1 3  ; print a level 1 text (i.e. something like "Bootloader level 1: <text>")
  13.     call   print_level1
  14.     PRINT  %1, %2, %3
  15. %endmacro
  16. %macro PRINTI 1  ; print an info line (i.e. PRINT1, but without having to specify a dark grey color)
  17.     mov   esi, %1
  18.     call  print_info
  19. %endmacro
  20. %macro FAIL 2  ; a failure condition--print an error and hang
  21.     mov  esi, %2
  22.     %1   fail
  23. %endmacro
  24.  
  25. main:
  26.     ;Setup segment registers
  27.     ;   Setup stack
  28.     mov  ax, 0x0000
  29.     mov  ss, ax
  30.     mov  sp, 0x7C00 ; Stack grows down from offset 0x7C00 toward 0x0000.
  31.     ;   Setup ES to point to text video memory
  32.     mov  ax, 0xB800
  33.     mov  es, ax
  34.     ;   The DS should be 0x0000.  Apparently it is by default, but set it to that just to be sure.
  35.     mov  ax, 0x0000
  36.     mov  ds, ax
  37.  
  38.     mov  [bootdisk], dl  ; boot drive number given by the bios in dl
  39.  
  40.     ;Print splash info
  41.     PRINT  str_ss_1, 0x0, 0x2
  42.  
  43.     CURSOR_ADD_Y 2
  44.  
  45.     ;Enable A20 line
  46.     mov   ax, 0x2401     ; 0x2400 to disable
  47.     int   0x15
  48.     FAIL  jc, str_f_a20
  49.  
  50.     ;Load kernel
  51.     PRINTI  str_lk_1
  52.     ;   Check support INT13h extensions for LBA addressing
  53.     mov     ah, 0x41
  54.     mov     bx, 0x55AA
  55.     mov     dl, 0x80
  56.     int     0x13
  57.     FAIL    jc, str_f_int13
  58.     PRINTI  str_lk_2
  59.     ;   Do the transfer.  See http://en.wikipedia.org/wiki/INT_13H#INT_13h_AH.3D42h:_Extended_Read_Sectors_From_Drive
  60.     mov     si, DAP
  61.     mov     ah, 0x42
  62.     mov     dl, [bootdisk]
  63.     PRINTI  str_lk_3
  64.     int     0x13
  65.     PRINTI  str_lk_4
  66.     FAIL    jc, str_f_load
  67.  
  68.     ;Kernel returned
  69.     FAIL    jmp, str_f_kr
  70.  
  71. print_info:
  72.     push  esi
  73.     call  print_level1
  74.     pop   esi
  75.     SET_COLOR_L(0x8,0x0)
  76.     call  print_string
  77.     ret
  78. fail:
  79.     push  esi
  80.     call  print_level1
  81.     pop   esi
  82.     SET_COLOR_L(0x4,0x0)
  83.     call  print_string
  84.     jmp   $
  85.  
  86. DAP:            ; Disk Address Packet
  87.     ;Size of DAP
  88.     db  0x10
  89.     ;Unused
  90.     db  0
  91.     ;Number of sectors to read
  92.     dw  1       ; int 13 resets this to # of blocks actually read/written
  93.     ;Offset and segment of destination
  94.     ;db  0x00    ; offset
  95.     ;db  0x7C
  96.     ;db  0x00    ; segment
  97.     ;db  0x00
  98.     dw  0x8000  ; offset
  99.     dw  0x0000  ; segment
  100.     ;LBA of start
  101.     dd  1       ; lower 4 bytes
  102.     dd  0       ; upper 4 bytes
  103.  
  104. bootdisk  db 0
  105.  
  106. ;ASCII 13 is \r, ASCII 10 is \n
  107. str_ss_1  db "Bootloader 1",13,10,"Ian Mallett",13,10,0
  108.  
  109. str_lk_1  db "load level 2:",13,10,0
  110. str_lk_2  db "INT13 supported!",13,10,0
  111. str_lk_3  db "load start:",13,10,0
  112. str_lk_4  db "load done!",13,10,0
  113.  
  114. str_f_kr     db "kernel returned!",0
  115. str_f_a20    db "no A20!",0
  116. str_f_int13  db "no INT13!",0
  117. str_f_load   db "load failed!",0
  118.  
  119. ;Fill the rest of sector with 0
  120. TIMES  510 - ($ - $$) db 0
  121.  
  122. ;Add boot signature at the end of bootloader
  123. DW 0xAA55
Advertisement
Add Comment
Please, Sign In to add comment