Medad CChien | c73a082 | 2020-06-18 16:57:57 +0800 | [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 | ;; |
| 19 | *) |
| 20 | echo "invalid config $config" >&2 |
| 21 | exit 1 |
| 22 | ;; |
| 23 | esac |
| 24 | |
| 25 | set -ex |
| 26 | |
| 27 | case "$action" in |
| 28 | start) |
| 29 | mkdir -p $gadget_dir |
| 30 | ( |
| 31 | cd $gadget_dir |
| 32 | # http://www.linux-usb.org/usb.ids |
| 33 | # |-> 1d6b Linux Foundation |
| 34 | # |-> 0104 Multifunction Composite Gadget |
| 35 | echo "0x1d6b" > idVendor |
| 36 | echo "0x0104" > idProduct |
| 37 | mkdir -p strings/0x409 |
| 38 | echo "OpenBMC" > strings/0x409/manufacturer |
| 39 | echo "Virtual Media Device" > strings/0x409/product |
| 40 | mkdir -p configs/c.1/strings/0x409 |
| 41 | echo "config 1" > configs/c.1/strings/0x409/configuration |
| 42 | mkdir -p functions/mass_storage.usb0 |
| 43 | ln -s functions/mass_storage.usb0 configs/c.1 |
| 44 | echo 1 > functions/mass_storage.usb0/lun.0/removable |
| 45 | echo 1 > functions/mass_storage.usb0/lun.0/ro |
| 46 | echo 0 > functions/mass_storage.usb0/lun.0/cdrom |
| 47 | echo $nbd_device > functions/mass_storage.usb0/lun.0/file |
| 48 | echo "f0831000.udc" > UDC |
| 49 | ) |
| 50 | ;; |
| 51 | stop) |
| 52 | ( |
| 53 | cd $gadget_dir |
| 54 | rm configs/c.1/mass_storage.usb0 |
| 55 | rmdir functions/mass_storage.usb0 |
| 56 | rmdir configs/c.1/strings/0x409 |
| 57 | rmdir configs/c.1 |
| 58 | rmdir strings/0x409 |
| 59 | ) |
| 60 | rmdir $gadget_dir |
| 61 | ;; |
| 62 | *) |
| 63 | echo "invalid action $action" >&2 |
| 64 | exit 1 |
| 65 | esac |
| 66 | |
| 67 | exit 0 |
| 68 | |