Search This Blog

Powered By Blogger

Tuesday, April 25, 2017

An attempt to make a minimal busybox rootfs with glibc dropbear and wpa_supplicant on Orange pi zero Part1

This blog post and anything that comes after this and is related to this is probably gonna be less like a tutorial and more like an attempt to keep a record of whatever I am doing/did as I have found the steps forgotten pretty much every time I tried repeating them.

Okay,lets get started. I love single board computers. I have various ones(mostly from China). I was formerly working in an R&D company and I was doing basic linux image building using various build systems there. Now,up untill a few days ago,I was quite happy with the build systems available but then I saw a wonderful video on how someone managed to boot linux with busybox in just 1 frigging second! Here is the video,  


That got me thinking, hmm ,lets try that! First I will try to create a very basic filesystem and boot and will also enable wifi. Fast booting is for later experiments.
I chose Orange pi Zero as my guinea pig as I am playing with that board now.Its a pretty nice board with an Allwinner H2 quad core processor,512MB RAM and 10/100 ethernet and wifi! It costs about $6.99 in aliexpress(mine was twice as much as I bought it from a local source).
 
Alright,so the first step was to boot obviously.So,I downloaded the latest u-boot source and tried to build it for orange pi zero(u-boot has defconfig for this board so I didnt have to write my own) and volla! it failed. It was a gcc issue.I am running Ubuntu 16.04 and the default gcc version is 5.4.0(same for the arm-linux-gnueabihf-gcc I used) and u-boot and linux build for Allwinner boards doesnt go well with that. 
Then after some other random attempts,I tried another weird idea. I saw this link where they ran OpenWrt on that board using kernel and u-boot from Armbian build system.

So I thought,lets try it this way first.But, my requirement is a very basic filesystem with thing that I need only.For now, that is just wifi and dropbear with scp support.Now I am using u-boot and kernel from Armbian with all the drivers(I will load whatever I want).

For doing most of the steps,a linux system is needed. You can try linux in VirtualBox or VMWare if you are in Windows. For some of the super annoying problems, even I had to switch to a 32bit ubuntu server VM image as my 64 bit Ubuntu was throwing errors during some builds.
Note: use sudo if you see permission denied.
so here are my steps.
  1. Download/build an armbian ubuntu xenial headless image(kernel 3.4.113 for now). Armbian website has very nice download page for each of the single board computers they support.The Legacy one was the choice here.I will try the mainline one too.
  2. Write that to SD card.Now you have a nice Armbian filesystem with u-boot and 3.4.113 legacy kernel.
  3. Now delete everything there except boot and lib folder. Inside lib folder, you can delete everything except modules and firmware folders.
  4. Now time for some build operations. First some prerequisites.Some of them may not even be prerequisites, but I am still including them anyway.Note that these may not contain everything you need.So if an error comes like no such file bla bla bla,feel free to google.Now,open up a terminal and type,
    sudo apt-get install build-essential gawk autoconf libncurses5-dev git libssl-dev crossbuild-essential-armhf
  5. Once we are done with that. its time to build busybox. Its a set of many useful linux tools. Some of the version selections etc might look weird but its basically a compilation of things I scavenged from the internet.
    wget http://busybox.net/downloads/busybox-1.24.1.tar.bz2
    tar -xjf busybox-1.24.1.tar.bz2
    make ARCH=arm CROSS_COMPILE=arm-linux-gnueabihf- defconfig
    make ARCH=arm CROSS_COMPILE=arm-linux-gnueabihf- -j8
    make ARCH=arm CROSS_COMPILE=arm-linux-gnueabihf- install
          It then copies the files to _install folder in the busybox directory. Now we copy it to SD card. probably we could have copied it in the last command by specifying CONFIG_PREFIX.
    Now run,
    sudo rsync -a _install/ /path/to/sd/card

    This copies the contents of _install to SD card with proper permission.
  6. We are almost done on that.Now time to do some finishing touches.
    cd /path/to/sd/card
    mkdir proc sys dev etc
    sudo mknod dev/console c 5 1
    (note the missing / before dev.we are not working on our PC filesystem here.so dont put a / before anything unless specified here)
    sudo mknod dev/null c 1 3
    Now we create the inittab file where we specify what to load during boot
    gedit etc/inittab (add sudo if says permission denied)
    copy paste the following
    null::sysinit:/bin/mount -t proc proc /proc
    null::sysinit:/bin/hostname -F /etc/hostname
    null::respawn:/sbin/getty -L ttyS0 115200 vt100
    null::shutdown:/sbin/umount -a -r
    Save and close
    Now,
    mkdir root
    gedit etc/passwd

    copy paste the following
    root:x:0:0:root:/root:/bin/sh
    Save and exit
    gedit etc/shadow
    copy paste
    root::10933:0:99999:7:::
    Save and exit
    mkdir -p usr/share/udhcpc
    cp /path/to/busybox_src/examples/udhcp/simple.script usr/share/udhcpc/default.script
    gedit etc/hostname
    testlinux
  7. Now we make glibc which is used by various programs.
    wget http://ftp.gnu.org/gnu/libc/glibc-2.22.tar.xz
    tar -xJf glibc-2.22.tar.xz
    mkdir glibc-build
    cd glibc-build/
    ../glibc-2.22/configure arm-linux-gnueabihf --target=arm-linux-gnueabihf --build=x86_64-pc-linux-gnu --prefix= --enable-add-ons
    make
    make install install_root=/path/to/sd/card
    If you get error like LD_LIBRARY_PATH should not contain current directory etc. during configuration, run
    unset LD_LIBRARY_PATH
    then run configure again. 
  8. Now that everything is done,Open up a terminal in ur sd card folder and type sync.
Now remove the SD card and put it in your Orange pi Zero SD slot. Attach a USB-serial to it and fire up your favorite terminal emulator(picocom in my case). Power the Orange pi and you should see something like this.  
U-Boot SPL 2017.03-armbian (Mar 16 2017 - 14:23:25)
DRAM: 512 MiB
Trying to boot from MMC1


U-Boot 2017.03-armbian (Mar 16 2017 - 14:23:25 +0600) Allwinner Technology

CPU:   Allwinner H3 (SUN8I 1680)
Model: Xunlong Orange Pi Zero
DRAM:  512 MiB
MMC:   SUNXI SD/MMC: 0
*** Warning - bad CRC, using default environment

Setting up a 720x576i composite-pal console (overscan 32x20)
Error: no valid bmp image at 66000000
In:    serial
Out:   vga
Err:   vga
Net:   phy interface0
eth0: ethernet@1c30000
Hit any key to stop autoboot:  0
6944 bytes read in 159 ms (42 KiB/s)
switch to partitions #0, OK
mmc0 is current device
Scanning mmc 0:1...
Found U-Boot script /boot/boot.scr
1884 bytes read in 147 ms (11.7 KiB/s)
## Executing script at 43100000
115 bytes read in 123 ms (0 Bytes/s)
5156878 bytes read in 633 ms (7.8 MiB/s)
4754656 bytes read in 620 ms (7.3 MiB/s)
Found legacy kernel configuration
35384 bytes read in 487 ms (70.3 KiB/s)
## Loading init Ramdisk from Legacy Image at 43300000 ...
   Image Name:   uInitrd
   Image Type:   ARM Linux RAMDisk Image (gzip compressed)
   Data Size:    5156814 Bytes = 4.9 MiB
   Load Address: 00000000
   Entry Point:  00000000
   Verifying Checksum ... OK
   Loading Ramdisk to 49b15000, end 49ffffce ... OK

Starting kernel ...

[sun8i_fixup]: From boot, get meminfo:
    Start:    0x40000000
    Size:    512MB
ion_carveout reserve: 160m@0 256m@0 130m@1 200m@1
ion_reserve_select: ion chipid  [0x2c00042!
ion_reserve_common: ion reserve: [0x57e00000, 0x60000000]!
Loading, please wait...
starting version 229
Begin: Loading essential drivers ... done.
Begin: Running /scripts/init-premount ... done.
Begin: Mounting root file system ... Begin: Running /scripts/local-top ... done.
Begin: Running /scripts/local-premount ... Scanning for Btrfs filesystems
done.
Begin: Will now check root file system ... fsck from util-linux 2.27.1
[/sbin/fsck.ext4 (1) -- /dev/mmcblk0p1] fsck.ext4 -a -C0 /dev/mmcblk0p1
/dev/mmcblk0p1: recovering journal
/dev/mmcblk0p1: clean, 4787/89584 files, 89637/358144 blocks
done.
done.
Begin: Running /scripts/local-bottom ... done.
Begin: Running /scripts/init-bottom ... done.
mount: mounting /run on /root/run failed: No such file or directory

testboard login: root
login[151]: root login on 'ttyS0'
~ #
                                
 
Right now some errors are there and we will fix them.Right now the boot time is about 9 seconds from boot to console login.
References used:

No comments:

Post a Comment