Skip to contentSkip to conversation

socfpga: Add a signing tool that automatically signs the preloader.

PATCH New Charles Manning 4 participants 6 messages Feb 19, 2014 – Feb 22, 2014

lore
Share & tools
lore
#0 lore raw

PATCH 1/1 socfpga: Add a signing tool that automatically signs the preloader.

No need to use the altera tool any more...

---
 spl/Makefile           |    8 ++
 tools/Makefile         |    9 +-
 tools/socfpga-signer.c |  294 ++++++++++++++++++++++++++++++++++++++++++++++++
 3 files changed, 310 insertions(+), 1 deletion(-)
 create mode 100644 tools/socfpga-signer.c
spl/Makefile +8 -0
index c0abe3d..086b1e6 100644
@@ -144,8 +144,12 @@ $(OBJTREE)/MLO:	$(obj)u-boot-spl.bin
 
 $(OBJTREE)/MLO.byteswap: $(obj)u-boot-spl.bin
 	$(OBJTREE)/tools/mkimage -T omapimage -n byteswap \
+
 		-a $(CONFIG_SPL_TEXT_BASE) -d $< $@
 
+$(OBJTREE)/socfpga-signed-preloader.bin: $(obj)u-boot-spl.bin
+	$(OBJTREE)/tools/socfpga-signer -i $< -o $@ -p
+
 ifneq ($(CONFIG_IMX_CONFIG),)
 $(OBJTREE)/SPL:	$(obj)u-boot-spl.bin
 	$(OBJTREE)/tools/mkimage -n  $(SRCTREE)/$(CONFIG_IMX_CONFIG) -T imximage \
@@ -154,6 +158,10 @@ endif
 
 ALL-y	+= $(obj)u-boot-spl.bin
 
+ifdef CONFIG_SOCFPGA
+ALL-y += $(OBJTREE)/socfpga-signed-preloader.bin
+endif
+
 ifdef CONFIG_SAMSUNG
 ALL-y	+= $(obj)$(BOARD)-spl.bin
 endif
tools/Makefile +8 -1
index 686840a..1ed2281 100644
@@ -73,7 +73,8 @@ BIN_FILES-$(CONFIG_MX28) += mxsboot$(SFX)
 BIN_FILES-$(CONFIG_NETCONSOLE) += ncb$(SFX)
 BIN_FILES-$(CONFIG_SHA1_CHECK_UB_IMG) += ubsha1$(SFX)
 BIN_FILES-$(CONFIG_KIRKWOOD) += kwboot$(SFX)
-
+BIN_FILES-$(CONFIG_SOCFPGA) += socfpga-signer$(SFX)
+ 
 # Source files which exist outside the tools directory
 EXT_OBJ_FILES-$(CONFIG_BUILD_ENVCRC) += common/env_embedded.o
 EXT_OBJ_FILES-y += common/image.o
@@ -104,6 +105,7 @@ NOPED_OBJ_FILES-y += os_support.o
 OBJ_FILES-$(CONFIG_SHA1_CHECK_UB_IMG) += ubsha1.o
 NOPED_OBJ_FILES-y += ublimage.o
 OBJ_FILES-$(CONFIG_KIRKWOOD) += kwboot.o
+OBJ_FILES-$(CONFIG_SOCFPGA) += socfpga-signer.o
 
 # Don't build by default
 #ifeq ($(ARCH),ppc)
@@ -243,6 +245,11 @@ $(obj)kwboot$(SFX): $(obj)kwboot.o
 	$(HOSTCC) $(HOSTCFLAGS) $(HOSTLDFLAGS) -o $@ $^
 	$(HOSTSTRIP) $@
 
+
+$(obj)socfpga-signer$(SFX): $(obj)socfpga-signer.o
+	$(HOSTCC) $(HOSTCFLAGS) $(HOSTLDFLAGS) -o $@ $^
+	$(HOSTSTRIP) $@
+
 # Some of the tool objects need to be accessed from outside the tools directory
 $(obj)%.o: $(SRCTREE)/common/%.c
 	$(HOSTCC) -g $(HOSTCFLAGS_NOPED) -c -o $@ $<
