bootloader does not seem to work

Question about which tools to use, bugs, the best way to implement a function, etc should go here. Don't forget to see if your question is answered in the wiki first! When in doubt post here.
ggodw000
Member
Member
Posts: 396
Joined: Wed Nov 18, 2015 3:04 pm
Location: San Jose San Francisco Bay Area
Contact:

bootloader does not seem to work

Post by ggodw000 »

OSDEV POST:
I have been away from my project for about 3 months and started resuming on the bootloader.
Basically I have 3 file:
copyraw.exe - copies boot.bin to mbr (without overwriting p-table) and copies exp.exe below to sector 1 and so on.
boot.bin - bootloader that is less than 512 bytes.
exp.exe - actual executable that is loaded by bootloader.

Initially no concern regarding the FAT, it is all raw stuff.

To prove the bootloader is loaded successfully to 7c00h, i put small snippet to printed chars starting from 0 incrementing by 2 10 times (02468... ) to beginning of video buffer area b800h This part works.
After that boot loader should copy exp.exe file starting from sector 1 to 8000h and subsequent sectors to 8200h and so on.
The exp.exe starts with printing ABCD... 10h times to video buffer area b820h. This is where it does not seem to work anymore which I am still checking.

Code: Select all

.
;boot.asm

686p

    include macros.inc

;   re-defined the part of disk.inc here so that no need to include
;   disk.asm,

DAP_OFFSET_SIZE =       0
DAP_OFFSET_UNUSED =     1
DAP_OFFSET_NO_SECTORS = 2
DAP_OFFSET_BUFFER_PTR = 4
DAP_OFFSET_SECTOR_START = 8

sta segment para stack use16 'stack'
sta ends

data segment para public 'data'
data ends

code segment para public use16 'code'
assume cs:code, ds:data,ss:sta

M_EXTERNDEF

;   code to be copied to 1st sector of HDD.
;   this code will do a boot strap by copying main program into another
;   program and will do a jmp.
;   the size of this function must be less than 512 bytes in order to
;   fit onto 1st sector.

    main    proc    far

; 	Print a series of char into screen buffer, to verify this code has been reached
;	and been executed by a processor. 

    mov     si, 0b800h
    mov     ds, si
    sub     si, si              ; (DS:SI) = video buffer.
    mov     cx, 10h				; display 80 times.
    mov     ax, 0e30h			; char to display byte1: color, byte2: char 0.

loop1:
    mov     ds:[si], al
    inc     al                  ; (AL) = next digit.
    cmp     al, 39h             ; reset to 0 if 9
    jb      @f
    mov     al, 30h
@@:
    add     si, 2               ; (SI) = next char.
    loop    loop1

    mov     dl, 80h             ; disk 0

    mov     si, 7e00h           ;
    mov     ds, si
    sub     si, si              ; (DS:SI) = 0000:7e00h = DAP area.

    mov     byte ptr ds:[si+DAP_OFFSET_SIZE], 10h
    mov     byte ptr ds:[si+DAP_OFFSET_UNUSED], 00h
    mov     word ptr ds:[si+DAP_OFFSET_NO_SECTORS], (400h * 64) ; copy 64k

    mov     word ptr ds:[si+DAP_OFFSET_BUFFER_PTR], 8000h
    mov     word ptr ds:[si+DAP_OFFSET_BUFFER_PTR+2], 00h

    mov     dword ptr ds:[si+DAP_OFFSET_SECTOR_START], eax
    mov     dword ptr ds:[si+DAP_OFFSET_SECTOR_START+4], 0h ; set starting sector No for upper 48 lba.

    mov     ah, 42h             ; (AH) = fcn No. for extended disk read.
    int     13h

    sub     si, si
    mov     ds, si
    mov     si, 8000h           ; (DS:SI) = location, 0:8000h to jump to, pt of no ret.

    jmp     word ptr ds:[si]
	
;	Should never reach here. Also up to this point should better not exceed 512 bytes.
 
	fileEnd    db  55h, 0aah
    main    endp
code    ends
    end     main

;exp.asm


.686p

    include macros.inc
    include exp.inc

code segment para public use16 'code'
assume cs:code, ds:data,ss:sta
M_EXTERNDEF

main    proc    far
    mov     ax, DATA
    mov     ds, ax
    mov     si, 0b800h
    add     si, 20h             ; Leave bootloader written area behind.
    mov     ds, si
    sub     si, si              ; (DS:SI) = video buffer.
    mov     cx, 10h             ; display 80 times.
    mov     ax, 'A'             ; char to display byte1: color, byte2: char 0.

loop1:
    mov     ds:[si], al
    inc     al                  ; (AL) = next digit.
    cmp     al, 'E'             ; reset to 0 if 9
    jb      @f
    mov     al, 'A'
@@:
    add     si, 2               ; (SI) = next char.
    loop    loop1

