blob: fae701bad2eb3c667e8134341d1740a5531a0269 [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>
9#include <image.h>
10#include <linux/delay.h>
Jerome Forissier27ab7162025-04-17 15:27:00 +020011#include <linux/kconfig.h>
Jerome Forissier6a78e962024-10-16 12:04:05 +020012#include <lwip/apps/tftp_client.h>
13#include <lwip/timeouts.h>
14#include <mapmem.h>
15#include <net.h>
16#include <time.h>
17
18#define PROGRESS_PRINT_STEP_BYTES (10 * 1024)
Jerome Forissierf5702a42025-04-28 11:24:07 +020019/* Max time to wait for first data packet from server */
20#define NO_RSP_TIMEOUT_MS 10000
Jerome Forissier6a78e962024-10-16 12:04:05 +020021
22enum done_state {
23 NOT_DONE = 0,
24 SUCCESS,
25 FAILURE,
26 ABORTED
27};
28
29struct tftp_ctx {
30 ulong daddr;
31 ulong size;
32 ulong block_count;
33 ulong start_time;
34 enum done_state done;
35};
36
Jerome Forissier27ab7162025-04-17 15:27:00 +020037/**
38 * store_block() - copy received data
39 *
40 * This function is called by the receive callback to copy a block of data
41 * into its final location (ctx->daddr). Before doing so, it checks if the copy
42 * is allowed.
43 *
44 * @ctx: the context for the current transfer
45 * @src: the data received from the TCP stack
46 * @len: the length of the data
47 */
48static int store_block(struct tftp_ctx *ctx, void *src, u16_t len)
49{
50 ulong store_addr = ctx->daddr;
51 void *ptr;
52
53 if (CONFIG_IS_ENABLED(LMB)) {
54 if (store_addr + len < store_addr ||
55 lmb_read_check(store_addr, len)) {
56 puts("\nTFTP error: ");
57 puts("trying to overwrite reserved memory...\n");
58 return -1;
59 }
60 }
61
62 ptr = map_sysmem(store_addr, len);
63 memcpy(ptr, src, len);
64 unmap_sysmem(ptr);
65
66 ctx->daddr += len;
67 ctx->size += len;
68 ctx->block_count++;
69 if (ctx->block_count % 10 == 0) {
70 putc('#');
71 if (ctx->block_count % (65 * 10) == 0)
72 puts("\n\t ");
73 }
74
75 return 0;
76}
77
Jerome Forissier6a78e962024-10-16 12:04:05 +020078static void *tftp_open(const char *fname, const char *mode, u8_t is_write)
79{
80 return NULL;
81}
82
83static void tftp_close(void *handle)
84{
85 struct tftp_ctx *ctx = handle;
86 ulong elapsed;
87
88 if (ctx->done == FAILURE || ctx->done == ABORTED) {
89 /* Closing after an error or Ctrl-C */
90 return;
91 }
92 ctx->done = SUCCESS;
93
94 elapsed = get_timer(ctx->start_time);
95 if (elapsed > 0) {
96 puts("\n\t "); /* Line up with "Loading: " */
97 print_size(ctx->size / elapsed * 1000, "/s");
98 }
99 puts("\ndone\n");
100 printf("Bytes transferred = %lu (%lx hex)\n", ctx->size, ctx->size);
101
102 if (env_set_hex("filesize", ctx->size)) {
103 log_err("filesize not updated\n");
104 return;
105 }
106}
107
108static int tftp_read(void *handle, void *buf, int bytes)
109{
110 return 0;
111}
112
113static int tftp_write(void *handle, struct pbuf *p)
114{
115 struct tftp_ctx *ctx = handle;
116 struct pbuf *q;
117
Jerome Forissier27ab7162025-04-17 15:27:00 +0200118 for (q = p; q; q = q->next)
119 if (store_block(ctx, q->payload, q->len) < 0)
120 return -1;
Jerome Forissier6a78e962024-10-16 12:04:05 +0200121
122 return 0;
123}
124
125static void tftp_error(void *handle, int err, const char *msg, int size)
126{
127 struct tftp_ctx *ctx = handle;
128 char message[100];
129
130 ctx->done = FAILURE;
131 memset(message, 0, sizeof(message));
132 memcpy(message, msg, LWIP_MIN(sizeof(message) - 1, (size_t)size));
133
134 printf("\nTFTP error: %d (%s)\n", err, message);
135}
136
137static const struct tftp_context tftp_context = {
138 tftp_open,
139 tftp_close,
140 tftp_read,
141 tftp_write,
142 tftp_error
143};
144
Jerome Forissierf5702a42025-04-28 11:24:07 +0200145static void no_response(void *arg)
146{
147 struct tftp_ctx *ctx = (struct tftp_ctx *)arg;
148
149 if (ctx->size)
150 return;
151
152 printf("Timeout!\n");
153 ctx->done = FAILURE;
154}
155
Jerome Forissier6a78e962024-10-16 12:04:05 +0200156static int tftp_loop(struct udevice *udev, ulong addr, char *fname,
157 ip_addr_t srvip, uint16_t srvport)
158{
159 struct netif *netif;
160 struct tftp_ctx ctx;
161 err_t err;
162
163 if (!fname || addr == 0)
164 return -1;
165
166 if (!srvport)
167 srvport = TFTP_PORT;
168
169 netif = net_lwip_new_netif(udev);
170 if (!netif)
171 return -1;
172
173 ctx.done = NOT_DONE;
174 ctx.size = 0;
175 ctx.block_count = 0;
176 ctx.daddr = addr;
177
178 printf("Using %s device\n", udev->name);
179 printf("TFTP from server %s; our IP address is %s\n",
Jerome Forissier97083502024-11-07 12:27:57 +0100180 ip4addr_ntoa(&srvip), env_get("ipaddr"));
Jerome Forissier6a78e962024-10-16 12:04:05 +0200181 printf("Filename '%s'.\n", fname);
182 printf("Load address: 0x%lx\n", ctx.daddr);
183 printf("Loading: ");
184
185 err = tftp_init_client(&tftp_context);
186 if (!(err == ERR_OK || err == ERR_USE))
187 log_err("tftp_init_client err: %d\n", err);
188
Jerome Forissier0fb81902024-10-16 12:04:13 +0200189 tftp_client_set_blksize(CONFIG_TFTP_BLOCKSIZE);
190
Jerome Forissier6a78e962024-10-16 12:04:05 +0200191 ctx.start_time = get_timer(0);
192 err = tftp_get(&ctx, &srvip, srvport, fname, TFTP_MODE_OCTET);
193 /* might return different errors, like routing problems */
194 if (err != ERR_OK) {
195 printf("tftp_get() error %d\n", err);
196 net_lwip_remove_netif(netif);
197 return -1;
198 }
199
Jerome Forissierf5702a42025-04-28 11:24:07 +0200200 sys_timeout(NO_RSP_TIMEOUT_MS, no_response, &ctx);
Jerome Forissier6a78e962024-10-16 12:04:05 +0200201 while (!ctx.done) {
202 net_lwip_rx(udev, netif);
203 sys_check_timeouts();
204 if (ctrlc()) {
205 printf("\nAbort\n");
206 ctx.done = ABORTED;
207 break;
208 }
209 }
Jerome Forissierf5702a42025-04-28 11:24:07 +0200210 sys_untimeout(no_response, (void *)&ctx);
Jerome Forissier6a78e962024-10-16 12:04:05 +0200211
212 tftp_cleanup();
213
214 net_lwip_remove_netif(netif);
215
216 if (ctx.done == SUCCESS) {
217 if (env_set_hex("fileaddr", addr)) {
218 log_err("fileaddr not updated\n");
219 return -1;
220 }
221 efi_set_bootdev("Net", "", fname, map_sysmem(addr, 0),
222 ctx.size);
223 return 0;
224 }
225
226 return -1;
227}
Jerome Forissier1ff00362024-10-16 12:04:03 +0200228
229int do_tftpb(struct cmd_tbl *cmdtp, int flag, int argc, char *const argv[])
230{
Jerome Forissier6a78e962024-10-16 12:04:05 +0200231 int ret = CMD_RET_SUCCESS;
232 char *arg = NULL;
233 char *words[3] = { };
234 char *fname = NULL;
235 char *server_ip = NULL;
236 char *server_port = NULL;
237 char *end;
238 ip_addr_t srvip;
Jerome Forissier97083502024-11-07 12:27:57 +0100239 u16 port = TFTP_PORT;
Jerome Forissier6a78e962024-10-16 12:04:05 +0200240 ulong laddr;
241 ulong addr;
242 int i;
243
244 laddr = env_get_ulong("loadaddr", 16, image_load_addr);
245
246 switch (argc) {
247 case 1:
248 fname = env_get("bootfile");
249 break;
250 case 2:
251 /*
252 * Only one arg - accept two forms:
253 * Just load address, or just boot file name. The latter
254 * form must be written in a format which can not be
255 * mis-interpreted as a valid number.
256 */
257 addr = hextoul(argv[1], &end);
258 if (end == (argv[1] + strlen(argv[1]))) {
259 laddr = addr;
260 fname = env_get("bootfile");
261 } else {
262 arg = strdup(argv[1]);
263 }
264 break;
265 case 3:
266 laddr = hextoul(argv[1], NULL);
267 arg = strdup(argv[2]);
268 break;
269 default:
270 ret = CMD_RET_USAGE;
271 goto out;
272 }
273
274 if (!arg)
275 arg = net_boot_file_name;
276
277 if (arg) {
278 /* Parse [ip:[port:]]fname */
279 i = 0;
Jerome Forissier97083502024-11-07 12:27:57 +0100280 while ((*(words + i) = strsep(&arg, ":")))
Jerome Forissier6a78e962024-10-16 12:04:05 +0200281 i++;
282
283 switch (i) {
284 case 3:
285 server_ip = words[0];
286 server_port = words[1];
287 fname = words[2];
288 break;
289 case 2:
290 server_ip = words[0];
291 fname = words[1];
292 break;
293 case 1:
294 fname = words[0];
295 break;
296 default:
297 break;
298 }
299 }
300
301 if (!server_ip)
302 server_ip = env_get("tftpserverip");
303 if (!server_ip)
304 server_ip = env_get("serverip");
305 if (!server_ip) {
306 log_err("error: tftpserverip/serverip has to be set\n");
307 ret = CMD_RET_FAILURE;
308 goto out;
309 }
310
311 if (server_port)
312 port = dectoul(server_port, NULL);
313
314 if (!ipaddr_aton(server_ip, &srvip)) {
315 log_err("error: ipaddr_aton\n");
316 ret = CMD_RET_FAILURE;
317 goto out;
318 }
319
320 if (!fname) {
321 log_err("error: no file name\n");
322 ret = CMD_RET_FAILURE;
323 goto out;
324 }
325
326 if (!laddr) {
327 log_err("error: no load address\n");
328 ret = CMD_RET_FAILURE;
329 goto out;
330 }
331
Jerome Forissier86378a52025-04-15 23:17:36 +0200332 if (net_lwip_eth_start() < 0) {
333 ret = CMD_RET_FAILURE;
334 goto out;
335 }
Jerome Forissier6a78e962024-10-16 12:04:05 +0200336
337 if (tftp_loop(eth_get_dev(), laddr, fname, srvip, port) < 0)
338 ret = CMD_RET_FAILURE;
339out:
340 free(arg);
341 return ret;
Jerome Forissier1ff00362024-10-16 12:04:03 +0200342}