diff mbox series

ffspart: Don't require user to create blank partitions manually

Message ID 20180529042518.22459-1-sam@mendozajonas.com
State Accepted
Headers show
Series ffspart: Don't require user to create blank partitions manually | expand

Commit Message

Sam Mendoza-Jonas May 29, 2018, 4:25 a.m. UTC
Add '--allow-empty' which allows the filename for a given partition to
be blank. If set ffspart will set that part of the PNOR file 'blank' and
set ECC bits if required.
Without this option behaviour is unchanged and ffspart will return an
error if it can not find the partition file.

Signed-off-by: Samuel Mendoza-Jonas <sam@mendozajonas.com>
---
 external/ffspart/ffspart.c                    |  41 ++++++++++++++----
 .../ffspart/test/files/16-create-blank.in     |   4 ++
 .../ffspart/test/files/16-create-blank.out    | Bin 0 -> 2560 bytes
 external/ffspart/test/results/00-usage.out    |   5 ++-
 .../ffspart/test/results/01-param-sanity.out  |   5 ++-
 .../test/results/01.1-param-sanity.out        |   5 ++-
 external/ffspart/test/tests/16-create-blank   |  15 +++++++
 7 files changed, 64 insertions(+), 11 deletions(-)
 create mode 100644 external/ffspart/test/files/16-create-blank.in
 create mode 100644 external/ffspart/test/files/16-create-blank.out
 create mode 100644 external/ffspart/test/tests/16-create-blank

Comments

Stewart Smith June 5, 2018, 4:15 a.m. UTC | #1
Samuel Mendoza-Jonas <sam@mendozajonas.com> writes:
> Add '--allow-empty' which allows the filename for a given partition to
> be blank. If set ffspart will set that part of the PNOR file 'blank' and
> set ECC bits if required.
> Without this option behaviour is unchanged and ffspart will return an
> error if it can not find the partition file.

Makes sense. Merged to master as of 5cc781de88032d7e4c5e36399098d54e3cefd126
diff mbox series

Patch

diff --git a/external/ffspart/ffspart.c b/external/ffspart/ffspart.c
index d737e302..179ed582 100644
--- a/external/ffspart/ffspart.c
+++ b/external/ffspart/ffspart.c
@@ -122,16 +122,16 @@  static struct ffs_hdr *parse_toc(const char *line, uint32_t block_size,
 }
 
 static int parse_entry(struct blocklevel_device *bl,
-		struct ffs_hdr **tocs, const char *line)
+		struct ffs_hdr **tocs, const char *line, bool allow_empty)
 {
 	char name[FFS_PART_NAME_MAX + 2] = { 0 };
 	struct ffs_entry_user user = { 0 };
-	uint32_t pbase, psize, pactual;
+	uint32_t pbase, psize, pactual, i;
 	struct ffs_entry *new_entry;
 	struct stat data_stat;
 	const char *filename;
 	bool added = false;
-	uint8_t *data_ptr;
+	uint8_t *data_ptr, ecc = 0;
 	int data_fd, rc;
 	char *pos;
 
@@ -290,6 +290,25 @@  static int parse_entry(struct blocklevel_device *bl,
 					"(%m)\n", filename, name);
 		munmap(data_ptr, pactual);
 		close(data_fd);
+	} else {
+		if (!allow_empty) {
+			fprintf(stderr, "Filename missing for partition %s!\n",
+					name);
+			return -1;
+		}
+		if (has_ecc(new_entry)) {
+			i = pbase + 8;
+			while (i < pbase + psize) {
+				rc = blocklevel_write(bl, i, &ecc, sizeof(ecc));
+				if (rc) {
+					fprintf(stderr, "\nError setting ECC byte at 0x%08x\n",
+							i);
+					return rc;
+				}
+				i += 9;
+			}
+		}
+
 	}
 
 	return 0;
@@ -303,8 +322,10 @@  static void print_version(void)
 static void print_help(const char *pname)
 {
 	print_version();
-	printf("Usage: %s [options] -s size -c num -i layout_file -p pnor_file ...\n\n", pname);
+	printf("Usage: %s [options] -e -s size -c num -i layout_file -p pnor_file ...\n\n", pname);
 	printf(" Options:\n");
+	printf("\t-e, --allow_empty\n");
+	printf("\t\tCreate partition as blank if not specified (sets ECC if flag set)\n\n");
 	printf("\t-s, --block_size=size\n");
 	printf("\t\tSize (in hex with leading 0x) of the blocks on the flash in bytes\n\n");
 	printf("\t-c, --block_count=num\n");
@@ -318,7 +339,7 @@  static void print_help(const char *pname)
 int main(int argc, char *argv[])
 {
 	char *pnor = NULL, *input = NULL, line[MAX_LINE];
-	bool toc_created = false, bad_input = false;
+	bool toc_created = false, bad_input = false, allow_empty = false;
 	uint32_t block_size = 0, block_count = 0;
 	struct ffs_hdr *tocs[MAX_TOCS] = { 0 };
 	struct blocklevel_device *bl = NULL;
@@ -328,19 +349,23 @@  int main(int argc, char *argv[])
 
 	while(1) {
 		struct option long_opts[] = {
+			{"allow_empty", no_argument,		NULL,	'e'},
 			{"block_count",	required_argument,	NULL,	'c'},
 			{"block_size",	required_argument,	NULL,	's'},
-			{"debug",	no_argument,	NULL,	'g'},
+			{"debug",	no_argument,		NULL,	'g'},
 			{"input",	required_argument,	NULL,	'i'},
 			{"pnor",	required_argument,	NULL,	'p'},
 			{NULL,	0,	0, 0}
 		};
 		int c, oidx = 0;
 
-		c = getopt_long(argc, argv, "+:c:gi:p:s:", long_opts, &oidx);
+		c = getopt_long(argc, argv, "+:ec:gi:p:s:", long_opts, &oidx);
 		if (c == EOF)
 			break;
 		switch(c) {
+		case 'e':
+			allow_empty = true;
+			break;
 		case 'c':
 			block_count = strtoul(optarg, NULL, 0);
 			break;
@@ -459,7 +484,7 @@  int main(int argc, char *argv[])
 				}
 				toc_created = true;
 			}
-			rc = parse_entry(bl, tocs, line);
+			rc = parse_entry(bl, tocs, line, allow_empty);
 			if (rc) {
 				rc = 6;
 				goto parse_out;
diff --git a/external/ffspart/test/files/16-create-blank.in b/external/ffspart/test/files/16-create-blank.in
new file mode 100644
index 00000000..7cff6bf0
--- /dev/null
+++ b/external/ffspart/test/files/16-create-blank.in
@@ -0,0 +1,4 @@ 
+ONE,0x00400,0x00000100,EL,,
+TWO,0x00500,0x00000100,EF,,
+THREE,0x600,0x00000100,EF,,
+FOUR,0x0700,0x00000100,EF,,
diff --git a/external/ffspart/test/files/16-create-blank.out b/external/ffspart/test/files/16-create-blank.out
new file mode 100644
index 0000000000000000000000000000000000000000..43b9583b3703c360208a31a52468fb51e870ab95
GIT binary patch
literal 2560
zcmWG=3<_ajU|<AdW*}|=Vpa&3feXk+0RfJ|HwzMrN>BtL9OnN(0A#|<0n#Ajs0;e%
z^>zFExuP4&0u*3`+Rp@HLli(M6x9u2526T>z%kfp{WB!oA6*A9yx{hOJdM|W1_5;K
z#Pe70F#H+f5#)*%0&Gb3gTn%1B6j~%V*k74Iy>C_Lxa#<%#LI~I4o#o|IP*4>p}56
i3aB3f3@{@{AOqy}5$J8Wmq-0S8vn5DKT40R5C8y83<bmh

literal 0
HcmV?d00001

diff --git a/external/ffspart/test/results/00-usage.out b/external/ffspart/test/results/00-usage.out
index 3ad0441d..cf7213d7 100644
--- a/external/ffspart/test/results/00-usage.out
+++ b/external/ffspart/test/results/00-usage.out
@@ -1,7 +1,10 @@ 
 Open-Power FFS format tool VERSION
-Usage: ./ffspart [options] -s size -c num -i layout_file -p pnor_file ...
+Usage: ./ffspart [options] -e -s size -c num -i layout_file -p pnor_file ...
 
  Options:
+	-e, --allow_empty
+		Create partition as blank if not specified (sets ECC if flag set)
+
 	-s, --block_size=size
 		Size (in hex with leading 0x) of the blocks on the flash in bytes
 
diff --git a/external/ffspart/test/results/01-param-sanity.out b/external/ffspart/test/results/01-param-sanity.out
index 3ad0441d..cf7213d7 100644
--- a/external/ffspart/test/results/01-param-sanity.out
+++ b/external/ffspart/test/results/01-param-sanity.out
@@ -1,7 +1,10 @@ 
 Open-Power FFS format tool VERSION
-Usage: ./ffspart [options] -s size -c num -i layout_file -p pnor_file ...
+Usage: ./ffspart [options] -e -s size -c num -i layout_file -p pnor_file ...
 
  Options:
+	-e, --allow_empty
+		Create partition as blank if not specified (sets ECC if flag set)
+
 	-s, --block_size=size
 		Size (in hex with leading 0x) of the blocks on the flash in bytes
 
diff --git a/external/ffspart/test/results/01.1-param-sanity.out b/external/ffspart/test/results/01.1-param-sanity.out
index 3ad0441d..cf7213d7 100644
--- a/external/ffspart/test/results/01.1-param-sanity.out
+++ b/external/ffspart/test/results/01.1-param-sanity.out
@@ -1,7 +1,10 @@ 
 Open-Power FFS format tool VERSION
-Usage: ./ffspart [options] -s size -c num -i layout_file -p pnor_file ...
+Usage: ./ffspart [options] -e -s size -c num -i layout_file -p pnor_file ...
 
  Options:
+	-e, --allow_empty
+		Create partition as blank if not specified (sets ECC if flag set)
+
 	-s, --block_size=size
 		Size (in hex with leading 0x) of the blocks on the flash in bytes
 
diff --git a/external/ffspart/test/tests/16-create-blank b/external/ffspart/test/tests/16-create-blank
new file mode 100644
index 00000000..02640e4e
--- /dev/null
+++ b/external/ffspart/test/tests/16-create-blank
@@ -0,0 +1,15 @@ 
+#! /bin/sh
+
+touch $DATA_DIR/$CUR_TEST.gen
+
+run_binary "./ffspart" "-s 0x100 -c 10 -i $DATA_DIR/$CUR_TEST.in -p $DATA_DIR/$CUR_TEST.gen --allow_empty"
+if [ "$?" -ne 0 ] ; then
+	fail_test
+fi
+
+if ! cmp $DATA_DIR/$CUR_TEST.out $DATA_DIR/$CUR_TEST.gen ; then
+	echo "Output differs"
+	fail_test
+fi
+
+pass_test