Skip to contentSkip to conversation

Support unified u-boot SPL for SD cards and FEL mode

PATCH Superseded Siarhei Siamashka 3 participants 7 messages Feb 9, 2015 – Feb 11, 2015

lore
Share & tools
lore
#0 lore raw

v2 cover Support unified u-boot SPL for SD cards and FEL mode

An updated variant of the older patch

    http://lists.denx.de/pipermail/u-boot/2015-February/204024.html

These patches are not for the u-boot code (unless u-boot decides to cannibalize the 'fel' tool later) but still provide a glue layer between the Allwinner BROM code and the u-boot SPL code. And need to play nice with both of them.

The updated 'fel' tool is supposed to be used together with u-boot v2015.04 and provide support for booting regular u-boot SPL binaries over USB via FEL mode. So that a special u-boot sunxi FEL configuration can be eventually dropped in the future.

The usage instructions are in the 'New method of booting u-boot over USB (u-boot v2015.04 and later versions)' section of the linux-sunxi wiki page

    http://linux-sunxi.org/index.php?title=FEL/USBBoot&oldid=12979

It temporarily moves the BROM data from the first 32 KiB of SRAM into a different SRAM section before calling the SPL code. And then moves it back before returning from the SPL back to the FEL code in BROM. This provides a contiguous 32 KiB block in the beginning of SRAM for use by the SPL. And in the case if an extra SRAM section is not available, we can at least "defragmemnt" the free memory areas and still provide a contiguous ~21 KiB block to the SPL in the beginning of SRAM.

This code is also available in the following branch:

    https://github.com/ssvb/sunxi-tools/commits/20150206-fel-large-spl-support

Siarhei Siamashka (2):
fel: Split 'aw_fel_get_version' into 'get' and 'print' variants
fel: New command for loading U-Boot SPL binaries in eGON format

 fel-to-spl-thunk.S | 172 +++++++++++++++++++++++++++++++
 fel-to-spl-thunk.h |  69 +++++++++++++
 fel.c              | 291 +++++++++++++++++++++++++++++++++++++++++++++++++----
 3 files changed, 510 insertions(+), 22 deletions(-)
 create mode 100644 fel-to-spl-thunk.S
 create mode 100644 fel-to-spl-thunk.h
Signature · 1 line
2.0.5
#1 lore raw

PATCH v2 1/2 fel: Split 'aw_fel_get_version' into 'get' and 'print' variants

Now aw_fel_get_version() can get the SoC ID for internal usage from the other functions. And aw_fel_print_version() is used to print the formatted string to stdout.

---
Changes in v2:
- This code has been isolated into a separate patch. No other changes.

 fel.c | 53 +++++++++++++++++++++++++++++++----------------------
 1 file changed, 31 insertions(+), 22 deletions(-)
fel.c +31 -22
index c21d6ec..542bf4b 100644
@@ -41,6 +41,17 @@ struct  aw_usb_request {
 	char	pad[10];
 }  __attribute__((packed));
 
+struct aw_fel_version {
+	char signature[8];
+	uint32_t soc_id;	/* 0x00162300 */
+	uint32_t unknown_0a;	/* 1 */
+	uint16_t protocol;	/* 1 */
+	uint8_t  unknown_12;	/* 0x44 */
+	uint8_t  unknown_13;	/* 0x08 */
+	uint32_t scratchpad;	/* 0x7e00 */
+	uint32_t pad[2];	/* unused */
+} __attribute__((packed));
+
 static const int AW_USB_READ = 0x11;
 static const int AW_USB_WRITE = 0x12;
 
@@ -136,32 +147,27 @@ void aw_read_fel_status(libusb_device_handle *usb)
 	aw_usb_read(usb, &buf, sizeof(buf));
 }
 
-void aw_fel_get_version(libusb_device_handle *usb)
+void aw_fel_get_version(libusb_device_handle *usb, struct aw_fel_version *buf)
 {
-	struct aw_fel_version {
-		char signature[8];
-		uint32_t soc_id;	/* 0x00162300 */
-		uint32_t unknown_0a;	/* 1 */
-		uint16_t protocol;	/* 1 */
-		uint8_t  unknown_12;	/* 0x44 */
-		uint8_t  unknown_13;	/* 0x08 */
-		uint32_t scratchpad;	/* 0x7e00 */
-		uint32_t pad[2];	/* unused */
-	} __attribute__((packed)) buf;
-
 	aw_send_fel_request(usb, AW_FEL_VERSION, 0, 0);
-	aw_usb_read(usb, &buf, sizeof(buf));
+	aw_usb_read(usb, buf, sizeof(*buf));
 	aw_read_fel_status(usb);
 
-	buf.soc_id = le32toh(buf.soc_id);
-	buf.unknown_0a = le32toh(buf.unknown_0a);
-	buf.protocol = le32toh(buf.protocol);
-	buf.scratchpad = le16toh(buf.scratchpad);
-	buf.pad[0] = le32toh(buf.pad[0]);
-	buf.pad[1] = le32toh(buf.pad[1]);
+	buf->soc_id = (le32toh(buf->soc_id) >> 8) & 0xFFFF;
+	buf->unknown_0a = le32toh(buf->unknown_0a);
+	buf->protocol = le32toh(buf->protocol);
+	buf->scratchpad = le16toh(buf->scratchpad);
+	buf->pad[0] = le32toh(buf->pad[0]);
+	buf->pad[1] = le32toh(buf->pad[1]);
+}
+
+void aw_fel_print_version(libusb_device_handle *usb)
+{
+	struct aw_fel_version buf;
+	aw_fel_get_version(usb, &buf);
 
 	const char *soc_name="unknown";
-	switch ((buf.soc_id >> 8) & 0xFFFF) {
+	switch (buf.soc_id) {
 	case 0x1623: soc_name="A10";break;
 	case 0x1625: soc_name="A13";break;
 	case 0x1633: soc_name="A31";break;
@@ -170,7 +176,10 @@ void aw_fel_get_version(libusb_device_handle *usb)
 	case 0x1639: soc_name="A80";break;
 	}
 
-	printf("%.8s soc=%08x(%s) %08x ver=%04x %02x %02x scratchpad=%08x %08x %08x\n", buf.signature, buf.soc_id, soc_name, buf.unknown_0a, buf.protocol, buf.unknown_12, buf.unknown_13, buf.scratchpad, buf.pad[0], buf.pad[1]);
+	printf("%.8s soc=%08x(%s) %08x ver=%04x %02x %02x scratchpad=%08x %08x %08x\n",
+		buf.signature, buf.soc_id, soc_name, buf.unknown_0a,
+		buf.protocol, buf.unknown_12, buf.unknown_13,
+		buf.scratchpad, buf.pad[0], buf.pad[1]);
 }
 
 void aw_fel_read(libusb_device_handle *usb, uint32_t offset, void *buf, size_t len)
@@ -387,7 +396,7 @@ int main(int argc, char **argv)
 			aw_fel_execute(handle, strtoul(argv[2], NULL, 0));
 			skip=3;
 		} else if (strncmp(argv[1], "ver", 3) == 0 && argc > 1) {
-			aw_fel_get_version(handle);
+			aw_fel_print_version(handle);
 			skip=1;
 		} else if (strcmp(argv[1], "write") == 0 && argc > 3) {
 			size_t size;
Signature · 1 line
2.0.5
#2 lore raw

PATCH v2 2/2 fel: New command for loading U-Boot SPL binaries in eGON format

In reply to Siarhei Siamashka

Now it is possible to load and execute the same U-Boot SPL, as used for booting from SD cards. Just a different delivery method (a USB OTG cable instead of an SD card) for handling exactly the same content.

The only argument for this new command is the name of the SPL binary file (with a eGON header generated by the 'mksunxiboot' tool). Now the 'fel' tool can be run as:

fel spl u-boot-sunxi-with-spl.bin

Before this change, the SPL was only able to use the memory between addresses 0x2000 and ~0x5D00, totalling to something like ~15 KiB. This is the biggest contiguous area in SRAM, which is not used by the FEL code from the BROM. Unfortunately, it is rather small. And also the unusual starting offset was making it difficult to use the same SPL binary for booting from the SD card and via FEL.

There are surely more unused parts of SRAM, but they are scattered across multiple locations, primarily because the FEL code from the BROM sets up two stacks at inconvenient locations (the IRQ handler stack at 0x2000, and a regular stack at 0x7000). Essentially, the problem to solve here is to ensure a sufficiently large and consistent SRAM address space for the SPL without any potentially SoC specific holes in the case of booting over USB via FEL.

This is achieved by injecting special entry/exit thunk code, which is moving the data in SRAM to provide a contiguous space for the SPL at the beginning of SRAM, while still preserving the the data from the BROM elsewhere. When the SPL tries to return control back to the FEL code in the BROM, the thunk code moves the data back to its original place. Additionally, the eGON checksum is verified to ensure that no data corruption has happened due to some unexpected clash with the FEL protocol code from the BROM.

So the thunk code takes care of the address space allocation uglyness and provides the U-Boot SPL with a somewhat nicer abstraction. Now the FEL booted SPL on A10/A13/A20/A31 can use up to 32 KiB of SRAM because the BROM data is saved to different SRAM section. There is also generic code, which does not rely on extra SRAM sections, but just glues together the unused free space from both BROM FEL stacks to provide something like ~21 KiB to the SPL.

---
Changes in v2:
- Return to the BROM FEL code instead of trying to re-start it by jumping to the 0xffff0020 address. The USB connection re-initialization is too unreliable in practice.
- Detect if the cache is enabled and refuse to work in this case
- Overwrite the "eGON.BT0" signature in memory with "eGON.FEL" if everything is fine. And overwrite it with "eGON.BAD" if a checksum error had been detected.

 fel-to-spl-thunk.S | 172 ++++++++++++++++++++++++++++++++++++++
 fel-to-spl-thunk.h |  69 ++++++++++++++++
 fel.c              | 238 +++++++++++++++++++++++++++++++++++++++++++++++++++++
 3 files changed, 479 insertions(+)
 create mode 100644 fel-to-spl-thunk.S
 create mode 100644 fel-to-spl-thunk.h
fel-to-spl-thunk.S new file +172 -0
new file mode 100644
index 0000000..93350b3
@@ -0,0 +1,172 @@
+/*
+ * Copyright ? 2015 Siarhei Siamashka <[email protected]>
+ *
+ * Permission is hereby granted, free of charge, to any person obtaining a
+ * copy of this software and associated documentation files (the "Software"),
+ * to deal in the Software without restriction, including without limitation
+ * the rights to use, copy, modify, merge, publish, distribute, sublicense,
+ * and/or sell copies of the Software, and to permit persons to whom the
+ * Software is furnished to do so, subject to the following conditions:
+ *
+ * The above copyright notice and this permission notice (including the next
+ * paragraph) shall be included in all copies or substantial portions of the
+ * Software.
+ *
+ * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
+ * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
+ * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.  IN NO EVENT SHALL
+ * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
+ * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
+ * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
+ * DEALINGS IN THE SOFTWARE.
+ */
+
+/*************************************************************************/
+/* Usage instructions: "ruby -x fel-to-spl-thunk.S > fel-to-spl-thunk.h" */
+/*************************************************************************/
+
+#if 0 
+#!/usr/bin/env ruby
+
+def tool_exists(tool_name)
+    `which #{tool_name} > /dev/null 2>&1`
+    return $?.to_i == 0
+end
+
+toolchains = [
+  "arm-none-eabi-",
+  "arm-linux-gnueabihf-",
+  "arm-none-linux-gnueabi-",
+  "armv7a-hardfloat-linux-gnueabi-",
+]
+
+toolchain = toolchains.find { |toolchain| tool_exists("#{toolchain}gcc") }
+abort "Can't find any ARM crosscompiler\n" unless toolchain
+
+system("#{toolchain}gcc -o #{$PROGRAM_NAME}.o -c #{$PROGRAM_NAME}")
+exit($?.to_i) if $?.to_i != 0
+
+`#{toolchain}objdump -d #{$PROGRAM_NAME}.o`.each_line {|l|
+    next unless l =~ /(\h+)\:\s+(\h+)\s+(\S+)\s+([^;]*)/
+    printf("\t0x%s, /* %8s:    %-10s %-28s */\n", $2, $1, $3, $4.strip)
+}
+
+__END__
+#endif
+
+/*************************************************************************/
+
+BUF1		.req	r0
+BUF2		.req	r1
+TMP1		.req	r2
+TMP2		.req	r3
+SWAPTBL		.req	r4
+FULLSIZE	.req	r5
+BUFSIZE		.req	r6
+CHECKSUM	.req	r7
+
+entry_point:
+	b	setup_stack
+
+stack_begin:
+	nop
+	nop
+	nop
+	nop
+	nop
+	nop
+	nop
+	nop
+stack_end:
+	nop
+
+	/* A function, which walks the table and swaps all buffers */
+swap_all_buffers:
+	adr	SWAPTBL,   swaptbl_start
+swap_next_buffer:
+	ldr	BUF1,      [SWAPTBL],  #4
+	ldr	BUF2,      [SWAPTBL],  #4
+	ldr	BUFSIZE,   [SWAPTBL],  #4
+	cmp	BUFSIZE,   #0
+	bxeq    lr
+swap_next_word:
+	ldr	TMP1,      [BUF1]
+	ldr	TMP2,      [BUF2]
+	subs	BUFSIZE,   BUFSIZE,    #4
+	str	TMP1,      [BUF2],     #4
+	str	TMP2,      [BUF1],     #4
+	bne	swap_next_word
+	b	swap_next_buffer
+
+setup_stack: /* Save the original SP, LR and CPSR to stack */
+	adr	BUF1,      stack_end
+	str	sp,        [BUF1, #-4]!
+	mov	sp,        BUF1
+	mrs	TMP1,      cpsr
+	push	{TMP1, lr}
+
+	/* Disable IRQ and FIQ */
+	orr	TMP1,      #0xc0
+	msr	cpsr_c,    TMP1
+
+	/* Check if the instructions or data cache is enabled */
+	mrc	p15, 0, TMP1, c1, c0, 0
+	movw	TMP2,      #((1 << 12) | (1 << 2))
+	tst	TMP1,      TMP2
+	bne	cache_is_unsupported
+
+	bl	swap_all_buffers
+
+verify_checksum:
+	movw	CHECKSUM,  #0x6c39
+	movt	CHECKSUM,  #0x5f0a
+	mov	BUF1,      #0
+	ldr     FULLSIZE,  [BUF1, #16]
+check_next_word:
+	ldr	TMP1,      [BUF1],   #4
+	subs	FULLSIZE,  FULLSIZE, #4
+	add	CHECKSUM,  CHECKSUM, TMP1
+	bne	check_next_word
+
+	mov	BUF1,      #0
+	ldr	TMP1,      [BUF1, #12]
+	subs	CHECKSUM,  CHECKSUM, TMP1, lsl #1
+	bne	checksum_is_bad
+
+	/* Change 'eGON.BT0' -> 'eGON.FEL' */
+	mov	BUF1,      #0
+	movw	TMP1,      (('F' << 8) + '.')
+	movt	TMP1,      (('L' << 8) + 'E')
+	str	TMP1,      [BUF1, #8]
+
+	/* Call the SPL code */
+	dsb
+	isb
+	blx	BUF1
+
+	/* Return back to FEL */
+	b	return_to_fel
+
+cache_is_unsupported:
+	/* Bail out if cache is enabled and change 'eGON.BT0' -> 'eGON.???' */
+	mov	BUF1,      #0
+	movw	TMP1,      (('?' << 8) + '.')
+	movt	TMP1,      (('?' << 8) + '?')
+	str	TMP1,      [BUF1, #8]
+	b	return_to_fel
+
+checksum_is_bad:
+	/* The checksum test failed, so change 'eGON.BT0' -> 'eGON.BAD' */
+	mov	BUF1,      #0
+	movw	TMP1,      (('B' << 8) + '.')
+	movt	TMP1,      (('D' << 8) + 'A')
+	str	TMP1,      [BUF1, #8]
+
+return_to_fel:
+	bl	swap_all_buffers
+	pop	{TMP1, lr}
+	msr	cpsr_c,    TMP1 /* Restore the original CPSR */
+	ldr     sp,        [sp]
+	bx	lr
+
+swaptbl_start:
fel-to-spl-thunk.h new file +69 -0

69 lines not on this page · raw message

fel.c +238 -0

262 lines not on this page · raw message

Signature · 1 line
2.0.5
#3 lore raw

v2 cover Support unified u-boot SPL for SD cards and FEL mode

In reply to Siarhei Siamashka

Hi Siarhei,

Quoted 7 lines · Siarhei Siamashka

An updated variant of the older patch

    http://lists.denx.de/pipermail/u-boot/2015-February/204024.html

These patches are not for the u-boot code (unless u-boot decides to cannibalize the 'fel' tool later) but still provide a glue layer between the Allwinner BROM code and the u-boot SPL code. And need to play nice with both of them.

It's an interesting idea actually - I wonder what people think about putting these tools in the source tree. I suspect it could get a bit out of hand? But it would make it easier to keep things in sync,

[snip]

Regards,
Simon

#4 lore raw

v2 cover Support unified u-boot SPL for SD cards and FEL mode

Quoted 7 lines · On Mon, 9 Feb 2015 15:14:51 -0700 Simon Glass <[email protected]> wrote:

Hi Siarhei,

Quoted 3 lines · Siarhei Siamashka

An updated variant of the older patch

    http://lists.denx.de/pipermail/u-boot/2015-February/204024.html
Quoted 8 lines
Quoted 4 lines

These patches are not for the u-boot code (unless u-boot decides to cannibalize the 'fel' tool later) but still provide a glue layer between the Allwinner BROM code and the u-boot SPL code. And need to play nice with both of them.

It's an interesting idea actually - I wonder what people think about putting these tools in the source tree. I suspect it could get a bit out of hand? But it would make it easier to keep things in sync,

Yes, keeping it is sync with the mksunxiboot program would be easier in this case.

But I searched a bit, and can see that sunxi-tools is already packaged in some linux distributions, for example:

https://packages.debian.org/search?keywords=sunxi-tools

Maybe it's better to keep the 'fel' tool in the 'sunxi-tools' package and just make sure that it gets reasonably well supported in linux distributions.

In addition, there might be other bootloaders or "bare metal" users. I have found this very cool project, which relies on the FEL mode and allows to run code on the OpenRISC core in Allwinner A31:

https://github.com/skristiansson/ar100-info

What's particularly interesting is that it apparently had the DRAM controller support for A31 (sun6i) even before this was implemented in u-boot :)

Signature · 2 lines
Best regards,
Siarhei Siamashka
#5 lore raw

v2 cover Support unified u-boot SPL for SD cards and FEL mode

In reply to Siarhei Siamashka

Hi,

Quoted 33 lines · Siarhei Siamashka

An updated variant of the older patch

     http://lists.denx.de/pipermail/u-boot/2015-February/204024.html

These patches are not for the u-boot code (unless u-boot decides to cannibalize the 'fel' tool later) but still provide a glue layerHi, between the Allwinner BROM code and the u-boot SPL code. And need to play nice with both of them.

The updated 'fel' tool is supposed to be used together with u-boot v2015.04 and provide support for booting regular u-boot SPL binaries over USB via FEL mode. So that a special u-boot sunxi FEL configuration can be eventually dropped in the future.

The usage instructions are in the 'New method of booting u-boot over USB (u-boot v2015.04 and later versions)' section of the linux-sunxi wiki page

     http://linux-sunxi.org/index.php?title=FEL/USBBoot&oldid=12979

It temporarily moves the BROM data from the first 32 KiB of SRAM into a different SRAM section before calling the SPL code. And then moves it back before returning from the SPL back to the FEL code in BROM. This provides a contiguous 32 KiB block in the beginning of SRAM for use by the SPL. And in the case if an extra SRAM section is not available, we can at least "defragmemnt" the free memory areas and still provide a contiguous ~21 KiB block to the SPL in the beginning of SRAM.

This code is also available in the following branch:

     https://github.com/ssvb/sunxi-tools/commits/20150206-fel-large-spl-support

Siarhei Siamashka (2):
fel: Split 'aw_fel_get_version' into 'get' and 'print' variants
fel: New command for loading U-Boot SPL binaries in eGON format

Quoted 6 lines
  fel-to-spl-thunk.S | 172 +++++++++++++++++++++++++++++++
  fel-to-spl-thunk.h |  69 +++++++++++++
  fel.c              | 291 +++++++++++++++++++++++++++++++++++++++++++++++++----
  3 files changed, 510 insertions(+), 22 deletions(-)

create mode 100644 fel-to-spl-thunk.S
create mode 100644 fel-to-spl-thunk.h

Thanks for your work on this, ACK to both patches, feel free to push them.

One question, the new wiki instructions say that for the latest upstream u-boot _felconfig is no longer necessary, but Simon's patches to fix FEL support upstream contain some #ifdef FEL in start.S to skip some lowlevel init when in FEL mode, does that mean that skipping this is no longer necessary ?

Regards,

Hans

#6 lore raw

v2 cover Support unified u-boot SPL for SD cards and FEL mode

Quoted 36 lines · On Tue, 10 Feb 2015 09:21:59 +0100 Hans de Goede <[email protected]> wrote:

Hi,

Quoted 33 lines · Siarhei Siamashka

An updated variant of the older patch

     http://lists.denx.de/pipermail/u-boot/2015-February/204024.html

These patches are not for the u-boot code (unless u-boot decides to cannibalize the 'fel' tool later) but still provide a glue layerHi, between the Allwinner BROM code and the u-boot SPL code. And need to play nice with both of them.

The updated 'fel' tool is supposed to be used together with u-boot v2015.04 and provide support for booting regular u-boot SPL binaries over USB via FEL mode. So that a special u-boot sunxi FEL configuration can be eventually dropped in the future.

The usage instructions are in the 'New method of booting u-boot over USB (u-boot v2015.04 and later versions)' section of the linux-sunxi wiki page

     http://linux-sunxi.org/index.php?title=FEL/USBBoot&oldid=12979

It temporarily moves the BROM data from the first 32 KiB of SRAM into a different SRAM section before calling the SPL code. And then moves it back before returning from the SPL back to the FEL code in BROM. This provides a contiguous 32 KiB block in the beginning of SRAM for use by the SPL. And in the case if an extra SRAM section is not available, we can at least "defragmemnt" the free memory areas and still provide a contiguous ~21 KiB block to the SPL in the beginning of SRAM.

This code is also available in the following branch:

     https://github.com/ssvb/sunxi-tools/commits/20150206-fel-large-spl-support

Siarhei Siamashka (2):
fel: Split 'aw_fel_get_version' into 'get' and 'print' variants
fel: New command for loading U-Boot SPL binaries in eGON format

Quoted 8 lines
Quoted 6 lines
  fel-to-spl-thunk.S | 172 +++++++++++++++++++++++++++++++
  fel-to-spl-thunk.h |  69 +++++++++++++
  fel.c              | 291 +++++++++++++++++++++++++++++++++++++++++++++++++----
  3 files changed, 510 insertions(+), 22 deletions(-)

create mode 100644 fel-to-spl-thunk.S
create mode 100644 fel-to-spl-thunk.h

Thanks for your work on this, ACK to both patches, feel free to push them.

Thanks, pushed to sunxi-tool

Quoted 4 lines

One question, the new wiki instructions say that for the latest upstream u-boot _felconfig is no longer necessary, but Simon's patches to fix FEL support upstream contain some #ifdef FEL in start.S to skip some lowlevel init when in FEL mode, does that mean that skipping this is no longer necessary ?

Skipping is not necessary if we save/restore some additional registers instead. I'm just applying extra changes on top of Simon's patches, and it works (the wiki page links to the git branch with these changes). This is discussed at

    http://lists.denx.de/pipermail/u-boot/2015-February/204361.html
Signature · 2 lines
Best regards,
Siarhei Siamashka