summaryrefslogtreecommitdiffstats
path: root/commands/loadxy.c
blob: 2bfe482fc50a69e450ded56717ad89073fa1942c (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
/**
 * @file
 * @brief loady and loadx  support.
 *
 * Provides loadx (over X-Modem) and loady(over Y-Modem) support to download
 * images.
 *
 * FileName: commands/loadxy.c
 */
/*
 * (C) Copyright 2012 Robert Jarzmik <robert.jarzmik@free.fr>
 *
 * See file CREDITS for list of people who contributed to this
 * project.
 *
 * This program is free software; you can redistribute it and/or
 * modify it under the terms of the GNU General Public License as
 * published by the Free Software Foundation; either version 2 of
 * the License, or (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 */

/*
 * Serial up- and download support
 */
#include <common.h>
#include <command.h>
#include <console.h>
#include <xymodem.h>
#include <errno.h>
#include <getopt.h>
#include <fcntl.h>
#include <fs.h>
#include <malloc.h>

#define DEF_FILE	"image.bin"

/**
 * @brief provide the loady(Y-Modem or Y-Modem/G) support
 *
 * @param argc number of arguments
 * @param argv arguments of loady command
 *
 * @return success or failure
 */
static int do_loady(int argc, char *argv[])
{
	int is_ymodemg = 0, rc = 0, opt, rcode = 0;
	int load_baudrate = 0, current_baudrate;
	char *cname = NULL;
	struct console_device *cdev = NULL;

	while ((opt = getopt(argc, argv, "b:t:g")) > 0) {
		switch (opt) {
		case 'b':
			load_baudrate = (int)simple_strtoul(optarg, NULL, 10);
			break;
		case 'g':
			is_ymodemg = 1;
			break;
		case 't':
			cname = optarg;
			break;
		default:
			return COMMAND_ERROR_USAGE;
		}
	}

	if (cname)
		cdev = console_get_by_name(cname);
	else
		cdev = console_get_first_active();
	if (!cdev) {
		printf("%s:No console device %s with STDIN and STDOUT\n",
		       argv[0], cname ? cname : "default");
		return -ENODEV;
	}

	current_baudrate = console_get_baudrate(cdev);

	if (!load_baudrate)
		load_baudrate = current_baudrate;

	rc = console_set_baudrate(cdev, load_baudrate);
	if (rc)
		return rc;

	printf("## Ready for binary (ymodem) download at %d bps...\n",
	       load_baudrate ? load_baudrate : current_baudrate);

	if (is_ymodemg)
		rc = do_load_serial_ymodemg(cdev);
	else
		rc = do_load_serial_ymodem(cdev);

	if (rc < 0) {
		printf("## Binary (ymodem) download aborted (%d)\n", rc);
		rcode = 1;
	}

	rc = console_set_baudrate(cdev, current_baudrate);
	if (rc)
		return rc;

	return rcode;
}

BAREBOX_CMD_HELP_START(loady)
BAREBOX_CMD_HELP_TEXT("Options:")
BAREBOX_CMD_HELP_OPT("-g", "use Y-Modem/G (use on lossless tty such as USB)")
BAREBOX_CMD_HELP_OPT("-b BAUD", "baudrate for download (default: console baudrate)")
BAREBOX_CMD_HELP_OPT("-t NAME", "console name to use (default: current)")
BAREBOX_CMD_HELP_END

BAREBOX_CMD_START(loady)
	.cmd = do_loady,
	BAREBOX_CMD_DESC("load binary file over serial line (Y-Modem)")
	BAREBOX_CMD_OPTS("[-gtb]")
	BAREBOX_CMD_GROUP(CMD_GRP_BOOT)
	BAREBOX_CMD_HELP(cmd_loady_help)
BAREBOX_CMD_END


/**
 * @brief provide the loadx(X-Modem) support
 *
 * @param argc number of arguments
 * @param argv arguments of loadx command
 *
 * @return success or failure
 */
static int do_loadx(int argc, char *argv[])
{
	ulong offset = 0;
	int load_baudrate = 0, current_baudrate, rc, ofd, opt, rcode = 0;
	char *output_file = NULL, *cname = NULL;
	struct console_device *cdev = NULL;

	while ((opt = getopt(argc, argv, "f:b:t:o:c")) > 0) {
		switch (opt) {
		case 'f':
			output_file = optarg;
			break;
		case 'b':
			load_baudrate = (int)simple_strtoul(optarg, NULL, 10);
			break;
		case 'o':
			offset = (int)simple_strtoul(optarg, NULL, 10);
			break;
		case 't':
			cname = optarg;
			break;
		default:
			perror(argv[0]);
			return 1;
		}
	}

	if (cname)
		cdev = console_get_by_name(cname);
	else
		cdev = console_get_first_active();
	if (!cdev) {
		printf("%s:No console device %s with STDIN and STDOUT",
		       argv[0], cname ? cname : "default");
		return -ENODEV;
	}

	/* Load Defaults */
	if (!output_file)
		output_file = DEF_FILE;

	/* File should exist */
	ofd = open(output_file, O_WRONLY | O_CREAT);
	if (ofd < 0) {
		perror(argv[0]);
		return 3;
	}
	/* Seek to the right offset */
	if (offset) {
		int seek = lseek(ofd, offset, SEEK_SET);
		if (seek != offset) {
			close(ofd);
			ofd = 0;
			perror(argv[0]);
			return 4;
		}
	}

	current_baudrate = console_get_baudrate(cdev);

	if (!load_baudrate)
		load_baudrate = current_baudrate;

	rc = console_set_baudrate(cdev, load_baudrate);
	if (rc)
		return rc;

	printf("## Ready for binary (xmodem) download "
	       "to 0x%08lX offset on %s device at %d bps...\n", offset,
	       output_file, load_baudrate ? load_baudrate : current_baudrate);
	rcode = do_load_serial_xmodem(cdev, ofd);
	if (rcode < 0) {
		printf("## Binary (xmodem) download aborted (%d)\n", rcode);
		rcode = 1;
	}

	rc = console_set_baudrate(cdev, current_baudrate);
	if (rc)
		return rc;

	return rcode;
}

BAREBOX_CMD_HELP_START(loadx)
BAREBOX_CMD_HELP_TEXT("Options:")
BAREBOX_CMD_HELP_OPT("-f FILE", "download to FILE (default image.bin")
BAREBOX_CMD_HELP_OPT("-o OFFS", "destination file OFFSet (default 0)")
BAREBOX_CMD_HELP_OPT("-b BAUD", "baudrate for download (default: console baudrate)")
BAREBOX_CMD_HELP_OPT("-t NAME", "console name to use (default: current)")
BAREBOX_CMD_HELP_END

BAREBOX_CMD_START(loadx)
	.cmd = do_loadx,
	BAREBOX_CMD_DESC("load binary file over serial line (X-Modem)")
	BAREBOX_CMD_OPTS("[-fptbc]")
	BAREBOX_CMD_GROUP(CMD_GRP_BOOT)
	BAREBOX_CMD_HELP(cmd_loadx_help)
BAREBOX_CMD_END