blob: 94bacf630752d20357cef40775a25cca39d811dc [file] [log] [blame]
Jerome Forissier1ff00362024-10-16 12:04:03 +02001// SPDX-License-Identifier: GPL-2.0+
2/* Copyright (C) 2024 Linaro Ltd. */
3
4#include <command.h>
Jerome Forissier6a78e962024-10-16 12:04:05 +02005#include <console.h>
6#include <display_options.h>
7#include <dm/device.h>
8#include <efi_loader.h>
Tom Rinic31301c2025-05-15 17:31:50 -06009#include <env.h>
Jerome Forissier6a78e962024-10-16 12:04:05 +020010#include <image.h>
11#include <linux/delay.h>
Jerome Forissier27ab7162025-04-17 15:27:00 +020012#include <linux/kconfig.h>
Jerome Forissier6a78e962024-10-16 12:04:05 +020013#include <lwip/apps/tftp_client.h>
14#include <lwip/timeouts.h>
15#include <mapmem.h>
16#include <net.h>
17#include <time.h>
18
19#define PROGRESS_PRINT_STEP_BYTES (10 * 1024)
Jerome Forissierf5702a42025-04-28 11:24:07 +020020/* Max time to wait for first data packet from server */
21#define NO_RSP_TIMEOUT_MS 10000
Jerome Forissier6a78e962024-10-16 12:04:05 +020022
23enum done_state {
24 NOT_DONE = 0,
25 SUCCESS,
26 FAILURE,
27 ABORTED
28};
29
30struct tftp_ctx {
31 ulong daddr;
32 ulong size;
33 ulong block_count;
34 ulong start_time;
35 enum done_state done;
36};
37
Jerome Forissier27ab7162025-04-17 15:27:00 +020038/**
39 * store_block() - copy received data
40 *
41 * This function is called by the receive callback to copy a block of data
42 * into its final location (ctx->daddr). Before doing so, it checks if the copy
43 * is allowed.
44 *
45 * @ctx: the context for the current transfer
46 * @src: the data received from the TCP stack
47 * @len: the length of the data
48 */
49static int store_block(struct tftp_ctx *ctx, void *src, u16_t len)
50{
51 ulong store_addr = ctx->daddr;
52 void *ptr;
53
54 if (CONFIG_IS_ENABLED(LMB)) {
55 if (store_addr + len < store_addr ||
56 lmb_read_check(store_addr, len)) {
57 puts("\nTFTP error: ");
58 puts("trying to overwrite reserved memory...\n");
59 return -1;
60 }
61 }
62
63 ptr = map_sysmem(store_addr, len);
64 memcpy(ptr, src, len);
65 unmap_sysmem(ptr);
66
67 ctx->daddr += len;
68 ctx->size += len;
69 ctx->block_count++;
70 if (ctx->block_count % 10 == 0) {
71 putc('#');
72 if (ctx->block_count % (65 * 10) == 0)
73 puts("\n\t ");
74 }
75
76 return 0;
77}
78
Jerome Forissier6a78e962024-10-16 12:04:05 +020079static void *tftp_open(const char *fname, const char *mode, u8_t is_write)
80{
81 return NULL;
82}
83
84static void tftp_close(void *handle)
85{
86 struct tftp_ctx *ctx = handle;
87 ulong elapsed;
88
89 if (ctx->done == FAILURE || ctx->done == ABORTED) {
90 /* Closing after an error or Ctrl-C */
91 return;
92 }
93 ctx->done = SUCCESS;
94
95 elapsed = get_timer(ctx->start_time);
96 if (elapsed > 0) {
97 puts("\n\t "); /* Line up with "Loading: " */
98 print_size(ctx->size / elapsed * 1000, "/s");
99 }
100 puts("\ndone\n");
101 printf("Bytes transferred = %lu (%lx hex)\n", ctx->size, ctx->size);
102
103 if (env_set_hex("filesize", ctx->size)) {
104 log_err("filesize not updated\n");
105 return;
106 }
107}
108
109static int tftp_read(void *handle, void *buf, int bytes)
110{
111 return 0;
112}
113
114static int tftp_write(void *handle, struct pbuf *p)
115{
116 struct tftp_ctx *ctx = handle;
117 struct pbuf *q;
118
Jerome Forissier27ab7162025-04-17 15:27:00 +0200119 for (q = p; q; q = q->next)
120 if (store_block(ctx, q->payload, q->len) < 0)
121 return -1;
Jerome Forissier6a78e962024-10-16 12:04:05 +0200122
123 return 0;
124}
125
126static void tftp_error(void *handle, int err, const char *msg, int size)
127{
128 struct tftp_ctx *ctx = handle;
129 char message[100];
130
131 ctx->done = FAILURE;
132 memset(message, 0, sizeof(message));
133 memcpy(message, msg, LWIP_MIN(sizeof(message) - 1, (size_t)size));
134
135 printf("\nTFTP error: %d (%s)\n", err, message);
136}
137
138static const struct tftp_context tftp_context = {
139 tftp_open,
140 tftp_close,
141 tftp_read,
142 tftp_write,
143 tftp_error
144};
145
Jerome Forissierf5702a42025-04-28 11:24:07 +0200146static void no_response(void *arg)
147{
148 struct tftp_ctx *ctx = (struct tftp_ctx *)arg;
149
150 if (ctx->size)
151 return;
152
153 printf("Timeout!\n");
154 ctx->done = FAILURE;
155}
156
Jerome Forissier6a78e962024-10-16 12:04:05 +0200157static int tftp_loop(struct udevice *udev, ulong addr, char *fname,
158 ip_addr_t srvip, uint16_t srvport)
159{
Tim Harvey2e6a1602025-05-30 08:38:24 -0700160 int blksize = CONFIG_TFTP_BLOCKSIZE;
Jerome Forissier6a78e962024-10-16 12:04:05 +0200161 struct netif *netif;
162 struct tftp_ctx ctx;
Tim Harvey2e6a1602025-05-30 08:38:24 -0700163 const char *ep;
Jerome Forissier6a78e962024-10-16 12:04:05 +0200164 err_t err;
165
166 if (!fname || addr == 0)
167 return -1;
168
169 if (!srvport)
170 srvport = TFTP_PORT;
171
172 netif = net_lwip_new_netif(udev);
173 if (!netif)
174 return -1;
175
176 ctx.done = NOT_DONE;
177 ctx.size = 0;
178 ctx.block_count = 0;
179 ctx.daddr = addr;
180
181 printf("Using %s device\n", udev->name);
182 printf("TFTP from server %s; our IP address is %s\n",
Jerome Forissier97083502024-11-07 12:27:57 +0100183 ip4addr_ntoa(&srvip), env_get("ipaddr"));
Jerome Forissier6a78e962024-10-16 12:04:05 +0200184 printf("Filename '%s'.\n", fname);
185 printf("Load address: 0x%lx\n", ctx.daddr);
186 printf("Loading: ");
187
188 err = tftp_init_client(&tftp_context);
189 if (!(err == ERR_OK || err == ERR_USE))
190 log_err("tftp_init_client err: %d\n", err);
191
Tim Harvey2e6a1602025-05-30 08:38:24 -0700192 ep = env_get("tftpblocksize");
193 if (ep)
194 blksize = simple_strtol(ep, NULL, 10);
195 tftp_client_set_blksize(blksize);
Jerome Forissier0fb81902024-10-16 12:04:13 +0200196
Jerome Forissier6a78e962024-10-16 12:04:05 +0200197 ctx.start_time = get_timer(0);
198 err = tftp_get(&ctx, &srvip, srvport, fname, TFTP_MODE_OCTET);
199 /* might return different errors, like routing problems */
200 if (err != ERR_OK) {
201 printf("tftp_get() error %d\n", err);
202 net_lwip_remove_netif(netif);
203 return -1;
204 }
205
Jerome Forissierf5702a42025-04-28 11:24:07 +0200206 sys_timeout(NO_RSP_TIMEOUT_MS, no_response, &ctx);
Jerome Forissier6a78e962024-10-16 12:04:05 +0200207 while (!ctx.done) {
208 net_lwip_rx(udev, netif);
Jerome Forissier6a78e962024-10-16 12:04:05 +0200209 if (ctrlc()) {
210 printf("\nAbort\n");
211 ctx.done = ABORTED;
212 break;
213 }
214 }
Jerome Forissierf5702a42025-04-28 11:24:07 +0200215 sys_untimeout(no_response, (void *)&ctx);
Jerome Forissier6a78e962024-10-16 12:04:05 +0200216
217 tftp_cleanup();
218
219 net_lwip_remove_netif(netif);
220
221 if (ctx.done == SUCCESS) {
222 if (env_set_hex("fileaddr", addr)) {
223 log_err("fileaddr not updated\n");
224 return -1;
225 }
226 efi_set_bootdev("Net", "", fname, map_sysmem(addr, 0),
227 ctx.size);
228 return 0;
229 }
230
231 return -1;
232}
Jerome Forissier1ff00362024-10-16 12:04:03 +0200233
234int do_tftpb(struct cmd_tbl *cmdtp, int flag, int argc, char *const argv[])
235{
Jerome Forissier6a78e962024-10-16 12:04:05 +0200236 int ret = CMD_RET_SUCCESS;
237 char *arg = NULL;
238 char *words[3] = { };
239 char *fname = NULL;
240 char *server_ip = NULL;
241 char *server_port = NULL;
242 char *end;
243 ip_addr_t srvip;
Jerome Forissier97083502024-11-07 12:27:57 +0100244 u16 port = TFTP_PORT;
Jerome Forissier6a78e962024-10-16 12:04:05 +0200245 ulong laddr;
246 ulong addr;
247 int i;
248
249 laddr = env_get_ulong("loadaddr", 16, image_load_addr);
250
251 switch (argc) {
252 case 1:
253 fname = env_get("bootfile");
254 break;
255 case 2:
256 /*
257 * Only one arg - accept two forms:
258 * Just load address, or just boot file name. The latter
259 * form must be written in a format which can not be
260 * mis-interpreted as a valid number.
261 */
262 addr = hextoul(argv[1], &end);
263 if (end == (argv[1] + strlen(argv[1]))) {
264 laddr = addr;
265 fname = env_get("bootfile");
266 } else {
267 arg = strdup(argv[1]);
268 }
269 break;
270 case 3:
271 laddr = hextoul(argv[1], NULL);
272 arg = strdup(argv[2]);
273 break;
274 default:
275 ret = CMD_RET_USAGE;
276 goto out;
277 }
278
279 if (!arg)
280 arg = net_boot_file_name;
281
282 if (arg) {
283 /* Parse [ip:[port:]]fname */
284 i = 0;
Jerome Forissier97083502024-11-07 12:27:57 +0100285 while ((*(words + i) = strsep(&arg, ":")))
Jerome Forissier6a78e962024-10-16 12:04:05 +0200286 i++;
287
288 switch (i) {
289 case 3:
290 server_ip = words[0];
291 server_port = words[1];
292 fname = words[2];
293 break;
294 case 2:
295 server_ip = words[0];
296 fname = words[1];
297 break;
298 case 1:
299 fname = words[0];
300 break;
301 default:
302 break;
303 }
304 }
305
306 if (!server_ip)
307 server_ip = env_get("tftpserverip");
308 if (!server_ip)
309 server_ip = env_get("serverip");
310 if (!server_ip) {
311 log_err("error: tftpserverip/serverip has to be set\n");
312 ret = CMD_RET_FAILURE;
313 goto out;
314 }
315
316 if (server_port)
317 port = dectoul(server_port, NULL);
318
319 if (!ipaddr_aton(server_ip, &srvip)) {
320 log_err("error: ipaddr_aton\n");
321 ret = CMD_RET_FAILURE;
322 goto out;
323 }
324
325 if (!fname) {
326 log_err("error: no file name\n");
327 ret = CMD_RET_FAILURE;
328 goto out;
329 }
330
331 if (!laddr) {
332 log_err("error: no load address\n");
333 ret = CMD_RET_FAILURE;
334 goto out;
335 }
336
Jerome Forissier86378a52025-04-15 23:17:36 +0200337 if (net_lwip_eth_start() < 0) {
338 ret = CMD_RET_FAILURE;
339 goto out;
340 }
Jerome Forissier6a78e962024-10-16 12:04:05 +0200341
342 if (tftp_loop(eth_get_dev(), laddr, fname, srvip, port) < 0)
343 ret = CMD_RET_FAILURE;
344out:
345 free(arg);
346 return ret;
Jerome Forissier1ff00362024-10-16 12:04:03 +0200347}