grub-devel.gnu.org archive mirror
 help / color / mirror / Atom feed
From: Michael Chang via Grub-devel <grub-devel@gnu.org>
To: The development of GNU GRUB <grub-devel@gnu.org>
Cc: Michael Chang <mchang@suse.com>, Neal Gompa <ngompa13@gmail.com>,
	Marta Lewandowska <mlewando@redhat.com>
Subject: [PATCH v2 3/9] util/grub-editenv: add fs_envblk write helper
Date: Mon, 15 Sep 2025 17:08:42 +0800	[thread overview]
Message-ID: <20250915090848.131937-4-mchang@suse.com> (raw)
In-Reply-To: <20250915090848.131937-1-mchang@suse.com>

This patch adds the function fs_envblk_write to update the reserved
environment block on disk. The helper takes an in memory envblk buffer
and writes it back to the device at the location defined by the
fs_envblk specification. It performs size checks and uses file sync to
ensure that the updated data is flushed.

The helper is also added into the fs_envblk ops table, together with the
open helper from the previous patch. With this change the basic input
and output path for an external environment block is complete. The
choice of which variables should be written externally will be handled
by later patches.

Signed-off-by: Michael Chang <mchang@suse.com>
---
 util/grub-editenv.c | 38 +++++++++++++++++++++++++++++++++++++-
 1 file changed, 37 insertions(+), 1 deletion(-)

diff --git a/util/grub-editenv.c b/util/grub-editenv.c
index 4e5dffa86..26a81d2d0 100644
--- a/util/grub-editenv.c
+++ b/util/grub-editenv.c
@@ -131,9 +131,11 @@ struct fs_envblk_spec {
 typedef struct fs_envblk_spec *fs_envblk_spec_t;
 
 static grub_envblk_t fs_envblk_open (grub_envblk_t envblk);
+static void fs_envblk_write (grub_envblk_t envblk);
 
 struct fs_envblk_ops {
   grub_envblk_t (*open) (grub_envblk_t);
+  void (*write) (grub_envblk_t);
 };
 
 struct fs_envblk {
@@ -144,7 +146,8 @@ struct fs_envblk {
 typedef struct fs_envblk *fs_envblk_t;
 
 static struct fs_envblk_ops fs_envblk_ops = {
-  .open = fs_envblk_open
+  .open = fs_envblk_open,
+  .write = fs_envblk_write
 };
 
 static struct fs_envblk_spec fs_envblk_spec[] = {
@@ -358,6 +361,39 @@ write_envblk (const char *name, grub_envblk_t envblk)
   fclose (fp);
 }
 
+static void
+fs_envblk_write (grub_envblk_t envblk)
+{
+  FILE *fp;
+  const char *device;
+  off_t offset;
+  size_t size;
+
+  if (envblk == NULL)
+    return;
+
+  device = fs_envblk->dev;
+  offset = fs_envblk->spec->offset;
+  size = fs_envblk->spec->size;
+
+  if (grub_envblk_size (envblk) > size)
+    grub_util_error ("%s", _("environment block too small"));
+
+  fp = grub_util_fopen (device, "r+b");
+
+  if (! fp)
+    grub_util_error (_("cannot open `%s': %s"), device, strerror (errno));
+
+  if (fseek (fp, offset, SEEK_SET) < 0)
+    grub_util_error (_("cannot seek `%s': %s"), device, strerror (errno));
+
+  if (fwrite (grub_envblk_buffer (envblk), 1, grub_envblk_size (envblk), fp) != grub_envblk_size (envblk))
+    grub_util_error (_("cannot write to `%s': %s"), device, strerror (errno));
+
+  grub_util_file_sync (fp);
+  fclose (fp);
+}
+
 static void
 set_variables (const char *name, int argc, char *argv[])
 {
-- 
2.51.0


_______________________________________________
Grub-devel mailing list
Grub-devel@gnu.org
https://lists.gnu.org/mailman/listinfo/grub-devel

  parent reply	other threads:[~2025-09-15  9:11 UTC|newest]

Thread overview: 15+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2025-09-15  9:08 [PATCH v2 0/9] Add support for external environment block on Btrfs Michael Chang via Grub-devel
2025-09-15  9:08 ` [PATCH v2 1/9] util/grub-editenv: add basic structures and probe call for external envblk Michael Chang via Grub-devel
2025-09-15  9:08 ` [PATCH v2 2/9] util/grub-editenv: add fs_envblk open helper Michael Chang via Grub-devel
2025-09-15  9:08 ` Michael Chang via Grub-devel [this message]
2025-09-15  9:08 ` [PATCH v2 4/9] util/grub-editenv: wire set_variables to optional fs_envblk Michael Chang via Grub-devel
2025-09-15  9:08 ` [PATCH v2 5/9] util/grub-editenv: wire unset_variables " Michael Chang via Grub-devel
2025-09-15  9:08 ` [PATCH v2 6/9] util/grub-editenv: wire list_variables " Michael Chang via Grub-devel
2025-09-17 15:16   ` Sudhakar Kuppusamy
2025-09-15  9:08 ` [PATCH v2 7/9] btrfs: add environment block to reserved header area Michael Chang via Grub-devel
2025-09-15  9:08 ` [PATCH v2 8/9] 00_header.in: wire grub.cfg to use env_block when present Michael Chang via Grub-devel
2025-09-15  9:08 ` [PATCH v2 9/9] docs: add Btrfs env block and special env vars Michael Chang via Grub-devel
2025-09-16 12:05 ` [PATCH v2 0/9] Add support for external environment block on Btrfs Neal Gompa
     [not found] <mailman.4306.1757927367.1200.grub-devel@gnu.org>
2025-09-17 11:04 ` [PATCH v2 3/9] util/grub-editenv: add fs_envblk write helper Avnish Chouhan
2025-10-02  5:53   ` Michael Chang via Grub-devel
2025-10-07  5:22     ` Avnish Chouhan

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=20250915090848.131937-4-mchang@suse.com \
    --to=grub-devel@gnu.org \
    --cc=mchang@suse.com \
    --cc=mlewando@redhat.com \
    --cc=ngompa13@gmail.com \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).