123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558 |
- #!/usr/bin/env bash
- [[ "x${DEBUG+set}" = 'xset' ]] && set -v
- show_help() {
- cat << HELPSCREEN
- $0 -- conveniently edit grub{test}.cfg files in Libreboot
- ROM image files by automating their extraction with cbfstool
- and the user's editor of choice.
- Usage:
- $0 [OPTIONS] [ROMFILE]
- Options:
- -h | --help: show usage help
- -r | --realcfg: generate grub.cfg instead of grubtest.cfg
- -i | --inplace: do not create a modified romfile, instead
- modify the existing file
- -e | --editor [/path/to/]editor: open the cfg file with
- /path/to/editor instead of the value of \$EDITOR
- -s | --swapcfg: swap grub.cfg and grubtest.cfg
- -d | --diffcfg: diff grub.cfg and grubtest.cfg
- -D | --differ [/path/to/]differ: use /path/to/differ instead
- of "diff", can be an interactive program like vimdiff
- -x | --extract: extract either grub.cfg or grubtest.cfg
- depending on -r option
- HELPSCREEN
- }
- # Version number of script
- geversion="0.2.0"
- # Define the list of available option in both short and long form.
- shortopts="hvrie:sdD:x"
- longopts="help,version,realcfg,inplace,editor:,swapcfgs,diffcfgs,differ:,extract"
- # Variables for modifying the program's operation
- edit_realcfg=0
- edit_inplace=0
- do_swapcfgs=0
- do_diffcfgs=0
- do_extract=0
- cbfstool=""
- use_editor=""
- default_editor="vi"
- editor_rawarg=""
- use_differ=""
- default_differ="diff"
- differ_rawarg=""
- romfile=""
- get_options() {
-
- getopt --test > /dev/null
- if [[ $? -ne 4 ]]; then
- echo "Your environment lacks enhanced getopt, so you cannot run this script properly."
- exit 205
- fi
-
- parsedopts=$(getopt --options $shortopts --longoptions $longopts --name "$0" -- "$@")
- if [[ $? -ne 0 ]]; then
- echo "Unrecognized options."
- exit 206
- fi
-
- eval set -- "$parsedopts"
-
- while [[ 1 ]]; do
- case "$1" in
- -h|--help)
- show_help
-
- exit 200
- ;;
- -v|--version)
- show_version
-
- exit 201
- ;;
- -r|--realcfg)
- edit_realcfg=1
- shift
- ;;
- -i|--inplace)
- edit_inplace=1
- shift
- ;;
- -e|--editor)
- editor_rawarg="$2"
- shift 2
- ;;
- -s|--swapcfgs)
- do_swapcfgs=1
- shift
- ;;
- -d|--diffcfgs)
- do_diffcfgs=1
- shift
- ;;
- -D|--differ)
- differ_rawarg="$2"
- shift 2
- ;;
- -x|--extract)
- do_extract=1
- shift
- ;;
- --)
-
- shift
- break
- ;;
- *)
- echo "Something went wrong while interpreting the arguments!"
- echo "I hit \"$1\" and don't know what to do with it."
- exit 209
- ;;
- esac
- done
-
-
-
-
- if [[ $# -ne 1 ]]; then
- echo "You have specified multiple (or no) input files, which is unsupported."
- echo "Please rerun this command with just a single filename to remove any chance for ambiguity."
- exit 210
- fi
- romfile="$1"
- }
- determine_architecture() {
-
-
-
-
- arch="$(uname -m)"
- case "$arch" in
- armv7l|i686|x86_64)
- echo "Supported architecture \"$arch\" detected. You may proceed."
- cbfstool="${0%/*}/cbfstool/$arch/cbfstool"
- ;;
- *)
- echo "Unsupported architecture \"$arch\" detected! You may not proceed."
- exit 230
- ;;
- esac
- }
- determine_operation() {
- if [[ $do_swapcfgs -eq 1 ]]; then
- swap_configs
- exit $?
- elif [[ $do_diffcfgs -eq 1 ]]; then
- diff_configs
- exit $?
- elif [[ $do_extract -eq 1 ]]; then
- extract_config
- exit $?
- else
- edit_config
- exit $?
- fi
- }
- show_version() {
- echo "$0 $geversion"
- }
- find_differ() {
- found_differ=0
- if [[ -n "$differ_rawarg" ]]; then
- which "$differ_rawarg" &> /dev/null
- if [[ $? -eq 0 ]]; then
- echo "Using differ \"$differ_rawarg\"..."
- use_differ="$differ_rawarg"
- found_differ=1
- else
- echo "The provided \"$differ_rawarg\" is not a valid command!"
- echo "Defaulting to $default_differ..."
- use_differ="$default_differ"
- fi
- fi
-
- if [[ $found_differ -eq 1 ]]; then
- return
- else
- echo "Defaulting to $default_differ..."
- use_differ="$default_differ"
- fi
- }
- find_editor() {
- found_editor=0
- if [[ -n "$editor_rawarg" ]]; then
- which "$editor_rawarg" &> /dev/null
- if [[ $? -eq 0 ]]; then
- echo "Using editor \"$editor_rawarg\"..."
- use_editor="$editor_rawarg"
- found_editor=1
- else
- echo "The provided \"$editor_rawarg\" is not a valid command!"
- echo "Defaulting to $default_editor..."
- use_editor="$default_editor"
- fi
- fi
-
- if [[ $found_editor -eq 1 ]]; then
- return
- else
- if [[ -n "$EDITOR" ]]; then
- which "$EDITOR" &> /dev/null
- if [[ $? -ne 0 ]]; then
- echo "Your \$EDITOR is defined as $EDITOR, but is not a valid command!"
- echo "(This is bad. I highly suggest fixing this in your ~/.bashrc.)"
- echo "Defaulting to $default_editor..."
- use_editor="$default_editor"
- else
- echo "Using your defined \$EDITOR \"$EDITOR\"..."
- use_editor="$EDITOR"
- fi
- else
- echo "\$EDITOR blank, defaulting to $default_editor..."
- use_editor="$default_editor"
- fi
- fi
- }
- random_tempcfg() {
-
-
-
- [[ -n "$1" ]] && label="$1" || label="tempfile"
- [[ -n "$2" ]] && savedir="${2%/}" || savedir="/tmp"
-
-
- size=5
-
- while [[ 1 ]]; do
-
- rand="$(cat /dev/urandom | tr -dc 'a-zA-Z' | fold -w $size | head -n 1)"
-
- possible="$savedir/${label}_${rand}.tmp.cfg"
-
- if [[ ! -f "$possible" ]]; then
- echo "$possible"
- break
- fi
- done
- }
- modified_romfile() {
-
-
-
- [[ -n "$1" ]] && fullname="$1" || fullname=""
- [[ -n "$2" ]] && operation="$2" || operation="altered"
-
-
-
- if [[ "$fullname" = *.* ]]; then
- extension="${fullname##*.}"
- else
- extension=""
- fi
-
-
- if [[ -z "$fullname" ]]; then
- dirname="/tmp"
- filename="grubedited"
- else
- dirname="$(dirname "$fullname")"
- if [[ -z "$extension" ]]; then
- filename="$(basename "$fullname")"
- else
- filename="$(basename "$fullname" ".$extension")"
- fi
- fi
-
- while [[ 1 ]]; do
-
- now="$(date +"%Y%m%d_%H%M%S")"
-
- possible="$dirname/${filename}-${now}-${operation}.${extension}"
-
- if [[ ! -f "$possible" ]]; then
- echo "$possible"
- break
- fi
- done
- }
- swap_configs() {
-
-
-
-
-
-
-
-
-
-
-
-
- real2test="$(random_tempcfg "real2test")"
- test2real="$(random_tempcfg "test2real")"
- "$cbfstool" "$romfile" extract -n grub.cfg -f "$real2test"
- "$cbfstool" "$romfile" extract -n grubtest.cfg -f "$test2real"
-
- if [[ $edit_inplace -eq 1 ]]; then
- outfile="$romfile"
- else
- outfile="$(modified_romfile "$romfile" "swapped")"
- cp "$romfile" "$outfile"
- echo "Saving modified romfile to $outfile"
- fi
-
-
-
- sed -i -e 's/Load test configuration (grubtest.cfg)/Load test configuration (grub.cfg)/' "$real2test"
- sed -i -e 's/configfile \/grubtest.cfg/configfile \/grub.cfg/' "$real2test"
- sed -i -e 's/Load test configuration (grub.cfg)/Load test configuration (grubtest.cfg)/' "$test2real"
- sed -i -e 's/configfile \/grub.cfg/configfile \/grubtest.cfg/' "$test2real"
-
- "$cbfstool" "$outfile" remove -n grub.cfg
- "$cbfstool" "$outfile" remove -n grubtest.cfg
-
- "$cbfstool" "$outfile" add -t raw -n grub.cfg -f "$test2real"
- "$cbfstool" "$outfile" add -t raw -n grubtest.cfg -f "$real2test"
-
-
- rm "$test2real" "$real2test"
- }
- diff_configs() {
-
-
-
-
-
-
- find_differ
- grubcfg="$(random_tempcfg "grubcfg")"
- grubtestcfg="$(random_tempcfg "grubtestcfg")"
-
- "$cbfstool" "$romfile" extract -n grub.cfg -f "$grubcfg"
- "$cbfstool" "$romfile" extract -n grubtest.cfg -f "$grubtestcfg"
-
- "$use_differ" "$grubcfg" "$grubtestcfg"
-
- rm "$grubcfg"
- rm "$grubtestcfg"
- }
- edit_config() {
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
- find_editor
-
- if [[ $edit_realcfg -eq 1 ]]; then
- thisconfig="grub.cfg"
- else
- thisconfig="grubtest.cfg"
- fi
-
- tmp_editme="$(random_tempcfg "${thisconfig%.cfg}")"
- "$cbfstool" "$romfile" extract -n "$thisconfig" -f "$tmp_editme"
-
- "$use_editor" "$tmp_editme"
-
- if [[ $? -eq 0 ]]; then
-
- tmp_refcfg="/tmp/${thisconfig%.cfg}_ref.cfg"
- "$cbfstool" "$romfile" extract -n "$thisconfig" -f "$tmp_refcfg"
-
- diff -q "$tmp_editme" "$tmp_refcfg" &> /dev/null
- if [[ $? -ne 0 ]]; then
-
-
-
-
- if [[ $edit_inplace -eq 1 ]]; then
- outfile="$romfile"
- else
- if [[ $edit_realcfg -eq 1 ]]; then
- op="realcfg"
- else
- op="testcfg"
- fi
- outfile="$(modified_romfile "$romfile" "${op}_edited")"
- cp "$romfile" "$outfile"
- echo "Saving modified romfile to $outfile"
- fi
-
- "$cbfstool" "$outfile" remove -n "$thisconfig"
- "$cbfstool" "$outfile" add -t raw -n "$thisconfig" -f "$tmp_editme"
- else
- echo "No changes to config file. Not updating the ROM image."
- fi
-
- rm "$tmp_editme"
- rm "$tmp_refcfg"
- fi
- }
- extract_config() {
-
-
-
- if [[ $edit_realcfg -eq 1 ]]; then
- thisconfig="grub.cfg"
- else
- thisconfig="grubtest.cfg"
- fi
-
- tmp_extractme="$(random_tempcfg "${thisconfig%.cfg}" "$(dirname "$romfile")")"
- "$cbfstool" "$romfile" extract -n "$thisconfig" -f "$tmp_extractme"
- err=$?
- # Determine if cbfstool reported success.
- if [[ $err -ne 0 ]]; then
- echo "cbfstool reported an error ($err). If it succeeded anyway, check $tmp_extractme."
- else
- echo "Extracted $thisconfig from $romfile to file $tmp_extractme."
- fi
- }
- #################################
- #### PRIMARY EXECUTION FLOW #####
- #################################
- # Run through the primary function cascade.
- get_options $@
- determine_architecture
- determine_operation
|