All of lore.kernel.org
 help / color / mirror / Atom feed
From: Rajeshwari S Shinde <rajeshwari.s@samsung.com>
To: u-boot@lists.denx.de
Subject: [U-Boot] [PATCH 09/10 V6] SPL: EXYNOS: Prepare for variable size SPL support
Date: Tue, 29 Oct 2013 12:53:12 +0530	[thread overview]
Message-ID: <1383031393-6093-10-git-send-email-rajeshwari.s@samsung.com> (raw)
In-Reply-To: <1383031393-6093-1-git-send-email-rajeshwari.s@samsung.com>

When variable size SPL is used, the BL1 expects the SPL to be
encapsulated differently: instead of putting the checksum at a fixed
offset in the SPL blob, prepend the blob with a header including the
size and the checksum.

The enhancements include
	- adding a command line option, '--vs' to indicate the need for the
	variable size encapsulation
	- padding the fixed size encapsulated blob with 0xff instead of random
	memory contents
	- do not silently truncate the input file, report error instead
	- no need to explicitly closing files/freeing memory, this all happens
	on exit; removing cleanups it makes code clearer
	- profuse commenting
	- modify Makefile to allow enabling the new feature per board

Signed-off-by: Vadim Bendebury <vbendeb@chromium.org>
Signed-off-by: Rajeshwari S Shinde <rajeshwari.s@samsung.com>
Acked-by: Simon Glass <sjg@chromium.org>
---
Changes in V2:
	- None
Changes in V3:
	- None
Changes in V4:
	- Created a common exynos5-dt.h
Changes in V5:
	- None
Changes in V6:
	- None.
 include/configs/smdk5420.h |   7 ++
 spl/Makefile               |   7 +-
 tools/mkexynosspl.c        | 167 +++++++++++++++++++++++++++++++++------------
 3 files changed, 137 insertions(+), 44 deletions(-)

diff --git a/include/configs/smdk5420.h b/include/configs/smdk5420.h
index e580049..447f8e5 100644
--- a/include/configs/smdk5420.h
+++ b/include/configs/smdk5420.h
@@ -19,6 +19,8 @@
 
 #define CONFIG_ARCH_DEVICE_TREE		exynos5420
 
+#define CONFIG_VAR_SIZE_SPL
+
 #define CONFIG_SYS_SDRAM_BASE		0x20000000
 #define CONFIG_SYS_TEXT_BASE		0x23E00000
 
@@ -31,7 +33,12 @@
 /* select serial console configuration */
 #define CONFIG_SERIAL3			/* use SERIAL 3 */
 
+#ifdef CONFIG_VAR_SIZE_SPL
+#define CONFIG_SPL_TEXT_BASE		0x02024410
+#else
 #define CONFIG_SPL_TEXT_BASE	0x02024400
+#endif
+
 #define CONFIG_BOOTCOMMAND	"mmc read 20007000 451 2000; bootm 20007000"
 
 #define CONFIG_SYS_PROMPT		"SMDK5420 # "
diff --git a/spl/Makefile b/spl/Makefile
index b366ac2..d5f6ae8 100644
--- a/spl/Makefile
+++ b/spl/Makefile
@@ -184,8 +184,13 @@ endif
 all:	$(ALL-y)
 
 ifdef CONFIG_SAMSUNG
+ifdef CONFIG_VAR_SIZE_SPL
+VAR_SIZE_PARAM = --vs
+else
+VAR_SIZE_PARAM =
+endif
 $(obj)$(BOARD)-spl.bin: $(obj)u-boot-spl.bin
-	$(OBJTREE)/tools/mk$(BOARD)spl \
+	$(OBJTREE)/tools/mk$(BOARD)spl $(VAR_SIZE_PARAM) \
 		$(obj)u-boot-spl.bin $(obj)$(BOARD)-spl.bin
 endif
 
diff --git a/tools/mkexynosspl.c b/tools/mkexynosspl.c
index ef685b7..32b786c 100644
--- a/tools/mkexynosspl.c
+++ b/tools/mkexynosspl.c
@@ -14,93 +14,174 @@
 #include <compiler.h>
 
 #define CHECKSUM_OFFSET		(14*1024-4)
-#define BUFSIZE			(14*1024)
 #define FILE_PERM		(S_IRUSR | S_IWUSR | S_IRGRP \
 				| S_IWGRP | S_IROTH | S_IWOTH)
 /*
-* Requirement:
-* IROM code reads first 14K bytes from boot device.
-* It then calculates the checksum of 14K-4 bytes and compare with data at
-* 14K-4 offset.
-*
-* This function takes two filenames:
-* IN  "u-boot-spl.bin" and
-* OUT "$(BOARD)-spl.bin as filenames.
-* It reads the "u-boot-spl.bin" in 16K buffer.
-* It calculates checksum of 14K-4 Bytes and stores at 14K-4 offset in buffer.
-* It writes the buffer to "$(BOARD)-spl.bin" file.
-*/
+ * Requirement for the fixed size SPL header:
+ * IROM code reads first (CHECKSUM_OFFSET + 4) bytes from boot device. It then
+ * calculates the checksum of CHECKSUM_OFFSET bytes and compares with data at
+ * CHECKSUM_OFFSET location.
+ *
+ * Requirement for the variable size SPL header:
+
+ * IROM code reads the below header to find out the size of the blob (total
+ * size, header size included) and its checksum. Then it reads the rest of the
+ * blob [i.e size - sizeof(struct var_size_header) bytes], calculates the
+ * checksum and compares it with value read from the header.
+ */
+struct var_size_header {
+	uint32_t spl_size;
+	uint32_t spl_checksum;
+	uint32_t reserved[2];
+};
+
+static const char *prog_name;
+
+static void write_to_file(int ofd, void *buffer, int size)
+{
+	if (write(ofd, buffer, size) == size)
+		return;
+
+	fprintf(stderr, "%s: Failed to write to output file: %s\n",
+		prog_name, strerror(errno));
+	exit(EXIT_FAILURE);
+}
 
+/*
+ * The argv is expected to include one optional parameter and two filenames:
+ * [--vs] IN OUT
+ *
+ * --vs - turns on the variable size SPL mode
+ * IN  - the u-boot SPL binary, usually u-boot-spl.bin
+ * OUT - the prepared SPL blob, usually ${BOARD}-spl.bin
+ *
+ * This utility first reads the "u-boot-spl.bin" into a buffer. In case of
+ * fixed size SPL the buffer size is exactly CHECKSUM_OFFSET (such that
+ * smaller u-boot-spl.bin gets padded with 0xff bytes, the larger than limit
+ * u-boot-spl.bin causes an error). For variable size SPL the buffer size is
+ * eqaul to size of the IN file.
+ *
+ * Then it calculates checksum of the buffer by just summing up all bytes.
+ * Then
+ *
+ * - for fixed size SPL the buffer is written into the output file and the
+ *   checksum is appended to the file in little endian format, which results
+ *   in checksum added exactly at CHECKSUM_OFFSET.
+ *
+ * - for variable size SPL the checksum and file size are stored in the
+ *   var_size_header structure (again, in little endian format) and the
+ *   structure is written into the output file. Then the buffer is written
+ *   into the output file.
+ */
 int main(int argc, char **argv)
 {
-	unsigned char buffer[BUFSIZE];
+	unsigned char *buffer;
 	int i, ifd, ofd;
 	uint32_t checksum = 0;
 	off_t	len;
-	ssize_t count;
+	int	var_size_flag, read_size, count;
 	struct stat stat;
-
-	if (argc != 3) {
-		fprintf(stderr, "Usage: %s <infile> <outfile>\n", argv[0]);
+	const int if_index = argc - 2; /* Input file name index in argv. */
+	const int of_index = argc - 1; /* Output file name index in argv. */
+
+	/* Strip path off the program name. */
+	prog_name = strrchr(argv[0], '/');
+	if (prog_name)
+		prog_name++;
+	else
+		prog_name = argv[0];
+
+	if ((argc < 3) ||
+	    (argc > 4) ||
+	    ((argc == 4) && strcmp(argv[1], "--vs"))) {
+		fprintf(stderr, "Usage: %s [--vs] <infile> <outfile>\n",
+			prog_name);
 		exit(EXIT_FAILURE);
 	}
 
-	ifd = open(argv[1], O_RDONLY);
+	/* four args mean variable size SPL wrapper is required */
+	var_size_flag = (argc == 4);
+
+	ifd = open(argv[if_index], O_RDONLY);
 	if (ifd < 0) {
 		fprintf(stderr, "%s: Can't open %s: %s\n",
-			argv[0], argv[1], strerror(errno));
+			prog_name, argv[if_index], strerror(errno));
 		exit(EXIT_FAILURE);
 	}
 
-	ofd = open(argv[2], O_WRONLY | O_CREAT | O_TRUNC, FILE_PERM);
+	ofd = open(argv[of_index], O_WRONLY | O_CREAT | O_TRUNC, FILE_PERM);
 	if (ifd < 0) {
 		fprintf(stderr, "%s: Can't open %s: %s\n",
-			argv[0], argv[2], strerror(errno));
-		close(ifd);
+			prog_name, argv[of_index], strerror(errno));
 		exit(EXIT_FAILURE);
 	}
 
 	if (fstat(ifd, &stat)) {
 		fprintf(stderr, "%s: Unable to get size of %s: %s\n",
-			argv[0], argv[1], strerror(errno));
-		close(ifd);
-		close(ofd);
+			prog_name, argv[if_index], strerror(errno));
 		exit(EXIT_FAILURE);
 	}
 
 	len = stat.st_size;
 
-	count = (len < CHECKSUM_OFFSET) ? len : CHECKSUM_OFFSET;
-
-	if (read(ifd, buffer, count) != count) {
-		fprintf(stderr, "%s: Can't read %s: %s\n",
-			argv[0], argv[1], strerror(errno));
+	if (var_size_flag) {
+		read_size = len;
+		count = len;
+	} else {
+		if (len > CHECKSUM_OFFSET) {
+			fprintf(stderr,
+				"%s: %s is too big (exceeds %d bytes)\n",
+				prog_name, argv[if_index], CHECKSUM_OFFSET);
+			exit(EXIT_FAILURE);
+		}
+		count = CHECKSUM_OFFSET;
+		read_size = len;
+	}
 
-		close(ifd);
-		close(ofd);
+	buffer = malloc(count);
+	if (!buffer) {
+		fprintf(stderr,
+			"%s: Failed to allocate %d bytes to store %s\n",
+			prog_name, count, argv[if_index]);
+		exit(EXIT_FAILURE);
+	}
 
+	if (read(ifd, buffer, read_size) != read_size) {
+		fprintf(stderr, "%s: Can't read %s: %s\n",
+			prog_name, argv[if_index], strerror(errno));
 		exit(EXIT_FAILURE);
 	}
 
-	for (i = 0, checksum = 0; i < CHECKSUM_OFFSET; i++)
-		checksum += buffer[i];
+	/* Pad if needed with 0xff to make flashing faster. */
+	if (read_size < count)
+		memset((char *)buffer + read_size, 0xff, count - read_size);
 
+	for (i = 0, checksum = 0; i < count; i++)
+		checksum += buffer[i];
 	checksum = cpu_to_le32(checksum);
 
-	memcpy(&buffer[CHECKSUM_OFFSET], &checksum, sizeof(checksum));
-
-	if (write(ofd, buffer, BUFSIZE) != BUFSIZE) {
-		fprintf(stderr, "%s: Can't write %s: %s\n",
-			argv[0], argv[2], strerror(errno));
+	if (var_size_flag) {
+		/* Prepare and write out the variable size SPL header. */
+		struct var_size_header vsh;
+		uint32_t spl_size;
 
-		close(ifd);
-		close(ofd);
+		memset(&vsh, 0, sizeof(vsh));
+		memcpy(&vsh.spl_checksum, &checksum, sizeof(checksum));
 
-		exit(EXIT_FAILURE);
+		spl_size = cpu_to_le32(count + sizeof(struct var_size_header));
+		memcpy(&vsh.spl_size, &spl_size, sizeof(spl_size));
+		write_to_file(ofd, &vsh, sizeof(vsh));
 	}
 
+	write_to_file(ofd, buffer, count);
+
+	/* For fixed size SPL checksum is appended in the end. */
+	if (!var_size_flag)
+		write_to_file(ofd, &checksum, sizeof(checksum));
+
 	close(ifd);
 	close(ofd);
+	free(buffer);
 
 	return EXIT_SUCCESS;
 }
-- 
1.7.12.4

  parent reply	other threads:[~2013-10-29  7:23 UTC|newest]

Thread overview: 37+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2013-10-29  7:23 [U-Boot] [PATCH 00/10 V6] EXYNOS5420: Add SMDK5420 board support Rajeshwari S Shinde
2013-10-29  7:23 ` [U-Boot] [PATCH 01/10 V6] EXYNOS5: Create a common board file Rajeshwari S Shinde
2013-11-08  8:13   ` Przemyslaw Marczak
2013-11-08  8:37     ` Rajeshwari Birje
2013-11-08  9:03       ` Przemyslaw Marczak
2013-11-08  9:27         ` Rajeshwari Birje
2013-11-08  9:46           ` Przemyslaw Marczak
2013-11-08  9:52             ` Rajeshwari Birje
2013-10-29  7:23 ` [U-Boot] [PATCH 02/10 V6] Exynos5420: Add base addresses for 5420 Rajeshwari S Shinde
2013-11-13  3:20   ` Minkyu Kang
2013-10-29  7:23 ` [U-Boot] [PATCH 03/10 V6] Exynos5420: Add clock initialization " Rajeshwari S Shinde
2013-11-13  2:45   ` Minkyu Kang
2013-11-13 11:17     ` Rajeshwari Birje
2013-11-14  1:56       ` Minkyu Kang
2013-10-29  7:23 ` [U-Boot] [PATCH 04/10 V6] Exynos5420: Add DDR3 " Rajeshwari S Shinde
2013-11-13  3:24   ` Minkyu Kang
2013-10-29  7:23 ` [U-Boot] [PATCH 05/10 V6] Exynos5420: Add support for 5420 in pinmux and gpio Rajeshwari S Shinde
2013-11-13  3:01   ` Minkyu Kang
2013-11-13  6:04     ` Rajeshwari Birje
2013-11-13  6:08       ` Rajeshwari Birje
2013-11-14  2:46       ` Minkyu Kang
2013-10-29  7:23 ` [U-Boot] [PATCH 06/10 V6] Exynos5420: Add base patch for SMDK5420 Rajeshwari S Shinde
2013-11-13  3:09   ` Minkyu Kang
2013-10-29  7:23 ` [U-Boot] [PATCH 07/10 V6] DTS: Add dts support " Rajeshwari S Shinde
2013-11-13  3:17   ` Minkyu Kang
2013-11-13  4:26     ` Rajeshwari Birje
2013-11-14  2:01       ` Minkyu Kang
2013-11-15  2:32         ` Rajeshwari Birje
2013-10-29  7:23 ` [U-Boot] [PATCH 08/10 V6] Config: Add initial config " Rajeshwari S Shinde
2013-11-13  3:31   ` Minkyu Kang
2013-10-29  7:23 ` Rajeshwari S Shinde [this message]
2013-10-29  7:23 ` [U-Boot] [PATCH 10/10 V6] DWMMC: SMDK5420: Disable SMU for eMMC Rajeshwari S Shinde
2013-10-29 10:24   ` Jaehoon Chung
2013-10-31  7:50   ` Pantelis Antoniou
2013-10-31  8:42 ` [U-Boot] [PATCH 00/10 V6] EXYNOS5420: Add SMDK5420 board support Rajeshwari Birje
2013-11-06 11:42   ` Rajeshwari Birje
2013-11-07  1:32     ` Minkyu Kang

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=1383031393-6093-10-git-send-email-rajeshwari.s@samsung.com \
    --to=rajeshwari.s@samsung.com \
    --cc=u-boot@lists.denx.de \
    /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 an external index of several public inboxes,
see mirroring instructions on how to clone and mirror
all data and code used by this external index.