From c6c99c428cd2e02956de213b504b2e5ab9f98c61 2018-11-25 01:21:43 From: Unknown Date: 2018-11-25 01:21:43 Subject: [PATCH] Better Bluetooth and serial - ENABLE_BLUETOOTH to enable bluetooth - ENABLE_MINIUART_OVERLAY to enable bluetooth with full serial console - ENABLE_PRINTK to print kernel messages - ENABLE_MINIUART_OVERLAY to get slow bt and fast serial console on RPI0,3,3P - ENABLE_TURBO to set cpu to max speed. helpfull if used with - - ENABLE_MINIUART_OVERLAY as RPI would be otherwise set to lowest clock possible - install serial alias by default. some projcets need them as they are a default package on raspian - --- diff --git a/README.md b/README.md index d070161..21359ac 100644 --- a/README.md +++ b/README.md @@ -153,7 +153,19 @@ Set the IP address for the second NTP server. #### Basic system features: ##### `ENABLE_CONSOLE`=true -Enable serial console interface. Recommended if no monitor or keyboard is connected to the RPi2/3. In case of problems fe. if the network (auto) configuration failed - the serial console can be used to access the system. +Enable serial console interface. Recommended if no monitor or keyboard is connected to the RPi2/3. In case of problems fe. if the network (auto) configuration failed - the serial console can be used to access the system. On RPI `0` `3` `3P` the CPU speed is locked at lowest speed. + +##### `ENABLE_PRINTK`=false +Enables printing kernel messages to konsole. printk is `3 4 1 3` as in raspbian. + +##### `ENABLE_BLUETOOTH`=false +Enable onboard Bluetooth interface on the RPi0/3/3P. See: https://spellfoundry.com/2016/05/29/configuring-gpio-serial-port-raspbian-jessie-including-pi-3/ + +##### `ENABLE_MINIUART_OVERLAY`=false +Enable Bluetooth to use this. Adds overlay to swap UART0 with UART1. Enabling (slower) Bluetooth and full speed serial console. - RPI `0` `3` `3P` have a fast `hardware UART0` (ttyAMA0) and a `mini UART1` (ttyS0)! RPI `1` `1P` `2` only have a `hardware UART0`. `UART0` is considered better, because is faster and more stable than `mini UART1`. By default the Bluetooth modem is mapped to the `hardware UART0` and `mini UART` is used for console. The `mini UART` is a problem for the serial console, because its baudrate depends on the cpu frequency, which is changing on runtime. Resulting in a volatile baudrate and thus in an unusable serial console. + +##### `ENABLE_TURBO`=false +Enable Turbo mode. This setting locks cpu at highest frequency. As setting ENABLE_CONSOLE=true locks RPI to lowest CPU speed, this is can be used additionally to lock cpu hat max speed. Need a good power supply and probably cooling for the Raspberry PI. ##### `ENABLE_I2C`=false Enable I2C interface on the RPi2/3. Please check the [RPi2/3 pinout diagrams](https://elinux.org/RPi_Low-level_peripherals) to connect the right GPIO pins. @@ -173,9 +185,6 @@ Allow the installation of non-free Debian packages that do not comply with the D ##### `ENABLE_WIRELESS`=false Download and install the [closed-source firmware binary blob](https://github.com/RPi-Distro/firmware-nonfree/raw/master/brcm) that is required to run the internal wireless interface of the Raspberry Pi model `3`. This parameter is ignored if the specified `RPI_MODEL` is not `3`. -##### `ENABLE_BLUETOOTH`=false -Enable Bluetooth interface on the RPi0/3/3P. - ##### `ENABLE_RSYSLOG`=true If set to false, disable and uninstall rsyslog (so logs will be available only in journal files) diff --git a/bootstrap.d/13-kernel.sh b/bootstrap.d/13-kernel.sh index e4fdb3b..d4f9a2d 100644 --- a/bootstrap.d/13-kernel.sh +++ b/bootstrap.d/13-kernel.sh @@ -89,13 +89,8 @@ if [ "$BUILD_KERNEL" = true ] ; then #Switch to KERNELSRC_DIR cd "${KERNEL_DIR}" - # GPL v2.0 - #https://github.com/sakaki-/bcmrpi3-kernel-bis/blob/master/conform_config.sh + # enable ZSWAP see https://askubuntu.com/a/472227 or https://wiki.archlinux.org/index.php/zswap if [ "$KERNEL_ZSWAP" = true ] && { [ "$RPI_MODEL" = 3 ] || [ "$RPI_MODEL" = 3P ] ; } ; then - # enable ZSWAP support for better performance during large builds etc. - # requires activation via kernel parameter or sysfs - # see e.g. https://askubuntu.com/a/472227 for a summary of ZSWAP (vs ZRAM etc.) - # and e.g. https://wiki.archlinux.org/index.php/zswap for parameters etc. set_kernel_config ZPOOL y set_kernel_config ZSWAP y set_kernel_config ZBUD y @@ -103,17 +98,16 @@ if [ "$BUILD_KERNEL" = true ] ; then set_kernel_config ZSMALLOC y set_kernel_config PGTABLE_MAPPING y fi - + + # enable basic KVM support; see https://www.raspberrypi.org/forums/viewtopic.php?f=63&t=210546&start=25#p1300453 if [ "$KERNEL_VIRT" = true ] && { [ "$RPI_MODEL" = 3 ] || [ "$RPI_MODEL" = 3P ] ; } ; then - # enable basic KVM support; see e.g. - # https://www.raspberrypi.org/forums/viewtopic.php?f=63&t=210546&start=25#p1300453 set_kernel_config VIRTUALIZATION y set_kernel_config KVM y set_kernel_config VHOST_NET m set_kernel_config VHOST_CROSS_ENDIAN_LEGACY y fi - #See https://github.com/raspberrypi/linux/issues/2177#issuecomment-354647406 - # Netfilter kernel support + + # Netfilter kernel support See https://github.com/raspberrypi/linux/issues/2177#issuecomment-354647406 if [ "$KERNEL_NF" = true ] && { [ "$RPI_MODEL" = 3 ] || [ "$RPI_MODEL" = 3P ] ; } ; then set_kernel_config CONFIG_NETFILTER_XTABLES m set_kernel_config CONFIG_NF_DUP_NETDEV m @@ -205,9 +199,7 @@ if [ "$BUILD_KERNEL" = true ] ; then set_kernel_config NETFILTER_XTABLES m fi - #https://groups.google.com/forum/#!topic/linux.gentoo.user/_2aSc_ztGpA - #https://github.com/torvalds/linux/blob/master/init/Kconfig#L848 - # Enables BPF syscall for systemd-journald + # Enables BPF syscall for systemd-journald see https://github.com/torvalds/linux/blob/master/init/Kconfig#L848 or https://groups.google.com/forum/#!topic/linux.gentoo.user/_2aSc_ztGpA if [ "$KERNEL_BPF" = true ] && { [ "$RPI_MODEL" = 3 ] || [ "$RPI_MODEL" = 3P ] ; } ; then set_kernel_config CONFIG_BPF_SYSCALL y set_kernel_config CONFIG_CGROUP_BPF y diff --git a/bootstrap.d/15-rpi-config.sh b/bootstrap.d/15-rpi-config.sh index 675f5f9..f718b9c 100644 --- a/bootstrap.d/15-rpi-config.sh +++ b/bootstrap.d/15-rpi-config.sh @@ -51,8 +51,6 @@ else fi fi - - # Add encrypted root partition to cmdline.txt if [ "$ENABLE_CRYPTFS" = true ] ; then if [ "$ENABLE_SPLITFS" = true ] ; then @@ -66,10 +64,97 @@ if [ "$ENABLE_CRYPTFS" = true ] ; then fi fi -# Add serial console support -#if [ "$ENABLE_CONSOLE" = true ] ; then -# CMDLINE="${CMDLINE} console=ttyAMA0,115200 kgdboc=ttyAMA0,115200" -#fi +#locks cpu at max frequency +if [ "$ENABLE_TURBO" = true ] ; then + echo "force_turbo=1" >> "${BOOT_DIR}/config.txt" +fi + +if [ "$ENABLE_PRINTK" = true ] ; then + install_readonly files/sysctl.d/83-rpi-printk.conf "${ETC_DIR}/sysctl.d/83-rpi-printk.conf" +fi + +# Install udev rule for serial alias +install_readonly files/etc/99-com.rules "${ETC_DIR}/udev/rules.d/99-com.rules" + +if [ "$RPI_MODEL" = 0 ] || [ "$RPI_MODEL" = 3 ] || [ "$RPI_MODEL" = 3P ] ; then + + # RPI0,3,3P Use default ttyS0 (mini-UART)as serial interface + SET_SERIAL="ttyS0" + + # Bluetooth enabled + if [ "$ENABLE_BLUETOOTH" = true ] ; then + # Create temporary directory for Bluetooth sources + temp_dir=$(as_nobody mktemp -d) + + # Fetch Bluetooth sources + as_nobody git -C "${temp_dir}" clone "${BLUETOOTH_URL}" + + # Copy downloaded sources + mv "${temp_dir}/pi-bluetooth" "${R}/tmp/" + + # Bluetooth firmware from arch aur https://aur.archlinux.org/packages/pi-bluetooth/ + as_nobody wget -q -O "${R}/tmp/pi-bluetooth/LICENCE.broadcom_bcm43xx" https://aur.archlinux.org/cgit/aur.git/plain/LICENCE.broadcom_bcm43xx?h=pi-bluetooth + as_nobody wget -q -O "${R}/tmp/pi-bluetooth/BCM43430A1.hcd" https://aur.archlinux.org/cgit/aur.git/plain/BCM43430A1.hcd?h=pi-bluetooth + + # Set permissions + chown -R root:root "${R}/tmp/pi-bluetooth" + + # Install tools + install_readonly "${R}/tmp/pi-bluetooth/usr/bin/btuart" "${R}/usr/bin/btuart" + install_readonly "${R}/tmp/pi-bluetooth/usr/bin/bthelper" "${R}/usr/bin/bthelper" + + # Install bluetooth udev rule + install_readonly "${R}/tmp/pi-bluetooth/lib/udev/rules.d/90-pi-bluetooth.rules" "${LIB_DIR}/udev/rules.d/90-pi-bluetooth.rules" + + # Install Firmware Flash file and apropiate licence + mkdir "${ETC_DIR}/firmware/" + install_readonly "${R}/tmp/pi-bluetooth/LICENCE.broadcom_bcm43xx" "${ETC_DIR}/firmware/LICENCE.broadcom_bcm43xx" + install_readonly "${R}/tmp/pi-bluetooth/BCM43430A1.hcd" "${ETC_DIR}/firmware/LICENCE.broadcom_bcm43xx" + install_readonly "${R}/tmp/pi-bluetooth/debian/pi-bluetooth.bthelper@.service" "${ETC_DIR}/systemd/system/pi-bluetooth.bthelper@.service" + install_readonly "${R}/tmp/pi-bluetooth/debian/pi-bluetooth.hciuart.service" "${ETC_DIR}/systemd/system/pi-bluetooth.hciuart.service" + + # Remove temporary directory + rm -fr "${temp_dir}" + + # Switch Pi3 Bluetooth function to use the mini-UART (ttyS0) and restore UART0/ttyAMA0 over GPIOs 14 & 15. Slow Bluetooth and slow cpu. Use /dev/ttyS0 instead of /dev/ttyAMA0 + if [ "$ENABLE_MINIUART_OVERLAY" = true ] ; then + + # set overlay to swap ttyAMA0 and ttyS0 + echo "dtoverlay=pi3-miniuart-bt" >> "${BOOT_DIR}/config.txt" + + # if force_turbo didn't lock cpu at high speed, lock it at low speed (XOR logic) or miniuart will be broken + if [ "$ENABLE_TURBO" = false ] ; then + echo "core_freq=250" >> "${BOOT_DIR}/config.txt" + fi + fi + + else # if ENABLE_BLUETOOTH = false + # set overlay to disable bluetooth + echo "dtoverlay=pi3-disable-bt" >> "${BOOT_DIR}/config.txt" + fi # ENABLE_BLUETOOTH end + +else + # RPI1,1P,2 Use default ttyAMA0 (full UART) as serial interface + SET_SERIAL="ttyAMA0" +fi + +# may need sudo systemctl disable hciuart +if [ "$ENABLE_CONSOLE" = true ] ; then + echo "enable_uart=1" >> "${BOOT_DIR}/config.txt" + + # add string to cmdline + CMDLINE="${CMDLINE} console=serial0,115200" + + # Enable serial console systemd style + chroot_exec systemctl start serial-getty@"$SET_SERIAL".service + chroot_exec systemctl enable serial-getty@"$SET_SERIAL".service +else + echo "enable_uart=0" >> "${BOOT_DIR}/config.txt" + + # Enable serial console systemd style + chroot_exec systemctl stop serial-getty@"$SET_SERIAL".service + chroot_exec systemctl disable serial-getty@"$SET_SERIAL".service +fi # Remove IPv6 networking support if [ "$ENABLE_IPV6" = false ] ; then @@ -99,53 +184,6 @@ if [ "$ENABLE_INITRAMFS" = true ] ; then echo "initramfs initramfs-${KERNEL_VERSION} followkernel" >> "${BOOT_DIR}/config.txt" fi -# Disable RPi3 Bluetooth and restore ttyAMA0 serial device -if [ "$RPI_MODEL" = 3 ] || [ "$RPI_MODEL" = 3P ] ; then - if [ "$ENABLE_CONSOLE" = true ] && [ "$ENABLE_UBOOT" = false ] ; then - echo "dtoverlay=pi3-disable-bt" >> "${BOOT_DIR}/config.txt" - echo "enable_uart=1" >> "${BOOT_DIR}/config.txt" - fi -fi - -if [ "$ENABLE_BLUETOOTH" = true ] ; then - # Create temporary directory for Bluetooth sources - temp_dir=$(as_nobody mktemp -d) - - # Fetch Bluetooth sources - as_nobody git -C "${temp_dir}" clone "${BLUETOOTH_URL}" - - # Copy downloaded sources - mv "${temp_dir}/pi-bluetooth" "${R}/tmp/" - - # Raspberry-sys-mod package for /dev/serial device needed by bluetooth service - as_nobody wget -q -O "${R}/tmp/pi-bluetooth/99-com.rules" https://raw.githubusercontent.com/RPi-Distro/raspberrypi-sys-mods/master/etc.armhf/udev/rules.d/99-com.rules - # Bluetooth firmware from arch aur https://aur.archlinux.org/packages/pi-bluetooth/ - as_nobody wget -q -O "${R}/tmp/pi-bluetooth/LICENCE.broadcom_bcm43xx" https://aur.archlinux.org/cgit/aur.git/plain/LICENCE.broadcom_bcm43xx?h=pi-bluetooth - as_nobody wget -q -O "${R}/tmp/pi-bluetooth/BCM43430A1.hcd" https://aur.archlinux.org/cgit/aur.git/plain/BCM43430A1.hcd?h=pi-bluetooth - - # Set permissions - chown -R root:root "${R}/tmp/pi-bluetooth" - - # Install tools - install_readonly "${R}/tmp/pi-bluetooth/usr/bin/btuart" "${R}/usr/bin/btuart" - install_readonly "${R}/tmp/pi-bluetooth/usr/bin/bthelper" "${R}/usr/bin/bthelper" - - # Install bluetooth udev rule - install_readonly "${R}/tmp/pi-bluetooth/lib/udev/rules.d/90-pi-bluetooth.rules" "${LIB_DIR}/udev/rules.d/90-pi-bluetooth.rules" - - # Install Firmware Flash file and apropiate licence - mkdir "${ETC_DIR}/firmware/" - install_readonly "${R}/tmp/pi-bluetooth/LICENCE.broadcom_bcm43xx" "${ETC_DIR}/firmware/LICENCE.broadcom_bcm43xx" - install_readonly "${R}/tmp/pi-bluetooth/BCM43430A1.hcd" "${ETC_DIR}/firmware/LICENCE.broadcom_bcm43xx" - install_readonly "${R}/tmp/pi-bluetooth/debian/pi-bluetooth.bthelper@.service" "${ETC_DIR}/systemd/system/pi-bluetooth.bthelper@.service" - install_readonly "${R}/tmp/pi-bluetooth/debian/pi-bluetooth.hciuart.service" "${ETC_DIR}/systemd/system/pi-bluetooth.hciuart.service" - # Install udev rule for bluetooth device - install_readonly "${R}/tmp/pi-bluetooth/99-com.rules" "${ETC_DIR}/udev/rules.d/99-com.rules" - - # Remove temporary directory - rm -fr "${temp_dir}" -fi - # Create firmware configuration and cmdline symlinks ln -sf firmware/config.txt "${R}/boot/config.txt" ln -sf firmware/cmdline.txt "${R}/boot/cmdline.txt" diff --git a/bootstrap.d/30-security.sh b/bootstrap.d/30-security.sh index 8ad8275..75e3625 100644 --- a/bootstrap.d/30-security.sh +++ b/bootstrap.d/30-security.sh @@ -22,8 +22,3 @@ else # Set no root password to disable root login chroot_exec usermod -p \'!\' root fi - -# Enable serial console systemd style -if [ "$ENABLE_CONSOLE" = true ] ; then - chroot_exec systemctl enable serial-getty\@ttyAMA0.service -fi diff --git a/files/sysctl.d/83-rpi-printk.conf b/files/sysctl.d/83-rpi-printk.conf new file mode 100644 index 0000000..f8948fb --- /dev/null +++ b/files/sysctl.d/83-rpi-printk.conf @@ -0,0 +1 @@ +kernel.printk = 3 4 1 3 \ No newline at end of file diff --git a/files/udev/99-com.rules b/files/udev/99-com.rules new file mode 100644 index 0000000..fd6621d --- /dev/null +++ b/files/udev/99-com.rules @@ -0,0 +1,33 @@ +SUBSYSTEM=="input", GROUP="input", MODE="0660" +SUBSYSTEM=="i2c-dev", GROUP="i2c", MODE="0660" +SUBSYSTEM=="spidev", GROUP="spi", MODE="0660" +SUBSYSTEM=="bcm2835-gpiomem", GROUP="gpio", MODE="0660" + +SUBSYSTEM=="gpio", GROUP="gpio", MODE="0660" +SUBSYSTEM=="gpio*", PROGRAM="/bin/sh -c '\ + chown -R root:gpio /sys/class/gpio && chmod -R 770 /sys/class/gpio;\ + chown -R root:gpio /sys/devices/virtual/gpio && chmod -R 770 /sys/devices/virtual/gpio;\ + chown -R root:gpio /sys$devpath && chmod -R 770 /sys$devpath\ +'" + +KERNEL=="ttyAMA[01]", PROGRAM="/bin/sh -c '\ + ALIASES=/proc/device-tree/aliases; \ + if cmp -s $ALIASES/uart0 $ALIASES/serial0; then \ + echo 0;\ + elif cmp -s $ALIASES/uart0 $ALIASES/serial1; then \ + echo 1; \ + else \ + exit 1; \ + fi\ +'", SYMLINK+="serial%c" + +KERNEL=="ttyS0", PROGRAM="/bin/sh -c '\ + ALIASES=/proc/device-tree/aliases; \ + if cmp -s $ALIASES/uart1 $ALIASES/serial0; then \ + echo 0; \ + elif cmp -s $ALIASES/uart1 $ALIASES/serial1; then \ + echo 1; \ + else \ + exit 1; \ + fi \ +'", SYMLINK+="serial%c" diff --git a/rpi23-gen-image.sh b/rpi23-gen-image.sh index 9f7afb1..65cdbd6 100755 --- a/rpi23-gen-image.sh +++ b/rpi23-gen-image.sh @@ -112,7 +112,9 @@ APT_PROXY=${APT_PROXY:=""} APT_SERVER=${APT_SERVER:="ftp.debian.org"} # Feature settings +ENABLE_PRINTK=${ENABLE_PRINTK:=false} ENABLE_BLUETOOTH=${ENABLE_BLUETOOTH:=false} +ENABLE_MINIUART_OVERLAY=${ENABLE_MINIUART_OVERLAY:=false} ENABLE_CONSOLE=${ENABLE_CONSOLE:=true} ENABLE_I2C=${ENABLE_I2C:=false} ENABLE_SPI=${ENABLE_SPI:=false} @@ -214,7 +216,7 @@ set +x #Check if apt-cacher-ng has port 3142 open and set APT_PROXY APT_CACHER_RUNNING=$(lsof -i :3142 | grep apt-cacher-ng | cut -d ' ' -f3 | uniq) -if [ -n ${APT_CACHER_RUNNING} ] ; then +if [ -n "${APT_CACHER_RUNNING}" ] ; then APT_PROXY=http://127.0.0.1:3142/ fi @@ -322,30 +324,26 @@ case "$RPI_MODEL" in ;; esac -# Prepare date string for default image file name -DATE="$(date +%Y-%m-%d)" -if [ -z "$KERNEL_BRANCH" ] ; then - IMAGE_NAME=${IMAGE_NAME:=${BASEDIR}/${DATE}-${KERNEL_ARCH}-CURRENT-rpi${RPI_MODEL}-${RELEASE}-${RELEASE_ARCH}} -else - IMAGE_NAME=${IMAGE_NAME:=${BASEDIR}/${DATE}-${KERNEL_ARCH}-${KERNEL_BRANCH}-rpi${RPI_MODEL}-${RELEASE}-${RELEASE_ARCH}} -fi - +# Raspberry PI 0,3,3P with Bluetooth and Wifi onboard if [ "$RPI_MODEL" = 0 ] || [ "$RPI_MODEL" = 3 ] || [ "$RPI_MODEL" = 3P ] ; then # Include bluetooth packages on supported boards if [ "$ENABLE_BLUETOOTH" = true ] && [ "$ENABLE_CONSOLE" = false ]; then APT_INCLUDES="${APT_INCLUDES},bluetooth,bluez" fi - # Check if console or bluetooth configuration is invalid on RPI 0,3,3P - if [ "$ENABLE_BLUETOOTH" = true ] && [ "$ENABLE_CONSOLE" = true ]; then - echo "error: ENABLE_BLUETOOTH and ENABLE_CONSOLE can't be active on the same time." +else # Raspberry PI 1,1P,2 without Wifi and bluetooth onboard + # Check if the internal wireless interface is not supported by the RPi model + if [ "$ENABLE_WIRELESS" = true ] || [ "$ENABLE_BLUETOOTH" = true ]; then + echo "error: The selected Raspberry Pi model has no integrated interface for wireless or bluetooth" exit 1 - fi - else # if [ "$RPI_MODEL" = 1 ] || [ "$RPI_MODEL" = 1P ] || [ "$RPI_MODEL" = 2 ] ; then - # Check if the internal wireless interface is not supported by the RPi model - if [ "$ENABLE_WIRELESS" = true ] ; then - echo "error: The selected Raspberry Pi model has no internal wireless interface" - exit 1 - fi + fi +fi + +# Prepare date string for default image file name +DATE="$(date +%Y-%m-%d)" +if [ -z "$KERNEL_BRANCH" ] ; then + IMAGE_NAME=${IMAGE_NAME:=${BASEDIR}/${DATE}-${KERNEL_ARCH}-CURRENT-rpi${RPI_MODEL}-${RELEASE}-${RELEASE_ARCH}} +else + IMAGE_NAME=${IMAGE_NAME:=${BASEDIR}/${DATE}-${KERNEL_ARCH}-${KERNEL_BRANCH}-rpi${RPI_MODEL}-${RELEASE}-${RELEASE_ARCH}} fi # Check if DISABLE_UNDERVOLT_WARNINGS parameter value is supported