2015-09-14 06:11:38 +02:00
2015-09-11 10:17:17 +02:00
2015-09-11 10:17:17 +02:00
2015-09-12 11:37:30 +02:00
2015-09-12 11:37:30 +02:00
2015-09-06 16:22:10 +02:00
2015-09-11 10:17:17 +02:00
2015-09-12 11:37:30 +02:00
2015-09-11 12:12:31 +02:00
2015-09-11 12:12:31 +02:00
2015-09-11 10:17:17 +02:00
2015-09-11 12:12:31 +02:00
2015-09-12 11:37:30 +02:00

x86 Bare Metal Examples

Hello world examples of programs without an OS. A.K.A. bare bones.

  1. Examples
    1. printf
    2. min
    3. no-ld-script
    4. bios_one_char
    5. bios_hello_world
    6. shutdown_apm
    7. multiboot/
    8. TODO not working
      1. UEFI
      2. hajji
    9. nasm
  2. BIOS
  3. Multiboot
  4. TODO

Getting started

sudo apt-get install build-essential gnu-efi qemu

# Run the default program on QEMU.
make run

# Run a given program.
make run RUN=min
make run RUN=bios_one_char

Tested on Ubuntu 14.04.

Run on real hardware: for insert as USB, determine its device (/dev/sdX) with:

sudo lsblk
sudo fdisk -l

then run:

sudo dd if=bios_hello_world.img of=/dev/sdX

Then:

  • insert the USB in a computer
  • during boot, hit some special key, usually F12
  • choose to boot from the USB

Tested on: ThinkPad T400.

More assembly info at: https://github.com/cirosantilli/assembly-cheat

Formats

When we create a regular Linux program, we generate an ELF file, which is read by the OS.

Without an OS, we can use the following formats:

Gotchas

  • bytes 511 and 512 of the boot sector must be 0x55aa or else the BIOS will refuse to load

  • BIOS loads the program into memory at the address 0x7C00.

    We must tell that magic number to the linker somehow, either with a linker script, -tText=-Ttext 0x7C00 or NASM org 0x7c00.

    This will only matter when you access a memory address, because of relocation.

    If you don't know what relocation is, first read this: http://stackoverflow.com/questions/12122446/how-does-c-linking-work-in-practice/30507725#30507725

    When we link a normal program with an OS, the linker tells where it wants the OS to place it in virtual memory.

    But for the boot sector, the BIOS puts the program into memory. So we must tell that to the linker somehow. Otherwise it cannot know what addresses to use for instructions.

  • x86 processors start in 16-bit mode.

IO

You cannot use any libraries, so how to do IO? Some ways that this can be done:

Showdown and restart can be managed with either:

See also: http://wiki.osdev.org/Shutdown

Bibliography

The following did not work on my machine out of the box:

Description
No description provided
Readme 1.1 MiB
Languages
Assembly 64.3%
C 27.6%
Makefile 6.8%
Shell 1.2%