[PATCH 2/3] parted: align partitions to 1MiB

Sascha Hauer s.hauer at pengutronix.de
Thu May 22 06:40:23 PDT 2025


Common partitioning tools align partition start offsets and sizes to
1MiB. Do likewise in barebox parted to avoid unnecessary write
amplification due to unaligned partition starts.

Signed-off-by: Sascha Hauer <s.hauer at pengutronix.de>
---
 commands/parted.c    | 10 ++++++++--
 include/partitions.h |  4 ++++
 2 files changed, 12 insertions(+), 2 deletions(-)

diff --git a/commands/parted.c b/commands/parted.c
index 68b056a0758a14e597d72f76cdc4ba6781a06051..221c6fc98601f1532ea85e000d1e2147f7a5af63 100644
--- a/commands/parted.c
+++ b/commands/parted.c
@@ -8,6 +8,7 @@
 #include <linux/sizes.h>
 #include <partitions.h>
 #include <linux/math64.h>
+#include <range.h>
 
 static struct partition_desc *gpdesc;
 static bool table_needs_write;
@@ -160,13 +161,18 @@ static int do_mkpart(struct block_device *blk, int argc, char *argv[])
 	end *= mult;
 
 	/* If not on sector boundaries move start up and end down */
-	start = ALIGN(start, SECTOR_SIZE);
-	end = ALIGN_DOWN(end, SECTOR_SIZE);
+	start = ALIGN(start, SZ_1M);
+	end = ALIGN_DOWN(end, SZ_1M);
 
 	/* convert to LBA */
 	start >>= SECTOR_SHIFT;
 	end >>= SECTOR_SHIFT;
 
+	if (end == start) {
+		printf("Error: After alignment the partition has zero size\n");
+		return -EINVAL;
+	}
+
 	/*
 	 * When unit is >= KB then substract one sector for user convenience.
 	 * It allows to start the next partition where the previous ends
diff --git a/include/partitions.h b/include/partitions.h
index 7fd6899bd3b88f691385c330bb7900d9ec1547ac..dd3e631c5aa337d752312cd94be5e7dbb4f527a5 100644
--- a/include/partitions.h
+++ b/include/partitions.h
@@ -11,6 +11,7 @@
 #include <filetype.h>
 #include <linux/uuid.h>
 #include <linux/list.h>
+#include <linux/sizes.h>
 
 #define MAX_PARTITION		128
 #define MAX_PARTITION_NAME	38
@@ -55,6 +56,9 @@ struct partition_parser {
 	const char *name;
 };
 
+#define PARTITION_ALIGN_SIZE	SZ_1M
+#define PARTITION_ALIGN_SECTORS	(PARTITION_ALIGN_SIZE >> SECTOR_SHIFT)
+
 void partition_desc_init(struct partition_desc *pd, struct block_device *blk);
 int partition_parser_register(struct partition_parser *p);
 struct partition_desc *partition_table_read(struct block_device *blk);

-- 
2.39.5




More information about the barebox mailing list