Ok the partition works perfectly. It is created with this script:
Code:
#Create the partition
dd if=/dev/zero of=partition.dd bs=512 count=65536 # count = [ K = megabyte; K*(1024)^2/512 ]
mkfs.vfat -F 16 partition.dd
#Add file to the partition
mcopy -i partition.dd path/to/file.eg ::/
#Add the bootloader to the partition
dd if=path/to/boot.bin of=partition.dd seek=0 count=1 conv=notrunc bs=3
dd if=path/to/boot.bin of=partition.dd seek=83 seek=83 skip=83 count=$[512-83] conv=notrunc bs=1
Then I Create the disk image and copy the partition in it:
Code:
#Create the Disk image
dd if=/dev/zero of=disk.dd bs=512 count=$[2048+65536]
echo -e "n \n p \n \n \n \n t \n 6\n a \n w" | fdisk disk.dd
#Load the MBR
dd if=bin/boot/mbr.bin of=disk.dd seek=0 count=1 conv=notrunc bs=436
#Add the partition to the disk
dd if=partition.dd of=disk.dd conv=notrunc bs=512 seek=2048
If I boot just the partition.dd with:
Code:
qemu-system-x86_64 partition.dd
It prints "Hello, World!" but if i boot the disk.dd it prints nothing, instead it clears the screen (which I think is not a good thing).
Is it something with the disk reading part?
This is the code for the MBR:
Code:
bits 16
org 0x0600
start:
cli ; We do not want to be interrupted
xor ax, ax ; 0 AX
mov ds, ax ; Set Data Segment to 0
mov es, ax ; Set Extra Segment to 0
mov ss, ax ; Set Stack Segment to 0
mov sp, ax ; Set Stack Pointer to 0
.CopyLower:
mov cx, 0x0100 ; 256 WORDs in MBR
mov si, 0x7C00 ; Current MBR Address
mov di, 0x0600 ; New MBR Address
rep movsw ; Copy MBR
jmp 0:LowStart ; Jump to new Address
LowStart:
sti ; Start interrupts
mov BYTE [bootDrive], dl ; Save BootDrive
.CheckPartitions: ; Check Partition Table For Bootable Partition
mov bx, PT1 ; Base = Partition Table Entry 1
mov cx, 4 ; There are 4 Partition Table Entries
.CKPTloop:
mov al, BYTE [bx] ; Get Boot indicator bit flag
test al, 0x80 ; Check For Active Bit
jnz .CKPTFound ; We Found an Active Partition
add bx, 0x10 ; Partition Table Entry is 16 Bytes
dec cx ; Decrement Counter
jnz .CKPTloop ; Loop
jmp error.noBootablePartition ; ERROR!
.CKPTFound:
mov WORD [PToff], bx ; Save Offset
add bx, 8 ; Increment Base to LBA Address
.ReadVBR:
mov EBX, DWORD [bx] ; Start LBA of Active Partition
mov di, 0x7C00 ; We Are Loading VBR to 0x07C0:0x0000
mov cx, 1 ; Only one sector
call ReadSectors ; Read Sector
.jumpToVBR:
cmp WORD [0x7DFE], 0xAA55 ; Check Boot Signature
jne error ; Error if not Boot Signature
mov si, WORD [PToff] ; Set DS:SI to Partition Table Entry
mov dl, BYTE [bootDrive] ; Set DL to Drive Number
jmp 0x7C00 ; Jump To VBR
error:
jmp hang
.noBootablePartition:
mov si, Message.Error.NoBootablePartition
call BiosPrint
jmp hang
hang:
cli
hlt
jmp hang
absoluteSector db 0x00
absoluteHead db 0x00
absoluteTrack db 0x00
datasector dw 0x0000
cluster dw 0x0000
bpbSectorsPerCluster: DB 1
bpbSectorsPerTrack: DW 18
bpbHeadsPerCylinder: DW 2
bpbBytesPerSector: DW 512
;********************;
; Convert CHS to LBA ;
; Parameters: ;
; ax => CHS ;
; Returns: ;
; ax => LBA ;
;********************;
CHSLBA:
sub ax, 0x0002 ; zero base cluster number
xor cx, cx
mov cl, BYTE [bpbSectorsPerCluster] ; convert byte to word
mul cx
add ax, WORD [datasector] ; base data sector
ret
;********************;
; Convert LBA to CHS ;
; Parameters: ;
; ax => LBA ;
; Returns: ;
; ax => CHS ;
;********************;
LBACHS:
xor dx, dx ; prepare dx:ax for operation
div WORD [bpbSectorsPerTrack] ; calculate
inc dl ; adjust for sector 0
mov BYTE [absoluteSector], dl
xor dx, dx ; prepare dx:ax for operation
div WORD [bpbHeadsPerCylinder] ; calculate
mov BYTE [absoluteHead], dl
mov BYTE [absoluteTrack], al
ret
;***********************************;
; Reads a series of sectors ;
; Parameters: ;
; cx => Number of sectors to read ;
; ax => Starting sector ;
; es:ebx => Buffer to read to ;
;***********************************;
ReadSectors:
pusha
.start:
mov di, 0x0005 ; five retries for error
.sectorloop:
call LBACHS ; convert starting sector to CHS
mov ah, 0x02 ; BIOS read sector
mov al, 0x01 ; read one sector
mov ch, BYTE [absoluteTrack] ; track
mov cl, BYTE [absoluteSector] ; sector
mov dh, BYTE [absoluteHead] ; head
mov dl, BYTE [bootDrive] ; drive
int 0x13 ; invoke BIOS
jnc .success ; test for read error
xor ax, ax ; BIOS reset disk
int 0x13 ; invoke BIOS
dec di ; decrement error counter
jnz .sectorloop ; attempt to read again
.success:
push cx ; queue next segment
mov cx, WORD [bpbBytesPerSector]
shr cx, 4
mov dx, es
add dx, cx
mov es, dx
pop cx
inc ax ; queue next sector
loop .start ; read next sector
popa
ret
;************************;
; Parameters: ;
; si => string pointer ;
;************************;
BiosPrint:
pusha
.loop:
lodsb
or al, al
jz .done
mov ah, 0x0E
int 0x10
jmp .loop
.done:
popa
ret
Message.Error.NoBootablePartition db "No bootable partition found.", 13, 10, 0
Message.cazzo db "CAZZO", 13, 10, 0
bootDrive db 0 ; Our Drive Number Variable
PToff dw 0 ; Our Partition Table Entry Offset
times (0x1b4 - ($-$$)) nop ; Pad For MBR Partition Table
UID times 10 db 0 ; Unique Disk ID
PT1 times 16 db 0 ; First Partition Entry
PT2 times 16 db 0 ; Second Partition Entry
PT3 times 16 db 0 ; Third Partition Entry
PT4 times 16 db 0 ; Fourth Partition Entry
dw 0xAA55 ; Boot Signature