1b2398200SPatrick Williams#!/bin/bash -e
279f697e0SAnthony Wilson
379f697e0SAnthony Wilsonset -euo pipefail
479f697e0SAnthony Wilson
5189cf248SAnthony WilsonOPTS="bmcstate,bootprogress,chassiskill,chassisoff,chassison,chassisstate,hoststate,\
6a65d30d1SVishwanatha Subbannaosstate,power,poweroff,poweron,state,status,hostrebootoff,hostrebooton,recoveryoff,recoveryon,\
7ad1afe58SAndrew Geisslerbmcrebootoff, bmcrebooton, listbootblock listlogs showlog deletelogs, stopofftargets"
80f35983dSAnthony Wilson
90e044c48SPotin LaiUSAGE="Usage: obmcutil [-h] [--wait] [--verbose] [--id=<INSTANCE_ID>]
100f35983dSAnthony Wilson{$OPTS}"
1179f697e0SAnthony Wilson
1279f697e0SAnthony WilsonINTERFACE_ROOT=xyz.openbmc_project
1379f697e0SAnthony WilsonSTATE_INTERFACE=$INTERFACE_ROOT.State
146d3a2c54SVishwanatha SubbannaCONTROL_INTERFACE=$INTERFACE_ROOT.Control
1579f697e0SAnthony Wilson
1679f697e0SAnthony WilsonOBJECT_ROOT=/xyz/openbmc_project
1779f697e0SAnthony WilsonSTATE_OBJECT=$OBJECT_ROOT/state
186d3a2c54SVishwanatha SubbannaCONTROL_OBJECT=$OBJECT_ROOT/control
1979f697e0SAnthony Wilson
207a787dd7SVishwanatha SubbannaHOST_TIMEOUT_TARGET=obmc-host-timeout@0.target
2184b3b29eSVishwanatha SubbannaHOST_CRASH_TARGET=obmc-host-crash@0.target
227a787dd7SVishwanatha Subbanna
23acf54d08SAnthony Wilson## NOTE: The following global variables are used only in the run_timeout cmd.
24acf54d08SAnthony Wilson## By declaring these globally instead of passing them through the
25acf54d08SAnthony Wilson## intermediary functions, which may not be "best practice", the readability
26acf54d08SAnthony Wilson## and cleanliness of the code should at least be increased.
27acf54d08SAnthony Wilson
28acf54d08SAnthony Wilson# The command passed in to be executed (e.g. poweron/off, status, etc.)
29acf54d08SAnthony Wilson# This will be be used in some instances of error reporting
30acf54d08SAnthony WilsonG_ORIG_CMD=
31acf54d08SAnthony Wilson# The state an interface should be in after executing the requested command.
32acf54d08SAnthony WilsonG_REQUESTED_STATE=
33acf54d08SAnthony Wilson# The query to run during a poweron/off or chassison/off to check that
34acf54d08SAnthony Wilson# the requested state (G_REQUESTED_STATE) of the interface has been reached.
35acf54d08SAnthony WilsonG_QUERY=
36acf54d08SAnthony Wilson# Wait the set period of time for state transitions to be successful before
37acf54d08SAnthony Wilson# continuing on with the program or reporting an error if timeout reached.
38acf54d08SAnthony WilsonG_WAIT=
3960c3ac8cSAndrew Jeffery# Print the journal to the console
4060c3ac8cSAndrew JefferyG_VERBOSE=
410e044c48SPotin Lai# Instance id, default 0
420e044c48SPotin LaiG_INSTANCE_ID="0"
43acf54d08SAnthony Wilson
44d182bff5SPatrick Williamsfunction print_help()
45f3f16fa9SAnthony Wilson{
46f3f16fa9SAnthony Wilson    echo "$USAGE"
47f3f16fa9SAnthony Wilson    echo ""
48f3f16fa9SAnthony Wilson    echo "positional arguments:"
490f35983dSAnthony Wilson    echo "  {$OPTS}"
50f3f16fa9SAnthony Wilson    echo ""
516d3a2c54SVishwanatha Subbanna    echo "Examples:"
526d3a2c54SVishwanatha Subbanna    echo ""
53a65d30d1SVishwanatha Subbanna    echo "obmcutil hostrebootoff Disable auto reboot of Host from Quiesce state"
543191be88SAndrew Geissler    echo "obmcutil hostrebootoffonetime Disable auto reboot of Host from"
553191be88SAndrew Geissler    echo "                              Quiesce state for a single boot"
56a65d30d1SVishwanatha Subbanna    echo "obmcutil hostrebooton   Enable auto reboot of Host from Quiesce state"
57a65d30d1SVishwanatha Subbanna    echo ""
58a65d30d1SVishwanatha Subbanna    echo "obmcutil bmcrebootoff   Disable reboot of BMC"
59a65d30d1SVishwanatha Subbanna    echo "obmcutil bmcrebooton    Enable reboot of BMC"
606d3a2c54SVishwanatha Subbanna    echo ""
6184b3b29eSVishwanatha Subbanna    echo "obmcutil recoveryoff    Disable handling boot watchdog timeout and host crash"
62a65d30d1SVishwanatha Subbanna    echo "                        Also, disable BMC and Host auto reboots"
63a65d30d1SVishwanatha Subbanna    echo ""
6484b3b29eSVishwanatha Subbanna    echo "obmcutil recoveryon     Enable handling boot watchdog timeout and host crash"
65a65d30d1SVishwanatha Subbanna    echo "                        Also, enable BMC and Host auto reboots"
667a787dd7SVishwanatha Subbanna    echo ""
67*24b25a46SAnusha Dathatri    echo "obmcutil recoverystatus Display the status of handling boot watchdog timeout and host crash"
68*24b25a46SAnusha Dathatri    echo "                        and also the status of BMC and Host auto reboots setting"
69*24b25a46SAnusha Dathatri    echo ""
703b7b5619SAndrew Geissler    echo "obmcutil listbootblock  Check for and list any errors blocking the boot"
713b7b5619SAndrew Geissler    echo "                        of the system"
723b7b5619SAndrew Geissler    echo ""
73295ee4fbSAndrew Geissler    echo "obmcutil listlogs       List all phosphor-logging entries on the"
74295ee4fbSAndrew Geissler    echo "                        system"
75295ee4fbSAndrew Geissler    echo ""
76d8c63204SAndrew Geissler    echo "obmcutil showlog <log>  Display details of input log. Format of <log>"
77d8c63204SAndrew Geissler    echo "                        should match listlogs output"
78d8c63204SAndrew Geissler    echo ""
7942f2898dSAndrew Geissler    echo "obmcutil deletelogs     Delete all phosphor-logging entries from"
8042f2898dSAndrew Geissler    echo "                        system"
81ad1afe58SAndrew Geissler    echo "obmcutil stopofftargets Manually stop all obmc targets in power off"
82ad1afe58SAndrew Geissler    echo "                        path"
8342f2898dSAndrew Geissler    echo ""
84d8779cd8SAndrew Geissler    echo "optional arguments (must precede the positional options above):"
85f3f16fa9SAnthony Wilson    echo "  -h, --help          show this help message and exit"
86acf54d08SAnthony Wilson    echo "  -w, --wait          block until state transition succeeds or fails"
8760c3ac8cSAndrew Jeffery    echo "  -v, --verbose       print the journal to stdout if --wait is supplied"
880e044c48SPotin Lai    echo "  -i, -id             instance id, default 0"
89f3f16fa9SAnthony Wilson    exit 0
90f3f16fa9SAnthony Wilson}
91f3f16fa9SAnthony Wilson
92d182bff5SPatrick Williamsfunction run_timeout()
93acf54d08SAnthony Wilson{
94acf54d08SAnthony Wilson    local timeout="$1"; shift
95b2398200SPatrick Williams    local cmd="$*"
9660c3ac8cSAndrew Jeffery    local verbose_child=
9760c3ac8cSAndrew Jeffery
982869a926SAndrew Jeffery    if [ -n "$G_VERBOSE" ]; then
9960c3ac8cSAndrew Jeffery        journalctl -f &
10060c3ac8cSAndrew Jeffery        verbose_child=$!
10160c3ac8cSAndrew Jeffery    fi
102acf54d08SAnthony Wilson
103acf54d08SAnthony Wilson    $cmd
104acf54d08SAnthony Wilson
105acf54d08SAnthony Wilson    # Run a background query for the transition to the expected state
106acf54d08SAnthony Wilson    # This will be killed if the transition doesn't succeed within
107acf54d08SAnthony Wilson    # a timeout period.
108acf54d08SAnthony Wilson    (
109b2398200SPatrick Williams        while ! grep -q "$G_REQUESTED_STATE" <<< "$(handle_cmd "$G_QUERY")" ; do
110acf54d08SAnthony Wilson            sleep 1
111acf54d08SAnthony Wilson        done
112acf54d08SAnthony Wilson    ) &
11360c3ac8cSAndrew Jeffery    wait_child=$!
114acf54d08SAnthony Wilson
115acf54d08SAnthony Wilson    # Could be bad if process is killed before 'timeout' occurs if
116acf54d08SAnthony Wilson    # transition doesn't succeed.
117acf54d08SAnthony Wilson    trap -- "" SIGTERM
118acf54d08SAnthony Wilson
119acf54d08SAnthony Wilson    # Workaround for lack of 'timeout' command.
120acf54d08SAnthony Wilson    (
121b2398200SPatrick Williams        sleep "$timeout"
12260c3ac8cSAndrew Jeffery        kill $wait_child
123acf54d08SAnthony Wilson    ) > /dev/null 2>&1 &
124acf54d08SAnthony Wilson
12560c3ac8cSAndrew Jeffery    if ! wait $wait_child; then
126acf54d08SAnthony Wilson        echo "Unable to confirm '$G_ORIG_CMD' success" \
127acf54d08SAnthony Wilson            "within timeout period (${timeout}s)"
128acf54d08SAnthony Wilson    fi
12960c3ac8cSAndrew Jeffery
1308be70293SAndrew Jeffery    if [ -n "$verbose_child" ]; then
13160c3ac8cSAndrew Jeffery        kill $verbose_child
13260c3ac8cSAndrew Jeffery    fi
133acf54d08SAnthony Wilson}
134acf54d08SAnthony Wilson
135d182bff5SPatrick Williamsfunction run_cmd()
136acf54d08SAnthony Wilson{
137b2398200SPatrick Williams    local cmd="$*";
138acf54d08SAnthony Wilson
139acf54d08SAnthony Wilson    if [ -n "$G_WAIT" ]; then
140b2398200SPatrick Williams        run_timeout "$G_WAIT" "$cmd"
141acf54d08SAnthony Wilson    else
142acf54d08SAnthony Wilson        $cmd
143acf54d08SAnthony Wilson    fi
144acf54d08SAnthony Wilson}
145acf54d08SAnthony Wilson
146d182bff5SPatrick Williamsfunction set_property()
1473ae0a354SAnthony Wilson{
148acf54d08SAnthony Wilson    run_cmd busctl set-property "$@"
1493ae0a354SAnthony Wilson}
1503ae0a354SAnthony Wilson
151d182bff5SPatrick Williamsfunction get_property()
15279f697e0SAnthony Wilson{
153acf54d08SAnthony Wilson    G_WAIT=""
154acf54d08SAnthony Wilson    run_cmd busctl get-property "$@"
15579f697e0SAnthony Wilson}
15679f697e0SAnthony Wilson
157d182bff5SPatrick Williamsfunction state_query()
15879f697e0SAnthony Wilson{
159b2398200SPatrick Williams    local state
160b2398200SPatrick Williams    state=$(get_property "$@" | cut -d '"' -f2)
161b2398200SPatrick Williams    printf "%-20s: %s\n" "$4" "$state"
16279f697e0SAnthony Wilson}
16379f697e0SAnthony Wilson
164d182bff5SPatrick Williamsfunction print_usage_err()
165ea87db40SAnthony Wilson{
166ea87db40SAnthony Wilson    echo "ERROR: $1" >&2
167ea87db40SAnthony Wilson    echo "$USAGE"
168ea87db40SAnthony Wilson    exit 1
169ea87db40SAnthony Wilson}
170ea87db40SAnthony Wilson
171d182bff5SPatrick Williamsfunction mask_systemd_target()
1727a787dd7SVishwanatha Subbanna{
173b2398200SPatrick Williams    target="$*"
174b2398200SPatrick Williams    systemctl mask "$target"
1757a787dd7SVishwanatha Subbanna}
1767a787dd7SVishwanatha Subbanna
177d182bff5SPatrick Williamsfunction unmask_systemd_target()
1787a787dd7SVishwanatha Subbanna{
179b2398200SPatrick Williams    target="$*"
180b2398200SPatrick Williams    systemctl unmask "$target"
1817a787dd7SVishwanatha Subbanna}
1827a787dd7SVishwanatha Subbanna
183*24b25a46SAnusha Dathatrifunction get_systemd_target_state()
184*24b25a46SAnusha Dathatri{
185*24b25a46SAnusha Dathatri    target="$*"
186*24b25a46SAnusha Dathatri    enabled_state=$(systemctl is-enabled "$target")
187*24b25a46SAnusha Dathatri    echo "$enabled_state"
188*24b25a46SAnusha Dathatri}
189*24b25a46SAnusha Dathatri
190d182bff5SPatrick Williamsfunction disable_bmc_reboot()
191a65d30d1SVishwanatha Subbanna{
192a65d30d1SVishwanatha Subbanna    dir="/run/systemd/system/"
193a65d30d1SVishwanatha Subbanna    file="reboot-guard.conf"
194a65d30d1SVishwanatha Subbanna    units=("reboot" "poweroff" "halt")
195a65d30d1SVishwanatha Subbanna
196a65d30d1SVishwanatha Subbanna    for unit in "${units[@]}"; do
197b2398200SPatrick Williams        mkdir -p "${dir}${unit}.target.d"
198b2398200SPatrick Williams        echo -e "[Unit]\nRefuseManualStart=yes" >> "${dir}${unit}.target.d/${file}"
199a65d30d1SVishwanatha Subbanna    done
200a65d30d1SVishwanatha Subbanna}
201a65d30d1SVishwanatha Subbanna
202d182bff5SPatrick Williamsfunction enable_bmc_reboot()
203a65d30d1SVishwanatha Subbanna{
204a65d30d1SVishwanatha Subbanna    dir="/run/systemd/system/"
205a65d30d1SVishwanatha Subbanna    file="reboot-guard.conf"
206a65d30d1SVishwanatha Subbanna    units=("reboot" "poweroff" "halt")
207a65d30d1SVishwanatha Subbanna
208a65d30d1SVishwanatha Subbanna    for unit in "${units[@]}"; do
209b2398200SPatrick Williams        rm -rf "${dir}${unit}.target.d/${file}"
210b2398200SPatrick Williams        rm -rf "${dir}${unit}.target.d"
211a65d30d1SVishwanatha Subbanna    done
212a65d30d1SVishwanatha Subbanna}
213a65d30d1SVishwanatha Subbanna
214*24b25a46SAnusha Dathatrifunction get_bmc_reboot_status()
215*24b25a46SAnusha Dathatri{
216*24b25a46SAnusha Dathatri    dir="/run/systemd/system/"
217*24b25a46SAnusha Dathatri    file="reboot-guard.conf"
218*24b25a46SAnusha Dathatri    units=("reboot" "poweroff" "halt")
219*24b25a46SAnusha Dathatri
220*24b25a46SAnusha Dathatri    # if file do
221*24b25a46SAnusha Dathatri    for unit in "${units[@]}"; do
222*24b25a46SAnusha Dathatri        if [ -e "${dir}${unit}.target.d/${file}" ]; then
223*24b25a46SAnusha Dathatri            echo "off"
224*24b25a46SAnusha Dathatri            return 0
225*24b25a46SAnusha Dathatri        fi
226*24b25a46SAnusha Dathatri    done
227*24b25a46SAnusha Dathatri    echo "on"
228*24b25a46SAnusha Dathatri    return 0
229*24b25a46SAnusha Dathatri}
230*24b25a46SAnusha Dathatri
231*24b25a46SAnusha Dathatrifunction get_host_reboot_status()
232*24b25a46SAnusha Dathatri{
233*24b25a46SAnusha Dathatri    OBJECT=$CONTROL_OBJECT/host$G_INSTANCE_ID/auto_reboot
234*24b25a46SAnusha Dathatri    SERVICE=$(mapper get-service "$OBJECT")
235*24b25a46SAnusha Dathatri    INTERFACE=$CONTROL_INTERFACE.Boot.RebootPolicy
236*24b25a46SAnusha Dathatri    PROPERTY=AutoReboot
237*24b25a46SAnusha Dathatri    output="$(get_property "$SERVICE" "$OBJECT" $INTERFACE $PROPERTY)"
238*24b25a46SAnusha Dathatri    echo "${output//b /}"
239*24b25a46SAnusha Dathatri}
240*24b25a46SAnusha Dathatri
2413b7b5619SAndrew Geissler# will write blocking errors to stdout
242d182bff5SPatrick Williamsfunction check_boot_block_errors()
2433b7b5619SAndrew Geissler{
2443b7b5619SAndrew Geissler    # array of boot block objects
2453b7b5619SAndrew Geissler    blockArray=()
2463b7b5619SAndrew Geissler
2473b7b5619SAndrew Geissler    # Look for any objects under logging that implement the
2483b7b5619SAndrew Geissler    # xyz.openbmc_project.Logging.ErrorBlocksTransition
2493b7b5619SAndrew Geissler    subtree="$(busctl call xyz.openbmc_project.ObjectMapper \
2503b7b5619SAndrew Geissler               /xyz/openbmc_project/object_mapper \
2513b7b5619SAndrew Geissler               xyz.openbmc_project.ObjectMapper \
2523b7b5619SAndrew Geissler               GetSubTree sias "/xyz/openbmc_project/logging/" 0 1 \
2533b7b5619SAndrew Geissler               xyz.openbmc_project.Logging.ErrorBlocksTransition)"
2543b7b5619SAndrew Geissler
2553b7b5619SAndrew Geissler    # remove quotation marks
256b2398200SPatrick Williams    # shellcheck disable=SC2001
257b2398200SPatrick Williams    subtree="$(echo "$subtree" | sed 's/\"//g')"
2583b7b5619SAndrew Geissler
2593b7b5619SAndrew Geissler    for entry in $subtree; do
2603b7b5619SAndrew Geissler        if [[ ${entry} =~ "xyz/openbmc_project/logging/block"* ]]; then
261b2398200SPatrick Williams            blockArray+=( "$entry" )
2623b7b5619SAndrew Geissler        fi
2633b7b5619SAndrew Geissler    done
2643b7b5619SAndrew Geissler
2653b7b5619SAndrew Geissler    # now find associated error log for each boot block error
2663b7b5619SAndrew Geissler    for berror in "${blockArray[@]}"; do
267b2398200SPatrick Williams        assocs="$(busctl call xyz.openbmc_project.Logging "$berror" \
2683b7b5619SAndrew Geissler                  org.freedesktop.DBus.Properties Get \
2693b7b5619SAndrew Geissler                  ss xyz.openbmc_project.Association.Definitions Associations)"
2703b7b5619SAndrew Geissler
2713b7b5619SAndrew Geissler        # remove quotation marks
272b2398200SPatrick Williams        # shellcheck disable=SC2001
273b2398200SPatrick Williams        assocs="$(echo "$assocs" | sed 's/\"//g')"
2743b7b5619SAndrew Geissler
2753b7b5619SAndrew Geissler        for entry in $assocs; do
2763b7b5619SAndrew Geissler            if [[ ${entry} =~ "xyz/openbmc_project/logging/entry"* ]]; then
2773b7b5619SAndrew Geissler                echo "Blocking Error: $entry"
2783b7b5619SAndrew Geissler            fi
2793b7b5619SAndrew Geissler        done
2803b7b5619SAndrew Geissler    done
2813b7b5619SAndrew Geissler}
2823b7b5619SAndrew Geissler
283deb6bb45SAndrew Geissler# helper function to check for boot block errors and notify user
284d182bff5SPatrick Williamsfunction check_and_warn_boot_block()
285deb6bb45SAndrew Geissler{
286deb6bb45SAndrew Geissler    blockingErrors=$(check_boot_block_errors)
287b2398200SPatrick Williams    if [ -n "$blockingErrors" ]; then
288deb6bb45SAndrew Geissler        echo !!!!!!!!!!
289deb6bb45SAndrew Geissler        echo "WARNING! System has blocking errors that will prevent boot"
290deb6bb45SAndrew Geissler        echo "$blockingErrors"
291deb6bb45SAndrew Geissler        echo !!!!!!!!!!
292deb6bb45SAndrew Geissler    fi
293deb6bb45SAndrew Geissler}
294deb6bb45SAndrew Geissler
295295ee4fbSAndrew Geissler# list all phosphor-logging entries
296d182bff5SPatrick Williamsfunction list_logs()
297295ee4fbSAndrew Geissler{
298295ee4fbSAndrew Geissler    # Look for any objects under logging that implement the
299295ee4fbSAndrew Geissler    # xyz.openbmc_project.Logging.Entry
300295ee4fbSAndrew Geissler    busctl -j call xyz.openbmc_project.ObjectMapper \
301295ee4fbSAndrew Geissler        /xyz/openbmc_project/object_mapper \
302295ee4fbSAndrew Geissler        xyz.openbmc_project.ObjectMapper \
303295ee4fbSAndrew Geissler        GetSubTreePaths sias "/xyz/openbmc_project/logging/" 0 1 \
304295ee4fbSAndrew Geissler        xyz.openbmc_project.Logging.Entry
305295ee4fbSAndrew Geissler}
306295ee4fbSAndrew Geissler
307d8c63204SAndrew Geissler# display input log details
308d182bff5SPatrick Williamsfunction show_log()
309d8c63204SAndrew Geissler{
310d8c63204SAndrew Geissler    busctl -j call xyz.openbmc_project.Logging \
311b2398200SPatrick Williams        "$1" \
312d8c63204SAndrew Geissler        org.freedesktop.DBus.Properties \
313d8c63204SAndrew Geissler        GetAll s xyz.openbmc_project.Logging.Entry
314d8c63204SAndrew Geissler}
315d8c63204SAndrew Geissler
31642f2898dSAndrew Geissler# delete all phosphor-logging entries
317d182bff5SPatrick Williamsfunction delete_logs()
31842f2898dSAndrew Geissler{
31942f2898dSAndrew Geissler    busctl call xyz.openbmc_project.Logging \
32042f2898dSAndrew Geissler        /xyz/openbmc_project/logging \
32142f2898dSAndrew Geissler        xyz.openbmc_project.Collection.DeleteAll DeleteAll
32242f2898dSAndrew Geissler}
32342f2898dSAndrew Geissler
324ad1afe58SAndrew Geissler# stop all targets associated with powering off a system
325d182bff5SPatrick Williamsfunction stop_off_targets()
326ad1afe58SAndrew Geissler{
327ad1afe58SAndrew Geissler    systemctl stop \
328ad1afe58SAndrew Geissler        obmc-chassis-powered-off@0.target \
329ad1afe58SAndrew Geissler        obmc-host-stop-pre@0.target \
330ad1afe58SAndrew Geissler        obmc-host-stopped@0.target \
331ad1afe58SAndrew Geissler        obmc-host-stopping@0.target \
332ad1afe58SAndrew Geissler        obmc-power-off@0.target \
333ad1afe58SAndrew Geissler        obmc-power-stop-pre@0.target \
334ad1afe58SAndrew Geissler        obmc-power-stop@0.target
335ad1afe58SAndrew Geissler}
336ad1afe58SAndrew Geissler
337d182bff5SPatrick Williamsfunction handle_cmd()
33879f697e0SAnthony Wilson{
33979f697e0SAnthony Wilson    case "$1" in
3403ae0a354SAnthony Wilson        chassisoff)
3410e044c48SPotin Lai            OBJECT=$STATE_OBJECT/chassis$G_INSTANCE_ID
3428aca0507SAndrew Geissler            SERVICE=$(mapper get-service "$OBJECT")
3433ae0a354SAnthony Wilson            INTERFACE=$STATE_INTERFACE.Chassis
3443ae0a354SAnthony Wilson            PROPERTY=RequestedPowerTransition
3453ae0a354SAnthony Wilson            VALUE=$INTERFACE.Transition.Off
346acf54d08SAnthony Wilson            G_REQUESTED_STATE=$INTERFACE.PowerState.Off
347acf54d08SAnthony Wilson            G_QUERY="chassisstate"
3488aca0507SAndrew Geissler            set_property "$SERVICE" "$OBJECT" $INTERFACE $PROPERTY "s" $VALUE
3493ae0a354SAnthony Wilson            ;;
3503ae0a354SAnthony Wilson        chassison)
351deb6bb45SAndrew Geissler            check_and_warn_boot_block
3520e044c48SPotin Lai            OBJECT=$STATE_OBJECT/chassis$G_INSTANCE_ID
3538aca0507SAndrew Geissler            SERVICE=$(mapper get-service "$OBJECT")
3543ae0a354SAnthony Wilson            INTERFACE=$STATE_INTERFACE.Chassis
3553ae0a354SAnthony Wilson            PROPERTY=RequestedPowerTransition
3563ae0a354SAnthony Wilson            VALUE=$INTERFACE.Transition.On
357acf54d08SAnthony Wilson            G_REQUESTED_STATE=$INTERFACE.PowerState.On
358acf54d08SAnthony Wilson            G_QUERY="chassisstate"
3598aca0507SAndrew Geissler            set_property "$SERVICE" "$OBJECT" $INTERFACE $PROPERTY "s" $VALUE
3603ae0a354SAnthony Wilson            ;;
3613ae0a354SAnthony Wilson        poweroff)
3620e044c48SPotin Lai            OBJECT=$STATE_OBJECT/host$G_INSTANCE_ID
3638aca0507SAndrew Geissler            SERVICE=$(mapper get-service "$OBJECT")
3643ae0a354SAnthony Wilson            INTERFACE=$STATE_INTERFACE.Host
3653ae0a354SAnthony Wilson            PROPERTY=RequestedHostTransition
3663ae0a354SAnthony Wilson            VALUE=$INTERFACE.Transition.Off
367acf54d08SAnthony Wilson            G_REQUESTED_STATE=$INTERFACE.HostState.Off
368acf54d08SAnthony Wilson            G_QUERY="hoststate"
3698aca0507SAndrew Geissler            set_property "$SERVICE" "$OBJECT" $INTERFACE $PROPERTY "s" $VALUE
3703ae0a354SAnthony Wilson            ;;
3713ae0a354SAnthony Wilson        poweron)
372deb6bb45SAndrew Geissler            check_and_warn_boot_block
3730e044c48SPotin Lai            OBJECT=$STATE_OBJECT/host$G_INSTANCE_ID
3748aca0507SAndrew Geissler            SERVICE=$(mapper get-service "$OBJECT")
3753ae0a354SAnthony Wilson            INTERFACE=$STATE_INTERFACE.Host
3763ae0a354SAnthony Wilson            PROPERTY=RequestedHostTransition
3773ae0a354SAnthony Wilson            VALUE=$INTERFACE.Transition.On
378acf54d08SAnthony Wilson            G_REQUESTED_STATE=$INTERFACE.HostState.Running
379acf54d08SAnthony Wilson            G_QUERY="hoststate"
3808aca0507SAndrew Geissler            set_property "$SERVICE" "$OBJECT" $INTERFACE $PROPERTY "s" $VALUE
3813ae0a354SAnthony Wilson            ;;
38279f697e0SAnthony Wilson        bmcstate)
38379f697e0SAnthony Wilson            OBJECT=$STATE_OBJECT/bmc0
38479f697e0SAnthony Wilson            SERVICE=$(mapper get-service $OBJECT)
38579f697e0SAnthony Wilson            INTERFACE=$STATE_INTERFACE.BMC
38679f697e0SAnthony Wilson            PROPERTY=CurrentBMCState
387b2398200SPatrick Williams            state_query "$SERVICE" $OBJECT $INTERFACE $PROPERTY
38879f697e0SAnthony Wilson            ;;
38979f697e0SAnthony Wilson        chassisstate)
3900e044c48SPotin Lai            OBJECT=$STATE_OBJECT/chassis$G_INSTANCE_ID
3918aca0507SAndrew Geissler            SERVICE=$(mapper get-service "$OBJECT")
39279f697e0SAnthony Wilson            INTERFACE=$STATE_INTERFACE.Chassis
39379f697e0SAnthony Wilson            PROPERTY=CurrentPowerState
3948aca0507SAndrew Geissler            state_query "$SERVICE" "$OBJECT" $INTERFACE $PROPERTY
39579f697e0SAnthony Wilson            ;;
39679f697e0SAnthony Wilson        hoststate)
3970e044c48SPotin Lai            OBJECT=$STATE_OBJECT/host$G_INSTANCE_ID
3988aca0507SAndrew Geissler            SERVICE=$(mapper get-service "$OBJECT")
39979f697e0SAnthony Wilson            INTERFACE=$STATE_INTERFACE.Host
40079f697e0SAnthony Wilson            PROPERTY=CurrentHostState
4018aca0507SAndrew Geissler            state_query "$SERVICE" "$OBJECT" $INTERFACE $PROPERTY
40279f697e0SAnthony Wilson            ;;
40386cffd9cSAlexander Filippov        osstate)
4040e044c48SPotin Lai            OBJECT=$STATE_OBJECT/host$G_INSTANCE_ID
4058aca0507SAndrew Geissler            SERVICE=$(mapper get-service "$OBJECT")
40686cffd9cSAlexander Filippov            INTERFACE=$STATE_INTERFACE.OperatingSystem.Status
40786cffd9cSAlexander Filippov            PROPERTY=OperatingSystemState
4088aca0507SAndrew Geissler            state_query "$SERVICE" "$OBJECT" $INTERFACE $PROPERTY
40986cffd9cSAlexander Filippov            ;;
41079f697e0SAnthony Wilson        state|status)
41186cffd9cSAlexander Filippov            for query in bmcstate chassisstate hoststate bootprogress osstate
41279f697e0SAnthony Wilson            do
41379f697e0SAnthony Wilson                handle_cmd $query
41479f697e0SAnthony Wilson            done
415deb6bb45SAndrew Geissler            check_and_warn_boot_block
41679f697e0SAnthony Wilson            ;;
41750c5f88dSAnthony Wilson        bootprogress)
4180e044c48SPotin Lai            OBJECT=$STATE_OBJECT/host$G_INSTANCE_ID
4198aca0507SAndrew Geissler            SERVICE=$(mapper get-service "$OBJECT")
42050c5f88dSAnthony Wilson            INTERFACE=$STATE_INTERFACE.Boot.Progress
42150c5f88dSAnthony Wilson            PROPERTY=BootProgress
4228aca0507SAndrew Geissler            state_query "$SERVICE" "$OBJECT" $INTERFACE $PROPERTY
42350c5f88dSAnthony Wilson            ;;
4240f35983dSAnthony Wilson        power)
4250f35983dSAnthony Wilson            OBJECT=/org/openbmc/control/power0
4260f35983dSAnthony Wilson            SERVICE=$(mapper get-service $OBJECT)
4270f35983dSAnthony Wilson            INTERFACE=org.openbmc.control.Power
428d182bff5SPatrick Williams            for property in pgood state pgood_timeout; do
4290f35983dSAnthony Wilson                # get_property can potentially return several
4300f35983dSAnthony Wilson                # different formats of values, so we do the parsing outside
4310f35983dSAnthony Wilson                # of get_property depending on the query. These queries
4320f35983dSAnthony Wilson                # return 'i VALUE' formatted strings.
433d182bff5SPatrick Williams                STATE=$(get_property "$SERVICE" "$OBJECT" "$INTERFACE" "$property" | sed 's/i[ ^I]*//')
434b2398200SPatrick Williams                printf "%s = %s\n" $property "$STATE"
4350f35983dSAnthony Wilson            done
4360f35983dSAnthony Wilson            ;;
437189cf248SAnthony Wilson        chassiskill)
438189cf248SAnthony Wilson            /usr/libexec/chassiskill
439189cf248SAnthony Wilson            ;;
440a65d30d1SVishwanatha Subbanna        hostrebootoff)
4410e044c48SPotin Lai            OBJECT=$CONTROL_OBJECT/host$G_INSTANCE_ID/auto_reboot
4428aca0507SAndrew Geissler            SERVICE=$(mapper get-service "$OBJECT")
4436d3a2c54SVishwanatha Subbanna            INTERFACE=$CONTROL_INTERFACE.Boot.RebootPolicy
4446d3a2c54SVishwanatha Subbanna            PROPERTY=AutoReboot
4456d3a2c54SVishwanatha Subbanna            VALUE=false
4468aca0507SAndrew Geissler            set_property "$SERVICE" "$OBJECT" $INTERFACE $PROPERTY "b" $VALUE
4476d3a2c54SVishwanatha Subbanna            ;;
4483191be88SAndrew Geissler        hostrebootoffonetime)
4490e044c48SPotin Lai            OBJECT=$CONTROL_OBJECT/host$G_INSTANCE_ID/auto_reboot/one_time
4508aca0507SAndrew Geissler            SERVICE=$(mapper get-service "$OBJECT")
4513191be88SAndrew Geissler            INTERFACE=$CONTROL_INTERFACE.Boot.RebootPolicy
4523191be88SAndrew Geissler            PROPERTY=AutoReboot
4533191be88SAndrew Geissler            VALUE=false
4548aca0507SAndrew Geissler            set_property "$SERVICE" "$OBJECT" $INTERFACE $PROPERTY "b" $VALUE
4553191be88SAndrew Geissler            ;;
456a65d30d1SVishwanatha Subbanna        hostrebooton)
4570e044c48SPotin Lai            OBJECT=$CONTROL_OBJECT/host$G_INSTANCE_ID/auto_reboot
4588aca0507SAndrew Geissler            SERVICE=$(mapper get-service "$OBJECT")
4596d3a2c54SVishwanatha Subbanna            INTERFACE=$CONTROL_INTERFACE.Boot.RebootPolicy
4606d3a2c54SVishwanatha Subbanna            PROPERTY=AutoReboot
4616d3a2c54SVishwanatha Subbanna            VALUE=true
4628aca0507SAndrew Geissler            set_property "$SERVICE" "$OBJECT" $INTERFACE $PROPERTY "b" $VALUE
4636d3a2c54SVishwanatha Subbanna            ;;
464a65d30d1SVishwanatha Subbanna        bmcrebootoff)
465a65d30d1SVishwanatha Subbanna            disable_bmc_reboot
466a65d30d1SVishwanatha Subbanna            ;;
467a65d30d1SVishwanatha Subbanna        bmcrebooton)
468a65d30d1SVishwanatha Subbanna            enable_bmc_reboot
469a65d30d1SVishwanatha Subbanna            ;;
4707a787dd7SVishwanatha Subbanna        recoveryoff)
471a65d30d1SVishwanatha Subbanna            handle_cmd hostrebootoff
472a65d30d1SVishwanatha Subbanna            handle_cmd bmcrebootoff
4737a787dd7SVishwanatha Subbanna            mask_systemd_target $HOST_TIMEOUT_TARGET
47484b3b29eSVishwanatha Subbanna            mask_systemd_target $HOST_CRASH_TARGET
4757a787dd7SVishwanatha Subbanna            ;;
4767a787dd7SVishwanatha Subbanna        recoveryon)
477a65d30d1SVishwanatha Subbanna            handle_cmd hostrebooton
478a65d30d1SVishwanatha Subbanna            handle_cmd bmcrebooton
4797a787dd7SVishwanatha Subbanna            unmask_systemd_target $HOST_TIMEOUT_TARGET
48084b3b29eSVishwanatha Subbanna            unmask_systemd_target $HOST_CRASH_TARGET
4817a787dd7SVishwanatha Subbanna            ;;
482*24b25a46SAnusha Dathatri        recoverystatus)
483*24b25a46SAnusha Dathatri            host_reboot_state=$(get_host_reboot_status)
484*24b25a46SAnusha Dathatri            if [[ $host_reboot_state == "true" ]]; then
485*24b25a46SAnusha Dathatri                host_reboot_status=1
486*24b25a46SAnusha Dathatri            else
487*24b25a46SAnusha Dathatri                host_reboot_status=0
488*24b25a46SAnusha Dathatri            fi
489*24b25a46SAnusha Dathatri
490*24b25a46SAnusha Dathatri            bmc_reboot_state=$(get_bmc_reboot_status)
491*24b25a46SAnusha Dathatri            if [[ $bmc_reboot_state == "on" ]]; then
492*24b25a46SAnusha Dathatri                bmc_reboot_status=1
493*24b25a46SAnusha Dathatri            else
494*24b25a46SAnusha Dathatri                bmc_reboot_status=0
495*24b25a46SAnusha Dathatri            fi
496*24b25a46SAnusha Dathatri
497*24b25a46SAnusha Dathatri            host_timeout_target_state=$(get_systemd_target_state $HOST_TIMEOUT_TARGET)
498*24b25a46SAnusha Dathatri            if [[ $host_timeout_target_state == "masked" ]]; then
499*24b25a46SAnusha Dathatri                host_timeout_status=0
500*24b25a46SAnusha Dathatri            else
501*24b25a46SAnusha Dathatri                host_timeout_status=1
502*24b25a46SAnusha Dathatri            fi
503*24b25a46SAnusha Dathatri
504*24b25a46SAnusha Dathatri            host_crash_target_state=$(get_systemd_target_state $HOST_CRASH_TARGET)
505*24b25a46SAnusha Dathatri            if [[ $host_crash_target_state == "masked" ]]; then
506*24b25a46SAnusha Dathatri                host_crash_status=0
507*24b25a46SAnusha Dathatri            else
508*24b25a46SAnusha Dathatri                host_crash_status=1
509*24b25a46SAnusha Dathatri            fi
510*24b25a46SAnusha Dathatri
511*24b25a46SAnusha Dathatri            if (( host_reboot_status && bmc_reboot_status && host_timeout_status && host_crash_status )); then
512*24b25a46SAnusha Dathatri                echo "recovery: On"
513*24b25a46SAnusha Dathatri            elif (( !host_reboot_status && !bmc_reboot_status && !host_timeout_status && !host_crash_status )); then
514*24b25a46SAnusha Dathatri                echo "recovery: Off"
515*24b25a46SAnusha Dathatri            else
516*24b25a46SAnusha Dathatri                echo "recovery: Undefined"
517*24b25a46SAnusha Dathatri            fi
518*24b25a46SAnusha Dathatri
519*24b25a46SAnusha Dathatri            declare -A status
520*24b25a46SAnusha Dathatri            status[0]="Off"
521*24b25a46SAnusha Dathatri            status[1]="On"
522*24b25a46SAnusha Dathatri
523*24b25a46SAnusha Dathatri            printf "  %-11s: %s\n" "hostReboot" "${status[$host_reboot_status]}"
524*24b25a46SAnusha Dathatri            printf "  %-11s: %s\n" "bmcReboot" "${status[$bmc_reboot_status]}"
525*24b25a46SAnusha Dathatri            printf "  %-11s: %s\n" "hostTimeout" "${status[$host_timeout_status]}"
526*24b25a46SAnusha Dathatri            printf "  %-11s: %s\n" "hostCrash" "${status[$host_crash_status]}"
527*24b25a46SAnusha Dathatri            ;;
5283b7b5619SAndrew Geissler        listbootblock)
5293b7b5619SAndrew Geissler            blockingErrors=$(check_boot_block_errors)
5303b7b5619SAndrew Geissler            if [ -z "$blockingErrors" ]; then
5313b7b5619SAndrew Geissler                echo "No blocking errors present"
5323b7b5619SAndrew Geissler            else
5333b7b5619SAndrew Geissler                echo "$blockingErrors"
5343b7b5619SAndrew Geissler            fi
5353b7b5619SAndrew Geissler            ;;
536295ee4fbSAndrew Geissler        listlogs)
537295ee4fbSAndrew Geissler            list_logs
538295ee4fbSAndrew Geissler            ;;
539d8c63204SAndrew Geissler        showlog)
540b2398200SPatrick Williams            show_log "$2"
541d8c63204SAndrew Geissler            ;;
54242f2898dSAndrew Geissler        deletelogs)
54342f2898dSAndrew Geissler            delete_logs
54442f2898dSAndrew Geissler            ;;
545ad1afe58SAndrew Geissler        stopofftargets)
546ad1afe58SAndrew Geissler            stop_off_targets
547ad1afe58SAndrew Geissler            ;;
54879f697e0SAnthony Wilson        *)
549ea87db40SAnthony Wilson            print_usage_err "Invalid command '$1'"
55079f697e0SAnthony Wilson            ;;
55179f697e0SAnthony Wilson    esac
55279f697e0SAnthony Wilson}
55379f697e0SAnthony Wilson
554d8779cd8SAndrew Geisslershiftcnt=0
555ea87db40SAnthony Wilsonfor arg in "$@"; do
556ea87db40SAnthony Wilson    case $arg in
557acf54d08SAnthony Wilson        -w|--wait)
558acf54d08SAnthony Wilson            G_WAIT=30
559d8779cd8SAndrew Geissler            shiftcnt=$((shiftcnt+1))
560acf54d08SAnthony Wilson            continue
561acf54d08SAnthony Wilson            ;;
562ea87db40SAnthony Wilson        -h|--help)
563ea87db40SAnthony Wilson            print_help
564ea87db40SAnthony Wilson            ;;
56560c3ac8cSAndrew Jeffery        -v|--verbose)
56660c3ac8cSAndrew Jeffery            G_VERBOSE=y
567d8779cd8SAndrew Geissler            shiftcnt=$((shiftcnt+1))
56860c3ac8cSAndrew Jeffery            ;;
5690e044c48SPotin Lai        -i=*|--id=*)
5700e044c48SPotin Lai            G_INSTANCE_ID="${arg#*=}"
5710e044c48SPotin Lai            shiftcnt=$((shiftcnt+1))
5720e044c48SPotin Lai            ;;
573ea87db40SAnthony Wilson        -*)
574ea87db40SAnthony Wilson            print_usage_err "Unknown option: $arg"
575ea87db40SAnthony Wilson            ;;
576ea87db40SAnthony Wilson        *)
577acf54d08SAnthony Wilson            G_ORIG_CMD=$arg
578d8779cd8SAndrew Geissler            # shift out the optional parameters
579d8779cd8SAndrew Geissler            shift $shiftcnt
580d8c63204SAndrew Geissler            # pass all arguments to handle_cmd in case command takes additional
581d8c63204SAndrew Geissler            # parameters
582d8c63204SAndrew Geissler            handle_cmd "$@"
583ea87db40SAnthony Wilson            break
584ea87db40SAnthony Wilson            ;;
585ea87db40SAnthony Wilson    esac
586ea87db40SAnthony Wilsondone
587