How to write a bare-metal hello world program for PowerPC

2.4k views Asked by At

I need to write a program on bare-metal PowerPC system. As a newbie to bare-metal programming without OS/bootloarder, I decide to write a hello world program to start. I googled some post about this, and found out something about ARM like Beagleboard bare metal programming or Hello world, bare metal Beagleboard.

I don't very clear if they are suitable for porting to PowerPC platform. I cannot find PowerPC's hello world example for beginner. Anyone have experience of bare-metal development for PowerPC, without bootloader or OS?

Thanks.

3

There are 3 answers

4
ginger On

First of all, which CPU is this? Secondly, the CPU is not everything.

If you have no starting point, you can study up the BIOS of the architecture you want to write this code for. Then you can write a boot sector which gives you the output you want. Check this page for some examples: Rough guide to assembly

0
Seth Robertson On

Based on the @Pratik Parvati answer, I have updated instructions for 2023 and more recent tool-chains. Please upvote Pratik (as well) if you find this answer useful.

I executed the following commands:

  • mkdir build-gcc build-binutils build-newlib build-gdb
  • binutils: ( cd build-binutils; export TARGET=powerpc-eabi; export PREFIX=/usr/local/$TARGET; export PATH=$PREFIX/bin:$PATH; ../binutils-2.40/configure --target=$TARGET --prefix=$PREFIX && make -j4 all && make install)
  • Based on some other guidance (maybe unneeded) I put a newlib symlink in gcc: ln -s newlib-4.3.0.20230120 gcc-13.1.0/.
  • gcc: ( cd build-gcc; export TARGET=powerpc-eabi; export PREFIX=/usr/local/$TARGET; export PATH=$PREFIX/bin:$PATH; ../gcc-13.1.0/configure --enable-languages=c --disable-multilib --target=$TARGET --prefix=$PREFIX --without-headers --with-newlib && make -j4 all-gcc && make install-gcc)
  • newlib: (cd build-newlib; export TARGET=powerpc-eabi; export PREFIX=/usr/local/$TARGET; export PATH=$PREFIX/bin:$PATH; ../newlib-4.3.0.20230120/configure --target=$TARGET --prefix=$PREFIX && make -j4 all && make install)
  • gdb: (cd build-gdb; export TARGET=powerpc-eabi; export PREFIX=/usr/local/$TARGET; export PATH=$PREFIX/bin:$PATH; ../gdb-13.2/configure --target=$TARGET --prefix=$PREFIX && make -j4 all && make install)
  • ppc_hw: (cd ppc_hw; sed -i -e 's:~.*/::' -e 's/-gstabs\+//' makefile; export TARGET=powerpc-eabi; export PREFIX=/usr/local/$TARGET; export PATH=$PREFIX/bin:$PATH; ( make all || make all ) && ((sleep 10; skill qemu-system-ppc) & make run))

I have not attempted to use gdb yet.

I was unable to use newlib successfully. When I attempted to use snprintf, after defining noop random system calls the linker forced me, I got an illegal instruction in QEMU. Instead I switched to https://github.com/PetteriAimonen/Baselibc and after minor hacking was able to get that to work, at least at the snprintf level.

0
Pratik Parvati On

Random notes/links I collected for trying to get a bare metal PPC system to boot in Qemu There are plenty of examples all around for doing embedded, bare-metal programming on ARM platforms, but the PowerPC examples seem to be sparse.

Some ARM links:

http://opensourceforu.com/2011/07/qemu-for-embedded-systems-development-part-2/ https://balau82.wordpress.com/2010/02/28/hello-world-for-bare-metal-arm-using-qemu/

Building GNU GCC cross compiler

1) Packages Needed

binutils https://ftp.gnu.org/gnu/binutils/

GCC https://ftp.gnu.org/gnu/gcc/gcc-4.1.1/

newlib ftp://sourceware.org/pub/newlib/index.html

GDB http://www.gnu.org/software/gdb/gdb.html

2) Set environment variables

$ export TARGET=powerpc-eabi
$ export PREFIX=/usr/local/$TARGET
$ export PATH=$PATH:$PREFIX/bin

3) Build binutils

$ tar xjfv binutils-2.17.tar.bz2
$ mkdir build-binutils
$ cd build-binutils
$ ../binutils-2.17/configure --target=$TARGET --prefix=$PREFIX
$ make all
$ make install

4) Build bootstrap GCC

$ tar xjfv gcc-4.1.1.tar.bz2
$ mkdir build-gcc
$ cd build-gcc
$ ../gcc-4.1.1/configure --target=$TARGET --prefix=$PREFIX --without-headers --with-newlib  --with-gnu-as --with-gnu-ld
$ make all-gcc
$ make install-gcc

5) Build newlib

$ tar xzfv newlib-1.14.0.tar.gz 
$ mkdir build-newlib
$ cd build-newlib
$ ../newlib-1.14.0/configure --target=$TARGET --prefix=$PREFIX
$ make all
$ make install

6) Build GCC again with newlib

$ cd build-gcc
$ ../gcc-4.1.1/configure --target=$TARGET --prefix=$PREFIX --with-newlib --with-gnu-as --with-gnu-ld --disable-shared --disable-libssp
$ make all
$ make install

7) Build GDB

$ tar xjfv gdb-6.3.tar.bz2  
$ mkdir build-gdb
$ cd build-gdb
$ ../gdb-6.3/configure --target=$TARGET --prefix=$PREFIX --enable-sim-powerpc --enable-sim-stdio
$ make all
$ make install

Example bare metal hello world!!! https://github.com/ara4711/ppc_hw

  • In makefile change, PREFIX=$(PROC)-$(TYPE)- to PREFIX=/usr/local/powerpc-eabi/bin/$(PROC)-$(TYPE)-

  • In makefile give path of qemu-system-ppc to QEMU variable.

  • Command make will generate the test.bin.

  • Command make run will load the binary and the print “Test Hello
    world!“ is displayed on the console

  • Command make debug to debug test program.

  • Press Ctrl+a and x to terminate QEMU

QEMU implements a gdb connector using a TCP connection. To do so, run make debug

this command freezes the system before executing any guest code and waits for a connection on the TCP port 1234. From another terminal, run powerpc-eabi-gdb and enter the commands:

target remote localhost:1234
file test.elf

This connects to the QEMU system and loads the debugging symbols of the test program, whose binary image is already loaded in the system memory. From there, it is possible to run the program with the continue command, single-step the program and debug it in general. The exit command in gdb closes both the debugger and the emulator.