From: Simon Goldschmidt <goldsimon@gmx.de>
To: Ilias Apalodimas <ilias.apalodimas@linaro.org>,
Maxim Uvarov <maxim.uvarov@linaro.org>
Cc: u-boot@lists.denx.de, pbrobinson@gmail.com,
joe.hershberger@ni.com, rfried.dev@gmail.com, trini@konsulko.com
Subject: Re: [PATCHv8 05/15] net/lwip: implement tftp cmd
Date: Wed, 13 Sep 2023 10:38:32 +0200 [thread overview]
Message-ID: <e5931bb5-2dfd-d5f9-69b2-38d14eebf181@gmx.de> (raw)
In-Reply-To: <ZQFTbcuLihQm2qoI@hera>
On 13.09.2023 08:15, Ilias Apalodimas wrote:
>> +
>> +/*
>> + * (C) Copyright 2023 Linaro Ltd. <maxim.uvarov@linaro.org>
>> + */
>> +
>> +#include <common.h>
>> +#include <command.h>
>> +#include <console.h>
>> +#include <bootstage.h>
>> +
>> +#include "tftp_client.h"
>> +#include "tftp_server.h"
>> +#include <tftp_example.h>
>> +
>> +#include <string.h>
>> +
>> +#include <net/ulwip.h>
>> +
>> +static ulong daddr;
>> +static ulong size;
>> +
>> +static void *tftp_open(const char *fname, const char *mode, u8_t is_write)
>> +{
>> + return NULL;
>> +}
>> +
>> +static void tftp_close(void *handle)
>> +{
>> + log_info("\ndone\n");
>> + log_info("Bytes transferred = %ld (0x%lx hex)\n", size, size);
>> +
>> + bootstage_mark_name(BOOTSTAGE_KERNELREAD_STOP, "tftp_done");
>> + if (env_set_hex("filesize", size)) {
>> + log_err("filesize not updated\n");
>> + ulwip_exit(-1);
>> + return;
>> + }
>> + ulwip_exit(0);
>> +}
>> +
>> +static int tftp_read(void *handle, void *buf, int bytes)
>> +{
>> + return 0;
>> +}
>> +
>> +static int tftp_write(void *handle, struct pbuf *p)
>> +{
>> + struct pbuf *q;
>> +
>> + for (q = p; q != NULL; q = q->next) {
>> + memcpy((void *)daddr, q->payload, q->len);
>> + daddr += q->len;
>> + size += q->len;
>> + log_info("#");
>> + }
>> +
>> + return 0;
>> +}
>> +
>> +static void tftp_error(void *handle, int err, const char *msg, int size)
>> +{
>> + char message[100];
>> +
>> + memset(message, 0, sizeof(message));
>> + memcpy(message, msg, LWIP_MIN(sizeof(message) - 1, (size_t)size));
>> +
>> + log_info("TFTP error: %d (%s)", err, message);
>> +}
>> +
>> +static const struct tftp_context tftp = {
>> + tftp_open,
>> + tftp_close,
>> + tftp_read,
>> + tftp_write,
>> + tftp_error
>> +};
>> +
>> +int ulwip_tftp(ulong addr, char *fname)
>> +{
>> + void *f = (void *)0x1; /* unused fake file handle*/
>
> I haven't dug into lwip details yet, but I am not sure this is safe to use.
> Simon, since you maintain the lwip part can you elaborate a bit more?
From the lwIP design, using an invalid pointer here is ok: that pointer
is only used as a client handle which is never dereferenced internally.
The only requirement is that it is not NULL, as that is the validity
check for the tftp client to know the handle is valid (or e.g. open
failed etc.).
So while we can leave 0x1 here, using a static uint8_t would probably be
better, at the expense of using a byte for nothing.
Regards,
Simon
>
>> + err_t err;
>> + ip_addr_t srv;
>> + int ret;
>> + char *server_ip;
>> +
>> + if (!fname || addr == 0)
>> + return CMD_RET_FAILURE;
>> +
>> + size = 0;
>> + daddr = addr;
>> + server_ip = env_get("serverip");
>> + if (!server_ip) {
>> + log_err("error: serverip variable has to be set\n");
>> + return CMD_RET_FAILURE;
>> + }
>> +
>> + ret = ipaddr_aton(server_ip, &srv);
>> + if (!ret) {
>> + log_err("error: ipaddr_aton\n");
>> + return CMD_RET_FAILURE;
>> + }
>> +
>> + log_info("TFTP from server %s; our IP address is %s\n",
>> + server_ip, env_get("ipaddr"));
>> + log_info("Filename '%s'.\n", fname);
>> + log_info("Load address: 0x%lx\n", daddr);
>> + log_info("Loading:");
>> +
>> + bootstage_mark_name(BOOTSTAGE_KERNELREAD_START, "tftp_start");
>> +
>> + err = tftp_init_client(&tftp);
>> + if (!(err == ERR_OK || err == ERR_USE))
>> + log_err("tftp_init_client err: %d\n", err);
>> +
>> + err = tftp_get(f, &srv, TFTP_PORT, fname, TFTP_MODE_OCTET);
>> + /* might return different errors, like routing problems */
>> + if (err != ERR_OK) {
>> + log_err("tftp_get err=%d\n", err);
>> + return CMD_RET_FAILURE;
>> + }
>> +
>> + if (env_set_hex("fileaddr", addr)) {
>> + log_err("fileaddr not updated\n");
>> + return CMD_RET_FAILURE;
>> + }
>> +
>> + return CMD_RET_SUCCESS;
>> +}
>> --
>> 2.30.2
>>
>
> Thanks
> /Ilias
next prev parent reply other threads:[~2023-09-13 8:38 UTC|newest]
Thread overview: 40+ messages / expand[flat|nested] mbox.gz Atom feed top
2023-09-08 13:53 [PATCHv8 00/15] net/lwip: add lwip library for the network stack Maxim Uvarov
2023-09-08 13:53 ` [PATCHv8 01/15] net/lwip: add doc/develop/net_lwip.rst Maxim Uvarov
2023-09-12 7:40 ` Ilias Apalodimas
2023-09-08 13:53 ` [PATCHv8 02/15] net/lwip: integrate lwIP library Maxim Uvarov
2023-09-08 13:53 ` [PATCHv8 03/15] net/lwip: implement dns cmd Maxim Uvarov
2023-09-13 5:56 ` Ilias Apalodimas
2023-09-13 8:32 ` Simon Goldschmidt
2023-09-13 13:46 ` Maxim Uvarov
2023-09-08 13:53 ` [PATCHv8 04/15] net/lwip: implement dhcp cmd Maxim Uvarov
2023-09-13 6:07 ` Ilias Apalodimas
2023-09-13 8:35 ` Simon Goldschmidt
2023-09-08 13:53 ` [PATCHv8 05/15] net/lwip: implement tftp cmd Maxim Uvarov
2023-09-13 6:15 ` Ilias Apalodimas
2023-09-13 8:38 ` Simon Goldschmidt [this message]
2023-09-13 13:38 ` Maxim Uvarov
2023-09-08 13:53 ` [PATCHv8 06/15] net/lwip: implement wget cmd Maxim Uvarov
2023-09-13 6:25 ` Ilias Apalodimas
2023-09-08 13:53 ` [PATCHv8 07/15] net/lwip: implement ping cmd Maxim Uvarov
2023-09-13 6:28 ` Ilias Apalodimas
2023-09-08 13:53 ` [PATCHv8 08/15] net/lwip: add lwIP configuration Maxim Uvarov
2023-09-08 13:53 ` [PATCHv8 09/15] net/lwip: implement lwIP port to U-Boot Maxim Uvarov
2023-09-08 13:53 ` [PATCHv8 10/15] net/lwip: update .gitignore with lwIP Maxim Uvarov
2023-09-08 13:53 ` [PATCHv8 11/15] net/lwip: connection between cmd and lwip apps Maxim Uvarov
2023-09-08 13:53 ` [PATCHv8 12/15] net/lwip: replace original net commands with lwip Maxim Uvarov
2023-09-08 13:53 ` [PATCHv8 13/15] net/lwip: split net.h to net.h, arp.h and eth.h Maxim Uvarov
2023-09-08 13:53 ` [PATCHv8 14/15] net/lwip: drop old net/wget Maxim Uvarov
2023-09-08 13:53 ` [PATCHv8 15/15] net/lwip/wget add port selection Maxim Uvarov
2023-09-13 6:30 ` Ilias Apalodimas
2023-09-08 13:59 ` [PATCHv8 00/15] net/lwip: add lwip library for the network stack Tom Rini
2023-09-12 11:41 ` Maxim Uvarov
2023-09-12 19:26 ` Simon Glass
2023-09-13 7:31 ` Maxim Uvarov
2023-09-13 7:53 ` Ilias Apalodimas
2023-09-13 8:43 ` Simon Goldschmidt
2023-09-13 10:06 ` Peter Robinson
2023-09-13 13:14 ` Tom Rini
2023-09-13 13:34 ` Maxim Uvarov
2023-09-21 16:29 ` Simon Glass
2023-09-22 10:56 ` Simon Goldschmidt
2023-09-22 16:13 ` Tom Rini
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=e5931bb5-2dfd-d5f9-69b2-38d14eebf181@gmx.de \
--to=goldsimon@gmx.de \
--cc=ilias.apalodimas@linaro.org \
--cc=joe.hershberger@ni.com \
--cc=maxim.uvarov@linaro.org \
--cc=pbrobinson@gmail.com \
--cc=rfried.dev@gmail.com \
--cc=trini@konsulko.com \
--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 a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox