All of lore.kernel.org
 help / color / mirror / Atom feed
From: Heinrich Schuchardt <xypron.glpk@gmx.de>
To: u-boot@lists.denx.de
Subject: [PATCH 2/8] semihosting: Add support for writing to a file
Date: Mon, 18 May 2020 19:04:18 +0200	[thread overview]
Message-ID: <b9e4f0f8-7a9b-11c5-3007-5061a6b7859f@gmx.de> (raw)
In-Reply-To: <20200430173630.15608-3-sughosh.ganu@linaro.org>

On 4/30/20 7:36 PM, Sughosh Ganu wrote:
> Add a function to enable writing to a file. Currently, support is
> added for writing to a binary file. This would be used for
> implementing the firmware update functionality for the qemu arm64
> platform.
>
> Signed-off-by: Sughosh Ganu <sughosh.ganu@linaro.org>
> ---
>  arch/arm/lib/semihosting.c | 41 ++++++++++++++++++++++++++++++++++++--
>  include/semihosting.h      |  1 +
>  2 files changed, 40 insertions(+), 2 deletions(-)
>
> diff --git a/arch/arm/lib/semihosting.c b/arch/arm/lib/semihosting.c
> index 3aeda1303a..08181132d1 100644
> --- a/arch/arm/lib/semihosting.c
> +++ b/arch/arm/lib/semihosting.c
> @@ -17,11 +17,18 @@
>
>  #define SYSOPEN		0x01
>  #define SYSCLOSE	0x02
> +#define SYSWRITE	0x5
>  #define SYSREAD		0x06
>  #define SYSFLEN		0x0C
>
> -#define MODE_READ	0x0
> -#define MODE_READBIN	0x1
> +#define MODE_READ		0x0
> +#define MODE_READBIN		0x1
> +#define MODE_READPLUS		0x2
> +#define MODE_READPLUSBIN	0x3
> +#define MODE_WRITE		0x4
> +#define MODE_WRITEBIN		0x5
> +#define MODE_WRITEPLUS		0x6
> +#define MODE_WRITEPLUSBIN	0x7
>
>  /*
>   * Call the handler
> @@ -61,6 +68,8 @@ long smh_open(const char *fname, char *modestr)
>  		mode = MODE_READ;
>  	} else if (!(strcmp(modestr, "rb"))) {
>  		mode = MODE_READBIN;
> +	} else if (!strcmp(modestr, "w+b")) {
> +		mode = MODE_WRITEPLUSBIN;

We could have a string matching each of the 8 constants above:

static char modes[] = "r\0\0\0rb\0\0r+\0\0r+b\0w\0\0\0wb\0\0w+\0\0w+b";
// This should use less bytes than 8 separate strings.

        for (mode = 0; mode < 8; ++mode) {
                if (!strcmp(&modes[4 * mode], modestr))
                        break;
        }
        if (mode & 8)
		printf("%s: ERROR mode \'%s\' not supported\n" ...

But why are we passing the mode as string anyway?
We should use an enum parameter instead.

Best regards

Heinrich

>  	} else {
>  		printf("%s: ERROR mode \'%s\' not supported\n", __func__,
>  		       modestr);
> @@ -114,6 +123,34 @@ long smh_read(long fd, void *memp, size_t len)
>  	return 0;
>  }
>
> +/*
> + * Write 'len' bytes into the file referenced by the fd. Returns 0 on success, else
> + * a negavite value for failure
> + */
> +long smh_write(long fd, void *memp, size_t len)
> +{
> +	long ret;
> +	struct smh_write_s {
> +		long fd;
> +		void *memp;
> +		size_t len;
> +	} write;
> +
> +	debug("%s: fd %ld, memp %p, len %zu\n", __func__, fd, memp, len);
> +
> +	write.fd = fd;
> +	write.memp = memp;
> +	write.len = len;
> +
> +	ret = smh_trap(SYSWRITE, &write);
> +
> +	if (ret > 0)
> +		printf("%s: ERROR ret %ld, fd %ld, len %zu, memp %p\n",
> +		       __func__, ret, fd, len, memp);
> +
> +	return ret == 0 ? 0 : -1;
> +}
> +
>  /*
>   * Close the file using the file descriptor
>   */
> diff --git a/include/semihosting.h b/include/semihosting.h
> index f1bf419275..fa5cecddf2 100644
> --- a/include/semihosting.h
> +++ b/include/semihosting.h
> @@ -8,6 +8,7 @@
>
>  long smh_open(const char *fname, char *modestr);
>  long smh_read(long fd, void *memp, size_t len);
> +long smh_write(long fd, void *memp, size_t len);
>  long smh_close(long fd);
>
>  #endif /* _SEMIHOSTING_H_ */
>

  reply	other threads:[~2020-05-18 17:04 UTC|newest]

Thread overview: 41+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2020-04-30 17:36 [PATCH 0/8] qemu: arm64: Add support for uefi firmware management protocol routines Sughosh Ganu
2020-04-30 17:36 ` [PATCH 1/8] semihosting: Change semihosting file operation functions into global symbols Sughosh Ganu
2020-05-11  3:05   ` Akashi Takahiro
2020-05-18 16:34     ` Heinrich Schuchardt
2020-04-30 17:36 ` [PATCH 2/8] semihosting: Add support for writing to a file Sughosh Ganu
2020-05-18 17:04   ` Heinrich Schuchardt [this message]
2020-04-30 17:36 ` [PATCH 3/8] qemu: arm64: Add support for efi firmware management protocol routines Sughosh Ganu
2020-04-30 18:39   ` Heinrich Schuchardt
2020-04-30 19:13     ` Sughosh Ganu
2020-05-01  9:33       ` Heinrich Schuchardt
2020-05-05 11:15         ` Grant Likely
2020-05-05 17:04           ` Heinrich Schuchardt
2020-05-05 17:23             ` Grant Likely
2020-05-05 17:57               ` Heinrich Schuchardt
2020-05-06 15:04                 ` Grant Likely
2020-05-09 10:04                   ` Heinrich Schuchardt
2020-05-10 11:59                     ` Sughosh Ganu
2020-05-18 17:14                     ` Grant Likely
2020-05-07  2:33         ` Akashi Takahiro
2020-05-07 20:47           ` Heinrich Schuchardt
2020-05-07 23:36             ` Akashi Takahiro
2020-04-30 17:36 ` [PATCH 4/8] efi_loader: Allow parsing of miscellaneous signature database variables Sughosh Ganu
2020-04-30 17:36 ` [PATCH 5/8] efi_loader: Make the pkcs7 header parsing function an extern Sughosh Ganu
2020-05-07  7:34   ` Akashi Takahiro
2020-05-07 11:18     ` Sughosh Ganu
2020-05-08  0:51       ` Akashi Takahiro
2020-05-10 11:20         ` Sughosh Ganu
2020-04-30 17:36 ` [PATCH 6/8] efi: capsule: Add support for uefi capsule authentication Sughosh Ganu
2020-05-07  8:19   ` Akashi Takahiro
2020-05-07 11:50     ` Sughosh Ganu
2020-05-08  0:42       ` Akashi Takahiro
2020-05-10 11:26         ` Sughosh Ganu
2020-05-11  2:45           ` Akashi Takahiro
2020-04-30 17:36 ` [PATCH 7/8] qemu: arm64: " Sughosh Ganu
2020-04-30 17:36 ` [PATCH 8/8] qemu: arm64: Add documentation for capsule update Sughosh Ganu
2020-04-30 18:37   ` Heinrich Schuchardt
2020-04-30 19:08     ` Sughosh Ganu
2020-04-30 19:27       ` Tom Rini
2020-05-01  5:47         ` Sughosh Ganu
2020-05-07  2:10           ` Akashi Takahiro
2020-05-07 20:52             ` Heinrich Schuchardt

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=b9e4f0f8-7a9b-11c5-3007-5061a6b7859f@gmx.de \
    --to=xypron.glpk@gmx.de \
    --cc=u-boot@lists.denx.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 an external index of several public inboxes,
see mirroring instructions on how to clone and mirror
all data and code used by this external index.