1#!/bin/sh 2# 3# This file is part of the flashrom project. It is derived from 4# board_status.sh in coreboot. 5# 6# Copyright (C) 2016 Google Inc. 7# Copyright (C) 2014 Sage Electronic Engineering, LLC. 8 9USE_CUSTOM_HOOKS=0 10if [ -n "$CUSTOM_HOOKS_FILENAME" ]; then 11 USE_CUSTOM_HOOKS=1 12 . "$CUSTOM_HOOKS_FILENAME" 13 if [ $? -ne 0 ]; then 14 echo "Failed to source custom hooks file." 15 exit $EXIT_FAILURE 16 fi 17 18 if ! custom_hook_sanity_check; then 19 echo "Failed to run sanity check for custom hooks." 20 exit $EXIT_FAILURE 21 fi 22fi 23 24# test a command 25# 26# $1: 0 ($LOCAL) to run command locally, 27# 1 ($REMOTE) to run remotely if remote host defined 28# $2: command to test 29# $3: 0 ($FATAL) Exit with an error if the command fails 30# 1 ($NONFATAL) Don't exit on command test failure 31test_cmd() 32{ 33 local rc 34 local cmd__="$(echo $2 | cut -d ' ' -f -1)" 35 local args="$(echo $2 | cut -d ' ' -f 2-)" 36 37 if [ -e "$cmd__" ]; then 38 return 39 fi 40 41 if [ "$1" -eq "$REMOTE" ] && [ -n "$REMOTE_HOST" ]; then 42 ssh $REMOTE_PORT_OPTION root@${REMOTE_HOST} command -v "$cmd__" $args > /dev/null 2>&1 43 rc=$? 44 else 45 command -v "$cmd__" $args >/dev/null 2>&1 46 rc=$? 47 fi 48 49 if [ $rc -eq 0 ]; then 50 return 0 51 fi 52 53 if [ "$3" = "1" ]; then 54 return 1 55 fi 56 57 echo "$2 not found" 58 exit $EXIT_FAILURE 59} 60 61# Same args as cmd() but with the addition of $4 which determines if the 62# command should be totally silenced or not. 63_cmd() 64{ 65 local silent=$4 66 local rc=0 67 68 if [ -n "$3" ]; then 69 pipe_location="${3}" 70 else 71 pipe_location="/dev/null" 72 fi 73 74 if [ $1 -eq $REMOTE ] && [ -n "$REMOTE_HOST" ]; then 75 if [ $silent -eq 0 ]; then 76 ssh $REMOTE_PORT_OPTION "root@${REMOTE_HOST}" "$2" > "$pipe_location" 2>/dev/null 77 rc=$? 78 else 79 ssh $REMOTE_PORT_OPTION "root@${REMOTE_HOST}" "$2" >/dev/null 2>&1 80 rc=$? 81 fi 82 else 83 if [ $USE_CUSTOM_HOOKS -eq 1 ]; then 84 preflash_hook $1 "$2" "$3" $4 85 fi 86 87 if [ $silent -eq 0 ]; then 88 $SUDO_CMD $2 > "$pipe_location" 2>/dev/null 89 rc=$? 90 else 91 $SUDO_CMD $2 >/dev/null 2>&1 92 rc=$? 93 fi 94 95 if [ $USE_CUSTOM_HOOKS -eq 1 ]; then 96 postflash_hook $1 "$2" "$3" $4 97 fi 98 fi 99 100 return $rc 101} 102 103# run a command 104# 105# $1: 0 ($LOCAL) to run command locally, 106# 1 ($REMOTE) to run remotely if remote host defined 107# $2: command 108# $3: filename to direct output of command into 109cmd() 110{ 111 _cmd $1 "$2" "$3" 0 112 113 if [ $? -eq 0 ]; then 114 return 115 fi 116 117 echo "Failed to run \"$2\", aborting" 118 rm -f "$3" # don't leave an empty file 119 return $EXIT_FAILURE 120} 121 122# run a command silently 123# 124# $1: 0 ($LOCAL) to run command locally, 125# 1 ($REMOTE) to run remotely if remote host defined 126# $2: command 127scmd() 128{ 129 _cmd $1 "$2" "" 1 130 131 if [ $? -eq 0 ]; then 132 return 133 fi 134 135 echo "Failed to run \"$2\", aborting" 136 return $EXIT_FAILURE 137} 138