From patchwork Wed Jul 27 06:19:20 2016 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Alexey Kardashevskiy X-Patchwork-Id: 653106 Return-Path: X-Original-To: incoming@patchwork.ozlabs.org Delivered-To: patchwork-incoming@bilbo.ozlabs.org Received: from lists.ozlabs.org (lists.ozlabs.org [103.22.144.68]) (using TLSv1.2 with cipher AECDH-AES256-SHA (256/256 bits)) (No client certificate requested) by ozlabs.org (Postfix) with ESMTPS id 3rzlRB23Qtz9t2c for ; Wed, 27 Jul 2016 16:27:50 +1000 (AEST) Received: from ozlabs.org (lists.ozlabs.org [IPv6:2401:3900:2:1::3]) by lists.ozlabs.org (Postfix) with ESMTP id 3rzlR96xshzDrFR for ; Wed, 27 Jul 2016 16:27:49 +1000 (AEST) X-Original-To: slof@lists.ozlabs.org Delivered-To: slof@lists.ozlabs.org Received: from ozlabs.ru (ozlabs.ru [83.169.36.222]) by lists.ozlabs.org (Postfix) with ESMTP id 3rzlR15Y8gzDrFD for ; Wed, 27 Jul 2016 16:27:41 +1000 (AEST) Received: from vpl2.ozlabs.ibm.com (localhost [IPv6:::1]) by ozlabs.ru (Postfix) with ESMTP id 5237112072C; Wed, 27 Jul 2016 08:19:47 +0200 (CEST) From: Alexey Kardashevskiy To: slof@lists.ozlabs.org Date: Wed, 27 Jul 2016 16:19:20 +1000 Message-Id: <1469600360-19292-8-git-send-email-aik@ozlabs.ru> X-Mailer: git-send-email 2.5.0.rc3 In-Reply-To: <1469600360-19292-1-git-send-email-aik@ozlabs.ru> References: <1469600360-19292-1-git-send-email-aik@ozlabs.ru> Subject: [SLOF] [PATCH slof 7/7] tools: add copy functionality X-BeenThere: slof@lists.ozlabs.org X-Mailman-Version: 2.1.22 Precedence: list List-Id: "Patches for https://github.com/aik/SLOF" List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Cc: Adrian Reber MIME-Version: 1.0 Errors-To: slof-bounces+incoming=patchwork.ozlabs.org@lists.ozlabs.org Sender: "SLOF" From: Adrian Reber 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 Signed-off-by: Alexey Kardashevskiy --- tools/sloffs.c | 64 +++++++++++++++++++++++++++++++++++++++++++++++++++++++++- 1 file changed, 63 insertions(+), 1 deletion(-) diff --git a/tools/sloffs.c b/tools/sloffs.c index 4d998bb..91a23c3 100644 --- a/tools/sloffs.c +++ b/tools/sloffs.c @@ -580,6 +580,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"); @@ -595,6 +646,9 @@ usage(void) 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); } @@ -610,9 +664,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; @@ -639,6 +694,10 @@ main(int argc, char *argv[]) case 'o': output = strdup(optarg); break; + case 'c': + mode = 'c'; + output = strdup(optarg); + break; case 'h': default: usage(); @@ -672,6 +731,9 @@ main(int argc, char *argv[]) } sloffs_append(fd, append, output); break; + case 'c': + sloffs_copy(fd, output); + break; } free(append);