Thread: Writing a boot sector. I must be mad...

  1. #1
    Registered /usr
    Join Date
    Aug 2001
    Location
    Newport, South Wales, UK
    Posts
    1,273

    Red face Writing a boot sector. I must be mad...

    Hello,

    I thought I'd start a new week with something completely different from my usual Windows-based exploits, so I've had a go at making a boot sector program. Naturally this can't be done in C, so I also had a crash course in Assembly Language (Hugs his copy of "Assembly Language for Intel-Based Computers"), which lasted about ten minutes.

    Anywho, after ressurecting the Turbo C compiler I used to use, I proceded to write the following:-
    Code:
    .model tiny
    .code
    
    org 0
    
    jmp start
    welcome db 'Hello non-OS World!',0
    
    start:
    mov ax, 07C0h
    mov ds, ax
    mov es, ax
    mov si, offset welcome
    
    print:
    
    lodsb
    cmp al, 0
    je finish
    
    mov ah, 0Eh
    mov bx, 7
    int 10h
    
    jmp print
    
    finish:
    
    jmp finish
    
    org 510
    dw 0AA55h
    
    end
    I pushed the code through TASM, then tried TLINK /t for a .COM file, but it wasn't to be ("Invalid entry point"), so I had to settle for an .EXE. I ripped the last 512 bytes from the executable, wrote it to the boot sector of a floppy disk, and rebooted.

    It didn't quite seem to go to plan, as all that happened was that 3 non-alphanumeric characters were displayed and that was it.

    Now obviously, as a result of writing that boot sector, the disk ceased to work as a storage medium and was completely unreadable before I formatted it. This leads me to believe that:-

    1. I overwrote the FAT information. Whoops...
    2. There must be certain mandatory fields of data required on every disk regardless of file system.

    Am I right?

  2. #2
    Registered /usr
    Join Date
    Aug 2001
    Location
    Newport, South Wales, UK
    Posts
    1,273
    Believe it or not Salem, that was the place I started from

    I made my code to be as similar to that presented on that site, but they all use NASM, which is a bit different. I've now worked out that my problems must stem from not being able to locate my data. It's in segment 7C00 somewhere...

  3. #3
    &TH of undefined behavior Fordy's Avatar
    Join Date
    Aug 2001
    Posts
    5,793
    I would have to aggree with Salem...the bootsector needs to be loaded at a specific region of memory - usually 0x7C00......

    Also you cant use a EXE as it will have the DOS signiture and header at the start...you should use a COM file

    I have one I did in MASM once...I'll see if I can dig it out for ideas (MASM and TASM are pretty close)

  4. #4
    Registered /usr
    Join Date
    Aug 2001
    Location
    Newport, South Wales, UK
    Posts
    1,273
    Originally posted by Fordy
    I would have to aggree with Salem...the bootsector needs to be loaded at a specific region of memory - usually 0x7C00......
    Hmm, looking at other examples they seem to use "org 7C00h" instead of "org 0". I've tried that now, but little improvement (A single non-alphanumeric character is printed now... )

    Also you cant use a EXE as it will have the DOS signiture and header at the start...you should use a COM file
    Originally posted by SMurf
    I ripped the last 512 bytes from the executable...
    ...hence eliminating the sig and headers.

    Originally posted by Fordy
    I have one I did in MASM once...I'll see if [I can dig it out for ideas (MASM and TASM are pretty close)
    Am I really that out of touch? It seems all the kids these days use NASM. Bring back the 80's, that's what I say (Not that I was alive for four of those years, but...)

  5. #5
    &TH of undefined behavior Fordy's Avatar
    Join Date
    Aug 2001
    Posts
    5,793
    Originally posted by SMurf
    ...hence eliminating the sig and headers.
    Oops.....didnt see that......would still try to use a COM file though dont know much about the old DOS EXE format apart from the header at the start of the file.........


    Originally posted by SMurf
    Am I really that out of touch? It seems all the kids these days use NASM. Bring back the 80's, that's what I say (Not that I was alive for four of those years, but...)
    Ssome people still like MASM...I like it because of the windows stuff that comes with the free version released by Hutch

  6. #6
    &TH of undefined behavior Fordy's Avatar
    Join Date
    Aug 2001
    Posts
    5,793
    Ok...here's a MASM version of a simple bootstrap

    Code:
    .model tiny
    
    
    
    COMSEG SEGMENT
    org 7C00h
    start:
    
            mov ax, 00h         
            mov ds, ax
            
            lea si,[ds: MyStr]
            mov ah, 0Eh           
            mov bh, 00h            
            mov cx,15
    loopit:	lodsb
            int 10h
            LOOP loopit
    
            jmp $                   
    
    	MyStr db "Hello Bootstrap" 
    	db 510-($-start) dup (0)   ;Loads of zeroes
            dw 0AA55h              
    
    COMSEG ENDS
    end start
    Now this is a simple 1 segment piece of code that houses the display string, the executable code, the ending sig and a lot of zeros (there's a little trick on with the DB statement that works for MASM)...now all the code does is to blast a message to the screen and sit on the Jmp $ line indefinately.........

    to assemble you use

    ml /AT /c /Zm Bootstrap.asm youramsfile.asm
    This assembles the code as a single segment chunk without linking and sets the assembler to an older version (probably worth doing)

    Now to link (for this you need the old DOS version of M$ Link...if you hav an anciant version of Visual C that's great...or otherwise go to Iczelion's site for a free download)

    LINK /TINY youramsfile
    Keep pressing enter until the program terminates...you ill get 2 warnings -

    LINK : warning L4045: name of output file is Bootstrap.com'
    LINK : warning L4055: start address not equal to 0x100 for /TINY
    The first one is fine as there was no need to worry about a specific file name (you just want the contents) and the second one is obviously unavoidable as this isnt a standard COM file!

    Now open the COM file and you will see a little code......a string....lots of zeros and the end sig...and all at 512 bytes - horrah!

    Now throw your Win2K/XP disk in the drive and install the Support tools (full option).....this gives you a neat little tool called dskprobe.exe....run this prog and open the COM file...you will see the 512 bytes binary you saw earlier.....now open a logical drive....double click on A: and set the handle as "Active"....the write the 512 bytes to the first sector...then boot from the disk and you now have a (very limited) boot disk.......you will need to reformat the disk to reuse it as you didnt go to the effort of copying a file system (though if you are feeling challenged you can look at the FAT format and add that into your code too)

    If you like the idea of this, then visit Compuboy's site - http://www.electrichamster.net/Lucie as the above code is just my port of his NASM code he includes in his Bootloader walkthrough

  7. #7
    Registered /usr
    Join Date
    Aug 2001
    Location
    Newport, South Wales, UK
    Posts
    1,273
    Bah. I downloaded MASM and the DOS linker, assembled and linked your code, placed it onto the boot sector of a disk and rebooted. All I got was a "H", an up-down arrow character, a space, a smiley face, another two spaces, then "bootstrp" and that's it.

    If you've got a built and working version of this code could you send it to me? Either I'm going wrong somewhere in the building process or my BIOS is one of those awkward ones that destroys register values at leisure.

  8. #8
    &TH of undefined behavior Fordy's Avatar
    Join Date
    Aug 2001
    Posts
    5,793
    Maybe you are doing something wrong...

    Here's the COM file...it wont run as a prog for obvious reasons....if you cant get it working, I guess your methods for writting it to disk are wrong

  9. #9
    Registered /usr
    Join Date
    Aug 2001
    Location
    Newport, South Wales, UK
    Posts
    1,273
    Very, very strange...

    I can't get your bootsector to actually display the string it's supposed to, but I replaced your infinite loop with:-
    Code:
    Main_Loop:
    mov     ah,1                    ; Loop until key is pressed
    int     16h
    jz      Main_Loop
    xor     ah,ah                   ; Remove key
    int     16h
    
    finish:
    int 19h
    This goes through the boot process again when a key is pressed (Much like the normal "non-system disk" program). When I load this at boot, a string of gibberish is displayed, but if I press any key, it does initiate INT 19h. This leads me to believe that the code is working but the data isn't being referenced properly. Perhaps my BIOS is dumb and needs a guiding hand or something?

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Folding@Home Cboard team?
    By jverkoey in forum A Brief History of Cprogramming.com
    Replies: 398
    Last Post: 10-11-2005, 08:44 AM
  2. dual boot Win XP, win 2000
    By Micko in forum Tech Board
    Replies: 6
    Last Post: 05-30-2005, 02:55 PM
  3. help! fifo read problem
    By judoman in forum C Programming
    Replies: 1
    Last Post: 08-16-2004, 09:19 AM
  4. freebsd and redhat dual boot.
    By xddxogm3 in forum Linux Programming
    Replies: 1
    Last Post: 05-09-2004, 06:06 PM
  5. CD Boot - Need Help
    By (TNT) in forum A Brief History of Cprogramming.com
    Replies: 16
    Last Post: 10-19-2001, 12:29 PM