Page 1 of 1

issues reading from disk image

Posted: Tue Oct 20, 2015 4:42 pm
by spectrum
Dear all,

have the following issue:
- i am in real mode
- i load a binary into memory, using ATA lba read, more or less same routine as here

http://wiki.osdev.org/ATA_read/write_sectors

It works perfect in bochs, while in virtualbox, if i read into memory i.e. 4096 bytes, only first 512 bytes, or maybe less (should dump) are read correctly, other bytes until 4096 are garbage.

Every help is really appreciated.
Regards
Angelo

Re: issues reading from disk image

Posted: Tue Oct 20, 2015 5:05 pm
by Kevin
This code doesn't seem to wait for set DRQ and cleared BSY after each sector, which it should be doing.

Re: issues reading from disk image

Posted: Wed Oct 21, 2015 1:20 pm
by spectrum
thanks,

well, i am reading multisector so i suppose waiting a single time for complete should be ok

Re: issues reading from disk image

Posted: Wed Oct 21, 2015 4:29 pm
by spectrum
Hi,

got the solution

Bochs is more flexible, or fast, or permissive. It allows, after READ cmd 0x20 has been issued, to read the whole "multi" sector data (so more than 512, in my case 4096).
Virtualbox no. It is more picky, or more as in IDE spec (that i can't find actually), and the wiki code i posted in first thread doesn't work.
Virtualbox wants you to re-check for DRQ bit before reading every sector data with the 256-len "rep insw".

I tried to add a note in wiki but couldn't login for too many wrong password attempts, so at least the thing is tracked here.

Below my final code, if someone is interested:

Code: Select all

static inline void _read_sect_lba(
	unsigned char *sbuff, uint32_t lba_address, size_t sectors)
{
	/*
	 * steps
	 *
	 * 1 - before any operation on the hd, we wait disk rdy
	 * 2 - setup address and issue read
	 * 3 - wait for DRQ bit to be set
	 * 4 - read (bochs-multi)sector data
	 *
	 * NOTE: bochs here allows to read whole multisector data
	 *       while virtualbox, more picky, want you wait DRQ for
	 *       each sector (loop to 3).
	 * 
	 * Functions stay modified for all emulators to work.
	 *
	 *
	 */
	__asm__ __volatile__ (
		"	and	$0xfffffff, %%rax	\n"
		"	mov	%%rax, %%rbx		\n"
		"	_d_rdy				\n"
		"	mov	$0x1f6, %%dx		\n"
		"	shr	$24, %%eax		\n"
		"	or	$0xe0, %%al		\n"
		"	out	%%al, %%dx		\n"
		"	mov	$0x1f2, %%dx		\n"
		"	mov	%%cl, %%al		\n"
		"	out	%%al, %%dx		\n"
		"	inc	%%dx			\n"
		"	mov	%%rbx, %%rax		\n"
		"	out	%%al, %%dx		\n"
		"	inc	%%dx			\n"
		"	mov	%%rbx, %%rax		\n"
		"	shr	$8, %%rax		\n"
		"	out	%%al, %%dx		\n"
		"	inc	%%dx			\n"
		"	mov	%%rbx, %%rax		\n"
		"	shr	$16, %%rax		\n"
		"	out	%%al, %%dx		\n"
		"	mov	$0x1f7, %%dx		\n"
		"	mov	$0x20, %%al		\n"
		"	out	%%al, %%dx		\n"
		"	mov	%%rcx, %%r8		\n"
		"check:					\n"
		"	_b_rdy				\n"
		"	mov	$256, %%rcx		\n"
		"	mov	$0x1f0, %%dx		\n"
		"	rep	insw			\n"
		"	dec	%%r8			\n"
		"	cmp	$0, %%r8		\n"
		"	jne	check			\n"
		:
		: "c"(sectors), "a"(lba_address), "D"(sbuff)
		: "%rdx"
	);
}
Bye and thanks

Re: issues reading from disk image

Posted: Wed Oct 21, 2015 4:31 pm
by Kevin
spectrum wrote:well, i am reading multisector so i suppose waiting a single time for complete should be ok
Feel free to suppose whatever you want, but the spec says otherwise.