File:  [NetBSD Developer Wiki] / wikisrc / ports / evbarm / raspberry_pi.mdwn
Revision 1.78: download - view: text, annotated - select for diffs
Tue Oct 30 23:09:40 2018 UTC (2 years, 3 months ago) by gdt
Branches: MAIN
CVS tags: HEAD
raspberry_pi: improve links, reference vcgencmd

[[!meta title="NetBSD/evbarm on Raspberry Pi"]]

This page attempts to document and coordinate efforts towards NetBSD/evbarm on [Raspberry Pi](http://www.raspberrypi.org). All board variants are supported.

Initial, limited, Raspberry Pi support was introduced in NetBSD 6.0. NetBSD 7.0 adds complete support for the board, along with introducing support for the quad-core Raspberry Pi 2 board.  Raspberry Pi 3 support was added for NetBSD 8, and backported to NetBSD 7 in July of 2017.  (This page assumes those using NetBSD 7 are using 7.2, or the netbsd-7 branch after mid 2018.)

[[images/raspberrypi.jpg]]

[[!toc levels=2]]

<small>([Raspberry Pi image](http://www.flickr.com/photos/42325803@N07/8118758647/) by Christopher Lee used under CC-By-2.0 license)</small>

# What works (and what doesn't yet)

## NetBSD 7 and NetBSD 8

 - RaspberryPi 1, 2, 3 (except Pi 3 builtin WiFi and bluetooth)
 - multiple processors on 2/3
 - boots normally to multiuser, with FAT32 boot partition on uSD
 - root filesystem can be uSD or USB-attached mass storage
 - serial or graphics console (with EDID query / parsing)
 - X11 via HDMI
 - GPU (VCHIQ) - 3D and video decode. man page missing.
 - USB host controller - dwctwo(4) and most devices work
 - USB Ethernet - usmsc(4)
 - DMA controller driver and sdhc(4) support
 - RNG
 - Audio: works. man page missing.
 - GPIO
 - I²C: works, could use enhancements, man page
 - SPI: could use enhancements, man page

## NetBSD current

 - Raspberry Pi 3 builtin bluetooth
 - Raspberry Pi 3 new SD host controller driver

## What needs work

 - USB (host); isochronous transfers.
 - Raspberry Pi 3 builtin WiFi

# CPU types

 - Raspberry Pi 1 uses "earmv6hf".
 - Raspberry Pi 2 uses "earmv7hf".
 - Raspberry Pi 3 uses "earmv7hf".

Note that one can run earmv6hf code on the 2 and 3.  See also
[[NetBSD/aarch64|aarch64]] for running the Pi 2/3 in 64-bit mode.

# Installation

## SD card structure

The Raspberry Pi looks for firmware and kernel.img on the first FAT32 partition of the uSD card.  A separate kernel (kernel7.img) is used on RPI2 and RPI3.

The NetBSD kernel will then use the FFS partition as the root filesystem.

A 2 GB card is the smallest workable size.  The NetBSD filesystem will be expanded to fit.

## Choosing a version

First, decide if you want to install a formal release (7.2 or 8.0), a stable branch build (netbsd-7, netbsd-8), or NetBSD-current.  For people who don't know how to choose among those, 8.0 or netbsd-8 is probably best.

See also "ebijun's image", below, which is NetBSD-current and includes packages.

## Getting bits to install

You can either build a release yourself with build.sh, or get one from the NetBSD FTP servers.

Both will provide rpi.img.gz and rpi_inst.img.gz.  Each is an image to be written to a uSD card, and has a FAT32 partition for booting.  In rpi.img.gz, there is also an FFS partition for NetBSD.

### Building yourself

Getting sources and building a release with build.sh is not special for evbarm.  Pick a CPU type alias and pass it to build.sh with -m.  Examples (the first two are equivalent):

 - ./build.sh -m earmv6hf -u release
 - ./build.sh -m evbarm -a earmv6hf -u release
 - ./build.sh -m evbarm -a earmv7hf -u release

### NetBSD FTP servers

NetBSD provides nightly builds on [nyftp.netbsd.org](http://nyftp.netbsd.org/pub/NetBSD-daily/).  These are equivalent to building yourself.

 - The 'evbarm-earmv6hf/binary/gzimg/' directory contains an rpi.img file that can be used as a single image for both boards.
 - The 'evbarm-earmv7hf/binary/gzimg/' directory contains an armv7.img file that is optimized for Raspberry Pi 2/3.
 - The old stable build directory will be under netbsd-7/YYYYMMDDHHMMZ/ (for example, http://nyftp.netbsd.org/pub/NetBSD-daily/netbsd-7/201710201440Z/evbarm-earmv6hf/binary/gzimg)    
 - The stable build directory will be under netbsd-8/YYYYMMDDHHMMZ/ (for example, http://nyftp.netbsd.org/pub/NetBSD-daily/netbsd-8/201710211010Z/evbarm-earmv6hf/binary/gzimg/)
 - The HEAD/current directory build will be under HEAD/YYYYMMDDHHMMZ/ (for example, http://nyftp.netbsd.org/pub/NetBSD-daily/HEAD/201710202210Z/evbarm-earmv7hf/binary/gzimg/)

## Preparing a uSD card

Once you have rpi.img.gz (or rpi_inst), put it on a uSD card using gunzip and dd, for example:

 - gunzip rpi.img.gz
 - dd if=rpi.img of=/dev/disk1

### Serial Console

By default the rpi.img is set to use the HDMI output.  If you wish to use a serial console, first mount the FAT32 partition and then
edit cmdline.txt and remove '"console=fb"'.

 - Most (all?) USB-to-TTL serial adapters only connect Tx, Rx and ground, and do not connect any flow control lines. An effect of missing flow control is that you see console output, but cannot type anything. If so, adjust your serial console application's flow control settings to "none".

   In Kermit, the command is "set flow none".

   In minicom, run "minicom -s" and set hardware flow control to "no"

### Enabling ssh

If you want to enable ssh with the standard image, so that you can log in over the net without either a serial or HDMI console, mount the ffs partition, place /root/.ssh/authorized_keys, uncomment PermitRootLogin in /etc/ssh/sshd_config, and comment out the rc_configure=NO in /etc/rc.conf.  Besides having to find the IP address, you will have to wait for the partition resizing and reboot.

### Installation with sshramdisk image

build.sh (and hence the FTP site) also creates an image 'rpi_inst.img.gz' specifically for installation without HDMI or a serial console.  Note that this image is much smaller and that you will need to fetch the sets over the network.  To use this method, write that image to a uSD card as above, and then:

 - Ensure that you have a lan with a DHCP server.
 - Connect an Ethernet cable from the RPI to the LAN.
 - After starting DHCP client, SSH login to with user "sysinst", and password "netbsd".
   - Be careful to note the ip address given during DHCP so you don't lose your connection
   - Also for after the sysinst is done and the system reboots
 - sysinst started!

## Installation via ebijun's image

As an alternative to the standard installation images, Jun Ebihara
provides an install image for Raspberry Pi that includes packages.  It
is based on NetBSD-current and is built for earmv6hf, and thus will
work on Raspberry Pi 1, 2 and 3.  This image is typically updated
every few weeks.

 - [https://github.com/ebijun/NetBSD/blob/master/RPI/RPIimage/Image/README](https://github.com/ebijun/NetBSD/blob/master/RPI/RPIimage/Image/README)

# Maintaining a system

## vcgencmd

The program vcgencmd can be found in pkgsrc/misc/raspberrypi-userland.


## Updating the kernel

 - Build a new kernel, e.g. using build.sh. It will tell you where the ELF version of the kernel is, e.g.

         ...
         Kernels built from RPI2:
          /Users/feyrer/work/NetBSD/cvs/src-current/obj.evbarm-Darwin-XXX/sys/arch/evbarm/compile/RPI2/netbsd
         ...

 - Besides the "netbsd" kernel in ELF format, there is also a "netbsd.img" (for current) or "netbsd.bin" (for 7 and 8) kernel that is in a format that the Raspberry can boot.
 - Depending on your hardware version, copy this either to /boot/kernel.img (First generation Pi, Pi Zero hardware) or to /boot/kernel7.img (Pi 2, Pi 3 hardware)
 - reboot

## Updating the firmware

A section below describes the process of updating NetBSD's copy of the firmware from upstream, with testing, by NetBSD developers.  This section is about updating a system's firmware from the firmware in a version of NetBSD.

TODO: Explain where the firmware is in the source tree, and if it is in the installed system image (such as /usr/mdec).  Explain any particular cautions.

## Booting

The device boots by finding a file "bootcode.bin".   The primary location is a FAT32 partition on the uSD card, and an additional location is on a USB drive.  See the [[upstream documentation on booting]](https://www.raspberrypi.org/documentation/hardware/raspberrypi/bootmodes/) and read all the subpages.

The standard approach is to use a uSD card, with a fdisk partition table containing a FAT32 partition marked active, and a NetBSD partition.  The NetBSD partition will then contain a disklabel, pointing to an FFS partition (a), a swap paritiion (b) and the FAT32 boot partition mounted as /boot (e).  The file /boot/cmdline.txt has a line to set the root partition.

One wrinkle in the standard approach is that the disk layout is "boot swap /", but the NetBSD fdisk partition starts at the location of /.   The / partition can hold a disklabel, while swap cannot.   It is normal to have swap after / (and thus within the fdisk partition), but the arrangement used permits growing / on first boot, for the typical case where a larger uSD is used, compared to the minimum image size.

An alternate approach is to have the boot FAT32 partition as above, but to have the entire system including root on an external disk.  This is configured by changing root=ld0a to root=sd0a or root=dk0 (depending on disklabel/GPT).  Besides greater space, part of the point is to avoid writing to the uSD card.

A third approach, workable on the Pi 3 only, is to configure USB host booting (already enableed on the 3+; see the upstream documentation) and have the boot partition also on the external device.  In this case the external device must have an MBR because the hardware's first-stage boot does not have GPT support.   \todo Explain if this has been observed to work.
\todo In theory the [[procedure to program USB host boot mode]](https://www.raspberrypi.org/documentation/hardware/raspberrypi/bootmodes/msd.md) will function on a NetBSD system because the programming is done by bootcode.bin.

\todo Explain USB enumeration and how to ensure that the correct boot and root devices are found if one has e.g. a small SSD for the system and a big disk.

# Wireless Networking

Note that the built-in WiFi in the RPI3 is not yet supported.   USB WiFi interfaces (that work on NetBSD in general) should all work.

 - A Realtek 802.11n USB adaptor configures as urtwn(4).
   - Configure with wpa_supplicant in /etc/rc.conf -

           ifconfig_urtwn0=dhcp
           dhcpcd=YES
           dhcpcd_flags="-q -b"
           wpa_supplicant=YES
           wpa_supplicant_flags="-B -i urtwn0 -c /etc/wpa_supplicant.conf"
   - A sample wpa_supplicant.conf can be found at /usr/share/examples/wpa_supplicant/wpa_supplicant.conf

# GPU

## Video playback
Accelerated video playback is supported in NetBSD 7 with the [OMXPlayer](http://pkgsrc.se/multimedia/omxplayer) application and through GStreamer with the [omx](http://pkgsrc.se/multimedia/gst-plugins1-omx) plugin.

## OpenGL ES
Accelerated OpenGL ES is supported in NetBSD 7. The GL ES client libraries are included with the [misc/raspberrypi-userland](http://pkgsrc.se/misc/raspberrypi-userland) package.

## Quake 3
A Raspberry Pi optimized build of *ioquake3* is available in the [games/ioquake3-raspberrypi](http://pkgsrc.se/games/ioquake3-raspberrypi) package. To use it, the following additional resources are required:

 - pak0.pk3 from Quake 3 CD
 - additional pak files from the [games/ioquake3-pk3](http://pkgsrc.se/games/ioquake3-pk3) package
 - read/write permissions on /dev/vchiq and /dev/wsmouse

Place the pak0.pk3 file in the /usr/pkg/lib/ioquake3/baseq3 directory.

## RetroArch / Libretro
Using [emulators/retroarch](http://pkgsrc.se/emulators/retroarch) it is possible to run many emulators at full speed the Raspberry Pi. Emulator cores for various gaming consoles are available in the [emulators/libretro-*](http://pkgsrc.se/search.php?so=libretro-) packages. To begin using retroarch:

 - Install [emulators/retroarch](http://pkgsrc.se/emulators/retroarch)
 - Install the libretro core for the system you would like to emulate (lets take [emulators/libretro-gambatte](http://pkgsrc.se/emulators/libretro-gambatte), a GameBoy Color emulator, as an example).
 - Plug in a USB HID compatible Gamepad, such as the Logitech F710 in "DirectInput" mode (set "D/X" switch to "D").
 - Create a config file for your gamepad using *retroarch-joyconfig*.
[[!template  id=programlisting text="""
$ retroarch-joyconfig -o gamepad.cfg
"""]]
 - Launch the emulator from the command-line (no X required):
[[!template  id=programlisting text="""
$ retroarch --appendconfig gamepad.cfg -L /usr/pkg/lib/libretro/gambatte_libretro.so game.gbc
"""]]

# Developer notes

These notes are for people working on improvements to RPI support in NetBSD.

## Updating the firmware version in the NetBSD sources

(Note that trying new firmware may result in a non-bootable system, so
be prepared to recover the bootable media with another system.)

Upstream firmware releases are
[on GitHub](https://github.com/raspberrypi/firmware/releases).
Copy all files except `kernel*.img` into `/boot` and reboot.

New firmware should pass all of the following tests before being committed to NetBSD.

- Audio
- OMXPlayer (and [[!template id=man name="vchiq"]])
- Serial/framebuffer console
- CPU frequency scaling

Tests shoudl be run on all of `rpi[0123]`.


CVSweb for NetBSD wikisrc <wikimaster@NetBSD.org> software: FreeBSD-CVSweb