tools/socfpga-signer.c new file +294 -0

294 lines not on this page · raw message

Signature · 1 line
1.7.9.5
#1 lore raw

PATCH 1/1 socfpga: Add a signing tool that automatically signs the preloader.

Quoted 3 lines · Charles Manning

No need to use the altera tool any more...

[snip]

tools/socfpga-signer.c line 184

Quoted 2 lines
tools/socfpga-signer.c new file +2 -0
+	//crcval = crc_calc ((uint32_t *)buf, len/4);
+	printf("crc %x\n", crcval);

Debug stuff left in?

tools/socfpga-signer.c line 205

Quoted 5 lines
tools/socfpga-signer.c new file +5 -0
+static char *out_filename = "";
+
+
+
+static void bad_exit(const char *str)

Extra spaces.

Finally, we can't do this as an extension of mkimage? We do other headers with that already, does it really not fit that model? Thanks!

Signature · 8 lines
Tom
-------------- next part --------------
A non-text attachment was scrubbed...
Name: not available
Type: application/pgp-signature
Size: 836 bytes
Desc: Digital signature
URL: <http://lists.denx.de/pipermail/u-boot/attachments/20140219/2611a0cf/attachment.pgp>
#2 lore raw

PATCH 1/1 socfpga: Add a signing tool that automatically signs the preloader.

Hi Tom

Quoted 12 lines · On Thursday 20 February 2014 13:32:37 Tom Rini wrote:
Quoted 3 lines · Charles Manning

No need to use the altera tool any more...

[snip]

tools/socfpga-signer.c line 184

Quoted 2 lines
tools/socfpga-signer.c new file +2 -0
+	//crcval = crc_calc ((uint32_t *)buf, len/4);
+	printf("crc %x\n", crcval);

Debug stuff left in?

Quoted 7 lines

tools/socfpga-signer.c line 205

Quoted 5 lines
tools/socfpga-signer.c new file +5 -0
+static char *out_filename = "";
+
+
+
+static void bad_exit(const char *str)

Extra spaces.

Thanks for the feedback.

I will fix those and resubmit.

Quoted 3 lines

Finally, we can't do this as an extension of mkimage? We do other headers with that already, does it really not fit that model? Thanks!

I looked at doing a mkimage integration, but is this really the way to go in all cases?

The signed image does not really apply a header as such. It mashes in a "header" at position 0x40 and then appends a checksum at the end, then pads the file out to 64k.

The final file is:
* 64 bytes of executable (interrupt vector table)
* "header"
* more code
*crc32
*padding with 0x00 to 64k.

Adding it to mkimage and "cooking" the binary was not too hard. I could not figure out how to extend the size of the buffer and pad it out to 64k. I'm sure I could figure that out... it's just software..

If it really **must** be part of mkimage, I'll do that.

If so, what mkimage operation need to be supported?

Changing type makes no sense (apart from signing a binary). There are no uboot-format header/marker or anything. Verification probably makes sense, but that's it.

Thoughts??

-- Charles

#3 lore raw

PATCH 1/1 socfpga: Add a signing tool that automatically signs the preloader.

Dear Charles,

Quoted 3 lines · In message <[email protected]> you wrote:

I looked at doing a mkimage integration, but is this really the way to go in all cases?

...only where it fits, of course.

Quoted 10 lines

The signed image does not really apply a header as such. It mashes in a "header" at position 0x40 and then appends a checksum at the end, then pads the file out to 64k.

The final file is:
* 64 bytes of executable (interrupt vector table)
* "header"
* more code
*crc32
*padding with 0x00 to 64k.

Sounds exactly as if you were making an image, so yes, mkimage fits.

Best regards,

Wolfgang Denk

--
DENX Software Engineering GmbH, MD: Wolfgang Denk & Detlev Zundel
HRB 165235 Munich, Office: Kirchenstr.5, D-82194 Groebenzell, Germany
Phone: (+49)-8142-66989-10 Fax: (+49)-8142-66989-80 Email: wd at denx.de Humanity has the stars in its future, and that future is too important to be lost under the burden of juvenile folly and ignorant
superstition. - Isaac Asimov

