Skip to content
Snippets Groups Projects
Verified Commit ce05d2d8 authored by Minecrell's avatar Minecrell Committed by Alexey Minnekhanov
Browse files

main/postmarketos-initramfs: unify setup_usb_storage (MR 5912)

Right now we have two similar implementations of setting up a USB mass
storage gadget, in init_functions.sh and as separate script for the
debug-shell. Given that init_functions.sh is available in debug-shell
anyway, unify both in a common setup_usb_storage_configfs() function.

Having this included in the initramfs debug-shell by default (without
installing postmarketos-mkinitfs-hook-debug-shell) also simplifies
recovery, since you can set up the USB storage for repairing the flashed
postmarketOS installation in case of boot failures. The size overhead is
minimal, since this has no extra dependencies.
parent 854b37b9
Branches
No related tags found
1 merge request!5912main/postmarketos-initramfs: unify setup_usb_storage, add pmos.usb-storage= option
......@@ -21,7 +21,6 @@ sh_files="
./main/postmarketos-installkernel/installkernel-pmos
./main/postmarketos-initramfs/init.sh
./main/postmarketos-initramfs/init_functions.sh
./main/postmarketos-mkinitfs-hook-debug-shell/setup_usb_storage.sh
./main/postmarketos-mkinitfs-hook-netboot/netboot.sh
./main/ttyescape/*.post-install
./main/unl0kr/unlock.sh
......
......@@ -11,6 +11,7 @@ SUBPARTITION_DEV="${SUBPARTITION_DEV:-}"
CONFIGFS="/config/usb_gadget"
CONFIGFS_ACM_FUNCTION="acm.usb0"
CONFIGFS_MASS_STORAGE_FUNCTION="mass_storage.0"
HOST_IP="${unudhcpd_host_ip:-172.16.42.1}"
deviceinfo_getty="${deviceinfo_getty:-}"
......@@ -812,6 +813,51 @@ run_getty() {
} &
}
setup_usb_storage_configfs() {
active_udc="$(cat $CONFIGFS/g1/UDC)"
storage_dev="$1"
if ! [ -e "$CONFIGFS" ]; then
echo " $CONFIGFS does not exist, can't set up storage gadget"
return 1
fi
if [ -z "$storage_dev" ]; then
if [ -e "$CONFIGFS/g1/configs/c.1/$CONFIGFS_MASS_STORAGE_FUNCTION" ]; then
echo "Disabling USB mass storage gadget"
unlink "$CONFIGFS/g1/configs/c.1/$CONFIGFS_MASS_STORAGE_FUNCTION"
setup_usb_configfs_udc
fi
return 0
fi
if ! [ -b "$storage_dev" ]; then
echo " Storage device '$storage_dev' is not a block device"
return 1
fi
# Set up network gadget if not already done
if [ -z "$active_udc" ]; then
setup_usb_network_configfs "skip_udc"
else
# Unset UDC before reconfiguring gadget
echo "" > $CONFIGFS/g1/UDC
fi
# Create mass storage function
mkdir -p "$CONFIGFS/g1/functions/$CONFIGFS_MASS_STORAGE_FUNCTION" \
|| echo " Couldn't create $CONFIGFS/g1/functions/$CONFIGFS_MASS_STORAGE_FUNCTION"
echo "$storage_dev" > "$CONFIGFS/g1/functions/$CONFIGFS_MASS_STORAGE_FUNCTION/lun.0/file"
# Link the mass storage function to the configuration
ln -sf "$CONFIGFS/g1/functions/$CONFIGFS_MASS_STORAGE_FUNCTION" "$CONFIGFS/g1/configs/c.1" \
|| echo " Couldn't symlink $CONFIGFS_MASS_STORAGE_FUNCTION"
setup_usb_configfs_udc
return 0
}
debug_shell() {
echo "Entering debug shell"
# if we have a UDC it's already been configured for USB networking
......@@ -847,12 +893,10 @@ debug_shell() {
# Add pmos_logdump message only if relevant
if [ -n "$have_udc" ]; then
echo "Run 'pmos_logdump' to generate a log dump and expose it over USB." >> /README
fi
if [ -n "$have_udc" ] && [ -f /usr/bin/setup_usb_storage ]; then
cat <<-EOF >> /README
You can expose storage devices over USB with
'setup_usb_storage /dev/DEVICE'
'setup_usb_storage_configfs /dev/DEVICE'
EOF
fi
......@@ -948,11 +992,13 @@ debug_shell() {
fi
done
# Remove the ACM gadget device
# Remove the ACM/mass storage gadget devices
# FIXME: would be nice to have a way to keep this on and
# pipe kernel/init logs to it.
rm -f $CONFIGFS/g1/configs/c.1/"$CONFIGFS_ACM_FUNCTION"
rmdir $CONFIGFS/g1/functions/"$CONFIGFS_ACM_FUNCTION"
rm -f "$CONFIGFS/g1/configs/c.1/$CONFIGFS_MASS_STORAGE_FUNCTION"
rmdir "$CONFIGFS/g1/functions/$CONFIGFS_MASS_STORAGE_FUNCTION"
setup_usb_configfs_udc
show_splash "Loading..."
......@@ -1123,32 +1169,12 @@ create_logs_disk() {
# Make logs available via mass storage gadget
export_logs() {
local loop_dev=""
usb_mass_storage_function="mass_storage.0"
active_udc="$(cat $CONFIGFS/g1/UDC)"
local loop_dev
loop_dev="$(losetup -f)"
create_logs_disk "$loop_dev"
echo "Making logs available via mass storage"
# Set up network gadget if not already done
if [ -z "$active_udc" ]; then
setup_usb_network_configfs "skip_udc"
else
# Unset UDC
echo "" > $CONFIGFS/g1/UDC
fi
mkdir "$CONFIGFS"/g1/functions/"$usb_mass_storage_function" || return
echo "$loop_dev" > "$CONFIGFS"/g1/functions/"$usb_mass_storage_function"/lun.0/file
ln -s "$CONFIGFS"/g1/functions/"$usb_mass_storage_function" \
"$CONFIGFS"/g1/configs/c.1 || return
setup_usb_configfs_udc
setup_usb_storage_configfs "$loop_dev"
}
fail_halt_boot() {
......
......@@ -2,7 +2,6 @@
/usr/bin/fftest
/usr/bin/libinput
/usr/libexec/libinput/*
/usr/libexec/postmarketos-mkinitfs/setup_usb_storage.sh:/usr/bin/setup_usb_storage
/usr/sbin/fbdebug
/usr/sbin/reboot-mode
/usr/sbin/telnetd
pkgname=postmarketos-mkinitfs-hook-debug-shell
pkgver=0.7.0
pkgver=0.8.0
pkgrel=0
pkgdesc="Root shell in the initramfs (security hole, for debugging only)"
url="https://postmarketos.org"
depends="postmarketos-mkinitfs devicepkg-utils fbdebug evtest linuxconsoletools reboot-mode libinput libinput-tools"
source="20-debug-shell.files setup_usb_storage.sh"
source="20-debug-shell.files"
arch="noarch"
license="GPL2"
options="!check"
......@@ -12,11 +12,8 @@ options="!check"
package() {
install -Dm644 "$srcdir"/20-debug-shell.files \
"$pkgdir"/usr/share/mkinitfs/files/20-debug-shell.files
install -Dm755 "$srcdir"/setup_usb_storage.sh \
"$pkgdir"/usr/libexec/postmarketos-mkinitfs/setup_usb_storage.sh
}
sha512sums="
a739bc47d905d189edb26d9ebfd062023720fefdaab27207471c16d53a9c12ea8b81092a1047d8f2300e42ba500bdf6c5a3343aca55aab5bf8e84d68eb5680ab 20-debug-shell.files
75d485c2e9f352cfd717b7a92753a9dfc4a72526a44bcbb784eacb4ef9011072b3ffa1c42a317c0940598cc076fb6c61676c440e5b188378b19ca08d882c1338 setup_usb_storage.sh
845d2eb6ab72c5c1472d65305241ca21dbc26784684e9ae99a180a255f8f7f62123b363d73c642df1f9687106340078311a68a76c123f16829b941bff9151735 20-debug-shell.files
"
#!/bin/sh
# shellcheck disable=SC1091
. /init_functions.sh
. /usr/share/misc/source_deviceinfo
# See: https://www.kernel.org/doc/Documentation/usb/gadget_configfs.txt
_configfs=/config/usb_gadget
if ! [ -e "$_configfs" ]; then
echo "/config/usb_gadget does not exist, can't setup configfs usb mass storage gadget"
exit 1
fi
# Create storage function.
usb_mass_storage_function="mass_storage.0"
if [ ! -d $_configfs/g1/functions/"$usb_mass_storage_function" ]; then
if ! mkdir $_configfs/g1/functions/"$usb_mass_storage_function"; then
echo "Couldn't create $_configfs/g1/functions/$usb_mass_storage_function"
exit 1
fi
fi
lun="0"
storage_dev="$1"
if [ -z "${storage_dev}" ]; then
#https://www.kernel.org/doc/html/latest/usb/gadget_configfs.html#cleaning-up
#First unlink the config
if [ -e $_configfs/g1/configs/c.1/"$usb_mass_storage_function" ]; then
echo "Disabling current usb storage device"
rm $_configfs/g1/configs/c.1/"$usb_mass_storage_function"
fi
#Delete the function config
if [ -d $_configfs/g1/functions/"$usb_mass_storage_function" ]; then
rmdir $_configfs/g1/functions/"$usb_mass_storage_function"
fi
elif [ -e "${storage_dev}" ]; then
if [ ! -d $_configfs/g1/functions/"$usb_mass_storage_function/lun.$lun" ]; then
if ! mkdir $_configfs/g1/functions/"$usb_mass_storage_function/lun.$lun"; then
echo " Couldn't create $_configfs/g1/functions/$usb_mass_storage_function/lun.$lun"
exit 1
fi
echo "0" > $_configfs/g1/functions/"$usb_mass_storage_function/lun.$lun"/removable
echo "0" > $_configfs/g1/functions/"$usb_mass_storage_function/lun.$lun"/nofua
echo "0" > $_configfs/g1/functions/"$usb_mass_storage_function/lun.$lun"/cdrom
echo "LUN $lun" > $_configfs/g1/functions/"$usb_mass_storage_function/lun.$lun"/inquiry_string
fi
echo "Setting $storage_dev as current usb storage"
echo "$storage_dev" > $_configfs/g1/functions/"$usb_mass_storage_function/lun.$lun"/file
# Link the usb storage instance to the configuration
if [ ! -e $_configfs/g1/configs/c.1/"$usb_mass_storage_function" ]; then
if ! ln -s $_configfs/g1/functions/"$usb_mass_storage_function" $_configfs/g1/configs/c.1; then
echo "Couldn't symlink $usb_mass_storage_function"
exit 1
fi
fi
else
echo "$storage_dev not found"
exit 1
fi
#Reset UDC to apply changes
setup_usb_configfs_udc
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment