mail archive of the barebox mailing list
 help / color / mirror / Atom feed
* [PATCH 0/3] scripts: imx: make imx-usb-loader compilable for MinGW
@ 2023-04-11  9:38 Ahmad Fatoum
  2023-04-11  9:38 ` [PATCH 1/3] imx-usb-loader: don't depend on arpa/inet.h for endianness conversion Ahmad Fatoum
                   ` (3 more replies)
  0 siblings, 4 replies; 5+ messages in thread
From: Ahmad Fatoum @ 2023-04-11  9:38 UTC (permalink / raw)
  To: barebox; +Cc: Johannes Schneider

While the system flashing Linux is often running Linux itself, it may
be Windows instead. To support this, let's support building
imx-usb-loader for Windows.

Only compiled-tested on Debian 11 with:

   x86_64-w64-mingw32-gcc (GCC) 10-win32 20210110 (gcc-mingw-w64-x86-64-posix)
   libusb-1.0.26-binaries.7z  (from Github releases page)

  CC=${CC:-x86_64-w64-mingw32-gcc}
  LIBUSB=${LIBUSB:-../libusb-binaries}
  
  CPPFLAGS="-isystem $LIBUSB -I scripts/include/ -I include/mach/"
  LDFLAGS="-L $LIBUSB -lusb-1.0"
  
  $CC -c -o imx.o scripts/imx/imx.c $CPPFLAGS
  $CC -c -o imx-usb-loader.o scripts/imx/imx-usb-loader.c $CPPFLAGS
  
  $CC -o imx-usb-loader.exe imx.o imx-usb-loader.o $LDFLAGS


Ahmad Fatoum (3):
  imx-usb-loader: don't depend on arpa/inet.h for endianness conversion
  scripts: compiler.h: add Windows support
  scripts: common: drop unused mman.h include

 scripts/common.c             |  1 -
 scripts/compiler.h           | 33 ++++++++++++++++++++++++
 scripts/imx/imx-usb-loader.c | 49 ++++++++++++++++++------------------
 3 files changed, 57 insertions(+), 26 deletions(-)

-- 
2.39.2




^ permalink raw reply	[flat|nested] 5+ messages in thread

* [PATCH 1/3] imx-usb-loader: don't depend on arpa/inet.h for endianness conversion
  2023-04-11  9:38 [PATCH 0/3] scripts: imx: make imx-usb-loader compilable for MinGW Ahmad Fatoum
