Maksym Sloyko | 9634273 | 2021-08-03 21:54:28 +0000 | [diff] [blame] | 1 | #!/bin/bash |
| 2 | # Copyright 2021 Google LLC |
| 3 | # |
| 4 | # Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | # you may not use this file except in compliance with the License. |
| 6 | # You may obtain a copy of the License at |
| 7 | # |
| 8 | # http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | # |
| 10 | # Unless required by applicable law or agreed to in writing, software |
| 11 | # distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | # See the License for the specific language governing permissions and |
| 14 | # limitations under the License. |
| 15 | |
| 16 | |
| 17 | # List of options the script accepts. Trailing column means that the option |
| 18 | # requires an argument. |
| 19 | ARGUMENT_LIST=( |
| 20 | "help" |
| 21 | "product-id:" |
| 22 | "product-name:" |
| 23 | "host-mac:" |
| 24 | "bind-device:" |
| 25 | "dev-mac:" |
William A. Kennington III | f8a7774 | 2022-02-28 18:05:54 -0800 | [diff] [blame] | 26 | "dev-type:" |
Maksym Sloyko | 9634273 | 2021-08-03 21:54:28 +0000 | [diff] [blame] | 27 | "gadget-dir-name:" |
| 28 | "iface-name:" |
| 29 | ) |
| 30 | |
| 31 | print_usage() { |
| 32 | cat <<HELP |
| 33 | $0 [OPTIONS] [stop|start] |
| 34 | Create USB Gadget Configuration |
| 35 | --product-id USB Product Id for the gadget. |
| 36 | --product-name Product name string (en) for the gadget. |
| 37 | --host-mac MAC address of the host part of the connection. Optional. |
| 38 | --dev-mac MAC address of the device (gadget) part of the connection. Optional. |
William A. Kennington III | f8a7774 | 2022-02-28 18:05:54 -0800 | [diff] [blame] | 39 | --dev-type Type of gadget to instantiate. Default: "eem" |
Maksym Sloyko | 9634273 | 2021-08-03 21:54:28 +0000 | [diff] [blame] | 40 | --bind-device Name of the device to bind, as listed in /sys/class/udc/ |
| 41 | --gadget-dir-name Optional base name for gadget directory. Default: "g1" |
| 42 | --iface-name Optional name of the network interface. Default: "usb0" |
| 43 | --help Print this help and exit. |
| 44 | HELP |
| 45 | } |
| 46 | |
| 47 | gadget_start() { |
William A. Kennington III | a9de0df | 2022-02-22 17:11:13 -0800 | [diff] [blame] | 48 | # Add the gbmcbr configuration if this is a relevant device |
| 49 | if (( ID_VENDOR == 0x18d1 && ID_PRODUCT == 0x22b )); then |
| 50 | mkdir -p /run/systemd/network |
| 51 | cat >/run/systemd/network/+-bmc-"${IFACE_NAME}".network <<EOF |
| 52 | [Match] |
| 53 | Name=${IFACE_NAME} |
| 54 | [Network] |
| 55 | Bridge=gbmcbr |
| 56 | [Bridge] |
| 57 | Cost=85 |
| 58 | EOF |
| 59 | networkctl reload || true |
| 60 | fi |
| 61 | |
Maksym Sloyko | 9634273 | 2021-08-03 21:54:28 +0000 | [diff] [blame] | 62 | local gadget_dir="${CONFIGFS_HOME}/usb_gadget/${GADGET_DIR_NAME}" |
| 63 | mkdir -p "${gadget_dir}" |
| 64 | echo ${ID_VENDOR} > "${gadget_dir}/idVendor" |
| 65 | echo ${ID_PRODUCT} > "${gadget_dir}/idProduct" |
| 66 | |
| 67 | local str_en_dir="${gadget_dir}/strings/0x409" |
| 68 | mkdir -p "${str_en_dir}" |
| 69 | echo ${STR_EN_VENDOR} > "${str_en_dir}/manufacturer" |
| 70 | echo ${STR_EN_PRODUCT} > "${str_en_dir}/product" |
| 71 | |
| 72 | local config_dir="${gadget_dir}/configs/c.1" |
| 73 | mkdir -p "${config_dir}" |
| 74 | echo 100 > "${config_dir}/MaxPower" |
| 75 | mkdir -p "${config_dir}/strings/0x409" |
William A. Kennington III | f8a7774 | 2022-02-28 18:05:54 -0800 | [diff] [blame] | 76 | echo "${DEV_TYPE^^}" > "${config_dir}/strings/0x409/configuration" |
Maksym Sloyko | 9634273 | 2021-08-03 21:54:28 +0000 | [diff] [blame] | 77 | |
William A. Kennington III | f8a7774 | 2022-02-28 18:05:54 -0800 | [diff] [blame] | 78 | local func_dir="${gadget_dir}/functions/${DEV_TYPE}.${IFACE_NAME}" |
Maksym Sloyko | 9634273 | 2021-08-03 21:54:28 +0000 | [diff] [blame] | 79 | mkdir -p "${func_dir}" |
| 80 | |
| 81 | if [[ -n $HOST_MAC_ADDR ]]; then |
| 82 | echo ${HOST_MAC_ADDR} > ${func_dir}/host_addr |
| 83 | fi |
| 84 | |
| 85 | if [[ -n $DEV_MAC_ADDR ]]; then |
| 86 | echo ${DEV_MAC_ADDR} > ${func_dir}/dev_addr |
| 87 | fi |
| 88 | |
| 89 | ln -s "${func_dir}" "${config_dir}" |
| 90 | |
| 91 | echo "${BIND_DEVICE}" > ${gadget_dir}/UDC |
| 92 | } |
| 93 | |
| 94 | gadget_stop() { |
| 95 | local gadget_dir="${CONFIGFS_HOME}/usb_gadget/${GADGET_DIR_NAME}" |
William A. Kennington III | f8a7774 | 2022-02-28 18:05:54 -0800 | [diff] [blame] | 96 | rm -f ${gadget_dir}/configs/c.1/${DEV_TYPE}.${IFACE_NAME} |
Maksym Sloyko | 9634273 | 2021-08-03 21:54:28 +0000 | [diff] [blame] | 97 | rm -rf ${gadget_dir}/configs/c.1/strings/0x409 |
| 98 | rm -rf ${gadget_dir}/configs/c.1 |
| 99 | rm -rf ${gadget_dir}/strings/0x409 |
William A. Kennington III | f8a7774 | 2022-02-28 18:05:54 -0800 | [diff] [blame] | 100 | rm -rf ${gadget_dir}/functions/${DEV_TYPE}.${IFACE_NAME} |
Maksym Sloyko | 9634273 | 2021-08-03 21:54:28 +0000 | [diff] [blame] | 101 | rm -rf ${gadget_dir} |
William A. Kennington III | a9de0df | 2022-02-22 17:11:13 -0800 | [diff] [blame] | 102 | |
| 103 | rm -f /run/systemd/network/+-bmc-"${IFACE_NAME}".network |
| 104 | networkctl reload |
Maksym Sloyko | 9634273 | 2021-08-03 21:54:28 +0000 | [diff] [blame] | 105 | } |
| 106 | |
| 107 | opts=$(getopt \ |
| 108 | --longoptions "$(printf "%s," "${ARGUMENT_LIST[@]}")" \ |
| 109 | --name "$(basename "$0")" \ |
| 110 | --options "" \ |
| 111 | -- "$@" |
| 112 | ) |
| 113 | |
| 114 | eval set --$opts |
| 115 | |
| 116 | CONFIGFS_HOME=${CONFIGFS_HOME:-/sys/kernel/config} |
| 117 | ID_VENDOR="0x18d1" # Google |
| 118 | ID_PRODUCT="" |
| 119 | STR_EN_VENDOR="Google" |
| 120 | STR_EN_PRODUCT="" |
| 121 | DEV_MAC_ADDR="" |
William A. Kennington III | f8a7774 | 2022-02-28 18:05:54 -0800 | [diff] [blame] | 122 | DEV_TYPE="eem" |
Maksym Sloyko | 9634273 | 2021-08-03 21:54:28 +0000 | [diff] [blame] | 123 | HOST_MAC_ADDR="" |
| 124 | BIND_DEVICE="" |
| 125 | ACTION="start" |
| 126 | GADGET_DIR_NAME="g1" |
| 127 | IFACE_NAME="usb0" |
| 128 | while [[ $# -gt 0 ]]; do |
| 129 | case "$1" in |
| 130 | --product-id) |
| 131 | ID_PRODUCT=$2 |
| 132 | shift 2 |
| 133 | ;; |
| 134 | --product-name) |
| 135 | STR_EN_PRODUCT=$2 |
| 136 | shift 2 |
| 137 | ;; |
| 138 | --host-mac) |
| 139 | HOST_MAC_ADDR=$2 |
| 140 | shift 2 |
| 141 | ;; |
| 142 | --dev-mac) |
| 143 | DEV_MAC_ADDR=$2 |
| 144 | shift 2 |
| 145 | ;; |
William A. Kennington III | f8a7774 | 2022-02-28 18:05:54 -0800 | [diff] [blame] | 146 | --dev-type) |
| 147 | DEV_TYPE=$2 |
| 148 | shift 2 |
| 149 | ;; |
Maksym Sloyko | 9634273 | 2021-08-03 21:54:28 +0000 | [diff] [blame] | 150 | --bind-device) |
| 151 | BIND_DEVICE=$2 |
| 152 | shift 2 |
| 153 | ;; |
| 154 | --gadget-dir-name) |
| 155 | GADGET_DIR_NAME=$2 |
| 156 | shift 2 |
| 157 | ;; |
| 158 | --iface-name) |
| 159 | IFACE_NAME=$2 |
| 160 | shift 2 |
| 161 | ;; |
| 162 | --help) |
| 163 | print_usage |
| 164 | exit 0 |
| 165 | ;; |
| 166 | start) |
| 167 | ACTION="start" |
| 168 | shift 1 |
| 169 | break |
| 170 | ;; |
| 171 | stop) |
| 172 | ACTION="stop" |
| 173 | shift 1 |
| 174 | break |
| 175 | ;; |
| 176 | --) |
| 177 | shift 1 |
| 178 | ;; |
| 179 | *) |
| 180 | break |
| 181 | ;; |
| 182 | esac |
| 183 | done |
| 184 | |
| 185 | if [[ $ACTION == "stop" ]]; then |
| 186 | gadget_stop |
| 187 | else |
William A. Kennington III | 986d3a6 | 2022-03-31 17:58:30 -0700 | [diff] [blame^] | 188 | rc=0 |
| 189 | gadget_start || rc=$? |
| 190 | (( rc == 0 )) || gadget_stop || true |
| 191 | exit $rc |
Maksym Sloyko | 9634273 | 2021-08-03 21:54:28 +0000 | [diff] [blame] | 192 | fi |