blob: 56d27f8212122eaa9c41c944b81ecb3ded58053f [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>
12#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
35static void *tftp_open(const char *fname, const char *mode, u8_t is_write)
36{
37 return NULL;
38}
39
40static void tftp_close(void *handle)
41{
42 struct tftp_ctx *ctx = handle;
43 ulong elapsed;
44
45 if (ctx->done == FAILURE || ctx->done == ABORTED) {
46 /* Closing after an error or Ctrl-C */
47 return;
48 }
49 ctx->done = SUCCESS;
50
51 elapsed = get_timer(ctx->start_time);
52 if (elapsed > 0) {
53 puts("\n\t "); /* Line up with "Loading: " */
54 print_size(ctx->size / elapsed * 1000, "/s");
55 }
56 puts("\ndone\n");
57 printf("Bytes transferred = %lu (%lx hex)\n", ctx->size, ctx->size);
58
59 if (env_set_hex("filesize", ctx->size)) {
60 log_err("filesize not updated\n");
61 return;
62 }
63}
64
65static int tftp_read(void *handle, void *buf, int bytes)
66{
67 return 0;
68}
69
70static int tftp_write(void *handle, struct pbuf *p)
71{
72 struct tftp_ctx *ctx = handle;
73 struct pbuf *q;
74
Jerome Forissier97083502024-11-07 12:27:57 +010075 for (q = p; q; q = q->next) {
Jerome Forissier6a78e962024-10-16 12:04:05 +020076 memcpy((void *)ctx->daddr, q->payload, q->len);
77 ctx->daddr += q->len;
78 ctx->size += q->len;
79 ctx->block_count++;
80 if (ctx->block_count % 10 == 0) {
81 putc('#');
82 if (ctx->block_count % (65 * 10) == 0)
83 puts("\n\t ");
84 }
85 }
86
87 return 0;
88}
89
90static void tftp_error(void *handle, int err, const char *msg, int size)
91{
92 struct tftp_ctx *ctx = handle;
93 char message[100];
94
95 ctx->done = FAILURE;
96 memset(message, 0, sizeof(message));
97 memcpy(message, msg, LWIP_MIN(sizeof(message) - 1, (size_t)size));
98
99 printf("\nTFTP error: %d (%s)\n", err, message);
100}
101
102static const struct tftp_context tftp_context = {
103 tftp_open,
104 tftp_close,
105 tftp_read,
106 tftp_write,
107 tftp_error
108};
109
110static int tftp_loop(struct udevice *udev, ulong addr, char *fname,
111 ip_addr_t srvip, uint16_t srvport)
112{
113 struct netif *netif;
114 struct tftp_ctx ctx;
115 err_t err;
116
117 if (!fname || addr == 0)
118 return -1;
119
120 if (!srvport)
121 srvport = TFTP_PORT;
122
123 netif = net_lwip_new_netif(udev);
124 if (!netif)
125 return -1;
126
127 ctx.done = NOT_DONE;
128 ctx.size = 0;
129 ctx.block_count = 0;
130 ctx.daddr = addr;
131
132 printf("Using %s device\n", udev->name);
133 printf("TFTP from server %s; our IP address is %s\n",
Jerome Forissier97083502024-11-07 12:27:57 +0100134 ip4addr_ntoa(&srvip), env_get("ipaddr"));
Jerome Forissier6a78e962024-10-16 12:04:05 +0200135 printf("Filename '%s'.\n", fname);
136 printf("Load address: 0x%lx\n", ctx.daddr);
137 printf("Loading: ");
138
139 err = tftp_init_client(&tftp_context);
140 if (!(err == ERR_OK || err == ERR_USE))
141 log_err("tftp_init_client err: %d\n", err);
142
Jerome Forissier0fb81902024-10-16 12:04:13 +0200143 tftp_client_set_blksize(CONFIG_TFTP_BLOCKSIZE);
144
Jerome Forissier6a78e962024-10-16 12:04:05 +0200145 ctx.start_time = get_timer(0);
146 err = tftp_get(&ctx, &srvip, srvport, fname, TFTP_MODE_OCTET);
147 /* might return different errors, like routing problems */
148 if (err != ERR_OK) {
149 printf("tftp_get() error %d\n", err);
150 net_lwip_remove_netif(netif);
151 return -1;
152 }
153
154 while (!ctx.done) {
155 net_lwip_rx(udev, netif);
156 sys_check_timeouts();
157 if (ctrlc()) {
158 printf("\nAbort\n");
159 ctx.done = ABORTED;
160 break;
161 }
162 }
163
164 tftp_cleanup();
165
166 net_lwip_remove_netif(netif);
167
168 if (ctx.done == SUCCESS) {
169 if (env_set_hex("fileaddr", addr)) {
170 log_err("fileaddr not updated\n");
171 return -1;
172 }
173 efi_set_bootdev("Net", "", fname, map_sysmem(addr, 0),
174 ctx.size);
175 return 0;
176 }
177
178 return -1;
179}
Jerome Forissier1ff00362024-10-16 12:04:03 +0200180
181int do_tftpb(struct cmd_tbl *cmdtp, int flag, int argc, char *const argv[])
182{
Jerome Forissier6a78e962024-10-16 12:04:05 +0200183 int ret = CMD_RET_SUCCESS;
184 char *arg = NULL;
185 char *words[3] = { };
186 char *fname = NULL;
187 char *server_ip = NULL;
188 char *server_port = NULL;
189 char *end;
190 ip_addr_t srvip;
Jerome Forissier97083502024-11-07 12:27:57 +0100191 u16 port = TFTP_PORT;
Jerome Forissier6a78e962024-10-16 12:04:05 +0200192 ulong laddr;
193 ulong addr;
194 int i;
195
196 laddr = env_get_ulong("loadaddr", 16, image_load_addr);
197
198 switch (argc) {
199 case 1:
200 fname = env_get("bootfile");
201 break;
202 case 2:
203 /*
204 * Only one arg - accept two forms:
205 * Just load address, or just boot file name. The latter
206 * form must be written in a format which can not be
207 * mis-interpreted as a valid number.
208 */
209 addr = hextoul(argv[1], &end);
210 if (end == (argv[1] + strlen(argv[1]))) {
211 laddr = addr;
212 fname = env_get("bootfile");
213 } else {
214 arg = strdup(argv[1]);
215 }
216 break;
217 case 3:
218 laddr = hextoul(argv[1], NULL);
219 arg = strdup(argv[2]);
220 break;
221 default:
222 ret = CMD_RET_USAGE;
223 goto out;
224 }
225
226 if (!arg)
227 arg = net_boot_file_name;
228
229 if (arg) {
230 /* Parse [ip:[port:]]fname */
231 i = 0;
Jerome Forissier97083502024-11-07 12:27:57 +0100232 while ((*(words + i) = strsep(&arg, ":")))
Jerome Forissier6a78e962024-10-16 12:04:05 +0200233 i++;
234
235 switch (i) {
236 case 3:
237 server_ip = words[0];
238 server_port = words[1];
239 fname = words[2];
240 break;
241 case 2:
242 server_ip = words[0];
243 fname = words[1];
244 break;
245 case 1:
246 fname = words[0];
247 break;
248 default:
249 break;
250 }
251 }
252
253 if (!server_ip)
254 server_ip = env_get("tftpserverip");
255 if (!server_ip)
256 server_ip = env_get("serverip");
257 if (!server_ip) {
258 log_err("error: tftpserverip/serverip has to be set\n");
259 ret = CMD_RET_FAILURE;
260 goto out;
261 }
262
263 if (server_port)
264 port = dectoul(server_port, NULL);
265
266 if (!ipaddr_aton(server_ip, &srvip)) {
267 log_err("error: ipaddr_aton\n");
268 ret = CMD_RET_FAILURE;
269 goto out;
270 }
271
272 if (!fname) {
273 log_err("error: no file name\n");
274 ret = CMD_RET_FAILURE;
275 goto out;
276 }
277
278 if (!laddr) {
279 log_err("error: no load address\n");
280 ret = CMD_RET_FAILURE;
281 goto out;
282 }
283
Jerome Forissier86378a52025-04-15 23:17:36 +0200284 if (net_lwip_eth_start() < 0) {
285 ret = CMD_RET_FAILURE;
286 goto out;
287 }
Jerome Forissier6a78e962024-10-16 12:04:05 +0200288
289 if (tftp_loop(eth_get_dev(), laddr, fname, srvip, port) < 0)
290 ret = CMD_RET_FAILURE;
291out:
292 free(arg);
293 return ret;
Jerome Forissier1ff00362024-10-16 12:04:03 +0200294}