My os works in qemu but not on real hardware

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.
Post Reply
computertrick
Member
Member
Posts: 71
Joined: Wed May 29, 2013 1:07 pm

My os works in qemu but not on real hardware

Post by computertrick »

I am running the operating system from a USB stick the code works fine in qemu but on real hardware their is problems. Here is my code

Code: Select all

org 0 ; we start everything at zero
	jmp short start
	nop     
MANU_DESC 			db 'mkdosfs '
BLOCK_SIZE	 		dw 512
BLOCKS_PER_ALLOCATION_UNIT 	db 128
RESERVED_BLOCKS			dw 1
TOTAL_FATS			db 1
TOTAL_ROOT_ENTRIES		dw 512
TOTAL_BLOCKS			dw 0xffff
MEDIA_DESCRIPTOR		db 0xf8
BLOCKS_PER_FAT			dw 0x01
BLOCKS_PER_TRACK		dw 18
TOTAL_HEADS			dw 0x02
HIDDEN_BLOCKS			dd 0x00
TOTAL_BLOCKS_2			dd 0x00
DRIVE_NUMBER			dw 0x00
EXTENDED_BOOT_SIGNATURE		db 0x29
VOLUME_SERIAL_NUMBER		dd 0x9d86f18c
VOLUME_LABEL			db 'SEAGULL    '
FILE_SYSTEM_IDENTIFIER		db 'FAT16   '

; Calculated in memory
ROOT_DIRECTORY		dd 0x00

DAPACK:
	db 0x10
	db 0
.len:	dw 1
.loc:	dd 0x7e00
.sec:	dd 2
	dd 0
	
start:
	cli
	mov ax, 0x7c0
	mov ds, ax
	mov es, ax
	mov sp, 0x1000
	mov bp, 0
	mov ss, bp
	sti

	mov byte [DRIVE], dl

; Root Directory Logical Sector =  (TOTAL_FATS * BLOCKS_PER_FAT) + (RESERVED_BLOCKS)

	mov ax, [TOTAL_FATS]
	mul word [BLOCKS_PER_FAT]
	mov word [ROOT_DIRECTORY+2], dx
	push dx
	add ax, [RESERVED_BLOCKS]
	mov word [ROOT_DIRECTORY], ax


; Load root directory


	mov word [DAPACK.sec], ax
	pop dx
	mov word [DAPACK.sec+2], dx

	mov si, DAPACK		; address of "disk address packet"
	mov ah, 0x42		; AL is unused
	mov dl, [DRIVE]		; drive number 0 (OR the drive # with 0x80)
	int 0x13
	jc err

	mov si, 512
	call print

	mov ah, 0eh
	mov al, 65
	int 10h

	mov si, 0x7e00
	call print

	mov ah, 0eh
	mov al, 65
	int 10h

	mov si, 0x7e0
	call print

	mov ah, 0eh
	mov al, 65
	int 10h


	mov si, 0
keyboard:
	mov ah, 00
	int 16h
	lodsb
	mov ah, 0eh
	int 10h
	jmp keyboard

err:
	mov si, ERROR
	call print
	jmp $


print:
	pusha
	mov ah, 0eh
.repeat:
	lodsb
	cmp al, 0
	je .done
	int 10h
	jmp .repeat
.done:
	popa
	ret

print_until:
	mov ah, 0eh
.repeat:
	lodsb
	cmp bx, 0
	je .done
	int 10h
	dec bx
	jmp .repeat
.done:
	popa
	ret

ERROR db "Failed to load sector", 10, 13, 0
DRIVE db 0

times 510-($-$$) db 0 ; fill in the remaining space with zero
dw 0xaa55 ; legacy boot signature
As you can see I have set up some tests e.g space bar reads current byte in memory then increments and I can say with certainty their is no data at 0x7e00. I thought maybe it was a segment issue so I tried a further tests e.g reading from 0x7e0, 0x7e00. I have the american megatrends bios. Can anybody see anything in this code that could cause it to run incorrectly I was thinking maybe its something to do with the way its emulating my USB stick but I'm not sure
1100110100010011
User avatar
DavidCooper
Member
Member
Posts: 1150
Joined: Wed Oct 27, 2010 4:53 pm
Location: Scotland

Re: My os works in qemu but not on real hardware

Post by DavidCooper »

I think it's a waste of time trying to set up a flash drive to pretend to be a floppy disk - you may eventually find a way to get it to work on one machine, but it may behave differently on another machine. Your BPB (which I assume is in the first sector on the disk rather than a partition) tells the BIOS it's pretending to be a floppy disk, but then you're using LBA to try to access it, and that may be something the BIOS can't do when emulating a floppy drive.

Change your plan now and save yourself a lot of trouble. Write your code for a hard disk (which is how a flash drive normally behaves) and put it in a partition, using an MBR in sector 0 to boot the VBR at the start of the partition, and your BPB and boot code will be at the start of the VBR with values appropriate for a hard disk, probably using FAT 32. You can use free space after the VBR to hold more code for loading your OS, or just develop an initial version of your OS in that space until you're ready to turn it into a proper file.
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