Skip to content

Instantly share code, notes, and snippets.

@satmandu
Last active July 22, 2023 23:37
Show Gist options
  • Save satmandu/c462ab301cbe09bd6e7cf4db7f626727 to your computer and use it in GitHub Desktop.
Save satmandu/c462ab301cbe09bd6e7cf4db7f626727 to your computer and use it in GitHub Desktop.
Generate an arm64 rpi userland for use on ubuntu/arm64.
#!/bin/bash -x
workdir="${HOME}/workdir"
[[ ! -d "$workdir" ]] && ( mkdir -p "$workdir" || exit 1)
[[ ! -d "$workdir"/tmp ]] && ( mkdir -p "$workdir"/tmp || exit 1)
[[ ! -d "$workdir"/output ]] && ( mkdir -p "$workdir"/output || exit 1)
echo "workdir is ${workdir}"
tmpdir=$(mktemp -d deb_XXXX -p "$workdir"/tmp)
deb_temp=${tmpdir}/deb
extract_tmp=${tmpdir}/extract
echo "tmpdir is ${tmpdir}"
rm -rf "${workdir}"/rpiuserland.compile.log
sudo apt install -y git patchelf
is_git_repo="$(
cd "${workdir}"/rpi-userland || exit
git rev-parse --is-inside-work-tree 2>/dev/null
)"
if [ "$is_git_repo" ]; then
cd "$workdir"/rpi-userland && git clean -d -x -f
else
rm -rf "${workdir}"/rpi-userland
cd "$workdir" && git clone --depth 1 https://github.com/raspberrypi/userland rpi-userland
fi
USERLANDREV=$(git -C "${workdir}"/rpi-userland rev-parse --short HEAD) >/dev/null
mkdir -p "${workdir}"/scripts
(
cd "${workdir}"/scripts && curl -OL https://github.com/satmandu/docker-rpi4-imagebuilder/raw/master/scripts/patch_rpi-userland.sh
chmod +x patch_rpi-userland.sh
)
echo "* Compiling Raspberry Pi userland source."
cd "${workdir}"/rpi-userland/ || exit
[[ -e "${workdir}"/scripts/patch_rpi-userland.sh ]] && {
cd "${workdir}"/rpi-userland/ && "${workdir}"/scripts/patch_rpi-userland.sh
true
}
sed -i 's/__bitwise/FDT_BITWISE/' "${workdir}"/rpi-userland/opensrc/helpers/libfdt/libfdt_env.h
sed -i 's/__force/FDT_FORCE/' "${workdir}"/rpi-userland/opensrc/helpers/libfdt/libfdt_env.h
echo "compiling rpi userland"
PKGVERSION="$(date +%d%m%Y):${USERLANDREV}"
cat <<-EOF | tee -a "${workdir}"/rpi-userland/CMakeLists.txt
SET(CPACK_GENERATOR "DEB")
SET(CPACK_PACKAGE_NAME "rpiuserland")
SET(CPACK_SET_DESTDIR TRUE)
SET(CPACK_DEBIAN_PACKAGE_MAINTAINER "noone")
SET(CPACK_PACKAGE_VERSION_MAJOR "$PKGVERSION")
SET(CPACK_PACKAGE_VERSION_MINOR "0")
SET(CPACK_PACKAGE_VERSION_PATCH "1~alpha1")
SET(CPACK_DEBIAN_PACKAGE_DEPENDS "libgcc1-armhf-cross,libstdc++6-armhf-cross,libc6-armhf-cross")
include(CPack)
EOF
CROSS_COMPILE=aarch64-linux-gnu- ./buildme --aarch64 &>>"${workdir}"/rpi_userland_build.compile.log
echo "configuring package"
cd "${workdir}"/rpi-userland/build/raspberry/release/ || exit
make package
sudo rm -rf "${deb_temp}"
mkdir -p "${deb_temp}"
sudo rm -rf "${extract_tmp}"
mkdir -p "${extract_tmp}"
dpkg-deb -R "$workdir"/rpi-userland/build/raspberry/release/vmcs_host_apps-1.0-Source.deb "${extract_tmp}"/
cd "${workdir}"/rpi-userland/build/raspberry/release/ && sudo make DESTDIR="${deb_temp}"/ install
sed -i "s/1.0/$PKGVERSION/" "${extract_tmp}"/DEBIAN/control
mv "${extract_tmp}"/DEBIAN "${deb_temp}"/
rm -rf "${extract_tmp}"
mkdir -p "${deb_temp}"/etc/ld.so.conf.d/
echo '/opt/vc/lib' >"${deb_temp}"/etc/ld.so.conf.d/vc.conf
mkdir -p "${deb_temp}"/etc/environment.d
cat <<-EOF >"${deb_temp}"/etc/environment.d/10-vcgencmd.conf
# /etc/environment.d/10-vcgencmd.conf
# Do not edit this file
PATH="/opt/vc/bin:/opt/vc/sbin"
ROOTPATH="/opt/vc/bin:/opt/vc/sbin"
LDPATH="/opt/vc/lib"
EOF
chmod +x "${deb_temp}"/etc/environment.d/10-vcgencmd.conf
mkdir -p "${deb_temp}"/etc/profile.d/
cat <<-'EOF' >"${deb_temp}"/etc/profile.d/98-rpi.sh
# /etc/profile.d/98-rpi.sh
# Adds Raspberry Pi Foundation userland binaries to path
export PATH="$PATH:/opt/vc/bin:/opt/vc/sbin"
EOF
chmod +x "${deb_temp}"/etc/profile.d/98-rpi.sh
cat <<-EOF >"${deb_temp}"/etc/ld.so.conf.d/00-vmcs.conf
/opt/vc/lib
EOF
SUDOPATH=$(sudo sed -n 's/\(^.*secure_path="\)//p' /etc/sudoers | sed s'/.$//')
SUDOPATH="${SUDOPATH:-/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin:/snap/bin}"
SUDOPATH+=":/opt/vc/bin:/opt/vc/sbin"
# Add path to sudo
mkdir -p "${deb_temp}"/etc/sudoers.d
echo "* Adding rpi util path to sudo."
cat <<-EOF >>"${deb_temp}"/etc/sudoers.d/rpi
Defaults secure_path=$SUDOPATH
EOF
chmod 0440 "${deb_temp}"/etc/sudoers.d/rpi
sudo chown root:root "${deb_temp}"/etc/sudoers.d/rpi
# Get non-arm64 vcdbg and patch for use on armhf
mkdir -p "${deb_temp}"/usr/arm-linux-gnueabihf/lib
cd "${deb_temp}"/usr/arm-linux-gnueabihf/lib || exit
curl -OL https://github.com/Hexxeh/rpi-firmware/raw/master/vc/hardfp/opt/vc/lib/libdebug_sym.so
curl -OL https://github.com/Hexxeh/rpi-firmware/raw/master/vc/hardfp/opt/vc/lib/libelftoolchain.so
curl -OL https://github.com/Hexxeh/rpi-firmware/raw/master/vc/hardfp/opt/vc/lib/libvcos.so
cd "${deb_temp}"/opt/vc/bin/ || exit
sudo curl -OL https://github.com/Hexxeh/rpi-firmware/raw/master/vc/hardfp/opt/vc/bin/vcdbg
sudo chmod +x "${deb_temp}"/opt/vc/bin/vcdbg
sudo cp "${deb_temp}"/opt/vc/bin/vcdbg "${deb_temp}"/opt/vc/bin/vcdbg.orig
sudo patchelf --force-rpath --set-rpath "/usr/arm-linux-gnueabihf/lib" "${deb_temp}"/opt/vc/bin/vcdbg
sudo patchelf --set-interpreter /usr/arm-linux-gnueabihf/lib/ld-linux-armhf.so.3 "${deb_temp}"/opt/vc/bin/vcdbg
cd "${workdir}"/rpi-userland/build/raspberry/release/ || exit
sudo dpkg-deb --root-owner-group -b "${deb_temp}" "${workdir}"/output/ &>>"${workdir}"/rpi_userland_build.compile.log
sudo rm -rf "${workdir}"/rpi-userland/build/
sudo rm -rf "${tmpdir}"
echo "Package is at ${workdir}/output/rpiuserland_${USERLANDREV}_arm64.deb ."
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment