flat assembler
Message board for the users of flat assembler.

Index > OS Construction > Help:Second Stage

Author
Thread Post new topic Reply to topic
BOTOKILLER



Joined: 07 Jan 2011
Posts: 154
Location: Ukraine
BOTOKILLER 06 Jun 2011, 12:28
Hi everyone!
I've rewritten everything that I had before succefully, bootloader done and working OK, now im trying to code second stage bootloader, but I got stuck at one place, here is the code:
Code:
        USE16
        ORG 0h

        BEGIN:

        jmp START

        DATASECT:

        DAP:
        DAP_Size             db 10h         ;1h
        DAP_Res1             db 0           ;2h
        DAP_Bytes2Transfer   db 1h          ;3h
        DAP_Res2             db 0           ;4h
        DAP_Buff_Addr1       dw 0h          ;6h
        DAP_Buff_Addr2       dw 80h         ;8h
        DAP_LBA              dq 3h         ;9h

        DAT_Disk_Num         db 80h         ;12h          number of device 80h-winchester
        DAT_Num_Of_Sects     dw 10h         ;13h          the number of 512byte sectors, that contain your kernel

        END_DAP:

        VID:
        VID_Cursor           dw 0h          ;15h
        END_VID:

        GDT:
        dw  GDT_ENDS - GDT_STARTS - 1h      ;17h
        dd  80000h + GDT_STARTS
        GDT_STARTS:

        REC0:
                             dq 0h
        REC1:
        Limit_0_15           dw 0FFFFh
        Base_0_15            dw 0h
        Base_16_23           db 0h
        Access               db 10011010b;||Present bit||Priviligies||1||Code-1, Data - 0||if want to execute from low levels - 1, else 0|| can write here? yes=1||0||
        LimAndFlags          db 11001111b;||Granularity||1||0||0||Limit 16-19||
        Base_24_31           db 0h

        REC2:
        aLimit_0_15           dw 0FFFFh
        aBase_0_15            dw 0h
        aBase_16_23           db 0h
        aAccess               db 10010010b;||Present bit||Priviligies||1||Code-1, Data - 0||if want to execute from low levels - 1, else 0|| can write here? yes=1||0||
        aLimAndFlags          db 11001111b;||Granularity||1||0||0||Limit 16-19||
        aBase_24_31           db 0h

        REC3:
        bLimit_0_15           dw 1FFh
        bBase_0_15            dw 800h
        bBase_16_23           db 0h
        bAccess               db 11110000b;||Present bit||Priviligies||1||Code-1, Data - 0||if want to execute from low levels - 1, else 0|| can write here? yes=1||0||
        bLimAndFlags          db 01000000b;||Granularity||1||0||0||Limit 16-19||
        bBase_24_31           db 0h

        REC4:                                                                       ;I cant say exactly but probably TSS is going to be here
        cLimit_0_15           dw 0FFh
        cBase_0_15            dw 0A00h
        cBase_16_23           db 0h
        cAccess               db 89h;||Present bit||Priviligies||1||Code-1, Data - 0||if want to execute from low levels - 1, else 0|| can write here? yes=1||0||
        cLimAndFlags          db 40h;||Granularity||1||0||0||Limit 16-19||
        cBase_24_31           db 0h

        ; ______________________________________________
        ;| MEMORY HOLE OF 2048d BYTES FOR IDT           |
        ;|______________________________________________|

        REC5:
        dLimit_0_15           dw 04FFh
        dBase_0_15            dw 1300h
        dBase_16_23           db 0h
        dAccess               db 10010010b;||Present bit||Priviligies||1||Code-1, Data - 0||if want to execute from low levels - 1, else 0|| can write here? yes=1||0||
        dLimAndFlags          db 11000000b;||Granularity||1||0||0||Limit 16-19||
        dBase_24_31           db 0h

        REC6:
        eLimit_0_15           dw 0FFFFh
        eBase_0_15            dw 0h
        eBase_16_23           db 0h
        eAccess               db 10011010b;||Present bit||Priviligies||1||Code-1, Data - 0||if want to execute from low levels - 1, else 0|| can write here? yes=1||0||
        eLimAndFlags          db 11001111b;||Granularity||1||0||0||Limit 16-19||
        eBase_24_31           db 0h

        GDT_ENDS:


        ;FINALY OS IS MAPPED LIKE THIS
        ; 00000000h | 000007FFh | GDT (descriptor + 255 entries)
        ; 00000800h | 000009FFh | SDA(system data area)
        ; 00000A00h | 00000AFFh | TSS and small space(maybe one more TSS later)
        ; 00000B00h | 000012FFh | IDT (255 entries + descriptor)
        ; 00001300h | 000017FFh | SYSTEM STACK (1280 bytes)
        ; 00001800h |     x     | kernel code(ints inside)
        ;
        ;
        ;



        END_DATASECT:

        ERROR:
        jmp ERROR

        START:
        mov ax, 8000h
        mov ds, ax
        xor ax, ax
        mov fs, ax
        mov ax, 9000h
        mov ss, ax
        mov esp, 0500h
        mov ax, 80h
        mov gs, ax
        mov bp, BEGIN
        ;here is it! everything prepared
        ;ds,cs - point to this segment
        ;es points to b800h
        ;ss points to higher place
        ;gs points to the first secto of kernel
        ;bp points to the start of datasect
        ;fs points to 0
        ; its time for action!

        LOAD_KERN:                              ; service for reading data from disk specified DAT_Disk_Num
        mov ah, 42h
        xor al, al
        mov dl, [cs:bp+12h]
        mov si, DAP
        mov cx, [cs:bp+13h]

        READ_LOOP:
        pusha
        int 13h
        jc ERROR
        popa
        add word [ds:si+6h], 20h
        inc byte [ds:si+8h]
        loop READ_LOOP                          ; service ends here
        END_LOAD_KERN:

        COLLECT_DATA:

        DETECT_VENDOR:
        xor eax, eax
        cpuid
        mov [gs:0h], ecx

        DETECT_CPU:
        mov eax, 01h
        cpuid
        mov [gs:4h], eax

        NUMBER_OF_HDD:
        mov al, [fs:475h]
        mov [gs:0Ch], al

        BIOS_HARDWARE:
        mov ax, [fs:410h]
        mov [gs:0Dh], ax

        MEMORY_MAP:
        mov byte [gs:10h], 0h
        mov di, gs
        mov es, di
        mov di, 11h
        xor ebx, ebx
        mov edx, 534D4150h

        MM_LOOP:
        mov eax, 0E820h
        mov ecx, 24d
        int 15h
        jc ERROR
        add di, 24d
        inc byte [gs:10h]
        cmp ebx, 0h
        jne MM_LOOP

        ;
        ; enogh data about cpu, time to go further.........
        ;
;_________________________________I WILL WORK ON IT LATER
;        PCI_BUS_INFO:
 ;       mov ax, 0B101h
  ;      xor edi, edi
   ;     int 1Ah
    ;    jc ERROR
     ;   mov [gs:0Fh], cl
      ;  inc cx
       ; push cx
       ; xor edx, edx
        ;xor ebx, ebx

;        SCAN_DEVICES:
 ;       pop cx
  ;      dec cx
   ;     push cx
    ;    xor ch, ch
     ;   shl ecx, 10h
      ;  xor eax, eax
      ;  add eax, ecx
        ;mov al, 00100000b
      ;  mov dx, 0CF8h
       ; out dx, eax
        ;mov dx, 0CFCh
        ;in eax, dx
;______________________________________________________________________


        ;here we loaded SDA(System Data Area) with following
        ;800h(dd) vendor: 'ntel' - Intel
        ;804h(dd) system info
        ;808h(dd) !!reserved!!
        ;80Ch(db) number of hdd detected by bios
        ;80Dh(dw) BIOS flags for detected hardware
        ;80Fh(db) !!reserved!!
        ;810h(db) number of memory map tables entries
        ;811h(  ) memory map
        ;done
        ; its time to set up our kernel for pmode

        PMJMP:
        cli

        lea eax, [cs:bp+17h]
        lgdt [eax]

        mov ax, 0x2401
        int 0x15

        mov eax, cr0
        or eax, 1h
        mov cr0, eax
        jmp pword 08h:80000h+PMODE

        USE32
        PMODE:                                   ;congrates! we are in PM!

        SETREG32:                                ; here it loads segment registers and esp
        mov ax, 10h
        mov ds, ax
        mov fs, ax
        mov gs, ax
        mov es, ax
        mov ax, 28h
        mov ss, ax
        mov esp, 4FFh

       ;________________
       :PROBLEM STARTS HERE
       ;_______________________________

        MAKE_IT_WORK:
        mov ebp, 80000h+17h
        xor esi, esi
        mov ecx, GDT_ENDS - GDT_STARTS + 6d
        call MOVE_BLOCK
        jmp pword 30h:80000h+THIS
        THIS:
        mov dword [ds:2h], 6h
        mov dword [ds:0Eh], 18001000h
        mov dword [ds:12h], 00409A00h
        lgdt [0h]
        jmp pword 08h:0000h



        LOOLP:
        nop
        jmp LOOLP

        MOVE_BLOCK:                              ; function that moves block of data specified in ds:ebp to ds:esi with length ecx
        mov al, [ds:ebp+ecx-1]
        mov [ds:esi+ecx-1], al
        loop MOVE_BLOCK
        ret


        times 646 db 0      ;total must be 7680d
        db 'F'

    

this code is loaded at 8000:0000h, starts working, loads kerenel, collects data, go to PM, but it fails when i try to put kenel code segment to 08h.
How to solve the problem???

PS: Dont look at my slogan, it doesnt applies now)))

_________________
_______________________________
NSOS
Post 06 Jun 2011, 12:28
View user's profile Send private message Reply with quote
Enko



Joined: 03 Apr 2007
Posts: 676
Location: Mar del Plata
Enko 06 Jun 2011, 13:51
Quote:

jmp pword 08h:80000h+PMODE

USE32
PMODE: ;congrates! we are in PM!

SETREG32:

my guess is that teh problem is here, and not afterwards.

shouldnt it be like:

jmp pword 08h:80000h

;second stage
USE32
org 80000h

or

jpm PWORD 08h:PMODE
PMODE:
use32
org 80000h
Post 06 Jun 2011, 13:51
View user's profile Send private message Reply with quote
BOTOKILLER



Joined: 07 Jan 2011
Posts: 154
Location: Ukraine
BOTOKILLER 06 Jun 2011, 14:33
Enko wrote:
Quote:

jmp pword 08h:80000h+PMODE

USE32
PMODE: ;congrates! we are in PM!

SETREG32:

my guess is that teh problem is here, and not afterwards.

shouldnt it be like:

jmp pword 08h:80000h

;second stage
USE32
org 80000h

or

jpm PWORD 08h:PMODE
PMODE:
use32
org 80000h

no, this whole code is loaded at 80000h including 16bit part
and doesnt work only thae part that marked 'PROBLEM STARTS HERE'

_________________
_______________________________
NSOS
Post 06 Jun 2011, 14:33
View user's profile Send private message Reply with quote
garystampa



Joined: 25 May 2011
Posts: 52
Location: Central FLorida
garystampa 06 Jun 2011, 17:41
Given what MOVE_BLOCK does, do you really want to move from EBP=80017 to ESI=0 ?

mov ecx, GDT_ENDS - GDT_STARTS + 6d

What is 6d ? Six decimal? Or should it be 0x6d ?

Seems the length is END - START, why add more?


Last edited by garystampa on 06 Jun 2011, 18:08; edited 1 time in total
Post 06 Jun 2011, 17:41
View user's profile Send private message Reply with quote
BOTOKILLER



Joined: 07 Jan 2011
Posts: 154
Location: Ukraine
BOTOKILLER 06 Jun 2011, 17:43
garystampa wrote:
Given what MOVE_BLOCK does, do you really want to move from EBP=80017 to ESI=0 ?

mov ecx, GDT_ENDS - GDT_STARTS + 6d

What is 6d ? Six decimal? Or should it be 0x6d ?

Move block moves block of data specified in ds:ebp to ds:esi of length specified in ecx
6d is length of gdt descriptor which is transferred too for lgdt [0h]

_________________
_______________________________
NSOS
Post 06 Jun 2011, 17:43
View user's profile Send private message Reply with quote
garystampa



Joined: 25 May 2011
Posts: 52
Location: Central FLorida
garystampa 06 Jun 2011, 17:56
This doesn't seem to count up right:

db ;1h
db ;2h
db ;3h
db ;4h
dw ;6h
dw ;8h
dq ;9h
db ;12h
dw ;13h
dw ;15h
dw ;17h

For using as offsets, it seems it should be:

db ;0h
db ;1h
db ;2h
db ;3h
dw ;4,5h
dw ;6,7h
dq ;8,9,a,b, c,d,e,fh
db ;10h
dw ;11,12h
dw ;13,14h
dw ;15h

I think you're mis-calculating the GDT offset as 17h. Why not just use the offset that FASM will calculate? If you use absolutes, seems like you'd also have to add DATASECT to the offset, no?

Also, why put it on an odd boundary?
Post 06 Jun 2011, 17:56
View user's profile Send private message Reply with quote
BOTOKILLER



Joined: 07 Jan 2011
Posts: 154
Location: Ukraine
BOTOKILLER 07 Jun 2011, 07:48
garystampa wrote:
This doesn't seem to count up right:

db ;1h
db ;2h
db ;3h
db ;4h
dw ;6h
dw ;8h
dq ;9h
db ;12h
dw ;13h
dw ;15h
dw ;17h

For using as offsets, it seems it should be:

db ;0h
db ;1h
db ;2h
db ;3h
dw ;4,5h
dw ;6,7h
dq ;8,9,a,b, c,d,e,fh
db ;10h
dw ;11,12h
dw ;13,14h
dw ;15h

I think you're mis-calculating the GDT offset as 17h. Why not just use the offset that FASM will calculate? If you use absolutes, seems like you'd also have to add DATASECT to the offset, no?

Also, why put it on an odd boundary?

I use a VirtualBox debugger, and it says to me that GDT is loaded successfuly, the only problem is jumps


Thank You all of you who made efforts to help me, but I have handled it myself(Rewrote the part that didnt work)))))

_________________
_______________________________
NSOS
Post 07 Jun 2011, 07:48
View user's profile Send private message Reply with quote
Display posts from previous:
Post new topic Reply to topic

Jump to:  


< Last Thread | Next Thread >
Forum Rules:
You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot vote in polls in this forum
You cannot attach files in this forum
You can download files in this forum


Copyright © 1999-2025, Tomasz Grysztar. Also on GitHub, YouTube.

Website powered by rwasa.