flat assembler
Message board for the users of flat assembler.

Index > OS Construction > Reading Sectors at boot time

Author
Thread Post new topic Reply to topic
crc



Joined: 21 Jun 2003
Posts: 637
Location: Penndel, PA [USA]
crc 14 Nov 2004, 07:57
I'm having some trouble with my boot sector. Basically it needs to load the 35 sectors following the boot sector to $7e00. This works under Bochs, Qemu, and one of my test PC's. The problem seems to be that some PC's don't support multitrack reads Sad

Anyway, here's the code. The routine in question is bootload. If anyone could help me adapt this to work on more PC's, I'd be very grateful.

Code:
; RetroForth/Native
; --------------------------------------------------------
; 1.44MB Boot Sector
; For loading RETROFORTH from a raw floppy disk
; --------------------------------------------------------
; Rev      Date       Notes
; ========================================================
;  -        --        Initial version for RETRO4
;  A    08/10/2004    Ported to FASM
;  B    08/14/2004    Added boot message, turns off floppy
;                     if an error happens
;  C    08/15/2004    New A20 init, jump to code fix
; --------------------------------------------------------
START equ 0x11000         ; ORG of kernel
                             ;
use16                              ;
ORG 0x7C00                 ; Make sure we're where we
jmp 0x0000:start             ; expect to be...
start:                         ;
      xor ax,ax            ; DS = ES = SS = 0
      mov ds,ax           ;
      mov ss,ax            ;
      mov es,ax            ;
      mov sp,7000h         ;

      mov si, welcome          ;
      call putstr          ;
; --------------------------------------------------------
; Load the kernel from the disk
; --------------------------------------------------------
bootload:                       ;
   mov cx,3                ;# retries
.retry:                       ;
   push cx                 ;
   mov ax,7e0h             ;Load to es:bx -- $7e00
 mov es,ax               ;
   xor bx,bx               ;
   xor cx,cx               ;ch=0 (track)
       inc cl                  ;cl=2 (sector, 1-based)
     inc cl                  ;
   xor dx,dx               ;dl=0 (drive)  dh=0 (head)
  mov ax,240h             ;al=35 (sectors)
    int 0x13                ;BIOS service $1302 - disk read
                             ;
   jnc .success            ;
   pop cx                  ;
   loop .retry             ;
.error:                        ;
   xor al,al               ; shut off floppy motor
     mov dx,0x3f2            ;
   out dx,al               ;
   mov si, failed          ; Display 'failed' message
        call putstr             ;
   jmp $                   ; Hang the system
.success:                      ;
   xor al,al               ; shut off floppy motor
     mov dx,0x3f2            ;
   out dx,al               ;
   mov si, success         ; Display 'success' message
       call putstr
; --------------------------------------------------------
; Switch to protected mode
; --------------------------------------------------------
      cli                      ; Stop interrupts
      call enable_A20              ; Enable A20
      lgdt [gdt]                ; Load the GDT
      mov eax,cr0             ;
      inc eax                      ;
      mov cr0,eax          ; Switch to pmode
      jmp dword 8:protected     ;
use32
protected:                   ; If we get here, it worked
 mov ax,0x10             ;
   mov ds,ax               ; Data segment
      mov es,ax               ; Extra segment
     mov ss,ax               ; Stack segment
     mov esp,0x50000         ; Return stack
      cld                     ;
   mov esi,7e00h           ; Move kernel from $7e00 to
 mov edi,START           ; $110000
   mov edx,edi             ;
   mov ecx,(kernel.end-kernel)/4
       cld                     ;
   rep movsd               ;
   call edx                ; Jump to kernel
    jmp $                   ; Problem? Just Hang!

; --------------------------------------------------------
; Data structures
; --------------------------------------------------------
gdt:  dw 0x18
     dd gdt
      dw 0
        dw 0xffff, 0, 0x9a00, 0xcf      ;code
       dw 0xffff, 0, 0x9200, 0xcf      ;data
welcome db 'RETRO7: Loading Kernel...     ', 0
success db 'Success!'
crlf        db 10, 13, 0
failed  db 'Failed!', 0
; --------------------------------------------------------
; Code to enable A20
; --------------------------------------------------------
use16
enable_A20:
   call    a20wait
     mov     al,0xAD
     out     0x64,al

 call    a20wait
     mov     al,0xD0
     out     0x64,al

 call    a20wait2
    in      al,0x60
     push    eax

     call    a20wait
     mov     al,0xD1
     out     0x64,al

 call    a20wait
     pop     eax
 or      al,2
        out     0x60,al

 call    a20wait
     mov     al,0xAE
     out     0x64,al

 call    a20wait
     ret

a20wait:
.l0:     mov     ecx,65536
.l1:   in      al,0x64
     test    al,2
        jz      .l2
 loop    .l1
 jmp     .l0
