Guest User

boot1.asm

a guest
Jul 22nd, 2014
321
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. bits 16
  2.  
  3. org 0x7c00
  4.  
  5. start: jmp main
  6.  
  7. ; OEM Parameter Block
  8.  
  9. bpbOEM db "irisOS  "
  10. ;TIMES 0Bh-$+start DB 0
  11.  
  12. bpbBytesPerSector: DW 512
  13. bpbSectorsPerCluster: DB 1
  14. bpbReservedSectors: DW 1
  15. bpbNumberOfFATs: DB 2
  16. bpbRootEntries: DW 224
  17. bpbTotalSectors: DW 2880
  18. bpbMedia: DB 0xF0
  19. bpbSectorsPerFAT: DW 9
  20. bpbSectorsPerTrack: DW 18
  21. bpbHeadsPerCylinder: DW 2
  22. bpbHiddenSectors: DD 0
  23. bpbTotalSectorsBig: DD 0
  24. bsDriveNumber: DB 0
  25. bsUnused: DB 0
  26. bsExtBootSignature: DB 0x29
  27. bsSerialNumber: DD 0xa0a1a2a3
  28. bsVolumeLabel: DB "MOS FLOPPY "
  29. bsFileSystem: DB "FAT12   "
  30.  
  31. ; Subroutines
  32.  
  33. Print:
  34.     lodsb
  35.     or al, al
  36.     jz PrintDone
  37.     mov ah, 0eh
  38.     int 10h
  39.     jmp Print
  40. PrintDone:
  41.     ret
  42.    
  43. ReadSectors:
  44.     .MAIN:
  45.         mov di, 0x0005
  46.     .SECTORLOOP:
  47.         push ax
  48.         push bx
  49.         push cx
  50.         call LBACHS
  51.         mov ah, 0x02
  52.         mov al, 0x01
  53.         mov ch, BYTE [absoluteTrack]
  54.         mov cl, BYTE [absoluteSector]
  55.         mov dh, BYTE [absoluteHead]
  56.         mov dl, BYTE [bsDriveNumber]
  57.         int 0x13
  58.         jnc .SUCCESS
  59.         xor ax, ax
  60.         int 0x13
  61.         dec di
  62.         pop cx
  63.         pop bx
  64.         pop ax
  65.         jnz .SECTORLOOP
  66.         int 0x18
  67.     .SUCCESS:
  68.         mov si, msgProgress
  69.         call Print
  70.         pop cx
  71.         pop bx
  72.         pop ax
  73.         add bx, WORD [bpbBytesPerSector]
  74.         inc ax
  75.         loop .MAIN
  76.         ret
  77.  
  78. ClusterLBA:
  79.     sub ax, 0x0002
  80.     xor cx, cx
  81.     mov cl, BYTE [bpbSectorsPerCluster]
  82.     mul cx
  83.     add ax, WORD [datasector]
  84.     ret
  85.    
  86. LBACHS:
  87.     xor dx, dx
  88.     div WORD [bpbSectorsPerTrack]
  89.     inc dl
  90.     mov BYTE [absoluteSector], dl
  91.     xor dx, dx
  92.     div WORD [bpbHeadsPerCylinder]
  93.     mov BYTE [absoluteHead], dl
  94.     mov BYTE [absoluteTrack], al
  95.     ret
  96.  
  97. ; Bootloader Entry
  98.    
  99. main:
  100.    
  101.     cli
  102.     mov ax, 0x07C0
  103.     mov ds, ax
  104.     mov es, ax
  105.     mov fs, ax
  106.     mov gs, ax
  107.    
  108.     mov ax, 0x0000
  109.     mov ss, ax
  110.     mov sp, 0xFFFF
  111.     sti
  112.    
  113.     mov si, msgLoading
  114.     call Print
  115.    
  116. LOAD_ROOT:
  117.  
  118.     xor cx, cx
  119.     xor dx, dx
  120.     mov ax, 0x0020
  121.     mul WORD [bpbRootEntries]
  122.     div WORD [bpbBytesPerSector]
  123.     xchg ax, cx
  124.    
  125.     mov al, BYTE [bpbNumberOfFATs]
  126.     mul WORD [bpbSectorsPerFAT]
  127.     add ax, WORD [bpbReservedSectors]
  128.     mov WORD [datasector], ax
  129.     add WORD [datasector], cx
  130.    
  131.     mov bx, 0x0200
  132.     call ReadSectors
  133.    
  134.     mov cx, WORD [bpbRootEntries]
  135.     mov di, 0x0200
  136.    
  137. .LOOP:
  138.     push cx
  139.     mov cx, 0x000B
  140.     mov si, ImageName
  141.     push di
  142. rep cmpsb
  143.     pop di
  144.     je LOAD_FAT
  145.     pop cx
  146.     add di, 0x0020
  147.     loop .LOOP
  148.     jmp FAILURE
  149.    
  150. LOAD_FAT:
  151.  
  152.     mov si, msgCRLF
  153.     call Print
  154.     mov dx, WORD [di + 0x001a]
  155.     mov WORD [cluster], dx
  156.    
  157.     xor ax, ax
  158.     mov al, BYTE [bpbNumberOfFATs]
  159.     mul WORD [bpbSectorsPerFAT]
  160.     mov cx, ax
  161.    
  162.     mov ax, WORD [bpbReservedSectors]
  163.    
  164.     mov bx, 0x0200
  165.     call ReadSectors
  166.    
  167.     mov si, msgCRLF
  168.     call Print
  169.     mov ax, 0x0050
  170.     mov es, ax
  171.     mov bx, 0x0000
  172.     push bx
  173.    
  174. LOAD_IMAGE:
  175.  
  176.     mov ax, WORD [cluster]
  177.     pop bx
  178.     call ClusterLBA
  179.     xor cx, cx
  180.     mov cl, BYTE [bpbSectorsPerCluster]
  181.     call ReadSectors
  182.     push bx
  183.    
  184.     mov ax, WORD [cluster]
  185.     mov cx, ax
  186.     mov dx, ax
  187.     shr dx, 0x0001
  188.     add cx, dx
  189.     mov bx, 0x0200
  190.     add bx, cx
  191.     mov dx, WORD [bx]
  192.     test ax, 0x0001
  193.     jnz .ODD_CLUSTER
  194.    
  195. .EVEN_CLUSTER:
  196.     and dx, 0000111111111111b
  197.     jmp .DONE
  198.  
  199. .ODD_CLUSTER:
  200.     shr dx, 0x0004
  201.    
  202. .DONE:
  203.     mov WORD [cluster], dx
  204.     cmp dx, 0x0FF0
  205.     jb LOAD_IMAGE
  206.    
  207. DONE:
  208.  
  209.     mov si, msgCRLF
  210.     call Print
  211.     push WORD 0x0050
  212.     push WORD 0x0000
  213.     retf
  214.    
  215. FAILURE:
  216.  
  217.     mov si, msgFailure
  218.     call Print
  219.     mov ah, 0x00
  220.     int 0x16
  221.     int 0x19
  222.    
  223. absoluteSector db 0x00
  224. absoluteHead db 0x00
  225. absoluteTrack db 0x00
  226.  
  227. datasector dw 0x0000
  228. cluster dw 0x0000
  229. ImageName db "KRNLDR  SYS"
  230. msgLoading db 0x0D, 0x0A, "Loading Boot Image ", 0x0D, 0x0A, 0x00
  231. msgCRLF db 0x0D, 0x0A, 0x00
  232. msgProgress db ".", 0x00
  233. msgFailure db 0x0D, 0x0A, "ERROR: Press any key to reboot", 0x0A, 0x00
  234.    
  235. times 510 - ($-$$) db 0
  236. dw 0xAA55
Advertisement
Add Comment
Please, Sign In to add comment