mail archive of the barebox mailing list
 help / color / mirror / Atom feed
From: Ahmad Fatoum <a.fatoum@pengutronix.de>
To: barebox@lists.infradead.org
Cc: Ahmad Fatoum <a.fatoum@pengutronix.de>, rcz@pengutronix.de
Subject: [PATCH 04/17] MIPS: qemu-malta: generate swapped image as part of multi-image build
Date: Mon, 12 Apr 2021 09:16:38 +0200	[thread overview]
Message-ID: <20210412071651.8769-5-a.fatoum@pengutronix.de> (raw)
In-Reply-To: <20210412071651.8769-1-a.fatoum@pengutronix.de>

Having to manually swap the words in the MIPS Malta image for QEMU
little endian emulation is annoying.

Have the multi-image build for Malta generate a second .swapped
image that can be readily used if needed.

Cc: Antony Pavlov <antonynpavlov@gmail.com>
Signed-off-by: Ahmad Fatoum <a.fatoum@pengutronix.de>
---
 Documentation/boards/mips/qemu-malta.rst | 16 ++---
 images/.gitignore                        |  1 +
 images/Makefile                          |  2 +-
 images/Makefile.malta                    |  8 ++-
 scripts/Makefile                         |  1 +
 scripts/bswap.c                          | 83 ++++++++++++++++++++++++
 6 files changed, 97 insertions(+), 14 deletions(-)
 create mode 100644 scripts/bswap.c

diff --git a/Documentation/boards/mips/qemu-malta.rst b/Documentation/boards/mips/qemu-malta.rst
index e188ae8c642a..fd37d5edb229 100644
--- a/Documentation/boards/mips/qemu-malta.rst
+++ b/Documentation/boards/mips/qemu-malta.rst
@@ -10,31 +10,23 @@ QEMU run string:
 
   qemu-system-mips -nodefaults -M malta -m 256 \
       -device VGA -serial stdio -monitor null \
-      -bios barebox-flash-image
+      -bios ./images/barebox-qemu-malta.img
 
 
 Little-endian mode
 ------------------
 
-Running little-endian Malta is a bit tricky.
 In little-endian mode the 32bit words in the boot flash image are swapped,
 a neat trick which allows bi-endian firmware.
 
-You have to swap words of ``zbarebox.bin`` image, e.g.:
-
-.. code-block:: sh
-
-  echo arch/mips/pbl/zbarebox.bin \
-      | cpio --create \
-      | cpio --extract --swap --unconditional
-
-QEMU run string:
+The barebox build generates a second ``./images/barebox-qemu-malta.img.swapped``
+image that can be used in this case, e.g.:
 
 .. code-block:: sh
 
   qemu-system-mipsel -nodefaults -M malta -m 256 \
       -device VGA -serial stdio -monitor null \
-      -bios barebox-flash-image
+      -bios ./images/barebox-qemu-malta.img.swapped
 
 
 Using GXemul
diff --git a/images/.gitignore b/images/.gitignore
index eafdb44b5bdd..3a9a77dad16e 100644
--- a/images/.gitignore
+++ b/images/.gitignore
@@ -32,3 +32,4 @@ barebox.sum
 *.mvebu1img
 *.stm32
 *.nmon
+*.swapped
diff --git a/images/Makefile b/images/Makefile
index c185982c17ed..cc330d957597 100644
--- a/images/Makefile
+++ b/images/Makefile
@@ -218,5 +218,5 @@ $(flash-list): $(image-y-path)
 clean-files := *.pbl *.pblb *.map start_*.imximg *.img barebox.z start_*.kwbimg \
 	start_*.kwbuartimg *.socfpgaimg *.mlo *.t20img *.t20img.cfg *.t30img \
 	*.t30img.cfg *.t124img *.t124img.cfg *.mlospi *.mlo *.mxsbs *.mxssd \
-	start_*.simximg start_*.usimximg *.zynqimg *.image
+	start_*.simximg start_*.usimximg *.zynqimg *.image *.swapped
 clean-files += pbl.lds
diff --git a/images/Makefile.malta b/images/Makefile.malta
index 5739ec464092..ce7b4424b63b 100644
--- a/images/Makefile.malta
+++ b/images/Makefile.malta
@@ -1,3 +1,9 @@
+quiet_cmd_bswap32_image = BSWAP4  $@
+      cmd_bswap32_image = scripts/bswap -B 4 $< > $@
+
+$(obj)/%.img.swapped: $(obj)/%.img FORCE
+	$(call if_changed,bswap32_image)
+
 pblb-$(CONFIG_BOARD_QEMU_MALTA) += start_qemu_malta
 FILE_barebox-qemu-malta.img = start_qemu_malta.pblb
-image-$(CONFIG_BOARD_QEMU_MALTA) += barebox-qemu-malta.img
+image-$(CONFIG_BOARD_QEMU_MALTA) += barebox-qemu-malta.img barebox-qemu-malta.img.swapped
diff --git a/scripts/Makefile b/scripts/Makefile
index 2d322fd1c923..57861fa9b2f8 100644
--- a/scripts/Makefile
+++ b/scripts/Makefile
@@ -16,6 +16,7 @@ hostprogs-always-$(CONFIG_KALLSYMS)			+= kallsyms
 hostprogs-always-$(CONFIG_MIPS)				+= mips-relocs
 hostprogs-always-$(CONFIG_MVEBU_HOSTTOOLS)		+= kwbimage kwboot mvebuimg
 hostprogs-always-$(CONFIG_ARCH_OMAP)			+= omap_signGP mk-omap-image
