hexdump: Add parameter to save output to variable
PATCH 1/1 hexdump: Add parameter to save output to variable
From: Carlo Caione <[email protected]>
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.
--- grub-core/commands/hexdump.c | 42 ++++++++++++++++++++++++++++++++++++++++-- 1 file changed, 40 insertions(+), 2 deletions(-)
grub-core/commands/hexdump.c +40 -2
@@ -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 ; }
Signature · 1 line
2.14.1
Quoted 8 lines · On Thu, Dec 14, 2017, 07:00 Carlo Caione <[email protected]> wrote:
From: Carlo Caione <[email protected]>
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.
Why not use the hibercheck command for this?
Quoted 117 lines
Signed-off-by Carlo Caione <[email protected]>--- grub-core/commands/hexdump.c | 42 ++++++++++++++++++++++++++++++++++++++++-- 1 file changed, 40 insertions(+), 2 deletions(-)grub-core/commands/hexdump.c +5
-0@@ -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"));grub-core/commands/hexdump.c +8
-0@@ -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] == ')'))grub-core/commands/hexdump.c +7
-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;grub-core/commands/hexdump.c +9
-1@@ -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){grub-core/commands/hexdump.c +11
-0@@ -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
[email protected]
https://lists.gnu.org/mailman/listinfo/grub-devel
Quoted 6 lines · Vladimir 'phcoder' Serbinenko
Quoted 3 lines · On Thu, Dec 14, 2017, 07:00 Carlo Caione <[email protected]> wrote:
From: Carlo Caione <[email protected]>
Quoted 7 lines
Quoted 5 lines
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.
Why not use the hibercheck command for this?
You mean grub-file --is-hibernated-hiberfil ? Well, simply because I didn't know that even existed.
I'll use that instead.
Thanks,
Signature · 1 line
Carlo Caione | +44.7384.69.16.04 | Endless