Building image for Raspberry Pi: up to date version

February 1, 2013 — 79 Comments

Update 1: Add -DDB_FROM_SRC to install targets
Update 2: Add user “pi” with password “raspberry”

It’s been a while since I posted original build instruction and a lot has change. Here is new version of it with some explanations:

All code has been moved to HEAD. freebsd-pi repository on github serves only historical purpose and I guess will be removed at some point in future. So in order to build image you need sources for -head

svn co svn://svn.freebsd.org/base/head

TARGET_CPUTYPE is dropped to in favor of TARGET_ARCH=armv6. U-Boot, firmware files and boot process were upgraded too. Current boot chain is as follows:

  • Pi is powered up and loads firmware files
  • Firmware loads FDT blob defined in config.txt as device_tree variable at address defined as device_tree_address and fixes up fields like memory size, clock frequency and MAC address
  • Firmware loads u-boot and passes control to it
  • U-boot loads boot.scr and executes it
  • Default boot.scr loads ubldr(loader(8)-compatible implementation built over U-Boot API) and passes control
  • ubldr checks FreeBSD partition for /boot/loader.rc, loads it, the loads /boot/kernel/kernel and passes control to it
  • loader.rc should contain “fdt addr 0×100″ command. It will pass FDT blob filled in step #2 to kernel

So updated build script will look something like this:

#!/bin/sh
set -e

# Change this
export GPU_MEM=128
export PI_USER=pi
export PI_USER_PASSWORD=raspberry
export SRCROOT=/src/FreeBSD/head
export MNTDIR=/mnt
export MAKEOBJDIRPREFIX=/src/FreeBSD/obj
export IMG=/src/FreeBSD/obj/bsd-pi.img

export TARGET_ARCH=armv6
export MAKESYSPATH=$SRCROOT/share/mk
export KERNCONF=RPI-B

if [ -z "$MNTDIR" ]; then
        echo "MNTDIR is not set properly"
        exit 1
fi

KERNEL=`realpath $MAKEOBJDIRPREFIX`/arm.armv6/`realpath $SRCROOT`/sys/$KERNCONF/kernel
UBLDR=`realpath $MAKEOBJDIRPREFIX`/arm.armv6/`realpath $SRCROOT`/sys/boot/arm/uboot/ubldr
DTB=`realpath $MAKEOBJDIRPREFIX`/arm.armv6/`realpath $SRCROOT`/sys/$KERNCONF/bcm2835-rpi-b.dtb

make -C $SRCROOT kernel-toolchain
make -C $SRCROOT KERNCONF=$KERNCONF WITH_FDT=yes buildkernel
make -C $SRCROOT MALLOC_PRODUCTION=yes buildworld

buildenv=`make -C $SRCROOT buildenvvars`

eval $buildenv make -C $SRCROOT/sys/boot clean
eval $buildenv make -C $SRCROOT/sys/boot obj
eval $buildenv make -C $SRCROOT/sys/boot UBLDR_LOADADDR=0x2000000 all

rm -f $IMG
dd if=/dev/zero of=$IMG bs=128M count=8
MDFILE=`mdconfig -a -f $IMG`
gpart create -s MBR ${MDFILE}

# Boot partition
gpart add -s 32m -t '!12' ${MDFILE}
gpart set -a active -i 1 ${MDFILE}
newfs_msdos -L boot -F 16 /dev/${MDFILE}s1
mount_msdosfs /dev/${MDFILE}s1 $MNTDIR
fetch -q -o - http://people.freebsd.org/~gonzo/arm/rpi/freebsd-uboot-20130201.tar.gz | tar -x -v -z -C $MNTDIR -f -

cat >> $MNTDIR/config.txt <<__EOC__
gpu_mem=$GPU_MEM
device_tree=devtree.dat
device_tree_address=0x100
disable_commandline_tags=1
__EOC__
cp $UBLDR $MNTDIR
cp $DTB $MNTDIR/devtree.dat
umount $MNTDIR

# FreeBSD partition
gpart add -t freebsd ${MDFILE}
gpart create -s BSD ${MDFILE}s2
gpart add -t freebsd-ufs ${MDFILE}s2
newfs /dev/${MDFILE}s2a

# Turn on Softupdates
tunefs -n enable /dev/${MDFILE}s2a
# Turn on SUJ with a minimally-sized journal.
# This makes reboots tolerable if you just pull power on the BB
# Note:  A slow SDHC reads about 1MB/s, so a 30MB
# journal can delay boot by 30s.
tunefs -j enable -S 4194304 /dev/${MDFILE}s2a
# Turn on NFSv4 ACLs
tunefs -N enable /dev/${MDFILE}s2a

mount /dev/${MDFILE}s2a $MNTDIR

make -C $SRCROOT DESTDIR=$MNTDIR -DDB_FROM_SRC installkernel
make -C $SRCROOT DESTDIR=$MNTDIR -DDB_FROM_SRC installworld
make -C $SRCROOT DESTDIR=$MNTDIR -DDB_FROM_SRC distribution

echo 'fdt addr 0x100' > $MNTDIR/boot/loader.rc

echo '/dev/mmcsd0s2a / ufs rw,noatime 1 1' > $MNTDIR/etc/fstab

cat > $MNTDIR/etc/rc.conf <<__EORC__
hostname="raspberry-pi"
ifconfig_ue0="DHCP"
sshd_enable="YES"

devd_enable="YES"
sendmail_submit_enable="NO"
sendmail_outbound_enable="NO"
sendmail_msp_queue_enable="NO"
__EORC__

cat > $MNTDIR/etc/ttys <<__EOTTYS__
ttyv0 "/usr/libexec/getty Pc" xterm on secure
ttyv1 "/usr/libexec/getty Pc" xterm on secure
ttyv2 "/usr/libexec/getty Pc" xterm on secure
ttyv3 "/usr/libexec/getty Pc" xterm on secure
ttyv4 "/usr/libexec/getty Pc" xterm on secure
ttyv5 "/usr/libexec/getty Pc" xterm on secure
ttyv6 "/usr/libexec/getty Pc" xterm on secure
ttyu0 "/usr/libexec/getty 3wire.115200" dialup on secure
__EOTTYS__

echo $PI_USER_PASSWORD | pw -V $MNTDIR/etc useradd -h 0 -n $PI_USER -c "Raspberry Pi User" -s /bin/csh -m
pw -V $MNTDIR/etc groupmod wheel -m $PI_USER
PI_USER_UID=`pw -V $MNTDIR/etc usershow $PI_USER | cut -f 3 -d :`
PI_USER_GID=`pw -V $MNTDIR/etc usershow $PI_USER | cut -f 4 -d :`
mkdir -p $MNTDIR/home/$PI_USER
chown $PI_USER_UID:$PI_USER_GID $MNTDIR/home/$PI_USER

umount $MNTDIR
mdconfig -d -u $MDFILE


gonzo

Posts

79 responses to Building image for Raspberry Pi: up to date version

  1. Why no vchiq this time?

  2. Success on a FreeBSD 9.1R amd64 build server. I needed to manually add the auditdistd user like so:
    # pw useradd auditdistd -u 78 -g 77 -d /var/empty -s /usr/sbin/nologin -c “Auditdistd unprivileged user”

    • Oh, I totally missed that. Actually Brooks Davis merged NetBSD’s mtree(8) that fixed this problem. You just need to add -DDB_FROM_SRC to install targets. I updated script.

      • I had to hack Makefile.inc1 to get the script to run (r246525)

        -IMAKE_MTREE= MTREE_CMD=”nmtree ${MTREEFLAGS}”
        +IMAKE_MTREE= MTREE_CMD=”/usr/sbin/nmtree ${MTREEFLAGS}”

        I forgot to cc you on the email I sent brooks. Other than that, I have a running FreeBSD RPi Obviously not the right fix but got it working.

        FreeBSD raspberry-pi 10.0-CURRENT FreeBSD 10.0-CURRENT #1 r246571M: Fri Feb 8 18:46:52 EST 2013 root@odyssey:/home/db/FreeBSD/obj/arm.armv6/home/db/svn/system/head/sys/RPI-B arm

  3. And a question: It looks like you are enabling SSH in the image. What would be the username/password?

  4. Do you no how to change overscan settings on FreeBSD I have been searching about for about 2 days and I cant find any info I am running my pi on a old tube tv and I am loosing about a inch on each side of the screen so I cant see much on the console. It has been annoying me I would really appreciate it if you could help.

    • I haven’t tried FreeBSD on Raspberry Pi with composite output so don’t have first hand experience. From OS point of view there are not many knobs to control video output. Did you check video-related options for config.txt? Here is the link: http://elinux.org/RPiconfig#Video There seems to be several overscan-related

      • Thanks for that also sorry for the slow reply I have been busy but it works I had it in my head that they /boot/config.txt was Linux only lol.

  5. What is -DDB_FROM_SRC for?

  6. All of these images are crashing when I run portsnap fetch extract lately.

  7. One other tweak would be to change the value of IMG to $MAKEOBJDIRPREFIX/bsd-pi.img so that IMG and MAKEOBJDIRPREFIX are always synchronized.

    • I do not think people would want image in their obj/ directory. It’s just that in my VM obj/ is a separate mountpoint with enough space for image.

  8. In the three lines invoking pw(8) near the end of the script the pi user is hardcoded as ‘pi’ , which breaks the script if $PI_USER is set to something different. Those three lines should use the user name in $PI_USER too.

  9. i have build a image on virtualbox and wrote the bsd.img to a 4gb sdcard, but when i turn the rasp on it only sees 1gb / not all 4GB, how to fix that? so all the space is used on the card for /

    • Replace

      dd if=/dev/zero of=$IMG bs=128M count=8
      

      with

      dd if=/dev/zero of=$IMG bs=128M count=32
      

      or otherwise adjust size that fits your card

      • This does not seem to work with a 16GB card. I adjusted above to make a 16GB image, but when I write to card, I get dd: /dev/da1: end of device. Is there a good guide on the math/physics behind this?

        • I’m not sure about a guide. I’d just dump image from the target card, check real size and adjusted blocksize/count properly.

          • Thanks. I think what I was missing was diskinfo(8). Once I manned that and was able to get a REAL size for my card in MB, I was able to create an image tailored to my card, with swap too!

  10. Hei,

    is it possible to compile freebsd with included gcc 4.2.2 or later?

    thanks.

  11. Hei gonzo.

    There is a problem with some packages too like minidlna. it says that armv6… is not supported just like with gcc.

    Any suggestions?

    Thanks

  12. I have added a number of switches and updates to this script in my own copy. Would like your permission to post on github. With a link back of course. Thoughts?

  13. ————————————————————–
    >>> World build completed on Wed Feb 20 11:36:44 MSK 2013
    ————————————————————–
    “/usr/head/sys/boot/Makefile”, line 4: Could not find bsd.arch.inc.mk
    make: fatal errors encountered — cannot continue

  14. I amended the build script to suit my system:

    -export SRCROOT=/usr/src/head
    -export MNTDIR=/mnt/rpi
    -export MAKEOBJDIRPREFIX=/usr/src/obj
    -export IMG=/usr/src/obj/bsd-pi.img
    +export SRCROOT=/src/FreeBSD/head
    +export MNTDIR=/mnt
    +export MAKEOBJDIRPREFIX=/src/FreeBSD/obj
    +export IMG=/src/FreeBSD/obj/bsd-pi.img

    -dd if=/dev/zero of=$IMG bs=128M count=32
    +dd if=/dev/zero of=$IMG bs=128M count=8

    But when I try to boot I get:
    Mounting from ufs:/dev/mmcsd0s2a failed with error 19.

    • Do you have full boot log?

      SDHCI controller is still picky about cards, I’m working on fixing it. You can try adding
      set
      set hw.bcm2835.sdhci.hs=”0″
      to loader.rc.

      Or hw.bcm2835.min_freq=8000000

  15. mine is a bit worst as it does not completely boot at all.

    at boot time, i end up getting the
    mountroot>

    prompt. And if i do a “?” it shows sometimes it only shows “mmcsd0″ as the device.
    a few seconds later
    it shows panic: mountroot: unable to (re-)mount root.
    then screen gets filled with
    db>
    db>
    db>
    db>

    … (at this point, remove the power and re-position the card), boot again

    i may get lucky when aftter
    mountroot> ?

    it may show:
    mmcsd0s2a
    mmcsd0s2
    mmcsd0s1
    mmcsd0

    if there’s no panic… then after the list ill do
    ufs:/dev/mmcsd0s2a

    then it boots, and goes through just fine. However, if I reboot again, it will not re-mount.

    • As I told to caesius – try experimenting with SDHCI driver tunables in loader.rc. On my raspberry Pi this combination works best:

      set hw.bcm2835.sdhci.hs=”0″
      set hw.bcm2835.min_freq="8000000"
      

      If it doesn’t work – try reducing min_freq to 400000.

      • This is a little bit annoying, two of my three different sd cards need these settings. Unfortunately, the problem does not occur at every reboot, so I’ve noticed it late. I made automatic reboot loops (100 reboots) to find it out. With both affected cards it occurs only in ~ 10% of the reboots. With these settings it does not occur.

  16. I’m having trouble getting the world to build. It seems to error out during the fsck_ffs compile. I’ve poked around but can’t seem to find the problem. Any ideas?

    cc1: warnings being treated as errors
    /root/FreeBSD/head/sbin/fsck_ffs/fsutil.c: In function ‘printIOstats’:
    /root/FreeBSD/head/sbin/fsck_ffs/fsutil.c:452: warning: format ‘%ld’ expects type ‘long int’, but argument 2 has type ‘time_t’
    *** [fsutil.o] Error code 1

    Stop in /root/FreeBSD/head/sbin/fsck_ffs.
    *** [fsck_ffs_make] Error code 1

    Stop in /root/FreeBSD/obj/arm.armv6/root/FreeBSD/head/rescue/rescue.
    *** [objs] Error code 1

    Stop in /root/FreeBSD/head/rescue/rescue.
    *** [all] Error code 1

    Stop in /root/FreeBSD/head/rescue.
    *** [rescue.all__D] Error code 1

    Stop in /root/FreeBSD/head.
    *** [everything] Error code 1

    Stop in /root/FreeBSD/head.
    *** [buildworld] Error code 1

    Stop in /root/FreeBSD/head.

  17. Nevermind, it needed to be type %lld instead of %ld. It looks like someone already fixed it in the latest svn.

  18. Whoops, didn’t see your post. Yeah. Building again! =)

    I was going to setup my personal server to do nightly builds and pack them into a normal and Berryboot compatible image. Would it be okay if I posted a download link when I get it all up and running?

  19. Hi, gonzo, I had build the img successful, but yet we only have the img, and how can i make a kernel for the qemu -kernel arg?

  20. Oh, I have saw the older post about qemu, sorry about that.

  21. Does overclocking work as usual in the config.txt, and is there any way I could measure CPU frequency in freebsd?

    • Yes, overclocking settings should work just fine. As for measuring CPU frequency – I do not think there is a way to do it right now, but I’ll take a look what can be done there

  22. Hello Gonzo
    great job getting FreeBSD running on the Raspberry. I love it.
    I booted it from the freebsd-pi-r245446.img.gz from http://downloads.raspberrypi.org
    It works fine, slow, but it works. ports compiles, so everything is fine in that department.

    I wanted to update kernel and world on my system, and then came my problems:

    On the Pi:
    The kernel and the tool-chain compiles fine. Building world fails in the the crypto/heimdal part w. “compile_et not found” I did try to get newer sources several times. still no joy.

    On a i386 vm:
    The code cross compiles fine. I can build bot kernel and world. works like a charm.

    The final problem the one that really nags me:
    If I do a make installkernel KERNCONF=RPI-B neither the cross-compiled kernel nor the kernel compiled on Pi will boot.
    In both cases: It reads in the kernel. I get all the way to the line:
    kernel arguments (null)
    Then nothing more happens. Not that I’m aware off
    I never see:
    root mount waiitng for: usb…

    On both machines I did :
    make kernel-toolchain
    make KERNCONF=RPI-B WITH_FDT=yes buildkernel
    make MALLOC_PRODUCTION=yes buildworld
    Just as described above. Of cause I remembered export =TARGET_ARCH=armv6 when cross compiling.
    On the Pi / was aready mounted. On the vm i mounted the sd card under /mnt and used DESTDIR=

    In both cases /boot is updated w. new kerne.

    Any suggestions is highly appreciated.

    Cheers

    • Try adding WITHOUT_KERBEROS=yes to your make commands.AFAIR there might be problem when you’re trying to build kerberos stuff on the system built originally without it.

  23. Great work!
    Are you familiar with http://www.pcduino.com/ ?
    Same CPU/GPU to the Hackberry.
    What is your view on a bsd supporting all functionality in pcduino?

    • Port is in very basic state but there is some work going on right now. ganbold@ works on Cubieboard and imp@ got a MarsBoard, so we can expect fairly decent port soon-ish.

  24. I could build the rpi image with 10-CURRENT as of 2013-03-27, but today (2013-04-13) the build fails with the following error message:

    cc -c -O -pipe -std=c99 -g -Wall -Wredundant-decls -Wnested-externs -Wstrict-prototypes -Wmissing-prototypes -Wpointer-arith -Winline -Wcast-qual -Wundef -Wno-pointer-sign -fformat-extensions -Wmissing-include-dirs -fdiagnostics-show-option -Wno-error-tautological-compare -Wno-error-empty-body -Wno-error-parentheses-equality -nostdinc -I. -I/root/rpi/head/sys -I/root/rpi/head/sys/contrib/altq -I/root/rpi/head/sys/contrib/libfdt -D_KERNEL -DHAVE_KERNEL_OPTION_HEADERS -include opt_global.h -ffreestanding -Werror /root/rpi/head/sys/arm/broadcom/bcm2835/bcm2835_intr.c
    /root/rpi/head/sys/arm/broadcom/bcm2835/bcm2835_gpio.c:389:13: error: unsequenced modification and access to ‘flags’ [-Werror,-Wunsequenced]
    if ((flags &= sc->sc_gpio_pins[i].gp_caps) != flags)
    ^ ~~~~~
    1 error generated.
    *** [bcm2835_gpio.o] Error code 1
    1 error
    *** [buildkernel] Error code 2
    1 error
    *** [buildkernel] Error code 2
    1 error

    NOTE: the error arrows are broken because the font is not fixed. The ^ is below the “&=”, while the ~~~~~ is below the “flags” at the end.

    • The code section is:

      /* Filter out unwanted flags. */
      if ((flags &= sc->sc_gpio_pins[i].gp_caps) != flags)
      return (EINVAL);

      I can see you would want to filter out flags which are not supported (by doing &= with gp_caps), but i do not see what the comparison with flags after that should do. Either it’s the old flags and the expression would be false if flags was changed – you could do & instead of &= then… Or it’s the new flags, the expression would be always true then. I changed the code so that it does what the comment above says. After applying this patch, it compiles for me. You might want to commit this. If the formatting is broken, i can email it to you.

      Index: sys/arm/broadcom/bcm2835/bcm2835_gpio.c
      ===================================================================
      — sys/arm/broadcom/bcm2835/bcm2835_gpio.c (revision 249439)
      +++ sys/arm/broadcom/bcm2835/bcm2835_gpio.c (working copy)
      @@ -386,8 +386,7 @@
      return (EINVAL);

      /* Filter out unwanted flags. */
      - if ((flags &= sc->sc_gpio_pins[i].gp_caps) != flags)
      - return (EINVAL);
      + flags &= sc->sc_gpio_pins[i].gp_caps;

      /* Can’t mix input/output together. */
      if ((flags & (GPIO_PIN_INPUT|GPIO_PIN_OUTPUT)) ==

  25. Hi, is it possible I can run this installation from a Linux machine?

  26. Is it possible to run a diskless system (apart from the FAT32 boot partition)? I notice that you were TFTP loading kernels previously so I guess it’s possible but it looks like uboot.img doesn’t probe the USB NIC by default.

    I’ve been running Linux with root-on-NFS but it’s a bit easier there because the kernel is in the FAT32 partition so I can just use cmdline.txt to tell the kernel to use root over NFS.

    Ideally, I’d like uboot (or ubldr) to use DHCP and either TFTP or NFS to load the kernel. Is this possible? If so, can you point me to some documentation on the necessary steps.

    • Yes, it’s possible. I run my development Pi in diskless mode. U-Boot just executes script in boot.scr if USB is not strarted – please attach serial port output for further investigation.

      As for how setup diskless boot I recently posted series of articles on this topic:
      http://kernelnomicon.org/?tag=netboot

      If there is some information missing – let me know, I’ll fill in missing pieces.

  27. Josef Larsson May 4, 2013 at 11:33 am

    Managed to get it running as a router now with USB ethernet. Thank you very much :) . It seems to be running at less than full speed but I have to test it thoroughly to say so confidently.

    One interesting behaviour is that ping latency seems to cycle in the following fashion for both ethernet cards: 10 ms, 9 ms, 8ms, … , 1ms, 10 ms, 9ms, 8ms, …, 1 ms and round again.

    Another problem seems to be the C++-compiler that get some kind of memory alignment error everytime I use it. (http://llvm.org/bugs/show_bug.cgi?id=15696)

    • If not known, it’s possible to switch from clang to gcc (which is still part
      of the base system), you have only to redefine CC, CXX and CPP in your
      /etc/make.conf . Not all ports will compile with clang at the moment.

      • Josef Larsson May 6, 2013 at 1:41 am

        Knew that already, but thanks anyway. Another approach is to set USE_GCC=any in the makefile of the port in question.

    • Ok, now I’m running in the same problem, tried to compile my first world directly on the pi. When sending my first post, I thought this was related to the llvm port and not to the world itself. Do you have a solution in the meanwhile?

      • Josef Larsson May 7, 2013 at 11:19 am

        Last time I checked, the clang-compiler built with world had the same version number as clang-devel in ports.

        If GCC won’t do for the world compilation, I do not think you can build world on the Pi before that bug is fixed.

        • I’ve tried to build the world with gcc, it breaks after 1 day compiling with
          an error.

          gcc -O -pipe -I/usr/src/lib/libc/include -I/usr/src/lib/libc/../../include -I/usr/src/lib/libc/arm -DNLS -D__DBINTERFACE_PRIVATE -I/usr/src/lib/libc/../../contrib/gdtoa -I/usr/src/lib/libc/../../contrib/libc-vis -DINET6 -I/usr/obj/usr/src/lib/libc -I/usr/src/lib/libc/resolv -D_ACL_PRIVATE -DPOSIX_MISTAKE -I/usr/src/lib/libc/../../contrib/jemalloc/include -I/usr/src/lib/libc/../../contrib/tzcode/stdtime -I/usr/src/lib/libc/stdtime -I/usr/src/lib/libc/locale -DBROKEN_DES -DPORTMAP -DDES_BUILTIN -I/usr/src/lib/libc/rpc -I/usr/src/lib/libc/arm/softfloat -I/usr/src/lib/libc/softfloat -DSOFTFLOAT_FOR_GCC -DYP -DNS_CACHING -DSYMBOL_VERSIONING -std=gnu99 -Wsystem-headers -Werror -Wall -Wno-format-y2k -Wno-uninitialized -Wno-pointer-sign -c accept4.S
          accept4.S: Assembler messages:
          accept4.S:3: Error: undefined symbol `SYS_accept4′ in operation
          *** [accept4.o] Error code 1

          Stop in /usr/src/lib/libc.
          *** [lib/libc__L] Error code 1

          Stop in /usr/src.
          *** [libraries] Error code 1

          Stop in /usr/src.
          *** [_libraries] Error code 1

          Stop in /usr/src.
          *** [buildworld] Error code 1

  28. As I tried to compile NodeJS (I know, ridiculous :p).. Does the current FreeBSD ARM support HardFloat, and if not.. are you aware of any work on adding HFloat support?

Trackbacks and Pingbacks:

  1. FreeBSD/Pi setup HowTo « FreeBSD developer's notebook - February 1, 2013

    [...] This instruction is no longer correct. New version is here. [...]

  2. FreeBSD ARM Tools ← Daveish - February 26, 2013

    [...] expansion on original by Gonzo from Building image for Raspberry Pi: up to date version. Added various switches and knobs to make the update / build process a little easier. Also, [...]

  3. FreeBSD на RaspberryPi - March 23, 2013

    [...] FreeBSD 10.x для RaspberryPi: http://kernelnomicon.org/?p=275. AKPC_IDS += "4202,";Popularity: unranked [...]

Leave a Reply

*

Text formatting is available via select HTML. <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>