Bootloader - USB Flash drive FAT32 reading problems

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.
makuc9
Member
Member
Posts: 33
Joined: Sun Mar 03, 2013 3:27 pm

Bootloader - USB Flash drive FAT32 reading problems

Post by makuc9 »

Hello, everyone.
I have a few questions regarding my bootloader, that I've put together.

Code: Select all

BITS 16
ORG 0x7C00

jmp	Skip
nop

;****************************************;
; ----- OEM Parameter block, FAT32 ----- ;
;****************************************;

BS_OEMName		db	"MSDOS5.0"	; Must be 8 bytes

; ----- FAT32 Bios Parameter Block ----- ;
BPB_BytsPerSec		dw	512
BPB_SecPerClus		db	8
BPB_RsvdSecCnt		dw	496
BPB_NumFATs			db	2
BPB_RootEntCnt		dw	0
BPB_TotSec16		dw	0
BPB_Media			db	248
BPB_FATSz16			dw	0
BPB_SecPerTrk		dw	6
BPB_NumHeads		dw	255
BPB_HiddSec			dd	128	; lHiddenSecs
BPB_TotSec32		dd	3948288

;*******************************************;
; ----- Extended BIOS Parameter Block ----- ;
;*******************************************;

BPB_FATSz32			dd	3848
BPB_ExtFlags		dw	0	; Bits 0-3 = Active FAT, 7 = !FAT
BPB_FSVer			dw	0
BPB_RootClus		dd	2
BPB_FSInfo			dw	1
BPB_BkBootSec		dw	6
; Next three values are represented as "BPB_Reserved" - 12bytes in length
BytesPerCluster		dd	0	;Reserved 1
CurrentCluster		dd	0	;Reserved 2
lDataSector			dd	0	;Reserved 3
;END Re-check
BS_DrvNum			db	128
BS_Reserved1		db	0
BS_BootSig			db	41
BS_VolID			dd	304085088
BS_VolLab			db	"NO NAME    "
BS_FilSysType		db	"COPYHERE"


; Booting process
Skip:
; ----- Adjust registers ----- ;
	cli
	xor ax, ax
	mov ds, ax
	mov es, ax
	mov fs, ax
	mov gs, ax
; Clears the screen!
	mov ah, 0Fh
	int 10h
	mov ah, 00h
	int 10h
; ----- Display load message ----- ;
	mov si, msgStart
	call print_s
	call print_nl
	
	; Calculate start of DATA region - FirstDataSector
	mov ax, [BPB_NumFATs]
	mul WORD[BPB_FATSz32]
	add ax, [BPB_RsvdSecCnt]
	; RootDirSectors on FAT32 is always 0! No need, to include it in calculations ...
	mov WORD[FirstDataSector], ax	; Stores calculated value to FirstDataSector for later use
	call LBACHS						; Run function to calculate CHS for disk reading
	
	; print calculated values
	; prints cylinder
	mov si, msgCylinder
	call print_s
	mov ax, WORD[aCylinder]
	call print_int
	call print_nl
	; prints head
	mov si, msgHead
	call print_s
	xor ax, ax
	mov al, BYTE[aHead]
	call print_int
	call print_nl
	; prints track
	mov si, msgTrack
	call print_s
	xor ax, ax
	mov al, BYTE[aTrack]
	call print_int
	call print_nl
	; prints sector
	mov si, msgSector
	call print_s
	xor ax, ax
	mov al, BYTE[aSector]
	call print_int
	call print_nl
	
	
	; Reads a single testing sector from disk
	call dReset
	mov ax, 0x8c00		; We can't set es register directly
	mov es, ax			; Therefore we set it in ax first
	xor ax, ax
	xor bx, bx
	xor cx, cx
	xor dx, dx
	mov ah, 02h
	mov al, 1
	mov ch, BYTE[aTrack]
	mov cl, BYTE[aSector]
	mov dh, BYTE[aHead]
	mov dl, [BOOT_DRIVE]
	jc .error
	
	mov ax, [0x8c00]
	call print_int
	call print_nl
	
	; Print reboot message... Freeze the system.
	call reboot
	jmp $
	hlt

.error:
	mov si, napakaBranja
	call print_s
	call print_nl
	call reboot
	jmp $
	hlt
	
	napakaBranja db "Napaka branja!", 0

