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.
User avatar
bluemoon
Member
Member
Posts: 1761
Joined: Wed Dec 01, 2010 3:41 am
Location: Hong Kong

Re: Bootloader - USB Flash drive FAT32 reading problems

Post by bluemoon »

USB does not naturally support CHS addressing mode, so if an BIOS is decent enough to support USB HDD, it's very odd that it does not work with LBA; I would doubt if CHS translation is otherwise working.
makuc9
Member
Member
Posts: 33
Joined: Sun Mar 03, 2013 3:27 pm

Re: Bootloader - USB Flash drive FAT32 reading problems

Post by makuc9 »

Hmmm, I was opening it as a logical disk and not physical (funny, or sadly, but last time I was looking I couldn't find the same strings using Find function in HxD ... I guess I was really careless. :? ). This is the problem than, I guess. So this means I still have to figure out how to discover location of logical first sector on a physical drive. Or is it always 128? But than how about multiple partitions? I guess I am just discovering what a noob I am. :(

And I though I was getting hang of assembly... :(

Thanks for your responses, specially you DavidCooper. I finally feel like I am getting somewhere again.

Now I'll try re-implement the direct LBA adressing (mostly because of what bluemoon said). Of course I'll start with interrupt 41h. Will keep you posted.
Mikemk
Member
Member
Posts: 409
Joined: Sat Oct 22, 2011 12:27 pm

Re: Bootloader - USB Flash drive FAT32 reading problems

Post by Mikemk »

makuc9 wrote:Or is it always 128?
No, it's always 1. Make sense?
But than how about multiple partitions?
Partitions are just represented by the data stored on the disk, they aren't physical separations.
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 »

makuc9 wrote:Hmmm, I was opening it as a logical disk and not physical
That catches out a lot of people, including me when I first used HxD. We take it in turns here to be the first to suggest it as the potential cause of the problem in a thread.
So this means I still have to figure out how to discover location of logical first sector on a physical drive. Or is it always 128? But than how about multiple partitions? I guess I am just discovering what a noob I am. :(
The logical first sector is the one pointed to by the first entry of the partition table found in sector 0 of the physical drive. Read up on MBR and more importantly the partition table to find out how HxD determines where the logical drive begins.
And I though I was getting hang of assembly... :(
You're better at it than I am. I've spotted something else in your code though:-
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
With 8c00 in ES it's going to load the sector in sixteen times higher up memory than the location where you later look to see if it loaded in.
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've went through microsoft's "FAT: General Overview of On-Disk Format" again ('Boot Sector and BPB Structure' chapter) and I think I've found the problem in determing location of actual address of ROOT directory. I should add BPB_HiddSec to LBA calculation. Now I'll have to run a few more test.

I've also reimplemented 41h function of BIOS int 13h to check for extension support (I've run it on another PC, newer - only 1 year old - and it passed the test. I'll have to re-check support on previous PC to make sure, but for now I am OK with this). I've also finally found a perfect way of running USB on VirtualBox, if anyone is interested, check http://www.rmprepusb.com/tutorials/how- ... are-server.

Now I am back to rebuilding direct LBA read function.

David, I don't think I really understood what you meant with:
"With 8c00 in ES it's going to load the sector in sixteen times higher up memory than the location where you later look to see if it loaded in."
I loaded it in 8c00h and I read what was stored at address 8c00h and simply print that. No jumps or anything and I should be able to access 1MB of memory from address I was loaded in, right? Or did I missed something else, too? (Since what I printed was always 0, I believe this makes sense ... 8192 sector is zeroed ... But than again, I might be wrong.)
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:David, I don't think I really understood what you meant with:
"With 8c00 in ES it's going to load the sector in sixteen times higher up memory than the location where you later look to see if it loaded in."
If you put 8c00 in ax and then mov it into ES and couple that with a BX containing zero to give the BIOS an address to load the sector to, that gives you the address 8c000, and that's a little over half way up the first megabyte of memory in the machine. When you then read the byte at 8c00, that's a byte located a little over half way up the first 64K segment of memory in the machine, 16 times lower down than the location the sector was loaded to. Well, it's either that or my mind's gone (and I'm always open to that possibility).
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 »

Hmmm, I am not really sure anymore, but I just tested all of the new changes that I have made (I completed direct LBA reading function) and it works. I successfully read the disk and printed the RIGHT character.

I always thought that addressing works like this: ES+BX, where set segment to a fixed value and in use offset only to increase the range (in case I read multiple sectors, so I have to read it to a different location - ES+512)...
I mean i have always zeroed one and used only one for addressing ... everything seems easier this way. Am I really wrong? Than how can this work as I want it to?

PS:
Thank you all for helping me fix bootloader and made me see my mistake. I am really grateful.
User avatar
JAAman
Member
Member
Posts: 879
Joined: Wed Oct 27, 2004 11:00 pm
Location: WA

Re: Bootloader - USB Flash drive FAT32 reading problems

Post by JAAman »

makuc9 wrote:Hmmm, I am not really sure anymore, but I just tested all of the new changes that I have made (I completed direct LBA reading function) and it works. I successfully read the disk and printed the RIGHT character.

I always thought that addressing works like this: ES+BX, where set segment to a fixed value and in use offset only to increase the range (in case I read multiple sectors, so I have to read it to a different location - ES+512)...
I mean i have always zeroed one and used only one for addressing ... everything seems easier this way. Am I really wrong? Than how can this work as I want it to?

PS:
Thank you all for helping me fix bootloader and made me see my mistake. I am really grateful.
no, BIOS loads the sector to ES:BX not ES+BX (think about it for a minute, if it was the later, how would you load data higher than 128k?)

in RMode addressing, segment is multiplied by 0x10 (16) and added to offset, so basically where BIOS is loading to is ES*16+BX

therefore, in your example ES= 0x8C00, BX=0
address ==
0x8C00 (segment register << 4 (multiplied by 16)
+0x0000 (offset added)
------------
0x8C000 (resulting physical address)

you will therefore have to set DS to 0x7C01 or higher to access the value (or use a seg override to use ES) -- most probably load DS=0x8C00 and then access address 0 (the same way you loaded it)
makuc9
Member
Member
Posts: 33
Joined: Sun Mar 03, 2013 3:27 pm

Re: Bootloader - USB Flash drive FAT32 reading problems

Post by makuc9 »

Hmm, thanks. That helped. i guess I'll have to take a closer look to all of this, than. :D
Post Reply