From ca4c33abb92929df30c554c2bc7b9c333ef5e2d1 Mon Sep 17 00:00:00 2001
From: Gabor Juhos <juhosg@openwrt.org>
Date: Thu, 7 Nov 2013 19:53:01 +0000
Subject: [PATCH] tools/mtd-utils: add EOF marker support to libubigen/ubinize

Signed-off-by: Gabor Juhos <juhosg@openwrt.org>

SVN-Revision: 38682
---
 ...add-ubigen_write_terminator-function.patch | 89 +++++++++++++++++++
 .../201-ubinize-add-terminator-support.patch  | 77 ++++++++++++++++
 2 files changed, 166 insertions(+)
 create mode 100644 tools/mtd-utils/patches/200-libubigen-add-ubigen_write_terminator-function.patch
 create mode 100644 tools/mtd-utils/patches/201-ubinize-add-terminator-support.patch

diff --git a/tools/mtd-utils/patches/200-libubigen-add-ubigen_write_terminator-function.patch b/tools/mtd-utils/patches/200-libubigen-add-ubigen_write_terminator-function.patch
new file mode 100644
index 0000000000..8bd642fbc8
--- /dev/null
+++ b/tools/mtd-utils/patches/200-libubigen-add-ubigen_write_terminator-function.patch
@@ -0,0 +1,89 @@
+--- a/ubi-utils/src/libubigen.c
++++ b/ubi-utils/src/libubigen.c
+@@ -122,8 +122,9 @@ int ubigen_add_volume(const struct ubige
+ 	return 0;
+ }
+ 
+-void ubigen_init_ec_hdr(const struct ubigen_info *ui,
+-		        struct ubi_ec_hdr *hdr, long long ec)
++static void __ubigen_init_ec_hdr(const struct ubigen_info *ui,
++				 struct ubi_ec_hdr *hdr, long long ec,
++				 int eof)
+ {
+ 	uint32_t crc;
+ 
+@@ -136,10 +137,22 @@ void ubigen_init_ec_hdr(const struct ubi
+ 	hdr->data_offset = cpu_to_be32(ui->data_offs);
+ 	hdr->image_seq = cpu_to_be32(ui->image_seq);
+ 
++	if (eof) {
++		hdr->padding1[0] = 'E';
++		hdr->padding1[1] = 'O';
++		hdr->padding1[2] = 'F';
++	}
++
+ 	crc = mtd_crc32(UBI_CRC32_INIT, hdr, UBI_EC_HDR_SIZE_CRC);
+ 	hdr->hdr_crc = cpu_to_be32(crc);
+ }
+ 
++void ubigen_init_ec_hdr(const struct ubigen_info *ui,
++		        struct ubi_ec_hdr *hdr, long long ec)
++{
++	__ubigen_init_ec_hdr(ui, hdr, ec, 0);
++}
++
+ void ubigen_init_vid_hdr(const struct ubigen_info *ui,
+ 			 const struct ubigen_vol_info *vi,
+ 			 struct ubi_vid_hdr *hdr, int lnum,
+@@ -307,6 +320,39 @@ int ubigen_write_layout_vol(const struct
+ 	}
+ 
+ 	free(outbuf);
++	return 0;
++
++out_free:
++	free(outbuf);
++	return -1;
++}
++
++int ubigen_write_eof_markers(const struct ubigen_info *ui, long long ec,
++			     int count, int out_fd)
++{
++	char *outbuf;
++	int peb_size = ui->peb_size;
++
++	outbuf = malloc(peb_size);
++	if (!outbuf) {
++		sys_errmsg("cannot allocate %d bytes of memory", peb_size);
++		return -1;
++	}
++
++	memset(outbuf, 0xFF, peb_size);
++	__ubigen_init_ec_hdr(ui, (struct ubi_ec_hdr *)outbuf, ec, 1);
++
++	while (count) {
++		if (write(out_fd, outbuf, peb_size) != peb_size) {
++			sys_errmsg("cannot write %d bytes to the output file",
++				   peb_size);
++			goto out_free;
++		}
++
++		count--;
++	}
++
++	free(outbuf);
+ 	return 0;
+ 
+ out_free:
+--- a/ubi-utils/include/libubigen.h
++++ b/ubi-utils/include/libubigen.h
+@@ -188,6 +188,9 @@ int ubigen_write_layout_vol(const struct
+ 			    long long ec1, long long ec2,
+ 			    struct ubi_vtbl_record *vtbl, int fd);
+ 
++int ubigen_write_eof_markers(const struct ubigen_info *ui, long long ec,
++			     int count, int out_fd);
++
+ #ifdef __cplusplus
+ }
+ #endif
diff --git a/tools/mtd-utils/patches/201-ubinize-add-terminator-support.patch b/tools/mtd-utils/patches/201-ubinize-add-terminator-support.patch
new file mode 100644
index 0000000000..73b0c070a7
--- /dev/null
+++ b/tools/mtd-utils/patches/201-ubinize-add-terminator-support.patch
@@ -0,0 +1,77 @@
+--- a/ubi-utils/src/ubinize.c
++++ b/ubi-utils/src/ubinize.c
+@@ -70,6 +70,8 @@ static const char optionsstr[] =
+ "                             (default is 1)\n"
+ "-Q, --image-seq=<num>        32-bit UBI image sequence number to use\n"
+ "                             (by default a random number is picked)\n"
++"-E, --eof-markers=<num>      number of eof-markers to put at the end of the\n"
++"                             output image\n"
+ "-v, --verbose                be verbose\n"
+ "-h, --help                   print help message\n"
+ "-V, --version                print program version";
+@@ -79,7 +81,7 @@ static const char usage[] =
+ "\t\t[-x <num>] [-Q <num>] [-v] [-h] [-V] [--output=<filename>] [--peb-size=<bytes>]\n"
+ "\t\t[--min-io-size=<bytes>] [--sub-page-size=<bytes>] [--vid-hdr-offset=<num>]\n"
+ "\t\t[--erase-counter=<num>] [--ubi-ver=<num>] [--image-seq=<num>] [--verbose] [--help]\n"
+-"\t\t[--version] ini-file\n"
++"\t\t[--eof-markers=<num>] [--version] ini-file\n"
+ "Example: " PROGRAM_NAME " -o ubi.img -p 16KiB -m 512 -s 256 cfg.ini - create UBI image\n"
+ "         'ubi.img' as described by configuration file 'cfg.ini'";
+ 
+@@ -125,6 +127,7 @@ static const struct option long_options[
+ 	{ .name = "erase-counter",  .has_arg = 1, .flag = NULL, .val = 'e' },
+ 	{ .name = "ubi-ver",        .has_arg = 1, .flag = NULL, .val = 'x' },
+ 	{ .name = "image-seq",      .has_arg = 1, .flag = NULL, .val = 'Q' },
++	{ .name = "eof-markers",    .has_arg = 1, .flag = NULL, .val = 'E' },
+ 	{ .name = "verbose",        .has_arg = 0, .flag = NULL, .val = 'v' },
+ 	{ .name = "help",           .has_arg = 0, .flag = NULL, .val = 'h' },
+ 	{ .name = "version",        .has_arg = 0, .flag = NULL, .val = 'V' },
+@@ -144,6 +147,7 @@ struct args {
+ 	uint32_t image_seq;
+ 	int verbose;
+ 	dictionary *dict;
++	int eof_markers;
+ };
+ 
+ static struct args args = {
+@@ -162,7 +166,7 @@ static int parse_opt(int argc, char * co
+ 		int key, error = 0;
+ 		unsigned long int image_seq;
+ 
+-		key = getopt_long(argc, argv, "o:p:m:s:O:e:x:Q:vhV", long_options, NULL);
++		key = getopt_long(argc, argv, "o:p:m:s:O:e:x:Q:E:vhV", long_options, NULL);
+ 		if (key == -1)
+ 			break;
+ 
+@@ -222,6 +226,12 @@ static int parse_opt(int argc, char * co
+ 			args.image_seq = image_seq;
+ 			break;
+ 
++		case 'E':
++			args.eof_markers = simple_strtoul(optarg, &error);
++			if (error)
++				return errmsg("bad number of eof-markers: \"%s\"", optarg);
++			break;
++
+ 		case 'v':
+ 			args.verbose = 1;
+ 			break;
+@@ -599,6 +609,18 @@ int main(int argc, char * const argv[])
+ 			printf("\n");
+ 	}
+ 
++	if (args.eof_markers) {
++		verbose(args.verbose, "writing %d eof-marker blocks",
++			args.eof_markers);
++
++		err = ubigen_write_eof_markers(&ui, args.ec, args.eof_markers,
++					       args.out_fd);
++		if (err) {
++			errmsg("cannot write eof-marker blocks");
++			goto out_free;
++		}
++	}
++
+ 	verbose(args.verbose, "writing layout volume");
+ 
+ 	err = ubigen_write_layout_vol(&ui, 0, 1, args.ec, args.ec, vtbl, args.out_fd);
-- 
GitLab