blob: 279145227224559255b4245cf1490485c6d69b39 [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{
160 struct netif *netif;
161 struct tftp_ctx ctx;
162 err_t err;
163
164 if (!fname || addr == 0)
165 return -1;
166
167 if (!srvport)
168 srvport = TFTP_PORT;
169
170 netif = net_lwip_new_netif(udev);
171 if (!netif)
172 return -1;
173
174 ctx.done = NOT_DONE;
175 ctx.size = 0;
176 ctx.block_count = 0;
177 ctx.daddr = addr;
178
179 printf("Using %s device\n", udev->name);
180 printf("TFTP from server %s; our IP address is %s\n",
Jerome Forissier97083502024-11-07 12:27:57 +0100181 ip4addr_ntoa(&srvip), env_get("ipaddr"));
Jerome Forissier6a78e962024-10-16 12:04:05 +0200182 printf("Filename '%s'.\n", fname);
183 printf("Load address: 0x%lx\n", ctx.daddr);
184 printf("Loading: ");
185
186 err = tftp_init_client(&tftp_context);
187 if (!(err == ERR_OK || err == ERR_USE))
188 log_err("tftp_init_client err: %d\n", err);
189
Jerome Forissier0fb81902024-10-16 12:04:13 +0200190 tftp_client_set_blksize(CONFIG_TFTP_BLOCKSIZE);
191
Jerome Forissier6a78e962024-10-16 12:04:05 +0200192 ctx.start_time = get_timer(0);
193 err = tftp_get(&ctx, &srvip, srvport, fname, TFTP_MODE_OCTET);
194 /* might return different errors, like routing problems */
195 if (err != ERR_OK) {
196 printf("tftp_get() error %d\n", err);
197 net_lwip_remove_netif(netif);
198 return -1;
199 }
200
Jerome Forissierf5702a42025-04-28 11:24:07 +0200201 sys_timeout(NO_RSP_TIMEOUT_MS, no_response, &ctx);
Jerome Forissier6a78e962024-10-16 12:04:05 +0200202 while (!ctx.done) {
203 net_lwip_rx(udev, netif);
Jerome Forissier6a78e962024-10-16 12:04:05 +0200204 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}