LBACHS:
	xor dx, dx						; prepare dx for division (has to be cleaned - remainder is stored here)
	div WORD [BPB_SecPerTrk]		; calculates  ax / SectorsPerTrack
	inc dl							; adjust for sector 0
	mov BYTE [aSector], dl			; store SectorToRead
	xor dx, dx						; prepare dx for another division
	div WORD [BPB_NumHeads]			; divides ax with number of heads
	mov BYTE [aHead], dl			; stores with which head to read choosen sector
	mov BYTE [aTrack], al			; stores on which track selected sector is located.
	xor dx, dx
	mov ax, [FirstDataSector]
	div WORD[BPB_NumHeads]
	xor dx, dx
	div WORD[BPB_SecPerTrk]
	mov WORD[aCylinder], ax
	ret
	
;*********************************
;*** Preveri kej je s headi!! ***
;*********************************
	
msgHead db "Head: ", 0
msgTrack db "Track: ", 0
msgSector db "Sector: ", 0
msgCylinder db "Cylinder: ",0
aHead db 0x00
aTrack db 0x00
aSector db 0x00
aCylinder dw 0x00
;**********************************;
; ----- Predefined functions ----- ;
;**********************************;
; Printing functions
print_s:
	lodsb			; get next character
	or al, al		; check, if null-terminator
	jz f_end		; yes? -> printing done
	call print_char	; Print character function
	jmp print_s		; repeat until null-terminator found

print_nl:
	mov si, msgNewLine
	call print_s
	ret

print_int:			; Breaks the number in ax register for printing
mov bx,10
		xor cx,cx
	@a:
		xor dx,dx
		div bx
		inc cx
		push dx
		and ax,ax
		jnz short @a		; Jumps to previous @
	@b:
		pop ax
		add al,"0"
		call print_char
		loop @b				; Jumps to previous @
	ret


print_char:				; Function to print single character on screen
		mov ah, 0eh		; Prepare registers for character printing
		push bx
		xor bh, bh
		mov bl, 0fh
		int 10h			; Call BIOS interrupt
		pop bx
		ret

f_end:				; Return back upon function completion
	ret

; Other function
press_any_key:
	mov ah, 0	; wait for key
	int 016h
	ret
reboot:
	mov si, msgRestart
	call print_s
	call press_any_key
	db 0EAh		; Jump to FFFF:0000 (reboot)
	dw 0000h
	dw 0FFFFh

; Disk reading functions
dReset:
	mov ah, 0		; reset storage device
	mov dl, [BOOT_DRIVE]	; drive 0 is active storage
	int 0x13			; call BIOS reset interrupt
	jc dReset			; If CF (carry flag) carries error, try to reset again.
	ret

; ----- Data ----- ;
BOOT_DRIVE db 80h ; 80h for 1st HDD, 00h for 1st floppy

msgStart db "Boot sequence started...", 0x00
msgRestart db "Reboot...", 0x00
msgProgress db ".", 0x00
msgNewLine db 0Dh, 0Ah
FirstDataSector dw 0x0000

   
TIMES 510-($-$$) db 0
DW 0xAA55
Why do I keep printing number 0? Letter 'T' isn't number 0, it is represented by number 84. Is there something wrong with my functions? For now it is only supposed to print first byte of datasector of FAT32 data table for me to see if reading is working... Which should be 'T' (if I check it with HxD editor).

Well, I'm lost. Any help appreciated.
User avatar
Combuster
Member
Member
Posts: 9301
Joined: Wed Oct 18, 2006 3:45 am
Libera.chat IRC: [com]buster
Location: On the balcony, where I can actually keep 1½m distance
Contact:

Re: Bootloader - USB Flash drive FAT32 reading problems

Post by Combuster »

Well, it helps if you actually call the bios to read a sector.

That and I'm missing everything partition-table related.
"Certainly avoid yourself. He is a newbie and might not realize it. You'll hate his code deeply a few years down the road." - Sortie
[ My OS ] [ VDisk/SFS ]
makuc9
Member
Member
Posts: 33
Joined: Sun Mar 03, 2013 3:27 pm

Re: Bootloader - USB Flash drive FAT32 reading problems

Post by makuc9 »

Nothing, I'd just like to read them and print the first character ... to see if everything works.
User avatar
xenos
Member
Member
Posts: 1121
Joined: Thu Aug 11, 2005 11:00 pm
Libera.chat IRC: xenos1984
Location: Tartu, Estonia
Contact:

Re: Bootloader - USB Flash drive FAT32 reading problems

Post by xenos »

I guess Combuster is referring to this:
makuc9 wrote:

Code: Select all

	; Reads a single testing sector from disk
	call dReset
	mov ax, 0x8c00		; We can't set es register directly
	mov es, ax			; Therefore we set it in ax first
	xor ax, ax
	xor bx, bx
	xor cx, cx
	xor dx, dx
	mov ah, 02h
	mov al, 1
	mov ch, BYTE[aTrack]
	mov cl, BYTE[aSector]
	mov dh, BYTE[aHead]
	mov dl, [BOOT_DRIVE]
	jc .error
