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 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431
|
/*
* imx_uart:
*
* Program to download and execute an image over the serial boot protocol
* on i.MX series processors.
*
* This library is free software; you can redistribute it and/or
* modify it under the terms of the GNU Lesser General Public
* License as published by the Free Software Foundation; either
* version 2.1 of the License, or (at your option) any later version.
*
* This library 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
* Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public
* License along with this library; if not, write to the Free Software
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
*/
#include <stdio.h>
#include <sys/types.h>
#include <time.h>
#include <ctype.h>
#include <errno.h>
#include <string.h>
#include <stdlib.h>
#include <stdint.h>
#include <getopt.h>
#include <fcntl.h>
#ifndef WIN32
#include <termios.h>
#include <sys/ioctl.h>
#endif
#include "portable.h"
#include "imx_sdp.h"
#include "imx_loader.h"
#include "imx_loader_config.h"
extern int debugmode;
#define get_min(a, b) (((a) < (b)) ? (a) : (b))
int transfer_uart(struct sdp_dev *dev, int report, unsigned char *p, unsigned size,
unsigned int expected, int* last_trans)
{
int fd = *(int *)dev->priv;
if (report < 3) {
*last_trans = write(fd, p, size);
} else {
// Read...
int ret;
*last_trans = 0;
while (*last_trans < (int)expected)
{
ret = read(fd, p, expected - *last_trans);
if (ret < 0)
return ret;
// err is transfered bytes...
*last_trans += ret;
p += ret;
}
}
return 0;
}
#ifndef WIN32
int uart_connect(int *uart_fd, char const *tty, int usertscts, int associate, struct termios *orig)
#else
int uart_connect(int *uart_fd, char const *tty, int usertscts, int associate, DCB* orig)
#endif
{
int err = 0, count = 0;
int retry = 10;
#ifndef WIN32
int flags = O_RDWR | O_NOCTTY | O_SYNC;
struct termios key;
#else
int flags = O_RDWR | _O_BINARY;
DCB dcb;
COMMTIMEOUTS timeouts;
HANDLE handle;
#endif
char magic[] = { 0x23, 0x45, 0x45, 0x23 };
char magic_response[4];
char *buf;
#ifndef WIN32
memset(&key,0,sizeof(key));
#endif
memset(&magic_response,0,sizeof(magic_response));
*uart_fd = open(tty, flags);
if (*uart_fd < 0) {
printf("tty %s\n", tty);
fprintf(stdout, "open() failed: %s\n", strerror(errno));
return *uart_fd;
}
#ifndef WIN32
// Get original terminal settings
err = tcgetattr(*uart_fd, orig);
// 8 data bits
key.c_cflag |= CS8;
key.c_cflag |= CLOCAL | CREAD;
if (usertscts)
key.c_cflag |= CRTSCTS;
key.c_cflag |= B115200;
// Enable blocking read, 0.5s timeout...
key.c_lflag &= ~ICANON; // Set non-canonical mode
key.c_cc[VTIME] = 5;
err = tcsetattr(*uart_fd, TCSANOW, &key);
if (err < 0) {
fprintf(stdout, "tcsetattr() failed: %s\n", strerror(errno));
close(*uart_fd);
return err;
}
err = tcflush(*uart_fd, TCIOFLUSH);
#else
handle=(HANDLE)_get_osfhandle(*uart_fd);
orig->DCBlength=sizeof(DCB);
GetCommState(handle,orig);
memset(&dcb,0,sizeof(DCB));
dcb.DCBlength=sizeof(DCB);
dcb.fBinary=TRUE;
dcb.fParity=FALSE;
dcb.BaudRate=CBR_115200;
dcb.ByteSize=8;
if (usertscts)
{
dcb.fRtsControl=RTS_CONTROL_ENABLE;
dcb.fOutxCtsFlow=TRUE;
}
if (!SetCommState(handle,&dcb))
{
fprintf(stdout, "SetCommState() failed: %d\n", GetLastError());
close(*uart_fd);
return err;
}
memset(&timeouts,0,sizeof(COMMTIMEOUTS));
timeouts.ReadIntervalTimeout=MAXDWORD;
timeouts.ReadTotalTimeoutMultiplier=MAXDWORD;
timeouts.ReadTotalTimeoutConstant=500;
if (!SetCommTimeouts(handle,&timeouts))
{
fprintf(stdout, "SetCommTimeouts() failed: %d\n", GetLastError());
close(*uart_fd);
return err;
}
if (!PurgeComm(handle,PURGE_TXABORT|PURGE_RXABORT))
{
fprintf(stdout, "PurgeComm() failed: %d\n", GetLastError());
close(*uart_fd);
return err;
}
#endif
if (!associate)
return err;
// Association phase, send and receive 0x23454523
printf("starting associating phase");
while(retry--) {
#ifndef WIN32
// Flush again before retrying
err = tcflush(*uart_fd, TCIOFLUSH);
#endif
write(*uart_fd, magic, sizeof(magic));
buf = magic_response;
count = 0;
while (count < 4) {
err = read(*uart_fd, buf, 4 - count);
/* read timeout.. */
if (err <= 0)
break;
count += err;
buf += err;
}
if (!memcmp(magic, magic_response, sizeof(magic_response)))
break;
printf(".");
fflush(stdout);
#ifndef WIN32
err = tcflush(*uart_fd, TCIOFLUSH);
#endif
msleep(1000);
}
printf("\n");
fflush(stdout);
if (!retry) {
fprintf(stderr, "associating phase failed, make sure the device"
" is in recovery mode\n");
close(*uart_fd);
return -2;
}
if (memcmp(magic, magic_response, sizeof(magic_response))) {
fprintf(stderr, "magic missmatch, response was 0x%08x\n",
*(uint32_t *)magic_response);
close(*uart_fd);
return -3;
}
fprintf(stderr, "association phase succeeded, response was 0x%08x\n",
*(uint32_t *)magic_response);
return 0;
}
#ifndef WIN32
void uart_close(int *uart_fd, struct termios *orig)
#else
void uart_close(int *uart_fd, DCB* orig)
#endif
{
#ifndef WIN32
int err;
// Restore original terminal settings
err = tcsetattr(*uart_fd, TCSAFLUSH, orig);
if (err < 0)
fprintf(stdout, "tcsetattr() failed: %s\n", strerror(errno));
#else
HANDLE handle;
handle=(HANDLE)_get_osfhandle(*uart_fd);
SetCommState(handle,orig);
#endif
close(*uart_fd);
}
void print_usage(void)
{
printf("Usage: imx_uart [OPTIONS...] UART CONFIG [JOBS...]\n"
#ifndef WIN32
" e.g. imx_uart -n /dev/ttyUSB0 vybrid_usb_work.conf u-boot.imx\n"
#else
" e.g. imx_uart -n COM1: vybrid_uart_work.conf eboot.img\n"
#endif
"Load data on target connected to UART using serial download protocol as\n"
"configured in CONFIG file.\n"
"\n"
"Where OPTIONS are\n"
" -h --help Show this help\n"
" -v --verify Verify downloaded data\n"
" -n --no-rtscts Do not use RTS/CTS flow control\n"
" Default is to use RTS/CTS, Vybrid requires them\n"
" -N --no-association Do not do serial Association Phase\n"
" -d --debugmode Enable debug logs\n"
"\n"
"And where [JOBS...] are\n"
" FILE [-lLOADADDR] [-sSIZE] ...\n"
"Multiple jobs can be configured. The first job is treated special, load\n"
"address, jump address, and length are read from the IVT header. If no job\n"
"is specified, the jobs defined in the target specific configuration file\n"
"is being used.\n");
}
int parse_opts(int argc, char * const *argv, char const **ttyfile,
char const **conffile, int *verify, int *usertscts,
int *associate, struct sdp_work **cmd_head)
{
char c;
*conffile = NULL;
*ttyfile = NULL;
static struct option long_options[] = {
{"help", no_argument, 0, 'h' },
{"verify", no_argument, 0, 'v' },
{"version", no_argument, 0, 'V' },
{"debugmode", no_argument, 0, 'd' },
{"no-rtscts", no_argument, 0, 'n' },
{"no-association", no_argument, 0, 'N' },
{0, 0, 0, 0 },
};
while ((c = getopt_long(argc, argv, "+hdvVnN", long_options, NULL)) != -1) {
switch (c)
{
case 'h':
case '?':
print_usage();
return 1;
case 'd':
debugmode = 1; /* global extern */
break;
case 'n':
*usertscts = 0;
break;
case 'N':
*associate = 0;
break;
case 'v':
*verify = 1;
break;
case 'V':
printf("imx_usb " IMX_LOADER_VERSION "\n");
return 1;
}
}
// Options parsed, get mandatory arguments...
if (optind >= argc) {
fprintf(stderr, "non optional argument UART is missing\n");
return -1;
}
*ttyfile = argv[optind];
optind++;
if (optind >= argc) {
fprintf(stderr, "non optional argument CONFIG is missing\n");
return -1;
}
*conffile = argv[optind];
optind++;
if (optind < argc) {
// Parse optional job arguments...
*cmd_head = parse_cmd_args(argc - optind, &argv[optind]);
}
return 0;
}
#define ARRAY_SIZE(w) sizeof(w)/sizeof(w[0])
int main(int argc, char * const argv[])
{
struct sdp_dev *p_id;
int err = 0;
int verify = 0;
int usertscts = 1;
int associate = 1;
int uart_fd;
struct sdp_work *curr;
char const *conf;
char const *ttyfile;
char const *conffilepath;
char const *conffile;
char const *basepath;
#ifndef WIN32
struct termios orig;
#else
DCB orig;
#endif
curr=NULL;
err = parse_opts(argc, argv, &ttyfile, &conffilepath, &verify,
&usertscts, &associate, &curr);
if (err < 0)
return EXIT_FAILURE;
else if (err > 0)
return EXIT_SUCCESS;
// Get machine specific configuration file..
if ((conffile = strrchr(conffilepath, PATH_SEPARATOR)) == NULL) {
// Only a file was given as configuration
basepath = get_base_path(argv[0]);
conffile = conffilepath;
} else {
// A whole path is given as configuration
basepath = get_base_path(conffilepath);
conffile++; // Filename starts after slash
}
conf = conf_file_name(conffile, basepath, get_global_conf_path());
if (conf == NULL)
return -1;
p_id = parse_conf(conf);
if (!p_id)
return -1;
// Open UART and start associating phase...
err = uart_connect(&uart_fd, ttyfile, usertscts, associate, &orig);
if (err < 0)
return EXIT_FAILURE;
p_id->transfer = &transfer_uart;
// UART private pointer is TTY file descriptor...
p_id->priv = &uart_fd;
// By default, use work from config file...
if (curr == NULL)
curr = p_id->work;
err = do_work(p_id, &curr, verify);
dbg_printf("do_work finished with err=%d, curr=%p\n", err, curr);
uart_close(&uart_fd, &orig);
return err;
}
|