├── README.md ├── bb-enable-spi-devicetree.sh ├── bb-get-rcn-kernel-source.sh └── bb-show-serial.sh /README.md: -------------------------------------------------------------------------------- 1 | Beaglebone Ubuntu Scripts 2 | ========================= 3 | 4 | A collection of scripts that are probably useful if you are playing with Ubuntu on a Beaglebone (Black). I use these not only to automate tasks, but also as a knowledge repository. 5 | 6 | bb-enable-spi-devicetree 7 | ------------------------ 8 | 9 | This script modifies the flattened device-tree blob used by uboot on 3.8 and later kernels for BeagleBone (Black) to enable SPI1 and export the first chip-select line (cs0) as spidev device. The pinmux settings will also be set. After running this script, a reboot is required to load the modified device-tree. 10 | 11 | It should work on BeagleBone/Ubuntu, but might also work on other distros. 12 | 13 | bb-get-rcn-kernel-source 14 | ------------------------ 15 | 16 | This script downloads, patches and prepares the kernel sources for BeagleBone (Black) kernels distributed by http://rcn-ee.net in order to enable you to compile kernel modules right on the BeagleBone: After installing the kernel sources with this script, you should be able to build kernel modules via their respective makefile. 17 | 18 | Note that this will only work for those kernels for which rcn-ee has a kernel-headers package as well – This should be the case for most older and some newer kernels, but there was a time where the kernel-headers package was missing from the distributions. The script will notify you if you want to run it for a kernel for which no kernel-headers package is available. 19 | 20 | By default, the script will prepare the sources for the currently running kernel. However, you can also specify a different kernel version as the first script argument, e.g. `./bb-get-rcn-kernel-source.sh 3.8.13-bone19` 21 | 22 | The script tries to determine your linux distribution automatically, but if that fails or you want to manually specify one, edit the DIST variable at the top of the script. 23 | 24 | Oh, and also ensure that you have all the necessary dependencies installed, since the script doesn't check: gcc, make and all the usual suspects for building kernel-related things. 25 | 26 | I've only tested this on Ubuntu, but it should probably also work with Debian. 27 | 28 | bb-show-serial 29 | -------------- 30 | 31 | This script reads the serial number from the i2c-connected eeprom available on BeagleBone (Black). It should work both on device-tree and pre-device-tree kernels. 32 | 33 | The serial number is unique for each BeagleBone (Black) and also includes the week/year of manufacture in the first 4 digits. 34 | 35 | I only tested this on Ubuntu, but it should probably work on other distros as well. 36 | -------------------------------------------------------------------------------- /bb-enable-spi-devicetree.sh: -------------------------------------------------------------------------------- 1 | #!/bin/sh 2 | # 3 | # This script modifies the flattened device-tree blob used by uboot on 3.8 and 4 | # later kernels for BeagleBone (Black) to enable SPI1 and export the first 5 | # chip-select line (cs0) as spidev device. The pinmux settings will also be 6 | # set. After running this script, a reboot is required to load the modified 7 | # device-tree. 8 | # 9 | # It should work on BeagleBone/Ubuntu, but might also work on other distros. 10 | # 11 | 12 | FDT_SPI_DEV="spi@481a0000" 13 | 14 | UBOOTBASE="/boot" 15 | FDTBASE="${UBOOTBASE}/dtbs/$(uname -r)" 16 | FDT_SPI_PATH="/ocp/${FDT_SPI_DEV}" 17 | 18 | REBOOT_REQUIRED="" 19 | 20 | notif () { 21 | echo "\033[1;34m${1}\033[0m${2}" 22 | } 23 | 24 | fail () { 25 | echo "\033[1;31m${1}\033[0m${2}" 26 | exit 0 27 | } 28 | 29 | checks () { 30 | if ! [ $(id -u) = 0 ]; then 31 | fail "you need to be root to run this (or use sudo)." 32 | fi 33 | 34 | has_fdttools=$(which fdtget 2>/dev/null) 35 | if [ ! "${has_fdttools}" ]; then 36 | fail "you need to install the device tree tools (apt-get install device-tree-compiler)." 37 | fi 38 | } 39 | 40 | find_devicetree_file () { 41 | EEPROM="/sys/bus/i2c/devices/1-0050/eeprom" 42 | 43 | if [ ! -f "${EEPROM}" ]; then 44 | EEPROM="/sys/bus/i2c/devices/0-0050/eeprom" 45 | fi 46 | 47 | if [ ! -f "${EEPROM}" ]; then 48 | EEPROM="/sys/bus/i2c/devices/0-0050/at24-0/nvmem" 49 | fi 50 | 51 | if [ ! -f "${EEPROM}" ]; then 52 | fail "i2c eeprom file not found in sysfs." 53 | fi 54 | 55 | board_type="$(hexdump -e '8/1 "%c"' "$EEPROM" -s 4 -n 8 2>&1)" 56 | 57 | if [ -z ${board_type} ]; then 58 | fail "failed to extract board type from eeprom." 59 | fi 60 | 61 | case $board_type in 62 | "A335BONE") FDT="am335x-bone.dtb"; notif "detected BeagleBone";; 63 | "A335BNLT") FDT="am335x-boneblack.dtb"; notif "detected BeagleBone Black";; 64 | *) fail "failed to detect board type: unknown board \"$board_type\"";; 65 | esac 66 | 67 | if [ ! -f "${FDTBASE}/${FDT}" ]; then 68 | fail "flattened device-tree file cannot be found at " "${FDTBASE}/${FDT}" 69 | else 70 | notif "found flattened device-tree at " "${FDTBASE}/${FDT}" 71 | fi 72 | } 73 | 74 | enable_spi () { 75 | SPISTATUS=$(fdtget "${FDTBASE}/${FDT}" "${FDT_SPI_PATH}" status 2>&1) 76 | 77 | if [ "${SPISTATUS}" != "${SPISTATUS#*FDT_ERR_NOTFOUND}" ]; then 78 | fail "path to spi device not found in flattened device-tree: " "${FDT_SPI_PATH}" 79 | fi 80 | 81 | if [ "${SPISTATUS}" = "okay" ]; then 82 | notif "spi device at ${FDT_SPI_PATH} is already enabled, nothing to do." 83 | else 84 | fdtput -t s "${FDTBASE}/${FDT}" "${FDT_SPI_PATH}" "status" "okay" 2>&1 85 | 86 | SPISTATUS=$(fdtget "${FDTBASE}/${FDT}" "${FDT_SPI_PATH}" status 2>&1) 87 | 88 | if [ "${SPISTATUS}" != "okay" ]; then 89 | fail "failed to enable spi device at ${FDT_SPI_PATH}. status is now: " ${SPISTATUS} 90 | else 91 | notif "successfully enabled " ${FDT_SPI_PATH} 92 | REBOOT_REQUIRED="yes" 93 | fi 94 | fi 95 | } 96 | 97 | find_free_phandle () { 98 | TREE="$1" 99 | PHANDLE= 100 | 101 | for i in $(seq 1 65535); do 102 | p=$(printf "0x%x" $i) 103 | has_phandle=$(grep "phandle = <$p>;" "${TREE}") 104 | if [ -z "${has_phandle}" ]; then 105 | echo "$p" 106 | break 107 | fi 108 | done; 109 | } 110 | 111 | enable_spi_dev () { 112 | SPIDEVSTATUS=$(fdtget "${FDTBASE}/${FDT}" "${FDT_SPI_PATH}/spidev@0" reg 2>&1) 113 | 114 | if [ "${SPIDEVSTATUS}" = "${SPIDEVSTATUS#*FDT_ERR_NOTFOUND}" ]; then 115 | notif "spidev at ${FDT_SPI_PATH}/spidev@0 already present, nothing to do." 116 | else 117 | FDTTEMP=$(mktemp -u) 118 | DTSTEMP=$(mktemp -u) 119 | DTSTEMP2=$(mktemp -u) 120 | 121 | dtc -I dtb -O dts -o "${FDTTEMP}" "${FDTBASE}/${FDT}" 122 | 123 | PHANDLE=$(find_free_phandle "${FDTTEMP}") 124 | 125 | if [ -z "${PHANDLE}" ]; then 126 | rm -f rm -f "${FDTTEMP}" 127 | fail "failed to find a phandle for the pinmux settings." 128 | fi 129 | 130 | cat "${FDTTEMP}" | perl -ne "if (\$f>0 && /\};\$/) { print \"\n};\npinmux_spi1_pins \{\npinctrl-single,pins = <0x190 0x13 0x194 0x33 0x198 0x13 0x19c 0x13 0x164 0x12>;\nlinux,phandle = <${PHANDLE}>;\nphandle = <${PHANDLE}>;\n};\n\n\"; \$f=0; \$ff = 1;} else { print; } \$f=1 if (\$ff!=1 && /pinmux_userled_pins {/)" > "${DTSTEMP}" 131 | cat "${DTSTEMP}" | perl -ne "if (\$f>0 && /\};\$/) { print \"\npinctrl-0 = <${PHANDLE}>;\nspidev: spidev\@0 \{\ncompatible = \\\"linux,spidev\\\";\nreg = <0>;\nspi-max-frequency = <24000000>;\n};\n};\n\n\"; \$f=0; \$ff=1;} else { print; } \$f=1 if (\$ff!=1 && /$(echo "${FDT_SPI_DEV}" | sed -nE "s/@/\\\@/p") {/)" > "${DTSTEMP2}" 132 | 133 | dtc -I dts -O dtb -o "${FDTBASE}/${FDT}" ${DTSTEMP2} 134 | 135 | #rm -f "${FDTTEMP}" 136 | #rm -f "${DTSTEMP}" 137 | #rm -f "${DTSTEMP2}" 138 | 139 | SPIDEVSTATUS=$(fdtget "${FDTBASE}/${FDT}" "${FDT_SPI_PATH}/spidev@0" reg 2>&1) 140 | 141 | if [ "0" = "${SPIDEVSTATUS}" ]; then 142 | notif "successfully enabled " "${FDT_SPI_PATH}/spidev@0" 143 | REBOOT_REQUIRED="yes" 144 | else 145 | fail "failed to enable " "${FDT_SPI_PATH}/spidev@0" 146 | fi 147 | fi 148 | } 149 | 150 | checks 151 | find_devicetree_file 152 | enable_spi 153 | enable_spi_dev 154 | 155 | if [ "yes" = "${REBOOT_REQUIRED}" ]; then 156 | notif "please reboot." 157 | fi 158 | 159 | -------------------------------------------------------------------------------- /bb-get-rcn-kernel-source.sh: -------------------------------------------------------------------------------- 1 | #!/bin/sh 2 | # 3 | # This script downloads, patches and prepares the kernel sources for BeagleBone 4 | # (Black) kernels distributed by http://rcn-ee.net in order to enable you to 5 | # compile kernel modules right on the BeagleBone: After installing the kernel 6 | # sources with this script, you should be able to build kernel modules via 7 | # their respective makefile. 8 | # 9 | # Note that this will only work for those kernels for which rcn-ee has a 10 | # kernel-headers package as well – This should be the case for most older 11 | # and some newer kernels, but there was a time where the kernel-headers package 12 | # was missing from the distributions. The script will notify you if you want 13 | # to run it for a kernel for which no kernel-headers package is available. 14 | # 15 | # By default, the script will prepare the sources for the currently running 16 | # kernel. However, you can also specify a different kernel version as the 17 | # first script argument. 18 | # 19 | # Oh, and also ensure that you have all the necessary dependencies installed, 20 | # since the script doesn't check: gcc, make and all the usual suspects for 21 | # building kernel-related things. 22 | # 23 | # The script tries to determine your linux distribution automatically, but 24 | # if that fails or you want to manually specify one, edit the DIST variable 25 | # at the top of the script. 26 | # 27 | # I've only tested this on Ubuntu, but it should probably also work with 28 | # Debian. 29 | # 30 | 31 | DIST="" 32 | 33 | BASE_URL="http://rcn-ee.net/deb" 34 | OFFICIAL_KERNEL_BASE_URL="https://www.kernel.org/pub/linux/kernel" 35 | 36 | KVER=$(uname -r) 37 | MAIN_KVER=$(echo ${KVER} | sed -nE 's/^(([0-9]+\.?)+).*/\1/gp' | sed 's/.0//g') 38 | 39 | DDIR=$(mktemp -d) 40 | 41 | clean_up () { 42 | rm -rf "${DDIR}" 43 | } 44 | 45 | notif () { 46 | echo "\033[1;34m${1}\033[0m${2}" 47 | } 48 | 49 | fail () { 50 | echo "\033[1;31m${1}\033[0m${2}" 51 | clean_up 52 | exit 0 53 | } 54 | 55 | checks () { 56 | if [ -d "/usr/src/linux-${KVER}" ]; then 57 | notif "directory /usr/src/linux-${KVER} already exists. nothing to do!" 58 | exit 0 59 | fi 60 | 61 | if ! [ $(id -u) = 0 ]; then 62 | fail "You need to be root to run this (or use sudo)." 63 | fi 64 | } 65 | 66 | determine_dist () { 67 | #lsb_release: is not installed by default when using debian's debootstrap script. 68 | if [ -z "${DIST}" ]; then 69 | has_lsb_release=$(which lsb_release 2>/dev/null) 70 | if [ "${has_lsb_release}" ] ; then 71 | release=$(lsb_release -cs) 72 | dpkg_arch=$(dpkg --print-architecture) 73 | DIST="${release}-${dpkg_arch}" 74 | fi 75 | fi 76 | 77 | if [ -z "${DIST}" ]; then 78 | fail "failed to determine linux distro: either install lsb-release (apt-get install lsb-release) or specify distribution manually at the top of this script, as \$DIST." 79 | fi 80 | 81 | RURL="${BASE_URL}/${DIST}/v${KVER}" 82 | 83 | notif "using linux distribution: " "${DIST}" 84 | } 85 | 86 | get_rcn_kernel_header_package () { 87 | notif "getting file listing from ${RURL}/..." 88 | wget --directory-prefix="${DDIR}" "${RURL}/" 89 | 90 | KPATCH=$(cat ${DDIR}/index.html | grep "patch" | sed -nE 's/ */dev/null) 147 | if [ "${has_gzcat}" ] ; then 148 | ZCAT="${has_gzcat}" 149 | fi 150 | 151 | notif "applying patch ${KPATCH} to linux-${MAIN_KVER}..." 152 | sleep 1 153 | 154 | $ZCAT "${DDIR}/${KPATCH}" | patch -d "${DDIR}/linux-${MAIN_KVER}" -p1 155 | } 156 | 157 | install_kernel_source () { 158 | notif "moving kernel source to /usr/src/linux-${KVER}..." 159 | 160 | mkdir -p /usr/src/ 161 | rm -rf "/usr/src/linux-${KVER}" 162 | 163 | mv "${DDIR}/linux-${MAIN_KVER}" "/usr/src/linux-${KVER}" 164 | mv "${SYMVERS}" "/usr/src/linux-${KVER}/Module.symvers" 165 | mv "${KCONFIG}" "/usr/src/linux-${KVER}/.config" 166 | 167 | mkdir -p "/lib/modules/${KVER}" 168 | rm -f "/lib/modules/${KVER}/build" 169 | ln -s "/usr/src/linux-${KVER}" "/lib/modules/${KVER}/build" 170 | } 171 | 172 | prepare_kernel_source () { 173 | notif "preparing kernel source at /usr/src/linux-${KVER}..." 174 | 175 | CURPWD=$PWD 176 | 177 | cd "/usr/src/linux-${KVER}" 178 | 179 | make oldconfig 180 | make prepare 181 | make scripts 182 | 183 | cd "${CURPWD}" 184 | } 185 | 186 | if [ ! -z ${1} ]; then 187 | KVER="${1}" 188 | fi 189 | 190 | notif "installing kernel sources for ${KVER}..." 191 | notif 192 | 193 | checks 194 | determine_dist 195 | get_rcn_kernel_header_package 196 | unpack_rcn_kernel_header_package 197 | get_official_kernel_source 198 | patch_kernel_source 199 | install_kernel_source 200 | prepare_kernel_source 201 | clean_up 202 | 203 | notif "done: kernel sources for ${KVER} are now installed." 204 | notif "you should be able to compile kernel modules." 205 | 206 | exit 0 207 | -------------------------------------------------------------------------------- /bb-show-serial.sh: -------------------------------------------------------------------------------- 1 | #!/bin/sh 2 | # 3 | # This script reads the serial number from the i2c-connected eeprom available 4 | # on BeagleBone (Black). It should work both on device-tree and pre-device-tree 5 | # kernels. 6 | # 7 | # The serial number is unique for each BeagleBone (Black) and also includes 8 | # the week/year of manufacture in the first 4 digits. 9 | # 10 | # I only tested this on Ubuntu, but it should probably work on other distros 11 | # as well. 12 | # 13 | 14 | notif () { 15 | echo "\033[1;34m${1}\033[0m${2}" 16 | } 17 | 18 | fail () { 19 | echo "\033[1;31m${1}\033[0m${2}" 20 | exit 0 21 | } 22 | 23 | checks () { 24 | if ! [ $(id -u) = 0 ]; then 25 | fail "you need to be root to run this (or use sudo)." 26 | fi 27 | 28 | has_hexdump=$(which hexdump 2>/dev/null) 29 | if [ ! "${has_hexdump}" ]; then 30 | fail "you need to install the BSD utils (apt-get install bsdmainutils)." 31 | fi 32 | } 33 | 34 | print_serial () { 35 | EEPROM="/sys/bus/i2c/devices/1-0050/eeprom" 36 | 37 | if [ ! -f "${EEPROM}" ]; then 38 | EEPROM="/sys/bus/i2c/devices/0-0050/eeprom" 39 | fi 40 | 41 | if [ ! -f "${EEPROM}" ]; then 42 | EEPROM="/sys/bus/i2c/devices/0-0050/at24-0/nvmem" 43 | fi 44 | 45 | if [ ! -f "${EEPROM}" ]; then 46 | fail "i2c eeprom file not found in sysfs." 47 | fi 48 | 49 | SERIAL=$(hexdump -e '8/1 "%c"' "${EEPROM}" -s 16 -n 12 2>&1) 50 | 51 | if [ "${SERIAL}" = "${SERIAL#*BB}" ]; then 52 | fail "failed to extract serial number from i2c eeprom: " "${SERIAL}" 53 | fi 54 | 55 | notif "beaglebone serial number: " "${SERIAL}" 56 | } 57 | 58 | print_serial 59 | --------------------------------------------------------------------------------