loading file from bootstrap

Question about which tools to use, bugs, the best way to implement a function, etc should go here. Don't forget to see if your question is answered in the wiki first! When in doubt post here.
Post Reply
sham

loading file from bootstrap

Post by sham »

hello

i have a little problem here, as bootstrap cant be more than
512 bytes and i want to write code for an editor in my os, for this reason i have to write editor code outside of my bootstrap code, right ???.

so how can i do the following task on winxp/2000 based system using nasm.

1. code which i can use in my bootstrap to load the file which
contain editor code ?

2. how can i assemble these two programs, and do i have to link them ?

3. i know that "debug boot.bin" and then "-w 100 0 0 1" will write my bootstrap on floppy but how can i write my edit program on floppy and at what address (pls explain)?

many thanks for your help

sham :)
AR

Re:loading file from bootstrap

Post by AR »

1. The bootsector is only 1 sector in size, if you need more to load the second stage loader (or Kernel directly) then you can just "Int 10h" the rest of the sectors in (presuming they're contiguous). To load the Kernel off a disk without a filesystem you just read it sector by sector from whereever you put it. If there is a filesystem (ie. FAT) then you'll need to read the root directory and FAT to find and load the Kernel.

2. The Kernel and loader are seperate programs (per se), they are not directly linked to each other. The loader simply loads the Kernel off the disk then executes a JMP instruction to enter the Kernel's code. You would assemble them using "nasm -f bin MyBootSector.asm -o Boot.bin" and "nasm -f bin Kernel.asm -o Kernel.bin"

3. You'd write it out to the disk by opening it and changing the write location, debug's help says it's in the format of: "[address] [drive] [firstsector] [number]" so assumeably if you wanted to write the Kernel out you'd "debug Kernel.bin" then "w 100 0 5 10". The command is untested but it should write out a 10*512 Kernel starting at sector 5 (which is the 6th from the start) on the disk [note: I don't know what the "address" field is].
Red Shaya

Re:loading file from bootstrap

Post by Red Shaya »

Hello Sham

You are describing a situation where you have a boot loder that needs to load the rest of your OS.

The boot loder loads to adress 0000:7c00. You should write it so it loads your second bin file (lets call it EDITOR.BIN) from the diskette to a memory location (0000:7e00 for example). If you are using BIOS interrupts than look at INT 13 to see how to read sectors from the disk to memory.

after you have loaded the EDITOR.BIN into memory you need to make a JMP to that memory address.

Here are some suggestions. COM files are automatically loaded into CS:0100, so if you use ORG 0100 at your starting address you could use DEBUG.EXE for debugging your code under DOS.
(Just rename the file from EDITOR.BIN to EDITOR.COM if you want to run it as a seperate file outside DEBUG)

After debugging the EDITOR.BIN you'll have a working code.
Now load the code into memory by the bootloder.
If you used ORG 0100 your code assumes its starting address is at 0100 but in reality it is loaded at 7e00. To fix this problem you'd have to change the CS register too. The CS register cannot be changed directly (By MOV command for example) to change it make a far JMP to 07d0:0100.

About writing your code to the disk using DEBUG:
I keep sectors 2-4 free for OS parameters and file managment tables so I place my first OS code on sector 5 (you can choose any place you like)
To write EDITOR.BIN into sector 5 do the following:
DEBUG EDITOR.BIN ; this will load EDITOR.BIN to CS:0100
-W 100 0 4 n ; n = number of sectors to write from memory address 0100 starting from sector 5 (4+1)

to better understand the R(ead) and W(rite) commands onsider the following example:

DEBUG ; execute debug
-n EDITOR.BIN ; our file is called EDITOR.BIN
-l 7c00 ; load EDITOR.BIN into memory address 7C00
-w 7c00 0 10 8 ; write from memory address 7c00 into drive 0 (diskette drive A:) from sector 11, 8 sectors (4K bytes)
User avatar
Solar
Member
Member
Posts: 7615
Joined: Thu Nov 16, 2006 12:01 pm
Location: Germany
Contact:

Re:loading file from bootstrap

Post by Solar »

If you are more interested in getting beyond bootloader stage than in how to write a bootloader, you should check out GRUB, which is a readily available, versatile bootloader with a very well-defined bootloader / kernel interface.

Check out the OS-FAQ for details (especially the "BareBones" page).
Every good solution is obvious once you've found it.
Post Reply