[SLOF] [PATCH 12/13] tools: add copy functionality

Adrian Reber adrian at lisas.de
Wed Jun 15 18:29:59 AEST 2016


sloffs can now copy a SLOF flash image from one file to
another. This is especially useful when the input is not
a regular file, but the slof_flash device. The -c, --copy
parameter copies only the actual SLOF flash image instead
of the whole device. This should result in a file, which
is identical to the file from the SLOF build process.

(cherry picked from commit 587eb035ac1fb17c3b8ab236bb06e793f08bb3ea)

Cherry picked from https://lisas.de/~adrian/slof/slof.git/

Signed-off-by: Adrian Reber <adrian at lisas.de>
---
 tools/sloffs.c | 83 ++++++++++++++++++++++++++++++++++++++++++++++++++--------
 1 file changed, 72 insertions(+), 11 deletions(-)

diff --git a/tools/sloffs.c b/tools/sloffs.c
index b416b58..5c77160 100644
--- a/tools/sloffs.c
+++ b/tools/sloffs.c
@@ -1,5 +1,5 @@
 /******************************************************************************
- * Copyright (c) 2008 Adrian Reber
+ * Copyright (c) 2008, 2009 Adrian Reber
  * All rights reserved.
  * This program and the accompanying materials
  * are made available under the terms of the BSD License
@@ -574,6 +574,57 @@ sloffs_list(const int fd)
 }
 
 static void
+sloffs_copy(const int file, const char *name)
+{
+	uint64_t len;
+	int out;
+	unsigned char *write_buf;
+	int i;
+	struct stH *header;
+
+	header = sloffs_header(file);
+
+	if (!header)
+		return;
+
+	len = be64_to_cpu(header->flashlen);
+	free(header);
+
+	out = open(name, O_CREAT | O_RDWR | O_TRUNC, 00666);
+
+	if (out == -1) {
+		perror(name);
+		exit(1);
+	}
+	/* write byte at the end to be able to mmap it */
+	lseek(out, len - 1, SEEK_SET);
+	write(out, "", 1);
+	write_buf = mmap(NULL, len, PROT_WRITE, MAP_SHARED, out, 0);
+
+	if (write_buf == MAP_FAILED) {
+		perror("mmap");
+		exit(1);
+	}
+
+	lseek(file, 0, SEEK_SET);
+
+	for (;;) {
+		i = read(file, write_buf, len);
+		if (i < 0) {
+			perror("read");
+			exit(1);
+		}
+		if (i == 0)
+			break;
+		write_buf += i;
+		len -= i;
+	}
+
+	munmap(write_buf, len);
+	close(out);
+}
+
+static void
 usage(void)
 {
 	printf("sloffs lists or changes a SLOF flash image\n\n");
@@ -581,15 +632,17 @@ usage(void)
 	printf("  sloffs [OPTION]... [FILE]\n\n");
 	printf("Options:\n");
 	printf("  -h, --help             show this help, then exit\n");
-	printf(
-		"  -l, --list             print a listing of all files in the image\n");
+	printf("  -l, --list             list all files in the flash image\n");
 	printf("  -v, --version          print the version, then exit\n");
 	printf("  -d, --dump             dump the information from the header\n");
-	printf(
-		"  -a, --append=FILENAME  append file at the end of the existing image\n");
-	printf("  -o, --output=FILENAME  if appending a file this parameter is\n");
-	printf(
-		"                         to specify the name of the output file\n");
+	printf("  -a, --append=FILENAME  append file at the end of\n");
+	printf("                         the existing image\n");
+	printf("  -o, --output=FILENAME  if appending a file this parameter\n");
+	printf("                         is necessary to specify the name of\n");
+	printf("                         the output file\n");
+	printf("  -c, --copy=FILENAME    copy SLOF image to specified file\n");
+	printf("                         this is especially useful if the\n");
+	printf("                         source file is /dev/slof_flash\n");
 	printf("\n");
 	exit(1);
 }
@@ -605,9 +658,10 @@ main(int argc, char *argv[])
 		{ "dump", 0, NULL, 'd' },
 		{ "append", 1, NULL, 'a' },
 		{ "output", 1, NULL, 'o' },
+		{ "copy", 1, NULL, 'o' },
 		{ 0, 0, 0, 0 }
 	};
-	const char *soption = "dhlva:o:";
+	const char *soption = "dhlva:o:c:";
 	int c;
 	char mode = 0;
 	char *append = NULL;
@@ -634,6 +688,10 @@ main(int argc, char *argv[])
 		case 'o':
 			output = strdup(optarg);
 			break;
+		case 'c':
+			mode = 'c';
+			output = strdup(optarg);
+			break;
 		case 'h':
 		default:
 			usage();
@@ -661,12 +719,15 @@ main(int argc, char *argv[])
 		break;
 	case 'a':
 		if (!output) {
-			printf(
-				"sloffs requires -o, --output=FILENAME when in append mode\n\n");
+			printf("sloffs requires -o, --output=FILENAME"
+			       " when in append mode\n\n");
 			usage();
 		}
 		sloffs_append(fd, append, output);
 		break;
+	case 'c':
+		sloffs_copy(fd, output);
+		break;
 	}
 
 	free(append);
-- 
2.7.4



More information about the SLOF mailing list