I guess there should be an int 0x13 before the last line? Just loading the registers with some parameters won't load you any data from the disk, unless you call the BIOS interrupt...
Programmers' Hardware Database // GitHub user: xenos1984; OS project: NOS
makuc9
Member
Member
Posts: 33
Joined: Sun Mar 03, 2013 3:27 pm

Re: Bootloader - USB Flash drive FAT32 reading problems

Post by makuc9 »

Yea, I see it (thanks for telling me) ... Still unsure how that happened, but I did put it in there now (I've just been testing when you, XenOS replied, but I'm glad I put it in the same spot) and it is still not working. Any more thoughts? (No error messages are shown)
Mikemk
Member
Member
Posts: 409
Joined: Sat Oct 22, 2011 12:27 pm

Re: Bootloader - USB Flash drive FAT32 reading problems

Post by Mikemk »

you are reading cylinder 40, head 82, sector 4.
you should be reading cylinder 41, head 0, sector 4.
your math is wrong.
Programming is 80% Math, 20% Grammar, and 10% Creativity <--- Do not make fun of my joke!
If you're new, check this out.
makuc9
Member
Member
Posts: 33
Joined: Sun Mar 03, 2013 3:27 pm

Re: Bootloader - USB Flash drive FAT32 reading problems

Post by makuc9 »

Actually, when I run it it says I am reading:
Cylinder: 0
Head: 130
Track: 0
Sector: 3
Am I really that off?
Mikemk
Member
Member
Posts: 409
Joined: Sat Oct 22, 2011 12:27 pm

Re: Bootloader - USB Flash drive FAT32 reading problems

Post by Mikemk »

I traced it with a calculator through the LBA part It's really messy Also, I think you might be passing the wrong LBA into LBACHS.
Programming is 80% Math, 20% Grammar, and 10% Creativity <--- Do not make fun of my joke!
If you're new, check this out.
makuc9
Member
Member
Posts: 33
Joined: Sun Mar 03, 2013 3:27 pm

Re: Bootloader - USB Flash drive FAT32 reading problems

Post by makuc9 »

I did thought about this possiblity myself, but with all my reading I just couldn't get anywhere. But, however, I've also read that USB can't have multiple partitions, therefore I figured this might be the reason ...

I am actually running it with VM VirtualBox. And on real hardware (which is where i am actually trying the reading process ... I don't really like all the complications required to run from USB in virtual, altough I will have to in the future...

Do you have any suggestions? Updated functions, maybe? I think I am getting lost here... I am stuck in the same spot for a couple of days already... But I still don't want to give up!
Mikemk
Member
Member
Posts: 409
Joined: Sat Oct 22, 2011 12:27 pm

Re: Bootloader - USB Flash drive FAT32 reading problems

Post by Mikemk »

Are you sure you are booting from drive 80? I've heard most bioses pass the boot drive in dl, so try printing that to the screen.
Programming is 80% Math, 20% Grammar, and 10% Creativity <--- Do not make fun of my joke!
If you're new, check this out.
makuc9
Member
Member
Posts: 33
Joined: Sun Mar 03, 2013 3:27 pm

Re: Bootloader - USB Flash drive FAT32 reading problems

Post by makuc9 »

Well, I just tested what you said, but I don't really think it is working... Anyway, if I change it to anything else, I get an error (disk reset -> doesn't even get to disk read). Any more thoughts? Anything is useful ...
Mikemk
Member
Member
Posts: 409
Joined: Sat Oct 22, 2011 12:27 pm

Re: Bootloader - USB Flash drive FAT32 reading problems

Post by Mikemk »

Run it through bochs in debugging mode.
Programming is 80% Math, 20% Grammar, and 10% Creativity <--- Do not make fun of my joke!
If you're new, check this out.
User avatar
DavidCooper
Member
Member
Posts: 1150
Joined: Wed Oct 27, 2010 4:53 pm
Location: Scotland

Re: Bootloader - USB Flash drive FAT32 reading problems

Post by DavidCooper »

Just took a look through your code - lots of places for mistakes to mess it up, primarily because you're using CHS (which you could avoid).
BPB_SecPerTrk dw 6
Is that likely? I suspect it should be 63.
makuc9 wrote:I've also read that USB can't have multiple partitions, therefore I figured this might be the reason ...
The number of partitions shouldn't be an issue.
I am actually running it with VM VirtualBox. And on real hardware (which is where i am actually trying the reading process ... I don't really like all the complications required to run from USB in virtual, altough I will have to in the future...
QEMU makes it easy to boot from USB - there are no complications.
Do you have any suggestions? Updated functions, maybe? I think I am getting lost here... I am stuck in the same spot for a couple of days already... But I still don't want to give up!
Debug it. Recalculate and check your numbers. See if the sector you're trying to load is in the right place on the flash drive. Open the physical drive in a hex editor, find the sector you want to load and then write down its sector number LBA. Post that value here along with the CHS values you're seeing in registers just before calling the BIOS to do the loading. If they match up properly, the right sector will load.
Help the people of Laos by liking - https://www.facebook.com/TheSBInitiative/?ref=py_c

MSB-OS: http://www.magicschoolbook.com/computing/os-project - direct machine code programming
makuc9
Member
Member
Posts: 33
Joined: Sun Mar 03, 2013 3:27 pm

Re: Bootloader - USB Flash drive FAT32 reading problems

Post by makuc9 »

I always wanted to avoid CHS. I tried the extended functions, but my real hardware reported they are unsupported (I do believe, however, that this is because of USB ... It is quite old and it's default filesystem is FAT(only) - for which i believe means FAT16 - in windows "right click -> format" utility. Altough it is 2GB in size). (with extended function I meant the ah=42h function in int 13h BIOS interrupt). My PC is only 3-4 years old, that's why I doub that it is PC's fault. But I might have to try to look for BIOS update, just in case and I might want to test it in a newer machine in the future. If you have any link that will help me with avoiding CHS, I'd be very happy, too.

You were right about the BPB_SecPerTrk, however. It is 63, but the mistake was only made here (I was making some minor adjustments to the code once I pasted it here ... some comments removal and experimental asm calls I've been doing for testing what's wrong).

By running it in virtual environment I got value 8192, which is the same as the one in HxD (if I navigate there ... I did it with the find function in HxD waay before I even implemented integer printing function to the bootloader, because I also thought that there might be something wrong here. But than I thought that maybe this address is not the absolute one and that there is another mistake. There has to be another mistake. Maybe in LBA to CHS conversion or something...)

I am still working on it and I still have a few experiments in mind to test them (I've started everything from scratch - except printing to screen functions), just in case. Will post some more soon, hopefully tomorrow. Any new response appreciated. And thank you DavidCooper. I'll have to take a closer look at some of your suggestions. Will also post some more response tomorrow about that, if I'll have time. And your new responses are very welcome.
User avatar
DavidCooper
Member
Member
Posts: 1150
Joined: Wed Oct 27, 2010 4:53 pm
Location: Scotland

Re: Bootloader - USB Flash drive FAT32 reading problems

Post by DavidCooper »

makuc9 wrote:I always wanted to avoid CHS. I tried the extended functions, but my real hardware reported they are unsupported (I do believe, however, that this is because of USB ... It is quite old and it's default filesystem is FAT(only) - for which i believe means FAT16 - in windows "right click -> format" utility. Altough it is 2GB in size). (with extended function I meant the ah=42h function in int 13h BIOS interrupt). My PC is only 3-4 years old, that's why I doub that it is PC's fault. But I might have to try to look for BIOS update, just in case and I might want to test it in a newer machine in the future. If you have any link that will help me with avoiding CHS, I'd be very happy, too.
It might be worth checking to see if the ah=42h function is available again just in case your code was faulty the last time you tested it. Apart from ancient 486s, I've only worked with machines made in the last four to six years, and all of them support the extensions with USB drives, but maybe that's just down to luck, so with your machine you might well be stuck with CHS (and an 8GB addressable range) - I hope that isn't the case as it's very limiting.
By running it in virtual environment I got value 8192, which is the same as the one in HxD (if I navigate there ... I did it with the find function in HxD waay before I even implemented integer printing function to the bootloader, because I also thought that there might be something wrong here. But than I thought that maybe this address is not the absolute one and that there is another mistake. There has to be another mistake. Maybe in LBA to CHS conversion or something...)
8192 translates to track 0, head 82h, sector 3...that's the same as what you reported earlier:-
Actually, when I run it it says I am reading:
Cylinder: 0
Head: 130
Track: 0
Sector: 3
82h = 128 + 2. So, it really ought to work, unless you're making the mistake of opening the drive in HxD as a logical drive instead of as a physical drive. If it isn't that, can you provide a copy of your bootsector code - I'd like to run it through a debugger to see exactly what it's getting up to. Just copy the bootsector straight out of HxD and post it here.
Help the people of Laos by liking - https://www.facebook.com/TheSBInitiative/?ref=py_c

MSB-OS: http://www.magicschoolbook.com/computing/os-project - direct machine code programming
Post Reply