NirajJha wrote:Can i test this on virtual box vdi image. DO i need to copy of the 1st stage code 0200000 to 2001F0 and second stage from 0200600 to 0200B12 or from 0200200 .
Stage1 must be 200h bytes long, and stage2 must be multiple of 200h bytes. I suggest to create raw image, and copy the stage1 in the first sector of the disk at offset 0 (if you want to use it as an MBR) or in the first sector of a partition (if you intend to use it as VBR), and save the stage2's first lba address at dword 0x1B0 (which will be 2 if you add the stage2 right after the MBR and before the first partition).
I cannot tell you exactly where my stage2 is located, as I copy it as a simple file on the first partition. Depending on configuration, that first partition is either a FATx (in which case I use BOOTBOOT\LOADER) or a partition with my own fs,
FS/Z (in that case I use /sys/loader). Regardless my disk creation utility (
mkfs disk) locates the stage2 part and records it's starting sector in the MBR (and also stores stage1 code there). So when my image (bin/disk.dd) is finished, everything is at place to boot. If you want to test it with Virtualbox, you can use VBoxManage utility to create vdi from the raw image (part of Virtualbox, see my makefile for
make vdi).
Other than that if you meant memory addresses, you cannot access memory above 0FFFFh in real mode (assuming your segment is 0). The stage1 is loaded at 7C00h (or 7C0h:0) by the BIOS, and then it relocates itself to 0:600h. The stage2 (which is loaded by stage1, GRUB or already accessible in ROM above C8000h) relocates itself after that, at 0:800h if necessary (the memory map is described in the beginning of the source
in detail).
If you use BOOTBOOT stage1 and roll your own stage2, you should use ORG 800h, zero out segment registers and the stack pointer.
Code: Select all
ORG 800H
SecondSector:
DB 55H, 0AAH, 0, 0E9H ; magic bytes, don't care, don't change
DW main ; label to execute on start
main:
xor ax, ax
mov sp, ax ; see below
mov ds, ax
mov es, ax
...etc.
You shouldn't bother with size, checksum and relocation at all (as you don't want to store your stage2 in ROM) therefore the first 6 bytes won't change. Now I've also used a little trick here with the stack. When you push your first item on the stack, it will decrease the sp pointer from 0 which will wrap around to 0FFFEh and will store the item there. That's the highest address you can use in small model in real mode, therefore it provides the biggest space for your program and the stack. You can of course use "mov sp, 07C00h" if you like.
You can place your stage2 code anywhere on the disk (as long as it's sector aligned, that is), just don't forget to save the starting LBA address in stage1's sector (that can be detected with a hexeditor the same way my mkfs utility does: look for the magic bytes, then use offset/512+1). If you're not creating a raw image and convert it to vdi afterwards as suggested, then you should subtract the size of the vdi header (in other words the offset of MBR) from the offset first. So for example if vdi header is 200000h bytes long, mbr.bin should be copied to 200000h-2001FFh. If you choose to save your stage2 right after it, then store that to 200200h-207DFFh (or less if it's smaller), and set 2 at 2001B0h. If you choose to create a partition with your stage2 as a file on it, then locate the magic bytes in the vdi. Let's say they are at 201600h, then 2001B0h should be 0Ch ('cos (201600h-200000h)/200h+1=0Ch).