#!/bin/bash
# available under this license https://opensource.org/license/isc-license-txt
set -euo pipefail
# ref: https://www.reddit.com/r/linux4noobs/comments/1h8xcfb/libcomposite_mass_storage_with_2_luns/
mkdir -p /sys/kernel/config/usb_gadget/mygadget
cd /sys/kernel/config/usb_gadget/mygadget
echo 0x1d6b > idVendor
echo 0x0104 > idProduct
echo 0x0100 > bcdDevice
echo 0x0200 > bcdUSB
mkdir -p strings/0x409
echo "1234567890" > strings/0x409/serialnumber
echo "hydrogen18" > strings/0x409/manufacturer
echo "device1" > strings/0x409/product
mkdir -p configs/c.1/strings/0x409
echo "Config 1: Mass Storage" > configs/c.1/strings/0x409/configuration
echo 250 > configs/c.1/MaxPower

# lun0 is read only for the ISO file
mkdir -p functions/mass_storage.usb0
echo 0 > functions/mass_storage.usb0/lun.0/cdrom
echo 0 > functions/mass_storage.usb0/lun.0/ro
echo /opt/iso/alpine-standard-3.21.3-x86_64.iso > functions/mass_storage.usb0/lun.0/file

# lun1 is read-write for the install target
echo 1 > functions/mass_storage.usb0/lun.1/cdrom
echo 1 > functions/mass_storage.usb0/lun.1/ro
echo /opt/storage/install.img > functions/mass_storage.usb0/lun.1/file

ls /sys/class/udc > UDC
