Writing at boot sector to the first sector of a disk 
Author Message
 Writing at boot sector to the first sector of a disk

Hi
  After making a boot sector, how dould I go about writing it to the 1st sector
of a floppy disk or hard disk?

Steven Graham

--
Posted via Mailgate.ORG Server - http://www.*-*-*.com/



Mon, 28 Jun 2004 06:07:38 GMT  
 Writing at boot sector to the first sector of a disk


Quote:
>Hi
>  After making a boot sector, how dould I go about writing it to the 1st
sector
>of a floppy disk or hard disk?

>Steven Graham

The same way you write any other sector, if you are writing your own OS.  In
DOS
you can use the BIOS (int 13h).  In Linux, write to /dev/fd0 or /dev/hda.
The
methods may vary in other operating systems.


Mon, 28 Jun 2004 07:55:59 GMT  
 Writing at boot sector to the first sector of a disk
Experiment on floppies first!!!

Something like this fragment:

;Now write newly created boot record to floppy, if error exit
                MOV     AX,0301                 ;Write sectors
                MOV     BX,OFFSET BOOT_RECORD   ;Buffer with new boot record
                CALL    READ_WRITE              ;Write a sector
                MOV     DX,OFFSET MOD_COMPLETE  ;Completed message
                JNC     MESS_EXIT               ;No error
                MOV     DX,OFFSET FLOPW_FAIL    ;Error message
                JMP     MESS_EXIT               ;Exit
                ...
                ...

;Reads or writes bootrecord floppy A: or B:
;Entered AX=0201 or 0301, read or write 1 sector ES:BX=Offset bootrecord
data
READ_WRITE      PROC    NEAR                    ;Exit CF=1 when fail
                MOV     SI,0005                 ;Execute five tries if needed
TRY_AGAIN:      PUSH    AX                      ;Save function
                MOV     CX,0001                 ;Cylinder 0, sector 1
                MOV     DX,[CURRENT_FLOP]       ;Drive number
                INT     13                      ;BIOS
                JNC     FLOP_DONE               ;No, error
                XOR     AX,AX                   ;Reset
                INT     13                      ;BIOS
                POP     AX                      ;Function back
                DEC     SI                      ;Counter
                JNZ     TRY_AGAIN               ;Try 5 times
                STC                             ;Didn't succeed
                JMP     RW_EXIT                 ;Exit CF=1
FLOP_DONE:      POP     AX                      ;Clear stack
RW_EXIT:        RET
READ_WRITE      ENDP

Quote:
>From Ralf Brown's Interrupt list

INT 13 03-- - DISK - WRITE DISK SECTOR(S)

Category: B - BIOS

Inp.:
        AH = 03h
        AL = number of sectors to write (must be nonzero)
        CH = low eight bits of cylinder number
        CL = sector number 1-63 (bits 0-5)
             high two bits of cylinder (bits 6-7, hard disk only)
        DH = head number
        DL = drive number (bit 7 set for hard disk)
        ES:BX -> data buffer
Return: CF set on error
        CF clear if successful
        AH = status (see #00234)
        AL = number of sectors transferred
              (only valid if CF set for some BIOSes)
Notes:  errors on a floppy may be due to the motor failing to spin up
quickly
          enough; the write should be retried at least three times, resetting
          the disk with AH=00h between attempts

        most BIOSes support "multitrack" writes, where the value in AL
          exceeds the number of sectors remaining on the track, in which
          case any additional sectors are written beginning at sector 1 on
          the following head in the same cylinder; the CONFIG.SYS command
          MULTITRACK can be used to force DOS to split disk accesses which
          would wrap across a track boundary into two separate calls
        the IBM AT BIOS and many other BIOSes use only the low four bits of
          DH (head number) since the WD-1003 controller which is the standard
          AT controller (and the controller that IDE emulates) only supports
          16 heads
        AWARD AT BIOS and AMI 386sx BIOS have been extended to handle more
          than 1024 cylinders by placing bits 10 and 11 of the cylinder number

          into bits 6 and 7 of DH
        under Windows95, an application must issue a physical volume lock on
          the drive via INT 21/AX=440Dh before it can successfully write to
          the disk with this function
SeeAlso: AH=02h,AH=0Bh,AH=07h"V10DISK.SYS",AH=22h"PS/1",AH=43h"IBM"
SeeAlso: INT 21/AX=440Dh"DOS 3.2+",INT 4D/AH=03h
INT 13
Copied from Ralf Brown's Interrupt List

--

Eric P. van Westendorp  Tel: +31(0252)210579
Reigerslaan 22  2215NN Voorhout  Netherlands

Quote:

> Hi
>   After making a boot sector, how dould I go about writing it to the 1st sector
> of a floppy disk or hard disk?

> Steven Graham

> --
> Posted via Mailgate.ORG Server - http://www.Mailgate.ORG



Mon, 28 Jun 2004 07:56:04 GMT  
 Writing at boot sector to the first sector of a disk
If you're using DOS/Windows, the easiest thing to do is to use debug:

debug yourcode.bs

THEN in debug,

- w 100 0 0 1

This writes the code from address 100 onto drive 0 (the floppy),
beginning at sector 0, and writes 1 sector. Subsequent attempts to do
this may result in a 'Write protection error'. My reckoning on this is
that it's due to settings in the boot sector data area, but you may want
a second opinion. Anyway, just reformatting the drive will fix it.

I'm not sure how to do this in Linux et al -- I've seen instructions for
it online but since I've never had to do it I haven't made note of it.

Good luck,
CC


Quote:

> Hi
>   After making a boot sector, how dould I go about writing it to the 1st
>   sector
> of a floppy disk or hard disk?

> Steven Graham



Mon, 28 Jun 2004 09:55:52 GMT  
 Writing at boot sector to the first sector of a disk

Quote:

> Hi
>   After making a boot sector, how dould I go about writing it to the 1st sector
> of a floppy disk or hard disk?

For a floppy...

debug myboot.bin
- w 100 0 0 1

or, if you're lucky :)

dd if=myboot.bin of=/dev/fd0

There are other utilities - other posters have shown you how to write
your own.

For hard drives, the situation is a bit more complicated. The first
physical sector is the Master Boot Record, including the Partition
Table. The first logical sector on your partition is the boot-sector for
that partition. The latter is probably what you want. Obvious changes to
the floppy methods should work - if you can't figure it out, you're
probably not ready to be writing to your hard drive :)

Be careful,
Frank



Mon, 28 Jun 2004 13:55:58 GMT  
 Writing at boot sector to the first sector of a disk

Quote:

> Hi
>   After making a boot sector, how dould I go about writing it to the 1st sector
> of a floppy disk or hard disk?

> Steven Graham

Search for John Fine's page. On his page you'd find a utility called
partcopy.exe

download it to your computer and run the following command:

c:\partcopy (your binary file) 0 200 -f0

where f0 is the first floppy disk, f1 is second ...etc.

0 to 200 (hex) is the address on the disk where it will be saved. so,
it will be copied from location 0 hex to 200 hex (512 bytes)

Try it, http://www.execpc.com/~geezer/johnfine/pcopy02.zip



Tue, 29 Jun 2004 07:55:58 GMT  
 Writing at boot sector to the first sector of a disk
Hi
  Thanks for your support of my query. The only other thing
that I was wondering, if my bootsector is smaller than 512 bytes, how do I pad
it using MASM. I've seen it on nasm, but what about MASM?

Steven Graham

--
Posted via Mailgate.ORG Server - http://www.Mailgate.ORG



Sun, 04 Jul 2004 04:44:38 GMT  
 Writing at boot sector to the first sector of a disk

Quote:

> Hi
>   Thanks for your support of my query. The only other thing
> that I was wondering, if my bootsector is smaller than 512 bytes, how do I pad
> it using MASM. I've seen it on nasm, but what about MASM?

> Steven Graham

Hi,
        Hmm, "padding" a bootsector would seem to be a strange thing to
do. If my memory serves me right, the bootsector's 511/512th byte has to
be 55 AA before it is considered a valid boot sector. Since you must have
written your 511/512th byte, your bootsector must be 512 bytes already.
        On the otherhand, if you really mean what you said, then you could
simply do something like:

        trueSector DB DUP 512(?)
        originalSector  .... ; Your existing boot sector

        lea     si, originalSector
        lea     di, trueSector
        mov     cx, sizeofSector
        rep     movsb  

Ka-Chi



Sun, 04 Jul 2004 10:15:21 GMT  
 Writing at boot sector to the first sector of a disk
I think I've found the code to do it, some of the last few lines of this:

;Generic boot sector shell.  Written by Chris Lattner 1995
;Code+Data MUST be less than 510 bytes long!

_Text SEGMENT PUBLIC USE16
  assume CS:_Text, DS:_Text
  org 0

EntryPoint:
  db 0EAh  ;jmp far SEG:OFS    ;Currently we are at 0:7C00
  dw OFFSET AfterData, 7C0h    ;This makes us be at 7C0:0

;Put any data here!

AfterData:
  push CS
  pop DS     ; update DS to be 7C0 instead of 0

;Put code here!

  jmp $    ; Hang out...

org 510    ; Make the file 512 bytes long
  dw 0AA55h  ; Add the boot signature
_Text ENDS
  END

the org instruction is what I needed!
The guy who use this had done a generic segment with both data and code,
so puts the 0AA55h at the end. How would this be done with seperate data
and code segments?

Steven Graham

--
Posted via Mailgate.ORG Server - http://www.Mailgate.ORG



Tue, 06 Jul 2004 02:55:54 GMT  
 Writing at boot sector to the first sector of a disk
Hi,

Quote:
> the org instruction is what I needed!
> The guy who use this had done a generic segment with both data and code,
> so puts the 0AA55h at the end. How would this be done with seperate data
> and code segments?

> Steven Graham

You would not WANT a separate data and code segment for a boot sector,
since splitting the segments would mean that at compile time, the two sets
of code/data may not be in a contiguous area, and you cannot write to the
boot sector correctly. Not to mention that if you had two segments, then
your boot sector code must also be modified after compile time to adjust
the segments relocation information.....So much trouble, WHY??? It is only
400 odds bytes anyway!!

I forgot why you needed to modify the boot sector, but in general, if you
want to insert you custom boot process in place of the existing OS, and
still want to be able to boot to the OS, you would do the following (it's
adhoc and dirty, but it's the easist way)

1. Save you existing boot sector into a file
2. Disassemble the file to see what the original entry code is (or you can
   just incorporate the file into your custom boot process)
3. Boot sector code should do:
        Setup necessary segments information
        Load the custom boot process (by reading from a set disk position,
        or use the dir info to load your BootProcess.BIN)
4. Boot Process
        Do whatever you want to do
        Restore the system back to pre-boot state
        Run the original entry code (either by jumping directly to the
        code in the existing boot sector if you have incorporated it, or
        jump to the disassembled entry point)

If you do the above, then you would realise that there is absolutely no
need for a separate data/code segment in the boot sector code. Even your
custom boot process should be a .COM file (ie one segment) to make it
easier on your coding. If you do use .EXE and multiple segments, then you
would need to either extract the relocation information, or to hardwire
some code in the .EXE so you can relocate yourself. Unless you are
planning to write some major code, I would recommend sticking to .COM
because it's just too much hassle.

Ka-Chi



Tue, 06 Jul 2004 07:56:00 GMT  
 Writing at boot sector to the first sector of a disk

Quote:

> This writes the code from address 100 onto drive 0 (the floppy),
> beginning at sector 0, and writes 1 sector. Subsequent attempts to do
> this may result in a 'Write protection error'. My reckoning on this is
> that it's due to settings in the boot sector data area, but you may want
> a second opinion. Anyway, just reformatting the drive will fix it.

Well, the bootsector on FAT floppies contains information that DOS reads
and, when not present, it issues all kinds of errors like "cannot read disk
in A:" and stuff like that...plus, any virus protection programs you have
running might not be too happy with the bootsector (the most vurnerable part
of a system, really, because whoever controls the bootsector controls
everything to come after and, thus, the machine itself :) being messed
around with willy-nilly...but it's possible to make the disk FAT-compatible
(by filling in some data fields in the bootsector and having FATs and
directories and stuff :) but with your own code instead (well, at least, DOS
will be happy with that...a strict virus program might get fussy that the
bootsector is not identical to a set of standard bootsectors that each DOS
and Windows generates and signal it as "an evil virus" when it's nothing of
the sort :)...

So, yes, it's "settings in the boot sector data area", as you put
it...although, to my ear, that makes it sound more complex than it really
is...it's just a bunch of data in the boot sector telling it where things
are, like the FAT tables and stuff...plus, a string field that has the name
of the OS that formatted the disk...that sort of thing, which it obviously
needs, to know where things are stored on the disk...basically, DOS reads
the information in the bootsector to find out what's what and if it's not as
expected, it'll start complaining about not being able to read the disk or
that the disk is damaged or other {*filter*}like that...when, really, the error
message should be something more like " 'This is not a DOS disk, so I have
no idea how to handle it...so, it's probably best to just leave it alone and
refuse to work with it, I think' error"...

Beth:)



Wed, 07 Jul 2004 08:56:00 GMT  
 
 [ 11 post ] 

 Relevant Pages 

1. Need help reading/writing boot sector (floppy disk)

2. Boot sector for floppy disk

3. boot disk sector

4. boot sector of a disk

5. PROBLEM ON BOOT SECTOR OF A 1 Go PC Hard Disk - HELP NEEDED

6. boot sector writing

7. Writing To Boot Sector

8. Write a boot sector

9. writing to floppy boot sector

10. boot sector writing

11. MS-DOS Boot Sector & Boot Programs

12. DOS Sector => Physical Sector

 

 
Powered by phpBB® Forum Software