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