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