Actions

EmSys

Difference between revisions of "Building the Linux Kernel - Cross-compiling for a BeagleBone Black"

From EdWiki

m (Mainline - Download)
m (Test Cross Compiler)
 
(19 intermediate revisions by the same user not shown)
Line 2: Line 2:
 
== Building the Linux Kernel - Cross-compiling for a BeagleBone Black ==
 
== Building the Linux Kernel - Cross-compiling for a BeagleBone Black ==
 
=== Install GNU toolchain for the A-Profile Architecture ===
 
=== Install GNU toolchain for the A-Profile Architecture ===
$ cd ~/Downloads<br />
+
{{EmSys:Install GNU toolchain for the A-Profile Architecture}}
$ Download GNU Toolchain for the A-profile Architecture: 9.2-2019.12 from [https://armkeil.blob.core.windows.net/developer/Files/downloads/gnu-a/9.2-2019.12/binrel/gcc-arm-9.2-2019.12-x86_64-arm-none-linux-gnueabihf.tar.xz here]<br />
+
$ sudo tar xvf ~/Downloads/gcc-arm-9.2-2019.12-x86_64-arm-none-linux-gnueabihf.tar.xz -C /opt/<br />
+
$ export CC=/opt/gcc-arm-9.2-2019.12-x86_64-arm-none-linux-gnueabihf/bin/arm-none-linux-gnueabihf-
+
  
 
=== Install Dependencies ===
 
=== Install Dependencies ===
Line 14: Line 11:
  
 
=== Test Cross Compiler ===
 
=== Test Cross Compiler ===
  $ ${CC}gcc --version
+
 
arm-none-linux-gnueabihf-gcc (GNU Toolchain for the A-profile Architecture 9.2-2019.12 (arm-9.10)) 9.2.1 20191025<br />
+
  $ ${CC}gcc --version<br />
Copyright (C) 2019 Free Software Foundation, Inc.<br />
+
 
 +
arm-linux-gnueabihf-gcc (GNU Toolchain for the A-profile Architecture 8.3-2019.03 (arm-rel-8.36)) 8.3.0<br />
 +
Copyright (C) 2018 Free Software Foundation, Inc.<br />
 
This is free software; see the source for copying conditions.  There is NO<br />
 
This is free software; see the source for copying conditions.  There is NO<br />
 
warranty; not even for MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.<br />
 
warranty; not even for MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.<br />
  
 
=== Bootloader: U-Boot ===
 
=== Bootloader: U-Boot ===
Das U-Boot – the Universal Boot Loader: [http://www.denx.de/wiki/U-Boot U-Boot]<br />
+
{{EmSys:Build U-Boot For BeagleBone}}
eewiki.net patch archive: [https://github.com/eewiki/u-boot-patches u-boot-patches]<br />
+
 
+
==== Download ====
+
 
+
$ cd ~/bbb_build
+
$ git clone https://github.com/u-boot/u-boot
+
$ cd u-boot/
+
$ git checkout v2019.04 -b v2019.04
+
 
+
==== Patches ====
+
 
+
$ cd ~/bbb_build/u-boot
+
$ wget -c https://github.com/eewiki/u-boot-patches/raw/master/v2019.04/0001-am335x_evm-uEnv.txt-bootz-n-fixes.patch
+
$ wget -c https://github.com/eewiki/u-boot-patches/raw/master/v2019.04/0002-U-Boot-BeagleBone-Cape-Manager.patch
+
 
+
$ patch -p1 < 0001-am335x_evm-uEnv.txt-bootz-n-fixes.patch
+
$ patch -p1 < 0002-U-Boot-BeagleBone-Cape-Manager.patch
+
 
+
==== Configure and Build ====
+
 
+
$ make ARCH=arm CROSS_COMPILE=${CC} distclean
+
$ make ARCH=arm CROSS_COMPILE=${CC} am335x_evm_defconfig
+
$ make ARCH=arm CROSS_COMPILE=${CC}
+
  
 
=== Linux Kernel ===
 
=== Linux Kernel ===
Line 58: Line 34:
 
<big>For am33x-rt-v5.4 (Longterm 5.4.x + Real-Time Linux):</big>
 
<big>For am33x-rt-v5.4 (Longterm 5.4.x + Real-Time Linux):</big>
  
  $ git checkout origin/am33x-rt-v5.4 -b am33x-rt-v5.4
+
   
 +
$ git checkout origin/am33x-rt-v5.4 -b am33x-rt-v5.4
  
 
==== Build ====
 
==== Build ====
 
  $ ./build_kernel.sh
 
  $ ./build_kernel.sh
  
=== Development/Hacking: ===
+
==== Development/Hacking: ====
  
 
first run (to setup baseline tree): ''./build_kernel.sh'' then modify files under KERNEL directory then run (to rebuild with your changes): ''./tools/rebuild.sh''
 
first run (to setup baseline tree): ''./build_kernel.sh'' then modify files under KERNEL directory then run (to rebuild with your changes): ''./tools/rebuild.sh''
 
+
<!--
 
=== TI BSP ===
 
=== TI BSP ===
 
'''Download:'''
 
'''Download:'''
Line 75: Line 52:
  
 
==== For TI v5.4.x: Real-Time ====
 
==== For TI v5.4.x: Real-Time ====
 
 
  $ git checkout origin/ti-linux-rt-5.4.y -b tmp
 
  $ git checkout origin/ti-linux-rt-5.4.y -b tmp
  
Line 81: Line 57:
  
 
  $ ./build_kernel.sh
 
  $ ./build_kernel.sh
 +
-->
  
 
=== Root File System ===
 
=== Root File System ===
Line 110: Line 87:
 
[[image:dev_sd_card.jpg]]
 
[[image:dev_sd_card.jpg]]
 
<br /><br />
 
<br /><br />
Thus you would use:
+
Thus you would use:<br />
  
  $ export DISK=/dev/sdb
+
  $ export DISK=/dev/sdb <br /><br />
  
Erase partition table/labels on microSD card:
+
Erase partition table/labels on microSD card:<br /><br />
  
  $ sudo dd if=/dev/zero of=${DISK} bs=1M count=10
+
  $ sudo dd if=/dev/zero of=${DISK} bs=1M count=10<br />
  
=== Install Bootloader ===
+
==== Install Bootloader ====
 
  $ cd ~/bbb_build
 
  $ cd ~/bbb_build
 
  $ sudo dd if=./u-boot/MLO of=${DISK} count=1 seek=1 bs=128k
 
  $ sudo dd if=./u-boot/MLO of=${DISK} count=1 seek=1 bs=128k
 
  $ sudo dd if=./u-boot/u-boot.img of=${DISK} count=2 seek=1 bs=384k
 
  $ sudo dd if=./u-boot/u-boot.img of=${DISK} count=2 seek=1 bs=384k
  
=== Create Partition Layout ===
+
==== Create Partition Layout ====
  
 
  $ sudo sfdisk ${DISK} <<-__EOF__
 
  $ sudo sfdisk ${DISK} <<-__EOF__
Line 129: Line 106:
 
  __EOF__
 
  __EOF__
  
=== Format Partition ===
+
==== Format Partition ====
 
With mkfs.ext4 1.43, we need to make sure metadata_csum and 64bit are disabled.
 
With mkfs.ext4 1.43, we need to make sure metadata_csum and 64bit are disabled.
 
As the version of U-Boot needed for this target CAN NOT correctly handle reading files with these newer ext4 options.
 
As the version of U-Boot needed for this target CAN NOT correctly handle reading files with these newer ext4 options.
Line 135: Line 112:
 
  $ sudo mkfs.ext4 -L rootfs -O ^metadata_csum,^64bit ${DISK}1
 
  $ sudo mkfs.ext4 -L rootfs -O ^metadata_csum,^64bit ${DISK}1
  
=== Mount Partition ===
+
==== Mount Partition ====
On most systems these partitions may will be auto-mounted...
+
On most systems these partitions may be auto-mounted...
  
 
  $ sudo mkdir -p /media/rootfs/
 
  $ sudo mkdir -p /media/rootfs/
 
  $ sudo mount ${DISK}1 /media/rootfs/
 
  $ sudo mount ${DISK}1 /media/rootfs/
  
=== Backup Bootloader ===
+
==== Backup Bootloader ====
 
This version of MLO/u-boot.img will be used on the "eMMC" flasher script on this page.
 
This version of MLO/u-boot.img will be used on the "eMMC" flasher script on this page.
  
Line 154: Line 131:
 
  $ export kernel_version=5.4.70-bone-rt-r38
 
  $ export kernel_version=5.4.70-bone-rt-r38
  
=== Copy Root File System ===
+
==== Copy Root File System ====
 
  $ ~/bbb_build
 
  $ ~/bbb_build
 
  $ sudo tar xfvp ./debian-10.4-minimal-armhf-2020-05-10/armhf-rootfs-debian-buster.tar -C /media/rootfs/
 
  $ sudo tar xfvp ./debian-10.4-minimal-armhf-2020-05-10/armhf-rootfs-debian-buster.tar -C /media/rootfs/
 
  $ sync
 
  $ sync
$ sudo chown root:root /media/rootfs/
 
$ sudo chmod 755 /media/rootfs/
 
  
=== Set uname_r in /boot/uEnv.txt ===
+
==== Set uname_r in /boot/uEnv.txt ====
 
  $ cd ~/bbb_build
 
  $ cd ~/bbb_build
 
  $ sudo sh -c "echo 'uname_r=${kernel_version}' >> /media/rootfs/boot/uEnv.txt"
 
  $ sudo sh -c "echo 'uname_r=${kernel_version}' >> /media/rootfs/boot/uEnv.txt"
  
=== Copy Kernel Image ===
+
==== Copy Kernel Image ====
 
  $ cd ~/bbb_build
 
  $ cd ~/bbb_build
 
  $ sudo cp -v ./bb-kernel/deploy/${kernel_version}.zImage /media/rootfs/boot/vmlinuz-${kernel_version}
 
  $ sudo cp -v ./bb-kernel/deploy/${kernel_version}.zImage /media/rootfs/boot/vmlinuz-${kernel_version}
  
=== Copy Kernel Device Tree Binaries ===
+
==== Copy Kernel Device Tree Binaries ====
 
  $ cd ~/bbb_build
 
  $ cd ~/bbb_build
 
  $ sudo mkdir -p /media/rootfs/boot/dtbs/${kernel_version}/
 
  $ sudo mkdir -p /media/rootfs/boot/dtbs/${kernel_version}/
 
  $ sudo tar xfv ./bb-kernel/deploy/${kernel_version}-dtbs.tar.gz -C /media/rootfs/boot/dtbs/${kernel_version}/
 
  $ sudo tar xfv ./bb-kernel/deploy/${kernel_version}-dtbs.tar.gz -C /media/rootfs/boot/dtbs/${kernel_version}/
  
=== Copy Kernel Modules ===
+
==== Copy Kernel Modules ====
 
  $ cd ~/bbb_build
 
  $ cd ~/bbb_build
 
  $ sudo tar xfv ./bb-kernel/deploy/${kernel_version}-modules.tar.gz -C /media/rootfs/
 
  $ sudo tar xfv ./bb-kernel/deploy/${kernel_version}-modules.tar.gz -C /media/rootfs/
  
=== File Systems Table (/etc/fstab) ===
+
==== File Systems Table (/etc/fstab) ====
 
$ sudo sh -c "echo '/dev/mmcblk0p1  /  auto  errors=remount-ro  0  1' >> /media/rootfs/etc/fstab"
 
$ sudo sh -c "echo '/dev/mmcblk0p1  /  auto  errors=remount-ro  0  1' >> /media/rootfs/etc/fstab"
  
Line 193: Line 168:
 
iface eth0 inet dhcp
 
iface eth0 inet dhcp
  
=== Networking: Using a shared SD card with Multiple BeagleBone ===
+
==== Networking: Using a shared SD card with Multiple BeagleBone ====
 
To always enable the Ethernet interface as eth0.<br />
 
To always enable the Ethernet interface as eth0.<br />
 
Edit: /etc/udev/rules.d/70-persistent-net.rules
 
Edit: /etc/udev/rules.d/70-persistent-net.rules
Line 199: Line 174:
 
  $ sudo nano /media/rootfs/etc/udev/rules.d/70-persistent-net.rules
 
  $ sudo nano /media/rootfs/etc/udev/rules.d/70-persistent-net.rules
  
==== Add ====
+
===== Add =====
 
<nowiki>#</nowiki> BeagleBone: net device ()<br />
 
<nowiki>#</nowiki> BeagleBone: net device ()<br />
 
SUBSYSTEM=="net", ACTION=="add", DRIVERS=="?*", ATTR{dev_id}=="0x0", ATTR{type}=="1", KERNEL=="eth*", NAME="eth0"
 
SUBSYSTEM=="net", ACTION=="add", DRIVERS=="?*", ATTR{dev_id}=="0x0", ATTR{type}=="1", KERNEL=="eth*", NAME="eth0"
  
 
=== Remove microSD/SD card ===
 
=== Remove microSD/SD card ===
 +
$ sudo chown root:root /media/rootfs/
 +
$ sudo chmod 755 /media/rootfs/
 
  $ sync
 
  $ sync
 
  $ sudo umount /media/rootfs
 
  $ sudo umount /media/rootfs
  
Plug the microSD card in to beaglebone black and press hold "'''Boot Switch'''" button and power it up.
+
=== Boot new image ===
 +
* Connect the Micro SD Card to the BBB SD Card Connector
 +
* Power-Up the BBB  ( You can use mini USB (P4) )
 +
* Connect Serial Cable to Beagleboard as explained [[EmSys:BeagleBone Black Serial | here]]
 +
* Press and Hold '''S2''' Button then press and release '''S3''' Button
 +
 
 +
 
 +
'''Note:'''
 +
To boot from the microSD card , you need to hold the button S2, this button is near the host USB port, on the other side of the ethernet port. If this is not held, the board may follow its default boot order and try booting from the onboard eMMC, if a uboot image is available on the onboard eMMC, our uboot image will not be built.
 +
 
 +
=== Flashing eMMC ===
 +
To set up the standalone microSD image to automatically flash the eMMC on powerup. Login as debian (password = temppwd) and edit /boot/uEnv.txt with your preferred editor.
 +
 
 +
In /boot/uEnv.txt:
 +
 
 +
<nowiki>##</nowiki>enable BBB: eMMC Flasher:<br />
 +
<nowiki>#</nowiki>cmdline=init=/opt/scripts/tools/eMMC/init-eMMC-flasher-v3.sh<br />
 +
 
 +
Change to:<br />
 +
 
 +
<nowiki>##</nowiki>enable BBB: eMMC Flasher:<br />
 +
cmdline=init=/opt/scripts/tools/eMMC/init-eMMC-flasher-v3.sh<br />

Latest revision as of 07:46, 3 December 2020

Building the Linux Kernel - Cross-compiling for a BeagleBone Black

Install GNU toolchain for the A-Profile Architecture

$ cd ~/Downloads
$ Download GNU Toolchain for the A-profile Architecture: 8.3-2019.03 from here
$ sudo tar xvf ~/Downloads/gcc-arm-8.3-2019.03-x86_64-arm-linux-gnueabihf.tar.xz -C /opt/
$ export CC=/opt/gcc-arm-8.3-2019.03-x86_64-arm-linux-gnueabihf/bin/arm-linux-gnueabihf-

Install Dependencies

$ sudo apt-get install bison flex lzop lzma libmpc-dev u-boot-tools libncurses5-dev:amd64 gettext
$ mkdir ~/bbb_build
$ cd ~/bbb_build

Test Cross Compiler

$  ${CC}gcc --version

arm-linux-gnueabihf-gcc (GNU Toolchain for the A-profile Architecture 8.3-2019.03 (arm-rel-8.36)) 8.3.0
Copyright (C) 2018 Free Software Foundation, Inc.
This is free software; see the source for copying conditions. There is NO
warranty; not even for MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.

Bootloader: U-Boot

Build U-Boot For BeagleBone

Das U-Boot – the Universal Boot Loader: U-Boot
eewiki.net patch archive: u-boot-patches

Download
$ cd ~/bbb_build
$ git clone https://github.com/u-boot/u-boot
$ cd u-boot/
$ git checkout v2019.04 -b v2019.04
Patches
$ cd ~/bbb_build/u-boot
$ wget -c https://github.com/eewiki/u-boot-patches/raw/master/v2019.04/0001-am335x_evm-uEnv.txt-bootz-n-fixes.patch
$ wget -c https://github.com/eewiki/u-boot-patches/raw/master/v2019.04/0002-U-Boot-BeagleBone-Cape-Manager.patch
 
$ patch -p1 < 0001-am335x_evm-uEnv.txt-bootz-n-fixes.patch
$ patch -p1 < 0002-U-Boot-BeagleBone-Cape-Manager.patch
Configure and Build
$ make ARCH=arm CROSS_COMPILE=${CC} distclean
$ make ARCH=arm CROSS_COMPILE=${CC} am335x_evm_defconfig
$ make ARCH=arm CROSS_COMPILE=${CC}


Linux Kernel

This script will build the kernel, modules, device tree binaries and copy them to the deploy directory.

Mainline - Download

$ cd ~/bbb_build
$ git clone https://github.com/RobertCNelson/bb-kernel
$ cd bb-kernel/


For am33x-rt-v5.4 (Longterm 5.4.x + Real-Time Linux):


$ git checkout origin/am33x-rt-v5.4 -b am33x-rt-v5.4

Build

$ ./build_kernel.sh

Development/Hacking:

first run (to setup baseline tree): ./build_kernel.sh then modify files under KERNEL directory then run (to rebuild with your changes): ./tools/rebuild.sh

Root File System

Debian 9

User : debian
Password : temppwd

Download

$ cd ~/bbb_build
$ wget -c https://rcn-ee.com/rootfs/eewiki/minfs/debian-10.4-minimal-armhf-2020-05-10.tar.xz

Verify

$ sha256sum debian-10.4-minimal-armhf-2020-05-10.tar.xz

sha256sum output

cd598e42850cbef87602bf15ee343abfbf0d8c6ba81028c741672b5f24263534  debian-10.4-minimal-armhf-2020-05-10.tar.xz

Extract

$ tar xf debian-10.4-minimal-armhf-2020-05-10.tar.xz

Setup microSD card

We need to access the External Drive to be utilized by the target device. Run lsblk to help figure out what linux device has been reserved for your External Drive.

$ lsblk

Dev sd card.jpg

Thus you would use:

$ export DISK=/dev/sdb 

Erase partition table/labels on microSD card:

$ sudo dd if=/dev/zero of=${DISK} bs=1M count=10

Install Bootloader

$ cd ~/bbb_build
$ sudo dd if=./u-boot/MLO of=${DISK} count=1 seek=1 bs=128k
$ sudo dd if=./u-boot/u-boot.img of=${DISK} count=2 seek=1 bs=384k

Create Partition Layout

$ sudo sfdisk ${DISK} <<-__EOF__
4M,,L,*
__EOF__

Format Partition

With mkfs.ext4 1.43, we need to make sure metadata_csum and 64bit are disabled. As the version of U-Boot needed for this target CAN NOT correctly handle reading files with these newer ext4 options.

$ sudo mkfs.ext4 -L rootfs -O ^metadata_csum,^64bit ${DISK}1

Mount Partition

On most systems these partitions may be auto-mounted...

$ sudo mkdir -p /media/rootfs/
$ sudo mount ${DISK}1 /media/rootfs/

Backup Bootloader

This version of MLO/u-boot.img will be used on the "eMMC" flasher script on this page.

$ cd ~/bbb_build
$ sudo mkdir -p /media/rootfs/opt/backup/uboot/
$ sudo cp -v ./u-boot/MLO /media/rootfs/opt/backup/uboot/
$ sudo cp -v ./u-boot/u-boot.img /media/rootfs/opt/backup/uboot/

Install Kernel and Root File System

Copy and paste that "export kernel_version=4.X.Y-Z" exactly as shown in your own build/desktop environment and hit enter to create an environment variable to be used later.

$ export kernel_version=5.4.70-bone-rt-r38

Copy Root File System

$ ~/bbb_build
$ sudo tar xfvp ./debian-10.4-minimal-armhf-2020-05-10/armhf-rootfs-debian-buster.tar -C /media/rootfs/
$ sync

Set uname_r in /boot/uEnv.txt

$ cd ~/bbb_build
$ sudo sh -c "echo 'uname_r=${kernel_version}' >> /media/rootfs/boot/uEnv.txt"

Copy Kernel Image

$ cd ~/bbb_build
$ sudo cp -v ./bb-kernel/deploy/${kernel_version}.zImage /media/rootfs/boot/vmlinuz-${kernel_version}

Copy Kernel Device Tree Binaries

$ cd ~/bbb_build
$ sudo mkdir -p /media/rootfs/boot/dtbs/${kernel_version}/
$ sudo tar xfv ./bb-kernel/deploy/${kernel_version}-dtbs.tar.gz -C /media/rootfs/boot/dtbs/${kernel_version}/

Copy Kernel Modules

$ cd ~/bbb_build
$ sudo tar xfv ./bb-kernel/deploy/${kernel_version}-modules.tar.gz -C /media/rootfs/

File Systems Table (/etc/fstab)

$ sudo sh -c "echo '/dev/mmcblk0p1 / auto errors=remount-ro 0 1' >> /media/rootfs/etc/fstab"

Networking

Edit: /etc/network/interfaces

$ sudo nano /media/rootfs/etc/network/interfaces

Add

auto lo
iface lo inet loopback

auto eth0
iface eth0 inet dhcp

Networking: Using a shared SD card with Multiple BeagleBone

To always enable the Ethernet interface as eth0.
Edit: /etc/udev/rules.d/70-persistent-net.rules

$ sudo nano /media/rootfs/etc/udev/rules.d/70-persistent-net.rules
Add

# BeagleBone: net device ()
SUBSYSTEM=="net", ACTION=="add", DRIVERS=="?*", ATTR{dev_id}=="0x0", ATTR{type}=="1", KERNEL=="eth*", NAME="eth0"

Remove microSD/SD card

$ sudo chown root:root /media/rootfs/
$ sudo chmod 755 /media/rootfs/
$ sync
$ sudo umount /media/rootfs

Boot new image

  • Connect the Micro SD Card to the BBB SD Card Connector
  • Power-Up the BBB ( You can use mini USB (P4) )
  • Connect Serial Cable to Beagleboard as explained here
  • Press and Hold S2 Button then press and release S3 Button


Note: To boot from the microSD card , you need to hold the button S2, this button is near the host USB port, on the other side of the ethernet port. If this is not held, the board may follow its default boot order and try booting from the onboard eMMC, if a uboot image is available on the onboard eMMC, our uboot image will not be built.

Flashing eMMC

To set up the standalone microSD image to automatically flash the eMMC on powerup. Login as debian (password = temppwd) and edit /boot/uEnv.txt with your preferred editor.

In /boot/uEnv.txt:

##enable BBB: eMMC Flasher:
#cmdline=init=/opt/scripts/tools/eMMC/init-eMMC-flasher-v3.sh

Change to:

##enable BBB: eMMC Flasher:
cmdline=init=/opt/scripts/tools/eMMC/init-eMMC-flasher-v3.sh