libreboot

Unnamed repository; edit this file 'description' to name the repository.
Log | Files | Refs | README

commit a36b0c7ccfad15941905185557cc4129f2f8a860
parent 83fa8757681976e8238fcae848864c4cfab738aa
Author: Leah Rowe <info@minifree.org>
Date:   Wed, 31 May 2017 23:48:23 +0000

Merge branch 'master' of zyliwax/libreboot into master

Diffstat:
docs/grub/grubeditor.md | 105++++++++++++++++++++++++++++++++++++++++++++++---------------------------------
resources/scripts/misc/grubeditor.sh | 389+++++++++++++++++++++++++++++++++++++++++++++++++------------------------------
2 files changed, 304 insertions(+), 190 deletions(-)

diff --git a/docs/grub/grubeditor.md b/docs/grub/grubeditor.md @@ -1,5 +1,4 @@ -GRUB EDITOR -=========== +# GRUB EDITOR Libreboot ROM images now support fluid _grub.cfg_ and _grubtest.cfg_ configuration editing with the grubeditor.sh script! Instead of manually running @@ -16,35 +15,12 @@ you broke the main grub.cfg and know that your grubtest.cfg still works. Lastly, it can also run diff on these two configuration files to show you how they differ. -Introduction -============ +## Requirements -grubeditor.sh takes a number of options, the only one which is required being a -valid Libreboot ROM image that uses the GRUB2 payload and contains both -_grub.cfg_ and _grubtest.cfg_ files. Additional options should come _before_ the -ROM image file on the command line. - -` -You can call the script the `-h` or `--help` option to view a brief summary -of the options available. Consider this guide a more extensive version of this -screen. - -grubeditor.sh supports extended getopt, which means that all options have both a -short and a long form which respectively begin with a single hyphen and a double -hyphen. You can group together several short options with a single hyphen like -`-ris` (or not, like `-r -i -s`). Long options should be written as -standalone arguments. - -Lastly, you can check which version of grubeditor.sh you are using with the -`-v` or `--version` option. - -Requirements -============ - -grubeditor.sh requires an x86, x86_64, or armv7l environment, since these are +grubeditor.sh requires an x86, x86\_64, or armv7l environment, since these are the environments for which cbfstools binaries are provided. Additionally, grubeditor.sh needs a Bash environment with extended getopt functionality that -can run the `diff` command and write to /tmp. Lastly, the script expects to +can run the **diff** command and write to /tmp. Lastly, the script expects to live in the top directory of the Libreboot utilities package so it can properly call cbfstool. @@ -56,10 +32,26 @@ Linux LiveCD of your choice which provides these essentials. Optionally, you should make sure your EDITOR variable is set. Otherwise, grubeditor.sh will default to using vi, which may not exist on your system. You can override this default or the contents of your EDITOR variable using the -`-e` or `--editor` command. +**-e** or **--editor** command. + +## Usage help -Editing configuration files -=========================== +grubeditor.sh takes a number of options, the only one which is required being a +valid Libreboot ROM image that uses the GRUB2 payload and contains both +_grub.cfg_ and _grubtest.cfg_ files. Additional options should come _before_ the +ROM image file on the command line. + +grubeditor.sh supports combining several short options with a single hyphen +like **-ris**, but you can also list them separately like **-r -i -s**. Long +options must always be written as standalone arguments. + +You can use the **-h** or **--help** option to view a brief summary of the +options available. Consider this guide a more extensive version of this screen. + +Lastly, you can check which version of grubeditor.sh you are using with the +**-v** or **--version** option. + +## Editing configuration files Invoked without any arguments except for the ROM image, grubeditor.sh will attempt to extract the _grubtest.cfg_ file from the provided ROM image and @@ -69,33 +61,58 @@ name in the same directory, except that the new ROM file will end with ".modified". You can then flash this ROM image to your platform's BIOS chip. If you would prefer to edit the actual _grub.cfg_ configuration file, use the -`-r` or the `--realcfg` option. Everything else will work the same except -that your editor will open the _grub.cfg_ instead. +**-r** or the **--realcfg** option. Everything else will work the same except +that your editor will open the _grub.cfg_ instead. If you would prefer to overwrite your existing ROM image instead of creating a -new one ending in ".modified" use the `-i` or `--inplace` option. -Naturally, you can combine this option with the `-r/--realcfg` option +new one ending in ".modified" use the **-i** or **--inplace** option. +Naturally, you can combine this option with the **-r/--realcfg** option described above. -Swapping and diffing grub.cfg and grubtest.cfg -============================================== +## Swapping grub.cfg and grubtest.cfg grubeditor.sh supports swapping the _grub.cfg_ and _grubtest.cfg_ configuration -files with the `-s` or `--swap` options. This will create a new ROM image +files with the **-s** or **--swap** option. This will create a new ROM image alongside the existing ROM image ending with ".modified" which has these files swapped. Naturally, you can request this operation overwrite the existing file -instead using the `-i/--inplace` option. +instead using the **-i/--inplace** option. + +Note that the script will automatically modify the "Load test configuration +(grubtest.cfg)" menu entry in both configuration files during this operation. +If this was not done, these entries would end up being self-referential after +the rename, breaking their intended functionality of changing between the +configuration files. -Lastly, grubeditor.sh supports comparing _grub.cfg_ and _grubtest.cfg_ files for -differences with the `-d` or `--diffcfg` option. This uses the diff command +For best results, please do not modify this section without studying the source +code of _grubeditor.sh_ and making sure your edits do not impact the script's +ability to perform this modification + +## Swapping grub.cfg and grubtest.cfg + +grubeditor.sh supports comparing _grub.cfg_ and _grubtest.cfg_ files for +differences with the **-d** or **--diffcfg** option. This uses the diff command by default, but if you want to use another program (e.g. vimdiff), you can -specify it with the `-D` or `--differ` option. Note that this mode is only +specify it with the **-D** or **--differ** option. Note that this mode is only intended to show differences in the files and does not support updating the configs themselves, so any changes you make in an interactive differ will be ignored. -Conclusion -========== +## Extracting a configuration file + +You can simply extract a configuration file using the **-x** or **--extract** +option. This option is responsive to the **-r/--realcfg** option for choosing +between grubtest.cfg and grub.cfg. + +## Pending development + +TODO: +- allow injecting configuration files to complement the extractor. +- detect potentially devastating corner cases, however rare they may actually be +- i can't specify a quoted command line with arguments to -e or -D, why? +- support editing both config files if using an interactive differ +- work with other types of files besides the grub configuration files. + +## Conclusion I hope that grubeditor.sh will significantly ease modifying your configuration files in your Libreboot ROM files. diff --git a/resources/scripts/misc/grubeditor.sh b/resources/scripts/misc/grubeditor.sh @@ -19,34 +19,6 @@ # along with this program. If not, see <http://www.gnu.org/licenses/>. # -# Usage: -# ./grubeditor.sh [options] romimage -# -# Supported 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 $EDITOR -# -# -s | --swapcfg: swap grub.cfg and grubtest.cfg, incompatible with other -# options besides -i -# -# -x | --extractcfg: extract either grub.cfg or grubtest.cfg depending on -# whether -r is set -# -# -d | --diffcfg: diff grub.cfg and grubtest.cfg, incompatible with other -# options besides -D -# -# -D | --differ [/path/to/]differ: use /path/to/differ instead of "diff", can -# be an interactive program like vimdiff -# - # THIS BLOCK IS EXPERIMENTAL # Allow debugging by running DEBUG= ${0}. [[ "x${DEBUG+set}" = 'xset' ]] && set -v @@ -54,18 +26,57 @@ # -e kills the script if any function returns not-zero #set -u +############## +# HELP COMMAND +############## + +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.1.1" +geversion="0.2.0" # Define the list of available option in both short and long form. -shortopts="hrie:sdD:" -longopts="help,realcfg,inplace,editor:,swapcfgs,diffcfgs,differ:" +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 # Path to cbfstool, filled by detect_architecture # (Possible to provide explicitly and disclaim warranty?) cbfstool="" @@ -80,7 +91,18 @@ differ_rawarg="" # Last but not least, the rom file itself romfile="" -# This program works primarily from a cascade of functions. Let's define them. +############################ +#### PRIMARY FUNCTIONS ##### +############################ +# The script effectively lives in a series of function definitions, which are +# provided here before their calls to ensure that they have been declared. +# +# Please scroll to the bottom of the script to see how this cascade of +# functions gets initiated. + +################ +# OPTION SCRAPER +################ get_options() { # Test for enhanced getopt. @@ -91,7 +113,7 @@ get_options() { fi # Parse the command line options based on the previously defined values. - parsedopts=$(getopt --options ${shortopts} --longoptions ${longopts} --name "${0}" -- "$@") + parsedopts=$(getopt --options $shortopts --longoptions $longopts --name "$0" -- "$@") if [[ $? -ne 0 ]]; then # getopt didn't approve of your arguments echo "Unrecognized options." exit 206 @@ -137,6 +159,10 @@ get_options() { differ_rawarg="$2" shift 2 ;; + -x|--extract) + do_extract=1 + shift + ;; --) # Stop interpreting arguments magically. shift @@ -144,7 +170,7 @@ get_options() { ;; *) echo "Something went wrong while interpreting the arguments!" - echo "I hit \"${1}\" and don't know what to do with it." + echo "I hit \"$1\" and don't know what to do with it." exit 209 ;; esac @@ -167,13 +193,13 @@ determine_architecture() { # is over, the variable $cbfstool gets filled with the appropriate value # for use by the rest of the script. arch="$(uname -m)" - case "${arch}" in + case "$arch" in armv7l|i686|x86_64) - echo "Supported architecture \"${arch}\" detected. You may proceed." - cbfstool="${0%/*}/cbfstool/${arch}/cbfstool" + echo "Supported architecture \"$arch\" detected. You may proceed." + cbfstool="${0%/*}/cbfstool/$arch/cbfstool" ;; *) - echo "Unsupported architecture \"${arch}\" detected! You may not proceed." + echo "Unsupported architecture \"$arch\" detected! You may not proceed." exit 230 ;; esac @@ -186,85 +212,98 @@ determine_operation() { elif [[ $do_diffcfgs -eq 1 ]]; then diff_configs exit $? + elif [[ $do_extract -eq 1 ]]; then + extract_config + exit $? else edit_config exit $? fi } -# These functions are not part of the primary function cascade but are -# referenced within them either directly or indirectly from other helper -# functions depending on the operations requested by the user. +################ +# VERSION SHOWER +################ + +show_version() { + echo "$0 $geversion" +} + +########################## +# EXTERNAL COMMAND FINDERS +########################## -# External command finders. find_differ() { found_differ=0 - if [[ -n "${differ_rawarg}" ]]; then - which "${differ_rawarg}" &> /dev/null + if [[ -n "$differ_rawarg" ]]; then + which "$differ_rawarg" &> /dev/null if [[ $? -eq 0 ]]; then - echo "Using differ \"${differ_rawarg}\"..." - use_differ="${differ_rawarg}" + 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}" + 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}" + 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 [[ -n "$editor_rawarg" ]]; then + which "$editor_rawarg" &> /dev/null if [[ $? -eq 0 ]]; then - echo "Using editor \"${editor_rawarg}\"..." - use_editor="${editor_rawarg}" + 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}" + 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 [[ -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 "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}" + echo "Defaulting to $default_editor..." + use_editor="$default_editor" else echo "Using your defined \$EDITOR \"$EDITOR\"..." - use_editor="${EDITOR}" + use_editor="$EDITOR" fi else - echo "\$EDITOR blank, defaulting to ${default_editor}..." - use_editor="${default_editor}" + echo "\$EDITOR blank, defaulting to $default_editor..." + use_editor="$default_editor" fi fi } -# Filename randomizer. -random_filer() { +####################### +# FILE NAMING FUNCTIONS +####################### + +random_tempcfg() { # Inputs: # $1 is a descriptive label for the file # $2 is directory (becomes /tmp if not set) - [[ -n "${1}" ]] && label="${1}" || label="tempfile" - [[ -n "${2}" ]] && savedir="${2%/}" || savedir="/tmp" + [[ -n "$1" ]] && label="$1" || label="tempfile" + [[ -n "$2" ]] && savedir="${2%/}" || savedir="/tmp" # Hardcoded string size for multiple good reasons (no processing weird # input, prevent malicious overflows, etc.) @@ -272,60 +311,76 @@ random_filer() { # Loop forever until a free filename is found. while [[ 1 ]]; do - # Read data from /dev/urandom and convert into random ASCII strings. - rand=$(cat /dev/urandom | tr -dc 'a-zA-Z' | fold -w $size | head -n 1) + rand="$(cat /dev/urandom | tr -dc 'a-zA-Z' | fold -w $size | head -n 1)" # Build a complete filename with a hardcoded extension. - possible="${savedir}/${label}_${rand}.tmp.cfg" + possible="$savedir/${label}_${rand}.tmp.cfg" # See if file doesn't exist and return it as string or keep going. - if [[ ! -f "${possible}" ]]; then - echo "${possible}" + if [[ ! -f "$possible" ]]; then + echo "$possible" break fi - done } -# Primary command functions. -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 +modified_romfile() { + # Inputs: + # $1 is the path to the file, optional + # $2 provides an optional description of the modification + [[ -n "$1" ]] && fullname="$1" || fullname="" + [[ -n "$2" ]] && operation="$2" || operation="altered" + + # Attempt to extract an extension from the file (it's probably .rom but + # can't assume so). $extension will not include prefixing dot, just the + # extension itself! + if [[ "$fullname" = *.* ]]; then + extension="${fullname##*.}" + else + extension="" + fi --e | --editor [/path/to/]editor: open the cfg file with /path/to/editor instead of the value of \$EDITOR + # Break up full path into containing directory and raw filename, providing + # an attempt at graceful failure if they are absent. + 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 --s | --swapcfg: swap grub.cfg and grubtest.cfg + # Loop forever until a free filename is found. + while [[ 1 ]]; do + # Grab the current date. + now="$(date +"%Y%m%d_%H%M%S")" --d | --diffcfg: diff grub.cfg and grubtest.cfg + # Build a complete filename with a hardcoded extension. + possible="$dirname/${filename}-${now}-${operation}.${extension}" --D | --differ [/path/to/]differ: use /path/to/differ instead of "diff", can be an interactive program like vimdiff -HELPSCREEN + # See if file doesn't exist and return it as string or keep going. + if [[ ! -f "$possible" ]]; then + echo "$possible" + break + fi + done } -show_version() { - echo "${geversion}" -} +########################### +# PRIMARY PROGRAM FUNCTIONS +########################### 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}. + # 2. If --inplace not specified, copy $romfile to a new file 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 @@ -334,74 +389,83 @@ swap_configs() { # 6. You're done! # Extract config files from provided romfile. - real2test="$(random_filer "real2test")" - test2real="$(random_filer "test2real")" - "${cbfstool}" "${romfile}" extract -n grub.cfg -f "${real2test}" - "${cbfstool}" "${romfile}" extract -n grubtest.cfg -f "${test2real}" + real2test="$(random_tempcfg "real2test")" + test2real="$(random_tempcfg "test2real")" + "$cbfstool" "$romfile" extract -n grub.cfg -f "$real2test" + "$cbfstool" "$romfile" extract -n grubtest.cfg -f "$test2real" # Determine whether to edit inplace or make a copy. if [[ $edit_inplace -eq 1 ]]; then - outfile="${romfile}" + outfile="$romfile" else - cp "${romfile}" "${romfile}.modified" - outfile="${romfile}.modified" + outfile="$(modified_romfile "$romfile" "swapped")" + cp "$romfile" "$outfile" + echo "Saving modified romfile to $outfile" fi + # Since we are swapping the configs, we must modify their "load test config" + # options or else they will simply reference themselves, rendering the user + # unable to (easily) load the other config. + 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" + # Remove config files from the output file. - "${cbfstool}" "${outfile}" remove -n grub.cfg - "${cbfstool}" "${outfile}" remove -n grubtest.cfg + "$cbfstool" "$outfile" remove -n grub.cfg + "$cbfstool" "$outfile" remove -n grubtest.cfg # Embed new configs into the output file. - "${cbfstool}" ${outfile} add -t raw -n grub.cfg -f "${test2real}" - "${cbfstool}" ${outfile} add -t raw -n grubtest.cfg -f "${real2test}" + "$cbfstool" "$outfile" add -t raw -n grub.cfg -f "$test2real" + "$cbfstool" "$outfile" add -t raw -n grubtest.cfg -f "$real2test" # Delete the tempfiles. - rm "${test2real}" "${real2test}" + rm "$test2real" "$real2test" } diff_configs() { # Procedure: # 1. Call cbfstool twice, once to extract grub.cfg and grubtest.cfg. - # 2. Execute ${use_differ} grub.cfg grubtest.cfg #. + # 2. Execute $use_differ grub.cfg grubtest.cfg #. # 3. Delete the extracted grub.cfg and grubtest.cfg files. # 4. You're done! # Determine the differ command to use. find_differ - grubcfg="$(random_filer "grubcfg")" - testcfg="$(random_filer "testcfg")" + grubcfg="$(random_tempcfg "grubcfg")" + grubtestcfg="$(random_tempcfg "grubtestcfg")" # Extract config files from provided romfile. - "${cbfstool}" "${romfile}" extract -n grub.cfg -f "${grubcfg}" - "${cbfstool}" "${romfile}" extract -n grubtest.cfg -f "${testcfg}" + "$cbfstool" "$romfile" extract -n grub.cfg -f "$grubcfg" + "$cbfstool" "$romfile" extract -n grubtest.cfg -f "$grubtestcfg" # Run the differ command with real as first option, test as second option. - "${use_differ}" "${grubcfg}" "${testcfg}" + "$use_differ" "$grubcfg" "$grubtestcfg" # Delete the temporary copies of the configuration files. - rm "${grubcfg}" - rm "${testcfg}" + rm "$grubcfg" + rm "$grubtestcfg" } edit_config() { # Procedure: - # 1. If --realcfg specified, set ${thisconfig} to "grub.cfg". Otherwise, - # set ${thisconfig} to "grubtest.cfg". - # 2. Call cbfstool once to extract ${thisconfig} from ${romfile}. - # 3. Run ${use_editor} ${thisconfig}. - # 4. If ${use_editor} returns zero, proceed with update procedure: - # 5. Call cbfstool once to extract ${thisconfig} from ${romfile}. + # 1. If --realcfg specified, set $thisconfig to "grub.cfg". Otherwise, + # set $thisconfig to "grubtest.cfg". + # 2. Call cbfstool once to extract $thisconfig from $romfile. + # 3. Run $use_editor $thisconfig. + # 4. If $use_editor returns zero, proceed with update procedure: + # 5. Call cbfstool once to extract $thisconfig from $romfile. # 6. Quietly diff the extracted file with the edited file. If diff returns # zero, take no action: warn the user that the files were the same, delete # both files, then skip the remaining steps (you're done)! Otherwise, the # files are different and you must continue with the update procedure. - # 7. If --inplace not specified, copy ${romfile} to ${romfile}.modified and + # 7. If --inplace not specified, copy $romfile to a new filename and # implement remaining steps on this copy. Otherwise, implement remaining - # steps on ${romfile}. - # 8. Call cbfstool once to delete internal pre-update ${thisconfig} from + # steps on $romfile. + # 8. Call cbfstool once to delete internal pre-update $thisconfig from # the rom file. - # 9. Call cbfstool once to embed the updated ${thisconfig} that was just + # 9. Call cbfstool once to embed the updated $thisconfig that was just # edited into the rom file. # 10. Alert the user of success (either explicitly or by not saying # anything, either way return zero). @@ -418,42 +482,75 @@ edit_config() { fi # Extract the desired configuration file from the romfile. - tmp_editme="$(random_filer "${thisconfig%.cfg}")" - "${cbfstool}" "${romfile}" extract -n "${thisconfig}" -f "${tmp_editme}" + tmp_editme="$(random_tempcfg "${thisconfig%.cfg}")" + "$cbfstool" "$romfile" extract -n "$thisconfig" -f "$tmp_editme" # Launch the editor! - "${use_editor}" "${tmp_editme}" + "$use_editor" "$tmp_editme" # Did the user commit the edit? if [[ $? -eq 0 ]]; then # See if it actually changed from what exists in the cbfs. tmp_refcfg="/tmp/${thisconfig%.cfg}_ref.cfg" - "${cbfstool}" "${romfile}" extract -n "${thisconfig}" -f "${tmp_refcfg}" + "$cbfstool" "$romfile" extract -n "$thisconfig" -f "$tmp_refcfg" # Diff the files as quietly as possible. - diff -q "${tmp_editme}" "${tmp_refcfg}" &> /dev/null + diff -q "$tmp_editme" "$tmp_refcfg" &> /dev/null if [[ $? -ne 0 ]]; then # The files differ, so it won't be frivolous to update the config. # See if the user wants to edit the file in place. # (This code should really be genericized and placed in a function # to avoid repetition.) if [[ $edit_inplace -eq 1 ]]; then - outfile="${romfile}" + outfile="$romfile" else - cp "${romfile}" "${romfile}.modified" - outfile="${romfile}.modified" + 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 # Remove the old config, add in the new one. - "${cbfstool}" "${outfile}" remove -n "${thisconfig}" - "${cbfstool}" "${outfile}" add -t raw -n "${thisconfig}" -f "${tmp_editme}" + "$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 # We are done with the config files. Delete them. - rm "${tmp_editme}" - rm "${tmp_refcfg}" + rm "$tmp_editme" + rm "$tmp_refcfg" + fi +} + +extract_config() { + # This simply extracts a given config and responds to + # the -r flag. + + # Determine whether we are extracting the real config or the test config. + if [[ $edit_realcfg -eq 1 ]]; then + thisconfig="grub.cfg" + else + thisconfig="grubtest.cfg" + fi + + # Extract to a unique filename. + 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