flat assembler
Message board for the users of flat assembler.

Index > OS Construction > Annoying problem

Author
Thread Post new topic Reply to topic
calpol2004



Joined: 16 Dec 2004
Posts: 110
calpol2004
EDIT: No worries, just set up bpb wrong. Well whoever the wrote the bootsector did Sad. Fixed it.

I'm having a problem with my bootsector, it works, just whenever i want to recompile i have to reformat the disk then put my kernel.bin on the floppy then add the bootsector. I used to put the following code in my bootsector:

Code:
TIMES 0Bh-$+code_start  DB 0    


but it doesn't work anymore, i think it's because i'm using a different bootsector than before which starts at offset 0 and sets up the segments to 7C00h later. I could go back to my old bootsector but this one enables me to load a bigger kernel (last one only allowed 1 track) so i really don't want to.

I really need to know how to alter my bootsector so that i can do "copy KERNEL.BIN A:\KERNEL.BIN" in my batch file after writing my bootsector with partcopy without it complaining the floppy is unformatted. I have tried to doing "FORMAT A:\" at the start of my batch but it asks loads of questions.

heres my bootsector code:

Code:
org 0
use16

jmp short main
nop

bpbOEM                  db "My OS   "                   ; OEM identifier (Cannot exceed 8 bytes!)
bpbBytesPerSector:      DW 512
bpbSectorsPerCluster:   DB 1
bpbReservedSectors:     DW 1
bpbNumberOfFATs:        DB 2
bpbRootEntries:         DW 224
bpbTotalSectors:        DW 2880
bpbMedia:               DB 0xF0  ;      Bug Fix -- This should be 0xF0
bpbSectorsPerFAT:       DW 9
bpbSectorsPerTrack:     DW 18
bpbHeadsPerCylinder:    DW 2
bpbHiddenSectors:       DD 0
bpbTotalSectorsBig:     DD 0
bsDriveNumber:          DB 0
bsUnused:               DB 0
bsExtBootSignature:     DB 0x29
bsSerialNumber:         DD 0xa0a1a2a3
bsVolumeLabel:          DB "MOS FLOPPY "
bsFileSystem:           DB "FAT12   "

main:
cli                                           ; disable interrupts
mov     ax, 0x07C0                            ; setup registers to point to our segment
mov     ds, ax
mov     es, ax
mov     fs, ax
mov     gs, ax
mov     ax, 0x0000                            ; set the stack
mov     ss, ax
mov     sp, 0xFFFF
sti
mov si,msgLoading
call print

LOAD_ROOT:
xor cx,cx
xor dx,dx
mov ax,0x0020
mul WORD [bpbRootEntries]
div WORD [bpbBytesPerSector]
xchg ax,cx
mov     al, BYTE [bpbNumberOfFATs]            ; number of FATs
mul     WORD [bpbSectorsPerFAT]               ; sectors used by FATs
add     ax, WORD [bpbReservedSectors]         ; adjust for bootsector
mov     WORD [datasector], ax                 ; base of root directory
add     WORD [datasector], cx
mov     bx,0x0200
call    ReadSectors
mov     cx, WORD [bpbRootEntries]
mov     di,0x0200

.LOOP:
push cx
mov  cx,0x000B
mov  si,loader
push di
rep  cmpsb
pop  di
je   LOAD_FAT
pop  cx
add  di,0x0020
loop .LOOP
jmp FAILURE

LOAD_FAT:
mov si,msgCRLF
call print
mov     dx, WORD [di + 0x001A]
mov     WORD [cluster], dx
xor     ax, ax
mov     al, BYTE [bpbNumberOfFATs]          ; number of FATs
mul     WORD [bpbSectorsPerFAT]             ; sectors used by FATs
mov     cx, ax
mov     ax, WORD [bpbReservedSectors]
mov     bx, 0x0200                          ; copy FAT above bootcode
call    ReadSectors
mov     si, msgCRLF
call    print
mov     ax, 0x0050
mov     es, ax                              ; destination for image
mov     bx, 0x0000                          ; destination for image
push    bx

LOAD_IMAGE:
mov     ax, WORD [cluster]                  ; cluster to read
pop     bx                                  ; buffer to read into
call    ClusterLBA                          ; convert cluster to LBA
xor     cx, cx
mov     cl, BYTE [bpbSectorsPerCluster]     ; sectors to read
call    ReadSectors
push    bx
mov     ax, WORD [cluster]                  ; identify current cluster
mov     cx, ax                              ; copy current cluster
mov     dx, ax                              ; copy current cluster
shr     dx, 0x0001                          ; divide by two
add     cx, dx                              ; sum for (3/2)
mov     bx, 0x0200                          ; location of FAT in memory
add     bx, cx                              ; index into FAT
mov     dx, WORD [bx]                       ; read two bytes from FAT
test    ax, 0x0001
jnz     .ODD_CLUSTER
.EVEN_CLUSTER:
        and     dx, 0000111111111111b               ; take low twelve bits
        jmp     .DONE
.ODD_CLUSTER:
        shr     dx, 0x0004                          ; take high twelve bits
.DONE:
        mov     WORD [cluster], dx                  ; store new cluster
        cmp     dx, 0x0FF0                          ; test for end of file
        jb      LOAD_IMAGE
        DONE:
        mov     si, msgCRLF
        call    print
        push    WORD 0x0050
        push    WORD 0x0000
retf

FAILURE:
mov     si, msg_Failure
call    print
mov     ah, 0x00
int     0x16                                ; await keypress
int     0x19

print:
lodsb
or al,al
jz  printdone
mov ah,0x0E
mov bx,0x0007
int 0x10
jmp print
printdone:
ret

ReadSectors:
     .MAIN:
          mov     di, 0x0005                          ; five retries for error
     .SECTORLOOP:
          push    ax
          push    bx
          push    cx
          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 [bsDriveNumber]            ; 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
          pop     cx
          pop     bx
          pop     ax
          jnz     .SECTORLOOP                         ; attempt to read again
          int     0x18
     .SUCCESS:
          mov     si, msgProgress
          call    print
          pop     cx
          pop     bx
          pop     ax
          add     bx, WORD [bpbBytesPerSector]        ; queue next buffer
          inc     ax                                  ; queue next sector
          loop    .MAIN                               ; read next sector
          ret

ClusterLBA:
          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

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

;========================================
; Main boot sector code starts from here.
;========================================

                       ; warm boot computer

;variables
absoluteSector db 0x00
absoluteHead   db 0x00
absoluteTrack  db 0x00
datasector  dw 0x0000
cluster     dw 0x0000
msgLoading              db 'Starting.',13,10,0
msgProgress             db '.',0
msg_Failure             db 'Error!',13,10,0
msgCRLF                 db 0x0D, 0x0A, 0x00
loader                  db 'LOADER  BIN'


TIMES 510-($-$$) DB 0
DW 0xAA55                                                                          
    


Last edited by calpol2004 on 26 Jul 2007, 23:35; edited 2 times in total
Post 26 Jul 2007, 23:17
View user's profile Send private message MSN Messenger Reply with quote
calpol2004



Joined: 16 Dec 2004
Posts: 110
calpol2004
No worries, figured it out. Just needed to remove the line:

Code:
bpbOEM                  db 'My OS   '    


Weird how they all have to be in the right order or everything just messes up Confused.


Last edited by calpol2004 on 26 Jul 2007, 23:32; edited 1 time in total
Post 26 Jul 2007, 23:25
View user's profile Send private message MSN Messenger Reply with quote
rugxulo



Joined: 09 Aug 2005
Posts: 2341
Location: Usono (aka, USA)
rugxulo
calpol2004 wrote:

I have tried to doing "FORMAT A:\" at the start of my batch but it asks loads of questions.


Well, just FYI, this may be useful for future reference (though I haven't tried it):

Quote:

The /autotest parameter for format is undocumented and runs format to work without asking for parameters.
Post 26 Jul 2007, 23:27
View user's profile Send private message Visit poster's website 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-2020, Tomasz Grysztar.

Powered by rwasa.