/src/barebox/commands/loadb.c
Line | Count | Source |
1 | | // SPDX-License-Identifier: GPL-2.0-or-later |
2 | | // SPDX-FileCopyrightText: © 2000-2004 Wolfgang Denk <wd@denx.de>, DENX Software Engineering |
3 | | /** |
4 | | * @file |
5 | | * @brief LoadB and LoadY support. |
6 | | * |
7 | | * Provides loadb (over Kermit) and LoadY(over Y modem) support to download |
8 | | * images. |
9 | | * |
10 | | * FileName: commands/loadb.c |
11 | | */ |
12 | | |
13 | | /* |
14 | | * Serial up- and download support |
15 | | */ |
16 | | #include <common.h> |
17 | | #include <command.h> |
18 | | #include <console.h> |
19 | | #include <errno.h> |
20 | | #include <libfile.h> |
21 | | #include <environment.h> |
22 | | #include <cache.h> |
23 | | #include <getopt.h> |
24 | | #include <fs.h> |
25 | | #include <fcntl.h> |
26 | | #include <malloc.h> |
27 | | |
28 | | #define XON_CHAR 17 |
29 | | #define XOFF_CHAR 19 |
30 | 0 | #define START_CHAR 0x01 |
31 | 0 | #define ETX_CHAR 0x03 |
32 | 0 | #define END_CHAR 0x0D |
33 | 0 | #define SPACE 0x20 |
34 | 0 | #define K_ESCAPE 0x23 |
35 | 0 | #define SEND_TYPE 'S' |
36 | 0 | #define DATA_TYPE 'D' |
37 | 0 | #define ACK_TYPE 'Y' |
38 | 0 | #define NACK_TYPE 'N' |
39 | 0 | #define BREAK_TYPE 'B' |
40 | 0 | #define tochar(x) ((char) (((x) + SPACE) & 0xff)) |
41 | 0 | #define untochar(x) ((int) (((x) - SPACE) & 0xff)) |
42 | 0 | #define DEF_FILE "image.bin" |
43 | | |
44 | | static int ofd; /* output file descriptor */ |
45 | | |
46 | | /* Size of my buffer to write to o/p file */ |
47 | 0 | #define MAX_WRITE_BUFFER 4096 /* Write size to o/p file */ |
48 | | static char *write_buffer; /* buffer for finalized data to write */ |
49 | | static int write_idx; /* index to the current location in buffer */ |
50 | | |
51 | | static char his_eol; /* character he needs at end of packet */ |
52 | | static int his_pad_count; /* number of pad chars he needs */ |
53 | | static char his_pad_char; /* pad chars he needs */ |
54 | | static char his_quote; /* quote chars he'll use */ |
55 | | static struct console_device *cdev; /* The console device we are using */ |
56 | | |
57 | | static void sendchar(char c) |
58 | 0 | { |
59 | 0 | cdev->putc(cdev, c); |
60 | 0 | } |
61 | | |
62 | | static int receivechar(void) |
63 | 0 | { |
64 | 0 | return cdev->getc(cdev); |
65 | 0 | } |
66 | | |
67 | | static void send_pad(void) |
68 | 0 | { |
69 | 0 | int count = his_pad_count; |
70 | |
|
71 | 0 | while (count-- > 0) |
72 | 0 | sendchar(his_pad_char); |
73 | 0 | } |
74 | | |
75 | | /* converts escaped kermit char to binary char */ |
76 | | static char ktrans(char in) |
77 | 0 | { |
78 | 0 | if ((in & 0x60) == 0x40) { |
79 | 0 | return (char)(in & ~0x40); |
80 | 0 | } else if ((in & 0x7f) == 0x3f) { |
81 | 0 | return (char)(in | 0x40); |
82 | 0 | } else |
83 | 0 | return in; |
84 | 0 | } |
85 | | |
86 | | static int chk1(char *buffer) |
87 | 0 | { |
88 | 0 | int total = 0; |
89 | |
|
90 | 0 | while (*buffer) { |
91 | 0 | total += *buffer++; |
92 | 0 | } |
93 | 0 | return (int)((total + ((total >> 6) & 0x03)) & 0x3f); |
94 | 0 | } |
95 | | |
96 | | static void s1_sendpacket(char *packet) |
97 | 0 | { |
98 | 0 | send_pad(); |
99 | 0 | while (*packet) |
100 | 0 | sendchar(*packet++); |
101 | 0 | } |
102 | | |
103 | | static char a_b[24]; |
104 | | static void send_ack(int n) |
105 | 0 | { |
106 | 0 | a_b[0] = START_CHAR; |
107 | 0 | a_b[1] = tochar(3); |
108 | 0 | a_b[2] = tochar(n); |
109 | 0 | a_b[3] = ACK_TYPE; |
110 | 0 | a_b[4] = '\0'; |
111 | 0 | a_b[4] = tochar(chk1(&a_b[1])); |
112 | 0 | a_b[5] = his_eol; |
113 | 0 | a_b[6] = '\0'; |
114 | 0 | s1_sendpacket(a_b); |
115 | 0 | } |
116 | | |
117 | | static void send_nack(int n) |
118 | 0 | { |
119 | 0 | a_b[0] = START_CHAR; |
120 | 0 | a_b[1] = tochar(3); |
121 | 0 | a_b[2] = tochar(n); |
122 | 0 | a_b[3] = NACK_TYPE; |
123 | 0 | a_b[4] = '\0'; |
124 | 0 | a_b[4] = tochar(chk1(&a_b[1])); |
125 | 0 | a_b[5] = his_eol; |
126 | 0 | a_b[6] = '\0'; |
127 | 0 | s1_sendpacket(a_b); |
128 | 0 | } |
129 | | |
130 | | /* os_data_* data buffer handling before flushing the data to the o/p device |
131 | | * we will only dump valid packets to the device. |
132 | | */ |
133 | | static int os_data_count, os_old_count, os_data_total; |
134 | | static char *os_data_addr, *os_old_buffer; |
135 | | |
136 | | static void os_data_init(void) |
137 | 0 | { |
138 | 0 | os_data_count = 0; |
139 | 0 | os_old_count = 0; |
140 | 0 | os_data_addr = NULL; |
141 | 0 | os_old_buffer = NULL; |
142 | 0 | } |
143 | | |
144 | | static int os_data_alloc(int length) |
145 | 0 | { |
146 | | /* If already allocated, free the memory |
147 | | */ |
148 | 0 | if (os_data_addr != NULL) { |
149 | 0 | os_data_addr -= os_data_count; |
150 | 0 | free(os_data_addr); |
151 | 0 | } |
152 | 0 | os_data_count = 0; |
153 | 0 | os_data_addr = (char *)malloc(length); |
154 | 0 | if (os_data_addr == NULL) |
155 | 0 | return -1; |
156 | 0 | return 0; |
157 | 0 | } |
158 | | |
159 | | static int os_data_save(void) |
160 | 0 | { |
161 | 0 | int ret = 0; |
162 | 0 | int copy_size; |
163 | | /* if there was an old data, flush it */ |
164 | 0 | if (os_old_buffer) { |
165 | 0 | char *ori_buf = os_old_buffer; |
166 | 0 | do { |
167 | 0 | if (write_idx == MAX_WRITE_BUFFER) { |
168 | 0 | ret = write(ofd, write_buffer, |
169 | 0 | MAX_WRITE_BUFFER); |
170 | 0 | if (ret < 0) { |
171 | 0 | eprintf("write to device failed\n"); |
172 | 0 | return ret; |
173 | 0 | } |
174 | 0 | write_idx = 0; |
175 | 0 | } |
176 | 0 | copy_size = min(os_old_count, (MAX_WRITE_BUFFER - |
177 | 0 | write_idx)); |
178 | 0 | memcpy(write_buffer + write_idx, os_old_buffer, |
179 | 0 | copy_size); |
180 | 0 | write_idx += copy_size; |
181 | 0 | os_data_total += copy_size; |
182 | 0 | os_old_count -= copy_size; |
183 | 0 | os_old_buffer += copy_size; |
184 | 0 | } while (os_old_count); /* if remaining bytes */ |
185 | 0 | free(ori_buf); |
186 | 0 | } |
187 | 0 | os_old_count = os_data_count; |
188 | 0 | os_old_buffer = os_data_addr - os_data_count; |
189 | 0 | os_data_count = 0; |
190 | 0 | os_data_addr = NULL; |
191 | 0 | return 0; |
192 | 0 | } |
193 | | |
194 | | static void os_data_dump(void) |
195 | 0 | { |
196 | 0 | if (os_old_buffer) |
197 | 0 | free(os_old_buffer); |
198 | 0 | if (os_data_addr) { |
199 | 0 | os_data_addr -= os_data_count; |
200 | 0 | free(os_data_addr); |
201 | 0 | } |
202 | 0 | os_data_init(); |
203 | 0 | } |
204 | | static void os_data_char(char new_char) |
205 | 0 | { |
206 | 0 | *os_data_addr++ = new_char; |
207 | 0 | os_data_count++; |
208 | 0 | } |
209 | | |
210 | | /* k_data_* simply handles the kermit escape translations */ |
211 | | static int k_data_escape, k_data_escape_saved; |
212 | | static void k_data_init(void) |
213 | 0 | { |
214 | 0 | k_data_escape = 0; |
215 | 0 | } |
216 | | static void k_data_save(void) |
217 | 0 | { |
218 | 0 | k_data_escape_saved = k_data_escape; |
219 | 0 | } |
220 | | static void k_data_restore(void) |
221 | 0 | { |
222 | 0 | k_data_escape = k_data_escape_saved; |
223 | 0 | } |
224 | | static void k_data_char(char new_char) |
225 | 0 | { |
226 | 0 | if (k_data_escape) { |
227 | | /* last char was escape - translate this character */ |
228 | 0 | os_data_char(ktrans(new_char)); |
229 | 0 | k_data_escape = 0; |
230 | 0 | } else { |
231 | 0 | if (new_char == his_quote) { |
232 | | /* this char is escape - remember */ |
233 | 0 | k_data_escape = 1; |
234 | 0 | } else { |
235 | | /* otherwise send this char as-is */ |
236 | 0 | os_data_char(new_char); |
237 | 0 | } |
238 | 0 | } |
239 | 0 | } |
240 | | |
241 | 0 | #define SEND_DATA_SIZE 20 |
242 | | static char send_parms[SEND_DATA_SIZE]; |
243 | | static char *send_ptr; |
244 | | |
245 | | /** |
246 | | * @brief interprets the protocol info and builds and |
247 | | * sends an appropriate ack for what we can do |
248 | | * |
249 | | * @param n sequence |
250 | | * |
251 | | * @return void |
252 | | */ |
253 | | static void handle_send_packet(int n) |
254 | 0 | { |
255 | 0 | int length = 3; |
256 | 0 | int bytes; |
257 | | |
258 | | /* initialize some protocol parameters */ |
259 | 0 | his_eol = END_CHAR; /* default end of line character */ |
260 | 0 | his_pad_count = 0; |
261 | 0 | his_pad_char = '\0'; |
262 | 0 | his_quote = K_ESCAPE; |
263 | | |
264 | | /* ignore last character if it filled the buffer */ |
265 | 0 | if (send_ptr == &send_parms[SEND_DATA_SIZE - 1]) |
266 | 0 | --send_ptr; |
267 | 0 | bytes = send_ptr - send_parms; /* how many bytes we'll process */ |
268 | 0 | do { |
269 | 0 | if (bytes-- <= 0) |
270 | 0 | break; |
271 | | /* handle MAXL - max length */ |
272 | | /* ignore what he says - most I'll take (here) is 94 */ |
273 | 0 | a_b[++length] = tochar(94); |
274 | 0 | if (bytes-- <= 0) |
275 | 0 | break; |
276 | | /* handle TIME - time you should wait for my packets */ |
277 | | /* ignore what he says - don't wait for my ack longer than 1 second */ |
278 | 0 | a_b[++length] = tochar(1); |
279 | 0 | if (bytes-- <= 0) |
280 | 0 | break; |
281 | | /* handle NPAD - number of pad chars I need */ |
282 | | /* remember what he says - I need none */ |
283 | 0 | his_pad_count = untochar(send_parms[2]); |
284 | 0 | a_b[++length] = tochar(0); |
285 | 0 | if (bytes-- <= 0) |
286 | 0 | break; |
287 | | /* handle PADC - pad chars I need */ |
288 | | /* remember what he says - I need none */ |
289 | 0 | his_pad_char = ktrans(send_parms[3]); |
290 | 0 | a_b[++length] = 0x40; /* He should ignore this */ |
291 | 0 | if (bytes-- <= 0) |
292 | 0 | break; |
293 | | /* handle EOL - end of line he needs */ |
294 | | /* remember what he says - I need CR */ |
295 | 0 | his_eol = untochar(send_parms[4]); |
296 | 0 | a_b[++length] = tochar(END_CHAR); |
297 | 0 | if (bytes-- <= 0) |
298 | 0 | break; |
299 | | /* handle QCTL - quote control char he'll use */ |
300 | | /* remember what he says - I'll use '#' */ |
301 | 0 | his_quote = send_parms[5]; |
302 | 0 | a_b[++length] = '#'; |
303 | 0 | if (bytes-- <= 0) |
304 | 0 | break; |
305 | | /* handle QBIN - 8-th bit prefixing */ |
306 | | /* ignore what he says - I refuse */ |
307 | 0 | a_b[++length] = 'N'; |
308 | 0 | if (bytes-- <= 0) |
309 | 0 | break; |
310 | | /* handle CHKT - the clock check type */ |
311 | | /* ignore what he says - I do type 1 (for now) */ |
312 | 0 | a_b[++length] = '1'; |
313 | 0 | if (bytes-- <= 0) |
314 | 0 | break; |
315 | | /* handle REPT - the repeat prefix */ |
316 | | /* ignore what he says - I refuse (for now) */ |
317 | 0 | a_b[++length] = 'N'; |
318 | 0 | if (bytes-- <= 0) |
319 | 0 | break; |
320 | | /* handle CAPAS - the capabilities mask */ |
321 | | /* ignore what he says - I only do long packets - I don't do windows */ |
322 | 0 | a_b[++length] = tochar(2); /* only long packets */ |
323 | 0 | a_b[++length] = tochar(0); /* no windows */ |
324 | 0 | a_b[++length] = tochar(94); /* large packet msb */ |
325 | 0 | a_b[++length] = tochar(94); /* large packet lsb */ |
326 | 0 | } while (0); |
327 | |
|
328 | 0 | a_b[0] = START_CHAR; |
329 | 0 | a_b[1] = tochar(length); |
330 | 0 | a_b[2] = tochar(n); |
331 | 0 | a_b[3] = ACK_TYPE; |
332 | 0 | a_b[++length] = '\0'; |
333 | 0 | a_b[length] = tochar(chk1(&a_b[1])); |
334 | 0 | a_b[++length] = his_eol; |
335 | 0 | a_b[++length] = '\0'; |
336 | 0 | s1_sendpacket(a_b); |
337 | 0 | } |
338 | | |
339 | | /** |
340 | | * @brief receives a OS Open image file over kermit line |
341 | | * |
342 | | * @return bytes read |
343 | | */ |
344 | | static int k_recv(void) |
345 | 0 | { |
346 | 0 | char new_char; |
347 | 0 | char k_state, k_state_saved; |
348 | 0 | int sum; |
349 | 0 | int done; |
350 | 0 | int length; |
351 | 0 | int n, last_n; |
352 | 0 | int len_lo, len_hi; |
353 | | |
354 | | /* initialize some protocol parameters */ |
355 | 0 | his_eol = END_CHAR; /* default end of line character */ |
356 | 0 | his_pad_count = 0; |
357 | 0 | his_pad_char = '\0'; |
358 | 0 | his_quote = K_ESCAPE; |
359 | | |
360 | | /* initialize the k_recv and k_data state machine */ |
361 | 0 | done = 0; |
362 | 0 | k_state = 0; |
363 | 0 | k_data_init(); |
364 | 0 | k_state_saved = k_state; |
365 | 0 | k_data_save(); |
366 | 0 | os_data_init(); |
367 | 0 | os_data_total = 0; |
368 | 0 | n = 0; /* just to get rid of a warning */ |
369 | 0 | last_n = -1; |
370 | | |
371 | | /* expect this "type" sequence (but don't check): |
372 | | S: send initiate |
373 | | F: file header |
374 | | D: data (multiple) |
375 | | Z: end of file |
376 | | B: break transmission |
377 | | */ |
378 | | |
379 | | /* enter main loop */ |
380 | 0 | while (!done) { |
381 | | /* set the send packet pointer to begining of send packet parms */ |
382 | 0 | send_ptr = send_parms; |
383 | | |
384 | | /* With each packet, start summing the bytes starting with the length. |
385 | | Save the current sequence number. |
386 | | Note the type of the packet. |
387 | | If a character less than SPACE (0x20) is received - error. |
388 | | */ |
389 | | |
390 | | /* get a packet */ |
391 | | /* wait for the starting character or ^C */ |
392 | 0 | for (;;) { |
393 | 0 | switch (receivechar()) { |
394 | 0 | case START_CHAR: /* start packet */ |
395 | 0 | goto START; |
396 | 0 | case ETX_CHAR: /* ^C waiting for packet */ |
397 | | /* Save last success buffer and quit */ |
398 | 0 | (void)os_data_save(); |
399 | 0 | return (0); |
400 | 0 | default: |
401 | 0 | ; |
402 | 0 | } |
403 | 0 | } |
404 | 0 | START: |
405 | | /* get length of packet */ |
406 | 0 | sum = 0; |
407 | 0 | new_char = receivechar(); |
408 | 0 | if ((new_char & 0xE0) == 0) |
409 | 0 | goto packet_error; |
410 | 0 | sum += new_char & 0xff; |
411 | 0 | length = untochar(new_char); |
412 | | /* get sequence number */ |
413 | 0 | new_char = receivechar(); |
414 | 0 | if ((new_char & 0xE0) == 0) |
415 | 0 | goto packet_error; |
416 | 0 | sum += new_char & 0xff; |
417 | 0 | n = untochar(new_char); |
418 | 0 | --length; |
419 | | |
420 | | /* NEW CODE - check sequence numbers for retried packets */ |
421 | | /* Note - this new code assumes that the sequence number is correctly |
422 | | * received. Handling an invalid sequence number adds another layer |
423 | | * of complexity that may not be needed - yet! At this time, I'm hoping |
424 | | * that I don't need to buffer the incoming data packets and can write |
425 | | * the data into memory in real time. |
426 | | */ |
427 | 0 | if (n == last_n) { |
428 | | /* same sequence number, restore the previous state */ |
429 | 0 | k_state = k_state_saved; |
430 | 0 | k_data_restore(); |
431 | | /* Dump any previously allocated data including previous |
432 | | * buffer host wishes to retry.. */ |
433 | 0 | os_data_dump(); |
434 | 0 | } else { |
435 | | /* new sequence number, checkpoint the download */ |
436 | 0 | last_n = n; |
437 | 0 | k_state_saved = k_state; |
438 | 0 | k_data_save(); |
439 | 0 | } |
440 | | /* END NEW CODE */ |
441 | | |
442 | | /* get packet type */ |
443 | 0 | new_char = receivechar(); |
444 | 0 | if ((new_char & 0xE0) == 0) |
445 | 0 | goto packet_error; |
446 | 0 | sum += new_char & 0xff; |
447 | 0 | k_state = new_char; |
448 | 0 | --length; |
449 | | /* check for extended length */ |
450 | 0 | if (length == -2) { |
451 | | /* (length byte was 0, decremented twice) */ |
452 | | /* get the two length bytes */ |
453 | 0 | new_char = receivechar(); |
454 | 0 | if ((new_char & 0xE0) == 0) |
455 | 0 | goto packet_error; |
456 | 0 | sum += new_char & 0xff; |
457 | 0 | len_hi = untochar(new_char); |
458 | 0 | new_char = receivechar(); |
459 | 0 | if ((new_char & 0xE0) == 0) |
460 | 0 | goto packet_error; |
461 | 0 | sum += new_char & 0xff; |
462 | 0 | len_lo = untochar(new_char); |
463 | 0 | length = len_hi * 95 + len_lo; |
464 | | /* check header checksum */ |
465 | 0 | new_char = receivechar(); |
466 | 0 | if ((new_char & 0xE0) == 0) |
467 | 0 | goto packet_error; |
468 | 0 | if (new_char != |
469 | 0 | tochar((sum + ((sum >> 6) & 0x03)) & 0x3f)) |
470 | 0 | goto packet_error; |
471 | 0 | sum += new_char & 0xff; |
472 | | /* --length; |
473 | | * new length includes only data and block check to come |
474 | | */ |
475 | 0 | } |
476 | | /* Try to allocate data chunk */ |
477 | 0 | if (length > 1) { |
478 | 0 | if (os_data_alloc(length) < 0) { |
479 | | /* too large.. NACK the data back */ |
480 | 0 | goto packet_error; |
481 | 0 | } |
482 | 0 | } |
483 | 0 | while (length > 1) { |
484 | 0 | new_char = receivechar(); |
485 | 0 | if ((new_char & 0xE0) == 0) |
486 | 0 | goto packet_error; |
487 | 0 | sum += new_char & 0xff; |
488 | 0 | --length; |
489 | 0 | if (k_state == DATA_TYPE) { |
490 | | /* pass on the data if this is a data packet */ |
491 | 0 | k_data_char(new_char); |
492 | 0 | } else if (k_state == SEND_TYPE) { |
493 | | /* save send pack in buffer as is */ |
494 | 0 | *send_ptr++ = new_char; |
495 | | /* if too much data, back off the pointer */ |
496 | 0 | if (send_ptr >= &send_parms[SEND_DATA_SIZE]) |
497 | 0 | --send_ptr; |
498 | 0 | } |
499 | 0 | } |
500 | | /* get and validate checksum character */ |
501 | 0 | new_char = receivechar(); |
502 | 0 | if ((new_char & 0xE0) == 0) |
503 | 0 | goto packet_error; |
504 | 0 | if (new_char != tochar((sum + ((sum >> 6) & 0x03)) & 0x3f)) |
505 | 0 | goto packet_error; |
506 | | /* get END_CHAR */ |
507 | 0 | new_char = receivechar(); |
508 | 0 | if (new_char != END_CHAR) { |
509 | 0 | packet_error: |
510 | | /* restore state machines */ |
511 | 0 | k_state = k_state_saved; |
512 | 0 | k_data_restore(); |
513 | | /* send a negative acknowledge packet in */ |
514 | 0 | send_nack(n); |
515 | 0 | } else if (k_state == SEND_TYPE) { |
516 | | /* crack the protocol parms, build an appropriate ack packet */ |
517 | 0 | handle_send_packet(n); |
518 | 0 | } else { |
519 | | /* send simple acknowledge packet in */ |
520 | 0 | send_ack(n); |
521 | | /* Flush old buffer, |
522 | | * Store current buffer as old buffer */ |
523 | 0 | os_data_save(); |
524 | | /* quit if end of transmission */ |
525 | 0 | if (k_state == BREAK_TYPE) { |
526 | | /* Flush remaining buffer */ |
527 | 0 | os_data_save(); |
528 | 0 | done = 1; |
529 | 0 | } |
530 | 0 | } |
531 | 0 | } |
532 | 0 | return ((ulong) os_data_total); |
533 | 0 | } |
534 | | |
535 | | /** |
536 | | * @brief loadb Support over kermit protocol |
537 | | * |
538 | | * @return download size |
539 | | */ |
540 | | static ulong load_serial_bin(void) |
541 | 0 | { |
542 | 0 | int size, i; |
543 | | |
544 | | /* Try to allocate the buffer we shall write to files */ |
545 | 0 | write_buffer = malloc(MAX_WRITE_BUFFER); |
546 | 0 | if (write_buffer == NULL) { |
547 | 0 | eprintf("could not allocate file i/o buffer\n"); |
548 | 0 | return -ENOMEM; |
549 | 0 | } |
550 | | |
551 | | /* Lets get the image from host */ |
552 | 0 | size = k_recv(); |
553 | | |
554 | | /* |
555 | | * Gather any trailing characters (for instance, the ^D which |
556 | | * is sent by 'cu' after sending a file), and give the |
557 | | * box some time (100 * 1 ms) |
558 | | */ |
559 | 0 | for (i = 0; i < 100; ++i) { |
560 | 0 | if (cdev->tstc(cdev)) |
561 | 0 | (void)receivechar();; |
562 | 0 | udelay(1000); |
563 | 0 | } |
564 | | |
565 | | /* Flush out the remaining data if any */ |
566 | 0 | if (write_idx > 0) { |
567 | 0 | i = write(ofd, write_buffer, write_idx); |
568 | 0 | if (i < 0) { |
569 | 0 | eprintf("write to device failed\n"); |
570 | 0 | size = i; |
571 | 0 | goto err_quit; |
572 | 0 | } |
573 | 0 | write_idx = 0; |
574 | 0 | } |
575 | 0 | printf("## Total Size = 0x%08x = %d Bytes\n", size, size); |
576 | 0 | pr_setenv("filesize", "%X", size); |
577 | |
|
578 | 0 | err_quit: |
579 | 0 | free(write_buffer); |
580 | 0 | return size; |
581 | 0 | } |
582 | | |
583 | | /** |
584 | | * @brief provide the loadb(Kermit) or loadY mode support |
585 | | * |
586 | | * @param cmdtp |
587 | | * @param argc |
588 | | * @param argv |
589 | | * |
590 | | * @return success or failure |
591 | | */ |
592 | | static int do_load_serial_bin(int argc, char *argv[]) |
593 | 0 | { |
594 | 0 | ulong offset = 0; |
595 | 0 | ulong addr; |
596 | 0 | int load_baudrate = 0, current_baudrate; |
597 | 0 | int rcode = 0, ret; |
598 | 0 | int opt; |
599 | 0 | char *output_file = NULL; |
600 | 0 | int current_active; |
601 | 0 | char *console_dev_name = NULL; |
602 | |
|
603 | 0 | while ((opt = getopt(argc, argv, "f:b:o:c:")) > 0) { |
604 | 0 | switch (opt) { |
605 | 0 | case 'f': |
606 | 0 | output_file = optarg; |
607 | 0 | break; |
608 | 0 | case 'b': |
609 | 0 | load_baudrate = (int)simple_strtoul(optarg, NULL, 10); |
610 | 0 | break; |
611 | 0 | case 'o': |
612 | 0 | offset = (int)simple_strtoul(optarg, NULL, 10); |
613 | 0 | break; |
614 | 0 | case 'c': |
615 | 0 | console_dev_name = optarg; |
616 | 0 | break; |
617 | 0 | default: |
618 | 0 | return COMMAND_ERROR_USAGE; |
619 | 0 | } |
620 | 0 | } |
621 | | |
622 | 0 | if (console_dev_name) { |
623 | 0 | cdev = console_get_by_name(console_dev_name); |
624 | 0 | if (!cdev) { |
625 | 0 | printf("Console %s not found\n", console_dev_name); |
626 | 0 | return -ENODEV; |
627 | 0 | } |
628 | 0 | } else { |
629 | 0 | cdev = console_get_first_interactive(); |
630 | 0 | if (!cdev) { |
631 | 0 | printf("No console device with STDIN and STDOUT\n"); |
632 | 0 | return -ENODEV; |
633 | 0 | } |
634 | 0 | } |
635 | 0 | current_baudrate = console_get_baudrate(cdev); |
636 | 0 | current_active = console_get_active(cdev); |
637 | | |
638 | | /* Load Defaults */ |
639 | 0 | if (load_baudrate == 0) |
640 | 0 | load_baudrate = current_baudrate; |
641 | 0 | if (NULL == output_file) |
642 | 0 | output_file = DEF_FILE; |
643 | | |
644 | | /* File should exist */ |
645 | 0 | ofd = open_and_lseek(output_file, O_WRONLY | O_CREAT, offset); |
646 | 0 | if (ofd < 0) { |
647 | 0 | printf("Could not open \"%s\": %m\n", output_file); |
648 | 0 | return 3; |
649 | 0 | } |
650 | | |
651 | 0 | printf("## Ready for binary (kermit) download " |
652 | 0 | "to 0x%08lX offset on %s device at %d bps...\n", offset, |
653 | 0 | output_file, load_baudrate); |
654 | |
|
655 | 0 | console_set_active(cdev, 0); |
656 | 0 | ret = console_set_baudrate(cdev, load_baudrate); |
657 | 0 | if (ret) |
658 | 0 | return ret; |
659 | | |
660 | 0 | addr = load_serial_bin(); |
661 | 0 | if (addr == 0) { |
662 | 0 | printf("## Binary (kermit) download aborted\n"); |
663 | 0 | rcode = 1; |
664 | 0 | } |
665 | |
|
666 | 0 | ret = console_set_baudrate(cdev, current_baudrate); |
667 | 0 | if (ret) |
668 | 0 | return ret; |
669 | | |
670 | 0 | console_set_active(cdev, current_active); |
671 | |
|
672 | 0 | close(ofd); |
673 | 0 | ofd = 0; |
674 | 0 | return rcode; |
675 | 0 | } |
676 | | |
677 | | BAREBOX_CMD_HELP_START(loadb) |
678 | | BAREBOX_CMD_HELP_TEXT("") |
679 | | BAREBOX_CMD_HELP_TEXT("Options:") |
680 | | BAREBOX_CMD_HELP_OPT("-f FILE", "download to FILE (default image.bin)") |
681 | | BAREBOX_CMD_HELP_OPT("-o OFFS", "destination file OFFSet (default 0)") |
682 | | BAREBOX_CMD_HELP_OPT("-b BAUD", "baudrate for download (default: console baudrate)") |
683 | | BAREBOX_CMD_HELP_OPT("-c CONSOLE", "Specify console (default: first active console)") |
684 | | BAREBOX_CMD_HELP_END |
685 | | |
686 | | BAREBOX_CMD_START(loadb) |
687 | | .cmd = do_load_serial_bin, |
688 | | BAREBOX_CMD_DESC("load binary file over serial line (Kermit)") |
689 | | BAREBOX_CMD_OPTS(" FILE") |
690 | | BAREBOX_CMD_GROUP(CMD_GRP_BOOT) |
691 | | BAREBOX_CMD_HELP(cmd_loadb_help) |
692 | | BAREBOX_CMD_END |