2012-05-27 11:44:51 +00:00
|
|
|
/*
|
2014-10-22 10:13:21 +00:00
|
|
|
* Boot a Marvell SoC, with Xmodem over UART0.
|
|
|
|
* supports Kirkwood, Dove, Armada 370, Armada XP
|
2012-05-27 11:44:51 +00:00
|
|
|
*
|
|
|
|
* (c) 2012 Daniel Stodden <daniel.stodden@gmail.com>
|
|
|
|
*
|
|
|
|
* References: marvell.com, "88F6180, 88F6190, 88F6192, and 88F6281
|
|
|
|
* Integrated Controller: Functional Specifications" December 2,
|
|
|
|
* 2008. Chapter 24.2 "BootROM Firmware".
|
|
|
|
*/
|
|
|
|
|
2016-01-07 13:12:04 +00:00
|
|
|
#include "kwbimage.h"
|
|
|
|
#include "mkimage.h"
|
2021-09-24 21:06:42 +00:00
|
|
|
#include "version.h"
|
2016-01-07 13:12:04 +00:00
|
|
|
|
2012-05-27 11:44:51 +00:00
|
|
|
#include <stdlib.h>
|
|
|
|
#include <stdio.h>
|
|
|
|
#include <string.h>
|
|
|
|
#include <stdarg.h>
|
2016-01-07 13:12:04 +00:00
|
|
|
#include <image.h>
|
2012-05-27 11:44:51 +00:00
|
|
|
#include <libgen.h>
|
|
|
|
#include <fcntl.h>
|
|
|
|
#include <errno.h>
|
|
|
|
#include <unistd.h>
|
|
|
|
#include <stdint.h>
|
|
|
|
#include <termios.h>
|
2021-09-24 21:06:52 +00:00
|
|
|
#include <time.h>
|
2012-05-27 11:44:51 +00:00
|
|
|
#include <sys/stat.h>
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Marvell BootROM UART Sensing
|
|
|
|
*/
|
|
|
|
|
|
|
|
static unsigned char kwboot_msg_boot[] = {
|
|
|
|
0xBB, 0x11, 0x22, 0x33, 0x44, 0x55, 0x66, 0x77
|
|
|
|
};
|
|
|
|
|
2014-10-22 10:13:21 +00:00
|
|
|
static unsigned char kwboot_msg_debug[] = {
|
|
|
|
0xDD, 0x11, 0x22, 0x33, 0x44, 0x55, 0x66, 0x77
|
|
|
|
};
|
|
|
|
|
|
|
|
/* Defines known to work on Kirkwood */
|
2012-05-27 11:44:51 +00:00
|
|
|
#define KWBOOT_MSG_REQ_DELAY 10 /* ms */
|
|
|
|
#define KWBOOT_MSG_RSP_TIMEO 50 /* ms */
|
|
|
|
|
2014-10-22 10:13:21 +00:00
|
|
|
/* Defines known to work on Armada XP */
|
|
|
|
#define KWBOOT_MSG_REQ_DELAY_AXP 1000 /* ms */
|
|
|
|
#define KWBOOT_MSG_RSP_TIMEO_AXP 1000 /* ms */
|
|
|
|
|
2012-05-27 11:44:51 +00:00
|
|
|
/*
|
|
|
|
* Xmodem Transfers
|
|
|
|
*/
|
|
|
|
|
|
|
|
#define SOH 1 /* sender start of block header */
|
|
|
|
#define EOT 4 /* sender end of block transfer */
|
|
|
|
#define ACK 6 /* target block ack */
|
|
|
|
#define NAK 21 /* target block negative ack */
|
|
|
|
#define CAN 24 /* target/sender transfer cancellation */
|
|
|
|
|
2021-09-24 21:06:48 +00:00
|
|
|
#define KWBOOT_XM_BLKSZ 128 /* xmodem block size */
|
|
|
|
|
2012-05-27 11:44:51 +00:00
|
|
|
struct kwboot_block {
|
|
|
|
uint8_t soh;
|
|
|
|
uint8_t pnum;
|
|
|
|
uint8_t _pnum;
|
2021-09-24 21:06:48 +00:00
|
|
|
uint8_t data[KWBOOT_XM_BLKSZ];
|
2012-05-27 11:44:51 +00:00
|
|
|
uint8_t csum;
|
2021-07-23 09:14:14 +00:00
|
|
|
} __packed;
|
2012-05-27 11:44:51 +00:00
|
|
|
|
|
|
|
#define KWBOOT_BLK_RSP_TIMEO 1000 /* ms */
|
2021-09-24 21:06:52 +00:00
|
|
|
#define KWBOOT_HDR_RSP_TIMEO 10000 /* ms */
|
2012-05-27 11:44:51 +00:00
|
|
|
|
|
|
|
static int kwboot_verbose;
|
|
|
|
|
2014-10-22 10:13:21 +00:00
|
|
|
static int msg_req_delay = KWBOOT_MSG_REQ_DELAY;
|
|
|
|
static int msg_rsp_timeo = KWBOOT_MSG_RSP_TIMEO;
|
2016-02-16 21:28:19 +00:00
|
|
|
static int blk_rsp_timeo = KWBOOT_BLK_RSP_TIMEO;
|
2014-10-22 10:13:21 +00:00
|
|
|
|
2021-09-24 21:06:41 +00:00
|
|
|
static ssize_t
|
|
|
|
kwboot_write(int fd, const char *buf, size_t len)
|
|
|
|
{
|
|
|
|
size_t tot = 0;
|
|
|
|
|
|
|
|
while (tot < len) {
|
|
|
|
ssize_t wr = write(fd, buf + tot, len - tot);
|
|
|
|
|
|
|
|
if (wr < 0)
|
|
|
|
return -1;
|
|
|
|
|
|
|
|
tot += wr;
|
|
|
|
}
|
|
|
|
|
|
|
|
return tot;
|
|
|
|
}
|
|
|
|
|
2012-05-27 11:44:51 +00:00
|
|
|
static void
|
|
|
|
kwboot_printv(const char *fmt, ...)
|
|
|
|
{
|
|
|
|
va_list ap;
|
|
|
|
|
|
|
|
if (kwboot_verbose) {
|
|
|
|
va_start(ap, fmt);
|
|
|
|
vprintf(fmt, ap);
|
|
|
|
va_end(ap);
|
|
|
|
fflush(stdout);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
|
|
|
__spinner(void)
|
|
|
|
{
|
|
|
|
const char seq[] = { '-', '\\', '|', '/' };
|
|
|
|
const int div = 8;
|
|
|
|
static int state, bs;
|
|
|
|
|
|
|
|
if (state % div == 0) {
|
|
|
|
fputc(bs, stdout);
|
|
|
|
fputc(seq[state / div % sizeof(seq)], stdout);
|
|
|
|
fflush(stdout);
|
|
|
|
}
|
|
|
|
|
|
|
|
bs = '\b';
|
|
|
|
state++;
|
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
|
|
|
kwboot_spinner(void)
|
|
|
|
{
|
|
|
|
if (kwboot_verbose)
|
|
|
|
__spinner();
|
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
|
|
|
__progress(int pct, char c)
|
|
|
|
{
|
|
|
|
const int width = 70;
|
|
|
|
static const char *nl = "";
|
|
|
|
static int pos;
|
|
|
|
|
|
|
|
if (pos % width == 0)
|
|
|
|
printf("%s%3d %% [", nl, pct);
|
|
|
|
|
|
|
|
fputc(c, stdout);
|
|
|
|
|
|
|
|
nl = "]\n";
|
2021-09-24 21:06:46 +00:00
|
|
|
pos = (pos + 1) % width;
|
2012-05-27 11:44:51 +00:00
|
|
|
|
|
|
|
if (pct == 100) {
|
2021-09-24 21:06:46 +00:00
|
|
|
while (pos && pos++ < width)
|
2012-05-27 11:44:51 +00:00
|
|
|
fputc(' ', stdout);
|
|
|
|
fputs(nl, stdout);
|
2021-09-24 21:06:46 +00:00
|
|
|
nl = "";
|
|
|
|
pos = 0;
|
2012-05-27 11:44:51 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
fflush(stdout);
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
|
|
|
kwboot_progress(int _pct, char c)
|
|
|
|
{
|
|
|
|
static int pct;
|
|
|
|
|
|
|
|
if (_pct != -1)
|
|
|
|
pct = _pct;
|
|
|
|
|
|
|
|
if (kwboot_verbose)
|
|
|
|
__progress(pct, c);
|
2021-09-24 21:06:46 +00:00
|
|
|
|
|
|
|
if (pct == 100)
|
|
|
|
pct = 0;
|
2012-05-27 11:44:51 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
static int
|
|
|
|
kwboot_tty_recv(int fd, void *buf, size_t len, int timeo)
|
|
|
|
{
|
|
|
|
int rc, nfds;
|
|
|
|
fd_set rfds;
|
|
|
|
struct timeval tv;
|
|
|
|
ssize_t n;
|
|
|
|
|
|
|
|
rc = -1;
|
|
|
|
|
|
|
|
FD_ZERO(&rfds);
|
|
|
|
FD_SET(fd, &rfds);
|
|
|
|
|
|
|
|
tv.tv_sec = 0;
|
|
|
|
tv.tv_usec = timeo * 1000;
|
|
|
|
if (tv.tv_usec > 1000000) {
|
|
|
|
tv.tv_sec += tv.tv_usec / 1000000;
|
|
|
|
tv.tv_usec %= 1000000;
|
|
|
|
}
|
|
|
|
|
|
|
|
do {
|
|
|
|
nfds = select(fd + 1, &rfds, NULL, NULL, &tv);
|
|
|
|
if (nfds < 0)
|
|
|
|
goto out;
|
|
|
|
if (!nfds) {
|
|
|
|
errno = ETIMEDOUT;
|
|
|
|
goto out;
|
|
|
|
}
|
|
|
|
|
|
|
|
n = read(fd, buf, len);
|
2018-07-03 16:10:31 +00:00
|
|
|
if (n <= 0)
|
2012-05-27 11:44:51 +00:00
|
|
|
goto out;
|
|
|
|
|
|
|
|
buf = (char *)buf + n;
|
|
|
|
len -= n;
|
|
|
|
} while (len > 0);
|
|
|
|
|
|
|
|
rc = 0;
|
|
|
|
out:
|
|
|
|
return rc;
|
|
|
|
}
|
|
|
|
|
|
|
|
static int
|
|
|
|
kwboot_tty_send(int fd, const void *buf, size_t len)
|
|
|
|
{
|
2014-10-22 10:13:21 +00:00
|
|
|
if (!buf)
|
|
|
|
return 0;
|
|
|
|
|
2021-09-24 21:06:41 +00:00
|
|
|
if (kwboot_write(fd, buf, len) < 0)
|
|
|
|
return -1;
|
2012-05-27 11:44:51 +00:00
|
|
|
|
2021-09-24 21:06:41 +00:00
|
|
|
return tcdrain(fd);
|
2012-05-27 11:44:51 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
static int
|
|
|
|
kwboot_tty_send_char(int fd, unsigned char c)
|
|
|
|
{
|
|
|
|
return kwboot_tty_send(fd, &c, 1);
|
|
|
|
}
|
|
|
|
|
|
|
|
static speed_t
|
|
|
|
kwboot_tty_speed(int baudrate)
|
|
|
|
{
|
|
|
|
switch (baudrate) {
|
|
|
|
case 115200:
|
|
|
|
return B115200;
|
|
|
|
case 57600:
|
|
|
|
return B57600;
|
|
|
|
case 38400:
|
|
|
|
return B38400;
|
|
|
|
case 19200:
|
|
|
|
return B19200;
|
|
|
|
case 9600:
|
|
|
|
return B9600;
|
|
|
|
}
|
|
|
|
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
|
|
|
static int
|
|
|
|
kwboot_open_tty(const char *path, speed_t speed)
|
|
|
|
{
|
|
|
|
int rc, fd;
|
|
|
|
struct termios tio;
|
|
|
|
|
|
|
|
rc = -1;
|
|
|
|
|
|
|
|
fd = open(path, O_RDWR|O_NOCTTY|O_NDELAY);
|
|
|
|
if (fd < 0)
|
|
|
|
goto out;
|
|
|
|
|
|
|
|
memset(&tio, 0, sizeof(tio));
|
|
|
|
|
|
|
|
tio.c_iflag = 0;
|
|
|
|
tio.c_cflag = CREAD|CLOCAL|CS8;
|
|
|
|
|
|
|
|
tio.c_cc[VMIN] = 1;
|
|
|
|
tio.c_cc[VTIME] = 10;
|
|
|
|
|
|
|
|
cfsetospeed(&tio, speed);
|
|
|
|
cfsetispeed(&tio, speed);
|
|
|
|
|
|
|
|
rc = tcsetattr(fd, TCSANOW, &tio);
|
|
|
|
if (rc)
|
|
|
|
goto out;
|
|
|
|
|
|
|
|
rc = fd;
|
|
|
|
out:
|
|
|
|
if (rc < 0) {
|
|
|
|
if (fd >= 0)
|
|
|
|
close(fd);
|
|
|
|
}
|
|
|
|
|
|
|
|
return rc;
|
|
|
|
}
|
|
|
|
|
|
|
|
static int
|
|
|
|
kwboot_bootmsg(int tty, void *msg)
|
|
|
|
{
|
|
|
|
int rc;
|
|
|
|
char c;
|
2018-08-13 15:24:38 +00:00
|
|
|
int count;
|
2012-05-27 11:44:51 +00:00
|
|
|
|
2014-10-22 10:13:21 +00:00
|
|
|
if (msg == NULL)
|
|
|
|
kwboot_printv("Please reboot the target into UART boot mode...");
|
|
|
|
else
|
|
|
|
kwboot_printv("Sending boot message. Please reboot the target...");
|
2012-05-27 11:44:51 +00:00
|
|
|
|
|
|
|
do {
|
|
|
|
rc = tcflush(tty, TCIOFLUSH);
|
|
|
|
if (rc)
|
|
|
|
break;
|
|
|
|
|
2018-08-13 15:24:38 +00:00
|
|
|
for (count = 0; count < 128; count++) {
|
|
|
|
rc = kwboot_tty_send(tty, msg, 8);
|
|
|
|
if (rc) {
|
|
|
|
usleep(msg_req_delay * 1000);
|
|
|
|
continue;
|
|
|
|
}
|
2012-05-27 11:44:51 +00:00
|
|
|
}
|
|
|
|
|
2014-10-22 10:13:21 +00:00
|
|
|
rc = kwboot_tty_recv(tty, &c, 1, msg_rsp_timeo);
|
2012-05-27 11:44:51 +00:00
|
|
|
|
|
|
|
kwboot_spinner();
|
|
|
|
|
|
|
|
} while (rc || c != NAK);
|
|
|
|
|
|
|
|
kwboot_printv("\n");
|
|
|
|
|
|
|
|
return rc;
|
|
|
|
}
|
|
|
|
|
2014-10-22 10:13:21 +00:00
|
|
|
static int
|
|
|
|
kwboot_debugmsg(int tty, void *msg)
|
|
|
|
{
|
|
|
|
int rc;
|
|
|
|
|
|
|
|
kwboot_printv("Sending debug message. Please reboot the target...");
|
|
|
|
|
|
|
|
do {
|
|
|
|
char buf[16];
|
|
|
|
|
|
|
|
rc = tcflush(tty, TCIOFLUSH);
|
|
|
|
if (rc)
|
|
|
|
break;
|
|
|
|
|
|
|
|
rc = kwboot_tty_send(tty, msg, 8);
|
|
|
|
if (rc) {
|
|
|
|
usleep(msg_req_delay * 1000);
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
|
|
|
rc = kwboot_tty_recv(tty, buf, 16, msg_rsp_timeo);
|
|
|
|
|
|
|
|
kwboot_spinner();
|
|
|
|
|
|
|
|
} while (rc);
|
|
|
|
|
|
|
|
kwboot_printv("\n");
|
|
|
|
|
|
|
|
return rc;
|
|
|
|
}
|
|
|
|
|
2021-09-24 21:06:44 +00:00
|
|
|
static size_t
|
2012-05-27 11:44:51 +00:00
|
|
|
kwboot_xm_makeblock(struct kwboot_block *block, const void *data,
|
|
|
|
size_t size, int pnum)
|
|
|
|
{
|
2021-09-24 21:06:45 +00:00
|
|
|
size_t i, n;
|
2012-05-27 11:44:51 +00:00
|
|
|
|
2014-10-22 10:13:21 +00:00
|
|
|
block->soh = SOH;
|
2012-05-27 11:44:51 +00:00
|
|
|
block->pnum = pnum;
|
|
|
|
block->_pnum = ~block->pnum;
|
|
|
|
|
2021-09-24 21:06:48 +00:00
|
|
|
n = size < KWBOOT_XM_BLKSZ ? size : KWBOOT_XM_BLKSZ;
|
2012-05-27 11:44:51 +00:00
|
|
|
memcpy(&block->data[0], data, n);
|
2021-09-24 21:06:48 +00:00
|
|
|
memset(&block->data[n], 0, KWBOOT_XM_BLKSZ - n);
|
2012-05-27 11:44:51 +00:00
|
|
|
|
|
|
|
block->csum = 0;
|
|
|
|
for (i = 0; i < n; i++)
|
|
|
|
block->csum += block->data[i];
|
|
|
|
|
|
|
|
return n;
|
|
|
|
}
|
|
|
|
|
2021-09-24 21:06:52 +00:00
|
|
|
static uint64_t
|
|
|
|
_now(void)
|
|
|
|
{
|
|
|
|
struct timespec ts;
|
|
|
|
|
|
|
|
if (clock_gettime(CLOCK_MONOTONIC, &ts)) {
|
|
|
|
static int err_print;
|
|
|
|
|
|
|
|
if (!err_print) {
|
|
|
|
perror("clock_gettime() does not work");
|
|
|
|
err_print = 1;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* this will just make the timeout not work */
|
|
|
|
return -1ULL;
|
|
|
|
}
|
|
|
|
|
|
|
|
return ts.tv_sec * 1000ULL + (ts.tv_nsec + 500000) / 1000000;
|
|
|
|
}
|
|
|
|
|
2021-09-24 21:06:49 +00:00
|
|
|
static int
|
|
|
|
_is_xm_reply(char c)
|
|
|
|
{
|
|
|
|
return c == ACK || c == NAK || c == CAN;
|
|
|
|
}
|
|
|
|
|
2021-09-24 21:06:54 +00:00
|
|
|
static int
|
|
|
|
_xm_reply_to_error(int c)
|
|
|
|
{
|
|
|
|
int rc = -1;
|
|
|
|
|
|
|
|
switch (c) {
|
|
|
|
case ACK:
|
|
|
|
rc = 0;
|
|
|
|
break;
|
|
|
|
case NAK:
|
|
|
|
errno = EBADMSG;
|
|
|
|
break;
|
|
|
|
case CAN:
|
|
|
|
errno = ECANCELED;
|
|
|
|
break;
|
|
|
|
default:
|
|
|
|
errno = EPROTO;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
return rc;
|
|
|
|
}
|
|
|
|
|
2012-05-27 11:44:51 +00:00
|
|
|
static int
|
2021-09-24 21:06:51 +00:00
|
|
|
kwboot_xm_recv_reply(int fd, char *c, int allow_non_xm, int *non_xm_print)
|
2021-09-24 21:06:50 +00:00
|
|
|
{
|
2021-09-24 21:06:52 +00:00
|
|
|
int timeout = allow_non_xm ? KWBOOT_HDR_RSP_TIMEO : blk_rsp_timeo;
|
2021-09-24 21:06:53 +00:00
|
|
|
uint64_t recv_until = _now() + timeout;
|
2021-09-24 21:06:50 +00:00
|
|
|
int rc;
|
|
|
|
|
2021-09-24 21:06:53 +00:00
|
|
|
if (non_xm_print)
|
|
|
|
*non_xm_print = 0;
|
2021-09-24 21:06:51 +00:00
|
|
|
|
2021-09-24 21:06:50 +00:00
|
|
|
while (1) {
|
2021-09-24 21:06:52 +00:00
|
|
|
rc = kwboot_tty_recv(fd, c, 1, timeout);
|
2021-09-24 21:06:50 +00:00
|
|
|
if (rc) {
|
|
|
|
if (errno != ETIMEDOUT)
|
|
|
|
return rc;
|
2021-09-24 21:06:53 +00:00
|
|
|
else if (allow_non_xm && *non_xm_print)
|
2021-09-24 21:06:52 +00:00
|
|
|
return -1;
|
|
|
|
else
|
|
|
|
*c = NAK;
|
2021-09-24 21:06:50 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/* If received xmodem reply, end. */
|
|
|
|
if (_is_xm_reply(*c))
|
|
|
|
break;
|
|
|
|
|
|
|
|
/*
|
|
|
|
* If printing non-xmodem text output is allowed and such a byte
|
2021-09-24 21:06:52 +00:00
|
|
|
* was received, print it and increase receiving time.
|
2021-09-24 21:06:53 +00:00
|
|
|
* Otherwise decrease timeout by time elapsed.
|
2021-09-24 21:06:50 +00:00
|
|
|
*/
|
|
|
|
if (allow_non_xm) {
|
2021-09-24 21:06:52 +00:00
|
|
|
recv_until = _now() + timeout;
|
2021-09-24 21:06:50 +00:00
|
|
|
putchar(*c);
|
|
|
|
fflush(stdout);
|
2021-09-24 21:06:51 +00:00
|
|
|
*non_xm_print = 1;
|
2021-09-24 21:06:53 +00:00
|
|
|
} else {
|
|
|
|
timeout = recv_until - _now();
|
|
|
|
if (timeout < 0) {
|
|
|
|
errno = ETIMEDOUT;
|
|
|
|
return -1;
|
|
|
|
}
|
2021-09-24 21:06:50 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
static int
|
|
|
|
kwboot_xm_sendblock(int fd, struct kwboot_block *block, int allow_non_xm,
|
|
|
|
int *done_print)
|
2012-05-27 11:44:51 +00:00
|
|
|
{
|
2021-09-24 21:06:51 +00:00
|
|
|
int non_xm_print;
|
2012-05-27 11:44:51 +00:00
|
|
|
int rc, retries;
|
|
|
|
char c;
|
|
|
|
|
2021-09-24 21:06:50 +00:00
|
|
|
*done_print = 0;
|
|
|
|
|
2012-05-27 11:44:51 +00:00
|
|
|
retries = 16;
|
|
|
|
do {
|
|
|
|
rc = kwboot_tty_send(fd, block, sizeof(*block));
|
|
|
|
if (rc)
|
2021-09-24 21:06:43 +00:00
|
|
|
return rc;
|
2012-05-27 11:44:51 +00:00
|
|
|
|
2021-09-24 21:06:50 +00:00
|
|
|
if (allow_non_xm && !*done_print) {
|
|
|
|
kwboot_progress(100, '.');
|
|
|
|
kwboot_printv("Done\n");
|
|
|
|
*done_print = 1;
|
|
|
|
}
|
2014-10-22 10:13:21 +00:00
|
|
|
|
2021-09-24 21:06:51 +00:00
|
|
|
rc = kwboot_xm_recv_reply(fd, &c, allow_non_xm, &non_xm_print);
|
2021-09-24 21:06:50 +00:00
|
|
|
if (rc)
|
|
|
|
return rc;
|
2012-05-27 11:44:51 +00:00
|
|
|
|
2021-09-24 21:06:50 +00:00
|
|
|
if (!allow_non_xm && c != ACK)
|
2012-05-27 11:44:51 +00:00
|
|
|
kwboot_progress(-1, '+');
|
|
|
|
} while (c == NAK && retries-- > 0);
|
|
|
|
|
2021-09-24 21:06:51 +00:00
|
|
|
if (non_xm_print)
|
|
|
|
kwboot_printv("\n");
|
|
|
|
|
2021-09-24 21:06:54 +00:00
|
|
|
return _xm_reply_to_error(c);
|
|
|
|
}
|
2012-05-27 11:44:51 +00:00
|
|
|
|
2021-09-24 21:06:54 +00:00
|
|
|
static int
|
|
|
|
kwboot_xm_finish(int fd)
|
|
|
|
{
|
|
|
|
int rc, retries;
|
|
|
|
char c;
|
2012-05-27 11:44:51 +00:00
|
|
|
|
2021-09-24 21:06:54 +00:00
|
|
|
kwboot_printv("Finishing transfer\n");
|
|
|
|
|
|
|
|
retries = 16;
|
|
|
|
do {
|
|
|
|
rc = kwboot_tty_send_char(fd, EOT);
|
|
|
|
if (rc)
|
|
|
|
return rc;
|
|
|
|
|
|
|
|
rc = kwboot_xm_recv_reply(fd, &c, 0, NULL);
|
|
|
|
if (rc)
|
|
|
|
return rc;
|
|
|
|
} while (c == NAK && retries-- > 0);
|
|
|
|
|
|
|
|
return _xm_reply_to_error(c);
|
2012-05-27 11:44:51 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
static int
|
2021-09-24 21:06:48 +00:00
|
|
|
kwboot_xmodem_one(int tty, int *pnum, int header, const uint8_t *data,
|
|
|
|
size_t size)
|
2012-05-27 11:44:51 +00:00
|
|
|
{
|
2021-09-24 21:06:50 +00:00
|
|
|
int done_print = 0;
|
2021-09-24 21:06:48 +00:00
|
|
|
size_t sent, left;
|
|
|
|
int rc;
|
2012-05-27 11:44:51 +00:00
|
|
|
|
2021-09-24 21:06:48 +00:00
|
|
|
kwboot_printv("Sending boot image %s (%zu bytes)...\n",
|
|
|
|
header ? "header" : "data", size);
|
2012-05-27 11:44:51 +00:00
|
|
|
|
2021-09-24 21:06:48 +00:00
|
|
|
left = size;
|
|
|
|
sent = 0;
|
2012-05-27 11:44:51 +00:00
|
|
|
|
2021-09-24 21:06:48 +00:00
|
|
|
while (sent < size) {
|
2012-05-27 11:44:51 +00:00
|
|
|
struct kwboot_block block;
|
2021-09-24 21:06:50 +00:00
|
|
|
int last_block;
|
2021-09-24 21:06:48 +00:00
|
|
|
size_t blksz;
|
2012-05-27 11:44:51 +00:00
|
|
|
|
2021-09-24 21:06:48 +00:00
|
|
|
blksz = kwboot_xm_makeblock(&block, data, left, (*pnum)++);
|
|
|
|
data += blksz;
|
2012-05-27 11:44:51 +00:00
|
|
|
|
2021-09-24 21:06:50 +00:00
|
|
|
last_block = (left <= blksz);
|
|
|
|
|
|
|
|
rc = kwboot_xm_sendblock(tty, &block, header && last_block,
|
|
|
|
&done_print);
|
2012-05-27 11:44:51 +00:00
|
|
|
if (rc)
|
|
|
|
goto out;
|
|
|
|
|
2021-09-24 21:06:48 +00:00
|
|
|
sent += blksz;
|
|
|
|
left -= blksz;
|
|
|
|
|
2021-09-24 21:06:50 +00:00
|
|
|
if (!done_print)
|
|
|
|
kwboot_progress(sent * 100 / size, '.');
|
2021-09-24 21:06:48 +00:00
|
|
|
}
|
2012-05-27 11:44:51 +00:00
|
|
|
|
2021-09-24 21:06:50 +00:00
|
|
|
if (!done_print)
|
|
|
|
kwboot_printv("Done\n");
|
2012-05-27 11:44:51 +00:00
|
|
|
|
2021-09-24 21:06:48 +00:00
|
|
|
return 0;
|
2012-05-27 11:44:51 +00:00
|
|
|
out:
|
2021-09-24 21:06:47 +00:00
|
|
|
kwboot_printv("\n");
|
2012-05-27 11:44:51 +00:00
|
|
|
return rc;
|
2021-09-24 21:06:48 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
static int
|
|
|
|
kwboot_xmodem(int tty, const void *_img, size_t size)
|
|
|
|
{
|
|
|
|
const uint8_t *img = _img;
|
|
|
|
int rc, pnum;
|
|
|
|
size_t hdrsz;
|
|
|
|
|
2021-09-24 21:07:01 +00:00
|
|
|
hdrsz = kwbheader_size(img);
|
2021-09-24 21:06:48 +00:00
|
|
|
|
|
|
|
kwboot_printv("Waiting 2s and flushing tty\n");
|
|
|
|
sleep(2); /* flush isn't effective without it */
|
|
|
|
tcflush(tty, TCIOFLUSH);
|
|
|
|
|
|
|
|
pnum = 1;
|
|
|
|
|
|
|
|
rc = kwboot_xmodem_one(tty, &pnum, 1, img, hdrsz);
|
|
|
|
if (rc)
|
|
|
|
return rc;
|
|
|
|
|
|
|
|
img += hdrsz;
|
|
|
|
size -= hdrsz;
|
|
|
|
|
|
|
|
rc = kwboot_xmodem_one(tty, &pnum, 0, img, size);
|
|
|
|
if (rc)
|
|
|
|
return rc;
|
2012-05-27 11:44:51 +00:00
|
|
|
|
2021-09-24 21:06:54 +00:00
|
|
|
return kwboot_xm_finish(tty);
|
2012-05-27 11:44:51 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
static int
|
2021-09-24 21:06:40 +00:00
|
|
|
kwboot_term_pipe(int in, int out, const char *quit, int *s)
|
2012-05-27 11:44:51 +00:00
|
|
|
{
|
2021-09-24 21:06:41 +00:00
|
|
|
ssize_t nin;
|
2012-05-27 11:44:51 +00:00
|
|
|
char _buf[128], *buf = _buf;
|
|
|
|
|
2021-07-23 09:14:17 +00:00
|
|
|
nin = read(in, buf, sizeof(_buf));
|
2018-07-03 16:10:31 +00:00
|
|
|
if (nin <= 0)
|
2012-05-27 11:44:51 +00:00
|
|
|
return -1;
|
|
|
|
|
|
|
|
if (quit) {
|
|
|
|
int i;
|
|
|
|
|
|
|
|
for (i = 0; i < nin; i++) {
|
|
|
|
if (*buf == quit[*s]) {
|
|
|
|
(*s)++;
|
|
|
|
if (!quit[*s])
|
|
|
|
return 0;
|
|
|
|
buf++;
|
|
|
|
nin--;
|
2021-07-23 09:14:20 +00:00
|
|
|
} else {
|
2021-09-24 21:06:41 +00:00
|
|
|
if (kwboot_write(out, quit, *s) < 0)
|
|
|
|
return -1;
|
|
|
|
*s = 0;
|
2021-07-23 09:14:20 +00:00
|
|
|
}
|
2012-05-27 11:44:51 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-09-24 21:06:41 +00:00
|
|
|
if (kwboot_write(out, buf, nin) < 0)
|
|
|
|
return -1;
|
2012-05-27 11:44:51 +00:00
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
static int
|
|
|
|
kwboot_terminal(int tty)
|
|
|
|
{
|
|
|
|
int rc, in, s;
|
2021-09-24 21:06:40 +00:00
|
|
|
const char *quit = "\34c";
|
2012-05-27 11:44:51 +00:00
|
|
|
struct termios otio, tio;
|
|
|
|
|
|
|
|
rc = -1;
|
|
|
|
|
|
|
|
in = STDIN_FILENO;
|
|
|
|
if (isatty(in)) {
|
|
|
|
rc = tcgetattr(in, &otio);
|
|
|
|
if (!rc) {
|
|
|
|
tio = otio;
|
|
|
|
cfmakeraw(&tio);
|
|
|
|
rc = tcsetattr(in, TCSANOW, &tio);
|
|
|
|
}
|
|
|
|
if (rc) {
|
|
|
|
perror("tcsetattr");
|
|
|
|
goto out;
|
|
|
|
}
|
|
|
|
|
|
|
|
kwboot_printv("[Type Ctrl-%c + %c to quit]\r\n",
|
|
|
|
quit[0]|0100, quit[1]);
|
|
|
|
} else
|
|
|
|
in = -1;
|
|
|
|
|
|
|
|
rc = 0;
|
|
|
|
s = 0;
|
|
|
|
|
|
|
|
do {
|
|
|
|
fd_set rfds;
|
|
|
|
int nfds = 0;
|
|
|
|
|
|
|
|
FD_SET(tty, &rfds);
|
|
|
|
nfds = nfds < tty ? tty : nfds;
|
|
|
|
|
|
|
|
if (in >= 0) {
|
|
|
|
FD_SET(in, &rfds);
|
|
|
|
nfds = nfds < in ? in : nfds;
|
|
|
|
}
|
|
|
|
|
|
|
|
nfds = select(nfds + 1, &rfds, NULL, NULL, NULL);
|
|
|
|
if (nfds < 0)
|
|
|
|
break;
|
|
|
|
|
|
|
|
if (FD_ISSET(tty, &rfds)) {
|
|
|
|
rc = kwboot_term_pipe(tty, STDOUT_FILENO, NULL, NULL);
|
|
|
|
if (rc)
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
2021-09-24 21:06:39 +00:00
|
|
|
if (in >= 0 && FD_ISSET(in, &rfds)) {
|
2012-05-27 11:44:51 +00:00
|
|
|
rc = kwboot_term_pipe(in, tty, quit, &s);
|
|
|
|
if (rc)
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
} while (quit[s] != 0);
|
|
|
|
|
2021-07-23 09:14:18 +00:00
|
|
|
if (in >= 0)
|
|
|
|
tcsetattr(in, TCSANOW, &otio);
|
2021-07-23 09:14:19 +00:00
|
|
|
printf("\n");
|
2012-05-27 11:44:51 +00:00
|
|
|
out:
|
|
|
|
return rc;
|
|
|
|
}
|
|
|
|
|
|
|
|
static void *
|
2021-09-24 21:07:03 +00:00
|
|
|
kwboot_read_image(const char *path, size_t *size, size_t reserve)
|
2012-05-27 11:44:51 +00:00
|
|
|
{
|
2021-09-24 21:06:55 +00:00
|
|
|
int rc, fd;
|
2012-05-27 11:44:51 +00:00
|
|
|
struct stat st;
|
|
|
|
void *img;
|
2021-09-24 21:07:03 +00:00
|
|
|
off_t tot;
|
2012-05-27 11:44:51 +00:00
|
|
|
|
|
|
|
rc = -1;
|
|
|
|
img = NULL;
|
|
|
|
|
|
|
|
fd = open(path, O_RDONLY);
|
|
|
|
if (fd < 0)
|
|
|
|
goto out;
|
|
|
|
|
|
|
|
rc = fstat(fd, &st);
|
|
|
|
if (rc)
|
|
|
|
goto out;
|
|
|
|
|
2021-09-24 21:07:03 +00:00
|
|
|
img = malloc(st.st_size + reserve);
|
|
|
|
if (!img)
|
2012-05-27 11:44:51 +00:00
|
|
|
goto out;
|
2021-09-24 21:07:03 +00:00
|
|
|
|
|
|
|
tot = 0;
|
|
|
|
while (tot < st.st_size) {
|
|
|
|
ssize_t rd = read(fd, img + tot, st.st_size - tot);
|
|
|
|
|
|
|
|
if (rd < 0)
|
|
|
|
goto out;
|
|
|
|
|
|
|
|
tot += rd;
|
|
|
|
|
|
|
|
if (!rd && tot < st.st_size) {
|
|
|
|
errno = EIO;
|
|
|
|
goto out;
|
|
|
|
}
|
2012-05-27 11:44:51 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
rc = 0;
|
|
|
|
*size = st.st_size;
|
|
|
|
out:
|
|
|
|
if (rc && img) {
|
2021-09-24 21:07:03 +00:00
|
|
|
free(img);
|
2012-05-27 11:44:51 +00:00
|
|
|
img = NULL;
|
|
|
|
}
|
|
|
|
if (fd >= 0)
|
|
|
|
close(fd);
|
|
|
|
|
|
|
|
return img;
|
|
|
|
}
|
|
|
|
|
|
|
|
static uint8_t
|
2021-09-24 21:07:01 +00:00
|
|
|
kwboot_hdr_csum8(const void *hdr)
|
2012-05-27 11:44:51 +00:00
|
|
|
{
|
2021-09-24 21:07:01 +00:00
|
|
|
const uint8_t *data = hdr;
|
|
|
|
uint8_t csum;
|
|
|
|
size_t size;
|
|
|
|
|
|
|
|
size = kwbheader_size_for_csum(hdr);
|
2012-05-27 11:44:51 +00:00
|
|
|
|
|
|
|
for (csum = 0; size-- > 0; data++)
|
|
|
|
csum += *data;
|
|
|
|
|
|
|
|
return csum;
|
|
|
|
}
|
|
|
|
|
2021-09-24 21:06:57 +00:00
|
|
|
static int
|
|
|
|
kwboot_img_is_secure(void *img)
|
|
|
|
{
|
|
|
|
struct opt_hdr_v1 *ohdr;
|
|
|
|
|
|
|
|
for_each_opt_hdr_v1 (ohdr, img)
|
|
|
|
if (ohdr->headertype == OPT_HDR_V1_SECURE_TYPE)
|
|
|
|
return 1;
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2021-09-24 21:07:03 +00:00
|
|
|
static void
|
|
|
|
kwboot_img_grow_hdr(void *img, size_t *size, size_t grow)
|
|
|
|
{
|
|
|
|
uint32_t hdrsz, datasz, srcaddr;
|
|
|
|
struct main_hdr_v1 *hdr = img;
|
|
|
|
uint8_t *data;
|
|
|
|
|
|
|
|
srcaddr = le32_to_cpu(hdr->srcaddr);
|
|
|
|
|
|
|
|
hdrsz = kwbheader_size(img);
|
|
|
|
data = (uint8_t *)img + srcaddr;
|
|
|
|
datasz = *size - srcaddr;
|
|
|
|
|
|
|
|
/* only move data if there is not enough space */
|
|
|
|
if (hdrsz + grow > srcaddr) {
|
|
|
|
size_t need = hdrsz + grow - srcaddr;
|
|
|
|
|
|
|
|
/* move data by enough bytes */
|
|
|
|
memmove(data + need, data, datasz);
|
|
|
|
|
|
|
|
hdr->srcaddr = cpu_to_le32(srcaddr + need);
|
|
|
|
*size += need;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (kwbimage_version(img) == 1) {
|
|
|
|
hdrsz += grow;
|
|
|
|
hdr->headersz_msb = hdrsz >> 16;
|
|
|
|
hdr->headersz_lsb = cpu_to_le16(hdrsz & 0xffff);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2012-05-27 11:44:51 +00:00
|
|
|
static int
|
2021-09-24 21:07:03 +00:00
|
|
|
kwboot_img_patch_hdr(void *img, size_t *size)
|
2012-05-27 11:44:51 +00:00
|
|
|
{
|
|
|
|
int rc;
|
2015-09-29 07:19:59 +00:00
|
|
|
struct main_hdr_v1 *hdr;
|
2021-09-24 21:06:58 +00:00
|
|
|
uint32_t srcaddr;
|
2012-05-27 11:44:51 +00:00
|
|
|
uint8_t csum;
|
2015-09-29 07:19:59 +00:00
|
|
|
size_t hdrsz = sizeof(*hdr);
|
|
|
|
int image_ver;
|
2021-09-24 21:06:57 +00:00
|
|
|
int is_secure;
|
2012-05-27 11:44:51 +00:00
|
|
|
|
|
|
|
rc = -1;
|
|
|
|
hdr = img;
|
|
|
|
|
2021-09-24 21:07:03 +00:00
|
|
|
if (*size < hdrsz) {
|
2012-05-27 11:44:51 +00:00
|
|
|
errno = EINVAL;
|
|
|
|
goto out;
|
|
|
|
}
|
|
|
|
|
2021-09-24 21:07:00 +00:00
|
|
|
image_ver = kwbimage_version(img);
|
2021-07-23 09:14:22 +00:00
|
|
|
if (image_ver != 0 && image_ver != 1) {
|
2015-09-29 07:19:59 +00:00
|
|
|
fprintf(stderr, "Invalid image header version\n");
|
|
|
|
errno = EINVAL;
|
|
|
|
goto out;
|
|
|
|
}
|
|
|
|
|
2021-09-24 21:07:01 +00:00
|
|
|
hdrsz = kwbheader_size(hdr);
|
2015-09-29 07:19:59 +00:00
|
|
|
|
2021-09-24 21:07:03 +00:00
|
|
|
if (*size < hdrsz) {
|
2021-07-23 09:14:21 +00:00
|
|
|
errno = EINVAL;
|
|
|
|
goto out;
|
|
|
|
}
|
|
|
|
|
2021-09-24 21:07:01 +00:00
|
|
|
csum = kwboot_hdr_csum8(hdr) - hdr->checksum;
|
2015-09-29 07:19:59 +00:00
|
|
|
if (csum != hdr->checksum) {
|
2012-05-27 11:44:51 +00:00
|
|
|
errno = EINVAL;
|
|
|
|
goto out;
|
|
|
|
}
|
|
|
|
|
2021-09-24 21:06:58 +00:00
|
|
|
if (image_ver == 0) {
|
|
|
|
struct main_hdr_v0 *hdr_v0 = img;
|
|
|
|
|
|
|
|
hdr_v0->nandeccmode = IBR_HDR_ECC_DISABLED;
|
|
|
|
hdr_v0->nandpagesize = 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
srcaddr = le32_to_cpu(hdr->srcaddr);
|
|
|
|
|
|
|
|
switch (hdr->blockid) {
|
|
|
|
case IBR_HDR_SATA_ID:
|
|
|
|
if (srcaddr < 1) {
|
|
|
|
errno = EINVAL;
|
|
|
|
goto out;
|
|
|
|
}
|
|
|
|
hdr->srcaddr = cpu_to_le32((srcaddr - 1) * 512);
|
|
|
|
break;
|
|
|
|
|
|
|
|
case IBR_HDR_SDIO_ID:
|
|
|
|
hdr->srcaddr = cpu_to_le32(srcaddr * 512);
|
|
|
|
break;
|
|
|
|
|
|
|
|
case IBR_HDR_PEX_ID:
|
|
|
|
if (srcaddr == 0xFFFFFFFF)
|
|
|
|
hdr->srcaddr = cpu_to_le32(hdrsz);
|
|
|
|
break;
|
2021-09-24 21:06:59 +00:00
|
|
|
|
|
|
|
case IBR_HDR_SPI_ID:
|
|
|
|
if (hdr->destaddr == cpu_to_le32(0xFFFFFFFF)) {
|
|
|
|
kwboot_printv("Patching destination and execution addresses from SPI/NOR XIP area to DDR area 0x00800000\n");
|
|
|
|
hdr->destaddr = cpu_to_le32(0x00800000);
|
|
|
|
hdr->execaddr = cpu_to_le32(0x00800000);
|
|
|
|
}
|
|
|
|
break;
|
2021-09-24 21:06:58 +00:00
|
|
|
}
|
|
|
|
|
2021-09-24 21:07:03 +00:00
|
|
|
if (hdrsz > le32_to_cpu(hdr->srcaddr) ||
|
|
|
|
*size < le32_to_cpu(hdr->srcaddr) + le32_to_cpu(hdr->blocksize)) {
|
|
|
|
errno = EINVAL;
|
|
|
|
goto out;
|
|
|
|
}
|
|
|
|
|
2021-09-24 21:06:57 +00:00
|
|
|
is_secure = kwboot_img_is_secure(img);
|
2012-05-27 11:44:51 +00:00
|
|
|
|
2021-09-24 21:06:57 +00:00
|
|
|
if (hdr->blockid != IBR_HDR_UART_ID) {
|
|
|
|
if (is_secure) {
|
|
|
|
fprintf(stderr,
|
|
|
|
"Image has secure header with signature for non-UART booting\n");
|
|
|
|
errno = EINVAL;
|
|
|
|
goto out;
|
|
|
|
}
|
|
|
|
|
|
|
|
kwboot_printv("Patching image boot signature to UART\n");
|
|
|
|
hdr->blockid = IBR_HDR_UART_ID;
|
|
|
|
}
|
2012-05-27 11:44:51 +00:00
|
|
|
|
2021-09-24 21:07:03 +00:00
|
|
|
if (hdrsz % KWBOOT_XM_BLKSZ) {
|
|
|
|
size_t offset = (KWBOOT_XM_BLKSZ - hdrsz % KWBOOT_XM_BLKSZ) %
|
|
|
|
KWBOOT_XM_BLKSZ;
|
|
|
|
|
|
|
|
if (is_secure) {
|
|
|
|
fprintf(stderr, "Cannot align image with secure header\n");
|
|
|
|
errno = EINVAL;
|
|
|
|
goto out;
|
|
|
|
}
|
|
|
|
|
|
|
|
kwboot_printv("Aligning image header to Xmodem block size\n");
|
|
|
|
kwboot_img_grow_hdr(img, size, offset);
|
|
|
|
}
|
|
|
|
|
2021-09-24 21:07:01 +00:00
|
|
|
hdr->checksum = kwboot_hdr_csum8(hdr) - csum;
|
2012-05-27 11:44:51 +00:00
|
|
|
|
2021-09-24 21:07:03 +00:00
|
|
|
*size = le32_to_cpu(hdr->srcaddr) + le32_to_cpu(hdr->blocksize);
|
2012-05-27 11:44:51 +00:00
|
|
|
rc = 0;
|
|
|
|
out:
|
|
|
|
return rc;
|
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
|
|
|
kwboot_usage(FILE *stream, char *progname)
|
|
|
|
{
|
2021-09-24 21:06:42 +00:00
|
|
|
fprintf(stream, "kwboot version %s\n", PLAIN_VERSION);
|
2012-05-27 11:44:51 +00:00
|
|
|
fprintf(stream,
|
2016-02-16 21:28:17 +00:00
|
|
|
"Usage: %s [OPTIONS] [-b <image> | -D <image> ] [-B <baud> ] <TTY>\n",
|
2014-10-22 10:13:21 +00:00
|
|
|
progname);
|
2012-05-27 11:44:51 +00:00
|
|
|
fprintf(stream, "\n");
|
2014-10-22 10:13:21 +00:00
|
|
|
fprintf(stream,
|
|
|
|
" -b <image>: boot <image> with preamble (Kirkwood, Armada 370/XP)\n");
|
|
|
|
fprintf(stream,
|
|
|
|
" -D <image>: boot <image> without preamble (Dove)\n");
|
|
|
|
fprintf(stream, " -d: enter debug mode\n");
|
|
|
|
fprintf(stream, " -a: use timings for Armada XP\n");
|
2015-05-29 11:25:04 +00:00
|
|
|
fprintf(stream, " -q <req-delay>: use specific request-delay\n");
|
|
|
|
fprintf(stream, " -s <resp-timeo>: use specific response-timeout\n");
|
2016-02-16 21:28:19 +00:00
|
|
|
fprintf(stream,
|
|
|
|
" -o <block-timeo>: use specific xmodem block timeout\n");
|
2012-05-27 11:44:51 +00:00
|
|
|
fprintf(stream, "\n");
|
|
|
|
fprintf(stream, " -t: mini terminal\n");
|
|
|
|
fprintf(stream, "\n");
|
|
|
|
fprintf(stream, " -B <baud>: set baud rate\n");
|
|
|
|
fprintf(stream, "\n");
|
|
|
|
}
|
|
|
|
|
|
|
|
int
|
|
|
|
main(int argc, char **argv)
|
|
|
|
{
|
|
|
|
const char *ttypath, *imgpath;
|
2021-09-24 21:06:55 +00:00
|
|
|
int rv, rc, tty, term;
|
2012-05-27 11:44:51 +00:00
|
|
|
void *bootmsg;
|
2014-10-22 10:13:21 +00:00
|
|
|
void *debugmsg;
|
2012-05-27 11:44:51 +00:00
|
|
|
void *img;
|
|
|
|
size_t size;
|
|
|
|
speed_t speed;
|
|
|
|
|
|
|
|
rv = 1;
|
|
|
|
tty = -1;
|
|
|
|
bootmsg = NULL;
|
2014-10-22 10:13:21 +00:00
|
|
|
debugmsg = NULL;
|
2012-05-27 11:44:51 +00:00
|
|
|
imgpath = NULL;
|
|
|
|
img = NULL;
|
|
|
|
term = 0;
|
|
|
|
size = 0;
|
|
|
|
speed = B115200;
|
|
|
|
|
|
|
|
kwboot_verbose = isatty(STDOUT_FILENO);
|
|
|
|
|
|
|
|
do {
|
2016-02-16 21:28:19 +00:00
|
|
|
int c = getopt(argc, argv, "hb:ptaB:dD:q:s:o:");
|
2012-05-27 11:44:51 +00:00
|
|
|
if (c < 0)
|
|
|
|
break;
|
|
|
|
|
|
|
|
switch (c) {
|
|
|
|
case 'b':
|
|
|
|
bootmsg = kwboot_msg_boot;
|
|
|
|
imgpath = optarg;
|
|
|
|
break;
|
|
|
|
|
2014-10-22 10:13:21 +00:00
|
|
|
case 'D':
|
|
|
|
bootmsg = NULL;
|
|
|
|
imgpath = optarg;
|
|
|
|
break;
|
|
|
|
|
|
|
|
case 'd':
|
|
|
|
debugmsg = kwboot_msg_debug;
|
|
|
|
break;
|
|
|
|
|
2012-05-27 11:44:51 +00:00
|
|
|
case 'p':
|
2021-09-24 21:06:55 +00:00
|
|
|
/* nop, for backward compatibility */
|
2012-05-27 11:44:51 +00:00
|
|
|
break;
|
|
|
|
|
|
|
|
case 't':
|
|
|
|
term = 1;
|
|
|
|
break;
|
|
|
|
|
2014-10-22 10:13:21 +00:00
|
|
|
case 'a':
|
|
|
|
msg_req_delay = KWBOOT_MSG_REQ_DELAY_AXP;
|
|
|
|
msg_rsp_timeo = KWBOOT_MSG_RSP_TIMEO_AXP;
|
|
|
|
break;
|
|
|
|
|
2015-05-29 11:25:04 +00:00
|
|
|
case 'q':
|
|
|
|
msg_req_delay = atoi(optarg);
|
|
|
|
break;
|
|
|
|
|
|
|
|
case 's':
|
|
|
|
msg_rsp_timeo = atoi(optarg);
|
|
|
|
break;
|
|
|
|
|
2016-02-16 21:28:19 +00:00
|
|
|
case 'o':
|
|
|
|
blk_rsp_timeo = atoi(optarg);
|
|
|
|
break;
|
|
|
|
|
2012-05-27 11:44:51 +00:00
|
|
|
case 'B':
|
|
|
|
speed = kwboot_tty_speed(atoi(optarg));
|
|
|
|
if (speed == -1)
|
|
|
|
goto usage;
|
|
|
|
break;
|
|
|
|
|
|
|
|
case 'h':
|
|
|
|
rv = 0;
|
|
|
|
default:
|
|
|
|
goto usage;
|
|
|
|
}
|
|
|
|
} while (1);
|
|
|
|
|
2014-10-22 10:13:21 +00:00
|
|
|
if (!bootmsg && !term && !debugmsg)
|
2012-05-27 11:44:51 +00:00
|
|
|
goto usage;
|
|
|
|
|
|
|
|
if (argc - optind < 1)
|
|
|
|
goto usage;
|
|
|
|
|
|
|
|
ttypath = argv[optind++];
|
|
|
|
|
|
|
|
tty = kwboot_open_tty(ttypath, speed);
|
|
|
|
if (tty < 0) {
|
|
|
|
perror(ttypath);
|
|
|
|
goto out;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (imgpath) {
|
2021-09-24 21:07:03 +00:00
|
|
|
img = kwboot_read_image(imgpath, &size, KWBOOT_XM_BLKSZ);
|
2012-05-27 11:44:51 +00:00
|
|
|
if (!img) {
|
|
|
|
perror(imgpath);
|
|
|
|
goto out;
|
|
|
|
}
|
|
|
|
|
2021-09-24 21:07:03 +00:00
|
|
|
rc = kwboot_img_patch_hdr(img, &size);
|
2012-05-27 11:44:51 +00:00
|
|
|
if (rc) {
|
|
|
|
fprintf(stderr, "%s: Invalid image.\n", imgpath);
|
|
|
|
goto out;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-10-22 10:13:21 +00:00
|
|
|
if (debugmsg) {
|
|
|
|
rc = kwboot_debugmsg(tty, debugmsg);
|
|
|
|
if (rc) {
|
|
|
|
perror("debugmsg");
|
|
|
|
goto out;
|
|
|
|
}
|
2018-07-03 16:10:30 +00:00
|
|
|
} else if (bootmsg) {
|
2012-05-27 11:44:51 +00:00
|
|
|
rc = kwboot_bootmsg(tty, bootmsg);
|
|
|
|
if (rc) {
|
|
|
|
perror("bootmsg");
|
|
|
|
goto out;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if (img) {
|
|
|
|
rc = kwboot_xmodem(tty, img, size);
|
|
|
|
if (rc) {
|
|
|
|
perror("xmodem");
|
|
|
|
goto out;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if (term) {
|
|
|
|
rc = kwboot_terminal(tty);
|
|
|
|
if (rc && !(errno == EINTR)) {
|
|
|
|
perror("terminal");
|
|
|
|
goto out;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
rv = 0;
|
|
|
|
out:
|
|
|
|
if (tty >= 0)
|
|
|
|
close(tty);
|
|
|
|
|
|
|
|
if (img)
|
2021-09-24 21:07:03 +00:00
|
|
|
free(img);
|
2012-05-27 11:44:51 +00:00
|
|
|
|
|
|
|
return rv;
|
|
|
|
|
|
|
|
usage:
|
|
|
|
kwboot_usage(rv ? stderr : stdout, basename(argv[0]));
|
|
|
|
goto out;
|
|
|
|
}
|