Cross compile a Debian kernel for PlayStation 3

A previous article in this blog was dealing with building an optimized Linux kernel for Debian GNU/Linux on a PlayStation 3. This however required to build the kernel on the PS3 itself, while you may also have a powerful multi-core PC which should be able to do the job a lot faster.

This article deals with compiling a kernel Debian package on a PC running Debian GNU/Linux, through the use of a cross compiler.

Before we start

As we are supposed to use a PC because most compilation tasks will perform much faster than on the real hardware (ie, the PS3), you should know that most of the speedup will be achieved using concurrent compilation. Concurrent compilation makes possible to compile several source files in parallel, taking the number of available CPU cores as an advantage, potentially achieving a speedup which will be close to the number of available cores.

The number of cores for a specific computer can be obtained using the following command:

grep -c '^processor' /proc/cpuinfo

Note the obtained value, it will be useful for speeding up some compilation steps.

Setup a cross-compiling toolchain

The simplest method I found to get a working PC-to-PS3 cross compiler toolchain was to use the setup scripts from the pdaXrom-ng embedded Linux distribution. Here are the necessary steps:

As root, install the required packages:

apt-get install autoconf automake bison build-essential cmake flex gperf intltool libbz2-dev libcap-dev libglib2.0-dev libxml-simple-perl libxml2-dev lzma m4 rpm subversion texinfo xmlto zlib1g-dev

Still as root, create the destination directory, with rights for your normal user:

mkdir -p /opt/powerpc-ps3-linux
chown user:group /opt/powerpc-ps3-linux

Obviously replacing the user and group values with the correct user and group names.

Now, as the normal user, in a directory in user space:

svn checkout

This will download the latest version of the pdaXrom-ng source files in a new directory. Now, go to that directory:

cd pdaXrom-ng

In the rules/ file, locate the line containing MAKEARGS=-j4. Eventually replace the default value 4 with the actual number of cores (or more) in your computer.

Now, build and install the cross compiler with the command:

./ powerpc-ps3-linux

Now, have a cup of coffee and watch the cross-compiler build and install process.

Get the PS3 linux kernel source tree

You can follow the instructions of the section Get the kernel sources from this previous article.

Build the packages

As root, issue the commands:

apt-get install build-essential bzip2 libncurses5-dev git-core fakeroot initramfs-tools kernel-package module-assistant devscripts

Now, as the normal user, switch to the kernel source directory, then setup some environment variables with the commands:

export ARCH=powerpc

Set the value for CONCURRENCY_LEVEL to the actual number of cores in the compiling computer, or a little bit more.

Now, create the base configuration for the PS3 kernel:

make ps3_defconfig

You can then change the default configuration with the command:

make menuconfig

Now issue this command (do not ask me why, but without it, the process will not work):

echo \#\!/bin/sh > ./scripts/setlocalversion

And finally, the magic command to build all the required packages:

make-kpkg --arch powerpc --cross-compile /opt/powerpc-ps3-linux/toolchain/bin/powerpc-ps3-linux- --rootcmd fakeroot --initrd --append-to-version -ps3 --revision 20091231 kernel_image kernel_source kernel_headers

And that's all. The corresponding kernel image, headers and source packages should be generated in the parent directory of the kernel source directory.