Skip to contentSkip to conversation

GPT over MTD

PATCH Superseded Patrick Delaunay 4 participants 29 messages May 11, 2017 – Jun 6, 2017

Subsystems (2)
lore
Share & tools
lore
#0 lore raw

v6 cover GPT over MTD

I have a request to support GPT over MTD to have the MTD informations without U-Boot environment(CONFIG_ENV_IS_NOWHERE is a other requirement of my project to manage several board configuration with
the same defconfig; boot from NAND or NOR or SDCARD).

The current idea is to use the GPT header format to save the partitioning information directly in flash device (NOR or NAND), then the U-Boot MTD variables are rebuild from these GPT partitions and can be used by DISTRO command to search bootable binary.

This information are directly used by BootRom to locate and load the first bootstage.

The added code is under a new CONFIG : CONFIG_EFI_PARTITION_MTD and in the new file "disk/part_efi_mtd.c"

For implementation details, see doc/README.gpt.mtd

TODO: full support for modified command (if needed)
today I limit the support for the command expected used by DISTRO macro:
+ gpt write
+ part list

the current level is tested on sandbox with

- test/fs/gpt_mtd-test.sh

or manually with :

Quoted 4 lines

make O=sandbox sandbox_defconfig
make O=sandbox
dd if=/dev/zero of=spi.bin bs=1M count=4 sandbox/u-boot -d sandbox/arch/sandbox/dts/test.dtb

=> sf probe
SF: Detected M25P16 with page size 256 Bytes, erase size 64 KiB, total 2 MiB

=> setenv nor_part "name=part1,size=64KiB;name=part2,size=128KiB;\
name=boot,type=linux,bootable,size=512KiB"
=> gpt write nor 0 $nor_part
Writing GPT: success!

=> part list nor 0
Part Start LBA End LBA Name
Attributes
Type GUID
Partition GUID
1 0x00000080 0x000000ff "part1"

	attrs:	0x0000000000000000
	type:	ebd0a0a2-b9e5-4433-87c0-68b6b72699c7
	type:	data
	guid:	d117f98e-6f2c-0040-00b2-331a19f91cb2
  2	0x00000100	0x000001ff	"part2"
	attrs:	0x0000000000000000
	type:	ebd0a0a2-b9e5-4433-87c0-68b6b72699c7
	type:	data
	guid:	25718777-d0ad-0000-8060-02cb591c9737
  3	0x00000200	0x000005ff	"boot"
	attrs:	0x0000000000000004
	type:	0fc63daf-8483-4772-8e79-3d69d8477de4
	type:	linux
	guid:	8a4bb8b4-e304-0040-0036-aff5c9c495b1

=> setenv mtdids "nor0=nor0"
=> setenv mtdparts "mtdparts="
=> mtdparts gpt nor0
=> mtdparts
device nor0 <nor0>, # parts = 3
#: name size offset mask_flags

 0: part1               0x00010000	0x00010000	0
 1: part2               0x00020000	0x00020000	0
 2: boot                0x00080000	0x00040000	0

active partition: nor0,0 - (part1) 0x00010000 @ 0x00010000

defaults:
mtdids : none
mtdparts: none

=> part list nor 0 -bootable nor_boot_part
=> printenv nor_boot_part
nor_boot_part=3

test filtering feature (can be used to mask some partitions to linux according the GUID)
1/ only add MTD for some guid = linux
2/ then add MTD for some guid = data

=> mtdparts delall
=> setenv mtdparts "mtdparts="
=> setenv mtdids "nor0=nor0"
=> mtdparts gpt nor0 linux
=> mtdparts

device nor0 <nor0>, # parts = 1
#: name size offset mask_flags
0: boot 0x00080000 0x00040000 0

active partition: nor0,0 - (boot) 0x00080000 @ 0x00040000

defaults:
mtdids : none
mtdparts: none

=> mtdparts gpt nor0 data
=> mtdparts

device nor0 <nor0>, # parts = 3
#: name size offset mask_flags

 0: part1               0x00010000	0x00010000	0
 1: part2               0x00020000	0x00020000	0
 2: boot                0x00080000	0x00040000	0

active partition: nor0,2 - (boot) 0x00080000 @ 0x00040000

defaults:
mtdids : none
mtdparts: none

Changes in v6:
- rebase of the top of the latest v2017.05
- add comment for function part_print_gpt and part_print_efi
- add comment for function part_get_disk_info
- add comment for function prepare_backup_gpt_header
- add export function comment in part_efi_int.h
- invalid offset my_lba in is_secondary_gpt_valid_mtd() the secondary GPT header wasn't detected
- add comment for functions
- add distro support for UBI volume in GPT partition in MTD device
- add sandbox test (test/fs/gpt_mtd-test.sh) for modified commands

Changes in v5:
- rebase of the top of latest u-boot master
- solve warning and error
(dont' include linux/mtd/mtd.h in part.h)

Changes in v4:
- change RFC to PATCH
- update cover letter
- rebase on v2017.03

Changes in v3:
- solve issue in compute partition_entry_lba update :
missing / lba_size
- rework and split patches for unrelated change after review from Cristophe Kerello and Simon Glass
- rebase on v2017.03-rc1

Changes in v2:
- split serie to reduce patches size
(separate disk part and cmd impact and add precursor patch)
- split large function

Patrick Delaunay (18):
uuid: add a function for GUID string parsing
disk: part: refactor part_print_efi to prepare GPT over MTD
disk: part: refactor part_get_info_efi to prepare GPT over MTD
disk: part: prepare_backup_gpt_header rework
disk: part: compute partition_entry_lba
disk: part: efi: export internal function
disk: part: add support of GPT partitioning over MTD
cmd: gpt: remove blk_desc parameter in static function
cmd: gpt: use error number in set_gpt_info
cmd: gpt: add print_gpt_info_err
cmd: gpt: update write command to support GPT over MTD
cmd: part: update command to support GPT over MTD
cmd: mtdparts: move PART_ADD_DESC_MAXLEN
cmd: mtdparts: update command to support GPT over MTD
uuid: remove dependency with io.h
sandbox: GPT over MTD test
distro: add boot for UBI in GPT over MTD
tests: Add GPT over MTD test

 cmd/gpt.c                       | 152 +++++++++++---
 cmd/mtdparts.c                  | 144 ++++++++++++-
 cmd/part.c                      |  49 ++++-
 configs/sandbox_defconfig       |   3 +
 disk/Kconfig                    |  17 ++
 disk/Makefile                   |   1 +
 disk/part_efi.c                 | 152 +++++++-------
 disk/part_efi_int.h             |  70 ++++++
 disk/part_efi_mtd.c             | 456 ++++++++++++++++++++++++++++++++++++++++
 doc/README.gpt.mtd              | 187 ++++++++++++++++
 include/config_distro_bootcmd.h |  40 ++++
 include/configs/sandbox.h       |   6 +
 include/part.h                  |  42 +++-
 include/uuid.h                  |   1 +
 lib/uuid.c                      |  48 ++++-
 test/fs/gpt_mtd-test.sh         | 209 ++++++++++++++++++
 16 files changed, 1447 insertions(+), 130 deletions(-)
 create mode 100644 disk/part_efi_int.h
 create mode 100644 disk/part_efi_mtd.c
 create mode 100644 doc/README.gpt.mtd
 create mode 100755 test/fs/gpt_mtd-test.sh
Signature · 1 line
1.9.1
#1 lore raw

PATCH v6 1/18 uuid: add a function for GUID string parsing

uuid_guid_parse_str parses a string provided in commands line to found a valid GUID as valid uuid string or as shortcut (under CONFIG_PARTITION_TYPE_GUID).

---

Changes in v6:
- rebase of the top of the latest v2017.05

Changes in v5:
- rebase of the top of latest u-boot master

Changes in v4:
- change RFC to PATCH
- update cover letter
- rebase on v2017.03

Changes in v3:
- solve issue in compute partition_entry_lba update :
missing / lba_size
- rework and split patches for unrelated change after review from Cristophe Kerello and Simon Glass
- rebase on v2017.03-rc1

Changes in v2:
- split serie to reduce patches size
(separate disk part and cmd impact and add precursor patch)
- split large function

 include/uuid.h |  1 +
 lib/uuid.c     | 35 +++++++++++++++++++++++++++++++++++
 2 files changed, 36 insertions(+)
include/uuid.h +1 -0
index c3f423f..f444095 100644
@@ -40,6 +40,7 @@ void uuid_bin_to_str(unsigned char *uuid_bin, char *uuid_str, int str_format);
 int uuid_guid_get_bin(const char *guid_str, unsigned char *guid_bin);
 int uuid_guid_get_str(unsigned char *guid_bin, char *guid_str);
 #endif
+int uuid_guid_parse_str(const char *str, char *guid_str);
 void gen_rand_uuid(unsigned char *uuid_bin);
 void gen_rand_uuid_str(char *uuid_str, int str_format);
 #endif
lib/uuid.c +35 -0
index c8584ed..7ce822c 100644
@@ -136,6 +136,41 @@ int uuid_guid_get_str(unsigned char *guid_bin, char *guid_str)
 #endif
 
 /*
+ * uuid_guid_parse_str() - this function parses string to found a valid GUID.
+ *
+ * @param guid_str - pointer to string with partition type guid [37B]
+ *                   or GUID shorcut defined in list_guid[]
+ * @param guid_str - pointer to allocated partition type string [37B]
+ * @return 0 if OK, else -ENODEV
+ */
+int uuid_guid_parse_str(const char *str, char *guid_str)
+{
+#ifdef CONFIG_PARTITION_TYPE_GUID
+	int i;
+#endif
+
+	if (guid_str == NULL)
+		return -ENODEV;
+
+	if (uuid_str_valid(str)) {
+		memcpy(guid_str, str, UUID_STR_LEN + 1);
+		return 0;
+	}
+
+#ifdef CONFIG_PARTITION_TYPE_GUID
+	for (i = 0; i < ARRAY_SIZE(list_guid); i++) {
+		if (!strcmp(list_guid[i].string, str)) {
+			uuid_bin_to_str((unsigned char *)list_guid[i].guid.b,
+					guid_str, UUID_STR_FORMAT_GUID);
+			return 0;
+		}
+	}
+#endif
+
+	return -ENODEV;
+}
+
+/*
  * uuid_str_to_bin() - convert string UUID or GUID to big endian binary data.
  *
  * @param uuid_str - pointer to UUID or GUID string [37B] or GUID shorcut
Signature · 1 line
1.9.1
#2 lore raw

PATCH v6 2/18 disk: part: refactor part_print_efi to prepare GPT over MTD

In reply to Patrick Delaunay

split part_print_efi : create sub-functions part_print_gpt()

---

Changes in v6:
- add comment for function part_print_gpt and part_print_efi

Changes in v5: None
Changes in v4: None
Changes in v3: None
Changes in v2: None

 disk/part_efi.c | 63 ++++++++++++++++++++++++++++++++++++---------------------
 1 file changed, 40 insertions(+), 23 deletions(-)
disk/part_efi.c +40 -23
index 1b7ba27..88e925b 100644
@@ -175,34 +175,18 @@ static void prepare_backup_gpt_header(gpt_header *gpt_h)
 
 #if CONFIG_IS_ENABLED(EFI_PARTITION)
 /*
- * Public Functions (include/part.h)
+ * part_print_gpt() - display GUID Partition Table information
+ *
+ * @param gpt_head - pointer to GPT header
+ * @param gpt_pte - pointer to GPT partion entry array
  */
-
-void part_print_efi(struct blk_desc *dev_desc)
+static void part_print_gpt(gpt_header *gpt_head,
+			   gpt_entry *gpt_pte)
 {
-	ALLOC_CACHE_ALIGN_BUFFER_PAD(gpt_header, gpt_head, 1, dev_desc->blksz);
-	gpt_entry *gpt_pte = NULL;
 	int i = 0;
 	char uuid[37];
 	unsigned char *uuid_bin;
 
-	/* This function validates AND fills in the GPT header and PTE */
-	if (is_gpt_valid(dev_desc, GPT_PRIMARY_PARTITION_TABLE_LBA,
-			 gpt_head, &gpt_pte) != 1) {
-		printf("%s: *** ERROR: Invalid GPT ***\n", __func__);
-		if (is_gpt_valid(dev_desc, (dev_desc->lba - 1),
-				 gpt_head, &gpt_pte) != 1) {
-			printf("%s: *** ERROR: Invalid Backup GPT ***\n",
-			       __func__);
-			return;
-		} else {
-			printf("%s: ***        Using Backup GPT ***\n",
-			       __func__);
-		}
-	}
-
-	debug("%s: gpt-entry at %p\n", __func__, gpt_pte);
-
 	printf("Part\tStart LBA\tEnd LBA\t\tName\n");
 	printf("\tAttributes\n");
 	printf("\tType GUID\n");
@@ -229,10 +213,39 @@ void part_print_efi(struct blk_desc *dev_desc)
 		uuid_bin_to_str(uuid_bin, uuid, UUID_STR_FORMAT_GUID);
 		printf("\tguid:\t%s\n", uuid);
 	}
+}
+
+/*
+ * part_print_efi() - display EFI Partition information
+ *
+ * @param dev_desc - pointer to block device descriptor
+ */
+void part_print_efi(struct blk_desc *dev_desc)
+{
+	ALLOC_CACHE_ALIGN_BUFFER_PAD(gpt_header, gpt_head, 1, dev_desc->blksz);
+	gpt_entry *gpt_pte = NULL;
+
+	/* This function validates AND fills in the GPT header and PTE */
+	if (is_gpt_valid(dev_desc, GPT_PRIMARY_PARTITION_TABLE_LBA,
+			 gpt_head, &gpt_pte) != 1) {
+		printf("%s: *** ERROR: Invalid GPT ***\n", __func__);
+		if (is_gpt_valid(dev_desc, (dev_desc->lba - 1),
+				 gpt_head, &gpt_pte) != 1) {
+			printf("%s: *** ERROR: Invalid Backup GPT ***\n",
+			       __func__);
+			return;
+		} else {
+			printf("%s: ***        Using Backup GPT ***\n",
+			       __func__);
+		}
+	}
+
+	debug("%s: gpt-entry at %p\n", __func__, gpt_pte);
+
+	part_print_gpt(gpt_head, gpt_pte);
 
 	/* Remember to free pte */
 	free(gpt_pte);
-	return;
 }
 
 int part_get_info_efi(struct blk_desc *dev_desc, int part,
@@ -349,6 +362,10 @@ static int set_protective_mbr(struct blk_desc *dev_desc)
 	return 0;
 }
 
