tags:

views:

101

answers:

5

I'm interested in writing software that runs with as little booting as possible. What do I do? Is this an assembly question? Do I have to do something special with the disk the software is to run from?

To clarify, I'm looking for a point at which stdin and stdout are available, but not much else.

A: 

It depends on the operating system. You have to add your program to RunOnce registry key under windows, and to init script under linux (there are various init methods under linux, so there are various methods).

Mewp
I'm interested in writing software that runs before windows or linux are loaded. Is there a way to do that?
Joshua Moore
+3  A: 

If you want to run code after POST but before the OS starts, you need to either hook or replace the bootloader. There are no simple answers to the next few questions you're going to ask.

There are quite a few resources on the internet for writing your own bootloader - you might want to start with them:

If you find yourself looking for a good book on x86 assembly, try:

Nick Bastin
At the risk of there being no easy answers: Does that mean I'd be writing my own boot loader?
Joshua Moore
I looked up what POST means, yes, I was thinking just what you said. Would it be possible to make a new entry in the bootloader and have it point to my program?
Joshua Moore
Or rather replace the bootloader with your program. If you don't need more than 510 bytes that is. :)
Jens Björnhager
You could replace the bootloader, but then you'll ultimately have to launch the OS already installed on the machine, and you'll be very unhappy trying to do this. You're better off just learning how to interface with the 3-4 most common bootloaders, unless you're trying to do something truly evil to someone's computer.
Nick Bastin
Or you can get yourself a floppy drive.
Jens Björnhager
Floppy drive? What's that? :-) (You could burn the first 512 bytes of a CD as well, on most motherboards anyhow, or even a USB stick)
Nick Bastin
+1  A: 

A way to achive that effect might be to re-use some very limited OS like some cut-down Linux or maybe even old DOS and then having them run your code automatically at startup. But it depends on exactly why you want to do this if that solution would be suitable for you.

Edit: Or if that's not enough, I'd suggest looking at the Lilo or grub4dos or something similar to see if you can insert your code in there in some way. The forums at the Boot Land site might be useful also since they seem quite into figuring out how to customize the boot process.

ho1
DOS is still very much alive in the embedded world, gas pumps, cash registers, etc. DOS isnt bad. You should look at linux from scratch to understand the linux boot process and you will find you can short cut that dramatically and boot right into an embedded program.
dwelch
+1  A: 

Just run Linux without all the junk that distributions provide? My kernel takes 1.8 seconds to load. This is the easiest way to do it. See Linux From Scratch.

stdin and stdout are OS specific concepts, they don't exist pre-boot, (although maybe in some custom BIOS or bootloaders they do...)

The next lowest place would be the boot loader, some things are initialized at that point, but you still will have tons of work to do.

You can go even lower than that, load your own BIOS, but then you have to take care of all the non-standardized things that your BIOS takes care of.

Longpoke
Right after the bootloader would be perfect. I just want to be able to do basic input output.
Joshua Moore
A: 

Here is a simple boot loader to get you started. You will need nasm, dd, and a floppy disk.

http://www.cs.umbc.edu/courses/undergraduate/313/spring05/burt_katz/lectures/Lect11/newBootSector.html

; boot1.asm   stand alone program for floppy boot sector
; Compiled using            nasm -f bin boot1.asm
; Written to floppy with    dd if=boot1 of=/dev/fd0

; Boot record is loaded at 0000:7C00,
        ORG     7C00h

; load message address into SI register:
        LEA     SI,[msg]

; screen function:
        MOV     AH,0Eh

print:  MOV     AL,[SI]         
        CMP     AL,0         
        JZ      done               ; zero byte at end of string
        INT     10h                ; write character to screen.    
        INC     SI         
        JMP     print

; wait for 'any key':
done:   MOV     AH,0       
        INT     16h                ; waits for key press
                                   ; AL is ASCII code or zero
                                   ; AH is keyboard code

; store magic value at 0040h:0072h to reboot:
;       0000h - cold boot.
;       1234h - warm boot.
        MOV     AX,0040h
        MOV     DS,AX
        MOV     word[0072h],0000h  ; cold boot.
        JMP     0FFFFh:0000h       ; reboot!

msg     DB      'Welcome, I have control of the computer.',13,10
        DB      'Press any key to reboot.',13,10
        DB      '(after removing the floppy)',13,10,0
; end boot1
Jason