Page 1 of 3

My first bootsector

Posted: Tue Nov 08, 2011 5:00 am
by maurolarrat
.code16 # tell it is a 16 bit code

.org 0x0 # where to load in memory

.globl _start
_start:
jmp _start # infinite loop

.fill 507,1,0 # syntax: .fill repeat,size,value

.word 0x55AA # add the signature.

# as bootsector.s -o bootsector.o
# ld -Ttext 0x7C00 -o bootsector bootsector.o --oformat binary --entry=0

Is that correct?
I am using dd bs=512 count=1 if=bootsector of=/dev/sdb1 to write it in my pendrive for tests.

Re: My first bootsector

Posted: Tue Nov 08, 2011 5:08 am
by Solar
Well, the boot sector also includes partitioning information, so I'd be careful with all those zeroes you .fill'ed there...

Re: My first bootsector

Posted: Tue Nov 08, 2011 5:19 am
by maurolarrat
Solar wrote:Well, the boot sector also includes partitioning information, so I'd be careful with all those zeroes you .fill'ed there...
Do you mean that FAT16's stuff:

bootsector:
iOEM: .ascii "DevOS "
iSectSize: .word 0x200
iClustSize: .byte 1
iResSect: .word 1
iFatCnt: .byte 2
iRootSize: .word 224
iTotalSect: .word 2880
iMedia: .byte 0xF0
iFatSize: .word 9
iTrackSect: .word 9
iHeadCnt: .word 2
iHiddenSect: .int 0
iSect32: .int 0
iBootDrive: .byte 0
iReserved: .byte 0
iBootSign: .byte 0x29
iVolID: .ascii "seri"
acVolumeLabel:
root_strt: .byte 0,0
root_scts: .byte 0,0
file_strt: .byte 0,0
file_scts: .byte 0,0
.byte 0,0
rs_fail:
.byte 0
acFSType: .ascii "FAT16"

could you help me to fix my code (not to do it all, just to help me the way...please?)?

Re: My first bootsector

Posted: Tue Nov 08, 2011 5:25 am
by Chandra
maurolarrat wrote:Do you mean that FAT16's stuff:
No, that is Bios Parameter Block. Partition table is a different thing.
maurolarrat wrote:Could you help me to fix my code (not to do it all, just to help me the way...please?)?
You need to mention your problem.

Re: My first bootsector

Posted: Tue Nov 08, 2011 5:38 am
by maurolarrat
Chandra wrote:
maurolarrat wrote:Do you mean that FAT16's stuff:
No, that is Bios Parameter Block. Partition table is a different thing.
maurolarrat wrote:Could you help me to fix my code (not to do it all, just to help me the way...please?)?
You need to mention your problem.
Are BPB well defined or can i define my own parameters for using in my 4 GB HD ?
I read here http://homepage.ntlworld.com/jonathan.d ... block.html , and it seemed to me that it was developed by MS...

my aim is to write a simple bootsector using GAS to test it im my 4GB pendrive.

Re: My first bootsector

Posted: Tue Nov 08, 2011 12:02 pm
by egos
Flash drive usually has two kinds of boot loaders: MBR boot loader and one partition boot loader for every existing partition. What kind of boot loader do you want to write?

Re: My first bootsector

Posted: Tue Nov 08, 2011 2:08 pm
by turdus
maurolarrat wrote: .org 0x0 # where to load in memory
Won't work. BIOS loads your code at 0:7c00, not 0:0. You must set up stack too, and except dl holds the boot drive code, assume nothing.
maurolarrat wrote: I am using dd bs=512 count=1 if=bootsector of=/dev/sdb1 to write it in my pendrive for tests.
That's good if you want to write a volume boot record. You're writing it on the first sector of the first partition. If you want master boot record, use /dev/sdb.

Read for example this (first google result...), it has a disassembled example too.
http://www.dewassoc.com/kbase/hard_driv ... record.htm

Re: My first bootsector

Posted: Tue Nov 08, 2011 2:41 pm
by egos
turdus wrote:
maurolarrat wrote: .org 0x0 # where to load in memory
Won't work. BIOS loads your code at 0:7c00, not 0:0.
Actually, it could be correct: 7C0h:0.

Re: My first bootsector

Posted: Wed Nov 09, 2011 3:38 am
by turdus
egos wrote:Actually, it could be correct: 7C0h:0.
Actually no. Since linear=offs+segbase<<4,
0+7C0h<<4=7C00h,
7C00h+0<<4=7C00h
So theoretically both correct, but there are reasons why the latter preferred. It's easier to use segbase 0, requires fewer bytes to code, easier to debug etc. Original DOS bootsector code, the last DOS bootsector code, win95/98/ME bootsector code, and even win7 bootsector code uses 0:7C00h hence you're wrong, and I suggest to change that org.

Re: My first bootsector

Posted: Wed Nov 09, 2011 3:49 am
by rdos
I use a far-jump at the start of the boot-sector to make sure I'm in segment 7C0 and not 0, or something else. I only think it can be garanteed that BIOS/chain-loaders passes control to linear address 7C00, but not the context of segment registers.

Re: My first bootsector

Posted: Wed Nov 09, 2011 3:52 am
by rdos
turdus wrote:
egos wrote:Actually, it could be correct: 7C0h:0.
Actually no. Since linear=offs+segbase<<4,
0+7C0h<<4=7C00h,
7C00h+0<<4=7C00h
So theoretically both correct, but there are reasons why the latter preferred. It's easier to use segbase 0, requires fewer bytes to code, easier to debug etc. Original DOS bootsector code, the last DOS bootsector code, win95/98/ME bootsector code, and even win7 bootsector code uses 0:7C00h hence you're wrong, and I suggest to change that org.
Why would the latter be prefered? I prefer the first alternative, since then the boot-sector code will start at the correct offset (0), and there is no need to manipulate with orgs in order for the assembler to generate correct offsets.

Re: My first bootsector

Posted: Wed Nov 09, 2011 3:56 am
by rdos
turdus wrote:
maurolarrat wrote: .org 0x0 # where to load in memory
Won't work. BIOS loads your code at 0:7c00, not 0:0. You must set up stack too, and except dl holds the boot drive code, assume nothing.
Of course it does. If you use a jmp far 7C0:something before you reference labels. The alternative .org 0x7C00 will not work if BIOS passes control to 7C0:0.

Code: Select all

BootSectInit:
    jmp StartBoot
    nop

    db 'Rdos    '

BootMedia   boot_struc <>    ; BPB

StartBoot:
    db 0EAh
    dw OFFSET JmpBootCode
    dw 07C0h

JmpBootCode:

Re: My first bootsector

Posted: Wed Nov 09, 2011 3:58 am
by turdus
rdos wrote:Why would the latter be prefered?
Because all mainstream OS does. Sorry, no offense, but your hobby os' boot sector code is not mainstream per definitionem.
rdos wrote:I only think it can be garanteed that BIOS/chain-loaders passes control to linear address 7C00, but not the context of segment registers.
So it's more likely that segment registers left with value of zero than any other value; and as you pointed out you'll need a jump to be correct, a jump that OP's code lacks.

Re: My first bootsector

Posted: Wed Nov 09, 2011 4:03 am
by rdos
turdus wrote: So it's more likely that segment registers left with value of zero than any other value; and as you pointed out you'll need a jump to be correct, a jump that OP's code lacks.
What is most likely is of no interest. If both alternatives are possible, a far jmp will ALWAYS be needed regardless of which segment is prefered in order for the boot-loader to always work.

Re: My first bootsector

Posted: Wed Nov 09, 2011 4:29 am
by turdus
rdos wrote: What is most likely is of no interest. If both alternatives are possible, a far jmp will ALWAYS be needed regardless of which segment is prefered in order for the boot-loader to always work.
I beg your pardon, can you read?
turdus wrote:as you pointed out you'll need a jump to be correct
And, just because you pissed me off, I have to ask, how many of these use cs=7C0h and how many use cs=0:

Code: Select all

---original mbr---
7C18 EA1D060000    JMP	0000:061D
---windows mbr---
7C00 33C0          XOR   AX,AX
7C10 BF1B06        MOV   DI,061B
7C13 50            PUSH  AX
7C14 57            PUSH  DI
7C1A CB            RETF
---win7 mbr---
7C00 33C0          XOR   AX,AX 
7C17 50            PUSH  AX
7C18 681C06        PUSH  061C
7C1B CB            RETF
---grub---
7C4B EA507C0000    JMP   0000:7C50
---msdos vbr---
7C00 EB3C          JMP   7C3E
---windows vbr (mswin4.1)---
7C00 EB58          JMP   7C5A
---windows ntfs vbr (ntldr)---
7C00 EB52          JMP   7C54
Don't answer here, answer it to yourself. I think it's more than crystal clear which one is preferred.

Cheers