Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
ading2210
GitHub Repository: ading2210/shimboot
Path: blob/main/bootloader/opt/crossystem
596 views
#!/bin/bash

#a much cleaner implemenation of fakemurk's crossystem.sh 

crossystem_old=/tmp/crossystem_old
declare -A crossystem_dict=(
  ["cros_debug"]="1"
  ["dev_boot_legacy"]="0"
  ["dev_boot_signed_only"]="1"
  ["devsw_boot"]="0"
  ["devsw_cur"]="0"
  ["block_devmode"]="1"
  ["mainfw_act"]="A"
  ["mainfw_type"]="normal"
  ["recovery_reason"]="0"
  ["recovery_request"]="0"
  ["recoverysw_boot"]="0"
  ["recoverysw_cur"]="0"
)

dict_get() {
  echo "${crossystem_dict[$1]}"
}

parse_arg() {
  local arg="$1"

  #set values mode - pass this through without changes
  if [[ "$arg" =~ "=" ]]; then
    $crossystem_old "$@"

  #search value mode
  elif [[ "$arg" =~ "?" ]]; then
    key=$(echo $arg | cut -d "?" -f 1)
    query=$(echo $arg | cut -d "?" -f 2)
    if ! [ "${crossystem_dict[$key]}" = "$query" ]; then
      exit 1
    fi
  
  #get value mode
  elif [ "${crossystem_dict[$arg]}" ]; then
    printf "${crossystem_dict[$arg]}"
  
  #value not found - print help and exit
  else
    $crossystem_old --help
    exit 1
  fi
}

crossystem_out="$($crossystem_old)"
#crossystem_out=$(cat data/crossystem.txt)
  
#add crossystem output to the dict
while IFS= read -r line; do
  line=$(echo $line | cut -d "#" -f 1)
  key=$(echo $line | cut -d "=" -f 1 | xargs)
  value=$(echo $line | cut -d "=" -f 2 | xargs)
  if [ -z "${crossystem_dict[$key]}" ]; then
    crossystem_dict["$key"]="$value"
  fi
done <<< "$crossystem_out"

#crossystem called with no arguments, print out all properties
if [ -z "$1" ] || [ "$1" = "--all" ]; then
  keys="${!crossystem_dict[@]}"
  keys="$(echo "$keys" | tr " " "\n" | sort)"
  for key in $keys; do
    value="${crossystem_dict[$key]}"
    echo "$key = $value"
  done
  
  exit 0
fi

for arg in "$@"; do
  parse_arg $arg
done