broadcom/cygnus: add secimage and sign bootblock

secimage is a tool which adds a header and signature to the binary
first loaded by the soc. ARM core frequency is set to 1 Ghz.

BUG=chrome-os-partner:36421
BRANCH=broadcom-firmware
TEST=booted b0 board

Change-Id: Ia08600d45c47ee4f08d253980036916e44b0044a
Signed-off-by: Patrick Georgi <pgeorgi@chromium.org>
Original-Commit-Id: 36284d1b242c26b0b5aac2894f7ed1790da1ef15
Original-Signed-off-by: Daisuke Nojiri <dnojiri@chromium.org>
Original-Reviewed-on: https://chrome-internal-review.googlesource.com/197155
Original-Reviewed-by: Scott Branden <sbranden@broadcom.com>
Original-Reviewed-by: Julius Werner <jwerner@chromium.org>
Original-Commit-Queue: Daisuke Nojiri <dnojiri@google.com>
Original-Tested-by: Daisuke Nojiri <dnojiri@google.com>
Original-Change-Id: Iaddd24006b368c8f37e075cb51e151e985029f3b
Original-Reviewed-on: https://chromium-review.googlesource.com/264417
Reviewed-on: http://review.coreboot.org/9914
Tested-by: build bot (Jenkins)
Reviewed-by: Stefan Reinauer <stefan.reinauer@coreboot.org>
diff --git a/util/broadcom/secimage/io.c b/util/broadcom/secimage/io.c
new file mode 100644
index 0000000..4d99aad
--- /dev/null
+++ b/util/broadcom/secimage/io.c
@@ -0,0 +1,121 @@
+/*
+ * Copyright (C) 2015 Broadcom Corporation
+ *
+ * This program is free software; you can redistribute it and/or
+ * modify it under the terms of the GNU General Public License as
+ * published by the Free Software Foundation version 2.
+ *
+ * This program is distributed "as is" WITHOUT ANY WARRANTY of any
+ * kind, whether express or implied; without even the implied warranty
+ * of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+ * GNU General Public License for more details.
+ */
+
+
+#include <stdio.h>
+#include <string.h>
+#include "secimage.h"
+
+/*----------------------------------------------------------------------
+ * Name    : ReadBinaryFile
+ * Purpose : Read some data from file of raw binary
+ * Input   : fname : file to be read
+ *           buf : buffer which is the data desitnation
+ *           maxlen : maiximum length of data to be read
+ * Output  : none
+ *---------------------------------------------------------------------*/
+int ReadBinaryFile(char *fname, uint8_t *buf, int maxlen)
+{
+	FILE *fp = NULL;
+	int len = 0;
+
+	fp = fopen(fname, "rb");
+	if (fp == NULL)
+		return 0;
+	printf("fname=%s, len=%d\n", fname, maxlen);
+	len = fread(buf, 1, maxlen, fp);
+	fclose(fp);
+
+	return len;
+}
+
+
+/*----------------------------------------------------------------------
+ * Name    : FileSizeGet
+ * Purpose : Return the size of the file
+ * Input   : file: FILE * to the file to be processed
+ * Output  : none
+ *---------------------------------------------------------------------*/
+size_t FileSizeGet(FILE *file)
+{
+	long length;
+
+	fseek(file, 0, SEEK_END);
+	length = ftell(file);
+	rewind(file);
+	return (size_t)length;
+}
+
+
+/*----------------------------------------------------------------------
+ * Name    : DataRead
+ * Purpose : Read all the data from a file
+ * Input   : filename : file to be read
+ *           buf : buffer which is the data destination
+ *           length : length of data to be read
+ * Output  : none
+ *---------------------------------------------------------------------*/
+int DataRead(char *filename, uint8_t *buf, int *length)
+{
+	FILE *file;
+	int len = *length;
+
+	file = fopen(filename, "rb");
+	if (file == NULL) {
+		printf("Unable to open file: %s\n", filename);
+		return -1;
+	}
+	len = FileSizeGet(file);
+	if (len < *length)
+		*length = len;
+	else
+		/* Do not exceed the maximum length of the buffer */
+		len = *length;
+	if (fread((uint8_t *)buf, 1, len, file) != len) {
+		printf("Error reading data (%d bytes) from file: %s\n",
+		       len, filename);
+		return -1;
+	}
+	fclose(file);
+	return 0;
+}
+
+
+/*----------------------------------------------------------------------
+ * Name    : DataWrite
+ * Purpose : Write some binary data to a file
+ * Input   : filename : file to be written
+ *           buf : buffer which is the data source
+ *           length : length of data to be written
+ * Output  : none
+ *---------------------------------------------------------------------*/
+int DataWrite(char *filename, char *buf, int length)
+{
+	FILE *file;
+
+	file = fopen(filename, "wb");
+	if (file == NULL) {
+		printf("Unable to open output file %s\n", filename);
+		return -1;
+	}
+	if (fwrite(buf, 1, length, file) < length) {
+		printf("Unable to write %d bytes to output file %s (0x%X).\n",
+		       length, filename, ferror(file));
+		fclose(file);
+		return -1;
+	}
+
+	fflush(file);
+	fclose(file);
+	return 0;
+}