Bootsector problem(need help)

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.
achal11
Posts: 12
Joined: Wed Jul 21, 2010 10:07 am

Bootsector problem(need help)

Post by achal11 »

I am writing a boot sector code to load a c file .

1. I am using vmware as virtual machine , using floppy image to load my bootsector.
2. boot1.asm (file is also attached)

nasm boot1.asm -o boot

3. main.c as my c file (file attached)

gcc -o main main.c

4. I created an image file kernel.img by loading 'boot' at 1st sector
and 'main' is loaded at following sectors


PROBLEM
I am getting this output:

welcome to boot sector
downloaded sector 2,3 at 1000h

after this my it gets automatically reset and then same output and again reset and ...

..............................................................................................................................................
"boot1.asm"

Code: Select all

[BITS 16]
[ORG 0]
    
 jmp start 
 driveno   db 0
 a  db 'welcome to boot sector', 13,10,0 
 b  db 'downloaded sector 2,3 at 1000h', 13,10,0
 c db  'helloo',13,10,0
 
 start:
        mov [driveno],dl 
        mov ax,0x7c0
        mov ds ,ax
        mov gs, ax
        mov ss,ax
        mov sp,0x1000
      
        mov si,a
        call message 
        mov ax ,0
        mov es,ax
        mov bx,0x1000
                          ;mov cx,2    ; no of sectors to read
                          ;mov ax,1
                          ;mov si,ax
      
read_kn:
          call read_sector
                
                   
          mov si ,b
          call message 
       
enable_A20:  
                          ; mov dx,0x70
                          ; in al,dx
                          ; or al,0x80
                          ; out dx,al
          cli
     ;;enable a20 line
          mov al,0xd1
          out 0x64,al
          mov al,0x03
          out 0x60,al
          
          xor ax,ax
          mov ds , ax
           lgdt [GDT]
     ;;turn on protected mode 
         mov eax,cr0
         or al,1
         mov cr0,eax

     jmp      0x08: code.11

[BITS 32]
code:
.11:     mov ax,0x10
        mov ds,ax
        mov ss,ax
        mov esp,9000h  
       jmp  0x08:0x1000
       stop : jmp stop
[BITS 16]
read_sector:  
             push ax
             push cx
              push dx
             push bx
                        ; mov ax,si
                        ; xor dx,dx
                        ;inc ax
            mov cx,2    ; ch= cylinder cl= sector 
           xor dx,dx    ; dh= head= 0
                        ; mov bx,2
                        ;mov dh,dl
                        ;xor dl,dl
        
          mov dl,[driveno]  ; dl = drive no   
          pop bx            
          mov al,2
            mov ah,2
         int 0x13
          pop dx
          pop cx 
          pop ax
          ret  
message:                        ; Dump ds:si to screen.
                lodsb                   ; load byte at ds:si into al
                or al,al                ; test if character is 0 (end)
                jz done
                mov ah,0eh              ; put character
                mov bx,0007             ; attribute
                int 0x10                ; call BIOS
                jmp message
        done:
                ret



GDT:		dw gdt_limit ; null segment, used to store GDT metadata
		dd GDT
		dw 0

	.cseg:	dd 0x0000FFFF, 0x00CF9a00 ; code segment, 32-bit, 0  ;4GB
	.dseg:	dd 0x0000FFFF, 0x00CF9200 ; data segment, 32-bit, 0 ;to 4GB

	gdt_limit equ $-GDT-1
; --------------------------
; boot seal
; --------------------------
[bits 16]
times 512-($-$$)-2	db	0x00
	dw	0xaa55
Attachments
boot1.txt
(2.77 KiB) Downloaded 84 times
main.c
(1.29 KiB) Downloaded 93 times
Last edited by Candy on Wed Jul 21, 2010 12:32 pm, edited 1 time in total.
Reason: Code belongs in [code] tags, not in bright red. Some people have themes that conflict with that.
Gigasoft
Member
Member
Posts: 856
Joined: Sat Nov 21, 2009 5:11 pm

Re: Bootsector problem(need help)

Post by Gigasoft »

There are a lot of problems.

- You try to set the variable driveno before setting DS.
- The GDT pointer is accessed using the wrong offset (should be GDT+7c00h)
- code.11 is accessed using the wrong offset (should be code.11+7c00h)
- The GDT pointer contains the wrong base address (should be GDT+7c00h)
- Why do you set GS?
- The program "main" isn't flat and based at 1000h.
User avatar
Candy
Member
Member
Posts: 3882
Joined: Tue Oct 17, 2006 11:33 pm
Location: Eindhoven