.l2: ret


a20wait2:
.l0:        mov     ecx,65536
.l1:   in      al,0x64
     test    al,1
        jnz     .l2
 loop    .l1
 jmp     .l0
.l2: ret



putstr: ; si = string
        lodsb            ; al=str[i]
        or al, al        ; if(al==0)
        jz .ret          ;   return
 mov ah, 0x0E    ; put char
  mov bh, 0x00    ;
   mov bl, 14      ; bl = attribute
    int 0x10        ;
   jmp putstr         ; repeat
  .ret:
ret

; --------------------------------------------------------
; Go to end of sector
; --------------------------------------------------------
times   510-($-0x7c00)        db 0

; --------------------------------------------------------
; The 'signature' of the boot sector
; Not all systems require this, but *most do*
; --------------------------------------------------------
dw 0xAA55

; --------------------------------------------------------
; Include the precompiled kernel image AFTER the 512 bytes
; for the boot sector.
; --------------------------------------------------------
kernel:
 align 4
 file "native.bin"
 align 4
.end:    

_________________
Charles Childers, Programmer
Post 14 Nov 2004, 07:57
View user's profile Send private message Visit poster's website Reply with quote
bubach



Joined: 17 Sep 2004
Posts: 341
Location: Trollhättan, Sweden
bubach 14 Nov 2004, 12:54
I see that you are trying to load 35 sectors with one BIOS call, you can't load more then 18 sector at a time in a safe way.
Take a look at my "help optimizing bootsec..." thread for an example..

/ Christoffer


Last edited by bubach on 13 Feb 2012, 15:03; edited 1 time in total
Post 14 Nov 2004, 12:54
View user's profile Send private message Reply with quote
crc



Joined: 21 Jun 2003
Posts: 637
Location: Penndel, PA [USA]
crc 14 Nov 2004, 13:56
Quote:
you can´t load more then 18 sector at a time in a safe way.


*sigh* Why can't the BIOS be more capable Sad Oh well, I'll take a look at your code and see if I can make sense of it...
Post 14 Nov 2004, 13:56
View user's profile Send private message Visit poster's website Reply with quote
Octavio



Joined: 21 Jun 2003
Posts: 366
Location: Spain
Octavio 14 Nov 2004, 14:08
crc wrote:
Quote:
you can´t load more then 18 sector at a time in a safe way.


*sigh* Why can't the BIOS be more capable Sad Oh well, I'll take a look at your code and see if I can make sense of it...

and also you can´t traverse a dma page when reading data from the floppy, y recommend that you read sectors one by one (there is no speed difference) and your bufer must be aligned to a sector size.
Post 14 Nov 2004, 14:08
View user's profile Send private message Visit poster's website Reply with quote
ASHLEY4



Joined: 28 Apr 2004
Posts: 376
Location: UK
ASHLEY4 14 Nov 2004, 14:42
The best way is read/write a sector at a time, inc sector and loop, check for sector is more than 18, if so make sector 1, then check if head is 0 or 1, if 0 then inc, if 1 dec and add 1 to track.

keep doing this until you have come to your end address.

\\\\||////
(@@)
ASHLEY4.

Batteries not included, Some assembly required.
Post 14 Nov 2004, 14:42
View user's profile Send private message Reply with quote
Matrix



Joined: 04 Sep 2004
Posts: 1166
Location: Overflow
Matrix 14 Nov 2004, 16:11
that's right,
some bioses doesn't support reading more than 1 sectors, and if they do you should look out of reading out of track, cause' it will overflow and after 18 it will read same track 1 2 ...
you can read 1 sectors at a time no?
Post 14 Nov 2004, 16:11
View user's profile Send private message Visit poster's website Reply with quote
crc



Joined: 21 Jun 2003
Posts: 637
Location: Penndel, PA [USA]
crc 14 Nov 2004, 17:37
Quote:
you can read 1 sectors at a time no?


Actually, I've never attempted to do it sector by sector. I've always used variations of this boot sector routine, always loading as many sectors as I needed...

*sighs again* I hate real-mode and floppies Sad
Post 14 Nov 2004, 17:37
View user's profile Send private message Visit poster's website Reply with quote
ASHLEY4



Joined: 28 Apr 2004
Posts: 376
Location: UK
ASHLEY4 14 Nov 2004, 18:16
In my pmode floppy driver, i have code it to read 1 sector at a time, although you can call read track if you want, it gives you more control doing it sector by sector.

\\\\||////
(@@)
ASHLEY4.

Batteries not included, Some assembly required.
Post 14 Nov 2004, 18:16
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-2024, Tomasz Grysztar. Also on GitHub, YouTube.

Website powered by rwasa.