Subject: Re: Building ppc/powerpc kernel to run under QEMU.

From: Rob Landley <[email-masked]>
Date: Sat, 12 May 2007 00:05:58 -0400

On Friday 11 May 2007 11:00 pm, Doug Maxey wrote:
> > I'm told they've got that part working.  I once managed to get a powerpc 
> > Ubuntu ISO image to boot under qemu, I just can't _build_ a kernel that 
does.  
> > (I have no idea how to create the specially partitioned hard drive image 
it 
> > wants, and since I got 5 other architectures working without needing to do 
> > that, I'm looking for a way to make a prep kernel work so I won't have 
to.)
> 
> Have you tried to use parted (or fdisk) to set the first partition table
> entry as type 0x41?   I personally have no experience with qemu, but do
> fiddle with loading images with the real IBM firmware (a lot).  

Ok, backstory.

I have a cross-compile build system called Firmware Linux 
(http://landley.net/code/firmware).  It's a shell script that you run to 
build a system, entirely from source, for a given target platform.  
Cross-compile toolchain and all.

First it builds a cross compiler from source code (gcc, binutils, uClibc, and 
make headers_install from Linux), and then it does just enough 
cross-compiling with that to get you a minimal native build environment (a 
root filesystem containing uClibc, linux, busybox, gcc, binutils, make, and 
bash), so you can build natively under emulation from thereon in.  (MUCH 
easier than cross compiling.  Slower, but throwing hardware at the problem is 
easier than throwing engineers at it. I wrote a "why cross-compiling sucks" 
at http://landley.net/writing/docs/cross-compiling.html not that I have 
strong opinions on the topic or anything. :)

Anyway, I have this thing building a half-dozen archtectures already, and the 
ones I regularly test under are i686, x86-64, armv4l, and mipsel.  (Sparc's 
uClibc support is broken, and powerpc just DOESN'T LIKE ME.  I'm tackling the 
second first because I don't really care about sparc.)

All the other architectures (except sparc and powerpc) run under qemu.  I run 
the appropriate qemu variant with the "-kernel" command line option to use 
the built-in bootloader, and feed them an unpartitioned ext2 image containing 
the root filesystem, and all of 'em (except sparc and ppc) boot up to a shell 
prompt within the emulator and are capable of building and running "hello 
world".

There are idiosyncrasies.  (For example I have to feed arm a scsi hard drive 
instead of IDE, but that's just a different qemu command line.  And for some 
reason 2.6.21 broke arm's scsi support, this but worked in 2.6.20.)  But 
mostly, all the architectures work the same way.

Unfortunately, open hackware requires a partitioned hard drive image or it 
refuses to boot.  PREP looks enough like a paritioned image to fool the 
firmware into letting the built-in "-kernel" bootloader of qemu work.  None 
of the macintosh variants do.

> What I usually end up doing is dd'ing a bootloader into the appropriate
> sized 0x41 partition.  Am pretty sure no GPL stuff groks PREP partitions
> per se, other than seeing the part type.

With qemu I can go:

  qemu -kernel vmlinuzfile -hda walrus.img -nographic

And it'll load that kernel into memory and boot it, without it actually being 
anywhere in walrus.img.  On most platforms this bypasses the bios/firmware 
entirely, but apparently powerpc can't do this (due to the need to set up a 
fake hardware tree and pass it off to the kernel, or some such).  And the 
firmware is chock-full of built-in assumptions, and sits down and throws a 
tantrum if any of them are violated.

It's kind of annoying, really.  But if I could build a prep kernel, I could 
work around this.  Unfortunately, ARCH=ppc builds a prep kernel, but won't do 
a headers_install.  With ARCH=powerpc I can do a headers_installbuild a CHRP 
kernel that won't boot, but not a PREP kernel.

I could teach my build script to use one ARCH for headers_install and one for 
building the kernel, but that's amazingly ugly and I dont have to do it for 
any of the other architectures.

I could also write a wrapper to turn CHRP into a partitioned hard drive image, 
which looks like what I'm going to have to do.

> AIX stores its kernel in the PREP part, and boots from that partition
> (no bootloader, loads kernel directly).

That's what I'm trying to use, yeah.  ARCH=ppc does that, ARCH=powerpc does 
not.

The current mercurial version of Firmware Linux builds a CHRP kernel I can't 
boot.  One of my users donated a config that sort of builds a PREP kernel 
(attached), but you have to patch the ./cross-compiler.sh 
and ./mini-native.sh build scripts to feed in "powerpc" instead of $KARCH to 
headers_install.

Rob