mail archive of the barebox mailing list
 help / color / mirror / Atom feed
From: Sascha Hauer <s.hauer@pengutronix.de>
To: BAREBOX <barebox@lists.infradead.org>
Subject: [PATCH 4/6] partitions: gpt: write inactive GPT first
Date: Wed, 03 Dec 2025 16:19:08 +0100	[thread overview]
Message-ID: <20251203-efi-partition-refresh-v1-4-f0b6e79b5fa0@pengutronix.de> (raw)
In-Reply-To: <20251203-efi-partition-refresh-v1-0-f0b6e79b5fa0@pengutronix.de>

There are two GPT partition tables on a device, the primary at the
beginning of a device and the alternate at the end of the device.

For the sake of being more robust against power failures let's write the
inactive GPT first and the currently active one afterwards. This always
leaves us a usable GPT on the device when updating the partition table.

Signed-off-by: Sascha Hauer <s.hauer@pengutronix.de>
---
 common/partitions/efi.c | 9 +++++++--
 1 file changed, 7 insertions(+), 2 deletions(-)

diff --git a/common/partitions/efi.c b/common/partitions/efi.c
index 8da62acf36646b62ff2fac25673adbc48650d299..1766f0123353d502065673d02e983e6cdd51b9af 100644
--- a/common/partitions/efi.c
+++ b/common/partitions/efi.c
@@ -27,6 +27,8 @@ struct efi_partition_desc {
 	gpt_header *gpt;
 	gpt_entry *ptes;
 	struct param_d *param_guid;
+	bool good_pgpt;
+	bool good_agpt;
 };
 
 struct efi_partition {
@@ -424,6 +426,9 @@ static int find_valid_gpt(struct efi_partition_desc *epd, void *buf)
 	if (IS_ENABLED(CONFIG_PARTITION_DISK_EFI_GPT_COMPARE))
 		compare_gpts(blk->dev, pgpt, agpt, lastlba);
 
+	epd->good_pgpt = good_pgpt;
+	epd->good_agpt = good_agpt;
+
 	/* The good cases */
 	if (good_pgpt) {
 		epd->gpt  = pgpt;
@@ -810,11 +815,11 @@ static __maybe_unused int efi_partition_write(struct partition_desc *pd)
 	if (ret)
 		return ret;
 
-	ret = __efi_partition_write(epd, true);
+	ret = __efi_partition_write(epd, !epd->good_pgpt);
 	if (ret)
 		goto err_block_write;
 
-	ret = __efi_partition_write(epd, false);
+	ret = __efi_partition_write(epd, epd->good_pgpt);
 	if (ret)
 		goto err_block_write;
 

-- 
2.47.3




  parent reply	other threads:[~2025-12-03 15:19 UTC|newest]

Thread overview: 16+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2025-12-03 15:19 [PATCH 0/6] partitions: GPT: refresh partition tables when necessary Sascha Hauer
2025-12-03 15:19 ` [PATCH 1/6] partitions: gpt: pass epd context pointer to find_valid_gpt() Sascha Hauer
2025-12-15 12:28   ` Ahmad Fatoum
2025-12-03 15:19 ` [PATCH 2/6] partitions: gpt: only write actual ptes size to device Sascha Hauer
2025-12-15 12:29   ` Ahmad Fatoum
2025-12-03 15:19 ` [PATCH 3/6] partitions: gpt: factor out a function to write primary/alternate GPT Sascha Hauer
2025-12-15 12:34   ` Ahmad Fatoum
2025-12-03 15:19 ` Sascha Hauer [this message]
2025-12-15 12:38   ` [PATCH 4/6] partitions: gpt: write inactive GPT first Ahmad Fatoum
2025-12-03 15:19 ` [PATCH 5/6] partitions: gpt: fix GPT restauration from alternate GPT Sascha Hauer
2025-12-15 12:41   ` Ahmad Fatoum
2025-12-03 15:19 ` [PATCH 6/6] partitions: gpt: refresh partition tables when necessary Sascha Hauer
2025-12-15 12:47   ` Ahmad Fatoum
2025-12-15 14:13     ` Sascha Hauer
2025-12-15 14:15       ` Ahmad Fatoum
2025-12-15 14:09 ` [PATCH 0/6] partitions: GPT: " Sascha Hauer

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=20251203-efi-partition-refresh-v1-4-f0b6e79b5fa0@pengutronix.de \
    --to=s.hauer@pengutronix.de \
    --cc=barebox@lists.infradead.org \
    /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