Last edited by ggodw000 on Sat Apr 30, 2016 11:17 pm, edited 2 times in total.
key takeaway after spending yrs on sw industry: big issue small because everyone jumps on it and fixes it. small issue is big since everyone ignores and it causes catastrophy later. #devilisinthedetails
ggodw000
Member
Member
Posts: 396
Joined: Wed Nov 18, 2015 3:04 pm
Location: San Jose San Francisco Bay Area
Contact:

Re: bootloader does not seem to work

Post by ggodw000 »

One thing I needed to verify was that copyraw has copied the all sectors correctly. Here are the copyraw source and its log. I can see it is all correct.
1. Before anything, it reads sector 0 to DAP area.
2. Open boot.bin first and overwrites DAP (minutes 64 bytes to preserve) to sector 0.
3. Opens the exp.exe and skip over first 2x512 chunks since it is DOS header area.
Also on exp.exe, I placed code segment before any other segment (DATA, STACK) so that CODE segment is immediately after DOS header.

From the copyraw.log, I can see it all copied correctly, by printing first few bytes of 512 chunk just copying and reading back and print. Using hiew disassembler, I can see those bytes are correct. (I just pasted the entire source code for copyraw.asm and its log below.

.686p

Code: Select all

; 
    include macros.inc
    include disk.inc

;   re-defined the part of disk.inc here so that no need to include
;   disk.asm,

DAP_OFFSET_SIZE =       0
DAP_OFFSET_UNUSED =     1
DAP_OFFSET_NO_SECTORS = 2
DAP_OFFSET_BUFFER_PTR = 4
DAP_OFFSET_SECTOR_START = 8

sta segment para stack use16 'stack'
sta ends

;   here we define variables bootloader.

data segment para public 'data'
fileName    db  "boot.bin", 0
dosHdrBuffer db  512 dup (36h)
mbrBuffer   db  512 dup (37h)   ; mbr write buffer.
mbrReadBuffer db 512 dup (35h)  ; mbr read first and partition table is copied to mbrBuffer
filePointer dw  ?
readSize    dw  ?

;   here we define variables for file being loaded. will be kernel.

fileName1   db  "exp.exe$"
mbrBuffer1  db  512 dup (38h)
filePointer1 dw  ?
readSize1   dw  ?
counter     dd  0

;   disk access packet area.

dap             db  16  dup(31h)
data ends

code segment para public use16 'code'
assume cs:code, ds:data,ss:sta

M_EXTERNDEF

;   code to be copy mbr code to 1st sector of HDD (sector No. 0).
;   this will open boot.bin (bootloader) file and reads first 1024
;   (should read entire file) bytes into pre-defined buffer and will copy
;   the entire file content (minus DOS header) into Disk 80h sector 0.

    main    proc    far

;   do not run from windows, prevent accidental erase of MBR in case it is allowed.

    mov     eax, cr0
    test    eax, 01
    jz      mainLab1

    M_PRINTF "\nCan not run from protected mode."
    jmp     mainExit

mainLab1:

;   Read sector 0 first. Doing so will help preserve the partition table
;   when we write back after loading boot.bin and overwrite first 200h-64 bytes.

    mov     dl, 80h             ; disk 0

    mov     si, DATA
    mov     ds, si
    lea     si, dap             ; (DS:SI) = SEG:OFF pointer to dap.

;   Prepare DAP area.

    mov     cx, 1               ; (CX) = No. of sectors.
    mov     di, DATA
    mov     es, di
    lea     di, mbrBuffer       ; (DS:SI) = SEG:OFF pointer to dap.

    mov     byte ptr ds:[si+DAP_OFFSET_SIZE], 10h
    mov     byte ptr ds:[si+DAP_OFFSET_UNUSED], 00h
    mov     word ptr ds:[si+DAP_OFFSET_NO_SECTORS], cx

    mov     word ptr ds:[si+DAP_OFFSET_BUFFER_PTR], di
    mov     word ptr ds:[si+DAP_OFFSET_BUFFER_PTR+2], es

    mov     dword ptr ds:[si+DAP_OFFSET_SECTOR_START], 0h
    mov     dword ptr ds:[si+DAP_OFFSET_SECTOR_START+4], 0h ; set starting sector No for upper 48 lba.

;   Issue read DOS function to read into region defined by DAP area.

    mov     ah, 42h             ; (AH) = fcn No. for extended disk read.
    int     13h                 ; (AH) = return code if error reading.
    jnc     mainLab1a

    M_PRINTF "\nRead sector 0 failure. Exiting (Error code): "
    M_PRINTBYTE ah
    jmp     mainExit

mainLab1a:
    M_PRINTF "\nRead sector 0 success."
    M_PRINTSTR_1616_NL es, di, 0, 020h

;   Open bootstrap file. Will copy first 512-64 bytes overwriting the DAP area.

    mov     ah, 3dh             ; (AH) = file open int 21h code.
    sub     al, al              ; (AL) = RO mode.
    mov     dx, DATA
    mov     ds, dx
    lea     dx, fileName        ; (DS:DX) = pointer to filename string.

    M_PRINTF "\nbootloader file (boot.bin): "
    M_PRINTSTR_1616_NL ds, dx, 1, 7

    int     21h                 ; (AX) = file handle.
    jnc     mainLab2

    M_PRINTF "\nFailure opening boot.bin file (error code): "
    M_PRINTWORD ax
    jmp     mainExit

;   Read till the end or up to 1024 - 64  (dos header + mbr - partition table size)
;   into buffer if FF pointer is successful.

mainLab2:
    M_PRINTF "\nSuccess opening file boot.bin (fileHandle), reading: "
    M_PRINTWORD ax

    mov     filePointer, ax     ; save file pointer.
    mov     ah, 3fh             ; (AH) = file read int 21h code.
    mov     bx, filePointer     ; (BX) = file pointer.
    mov     cx, 400h - 64       ; (CX) = 1024 - bytes but leave partition table area.
    mov     dx, DATA
    mov     ds, dx
    lea     dx, dosHdrBuffer    ; (DS:DX) = 16:16 buffer to read into.
    int     21h                 ; (AX) = No. of bytes read if success.
    jnc     mainLab3

    M_PRINTF "\nFailed to read boot.bin file (Error code): "
    M_PRINTWORD ax
    jmp     mainExit

;   copy to disk 80h sector 0. Since the first 200h bytes of read is dosheader
;   we point the DS:SI to right after dosHeaderBuffer which is mbrBuffer
;   which itself is 512 bytes.

mainLab3:
    M_PRINTF "\nRead success for boot.bin (No. of bytes): "
    M_PRINTWORD ax

    mov     readSize, ax        ; save read size.
    mov     dl, 80h             ; disk 0

;   Start writing DAP area back to sector 0.

    mov     si, DATA
    mov     ds, si
    lea     si, dap             ; (DS:SI) = SEG:OFF pointer to dap.

    mov     di, DATA
    mov     es, di
    lea     di, mbrBuffer       ; (DS:SI) = SEG:OFF pointer to dap.

    M_PRINTF "\nData to write to sector 0: "
    M_PRINTSTR_1616 es, di, 0, 020h

    mov     byte ptr ds:[si+DAP_OFFSET_SIZE], 10h
    mov     byte ptr ds:[si+DAP_OFFSET_UNUSED], 00h
    mov     word ptr ds:[si+DAP_OFFSET_NO_SECTORS], 1

    mov     word ptr ds:[si+DAP_OFFSET_BUFFER_PTR], di
    mov     word ptr ds:[si+DAP_OFFSET_BUFFER_PTR+2], es

    mov     dword ptr ds:[si+DAP_OFFSET_SECTOR_START], 0h
    mov     dword ptr ds:[si+DAP_OFFSET_SECTOR_START+4], 0h ; set starting sector No for upper 48 lba.

    M_PRINTF "\nDap: "
    M_PRINTSTR_1616 ds, si, 0, 010h

    mov     ah, 43h             ; (AH) = fcn No. for extended disk write.
    sub     al, al              ; (AL) = close write check. Not sure what it means.
    int     13h                 ; (AH) = return code if error writing.
    jnc     mainLab4

    M_PRINTF "\nWrite to sector 0 failure. Exiting (Error code): "
    M_PRINTBYTE ah

    jmp     mainExit

;   open loading file.

mainLab4:
    M_PRINTF "\nSuccess writing sector 0. Readback test: "

;   perform a sector 0 readback:

    mov     dl, 80h             ; disk 0

    mov     si, DATA
    mov     ds, si
    lea     si, dap             ; (DS:SI) = SEG:OFF pointer to dap.

;   prepare DAP area.

    mov     cx, 1               ; (CX) = No. of sectors.
    mov     di, DATA
    mov     es, di
    lea     di, mbrBuffer       ; (DS:SI) = SEG:OFF pointer to dap.

    mov     byte ptr ds:[si+DAP_OFFSET_SIZE], 10h
    mov     byte ptr ds:[si+DAP_OFFSET_UNUSED], 00h
    mov     word ptr ds:[si+DAP_OFFSET_NO_SECTORS], cx

    mov     word ptr ds:[si+DAP_OFFSET_BUFFER_PTR], di
    mov     word ptr ds:[si+DAP_OFFSET_BUFFER_PTR+2], es

    mov     dword ptr ds:[si+DAP_OFFSET_SECTOR_START], 0h
    mov     dword ptr ds:[si+DAP_OFFSET_SECTOR_START+4], 0h ; set starting sector No for upper 48 lba.

    mov     ah, 42h             ; (AH) = fcn No. for extended disk read.
    int     13h                 ; (AH) = return code if error writing.
    jnc     mainLab5

    M_PRINTF "\nReadback failure for sector 0. Exiting (Error code): "
    M_PRINTBYTE ah

mainLab5:
    M_PRINTF "\nReadback success for sector 0. "
    M_PRINTSTR_1616_NL es, di, 0, 020h

    M_PRINTF "\nClosing bootloader file"
    mov     ah, 3eh
    mov     bx, filePointer
    int     21h
    jnc     mainLab6

    M_PRINTF "\nWARNING: Failed to close file using handle: "
    M_PRINTWORD filePointer

mainLab6:
    M_PRINTF "\nClose file attempt is done"

;   start copying the bootable application.

    M_PRINTF "\nCopying the application file to raw sectors."

    mov     ah, 3dh             ; (AH) = file open int 21h code.
    sub     al, al              ; (AL) = RO mode.
    mov     dx, DATA
    mov     ds, dx
    lea     dx, fileName1       ; (DS:DX) = pointer to filename string.
    int     21h                 ; (AX) = file handle.
    jnc     mainLab2a

    M_PRINTF "\nFailed to open application file (error code): "
    M_PRINTWORD ax
    jmp     mainExit

;   read 200h chunk at a time and keep writing until it is done.
;   however skip the first 200h chunk since this is the DOS header.

mainLab2a:
    M_PRINTF "\nFile open success (fileHandle), reading 200h byte chunk at a time: "
    M_PRINTWORD ax

    mov     filePointer1, ax     ; save file pointer.
    mov     dword ptr counter, 0 ; initialize counter.

mainLoop1a:
    M_PRINTF "\nLoop No. "
    M_PRINTDWORD counter

    mov     ah, 3fh             ; (AH) = file read int 21h code.
    mov     bx, filePointer1    ; (BX) = file pointer.
    mov     cx, 200h            ; (CX) = 512 bytes per read.
    mov     dx, DATA
    mov     ds, dx
    lea     dx, mbrBuffer1      ; (DS:DX) = 16:16 buffer to read into.
    int     21h                 ; (AX) = No. of bytes read if success.
    jnc     mainLab3a

    M_PRINTF "\nFailed to read application file (Error code): "
    M_PRINTWORD ax
    jmp     mainExit

;   Write current read into disk sector. skip if it is first read chunk
;   since it is dos header.

mainLab3a:
    M_PRINTF "\nRead success for application file (No. of bytes): "
    M_PRINTWORD ax

    mov     readSize, ax        ; save read size.
    or      ax, ax              ; read size = 0?
    jz      mainExit            ; leave if so.

    cmp     dword ptr counter, 2 ; is it 3r read?
    jb      mainSkipFirstRead   ; if first/second read, jump over since dos header.

;   Not the first/second read, start writing.

    M_PRINTF "\nWriting from application area to sector"
    M_PRINTDWORD counter

    mov     dl, 80h             ; disk 0

    mov     si, DATA
    mov     ds, si
    lea     si, dap             ; (DS:SI) = SEG:OFF pointer to dap.

    mov     di, DATA
    mov     es, di
    lea     di, mbrBuffer1      ; (DS:SI) = SEG:OFF pointer to dap.

    M_PRINTF "\nData to write: "
    M_PRINTSTR_1616 es, di, 0, 020h

    mov     byte ptr ds:[si+DAP_OFFSET_SIZE], 10h
    mov     byte ptr ds:[si+DAP_OFFSET_UNUSED], 00h
    mov     word ptr ds:[si+DAP_OFFSET_NO_SECTORS], 01h
    mov     word ptr ds:[si+DAP_OFFSET_BUFFER_PTR], di
    mov     word ptr ds:[si+DAP_OFFSET_BUFFER_PTR+2], es
    mov     eax, counter        ; (EAX) = counter, serves as sector write No. too.
    dec     eax                 ; (EAX) = start with sector 1.

    M_PRINTF "\nWriting to sector No: "
    M_PRINTDWORD eax

    mov     dword ptr ds:[si+DAP_OFFSET_SECTOR_START+4], 0 ; set starting sector No for upper 48 lba.
    mov     dword ptr ds:[si+DAP_OFFSET_SECTOR_START], eax

    M_PRINTF "\nDAP area: "
    M_PRINTSTR_1616 ds, si, 0, 020h

    mov     ah, 43h             ; (AH) = fcn No. for extended disk write.
    int     13h

    jnc     mainLab4a

    M_PRINTF "\nWrite failure. Exiting (Error code): "
    M_PRINTBYTE ah

    jmp     mainExit

mainLab4a:

    M_PRINTF "\nSuccess writing sector. Readback test: "

;   Perform a sector N readback:

    mov     dl, 80h             ; disk 0

    mov     si, DATA
    mov     ds, si
    lea     si, dap             ; (DS:SI) = SEG:OFF pointer to dap.

;   prepare DAP area.

    mov     di, DATA
    mov     es, di
    lea     di, mbrBuffer1      ; (DS:SI) = SEG:OFF pointer to dap.

    mov     byte ptr ds:[si+DAP_OFFSET_SIZE], 10h
    mov     byte ptr ds:[si+DAP_OFFSET_UNUSED], 00h
    mov     word ptr ds:[si+DAP_OFFSET_NO_SECTORS], 1h

    mov     word ptr ds:[si+DAP_OFFSET_BUFFER_PTR], di
    mov     word ptr ds:[si+DAP_OFFSET_BUFFER_PTR+2], es

    mov     eax, counter        ; (EAX) = counter, serves as sector write No. too.
    dec     eax                 ; (EAX) = disk sector No. = counter - 1.
    M_PRINTF "\nReadback sector No: "
    M_PRINTDWORD eax
    mov     dword ptr ds:[si+DAP_OFFSET_SECTOR_START], eax
    mov     dword ptr ds:[si+DAP_OFFSET_SECTOR_START+4], 0h ; set starting sector No for upper 48 lba.

    mov     ah, 42h             ; (AH) = fcn No. for extended disk read.
    int     13h                 ; (AH) = return code if error writing.
    jnc     mainLab5a

    M_PRINTF "\nReadback failure. Exiting (Error code): "
    M_PRINTBYTE ah

mainLab5a:
    M_PRINTF "\nReadback success. "
    M_PRINTSTR_1616_NL es, di, 0, 020h

mainSkipFirstRead:
    inc     dword ptr counter
    jmp     mainLoop1a

mainExit:
    M_PRINTF "\nClosing application file"
    mov     ah, 3eh
    mov     bx, filePointer1
    int     21h
    jnc     mainExit1

    M_PRINTF "\nWARNING: Failed to close file using handle: "
    M_PRINTWORD filePointer

mainExit1:
    mov     ax, 4c00h
    int     21h

    main    endp
code    ends
    end     main

;COPYRAW.LOG:


Read sector 0 success.


0701:0209:  BE 00 B8 8E DE 2B F6 B9 : 10 00 B8 30 0E 88 04 FE
0701:0219:  C0 FE C0 3C 39 72 02 B0 : 30 83 C6 02 E2 EF B2 80
bootloader file (boot.bin):

boot.bi
Success opening file boot.bin (fileHandle), reading: 0005
Read success for boot.bin (No. of bytes): 0258
Data to write to sector 0:
0701:0209:  BE 00 B8 8E DE 2B F6 B9 : 10 00 B8 30 0E 88 04 FE
0701:0219:  C0 FE C0 3C 39 72 02 B0 : 30 83 C6 02 E2 EF B2 80
Dap:
0701:081D:  10 00 01 00 09 02 01 07 : 00 00 00 00 00 00 00 00
Success writing sector 0. Readback test:
Readback success for sector 0.


0701:0209:  BE 00 B8 8E DE 2B F6 B9 : 10 00 B8 30 0E 88 04 FE
0701:0219:  C0 FE C0 3C 39 72 02 B0 : 30 83 C6 02 E2 EF B2 80
Closing bootloader file
Close file attempt is done
Copying the application file to raw sectors.
File open success (fileHandle), reading 200h byte chunk at a time: 0005
Loop No. 00000000
Read success for application file (No. of bytes): 0200
Loop No. 00000001
Read success for application file (No. of bytes): 0200
Loop No. 00000002
Read success for application file (No. of bytes): 0200
Writing from application area to sector00000002
Data to write:
0701:0615:  B8 95 04 8E D8 BE 00 B8 : 83 C6 20 8E DE 2B F6 B9
0701:0625:  10 00 B8 41 00 88 04 FE : C0 3C 45 72 02 B0 41 83
Writing to sector No: 00000001
DAP area:
0701:081D:  10 00 01 00 15 06 01 07 : 01 00 00 00 00 00 00 00
0701:082D:  00 00 00 74 65 73 74 20 : 73 74 72 69 6E 67 20 69
Success writing sector. Readback test:
Readback sector No: 00000001
Readback success.


0701:0615:  B8 95 04 8E D8 BE 00 B8 : 83 C6 20 8E DE 2B F6 B9
0701:0625:  10 00 B8 41 00 88 04 FE : C0 3C 45 72 02 B0 41 83
Loop No. 00000003
Read success for application file (No. of bytes): 0200
Writing from application area to sector00000003
Data to write:
0701:0615:  80 C2 07 B4 02 CD 21 58 : 5A 5A 52 8C DA 80 E2 FF
0701:0625:  52 50 8A D2 C0 EA 04 80 : E2 0F 80 C2 30 80 FA 39
Writing to sector No: 00000002
DAP area:
0701:081D:  10 00 01 00 15 06 01 07 : 02 00 00 00 00 00 00 00
0701:082D:  00 00 00 74 65 73 74 20 : 73 74 72 69 6E 67 20 69
Success writing sector. Readback test:
Readback sector No: 00000002
Readback success.


0701:0615:  80 C2 07 B4 02 CD 21 58 : 5A 5A 52 8C DA 80 E2 FF
0701:0625:  52 50 8A D2 C0 EA 04 80 : E2 0F 80 C2 30 80 FA 39
Loop No. 00000004
Read success for application file (No. of bytes): 0200
Writing from application area to sector00000004
Data to write:
0701:0615:  76 03 80 C2 07 B4 02 CD : 21 58 5A 52 50 8A D2 80
0701:0625:  E2 0F 80 C2 30 80 FA 39 : 76 03 80 C2 07 B4 02 CD
Writing to sector No: 00000003
DAP area:
0701:081D:  10 00 01 00 15 06 01 07 : 03 00 00 00 00 00 00 00
0701:082D:  00 00 00 74 65 73 74 20 : 73 74 72 69 6E 67 20 69
Success writing sector. Readback test:
Readback sector No: 00000003
Readback success.


0701:0615:  76 03 80 C2 07 B4 02 CD : 21 58 5A 52 50 8A D2 80
0701:0625:  E2 0F 80 C2 30 80 FA 39 : 76 03 80 C2 07 B4 02 CD
Loop No. 00000005
Read success for application file (No. of bytes): 0200
Writing from application area to sector00000005
Data to write:
0701:0615:  EB 0F 74 68 69 73 20 69 : 73 20 65 78 74 46 63 6E
0701:0625:  24 8C CE 8E DE 8D 36 02 : 06 2A C9 B5 01 0E E8 3C
Writing to sector No: 00000004
DAP area:
0701:081D:  10 00 01 00 15 06 01 07 : 04 00 00 00 00 00 00 00
0701:082D:  00 00 00 74 65 73 74 20 : 73 74 72 69 6E 67 20 69
Success writing sector. Readback test:
Readback sector No: 00000004
Readback success.


0701:0615:  EB 0F 74 68 69 73 20 69 : 73 20 65 78 74 46 63 6E
0701:0625:  24 8C CE 8E DE 8D 36 02 : 06 2A C9 B5 01 0E E8 3C
Loop No. 00000006
Read success for application file (No. of bytes): 0200
Writing from application area to sector00000006
Data to write:
0701:0615:  50 8A D2 C0 EA 04 80 E2 : 0F 80 C2 30 80 FA 39 76
0701:0625:  03 80 C2 07 B4 02 CD 21 : 58 5A 52 50 8A D2 80 E2
Writing to sector No: 00000005
DAP area:
0701:081D:  10 00 01 00 15 06 01 07 : 05 00 00 00 00 00 00 00
0701:082D:  00 00 00 74 65 73 74 20 : 73 74 72 69 6E 67 20 69
Success writing sector. Readback test:
Readback sector No: 00000005
Readback success.

key takeaway after spending yrs on sw industry: big issue small because everyone jumps on it and fixes it. small issue is big since everyone ignores and it causes catastrophy later. #devilisinthedetails
User avatar
SpyderTL
Member
Member
Posts: 1074
Joined: Sun Sep 19, 2010 10:05 pm

Re: bootloader does not seem to work

Post by SpyderTL »

tl;dr; :mrgreen:

A few suggestions, though. First, it would be easier for us to help if your code was available online, like on github.

Second, you are eventually going to need to find a debugging solution, so that you can track down these types of issues yourself. I recommend running your OS on BOCHS with debugging enabled. That will allow you to step through your code one instruction at a time and see what is actually happening. It will also give you fairly understandable errors if you are setting up your hardware incorrectly.

Maybe someone else can spot the problem above, but it's a little hard for me to read. I don't think that you are going to be able to use different sections in your boot loader or your EXE, at this point. There is no OS to load them properly. For now, you should probably just stick to flat 16-bit Assembly. (i.e. remove all of the compiler directives, and just start with assembly instructions.)
Project: OZone
Source: GitHub
Current Task: LIB/OBJ file support
"The more they overthink the plumbing, the easier it is to stop up the drain." - Montgomery Scott
FallenAvatar
Member
Member
Posts: 283
Joined: Mon Jan 03, 2011 6:58 pm

Re: bootloader does not seem to work

Post by FallenAvatar »

The most likely problem is that your *.exe expects 32-Bit PMode and you are running it in 16-Bit RMode.

Beyond that, you haven't given us near enough info to figure out what the issue is.

- Monk
ggodw000
Member
Member
Posts: 396
Joined: Wed Nov 18, 2015 3:04 pm
Location: San Jose San Francisco Bay Area
Contact:

Re: bootloader does not seem to work

Post by ggodw000 »

SpyderTL wrote:tl;dr; :mrgreen:

A few suggestions, though. First, it would be easier for us to help if your code was available online, like on github.

Second, you are eventually going to need to find a debugging solution, so that you can track down these types of issues yourself. I recommend running your OS on BOCHS with debugging enabled. That will allow you to step through your code one instruction at a time and see what is actually happening. It will also give you fairly understandable errors if you are setting up your hardware incorrectly.

Maybe someone else can spot the problem above, but it's a little hard for me to read. I don't think that you are going to be able to use different sections in your boot loader or your EXE, at this point. There is no OS to load them properly. For now, you should probably just stick to flat 16-bit Assembly. (i.e. remove all of the compiler directives, and just start with assembly instructions.)
yeah it is a lot of code, even after 3 months after, i myself had hard time reading, i ll see what i can do. so far i did on vbox need to try on amd dg tool. it is on git repo in my home server,later i plan to setup github.
key takeaway after spending yrs on sw industry: big issue small because everyone jumps on it and fixes it. small issue is big since everyone ignores and it causes catastrophy later. #devilisinthedetails
Octocontrabass
Member
Member
Posts: 5587
Joined: Mon Mar 25, 2013 7:01 pm

Re: bootloader does not seem to work

Post by Octocontrabass »

In boot.asm:

Code: Select all

    mov     dl, 80h             ; disk 0
Your bootloader does not support chain loading.

Code: Select all

    mov     si, 7e00h           ;
    mov     ds, si
    sub     si, si              ; (DS:SI) = 0000:7e00h = DAP area.
Your comment says one thing, but your code says something entirely different.

Code: Select all

    mov     dword ptr ds:[si+DAP_OFFSET_SECTOR_START], eax
Something tells me this is not the sector you want.

Code: Select all

    mov     ah, 42h             ; (AH) = fcn No. for extended disk read.
    int     13h
It's generally a good idea to set up a stack before calling functions that require large amounts of stack space.

Code: Select all

    jmp     word ptr ds:[si]
What's the difference between "DS:SI" and "DS:[SI]"? What's the difference between a near jump and a far jump? (And why aren't you using a form of JMP that takes the destination address as an immediate value?)

In exp.asm:

Code: Select all

    loop    loop1
What happens after this loop finishes?

I'm not going to look at copyraw. Examine the contents of the disk yourself to verify they were written correctly. (Use something like HxD or xxd.)
ggodw000
Member
Member
Posts: 396
Joined: Wed Nov 18, 2015 3:04 pm
Location: San Jose San Francisco Bay Area
Contact:

Re: bootloader does not seem to work

Post by ggodw000 »

Octocontrabass wrote:In boot.asm:

Code: Select all

    mov     dl, 80h             ; disk 0
Your bootloader does not support chain loading.

Code: Select all

    mov     si, 7e00h           ;
    mov     ds, si
    sub     si, si              ; (DS:SI) = 0000:7e00h = DAP area.
Your comment says one thing, but your code says something entirely different.

Code: Select all

    mov     dword ptr ds:[si+DAP_OFFSET_SECTOR_START], eax
Something tells me this is not the sector you want.

Code: Select all

    mov     ah, 42h             ; (AH) = fcn No. for extended disk read.
    int     13h
It's generally a good idea to set up a stack before calling functions that require large amounts of stack space.

Code: Select all

    jmp     word ptr ds:[si]
What's the difference between "DS:SI" and "DS:[SI]"? What's the difference between a near jump and a far jump? (And why aren't you using a form of JMP that takes the destination address as an immediate value?)

In exp.asm:

Code: Select all

    loop    loop1
What happens after this loop finishes?

I'm not going to look at copyraw. Examine the contents of the disk yourself to verify they were written correctly. (Use something like HxD or xxd.)
thanks for pointers, not only these bugs, I also found some other bugs and fixed to code to looks like this:

1st of all in the boot.asm, instead of jumping to 0x8000 where i copied to executable, (because the jmp ds:si instruction does not work so i put jmp ds:[si] and forgot it).
Well what i did was put a label with org and jump to that label. I verified label is created 400h from the begining of code segment after boot.asm is built.

Code: Select all

ORG 0400h
lab1:
That is offset from code segment of boot.asm. since 7c00 is a boot.asm code segment and 7e00 designated for dap, and 8000 is for executable, it works out well.

However I found it still did not work.
so changed the video code so that it will copy few bytes from 0x8000 and print out by moving to b800:0000 buffer. The code was so buggy, I had to first debug it before reading the sectors, so pseudo looks like this:

; copy few bytes from 0x8000 and print.
; read sector 1 to X (I reduced No. of sector being read to just 1 for the time being)
; copy few bytes from 0x8000 and print.

Once bug free, last one should print out the first few bytes of executable. And when I fired up, it turns out, system is hanging right after doing ext. disk read. int 13h 42h. This is on oracle VM.
On hyper-v and also AMDSIM now, system wont boot from HDD at all.

I will continue on vbox and also try setting up stack. Perhaps that was causing int 13h to hang.
key takeaway after spending yrs on sw industry: big issue small because everyone jumps on it and fixes it. small issue is big since everyone ignores and it causes catastrophy later. #devilisinthedetails
User avatar
SpyderTL
Member
Member
Posts: 1074
Joined: Sun Sep 19, 2010 10:05 pm

Re: bootloader does not seem to work

Post by SpyderTL »

You might want to try booting from a floppy image, first. The only real structure that you absolutely need is to set the last two bytes of the first sector to 0x55 and 0xaa.

Then, once you have that working, move on to a HDD image.
Project: OZone
Source: GitHub
Current Task: LIB/OBJ file support
"The more they overthink the plumbing, the easier it is to stop up the drain." - Montgomery Scott
User avatar
jojo
Member
Member
Posts: 138
Joined: Mon Apr 18, 2016 9:50 am
Libera.chat IRC: jojo
Location: New York New York

Re: bootloader does not seem to work

Post by jojo »

I recommend running your OS on BOCHS with debugging enabled.
Definitely good advice, and that's what I was using for the last year or so with pretty good success. But I just wanted to chime in that I just tried using QEmu with GDB and holy crap is being able to step through your kernel source amazing.
Much recommended if you can assemble an ELF version of your bootsector with debugging symbols enabled to feed to GDB.
ggodw000
Member
Member
Posts: 396
Joined: Wed Nov 18, 2015 3:04 pm
Location: San Jose San Francisco Bay Area
Contact:

Re: bootloader does not seem to work

Post by ggodw000 »

jojo wrote:
I recommend running your OS on BOCHS with debugging enabled.
Definitely good advice, and that's what I was using for the last year or so with pretty good success. But I just wanted to chime in that I just tried using QEmu with GDB and holy crap is being able to step through your kernel source amazing.
Much recommended if you can assemble an ELF version of your bootsector with debugging symbols enabled to feed to GDB.
The Qemu sounds pretty good as I have some experience setting up and use it @ work. I'd rather very much try this out.
key takeaway after spending yrs on sw industry: big issue small because everyone jumps on it and fixes it. small issue is big since everyone ignores and it causes catastrophy later. #devilisinthedetails
User avatar
jojo
Member
Member
Posts: 138
Joined: Mon Apr 18, 2016 9:50 am
Libera.chat IRC: jojo
Location: New York New York

Re: bootloader does not seem to work

Post by jojo »

It's suuuuuper friggin' simple:

In one console:

Code: Select all

$ gdb
(gdb) target remote localhost:1234 
And in another:

Code: Select all

$ qemu-system-x86_64 -gdb tcp::1234 -fda <floppy image> -boot a
And once your GDB in the original console picks up the connection, it will hold QEmu in a halted state and you can do, for instance:

Code: Select all

(gdb) symbol bootsect.o
(gdb) break kernel.asm:mainLab1
(gdb) continue
Which, assuming that the source to your bootsector was called kernel.asm and bootsect.o is the ELF version of your code which you built on the side, will cause QEmu to boot up and then break at the label "mainLab1" in your assembly which will allow you to step freely through your code and examine the system state and all of that good jazz.
ggodw000
Member
Member
Posts: 396
Joined: Wed Nov 18, 2015 3:04 pm
Location: San Jose San Francisco Bay Area
Contact:

Re: bootloader does not seem to work

Post by ggodw000 »

great!! i will definitely try out. it will take some time to setup the linux and on top of that qemu but would be interesting to see what happens.
i also did setup stack, that got bit more complicated but managed to work it out.

First i have tried to slam the stack segment between code segments but compiler was putting two code segment lumped together and messes up the offsets:
dos header at 0x00
code segment at 0x200
stack segment: 0x400-64 (64 byte stack)
code segment at 0x400 containing jump label

so declared the data segment, put the jump label there and jumped to data segment, which is kinda weird but compiler did not complain so just went ahead:

dos header at 0x00
code segment at 0x200
stack segment: 0x400-64
data segment at 0x400 containing jump label.

and offsets were worked out nicely after compilation. However it still did not come out after issuing int 13h
key takeaway after spending yrs on sw industry: big issue small because everyone jumps on it and fixes it. small issue is big since everyone ignores and it causes catastrophy later. #devilisinthedetails
Hellbender
Member
Member
Posts: 63
Joined: Fri May 01, 2015 2:23 am
Libera.chat IRC: Hellbender

Re: bootloader does not seem to work

Post by Hellbender »

jojo wrote:$ qemu-system-x86_64 -gdb tcp::1234 -fda <floppy image> -boot a
And if you add "-S" to the mix, qemu will not start executing until gdb is connected and you say 'continue'. Good for debugging the bootloaded etc.
Hellbender OS at github.
User avatar
jojo
Member
Member
Posts: 138
Joined: Mon Apr 18, 2016 9:50 am
Libera.chat IRC: jojo
Location: New York New York

Re: bootloader does not seem to work

Post by jojo »

dos header at 0x00
Why does your binary have a dos header?
ggodw000
Member
Member
Posts: 396
Joined: Wed Nov 18, 2015 3:04 pm
Location: San Jose San Francisco Bay Area
Contact:

Re: bootloader does not seem to work

Post by ggodw000 »

jojo wrote:
dos header at 0x00
Why does your binary have a dos header?
I think it has something to do with small, tiny model. I just did not want to deal with it and just build exe file and bypass DOS hdr.
key takeaway after spending yrs on sw industry: big issue small because everyone jumps on it and fixes it. small issue is big since everyone ignores and it causes catastrophy later. #devilisinthedetails
Post Reply