From: Carlo Caione <ca...@endlessm.com> In endless we are using hexdump to read into the hiberfil.sys windows file to detect if windows is hybernated or not.
With this patch we introduce a new parameter to hexdump to enable the possibility to save the output to a variable. Signed-off-by: Carlo Caione <ca...@endlessm.com> --- grub-core/commands/hexdump.c | 42 ++++++++++++++++++++++++++++++++++++++++-- 1 file changed, 40 insertions(+), 2 deletions(-) diff --git a/grub-core/commands/hexdump.c b/grub-core/commands/hexdump.c index 4c884b3a1..e6517ad57 100644 --- a/grub-core/commands/hexdump.c +++ b/grub-core/commands/hexdump.c @@ -24,6 +24,7 @@ #include <grub/lib/hexdump.h> #include <grub/extcmd.h> #include <grub/i18n.h> +#include <grub/env.h> GRUB_MOD_LICENSE ("GPLv3+"); @@ -31,6 +32,8 @@ static const struct grub_arg_option options[] = { {"skip", 's', 0, N_("Skip offset bytes from the beginning of file."), 0, ARG_TYPE_INT}, {"length", 'n', 0, N_("Read only LENGTH bytes."), 0, ARG_TYPE_INT}, + {"set", 't', 0, N_("Set a variable to return value."), N_("VARNAME"), + ARG_TYPE_STRING}, {0, 0, 0, 0, 0, 0} }; @@ -42,6 +45,8 @@ grub_cmd_hexdump (grub_extcmd_context_t ctxt, int argc, char **args) grub_ssize_t size, length; grub_disk_addr_t skip; int namelen; + char *var_buf = NULL; + char *var_p = NULL; if (argc != 1) return grub_error (GRUB_ERR_BAD_ARGUMENT, N_("filename expected")); @@ -50,6 +55,14 @@ grub_cmd_hexdump (grub_extcmd_context_t ctxt, int argc, char **args) skip = (state[0].set) ? grub_strtoull (state[0].arg, 0, 0) : 0; length = (state[1].set) ? grub_strtoul (state[1].arg, 0, 0) : 256; + if (state[2].set) + { + var_buf = grub_malloc (length + 1); + if (! var_buf) + return grub_errno; + var_p = var_buf; + } + if (!grub_strcmp (args[0], "(mem)")) hexdump (skip, (char *) (grub_addr_t) skip, length); else if ((args[0][0] == '(') && (args[0][namelen - 1] == ')')) @@ -76,7 +89,13 @@ grub_cmd_hexdump (grub_extcmd_context_t ctxt, int argc, char **args) if (grub_disk_read (disk, sector, ofs, len, buf)) break; - hexdump (skip, buf, len); + if (state[2].set) + { + grub_memcpy (var_p, buf, len); + var_p += len; + } + else + hexdump (skip, buf, len); ofs = 0; skip += len; @@ -101,7 +120,15 @@ grub_cmd_hexdump (grub_extcmd_context_t ctxt, int argc, char **args) unsigned long len; len = ((length) && (size > length)) ? length : size; - hexdump (skip, buf, len); + + if (state[2].set) + { + grub_memcpy (var_p, buf, len); + var_p += len; + } + else + hexdump (skip, buf, len); + skip += len; if (length) { @@ -114,6 +141,17 @@ grub_cmd_hexdump (grub_extcmd_context_t ctxt, int argc, char **args) grub_file_close (file); } + if (state[2].set) + { + grub_ssize_t i; + + *var_p = 0; + for (i = 0; i < length - 1; i++) + var_buf[i] = ((var_buf[i] >= 32) && (var_buf[i] < 127)) ? var_buf[i] : '.'; + + grub_env_set(state[2].arg, var_buf); + } + return 0; } -- 2.14.1 _______________________________________________ Grub-devel mailing list Grub-devel@gnu.org https://lists.gnu.org/mailman/listinfo/grub-devel