Differences

This shows you the differences between two versions of the page.


Previous revision
Next revision
fuss:raspberry_pi [2020/07/21 05:52] – [Running a One-Off Script on First Machine Boot] office
Line 1: Line 1:
 +====== Enable UART Serial Communication ======
 +
 +Edit ''/boot/config.txt'' and append:
 +<code>
 +enable_uart=1
 +</code>
 +
 +to the end of the file and reboot.
 +
 +====== Disable Wifi Power Management ======
 +
 +When receiving large amounts of output over SSH the Raspberry Pi might render the console inoperable and the connection will time out in a few seconds.
 +
 +The following command adds some module options to the wifi kernel module to disable power management which is apparently responsible for the former issue.
 +
 +<code>
 +echo "options 8192cu rtw_power_mgnt=0 rtw_enusbss=0" | sudo tee --append /etc/modprobe.d/8192cu.conf
 +</code>
 +
 +====== Enable SSH on (First) Boot ======
 +
 +Create a blank file named ''ssh'' with no extension at the root of the boot partition of the SD card in order to make Raspbian start the SSH daemon on boot. One use for this trick is combining the Raspberry Pi with an ethernet hat such that the Raspberry can be setup without needing to attach a monitor and keyboard.
 +
 +====== Check if Hardware Decoders are Enabled ======
 +
 +<code>
 +vcgencmd codec_enabled MPG2
 +vcgencmd codec_enabled WVC1
 +</code>
 +
 +Note that the Raspberry Pi 4 does not have hardware decoders such that the commands will most likely state ''disabled''.
 +
 +====== Change Audio Output ======
 +
 +Issuing:
 +<code bash>
 +amixer controls
 +</code>
 +
 +will yield the controls available on the Raspberry Pi:
 +<code bash>
 +# amixer controls
 +numid=3,iface=MIXER,name='PCM Playback Route'
 +numid=2,iface=MIXER,name='PCM Playback Switch'
 +numid=1,iface=MIXER,name='PCM Playback Volume'
 +</code>
 +
 +The Raspberry Pi 2,3 and 4 can commute between sending sound through the $3.5mm$ audio jack:
 +<code bash>
 +amixer cset numid=3 1
 +</code>
 +or the HDMI output:
 +<code bash>
 +amixer cset numid=3 2
 +</code>
 +respectively, or:
 +<code bash>
 +amixer cset numid=3
 +</code>
 +to set the sound selection to autodetect where the jack will be used only if the HDMI cable is not connected.
 +
 +====== Fixing Various Network Issues ======
 +
 +Any of the following symptoms are to be detected?
 +  * SSH connections drop,
 +  * issuing commands with large output over SSH makes the SSH session hang,
 +  * connection issues toward package repositories on Raspbian
 +
 +They may all boil down to a non-uniform MTU setting across the network.
 +
 +====== Logging-in via SSH over USB ======
 +
 +The Raspberry Pi when connected to a PC can configure itself as an USB gadget and start a network over USB. A RNDIS network adapter is needed in most cases and can be downloaded from here:
 +
 +  * {{fuss:fuss_raspberry_pi_rndis_driver.zip|RNDIS.zip}}
 +
 +In order to log into the Raspberry Pi over SSH via the USB port, edit ''config.txt'' and add:
 +<code>
 +dtoverlay=dwc2
 +</code>
 +
 +Next, edit ''cmdline.txt'' and add the following to the kernel parameters between ''rootwait'' and ''quiet'':
 +<code>
 +modules-load=dwc2,g_ether
 +</code>
 +
 +Finally, add a blank file named ''ssh'' with no contents to the root of the ''boot'' partition.
 +
 +The Raspberry can now be connected to a PC via its USB port (not the USB port marked ''PWR''). The PC that the Raspberry Pi connects to will act as a router and should forward packets to the Raspberry Pi. 
 +
 +When the RNDIS interface is brought up, if Apple bonjour is installed and running on the machine that the Raspberry Pi is connected to, then the Raspberry Pi is supposed to register the DNS address ''raspberry.local'' via mDNS with Windows. Even if Apple bonjour is installed, the name might not be assigned even though the Rasbperry Pi might be configured properly as per the above. Also, it seems that in some cases when the RNDIS interface is brought up, the Raspberry Pi does not send any packets as all making an ARP table query via ''arp -a'' entirely futile.
 +
 +The first attempt would be to try connecting via the assigned DNS address: ''ssh pi@raspberry.local'' and enter ''raspberry'' at the password prompt. if that fails, a packet sniffer such as WireShark or TcpDump may reveal the initial Raspberry Pi's ARP requests for Google's DNS servers (''8.8.8.8''). Sometimes just restarting the bonjour service and disabling and re-enabling the RNDIS interface may get the Raspberry Pi to register its name.
 +
 +===== Connecting to a Bridge Automatically =====
 +
 +In order to connect the Raspberry Pi to a bridge as it is connected to a server, [[fuss/linux#automatically_add_all_rndis_devices_to_a_bridge|an udev rule can be written]] that will match all RNDIS adapters and connect them to a bridge.
 +
 +The only changes required on the Raspberry Pi is to edit ''/etc/network/interfaces.d/usb0'' and add the lines:
 +<code>
 +auto usb0
 +iface usb0 inet dhcp
 +</code>
 +
 +and save the file. This will make sure that when the Raspberry Pi boots, an IP address will be requested over the ''usb0'' interface and will work provided that the computer that the Raspberry Pi is connected to is running a DHCP server on the other end.
 +
 +====== Enabling Monitor Mode via Nexmon Drivers ======
 +
 +Some Raspberry Pi devices such as the Raspberry Pi Zero W can operate in monitor mode in order to sniff wireless traffic. Up to this date, there is no official way to get monitor mode working for the Raspberry SoC except for using alternative Nexmon drivers that replace the ''brcmfmac'' kernel module with a patched module enabling monitor mode. Unfortunately, the Nexmon drivers lag behind and last successful builds are known to work with Raspbian Jessie at best.
 +
 +Here is a short guide on how to build the nexmon drivers for Raspberry Pi W and substitute the kernel module in order to enable monitor mode.
 +
 +Install required packages to compile the drivers:
 +<code bash>
 +apt-get install subversion raspberrypi-kernel-headers git libgmp3-dev gawk qpdf bison flex make autoconf automake build-essential libtool libisl10
 +</code>
 +
 +Change directory to a compile space:
 +<code bash>
 +cd /usr/src
 +</code>
 +
 +and check out the source (official):
 +<code bash>
 +git clone https://github.com/MaximusBaton/nexmon.git
 +</code>
 +
 +or from Wizardry and Steamworks:
 +<code bash>
 +svn co http://svn.grimore.org/nexmon
 +</code>
 +
 +Set variables:
 +<code bash>
 +export NEXMON_ROOT=/usr/src/nexmon
 +</code>
 +
 +and load the provided environment:
 +<code bash>
 +source setup_env.sh
 +</code>
 +
 +Now compile the nexmon package from the top-level:
 +<code bash>
 +make
 +</code>
 +
 +Now build the kernel module and firmware:
 +<code bash>
 +cd patches/bcm43430a1/7_45_41_46/nexmon/
 +</code>
 +where:
 +  * ''bcm43430a1'' is for the Raspberry Pi Zero W
 +
 +<code bash>
 +make
 +</code>
 +
 +Now generate a backup of the original firmware file and install the patched firmware:
 +<code bash>
 +make backup-firmware
 +make install-firmware
 +</code>
 +
 +Compile ''nexutil'' since it might come in handy:
 +<code bash>
 +cd utilities/nexutil
 +make install
 +</code>
 +
 +Now to make the Raspberry load the modified kernel module after reboot it, the existing module is simply substituted for the module that was compiled at the previous step:
 +<code bash>
 +PATH_OF_DEFAULT_DRIVER_AT_REBOOT=$(modinfo brcmfmac | grep -m 1 -oP "^filename:(\s*?)(.*)$" | sed -e 's/^filename:\(\s*\)\(.*\)$/\2/g')
 +mv "$PATH_OF_DEFAULT_DRIVER_AT_REBOOT" "$PATH_OF_DEFAULT_DRIVER_AT_REBOOT.orig"
 +cp patches/bcm43430a1/7_45_41_46/nexmon/brcmfmac_4.14.y-nexmon/brcmfmac.ko $PATH_OF_DEFAULT_DRIVER_AT_REBOOT
 +</code>
 +
 +Finally, generate module dependencies:
 +<code bash>
 +depmod -a
 +</code>
 +
 +In order to set up the Wifi monitoring interface on boot, create the file  ''/etc/network/interfaces.d/mon0'' with the following contents:
 +<code>
 +auto mon0
 +iface mon0 inet manual
 +  pre-up iw wlan0 interface add mon0 type monitor
 +  wireless-mode monitor
 +
 +</code>
 +
 +and restart the Raspberry Pi.
 +
 +====== Running a One-Off Script on First Machine Boot ======
 +
 +To execute a script unconditionally edit ''/boot/cmdline.txt'' and set ''init'' to mount virtual filesystems and then execute a script. For example, the ''/boot/cmdline.txt'' file would contain:
 +<code>
 +init=/bin/bash -- -c "mount -t proc proc /proc; mount -t sysfs sys /sys; mount /boot; source /boot/firstboot.sh"
 +</code>
 +
 +that will end up executing ''/boot/firstboot.sh''.
 +
 +The contents of ''/boot/firstboot.sh'' should make sure to remove the ''init'' parameter from the kernel command line such that the first boot script does not run again, then perform the necessary operations and finally restart the system:
 +<code bash>
 +# Boot
 +mount -t tmpfs tmp /run
 +mkdir -p /run/systemd
 +mount / -o remount,rw
 +# Ensure the script does not run again.
 +sed -i 's| init=.*||' /boot/cmdline.txt
 +
 +# Run
 +
 +# Insert custom commands.
 +/usr/lib/raspi-config/init_resize.sh
 +/usr/bin/ssh-keygen -A
 +
 +# Reboot
 +sync
 +umount /boot
 +mount / -o remount,ro
 +sync
 +echo 1 > /proc/sys/kernel/sysrq
 +echo b > /proc/sysrq-trigger
 +sleep 5
 +</code>
 +
 +====== Downgrade or Upgrade to Specific Kernel Version ======
 +
 +Pick a commit from the ''rpi-firmware'' [[https://github.com/Hexxeh/rpi-firmware/commits/master|github project]], look at the commit hash and then issue:
 +<code bash>
 +rpi-update HASH
 +</code>
 +where:
 +  * ''HASH'' - is the commit hash
 +
 +
 +
  

fuss/raspberry_pi.txt · Last modified: 2024/02/24 05:08 by office

Access website using Tor Access website using i2p Wizardry and Steamworks PGP Key


For the contact, copyright, license, warranty and privacy terms for the usage of this website please see the contact, license, privacy, copyright.