Advertisement
k10101110

Untitled

Aug 2nd, 2023
217
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
ASM (NASM) 1.77 KB | Source Code | 0 0
  1. [bits 64]
  2.  
  3. %define READ_PORT 0x1f0
  4. %define ATA_DRIVE_HEAD_PORT 0x1f6
  5. %define SECTOR_COUNT_PORT 0x1f2
  6. %define SECTOR_NUM_PORT 0x1f3
  7. %define CYLINDER_LOW_PORT 0x1f4
  8. %define CYLINDER_HIGH_PORT 0x1f5
  9. %define COMMAND_PORT 0x1f7
  10. %define STATUS_REGISTER 0x1f7
  11. %define SLAVE_BIT 0
  12.  
  13. read_ata_CHS:
  14.     ; selecting master drive
  15.     mov dx, ATA_DRIVE_HEAD_PORT
  16.     mov al, (0xA0 | (SLAVE_BIT << 4) | 0x0)  ; head number 0, slavebit is 0 to indicate selected drive is master
  17.     out dx, al
  18.  
  19.     ; waiting 400 ns by looping fifteen times?
  20.  
  21. .init_loop:
  22.     mov cx, 0xf
  23.     mov dx, STATUS_REGISTER
  24.  
  25. .wait:
  26.     in al, dx
  27.     cmp cx, 0
  28.     je .check_bsy
  29.     loop .wait
  30.  
  31. .check_bsy:
  32.     and al, 0b10000000
  33.     cmp al, 0
  34.     je drive_ready
  35.  
  36. drive_error:
  37.     hlt
  38.  
  39. drive_ready:
  40.     ; we know that memory upto 0x9200 (excluding upper limit) has been read in 1st stage loader
  41.     ; sectors 2 to 11 have been read (inclusive range), we need to read upto 63 sectors
  42.     ; hence we need to read 63 - 12 + 1 = 52 sectors to read from range (12, 63)
  43.  
  44.     mov dx, SECTOR_COUNT_PORT
  45.     mov al, 52
  46.  
  47.     out dx, al
  48.  
  49.     mov dx, SECTOR_NUM_PORT
  50.     mov al, 12
  51.  
  52.     out dx, al
  53.  
  54.     mov dx, CYLINDER_LOW_PORT
  55.     xor al, al
  56.  
  57.     out dx, al
  58.  
  59.     mov dx, CYLINDER_HIGH_PORT
  60.     xor al, al
  61.  
  62.     out dx, al
  63.  
  64.     mov dx, COMMAND_PORT
  65.     mov al, 0x20
  66.  
  67.     out dx, al  ; sending read sectors command
  68.  
  69. .check_data_transfer:
  70.     in al, dx
  71.     shr al, 3                   ; checking if DRQ bit is set
  72.     and al, 1
  73.     cmp al, 0
  74.     je .check_data_transfer
  75.  
  76.  
  77.     mov rax, 512/2  ; 256 words
  78.     xor bx, bx
  79.     mov bx, 52
  80.     mul bx  ; 256 * 52 words to read
  81.     mov rcx, rax
  82.     mov rdi, 0x9200 ; reading data to [0x9200]
  83.     mov rdx, READ_PORT
  84.     rep insw
  85.  
  86.     jmp $
  87.  
  88.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement