Page 1 of 1

how to read from USB flash memory using BIOS

Posted: Fri Nov 01, 2013 4:43 pm
by abdulmughn
Hi, every one
excuse me, I need your help . since a week, I've been trying to write a simple boot-loader that would read some sectors from USB drive rather than boot sector(assuming these sectors contain the kernel), into somewhere in memory then print a word to indicate that it have successfully loading the kernel . i know the BIOS routine that could help me to read from HD is 13h after setting the required parameters. i have tried this way to read from USB flash memory but it failed. i don't know what's the problem . bellow i accompany may code. any help from you would be appreciated .
org 0x7c00

entry:
; zero out all segments registers
xor ax, ax
mov ds, ax
mov es, ax
mov ss, ax
mov cs, ax
;setup the stack
mov bp,0x8000
mov sp,bp

mov [BOOT_DRIVE],dl

mov dh,2; number of sectors
mov bx,0x9000;es:bx is where the disk will be loaded
mov dl,[BOOT_DRIVE]

call diskLoad

mov bl,[0x9000]
call printHexa ;to print bl in order to ensure the correct loading
jmp $



diskLoad:

push dx
; prepare paramaters for " int 13h "
mov ah,0x02;
mov al,dh
mov ch,0x00
mov dh,0x00
mov cl,0x02
;call BIOS disk read interrupt
int 0x13
;check whether the disk got loaded
jc DISK_ERROR
pop dx
cmp dh,al
jne DISK_ERROR
ret

DISK_ERROR:
mov bx , DISK_ERROR_MSG
call printString
jmp $
BOOT_DRIVE db 0
DISK_ERROR_MSG db "Disk read error!",0
%include "diskload.asm"
%include "printHexa.asm"
%include "printString.asm"
;padding boot sector
size equ $ - entry
%if size+2 > 512
%error "code is too large for boot sector"
%endif
times (512 - size - 2) db 0
db 0x55, 0xAA
;fill the next two sectors
times 1024 db 0xAB[/color]

Re: how to read from USB flash memory using BIOS

Posted: Sat Nov 02, 2013 12:58 am
by egos
Use code tags.

Code: Select all

	 mov cs, ax
What architecture are you using?

Code: Select all

	              ;setup the stack
	 mov bp,0x8000
	 mov sp,bp
Stack too small.

Re: how to read from USB flash memory using BIOS

Posted: Sat Nov 02, 2013 4:00 am
by abdulmughn
thanks egos. my pc architecture is x86_64

Re: how to read from USB flash memory using BIOS

Posted: Sat Nov 02, 2013 7:37 am
by egos
It was sarcasm meaning that there is no such command.

Re: how to read from USB flash memory using BIOS

Posted: Sat Nov 02, 2013 1:34 pm
by M2004
1) As egos pointed out your stack may be a problem. Why not setting it to 0x7c00?

2) You should retry loading sector(s) a number of times before giving up.

3) Finally: Are you sure that your pc actually boots from usb device?
From my experience some quite modern pc's simply fail to boot from usb.

regards
M2004