Received: from list by lists.gnu.org with archive (Exim 4.71) id 1ePSAu-0001yH-9W for mharc-grub-devel@gnu.org; Thu, 14 Dec 2017 06:59:40 -0500 Received: from eggs.gnu.org ([2001:4830:134:3::10]:38001) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1ePSAs-0001y1-2a for grub-devel@gnu.org; Thu, 14 Dec 2017 06:59:39 -0500 Received: from Debian-exim by eggs.gnu.org with spam-scanned (Exim 4.71) (envelope-from ) id 1ePSAo-0007hL-Ug for grub-devel@gnu.org; Thu, 14 Dec 2017 06:59:38 -0500 Received: from mail-wm0-x243.google.com ([2a00:1450:400c:c09::243]:37032) by eggs.gnu.org with esmtps (TLS1.0:RSA_AES_128_CBC_SHA1:16) (Exim 4.71) (envelope-from ) id 1ePSAo-0007gO-NA for grub-devel@gnu.org; Thu, 14 Dec 2017 06:59:34 -0500 Received: by mail-wm0-x243.google.com with SMTP id f140so10900644wmd.2 for ; Thu, 14 Dec 2017 03:59:34 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20161025; h=sender:from:to:cc:subject:date:message-id; bh=qdwQdPgmqtBG3VpuAshMjqrhvW7+sdDhncUA6xeIIZI=; b=FrS1lLxzo5f9ivZoJsBJHTzuROAFS7WuNUnXWsH6AveK89FRkWdd/LQq5C/+87GSVP 4BaRomgdLWMy3CeN8t4lr2LhBL6Xz4WvjkzOxbYgZTea9SP4Yh3TqocNokskkTw9WjF/ xpHiN9lY5YQuwSEiSpKAwLSeN2YQ2IGAOYg7Obap84xmPdRNkNir1VePb8g8O+K/ak9E NVBh6WnVvAtjXkUu5RDeRok/7PUR0vQ92QICqIZc6eg5pVzDWMGOHJZav6xy2OE1VdzE wgKsdGAUXI5rtR8Q4N/qn3HSjz9hLXheUJRX6D1No+tg1dxU2F1ZoWe1lJEHqsyyjBlI ZPQA== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; h=x-gm-message-state:sender:from:to:cc:subject:date:message-id; bh=qdwQdPgmqtBG3VpuAshMjqrhvW7+sdDhncUA6xeIIZI=; b=pLb7tDz8GpaXo9naE0PKvWYyZwsNik43dQTy2Og/ghKEKY72Iql97fEx4GY3cjiniO U2X/D5AyRlfggbgQnlIGRy63ZNqCwWSBfeFweiMGgWwuir3OkwWhZG+Pq+dBH5aoMr/F 0DD08DagpKHvHcdXk/Onrh9w0CLFowFSRLhxbfgMK0zyLWRcLLdCPolE2ic7Ce2L4hnL pOJUe9kg2YHiKjKZXWS6ObJzfYohnx738sLyWAFK9oobw9zb341GVwoS7XU7J7ea+74i OrXq0cV8HP+P+DjFtFvUvR2ySOiv3soUokq2s3V4I9303hAw6amOZwzQquf2O+Oa61+H iNFw== X-Gm-Message-State: AKGB3mJ41QI0KDdeiISxAegNXWv72I3hUV5arGv3PCeYZ/DE1YqwQcsh obeXPAKcJbyyZQDVgZoSTbcUpW1C X-Google-Smtp-Source: ACJfBousQyn8iKCmQzhOrRxD6qZnETyseDgnBuWFuXw5Wr7vTaC8dIgXeyImNlzKvJpVaaCrQzFm3w== X-Received: by 10.28.146.76 with SMTP id u73mr1972427wmd.28.1513252773138; Thu, 14 Dec 2017 03:59:33 -0800 (PST) Received: from localhost.localdomain ([109.231.236.7]) by smtp.gmail.com with ESMTPSA id n17sm6589736wmd.22.2017.12.14.03.59.31 (version=TLS1_2 cipher=ECDHE-RSA-AES128-GCM-SHA256 bits=128/128); Thu, 14 Dec 2017 03:59:32 -0800 (PST) Sender: Carlo Caione From: Carlo Caione To: grub-devel@gnu.org, linux@endlessm.com Cc: Carlo Caione Subject: [PATCH] hexdump: Add parameter to save output to variable Date: Thu, 14 Dec 2017 11:59:08 +0000 Message-Id: <20171214115908.11207-1-carlo@caione.org> X-Mailer: git-send-email 2.14.1 X-detected-operating-system: by eggs.gnu.org: Genre and OS details not recognized. X-Received-From: 2a00:1450:400c:c09::243 X-BeenThere: grub-devel@gnu.org X-Mailman-Version: 2.1.21 Precedence: list List-Id: The development of GNU GRUB List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , X-List-Received-Date: Thu, 14 Dec 2017 11:59:39 -0000 From: Carlo Caione 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 --- 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 #include #include +#include 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