⬆️ ⬇️

Banana Pi: via U-Boot to Arch Linux

For the fourth year in a row, since the release of the Raspberry Pi, various microcomputers have been delivered to markets around the world on non-x86 architectures that act as media centers, smart home controllers, web servers and anything else the geek wants!



By 2014-2015, not everyone was pleased with the computing capabilities of the “raspberry” and the release of dozens of its clones with more powerful hardware began. Most of them have drawbacks: a fixed outdated version of the kernel and bootloader, a small selection of distributions. Under the cut, I’ll tell you how to throw off the vendor chains with the example of Banana Pi.



Prehistory



In the second half of 2014, I wanted to have a home server, the choice was between the classic x86 architecture with the Intel Atom processor and something new for me - an ARM-based microcomputer.



After evaluating the price / performance / capability ratios, the choice fell on a Banana Pi - a baby with a dual-core Allwinner A20, 1 GB DDR3, a gigabit LAN, a SATA connector, and other amenities of similar devices. After playing for a couple of days, I made a case from plastic parts, in which Banana was sharpened and served for a long time for all sorts of small tasks.

')

The distribution was Bananian - this is Debian 7 Wheezy trimmed in many ways to save resources. There is no newer Debian 8.x to this day, you can imagine how old the software is there, which receives only security patches. All this ugliness lasted until mid-summer ...







Finally in July it became necessary to raise the web server and related services for its pet project. It was time to change old Debian to something less conservative and more convenient for me.



Formulation of the problem



There were several requirements for the distribution: there should be “out of the box” repositories covering all needs, the programs should be closest to the latest available versions. I follow the development of distributions and their programs, but at the same time I am too lazy to compile everything from sorts, so Arch Linux became the obvious choice.



On the manufacturer’s website in the downloads section, I was waiting for a year overdue image with a wired non-updated kernel (3.4.100), a loader of the same old age from another device (Cubieboard 2). On the Arch Linux site, Banana Pi support was not announced. Proceeding from all this, there was only one thing left: to build and run the distribution kit itself.



Materiel







Das U-Boot (the origin of the name is somehow related to submarines and wordplay in German and English, see U-boat )

This is an operating system loader, device-oriented, with MIPS, PowerPC, ARM and other architectures. He can do a lot: loads the system from NAND, NOR, SPI, MMC, SATA, USB, TFTP, NFS, can be stored in the device's ROM or external memory, has a built-in dhcp client and the shell, receives signals from the keyboard, outputs to UART, HDMI , VGA, analog video. Naturally to support all this you need to have drivers.



This is where the difference from x86 platforms begins - ARM does not have ACPI (advanced configuration and power management interface) and UEFI (interaction interface between the operating system and device firmware), and that is why there are no unified distributions for ARM computers, but the movement is UEFI and ACPI side has been going for several years. In the meantime, this movement is going on, we are in a real, harsh world and the bootloader needs to be compiled independently for different hardware. Iron is described in a data structure called Device Tree (dt) . More about DT can be found on the site dedicated to him: devicetree.org .



A set of boot files is supplied with the source code for the compilation, as well as a Device Tree for 1089 devices. Google, unfortunately, does not find this list, so it is available at the link: U-boot supported devices .







Linux Kernel



A lot has been written about him, a lot has been said, and now only a few facts are useful:

  1. The distributions I have encountered have stored the kernel in several forms:

    • Image is not a compressed image.
    • zImage is a compressed, self-extracting image.
    • uImage is an image that has a wrapper that includes some information for the U-Boot.


    Previously, uImage was used extensively for devices with U-Boot, but over the past years, the developers have taught the bootloader to work with zImage, which has become a common practice of storing a kernel image in the Arch Linux ARM distribution.
  2. Happy news! The core within the same architecture (ARMv5, ARMv6, ARMv7, ARMv8) is likely not to be compiled for each device. It uses its version of the Device Tree , called Device Tree Binary (dtb) . These are small files that tell the kernel which modules to load. More information about dtb can be found here .



From theory to practice



It will take:



Download and unpack the source code of the latest version of U-Boot:

 wget ftp://ftp.denx.de/pub/u-boot/u-boot-latest.tar.bz2 tar -jxf u-boot-latest.tar.bz2 


We find our piece of hardware in the u-boot-2015.07 / configs directory that appears and remember the name of the file. For Banana Pi, the configuration file is called Bananapi_defconfig .



Also remember that the Arch Linux ARM distribution kernel is stored in a zImage? This should be taken care of in advance and before compiling add the ability to load self-extracting kernels. I don’t have exact information on whether this is already enabled by default, so in the u-boot-2015.07 / include / configs / directory you need to find the header file for your microcomputer platform (for Banana Pi, this is sun7i.h ) and write inside:

 #ifndef __CONFIG_H #define __CONFIG_H ... #endif /* __CONFIG_H */ 


the string

 #define CONFIG_CMD_BOOTZ 


This will add the ability to use the bootz command in the bootloader to launch self-extracting compressed programs. A source



Now the compilation stage. All operations occur in the directory u-boot-2015.07 .

What if no cross-compilation?
If you compile on the same architecture (for example, on a computer with ARMv7), then make the CROSS_COMPILE flag omitted.



 make CROSS_COMPILE=arm-linux-gnueabihf- Bananapi_defconfig 


Directly bootloader compilation:

 make CROSS_COMPILE=arm-linux-gnueabihf- 


As a result, many files with the u-boot prefix should appear in our working folder. U-boot.bin contains the executable file of the loader, but we need a file with the postfix “with-spl”. SPL is a small program that is located directly in front of the bootloader and loads U-Boot itself into RAM. For Banana Pi, this is the u-boot-sunxi-with-spl.bin file . Save it, it will come in handy later.



The next step is to prepare a flash card with which the system will boot. What should markup look like:







We connect the flash card through the card reader and run the fdisk utility:

 fdisk /dev/sdb 


How to find out what is the name of your card?
A new file corresponding to the inserted map should appear in the / dev / directory. It will be defined either as a SCSI device (sda, sdb, sdc ...), or as a block MMC memory (mmcblk0, mmcblk1 ...).



Add MBR markup:

 Command (m for help): o Created a new DOS disklabel with disk identifier 0xa53166ce. 


And be sure to at least two sections:

  1. 50-100 MB

    fdisk markup first
     Command (m for help): n Partition type p primary (0 primary, 0 extended, 4 free) e extended (container for logical partitions) Select (default p): p Partition number (1-4, default 1): 1 First sector (2048-7831551, default 2048): 2048 Last sector, +sectors or +size{K,M,G,T,P} (2048-7831551, default 7831551): +75M Created a new partition 1 of type 'Linux' and of size 75 MiB. 




  2. 2 GB or more

    fdisk, markup second
     Command (m for help): n Partition type p primary (1 primary, 0 extended, 3 free) e extended (container for logical partitions) Select (default p): p Partition number (2-4, default 2): 2 First sector (155648-7831551, default 155648): 155648 Last sector, +sectors or +size{K,M,G,T,P} (155648-7831551, default 7831551): +3G Created a new partition 2 of type 'Linux' and of size 3 GiB. 






After all this, enter "w", thereby writing the changes to disk.



Why such sections? U-Boot can be managed using scripts, it reads them only from the first partition of the drive, and this section should be with the FAT file system. The Arch Linux future boot directory will be mounted in the same section for convenient access to the cores from both U-Boot scripts and for quick replacement of kernels from any computer. The second section is for the root of the system. The rest is optional.



After marking you need to clear the place for the loader. It is extremely important not to overwrite the first 512 bytes (MBR) and not to affect 1024 sectors and further (based on the table above):

 dd if=/dev/zero of=/dev/sdb bs=1K count=1023 seek=1 


We write the U-Boot:

 dd if=u-boot-sunxi-with-spl.bin of=/dev/sdb bs=1024 seek=8 


Now you can insert the USB flash drive into the destination device, turn it on and see how it blinks LEDs cheerfully, picks up the ip address via dhcp, tries to reach the tftp server to boot and friendly invites you to your Shell showing it through the output or UART.



But there is not enough one bootloader, there are still a few components left: a script for managing the U-Boot and the Arch Linux distribution.

Go to os.archlinuxarm.org and download the archive with the appropriate distribution of our architecture. For Banana, this is ArchLinuxARM-armv7-latest.tar.gz. Unpack in a separate directory and continue to manipulate the flash card.



Create file systems and mount them in the specified order:

 mkfs.vfat /dev/sdb1 mkfs.ext4 /dev/sdb2 mount /dev/sdb2 /mnt mkdir /mnt/boot mount /dev/sdb1 /mnt/boot 


Copy the entire contents of the unpacked archive into / mnt:

 cp -r ~/ArchLinux-generic-armv7/* /mnt 


Let's look at the first section of the flash drive (/ mnt / boot). Inside we are waiting for the dtbs folder and the compressed image of the kernel - zImage. dtbs are the very Device Tree Binary discussed above .

For ARMv7 there are as many as 301 dtb. The list can be seen here or directly by opening the distribution / boot distribution directory on your computer. If there is a dtb, then the kernel will start with almost 100% probability. For Banana Pi, the required file is sun7i-a20-bananapi.dtb . We need it for the last and final stage - writing a script for the U-Boot and, of course, running the entire system.



Let's get started Let's create the file boot.cmd right on the spot (in / mnt / boot ) and start writing.



First of all, let's order U-Boot to load a kernel image into RAM:

 fatload mmc 0 0x46000000 zImage 


Then dtb for the desired device:

 fatload mmc 0 0x49000000 dtbs/sun7i-a20-bananapi.dtb 


Set the parameters for running the kernel:

 setenv bootargs console=ttyS0,115200 earlyprintk root=/dev/mmcblk0p2 rw rootwait panic=10 


And let it boot from this area of ​​memory, thereby transferring control to the Linux kernel.

 bootz 0x46000000 - 0x49000000 


The memory areas available for download are different for each model, and unfortunately I don’t know how to determine them myself. I would be very happy if someone tells about this in simple words in the comments.



The script we wrote can only be compiled. To do this, the uboot-tools package has the mkimage utility, we will use it:

 mkimage -C none -A arm -T script -d boot.cmd boot.scr 


boot.cmd can be removed if desired, the bootloader only needs boot.scr.

And is there something else like boot.scr?
Yes, sometimes. U-Boot for some devices can be configured via a simple text file uEnv.txt, it depends on the vendor, but with Banana Pi this trick does not work.


And the final touch - we add sections to the etc / fstab system that will be connected at startup:

 /dev/mmcblk0p2 / ext4 rw,relatime,data=ordered 0 1 /dev/mmcblk0p1 /boot vfat rw,relatime,fmask=0022,dmask=0022,codepage=437,iocharset=iso8859-1,shortname=mixed,errors=remount-ro 0 2 


A flash drive can be inserted into the device and enjoy the fresh system. The dhcpd daemon will pick up the address itself if there is a DHCP server on your network. SSH server is added by default to autoload. The account on the default Arch Linux ARM images is always the same: root / root. Be sure to change the password and add a user with limited privileges.







Total



What did you get? The stars so agreed that the support of my microcomputer is in the list of 1089 devices supported by U-Boot and among the 301st among the mainline-supported kernels for ARMv7. Therefore, we got exactly what we wanted: a system with the latest software, which is stably updated from repositories, including the kernel of the system (currently 4.1.4). The stable bootloader branch is updated once every 3 months and you only need to compile it and repeat the installation procedure, as done above.



What does not work out of the box?
As part of the kernel and distribution, there is neither the kernel nor the userspace of the Mali driver. This is due to the fact that a full-featured build is possible only with the help of a proprietary SDK, which is provided only to ARM partners.


For the lazy



Prepared the image for Banana Pi:

Link , Mirror # 1 , Mirror # 2 .

md5 image in the archive: f36d707c4c1fd857b50d37501b4a3d7a

You can write the image to the flash card using the dd utility or win32diskimager (for Windows users).



Sources



linux-sunxi.org/Mainline_U-boot

elinux.org/RPi_Hub

ru.wikipedia.org/wiki/Das_U-Boot



I wish good luck to those who want to repeat this at home and will be glad to fair criticism.



Source: https://habr.com/ru/post/264259/



All Articles