Advertisement
Guest User

Untitled

a guest
Feb 25th, 2018
91
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1.     BITS 16
  2.  
  3.     jmp short bootloader_start 
  4.     nop            
  5.  
  6.  
  7.  
  8. OEMLabel        db "KAYLABOOT" 
  9. BytesPerSector      dw 512     
  10. SectorsPerCluster   db 1       
  11. ReservedForBoot     dw 1       
  12. NumberOfFats        db 2       
  13. RootDirEntries      dw 224     
  14. LogicalSectors      dw 2880    
  15. MediumByte      db 0F0h    
  16. SectorsPerFat       dw 9       
  17. SectorsPerTrack     dw 18      
  18. Sides           dw 2       
  19. HiddenSectors       dd 0       
  20. LargeSectors        dd 0       
  21. DriveNo         dw 0       
  22. Signature       db 41  
  23. VolumeID        dd 00000000h   
  24. VolumeLabel     db "KaylaOS";
  25. FileSystem      db "FAT12   "  
  26.  
  27.  
  28. ; ------------------------------------------------------------------
  29. ; Main bootloader code
  30.  
  31. bootloader_start:
  32.     mov ax, 07C0h          
  33.     add ax, 544        
  34.     cli            
  35.     mov ss, ax
  36.     mov sp, 4096
  37.     sti            
  38.  
  39.     mov ax, 07C0h          
  40.     mov ds, ax
  41.  
  42.  
  43.     cmp dl, 0
  44.     je no_change
  45.     mov [bootdev], dl  
  46.     mov ah, 8          
  47.     int 13h
  48.     jc fatal_disk_error
  49.     and cx, 3Fh        
  50.     mov [SectorsPerTrack], cx  
  51.     movzx dx, dh       
  52.     add dx, 1          
  53.     mov [Sides], dx
  54.  
  55. no_change:
  56.     mov eax, 0         
  57.  
  58.  
  59. floppy_ok:             
  60.     mov ax, 19         
  61.     call l2hts
  62.  
  63.     mov si, buffer     
  64.     mov bx, ds
  65.     mov es, bx
  66.     mov bx, si
  67.  
  68.     mov ah, 2      
  69.     mov al, 14         
  70.  
  71.     pusha          
  72.  
  73.  
  74. read_root_dir:
  75.     popa           
  76.     pusha
  77.  
  78.     stc        
  79.     int 13h            
  80.  
  81.     jnc search_dir     
  82.     call reset_floppy      
  83.     jnc read_root_dir      
  84.  
  85.     jmp reboot         
  86.  
  87.  
  88. search_dir:
  89.     popa
  90.  
  91.     mov ax, ds         
  92.     mov es, ax         
  93.     mov di, buffer
  94.  
  95.     mov cx, word [RootDirEntries]  
  96.     mov ax, 0          
  97.  
  98.  
  99. next_root_entry:
  100.     xchg cx, dx        
  101.  
  102.     mov si, kern_filename      
  103.     mov cx, 11
  104.     rep cmpsb
  105.     je found_file_to_load  
  106.  
  107.     add ax, 32         
  108.  
  109.     mov di, buffer         
  110.     add di, ax
  111.  
  112.     xchg dx, cx        
  113.     loop next_root_entry
  114.  
  115.     mov si, file_not_found     
  116.     call print_string
  117.     jmp reboot
  118.  
  119.  
  120. found_file_to_load:        
  121.     mov ax, word [es:di+0Fh]   
  122.     mov word [cluster], ax
  123.  
  124.     mov ax, 1          
  125.     call l2hts
  126.  
  127.     mov di, buffer         
  128.     mov bx, di
  129.  
  130.     mov ah, 2          
  131.     mov al, 9          
  132.  
  133.     pusha              
  134.  
  135.  
  136. read_fat:
  137.     popa               
  138.     pusha
  139.  
  140.     stc
  141.     int 13h        
  142.  
  143.     jnc read_fat_ok        
  144.     call reset_floppy  
  145.     jnc read_fat           
  146.  
  147. ; ******************************************************************
  148. fatal_disk_error:
  149. ; ******************************************************************
  150.     mov si, disk_error     
  151.     call print_string
  152.     jmp reboot     
  153.  
  154.  
  155. read_fat_ok:
  156.     popa
  157.  
  158.     mov ax, 2000h      
  159.     mov es, ax
  160.     mov bx, 0
  161.  
  162.     mov ah, 2      
  163.     mov al, 1
  164.  
  165.     push ax            
  166.  
  167.  
  168. load_file_sector:
  169.     mov ax, word [cluster]     
  170.     add ax, 31
  171.  
  172.     call l2hts         
  173.  
  174.     mov ax, 2000h          
  175.     mov es, ax
  176.     mov bx, word [pointer]
  177.  
  178.     pop ax         
  179.     push ax
  180.  
  181.     stc
  182.     int 13h
  183.  
  184.     jnc calculate_next_cluster 
  185.  
  186.     call reset_floppy      
  187.     jmp load_file_sector
  188.  
  189.  
  190. calculate_next_cluster:
  191.     mov ax, [cluster]
  192.     mov dx, 0
  193.     mov bx, 3
  194.     mul bx
  195.     mov bx, 2
  196.     div bx             
  197.     mov si, buffer
  198.     add si, ax         
  199.     mov ax, word [ds:si]
  200.  
  201.     or dx, dx          
  202.  
  203.     jz even        
  204.  
  205. odd:
  206.     shr ax, 4          
  207.     jmp short next_cluster_cont
  208.  
  209.  
  210. even:
  211.     and ax, 0FFFh          
  212.  
  213.  
  214. next_cluster_cont:
  215.     mov word [cluster], ax     
  216.  
  217.     cmp ax, 0FF8h          
  218.     jae end
  219.  
  220.     add word [pointer], 512    
  221.     jmp load_file_sector
  222.  
  223.  
  224. end:                   
  225.     pop ax             
  226.     mov dl, byte [bootdev]     
  227.  
  228.     jmp 2000h:0000h        
  229.  
  230.  
  231. ; ------------------------------------------------------------------
  232. ; BOOTLOADER SUBROUTINES
  233.  
  234. reboot:
  235.     mov ax, 0
  236.     int 16h            
  237.     mov ax, 0
  238.     int 19h            
  239.  
  240.  
  241. print_string:              
  242.     pusha
  243.  
  244.     mov ah, 0Eh        
  245.  
  246. .repeat:
  247.     lodsb          
  248.     cmp al, 0
  249.     je .done           
  250.     int 10h            
  251.     jmp short .repeat
  252.  
  253. .done:
  254.     popa
  255.     ret
  256.  
  257.  
  258. reset_floppy:      
  259.     push ax
  260.     push dx
  261.     mov ax, 0
  262.     mov dl, byte [bootdev]
  263.     stc
  264.     int 13h
  265.     pop dx
  266.     pop ax
  267.     ret
  268.  
  269.  
  270. l2hts:         
  271.            
  272.     push bx
  273.     push ax
  274.  
  275.     mov bx, ax         
  276.  
  277.     mov dx, 0          
  278.     div word [SectorsPerTrack]
  279.     add dl, 01h        
  280.     mov cl, dl         
  281.     mov ax, bx
  282.  
  283.     mov dx, 0          
  284.     div word [SectorsPerTrack]
  285.     mov dx, 0
  286.     div word [Sides]
  287.     mov dh, dl         
  288.     mov ch, al         
  289.  
  290.     pop ax
  291.     pop bx
  292.  
  293.     mov dl, byte [bootdev]     
  294.  
  295.     ret
  296.  
  297.  
  298. ; ------------------------------------------------------------------
  299. ; STRINGS AND VARIABLES
  300.  
  301.     kern_filename   db "KERNEL  BIN"   
  302.  
  303.     disk_error  db "Floppy error! Press any key...", 0
  304.     file_not_found  db "KERNEL.BIN not found!", 0
  305.  
  306.     bootdev     db 0    
  307.     cluster     dw 0    
  308.     pointer     dw 0    
  309.  
  310.  
  311. ; ------------------------------------------------------------------
  312. ; END OF BOOT SECTOR AND BUFFER START
  313.  
  314.     times 510-($-$$) db 0   ;
  315.     dw 0AA55h      
  316.  
  317.  
  318. buffer:            
  319.  
  320.  
  321. ; ==================================================================
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement