I'll be using C as well, which is why I'm linking. I did some more testing, and this is what I'm doing now. I use my first stage bootloader to load up gdt, as well as the second stage bootloader, I then jump over to the second stage bootloader, but after switching to protected mode, doing any form of jmp or nontrivial instruction causes things to go haywire.
I'll be mixing in some C to the second stage bootloader as well (which is why I am using ld instead of just -f bin).
The value of the GDT register seems to be correct, 00007c9c 00000017 so unlike before, the GDT is no longer 0.
I've removed the align 4086 from the linker file as well, here is my code:
bootstart (bootstart isn't linked to anything):
%macro print 2
mov ah, 0xa
mov al, [%1]
mov bl, 0xf
mov bh, 0
mov cx, %2
int 0x10
%endmacro
%macro printr 2
mov al, %1
mov bl, 0xf
mov bh, 0
mov cl, %2
mov ah, 0xa
int 0x10
%endmacro
[ORG 0x7c00]
[BITS 16]
jmp 0x0000:bootstart ; jump to set up CS:IP
bootstart:
; Set our data segment
xor ax, ax
mov ds, ax
mov es, ax
mov [dlvalue], dl
cli
print greeting, 2
jmp reset
; 512 bytes per sector
; 18 sectors per track
; 63 tracks
reset:
mov ah, 0 ; reset floppy disk function
mov dl, [dlvalue]
int 0x13 ; call BIOS
jmp readCD
print reading, 3
jc reset
readCDerror:
xor ch, ch
xor cl, cl
mov dl, [dlvalue]
print error, 4
jmp reset
readCD: ; Read 0x7700 bytes from the disk to point 0x500 in memory from beginning of cd
mov dh, 0 ; Drive head
mov ax, 0x500
mov es, ax ; Set es to 0x500, where we want to buffer to
xor bx, bx ; buffer is at 500:0, es:bx
mov ch, 0 ; Set track to 0
mov cl, 2 ; sector to 2
mov ah, 0x2 ; What we want to do with 0x13
mov al, 0x2 ; How much we want to read
mov dl, [dlvalue]
clc
int 0x13
jc readCDerror ; There was a problem with reading
jumpOut:
print load, 5
; Load GDT
cli
xor ax, ax
mov ds, ax
mov es, ax
mov ax, 0x9000 ; set stack at 0x9000-0xffff
mov ss, ax
mov sp, 0xFFFF
pusha
lgdt [TGDT]
popa
jmp 0x500:0x0 ; No problem, just jump to the entry point
data:
greeting db 'G'
error db 'E'
load db 'L'
reading db 'R'
dlvalue db 0
;========================================
;===================GDT==================
;========================================
align 4
BGDT:
; Null
dd 0
dd 0
; Code
dw 0xFFFF ; limit low
dw 0 ; base low
db 0 ; base middle
db 10011010b ; access
db 11001111b ; granularity
db 0 ; base high
; Data
dw 0xFFFF ; limit low
dw 0 ; base low
db 0 ; base middle
db 10010010b ; access
db 11001111b ; granularity
db 0 ; base high
EGDT:
TGDT:
dw EGDT - BGDT - 1 ; limit (Size of GDT)
dd BGDT
times 510-($-$$) db 0
dw 0xAA55
ssbootloader (linked to ssblinker):
;**********************ssbootloader.s**********************;
; The second stage bootloader
%macro print 2
mov ah, 0xa
mov al, [%1]
mov bl, 0xf
mov bh, 0
mov cx, %2
int 0x10
%endmacro
%macro printr 2
mov al, %1
mov bl, 0xf
mov bh, 0
mov cl, %2
mov ah, 0xa
int 0x10
%endmacro
section .data
greeting2 db 'A'
error db '2'
load db '3'
section .text
[BITS 16]
global ssblstart
ssblstart:
; Set our data segment
mov eax, cr0
or eax, 1
mov cr0, eax
cli
hlt
jmp 0x8:PmodeE
; printr [greeting2], 12
; mov ax, cr0
; or ax, 1
; mov cr0, axe
[BITS 32]
PmodeE:
cli
mov ax, 0x10 ; set data segments to data selector (0x10)
mov ds, ax
mov ss, ax
mov es, ax
mov esp, 90000h
cli
hlt
ssblinker:
/* **********************ssblinker.ld********************** */
/*
* This file is used to link the second stage bootloader.
*/
OUTPUT_ARCH(i386)
ENTRY(ssblstart)
SECTIONS
{
.text 0x500 :
{
code = .; _code = .; __code = .;
*(.text)
}
.data :
{
data = .; _data = .; __data = .;
*(.data)
*(.rodata)
}
.bss :
{
bss = .; _bss = .; __bss = .;
*(.bss)
}
end = .; _end = .; __end = .;
}
compilation code:
nasm bootstart.s -f bin -o $(IMGDIR)/bootstart.img
nasm ssbootloader.s -f elf -o $(OBJDIR)/ssbootloader.o
ld --oformat=binary -T $(SRC)/bootloader/ssblinker.ld -o img/ssbootloader.img objects/bootloader/ssbootloader.o
dd if=/dev/zero of=img/boot.img bs=1024 count=1440 seek=0 skip=0
dd if=img/bootstart.img of=img/boot.img seek=0 skip=0 bs=512 count=1 conv=notrunc
dd if=img/ssbootloader.img of=img/boot.img seek=1 skip=0 bs=512 count=10 conv=notrunc
mv img/boot.img bin/boot.img
sh ../../me/ciso.sh
cisco.sh just creates the iso. This builds a bootable img where it boots correctly in bootstart, and transfers to ssbootloader correctly, but after enabled bit 1 in cr0, things start messing up.