Skip to content

Instantly share code, notes, and snippets.

@Theldus
Created December 13, 2016 04:31
Show Gist options
  • Save Theldus/4258b4a024d66b9f0cd272c57179e80b to your computer and use it in GitHub Desktop.
Save Theldus/4258b4a024d66b9f0cd272c57179e80b to your computer and use it in GitHub Desktop.
[BITS 16]
;###############################################################################
; -.- Initial Setup -.-
;###############################################################################
;Setup stack
mov ax, 07C0h
add ax, 20h
mov ss, ax
mov sp, 4096
;Backup drive number
push dx
;Setup data
mov ax,07C0h
mov ds,ax
;Just to known its working
mov si, trCpy
call print
; ## Copy myself ##
;Check if LBA is available
mov dl, [esp]
call check_lba
;Read the first sector
mov ax, 1 ;Read 1 sector
mov bx, chain_addr ;into chain_addr offset
mov cx, 0 ;by starting at sector 0
mov dl, [esp] ;drive number
call read_lba
;Jumps to my new location
jmp 0:addr_jump
; -- addr_jump is here --
checkpoint:
mov ax, 0x100
mov ds, ax
nop
;Loads the VBR to the right place
mov si, trVbr
call print
;Checks
mov dl, [esp]
call check_lba
;Read the first sector
mov ax, 1 ;Read 1 sector
mov bx, vbr_addr ;into vbr_addr offset
mov cx, 63 ;by starting at the first partition, 2048 win7 or 63 winxp
mov dl, [esp] ;drive number
call read_lba
;Iterative mode
mov si, pressKey
call print
mov ah, 0
int 0x16
;Jumps to the original VBR
pop dx
jmp 0:vbr_addr
inf:
jmp inf
;###############################################################################
; -.- Disk Utils -.-
;###############################################################################
;-----------------------------------------------------
;check_lba: Checks if LBA Extensions is Present
;dl: drive number
;-----------------------------------------------------
check_lba:
mov ah, 0x41
mov bx, 0x55AA
int 0x13
jnc .ret
mov si, lba_np
call print
jmp hang
.ret:
retn
;-----------------------------------------------------
;read_lba: Read sectors from boot media using LBA mode
;ax: number of sectors to read
;bx: offset
;cx: start sector
;dl: drive number
;-----------------------------------------------------
read_lba:
mov [blkcnt], ax
mov [db_add], bx
mov [d_lba] , cx
mov si, DAP
mov ah, 0x42
int 0x13
jnc .ret
.error:
mov si, lba_fail
call print
jmp hang
.ret:
retn
;###############################################################################
; -.- Debugging Utils -.-
;###############################################################################
;---------------------------------------
;print: Print a string to screen
;si: pointer to string
;---------------------------------------
print:
mov ah, 0xE
mov bx, 0x0007
.printchar:
lodsb
cmp al, 0
je .done
int 0x10
jmp .printchar
.done:
ret
;---------------------------------------
;halt: Does nothing
;---------------------------------------
hang:
jmp hang
;###############################################################################
; -.- Some data and constants -.-
;###############################################################################
trCpy db 'Copying myself...', 13, 10, 0
trVbr db 'Loading VBR to the right place!', 13, 10, 0
lba_fail db 'Failed while trying to reading using LBA!!!', 13, 10, 0
lba_np db 'LBA Extensions seems to be not present!!', 13, 10, 0
pressKey db 13, 10, 'Press any key to boot...', 13, 10, 0
vbr_addr equ 0x7C00
chain_addr equ 0x1000
addr_jump equ (checkpoint-$$)+chain_addr
;--------------------------------------
; -.- Disk Address Packet structure -.-
;--------------------------------------
DAP:
db 0x10 ;DAP Size
db 0 ;Unused
blkcnt: dw 1 ;Number of sectors to be read
db_add: dw 0x1000 ;offset
dw 0 ;segment
d_lba: dd 1 ;start sector, sector starts in 1
dd 0 ;start sector, dont need to be used
;Fill up the media
times 510-($-$$) db 0
db 0x55
db 0xAA
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment