Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

Simple Bootloader to Run C program

102 views
Skip to first unread message

oraclerob

unread,
Dec 22, 2009, 9:33:05 PM12/22/09
to

I'm thinking about some of the projects we did at Uni some time ago...
you know the sort whereby we had a blank LSI or PDP-11 and we wrote
simple keyboard handlers and the like.... And looking at Minux lately
has got me thinking about why I got into this in the first place. The
romance of low level programming seems to have disappeared completely.

Recently using Vmware and writing to a floppy image I have been
experimenting with some helloworld bootload 16 bit asm code and it has
got me thinking about whether I could also load a C helloworld program
(linked of course) and get the bootload code to run it.

Looking at this, I'm beginning to think this might be a monumental
task. Maybe a floppy seek needs to be written? The asm needs to jmp to
a memory location and then unload itself? Then what about the linked C
code? it probably needs a number of kernel modules linked just to run?

What do you guys think? are there simple examples or would I be better
looking at what Minux does as a starting example or is this just a
pipe dream for basically a hobby project?

Tim Roberts

unread,
Dec 23, 2009, 5:03:23 AM12/23/09
to

oraclerob <rob.m...@MUNGED.microcosmotalk.com> wrote:
>
>Recently using Vmware and writing to a floppy image I have been
>experimenting with some helloworld bootload 16 bit asm code and it has
>got me thinking about whether I could also load a C helloworld program
>(linked of course) and get the bootload code to run it.

The problem with this is that a "linked" program is not a loadable memory
image. It requires additional processing in order to become runnable,
provided by a system loader of some kind that can interpret the linker
tables in the executable. You don't have that loader at boot time. That
is one of the services provided by lilo and grub; besides providing a user
interface, their last step is to be a loader to read in the Linux kernel
image (which is an executable file) and turn it into a runnable memory
image.
--
Tim Roberts, ti...@probo.com
Providenza & Boekelheide, Inc.

Alexei A. Frounze

unread,
Dec 23, 2009, 5:04:29 AM12/23/09
to

On Dec 22, 6:33 pm, oraclerob <rob.masc...@MUNGED.microcosmotalk.com>
wrote:

You can play with mine:
http://alexfru.narod.ru/os/fat/fat.html

You can modify it to switch to protected 32-bit mode and do much more
interesting stuff there. For that you'll need to use a 32-bit compiler
(Watcom, DJGPP/GCC, etc) at some point.

Alex

Rod Pemberton

unread,
Dec 23, 2009, 6:59:26 AM12/23/09
to

"oraclerob" <rob.m...@MUNGED.microcosmotalk.com> wrote in message
news:4b318161$0$4973$9a6e...@unlimited.newshosting.com...

It's doable. It's some work. alt.os.development is more suited to the
question. A few guys there have done exactly this, but usually to start
assembly code. C requires that you setup a stack and stack frame that is
compatible with the _specific_ C compiler's prolog and epilog. You then
need a method to tell your linker and/or loader code where the initial code
entry point is. This can be simple or hard depending on the compiler. You
may need to write a small amount of assembly as a header for this, even if
you're primarily using C code. Your bootloader will likely be 16-bit. If
your C code is 32-bit or 64-bit, you'll need to also switch the cpu to
32-bit or 64-bit mode from 16-bit.

Another problem with C code is that portions of the C library are dependent
on the host OS. I.e., you can't use them in your OS. Most write their own
small C library for functions like memcpy() and sprintf() etc. I did it the
other way around. Some parts of the C library are simple enough that they
are OS independent. You can use these "clean" parts of the host C library
without issues, or you can eliminate it later on. This is faster, but the
functions will have to be eliminated once the OS becomes self-hosting. I
can post a link to other posts of mine that mention what functions are
typically "clean", if you want.

C has other issues too that require assembly: need assembly wrappers around
interrupt routines, no special x86 instructions, etc.

But, I'd recommend a Multiboot compliant bootloader, e.g., GRUB or GRUB4DOS.
A Multiboot loader will load ELF (e.g., produced by GNU toolchain or Unix
environments) and other .obj's or executable code if the values in the
header are setup correctly. This will start your code as 32-bit, if it has
the multi-boot header. If you're doing 16-bit, you'll need something else.
If you're doing 64-bit, you'll need to write a 32-bit to 64-bit stub. There
are also other types of loaders available. I.e., you can load MS-DOS .exe's
or .obj's, or .coff or ELF .obj's and executables directly. You can look
at:


Coff2Com and OSLoader by Alexei Frounze (coff .obj's)
http://alexfru.chat.ru/programming/coffutil.zip
from here in "Coff Utils" section:
http://alexfru.chat.ru/epm.html

BootProg by Alexei Frounze (.com's, .exe's)
http://alexfru.chat.ru/programming/bootprog.zip
from here in "Bootsector that loads ..." section:
http://alexfru.chat.ru/epm.html

RunReloc by Chris Giese (elf, djgpp coff, win32 pe coff .obj's)
http://geezer.osdevbrasil.net/osd/exec/runreloc.zip
from here:
http://geezer.osdevbrasil.net/os/index.htm

DOS Multiboot loader by Chris Giese (multi-boot loader for DOS)
http://geezer.osdevbrasil.net/temp/mbload.zip
from here:
http://geezer.osdevbrasil.net/os/index.htm

HTH,


Rod Pemberton


oraclerob

unread,
Dec 23, 2009, 7:01:06 AM12/23/09
to

On Dec 23, 6:04 pm, "Alexei A. Frounze"

Hi Alex, I have had a quick look at yours and a more indepth look at
fatbootsect. One of the prereqs is that I can load this in using a
VMware floppy boot image as I do not have easy access to a floppy
disk. I do not see anything in your instructions regarding the use of
VMware. Can this be done? what you have provided looks very good.

Jim Leonard

unread,
Dec 23, 2009, 12:24:34 PM12/23/09
to

On Dec 23, 5:59=A0am, "Rod Pemberton" <do_not_h...@nohavenot.cmm> wrote:
> BootProg by Alexei Frounze (.com's, .exe's)http://alexfru.chat.ru/program=
ming/bootprog.zip
> from here in "Bootsector that loads ..." section:http://alexfru.chat.ru/e=
pm.html

BTW, this is some of my favorite asm source this decade. Nice and
clean, performs as advertised with 13 bytes to spare, and generally
good source to learn x86 asm from.

Alexei A. Frounze

unread,
Dec 23, 2009, 4:05:43 PM12/23/09
to

On Dec 23, 4:01 am, oraclerob <rob.masc...@MUNGED.microcosmotalk.com>
wrote:

There's nothing VMWare-specific in the code. It's general-purpose.

> Can this be done? what you have provided looks very good.

Try it. There're prebuilt floppy images that you can use right away
(fdda?.img).

Alex

oraclerob

unread,
Dec 23, 2009, 7:44:31 PM12/23/09
to

On Dec 24, 5:05 am, "Alexei A. Frounze"

Hi Alex, did you know that Nod32 thinks the pre built floppy images
are threats and quarantines them?

David

unread,
Dec 24, 2009, 4:05:53 AM12/24/09
to

You can also use a USB Flash Drive and replace the boot sector. Any
recent BIOS should permit booting from a USB drive. If you want to
see how, get Symantec's Ghost Solutions, create a USB flash drive
image on the drive, then use WinHex to see how sectors are
constructed. Just loading the MBR and PBR into debug.exe will show
you the instruction sequence.

On 24 Dec 2009 00:44:31 GMT, oraclerob

Alexei A. Frounze

unread,
Dec 24, 2009, 9:30:19 AM12/24/09
to

On Dec 23, 4:44=A0pm, oraclerob <rob.masc...@MUNGED.microcosmotalk.com>
wrote:
> On Dec 24, 5:05=A0am, "Alexei A. Frounze"
>
>
>
> <alexfrun...@MUNGED.microcosmotalk.com> wrote:
> > On Dec 23, 4:01=A0am, oraclerob <rob.masc...@MUNGED.microcosmotalk.com>
> > wrote:
>
> > > On Dec 23, 6:04=A0pm, "Alexei A. Frounze"
>
> > > <alexfrun...@MUNGED.microcosmotalk.com> wrote:
> > > > On Dec 22, 6:33=A0pm, oraclerob <rob.masc...@MUNGED.microcosmotalk.=
com>
> > > > wrote:
>
> > > > > I'm thinking about some of the projects we did at Uni some time a=
go...
> > > > > you know the sort whereby we had a blank LSI or PDP-11 and we wro=
te
> > > > > simple keyboard handlers and the like.... =A0And looking at Minux=
lately
> > > > > has got me thinking about why I got into this in the first place.=
The
> > > > > romance of low level programming seems to have disappeared comple=

tely.
>
> > > > > Recently using Vmware and writing to a floppy image I have been
> > > > > experimenting with some helloworld bootload 16 bit asm code and i=
t has
> > > > > got me thinking about whether I could also load a C helloworld pr=

ogram
> > > > > (linked of course) and get the bootload code to run it.
>
> > > > > Looking at this, I'm =A0beginning to think this might be a monume=
ntal
> > > > > task. Maybe a floppy seek needs to be written? The asm needs to j=
mp to
> > > > > a memory location and then unload itself? Then what about the lin=
ked C
> > > > > code? it probably needs a number of kernel modules linked just to=
run?
>
> > > > > What do you guys think? are there simple examples or would I be b=
etter
> > > > > looking at what Minux does as a starting example or is this just =

a
> > > > > pipe dream for basically a hobby project?
>
> > > > You can play with mine:http://alexfru.narod.ru/os/fat/fat.html
>
> > > > You can modify it to switch to protected 32-bit mode and do much mo=
re
> > > > interesting stuff there. For that you'll need to use a 32-bit compi=

ler
> > > > (Watcom, DJGPP/GCC, etc) at some point.
>
> > > > Alex
>
> > > Hi Alex, I have had a quick look at yours and a more indepth look at
> > > fatbootsect. One of the prereqs is that I can load this in using a
> > > VMware floppy boot image as I do not have easy access to a floppy
> > > disk. I do not see anything in your instructions regarding the use of
> > > VMware.
>
> > There's nothing VMWare-specific in the code. It's general-purpose.
>
> > > Can this be done? what you have provided looks very good.
>
> > Try it. There're prebuilt floppy images that you can use right away
> > (fdda?.img).
>
> > Alex
>
> Hi Alex, did you know that Nod32 thinks the pre built floppy images
> are threats and quarantines them?

I can't know about all idiosyncrasies of every single antiviral
program. I don't use Nod32.

Alex

da...@nowhere.net

unread,
Dec 24, 2009, 11:33:37 PM12/24/09
to

I suspect that many anti-virus programs when set to the maximum level
for boot sector validation will always flag a boot sector that does
not match the standard MS ones. How much of the sector is checked and
at what offsets are an exercise for those who need to know.

On 24 Dec 2009 14:30:19 GMT, "Alexei A. Frounze"

0 new messages