@ 2023-04-11  9:38 ` Ahmad Fatoum
  2023-04-11  9:38 ` [PATCH 2/3] scripts: compiler.h: add Windows support Ahmad Fatoum
                   ` (2 subsequent siblings)
  3 siblings, 0 replies; 5+ messages in thread
From: Ahmad Fatoum @ 2023-04-11  9:38 UTC (permalink / raw)
  To: barebox; +Cc: Johannes Schneider, Ahmad Fatoum

We already have <scripts/compiler.h>, which offers endianness conversion
for non-Linux platforms like MacOS. Let's use that to reduce our
dependency on external implementation-defined headers.

Signed-off-by: Ahmad Fatoum <a.fatoum@pengutronix.de>
---
 scripts/imx/imx-usb-loader.c | 49 ++++++++++++++++++------------------
 1 file changed, 24 insertions(+), 25 deletions(-)

diff --git a/scripts/imx/imx-usb-loader.c b/scripts/imx/imx-usb-loader.c
index 4bcb4cbab6fe..41d57906c752 100644
--- a/scripts/imx/imx-usb-loader.c
+++ b/scripts/imx/imx-usb-loader.c
@@ -31,7 +31,6 @@
 #include <stdlib.h>
 #include <libusb.h>
 #include <getopt.h>
-#include <arpa/inet.h>
 #include <linux/kernel.h>
 
 #include "../common.h"
@@ -594,8 +593,8 @@ static int read_memory(unsigned addr, void *dest, unsigned cnt)
 	int err;
 	int rem;
 	unsigned char tmp[64];
-	read_reg_command.addr = htonl(addr);
-	read_reg_command.cnt = htonl(cnt);
+	read_reg_command.addr = htobe32(addr);
+	read_reg_command.cnt = htobe32(cnt);
 
 	for (;;) {
 		err = transfer(1, &read_reg_command, 16, &last_trans);
@@ -651,8 +650,8 @@ static int write_memory(unsigned addr, unsigned val, int width)
 		.rsvd = 0,
 	};
 
-	write_reg_command.addr = htonl(addr);
-	write_reg_command.cnt = htonl(4);
+	write_reg_command.addr = htobe32(addr);
+	write_reg_command.cnt = htobe32(4);
 
 	if (verbose > 1)
 		printf("write memory reg: 0x%08x val: 0x%08x width: %d\n", addr, val, width);
@@ -671,7 +670,7 @@ static int write_memory(unsigned addr, unsigned val, int width)
 			return -1;
 	}
 
-	write_reg_command.data = htonl(val);
+	write_reg_command.data = htobe32(val);
 
 	for (;;) {
 		err = transfer(1, &write_reg_command, 16, &last_trans);
@@ -771,8 +770,8 @@ static int load_file(void *buf, unsigned len, unsigned dladdr,
 
 	len = ALIGN(len, 4);
 
-	dl_command.addr = htonl(dladdr);
-	dl_command.cnt = htonl(len);
+	dl_command.addr = htobe32(dladdr);
+	dl_command.cnt = htobe32(len);
 	dl_command.rsvd = type;
 
 	for (;;) {
@@ -833,7 +832,7 @@ static int sdp_jump_address(unsigned addr)
 	int last_trans, err;
 	int retry = 0;
 
-	jump_command.addr = htonl(addr);
+	jump_command.addr = htobe32(addr);
 
 	for (;;) {
 		err = transfer(1, &jump_command, 16, &last_trans);
@@ -862,10 +861,10 @@ static int do_dcd_v2_cmd_write(const unsigned char *dcd)
 	int set_bits = 0, clear_bits = 0;
 	int idx, bytes;
 	struct imx_dcd_v2_write *recs = (struct imx_dcd_v2_write *) dcd;
-	int num_rec = (ntohs(recs->length) - 4) /
+	int num_rec = (be16toh(recs->length) - 4) /
 		      sizeof(struct imx_dcd_v2_write_rec);
 	printf("DCD write: sub dcd length: 0x%04x, flags: 0x%02x\n",
-		ntohs(recs->length), recs->param);
+		be16toh(recs->length), recs->param);
 
 	if (recs->param & PARAMETER_FLAG_MASK) {
 		if (recs->param & PARAMETER_FLAG_SET)
@@ -886,8 +885,8 @@ static int do_dcd_v2_cmd_write(const unsigned char *dcd)
 
 	for (idx = 0; idx < num_rec; idx++) {
 		const struct imx_dcd_v2_write_rec *record = &recs->data[idx];
-		int ret = modify_memory(ntohl(record->addr),
-				 ntohl(record->val), bytes,
+		int ret = modify_memory(be32toh(record->addr),
+				 be32toh(record->val), bytes,
 				 set_bits, clear_bits);
 		if (ret < 0)
 			return ret;
@@ -902,13 +901,13 @@ static int do_dcd_v2_cmd_check(const unsigned char *dcd)
 	int bytes;
 	enum imx_dcd_v2_check_cond cond;
 	struct imx_dcd_v2_check *check = (struct imx_dcd_v2_check *) dcd;
-	switch (ntohs(check->length)) {
+	switch (be16toh(check->length)) {
 	case 12:
 		/* poll indefinitely */
 		poll_count = 0xffffffff;
 		break;
 	case 16:
-		poll_count = ntohl(check->count);
+		poll_count = be32toh(check->count);
 		if (poll_count == 0)
 			/* this command behaves as for NOP */
 			return 0;
@@ -941,10 +940,10 @@ static int do_dcd_v2_cmd_check(const unsigned char *dcd)
 		return -1;
 	}
 
-	mask = ntohl(check->mask);
+	mask = be32toh(check->mask);
 
 	fprintf(stderr, "DCD check condition %i on address 0x%x\n",
-		cond, ntohl(check->addr));
+		cond, be32toh(check->addr));
 	/* Reduce the poll count to some arbitrary practical limit.
 	   Polling via SRP commands will be much slower compared to
 	   polling when DCD is interpreted by the SOC microcode.
@@ -954,7 +953,7 @@ static int do_dcd_v2_cmd_check(const unsigned char *dcd)
 
 	while (poll_count > 0) {
 		uint32_t data = 0;
-		int ret = read_memory(ntohl(check->addr), &data, bytes);
+		int ret = read_memory(be32toh(check->addr), &data, bytes);
 		if (ret < 0)
 			return ret;
 
@@ -983,7 +982,7 @@ static int do_dcd_v2_cmd_check(const unsigned char *dcd)
 
 	fprintf(stderr, "Error: timeout waiting for DCD check condition %i "
 		"on address 0x%08x to match 0x%08x\n", cond,
-		ntohl(check->addr), ntohl(check->mask));
+		be32toh(check->addr), be32toh(check->mask));
 	return -1;
 }
 
@@ -1015,7 +1014,7 @@ static int process_dcd_table_ivt(const struct imx_flash_header_v2 *hdr,
 		fprintf(stderr, "Error: Unknown DCD header tag\n");
 		return -1;
 	}
-	m_length = ntohs(dcd_hdr->length);
+	m_length = be16toh(dcd_hdr->length);
 	dcd_end = dcd + m_length;
 	if (dcd_end > file_end) {
 		fprintf(stderr, "Error: DCD length %08x exceeds EOF\n",
@@ -1028,7 +1027,7 @@ static int process_dcd_table_ivt(const struct imx_flash_header_v2 *hdr,
 	while (dcd < dcd_end) {
 		int ret = 0;
 		struct imx_ivt_header *cmd_hdr = (struct imx_ivt_header *) dcd;
-		unsigned s_length = ntohs(cmd_hdr->length);
+		unsigned s_length = be16toh(cmd_hdr->length);
 		if (dcd +  s_length > file_end) {
 			fprintf(stderr, "Error: DCD length %08x exceeds EOF\n",
 				s_length);
@@ -1473,14 +1472,14 @@ static int mxs_load_buf(uint8_t *data, int size)
 	static struct mxs_command dl_command;
 	int last_trans, err;
 
-	dl_command.sign = htonl(0x424c5443); /* Signature: BLTC */
-	dl_command.tag = htonl(0x1);
-	dl_command.size = htonl(size);
+	dl_command.sign = htobe32(0x424c5443); /* Signature: BLTC */
+	dl_command.tag = htobe32(0x1);
+	dl_command.size = htobe32(size);
 	dl_command.flags = 0;
 	dl_command.rsvd[0] = 0;
 	dl_command.rsvd[1] = 0;
 	dl_command.cmd = MXS_CMD_FW_DOWNLOAD;
-	dl_command.dw_size = htonl(size);
+	dl_command.dw_size = htobe32(size);
 
 	err = transfer(1, &dl_command, 20, &last_trans);
 	if (err) {
-- 
2.39.2




^ permalink raw reply	[flat|nested] 5+ messages in thread

* [PATCH 2/3] scripts: compiler.h: add Windows support
  2023-04-11  9:38 [PATCH 0/3] scripts: imx: make imx-usb-loader compilable for MinGW Ahmad Fatoum
  2023-04-11  9:38 ` [PATCH 1/3] imx-usb-loader: don't depend on arpa/inet.h for endianness conversion Ahmad Fatoum
@ 2023-04-11  9:38 ` Ahmad Fatoum
  2023-04-11  9:38 ` [PATCH 3/3] scripts: common: drop unused mman.h include Ahmad Fatoum
  2023-04-11 12:30 ` [PATCH 0/3] scripts: imx: make imx-usb-loader compilable for MinGW Sascha Hauer
  3 siblings, 0 replies; 5+ messages in thread
From: Ahmad Fatoum @ 2023-04-11  9:38 UTC (permalink / raw)
  To: barebox; +Cc: Johannes Schneider, Ahmad Fatoum

We lack endianness conversion functions for Windows. Import them from
the public-domain portable_endian.h[1]. We skip the 64-bit XBox support
though as it's unlikely we'll need to run imx-usb-loader on that
particular game console.

While at it, only define min when it's undefined. This works around one
of the winapi headers indirectly included defining it.

[1]: https://gist.github.com/panzi/6856583

Signed-off-by: Ahmad Fatoum <a.fatoum@pengutronix.de>
---
 scripts/compiler.h | 33 +++++++++++++++++++++++++++++++++
 1 file changed, 33 insertions(+)

diff --git a/scripts/compiler.h b/scripts/compiler.h
index c932f715c551..925cad21b6cf 100644
--- a/scripts/compiler.h
+++ b/scripts/compiler.h
@@ -61,6 +61,37 @@ typedef unsigned int  uint;
 #elif defined(__OpenBSD__) || defined(__FreeBSD__) || \
       defined(__NetBSD__) || defined(__DragonFly__)
 # include <sys/endian.h>
+#elif defined _WIN32
+# if defined(_MSC_VER)
+#  include <stdlib.h>
+#  define htobe16(x) _byteswap_ushort(x)
+#  define htole16(x) (x)
+#  define be16toh(x) _byteswap_ushort(x)
+#  define le16toh(x) (x)
+#  define htobe32(x) _byteswap_ulong(x)
+#  define htole32(x) (x)
+#  define be32toh(x) _byteswap_ulong(x)
+#  define le32toh(x) (x)
+#  define htobe64(x) _byteswap_uint64(x)
+#  define htole64(x) (x)
+#  define be64toh(x) _byteswap_uint64(x)
+#  define le64toh(x) (x)
+# elif defined(__GNUC__) || defined(__clang__)
+#  define htobe16(x) __builtin_bswap16(x)
+#  define htole16(x) (x)
+#  define be16toh(x) __builtin_bswap16(x)
+#  define le16toh(x) (x)
+#  define htobe32(x) __builtin_bswap32(x)
+#  define htole32(x) (x)
+#  define be32toh(x) __builtin_bswap32(x)
+#  define le32toh(x) (x)
+#  define htobe64(x) __builtin_bswap64(x)
+#  define htole64(x) (x)
+#  define be64toh(x) __builtin_bswap64(x)
+#  define le64toh(x) (x)
+#else
+#  error platform not supported
+#endif
 #else /* assume Linux */
 # include <sys/types.h>
 # include <endian.h>
@@ -128,11 +159,13 @@ typedef uint32_t __u32;
 # define be64_to_cpu(x)		(x)
 #endif
 
+#ifndef min
 #define min(x, y) ({                            \
 	typeof(x) _min1 = (x);                  \
 	typeof(y) _min2 = (y);                  \
 	(void) (&_min1 == &_min2);              \
 	_min1 < _min2 ? _min1 : _min2; })
+#endif
 
 static inline void *xmalloc(size_t size)
 {
-- 
2.39.2




^ permalink raw reply	[flat|nested] 5+ messages in thread

* [PATCH 3/3] scripts: common: drop unused mman.h include
  2023-04-11  9:38 [PATCH 0/3] scripts: imx: make imx-usb-loader compilable for MinGW Ahmad Fatoum
  2023-04-11  9:38 ` [PATCH 1/3] imx-usb-loader: don't depend on arpa/inet.h for endianness conversion Ahmad Fatoum
  2023-04-11  9:38 ` [PATCH 2/3] scripts: compiler.h: add Windows support Ahmad Fatoum
@ 2023-04-11  9:38 ` Ahmad Fatoum
  2023-04-11 12:30 ` [PATCH 0/3] scripts: imx: make imx-usb-loader compilable for MinGW Sascha Hauer
  3 siblings, 0 replies; 5+ messages in thread
From: Ahmad Fatoum @ 2023-04-11  9:38 UTC (permalink / raw)
  To: barebox; +Cc: Johannes Schneider, Ahmad Fatoum

<sys/mman.h> is for mmap use of which there is none in scripts/common.c.
Drop it. This has the additional benefit that the file is now compilable
with x86_64-w64-mingw32-gcc.

Signed-off-by: Ahmad Fatoum <a.fatoum@pengutronix.de>
---
 scripts/common.c | 1 -
 1 file changed, 1 deletion(-)

diff --git a/scripts/common.c b/scripts/common.c
index e2c53c5aef71..88173bc9772a 100644
--- a/scripts/common.c
+++ b/scripts/common.c
@@ -9,7 +9,6 @@
 #include <string.h>
 #include <errno.h>
 #include <stdarg.h>
-#include <sys/mman.h>
 
 #include "common.h"
 
-- 
2.39.2




^ permalink raw reply	[flat|nested] 5+ messages in thread

* Re: [PATCH 0/3] scripts: imx: make imx-usb-loader compilable for MinGW
  2023-04-11  9:38 [PATCH 0/3] scripts: imx: make imx-usb-loader compilable for MinGW Ahmad Fatoum
                   ` (2 preceding siblings ...)
  2023-04-11  9:38 ` [PATCH 3/3] scripts: common: drop unused mman.h include Ahmad Fatoum
@ 2023-04-11 12:30 ` Sascha Hauer
  3 siblings, 0 replies; 5+ messages in thread
From: Sascha Hauer @ 2023-04-11 12:30 UTC (permalink / raw)
  To: Ahmad Fatoum; +Cc: barebox, Johannes Schneider

On Tue, Apr 11, 2023 at 11:38:41AM +0200, Ahmad Fatoum wrote:
> While the system flashing Linux is often running Linux itself, it may
> be Windows instead. To support this, let's support building
> imx-usb-loader for Windows.
> 
> Only compiled-tested on Debian 11 with:
> 
>    x86_64-w64-mingw32-gcc (GCC) 10-win32 20210110 (gcc-mingw-w64-x86-64-posix)
>    libusb-1.0.26-binaries.7z  (from Github releases page)
> 
>   CC=${CC:-x86_64-w64-mingw32-gcc}
>   LIBUSB=${LIBUSB:-../libusb-binaries}
>   
>   CPPFLAGS="-isystem $LIBUSB -I scripts/include/ -I include/mach/"
>   LDFLAGS="-L $LIBUSB -lusb-1.0"
>   
>   $CC -c -o imx.o scripts/imx/imx.c $CPPFLAGS
>   $CC -c -o imx-usb-loader.o scripts/imx/imx-usb-loader.c $CPPFLAGS
>   
>   $CC -o imx-usb-loader.exe imx.o imx-usb-loader.o $LDFLAGS
> 
> 
> Ahmad Fatoum (3):
>   imx-usb-loader: don't depend on arpa/inet.h for endianness conversion
>   scripts: compiler.h: add Windows support
>   scripts: common: drop unused mman.h include

Applied, thanks

Sascha

> 
>  scripts/common.c             |  1 -
>  scripts/compiler.h           | 33 ++++++++++++++++++++++++
>  scripts/imx/imx-usb-loader.c | 49 ++++++++++++++++++------------------
>  3 files changed, 57 insertions(+), 26 deletions(-)
> 
> -- 
> 2.39.2
> 
> 
> 

-- 
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 |



^ permalink raw reply	[flat|nested] 5+ messages in thread

end of thread, other threads:[~2023-04-11 12:31 UTC | newest]

Thread overview: 5+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2023-04-11  9:38 [PATCH 0/3] scripts: imx: make imx-usb-loader compilable for MinGW Ahmad Fatoum
2023-04-11  9:38 ` [PATCH 1/3] imx-usb-loader: don't depend on arpa/inet.h for endianness conversion Ahmad Fatoum
2023-04-11  9:38 ` [PATCH 2/3] scripts: compiler.h: add Windows support Ahmad Fatoum
2023-04-11  9:38 ` [PATCH 3/3] scripts: common: drop unused mman.h include Ahmad Fatoum
2023-04-11 12:30 ` [PATCH 0/3] scripts: imx: make imx-usb-loader compilable for MinGW Sascha Hauer

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox