From f4b95bb1a5f96011c7bb4016dc9d6927ddc592dd Mon Sep 17 00:00:00 2001 From: matt Date: Wed, 5 Apr 2017 06:08:09 -0400 Subject: [PATCH 3/7] Added mattle_opts.cfg --- grub-core/osdep/unix/config.c | 8 +++++-- include/grub/emu/config.h | 1 + util/config.c | 10 ++++++++ util/grub-install.c | 55 +++++++++++++++++++++++++++++++++++++++---- util/grub-mkconfig.in | 10 +++++++- util/grub-mkconfig_lib.in | 23 +++++++++++++++--- 6 files changed, 96 insertions(+), 11 deletions(-) diff --git a/grub-core/osdep/unix/config.c b/grub-core/osdep/unix/config.c index 65effa9..360b3d6 100644 --- a/grub-core/osdep/unix/config.c +++ b/grub-core/osdep/unix/config.c @@ -78,6 +78,10 @@ grub_util_load_config (struct grub_util_config *cfg) if (v && v[0] == 'y' && v[1] == '\0') cfg->is_cryptodisk_enabled = 1; + v = getenv ("GRUB_ENABLE_CRYPTODISK_MATTLE_OPTS"); + if (v && v[0] == 'y' && v[1] == '\0') + cfg->is_cryptodisk_enabled_mattle_opts = 1; + v = getenv ("GRUB_DISTRIBUTOR"); if (v) cfg->grub_distributor = xstrdup (v); @@ -105,8 +109,8 @@ grub_util_load_config (struct grub_util_config *cfg) *ptr++ = *iptr; } - strcpy (ptr, "'; printf \"GRUB_ENABLE_CRYPTODISK=%s\\nGRUB_DISTRIBUTOR=%s\\n\" " - "\"$GRUB_ENABLE_CRYPTODISK\" \"$GRUB_DISTRIBUTOR\""); + strcpy (ptr, "'; printf \"GRUB_ENABLE_CRYPTODISK=%s\\nGRUB_ENABLE_CRYPTODISK_MATTLE_OPTS=%s\\nGRUB_DISTRIBUTOR=%s\\n\" " + "\"$GRUB_ENABLE_CRYPTODISK\" \"$GRUB_ENABLE_CRYPTODISK_MATTLE_OPTS\" \"$GRUB_DISTRIBUTOR\""); argv[2] = script; argv[3] = '\0'; diff --git a/include/grub/emu/config.h b/include/grub/emu/config.h index 875d589..f761005 100644 --- a/include/grub/emu/config.h +++ b/include/grub/emu/config.h @@ -36,6 +36,7 @@ grub_util_get_localedir (void); struct grub_util_config { int is_cryptodisk_enabled; + int is_cryptodisk_enabled_mattle_opts; char *grub_distributor; }; diff --git a/util/config.c b/util/config.c index ebcdd8f..ec429fd 100644 --- a/util/config.c +++ b/util/config.c @@ -42,6 +42,16 @@ grub_util_parse_config (FILE *f, struct grub_util_config *cfg, int simple) cfg->is_cryptodisk_enabled = 1; continue; } + if (grub_strncmp (ptr, "GRUB_ENABLE_CRYPTODISK_MATTLE_OPTS=", + sizeof ("GRUB_ENABLE_CRYPTODISK_MATTLE_OPTS=") - 1) == 0) + { + ptr += sizeof ("GRUB_ENABLE_CRYPTODISK_MATTLE_OPTS=") - 1; + if (*ptr == '"' || *ptr == '\'') + ptr++; + if (*ptr == 'y') + cfg->is_cryptodisk_enabled_mattle_opts = 1; + continue; + } if (grub_strncmp (ptr, "GRUB_DISTRIBUTOR=", sizeof ("GRUB_DISTRIBUTOR=") - 1) == 0) { diff --git a/util/grub-install.c b/util/grub-install.c index 6c89c2b..109a4e2 100644 --- a/util/grub-install.c +++ b/util/grub-install.c @@ -79,6 +79,7 @@ static char *label_color; static char *label_bgcolor; static char *product_version; static int add_rs_codes = 1; +static char *mattle_opts_file = NULL; enum { @@ -109,7 +110,8 @@ enum OPTION_LABEL_FONT, OPTION_LABEL_COLOR, OPTION_LABEL_BGCOLOR, - OPTION_PRODUCT_VERSION + OPTION_PRODUCT_VERSION, + OPTION_MATTLE_OPTS_FILE }; static int fs_probe = 1; @@ -239,6 +241,11 @@ argp_parser (int key, char *arg, struct argp_state *state) install_device = xstrdup (arg); return 0; + case OPTION_MATTLE_OPTS_FILE: + free (mattle_opts_file); + mattle_opts_file = xstrdup (arg); + return 0; + default: return ARGP_ERR_UNKNOWN; } @@ -302,6 +309,8 @@ static struct argp_option options[] = { {"label-color", OPTION_LABEL_COLOR, N_("COLOR"), 0, N_("use COLOR for label"), 2}, {"label-bgcolor", OPTION_LABEL_BGCOLOR, N_("COLOR"), 0, N_("use COLOR for label background"), 2}, {"product-version", OPTION_PRODUCT_VERSION, N_("STRING"), 0, N_("use STRING as product version"), 2}, + {"mattle-opts-file", OPTION_MATTLE_OPTS_FILE, N_("FILE"), 0, + N_("use FILE instead of using %smattle_opts.cfg "), 2}, {0, 0, 0, 0, 0, 0} }; @@ -346,6 +355,8 @@ help_filter (int key, const char *text, void *input __attribute__ ((unused))) free (plats); return ret; } + case OPTION_MATTLE_OPTS_FILE: + return xasprintf (text, GRUB_SYSCONFDIR "/"); case ARGP_KEY_HELP_POST_DOC: return xasprintf (text, program_name, GRUB_BOOT_DIR_NAME "/" GRUB_DIR_NAME); default: @@ -523,16 +534,50 @@ probe_cryptodisk_uuid (grub_disk_t disk) } if (disk->dev->id == GRUB_DISK_DEVICE_CRYPTODISK_ID) { + static struct grub_util_config config; + grub_util_load_config (&config); const char *uuid = grub_util_cryptodisk_get_uuid (disk); + char *mattle_opts_name; /*absolute path for mattle_opts.cfg */ + static FILE *fpmattle; /*file pointer to be used as the local program name for mattle_opts.cfg */ + int k = 0; if (!load_cfg_f) - load_cfg_f = grub_util_fopen (load_cfg, "wb"); + load_cfg_f = grub_util_fopen (load_cfg, "wb"); have_load_cfg = 1; - - fprintf (load_cfg_f, "cryptomount -u %s\n", - uuid); + if (config.is_cryptodisk_enabled_mattle_opts) + { + if (mattle_opts_file != '\0') + mattle_opts_name = grub_strdup (mattle_opts_file); + else + mattle_opts_name = grub_util_path_concat (2, GRUB_SYSCONFDIR, "mattle_opts.cfg"); + fpmattle = grub_util_fopen (mattle_opts_name, "r"); + if (!fpmattle) + { + grub_util_error (_("cannot open `%s': %s, check to make sure %s has been created"), mattle_opts_name, + strerror (errno), mattle_opts_name); + } + while(1) + { + k = fgetc(fpmattle); + if (feof(fpmattle)) + break; + fprintf (load_cfg_f, "%c", k); + } + fclose (fpmattle); + free (mattle_opts_name); + if (mattle_opts_file) + free (mattle_opts_file); + grub_install_push_module ("exfat"); + grub_install_push_module ("fat"); + grub_install_push_module ("search_pt_uuid"); + grub_install_push_module ("search_fs_uuid"); + grub_install_push_module ("part_gpt"); + } + else + fprintf (load_cfg_f, "cryptomount -u %s\n ", uuid); /*This is to keep it like the original unaltered settings*/ } } + static int is_same_disk (const char *a, const char *b) { diff --git a/util/grub-mkconfig.in b/util/grub-mkconfig.in index f8496d2..33a0dbf 100644 --- a/util/grub-mkconfig.in +++ b/util/grub-mkconfig.in @@ -60,6 +60,7 @@ usage () { print_option_help "-o, --output=$(gettext FILE)" "$(gettext "output generated config to FILE [default=stdout]")" print_option_help "-h, --help" "$(gettext "print this message and exit")" print_option_help "-v, --version" "$(gettext "print the version information and exit")" + print_option_help "-m, --mattle-opts-file=$(gettext FILE)" "$(gettext "alternate mattle_opts.cfg FILE")" echo gettext "Report bugs to ."; echo } @@ -93,6 +94,11 @@ do --output=*) grub_cfg=`echo "$option" | sed 's/--output=//'` ;; + -m | --mattle-opts-file) + GRUB_ALT_OPTS_FILE=`argument $option "$@"`; shift;; + --mattle-opts-file=*) + GRUB_ALT_OPTS_FILE=`echo "$option" | sed 's/--mattle-opts-file=//'` + ;; -*) gettext_printf "Unrecognized option \`%s'\n" "$option" 1>&2 usage @@ -187,7 +193,8 @@ export GRUB_DEVICE \ GRUB_FS \ GRUB_FONT \ GRUB_PRELOAD_MODULES \ - GRUB_ACTUAL_DEFAULT + GRUB_ACTUAL_DEFAULT \ + GRUB_ALT_OPTS_FILE # These are optional, user-defined variables. export GRUB_DEFAULT \ @@ -225,6 +232,7 @@ export GRUB_DEFAULT \ GRUB_INIT_TUNE \ GRUB_SAVEDEFAULT \ GRUB_ENABLE_CRYPTODISK \ + GRUB_ENABLE_CRYPTODISK_MATTLE_OPTS \ GRUB_BADRAM \ GRUB_OS_PROBER_SKIP_LIST \ GRUB_DISABLE_SUBMENU diff --git a/util/grub-mkconfig_lib.in b/util/grub-mkconfig_lib.in index 60b31ca..45c9eaa 100644 --- a/util/grub-mkconfig_lib.in +++ b/util/grub-mkconfig_lib.in @@ -145,9 +145,26 @@ prepare_grub_to_access_device () done if [ x$GRUB_ENABLE_CRYPTODISK = xy ]; then - for uuid in `"${grub_probe}" --device $@ --target=cryptodisk_uuid`; do - echo "cryptomount -u $uuid" - done + if [ x$GRUB_ENABLE_CRYPTODISK_MATTLE_OPTS = xy ]; then + if test -n "$GRUB_ALT_OPTS_FILE"; then + if test -f "$GRUB_ALT_OPTS_FILE"; then + #echo "tested good" + while read -r WHOLE_FILE; do echo "$WHOLE_FILE" ; done < $GRUB_ALT_OPTS_FILE + else + gettext_printf "Error: cannot open %s\n" "$GRUB_ALT_OPTS_FILE" 1>&2 + exit 1 + fi + elif test -f "${prefix}/etc/mattle_opts.cfg"; then + while read -r WHOLE_FILE; do echo "$WHOLE_FILE" ; done < ${prefix}/etc/mattle_opts.cfg + else + gettext_printf "Error: cannot open %s\n" "${prefix}/etc/mattle_opts.cfg" 1>&2 + exit 1 + fi + else + for uuid in `"${grub_probe}" --device $@ --target=cryptodisk_uuid`; do + echo "cryptomount -u $uuid" + done + fi fi # If there's a filesystem UUID that GRUB is capable of identifying, use it; -- 2.7.4