NetBSD on an Edimax BR6104K(P)

Contents

Introduction

This may seem like a strange thing to do, after all, Linux is fairly well supported on this platform: ADMTek originally ported Linux 2.4.18, Midge and Amilda provide support for 2.4.3[12], and latterly, OpenWrt supports 2.6.19.2.

That said, the NetBSD kernel occupies less than 800KB compressed, as compared with 1.4MB for linux 2.6. This means that people wanting to make best use of the 2MB of available flash may wish to consider NetBSD.

The Edimax comes in two forms: With and without USB ports. The ('KP' model has USB). The 'K' model (without USB) can be obtained in the UK for around 17ukp, making it potentially the cheapest device capable of running NetBSD.

Disclaimer

Please note that this is a work-in-progress - I don't think you can consider this platform to be 'supported' by NetBSD, since the original ADM5120 port was for the RouterBOARD (http://www.routerboard.com)

It should go without saying that I am not responsible for you 'bricking' your router, although if you do, all is not lost, try this thread: http://midge.vlad.org.ua/forum/viewtopic.php?t=121.

Oh yes, and I'm a NetBSD newbie, so please contact me on si1356atyahoodotcodotuk with any corrections.

Prerequisites

I am assuming the you have either the BR-6104K or BR-6104KP Edimax router with adm5120 processor, 16MB of RAM and 2MB of flash. Identical hardware can be found in the Sweex LB000021 router. I am also assuming you have attached an 8-pin header to the device and have linked up an RS232 level shifter and are able to upload your own firmware using XModem. There is info explaining how to do this at: http://www.linux-mips.org/wiki/BR6104

Running NetBSD under Linux

I've installed VMWare Server so I can run NetBSD without uninstalling Linux. I'm using VMWare version 1.0.2, but other versions should also work. You can download it for free from http://www.vmware.com/download/server/ Select 'FreeBSD' as guest operating system when configuring a new virtual machine.

I've also successfully got NetBSD running under Qemu, however you will need to disable kqemu acceleration for this, to avoid errors during NetBSD bootup. This results in slightly slower operation, but it's still usable.

Getting NetBSD

I never actually burnt a CD, just downloaded the CD image from one of the mirrors at: http://www.netbsd.org/mirrors/#iso The file you are after is i386cd-3.1.iso. It's about 220MB in size. You can setup VMWare to connect the image to the emulated CR-ROM drive, then power on.

Installing NetBSD

The partitioning in NetBSD is different from Linux. I chose the option to Edit the MBR partition table instead of using the entire disk, but then created one huge partition in the MBR table for NetBSD. Then, when setting the NetBSD partition sizes (inside the NetBSD partition) I specified 3GB for / (root) leaving 5GB unused. 3GB is more than enough for what we'll be doing. I left Swap as the default of 128MB.

The rest of the installation is straightforward, except make sure you choose 'ksh' as default shell instead of 'sh', so you can edit command lines. I installed everything off the base CD. This provides all the tools that are needed for seting up a cross compiler.

Update: I've added a shockwave movie of an example install

Configuring NetBSD

You can configure the system to obtain a DHCP lease on boot by adding:

dhclient=YES

to /etc/rc.conf, or simple run:

dhclient

on every boot. This sets up the networking if vmware has been configured to use NAT networking for the guests.

Getting NetBSD-current

Now you have a working system, we need to get the current sources which have the ADM5120 support. I got the entire NetBSD tree:

$ cd /usr
$ CVSROOT=:pserver:anoncvs@anoncvs.NetBSD.org:/cvsroot
$ export CVSROOT
$ cvs login

Give password 'anoncvs', and then:

$ cvs checkout -P src

This will checkout the sources to /usr/src. You can also try to get the sources using cvsup but I couldn't get that working.

Note: This operation will take ages (in my case several hours).

Generating a cross-compiler

There is a script called build.sh which does all the work for you. You need only specify the target architecture with the -m option and give the 'tools' option:

$ cd src
$ ./build.sh -m evbmips-el tools

evbmips, is short for Evaluation Board Mips. 'el' means little-endian... I think.

Patching the kernel

I haven't created a patch, but the changes required for the Edimax are very simple. First we must stop NetBSD from attempting to read the command-line parameters for the kernel. The Edimax bootloader doesn't supply them in the correct format, so NetBSD panics when attempting to read them.

In /usr/src/sys/arch/evbmips/adm5120/machdep.c (follow the link for my version) prevent inspection of the kernel args, by adding a return from the copy_args() function to stop this function from processing the arguments:

>   copy_args(int argc, char **argv)
>   {
>           struct adm5120_config *admc = &adm5120_configuration;
>           int i;
>           char *buf;
>           size_t buflen, rc;
>           return;      /*  <----- !!!! add this !!!!!!   */

The Edimax bootloader starts executing code at offset 0x6d8 in the uncompressed firmware image. This is due to the way the linux 2.4.18 kernel is organised. We need to fake the same layout for NetBSD so we just add a .fill instruction to /usr/src/sys/arch/mips/mips/locore.S to change the entry point:

> #include "assym.h"
>
>         .set    noreorder
>
>         .globl  start
>         .globl  _C_LABEL(kernel_text)           # libkvm refers this
>         .fill   0x6d8          # <---- !!!!!Add this line!!!!
> start:
> _C_LABEL(kernel_text):
> #if defined(MIPS3_PLUS) && !defined(MIPS1)

Finally, you will see in /usr/src/sys/arch/evbmips/adm5120/machdep.c that the memory size is hard-coded to 4MB. The BR-6104KP has 16MB so you will want to change the MEMSIZE define near the start of the file to "16 * 1024 * 1024"

Configuring the kernel

We need to create a kernel config. I've used ADM5120-USB as a starting point and created ADM5120-EDIMAX:

$ cd sys/arch/evbmips/conf
$ wget linux-adm5120.sf.net/netbsd/ADM5120-EDIMAX

Note: Trial and error has found that if the memory disk is any larger than 5120 blocks in size the generated image won't work. This is either a bug in the bootloader or NetBSD, I've no idea which.

Prepare the makefile for the new kernel with:

$ /usr/src/tooldir.NetBSD-3.1-i386/bin/nbconfig ADM5120-EDIMAX

Compiling the kernel

This is easy:

$ cd ../compile/ADM5120-EDIMAX
$ /usr/src/tooldir.NetBSD-3.1-i386/bin/nbmake-evbmips-el depend
$ /usr/src/tooldir.NetBSD-3.1-i386/bin/nbmake-evbmips-el

This should generate the file 'netbsd' as specified by the 'config' line above.

Crunchgen

Now we need to prepare a root filesystem. You can use a utility called crunchgen for this:

$ cd /usr/src/sys/arch/evbmips/compile/ADM5120-EDIMAX
$ mkdir crunch
$ cd crunch
$ wget http://linux-adm5120.sf.net/netbsd/crunched.conf
$ wget http://linux-adm5120.sf.net/netbsd/Makefile
$ make

This will create the firmware 'netbsd.bin.gz'

Final Kernel Preparation

If you want to run the image from DRAM, you can just go ahead and upload this image. If you want to burn to flash you have to add a CSYS header. You can get a header generation program from here: http://www.student.tue.nl/Q/t.f.a.wilms/adm5120/files/mksyshdr.c Compile it and run it:

$ gcc mksyshdr.c -o mksyshdr
$ ./mksyshdr csys netbsd.bin.gz
$ cat csys netbsd.bin.gz > firmware.img

Then upload 'firmware.img'.

Booting from Flash

You can find the boot log, along with an example session here. If you want to try the firmware yourself it can be found in the download section.

References

Relevant NetBSD guides:

Linux forums for the Edimax:

Linux-mips Info:

UK source for the BR-6104KP: