mail archive of the barebox mailing list
 help / color / mirror / Atom feed
From: Alexey Galakhov <agalakhov@gmail.com>
To: barebox@lists.infradead.org
Cc: Alexey Galakhov <agalakhov@gmail.com>
Subject: [PATCH 4/7] S5P boot header and image generator
Date: Fri, 18 May 2012 15:43:28 +0600	[thread overview]
Message-ID: <1337334211-12576-5-git-send-email-agalakhov@gmail.com> (raw)
In-Reply-To: <1337334211-12576-1-git-send-email-agalakhov@gmail.com>

Signed-off-by: Alexey Galakhov <agalakhov@gmail.com>
---
 .gitignore          |    2 +
 Makefile            |    2 +-
 arch/arm/Makefile   |    7 +++
 scripts/Makefile    |    1 +
 scripts/s5p_cksum.c |  140 +++++++++++++++++++++++++++++++++++++++++++++++++++
 5 files changed, 151 insertions(+), 1 deletion(-)
 create mode 100644 scripts/s5p_cksum.c

diff --git a/.gitignore b/.gitignore
index 8e208e0..df0ed2c 100644
--- a/.gitignore
+++ b/.gitignore
@@ -33,6 +33,7 @@ barebox.S
 barebox.bin
 barebox.srec
 barebox.netx
+barebox.s5p
 barebox.map
 System.map
 Module.symvers
@@ -63,3 +64,4 @@ cscope.*
 # patches
 *.patch
 scripts/gen_netx_image
+scripts/s5p_cksum
diff --git a/Makefile b/Makefile
index 44b7dbd..91ded1b 100644
--- a/Makefile
+++ b/Makefile
@@ -1003,7 +1003,7 @@ CLEAN_DIRS  += $(MODVERDIR)
 CLEAN_FILES +=	barebox System.map include/generated/barebox_default_env.h \
                 .tmp_version .tmp_barebox* barebox.bin barebox.map barebox.S \
 		.tmp_kallsyms* barebox_default_env* barebox.ldr \
-		Doxyfile.version barebox.srec
+		Doxyfile.version barebox.srec barebox.s5p
 
 # Directories & files removed with 'make mrproper'
 MRPROPER_DIRS  += include/config include2 usr/include
diff --git a/arch/arm/Makefile b/arch/arm/Makefile
index d0bfd71..40291aa 100644
--- a/arch/arm/Makefile
+++ b/arch/arm/Makefile
@@ -164,6 +164,13 @@ ifeq ($(machine-y),netx)
 KBUILD_IMAGE := barebox.netx
 endif
 
+barebox.s5p: barebox.bin
+	$(Q)scripts/s5p_cksum barebox.bin barebox.s5p
+
+ifeq ($(CONFIG_ARCH_S5PCxx),y)
+KBUILD_IMAGE := barebox.s5p
+endif
+
 MLO: barebox.bin
 	@echo "  IFT    " $@
 	$(Q)scripts/omap_signGP barebox.bin $(TEXT_BASE) 1
diff --git a/scripts/Makefile b/scripts/Makefile
index cb049fd..784d205 100644
--- a/scripts/Makefile
+++ b/scripts/Makefile
@@ -10,6 +10,7 @@ hostprogs-y                      += mkimage
 hostprogs-y                      += bareboxenv
 hostprogs-$(CONFIG_ARCH_NETX)    += gen_netx_image
 hostprogs-$(CONFIG_ARCH_OMAP)    += omap_signGP
+hostprogs-$(CONFIG_ARCH_S5PCxx)  += s5p_cksum
 
 always		:= $(hostprogs-y) $(hostprogs-m)
 
diff --git a/scripts/s5p_cksum.c b/scripts/s5p_cksum.c
new file mode 100644
index 0000000..7142532
--- /dev/null
+++ b/scripts/s5p_cksum.c
@@ -0,0 +1,140 @@
+/*
+ * Copyright (C) 2012 Alexey Galakhov
+ *
+ * See file CREDITS for list of people who contributed to this
+ * project.
+ *
+ * This program is free software; you can redistribute it and/or
+ * modify it under the terms of the GNU General Public License
+ * version 2 as published by the Free Software Foundation.
+ *
+ * This program is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+ * GNU General Public License for more details.
+ *
+ * You should have received a copy of the GNU General Public License
+ * along with this program; if not, write to the Free Software
+ * Foundation, Inc., 59 Temple Place, Suite 330, Boston,
+ * MA 02111-1307 USA
+ */
+
+#include <stdio.h>
+#include <stdint.h>
+#include <stdlib.h>
+#include <string.h>
+#include <stddef.h>
+#include <errno.h>
+
+#define DEFAULT_BUF_SIZE 8192
+
+static int usage(const char* me)
+{
+	printf("Usage: %s <input> <output> [<bufsize]\n", me);
+	return 1;
+}
+
+static void put32(uint8_t *ptr, uint32_t value)
+{
+	ptr[0] = value & 0xFF;
+	ptr[1] = (value >> 8) & 0xFF;
+	ptr[2] = (value >> 16) & 0xFF;
+	ptr[3] = (value >> 24) & 0xFF;
+}
+
+static size_t safe_fread(void *buf, size_t len, FILE* file)
+{
+	size_t rd = fread(buf, 1, len, file);
+	if (! rd) {
+		if (ferror(file))
+			fprintf(stderr, "Error reading file: %s\n", strerror(errno));
+		else
+			fprintf(stderr, "Unexpected end of file\n");
+	}
+	return rd;
+}
+
+static size_t safe_fwrite(const void *buf, size_t len, FILE* file)
+{
+	size_t wr = fwrite(buf, 1, len, file);
+	if (wr != len) {
+		fprintf(stderr, "Error writing file: %s\n", strerror(errno));
+		return 0;
+	}
+	return wr;
+}
+
+static int process(FILE *input, FILE *output, uint8_t *buf, unsigned bufsize)
+{
+	size_t rd;
+	unsigned i;
+	uint32_t cksum;
+	/* Read first chunk */
+	rd = safe_fread(buf + 16, bufsize - 16, input);
+	if (! rd)
+		return 4;
+	/* Calculate header */
+	put32(buf + 0, bufsize);
+	cksum = 0;
+	for (i = 16; i < bufsize; ++i)
+		cksum += (uint32_t)buf[i];
+	put32(buf + 8, cksum);
+	if (! safe_fwrite(buf, bufsize, output))
+		return 4;
+	/* Copy the rest of file */
+	while (! feof(input)) {
+		rd = safe_fread(buf, bufsize, input);
+		if (! rd)
+			return 4;
+		if (! safe_fwrite(buf, rd, output))
+			return 4;
+	}
+	return 0;
+}
+
+static int work(const char* me, const char *infile, const char *outfile, unsigned bufsize)
+{
+	uint8_t *buf;
+	FILE *input;
+	FILE *output;
+	int ret;
+	if (bufsize < 512 || bufsize > 65536)
+		return usage(me);
+	buf = calloc(1, bufsize);
+	if (! buf) {
+		fprintf(stderr, "Unable to allocate %u bytes of memory\n", bufsize);
+		return 2;
+	}
+	input = fopen(infile, "r");
+	if (! input) {
+		fprintf(stderr, "Cannot open `%s' for reading\n", infile);
+		free(buf);
+		return 3;
+	}
+	output = fopen(outfile, "w");
+	if (! output) {
+		fprintf(stderr, "Cannot open `%s' for writing\n", outfile);
+		fclose(input);
+		free(buf);
+		return 3;
+	}
+
+	ret = process(input, output, buf, bufsize);
+
+	fclose(output);
+	fclose(input);
+	free(buf);
+	return ret;
+}
+
+int main(int argc, char** argv)
+{
+	switch (argc) {
+	case 3:
+		return work(argv[0], argv[1], argv[2], DEFAULT_BUF_SIZE);
+	case 4:
+		return work(argv[0], argv[1], argv[2], atoi(argv[3]));
+	default:
+		return usage(argv[0]);
+	}
+}
-- 
1.7.10


_______________________________________________
barebox mailing list
barebox@lists.infradead.org
http://lists.infradead.org/mailman/listinfo/barebox

  parent reply	other threads:[~2012-05-18  9:44 UTC|newest]

Thread overview: 29+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2012-05-15 13:20 [PATCHv3 0/7] Cleaned up version of S5PV210 code Alexey Galakhov
2012-05-15 13:20 ` [PATCH 1/7] Make S3C24xx config options available for all S3Cs Alexey Galakhov
2012-05-15 13:20 ` [PATCH 2/7] Split S3C generic and S3C24xx specific code Alexey Galakhov
2012-05-17 17:52   ` Sascha Hauer
2012-05-17 18:33     ` Alexey Galakhov
2012-05-17 19:49       ` Juergen Beisert
2012-05-18  6:49         ` Alexey Galakhov
2012-05-18  8:38           ` Sascha Hauer
2012-05-18  9:06             ` Alexey Galakhov
2012-05-18  8:35       ` Sascha Hauer
2012-05-18  9:43     ` [PATCHv4 0/7] S5PV210 support (upd.) Alexey Galakhov
2012-05-18  9:43       ` [PATCH 1/7] Make S3C24xx config options available for all S3Cs Alexey Galakhov
2012-05-18  9:43       ` [PATCHv4 2/7] Split S3C generic and S3C24xx specific code Alexey Galakhov
2012-05-18  9:43       ` [PATCH 3/7] Add support for Samsung S5P architecture (S5PV210) Alexey Galakhov
2012-05-18  9:43       ` Alexey Galakhov [this message]
2012-05-18  9:43       ` [PATCHv4 5/7] S5P lowlevel clock init Alexey Galakhov
2012-05-18  9:43       ` [PATCHv4 6/7] S5P DRAM support Alexey Galakhov
2012-05-21 20:02         ` Sascha Hauer
2012-05-22  9:17           ` Alexey Galakhov
2012-05-22 18:29             ` Sascha Hauer
2012-05-22 19:12               ` Alexey Galakhov
2012-05-18  9:43       ` [PATCHv4 7/7] Add FriendlyArm Tiny210 board (S5PV210) Alexey Galakhov
2012-05-21 20:00       ` [PATCHv4 0/7] S5PV210 support (upd.) Sascha Hauer
2012-05-22  9:18         ` Alexey Galakhov
2012-05-15 13:20 ` [PATCH 3/7] Add support for Samsung S5P architecture (S5PV210) Alexey Galakhov
2012-05-15 13:20 ` [PATCH 4/7] S5P boot header and image generator Alexey Galakhov
2012-05-15 13:20 ` [PATCH 5/7] S5P lowlevel clock init Alexey Galakhov
2012-05-15 13:20 ` [PATCH 6/7] S5P DRAM support Alexey Galakhov
2012-05-15 13:20 ` [PATCH 7/7] Add FriendlyArm Tiny210 board (S5PV210) Alexey Galakhov

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=1337334211-12576-5-git-send-email-agalakhov@gmail.com \
    --to=agalakhov@gmail.com \
    --cc=barebox@lists.infradead.org \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox