This guide explains step by step how to create a minimal linux image for the Rasperry PI 3,
using buildroot and a Linux host system (such as Fedora, Debian or Ubuntu).
The kernel is armv8 (64-bits).
Instead of using glibc, we will use uCLibc.
We will use a small initramfs image.
All files are stored in a single fat32 partition of the uSD card.
This step describes how to download buildroot, and build an image.
Create a working directory on the Linux host PC, for instance:
~$ mkdir -p lfs/rpi3/min_img
~$ cd lfs/rpi3/min_img
~/lfs/rpi3/min_img$
Download and extract the latest released version of buildroot:
~/lfs/rpi2/min_img$ wget --no-check-certificate https://buildroot.org/downloads/buildroot-2017.11.tar.bz2
~/lfs/rpi2/min_img$ tar -f buildroot-2017.11.tar.bz2 -x
~/lfs/rpi2/min_img$ cd buildroot-2017.11/
~/lfs/rpi2/min_img/buildroot-2017.11/$
Use either make config or make xconfig to adjust the configuration.
The configuration is:
Target options
- Target Architecture: AARCH64 (little endian)
Build options
Toolchain
- Custom kernel headers series4.9.x
System configuration
- Root password: root (or any password you want)
Kernel
- Kernel versionCustom Git repository
- URL of custom repositoryhttps://github.com/raspberrypi/linux.git
- Custom repository versionrpi-4.9.y
- Defconfig namebcmrpi3
- Select: Build a Device Tree Blob (DTB)
- Device Tree Source file namesbroadcom/bcm2710-rpi-3-b
Target packages
- Hardware handling / Firmware - Select: rpi-firmware
Filesystem images
- Select: cpio the root filesystem (for use as an initial RAM filesystem)
- Compression method: gzip
Host utilities
- Select: host dosfstools
- Select: host genimage
- Select: host mtools
Notes:
- By default, the build will use all cpu cores. Specify the maximum number of cores you want to use
(For instance, my machine at home is 8-cores CPU, I usually use 4 or 6 cores for the build)
You can download the configuration file
rpi3_minimal_defconfig
If you want to use the configuration file instead of changing the configuration manuall, you can use these steps:
~/lfs/rpi3/min_img/buildroot-2017.11$ wget https://rawgit.com/osingla/LinuxFromScratch/gh-pages/rpi3/minimal_img/rpi3_minimal_defconfig
~/lfs/rpi3/min_img/buildroot-2017.11$ mv rpi3_minimal_defconfig configs/
~/lfs/rpi3/min_img/buildroot-2017.11$ make rpi3_minimal_defconfig
Depending of the speed of your build machine and your Internet speed, the build could take a while, typically in the 20 to 30 minutes for a first time build.
Build now:
~/lfs/rpi3/min_img/buildroot-2017.11$ make
Once the build is done, you will have the kernel, the Device Tree Blob (DTB), and the root filesystem tar file:
~/lfs/rpi3/min_img/buildroot-2017.11$ tree --charset unicode -L 2 output/images/
output/images/
|-- bcm2710-rpi-3-b.dtb
|-- Image
|-- rootfs.cpio
|-- rootfs.cpio.gz
|-- rootfs.tar
`-- rpi-firmware
|-- bootcode.bin
|-- cmdline.txt
|-- config.txt
|-- fixup.dat
|-- overlays
`-- start.elf
2 directories, 10 files
This step describes how to create a uSD card using the image previously built.
Any uSD card will do, assuming it's at least a 2 GB card. >br>
We will create one partition on the uSD card. Actually, what really matters is the first partition of the uSD card:
- the 1st partition needs to be FAT32, and
- the 1st partition needs to be at least 128 MB
The first fat32 partition will hold
- the firmware (boot loader)
- the kernel and the device tree blob (dtb)
- the initramfs image
Use fdisk to create the partitions:
- partition 1 : type fat32 (12, 0x0C), 128 M
Once you have created the partitions on the uSD card, assuming your linux host assigned the device /dev/sdg
(you can check using dmesg), you will do something like this to copy the firmware, the kernel and the DTB:
~/lfs/rpi3/min_img/buildroot-2017.02.8$ rm -rf /tmp/rpi3-sd
~/lfs/rpi3/min_img/buildroot-2017.02.8$ mkdir -p /tmp/rpi3-sd
~/lfs/rpi3/min_img/buildroot-2017.02.8$ fdisk -l /dev/sdg
Disk /dev/sdg: 62.5 GiB, 67108864000 bytes, 131072000 sectors
Units: sectors of 1 * 512 = 512 bytes
Sector size (logical/physical): 512 bytes / 512 bytes
I/O size (minimum/optimal): 512 bytes / 512 bytes
Disklabel type: dos
Disk identifier: 0x00000000
Device Boot Start End Sectors Size Id Type
/dev/sdg1 * 2048 131071999 131069952 62.5G c W95 FAT32 (LBA)
~/lfs/rpi3/min_img/buildroot-2017.02.8$ sudo partprobe /dev/sdg
~/lfs/rpi3/min_img/buildroot-2017.02.8$ mkfs.vfat -n rpi3-sd /dev/sdg1
mkfs.fat 4.1 (2017-01-24)
mkfs.fat: warning - lowercase labels might not work properly with DOS or Windows
~/lfs/rpi3/min_img/buildroot-2017.02.8$ sudo mount /dev/sdg1 /tmp/rpi3-sd
~/lfs/rpi3/min_img/buildroot-2017.02.8$ sudo cp -r output/images/rpi-firmware/* /tmp/rpi3-sd/
~/lfs/rpi3/min_img/buildroot-2017.02.8$ sudo cp output/images/bcm2710-rpi-3-b.dtb /tmp/rpi3-sd/
~/lfs/rpi3/min_img/buildroot-2017.02.8$ sudo cp output/images/Image /tmp/rpi3-sd/
~/lfs/rpi3/min_img/buildroot-2017.02.8$ sudo cp output/images/rootfs.cpio.gz /tmp/rpi3-sd/
You now need to edit two files on the uSD card:
Since we use an initramfs image, we need to change the kernel parameters specified in /tmp/rpid3-sd/cmdline.txt:
change the line
root=/dev/mmcblk0p2 rootwait console=tty1 console=ttyAMA0,115200
with
console=tty1 console=ttyAMA0,11520
We need now to make a few modifications in /tmp/rpid3-sd/config.txt
Change the line
kernel=zImage
with
kernel=Image
Change the line
#initramfs rootfs.cpio.gz
with
initramfs rootfs.cpio.gz
Add the line at the end of the file
arm_control=0x200
You can now unmount the uSD card and eject it:
~/lfs/rpi3/min_img/buildroot-2017.11$ sudo umount /tmp/rpi3-sd
~/lfs/rpi3/min_img/buildroot-2017.11$ sudo eject /dev/sdg
I wrote a simple shell script which create the two partitions on the uSD card, and then copy the files.
Feel free to use it instead of typing all the commmands above.
~/lfs/rpi3/min_img/buildroot-2015.11.1$ wget https://raw.githubusercontent.com/osingla/LinuxFromScratch/gh-pages/create-sdcard1.sh
~/lfs/rpi4/min_img/buildroot-2015.11.1$ ./create-sdcard1.sh --device /dev/sdg
YOU NEED TO ADJUST THE DEVICE NAME.
PLEASE TAKE A LOOK AT THE OUTPUT OF dmesg TO SEE THE DEVICE NAME.
You are now ready to test your uSD card.
Insert this card on your Rasperry PI3, and apply power.
If all goes well, you should get a shell.