[PATCH 04/17] MIPS: qemu-malta: generate swapped image as part of multi-image build

Ahmad Fatoum a.fatoum at pengutronix.de
Mon Apr 12 10:45:19 BST 2021


Hello Jules,

On 12.04.21 11:27, Jules Maselbas wrote:
> Hi Ahmad,
> 
> Just in case you didn't know, the byte swap can be done
> without a writting C program but with a simple shell script.
> 
> You can use objcopy to create a byte swapped image:
> objcopy -I binary --reverse-bytes=4 barebox barebox.swapped
> 
> You can use truncate to make sure the file size is multiple of 4 bytes:
> truncate -s %4 file
> 
> bswap32 script could be implemented like this:
> cp $in $out
> truncate -s %4 $out
> objcopy -I binary --reverse-byte=4 $out

I did not know that objcopy can be used for that. Will try it out and
send a fixup.

Thanks!
Ahmad

> 
> Best,
> Jules
> 
> On Mon, Apr 12, 2021 at 09:16:38AM +0200, Ahmad Fatoum wrote:
>> 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 at gmail.com>
>> Signed-off-by: Ahmad Fatoum <a.fatoum at 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 at 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 at lists.infradead.org
>> http://lists.infradead.org/mailman/listinfo/barebox
>>
> 
> 

-- 
Pengutronix e.K.                           |                             |
Steuerwalder Str. 21                       | http://www.pengutronix.de/  |
31137 Hildesheim, Germany                  | Phone: +49-5121-206917-0    |
Amtsgericht Hildesheim, HRA 2686           | Fax:   +49-5121-206917-5555 |



More information about the barebox mailing list