aboutsummaryrefslogtreecommitdiff
path: root/resources/scripts
diff options
context:
space:
mode:
authorzyliwax <zyliwax@protonmail.com>2017-05-15 05:17:09 +0000
committerzyliwax <zyliwax@protonmail.com>2017-05-15 05:17:09 +0000
commit664e60dd4d5484ab0d64780dd2fd93f75bcd2e1d (patch)
tree971b6ae24bc8c4805925fcea7bd048fcbb435035 /resources/scripts
parentd4fbd345f88f41ff6c61e25d320eabfaa6d74d8e (diff)
downloadlibrebootfr-664e60dd4d5484ab0d64780dd2fd93f75bcd2e1d.tar.gz
librebootfr-664e60dd4d5484ab0d64780dd2fd93f75bcd2e1d.zip
grubeditor.sh
Updates to script: - Fixed broken test/actual config swapper - Updated cbfstool variable to reflect the script's home, not the romfile's home - Updated help screen to use long lines and provide actual usage syntax - Added a version number and included a function that displays it
Diffstat (limited to 'resources/scripts')
-rw-r--r--resources/scripts/misc/grubeditor.sh218
1 files changed, 131 insertions, 87 deletions
diff --git a/resources/scripts/misc/grubeditor.sh b/resources/scripts/misc/grubeditor.sh
index 025161f3..832e0dc6 100644
--- a/resources/scripts/misc/grubeditor.sh
+++ b/resources/scripts/misc/grubeditor.sh
@@ -46,6 +46,13 @@
# -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
+# -u kills the script if any variables are unassigned
+# -e kills the script if any function returns not-zero
+#set -u
+
# Define the list of available option in both short and long form.
shortopts="hrie:sdD:"
longopts="help,realcfg,inplace,editor:,swapcfgs,diffcfgs,differ:"
@@ -154,7 +161,7 @@ determine_architecture() {
case "${arch}" in
armv7l|i686|x86_64)
echo "Supported architecture \"${arch}\" detected. You may proceed."
- cbfstool="./cbfstool/${arch}/cbfstool"
+ cbfstool="${0%/*}/cbfstool/${arch}/cbfstool"
;;
*)
echo "Unsupported architecture \"${arch}\" detected! You may not proceed."
@@ -180,10 +187,108 @@ determine_operation() {
# referenced within them either directly or indirectly from other helper
# functions depending on the operations requested by the user.
+# External command finders.
+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
+}
+
+# Filename randomizer.
+random_filer() {
+ # 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"
+
+ # Hardcoded string size for multiple good reasons (no processing weird
+ # input, prevent malicious overflows, etc.)
+ size=5
+
+ # 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)
+
+ # Build a complete filename with a hardcoded extension.
+ 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}"
+ 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.
+"${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
@@ -192,15 +297,13 @@ automating their extraction with cbfstool and the user's editor of choice.
-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
+-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
+-D | --differ [/path/to/]differ: use /path/to/differ instead of "diff", can be an interactive program like vimdiff
HELPSCREEN
}
@@ -218,8 +321,10 @@ swap_configs() {
# 6. You're done!
# Extract config files from provided romfile.
- ${cbfstool} ${romfile} extract -n grub.cfg -f /tmp/real2test.cfg
- ${cbfstool} ${romfile} extract -n grubtest.cfg -f /tmp/test2real.cfg
+ real2test="$(random_filer "real2test")"
+ test2real="$(random_filer "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
@@ -230,15 +335,15 @@ swap_configs() {
fi
# 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 /tmp/test2real.cfg
- ${cbfstool} ${outfile} add -t raw -n grubtest.cfg -f /tmp/real2test.cfg
+ "${cbfstool}" ${outfile} add -t raw -n grub.cfg -f "${test2real}"
+ "${cbfstool}" ${outfile} add -t raw -n grubtest.cfg -f "${real2test}"
# Delete the tempfiles.
- rm /tmp/test2real.cfg /tmp/real2test.cfg
+ rm "${test2real}" "${real2test}"
}
diff_configs() {
@@ -252,11 +357,11 @@ diff_configs() {
find_differ
# Extract config files from provided romfile.
- ${cbfstool} ${romfile} extract -n grub.cfg -f /tmp/grub_tmpdiff.cfg
- ${cbfstool} ${romfile} extract -n grubtest.cfg -f /tmp/grubtest_tmpdiff.cfg
+ "${cbfstool}" "${romfile}" extract -n grub.cfg -f /tmp/grub_tmpdiff.cfg
+ "${cbfstool}" "${romfile}" extract -n grubtest.cfg -f /tmp/grubtest_tmpdiff.cfg
# Run the differ command with real as first option, test as second option.
- ${use_differ} /tmp/grub_tmpdiff.cfg /tmp/grubtest_tmpdiff.cfg
+ "${use_differ}" /tmp/grub_tmpdiff.cfg /tmp/grubtest_tmpdiff.cfg
}
edit_config() {
@@ -293,19 +398,19 @@ edit_config() {
fi
# Extract the desired configuration file from the romfile.
- tmp_editme="/tmp/${thisconfig%.cfg}_editme.cfg"
- ${cbfstool} ${romfile} extract -n ${thisconfig} -f ${tmp_editme}
+ tmp_editme="$(random_filer "${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.
@@ -318,75 +423,14 @@ edit_config() {
outfile="${romfile}.modified"
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}
- 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
+ rm "${tmp_editme}"
+ rm "${tmp_refcfg}"
fi
}