The GPT alternative header must be at the end of the device. When disk images are written to a device this often is not the case which results in warnings:
WARNING: mmc0: GPT:Primary header thinks Alt. header is not at the end of the disk. WARNING: mmc0: GPT:6561831 != 62160895 WARNING: mmc0: GPT:Alternate GPT header not at the end of the disk. WARNING: mmc0: GPT:6561831 != 62160895 WARNING: mmc0: GPT: Use parted to correct GPT errors. This patch adds support for automatically rewriting the partition table when this happens. This behaviour is optional and needs to be enabled at compile time with CONFIG_PARTITION_DISK_EFI_REFRESH. Also this is runtime configurable with global.system.gpt_refresh Link: https://lore.barebox.org/[email protected] Signed-off-by: Sascha Hauer <[email protected]> --- common/partitions/Kconfig | 14 ++++++ common/partitions/efi.c | 109 ++++++++++++++++++++++++++++++++++++++++++++-- 2 files changed, 120 insertions(+), 3 deletions(-) diff --git a/common/partitions/Kconfig b/common/partitions/Kconfig index 3bbcceedc191be783fa0a5ca0031c9fb24a85b1b..1007dbbe19d26f9b8008a41e227fc0ee93acf5cc 100644 --- a/common/partitions/Kconfig +++ b/common/partitions/Kconfig @@ -24,6 +24,20 @@ config PARTITION_DISK_EFI help Add support to handle partitions in GUID Partition Table style. +config PARTITION_DISK_EFI_REFRESH + depends on PARTITION_DISK_EFI && PARTITION_MANIPULATION + bool "refresh GPT partition on demand" + help + The GPT alternative header must be at the end of the device. When disk + images are written to a device this often is not the case which results + in warnings: + + GPT:Primary header thinks Alt. header is not at the end of the disk. + + Enable this option and set global.system.gpt_refresh to true to refresh + GPT partition tables when necessary. Only erroneous partition tables will + be rewritten, once fixed up partition tables will not be touched. + config PARTITION_DISK_EFI_GPT_NO_FORCE depends on PARTITION_DISK_EFI default y diff --git a/common/partitions/efi.c b/common/partitions/efi.c index 0fd60c9fd0442c3c571c685a5b0b41b8f33db7bd..e4293e0e9ccae128ee8d0d8b08ab82a463d8a10b 100644 --- a/common/partitions/efi.c +++ b/common/partitions/efi.c @@ -15,8 +15,11 @@ #include <common.h> #include <disks.h> #include <init.h> +#include <magicvar.h> +#include <globalvar.h> #include <asm/unaligned.h> #include <crc.h> +#include <fcntl.h> #include <linux/ctype.h> #include <efi/partition.h> @@ -38,6 +41,93 @@ struct efi_partition { static const int force_gpt = IS_ENABLED(CONFIG_PARTITION_DISK_EFI_GPT_NO_FORCE); +struct gpt_refresh { + struct block_device *blk; + struct list_head list; +}; + +static LIST_HEAD(gpt_refreshes); + +static unsigned int global_gpt_refresh; + +static int gpt_refresh_one(struct block_device *blk) +{ + struct partition_desc *pdesc; + int ret; + + if (!global_gpt_refresh) + return 0; + + ret = cdev_open(&blk->cdev, O_RDWR); + if (ret) + return ret; + + pdesc = partition_table_read(blk); + if (!pdesc) { + ret = -EINVAL; + goto out; + } + + ret = partition_table_write(pdesc); + if (ret) + goto out; + + ret = 0; + + partition_table_free(pdesc); +out: + cdev_close(&blk->cdev); + + if (ret) + dev_err(blk->dev, "Refreshing partition table failed: %pe\n", + ERR_PTR(ret)); + + return ret; +} + +static bool gpt_refresh_done; + +static int do_gpt_refreshes(void) +{ + struct gpt_refresh *r, *tmp; + + if (!IS_ENABLED(CONFIG_PARTITION_DISK_EFI_REFRESH)) + return 0; + + if (!global_gpt_refresh) + return 0; + + list_for_each_entry_safe(r, tmp, &gpt_refreshes, list) { + gpt_refresh_one(r->blk); + list_del(&r->list); + free(r); + } + + gpt_refresh_done = true; + + return 0; +} +postenvironment_initcall(do_gpt_refreshes); + +static void add_gpt_refresh(struct block_device *blk) +{ + struct gpt_refresh *r; + + if (!IS_ENABLED(CONFIG_PARTITION_DISK_EFI_REFRESH)) + return; + + if (gpt_refresh_done) { + gpt_refresh_one(blk); + return; + } + + r = xzalloc(sizeof(*r)); + + r->blk = blk; + + list_add_tail(&r->list, &gpt_refreshes); +} + /** * compute_partitions_entries_size() - return the size of all partitions * @gpt: GPT header @@ -290,9 +380,11 @@ is_pte_valid(const gpt_entry *pte, const u64 lastlba) * */ static void -compare_gpts(struct device *dev, gpt_header *pgpt, gpt_header *agpt, +compare_gpts(struct block_device *blk, gpt_header *pgpt, gpt_header *agpt, u64 lastlba) { + struct device *dev = blk->dev; + int error_found = 0; if (!pgpt || !agpt) return; @@ -376,8 +468,11 @@ compare_gpts(struct device *dev, gpt_header *pgpt, gpt_header *agpt, error_found++; } - if (error_found) + if (error_found) { + add_gpt_refresh(blk); dev_warn(dev, "GPT: Use parted to correct GPT errors.\n"); + } + return; } @@ -424,7 +519,7 @@ static int find_valid_gpt(struct efi_partition_desc *epd, void *buf) goto fail; if (IS_ENABLED(CONFIG_PARTITION_DISK_EFI_GPT_COMPARE)) - compare_gpts(blk->dev, pgpt, agpt, lastlba); + compare_gpts(blk, pgpt, agpt, lastlba); epd->good_pgpt = good_pgpt; epd->good_agpt = good_agpt; @@ -874,8 +969,16 @@ static struct partition_parser efi_partition_parser = { .name = "gpt", }; +#ifdef CONFIG_PARTITION_DISK_EFI_REFRESH +BAREBOX_MAGICVAR(global.system.gpt_refresh, "When true, refresh GPT partitions when necessary"); +#endif + static int efi_partition_init(void) { + if (IS_ENABLED(CONFIG_PARTITION_DISK_EFI_REFRESH)) + globalvar_add_simple_bool("system.gpt_refresh", + &global_gpt_refresh); + return partition_parser_register(&efi_partition_parser); } postconsole_initcall(efi_partition_init); -- 2.47.3
