Building U-Boot & Linux For The Beagle Bone Black (BBB)

Also want to checkout the following:
    https://unix.stackexchange.com/questions/457109/beaglebone-black-running-upstream-stable-kernel-u-boot-without-any-patches

Reference: This is just a copy (with some minor mods) of the instructions found here.

Build U-Boot, Linux Kernel, Create Bootable SD Card

The following worked fine on a Virtual Box Ubuntu VM running on Windows (didn't have a native linux box available :().

##
## Create a working directory
mkdir ~/bbb_build
cd ~/bbb_build

##
## Download the compiler
wget -c https://releases.linaro.org/components/toolchain/binaries/6.4-2018.05/arm-linux-gnueabihf/gcc-linaro-6.4.1-2018.05-x86_64_arm-linux-gnueabihf.tar.xz
# OR...                                                          /7.3-2018.05/arm-linux-gnueabihf/gcc-linaro-7.3.1-2018.05-x86_64_arm-linux-gnueabihf.tar.xz
# Browse https://releases.linaro.org/components/toolchain/binaries for the latest and greatest...
tar xf gcc-linaro-6.4.1-2018.05-x86_64_arm-linux-gnueabihf.tar.xz
export CC=`pwd`/gcc-linaro-6.4.1-2018.05-x86_64_arm-linux-gnueabihf/bin/arm-linux-gnueabihf-
export PATH=$PATH:`pwd`/gcc-linaro-6.4.1-2018.05-x86_64_arm-linux-gnueabihf/bin

##
## Download and build U-Boot
git clone https://github.com/u-boot/u-boot
cd u-boot/
git checkout v2018.09 -b tmp #<< Check for latest

## 
## Get and apply patches
wget -c https://rcn-ee.com/repos/git/u-boot-patches/v2018.09/0001-am335x_evm-uEnv.txt-bootz-n-fixes.patch
wget -c https://rcn-ee.com/repos/git/u-boot-patches/v2018.09/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}

##
## Clone and build Linux Kernel
## The clone actually checks out Robert Nelson's scripts and patches repository. It is the script build_kernel.sh
## that actually clones the linux kernel and then applies the relevant patches, downloads the cross compiler and
## then builds the kernel.
## NOTE: This bit might fail if you don't have all the dependencies installed... a few sudo apt installs will solve this
cd ~/bbb_build
git clone https://github.com/RobertCNelson/bb-kernel
cd bb-kernel/
git checkout origin/am33x-v4.9 -b tmp
./build_kernel.sh

##
## Get Root File System (Debian)
cd ~/bbb_build
wget -c https://rcn-ee.com/rootfs/eewiki/minfs/debian-9.5-minimal-armhf-2018-07-30.tar.xz
tar xf debian-9.5-minimal-armhf-2018-07-30.tar.xz

##
## Setup microSD card (Mine was /dev/sdb)
export DISK=/dev/sdb                                              #<< Note this is NOT /dev/sdbX where X is the parition
sudo dd if=/dev/zero of=${DISK} bs=1M count=10                    #<< Erase partition table
sudo dd if=./u-boot/MLO of=${DISK} count=1 seek=1 bs=128k         #<< Install Bootloader
sudo dd if=./u-boot/u-boot.img of=${DISK} count=2 seek=1 bs=384k  #<< Install Bootloader

##
## Create partition (sfdisk >= 2.26.x)
sudo sfdisk ${DISK} <<-__EOF__
4M,,L,*
__EOF__)

##
## Format Partition (mkfs.ext4 <= 1.42)
sudo mkfs.ext4 -L rootfs ${DISK}1

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

## Install Kernel and Root File System
export kernel_version=4.X.Y-Z #<< Taken from Linux Kernel Build Script Output
sudo tar xfvp ./*-*-*-armhf-*/armhf-rootfs-*.tar -C /media/rootfs/
sync
sudo chown root:root /media/rootfs/
sudo chmod 755 /media/rootfs/
sudo sh -c "echo 'uname_r=${kernel_version}' >> /media/rootfs/boot/uEnv.txt"
sudo cp -v ./bb-kernel/deploy/${kernel_version}.zImage /media/rootfs/boot/vmlinuz-${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}-modules.tar.gz -C /media/rootfs/
sudo sh -c "echo '/dev/mmcblk0p1  /  auto  errors=remount-ro  0  1' >> /media/rootfs/etc/fstab"

Building New Device Trees and Linux Kernels

Don't use the ./build_kernel.sh script again... this will clobber any changes you've made and start from scratch.

To rebuild the kernel:

cd ~/bbb/bb-kernel/KERNEL
export CROSS_COMPILE=arm-linux-gnueabihf-
export ARCH=arm
make -j8 [V=1]#<< Build the kernel and device tree
make -j8 dtbs #<< OR just build the device tree

Once the kernel is built, copy to your sd card like this:

cp arch/arm/boot/zImage /media/your-sd-card/boot/vmlinuz-4.4.145-bone23
# Take kernel version from build script output in setup stage