# Jump to other code in boot 0



## masghari (Jan 10, 2010)

Is it possible to have some code in a desired location of the hard disk and modify the boot0 code in a way that it jumps to that location, executes the program and returns to it's previous location? I believe there's not much space remained in boot0 to add more commands, but can this be done only using a "call" or is it more complicated then I think?


----------



## Alt (Jan 10, 2010)

You must load 'desired location of the hard disk' into memory before..


----------



## Beastie (Jan 10, 2010)

Of course you can, but the entire code must be in the first, unused, track or else you may overwrite something important in your filesystem or the system may later overwrite your code. In brief:
1. Move some of the boot0 code to your own boot0bis.
2. In the now free space in boot0, add BIOS code to read* boot0bis into a free memory location** and a jmp to that location.
3. In boot0bis, run the boot0 code you've previously moved, and at the end, add a jmp to jump back to boot0's memory location (0x600).


* /usr/src/sys/boot/i386/boot0/boot0ext.S already contains such code at line 350.
Check Ralf Brown's Interrupt List for more information on int 0x13, function 2.

** Make sure you don't overwrite anything vital (e.g. IDT, GDT, etc.)
You can get a small memory map for FreeBSD at the beginning of /usr/src/sys/boot/i386/boot2/boot1.S (*Memory Locations* comment).


----------



## aragon (Jan 11, 2010)

masghari said:
			
		

> I believe there's not much space remained in boot0 to add more commands


The whole point of boot0's design is to make it fit in 446 bytes.  Of course it is possible to make a boot loader jump to another location on the disk, but if you're considering labouring through adding that functionality you should know that there are already a few boot loaders like sysutils/grub that work precisely like that.


----------

