1#!/bin/bash
2# help information
3
4# Provide source directive to shellcheck.
5# shellcheck source=meta-fii/meta-mori/recipes-mori/mori-fw-utility/mori-fw/mori-lib.sh
6source /usr/libexec/mori-fw/mori-lib.sh
7
8function usage_rst() {
9  echo " mori rst [parameter]"
10  echo "        system   --> reset the host"
11  echo "        btn      --> trigger a power button event"
12  echo "        shutdown --> send out shutdown signal to CPU"
13  echo "        display  --> "
14}
15
16function usage_led() {
17  echo " mori led 'attn'/'boot' [parameter]"
18  echo "        on --> turn led on"
19  echo "        off --> turn led off"
20  echo "        toggle --> toggle led"
21  echo "        status --> get status of led"
22}
23
24function usage_uart() {
25  echo " mori uart [parameter]"
26  echo "        host --> show CPU console"
27  echo "        mpro --> show Mpro console"
28  echo "        swhost --> change to CPU console to ttyS1"
29  echo "        swmpro --> change to CPU 0 Mpro console to ttyS3"
30  echo "        swhosthr --> change CPU console to header"
31  echo "        swmprohr --> change Mpro console to header"
32  echo "        display  --> "
33}
34
35function usage_rtc() {
36  echo " mori rtc [parameter]"
37  echo "        lock  --> disable host access to rtc"
38  echo "        unlock  --> enable host access to rtc"
39  echo "        status  --> get status of host accessibility to rtc"
40}
41
42function usage_gpio() {
43  echo " mori gpio [parameter]"
44  echo "        get [GPIO_LINE_NAME] --> get the gpio value of GPIO_LINE_NAME"
45  echo "        set [GPIO_LINE_NAME] [GPIO_VALUE] --> set the gpio of GPIO_LINE_NAME to the value of GPIO_VALUE"
46}
47
48function usage() {
49  echo " mori BMC console system utilities"
50  echo " mori [optional] [parameter]"
51  echo "   rst     --> reset target device"
52  echo "   fw      --> get version"
53  echo "   uart    --> control the uart mux"
54  echo "   led     --> control the leds"
55  echo "   rtc     --> control host access to rtc"
56  echo "   gpio    --> control the gpios"
57}
58
59function reset() {
60  case $1 in
61    system)
62      # S0 system reset
63      echo "System has been reset, host will start booting in a few minutes"
64      ipmitool chassis power reset
65      ;;
66    btn)
67      # virtual power button on
68      set_gpio_ctrl POWER_OUT 0
69      sleep 1
70      set_gpio_ctrl POWER_OUT 1
71      ;;
72    shutdown)
73      # request host shutdown
74      set_gpio_ctrl S0_SHD_REQ  0
75      sleep 3
76      set_gpio_ctrl S0_SHD_REQ  1
77      ;;
78    forceOff)
79      # virtual power button off
80      set_gpio_ctrl POWER_OUT 0
81      sleep 6
82      set_gpio_ctrl POWER_OUT 1
83      ;;
84    display)
85      echo "Virtual AC Reset:     GPIO$(get_gpio_num HOTSWAP)" "State:$(get_gpio_ctrl HOTSWAP)"
86      echo "Virtual Power Button: GPIO$(get_gpio_num POWER_OUT)" "State:$(get_gpio_ctrl POWER_OUT)"
87      echo "S0 System Reset:      GPIO$(get_gpio_num S0_SYSRESET)" "State:$(get_gpio_ctrl S0_SYSRESET)"
88      echo "S0 Shutdown Request:  GPIO$(get_gpio_num S0_SHD_REQ)" "State:$(get_gpio_ctrl S0_SHD_REQ)"
89      ;;
90    *)
91      usage_rst
92    ;;
93  esac
94}
95
96function fw_rev() {
97  BMC_CPLD_VER_FILE="/run/cpld0.version"
98  MB_CPLD_VER_FILE="/run/cpld1.version"
99
100  cmd=$(cat ${BMC_CPLD_VER_FILE})
101  echo " BMC_CPLD: " "${cmd}"
102  cmd=$(cat ${MB_CPLD_VER_FILE})
103  echo " MB_CPLD: " "${cmd}"
104
105  # BMC Version
106
107  # Save VERSION_ID line in string "VERSION_ID=*-Major.Submajor.Minor.Subminor" and
108  # extract the substring after - sign "Major.Submajor.Minor.Subminor"
109  BMCVersion=$(awk '/VERSION_ID/' /etc/os-release | sed "s/.*-//g")
110  # BMCVersion="Major.Submajor.Minor"
111  echo " BMC: ${BMCVersion%.*}"
112
113  #BMC PWR Sequencer
114  i2cset -y -f -a "${I2C_BMC_PWRSEQ[0]}" 0x"${I2C_BMC_PWRSEQ[1]}" 0xfe 0x0000 w
115  cmd=$(i2cget -y -f -a "${I2C_BMC_PWRSEQ[0]}" 0x"${I2C_BMC_PWRSEQ[1]}" 0xfe i 2 | awk '{print substr($0,3)}')
116  echo " BMC PowerSequencer : ${cmd}"
117  #only display with smbios exists
118  if [[ -e /var/lib/smbios/smbios2 ]]; then
119    cmd=$(busctl get-property xyz.openbmc_project.Smbios.MDR_V2 \
120            /xyz/openbmc_project/inventory/system/chassis/motherboard/bios\
121            xyz.openbmc_project.Inventory.Decorator.Revision Version | awk '{print $2}')
122    echo " Bios: $cmd"
123  fi
124
125  if [[ ! $(which mb_power_sequencer_version) ]]; then
126    echo "mb_power_sequencer_version utility not installed"
127    return
128  fi
129
130  mb_power_sequencer_version "${I2C_MB_PWRSEQ[0]}" | grep REVISION
131}
132
133function uartmux() {
134  case $1 in
135    host)
136      if [ "$(tty)" ==  "/dev/ttyS0" ]; then
137        echo "Couldn't redirect to the host console within BMC local console"
138      else
139        echo "Entering Console use 'shift ~~..' to quit"
140        obmc-console-client -c /etc/obmc-console/server.ttyS1.conf
141      fi
142      ;;
143    mpro)
144      if [ "$(tty)" ==  "/dev/ttyS0" ]; then
145        echo "Couldn't redirect to the Mpro console within BMC local console"
146      else
147        echo "Entering Console use 'shift ~~..' to quit"
148        obmc-console-client -c /etc/obmc-console/server.ttyS3.conf
149      fi
150      ;;
151    swhost)
152      set_gpio_ctrl S0_UART0_BMC_SEL 1
153      ;;
154    swmpro)
155      set_gpio_ctrl S0_UART1_BMC_SEL 1
156      ;;
157    swhosthr)
158      set_gpio_ctrl S0_UART0_BMC_SEL 0
159      ;;
160    swmprohr)
161      set_gpio_ctrl S0_UART1_BMC_SEL 0
162      ;;
163    display)
164      if [ "$(get_gpio_ctrl S0_UART0_BMC_SEL)" -eq 1 ]; then
165        echo " CPU host to BMC console"
166      else
167        echo " CPU host to header"
168      fi
169
170      if [ "$(get_gpio_ctrl S0_UART1_BMC_SEL)" -eq 1 ]; then
171        echo " Mpro host to BMC console"
172      else
173        echo " Mpro host to header"
174      fi
175      ;;
176    *)
177      usage_uart
178      ;;
179  esac
180}
181
182function ledtoggle() {
183  case $1 in
184    boot)
185      cmd=$(get_gpio_ctrl SYS_BOOT_STATUS_LED)
186      case $2 in
187        on)
188          #turn on LED
189          set_gpio_ctrl SYS_BOOT_STATUS_LED 1
190          ;;
191        off)
192          #turn off LED
193          set_gpio_ctrl SYS_BOOT_STATUS_LED 0
194          ;;
195        toggle)
196          #toggle off LED
197          if [[ $cmd -eq 1 ]]; then
198            set_gpio_ctrl SYS_BOOT_STATUS_LED 0
199          fi
200
201          #toggle on LED
202          if [[ $cmd -eq 0 ]]; then
203            set_gpio_ctrl SYS_BOOT_STATUS_LED 1
204          fi
205          ;;
206        status)
207          #displayLED status
208          if [[ $cmd -eq 1 ]]; then
209            echo "on"
210          else
211            echo "off"
212          fi
213          ;;
214        *)
215          usage_led
216          ;;
217        esac
218      ;;
219    attn)
220      cmd=$(get_gpio_ctrl SYS_ERROR_LED)
221      case $2 in
222        on)
223          #turn on LED
224          set_gpio_ctrl SYS_ERROR_LED 1
225          ;;
226        off)
227          #turn off LED
228          set_gpio_ctrl SYS_ERROR_LED 0
229          ;;
230        toggle)
231          #toggle off LED
232          if [[ $cmd -eq 1 ]]; then
233            set_gpio_ctrl SYS_ERROR_LED 0
234          fi
235
236          #toggle on LED
237          if [[ $cmd -eq 0 ]]; then
238            set_gpio_ctrl SYS_ERROR_LED 1
239          fi
240          ;;
241        status)
242          #displayLED status
243          if [[ $cmd -eq 1 ]]; then
244            echo "on"
245          else
246            echo "off"
247          fi
248          ;;
249        *)
250          usage_led
251          ;;
252        esac
253      ;;
254    *)
255      usage_led
256      ;;
257    esac
258}
259
260function usblist() {
261  for i in {5..9}
262  do
263    cmd=$(devmem 0xf083"${i}"154)
264    printf "udc%d : 0xF083%d154-" "${i}" "${i}"
265    echo "$cmd"
266  done
267}
268
269function rtcctrl() {
270  case $1 in
271    lock)
272      # Disable host access to rtc
273      set_gpio_ctrl S0_RTC_LOCK 1
274      ;;
275    unlock)
276      # Enable host access to rtc
277      set_gpio_ctrl S0_RTC_LOCK 0
278      ;;
279    status)
280      cmd=$(get_gpio_ctrl S0_RTC_LOCK)
281      if [[ $cmd -eq 1 ]]; then
282        echo "locked"
283      else
284        echo "unlocked"
285      fi
286      ;;
287    *)
288      usage_rtc
289      ;;
290  esac
291}
292
293function gpioctrl() {
294  case $1 in
295    get)
296      get_gpio_ctrl "$2"
297      ;;
298    set)
299      set_gpio_ctrl "$2" "$3"
300      ;;
301    *)
302      usage_gpio
303      ;;
304  esac
305}
306
307case $1 in
308  rst)
309    reset "$2"
310    ;;
311  fw)
312    fw_rev
313    ;;
314  uart)
315    uartmux "$2"
316    ;;
317  usb)
318    usblist
319    ;;
320  led)
321    ledtoggle "$2" "$3"
322    ;;
323  rtc)
324    rtcctrl "$2"
325    ;;
326  gpio)
327    gpioctrl "$2" "$3" "$4"
328    ;;
329  *)
330    usage
331    ;;
332esac
333
334