123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461 |
- #!/bin/bash
- [[ "x${DEBUG+set}" = 'xset' ]] && set -v
- geversion="0.1.1"
- shortopts="hrie:sdD:"
- longopts="help,realcfg,inplace,editor:,swapcfgs,diffcfgs,differ:"
- edit_realcfg=0
- edit_inplace=0
- do_swapcfgs=0
- do_diffcfgs=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
- ;;
- --)
-
- 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 $?
- else
- edit_config
- exit $?
- fi
- }
- 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_filer() {
-
-
-
- [[ -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
- }
- 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
- HELPSCREEN
- }
- show_version() {
- echo "${geversion}"
- }
- swap_configs() {
- # Procedure:
- # 1. Call cbfstool twice, once each to extract grub.cfg and grubtest.cfg.
- # 2. If --inplace not specified, copy ${romfile} to ${romfile}.modified and
- # implement remaining steps on this copy. Otherwise, implement remaining
- # steps on ${romfile}.
- # 3. Call cbfstool twice, once each to delete grub.cfg and grubtest.cfg
- # from romfile.
- # 4. Call cbfstool twice, once to embed grubtest.cfg as grub.cfg into
- # romfile and again to embed grub.cfg as grubtest.cfg into romfile.
- # 5. Delete the extracted grub.cfg and grubtest.cfg files.
- # 6. You're done!
-
- real2test="$(random_filer "real2test")"
- test2real="$(random_filer "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
- cp "${romfile}" "${romfile}.modified"
- outfile="${romfile}.modified"
- fi
-
- "${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_filer "grubcfg")"
- testcfg="$(random_filer "testcfg")"
-
- "${cbfstool}" "${romfile}" extract -n grub.cfg -f "${grubcfg}"
- "${cbfstool}" "${romfile}" extract -n grubtest.cfg -f "${testcfg}"
-
- "${use_differ}" "${grubcfg}" "${testcfg}"
-
- rm "${grubcfg}"
- rm "${testcfg}"
- }
- edit_config() {
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
- find_editor
-
- if [[ $edit_realcfg -eq 1 ]]; then
- thisconfig="grub.cfg"
- else
- thisconfig="grubtest.cfg"
- fi
-
- tmp_editme="$(random_filer "${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
- cp "${romfile}" "${romfile}.modified"
- outfile="${romfile}.modified"
- 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
- }
- get_options $@
- determine_architecture
- determine_operation
|