#4 lore raw

PATCH 1/1 socfpga: Add a signing tool that automatically signs the preloader.

Quoted 13 lines · Wolfgang Denk

Dear Charles,

Quoted 3 lines · In message <[email protected]> you wrote:

I looked at doing a mkimage integration, but is this really the way to go in all cases?

...only where it fits, of course.

Quoted 4 lines

The signed image does not really apply a header as such. It mashes in a "header" at position 0x40 and then appends a checksum at the end, then pads the file out to 64k.

Quoted 8 lines
Quoted 6 lines

The final file is:
* 64 bytes of executable (interrupt vector table)
* "header"
* more code
*crc32
*padding with 0x00 to 64k.

Sounds exactly as if you were making an image, so yes, mkimage fits.

Right. This is like the omapimage case or similar where we take input, beat it around and add stuff, then have what the HW wants, but not a file we can further query with mkimage, and that's fine.

Signature · 8 lines
Tom
-------------- next part --------------
A non-text attachment was scrubbed...
Name: not available
Type: application/pgp-signature
Size: 836 bytes
Desc: Digital signature
URL: <http://lists.denx.de/pipermail/u-boot/attachments/20140220/20449294/attachment.pgp>
#5 lore raw

PATCH 1/1 socfpga: Add a signing tool that automatically signs the preloader.

In reply to Charles Manning
Quoted 2 lines · Charles Manning

No need to use the altera tool any more...

I like the idea of removing this external dependency. But there are some issues with your submission.

The commit message could use some more helpful text for those who are not familiar with the details of how the preloader gets mangled before it becomes acceptable to the ROM loader.

The patch does not apply to current U-Boot source code, can you re-check please? Although this might have been because master has moved between your sending the patch and my trying to apply it.

Then there are whitespace issues, make sure to check the coding style before submission. Other comments are further down below.

spl/Makefile $(OBJTREE)/MLO: $(obj)u-boot-spl.bin · line 148

Quoted 8 lines
spl/Makefile +1 -0
@@ -144,8 +144,12 @@ $(OBJTREE)/MLO:	$(obj)u-boot-spl.bin
 
 $(OBJTREE)/MLO.byteswap: $(obj)u-boot-spl.bin
 	$(OBJTREE)/tools/mkimage -T omapimage -n byteswap \
+
 		-a $(CONFIG_SPL_TEXT_BASE) -d $< $@

this looks suspicious: there has been a continued line, which you now continue with nothing, and have the remainder stand alone? Does it even work?

tools/socfpga-signer.c line 9

Quoted 12 lines
tools/socfpga-signer.c new file +9 -0
@@ -0,0 +1,294 @@
+
+[ ... ]
+
+#include <stdio.h>
+#include <fcntl.h>
+#include <stdint.h>
+#include <unistd.h>
+#include <stdlib.h>
+#include <string.h>

alpha-sort those, makes checking for duplicates easier and reduces conflicts during maintenance

tools/socfpga-signer.c line 37

Quoted 7 lines
tools/socfpga-signer.c new file +7 -0
+
+#define HEADER_OFFSET	0x40
+#define HEADER_SIZE	0x0C
+#define BUFFER_SIZE	(0x10000)
+#define MAX_IMAGE_SIZE 0xFF00
+#define VALIDATION_WORD	0x31305341
+#define FILL_BYTE	0x00

there's no point in the parentheses around a single value

isn't the maximum preloader size said to be 60KiB? aka 64KiB minus 4KiB for internal use by the ROM loader? that would be 0x1000 less than 0x10000, not just 0x100

tools/socfpga-signer.c line 50

Quoted 11 lines
tools/socfpga-signer.c new file +11 -0
+static uint16_t hdr_checksum(uint8_t *buf, int len)
+{
+	uint16_t ret = 0;
+	int i;
+
+	for(i = 0; i < len; i++) {
+		ret += (((uint16_t) *buf) & 0xff);
+		buf++;
+	}
+	return ret;
+}

the cast and masking should be unnecessary, you are dereferencing a pointer to a byte after all

tools/socfpga-signer.c line 65