+/*
+ * Public Functions (include/part.h)
+ */
+
 int write_gpt_table(struct blk_desc *dev_desc,
 		gpt_header *gpt_h, gpt_entry *gpt_e)
 {
Signature · 1 line
1.9.1
#3 lore raw

PATCH v6 3/18 disk: part: refactor part_get_info_efi to prepare GPT over MTD

In reply to Patrick Delaunay

create sub-functions part_get_disk_info()

---

Changes in v6:
- add comment for function part_get_disk_info

Changes in v5: None
Changes in v4: None
Changes in v3: None
Changes in v2: None

 disk/part_efi.c | 57 +++++++++++++++++++++++++++++++++++----------------------
 1 file changed, 35 insertions(+), 22 deletions(-)
disk/part_efi.c +35 -22
index 88e925b..16608e7 100644
@@ -216,6 +216,40 @@ static void part_print_gpt(gpt_header *gpt_head,
 }
 
 /*
+ * part_get_disk_info() - extract partition information for one GPT entry
+ *
+ * @param part - index of entry in gpt_pte
+ * @param blksz - blksize
+ * @param gpt_pte - pointer to GPT partion entry array
+ * @param gpt_pte - pointer to disk partition (output)
+ */
+static void part_get_disk_info(int part, unsigned int blksz,
+			       gpt_entry *gpt_pte, disk_partition_t *info)
+{
+	/* The 'lbaint_t' casting may limit the maximum disk size to 2 TB */
+	info->start = (lbaint_t)le64_to_cpu(gpt_pte[part - 1].starting_lba);
+	/* The ending LBA is inclusive, to calculate size, add 1 to it */
+	info->size = (lbaint_t)le64_to_cpu(gpt_pte[part - 1].ending_lba) + 1
+		     - info->start;
+	info->blksz = blksz;
+
+	sprintf((char *)info->name, "%s", print_efiname(&gpt_pte[part - 1]));
+	strcpy((char *)info->type, "U-Boot");
+	info->bootable = is_bootable(&gpt_pte[part - 1]);
+#if CONFIG_IS_ENABLED(PARTITION_UUIDS)
+	uuid_bin_to_str(gpt_pte[part - 1].unique_partition_guid.b, info->uuid,
+			UUID_STR_FORMAT_GUID);
+#endif
+#ifdef CONFIG_PARTITION_TYPE_GUID
+	uuid_bin_to_str(gpt_pte[part - 1].partition_type_guid.b,
+			info->type_guid, UUID_STR_FORMAT_GUID);
+#endif
+
+	debug("%s: start 0x" LBAF ", size 0x" LBAF ", name %s\n", __func__,
+	      info->start, info->size, info->name);
+}
+
+/*
  * part_print_efi() - display EFI Partition information
  *
  * @param dev_desc - pointer to block device descriptor
@@ -283,28 +317,7 @@ int part_get_info_efi(struct blk_desc *dev_desc, int part,
 		return -1;
 	}
 
-	/* The 'lbaint_t' casting may limit the maximum disk size to 2 TB */
-	info->start = (lbaint_t)le64_to_cpu(gpt_pte[part - 1].starting_lba);
-	/* The ending LBA is inclusive, to calculate size, add 1 to it */
-	info->size = (lbaint_t)le64_to_cpu(gpt_pte[part - 1].ending_lba) + 1
-		     - info->start;
-	info->blksz = dev_desc->blksz;
-
-	sprintf((char *)info->name, "%s",
-			print_efiname(&gpt_pte[part - 1]));
-	strcpy((char *)info->type, "U-Boot");
-	info->bootable = is_bootable(&gpt_pte[part - 1]);
-#if CONFIG_IS_ENABLED(PARTITION_UUIDS)
-	uuid_bin_to_str(gpt_pte[part - 1].unique_partition_guid.b, info->uuid,
-			UUID_STR_FORMAT_GUID);
-#endif
-#ifdef CONFIG_PARTITION_TYPE_GUID
-	uuid_bin_to_str(gpt_pte[part - 1].partition_type_guid.b,
-			info->type_guid, UUID_STR_FORMAT_GUID);
-#endif
-
-	debug("%s: start 0x" LBAF ", size 0x" LBAF ", name %s\n", __func__,
-	      info->start, info->size, info->name);
+	part_get_disk_info(part, dev_desc->blksz, gpt_pte, info);
 
 	/* Remember to free pte */
 	free(gpt_pte);
Signature · 1 line
1.9.1
#4 lore raw

PATCH v6 4/18 disk: part: prepare_backup_gpt_header rework

In reply to Patrick Delaunay

inverse the order of my_lba and alternate_lba values switch value => alternate_lba can be used in partition entry computation

---

Changes in v6: None
Changes in v5: None
Changes in v4: None
Changes in v3: None
Changes in v2: None

 disk/part_efi.c | 6 +++---
 1 file changed, 3 insertions(+), 3 deletions(-)
disk/part_efi.c +3 -3
index 16608e7..181a748 100644
@@ -161,9 +161,9 @@ static void prepare_backup_gpt_header(gpt_header *gpt_h)
 	uint64_t val;
 
 	/* recalculate the values for the Backup GPT Header */
-	val = le64_to_cpu(gpt_h->my_lba);
-	gpt_h->my_lba = gpt_h->alternate_lba;
-	gpt_h->alternate_lba = cpu_to_le64(val);
+	val = le64_to_cpu(gpt_h->alternate_lba);
+	gpt_h->alternate_lba = gpt_h->my_lba;
+	gpt_h->my_lba = cpu_to_le64(val);
 	gpt_h->partition_entry_lba =
 			cpu_to_le64(le64_to_cpu(gpt_h->last_usable_lba) + 1);
 	gpt_h->header_crc32 = 0;
Signature · 1 line
1.9.1
#5 lore raw

PATCH v6 5/18 disk: part: compute partition_entry_lba

In reply to Patrick Delaunay

the value for partition_entry_lba of secondary GPT (last usable lba + 1) is not correct with MTD support as last usable LBA need to be erase size aligned

The correct location of partition entry is always recomputed with the formula :
partition_entry_lba =
alternate_lba -
num_pte * sizeof_partition_entry / lba_size

---
the device mapping is

1/ for block case:

last_usable_lba -> partition_entry_lba - 1
partition_entry_lba -> PTE table (num * sizeof(PTE))
alternate_lba -> secondary GPT
-> end of device

2/ for MTD case : end of the last good block

-------------

last_usable_lba -> last good erase block - 1
-------------erase BLOCK aligned XXXXXXXXXXXXXXXXXX -> skipped LBA for MTD case partition_entry_lba -> PTE table (num * sizeof(PTE))
alternate_lba -> secondary GPT
-------------erase BLOCK aligned
xxxxx skipping bad block
->end of device

Changes in v6:
- add comment for function prepare_backup_gpt_header

Changes in v5: None
Changes in v4: None
Changes in v3: None
Changes in v2: None

 disk/part_efi.c | 16 +++++++++++-----
 1 file changed, 11 insertions(+), 5 deletions(-)
disk/part_efi.c +11 -5
index 181a748..23a582a 100644
@@ -155,7 +155,14 @@ static int validate_gpt_entries(gpt_header *gpt_h, gpt_entry *gpt_e)
 	return 0;
 }
 
-static void prepare_backup_gpt_header(gpt_header *gpt_h)
+/*
+ * prepare_backup_gpt_header() - change primary GPT header to backup GPT header
+ *
+ * @param gpt_h - pointer to GPT header (in=primary, out=secondary)
+ * @param pte_blk_cnt - number of block for size of partition entry array (PTE)
+ */
+static void prepare_backup_gpt_header(gpt_header *gpt_h,
+				      const int pte_blk_cnt)
 {
 	uint32_t calc_crc32;
 	uint64_t val;
@@ -164,8 +171,7 @@ static void prepare_backup_gpt_header(gpt_header *gpt_h)
 	val = le64_to_cpu(gpt_h->alternate_lba);
 	gpt_h->alternate_lba = gpt_h->my_lba;
 	gpt_h->my_lba = cpu_to_le64(val);
-	gpt_h->partition_entry_lba =
-			cpu_to_le64(le64_to_cpu(gpt_h->last_usable_lba) + 1);
+	gpt_h->partition_entry_lba = cpu_to_le64(val - pte_blk_cnt);
 	gpt_h->header_crc32 = 0;
 
 	calc_crc32 = efi_crc32((const unsigned char *)gpt_h,
@@ -409,7 +415,7 @@ int write_gpt_table(struct blk_desc *dev_desc,
 		       pte_blk_cnt, gpt_e) != pte_blk_cnt)
 		goto err;
 
-	prepare_backup_gpt_header(gpt_h);
+	prepare_backup_gpt_header(gpt_h, pte_blk_cnt);
 
 	if (blk_dwrite(dev_desc, (lbaint_t)le64_to_cpu(gpt_h->last_usable_lba)
 		       + 1, pte_blk_cnt, gpt_e) != pte_blk_cnt)
@@ -824,7 +830,7 @@ int write_mbr_and_gpt_partitions(struct blk_desc *dev_desc, void *buf)
 		return 1;
 	}
 
-	prepare_backup_gpt_header(gpt_h);
+	prepare_backup_gpt_header(gpt_h, gpt_e_blk_cnt);
 
 	/* write Backup GPT */
 	lba = le64_to_cpu(gpt_h->partition_entry_lba);
Signature · 1 line
1.9.1
#6 lore raw

PATCH v6 6/18 disk: part: efi: export internal function

In reply to Patrick Delaunay

export the needed functions for GPT over MTD

---

Changes in v6:
- add export function comment in part_efi_int.h

Changes in v5: None
Changes in v4: None
Changes in v3: None
Changes in v2: None

 disk/part_efi.c     | 58 +++++++-------------------------------------
 disk/part_efi_int.h | 70 +++++++++++++++++++++++++++++++++++++++++++++++++++++
 2 files changed, 79 insertions(+), 49 deletions(-)
 create mode 100644 disk/part_efi_int.h
disk/part_efi.c +9 -49
index 23a582a..2d5072c 100644
@@ -21,22 +21,11 @@
 #include <part_efi.h>
 #include <linux/compiler.h>
 #include <linux/ctype.h>
+#include "part_efi_int.h"
 
 DECLARE_GLOBAL_DATA_PTR;
 
 #ifdef HAVE_BLOCK_DEVICE
-/**
- * efi_crc32() - EFI version of crc32 function
- * @buf: buffer to calculate crc32 of
- * @len - length of buf
- *
- * Description: Returns EFI-style CRC32 value for @buf
- */
-static inline u32 efi_crc32(const void *buf, u32 len)
-{
-	return crc32(0, buf, len);
-}
-
 /*
  * Private function prototypes
  */
@@ -47,7 +36,6 @@ static int is_gpt_valid(struct blk_desc *dev_desc, u64 lba,
 				gpt_header *pgpt_head, gpt_entry **pgpt_pte);
 static gpt_entry *alloc_read_gpt_entries(struct blk_desc *dev_desc,
 					 gpt_header *pgpt_head);
-static int is_pte_valid(gpt_entry * pte);
 
 static char *print_efiname(gpt_entry *pte)
 {
@@ -72,8 +60,7 @@ static inline int is_bootable(gpt_entry *p)
 			sizeof(efi_guid_t));
 }
 
-static int validate_gpt_header(gpt_header *gpt_h, lbaint_t lba,
-		lbaint_t lastlba)
+int validate_gpt_header(gpt_header *gpt_h, lbaint_t lba, lbaint_t lastlba)
 {
 	uint32_t crc32_backup = 0;
 	uint32_t calc_crc32;
@@ -135,7 +122,7 @@ static int validate_gpt_header(gpt_header *gpt_h, lbaint_t lba,
 	return 0;
 }
 
-static int validate_gpt_entries(gpt_header *gpt_h, gpt_entry *gpt_e)
+int validate_gpt_entries(gpt_header *gpt_h, gpt_entry *gpt_e)
 {
 	uint32_t calc_crc32;
 
@@ -155,14 +142,8 @@ static int validate_gpt_entries(gpt_header *gpt_h, gpt_entry *gpt_e)
 	return 0;
 }
 
-/*
- * prepare_backup_gpt_header() - change primary GPT header to backup GPT header
- *
- * @param gpt_h - pointer to GPT header (in=primary, out=secondary)
- * @param pte_blk_cnt - number of block for size of partition entry array (PTE)
- */
-static void prepare_backup_gpt_header(gpt_header *gpt_h,
-				      const int pte_blk_cnt)
+void prepare_backup_gpt_header(gpt_header *gpt_h,
+			       const int pte_blk_cnt)
 {
 	uint32_t calc_crc32;
 	uint64_t val;
@@ -180,14 +161,7 @@ static void prepare_backup_gpt_header(gpt_header *gpt_h,
 }
 
 #if CONFIG_IS_ENABLED(EFI_PARTITION)
-/*
- * part_print_gpt() - display GUID Partition Table information
- *
- * @param gpt_head - pointer to GPT header
- * @param gpt_pte - pointer to GPT partion entry array
- */
-static void part_print_gpt(gpt_header *gpt_head,
-			   gpt_entry *gpt_pte)
+void part_print_gpt(gpt_header *gpt_head, gpt_entry *gpt_pte)
 {
 	int i = 0;
 	char uuid[37];
@@ -221,16 +195,8 @@ static void part_print_gpt(gpt_header *gpt_head,
 	}
 }
 
-/*
- * part_get_disk_info() - extract partition information for one GPT entry
- *
- * @param part - index of entry in gpt_pte
- * @param blksz - blksize
- * @param gpt_pte - pointer to GPT partion entry array
- * @param gpt_pte - pointer to disk partition (output)
- */
-static void part_get_disk_info(int part, unsigned int blksz,
-			       gpt_entry *gpt_pte, disk_partition_t *info)
+void part_get_disk_info(int part, unsigned int blksz,
+			gpt_entry *gpt_pte, disk_partition_t *info)
 {
 	/* The 'lbaint_t' casting may limit the maximum disk size to 2 TB */
 	info->start = (lbaint_t)le64_to_cpu(gpt_pte[part - 1].starting_lba);
@@ -989,13 +955,7 @@ static gpt_entry *alloc_read_gpt_entries(struct blk_desc *dev_desc,
 	return pte;
 }
 
-/**
- * is_pte_valid(): validates a single Partition Table Entry
- * @gpt_entry - Pointer to a single Partition Table Entry
- *
- * 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;
 
disk/part_efi_int.h new file +70 -0

70 lines not on this page · raw message

Signature · 1 line
1.9.1
#7 lore raw

PATCH v6 7/18 disk: part: add support of GPT partitioning over MTD

In reply to Patrick Delaunay

---

Changes in v6:
- invalid offset my_lba in is_secondary_gpt_valid_mtd() the secondary GPT header wasn't detected
- add comment for functions

Changes in v5:
- solve warning and error
(dont' include linux/mtd/mtd.h in part.h)

Changes in v4: None
Changes in v3: None
Changes in v2: None

 disk/Kconfig        |  17 ++
 disk/Makefile       |   1 +
 disk/part_efi_mtd.c | 456 ++++++++++++++++++++++++++++++++++++++++++++++++++++
 doc/README.gpt.mtd  | 187 +++++++++++++++++++++
 include/part.h      |  42 ++++-
 5 files changed, 701 insertions(+), 2 deletions(-)
 create mode 100644 disk/part_efi_mtd.c
 create mode 100644 doc/README.gpt.mtd
disk/Kconfig +17 -0
index 9396562..886a089 100644
@@ -96,6 +96,23 @@ config SPL_EFI_PARTITION
 	depends on  SPL && PARTITIONS
 	default y if EFI_PARTITION
 
+config EFI_PARTITION_MTD
+	bool "Support EFI GPT over MTD"
+	depends on EFI_PARTITION
+	help
+	  The GPT partition is normally defined only for block device with
+	  built-in controller which manage flash translation layer
+	  This option activate the GPT partition support over RAW device
+	  using the MTD framework
+	  - manage partition over MTD devices (as flash: NOR and NAND)
+	  - extract MTD information
+	  - update command gpt, mtdparts and part
+
+config SPL_EFI_PARTITION_MTD
+	bool "Enable EFI GPT over MTD for SPL"
+	depends on SPL_EFI_PARTITION
+	default y if EFI_PARTITION_MTD
+
 config PARTITION_UUIDS
 	bool "Enable support of UUID for partition"
 	depends on PARTITIONS
disk/Makefile +1 -0
index 12c0531..0d01160 100644
@@ -13,3 +13,4 @@ obj-$(CONFIG_$(SPL_)DOS_PARTITION)   += part_dos.o
 obj-$(CONFIG_$(SPL_)ISO_PARTITION)   += part_iso.o
 obj-$(CONFIG_$(SPL_)AMIGA_PARTITION) += part_amiga.o
 obj-$(CONFIG_$(SPL_)EFI_PARTITION)   += part_efi.o
+obj-$(CONFIG_$(SPL_)EFI_PARTITION_MTD) += part_efi_mtd.o
disk/part_efi_mtd.c new file +456 -0

456 lines not on this page · raw message

doc/README.gpt.mtd new file +187 -0

187 lines not on this page · raw message

include/part.h +40 -2

54 lines not on this page · raw message

Signature · 1 line
1.9.1
#8 lore raw

PATCH v6 7/18 disk: part: add support of GPT partitioning over MTD

Hi Patrick,

Quoted 17 lines · Patrick Delaunay

---

Changes in v6:
- invalid offset my_lba in is_secondary_gpt_valid_mtd() the secondary GPT header wasn't detected
- add comment for functions

Changes in v5:
- solve warning and error
(dont' include linux/mtd/mtd.h in part.h)

Changes in v4: None
Changes in v3: None
Changes in v2: None

Quoted 8 lines
 disk/Kconfig        |  17 ++
 disk/Makefile       |   1 +
 disk/part_efi_mtd.c | 456 ++++++++++++++++++++++++++++++++++++++++++++++++++++
 doc/README.gpt.mtd  | 187 +++++++++++++++++++++
 include/part.h      |  42 ++++-
 5 files changed, 701 insertions(+), 2 deletions(-)
 create mode 100644 disk/part_efi_mtd.c
 create mode 100644 doc/README.gpt.mtd

Can you please look at a few nits if you send a new version, or perhaps as a follow-up patch.

is_gpt_valid_mtd() could use a full function comment , including what the caller should do with the buffer.

common.h should go first in the #include sequence

Also, how can we add a sandbox test for this?

Regards,
Simon

#9 lore raw

PATCH v6 8/18 cmd: gpt: remove blk_desc parameter in static function

In reply to Patrick Delaunay

this patch prepare GPT over MTD support
blk_desc is only available for block device but not for MTD device

---

Changes in v6: None
Changes in v5: None
Changes in v4: None
Changes in v3: None
Changes in v2: None

 cmd/gpt.c | 17 +++++++++--------
 1 file changed, 9 insertions(+), 8 deletions(-)
cmd/gpt.c +9 -8
index 3e98821..5dfbce5 100644
@@ -155,7 +155,8 @@ static bool found_key(const char *str, const char *key)
  * set_gpt_info(): Fill partition information from string
  *		function allocates memory, remember to free!
  *
- * @param dev_desc - pointer block device descriptor
+ * @param lba - lba index
+ * @param blksz - block size for device
  * @param str_part - pointer to string with partition information
  * @param str_disk_guid - pointer to pointer to allocated string with disk guid
  * @param partitions - pointer to pointer to allocated partitions array
@@ -164,7 +165,8 @@ static bool found_key(const char *str, const char *key)
  * @return - zero on success, otherwise error
  *
  */
-static int set_gpt_info(struct blk_desc *dev_desc,
+static int set_gpt_info(unsigned int lba,
+			unsigned int blksz,
 			const char *str_part,
 			char **str_disk_guid,
 			disk_partition_t **partitions,
@@ -179,8 +181,7 @@ static int set_gpt_info(struct blk_desc *dev_desc,
 	uint64_t size_ll, start_ll;
 	lbaint_t offset = 0;
 
-	debug("%s:  lba num: 0x%x %d\n", __func__,
-	      (unsigned int)dev_desc->lba, (unsigned int)dev_desc->lba);
+	debug("%s:  lba num: 0x%x %d\n", __func__, lba, lba);
 
 	if (str_part == NULL)
 		return -1;
@@ -298,7 +299,7 @@ static int set_gpt_info(struct blk_desc *dev_desc,
 			parts[i].size = 0;
 		} else {
 			size_ll = ustrtoull(p, &p, 0);
-			parts[i].size = lldiv(size_ll, dev_desc->blksz);
+			parts[i].size = lldiv(size_ll, blksz);
 		}
 
 		free(val);
@@ -309,7 +310,7 @@ static int set_gpt_info(struct blk_desc *dev_desc,
 			if (extract_env(val, &p))
 				p = val;
 			start_ll = ustrtoull(p, &p, 0);
-			parts[i].start = lldiv(start_ll, dev_desc->blksz);
+			parts[i].start = lldiv(start_ll, blksz);
 			free(val);
 		}
 
@@ -341,7 +342,7 @@ static int gpt_default(struct blk_desc *blk_dev_desc, const char *str_part)
 	disk_partition_t *partitions = NULL;
 
 	/* fill partitions */
-	ret = set_gpt_info(blk_dev_desc, str_part,
+	ret = set_gpt_info(blk_dev_desc->lba, blk_dev_desc->blksz, str_part,
 			&str_disk_guid, &partitions, &part_count);
 	if (ret) {
 		if (ret == -1)
@@ -372,7 +373,7 @@ static int gpt_verify(struct blk_desc *blk_dev_desc, const char *str_part)
 	int ret = 0;
 
 	/* fill partitions */
-	ret = set_gpt_info(blk_dev_desc, str_part,
+	ret = set_gpt_info(blk_dev_desc->lba, blk_dev_desc->blksz, str_part,
 			&str_disk_guid, &partitions, &part_count);
 	if (ret) {
 		if (ret == -1) {
Signature · 1 line
1.9.1
#10 lore raw

PATCH v6 9/18 cmd: gpt: use error number in set_gpt_info

In reply to Patrick Delaunay

-1 => -ENOENT : "No partition list provided"
-2 => -EBADF : "Missing disk guid"
-3 => -EFAULT &
-4 => -EINVAL : "Partition list incomplete"

---

Changes in v6: None
Changes in v5: None
Changes in v4: None
Changes in v3: None
Changes in v2: None

 cmd/gpt.c | 30 +++++++++++++++---------------
 1 file changed, 15 insertions(+), 15 deletions(-)
cmd/gpt.c +15 -15
index 5dfbce5..a36dbdf 100644
@@ -184,7 +184,7 @@ static int set_gpt_info(unsigned int lba,
 	debug("%s:  lba num: 0x%x %d\n", __func__, lba, lba);
 
 	if (str_part == NULL)
-		return -1;
+		return -ENOENT;
 
 	str = strdup(str_part);
 
@@ -197,7 +197,7 @@ static int set_gpt_info(unsigned int lba,
 		gen_rand_uuid_str(*str_disk_guid, UUID_STR_FORMAT_STD);
 #else
 		free(str);
-		return -2;
+		return -EBADF;
 #endif
 	} else {
 		val = strsep(&val, ";");
@@ -209,7 +209,7 @@ static int set_gpt_info(unsigned int lba,
 		strsep(&s, ";");
 	}
 	if (strlen(s) == 0)
-		return -3;
+		return -EFAULT;
 
 	i = strlen(s) - 1;
 	if (s[i] == ';')
@@ -240,7 +240,7 @@ static int set_gpt_info(unsigned int lba,
 #ifdef CONFIG_RANDOM_UUID
 			gen_rand_uuid_str(parts[i].uuid, UUID_STR_FORMAT_STD);
 #else
-			errno = -4;
+			errno = -EINVAL;
 			goto err;
 #endif
 		} else {
@@ -248,7 +248,7 @@ static int set_gpt_info(unsigned int lba,
 				p = val;
 			if (strlen(p) >= sizeof(parts[i].uuid)) {
 				printf("Wrong uuid format for partition %d\n", i);
-				errno = -4;
+				errno = -EINVAL;
 				goto err;
 			}
 			strcpy((char *)parts[i].uuid, p);
@@ -264,7 +264,7 @@ static int set_gpt_info(unsigned int lba,
 			if (strlen(p) >= sizeof(parts[i].type_guid)) {
 				printf("Wrong type guid format for partition %d\n",
 				       i);
-				errno = -4;
+				errno = -EINVAL;
 				goto err;
 			}
 			strcpy((char *)parts[i].type_guid, p);
@@ -274,13 +274,13 @@ static int set_gpt_info(unsigned int lba,
 		/* name */
 		val = extract_val(tok, "name");
 		if (!val) { /* name is mandatory */
-			errno = -4;
+			errno = -EINVAL;
 			goto err;
 		}
 		if (extract_env(val, &p))
 			p = val;
 		if (strlen(p) >= sizeof(parts[i].name)) {
-			errno = -4;
+			errno = -EINVAL;
 			goto err;
 		}
 		strcpy((char *)parts[i].name, p);
@@ -289,7 +289,7 @@ static int set_gpt_info(unsigned int lba,
 		/* size */
 		val = extract_val(tok, "size");
 		if (!val) { /* 'size' is mandatory */
-			errno = -4;
+			errno = -EINVAL;
 			goto err;
 		}
 		if (extract_env(val, &p))
@@ -345,11 +345,11 @@ static int gpt_default(struct blk_desc *blk_dev_desc, const char *str_part)
 	ret = set_gpt_info(blk_dev_desc->lba, blk_dev_desc->blksz, str_part,
 			&str_disk_guid, &partitions, &part_count);
 	if (ret) {
-		if (ret == -1)
+		if (ret == -ENOENT)
 			printf("No partition list provided\n");
-		if (ret == -2)
+		if (ret == -EBADF)
 			printf("Missing disk guid\n");
-		if ((ret == -3) || (ret == -4))
+		if ((ret == -EFAULT) || (ret == -EINVAL))
 			printf("Partition list incomplete\n");
 		return -1;
 	}
@@ -376,15 +376,15 @@ static int gpt_verify(struct blk_desc *blk_dev_desc, const char *str_part)
 	ret = set_gpt_info(blk_dev_desc->lba, blk_dev_desc->blksz, str_part,
 			&str_disk_guid, &partitions, &part_count);
 	if (ret) {
-		if (ret == -1) {
+		if (ret == -ENOENT) {
 			printf("No partition list provided - only basic check\n");
 			ret = gpt_verify_headers(blk_dev_desc, gpt_head,
 						 &gpt_pte);
 			goto out;
 		}
-		if (ret == -2)
+		if (ret == -EBADF)
 			printf("Missing disk guid\n");
-		if ((ret == -3) || (ret == -4))
+		if ((ret == -EFAULT) || (ret == -EINVAL))
 			printf("Partition list incomplete\n");
 		return -1;
 	}
Signature · 1 line
1.9.1
#11 lore raw

PATCH v6 10/18 cmd: gpt: add print_gpt_info_err

In reply to Patrick Delaunay

add a common function between gpt_default and gpt_verify to display the error message associated to errno
the patch prepare GPT over MTD, as the same function will be reused

---

Changes in v6: None
Changes in v5: None
Changes in v4: None
Changes in v3: None
Changes in v2: None

 cmd/gpt.c | 28 +++++++++++++++++-----------
 1 file changed, 17 insertions(+), 11 deletions(-)
cmd/gpt.c +17 -11
index a36dbdf..a634071 100644
@@ -334,6 +334,21 @@ err:
 	return errno;
 }
 
+/**
+ * print_gpt_info_err(): display error message for set_gpt_info()
+ *
+ * @param ret - value of the result
+ */
+static void print_gpt_info_err(int ret)
+{
+	if (ret == -ENOENT)
+		printf("No partition list provided\n");
+	if (ret == -EBADF)
+		printf("Missing disk guid\n");
+	if ((ret == -EFAULT) || (ret == -EINVAL))
+		printf("Partition list incomplete\n");
+}
+
 static int gpt_default(struct blk_desc *blk_dev_desc, const char *str_part)
 {
 	int ret;
@@ -345,12 +360,7 @@ static int gpt_default(struct blk_desc *blk_dev_desc, const char *str_part)
 	ret = set_gpt_info(blk_dev_desc->lba, blk_dev_desc->blksz, str_part,
 			&str_disk_guid, &partitions, &part_count);
 	if (ret) {
-		if (ret == -ENOENT)
-			printf("No partition list provided\n");
-		if (ret == -EBADF)
-			printf("Missing disk guid\n");
-		if ((ret == -EFAULT) || (ret == -EINVAL))
-			printf("Partition list incomplete\n");
+		print_gpt_info_err(ret);
 		return -1;
 	}
 
@@ -376,16 +386,12 @@ static int gpt_verify(struct blk_desc *blk_dev_desc, const char *str_part)
 	ret = set_gpt_info(blk_dev_desc->lba, blk_dev_desc->blksz, str_part,
 			&str_disk_guid, &partitions, &part_count);
 	if (ret) {
+		print_gpt_info_err(ret);
 		if (ret == -ENOENT) {
-			printf("No partition list provided - only basic check\n");
 			ret = gpt_verify_headers(blk_dev_desc, gpt_head,
 						 &gpt_pte);
 			goto out;
 		}
-		if (ret == -EBADF)
-			printf("Missing disk guid\n");
-		if ((ret == -EFAULT) || (ret == -EINVAL))
-			printf("Partition list incomplete\n");
 		return -1;
 	}
 
Signature · 1 line
1.9.1
#12 lore raw

PATCH v6 11/18 cmd: gpt: update write command to support GPT over MTD

In reply to Patrick Delaunay

support gpt write for MTD device

Quoted 2 lines

gpt write nand 0
gpt write nor 0

And verify is not yet supported for MTD device

---

Changes in v6: None
Changes in v5: None
Changes in v4: None
Changes in v3: None
Changes in v2: None

 cmd/gpt.c | 87 ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++---
 1 file changed, 83 insertions(+), 4 deletions(-)
cmd/gpt.c +83 -4
index a634071..a40357a 100644
@@ -19,6 +19,7 @@
 #include <linux/ctype.h>
 #include <div64.h>
 #include <memalign.h>
+#include <linux/mtd/mtd.h>
 
 /**
  * extract_env(): Expand env name from string format '&{env_name}'
@@ -405,6 +406,57 @@ static int gpt_verify(struct blk_desc *blk_dev_desc, const char *str_part)
 	return ret;
 }
 
+#ifdef CONFIG_EFI_PARTITION_MTD
+/**
+ * gpt_default_mtd(): write GPT partition on MTD device
+ *
+ * @param mtd_info - pointer for MTD device descriptor
+ * @param str_part - pointer to string with partition information
+ *
+ * @return - zero on success, otherwise error
+ */
+static int gpt_default_mtd(struct mtd_info *mtd_info, const char *str_part)
+{
+	int ret;
+	char *str_disk_guid;
+	u8 part_count = 0;
+	disk_partition_t *partitions = NULL;
+	unsigned int lba = lldiv(mtd_info->size, MTD_LBA_SIZE);
+
+	if (!str_part)
+		return -1;
+
+	/* fill partitions */
+	ret = set_gpt_info(lba, MTD_LBA_SIZE, str_part,
+			   &str_disk_guid, &partitions, &part_count);
+	if (ret) {
+		print_gpt_info_err(ret);
+		return -1;
+	}
+
+	/* save partitions layout to disk */
+	ret = gpt_restore_mtd(mtd_info, str_disk_guid, partitions, part_count);
+	free(str_disk_guid);
+	free(partitions);
+
+	return ret;
+}
+
+/**
+ * gpt_verify_mtd(): verify GPT partition on MTD device
+ *
+ * @param mtd_info - pointer for MTD device descriptor
+ * @param str_part - pointer to string with partition information
+ *
+ * @return - zero on success, otherwise error
+ */
+static int gpt_verify_mtd(struct mtd_info *mtd_info, const char *str_part)
+{
+	printf("Verify no yet supported for MTD device\n");
+	return CMD_RET_FAILURE;
+}
+#endif
+
 /**
  * do_gpt(): Perform GPT operations
  *
@@ -421,6 +473,10 @@ static int do_gpt(cmd_tbl_t *cmdtp, int flag, int argc, char * const argv[])
 	int dev = 0;
 	char *ep;
 	struct blk_desc *blk_dev_desc = NULL;
+#ifdef CONFIG_EFI_PARTITION_MTD
+	struct mtd_info *mtd_info = NULL;
+	char mtd_dev[16];
+#endif
 
 	if (argc < 4 || argc > 5)
 		return CMD_RET_USAGE;
@@ -431,17 +487,40 @@ static int do_gpt(cmd_tbl_t *cmdtp, int flag, int argc, char * const argv[])
 		return CMD_RET_USAGE;
 	}
 	blk_dev_desc = blk_get_dev(argv[2], dev);
-	if (!blk_dev_desc) {
+	if (!blk_dev_desc)
+		ret = CMD_RET_FAILURE;
+#ifdef CONFIG_EFI_PARTITION_MTD
+	if (ret) {
+		ret = CMD_RET_SUCCESS;
+		sprintf(mtd_dev, "%s%d", argv[2], dev);
+		mtd_info = get_mtd_device_nm(mtd_dev);
+		if (IS_ERR(mtd_info))
+			ret = CMD_RET_FAILURE;
+	}
+#endif
+	if (ret) {
 		printf("%s: %s dev %d NOT available\n",
 		       __func__, argv[2], dev);
-		return CMD_RET_FAILURE;
+		return ret;
 	}
 
 	if ((strcmp(argv[1], "write") == 0) && (argc == 5)) {
 		printf("Writing GPT: ");
-		ret = gpt_default(blk_dev_desc, argv[4]);
+		ret = CMD_RET_FAILURE;
+		if (blk_dev_desc)
+			ret = gpt_default(blk_dev_desc, argv[4]);
+#ifdef CONFIG_EFI_PARTITION_MTD
+		if (mtd_info)
+			ret = gpt_default_mtd(mtd_info, argv[4]);
+#endif
 	} else if ((strcmp(argv[1], "verify") == 0)) {
-		ret = gpt_verify(blk_dev_desc, argv[4]);
+		ret = CMD_RET_FAILURE;
+		if (blk_dev_desc)
+			ret = gpt_verify(blk_dev_desc, argv[4]);
+#ifdef CONFIG_EFI_PARTITION_MTD
+		if (mtd_info)
+			ret = gpt_verify_mtd(mtd_info, argv[4]);
+#endif
 		printf("Verify GPT: ");
 	} else {
 		return CMD_RET_USAGE;
Signature · 1 line
1.9.1
#13 lore raw

PATCH v6 12/18 cmd: part: update command to support GPT over MTD

In reply to Patrick Delaunay

support part list for MTD device

Quoted 2 lines

part list nand 0
part list nor 0

---

Changes in v6: None
Changes in v5: None
Changes in v4: None
Changes in v3: None
Changes in v2: None

 cmd/part.c | 49 +++++++++++++++++++++++++++++++++++++++++--------
 1 file changed, 41 insertions(+), 8 deletions(-)
cmd/part.c +41 -8
index 8ba0598..ef5d979 100644
@@ -21,6 +21,7 @@
 #include <command.h>
 #include <part.h>
 #include <vsprintf.h>
+#include <linux/mtd/mtd.h>
 
 static int do_part_uuid(int argc, char * const argv[])
 {
@@ -48,10 +49,14 @@ static int do_part_uuid(int argc, char * const argv[])
 static int do_part_list(int argc, char * const argv[])
 {
 	int ret;
-	struct blk_desc *desc;
+	struct blk_desc *desc = NULL;
 	char *var = NULL;
 	bool bootable = false;
 	int i;
+#ifdef CONFIG_EFI_PARTITION_MTD
+	struct mtd_info *mtd = NULL;
+	char mtd_dev[16];
+#endif
 
 	if (argc < 2)
 		return CMD_RET_USAGE;
@@ -77,10 +82,19 @@ static int do_part_list(int argc, char * const argv[])
 			return CMD_RET_USAGE;
 	}
 
-	ret = blk_get_device_by_str(argv[0], argv[1], &desc);
-	if (ret < 0)
-		return 1;
-
+#ifdef CONFIG_EFI_PARTITION_MTD
+	if ((strlen(argv[0]) + strlen(argv[1])) < sizeof(mtd_dev)) {
+		sprintf(mtd_dev, "%s%s", argv[0], argv[1]);
+		mtd = get_mtd_device_nm(mtd_dev);
+	}
+	if (IS_ERR(mtd)) {
+#endif
+		ret = blk_get_device_by_str(argv[0], argv[1], &desc);
+		if (ret < 0)
+			return 1;
+#ifdef CONFIG_EFI_PARTITION_MTD
+	}
+#endif
 	if (var != NULL) {
 		int p;
 		char str[512] = { '\0', };
@@ -88,10 +102,21 @@ static int do_part_list(int argc, char * const argv[])
 
 		for (p = 1; p < 128; p++) {
 			char t[5];
-			int r = part_get_info(desc, p, &info);
+			int r;
 
-			if (r != 0)
-				continue;
+			if (desc) {
+				r = part_get_info(desc, p, &info);
+				if (r != 0)
+					continue;
+			}
+#ifdef CONFIG_EFI_PARTITION_MTD
+			else {
+				r = part_get_info_efi_mtd(mtd, p, &info);
+				/* Stop for first non valid partition */
+				if (r != 0)
+					break;
+			}
+#endif
 
 			if (bootable && !info.bootable)
 				continue;
@@ -100,10 +125,18 @@ static int do_part_list(int argc, char * const argv[])
 			strcat(str, t);
 		}
 		setenv(var, str);
+
 		return 0;
 	}
 
+#ifndef CONFIG_EFI_PARTITION_MTD
 	part_print(desc);
+#else
+	if (desc)
+		part_print(desc);
+	else
+		part_print_efi_mtd(mtd);
+#endif
 
 	return 0;
 }
Signature · 1 line
1.9.1
#14 lore raw

PATCH v6 13/18 cmd: mtdparts: move PART_ADD_DESC_MAXLEN

In reply to Patrick Delaunay

---

Changes in v6: None
Changes in v5: None
Changes in v4: None
Changes in v3: None
Changes in v2: None

 cmd/mtdparts.c | 4 +++-
 1 file changed, 3 insertions(+), 1 deletion(-)
cmd/mtdparts.c +3 -1
index 112bf1f..796bb98 100644
@@ -108,6 +108,9 @@ DECLARE_GLOBAL_DATA_PTR;
  * field for read-only partitions */
 #define MTD_WRITEABLE_CMD		1
 
+/* max size for partition description */
+#define PART_ADD_DESC_MAXLEN 64
+
 /* default values for mtdids and mtdparts variables */
 #if !defined(MTDIDS_DEFAULT)
 #ifdef CONFIG_MTDIDS_DEFAULT
@@ -1985,7 +1988,6 @@ static int do_mtdparts(cmd_tbl_t *cmdtp, int flag, int argc,
 
 	/* mtdparts add <mtd-dev> <size>[@<offset>] <name> [ro] */
 	if (((argc == 5) || (argc == 6)) && (strncmp(argv[1], "add", 3) == 0)) {
-#define PART_ADD_DESC_MAXLEN 64
 		char tmpbuf[PART_ADD_DESC_MAXLEN];
 #if defined(CONFIG_CMD_MTDPARTS_SPREAD)
 		struct mtd_info *mtd;
Signature · 1 line
1.9.1
#15 lore raw

PATCH v6 14/18 cmd: mtdparts: update command to support GPT over MTD

In reply to Patrick Delaunay

add new subcommand :
mtdparts gpt <mtd-dev> [<GUID>]

extract mtd partition from GPT header present in MTD device

Quoted 2 lines

mtdparts gpt nand0
mtdparts gpt nor0

extract mtd partitions only for some GUID

Quoted 2 lines

mtdparts gpt nand0 data
mtdparts gpt nor0 0FC63DAF-8483-4772-8E79-3D69D8477DE4

---

Changes in v6: None
Changes in v5: None
Changes in v4: None
Changes in v3: None
Changes in v2: None

 cmd/mtdparts.c | 140 ++++++++++++++++++++++++++++++++++++++++++++++++++++++++-
 1 file changed, 139 insertions(+), 1 deletion(-)
cmd/mtdparts.c +139 -1
index 796bb98..773a925 100644
@@ -1902,6 +1902,111 @@ static struct part_info* mtd_part_info(struct mtd_device *dev, unsigned int part
 	return NULL;
 }
 
+#ifdef CONFIG_EFI_PARTITION_MTD
+/**
+ * extract MTD info from GPT information
+ *
+ * @param device string to select GPT device
+ * @param p_guid_filter guid for filtering add
+ *
+ * @return 0 on success, -ENODEV if no such device otherwise error
+ */
+
+static int mtdparts_gpt(char *device, char *p_guid_filter)
+{
+	char tmpbuf[PART_ADD_DESC_MAXLEN];
+	char size_str[17], start_str[17];
+	u8 type, num, len;
+	struct mtdids *id;
+	int part_id;
+	struct mtd_info *mtd;
+	struct mtd_device *dev;
+	struct mtd_device *dev_tmp;
+	disk_partition_t info;
+	struct part_info *p;
+	int ret;
+
+	if (mtd_id_parse(device, NULL, &type, &num) != 0) {
+		printf("invalid MTD device %s\n", device);
+		return 1;
+	}
+
+	/* this may be the first run, initialize lists if needed
+	   and make sure we are in sync with env variables */
+	mtdparts_init();
+	/* don't treat error (mtdparts can be empty) */
+
+	id = id_find(type, num);
+	if (id == NULL) {
+		printf("no such device %s defined in mtdids variable\n",
+		       device);
+		return -ENODEV;
+	}
+
+	if (get_mtd_info(type, num, &mtd) || (mtd == NULL)) {
+		printf("no such MTD device %s\n", device);
+		return -ENODEV;
+	}
+
+	for (part_id = 1; part_id <= GPT_ENTRY_NUMBERS; part_id++) {
+		if (part_get_info_efi_mtd(mtd, part_id, &info))
+			break; /* Stop for first non valid partition */
+
+#ifdef CONFIG_PARTITION_TYPE_GUID
+		if (p_guid_filter &&
+		    strcmp(p_guid_filter, info.type_guid))
+			continue;
+#endif
+		sprintf(size_str, "%llx", (u64)(info.size * info.blksz));
+		sprintf(start_str, "%llx", (u64)(info.start * info.blksz));
+
+		len = strlen(id->mtd_id) + 1;	/* 'mtd_id:' */
+		len += strlen(size_str) + 2;	/* 0x size */
+		len += strlen(start_str) + 3;	/* @0x start */
+		len += strlen((char *)&info.name) + 2;	/* '(' name ')' */
+
+		if (len >= PART_ADD_DESC_MAXLEN) {
+			printf("too long partition description\n");
+			return -EINVAL;
+		}
+
+		sprintf(tmpbuf, "%s:0x%s at 0x%s(%s)",
+			id->mtd_id, size_str, start_str, info.name);
+
+		debug("add tmpbuf: %s\n", tmpbuf);
+
+		ret = device_parse(tmpbuf, NULL, &dev);
+		if (ret)
+			return ret;
+
+		debug("+ %s\t%d\t%s\n", MTD_DEV_TYPE(type), num,
+		      id->mtd_id);
+
+		p = list_entry(dev->parts.next, struct part_info, link);
+
+		dev_tmp = device_find(type, num);
+
+		if (dev_tmp == NULL) {
+			device_add(dev);
+		} else {
+			ret = part_add(dev_tmp, p);
+			if (ret) {
+				free(dev);
+				return ret;
+			}
+		}
+	}
+
+	ret = generate_mtdparts_save(last_parts, MTDPARTS_MAXLEN);
+	if (ret) {
+		printf("generated mtdparts too long, resetting to null\n");
+		return ret;
+	}
+
+	return 0;
+}
+#endif
+
 /***************************************************/
 /* U-Boot commands				   */
 /***************************************************/
@@ -1977,6 +2082,31 @@ static int do_mtdparts(cmd_tbl_t *cmdtp, int flag, int argc,
 		}
 	}
 
+#ifdef CONFIG_EFI_PARTITION_MTD
+	/* mtdparts gpt <mtd-dev> [GUID] */
+	if ((argc == 3 || argc == 4) && (strncmp(argv[1], "gpt", 3) == 0)) {
+#ifdef CONFIG_PARTITION_TYPE_GUID
+		char guid_str[UUID_STR_LEN + 1];
+		char *p_guid_filter = NULL;
+#endif
+
+		if (argc == 4) {
+#ifdef CONFIG_PARTITION_TYPE_GUID
+			p_guid_filter = guid_str;
+			if (uuid_guid_parse_str(argv[3], guid_str)) {
+				printf("invalid type gui %s\n", argv[3]);
+				return 1;
+			}
+			debug("filtered type gui %s (%s)\n", argv[3], guid_str);
+#else
+			puts("type GUI not support\n");
+#endif
+		}
+
+		return mtdparts_gpt(argv[2], p_guid_filter);
+	}
+#endif
+
 	/* make sure we are in sync with env variables */
 	if (mtdparts_init() != 0)
 		return 1;
@@ -2093,6 +2223,10 @@ static char mtdparts_help_text[] =
 	"mtdparts add.spread <mtd-dev> <size>[@<offset>] [<name>] [ro]\n"
 	"    - add partition, padding size by skipping bad blocks\n"
 #endif
+#ifdef CONFIG_EFI_PARTITION_MTD
+	"mtdparts gpt <mtd-dev> [<GUID>]\n"
+	"    - add partitions for device from gpt, filtered by GUID type\n"
+#endif
 	"mtdparts default\n"
 	"    - reset partition table to defaults\n"
 #if defined(CONFIG_CMD_MTDPARTS_SPREAD)
@@ -2122,7 +2256,11 @@ static char mtdparts_help_text[] =
 	"<size>     := standard linux memsize OR '-' to denote all remaining space\n"
 	"<offset>   := partition start offset within the device\n"
 	"<name>     := '(' NAME ')'\n"
-	"<ro-flag>  := when set to 'ro' makes partition read-only (not used, passed to kernel)";
+	"<ro-flag>  := when set to 'ro' makes partition read-only (not used, passed to kernel)\n"
+#ifdef CONFIG_EFI_PARTITION_MTD
+	"<GUID>     := partition guid\n"
+#endif
+	;
 #endif
 
 U_BOOT_CMD(
Signature · 1 line
1.9.1
#16 lore raw

PATCH v6 15/18 uuid: remove dependency with io.h

In reply to Patrick Delaunay

io.h is only used for CONFIG_RANDOM_UUID
- for clrsetbits_be16
- for clrsetbits_8
available only for some target (but not for SANDBOX)

---

Changes in v6: None
Changes in v5: None
Changes in v4: None
Changes in v3: None
Changes in v2: None

 lib/uuid.c | 13 ++++++-------
 1 file changed, 6 insertions(+), 7 deletions(-)
lib/uuid.c +6 -7
index 7ce822c..058b1c3 100644
@@ -8,7 +8,6 @@
 #include <linux/ctype.h>
 #include <errno.h>
 #include <common.h>
-#include <asm/io.h>
 #include <part_efi.h>
 #include <malloc.h>
 
@@ -278,13 +277,13 @@ void gen_rand_uuid(unsigned char *uuid_bin)
 	for (i = 0; i < sizeof(struct uuid) / sizeof(*ptr); i++)
 		*(ptr + i) = cpu_to_be32(rand());
 
-	clrsetbits_be16(&uuid.time_hi_and_version,
-			UUID_VERSION_MASK,
-			UUID_VERSION << UUID_VERSION_SHIFT);
+	/* UUID is big endian */
+	uuid.time_hi_and_version |= ~cpu_to_be16(UUID_VERSION_MASK);
+	uuid.time_hi_and_version &=
+		cpu_to_be16(UUID_VERSION << UUID_VERSION_SHIFT);
 
-	clrsetbits_8(&uuid.clock_seq_hi_and_reserved,
-		     UUID_VARIANT_MASK,
-		     UUID_VARIANT << UUID_VARIANT_SHIFT);
+	uuid.clock_seq_hi_and_reserved |= ~UUID_VARIANT_MASK;
+	uuid.clock_seq_hi_and_reserved &= UUID_VARIANT << UUID_VARIANT_SHIFT;
 
 	memcpy(uuid_bin, &uuid, sizeof(struct uuid));
 }
Signature · 1 line
1.9.1
#17 lore raw

PATCH v6 16/18 sandbox: GPT over MTD test

In reply to Patrick Delaunay

activate needed feature in sandbox to allow test GPT over MTD
- EFI_PARTITION_MTD
- SPI_FLASH_MTD
- CMD_MTDPARTS
- MTD_PARTITIONS
- MTD_DEVICE

activate 2 features usefull for test
- RANDOM_UUID
- PARTITION_TYPE_GUID

---

Changes in v6: None
Changes in v5: None
Changes in v4: None
Changes in v3: None
Changes in v2: None

 configs/sandbox_defconfig | 3 +++
 include/configs/sandbox.h | 6 ++++++
 2 files changed, 9 insertions(+)
configs/sandbox_defconfig +3 -0
index d3fee89..546f837 100644
@@ -57,6 +57,8 @@ CONFIG_CMD_CRAMFS=y
 CONFIG_CMD_EXT4_WRITE=y
 CONFIG_MAC_PARTITION=y
 CONFIG_AMIGA_PARTITION=y
+CONFIG_EFI_PARTITION_MTD=y
+CONFIG_PARTITION_TYPE_GUID=y
 CONFIG_OF_CONTROL=y
 CONFIG_OF_HOSTFILE=y
 CONFIG_NETCONSOLE=y
@@ -110,6 +112,7 @@ CONFIG_SPI_FLASH_SPANSION=y
 CONFIG_SPI_FLASH_STMICRO=y
 CONFIG_SPI_FLASH_SST=y
 CONFIG_SPI_FLASH_WINBOND=y
+CONFIG_SPI_FLASH_MTD=y
 CONFIG_DM_ETH=y
 CONFIG_PCI=y
 CONFIG_DM_PCI=y
include/configs/sandbox.h +6 -0
index 37c6132..5fe92b8 100644
@@ -37,6 +37,12 @@
 #define CONFIG_EXT4_WRITE
 #define CONFIG_HOST_MAX_DEVICES 4
 
+#define CONFIG_RANDOM_UUID
+
+/* Dynamic MTD partition support */
+#define CONFIG_CMD_MTDPARTS
+#define CONFIG_MTD_PARTITIONS
+#define CONFIG_MTD_DEVICE
 /*
  * Size of malloc() pool, before and after relocation
  */
Signature · 1 line
1.9.1
#18 lore raw

PATCH v6 17/18 distro: add boot for UBI in GPT over MTD

In reply to Patrick Delaunay

Now that we can expose boot for UBI partition on MTD devices (NOR and NAND).

This patch adds the distro script to automatically payloads from UBI volume called boot detected in a bootable partition in GPT partition in the MTD device.

example to start from nor0 and nand0:

	func(EFIMTD, nor, 0) \
	func(EFIMTD, nand, 0)

---

Changes in v6:
- add distro support for UBI volume in GPT partition in MTD device

Changes in v5: None
Changes in v4: None
Changes in v3: None
Changes in v2: None

 include/config_distro_bootcmd.h | 40 ++++++++++++++++++++++++++++++++++++++++
 1 file changed, 40 insertions(+)
include/config_distro_bootcmd.h +40 -0
index 0e01e82..39584e0 100644
@@ -68,6 +68,45 @@
 	BOOT_TARGET_DEVICES_references_MMC_without_CONFIG_CMD_MMC
 #endif
 
+#ifdef CONFIG_EFI_PARTITION_MTD
+#define BOOTENV_SHARED_EFIMTD \
+	"scan_efi_mtd_for_ubi=" \
+		"mtdparts gpt ${mtddev}${devnum}; " \
+		"part list ${mtddev} ${devnum} -bootable devplist; " \
+		"env exists devplist || setenv devplist 1; " \
+		"for distro_bootpart in ${devplist}; do " \
+			"setexpr mtd_bootpart ${distro_bootpart} - 1;" \
+			"if ubi part ${mtddev}${devnum},${mtd_bootpart} &&" \
+			"   ubifsmount ubi0; then " \
+				"setenv devtype ubi; " \
+				"setenv distro_bootpart 0; " \
+				"run scan_dev_for_boot;" \
+				"ubi detach;" \
+			"fi;" \
+		"done\0" \
+	\
+	"nor_boot=" \
+		"if sf probe ${devnum}; then " \
+			"setenv mtddev nor;" \
+			"run scan_efi_mtd_for_ubi;" \
+		"fi\0" \
+	\
+	"nand_boot=" \
+		"if nand device ${devnum}; then " \
+			"setenv mtddev nand;" \
+			"run scan_efi_mtd_for_ubi;" \
+		"fi\0"
+
+#define BOOTENV_DEV_EFIMTD BOOTENV_DEV_BLKDEV
+#define BOOTENV_DEV_NAME_EFIMTD	BOOTENV_DEV_NAME_BLKDEV
+#else
+#define BOOTENV_SHARED_EFIMTD
+#define BOOTENV_DEV_EFIMTD \
+	BOOT_TARGET_DEVICES_references_EFIMTD_without_CONFIG_EFI_PARTITION_MTD
+#define BOOTENV_DEV_NAME_EFIMTD \
+	BOOT_TARGET_DEVICES_references_EFIMTD_without_CONFIG_EFI_PARTITION_MTD
+#endif
+
 #ifdef CONFIG_CMD_UBIFS
 #define BOOTENV_SHARED_UBIFS \
 	"ubifs_boot=" \
@@ -322,6 +361,7 @@
 	BOOTENV_SHARED_IDE \
 	BOOTENV_SHARED_UBIFS \
 	BOOTENV_SHARED_EFI \
+	BOOTENV_SHARED_EFIMTD \
 	"boot_prefixes=/ /boot/\0" \
 	"boot_scripts=boot.scr.uimg boot.scr\0" \
 	"boot_script_dhcp=boot.scr.uimg\0" \
Signature · 1 line
1.9.1
#19 lore raw

PATCH v6 17/18 distro: add boot for UBI in GPT over MTD

Quoted 18 lines · Patrick Delaunay

Now that we can expose boot for UBI partition on MTD devices (NOR and NAND).

This patch adds the distro script to automatically payloads from UBI volume called boot detected in a bootable partition in GPT partition in the MTD device.

example to start from nor0 and nand0:

func(EFIMTD, nor, 0) \
func(EFIMTD, nand, 0)

---

Changes in v6:
- add distro support for UBI volume in GPT partition in MTD device

Quoted 7 lines

Changes in v5: None
Changes in v4: None
Changes in v3: None
Changes in v2: None

 include/config_distro_bootcmd.h | 40 ++++++++++++++++++++++++++++++++++++++++
 1 file changed, 40 insertions(+)
#20 lore raw

PATCH v6 18/18 tests: Add GPT over MTD test

In reply to Patrick Delaunay

test/fs/gpt_mtd-test.sh
today only test for SF device (nor0)

---

Changes in v6:
- add sandbox test (test/fs/gpt_mtd-test.sh) for modified commands

Changes in v5: None
Changes in v4: None
Changes in v3: None
Changes in v2: None

 test/fs/gpt_mtd-test.sh | 209 ++++++++++++++++++++++++++++++++++++++++++++++++
 1 file changed, 209 insertions(+)
 create mode 100755 test/fs/gpt_mtd-test.sh
test/fs/gpt_mtd-test.sh new file +209 -0
new file mode 100755
index 0000000..6cb2994
@@ -0,0 +1,209 @@
+#!/bin/bash
+#
+# (C) Copyright 2017 Patrick Delaunay
+#
+#  SPDX-License-Identifier:	GPL-2.0+
+#
+
+# Invoke this test script from U-Boot base directory as
+#      ./test/fs/gpt_mtd-test.sh
+# It currently tests the gpt over mtd support
+# Expected results are as follows:
+# Total Summary: TOTAL PASS: 7 TOTAL FAIL: 0
+
+# pre-requisite binaries list.
+PREREQ_BINS="dd "
+
+# Location of generated sandbox u-boot
+SANDBOX_DIR="sandbox"
+UBOOT="$SANDBOX_DIR/u-boot"
+
+# All generated output files from this test will be in $OUT_DIR
+# Hence everything is sandboxed.
+OUT_DIR="$SANDBOX_DIR/test/fs"
+
+# $OUT shall be the prefix of the test output. Their suffix will be .out
+OUT="${OUT_DIR}/fs-test"
+
+# DTB file to use
+DTB="$SANDBOX_DIR/arch/sandbox/dts/test.dtb"
+
+# ************************
+# * Functions start here *
+# ************************
+
+# Check if the prereq binaries exist, or exit
+function check_prereq() {
+	for prereq in $PREREQ_BINS; do
+		if [ ! -x "`which $prereq`" ]; then
+			echo "Missing $prereq binary. Exiting!"
+			exit
+		fi
+	done
+}
+
+# If 1st param is "clean", then clean out the generated files and exit
+function check_clean() {
+	if [ "$1" = "clean" ]; then
+		rm -rf "$OUT_DIR"
+		echo "Cleaned up generated files. Exiting"
+		exit
+	fi
+}
+
+# Generate sandbox U-Boot - gleaned from /test/dm/test-dm.sh
+function compile_sandbox() {
+	unset CROSS_COMPILE
+	NUM_CPUS=$(cat /proc/cpuinfo |grep -c processor)
+	make O=$SANDBOX_DIR sandbox_config
+	make O=$SANDBOX_DIR -s -j${NUM_CPUS}
+
+	# Check if U-Boot exists
+	if [ ! -x "$UBOOT" ]; then
+		echo "$UBOOT does not exist or is not executable"
+		echo "Build error?"
+		echo "Please run this script as ./test/fs/`basename $0`"
+		exit
+	fi
+}
+
+# Clean out all generated files other than the file system images
+# We save time by not deleting and recreating the file system images
+function prepare_env() {
+	dd if=/dev/zero of=spi.bin bs=1M count=4
+	mkdir -p ${OUT_DIR}
+}
+
+
+function test_gpt_mtd_nor() {
+
+	$UBOOT -d $DTB << EOF
+sf probe
+#
+# Test Case 1 - gpt write
+gpt write nor 0 "name=part1,size=64KiB;name=part2,type=system,size=128KiB;\
+name=boot,type=linux,bootable,size=512KiB"
+#
+# Test Case 2 - part list
+part list nor 0
+#
+# Test Case 3 - mtdparts
+setenv mtdids "nor0=nor0"
+setenv mtdparts "mtdparts="
+mtdparts gpt nor0
+mtdparts
+#
+# Test Case 4 - part list
+part list nor 0 -bootable boot_part
+printenv boot_part
+#
+# Test Case 5 - test filtering feature
+mtdparts delall
+setenv mtdparts "mtdparts="
+setenv mtdids "nor0=nor0"
+#
+# Test Case 5.1 - only add MTD for some guid = linux
+mtdparts gpt nor0 linux
+mtdparts
+#
+# Test Case 5.2 - then add MTD for some guid = data
+mtdparts gpt nor0 data
+mtdparts
+#
+# Test Case 5.3 - then add MTD for some guid = system
+mtdparts gpt nor0 system
+mtdparts
+#
+# Test Case 6.1 - check backup GPT header
+sf erase 0 0x10000
+part list nor 0
+# Test Case 6.2 - check backup GPT header
+sf erase 0x1F0000 0x10000
+part list nor 0
+#
+reset
+
+EOF
+}
+
+# 1st parameter is the text to print
+# if $? is 0 its a pass, else a fail
+# As a side effect it shall update env variable PASS and FAIL
+function pass_fail() {
+	if [ $? -eq 0 ]; then
+		echo pass - "$1"
+		PASS=$((PASS + 1))
+	else
+		echo FAIL - "$1"
+		FAIL=$((FAIL + 1))
+	fi
+}
+
+
+# 1st parameter is the name of the output file to check
+function check_results() {
+	echo "** Start $1"
+
+	PASS=0
+	FAIL=0
+
+	grep -A2 "Test Case 1 " "$1" | egrep -q "Writing GPT: success!"
+	pass_fail "TC1: gpt write"
+
+	grep -A6 "Test Case 2 " "$1" | egrep -q "part1"
+	pass_fail "TC2: part list"
+
+	grep -A7 "Test Case 3 " "$1" | egrep -q "parts = 3"
+	pass_fail "TC3: mtdparts"
+
+	grep -A4 "Test Case 4 " "$1" | egrep -q "boot_part=2 3"
+	pass_fail "TC4: part list"
+
+	grep -A4 "Test Case 5.1 " "$1" | egrep -q "parts = 1"
+	pass_fail "TC5.1: mtdparts gpt for linux"
+
+	grep -A4 "Test Case 5.2 " "$1" | egrep -q "parts = 2"
+	pass_fail "TC5.2: mtdparts gpt for data"
+
+	grep -A4 "Test Case 5.3 " "$1" | egrep -q "parts = 3"
+	pass_fail "TC5.2: mtdparts gpt for system"
+
+	grep -A6 "Test Case 6.1 " "$1" | egrep -q "Using Backup GPT"
+	pass_fail "TC6.1: check backup GPT use"
+
+	grep -A8 "Test Case 6.2 " "$1" | egrep -q "Invalid Backup GPT"
+	pass_fail "TC6.2: check backup GPT erase"
+
+	echo "** End $1"
+}
+
+# ********************
+# * End of functions *
+# ********************
+
+check_clean "$1"
+check_prereq
+compile_sandbox
+prepare_env
+
+# Track TOTAL_FAIL and TOTAL_PASS
+TOTAL_FAIL=0
+TOTAL_PASS=0
+
+OUT_FILE="${OUT}.sb.gpt_mtd_nor.out"
+test_gpt_mtd_nor > ${OUT_FILE} 2>&1
+
+check_results $OUT_FILE
+TOTAL_FAIL=$((TOTAL_FAIL + FAIL))
+TOTAL_PASS=$((TOTAL_PASS + PASS))
+
+
+echo "Total Summary: TOTAL PASS: $TOTAL_PASS TOTAL FAIL: $TOTAL_FAIL"
+echo "--------------------------------------------"
+if [ $TOTAL_FAIL -eq 0 ]; then
+	echo "PASSED"
+	exit 0
+else
+	echo "FAILED"
+	exit 1
+fi
Signature · 1 line
1.9.1
#21 lore raw

v6 cover GPT over MTD

In reply to Patrick Delaunay

Hi,

Quoted 4 lines · Patrick Delaunay

I have a request to support GPT over MTD to have the MTD informations without U-Boot environment(CONFIG_ENV_IS_NOWHERE is a other requirement of my project to manage several board configuration with
the same defconfig; boot from NAND or NOR or SDCARD).

What would happen if you have a bad block in the middle of the primary or secondary GPT headers (or both)?

Maxime

Signature · 10 lines
Maxime Ripard, Free Electrons
Embedded Linux and Kernel engineering
http://free-electrons.com
-------------- next part --------------
A non-text attachment was scrubbed...
Name: signature.asc
Type: application/pgp-signature
Size: 801 bytes
Desc: not available
URL: <http://lists.denx.de/pipermail/u-boot/attachments/20170511/9a823513/attachment.sig>
#22 lore raw

v6 cover GPT over MTD

Hi Maxime,

Quoted 11 lines

From: Maxime Ripard [mailto:maxime.ripard at free-electrons.com]
Sent: jeudi 11 mai 2017 10:20

Hi,

Quoted 4 lines · Patrick Delaunay

I have a request to support GPT over MTD to have the MTD informations without U-Boot environment(CONFIG_ENV_IS_NOWHERE is a other requirement of my project to manage several board configuration with the same defconfig; boot from NAND or NOR or SDCARD).

Quoted 5 lines

What would happen if you have a bad block in the middle of the primary or secondary GPT headers (or both)?

Maxime

All Bad block are skipped....
=> primary GPT header is located at the beginning of first good block => backup GPT header is located at the end the of last good block

And gpt create will failed if the erase block command (for primary or backup GPT) produce a new bad block.

I try to explain this behavior in the added file doc/README.gpt.mtd with the patch

[PATCH v6 07/18] disk: part: add support of GPT partitioning over MTD

regards

Patrick

#23 lore raw

v6 cover GPT over MTD

Quoted 19 lines · Patrick DELAUNAY

Hi Maxime,

Quoted 11 lines

From: Maxime Ripard [mailto:maxime.ripard at free-electrons.com]
Sent: jeudi 11 mai 2017 10:20

Hi,

Quoted 4 lines · Patrick Delaunay

I have a request to support GPT over MTD to have the MTD informations without U-Boot environment(CONFIG_ENV_IS_NOWHERE is a other requirement of my project to manage several board configuration with the same defconfig; boot from NAND or NOR or SDCARD).

Quoted 5 lines

What would happen if you have a bad block in the middle of the primary or secondary GPT headers (or both)?

Maxime

Quoted 6 lines

All Bad block are skipped....
=> primary GPT header is located at the beginning of first good block => backup GPT header is located at the end the of last good block

And gpt create will failed if the erase block command (for primary or backup GPT) produce a new bad block.

Right, but what happens if your block becomes bad or too corrupted after it's been written.

You mention in your Drawbacks section that if you erase the block and is now detected to be bad, u-boot will have to act upon it. But that can happen outside of U-Boot as well, or not directly to this block, by reads or writes disturb... In this case, your GPT header is gone, and you have no way to recover from it.

Maxime

Signature · 10 lines
Maxime Ripard, Free Electrons
Embedded Linux and Kernel engineering
http://free-electrons.com
-------------- next part --------------
A non-text attachment was scrubbed...
Name: signature.asc
Type: application/pgp-signature
Size: 801 bytes
Desc: not available
URL: <http://lists.denx.de/pipermail/u-boot/attachments/20170511/e0bc7cb6/attachment.sig>
#24 lore raw

v6 cover GPT over MTD

Hi Maxime

Quoted 32 lines

From: Maxime Ripard [mailto:maxime.ripard at free-electrons.com]
Sent: jeudi 11 mai 2017 16:46

Quoted 10 lines · Patrick DELAUNAY

Hi Maxime,

Quoted 8 lines

From: Maxime Ripard [mailto:maxime.ripard at free-electrons.com]
Sent: jeudi 11 mai 2017 10:20

Hi,

Quoted 2 lines · Patrick Delaunay

I have a request to support GPT over MTD to have the MTD informations without U-Boot

environment(CONFIG_ENV_IS_NOWHERE is a

Quoted 2 lines
Quoted 2 lines
Quoted 2 lines

other requirement of my project to manage several board configuration with the same defconfig; boot from NAND or NOR or

SDCARD).

Quoted 7 lines
Quoted 6 lines

What would happen if you have a bad block in the middle of the primary or secondary GPT headers (or both)?

Maxime

Quoted 6 lines

All Bad block are skipped....
=> primary GPT header is located at the beginning of first good block => backup GPT header is located at the end the of last good block

And gpt create will failed if the erase block command (for primary or backup GPT) produce a new bad block.

Quoted 8 lines

Right, but what happens if your block becomes bad or too corrupted after it's been written.

You mention in your Drawbacks section that if you erase the block and is now detected to be bad, u-boot will have to act upon it. But that can happen outside of U-Boot as well, or not directly to this block, by reads or writes disturb... In this case, your GPT header is gone, and you have no way to recover from it.

Yes, I known that it is the main issue for my proposal: the management of NAND bad block. But it is the same for all the binary in boot stage (SPL / U-Boot / U-Boot env) in NAND.

For my point of view, erase should done only when GPT header need to updated => for first flashing / complete update of NAND
=> for the refresh of the GPT header
So the NAND block becomes BAD only in this 2 cases.

If a read or write disturb for GPT block occur, it should be detected by NAND ECC
=> when unrecoverable error occur for one boot the backup GPT header should be used
PS: Perhaps need to do something in U-Boot to refresh primary GPT header from backup header informaiton

The expected strategy is to read the boot partitions
(all of them , GPT header , SPL and U-Boot) periodically outside of U-Boot and if ECC read are too important refresh them :
read the partition and write it again in RAW mode (skip bad block)

So if the partitioning is correctly managed
(with reserve some tank of good block for partition refresh) The GPT can de refreshed in raw mode without breaking the other partition

My idea is : to prepare a partionning with tank of good block 3 is this example :

0 => MBR + primary GPT
1 => tank block
2 => tank block
3 => tank block
--------------------- MTD1
4 => SPL
5 => tank block
6 => tank block
7 => tank block
----------------------MTD2
8 => U-Boot 1/3
9 => U-Boot 2/3
10 => U-Boot 3/3
11 => tank block
12 => tank block
13 => tank block
----------------------MTD3
14 => UBI for kernel
......
N-8 => last usable LBA
----------------------
N-7 => tank block
N-6 => tank block
N-5 => tank block
N-4 => backup GPT
N-3 => BBT (marked as bad)
N-2 => BBT (marked as bad)
N-1 => BBT (marked as bad)
N => BBT (marked as bad)

Block 0 and N-4 will be refreshed when ECC errors reach the threshold for any partition => refresh = read + erase + write (skip bad block)
=> if the erase command detect a bad block, the next tank block is used.

0 => Bad block (NEW)
1 => MBR + primary GPT
2 => tank block
3 => tank block

Same for backup GPT but with inversed way

N-7 => tank block
N-6 => tank block
N-5 => backup GPT
N-4 => bad block (NEW)

The number of tank block need to be choose correctly (To be defined with NAND information)

PS: it is the same for SPL and U-Boot partition

Do you know better solution to handle read disturb issue on boot partitions than refresh ?

Quoted 7 lines

Maxime

--
Maxime Ripard, Free Electrons
Embedded Linux and Kernel engineering
http://free-electrons.com

Patrick

#25 lore raw

v6 cover GPT over MTD

Quoted 43 lines · Patrick DELAUNAY

Hi Maxime

Quoted 32 lines

From: Maxime Ripard [mailto:maxime.ripard at free-electrons.com]
Sent: jeudi 11 mai 2017 16:46

Quoted 10 lines · Patrick DELAUNAY

Hi Maxime,

Quoted 8 lines

From: Maxime Ripard [mailto:maxime.ripard at free-electrons.com]
Sent: jeudi 11 mai 2017 10:20

Hi,

Quoted 2 lines · Patrick Delaunay

I have a request to support GPT over MTD to have the MTD informations without U-Boot

environment(CONFIG_ENV_IS_NOWHERE is a

Quoted 2 lines
Quoted 2 lines
Quoted 2 lines

other requirement of my project to manage several board configuration with the same defconfig; boot from NAND or NOR or

SDCARD).

Quoted 7 lines
Quoted 6 lines

What would happen if you have a bad block in the middle of the primary or secondary GPT headers (or both)?

Maxime

Quoted 6 lines

All Bad block are skipped....
=> primary GPT header is located at the beginning of first good block => backup GPT header is located at the end the of last good block

And gpt create will failed if the erase block command (for primary or backup GPT) produce a new bad block.

Quoted 8 lines

Right, but what happens if your block becomes bad or too corrupted after it's been written.

You mention in your Drawbacks section that if you erase the block and is now detected to be bad, u-boot will have to act upon it. But that can happen outside of U-Boot as well, or not directly to this block, by reads or writes disturb... In this case, your GPT header is gone, and you have no way to recover from it.

Quoted 3 lines

Yes, I known that it is the main issue for my proposal: the management of NAND bad block. But it is the same for all the binary in boot stage (SPL / U-Boot / U-Boot env) in NAND.

U-Boot environment can be stored in UBI, and iirc U-Boot too. And usually the SPLs can be stored at multiple offsets to reduce the risks.

Quoted 7 lines

For my point of view, erase should done only when GPT header need to updated

=> for first flashing / complete update of NAND => for the refresh of the GPT header
So the NAND block becomes BAD only in this 2 cases.

Quoted 5 lines

If a read or write disturb for GPT block occur, it should be detected by NAND ECC => when unrecoverable error occur for one boot the backup GPT header should be used PS: Perhaps need to do something in U-Boot to refresh primary GPT header from backup header informaiton

Like I was saying, the issue really isn't in U-Boot itself, but when U-Boot isn't there anymore to deal with those issues.

Quoted 53 lines

The expected strategy is to read the boot partitions
(all of them , GPT header , SPL and U-Boot) periodically outside of U-Boot and if ECC read are too important refresh them :
read the partition and write it again in RAW mode (skip bad block)

So if the partitioning is correctly managed
(with reserve some tank of good block for partition refresh) The GPT can de refreshed in raw mode without breaking the other partition

My idea is : to prepare a partionning with tank of good block 3 is this example :

0 => MBR + primary GPT
1 => tank block
2 => tank block
3 => tank block
--------------------- MTD1
4 => SPL
5 => tank block
6 => tank block
7 => tank block
----------------------MTD2
8 => U-Boot 1/3
9 => U-Boot 2/3
10 => U-Boot 3/3
11 => tank block
12 => tank block
13 => tank block
----------------------MTD3
14 => UBI for kernel
......
N-8 => last usable LBA
----------------------
N-7 => tank block
N-6 => tank block
N-5 => tank block
N-4 => backup GPT
N-3 => BBT (marked as bad)
N-2 => BBT (marked as bad)
N-1 => BBT (marked as bad)
N => BBT (marked as bad)

Block 0 and N-4 will be refreshed when ECC errors reach the threshold for any partition => refresh = read + erase + write (skip bad block)
=> if the erase command detect a bad block, the next tank block is used.

0 => Bad block (NEW)
1 => MBR + primary GPT
2 => tank block
3 => tank block

Same for backup GPT but with inversed way

Quoted 7 lines

N-7 => tank block
N-6 => tank block
N-5 => backup GPT
N-4 => bad block (NEW)

The number of tank block need to be choose correctly (To be defined with NAND information)

This really feels like you're trying to reinvent the UBI-partitions wheel, but yeah, that would make it a bit more robust.

Quoted 4 lines

PS: it is the same for SPL and U-Boot partition

Do you know better solution to handle read disturb issue on boot partitions than refresh ?

Unfortunately, I don't, but the biggest concern I have is that you have no way to tell that your system is basically in self-destruct mode outside of U-Boot. And that's an issue because:
A) U-Boot will be there for (hopefully) less than a second
B) An user getting a brand new system will have no idea that it needs to install something else so that his entire system do not fall apart.

Maxime

Signature · 10 lines
Maxime Ripard, Free Electrons
Embedded Linux and Kernel engineering
http://free-electrons.com
-------------- next part --------------
A non-text attachment was scrubbed...
Name: signature.asc
Type: application/pgp-signature
Size: 801 bytes
Desc: not available
URL: <http://lists.denx.de/pipermail/u-boot/attachments/20170516/1d35edef/attachment.sig>
#26 lore raw

v6 cover GPT over MTD

Thanks Maxime for the feedback,

Quoted 50 lines

-----Original Message-----
From: Maxime Ripard [mailto:maxime.ripard at free-electrons.com]
Sent: mardi 16 mai 2017 11:29

Quoted 45 lines · Patrick DELAUNAY

Hi Maxime

Quoted 42 lines

From: Maxime Ripard [mailto:maxime.ripard at free-electrons.com]
Sent: jeudi 11 mai 2017 16:46

Quoted 10 lines · Patrick DELAUNAY

Hi Maxime,

Quoted 8 lines

From: Maxime Ripard [mailto:maxime.ripard at free-electrons.com]
Sent: jeudi 11 mai 2017 10:20

Hi,

Quoted 2 lines · Patrick Delaunay

I have a request to support GPT over MTD to have the MTD informations without U-Boot

environment(CONFIG_ENV_IS_NOWHERE is a

Quoted 3 lines
Quoted 3 lines
Quoted 3 lines

other requirement of my project to manage several board configuration with the same defconfig; boot from NAND or NOR or

SDCARD).

Quoted 7 lines
Quoted 6 lines

What would happen if you have a bad block in the middle of the primary or secondary GPT headers (or both)?

Maxime

Quoted 7 lines

All Bad block are skipped....
=> primary GPT header is located at the beginning of first good block => backup GPT header is located at the end the of last good block

And gpt create will failed if the erase block command (for primary or backup GPT) produce a new bad block.

Right, but what happens if your block becomes bad or too corrupted after it's been written.

You mention in your Drawbacks section that if you erase the block and is now detected to be bad, u-boot will have to act upon it. But that can happen outside of U-Boot as well, or not directly to this block, by reads or writes disturb... In this case, your GPT header is gone, and you have no way to recover from it.

Quoted 6 lines
Quoted 3 lines

Yes, I known that it is the main issue for my proposal: the management of NAND bad block. But it is the same for all the binary in boot stage (SPL / U-Boot / U-Boot env) in NAND.

U-Boot environment can be stored in UBI, and iirc U-Boot too. And usually the SPLs can be stored at multiple offsets to reduce the risks.

So multiple copy for fist boot stage (SPL or other) to reduce the risk And first boot stage need to handle the all NAND constraint.

Then for U-Boot SPL, the more simple is to manage next stage in UBIFS volume.
And for other first boot stage, multiple copy to avoid all NAND issues (bad block / read disturb)

I agree that this architecture simplify the NAND management for SPL and next boot stage and allow improvement for SPL (FALCON mode).

Quoted 7 lines
Quoted 7 lines

For my point of view, erase should done only when GPT header need to updated

=> for first flashing / complete update of NAND => for the refresh of the GPT header So the NAND block becomes BAD only in this 2 cases.

Quoted 7 lines
Quoted 4 lines

If a read or write disturb for GPT block occur, it should be detected by NAND ECC => when unrecoverable error occur for one boot the backup GPT header should be used PS: Perhaps need to do something in U-Boot to refresh primary GPT header from backup header informaiton

Like I was saying, the issue really isn't in U-Boot itself, but when U-Boot isn't there anymore to deal with those issues.

Yes in this case the product is dead....

Quoted 58 lines
Quoted 58 lines

The expected strategy is to read the boot partitions (all of them , GPT header , SPL and U-Boot) periodically outside of U-Boot and if ECC read are too important refresh them :
read the partition and write it again in RAW mode (skip bad block)

So if the partitioning is correctly managed (with reserve some tank of good block for partition refresh) The GPT can de refreshed in raw mode without breaking the other partition

My idea is : to prepare a partionning with tank of good block 3 is this example :

0 => MBR + primary GPT
1 => tank block
2 => tank block
3 => tank block
--------------------- MTD1
4 => SPL
5 => tank block
6 => tank block
7 => tank block
----------------------MTD2
8 => U-Boot 1/3
9 => U-Boot 2/3
10 => U-Boot 3/3
11 => tank block
12 => tank block
13 => tank block
----------------------MTD3
14 => UBI for kernel
......
N-8 => last usable LBA
----------------------
N-7 => tank block
N-6 => tank block
N-5 => tank block
N-4 => backup GPT
N-3 => BBT (marked as bad)
N-2 => BBT (marked as bad)
N-1 => BBT (marked as bad)
N => BBT (marked as bad)

Block 0 and N-4 will be refreshed when ECC errors reach the threshold for any partition => refresh = read + erase + write (skip bad block) => if the erase command detect a bad block, the next tank block is used.

0 => Bad block (NEW)
1 => MBR + primary GPT
2 => tank block
3 => tank block

Same for backup GPT but with inversed way

N-7 => tank block
N-6 => tank block
N-5 => backup GPT
N-4 => bad block (NEW)

Quoted 5 lines
Quoted 2 lines

The number of tank block need to be choose correctly (To be defined with NAND information)

This really feels like you're trying to reinvent the UBI-partitions wheel, but yeah, that would make it a bit more robust.

Agree: more robust but still not perfect,
and many thinks to manage (number of good block in tank reservoir) to guaranty a number of boot.

Quoted 13 lines
Quoted 4 lines

PS: it is the same for SPL and U-Boot partition

Do you know better solution to handle read disturb issue on boot partitions than refresh ?

Unfortunately, I don't, but the biggest concern I have is that you have no way to tell that your system is basically in self-destruct mode outside of U-Boot. And that's an issue because:
A) U-Boot will be there for (hopefully) less than a second
B) An user getting a brand new system will have no idea that it needs to install something else so that his entire system do not fall apart.

Quoted 2 lines

Maxime

I understood the issue, and I agree with analyze.... after some internal discussion.

In fact, today I am less and less confident with the proposed strategy by my project for NAND because I see all the impacts since I wrote the first RFC and start to test it....

I think I need to cross-check my initial requirements on my side for the boot from NAND scenario (because the project to will use SPL but also a other primary bootloader)

Since the beginning the GPT was a strong request to have common behavior fot block device and MTD device but after it seens too difficult to guaranty boot with NAND constraint.

With your feedback, I will try to drop this requirement.... I will check this point internally and to push to switch to UBIFS.

Patrick

#27 lore raw

v6 cover GPT over MTD

In reply to Maxime Ripard

Hi Maxime and Tom

Quoted 5 lines

I think I need to cross-check my initial requirements on my side for the boot from NAND scenario (because the project to will use SPL but also a other primary bootloader)

Quoted 7 lines

Since the beginning the GPT was a strong request to have common behavior fot block device and MTD device but after it seens too difficult to guaranty boot with NAND constraint.

With your feedback, I will try to drop this requirement.... I will check this point internally and to push to switch to UBIFS.

Thanks for the remarks,

But finally I will drop this patchset, as I convinced my project to no don't use GPT for NOR and NAND devices after Maxime remarks.

Tom: what is the better way to drop all the patchset (with patman or with patchwork)

Change state to rejected with patchwork ?

Regards

#28 lore raw

v6 cover GPT over MTD

Quoted 15 lines · Patrick DELAUNAY

Hi Maxime and Tom

Quoted 5 lines

I think I need to cross-check my initial requirements on my side for the boot from NAND scenario (because the project to will use SPL but also a other primary bootloader)

Quoted 7 lines

Since the beginning the GPT was a strong request to have common behavior fot block device and MTD device but after it seens too difficult to guaranty boot with NAND constraint.

With your feedback, I will try to drop this requirement.... I will check this point internally and to push to switch to UBIFS.

Quoted 7 lines

Thanks for the remarks,

But finally I will drop this patchset, as I convinced my project to no don't use GPT for NOR and NAND devices after Maxime remarks.

Tom: what is the better way to drop all the patchset (with patman or with patchwork)

Change state to rejected with patchwork ?

I moved them to deferred or rejected, I forget which, yes.

Signature · 8 lines
Tom
-------------- next part --------------
A non-text attachment was scrubbed...
Name: signature.asc
Type: application/pgp-signature
Size: 819 bytes
Desc: Digital signature
URL: <http://lists.denx.de/pipermail/u-boot/attachments/20170606/0155de5a/attachment.sig>