How to load things into second sector

Question about which tools to use, bugs, the best way to implement a function, etc should go here. Don't forget to see if your question is answered in the wiki first! When in doubt post here.
Post Reply
gabemaiberger
Member
Member
Posts: 40
Joined: Tue Nov 13, 2012 2:54 pm

How to load things into second sector

Post by gabemaiberger »

Hey I need help loading something to the second sector of a hard drive so my bootsector can see it. The bootsector code is:

Code: Select all

BITS 16
ORG 0x7C00
CPU 386
jmp START

OEM_ID db "PARADIGM"
BytesPerSector dw 0x0200
SectorsPerCluster db 0x09
ReservedSectors dw 0x0020
TotalFATs db 0x02
MaxRootEntries dw 0x00E0
TotalSectorsSmall dw 0x0000
MediaDescriptor db 0xF0
SectorsPerFAT dd 0x0000101F
SectorsPerTrack dw 0x003F
NumHeads dw 0x0002
HiddenSectors dd 0x0000003F
TotalSectorsLarge dd 0x001DD600
BigSectorsPerFAT dd 0x00000778
Flags dw 0x0000
FSVersion dw 0x0000
RootDirectoryStart dd 0x00000002
FSInfoSector dw 0x0001
BackupBootSector dw 0x0006
times 13 db 0x00
DriveNumber db 0x80
Signature db 0x29
VolumeID dd 0xFFFFFFFF
VolumeLabel db "PARADIGM_BOOT"
SystemID db "FAT32"

START:
cli
mov ax, 0x0000
mov ds, ax
mov es, ax
mov fs, ax
mov gs, ax
mov ax, 0x0000
mov ss, ax
mov sp, 0x0000
sti
mov si, msgLoading
call DisplayMessage
mov cx, WORD [SectorsPerCluster]
xor ax, ax
mov al, BYTE [TotalFATs]
mul WORD [BigSectorsPerFAT]
add ax, WORD [ReservedSectors]
mov WORD [datasector], ax
xor ax, ax
mov ax, WORD [RootDirectoryStart]
call ClusterLBA
mov bx, 0x0200
call ReadSectors
mov cx, WORD [0x0080]
mov di, 0x0200
.LOOP:
push cx
mov cx, 0x000C ;length of filename
mov si, ImageName ;move image name into si
push di
rep cmpsb
pop di
je LOAD_FILE
pop cx
add di, 0x0020
loop .LOOP
jmp FAILURE

LOAD_FILE:
mov si, msgCRLF
call DisplayMessage
mov dx, WORD [di + 0x001A]
mov WORD [cluster], dx
mov ax, 0x0100
mov es, ax
mov bx, 0
xor cx, cx
mov cl, BYTE[SectorsPerCluster]
mov ax, WORD[cluster]
call ClusterLBA
call ReadSectors
jmp DONE

DONE:
mov si, msgCRLF
call DisplayMessage
push WORD 0x8000
push WORD 0x0000
retf

FAILURE:
mov si, msgFailure
call DisplayMessage
mov ah, 0x00
int 0x16
int 0x19

DisplayMessage:
lodsb
or al, al
jz .DONE
mov ah, 0x0E
mov bh, 0x00
mov bl, 0x07
int 0x10
jmp DisplayMessage
.DONE:
ret

ReadSectors:
.MAIN:
mov di, 5 ;5 tries total
.SECTORLOOP:
push ax
push bx
push cx
call LBACHS
mov ah, 0x02 ;function read sectors
mov al, 0x01 ;read 1 sector
mov ch, BYTE [absoluteTrack]
mov cl, BYTE [absoluteSector]
mov dh, BYTE [absoluteHead]
mov dl, BYTE [DriveNumber]
int 0x13 ;read disk
jnc .SUCCESS
xor ah, ah ;clear out ah
int 0x13 ;reset disk
dec di
pop cx
pop bx
pop ax
jnz .SECTORLOOP
int 0x18
.SUCCESS:
mov si, msgProgress
call DisplayMessage
pop cx
pop bx
pop ax
add bx, WORD [BytesPerSector]
inc ax
loop .MAIN
ret

ClusterLBA:
sub ax, 0x0002
xor cx, cx
mov cl, BYTE [SectorsPerCluster]
mul cx
add ax, WORD [datasector]
ret

LBACHS:
xor dx, dx ;clear out dx
div WORD [SectorsPerTrack]
inc dl
mov BYTE [absoluteSector], dl
xor dx, dx
div WORD [NumHeads]
mov BYTE [absoluteHead], dl
mov BYTE [absoluteTrack], al
ret

absoluteSector db 0x00
absoluteHead db 0x00
absoluteTrack db 0x00

datasector dw 0x0000
cluster dw 0x0000
ImageName dw "BOOTLOAD   BIN"
msgLoading db 0x0D, 0x0A, "Loading Boot Image", 0x0D, 0x0A, 0x00
msgCRLF db 0x0D, 0x0A, 0x00
msgProgress db ".", 0x00
msgFailure db 0x0D, 0x0A, "ERROR: Press Any Key to Reboot", 0x00

times 510-($-$$) db 0
dw 0xAA55
Thanks a lot!
mich
Posts: 18
Joined: Sun Nov 11, 2012 5:36 pm

Re: How to load things into second sector

Post by mich »

On a *nix system - assuming boot.bin is the flat binary of the code you posted and second.bin is the flat binary of code you want to be place at the second sector on the drive /dev/sdb - you'd do.

Code: Select all

cat boot.bin second.bin >> image.bin
dd if=image.bin of=/dev/sdb
gabemaiberger
Member
Member
Posts: 40
Joined: Tue Nov 13, 2012 2:54 pm

Re: How to load things into second sector

Post by gabemaiberger »

Thanks!

Code: Select all

var myCat="marshmallow"
Post Reply