Skip to contentSkip to conversation

bootcount: introduce GPT-backed bootcount device

PATCH In review [email protected] 3 participants 20 messages Jun 5 – Jun 26

lore
Share & tools
lore
#0 lore raw

cover bootcount: introduce GPT-backed bootcount device

This series adds new GPT-backed bootcount device with ChromiumOS-inspired counters/flags logic [1].

Patch 1 is ChromiumOS-style 16-bit flags manipulation routines

Patch 2-5 introduce extension to existing 'gpt' command allowing
the user to update top 16 bits of selected GPT PTE flags manually.

Patch 6 introduces the new bootcount device with compatible
"u-boot,bootcount-gpt"

Patch 7 allow U-Boot running as a coreboot payload, to use new
bootcount-gpt device.

Patch 8 excludes bootcount-gpt from being used in autoboot code path
to allow custom bootflow logic.

[1] https://chromium.googlesource.com/chromiumos/platform/vboot_reference/+/master/cgpt

Denis Mukhin (8):
cros: add common helpers for GPT flags
disk: efi: introduce {read,write}_disk_flags
test: disk: coverage for {read,write}_disk_flags
cmd: gpt: enable extension to manipulate GPT PTE attributes
configs: sandbox: enable GPT flags extension
bootcount: gpt: introduce GPT-backed bootcount device
bootcount: gpt: ensure coreboot payload enables DM_BOOTCOUNT
bootcount: gate bootcount_{inc,error}() behind
!CONFIG_DM_BOOTCOUNT_GPT

 cmd/Kconfig                       |   8 ++
 cmd/gpt.c                         |  97 ++++++++++++++++++++
 configs/sandbox_defconfig         |   1 +
 disk/part_efi.c                   | 141 +++++++++++++++++++++++++++-
 drivers/bootcount/Kconfig         |  10 +-
 drivers/bootcount/Makefile        |   1 +
 drivers/bootcount/bootcount-gpt.c | 140 ++++++++++++++++++++++++++++
 include/bootcount.h               |   3 +-
 include/cros_flags.h              |  74 +++++++++++++++
 include/part.h                    |  24 +++++
 test/dm/Makefile                  |   2 +-
 test/dm/gpt-flags.c               | 148 ++++++++++++++++++++++++++++++
 12 files changed, 641 insertions(+), 8 deletions(-)
 create mode 100644 drivers/bootcount/bootcount-gpt.c
 create mode 100644 include/cros_flags.h
 create mode 100644 test/dm/gpt-flags.c
Signature · 1 line
2.54.0
#1 lore raw

PATCH 1/8 cros: add common helpers for GPT flags

From: Denis Mukhin <[email protected]>

Add common helper to decode/encode ChromuimOS-style GPT attributes similarly to [1].

[1] https://chromium.googlesource.com/chromiumos/platform/vboot_reference/+/master/cgpt

---
 include/cros_flags.h | 74 ++++++++++++++++++++++++++++++++++++++++++++
 1 file changed, 74 insertions(+)
 create mode 100644 include/cros_flags.h
include/cros_flags.h new file +74 -0
new file mode 100644
index 000000000000..e9355fa5e908
@@ -0,0 +1,74 @@
+/* SPDX-License-Identifier: GPL-2.0+ */
+/*
+ * Bit definitions and masks for GPT attributes for Chromium OS.
+ *
+ *  63-61  -- (reserved)
+ *     60  -- read-only
+ *  59-58  -- (reserved)
+ *     57  -- error counter
+ *     56  -- success
+ *  55-52  -- tries
+ *  51-48  -- priority
+ *   47-3  -- UEFI: reserved for future use
+ *      2  -- UEFI: Legacy BIOS bootable
+ *      1  -- UEFI: partition is not mapped
+ *      0  -- UEFI: partition is required
+ *
+ * Reference:
+ *   https://chromium.googlesource.com/chromiumos/platform/vboot_reference/+/master/cgpt
+ */
+
+#ifndef _CROS_FLAGS_H
+#define _CROS_FLAGS_H
+
+#define CROS_FLAG_PRIORITY_MASK      0x000fU
+#define CROS_FLAG_TRIES_MASK         0x00f0U
+#define CROS_FLAG_SUCCESSFUL_MASK    0x0100U
+
+#define CROS_FLAG_PRIORITY_SHIFT     0U
+#define CROS_FLAG_TRIES_SHIFT        4U
+#define CROS_FLAG_SUCCESSFUL_SHIFT   8U
+
+static inline unsigned int
+cros_flags_get_priority(unsigned int flags)
+{
+	return (flags & CROS_FLAG_PRIORITY_MASK) >> CROS_FLAG_PRIORITY_SHIFT;
+}
+
+static inline unsigned int
+cros_flags_set_priority(unsigned int flags, unsigned int priority)
+{
+	flags &= ~CROS_FLAG_PRIORITY_MASK;
+	flags |= (priority & 0xf) << CROS_FLAG_PRIORITY_SHIFT;
+	return flags;
+}
+
+static inline unsigned int
+cros_flags_get_tries(unsigned int flags)
+{
+	return (flags & CROS_FLAG_TRIES_MASK) >> CROS_FLAG_TRIES_SHIFT;
+}
+
+static inline unsigned int
+cros_flags_set_tries(unsigned int flags, unsigned int tries)
+{
+	flags &= ~CROS_FLAG_TRIES_MASK;
+	flags |= (tries & 0xf) << CROS_FLAG_TRIES_SHIFT;
+	return flags;
+}
+
+static inline bool
+cros_flags_get_successful(unsigned int flags)
+{
+	return (flags & CROS_FLAG_SUCCESSFUL_MASK) >> CROS_FLAG_SUCCESSFUL_SHIFT;
+}
+
+static inline unsigned int
+cros_flags_set_successful(unsigned int flags, bool ok)
+{
+	flags &= ~CROS_FLAG_SUCCESSFUL_MASK;
+	flags |= ok << CROS_FLAG_SUCCESSFUL_SHIFT;
+	return flags;
+}
+
+#endif /* _CROS_FLAGS_H */
Signature · 1 line
2.54.0
#2 lore raw

Re: [PATCH v1 1/8] cros: add common helpers for GPT flags

Hi Denis,

Quoted 13 lines · None

cros: add common helpers for GPT flags

Add common helper to decode/encode ChromuimOS-style GPT attributes similarly to [1].

[1] https://chromium.googlesource.com/chromiumos/platform/vboot_reference/+/master/cgpt

include/cros_flags.h | 74 ++++++++++++++++++++++++++++++++++++++++++++++++++++

 1 file changed, 74 insertions(+)

include/cros_flags.h line 9

Quoted 9 lines
include/cros_flags.h +9 -0
@@ -0,0 +1,74 @@
+ * Bit definitions and masks for GPT attributes for Chromium OS.
+ *
+ *  63-61  -- (reserved)
+ *     60  -- read-only
+ *  59-58  -- (reserved)
+ *     57  -- error counter
+ *     56  -- success
+ *  55-52  -- tries
+ *  51-48  -- priority

The table describes bit positions in the full 64-bit GPT attribute field, but the masks and helpers below operate on a 16-bit value (the top 16 bits, already shifted down to bits 0-15). So priority lives at bits 0-3 here, not 48-51. Please reconcile the two - either re-document the table in terms of the 16-bit value the helpers consume, or have the helpers take the full 64-bit attribute and shift internally. As written, a reader who trusts the comment writes wrong code.

include/cros_flags.h line 4

Quoted 6 lines
include/cros_flags.h +4 -0
@@ -0,0 +1,74 @@
+#ifndef _CROS_FLAGS_H
+#define _CROS_FLAGS_H
+
+#define CROS_FLAG_PRIORITY_MASK      0x000fU

The header uses bool without including <stdbool.h> or <linux/types.h>, so it is not self-contained - it only compiles where the includer happens to have pulled those in first. Please add the include.

include/cros_flags.h line 2

Quoted 4 lines
include/cros_flags.h +2 -0
@@ -0,0 +1,74 @@
+ *     57  -- error counter
+ *     56  -- success

Re bit 57 the cover letter talks about counters/flags logic, but there are no get/set helpers for it. Does that mean it is not actually used?

include/cros_flags.h line 2

Quoted 4 lines
include/cros_flags.h +2 -0
@@ -0,0 +1,74 @@
+static inline unsigned int
+cros_flags_set_priority(unsigned int flags, unsigned int priority)

You can likely use uint here if it is only compiled for U-Boot and not the tools.

include/cros_flags.h line 44

Quoted 5 lines
include/cros_flags.h +5 -0
+{
+     flags &= ~CROS_FLAG_PRIORITY_MASK;
+     flags |= (priority & 0xf) << CROS_FLAG_PRIORITY_SHIFT;
+     return flags;
+}

The bare 0xf (and 0xf in set_tries()) duplicates the field width already encoded in CROS_FLAG_PRIORITY_MASK. Please express this as (CROS_FLAG_PRIORITY_MASK >> CROS_FLAG_PRIORITY_SHIFT) so the width lives in one place.

include/cros_flags.h line 5

Quoted 7 lines
include/cros_flags.h +5 -0
@@ -0,0 +1,74 @@
+static inline bool
+cros_flags_get_successful(unsigned int flags)
+{
+     return (flags & CROS_FLAG_SUCCESSFUL_MASK) >> CROS_FLAG_SUCCESSFUL_SHIFT;
+}

Simpler as !!(flags & CROS_FLAG_SUCCESSFUL_MASK) - no shift needed, and the bool conversion is explicit.

Quoted 2 lines

Add common helper to decode/encode ChromuimOS-style GPT attributes similarly to [1].

Typo - ChromuimOS should be ChromiumOS (or the old Chromium OS, which is what the header uses).

Regards,
Simon

#3 lore raw

Re: [PATCH v1 1/8] cros: add common helpers for GPT flags

In reply to [email protected]

Hi Denis,

Quoted 6 lines · On 6/5/26 5:39 AM, [email protected] wrote:

From: Denis Mukhin <[email protected]>

Add common helper to decode/encode ChromuimOS-style GPT attributes similarly to [1].

[1] https://chromium.googlesource.com/chromiumos/platform/vboot_reference/+/master/cgpt

Can you tell us *which* commit you used in the commit log. I'm assuming master is a moving branch so this makes it harder to look for future fixes.

Quoted 20 lines
---
  include/cros_flags.h | 74 ++++++++++++++++++++++++++++++++++++++++++++
  1 file changed, 74 insertions(+)

create mode 100644 include/cros_flags.h

include/cros_flags.h new file +8 -0
new file mode 100644
index 000000000000..e9355fa5e908
@@ -0,0 +1,74 @@
+/* SPDX-License-Identifier: GPL-2.0+ */
+/*
+ * Bit definitions and masks for GPT attributes for Chromium OS.
+ *
+ *  63-61  -- (reserved)
+ *     60  -- read-only
+ *  59-58  -- (reserved)
+ *     57  -- error counter

include/cros_flags.h line 18

Quoted 10 lines
include/cros_flags.h new file +10 -0
+ *     56  -- success
+ *  55-52  -- tries
+ *  51-48  -- priority
+ *   47-3  -- UEFI: reserved for future use
+ *      2  -- UEFI: Legacy BIOS bootable
+ *      1  -- UEFI: partition is not mapped
+ *      0  -- UEFI: partition is required
+ *
+ * Reference:
+ *   https://chromium.googlesource.com/chromiumos/platform/vboot_reference/+/master/cgpt

Possibly here as well so we don't get confused if this differs from master of vboot in years from now?

Quoted 46 lines
include/cros_flags.h new file +46 -0
+ */
+
+#ifndef _CROS_FLAGS_H
+#define _CROS_FLAGS_H
+
+#define CROS_FLAG_PRIORITY_MASK      0x000fU
+#define CROS_FLAG_TRIES_MASK         0x00f0U
+#define CROS_FLAG_SUCCESSFUL_MASK    0x0100U
+
+#define CROS_FLAG_PRIORITY_SHIFT     0U
+#define CROS_FLAG_TRIES_SHIFT        4U
+#define CROS_FLAG_SUCCESSFUL_SHIFT   8U
+
+static inline unsigned int
+cros_flags_get_priority(unsigned int flags)
+{
+	return (flags & CROS_FLAG_PRIORITY_MASK) >> CROS_FLAG_PRIORITY_SHIFT;
+}
+
+static inline unsigned int
+cros_flags_set_priority(unsigned int flags, unsigned int priority)
+{
+	flags &= ~CROS_FLAG_PRIORITY_MASK;
+	flags |= (priority & 0xf) << CROS_FLAG_PRIORITY_SHIFT;
+	return flags;
+}
+
+static inline unsigned int
+cros_flags_get_tries(unsigned int flags)
+{
+	return (flags & CROS_FLAG_TRIES_MASK) >> CROS_FLAG_TRIES_SHIFT;
+}
+
+static inline unsigned int
+cros_flags_set_tries(unsigned int flags, unsigned int tries)
+{
+	flags &= ~CROS_FLAG_TRIES_MASK;
+	flags |= (tries & 0xf) << CROS_FLAG_TRIES_SHIFT;
+	return flags;
+}
+
+static inline bool
+cros_flags_get_successful(unsigned int flags)
+{
+	return (flags & CROS_FLAG_SUCCESSFUL_MASK) >> CROS_FLAG_SUCCESSFUL_SHIFT;
+}

include/cros_flags.h line 74

Quoted 10 lines
include/cros_flags.h new file +10 -0
+
+static inline unsigned int
+cros_flags_set_successful(unsigned int flags, bool ok)
+{
+	flags &= ~CROS_FLAG_SUCCESSFUL_MASK;
+	flags |= ok << CROS_FLAG_SUCCESSFUL_SHIFT;
+	return flags;
+}
+
+#endif /* _CROS_FLAGS_H */

Would it make sense to use the nice GENMASK, FIELD_GET, FIELD_PREP macros from Linux for bitfield and masks?

Cheers,
Quentin

#4 lore raw

PATCH 2/8 disk: efi: introduce {read,write}_disk_flags

In reply to [email protected]

From: Denis Mukhin <[email protected]>

Introduce helper functions for updating top 16 bits of GUID partition table entry attributes. The value can be used to store extra information about bootflow, such as counters and status.

---
 disk/part_efi.c | 129 ++++++++++++++++++++++++++++++++++++++++++++++++
 include/part.h  |  24 +++++++++
 2 files changed, 153 insertions(+)
disk/part_efi.c +129 -0
index d8b17ec2e91a..6d5520e53912 100644
@@ -980,6 +980,135 @@ int write_mbr_and_gpt_partitions(struct blk_desc *desc, void *buf)
 
 	return 0;
 }
+
+int read_disk_flags(struct blk_desc *desc, int partnum, u16 *flags)
+{
+	ALLOC_CACHE_ALIGN_BUFFER_PAD(gpt_header, gpt_head, 1, desc->blksz);
+	gpt_entry *gpt_pte = NULL;
+	u64 attrs;
+
+	/* "part" argument must be at least 1 */
+	if (partnum < 1)
+		return -EINVAL;
+
+	gpt_repair_headers(desc);
+
+	/* This function validates AND fills in the GPT header and PTE */
+	if (find_valid_gpt(desc, gpt_head, &gpt_pte) != 1)
+		return -EINVAL;
+
+	if (partnum > le32_to_cpu(gpt_head->num_partition_entries) ||
+	    !is_pte_valid(&gpt_pte[partnum - 1])) {
+		free(gpt_pte);
+		return -EPERM;
+	}
+
+	attrs = le64_to_cpu(gpt_pte[partnum - 1].attributes.raw);
+	*flags = (attrs >> 48) & 0xFFFF;
+
+	free(gpt_pte);
+
+	return 0;
+}
+
+static int write_gpt_pte(struct blk_desc *desc, gpt_header *gpt_h, gpt_entry *gpt_e)
+{
+	const int cnt = BLOCK_CNT((le32_to_cpu(gpt_h->num_partition_entries)
+					   * sizeof(gpt_entry)), desc);
+	u32 crc32;
+	int rc;
+
+	crc32 = efi_crc32((const unsigned char *)gpt_e,
+			      le32_to_cpu(gpt_h->num_partition_entries) *
+			      le32_to_cpu(gpt_h->sizeof_partition_entry));
+	gpt_h->partition_entry_array_crc32 = cpu_to_le32(crc32);
+
+	gpt_h->header_crc32 = 0;
+	crc32 = efi_crc32((const unsigned char *)gpt_h,
+			      le32_to_cpu(gpt_h->header_size));
+	gpt_h->header_crc32 = cpu_to_le32(crc32);
+
+	rc = blk_dwrite(desc, le64_to_cpu(gpt_h->my_lba), 1, gpt_h);
+	if (rc != 1) {
+		pr_err("device %d: failed to update GPT header: %d\n",
+		       desc->devnum, rc);
+		return rc ?: -EIO;
+	}
+
+	rc = blk_dwrite(desc, le64_to_cpu(gpt_h->partition_entry_lba), cnt, gpt_e);
+	if (rc != cnt) {
+		pr_err("device %d: failed to update GPT PTE: %d\n",
+		       desc->devnum, rc);
+		return rc ?: -EIO;
+	}
+
+	return 0;
+}
+
+int write_disk_flags(struct blk_desc *desc, int partnum, u16 flags)
+{
+	ALLOC_CACHE_ALIGN_BUFFER_PAD(gpt_header, gpt_h1, 1, desc->blksz);
+	ALLOC_CACHE_ALIGN_BUFFER_PAD(gpt_header, gpt_h2, 1, desc->blksz);
+	gpt_entry *gpt_e1 = NULL, *gpt_e2 = NULL;
+	u64 attrs;
+	int rc;
+
+	/* "part" argument must be at least 1 */
+	if (partnum < 1)
+		return -EINVAL;
+
+	gpt_repair_headers(desc);
+
+	rc = is_gpt_valid(desc, GPT_PRIMARY_PARTITION_TABLE_LBA, gpt_h1, &gpt_e1);
+	if (!rc) {
+		rc = -EPERM;
+		goto out;
+	}
+	if (partnum > le32_to_cpu(gpt_h1->num_partition_entries) ||
+	    !is_pte_valid(&gpt_e1[partnum - 1])) {
+		rc = -EINVAL;
+		goto out;
+	}
+
+	attrs = le64_to_cpu(gpt_e1[partnum - 1].attributes.raw);
+	attrs &= ~(0xFFFFULL << 48);
+	attrs |= ((u64)flags << 48);
+	gpt_e1[partnum - 1].attributes.raw = cpu_to_le64(attrs);
+
+	rc = write_gpt_pte(desc, gpt_h1, gpt_e1);
+	if (rc < 0) {
+		pr_err("partnum %d: failed to update primary GPT PTE\n", partnum);
+		goto out;
+	}
+
+	rc = is_gpt_valid(desc, desc->lba - 1, gpt_h2, &gpt_e2);
+	if (!rc) {
+		rc = -EPERM;
+		goto out;
+	}
+	if (partnum > le32_to_cpu(gpt_h2->num_partition_entries) ||
+	    !is_pte_valid(&gpt_e2[partnum - 1])) {
+		rc = -EINVAL;
+		goto out;
+	}
+
+	attrs = le64_to_cpu(gpt_e2[partnum - 1].attributes.raw);
+	attrs &= ~(0xFFFFULL << 48);
+	attrs |= ((u64)flags << 48);
+	gpt_e2[partnum - 1].attributes.raw = cpu_to_le64(attrs);
+
+	rc = write_gpt_pte(desc, gpt_h2, gpt_e2);
+	if (rc < 0) {
+		pr_err("partnum %d: failed to update secondary GPT PTE\n", partnum);
+		goto out;
+	}
+
+out:
+	free(gpt_e1);
+	free(gpt_e2);
+
+	return rc;
+}
 #endif
 
 /*
include/part.h +24 -0
index 15daacd7faaa..4724e01c0a00 100644
@@ -678,6 +678,30 @@ int get_disk_guid(struct blk_desc *desc, char *guid);
  */
 int part_get_gpt_pte(struct blk_desc *desc, int part, gpt_entry *gpt_e);
 
+/**
+ * write_disk_flags() - Write the top 16 bits of GPT partition table
+ *                      entry attributes.
+ *
+ * @desc:	block device descriptor
+ * @partnum:	GPT partition ID
+ * @flags:	attributes value
+ *
+ * Return:	zero on success, otherwise error
+ */
+int write_disk_flags(struct blk_desc *desc, int partnum, u16 flags);
+
+/**
+ * read_disk_flags() - Read the top 16 bits of GPT partition table
+ *                     entry attributes.
+ *
+ * @desc:	block device descriptor
+ * @partnum:	GPT partition ID
+ * @flags:	pointer to attributes value (cannot be NULL)
+ *
+ * Return:	zero on success, otherwise error
+ */
+int read_disk_flags(struct blk_desc *desc, int partnum, u16 *flags);
+
 #endif
 
 #if CONFIG_IS_ENABLED(DOS_PARTITION)
Signature · 1 line
2.54.0
#5 lore raw

Re: [PATCH v1 2/8] disk: efi: introduce {read,write}_disk_flags

Hi Denis,

Quoted 14 lines · None

disk: efi: introduce {read,write}_disk_flags

Introduce helper functions for updating top 16 bits of GUID partition table entry attributes. The value can be used to store extra information about bootflow, such as counters and status.

disk/part_efi.c | 129 ++++++++++++++++++++++++++++++++++++++++++++++++++++++++

 include/part.h  |  24 +++++++++++
 2 files changed, 153 insertions(+)

disk/part_efi.c int write_mbr_and_gpt_partitions(struct blk_desc *desc, void *buf) · line 981

Quoted 2 lines
disk/part_efi.c +2 -0
@@ -980,6 +980,135 @@ int write_mbr_and_gpt_partitions(struct blk_desc *desc, void *buf)
+int read_disk_flags(struct blk_desc *desc, int partnum, u16 *flags)
+int write_disk_flags(struct blk_desc *desc, int partnum, u16 flags)

These names are too generic - they are not disk operations, they read/write the top 16 bits of a GPT PTE in a ChromiumOS-specific layout. Please rename to gpt_read_flags()/gpt_write_flags() and put the prototypes alongside the other GPT helpers (next to part_get_gpt_pte()). As-is, a casual reader would expect them to work on any partition table.

disk/part_efi.c int write_mbr_and_gpt_partitions(struct blk_desc *desc, void *buf) · line 990

Quoted 13 lines
disk/part_efi.c +11 -0
@@ -980,6 +980,135 @@ int write_mbr_and_gpt_partitions(struct blk_desc *desc, void *buf)
+int read_disk_flags(struct blk_desc *desc, int partnum, u16 *flags)
+{
+     ALLOC_CACHE_ALIGN_BUFFER_PAD(gpt_header, gpt_head, 1, desc->blksz);
+     gpt_entry *gpt_pte = NULL;
+     u64 attrs;
+
+     /* 'part' argument must be at least 1 */
+     if (partnum < 1)
+             return -EINVAL;
+
+     gpt_repair_headers(desc);

A read must not mutate the disk. gpt_repair_headers() performs writes when one of the two headers is bad - please drop this call from read_disk_flags(). The return value is also ignored in both functions:
if both GPTs are invalid the caller falls through into find_valid_gpt()/is_gpt_valid() anyway, so the call adds nothing for the read path and should be the caller's choice for the write path.

The doxygen says flags cannot be NULL but the function does not check it - either drop the comment or add a check.

Quoted 10 lines
disk/part_efi.c +8 -0
@@ -980,6 +980,135 @@ int write_mbr_and_gpt_partitions(struct blk_desc *desc, void *buf)
+     if (find_valid_gpt(desc, gpt_head, &gpt_pte) != 1)
+             return -EINVAL;
+
+     if (partnum > le32_to_cpu(gpt_head->num_partition_entries) ||
+         !is_pte_valid(&gpt_pte[partnum - 1])) {
+             free(gpt_pte);
+             return -EPERM;
+     }

...

disk/part_efi.c int write_mbr_and_gpt_partitions(struct blk_desc *desc, void *buf) · line 1071

Quoted 10 lines
disk/part_efi.c +10 -0
+     rc = is_gpt_valid(desc, GPT_PRIMARY_PARTITION_TABLE_LBA, gpt_h1, &gpt_e1);
+     if (!rc) {
+             rc = -EPERM;
+             goto out;
+     }
+     if (partnum > le32_to_cpu(gpt_h1->num_partition_entries) ||
+         !is_pte_valid(&gpt_e1[partnum - 1])) {
+             rc = -EINVAL;
+             goto out;
+     }

The errno choices are odd and inconsistent. -EPERM (operation not permitted) does not describe 'GPT could not be parsed' or 'partition does not exist'. read returns -EINVAL for invalid GPT and -EPERM for bad partnum; write swaps them. I suggest -EIO for the unreadable-GPT case and -ENOENT for 'partition does not exist', consistently.

Quoted 3 lines
disk/part_efi.c +1 -0
@@ -980,6 +980,135 @@ int write_mbr_and_gpt_partitions(struct blk_desc *desc, void *buf)
+     rc = is_gpt_valid(desc, GPT_PRIMARY_PARTITION_TABLE_LBA, gpt_h1, &gpt_e1);

...

disk/part_efi.c

Quoted 2 lines
disk/part_efi.c +2 -0
+     attrs = le64_to_cpu(gpt_e1[partnum - 1].attributes.raw);
+     attrs &= ~(0xFFFFULL << 48);

Please use lower-case hex.

Quoted 4 lines
disk/part_efi.c +4 -0
+     attrs |= ((u64)flags << 48);
+     gpt_e1[partnum - 1].attributes.raw = cpu_to_le64(attrs);
+
+     rc = write_gpt_pte(desc, gpt_h1, gpt_e1);

...

Quoted 1 line

+ rc = is_gpt_valid(desc, desc->lba - 1, gpt_h2, &gpt_e2);

...

disk/part_efi.c int write_mbr_and_gpt_partitions(struct blk_desc *desc, void *buf) · line 1100

Quoted 6 lines
disk/part_efi.c +6 -0
+     attrs = le64_to_cpu(gpt_e2[partnum - 1].attributes.raw);
+     attrs &= ~(0xFFFFULL << 48);
+     attrs |= ((u64)flags << 48);
+     gpt_e2[partnum - 1].attributes.raw = cpu_to_le64(attrs);
+
+     rc = write_gpt_pte(desc, gpt_h2, gpt_e2);

Two copies of the same logic varying only by LBA. Please factor into a helper that takes the LBA (or a primary/backup flag) and call it twice. Also please #define a constant for the bit-48 shift (CROS_FLAGS_SHIFT, or similar in cros_flags.h) rather than open-coding '<< 48' and '0xFFFFULL << 48' in three places.

disk/part_efi.c int write_mbr_and_gpt_partitions(struct blk_desc *desc, void *buf) · line 987

Quoted 10 lines
disk/part_efi.c +8 -0
@@ -980,6 +980,135 @@ int write_mbr_and_gpt_partitions(struct blk_desc *desc, void *buf)
+     rc = blk_dwrite(desc, le64_to_cpu(gpt_h->my_lba), 1, gpt_h);
+     if (rc != 1) {
+             pr_err("device %d: failed to update GPT header: %d\n",
+                    desc->devnum, rc);
+             return rc ?: -EIO;
+     }
+
+     rc = blk_dwrite(desc, le64_to_cpu(gpt_h->partition_entry_lba), cnt, gpt_e);

Please write the PTE array first, then the header - that is what write_gpt_table() does, and it is the safer ordering: a torn write leaves the on-disk header pointing at a CRC that does not match the on-disk PTE if you do header-first.

disk/part_efi.c int write_mbr_and_gpt_partitions(struct blk_desc *desc, void *buf) · line 983

Quoted 6 lines
disk/part_efi.c +4 -0
@@ -980,6 +980,135 @@ int write_mbr_and_gpt_partitions(struct blk_desc *desc, void *buf)
+static int write_gpt_pte(struct blk_desc *desc, gpt_header *gpt_h, gpt_entry *gpt_e)
+{
+     const int cnt = BLOCK_CNT((le32_to_cpu(gpt_h->num_partition_entries)
+                                        * sizeof(gpt_entry)), desc);

If primary write succeeds but the backup write fails (bad block at end of disk, etc.) you leave the two GPTs out of sync and return an error to the caller, who has no way to know what state the disk is in. Please mention this in the function comment, and consider attempting to roll back / retry rather than silently leaving an inconsistent state. Yes I know this shouldn't happen, but power can fail, device can reset, etc.

Regards,
Simon

#6 lore raw

PATCH 3/8 test: disk: coverage for {read,write}_disk_flags

In reply to [email protected]

From: Denis Mukhin <[email protected]>

Add unit test coverage for GPT flags functions.

To run the test:
truncate -s 16M mmc1.img
./test/py/test.py --bd sandbox --build -k test_gpt_flags -v

---
 disk/part_efi.c     |  12 ++--
 test/dm/Makefile    |   2 +-
 test/dm/gpt-flags.c | 148 ++++++++++++++++++++++++++++++++++++++++++++
 3 files changed, 156 insertions(+), 6 deletions(-)
 create mode 100644 test/dm/gpt-flags.c
disk/part_efi.c +7 -5
index 6d5520e53912..9d94c1bd4efa 100644
@@ -47,17 +47,19 @@ static inline u32 efi_crc32(const void *buf, u32 len)
 	return crc32(0, buf, len);
 }
 
+/* Public for unit tests. */
+int is_gpt_valid(struct blk_desc *desc, u64 lba, gpt_header *pgpt_head,
+			gpt_entry **pgpt_pte);
+int is_pte_valid(gpt_entry * pte);
+
 /*
  * Private function prototypes
  */
 
 static int pmbr_part_valid(dos_partition_t *part);
 static int is_pmbr_valid(legacy_mbr * mbr);
-static int is_gpt_valid(struct blk_desc *desc, u64 lba, gpt_header *pgpt_head,
-			gpt_entry **pgpt_pte);
 static gpt_entry *alloc_read_gpt_entries(struct blk_desc *desc,
 					 gpt_header *pgpt_head);
-static int is_pte_valid(gpt_entry * pte);
 static int find_valid_gpt(struct blk_desc *desc, gpt_header *gpt_head,
 			  gpt_entry **pgpt_pte);
 
@@ -1168,7 +1170,7 @@ static int is_pmbr_valid(legacy_mbr *mbr)
  * Description: returns 1 if valid,  0 on error, 2 if ignored header
  * If valid, returns pointers to PTEs.
  */
-static int is_gpt_valid(struct blk_desc *desc, u64 lba, gpt_header *pgpt_head,
+int is_gpt_valid(struct blk_desc *desc, u64 lba, gpt_header *pgpt_head,
 			gpt_entry **pgpt_pte)
 {
 	/* Confirm valid arguments prior to allocation. */
@@ -1316,7 +1318,7 @@ static gpt_entry *alloc_read_gpt_entries(struct blk_desc *desc,
  *
  * Description: returns 1 if valid,  0 on error.
  */
-static int is_pte_valid(gpt_entry * pte)
+int is_pte_valid(gpt_entry * pte)
 {
 	efi_guid_t unused_guid;
 
test/dm/Makefile +1 -1
index d69b0e08d66a..df2bc28078d3 100644
@@ -82,7 +82,7 @@ obj-y += ofread.o
 obj-y += of_extra.o
 obj-$(CONFIG_OSD) += osd.o
 obj-$(CONFIG_VIDEO) += panel.o
-obj-$(CONFIG_EFI_PARTITION) += part.o
+obj-$(CONFIG_EFI_PARTITION) += part.o gpt-flags.o
 obj-$(CONFIG_PCI) += pci.o
 obj-$(CONFIG_P2SB) += p2sb.o
 obj-$(CONFIG_PCI_ENDPOINT) += pci_ep.o
test/dm/gpt-flags.c new file +148 -0

148 lines not on this page · raw message

Signature · 1 line
2.54.0
#7 lore raw

Re: [PATCH v1 3/8] test: disk: coverage for {read,write}_disk_flags

Hi Denis,

Quoted 17 lines · None

test: disk: coverage for {read,write}_disk_flags

Add unit test coverage for GPT flags functions.

To run the test:
truncate -s 16M mmc1.img
./test/py/test.py --bd sandbox --build -k test_gpt_flags -v

disk/part_efi.c | 12 +++--

 test/dm/Makefile    |   2 +-
 test/dm/gpt-flags.c | 148 ++++++++++++++++++++++++++++++++++++++++++++++++++++
 3 files changed, 156 insertions(+), 6 deletions(-)

disk/part_efi.c static inline u32 efi_crc32(const void *buf, u32 len) · line 54

Quoted 8 lines
disk/part_efi.c +5 -0
@@ -47,17 +47,19 @@ static inline u32 efi_crc32(const void *buf, u32 len)
      return crc32(0, buf, len);
 }
 
+/* Public for unit tests. */
+int is_gpt_valid(struct blk_desc *desc, u64 lba, gpt_header *pgpt_head,
+                     gpt_entry **pgpt_pte);
+int is_pte_valid(gpt_entry * pte);
+

Please don't expose these by re-declaring the prototypes in two places (here and in test/dm/gpt-flags.c). Either put the prototypes in a private header that only the test pulls in, or rework the test so it goes through the public API. The "Public for unit tests" comment plus a duplicated extern declaration in the test file is fragile - if the signature ever changes, the test silently drifts.

Quoted 19 lines
test/dm/gpt-flags.c +17 -0
@@ -0,0 +1,148 @@
+static int mock_blk_desc(struct unit_test_state *uts, struct blk_desc **desc)
+{
+     struct udevice *dev;
+     char str_disk_guid[UUID_STR_LEN + 1];
+     struct disk_partition parts[2] = {
+             {
+                     .start = TEST_LBA_COUNT,
+                     .size = 1,
+                     .name = 'test1',
+             },
+             {
+                     .start = 49,
+                     .size = 1,
+                     .name = 'test2',
+             },
+     };
+

test/dm/gpt-flags.c line 27

Quoted 10 lines
test/dm/gpt-flags.c +10 -0
+     ut_assertok(uclass_get_device_by_name(UCLASS_BLK, 'mmc1.blk', &dev));
+     *desc = (struct blk_desc *)dev_get_uclass_plat(dev);
+
+     if (CONFIG_IS_ENABLED(RANDOM_UUID)) {
+             gen_rand_uuid_str(parts[0].uuid, UUID_STR_FORMAT_STD);
+             gen_rand_uuid_str(parts[1].uuid, UUID_STR_FORMAT_STD);
+             gen_rand_uuid_str(str_disk_guid, UUID_STR_FORMAT_STD);
+     }
+     ut_assertok(gpt_restore(*desc, str_disk_guid, parts,
+                             ARRAY_SIZE(parts)));

When CONFIG_RANDOM_UUID is not enabled, str_disk_guid is an uninitialised stack buffer and parts[].uuid is empty, so gpt_restore() calls uuid_str_to_bin() on garbage and fails. Please use a fixed GUID string (or assert the dependency on RANDOM_UUID) - silently relying on the sandbox defconfig is brittle.

test/dm/gpt-flags.c line 1

Quoted 3 lines
test/dm/gpt-flags.c +1 -0
@@ -0,0 +1,148 @@
+DM_TEST(test_gpt_flags_default, UTF_SCAN_FDT);

DM_TEST registers these under the dm suite (I am assuming DM is necessary?), and the convention is to prefix function names with dm_test_. Please rename to dm_test_gpt_flags_default etc - that is also what 'ut dm gpt_flags_default' will look for. Please give test_disk_flags_invalid_partnum the same prefix for consistency.

test/dm/gpt-flags.c line 11

Quoted 13 lines
test/dm/gpt-flags.c +11 -0
@@ -0,0 +1,148 @@
+static int test_gpt_flags_headers(struct unit_test_state *uts)
+{
+     struct blk_desc *desc;
+     u16 flags_in = 0x0111;
+
+     ut_assertok(mock_blk_desc(uts, &desc));
+     ut_assertok(write_disk_flags(desc, TEST_PARTNUM, flags_in));
+
+     {
+             ALLOC_CACHE_ALIGN_BUFFER_PAD(gpt_header, gpt_h1, 1, desc->blksz);
+             ALLOC_CACHE_ALIGN_BUFFER_PAD(gpt_header, gpt_h2, 1, desc->blksz);

The nested { ... } block is awkward - please put the buffers and locals to the top of the function.

test/dm/gpt-flags.c line 3

Quoted 5 lines
test/dm/gpt-flags.c +3 -0
@@ -0,0 +1,148 @@
+ * To run the test:
+ *   truncate -s 16M mmc1.img
+ *   ./test/py/test.py --bd sandbox --build -k test_gpt_flags -v

This belongs as automatic setup in test/py/tests/test_ut.py (see setup_fedora_image, setup_rauc_image, etc), not as manual instructions in the commit message. Otherwise CI won't run it and people reading the test years from now won't know where the image came from. Please wire it into test_ut_dm_init_bootstd() so 'ut dm gpt_flags_*' just works.

test/dm/gpt-flags.c line 9

Quoted 11 lines
test/dm/gpt-flags.c +9 -0
@@ -0,0 +1,148 @@
+static int test_disk_flags_invalid_partnum(struct unit_test_state *uts)
+{
+     struct blk_desc *desc;
+     u16 flags;
+
+     ut_assertok(mock_blk_desc(uts, &desc));
+     ut_asserteq(-EINVAL, read_disk_flags(desc, 0, &flags));
+     ut_asserteq(-EINVAL, write_disk_flags(desc, 0, 0));
+     ut_asserteq(-EINVAL, write_disk_flags(desc, 255, 0));

Missing a few cases: negative partnum, a partnum just past num_partition_entries (which returns -EPERM from read_disk_flags() and -EINVAL from write_disk_flags() = different return codes, worth a separate comment on patch 2), and read_disk_flags() with the high partnum. Also please test with a NULL flags pointer if read_disk_flags() is meant to reject it.

Regards,
Simon

#8 lore raw

PATCH 4/8 cmd: gpt: enable extension to manipulate GPT PTE attributes

In reply to [email protected]

From: Denis Mukhin <[email protected]>

Enable extension for existing 'gpt' command to manipulate top 16 bits of GPT PTE attributes to assist ChromiumOS-style boot counters.

Follow the existing syntax for specifying <interface, device, partition> tuple.

---
 cmd/Kconfig |  8 +++++
 cmd/gpt.c   | 97 +++++++++++++++++++++++++++++++++++++++++++++++++++++
 2 files changed, 105 insertions(+)
cmd/Kconfig +8 -0
index c71c6824a196..83409e60da8b 100644
@@ -1319,6 +1319,14 @@ config CMD_GPT
 	  Enable the 'gpt' command to ready and write GPT style partition
 	  tables.
 
+config CMD_GPT_FLAGS
+	bool "GPT partition flags commands"
+	depends on CMD_GPT
+	select PARTITION_TYPE_GUID
+	help
+	  Enables the GUID partition table attributes manipulation (bits 48:63)
+	  in a ChromiumOS-style manner.
+
 config CMD_GPT_RENAME
 	bool "GPT partition renaming commands"
 	depends on CMD_GPT
cmd/gpt.c +97 -0

119 lines not on this page · raw message

Signature · 1 line
2.54.0
#9 lore raw

Re: [PATCH v1 4/8] cmd: gpt: enable extension to manipulate GPT PTE attributes

Hi Denis,

Quoted 16 lines · None

cmd: gpt: enable extension to manipulate GPT PTE attributes

Enable extension for existing 'gpt' command to manipulate top 16 bits of GPT PTE attributes to assist ChromiumOS-style boot counters.

Follow the existing syntax for specifying <interface, device, partition> tuple.

cmd/Kconfig | 8 +++++

 cmd/gpt.c   | 97 +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
 2 files changed, 105 insertions(+)

cmd/gpt.c line 17

Quoted 7 lines
cmd/gpt.c +3 -0
@@ -11,6 +11,9 @@
  */
 
 #include <blk.h>
+#ifdef CONFIG_CMD_GPT_FLAGS
+#include <cros_flags.h>
+#endif
 #include <env.h>

Please drop the #ifdef - header guards make it harmless to include unconditionally, and U-Boot generally avoids conditional includes for plain headers.

cmd/gpt.c out: · line 1120

Quoted 4 lines
cmd/gpt.c +2 -0
@@ -1116,6 +1119,64 @@ out:
+static int do_set_gpt_flags(struct blk_desc *desc, int partnum,
+                            const char *key, int value)

The continuation here is indented with spaces instead of a tab plus spaces. Same problem in the pr_err() call below and at the closing brace:

cmd/gpt.c out: · line 1174

Quoted 4 lines
cmd/gpt.c +4 -0
+             pr_err("ERROR: partnum %d: failed to write GPT flags: %d\n",
+               partnum, rc);
+             return CMD_RET_FAILURE;
+    }

Spaces again.

cmd/gpt.c out: · line 1122

Quoted 6 lines
cmd/gpt.c +4 -0
@@ -1116,6 +1119,64 @@ out:
+     rc = write_disk_flags(desc, partnum, guid_flags);
+     if (rc < 0) {
+             pr_err("ERROR: partnum %d: failed to write GPT flags: %d\n",
+               partnum, rc);

Why pr_err() here, but printf() for the read-flags error a few lines up and inside do_get_gpt_flags()? Please pick one - this is a command handler, so printf() seems more consistent with the rest of the file.

cmd/gpt.c out: · line 1126

Quoted 10 lines
cmd/gpt.c +8 -0
@@ -1116,6 +1119,64 @@ out:
+     if (!strcmp(key, 'set-flags')) {
+             guid_flags = (u16)(value & 0xffff);
+     } else if (!strcmp(key, 'set-priority')) {
+             guid_flags = cros_flags_set_priority(guid_flags, value);
+     } else if (!strcmp(key, 'set-successful')) {
+             guid_flags = cros_flags_set_successful(guid_flags, value);
+     } else if (!strcmp(key, 'set-tries')) {
+             guid_flags = cros_flags_set_tries(guid_flags, value);

The help text advertises ranges like [0..15] and {0,1}, but nothing here rejects out-of-range input...the cros_flags_set_*() helpers silently mask to 4 bits, and set-flags silently truncates to 16 bits. Please validate value against the documented range and return CMD_RET_USAGE on bad input.

cmd/gpt.c static int do_gpt(struct cmd_tbl *cmdtp, int flag, int argc, char *const argv[]) · line 1247

Quoted 13 lines
cmd/gpt.c +11 -0
@@ -1176,6 +1237,23 @@ static int do_gpt(struct cmd_tbl *cmdtp, int flag, int argc, char *const argv[])
+     } else if (!strcmp(argv[1], 'get-flags')) {
+             if (argc < 5)
+                     return CMD_RET_FAILURE;
+             ret = do_get_gpt_flags(blk_dev_desc,
+                                    simple_strtol(argv[4], NULL, 0));
+     } else if (!strcmp(argv[1], 'set-flags') ||
+                !strcmp(argv[1], 'set-priority') ||
+                !strcmp(argv[1], 'set-successful') ||
+                !strcmp(argv[1], 'set-tries')) {
+             if (argc < 6)
+                     return CMD_RET_FAILURE;

The other branches fall through to CMD_RET_USAGE on bad argument counts (see the top of do_gpt()). Returning CMD_RET_FAILURE here means the user gets 'error!' instead of the usage hint. Please use CMD_RET_USAGE

Also, do_gpt() already gates argc at 4..5 (or 4..6 with CMD_GPT_RENAME) before any dispatch. From what I can tell, with CMD_GPT_FLAGS but not CMD_GPT_RENAME, 'gpt set-flags <if> <dev> <part> <val>' has argc == 6 and will be rejected up front. Please extend the early argc check so the new subcommands can actually be reached.

cmd/gpt.c static int do_gpt(struct cmd_tbl *cmdtp, int flag, int argc, char *const argv[]) · line 1238

Quoted 4 lines
cmd/gpt.c +2 -0
@@ -1176,6 +1237,23 @@ static int do_gpt(struct cmd_tbl *cmdtp, int flag, int argc, char *const argv[])
+             ret = do_get_gpt_flags(blk_dev_desc,
+                                    simple_strtol(argv[4], NULL, 0));

simple_strtol() has no failure indication - a typo in the partition number silently becomes 0 and writes flags to the wrong partition. The existing code at the top of do_gpt() uses dectoul() with an endptr check for argv[3] - please do the same for argv[4] (and argv[5] in the set case) and reject non-numeric input.

cmd/gpt.c static int do_gpt(struct cmd_tbl *cmdtp, int flag, int argc, char *const argv[]) · line 1240

Quoted 6 lines
cmd/gpt.c +4 -0
@@ -1176,6 +1237,23 @@ static int do_gpt(struct cmd_tbl *cmdtp, int flag, int argc, char *const argv[])
+             ret = do_set_gpt_flags(blk_dev_desc, /* argv[2,3] */
+                                    simple_strtol(argv[4], NULL, 0),
+                                    argv[1],
+                                    simple_strtol(argv[5], NULL, 0));

The /* argv[2,3] */ comment is cryptic - argv[2] and argv[3] have already been consumed via blk_get_dev() above. Please drop it.

cmd/gpt.c U_BOOT_CMD(gpt, CONFIG_SYS_MAXARGS, 1, do_gpt, · line 1320

Quoted 8 lines
cmd/gpt.c +6 -0
@@ -1237,4 +1315,23 @@ U_BOOT_CMD(gpt, CONFIG_SYS_MAXARGS, 1, do_gpt,
+#ifdef CONFIG_CMD_GPT_FLAGS
+     "gpt partition boot flag manipulation:\n"
+     " gpt get-flags <interface> <dev> <partid>\n"
+     "    - read top 16-bit of partition attributes (bits 48:63)\n"
+     " gpt set-flags <interface> <dev> <partid> <flags-in-hex>\n"
+     "    - write top 16-bit of partition attributes (bits 48:63)\n"

Minor: 'top 16-bit' reads as singular - please use 'top 16 bits'. The flags occupy bits 48..63 in the on-disk attribute, but the helpers and the command operate on a u16 - worth saying so explicitly so users do not try to pass a 64-bit value.

cmd/Kconfig config CMD_GPT · line 1325

Quoted 9 lines
cmd/Kconfig +7 -0
@@ -1319,6 +1319,14 @@ config CMD_GPT
+config CMD_GPT_FLAGS
+     bool "GPT partition flags commands"
+     depends on CMD_GPT
+     select PARTITION_TYPE_GUID
+     help
+       Enables the GUID partition table attributes manipulation (bits 48:63)
+       in a ChromiumOS-style manner.

Just to check - why select PARTITION_TYPE_GUID? The new subcommands operate on the attribute u64 via {read,write}_disk_flags() and do not appear to look at the partition-type GUID, even though I assume other things would break without it. If the dependency is real, please add a sentence to the help text; if not, drop the select.

Regards,
Simon

#10 lore raw

Re: [PATCH v1 4/8] cmd: gpt: enable extension to manipulate GPT PTE attributes

In reply to [email protected]

Hi Denis,

Quoted 86 lines · On 6/5/26 5:39 AM, [email protected] wrote:

From: Denis Mukhin <[email protected]>

Enable extension for existing 'gpt' command to manipulate top 16 bits of GPT PTE attributes to assist ChromiumOS-style boot counters.

Follow the existing syntax for specifying <interface, device, partition> tuple.

---
  cmd/Kconfig |  8 +++++
  cmd/gpt.c   | 97 +++++++++++++++++++++++++++++++++++++++++++++++++++++
  2 files changed, 105 insertions(+)
cmd/Kconfig +8 -0
index c71c6824a196..83409e60da8b 100644
@@ -1319,6 +1319,14 @@ config CMD_GPT
  	  Enable the 'gpt' command to ready and write GPT style partition
  	  tables.
 
+config CMD_GPT_FLAGS
+	bool "GPT partition flags commands"
+	depends on CMD_GPT
+	select PARTITION_TYPE_GUID
+	help
+	  Enables the GUID partition table attributes manipulation (bits 48:63)
+	  in a ChromiumOS-style manner.
+
  config CMD_GPT_RENAME
  	bool "GPT partition renaming commands"
  	depends on CMD_GPT
cmd/gpt.c +38 -0
index 205f696c8072..4d55cb588ac2 100644
@@ -11,6 +11,9 @@
   */
 
  #include <blk.h>
+#ifdef CONFIG_CMD_GPT_FLAGS
+#include <cros_flags.h>
+#endif
  #include <env.h>
  #include <log.h>
  #include <malloc.h>
@@ -1116,6 +1119,64 @@ out:
  }
  #endif
 
+#ifdef CONFIG_CMD_GPT_FLAGS
+static int do_get_gpt_flags(struct blk_desc *desc, int partnum)
+{
+	u16 guid_flags;
+	int rc;
+
+	rc = read_disk_flags(desc, partnum, &guid_flags);
+	if (rc < 0) {
+		printf("ERROR: partnum %d: failed to read GPT flags: %d\n",
+		       partnum, rc);
+		return CMD_RET_FAILURE;
+	}
+
+	printf("P=%d S=%d T=%d\n",
+	       cros_flags_get_priority(guid_flags),
+	       cros_flags_get_successful(guid_flags),
+	       cros_flags_get_tries(guid_flags));
+
+	return 0;
+}
+
+static int do_set_gpt_flags(struct blk_desc *desc, int partnum,
+                            const char *key, int value)
+{
+	u16 guid_flags;
+	int rc;
+
+	rc = read_disk_flags(desc, partnum, &guid_flags);
+	if (rc < 0) {
+		printf("ERROR: partnum %d: failed to read GPT flags: %d\n",
+		       partnum, rc);
+		return CMD_RET_FAILURE;
+	}
+
+	if (!strcmp(key, "set-flags")) {

cmd/gpt.c out: · line 1166

Quoted 10 lines
cmd/gpt.c +10 -0
+		guid_flags = (u16)(value & 0xffff);
+	} else if (!strcmp(key, "set-priority")) {
+		guid_flags = cros_flags_set_priority(guid_flags, value);
+	} else if (!strcmp(key, "set-successful")) {
+		guid_flags = cros_flags_set_successful(guid_flags, value);
+	} else if (!strcmp(key, "set-tries")) {
+		guid_flags = cros_flags_set_tries(guid_flags, value);
+	} else {
+		printf("ERROR: unsupported command '%s'\n", key);
+		return CMD_RET_FAILURE;

CMD_RET_USAGE

Quoted 17 lines
cmd/gpt.c +13 -0
+	}
+
+	rc = write_disk_flags(desc, partnum, guid_flags);
+	if (rc < 0) {
+		pr_err("ERROR: partnum %d: failed to write GPT flags: %d\n",
+               partnum, rc);
+		return CMD_RET_FAILURE;
+    }
+
+	return 0;
+}
+#endif /* CONFIG_CMD_GPT_FLAGS */
+
  /**
   * do_gpt(): Perform GPT operations
   *

cmd/gpt.c static int do_gpt(struct cmd_tbl *cmdtp, int flag, int argc, char *const argv[]) · line 1244

Quoted 8 lines
cmd/gpt.c +5 -0
@@ -1176,6 +1237,23 @@ static int do_gpt(struct cmd_tbl *cmdtp, int flag, int argc, char *const argv[])
  		ret = do_rename_gpt_parts(blk_dev_desc, argv[1], argv[4], argv[5]);
  	} else if ((strcmp(argv[1], "set-bootable") == 0)) {
  		ret = gpt_set_bootable(blk_dev_desc, argv[4]);
+#endif
+#ifdef CONFIG_CMD_GPT_FLAGS
+	} else if (!strcmp(argv[1], "get-flags")) {
+		if (argc < 5)
+			return CMD_RET_FAILURE;

CMD_RET_USAGE

cmd/gpt.c static int do_gpt(struct cmd_tbl *cmdtp, int flag, int argc, char *const argv[]) · line 1252

Quoted 8 lines
cmd/gpt.c +8 -0
+		ret = do_get_gpt_flags(blk_dev_desc,
+		                       simple_strtol(argv[4], NULL, 0));
+	} else if (!strcmp(argv[1], "set-flags") ||
+		   !strcmp(argv[1], "set-priority") ||
+		   !strcmp(argv[1], "set-successful") ||
+		   !strcmp(argv[1], "set-tries")) {
+		if (argc < 6)
+			return CMD_RET_FAILURE;

CMD_RET_USAGE

Quoted 13 lines
cmd/gpt.c +6 -0
+		ret = do_set_gpt_flags(blk_dev_desc, /* argv[2,3] */
+		                       simple_strtol(argv[4], NULL, 0),
+		                       argv[1],
+		                       simple_strtol(argv[5], NULL, 0));
  #endif
  	} else {
  		return CMD_RET_USAGE;
@@ -1237,4 +1315,23 @@ U_BOOT_CMD(gpt, CONFIG_SYS_MAXARGS, 1, do_gpt,
  	" gpt set-bootable mmc 0 boot_a,boot_b\n"
  	" gpt transpose mmc 0 1 2\n"
  #endif
+#ifdef CONFIG_CMD_GPT_FLAGS
+	"gpt partition boot flag manipulation:\n"

cmd/gpt.c U_BOOT_CMD(gpt, CONFIG_SYS_MAXARGS, 1, do_gpt, · line 1329

Quoted 10 lines
cmd/gpt.c +10 -0
+	" gpt get-flags <interface> <dev> <partid>\n"
+	"    - read top 16-bit of partition attributes (bits 48:63)\n"
+	" gpt set-flags <interface> <dev> <partid> <flags-in-hex>\n"
+	"    - write top 16-bit of partition attributes (bits 48:63)\n"
+	" gpt set-successful <interface> <dev> <partid> {0,1}\n"
+	"    - set 'boot successful' bit\n"
+	" gpt set-priority <interface> <dev> <partid> [0..15]\n"
+	"    - set priority; 0 - non-bootable\n"
+	" gpt set-tries <interface> <dev> <partid> [0..15]\n"
+	"    - set tries (boot counter)\n"

These are the first subcommands that use "partid" as an argument identifier, can you rather use a name we've already used in another subcommand (if applicable).

I'm wondering if we need the additional get-/set- prefix? It could be enough to pass a 4th argument to know whether we want a get or a set. Either way is fine I guess.

I really recommend to not go with decimal values, U-Boot has a mix of hexadecimal and base 10 numbers all over the place but we typically say the default should be hex, c.f. https://docs.u-boot-project.org/en/latest/usage/cmdline.html#representing-numbers

In any case, the base needs to be made explicit in doc/usage/cmd/gpt.rst so there is no possible confusion.

cmd/gpt.c U_BOOT_CMD(gpt, CONFIG_SYS_MAXARGS, 1, do_gpt, · line 1335

Quoted 6 lines
cmd/gpt.c +6 -0
+	" Example usage:\n"
+	" gpt get-flags nvme 0 1\n"
+	" gpt set-flags nvme 0 1 0xff\n"
+	" gpt set-priority nvme 0 1 0\n"
+	" gpt set-successful nvme 0 1 1\n"
+	" gpt set-tries nvme 0 1 15\n"

I don't think we want examples in the help text in U-Boot, it'll unnecessarily increase the size of the binary. I see we have it for other gpt subcommands so maybe that's fine /me shrugs.

We absolutely do need an update to doc/usage/cmd/gpt.rst to reflect those changes though (and there you can have examples!).

Cheers,
Quentin

#11 lore raw

PATCH 5/8 configs: sandbox: enable GPT flags extension

In reply to [email protected]

From: Denis Mukhin <[email protected]>

Enable CONFIG_CMD_GPT_FLAGS to allow users experiment with GPT flags manipulation via 'gpt' command.

---
 configs/sandbox_defconfig | 1 +
 1 file changed, 1 insertion(+)
configs/sandbox_defconfig +1 -0
index ba800f7d19da..07d3645b3d33 100644
@@ -87,6 +87,7 @@ CONFIG_CMD_GPIO=y
 CONFIG_CMD_GPIO_READ=y
 CONFIG_CMD_PWM=y
 CONFIG_CMD_GPT=y
+CONFIG_CMD_GPT_FLAGS=y
 CONFIG_CMD_GPT_RENAME=y
 CONFIG_CMD_IDE=y
 CONFIG_CMD_I2C=y
Signature · 1 line
2.54.0
Reviewed-by on v1 5/8
#12 lore raw

Re: [PATCH v1 5/8] configs: sandbox: enable GPT flags extension

Quoted 9 lines · None

configs: sandbox: enable GPT flags extension

Enable CONFIG_CMD_GPT_FLAGS to allow users experiment with GPT flags manipulation via 'gpt' command.

configs/sandbox_defconfig | 1 +

 1 file changed, 1 insertion(+)
#13 lore raw

PATCH 6/8 bootcount: gpt: introduce GPT-backed bootcount device

In reply to [email protected]

From: Denis Mukhin <[email protected]>

Introduce bootcount device backed by GPT PTE attributes (bits 48:63).

---
 drivers/bootcount/Kconfig         |   7 ++
 drivers/bootcount/Makefile        |   1 +
 drivers/bootcount/bootcount-gpt.c | 140 ++++++++++++++++++++++++++++++
 3 files changed, 148 insertions(+)
 create mode 100644 drivers/bootcount/bootcount-gpt.c
drivers/bootcount/Kconfig +7 -0
index 4c0c8d89bb40..eccd610e3390 100644
@@ -171,6 +171,13 @@ config DM_BOOTCOUNT_ZYNQMP
 	  Enable support for the bootcount API by utilising the Persistent
 	  Global General Storage Register 2 of the PMU.
 
+config DM_BOOTCOUNT_GPT
+	bool "Support GPT PTE-backed boot counter"
+	depends on EFI_PARTITION
+	help
+	  Add support for maintaining boot count within bits 48:63 of GUID
+	  partition table entry attributes (similarly to ChromiumOS).
+
 endmenu
 
 endif
drivers/bootcount/Makefile +1 -0
index 0cf79e428d68..927694979801 100644
@@ -17,3 +17,4 @@ obj-$(CONFIG_DM_BOOTCOUNT_I2C)	+= bootcount_dm_i2c.o
 obj-$(CONFIG_DM_BOOTCOUNT_SPI_FLASH)	+= spi-flash.o
 obj-$(CONFIG_DM_BOOTCOUNT_SYSCON) += bootcount_syscon.o
 obj-$(CONFIG_DM_BOOTCOUNT_ZYNQMP)	+= bootcount_zynqmp.o
+obj-$(CONFIG_DM_BOOTCOUNT_GPT)	+= bootcount-gpt.o
drivers/bootcount/bootcount-gpt.c new file +140 -0

140 lines not on this page · raw message

Signature · 1 line
2.54.0
#14 lore raw

Re: [PATCH v1 6/8] bootcount: gpt: introduce GPT-backed bootcount device

Hi Denis,

Quoted 15 lines · None

bootcount: gpt: introduce GPT-backed bootcount device

Introduce bootcount device backed by GPT PTE attributes (bits 48:63).

drivers/bootcount/Kconfig | 7 ++

 drivers/bootcount/Makefile        |   1 +
 drivers/bootcount/bootcount-gpt.c | 140 ++++++++++++++++++++++++++++++++++++++
 3 files changed, 148 insertions(+)
drivers/bootcount/bootcount-gpt.c +2 -0
@@ -0,0 +1,140 @@
+static int bootcount_gpt_get(struct udevice *dev, u32 *val)
+{

drivers/bootcount/bootcount-gpt.c line 12

Quoted 10 lines
drivers/bootcount/bootcount-gpt.c +10 -0
+     struct bootcount_gpt_priv *priv = dev_get_priv(dev);
+     int rc;
+
+     if (!priv->blk_desc) {
+             rc = bootcount_gpt_find(dev);
+             if (rc < 0)
+                     return rc;
+     }
+
+     rc = read_disk_flags(priv->blk_desc, priv->partnum, (u16 *)val);

Not portable. read_disk_flags() writes 16 bits at the address; on big-endian that lands in the high half of *val, and the other half is left uninitialised either way. Please use a local u16, then *val = local

Quoted 6 lines
drivers/bootcount/bootcount-gpt.c +4 -0
@@ -0,0 +1,140 @@
+static int bootcount_gpt_set(struct udevice *dev, const u32 val)
+{
+     struct bootcount_gpt_priv *priv = dev_get_priv(dev);
+     int rc;

drivers/bootcount/bootcount-gpt.c line 14

Quoted 10 lines
drivers/bootcount/bootcount-gpt.c +10 -0
+
+     if (!priv->blk_desc) {
+             rc = bootcount_gpt_find(dev);
+             if (rc < 0)
+                     return rc;
+     }
+
+     pr_debug("partnum %d: write GPT flags 0x%04x\n", priv->partnum, val);
+
+     rc = write_disk_flags(priv->blk_desc, priv->partnum, (u16)val);

The driver passes the raw 16 bits straight through but does not use the cros_flags helpers added in patch 1. The standard generic path is roughly:

   val = bootcount_load();
   bootcount_store(val + 1);

Against this driver, that increments the whole 16-bit attribute word, so priority (bits 0..3), successful (bit 8) etc. all get clobbered the moment the tries field overflows nibble 1. Either the get/set ops should isolate the tries field via cros_flags_get_tries()/cros_flags_set_tries(), or the commit message needs to spell out that this driver is intended only for callers that know the cgpt layout and that bootcount_inc()/bootcount_error() must not be used (which is what patch 8 enforces). As it stands the relationship between this driver and patch 1 is not obvious.

Quoted 13 lines
drivers/bootcount/bootcount-gpt.c +11 -0
@@ -0,0 +1,140 @@
+static int bootcount_gpt_probe(struct udevice *dev)
+{
+     struct bootcount_gpt_priv *priv = dev_get_priv(dev);
+     const char *str;
+     int uclass_id, devnum;
+
+     str = dev_read_string(dev, 'device-type');
+     if (!str)
+             return -ENOSYS;
+
+     uclass_id = uclass_get_by_name(str);

drivers/bootcount/bootcount-gpt.c line 21

Quoted 10 lines
drivers/bootcount/bootcount-gpt.c +10 -0
+     if (uclass_id == UCLASS_INVALID)
+             return -ENOSYS;
+
+     devnum = dev_read_s32_default(dev, 'devnum', 0);
+     if (devnum < 0)
+             return -ENOSYS;
+
+     str = dev_read_string(dev, 'partition-name');
+     if (!str)
+             return -ENOSYS;

Please move the DT parsing into an of_to_plat() callback (and a plat struct), matching bootcount-syscon and the rest of DM. Also -ENOSYS is the wrong errno for missing/invalid DT properties - use -EINVAL. dev_err() rather than pr_err()/pr_warn() throughout would identify which bootcount device is complaining.

drivers/bootcount/bootcount-gpt.c line 5

Quoted 7 lines
drivers/bootcount/bootcount-gpt.c +5 -0
@@ -0,0 +1,140 @@
+     if (info.bootable & PART_BOOTABLE) {
+             pr_warn("partition %s: legacy boot flag enabled, no GPT accounting\n",
+                     priv->partition_name);
+             return -EPERM;
+     }

Why does the legacy-BIOS bootable bit disqualify the partition from holding a counter in the top 16 bits of attributes? Those bits are independent of bit 2. If there is a real reason please put it in a comment; otherwise drop the check. Also -EPERM reads oddly here.

drivers/bootcount/Kconfig config DM_BOOTCOUNT_ZYNQMP · line 176

Quoted 8 lines
drivers/bootcount/Kconfig +6 -0
@@ -171,6 +171,13 @@ config DM_BOOTCOUNT_ZYNQMP
+config DM_BOOTCOUNT_GPT
+     bool "Support GPT PTE-backed boot counter"
+     depends on EFI_PARTITION
+     help
+       Add support for maintaining boot count within bits 48:63 of GUID
+       partition table entry attributes (similarly to ChromiumOS).

The commit message could explain the motivation (why GPT, why bits 48:63, relationship to the cgpt layout) rather than restating the subject line.

drivers/bootcount/bootcount-gpt.c line 5

Quoted 7 lines
drivers/bootcount/bootcount-gpt.c +5 -0
@@ -0,0 +1,140 @@
+     priv->blk_desc = dev_get_uclass_plat(bdev);
+     priv->partnum = partnum;
+
+     return 0;
+}

blk_desc and partnum are cached on first get/set and never refreshed. If the underlying block device is re-probed (or the disk re-partitioned) the driver keeps using a stale descriptor. At minimum worth a comment; ideally a .remove that clears the cache, or simply re-resolve on every call since this is not a hot path.

Regards,
Simon

#15 lore raw

PATCH 7/8 bootcount: gpt: ensure coreboot payload enables DM_BOOTCOUNT

In reply to [email protected]

From: Denis Mukhin <[email protected]>

Ensure that bootcount-gpt device is enabled in U-Boot when running as a coreboot payload.

---
 drivers/bootcount/Kconfig | 3 ++-
 1 file changed, 2 insertions(+), 1 deletion(-)
drivers/bootcount/Kconfig +2 -1
index eccd610e3390..70ef44a0dc82 100644
@@ -13,7 +13,8 @@ if BOOTCOUNT_LIMIT
 choice
 	prompt "Boot count device"
 	default BOOTCOUNT_AM33XX if AM33XX || SOC_DA8XX
-	default BOOTCOUNT_GENERIC
+	default BOOTCOUNT_GENERIC if !VENDOR_COREBOOT
+	default DM_BOOTCOUNT if VENDOR_COREBOOT
 
 config BOOTCOUNT_GENERIC
 	bool "Generic default boot counter"
Signature · 1 line
2.54.0
#16 lore raw

Re: [PATCH v1 7/8] bootcount: gpt: ensure coreboot payload enables DM_BOOTCOUNT

Hi Denis,

Quoted 12 lines · None

bootcount: gpt: ensure coreboot payload enables DM_BOOTCOUNT

Ensure that bootcount-gpt device is enabled in U-Boot when running as a coreboot payload.

drivers/bootcount/Kconfig | 3 ++-

 1 file changed, 2 insertions(+), 1 deletion(-)

drivers/bootcount/Kconfig if BOOTCOUNT_LIMIT · line 17

Quoted 6 lines
drivers/bootcount/Kconfig +2 -1
@@ -13,7 +13,8 @@ if BOOTCOUNT_LIMIT
 choice
      prompt "Boot count device"
      default BOOTCOUNT_AM33XX if AM33XX || SOC_DA8XX
-     default BOOTCOUNT_GENERIC
+     default BOOTCOUNT_GENERIC if !VENDOR_COREBOOT
+     default DM_BOOTCOUNT if VENDOR_COREBOOT

The subject says 'ensure bootcount-gpt is enabled' but this hunk selects DM_BOOTCOUNT, the generic DM choice - not DM_BOOTCOUNT_GPT. Please reconcile the two and spell out in the message what actually happens...the user still has to turn on DM_BOOTCOUNT_GPT under the DM_BOOTCOUNT menu.

The motivation is also missing. Why should every VENDOR_COREBOOT build silently switch its default from BOOTCOUNT_GENERIC to DM_BOOTCOUNT? Coreboot runs on many platforms with no GPT disk and no use for a DM bootcount, and changing a choice default will move existing configs on the next defconfig refresh. That seems heavy-handed for what is really 'a Ford/Chromium platform wants GPT bootcount' - wouldn't it be cleaner to set this in the relevant board defconfig rather than taking over the global default for all coreboot payloads? What do you think?

Regards,
Simon

#17 lore raw

PATCH 8/8 bootcount: gate bootcount_{inc, error}() behind !CONFIG_DM_BOOTCOUNT_GPT

In reply to [email protected]

From: Denis Mukhin <[email protected]>

Autoboot calls into bootcount_inc()/bootcount_error() which rely on environment variable.

Specifically, it may corrupt the state of a GPT-backed counter.

Gate bootcount_inc()/bootcount_error() by !CONFIG_DM_BOOTCOUNT_GPT to compile out bootcount_store() calls on autoboot path.

---
 include/bootcount.h | 3 ++-
 1 file changed, 2 insertions(+), 1 deletion(-)
include/bootcount.h +2 -1
index 86474569d365..8aae3cb7ff1b 100644
@@ -74,7 +74,8 @@ void bootcount_store(ulong);
  */
 ulong bootcount_load(void);
 
-#if defined(CONFIG_SPL_BOOTCOUNT_LIMIT) || defined(CONFIG_TPL_BOOTCOUNT_LIMIT) || defined(CONFIG_BOOTCOUNT_LIMIT)
+#if (defined(CONFIG_SPL_BOOTCOUNT_LIMIT) || defined(CONFIG_TPL_BOOTCOUNT_LIMIT) || defined(CONFIG_BOOTCOUNT_LIMIT)) && \
+	!defined(CONFIG_DM_BOOTCOUNT_GPT)
 
 #ifdef CONFIG_SYS_BOOTCOUNT_LE
 static inline void raw_bootcount_store(volatile u32 *addr, u32 data)
Signature · 1 line
2.54.0
#18 lore raw

Re: [PATCH v1 8/8] bootcount: gate bootcount_{inc, error}() behind !CONFIG_DM_BOOTCOUNT_GPT

Hi Denis,

Quoted 17 lines · None

bootcount: gate bootcount_{inc, error}() behind !CONFIG_DM_BOOTCOUNT_GPT

Autoboot calls into bootcount_inc()/bootcount_error() which rely on environment variable.

Specifically, it may corrupt the state of a GPT-backed counter.

Gate bootcount_inc()/bootcount_error() by !CONFIG_DM_BOOTCOUNT_GPT to compile out bootcount_store() calls on autoboot path.

include/bootcount.h | 3 ++-

 1 file changed, 2 insertions(+), 1 deletion(-)

include/bootcount.h void bootcount_store(ulong); · line 78

Quoted 6 lines
include/bootcount.h +2 -1
@@ -74,7 +74,8 @@ void bootcount_store(ulong);
  */
 ulong bootcount_load(void);
 
-#if defined(CONFIG_SPL_BOOTCOUNT_LIMIT) || defined(CONFIG_TPL_BOOTCOUNT_LIMIT) || defined(CONFIG_BOOTCOUNT_LIMIT)
+#if (defined(CONFIG_SPL_BOOTCOUNT_LIMIT) || defined(CONFIG_TPL_BOOTCOUNT_LIMIT) || defined(CONFIG_BOOTCOUNT_LIMIT)) && \
+     !defined(CONFIG_DM_BOOTCOUNT_GPT)

Hmm this needs a Kconfig solution...I don't like baking a specific driver's symbol into the generic bootcount header - it inverts the usual layering (the uclass header should not know about its drivers) and it does not scale: the next backend that wants the same behaviour has to add another '&& !defined(...)' here.

This also gates the inline helpers everywhere, not just on the autoboot path. The SPL increment at common/spl/spl.c:792 becomes a no-op too, as does any board code calling bootcount_inc()/bootcount_error(). That is a much bigger behavioural change than the subject suggests, and should be called out in the commit message.

Instead, how about you introduce a generic Kconfig (BOOTCOUNT_NO_INCREMENT, selected by DM_BOOTCOUNT_GPT) so the gate stays driver-agnostic, or handle it inside bootcount_store()/the uclass so the increment is silently dropped when the backend implements its own semantics. What do you think?

include/bootcount.h void bootcount_store(ulong); · line 78

Quoted 8 lines
include/bootcount.h +2 -1
@@ -74,7 +74,8 @@ void bootcount_store(ulong);
  */
 ulong bootcount_load(void);
 
-#if defined(CONFIG_SPL_BOOTCOUNT_LIMIT) || defined(CONFIG_TPL_BOOTCOUNT_LIMIT) || defined(CONFIG_BOOTCOUNT_LIMIT)
+#if (defined(CONFIG_SPL_BOOTCOUNT_LIMIT) || defined(CONFIG_TPL_BOOTCOUNT_LIMIT) || defined(CONFIG_BOOTCOUNT_LIMIT)) && \
+     !defined(CONFIG_DM_BOOTCOUNT_GPT)

The commit message says "may corrupt the state of a GPT-backed counter" but does not explain why - please spell out that the top 16 bits encode structured ChromiumOS flags (priority/tries/successful), so a plain ++ on the loaded value scrambles those fields rather than producing a meaningful count.

Subject also has a stray space inside the brace expansion:
'bootcount_{inc, error}()' - please make it 'bootcount_{inc,error}()' to match the body.

Regards,
Simon

#19 lore raw
In reply to [email protected]

Hi Denis,

Quoted 2 lines · None

This series adds new GPT-backed bootcount device with ChromiumOS-inspired counters/flags logic [1].

There is no update to doc/api/bootcount.rst describing the new backend, how to wire it into the DT, or the autoboot interaction introduced by patch 8. Please add a short section so users can discover it.

The use of include/cros_flags.h consumed by generic disk and bootcount code feels awkward - the bootcount driver itself does not include cros_flags.h, only the gpt command does, and read/write_disk_flags() operate on plain u16. It might be cleaner to put the generic 16-bit accessors in part.h (or a new gpt_flags.h) and keep the cros_* wrappers in cros_flags.h for the cgpt-compatible command surface only. What do you think?

Quoted 2 lines

Patch 6 introduces the new bootcount device with compatible
'u-boot,bootcount-gpt'

I can't find a device-tree binding document for the new compatible. The other bootcount drivers have entries under doc/device-tree-bindings/ (e.g. bootcount-syscon.txt, misc/bootcounter.txt) - please add one describing 'u-boot,bootcount-gpt' and its device-type, devnum and partition-name properties.

Regards,
Simon