Skip to content

Instantly share code, notes, and snippets.

@8
Created September 19, 2014 11:40
Show Gist options
  • Save 8/b01f196d24336a956c5f to your computer and use it in GitHub Desktop.
Save 8/b01f196d24336a956c5f to your computer and use it in GitHub Desktop.
makes /dev/ttyAMA0 usable on a new raspbian image by freeing it from logging kernel output
#!/bin/bash
# free-ttyAMA0 script written by mdk
# http://www.amescon.com
# this script configures the raspbian image to not use the ttyAMA0 port for debugging purposes and makes it useable in userland
function configure_ttyAMA0_fix_cmdlinetxt() {
local file="/boot/cmdline.txt"
local file_backup="${file}.bak"
# we need to remove all references of ttyAMA0 from /boot/cmdline.txt
echo -n "checking ${file}..."
# count the number of lines that contain a reference to /dev/ttyAMA0
local count=`grep ${file} -e ttyAMA0 -c`
if [[ "${count}" == "0" ]]; then
echo "OK"
return 0
else
echo "update required"
# create a backup
echo "backing up orig. file to ${file_backup}"
cp ${file} ${file_backup}
# stip all sequences that contain ttyAMA0
echo "updating file ${file}"
sed -i 's/[^\ ]*ttyAMA0[^\ ]*//g' /boot/cmdline.txt
return 1
fi
}
function configure_ttyAMA0_fix_etc_inittab() {
local file="/etc/inittab"
local file_backup="${file}.bak"
# we need to comment all lines that contain a reference to ttyAMA0
echo -n "checking ${file}..."
local count=`grep ${file} -e ttyAMA0 -c`
if [[ "${count}" == "0" ]]; then
echo "OK"
return 0
else
echo "update required"
# create a backup
echo "backing up orig. file to ${file_backup}"
cp ${file} ${file_backup}
# strip all lines that contain ttyAMA0
echo "updating file ${file}"
sed -i '/ttyAMA0/d' /etc/inittab
return 1
fi
}
# configures the raspberry pi to not use the /dev/ttyAMA0 for startup logging
function configure_ttyAMA0() {
local reboot_required=0
# we need to remove all references of ttyAMA0 from /boot/cmdline.txt and /etc/inittab so that the ttyAMA0 device becomes usable
configure_ttyAMA0_fix_cmdlinetxt;
if [[ ${?} -eq 1 ]]; then
reboot_required=1
fi
configure_ttyAMA0_fix_etc_inittab;
if [[ $? -eq 1 ]]; then
reboot_required=1
fi
# inform the use that a reboot is required
if [ $reboot_required == 1 ]; then
echo "startup files patched -> reboot required to use /dev/ttyAMA0 port"
fi
}
function main() {
# check if we're running as root
if [[ $EUID -ne 0 ]]; then
echo "This script must be run as root. Try 'sudo ./free-ttyAMA0.sh'."
else
configure_ttyAMA0;
fi
}
main $*; # entrypoint
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment