6#include <QtCore/QElapsedTimer>
8#include <QtCore/QThread>
16 , _sikRadio (sikRadio)
23 qCDebug(FirmwareUpgradeLog) <<
"open:" << portName;
25 _port.setPortName (portName);
32 if (!_port.open(QIODevice::ReadWrite)) {
33 _errorString = tr(
"Open failed on port %1: %2").arg(portName, _port.errorString());
39 QThread::msleep(1000);
44QString Bootloader::_getNextLine(
int timeoutMsecs)
47 QElapsedTimer timeout;
51 while (timeout.elapsed() < timeoutMsecs) {
54 if (_port.read(&oneChar, 1) > 0) {
55 if (oneChar ==
'\r') {
58 }
else if (oneChar ==
'\n' && foundCR) {
73 if (_inBootloaderMode) {
74 qCDebug(FirmwareUpgradeLog) <<
"Radio in bootloader mode already";
75 if (!_get3DRRadioBoardId(_boardID)) {
79 qCDebug(FirmwareUpgradeLog) <<
"Radio in normal mode";
84 if (!_port.waitForReadyRead(2000)) {
85 _errorString = tr(
"Unable to put radio into command mode +++");
88 QByteArray bytes = _port.readAll();
89 if (!bytes.contains(
"OK")) {
90 _errorString = tr(
"Radio did not respond to command mode");
96 QString echo = _getNextLine(2000);
97 if (echo.isEmpty() || echo !=
"ATI2") {
98 _errorString = tr(
"Radio did not respond to ATI2 command");
101 QString boardIdStr = _getNextLine(2000);
103 _boardID = boardIdStr.toInt(&ok);
104 if (boardIdStr.isEmpty() || !ok) {
105 _errorString = tr(
"Radio did not return board id");
109 bootloaderVersion = 0;
118 if (!_protoGetDevice(INFO_BL_REV, _bootloaderVersion)) {
121 if (_bootloaderVersion < BL_REV_MIN || _bootloaderVersion > BL_REV_MAX) {
122 _errorString = tr(
"Found unsupported bootloader version: %1").arg(_bootloaderVersion);
125 if (!_protoGetDevice(INFO_BOARD_ID, _boardID)) {
128 if (!_protoGetDevice(INFO_FLASH_SIZE, _boardFlashSize)) {
129 qWarning() << _errorString;
136 if (_boardID ==
boardIDPX4FMUV2 && _bootloaderVersion >= _bootloaderVersionV2CorrectFlash && _boardFlashSize > _flashSizeSmall) {
140 bootloaderVersion = _bootloaderVersion;
142 flashSize = _boardFlashSize;
148 qCDebug(FirmwareUpgradeLog) <<
"getBoardInfo failed:" << _errorString;
149 _errorString.prepend(tr(
"Get Board Info: "));
155 if (_sikRadio && !_inBootloaderMode) {
156 _write(
"AT&UPDATE\r\n");
157 if (!_port.waitForReadyRead(1500)) {
158 _errorString = tr(
"Unable to reboot radio (ready read)");
172 uint32_t timeout = _eraseTimeout;
175 if(_boardFlashSize > 2000 * 1024) {
177 timeout += (_boardFlashSize / 1e6) * 4000;
181 if (!_sendCommand(PROTO_CHIP_ERASE, timeout)) {
182 _errorString = tr(
"Erase failed: %1").arg(_errorString);
192 return _binProgram(image);
194 return _ihxProgram(image);
201 if (_sikRadio && !_inBootloaderMode) {
202 qCDebug(FirmwareUpgradeLog) <<
"reboot ATZ";
204 success = _write(
"ATZ\r\n");
206 qCDebug(FirmwareUpgradeLog) <<
"reboot";
207 success = _write(PROTO_BOOT) && _write(PROTO_EOC);
211 QThread::msleep(1000);
216bool Bootloader::_write(
const char* data)
218 return _write((uint8_t*)data, qstrlen(data));
221bool Bootloader::_write(
const uint8_t* data, qint64 maxSize)
223 qint64 bytesWritten = _port.write((
const char*)data, maxSize);
224 if (bytesWritten == -1) {
225 _errorString = tr(
"Write failed: %1").arg(_port.errorString());
226 qWarning() << _errorString;
229 if (bytesWritten != maxSize) {
230 _errorString = tr(
"Incorrect number of bytes returned for write: actual(%1) expected(%2)").arg(bytesWritten).arg(maxSize);
231 qWarning() << _errorString;
238bool Bootloader::_write(
const uint8_t
byte)
240 uint8_t buf[1] = {
byte };
241 return _write(buf, 1);
244bool Bootloader::_read(uint8_t* data, qint64 cBytesExpected,
int readTimeout)
246 QElapsedTimer timeout;
249 while (_port.bytesAvailable() < cBytesExpected) {
250 if (timeout.elapsed() > readTimeout) {
251 _errorString = tr(
"Timeout waiting for bytes to be available");
254 _port.waitForReadyRead(100);
258 bytesRead = _port.read((
char *)data, cBytesExpected);
260 if (bytesRead != cBytesExpected) {
261 _errorString = tr(
"Read failed: error: %1").arg(_port.errorString());
270bool Bootloader::_getCommandResponse(
int responseTimeout)
274 if (!_read(response, 2, responseTimeout)) {
275 _errorString.prepend(tr(
"Get Command Response: "));
280 if (response[0] != PROTO_INSYNC) {
281 _errorString = tr(
"Invalid sync response: 0x%1 0x%2").arg(response[0], 2, 16, QLatin1Char(
'0')).arg(response[1], 2, 16, QLatin1Char(
'0'));
283 }
else if (response[0] == PROTO_INSYNC && response[1] == PROTO_BAD_SILICON_REV) {
284 _errorString = tr(
"This board is using a microcontroller with faulty silicon and an incorrect configuration and should be put out of service.");
286 }
else if (response[1] != PROTO_OK) {
287 QString responseCode = tr(
"Unknown response code");
288 if (response[1] == PROTO_FAILED) {
289 responseCode =
"PROTO_FAILED";
290 }
else if (response[1] == PROTO_INVALID) {
291 responseCode =
"PROTO_INVALID";
293 _errorString = tr(
"Command failed: 0x%1 (%2)").arg(response[1], 2, 16, QLatin1Char(
'0')).arg(responseCode);
303bool Bootloader::_protoGetDevice(uint8_t param, uint32_t& value)
305 uint8_t buf[3] = { PROTO_GET_DEVICE, param, PROTO_EOC };
307 if (!_write(buf,
sizeof(buf))) {
310 if (!_read((uint8_t*)&value,
sizeof(value))) {
313 if (!_getCommandResponse()) {
320 _errorString.prepend(tr(
"Get Device: "));
327bool Bootloader::_sendCommand(
const uint8_t cmd,
int responseTimeout)
329 uint8_t buf[2] = { cmd, PROTO_EOC };
331 if (!_write(buf, 2)) {
336 if (!_getCommandResponse(responseTimeout)) {
343 _errorString.prepend(tr(
"Send Command: "));
350 if (!firmwareFile.open(QIODevice::ReadOnly)) {
351 _errorString = tr(
"Unable to open firmware file %1: %2").arg(image->
binFilename(), firmwareFile.errorString());
354 uint32_t imageSize = (uint32_t)firmwareFile.size();
357 if ((imageSize % 4) != 0) {
358 _errorString = tr(
"Firmware image size (%1 bytes) is not a multiple of 4").arg(imageSize);
362 uint8_t imageBuf[PROG_MULTI_MAX];
363 uint32_t bytesSent = 0;
366 static_assert((PROG_MULTI_MAX <= 0x8F) && ((PROG_MULTI_MAX % 4) == 0));
368 while (bytesSent < imageSize) {
369 int bytesToSend = imageSize - bytesSent;
370 if (bytesToSend > (
int)
sizeof(imageBuf)) {
371 bytesToSend = (int)
sizeof(imageBuf);
374 int bytesRead = firmwareFile.read((
char *)imageBuf, bytesToSend);
375 if (bytesRead == -1 || bytesRead != bytesToSend) {
376 _errorString = tr(
"Firmware file read failed: %1").arg(firmwareFile.errorString());
381 if (_write(PROTO_PROG_MULTI) &&
382 _write((uint8_t)bytesToSend) &&
383 _write(imageBuf, bytesToSend) &&
385 if (_getCommandResponse()) {
390 _errorString = tr(
"Flash failed: %1 at address 0x%2").arg(_errorString).arg(bytesSent, 8, 16, QLatin1Char(
'0'));
394 bytesSent += bytesToSend;
397 _imageCRC =
QGC::crc32((uint8_t *)imageBuf, bytesToSend, _imageCRC);
401 firmwareFile.close();
404 while (bytesSent < _boardFlashSize) {
405 const uint8_t fill = 0xFF;
416 uint32_t bytesSent = 0;
418 for (uint16_t index=0; index<image->
ihxBlockCount(); index++) {
420 uint16_t flashAddress;
423 if (!image->
ihxGetBlock(index, flashAddress, bytes)) {
424 _errorString = tr(
"Unable to retrieve block from ihx: index %1").arg(index);
428 qCDebug(FirmwareUpgradeVerboseLog) << QString(
"Bootloader::_ihxProgram - address:0x%1 size:%2 block:%3").arg(flashAddress, 8, 16, QLatin1Char(
'0')).arg(bytes.length()).arg(index);
433 if (_write(PROTO_LOAD_ADDRESS) &&
434 _write(flashAddress & 0xFF) &&
435 _write((flashAddress >> 8) & 0xFF) &&
438 if (_getCommandResponse()) {
444 _errorString = tr(
"Unable to set flash start address: 0x%2").arg(flashAddress, 8, 16, QLatin1Char(
'0'));
451 uint16_t bytesLeftToWrite = bytes.length();
453 while (bytesLeftToWrite > 0) {
454 uint8_t bytesToWrite;
456 if (bytesLeftToWrite > PROG_MULTI_MAX) {
457 bytesToWrite = PROG_MULTI_MAX;
459 bytesToWrite = bytesLeftToWrite;
463 if (_write(PROTO_PROG_MULTI) &&
464 _write(bytesToWrite) &&
465 _write(&((uint8_t *)bytes.data())[bytesIndex], bytesToWrite) &&
468 if (_getCommandResponse()) {
473 _errorString = tr(
"Flash failed: %1 at address 0x%2").arg(_errorString).arg(flashAddress, 8, 16, QLatin1Char(
'0'));
477 bytesIndex += bytesToWrite;
478 bytesLeftToWrite -= bytesToWrite;
479 bytesSent += bytesToWrite;
493 ret = _verifyBytes(image);
507 return _binVerifyBytes(image);
509 return _ihxVerifyBytes(image);
516 if (!firmwareFile.open(QIODevice::ReadOnly)) {
517 _errorString = tr(
"Unable to open firmware file %1: %2").arg(image->
binFilename(), firmwareFile.errorString());
520 uint32_t imageSize = (uint32_t)firmwareFile.size();
522 if ((imageSize % 4) != 0) {
523 _errorString = tr(
"Firmware image size (%1 bytes) is not a multiple of 4").arg(imageSize);
527 if (!_sendCommand(PROTO_CHIP_VERIFY)) {
531 uint8_t fileBuf[READ_MULTI_MAX];
532 uint8_t readBuf[READ_MULTI_MAX];
533 uint32_t bytesVerified = 0;
535 static_assert((READ_MULTI_MAX <= 0x8F) && ((READ_MULTI_MAX % 4) == 0));
537 while (bytesVerified < imageSize) {
538 int bytesToRead = imageSize - bytesVerified;
539 if (bytesToRead > (
int)
sizeof(readBuf)) {
540 bytesToRead = (int)
sizeof(readBuf);
543 int bytesRead = firmwareFile.read((
char *)fileBuf, bytesToRead);
544 if (bytesRead == -1 || bytesRead != bytesToRead) {
545 _errorString = tr(
"Firmware file read failed: %1").arg(firmwareFile.errorString());
550 if (_write(PROTO_READ_MULTI) &&
551 _write((uint8_t)bytesToRead) &&
554 if (_read(readBuf, bytesToRead)) {
555 if (_getCommandResponse()) {
561 _errorString = tr(
"Read failed: %1 at address: 0x%2").arg(_errorString).arg(bytesVerified, 8, 16, QLatin1Char(
'0'));
565 for (
int i=0; i<bytesToRead; i++) {
566 if (fileBuf[i] != readBuf[i]) {
567 _errorString = tr(
"Compare failed: expected(0x%1) actual(0x%2) at address: 0x%3").arg(fileBuf[i], 2, 16, QLatin1Char(
'0')).arg(readBuf[i], 2, 16, QLatin1Char(
'0')).arg(bytesVerified + i, 8, 16, QLatin1Char(
'0'));
572 bytesVerified += bytesToRead;
577 firmwareFile.close();
585 uint32_t bytesVerified = 0;
587 for (uint16_t index=0; index<image->
ihxBlockCount(); index++) {
589 uint16_t readAddress;
590 QByteArray imageBytes;
592 if (!image->
ihxGetBlock(index, readAddress, imageBytes)) {
593 _errorString = tr(
"Unable to retrieve block from ihx: index %1").arg(index);
597 qCDebug(FirmwareUpgradeLog) << QString(
"Bootloader::_ihxVerifyBytes - address:0x%1 size:%2 block:%3").arg(readAddress, 8, 16, QLatin1Char(
'0')).arg(imageBytes.length()).arg(index);
602 if (_write(PROTO_LOAD_ADDRESS) &&
603 _write(readAddress & 0xFF) &&
604 _write((readAddress >> 8) & 0xFF) &&
607 if (_getCommandResponse()) {
613 _errorString = tr(
"Unable to set read start address: 0x%2").arg(readAddress, 8, 16, QLatin1Char(
'0'));
620 uint16_t bytesLeftToRead = imageBytes.length();
622 while (bytesLeftToRead > 0) {
624 uint8_t readBuf[READ_MULTI_MAX];
626 if (bytesLeftToRead > READ_MULTI_MAX) {
627 bytesToRead = READ_MULTI_MAX;
629 bytesToRead = bytesLeftToRead;
633 if (_write(PROTO_READ_MULTI) &&
634 _write(bytesToRead) &&
637 if (_read(readBuf, bytesToRead)) {
638 if (_getCommandResponse()) {
644 _errorString = tr(
"Read failed: %1 at address: 0x%2").arg(_errorString).arg(readAddress, 8, 16, QLatin1Char(
'0'));
650 for (
int i=0; i<bytesToRead; i++) {
651 if ((uint8_t)imageBytes[bytesIndex + i] != readBuf[i]) {
652 _errorString = tr(
"Compare failed: expected(0x%1) actual(0x%2) at address: 0x%3")
653 .arg(
static_cast<qulonglong
>(
static_cast<quint8
>(imageBytes[bytesIndex + i])), 2, 16, QLatin1Char(
'0'))
654 .arg(
static_cast<qulonglong
>(
static_cast<quint8
>(readBuf[i])), 2, 16, QLatin1Char(
'0'))
655 .arg(
static_cast<qulonglong
>(readAddress + i), 8, 16, QLatin1Char(
'0'));
660 bytesVerified += bytesToRead;
661 bytesIndex += bytesToRead;
662 bytesLeftToRead -= bytesToRead;
672bool Bootloader::_verifyCRC(
void)
674 uint8_t buf[2] = { PROTO_GET_CRC, PROTO_EOC };
676 quint32 flashCRC = 0;
679 if (_write(buf, 2)) {
681 if (_read((uint8_t*)&flashCRC,
sizeof(flashCRC), _verifyTimeout)) {
682 if (_getCommandResponse()) {
691 if (_imageCRC != flashCRC) {
692 _errorString = tr(
"CRC mismatch: board(0x%1) file(0x%2)").arg(flashCRC, 4, 16, QLatin1Char(
'0')).arg(_imageCRC, 4, 16, QLatin1Char(
'0'));
699bool Bootloader::_syncWorker(
void)
702 if (_sendCommand(PROTO_GET_SYNC)) {
703 _inBootloaderMode =
true;
706 _errorString.prepend(
"Sync: ");
711bool Bootloader::_sync(
void)
715 bool success =
false;
716 for (
int i=0; i<3; i++) {
717 success = _syncWorker();
726bool Bootloader::_get3DRRadioBoardId(uint32_t& boardID)
728 uint8_t buf[2] = { PROTO_GET_DEVICE, PROTO_EOC };
730 if (!_write(buf,
sizeof(buf))) {
735 if (!_read((uint8_t*)buf, 2)) {
738 if (!_getCommandResponse()) {
744 _bootloaderVersion = 0;
750 _errorString.prepend(tr(
"Get Board Id: "));
#define QGC_LOGGING_CATEGORY(name, categoryStr)
bool program(const FirmwareImage *image)
void updateProgress(int curr, int total)
Signals progress indicator for long running bootloader utility routines.
static const int boardIDPX4FMUV2
PX4 V2 board, as from USB PID.
bool initFlashSequence(void)
static const int boardIDPX4FMUV3
Bootloader(bool sikRadio, QObject *parent=0)
This class manages interactions with the bootloader.
bool open(const QString portName)
bool verify(const FirmwareImage *image)
bool getBoardInfo(uint32_t &bootloaderVersion, uint32_t &boardID, uint32_t &flashSize)
Support for Intel Hex firmware file.
bool imageIsBinFormat(void) const
QString binFilename(void) const
bool ihxGetBlock(uint16_t index, uint16_t &address, QByteArray &bytes) const
uint32_t imageSize(void) const
Returns the number of bytes in the image.
uint16_t ihxBlockCount(void) const
bool waitForReadyRead(int msecs=30000) override
Returns true only for input that was not announced before.
QByteArray line(std::string_view command)
command as one CR/LF-terminated line.
quint32 crc32(const quint8 *src, unsigned len, unsigned state)