Author Topic: bootloading dos and or xp  (Read 40877 times)

Offline flyhigh427

  • Jr. Member
  • *
  • Posts: 60
Re: bootloading dos and or xp
« Reply #15 on: July 21, 2011, 01:16:14 PM »
when you load a bootmanger into the first 512  like grub. does it make a copy of the mbr ?
thanks

Offline Frank Kotler

  • NASM Developer
  • Hero Member
  • *****
  • Posts: 2667
  • Country: us
Re: bootloading dos and or xp
« Reply #16 on: July 21, 2011, 08:55:25 PM »
I suppose it depends on which bootmanager. I don't know what grub does. "Use the Source, Luke!", if all else fails. You probably want to make a copy of your MBR(s) for your own use, anyway. For examination, to see what it does, and for restoration if you mess it up. :)

The "partition table" is the most important part. Restoring one by "trial and error" is no fun! You've got everything "important" backed up anyway, right? The remainder of the MBR code is "easy"... just moves itself out of the way, reads the partition table to see which partition is bootable ("active"), loads the first sector of that partition to 0x7C00, and jumps to it...

Best,
Frank


Offline dreamaco

  • Jr. Member
  • *
  • Posts: 19
Re: bootloading dos and or xp
« Reply #17 on: July 22, 2011, 08:23:58 PM »
Actual code entrypoint of IO.SYS files are 0070h:0200h

As first 512bytes of IO.SYS are not actual code only to make it recognized as an okay file to boot. (2 bytes are checked)

offset 0200h also has 2 additional bytes, and together all these 4 bytes makes for a "valid IO.SYS". That's standard for this recogznition to happen. Bytes at 0200h are actually also effectively "NOP" code (DEC DX INC DX or similiar I cannot rember on top of my head, the source below has all the correct bytes in very few lines). so jumping 070h:200h is perfectly valid after loading IO.SYS into 70h:0000.

I have abit short of time so sorry If it's abit messy this description.. Check my source example

The filesize possible with SYS.IO's without additional own loadingcode are not impressive. Standard SYS.IO I think has code that loads rest of the massive size they may have. I have yet to discover if this is true as I'm not working this way any longer.

<CODEBOX>
; A simple io.sys example, and standardly loaded into 70h:0 (0:700h), Works with bootdisks of Win95 - Win7
db "MZ" ; signature that is needed (DEC BP, POP DX)
TIMES (512)-($-$$) db 90h ; skipped

; No ORG needed as only using absolute memory references.

BITS 16
; 70h:200h (0:900h)
db "BJ" ; signature (executable and effectively a nop)(INC DX DEC DX)

; In this example move a chunk into 7c00h, and then jump there. As any boot would do.
; move 64KB from Begin (70h:400h, which is address 0b00h), to 7c0:0, which is address 7c00h.
cli
mov ax,0xb0 ; (0b00h)
mov ds,ax
mov esi,0
mov ax,0x7c0 ; (7c00)
mov es,ax
mov edi,0
mov cx,16384
std; allow copy from overlapping memory (lower to higher)
rep movsd
mov ax,0
mov es,ax
mov fs,ax
mov gs,ax
; mov dl,bootid could be done here

jmp 0:0x7c00

TIMES (1024)-($-$$) db 90h
; 70h:400h
Begin:
;INCBIN "body.bin" ; Any ordinary bootcode with ORG 7c00h as any bootsector would, it could be larger than 512bytes of course. the limit is not explored by me as said earlier, but 4-8KB has been accomplished without any problem, but not the 200-300kb range as been succeeded without modification though so I suspect.. that IO.SYS usually includes code to load things further.
</CODEBOX>

NOTE
If what you need is having a custom bootable disc with custom bootsector, that should be easy to exchange, and while also having a perfectly working DOS disk, that you could manage files normally on..

Instead consider this I made, this is to use on any DOS disk / usbkey / camera memorycard etc, fully bootable while flexible use.
I found this to be a better solution then.

http://forum.nasm.us/index.php?topic=1154.0
« Last Edit: July 23, 2011, 02:18:43 AM by dreamaco »

Offline flyhigh427

  • Jr. Member
  • *
  • Posts: 60
Re: bootloading dos and or xp
« Reply #18 on: August 04, 2011, 03:20:25 PM »
hey yall when you write a bootmanager or loader to the mbr how do you not step on something importent?
i know you put your own program into memory and jump to it,but dos expects some of it self to be in the 512
mbr ,the names and such i think but not sure.
thanks

Offline Frank Kotler

  • NASM Developer
  • Hero Member
  • *****
  • Posts: 2667
  • Country: us
Re: bootloading dos and or xp
« Reply #19 on: August 05, 2011, 01:13:17 PM »
You don't step on something important by not stepping on something important - don't expect Nasm to prevent it. I don't think you'll find any of dos in your MBR - the "important" part is the partition table. The MBR will load the bootsector of the partition marked "active" (bootable). That's where you'd find names like "IO      SYS" (I think).

Best,
Frank


Offline dreamaco

  • Jr. Member
  • *
  • Posts: 19
Re: bootloading dos and or xp
« Reply #20 on: August 07, 2011, 01:16:37 AM »
My way looking at things are these; I see MBR being a regular bootsector just  with a bit more rigid layout.

The BIOS itself does not look into this tables except for few things. Don't want to go deeper without reason so I don't go there if not important.


; Minimal MBR, first sector of media

; 000-1BD Code (446 bytes)
times   0x1BE - ($-$$) db 0x90 ; actual code goes here, lets fill it with NOP here.

; Partition table follows, 4 entries (no Extended MBRs used so max 4 partions possible)

; 1BE-1CD (16 bytes) - Partition 1
db 0x80          ; Active? 80h meaning bootable 0 meaning non-bootable, only one of the four should be made bootable at a time.

; Partition start (in CHS format) Note that sectors in CHS format doesn't start at 0 but 1 (CHS starts at 0 0 1)
NOTE: db 0xFE,0xFF,0xFF tells BIOS and loaders to ignore CHS and force use LBA
db 0                ; HEAD      (0-255)
db 1+(0<<6)           ; SECTOR   (1-63, 0 not used) NOTE: The 2 highest bits are not used for sector but as 2 highest bits of cylinder
db 0                ; CYLINDER (0-1023) This bytes contain low 8 bits of its 10bits

db 0x6C          ; ID. Each OS/platform/filesystem has an ID to put here. Note: Some ID's actually means something special to some BIOS'es. eg. enables/disables INT 13h ext etc.
; Where partition ends (in CHS format) NOTE: db 0xFE,0xFF,0xFF tells BIOS and loaders to ignore CHS and force use LBA

; Partition end (in CHS format)
NOTE: db 0xFE,0xFF,0xFF tells BIOS and loaders to ignore CHS and force use LBA
db 0x7F                ; HEAD      (0-255)
db 0x3F+(0x3<<6)   ; SECTOR   (1-63, 0 not used) NOTE: The 2 highest bits are not used for sector but as 2 highest bits of cylinder
db 0x13                ; CYLINDER (0x313) (0-1023) (Note from line above 03 << 6 + 13h = 313h) This is 8 lowest bits of cylinder

; Where partition starts & ends (in LBA format) Note that sectors in LBA format does start at 0.
dd 63            ; 32bit LBA, (Sector 63 being the very standard place for a long time to have first partition start at) This field is called "relative sectors"
dd 100          ; 32bit LBA Length, number of sectors the partition should have) This field is called "total sectors"

; 1CE-1DD (16 bytes) - Partition 2 (same bytelayout as above)
dd 0,0,0,0

; 1DE-1ED (16 bytes) - Partition 3 (same bytelayout as above)
dd 0,0,0,0

; 1EE-1FD (16 bytes) - Partition 4 (same bytelayout as above)
dd 0,0,0,0

; 1FE-1FF (2 bytes) - Boot signature (This is also a must for a media to be bootable at all, few machines boot regardless but to be sure let's put it, don't leave home without it)
db 0x55,0xaa


Hope that shed some light on things :)

EDIT: Cleaned the table up abit.
« Last Edit: August 10, 2011, 04:50:09 AM by dreamaco »

Offline flyhigh427

  • Jr. Member
  • *
  • Posts: 60
Re: bootloading dos and or xp
« Reply #21 on: August 07, 2011, 10:02:58 AM »
thanks dreamco

Offline flyhigh427

  • Jr. Member
  • *
  • Posts: 60
Re: bootloading dos and or xp
« Reply #22 on: August 07, 2011, 10:48:30 AM »
ive been trying to load io.sys with this witch is for loading a bin.
do i have to change something ?
thanks

Code: [Select]

;*************************************************************************
;the ultimate boot-strap loader
;to load a file from a DOS FAT12 floppy as the OS
;*************************************************************************
[BITS 16]
[ORG 0x0000]
jmp     START
     
     OEM_ID                db "WILLIAM4"
;dos bpb start
     BytesPerSector        dw 0x0200
     SectorsPerCluster     db 0x01
     ReservedSectors       dw 0x0001
     TotalFATs             db 0x02
     MaxRootEntries        dw 0x00E0
     TotalSectorsSmall     dw 0x0B40
     MediaDescriptor       db 0xF0
     SectorsPerFAT         dw 0x0009
     SectorsPerTrack       dw 0x0012
     NumHeads              dw 0x0002
     HiddenSectors         dd 0x00000000
     TotalSectorsLarge     dd 0x00000000
     DriveNumber           db 0x00
     Flags                 db 0x00
     Signature             db 0x29
     VolumeID              dd 0x1501431d
     VolumeLabel           db "Am.s.c.d.x."
     SystemID              db "FAT12   "
;dos bpb end   
     START:
     ; code located at 0000:7C00, adjust segment registers
          cli
          mov     ax, 0x07C0
          mov     ds, ax
          mov     es, ax
          mov     fs, ax
          mov     gs, ax
     ; create stack
          mov     ax, 0x0000
          mov     ss, ax
          mov     sp, 0xFFFF
          sti
     ; post message
          mov     si, msgLoading
          call    DisplayMessage
     LOAD_ROOT:
     ; compute size of root directory and store in ‘cx’
          xor     cx, cx
          xor     dx, dx
          mov     ax, 0x0020                          ; 32 byte directory entry
          mul     WORD [MaxRootEntries]               ; total size of directory
          div     WORD [BytesPerSector]               ; sectors used by directory
          xchg    ax, cx
     ; compute location of root directory and store in ‘ax’
          mov     al, BYTE [TotalFATs]                ; number of FATs
          mul     WORD [SectorsPerFAT]                ; sectors used by FATs
          add     ax, WORD [ReservedSectors]          ; adjust for bootsector
          mov     WORD [datasector], ax               ; base of root directory
          add     WORD [datasector], cx
     ; read root directory into memory (7C00:0200)
          mov     bx, 0x0200                          ; copy root dir above bootcode
          call    ReadSectors
     ; browse root directory for binary image
          mov     cx, WORD [MaxRootEntries]           ; load loop counter
          mov     di, 0x0200                          ; locate first root entry
     .LOOP:
          push    cx
          mov     cx, 0x000B                          ; eleven character name
          mov     si, ImageName                       ; image name to find
          push    di
     rep  cmpsb                                       ; test for entry match
          pop     di
          je      LOAD_FAT
          pop     cx
          add     di, 0x0020                          ; queue next directory entry
          loop    .LOOP
          jmp     FAILURE
     LOAD_FAT:
     ; save starting cluster of boot image
          mov     si, msgCRLF
          call    DisplayMessage
          mov     dx, WORD [di + 0x001A]
          mov     WORD [cluster], dx                  ; file’s first cluster
     ; compute size of FAT and store in ‘cx’
          xor     ax, ax
          mov     al, BYTE [TotalFATs]                ; number of FATs
          mul     WORD [SectorsPerFAT]                ; sectors used by FATs
          mov     cx, ax
     ; compute location of FAT and store in ‘ax’
          mov     ax, WORD [ReservedSectors]          ; adjust for bootsector
     ; read FAT into memory (7C00:0200)
          mov     bx, 0x0200                          ; copy FAT above bootcode
          call    ReadSectors
     ; read image file into memory (0100:0000)
          mov     si, msgCRLF
          call    DisplayMessage
          mov     ax, 0x0070                          ; destination of image CS WAS O1OO
          mov     es, ax
          mov     bx, 0x0000                          ; destination for image IP
          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 [SectorsPerCluster]        ; sectors to read
          call    ReadSectors
          push    bx
     ; compute next cluster
          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    DisplayMessage
          push    WORD 0x0070                        ;WAS 0100
          push    WORD 0x0200                        ;WAS 0000
          retf
     FAILURE:
          mov     si, msgFailure
          call    DisplayMessage
          mov     ah, 0x00
          int     0x16                                ; await keypress
          int     0x19                                ; warm boot computer
     
     ;*************************************************************************
     ; PROCEDURE DisplayMessage
     ; display ASCIIZ string at ds:si via BIOS
     ;*************************************************************************
     DisplayMessage:
          lodsb                                       ; load next character
          or      al, al                              ; test for NUL character
          jz      .DONE
          mov     ah, 0x0E                            ; BIOS teletype
          mov     bh, 0x00                            ; display page 0
          mov     bl, 0x07                            ; text attribute
          int     0x10                                ; invoke BIOS
          jmp     DisplayMessage
     .DONE:
          ret
     
     ;*************************************************************************
     ; PROCEDURE ReadSectors
     ; reads ‘cx’ sectors from disk starting at ‘ax’ into
     ;memory location ‘es:bx’
     ;*************************************************************************
     ReadSectors:
     .MAIN:
          mov     di, 0x0005                          ; five retries for error
     .SECTORLOOP:
          push    ax
          push    bx
          push    cx
          call    LBACHS
          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 [DriveNumber]              ; 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    DisplayMessage
          pop     cx
          pop     bx
          pop     ax
          add     bx, WORD [BytesPerSector]           ; queue next buffer
          inc     ax                                  ; queue next sector
          loop    .MAIN                               ; read next sector
          ret
     
     ;*************************************************************************
     ; PROCEDURE ClusterLBA
     ; convert FAT cluster into LBA addressing scheme
     ; LBA = (cluster - 2) * sectors per cluster
     ;*************************************************************************
     ClusterLBA:
          sub     ax, 0x0002                          ; zero base cluster number
          xor     cx, cx
          mov     cl, BYTE [SectorsPerCluster]        ; convert byte to word
          mul     cx
          add     ax, WORD [datasector]               ; base data sector
          ret
     
     ;*************************************************************************
     ; PROCEDURE LBACHS
     ; convert ‘ax’ LBA addressing scheme to CHS addressing scheme
     ; absolute sector = (logical sector / sectors per track) + 1
     ; absolute head   = (logical sector / sectors per track) MOD number of heads
     ; absolute track  = logical sector / (sectors per track * number of heads)
     ;*************************************************************************
     LBACHS:
          xor     dx, dx                              ; prepare dx:ax for operation
          div     WORD [SectorsPerTrack]              ; calculate
          inc     dl                                  ; adjust for sector 0
          mov     BYTE [absoluteSector], dl
          xor     dx, dx                              ; prepare dx:ax for operation
          div     WORD [NumHeads]                     ; calculate
          mov     BYTE [absoluteHead], dl
          mov     BYTE [absoluteTrack], al
          ret

     absoluteSector db 0x00
     absoluteHead   db 0x00
     absoluteTrack  db 0x00
     
     datasector  dw 0x0000
     cluster     dw 0x0000
     ImageName   db "IO      SYS"
     msgLoading  db 0x0D, 0x0A, "Loading Image ", 0x0D, 0x0A, 0x00
     msgCRLF     db 0x0D, 0x0A, 0x00
     msgProgress db ".", 0x00
     msgFailure  db 0x0D, 0x0A, "ERROR: Press Any Key to Reboot", 0x00
                 ;db "IO      SYSMSDOS   SYS"
          TIMES 510-($-$$) DB 0
          DW 0xAA55

Offline dreamaco

  • Jr. Member
  • *
  • Posts: 19
Re: bootloading dos and or xp
« Reply #23 on: August 07, 2011, 11:36:31 AM »
ive been trying to load io.sys with this witch is for loading a bin.
do i have to change something ?
thanks

Just gazing quickly through..

What I think could be what's might messes things up is that you try load io.sys into offset 200h.
io.sys are normally loaded offset 0 and containing 200h worth of noise, making the actual codestart at offset 200h. But it's meant to be loaded at offset 0. 70:0 originally but which segment I guess doesn't matter.
 
Perhaps that is it? I understand changing into offset 0 when in 7c00 creates overlapping problems :(
Before attacking the problem you could try diassemble the io.sys to confirm it begins with garbled noise and code starts at 200h?

I am sorry, I see you are trying to load into 70h:0 and execute 70h:200h already.
This smells like one simple tiny pesky hidden bug

ADDED:
Quote
; read image file into memory (0100:0000)
          mov     si, msgCRLF
          call    DisplayMessage
          mov     ax, 0x0070                          ; destination of image CS WAS O1OO
          mov     es, ax
          mov     bx, 0x0000                          ; destination for image IP
          push    bx

Could it be simply a mov ds,ax missing?
« Last Edit: August 07, 2011, 12:46:14 PM by dreamaco »

Offline flyhigh427

  • Jr. Member
  • *
  • Posts: 60
Re: bootloading dos and or xp
« Reply #24 on: August 08, 2011, 02:02:49 PM »
hey yall trying a different way
if i load the mbr in to memory at  0000:7c00  then jump to it
do i have to move this into a different part of memory?
it tryes to load then i get a dos error
thanks all


Code: [Select]

;*************************************************************************
;the ultimate boot-strap loader
;to load a file from a DOS FAT12 floppy as the OS
;*************************************************************************
[BITS 16]
[ORG 0x0000]
jmp     START
     
     OEM_ID                db "WILLIAM4"
;dos bpb start
     BytesPerSector        dw 0x0200
     SectorsPerCluster     db 0x01
     ReservedSectors       dw 0x0001
     TotalFATs             db 0x02
     MaxRootEntries        dw 0x00E0
     TotalSectorsSmall     dw 0x0B40
     MediaDescriptor       db 0xF0
     SectorsPerFAT         dw 0x0009
     SectorsPerTrack       dw 0x0012
     NumHeads              dw 0x0002
     HiddenSectors         dd 0x00000000
     TotalSectorsLarge     dd 0x00000000
     DriveNumber           db 0x00
     Flags                 db 0x00
     Signature             db 0x29
     VolumeID              dd 0x1501431d
     VolumeLabel           db "Am.s.c.d.x."
     SystemID              db "FAT12   "
;dos bpb end   
     START:
     ; code located at 0000:7C00, adjust segment registers
          cli
          mov     ax, 0x07C0
          mov     ds, ax
          mov     es, ax
          mov     fs, ax
          mov     gs, ax
     ; create stack
          mov     ax, 0x0000
          mov     ss, ax
          mov     sp, 0xFFFF
          sti
     ; post message
          mov     si, msgLoading
          call    DisplayMessage
     LOAD_ROOT:
     ; compute size of root directory and store in ‘cx’
          xor     cx, cx
          xor     dx, dx
          mov     ax, 0x0020                          ; 32 byte directory entry
          mul     WORD [MaxRootEntries]               ; total size of directory
          div     WORD [BytesPerSector]               ; sectors used by directory
          xchg    ax, cx
     ; compute location of root directory and store in ‘ax’
          mov     al, BYTE [TotalFATs]                ; number of FATs
          mul     WORD [SectorsPerFAT]                ; sectors used by FATs
          add     ax, WORD [ReservedSectors]          ; adjust for bootsector
          mov     WORD [datasector], ax               ; base of root directory
          add     WORD [datasector], cx
     ; read root directory into memory (7C00:0200)
          mov     bx, 0x0200                          ; copy root dir above bootcode
          call    ReadSectors
     ; browse root directory for binary image
          mov     cx, WORD [MaxRootEntries]           ; load loop counter
          mov     di, 0x0200                          ; locate first root entry
     .LOOP:
          push    cx
          mov     cx, 0x000B                          ; eleven character name
          mov     si, ImageName                       ; image name to find
          push    di
     rep  cmpsb                                       ; test for entry match
          pop     di
          je      LOAD_FAT
          pop     cx
          add     di, 0x0020                          ; queue next directory entry
          loop    .LOOP
          jmp     FAILURE
     LOAD_FAT:
     ; save starting cluster of boot image
          mov     si, msgCRLF
          call    DisplayMessage
          mov     dx, WORD [di + 0x001A]
          mov     WORD [cluster], dx                  ; file’s first cluster
     ; compute size of FAT and store in ‘cx’
          xor     ax, ax
          mov     al, BYTE [TotalFATs]                ; number of FATs
          mul     WORD [SectorsPerFAT]                ; sectors used by FATs
          mov     cx, ax
     ; compute location of FAT and store in ‘ax’
          mov     ax, WORD [ReservedSectors]          ; adjust for bootsector
     ; read FAT into memory (7C00:0200)
          mov     bx, 0x0200                          ; copy FAT above bootcode
          call    ReadSectors
     ; read image file into memory (0100:0000)
          mov     si, msgCRLF
          call    DisplayMessage
         
          mov     ax, 0x7C00                          ; destination of image CS WAS O1OO
           
          mov     es, ax
          mov     bx, 0x0000                          ; destination for image IP
          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 [SectorsPerCluster]        ; sectors to read
          call    ReadSectors
          push    bx
     ; compute next cluster
          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    DisplayMessage
          push    WORD 0x7C00
          push    WORD 0x0000
          retf                               


     FAILURE:
          mov     si, msgFailure
          call    DisplayMessage
          mov     ah, 0x00
          int     0x16                                ; await keypress
          int     0x19                                ; warm boot computer
     
     ;*************************************************************************
     ; PROCEDURE DisplayMessage
     ; display ASCIIZ string at ds:si via BIOS
     ;*************************************************************************
     DisplayMessage:
          lodsb                                       ; load next character
          or      al, al                              ; test for NUL character
          jz      .DONE
          mov     ah, 0x0E                            ; BIOS teletype
          mov     bh, 0x00                            ; display page 0
          mov     bl, 0x07                            ; text attribute
          int     0x10                                ; invoke BIOS
          jmp     DisplayMessage
     .DONE:
          ret
     
     ;*************************************************************************
     ; PROCEDURE ReadSectors
     ; reads ‘cx’ sectors from disk starting at ‘ax’ into
     ;memory location ‘es:bx’
     ;*************************************************************************
     ReadSectors:
     .MAIN:
          mov     di, 0x0005                          ; five retries for error
     .SECTORLOOP:
          push    ax
          push    bx
          push    cx
          call    LBACHS
          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 [DriveNumber]              ; 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    DisplayMessage
          pop     cx
          pop     bx
          pop     ax
          add     bx, WORD [BytesPerSector]           ; queue next buffer
          inc     ax                                  ; queue next sector
          loop    .MAIN                               ; read next sector
          ret
     
     ;*************************************************************************
     ; PROCEDURE ClusterLBA
     ; convert FAT cluster into LBA addressing scheme
     ; LBA = (cluster - 2) * sectors per cluster
     ;*************************************************************************
     ClusterLBA:
          sub     ax, 0x0002                          ; zero base cluster number
          xor     cx, cx
          mov     cl, BYTE [SectorsPerCluster]        ; convert byte to word
          mul     cx
          add     ax, WORD [datasector]               ; base data sector
          ret
     
     ;*************************************************************************
     ; PROCEDURE LBACHS
     ; convert ‘ax’ LBA addressing scheme to CHS addressing scheme
     ; absolute sector = (logical sector / sectors per track) + 1
     ; absolute head   = (logical sector / sectors per track) MOD number of heads
     ; absolute track  = logical sector / (sectors per track * number of heads)
     ;*************************************************************************
     LBACHS:
          xor     dx, dx                              ; prepare dx:ax for operation
          div     WORD [SectorsPerTrack]              ; calculate
          inc     dl                                  ; adjust for sector 0
          mov     BYTE [absoluteSector], dl
          xor     dx, dx                              ; prepare dx:ax for operation
          div     WORD [NumHeads]                     ; calculate
          mov     BYTE [absoluteHead], dl
          mov     BYTE [absoluteTrack], al
          ret

     absoluteSector db 0x00
     absoluteHead   db 0x00
     absoluteTrack  db 0x00
     
     datasector  dw 0x0000
     cluster     dw 0x0000
     ImageName   db "TEST2   BIN"
     msgLoading  db 0x0D, 0x0A, "Loading Image ", 0x0D, 0x0A, 0x00
     msgCRLF     db 0x0D, 0x0A, 0x00
     msgProgress db ".", 0x00
     msgFailure  db 0x0D, 0x0A, "ERRORPress Any Key to Reboot", 0x00
                 
          TIMES 510-($-$$) DB 0
          DW 0xAA55
     ;*************************************************************************

« Last Edit: August 08, 2011, 02:45:50 PM by flyhigh427 »

Offline flyhigh427

  • Jr. Member
  • *
  • Posts: 60
Re: bootloading dos and or xp
« Reply #25 on: August 08, 2011, 03:15:08 PM »
hey it does work ,so thats how they do that
thanks yall

Offline Frank Kotler

  • NASM Developer
  • Hero Member
  • *****
  • Posts: 2667
  • Country: us
Re: bootloading dos and or xp
« Reply #26 on: August 08, 2011, 03:47:45 PM »
Following Dreamaco's advice, I disassembled io.sys. Indeed, it starts with 200h bytes of... mostly zeros (the expected "MZ" sig and a few more bytes first). At offset 200h, I see "inc dx", "dec dx" ("BJ") as expected. Following that, I see some rather "strange" code! It looks like it is expecting a "known" value in bp, and apparently some "known" values on the stack. To find out what these values might be, I'd have to disassemble the code that loads and jumps to io.sys. (not your code, but the original dos bootsector)

I'm a little confused. ("a little" he says!) You refer to an MBR, but your code is for FAT12. It would be unusual to find FAT12 used on a hard drive, and unusual to find an MBR on a floppy, although I guess you could do either or both...

The "usual" scheme would be for the MBR, loaded by BIOS to 0x7C00, to move itself out of the way, read the partition table to discover the "active" partition (you've only got one, I guess), load the "real" bootsector to 0x7C00, and jump to it. This is where I'd expect to see io.sys found and loaded (possibly in a "second stage" loader, not the 512 byte bootsector itself). As I said, io.sys seems to be expecting some "special" values in bp and on the stack, but I may be misinterpreting what I'm looking at...

Do you know how to use ndisasm - or some other disassembler - well enough to disassemble io.sys and/or some "known good" code that loads it, to compare with what you're trying to do? I can post what I've got for io.sys... I'm not sure I've got a "real MS" bootsector installed anywhere at present...

If you're getting a "dos error" (as opposed to a hang or reboot), it sounds as if you've gotten io.sys loaded, at least. That's a good start!

Just read your latest post, so I guess you've got it working. Never mind... :)

Best,
Frank


Offline Rob Neff

  • Forum Moderator
  • Full Member
  • *****
  • Posts: 429
  • Country: us
Re: bootloading dos and or xp
« Reply #27 on: August 08, 2011, 03:48:02 PM »
And there was much rejoicing!  :D

Offline flyhigh427

  • Jr. Member
  • *
  • Posts: 60
Re: bootloading dos and or xp
« Reply #28 on: August 08, 2011, 05:16:15 PM »
thanks all and frank too
ive been looking at gag boot manager that  thing is big ..
next ill try figuring out how to load form c drive
 

Offline dreamaco

  • Jr. Member
  • *
  • Posts: 19
Re: bootloading dos and or xp
« Reply #29 on: August 09, 2011, 12:03:46 AM »
Glad to hear things worked out! :)