blob: 3c38fb4b0b7812052e8db28f2f947c6e501edc1f [file] [log] [blame]
Maksym Sloyko96342732021-08-03 21:54:28 +00001#!/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.
19ARGUMENT_LIST=(
20 "help"
21 "product-id:"
22 "product-name:"
23 "host-mac:"
24 "bind-device:"
25 "dev-mac:"
William A. Kennington IIIf8a77742022-02-28 18:05:54 -080026 "dev-type:"
Maksym Sloyko96342732021-08-03 21:54:28 +000027 "gadget-dir-name:"
28 "iface-name:"
29)
30
31print_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 IIIf8a77742022-02-28 18:05:54 -080039 --dev-type Type of gadget to instantiate. Default: "eem"
Maksym Sloyko96342732021-08-03 21:54:28 +000040 --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.
44HELP
45}
46
47gadget_start() {
William A. Kennington IIIa9de0df2022-02-22 17:11:13 -080048 # 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]
53Name=${IFACE_NAME}
54[Network]
55Bridge=gbmcbr
56[Bridge]
57Cost=85
58EOF
59 networkctl reload || true
60 fi
61
Maksym Sloyko96342732021-08-03 21:54:28 +000062 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 IIIf8a77742022-02-28 18:05:54 -080076 echo "${DEV_TYPE^^}" > "${config_dir}/strings/0x409/configuration"
Maksym Sloyko96342732021-08-03 21:54:28 +000077
William A. Kennington IIIf8a77742022-02-28 18:05:54 -080078 local func_dir="${gadget_dir}/functions/${DEV_TYPE}.${IFACE_NAME}"
Maksym Sloyko96342732021-08-03 21:54:28 +000079 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
94gadget_stop() {
95 local gadget_dir="${CONFIGFS_HOME}/usb_gadget/${GADGET_DIR_NAME}"
William A. Kennington IIIf8a77742022-02-28 18:05:54 -080096 rm -f ${gadget_dir}/configs/c.1/${DEV_TYPE}.${IFACE_NAME}
Maksym Sloyko96342732021-08-03 21:54:28 +000097 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 IIIf8a77742022-02-28 18:05:54 -0800100 rm -rf ${gadget_dir}/functions/${DEV_TYPE}.${IFACE_NAME}
Maksym Sloyko96342732021-08-03 21:54:28 +0000101 rm -rf ${gadget_dir}
William A. Kennington IIIa9de0df2022-02-22 17:11:13 -0800102
103 rm -f /run/systemd/network/+-bmc-"${IFACE_NAME}".network
104 networkctl reload
Maksym Sloyko96342732021-08-03 21:54:28 +0000105}
106
107opts=$(getopt \
108 --longoptions "$(printf "%s," "${ARGUMENT_LIST[@]}")" \
109 --name "$(basename "$0")" \
110 --options "" \
111 -- "$@"
112)
113
114eval set --$opts
115
116CONFIGFS_HOME=${CONFIGFS_HOME:-/sys/kernel/config}
117ID_VENDOR="0x18d1" # Google
118ID_PRODUCT=""
119STR_EN_VENDOR="Google"
120STR_EN_PRODUCT=""
121DEV_MAC_ADDR=""
William A. Kennington IIIf8a77742022-02-28 18:05:54 -0800122DEV_TYPE="eem"
Maksym Sloyko96342732021-08-03 21:54:28 +0000123HOST_MAC_ADDR=""
124BIND_DEVICE=""
125ACTION="start"
126GADGET_DIR_NAME="g1"
127IFACE_NAME="usb0"
128while [[ $# -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 IIIf8a77742022-02-28 18:05:54 -0800146 --dev-type)
147 DEV_TYPE=$2
148 shift 2
149 ;;
Maksym Sloyko96342732021-08-03 21:54:28 +0000150 --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
183done
184
185if [[ $ACTION == "stop" ]]; then
186 gadget_stop
187else
William A. Kennington III986d3a62022-03-31 17:58:30 -0700188 rc=0
189 gadget_start || rc=$?
190 (( rc == 0 )) || gadget_stop || true
191 exit $rc
Maksym Sloyko96342732021-08-03 21:54:28 +0000192fi