Post Reply 
 
Thread Rating:
  • 0 Votes - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Simple Boot Disk and Typer
10-07-2009, 09:23 AM
Post: #1
Simple Boot Disk and Typer
This is something I keep coming back to every now and then, my boot disk. I like this project/toy of mine because its all in 16 bit programming and well gcc is 32 and 64 bit(with limited support for 16 bit) so it makes for some playing around to get the code stripped down and working but in the end its really rewarding doing something at this level...

So what does my boot disk do? Well not very much, It'll boot a Intel/AMD machine load some code, create a stack, execute and then wait for the user to press a key - when the user presses a key it'll display it...Yeah I know pretty fantastic but its just a programing toy.

So if the user presses the keys "slsdkflskjflsdksdsldjf"

The program will display "slsdkflskjflsdksdsldjf" when the user has pressed more than 2000 keys the program will loop back to the top of the screen again and overwrite what's in place....Like I said very low featured...Anyways here's the code

asem code that I stripped down and placed in the hex array in the C code below
Code:
.code16

.section .data

.section .text
    .global _start
_start:
            movw    $0xb800, %ax
            movw    %ax, %es
            movw    $0x8000, %ax
            movw    %ax, %ss
            movw    $0x0, %sp

            xorw    %bx, %bx

clearit:
            movb    $0x20, %es:(%bx)
            incw    %bx
            movb    $0x1f, %es:(%bx)
            incw    %bx

            cmpw    $4000, %bx
            jle    clearit



            call    tohere
        
            xorw    %bx, %bx
loop1:
            movb    $0, %ah
            int    $0x16

            cmpw    $4000, %bx
            jge    loop2
            
            movb    %al, %es:(%bx)
            addw    $2, %bx
            jmp    loop1

loop2:
            xorw    %bx, %bx
            movb    %al, %es:(%bx)
            addw    $2, %bx
            jmp    loop1

tohere:
            movb    $0x47, %es:0
            movb    $0x1f, %es:1

            movb    $0x34, %es:2
            movb    $0x1f, %es:3

            movb    $0x31, %es:4
            movb    $0x1f, %es:5

            movb    $0x34, %es:6
            movb    $0x1f, %es:7

            movb    $0x33, %es:8
            movb    $0x1f, %es:9

            movb    $0x20, %es:10
            movb    $0x1f, %es:11    

            movb    $0x48, %es:12
            movb    $0x1f, %es:13    

            movb    $0x61, %es:14
            movb    $0x1f, %es:15    

            movb    $0x63, %es:16
            movb    $0x1f, %es:17

            movb    $0x6b, %es:18
            movb    $0x1f, %es:19

            movb    $0x65, %es:20
            movb    $0x1f, %es:21    

            movb    $0x72, %es:22
            movb    $0x1f, %es:23            

            movb    $0x20, %es:24
            movb    $0x1f, %es:25    

            movb    $0x46, %es:26
            movb    $0x1f, %es:27    

            movb    $0x6f, %es:28
            movb    $0x1f, %es:29

            movb    $0x72, %es:30
            movb    $0x1f, %es:31

            movb    $0x75, %es:32
            movb    $0x1f, %es:33    

            movb    $0x6d, %es:34
            movb    $0x1f, %es:35    

            movb    $0x73, %es:36
            movb    $0x1f, %es:37                    

            movb    $0x20, %es:38
            movb    $0x1f, %es:39
            
            ret


C code - The boot disk for Linux or a boot image that can be used with QEMU
Code:
#include<stdio.h>
#include<stdlib.h>
#include<fcntl.h>

char boot_buf[512] =    {
0xB8,0x00,0xB8,0x8E,0xC0,0xB8,0x00,0x80,0x8E,0xD0,0xBC,0x00,0x00,0x31,0xDB,0x26,​0xC6,0x07,0x20,0x43,
0x26,0xC6,0x07,0x1F,0x43,0x81,0xFB,0xA0,0x0F,0x7E,0xF0,0xE8,0x1E,0x00,0x31,0xDB,​0xB4,0x00,0xCD,0x16,
0x81,0xFB,0xA0,0x0F,0x7D,0x08,0x26,0x88,0x07,0x83,0xC3,0x02,0xEB,0xEE,0x31,0xDB,​0x26,0x88,0x07,0x83,
0xC3,0x02,0xEB,0xE4,0x26,0xC6,0x06,0x00,0x00,0x47,0x26,0xC6,0x06,0x01,0x00,0x1F,​0x26,0xC6,0x06,0x02,
0x00,0x34,0x26,0xC6,0x06,0x03,0x00,0x1F,0x26,0xC6,0x06,0x04,0x00,0x31,0x26,0xC6,​0x06,0x05,0x00,0x1F,
0x26,0xC6,0x06,0x06,0x00,0x34,0x26,0xC6,0x06,0x07,0x00,0x1F,0x26,0xC6,0x06,0x08,​0x00,0x33,0x26,0xC6,
0x06,0x09,0x00,0x1F,0x26,0xC6,0x06,0x0A,0x00,0x20,0x26,0xC6,0x06,0x0B,0x00,0x1F,​0x26,0xC6,0x06,0x0C,
0x00,0x48,0x26,0xC6,0x06,0x0D,0x00,0x1F,0x26,0xC6,0x06,0x0E,0x00,0x61,0x26,0xC6,​0x06,0x0F,0x00,0x1F,
0x26,0xC6,0x06,0x10,0x00,0x63,0x26,0xC6,0x06,0x11,0x00,0x1F,0x26,0xC6,0x06,0x12,​0x00,0x6B,0x26,0xC6,
0x06,0x13,0x00,0x1F,0x26,0xC6,0x06,0x14,0x00,0x65,0x26,0xC6,0x06,0x15,0x00,0x1F,​0x26,0xC6,0x06,0x16,
0x00,0x72,0x26,0xC6,0x06,0x17,0x00,0x1F,0x26,0xC6,0x06,0x18,0x00,0x20,0x26,0xC6,​0x06,0x19,0x00,0x1F,
0x26,0xC6,0x06,0x1A,0x00,0x46,0x26,0xC6,0x06,0x1B,0x00,0x1F,0x26,0xC6,0x06,0x1C,​0x00,0x6F,0x26,0xC6,
0x06,0x1D,0x00,0x1F,0x26,0xC6,0x06,0x1E,0x00,0x72,0x26,0xC6,0x06,0x1F,0x00,0x1F,​0x26,0xC6,0x06,0x20,
0x00,0x75,0x26,0xC6,0x06,0x21,0x00,0x1F,0x26,0xC6,0x06,0x22,0x00,0x6D,0x26,0xC6,​0x06,0x23,0x00,0x1F,
0x26,0xC6,0x06,0x24,0x00,0x73,0x26,0xC6,0x06,0x25,0x00,0x1F,0x26,0xC6,0x06,0x26,​0x00,0x20,0x26,0xC6,
0x06,0x27,0x00,0x1F,0xC3
            };

int main(int argc, char**argv)
{
    int floppy_desc;

boot_buf[510]=0x55;//to make the floppy/image bootable
boot_buf[511]=0xaa;//to make the floppy/image bootable

floppy_desc=open("/dev/fd0",O_RDWR);//boot disk
//floppy_desc=open("dosimage",O_RDWR|O_CREAT, 0666);//boot image

lseek(floppy_desc,0,SEEK_CUR);
write(floppy_desc,boot_buf,512);
close(floppy_desc);
}

If you want to port this to windows then you'll have to port this line
Code:
floppy_desc=open("/dev/fd0",O_RDWR);//boot disk
Find all posts by this user
Quote this message in a reply
10-07-2009, 09:28 AM
Post: #2
RE: Simple Boot Disk and Typer
Nice ... Now you're getting into some advanced C stuff(Also assembly Tongue ) I have only a minor understanding as to what code is doing what in the assembly .. but the C is sort of straight forward ( except for the hex) also is fcntl.h a windows library ( or standard?)

"Character is determined more by the lack of certain experiences than by those one has had."
Friedrich Nietzsche
Visit this user's website Find all posts by this user
Quote this message in a reply
10-07-2009, 09:52 AM (This post was last modified: 10-07-2009 10:03 AM by G4143.)
Post: #3
RE: Simple Boot Disk and Typer
(10-07-2009 09:28 AM)Back_track Wrote:  Nice ... Now you're getting into some advanced C stuff(Also assembly Tongue ) I have only a minor understanding as to what code is doing what in the assembly .. but the C is sort of straight forward ( except for the hex) also is fcntl.h a windows library ( or standard?)

Everything in the above C program is portable except fcntl.h and these lines

Code:
floppy_desc=open("/dev/fd0",O_RDWR);//boot disk
//floppy_desc=open("dosimage",O_RDWR|O_CREAT, 0666);

If you want to port it to windows then you have to find a way to write to the floppy device directly or binary write to a file to make a boot image that QEMU can boot from...
Find all posts by this user
Quote this message in a reply
Post Reply 


Forum Jump:


 Quick Theme: