diff --git a/main/postmarketos-mkinitfs-hook-debug-shell/20-debug-shell.files b/main/postmarketos-mkinitfs-hook-debug-shell/20-debug-shell.files index b50cf397a..6d6bb2284 100644 --- a/main/postmarketos-mkinitfs-hook-debug-shell/20-debug-shell.files +++ b/main/postmarketos-mkinitfs-hook-debug-shell/20-debug-shell.files @@ -2,6 +2,7 @@ /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 diff --git a/main/postmarketos-mkinitfs-hook-debug-shell/20-debug-shell.sh b/main/postmarketos-mkinitfs-hook-debug-shell/20-debug-shell.sh index 33386deb6..d5cc3a743 100644 --- a/main/postmarketos-mkinitfs-hook-debug-shell/20-debug-shell.sh +++ b/main/postmarketos-mkinitfs-hook-debug-shell/20-debug-shell.sh @@ -12,6 +12,8 @@ show_splash "WARNING: debug-shell is active\\nhttps://postmarketos.org/debug-she echo "Create 'pmos_continue_boot' script" { echo "#!/bin/sh" + #Disable any active usb mass storage + echo "if [ -d /config/usb_gadget/g1/functions/mass_storage.0 ]; then setup_usb_storage; fi" echo "pkill -9 -f pmos_shell" echo "pkill -f pmos_loop_forever" echo "pkill -f telnetd.*:${TELNET_PORT}" @@ -57,6 +59,7 @@ echo "---" echo "WARNING: debug-shell is active on ${host_ip}:${TELNET_PORT}." echo "This is a security hole! Only use it for debugging, and" echo "uninstall the debug-shell hook afterwards!" +echo "You can expose storage devices using 'setup_usb_storage /dev/DEVICE'" echo "---" pmos_shell diff --git a/main/postmarketos-mkinitfs-hook-debug-shell/APKBUILD b/main/postmarketos-mkinitfs-hook-debug-shell/APKBUILD index 77e6b19d7..71414bf2f 100644 --- a/main/postmarketos-mkinitfs-hook-debug-shell/APKBUILD +++ b/main/postmarketos-mkinitfs-hook-debug-shell/APKBUILD @@ -1,10 +1,10 @@ pkgname=postmarketos-mkinitfs-hook-debug-shell -pkgver=0.4.7 +pkgver=0.5.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.sh 20-debug-shell.files" +source="20-debug-shell.sh 20-debug-shell.files setup_usb_storage.sh" arch="noarch" license="GPL2" options="!check" @@ -14,9 +14,12 @@ package() { "$pkgdir"/usr/share/mkinitfs/hooks/20-debug-shell.sh 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=" -42ef2f0a1089bba9cbe094b346b68df62b9499de0e6193e4d19bc0c491b2bc2ae14399908fd1371dd9111261fca0df4526f7dc13e7c861834b01fc03a3ed5fc4 20-debug-shell.sh -845d2eb6ab72c5c1472d65305241ca21dbc26784684e9ae99a180a255f8f7f62123b363d73c642df1f9687106340078311a68a76c123f16829b941bff9151735 20-debug-shell.files +0750a33f3b68fd6f751bdcf3c59656ec8c8af60e13c25b13394937134d4cbeaacd237b5d92e2a978319f705781f39712c71673367cec52b584f666af50b6f952 20-debug-shell.sh +a739bc47d905d189edb26d9ebfd062023720fefdaab27207471c16d53a9c12ea8b81092a1047d8f2300e42ba500bdf6c5a3343aca55aab5bf8e84d68eb5680ab 20-debug-shell.files +75d485c2e9f352cfd717b7a92753a9dfc4a72526a44bcbb784eacb4ef9011072b3ffa1c42a317c0940598cc076fb6c61676c440e5b188378b19ca08d882c1338 setup_usb_storage.sh " diff --git a/main/postmarketos-mkinitfs-hook-debug-shell/setup_usb_storage.sh b/main/postmarketos-mkinitfs-hook-debug-shell/setup_usb_storage.sh new file mode 100644 index 000000000..667124255 --- /dev/null +++ b/main/postmarketos-mkinitfs-hook-debug-shell/setup_usb_storage.sh @@ -0,0 +1,65 @@ +#!/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 +