Page 1 of 3
Can't rune the simplest of bootloader on a real PC
Posted: Thu Feb 27, 2020 11:54 am
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
Re: Can't rune the simplest of bootloader on a real PC
Posted: Thu Feb 27, 2020 12:10 pm
by quirck
The function 0x0E has parameter BL = color of the symbol. You set it to zero, so it prints a black 'a'
Re: Can't rune the simplest of bootloader on a real PC
Posted: Thu Feb 27, 2020 12:16 pm
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?
Re: Can't rune the simplest of bootloader on a real PC
Posted: Thu Feb 27, 2020 12:35 pm
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.
Re: Can't rune the simplest of bootloader on a real PC
Posted: Thu Feb 27, 2020 12:38 pm
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?
Re: Can't rune the simplest of bootloader on a real PC
Posted: Thu Feb 27, 2020 12:47 pm
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
Re: Can't rune the simplest of bootloader on a real PC
Posted: Thu Feb 27, 2020 12:52 pm
by PeterX
I might be wrong, but isn't a far JMP at the beginning of a FAT floppy MBR wrong?
Re: Can't rune the simplest of bootloader on a real PC
Posted: Thu Feb 27, 2020 12:54 pm
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?
Re: Can't rune the simplest of bootloader on a real PC
Posted: Thu Feb 27, 2020 1:00 pm
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
Re: Can't rune the simplest of bootloader on a real PC
Posted: Thu Feb 27, 2020 1:04 pm
by PeterX
I normally set bx to 7. That means gray character-colour.
That should help.
Re: Can't rune the simplest of bootloader on a real PC
Posted: Thu Feb 27, 2020 1:09 pm
by Szustarol
I have tried both suggestions, setting bx to 7 and performing a near jump, none of them helped
Re: Can't rune the simplest of bootloader on a real PC
Posted: Thu Feb 27, 2020 1:12 pm
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?
Re: Can't rune the simplest of bootloader on a real PC
Posted: Thu Feb 27, 2020 1:14 pm
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
Re: Can't rune the simplest of bootloader on a real PC
Posted: Thu Feb 27, 2020 1:17 pm
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.
Re: Can't rune the simplest of bootloader on a real PC
Posted: Thu Feb 27, 2020 1:18 pm
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