Nios II ucLinux

Last update:

11/22/2006 17:27:34

I have done some work on running uClinux and on a NIOS II from Altera. The biggest problem there is the constant obfuscation of the code and patches. Most of the code can be found (after long searches), but it is still quite some work to create decent patches. Just skip the introduction if you know what the Nios II is and just want to get on with hacking together some toolchain.


The NIOS II is a softcore RISC processor to embed in Altera FPGA’s. There are a number of problems with this platform:

  1. It is not yet widely accepted by the uC community. Most prefer to use a Xilinx solution with a hardcore PowerPC. One of the major advantages (and is often cited as such) is that the PowerPC toolchain (GNU) is much more stable, supported and tested (by GCC). In contrast, Altera provides a NIOS II toolchain based on the GNU compilers for C, but the releases are sketchy (3.4.1 and binutils 2.15), and often dirty: development is clearly biased towards a cygwin environment with all the (hardcoded) mess that goes with it. You will need to remove some crud.
  2. No MMU, so no fork, daemon, system, … Keep it simple!
  3. Furthermore, the toolchain is linked against newlib, though linking it against uclibc or other embedded libc libraries are not insurmountable; just provide the correct -msys-lib option (-msys-lib=lc). I chose to remove this in the source code (is pretty easy to find).
  4. Altera outsourced the porting of the uclinux kernel to microtronix. If you want to setup your own system with some idea of what is happening; be preapred to dig in dirty code: the code provided on is a snapshot of their CVS. Since they seem to check in everything but the kitchen sink: all CVS tags are changed; polluting patching. More of this problematic CVS usage includes: removal of all non-NIOS archs in the kernel tree and inline changes to the kernel source code, breaking other archs.
  5. Build limitations are (currently) gcc 3.4.3, 3.4.1 and 3.4.2 is trivial) and the binutils 2.15 with kernel headers 2.6.9).
  6. and…. we are booting the evaluation system with it 🙂

Of course, this approach is not without its advantages:

  1. Upgrading processor core without the requirement to upgrade the hardware (in most cases).
  2. The possibility to flexibly offload certain functionality to FPGA.
  3. Closer and more important advantages of Hardware/Software codesign.

All in all, certainly an interesting perspective.

Setting up a toolchain

This section describes the approach taken to create a custom toolchain for the NIOS II. Support for the Nios II is (not yet, and not likely) to be in the main GCC distribution. In the following, we always start from the well established upstream sources and where needed, a patch is provided. A large part of the patches is extracted out of the GPL’d Nios port of Altera and Microtronix. The patches have been extracted from the code, generalised (most of it was done for Cygwin) and cleaned up.

You should use the snapshots and patches as described on this page. As far as I know, all the snapshots I used can be downloaded here and lots of people managed to build their own toolchain using this information. The official upstream sources are in a constant state of development and not all the required changes for Nios II have been done. Also, the priorities within my company have shifted, so I have much less time to improve and develop the toolchain 😦. If anyone has done improvements on the documentation and/or has additional patches, please return the information and I’ll add it to this page (with the appropriate credits, of course).

The following sections have made it (together with work of other people) in buildroot. You should be able to download the latest nightly snapshot and compile a cross compilation toolchain with buildroot. The only things which might be interesting are the clean kernel patches and possibly busybox.

