About USB Disk Drives

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.
darklight
Posts: 11
Joined: Mon Oct 15, 2007 3:12 am

About USB Disk Drives

Post by darklight »

Hi, everybody... I have a couple of questions

First I need to boot from usb. But I don't know what number of device I have to pass to INT 0x13 func?
And second... When I was looking at hex dump of my flash drive I calculated that in the BPB section is Specified
Heads: 255
Sectors per track: 63
And It has totally 1981440 sectors... BUT

1981440 mod 255 * 63 != 0 !!!!!!!!!!!!!!!!

Please tell me WHY?

P.S. Sorry for my English :(
User avatar
AJ
Member
Member
Posts: 2646
Joined: Sun Oct 22, 2006 7:01 am
Location: Devon, UK
Contact:

Post by AJ »

Hi,

For disk access, pass the value specified in dl when the BIOS passes control to your boot loader.

As for the other thing, I'm not quite sure what you're getting at (not that I'm an expert on USB devices), but should the values actually be 256 and 64 (remember that you count from index zero in a lot of cases)? Also, how many tracks are there? Would that help to discover the source of the error. Can all tracks on a USB device be accessed using CHS? Perhaps there is just a discrepancy between CHS and LBA...

Cheers,
Adam
User avatar
JAAman
Member
Member
Posts: 879
Joined: Wed Oct 27, 2004 11:00 pm
Location: WA

Post by JAAman »

there are 3 pieces...

cylinders (number of tracks-per-head)
heads (number of read heads)
sectors/track (a track is heads * cylinders)

as for your numbers, thats quite expected -- CHS is not natural for use (CHS values havent been real physical values on hard drives since the days of the 400MB drives -- and not always then either), so when actually using the USB drive under an OS, you will want to use LBA instead of CHS -- the number of sectors on the drive do not divide evenly -- meaning there are some tracks that are not used completely -- not unexpected when using translated CHS

as for the drive number, it will probably be drive 0 (though it doesnt have to be...)
but should the values actually be 256 and 64 (remember that you count from index zero in a lot of cases)?
no it shouldnt -- heads are counted from 0, but the original BIOS (and some versions of DOS) use head 0xFF for special purpose, so that number will cause problems with some BIOS code
Also, how many tracks are there? Would that help to discover the source of the error.
irrelevant, as that only tells how many tracks there are -- i would however, check to see if C*H*S is greater or less than total sectors... as that will tell you if you are allowed to access the whole disk -- just an incomplete last cylinder or if you just cannot access the last part of the disk (either is possible, though i would guess that most of the time, it will cut off the end of the disk rather than allowing invalid addresses) -- this is one of the many weaknesses of the CHS system
darklight
Posts: 11
Joined: Mon Oct 15, 2007 3:12 am

Post by darklight »

Thanks guys, you've probably helped me ))))
User avatar
Dex
Member
Member
Posts: 1444
Joined: Fri Jan 27, 2006 12:00 am
Contact:

Post by Dex »

First you need to see what your BIOS can boot from, some use floppy emulation others hdd etc, if it users floppy emulation you need to use a bootable floppy image and write it to the drive using winimage etc.
You may need to partion the drive.

Let say your usb fob is fat16 and your BIOS users floppy emulation, then it will not boot on the PC, without adding a floppy image.

Here are some links that may help
http://menuet.2.forumer.com/index.php?s ... =1112&st=0
http://board.flatassembler.net/topic.php?p=51973#51973

PS: I found the best way to boot from usb fobs is to format it and click add sys files under windows, this will boot to Dos which if your OS can boot from Dos, you can add to the autoexe.bat .
NOTE: This for now, but i will when i have time try and sort through the usb boot mess.
darklight
Posts: 11
Joined: Mon Oct 15, 2007 3:12 am

Post by darklight »

Hi, Dex.. I have successfuly booted from usb, but when I saw look at the hex dump of my flash drive there was a partition table and a partition. In the first sector of drive was some windows bootloader. I don't know what it actually does, but I suppose that it is loading the first sector of first partition. Then I replaced the first sector of drive and booted from it. Then I needed to load the first sector of partition and then load the kernel. But I couldn't do it, because I don't know what method is using for addressing (lba or chs). I have tried both... but without success.
Today I filled with zeros the whole disk and then format it under fat16. I noticed that know there isn't partition table. The main loader is in 1st sector of drive. I have replaced it with mine, but I can't load my kernel.
I don't know what cylinder, head, and sector to pass int 0x13.

Any ideas? Welcome...
User avatar
Dex
Member
Member
Posts: 1444
Joined: Fri Jan 27, 2006 12:00 am
Contact:

Post by Dex »

This is what i would do, first with the usb plugged into your PC goto your BIOS setup and see what, it says in the boot order.
See if it say USB floppy or usb hdd.
Which ether it does, go here: http://alexfru.chat.ru/epm.html
and get "bootprog" use ether the fat12 or fat16 depending on how your BIOS is emulating, along with the simple test kernel that come with it.
Then let us know it any of thoughs work.
darklight
Posts: 11
Joined: Mon Oct 15, 2007 3:12 am

Post by darklight »

I'm using Acer Aspire 3690 series laptop with PhoenixBIOS.

During POST bios writes USB Flash Disk. In boot order section are USB HDD, USB FDD, USB KEY(?). My Flash Disk is recognized as USB Key. I don't know what it means. And I suppose that bios simulates my disk as HDD, because It can load a partitioned disk.

I don't know how many cylinders, heads and sectors my disk has. How can I know that? From partition table I know that 1st partition starts at cylinder 0, head 1, sector 1. Here is dump of partition table:

Code: Select all

00 00 00 00 00 00 00 00 00 00 00 00 00 00 80 01 
01 00 06 40 E0 C7 20 00 00 00 E0 3B 1E 00 00 00
And here is the code of my bootsector:

Code: Select all

use16
org 0x7C00
boot:	jmp		near start
	  nop

start:
		mov		ax, 0x03
		int		   0x10
		
		.reset_drive:
		mov		ah, 0
		int		   0x13
		or		  ah, ah
                jnz		  .reset_drive
		
		mov		ax, 0x7C00
		mov		es, ax
		mov		bx, 0x1000
		
		mov		ah, 0x02
		mov		al, 0x01
		mov		ch, 0x00               ; Cylinder = 0
		mov		cl, 0x01                ; Sector = 1
		mov		dh, 0x01               ; Head = 1
		int		   0x13
		
		or	 	  ah, ah
		jnz		.reset_drive
        
		jmp		0x0000:0x1000
		
		jmp		$
I'm replacing only first some bytes with my bootsector.
This code is loading to memory without any problems(it is working, displaying some text that I removed from code). But after that my pc hangs.

My bootsector in first partition only must display some text.

Please help me!!! I don't know what I need to do!
User avatar
Dex
Member
Member
Posts: 1444
Joined: Fri Jan 27, 2006 12:00 am
Contact:

Post by Dex »

Now you need to test whats at the offset (446 ) eg : 80h, if it not 80h its not a bootable partion, but if it is, put the WORD what at that address into DX, then add 2 to the pointer and put the WORD whats at that address into CX, now set the the int 13h pointer to point to a buffer layed out as a BPB for Fat16.
and call your int 13h read function.
Now you can use the info from the BPB to load the rest of your kernel etc.

NOTE: Also it's a good idea to save the DWORD thats at offset 454 has that the start cluster of the start partition, You will need that.
darklight
Posts: 11
Joined: Mon Oct 15, 2007 3:12 am

Post by darklight »

Sorry Dex, but I think that you didn't understand the problem.

I've already understanded what you said.

The problem is in INT 13h AH=0x02 function.
I know that my first partition starting at cylinder 0, head 1 and sector 1. I knew that from hex dump of my partition table. BUT I don't know is my bootsector code loading right sector to memory.

And sorry for off topic. Please tell me the difference of tracks and cylinders )))
User avatar
JAAman
Member
Member
Posts: 879
Joined: Wed Oct 27, 2004 11:00 pm
Location: WA

Post by JAAman »

Please tell me the difference of tracks and cylinders
a cylinder can be divided into tracks:

a track is a single head of a cylinder -- when you divide the geometry, a cylider is the first division, which is a portion of the disk which passes under the head (all heads are connected and move together, so all heads always point to the same cylinder) without moving the head -- the portion of a cylinder which is on a single head, is a track, so when the disk is read, first you read each sector on a track, then switch heads and read all heads, then move the heads to the next cylinder

basically, you can think of it as coordinates -- if the head is x, and the cylinder is y, the the track is (x,y)
darklight
Posts: 11
Joined: Mon Oct 15, 2007 3:12 am

Post by darklight »

OK, Dex. I've got! But you didn't answered my question. Is int 13h using CHS? And whence I can learn how many cylinders, heads, heads per cylinder, tracks per head, sectors per track has my flash disk???? (probably I need a program for that)
User avatar
JAAman
Member
Member
Posts: 879
Joined: Wed Oct 27, 2004 11:00 pm
Location: WA

Post by JAAman »

thats a trick question... the answer is none of the above... since your device doesnt have any of those things, however the bios emulation will simulate them

if you use usb=FDD, then the BIOS will probably do the same as a cd in floppy emulation mode, and load an image from the device into memory, and treat it as a 1.44 FDD boot device

if you use usb=HDD, then you can (and should) use LBA with the extensions to the BIOS disk calls -- i cant remember off the top of my head what they are though

if you use usb=usb, then its probably more like cd native booting -- your 'boot sector' is larger than 512 bytes, but it needs to know how to read the disk itself

so most likely what you want is USB=HDD, then use the LBA BIOS calls through the HDD read interface (i cant remember which int it is though... shouldnt be hard to find though)

im guessing on some of this though, since i havent done usb booting myself
User avatar
Dex
Member
Member
Posts: 1444
Joined: Fri Jan 27, 2006 12:00 am
Contact:

Post by Dex »

darklight wrote:OK, Dex. I've got! But you didn't answered my question. Is int 13h using CHS? And whence I can learn how many cylinders, heads, heads per cylinder, tracks per head, sectors per track has my flash disk???? (probably I need a program for that)
Yes it users CHS, but i can not understand the problem ?, do you want to know the "how many cylinders, heads, heads per cylinder, tracks per head, sectors per track" for your USB fob if so, do as i pointed out above to this:

Code: Select all

BpbFat16:
bsJmp1			rb	3		; 0x00
bsOemName1		rb	8		; 0x03
bsFat121:
bpbBytesPerSector1	rw	1		; 0x0B
bpbSectorsPerCluster1	rb	1		; 0x0D
bpbReservedSectors1	rw	1		; 0x0E
bpbNumberOfFATs1	rb	1		; 0x10
bpbRootEntries1 	rw	1		; 0x11
bpbTotalSectors1	rw	1		; 0x13
bpbMedia1		rb	1		; 0x15
bpbSectorsPerFAT1	rw	1		; 0x16
bpbSectorsPerTrack1	rw	1		; 0x18
bpbHeadsPerCylinder1	rw	1		; 0x1A
bpbHiddenSectors1	rd	1		; 0x1C
bpbTotalSectorsBig1	rd	1		; 0x20

bpb32SectorsPerFAT	rd	1		; 0x24
bpb32Flags		rw	1		; 0x28
bpb32Version		rw	1		; 0x2A
bpb32RootCluster	rd	1		; 0x2C
bpb32InfoSector 	rw	1		; 0x30
bpb32BootBackupStart	rw	1		; 0x32
bpb32Reserved		rb	12		; 0x34

;bs32DriveNumber        rb      1               ; 0x40
bs32DriveNumber 	rb	1		; 0x40
bs32Unused		rb	1		; 0x41
bs32ExtBootSignature	rb	1		; 0x42
bs32SerialNumber	rd	1		; 0x43
bs32VolumeLabel 	rb	11		; 0x47
bs32FileSystem		rb	8		; 0x52
bs32BootCode		rb	422		; 0x5A
and then you have all the info you need, as theres no one fits all.
Or are you saying you need info as tuts ?, or that your code crash in that case show us your code.

LINKS
http://www.ata-atapi.com/hiwchs.htm
http://www.ata-atapi.com/hiwdos.htm
http://www.ata-atapi.com/hiwmbr.htm
User avatar
Candy
Member
Member
Posts: 3882
Joined: Tue Oct 17, 2006 11:33 pm
Location: Eindhoven

Post by Candy »

Dex wrote:Yes it users CHS, but i can not understand the problem ?, do you want to know the "how many cylinders, heads, heads per cylinder, tracks per head, sectors per track" for your USB fob if so, do as i pointed out above to this:
You only answer the subset where the USB disk is FAT16 formatted and not partitioned - half the disks I've had were partitioned and I've regularly formatted them with something else (ext3, ntfs, *fs) which all don't happen to have a BPB.

Any ideas on how to do it other than reading nonpresent information in a specific file system?
Post Reply