+hostprogs-always-$(CONFIG_BOARD_QEMU_MALTA)		+= bswap
 hostprogs-always-$(CONFIG_ARCH_S5PCxx)			+= s5p_cksum
 hostprogs-always-$(CONFIG_ARCH_DAVINCI)			+= mkublheader
 HOSTCFLAGS_zynq_mkimage.o = -I$(srctree) -I$(srctree)/arch/arm/mach-zynq/include
diff --git a/scripts/bswap.c b/scripts/bswap.c
new file mode 100644
index 000000000000..e87d30cfe51e
--- /dev/null
+++ b/scripts/bswap.c
@@ -0,0 +1,83 @@
+// SPDX-License-Identifier: GPL-2.0-or-later
+// SPDX-FileCopyrightText: 2012 Jan Luebbe <j.luebbe@pengutronix.de>
+
+#define _BSD_SOURCE
+#define _DEFAULT_SOURCE
+
+#include <stdio.h>
+#include <sys/types.h>
+#include <sys/stat.h>
+#include <fcntl.h>
+#include <unistd.h>
+#include <stdint.h>
+#include <limits.h>
+#include <errno.h>
+#include <stdlib.h>
+#include <string.h>
+#include <getopt.h>
+#include <endian.h>
+
+static void usage(char *prgname)
+{
+	printf("usage: %s [OPTION] FILE > IMAGE\n"
+	       "\n"
+	       "options:\n"
+	       "  -B           number of bytes to swap each time\n",
+	       prgname);
+}
+
+int main(int argc, char *argv[])
+{
+	FILE *input;
+	int opt;
+	size_t size;
+	unsigned long bytes = 0;
+	uint32_t temp_u32;
+
+	while((opt = getopt(argc, argv, "B:")) != -1) {
+		switch (opt) {
+		case 'B':
+			bytes = strtoul(optarg, NULL, 0);
+			break;
+		}
+	}
+
+	if (optind >= argc) {
+		usage(argv[0]);
+		return 1;
+	}
+
+	if (bytes != 4) {
+		fprintf(stderr, "-B %s unsupported\n", optarg);
+		return 1;
+	}
+
+	input = fopen(argv[optind], "r");
+	if (input == NULL) {
+		perror("fopen");
+		return 1;
+	}
+
+	for (;;) {
+		size = fread(&temp_u32, 1, sizeof(uint32_t), input);
+		if (!size)
+			break;
+		if (size < 4 && !feof(input)) {
+			perror("fread");
+			return 1;
+		}
+
+		temp_u32 = htobe32(le32toh(temp_u32));
+		if (fwrite(&temp_u32, 1, sizeof(uint32_t), stdout) != 4) {
+			perror("fwrite");
+			return 1;
+		}
+	}
+
+	if (fclose(input) != 0) {
+		perror("fclose");
+		return 1;
+	}
+
+	return 0;
+}
-- 
2.29.2


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


  parent reply	other threads:[~2021-04-12  7:18 UTC|newest]

Thread overview: 21+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2021-04-12  7:16 [PATCH 00/17] add barebox in-tree testing infrastructure Ahmad Fatoum
2021-04-12  7:16 ` [PATCH 01/17] kbuild: add install target Ahmad Fatoum
2021-04-12  7:16 ` [PATCH 02/17] kbuild: add ARCH={i386, x86_64} as aliases for x86 Ahmad Fatoum
2021-04-12  7:16 ` [PATCH 03/17] kbuild: add ARCH=um alias for sandbox Ahmad Fatoum
2021-04-12  7:16 ` Ahmad Fatoum [this message]
2021-04-12  9:27   ` [PATCH 04/17] MIPS: qemu-malta: generate swapped image as part of multi-image build Jules Maselbas
2021-04-12  9:45     ` Ahmad Fatoum
2021-04-12  7:16 ` [PATCH 05/17] MIPS: qemu-malta: replace board code with syscon-reboot Ahmad Fatoum
2021-04-12  7:16 ` [PATCH 06/17] MIPS: configs: qemu-malta: enable Virt I/O Ahmad Fatoum
2021-04-12  7:16 ` [PATCH 07/17] openrisc: set default KBUILD_IMAGE Ahmad Fatoum
2021-04-12  7:16 ` [PATCH 08/17] Documentation: boards: RISC-V: update TinyEMU support Ahmad Fatoum
2021-04-12  7:16 ` [PATCH 09/17] test: add basic barebox self-test infrastructure Ahmad Fatoum
2021-04-12 10:12   ` Jules Maselbas
2021-04-12  7:16 ` [PATCH 10/17] test: self: port Linux printf kselftest Ahmad Fatoum
2021-04-12  7:16 ` [PATCH 11/17] test: add labgrid configs for some emulated targets Ahmad Fatoum
2021-04-12  7:16 ` [PATCH 12/17] test: add first sample tests Ahmad Fatoum
2021-04-12  7:16 ` [PATCH 13/17] test: support running labgrid-pytest from out-of-tree build directory Ahmad Fatoum
2021-04-12  7:16 ` [PATCH 14/17] test: add emulate.pl, a runner for barebox on emulated targets Ahmad Fatoum
2021-04-12  7:16 ` [PATCH 15/17] test: self: run selftests as part of the pytest suite Ahmad Fatoum
2021-04-12  7:16 ` [PATCH 16/17] test: add bthread test Ahmad Fatoum
2021-04-12  7:16 ` [PATCH 17/17] [RFC] test: run ./test/emulate.pl in Github action Ahmad Fatoum

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=20210412071651.8769-5-a.fatoum@pengutronix.de \
    --to=a.fatoum@pengutronix.de \
    --cc=barebox@lists.infradead.org \
    --cc=rcz@pengutronix.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 a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox