Adriana Kobylak | a4fddd7 | 2018-11-13 10:53:15 -0600 | [diff] [blame] | 1 | #!/bin/sh |
| 2 | |
| 3 | if [ $# -ne 2 ] |
| 4 | then |
| 5 | echo "usage: $0 <start|stop> <config>" >&2 |
| 6 | exit 1 |
| 7 | fi |
| 8 | |
| 9 | action=$1 |
| 10 | config=$2 |
| 11 | |
| 12 | gadget_name=mass-storage |
| 13 | gadget_dir=/sys/kernel/config/usb_gadget/$gadget_name |
| 14 | |
| 15 | case "$config" in |
| 16 | 0) |
| 17 | nbd_device=/dev/nbd0 |
| 18 | ;; |
Ravi Teja | 504c22f | 2020-07-19 06:46:25 -0500 | [diff] [blame] | 19 | 1) |
| 20 | nbd_device=/dev/nbd1 |
| 21 | ;; |
Adriana Kobylak | a4fddd7 | 2018-11-13 10:53:15 -0600 | [diff] [blame] | 22 | *) |
| 23 | echo "invalid config $config" >&2 |
| 24 | exit 1 |
| 25 | ;; |
| 26 | esac |
| 27 | |
| 28 | set -ex |
| 29 | |
| 30 | case "$action" in |
| 31 | start) |
| 32 | mkdir -p $gadget_dir |
| 33 | ( |
| 34 | cd $gadget_dir |
| 35 | # http://www.linux-usb.org/usb.ids |
| 36 | # |-> 1d6b Linux Foundation |
| 37 | # |-> 0104 Multifunction Composite Gadget |
| 38 | echo "0x1d6b" > idVendor |
| 39 | echo "0x0104" > idProduct |
| 40 | mkdir -p strings/0x409 |
| 41 | echo "OpenBMC" > strings/0x409/manufacturer |
| 42 | echo "Virtual Media Device" > strings/0x409/product |
| 43 | mkdir -p configs/c.1/strings/0x409 |
| 44 | echo "config 1" > configs/c.1/strings/0x409/configuration |
| 45 | mkdir -p functions/mass_storage.usb0 |
| 46 | ln -s functions/mass_storage.usb0 configs/c.1 |
| 47 | echo 1 > functions/mass_storage.usb0/lun.0/removable |
| 48 | echo 1 > functions/mass_storage.usb0/lun.0/ro |
| 49 | echo 0 > functions/mass_storage.usb0/lun.0/cdrom |
| 50 | echo $nbd_device > functions/mass_storage.usb0/lun.0/file |
| 51 | echo "1e6a0000.usb-vhub:p4" > UDC |
| 52 | ) |
| 53 | ;; |
| 54 | stop) |
| 55 | ( |
| 56 | cd $gadget_dir |
| 57 | rm configs/c.1/mass_storage.usb0 |
| 58 | rmdir functions/mass_storage.usb0 |
| 59 | rmdir configs/c.1/strings/0x409 |
| 60 | rmdir configs/c.1 |
| 61 | rmdir strings/0x409 |
| 62 | ) |
| 63 | rmdir $gadget_dir |
| 64 | ;; |
| 65 | *) |
| 66 | echo "invalid action $action" >&2 |
| 67 | exit 1 |
| 68 | esac |
| 69 | |
| 70 | exit 0 |
| 71 | |