The following will provide a HOWTO to create your customised NIOS II (GNU/Linux) toolchain. It is still not as clean as I would want it to be, but it is IMHO very usable. More and cleaner patches will be provides as work progresses.
You need to build the following components to get a toolchain:

  1. buildroot: buildroot (largely) takes care of the complex procedure of creating an embedded toolchain for you.
    Download the source code (I used the 20050513 snapshot) and apply the patches in this archive. Just apply them in the order defined in patches/series file or better; using quilt itself, place the patches directory in the top buildroot directory and execute.

    $ quilt push $(tail -1 patches/series)

    I noticed that the latest (20050602) uClibc snapshot was broken, just use this working snapshot of 20050517. Put in the ‘dl’ directory in buildroot. There are a number of changes in the uCLinux ABI that need to be changed (in the works).
    Cristiano Ghirardi noticed that you should not compile the toolchain with the gcc4.

  2. Even though buildroot provides the possibility to compile busybox in the process, It is currently extracted from the build process and built seperately afterwards.
    This busybox-1.00-pre8-nios2.patch.bz2 (older patch: here) patch is applied on busybox (1.00-pre8 release). Again, some paths are hardcoded, just look for the barco tag again in the patch and replace where needed. Again, busybox uses the Linux kernel configuration, you can make menuconfig, make and make install.
    UPDATE: dinno passes along this patch for the current stable release (Wed Dec 28 18:29:39 CET 2005). I have not tested this, but to all accounts it should work.
    Note that I install it (defined in the configuration file) on the location where I keep my NFS root. This is the starting configuration.
    1. 2.6.9: (these patches are still provided for completeness, but go for the 2.6.11) This patch adds support for the NIOS II based on a patched 2.6.9 kernel. First apply the uclinux patch and next this one. The barco diff is a cleaned (big clean) of here. Do not use this last patch, unless you want some wide range of drivers and filesystems you will not use anyway.

      [marc@scorpius nios2]$ gunzip linux-2.6.9-uc0-nios2-barco.diff.gz
      [marc@scorpius nios2]$ gunzip linux-2.6.9-uc0.patch.gz
      [marc@scorpius nios2]$ tar xvfj linux-2.6.9.tar.bz2
      [marc@scorpius nios2]$ cd linux-2.6.9
      [marc@scorpius linux-2.6.9]$ patch -p1 < ../linux-2.6.9-uc0.patch
      [marc@scorpius linux-2.6.9]$ patch -p1 < ../linux-2.6.9-uc0-nios2-barco.diff

      The compilation used a NFS root fs with this config.

    2. 2.6.11: This archive should contain everything you need to move from a stock ( kernel to a Nios II kernel. The series file suggests a order of applying, but you only need the first two patches (uC support, the patch is copied from, and nios II support) to boot a NFS kernel.
      The compilation used a NFS root fs with this config.
  3. Some notes to work with the NIOS II Evaluation board of Altera
    Kernel compilation:

    [marc@scorpius linux-2.6.11]$ make ARCH=nios2nommu CROSS_COMPILE=nios2-linux-uclibc- hwselect SYSPTF=std_1s10.ptf
    [marc@scorpius linux-2.6.11]$ make ARCH=nios2nommu CROSS_COMPILE=nios2-linux-uclibc- menuconfig
    [marc@scorpius linux-2.6.11]$ make ARCH=nios2nommu CROSS_COMPILE=nios2-linux-uclibc-

    You will need to find the std_1s10.ptf file from your Altera installation somewhere and place it in the kernel directory.

  4. Upload the kernel:

    [marc@scorpius linux-2.6.11]$ nios2-linux-objcopy -O binary vmlinux vmlinux.bin
    [marc@scorpius linux-2.6.11]$ nios2-download -g vmlinux.bin

    The -g option starts after uploading.

As for the nios2-download and the *.ptf files, I’m afraid those are Altera proprietary, you’ll need to contact Altera for those. They are included in the evaluation kits, if you are working on Nios II, you should have those anyway.

Binary toolchain

You can always get the binary snapshot of the toolchain we are using, if you don’t want to go through the building process. It’s not that big (34 MB uncompressed). See my contacts page for the details. Please use the subject Nios II toolchain, this way your mail will stick out in my inbox.

To Do

Some things I plan to upgrade in the following months/weeks, euhm, whenever I have time (see here).

As you can see from the screenshot, the entire environment is booting a working system 🙂 The patches have been submitted to buildroot and are currently in the bug reporting system.


This research is done in Barco in the division Barco Control Rooms (combined with a lot of morning, evening and weekend hacking 🙂, much to the despair of my lovely wife).

If you have more questions, just mail/IM me, it should be pretty easy to find me online.

Happy Hacking!

Thanks to the following people for their feedback and work to improve this toolchain:

  • Cristiano Ghirardi
  • Thomas Chou