Quoted 13 lines
tools/socfpga-signer.c new file +13 -0
+static void le16(uint8_t *buf, uint16_t v)
+{
+	buf[0] = (v >> 0) & 0xff;
+	buf[1] = (v >> 8) & 0xff;
+}
+
+static void le32(uint8_t *buf, uint32_t v)
+{
+	buf[0] = (v >>  0) & 0xff;
+	buf[1] = (v >>  8) & 0xff;
+	buf[2] = (v >> 16) & 0xff;
+	buf[3] = (v >> 24) & 0xff;
+}

can you come up with more descriptive names? it took a while before I noticed (at call sites) that "le16()" not only did conversion, but manipulated a buffer as well

does "put_le16()" sound better to you?

tools/socfpga-signer.c line 70

Quoted 4 lines
tools/socfpga-signer.c new file +4 -0
+static int align4(int v)
+{
+	return ((v + 3) / 4) * 4;
+}

isn't there some ROUNDUP() macro already? (it is in U-Boot, but I'm not sure for user space)

Quoted 28 lines
tools/socfpga-signer.c new file +28 -0
+static void build_header(uint8_t *buf,
+			  uint8_t version,
+			  uint8_t flags,
+			  uint16_t length_bytes)
+{
+	memset(buf, 0, HEADER_SIZE);
+
+	le32(buf + 0, VALIDATION_WORD);
+	buf[4] = version;
+	buf[5] = flags;
+	le16(buf + 6, length_bytes/4);
+	le16(buf + 10, hdr_checksum(buf, 10));
+}
+
+static uint32_t crc_table[256] = {
+
+	0x00000000, 0x04c11db7, 0x09823b6e, 0x0d4326d9,
+	0x130476dc, 0x17c56b6b, 0x1a864db2, 0x1e475005,
+	[ ... ]
+	0xafb010b1, 0xab710d06, 0xa6322bdf, 0xa2f33668,
+	0xbcb4666d, 0xb8757bda, 0xb5365d03, 0xb1f740b4
+};
+
+uint32_t crc32(uint32_t crc, void *_buf, int length)
+{
+	uint8_t *buf = _buf;
+
+	crc ^= ~0;

tools/socfpga-signer.c line 168

Quoted 10 lines
tools/socfpga-signer.c new file +10 -0
+
+	while (length--) {
+		crc = (crc << 8) ^ crc_table[((crc >> 24) ^ *buf) & 0xff];
+		buf++;
+	}
+
+	crc ^= ~0;
+
+	return crc;
+}

are you re-inventing CRC32 here? is there nothing that you can re-use?

Quoted 5 lines
tools/socfpga-signer.c new file +5 -0
+static int massage_buffer(uint8_t *buf,
+			uint8_t version, uint8_t flags,
+			int len, int pad_64k)
+{
+	uint32_t crcval;

tools/socfpga-signer.c line 184

Quoted 10 lines
tools/socfpga-signer.c new file +10 -0
+
+	/* Align the length up */
+	len = align4(len);
+
+	/* Build header, adding 4 bytes to length to hold the CRC32. */
+	build_header(buf + HEADER_OFFSET,  version, flags, len + 4);
+
+	crcval = crc32(0, buf, len);
+	//crcval = crc_calc ((uint32_t *)buf, len/4);
+	printf("crc %x\n", crcval);

dead code? remainders from during development? should go

tools/socfpga-signer.c line 192

Quoted 8 lines
tools/socfpga-signer.c new file +8 -0
+
+	le32(buf + len, crcval);
+
+	if(!pad_64k)
+		return len + 4;
+
+	return (1<<16);
+}

is this not BUFFER_SIZE? phrasing the same condition differently obfuscates what happens, and makes you miss stuff during maintenance

virtually yours
Gerhard Sittig

Signature · 3 lines
DENX Software Engineering GmbH,     MD: Wolfgang Denk & Detlev Zundel
HRB 165235 Munich, Office: Kirchenstr. 5, D-82194 Groebenzell, Germany
Phone: +49-8142-66989-0 Fax: +49-8142-66989-80  Email: office at denx.de