Re: Bootsector problem(need help)

Post by Candy »

Please learn how to indent your code to increase clarity. This looks like you're trying to program in Whitespace.
achal11
Posts: 12
Joined: Wed Jul 21, 2010 10:07 am

Re: Bootsector problem(need help)

Post by achal11 »

@Candy

Sorry for that.
I tried doing editing with spaces and tabs but it didn't work.
By the way thanks for doing it for me. :)
achal11
Posts: 12
Joined: Wed Jul 21, 2010 10:07 am

Re: Bootsector problem(need help)

Post by achal11 »

@gigasoft
Thx for the reply

Actually i was using a 'dump memory code' in between to check that "call read sector" worked or not,
gs was used for that .


what do you mean by : main isn't flat ?? what i need to do ??
achal11
Posts: 12
Joined: Wed Jul 21, 2010 10:07 am

Re: Bootsector problem(need help)

Post by achal11 »

There are a lot of problems.

- You try to set the variable driveno before setting DS.
I corrected that
- The GDT pointer is accessed using the wrong offset (should be GDT+7c00h)
- code.11 is accessed using the wrong offset (should be code.11+7c00h)
- The GDT pointer contains the wrong base address (should be GDT+7c00h)
I changed the origin

Still i m getting the same problem.My new code look like this

Code: Select all

[BITS 16]
[ORG 0x7c00]
    
 jmp start 
 driveno db 0
a  db 'welcome to boot sector', 13,10,0 
b  db 'downloaded sector 2,3 at 1000h', 13,10,0
c db  'helloo',13,10,0
 start:
       
      mov ax,0
      mov ds ,ax
      mov ax, 0x9000
      mov ss,ax
      mov sp,0xffff
      mov [driveno],dl
      mov si,a
      call message 
      mov ax ,0
      mov es,ax
      mov bx,0x1000
                          ;mov cx,2    ; no of sectors to read
                          ;mov ax,1
                          ;mov si,ax
      
read_kn:
      call read_sector
                    
      mov si,a
      call message
                    
      mov si ,b
      call message 
       
enable_A20:  
                          ; mov dx,0x70
                          ; in al,dx
                          ; or al,0x80
                          ; out dx,al
          cli
     ;;enable a20 line
          mov al,0xd1
          out 0x64,al
          mov al,0x03
          out 0x60,al
          
          mov ax,0
          mov ds , ax
           lgdt [GDT]
     ;;turn on protected mode 
         mov eax,cr0
         or al,1
         mov cr0,eax

     jmp      0x08: code.11

[BITS 32]
code:
.11:     mov ax,0x10
        mov ds,ax
        mov ss,ax
        mov esp,9000h  
       jmp  0x08:0x1000
       stop : jmp stop
[BITS 16]
read_sector:  
             push ax
             push cx
              push dx
             push bx
                        ; mov ax,si
                        ; xor dx,dx
                        ;inc ax
            mov cx,2    ; ch= cylinder cl= sector 
           xor dx,dx    ; dh= head= 0
                        ; mov bx,2
                        ;mov dh,dl
                        ;xor dl,dl
        
          mov dl,[driveno]  ; dl = drive no   
          pop bx            
          mov al,2
            mov ah,2
         int 0x13
          pop dx
          pop cx 
          pop ax
          ret  
message:                        ; Dump ds:si to screen.
                lodsb                   ; load byte at ds:si into al
                or al,al                ; test if character is 0 (end)
                jz done
                mov ah,0eh              ; put character
                mov bx,0007             ; attribute
                int 0x10                ; call BIOS
                jmp message
        done:
                ret



GDT:		dw gdt_limit ; null segment, used to store GDT metadata
		dd GDT
		dw 0

	.cseg:	dd 0x0000FFFF, 0x00CF9a00 ; code segment, 32-bit, 0  ;4GB
	.dseg:	dd 0x0000FFFF, 0x00CF9200 ; data segment, 32-bit, 0 ;to 4GB

	gdt_limit equ $-GDT-1
; --------------------------
; boot seal
; --------------------------
[bits 16]
times 512-($-$$)-2	db	0x00
	dw	0xaa55
gerryg400
Member
Member
Posts: 1801
Joined: Thu Mar 25, 2010 11:26 pm
Location: Melbourne, Australia

Re: Bootsector problem(need help)

Post by gerryg400 »

what do you mean by : main isn't flat ?? what i need to do ??
I'm guessing that gigasoft means that the file 'main' is perhaps in ELF or other format and is not a flat binary.

You are jumping to 0x1000. How do you know that the function 'main' is at 0x1000 ?

I suggest you inspect your main executable file with objdump.
If a trainstation is where trains stop, what is a workstation ?
egos
Member
Member
Posts: 612
Joined: Fri Nov 16, 2007 1:59 pm

Re: Bootsector problem(need help)

Post by egos »

- Put "cld" before first "message" call.
- Move even value into sp.
- Don't destroy EBDA.
- Replace "in al,70h"/"or al,80h"/"out 70h,al" with "mov al,8Fh"/"out 70h,al"/"in al,71h" and put this code after "cli".
- Add waiting code for A20 enabling like here:

Code: Select all

  call .WaitForEmpty
  jnz @f
  mov al,0D1h
  out 64h,al
  call .WaitForEmpty
  jnz @f
  mov al,0DFh
  out 60h,al
  call .WaitForEmpty
@@:
- Add parsing code for ELF to run it.
If you have seen bad English in my words, tell me what's wrong, please.
achal11
Posts: 12
Joined: Wed Jul 21, 2010 10:07 am

Re: Bootsector problem(need help)

Post by achal11 »

@gerryg400

its a bin file

About that entry point 0x1000h
i tried using link file but it gave me a warning
ld --oformat binary -Ttext 0x1000 main.o
warning : cannot find entry symbol start:defaulting to 00001000


@egos

its not enable a20 problem
i also tried using bios int 15....its bios way of enabling a20
if it fails it sets the carry flag
which i checked it didnt.

what do u meant by EBDA???
gerryg400
Member
Member
Posts: 1801
Joined: Thu Mar 25, 2010 11:26 pm
Location: Melbourne, Australia

Re: Bootsector problem(need help)

Post by gerryg400 »

its a bin file
Are you sure ? Originally you said you used

Code: Select all

gcc -o main main.c
This won't give you a binary file. I don't know about your tools but this command will usually build an ELF executable for your host operating system. You don't want that.

You will need to run ld yourself but you will need a linker script to get your 'main' into the correct format. The wiki has lots of information on this.
If a trainstation is where trains stop, what is a workstation ?
achal11
Posts: 12
Joined: Wed Jul 21, 2010 10:07 am

Re: Bootsector problem(need help)

Post by achal11 »

@gerryg400
Actually we also get an object file along with
i was using that
ld --oformat binary -Ttext 0x1000 main.o -o main
it creates main.bin file but with a warning

I got your point though ....
so i also used
gcc -c main.c -o main.o
and then used
ld --oformat binary -Ttext 0x1000 main.o -o main
but the warning is still coming
achal11
Posts: 12
Joined: Wed Jul 21, 2010 10:07 am

Re: Bootsector problem(need help)

Post by achal11 »

after some corrections this is my latest code.

Code: Select all



[BITS 16]
[ORG 0x7c00]
    
   jmp 0:start 
   driveno db 0
a  db 'welcome to boot sector', 13,10,0 
b  db 'downloaded sector 2,3 at 1000h', 13,10,0
c db  'helloo a20',13,10,0  
 
 start:
       
        mov ax,0
        mov ds ,ax
        
        mov ss,ax
        mov sp,0x9000
        mov [driveno],dl
      
        mov si,a
       call message 
     
       push ax
       call enable_a20
       pop ax 
       mov ax ,0
       mov es,ax
       mov bx,0x1000
                         
      
read_kn:
      
      call read_sector
                    
       mov si,a
       call message
                    
       mov si ,b
       call message 
       
        cli   
        mov ax,0
        mov ds , ax
          lgdt [GDT]
     ;;turn on protected mode 
         mov eax,cr0
         or eax,1
         mov cr0,eax

     jmp      0x08: protected_mode
       
enable_a20:
           mov ax,0x2401       ; enable a20 is wrking
           int 0x15 
               ;jc .abc       
                                 
               ;mov si ,c
               ;call message
           
       .abc: ret   
 
[BITS 32]

g db 'protected mode',13,10,0

protected_mode:
        mov ax,0x10
        mov ds,ax
        mov ss,ax
        mov esp,0x200000  
        
        mov esi , g            ;;this is not shown on output         
        call PutStr_32
        jmp  0x08:0x1000
       stop : jmp stop

PutStr_32:     
    mov edi, [PutStr_Ptr]
.nextchar:
    lodsb
    test al, al         
    jz .end     
    stosw
    jmp .nextchar 
  .end:
    mov [PutStr_Ptr], edi
    ret 

PutStr_Ptr dd 0xb8000


[BITS 16]
read_sector:  
             push ax
             push cx
              push dx
             
                        
                        ;inc ax
            mov cx,2    ; ch= cylinder cl= sector 
           xor dx,dx    ; dh= head= 0
                        
          mov dl,[driveno]  ; dl = drive no   
                      
          mov al,2
           mov ah,2
         int 0x13
          pop dx
          pop cx 
          pop ax
          ret  
message:                        ; Dump ds:si to screen.
                lodsb                   ; load byte at ds:si into al
                or al,al                ; test if character is 0 (end)
                jz done
                mov ah,0eh              ; put character
                mov bx,0007             ; attribute
                int 0x10                ; call BIOS
                jmp message
        done:
                ret

GDT:		dw gdt_limit ; null segment, used to store GDT metadata
		dd GDT
		dw 0

	.cseg:	dd 0x0000FFFF, 0x00CF9a00 ; code segment, 32-bit, 0  ;4GB
	.dseg:	dd 0x0000FFFF, 0x00CF9200 ; data segment, 32-bit, 0 ;to 4GB

	gdt_limit equ $-GDT-1


; --------------------------
; boot seal
; --------------------------
[bits 16]
times 512-($-$$)-2	db	0x00
	dw	0xaa55

" call PutStr_32"
the message in protected mode is not getting printed on the screen
it is rebooting after the last message call in real mode

" mov si,a
call message

mov si ,b
call message
"

maybe
the code is not even entering the protected mode block
i dont knw why??

I am using Vmware player, it might be the problem .
I tried using boch as emulator ...but i got stuck in config script(anyways thats a different matter)

so if someone has used boch or any other emulator before
could you try to check is that problem still there??( I am attaching both the files as attachment)
Attachments
main.c
(1.29 KiB) Downloaded 57 times
boot2.txt
(2.94 KiB) Downloaded 65 times
gerryg400
Member
Member
Posts: 1801
Joined: Thu Mar 25, 2010 11:26 pm
Location: Melbourne, Australia

Re: Bootsector problem(need help)

Post by gerryg400 »

Don't jmp 0x8:0x1000 yet. Move the

Code: Select all

stop : jmp stop
above the jmp 0x8:0x1000 untill you get the 'call PutStr_32' working.

Code: Select all

GDT:      dw gdt_limit ; null segment, used to store GDT metadata
      dd GDT
      dw 0

   .cseg:   dd 0x0000FFFF, 0x00CF9a00 ; code segment, 32-bit, 0  ;4GB
   .dseg:   dd 0x0000FFFF, 0x00CF9200 ; data segment, 32-bit, 0 ;to 4GB

   gdt_limit equ $-GDT-1
This looks completely wrong

EDIT: Sorry, seems it's not as wrong as I thought. Just never seen layed out that way before !!
If a trainstation is where trains stop, what is a workstation ?
gerryg400
Member
Member
Posts: 1801
Joined: Thu Mar 25, 2010 11:26 pm
Location: Melbourne, Australia

Re: Bootsector problem(need help)

Post by gerryg400 »

Code: Select all

PutStr_32:     
    mov edi, [PutStr_Ptr]
.nextchar:
    lodsb
    test al, al         
    jz .end     
    stosw
    jmp .nextchar
  .end:
    mov [PutStr_Ptr], edi
    ret

stosw requires a valid ES. Did you load ES with 0x10 ?
If a trainstation is where trains stop, what is a workstation ?
egos
Member
Member
Posts: 612
Joined: Fri Nov 16, 2007 1:59 pm

Re: Bootsector problem(need help)

Post by egos »

- Put "cld" before first "message" call!
- Add error checking for disk reading.
- What happens with NMI disabling code?
- The service int 15h/24h is bad supported by BIOSes. Preferable method is to use 8042 and system control port A (92h).
- Ensure for memory availability firstly:

Code: Select all

  mov ss,ax
  mov esp,0x200000
- Put "mov ah,7" in "PutStr_32" at least.
- You can get original cursor superposition from CRT registers.
If you have seen bad English in my words, tell me what's wrong, please.
Post Reply