Can't rune the simplest of bootloader on a real PC

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.
Szustarol
Posts: 20
Joined: Fri Mar 17, 2017 6:21 am

Can't rune the simplest of bootloader on a real PC

Post by Szustarol »

Hello.
I has been some time since I haven't been doing anything related to osdev, but I have decided to try again.
Now this is how my code looks like:

Code: Select all

org 0x7C00
bits 16
jmp 0x0000:start

;BIOS Parameter block:
times 0xB-($-$$) db 0

BPB:
	.bytesPerSector		dw 	512
	.sectorsPerCluster	db	1
	.reservedLogicalSectors dw	1
	.numberOfFATs		db	2
	.rootDirEntriesMax	dw	224
	.totalLogicalSectors	dw 	2880 ;assume 1.44MB floppy
	.mediaType		db	0xF0
	.sectorsPerFAT		dw	9
	.sectorsPerTrack	dw 	18
	.headsPerCylinder	dw	2
	.huddenSectorsCnt	dd	0
	.totalSectors		dd	80
	.driveNumber		db	0
	.flags			db	0
	.bootSignature		db	0x29
	.serialNumber		dd	0xa0a1a2a3
	.label			db	"MOS FLOPPY "
	.fstype			db	"FAT12   "

start:
	xor bx, bx
	mov ds, bx
	mov es, bx
	mov ah, 0x0e
	mov al, 'a'
	int 0x10
	jmp $
	cli
	hlt

times 446 - ($-$$) db 0
db 0x80
db 0x00;head
db 0x01;sector
db 0x00;cylinder
db 0x01
db 0x01, 0x02, 0x04
dd 0x00
dd 0x80
times 510-($-$$) db 0
dw 0xaa55
And the problem is, always, that it doesn't run on a real PC. Funny thing is, IT used to run ONCE (lucky shot) without those fancy BPB and MBR structures, and now It doesn't even print a single letter. I am appending 127 blocks of zeroes to the USB after this sector

Edit. When I run it on a real machine it just displays a blinking cursor
quirck
Member
Member
Posts: 42
Joined: Sun Nov 23, 2008 5:56 am
Location: Russia, Saint-Petersburg

Re: Can't rune the simplest of bootloader on a real PC

Post by quirck »

The function 0x0E has parameter BL = color of the symbol. You set it to zero, so it prints a black 'a'
Szustarol
Posts: 20
Joined: Fri Mar 17, 2017 6:21 am

Re: Can't rune the simplest of bootloader on a real PC

Post by Szustarol »

quirck wrote:The function 0x0E has parameter BL = color of the symbol. You set it to zero, so it prints a black 'a'
Shouldn't this work only for Graphics mode? I mean the bios default mode is text isn't it?
User avatar
Schol-R-LEA
Member
Member
Posts: 1925
Joined: Fri Oct 27, 2006 9:42 am
Location: Athens, GA, USA

Re: Can't rune the simplest of bootloader on a real PC

Post by Schol-R-LEA »

Szustarol wrote:
quirck wrote:The function 0x0E has parameter BL = color of the symbol. You set it to zero, so it prints a black 'a'
Shouldn't this work only for Graphics mode? I mean the bios default mode is text isn't it?
Yes, but text doesn't necessarily mean monochrome. Character cells in text mode have both the character value and an 'attribute' value, which includes a color setting.

As for the Boot Parameter Block, you only need those if you intend to support FAT. If you mean to use some other file system such as SFS or ext2, then it is unnecessary. Note that FAT12 is only used - and usable - for floppy disks and other small devices, as it has a size limit of 16MiB (unless you increase the cluster size to 8, in which case it is 32MiB).

The Master Boot Record, on the other hand, doesn't apply to floppy disks - for booting from a floppy, it goes directly from the boot sector. An MBR - or on newer, UEFI enabled systems (i.e., almost every system made after 2010), the GUID Partition Table - is only needed on disks with multiple partitions.

Speaking of UEFI and FAT12, what hardware are you trying to run on, and what sort of medium are you booting from? This boot sector will only work for a floppy disk, AFAIK.
Last edited by Schol-R-LEA on Thu Feb 27, 2020 12:50 pm, edited 2 times in total.
Rev. First Speaker Schol-R-LEA;2 LCF ELF JAM POEE KoR KCO PPWMTF
Ordo OS Project
Lisp programmers tend to seem very odd to outsiders, just like anyone else who has had a religious experience they can't quite explain to others.
quirck
Member
Member
Posts: 42
Joined: Sun Nov 23, 2008 5:56 am
Location: Russia, Saint-Petersburg

Re: Can't rune the simplest of bootloader on a real PC

Post by quirck »

Oh, you're right. So the cursor is blinking in the leftmost column, as if your code wasn't reached?

Interestingly, on my machine the code worked as intended. So I can only be guessing here -_-

Could it be that BIOS expects a DOS 3.31 BPB that starts at offset 3, verifies it somehow and refuses to load it?
Do you boot in floppy emulation or HDD emulation mode?
Szustarol
Posts: 20
Joined: Fri Mar 17, 2017 6:21 am

Re: Can't rune the simplest of bootloader on a real PC

Post by Szustarol »

quirck wrote:Oh, you're right. So the cursor is blinking in the leftmost column, as if your code wasn't reached?

Interestingly, on my machine the code worked as intended. So I can only be guessing here -_-

Could it be that BIOS expects a DOS 3.31 BPB that starts at offset 3, verifies it somehow and refuses to load it?
Do you boot in floppy emulation or HDD emulation mode?
Yes, the cursor is blinking in the leftmost column.
I don't really know what you mean by floppy/hdd emulation mode.
I compile it with nasm and do dd if=out.bin of=/dev/sda where sda is my pendrive
Schol-R-LEA wrote:This boot sector will only work for a floppy disk.
Why wouldn't it work with a USB?


Edit
I'm running it on B450M DS3H-CF motherboard with Ryzen 7 2700 cpu
PeterX
Member
Member
Posts: 590
Joined: Fri Nov 22, 2019 5:46 am

Re: Can't rune the simplest of bootloader on a real PC

Post by PeterX »

I might be wrong, but isn't a far JMP at the beginning of a FAT floppy MBR wrong?
Szustarol
Posts: 20
Joined: Fri Mar 17, 2017 6:21 am

Re: Can't rune the simplest of bootloader on a real PC

Post by Szustarol »

PeterX wrote:I might be wrong, but isn't a far JMP at the beginning of a FAT floppy MBR wrong?
I'm doing it to ensure that I end up at segment 0x0000, is there other way to ensure this? (I might be at 0x7c00:0x0000)
Also I've seen many people do this before so that's why I did it. Of course I'm not trying to say You're wrong Sir, just
curious why there are so many tutorials showing this procedure


Also, this site https://wiki.osdev.org/Problems_Booting_From_USB_Flash suggest's that MBR partition table is required for a proper boot,
but You guys say that I is only needed when booting multiple partitions. Can I ask for some clarification?
Also I have seen people saying that BPB is needed (or some BIOSes might not boot), or even that some BIOSes might
overwrite this area so It's secure to have data, not code there. How much truth does this hold?
PeterX
Member
Member
Posts: 590
Joined: Fri Nov 22, 2019 5:46 am

Re: Can't rune the simplest of bootloader on a real PC

Post by PeterX »

Szustarol wrote:
PeterX wrote:I might be wrong, but isn't a far JMP at the beginning of a FAT floppy MBR wrong?
I'm doing it to ensure that I end up at segment 0x0000, is there other way to ensure this? (I might be at 0x7c00:0x0000)
Also I've seen many people do this before so that's why I did it. Of course I'm not trying to say You're wrong Sir, just
curious why there are so many tutorials showing this procedure
Yeah, you are totally right, but I'm not sure if there's enough space before the BIOS Parameter block or if it has to be done after it. If I remember correctly, there are only three bytes before the BPB starts. And a far JMP takes more than 3 bytes, right?

EDIT: I looked it up at wikipedia and I'm wrong. Sorry. It's 0x0B not 3 bytes

Greetings
Peter
PeterX
Member
Member
Posts: 590
Joined: Fri Nov 22, 2019 5:46 am

Re: Can't rune the simplest of bootloader on a real PC

Post by PeterX »

I normally set bx to 7. That means gray character-colour.

That should help.
Szustarol
Posts: 20
Joined: Fri Mar 17, 2017 6:21 am

Re: Can't rune the simplest of bootloader on a real PC

Post by Szustarol »

I have tried both suggestions, setting bx to 7 and performing a near jump, none of them helped :(
PeterX
Member
Member
Posts: 590
Joined: Fri Nov 22, 2019 5:46 am

Re: Can't rune the simplest of bootloader on a real PC

Post by PeterX »

Szustarol wrote:I have tried both suggestions, setting bx to 7 and performing a near jump, none of them helped :(
How do you write the bootloader to the disk? Which command do you use exactly?
Szustarol
Posts: 20
Joined: Fri Mar 17, 2017 6:21 am

Re: Can't rune the simplest of bootloader on a real PC

Post by Szustarol »

PeterX wrote:
Szustarol wrote:I have tried both suggestions, setting bx to 7 and performing a near jump, none of them helped :(
How do you write the bootloader to the disk? Which command do you use exactly?

Code: Select all

nasm bootloader.asm -o bootloader.bin
sudo dd if=bootloader.bin of=/dev/sda bs=512
PeterX
Member
Member
Posts: 590
Joined: Fri Nov 22, 2019 5:46 am

Re: Can't rune the simplest of bootloader on a real PC

Post by PeterX »

Did you setup the BIOS to boot first from the drive equivalent to sda?

Because your boot code and dd instruction seem correct to me.
Szustarol
Posts: 20
Joined: Fri Mar 17, 2017 6:21 am

Re: Can't rune the simplest of bootloader on a real PC

Post by Szustarol »

PeterX wrote:Did you setup the BIOS to boot first from the drive equivalent to sda?

Because your boot code and dd instruction seem correct to me.
I'm selecting boot device manually
Post Reply