1
0
mirror of https://bitbucket.org/librepilot/librepilot.git synced 2025-01-18 03:52:11 +01:00

OP-21/Flight Bootloader PC APP- Finished, not tested on HW though.

git-svn-id: svn://svn.openpilot.org/OpenPilot/trunk@1543 ebee16cc-31ac-478f-84a7-5cbb03baadba
This commit is contained in:
zedamota 2010-09-05 23:15:45 +00:00 committed by zedamota
parent 44b41c32d2
commit 01b4c5d0e3
3 changed files with 184 additions and 83 deletions

View File

@ -4,10 +4,12 @@
#include "op_dfu.h" #include "op_dfu.h"
#include <QStringList> #include <QStringList>
#define PRIVATE false
int main(int argc, char *argv[]) int main(int argc, char *argv[])
{ {
QCoreApplication a(argc, argv); QCoreApplication a(argc, argv);
if(argc>1) if(argc>1||!PRIVATE)
{ {
bool verify; bool verify;
bool debug=false; bool debug=false;
@ -20,36 +22,50 @@ int main(int argc, char *argv[])
{ {
args<<argv[i]; args<<argv[i];
} }
if(args.contains("-debug"))
if(args.contains("-?")) debug=true;
if(args.contains("-?")||(!PRIVATE && argc==1))
{ {
cout<<"| Commands\n"; cout<<"_________________________________________________________________________\n";
cout<<"| \n"; cout<<"| Commands |\n";
cout<<"| -ls : lists available devices\n"; cout<<"| |\n";
cout<<"| -p <file> : program hw (requires:-d - optionals:-v,-w)\n"; cout<<"| -ls : lists available devices |\n";
cout<<"| -v : verify (requires:-d)\n"; cout<<"| -p <file> : program hw (requires:-d - optionals:-v,-w) |\n";
cout<<"| -dn <file> : download firmware to file (requires:-d)\n"; cout<<"| -v : verify (requires:-d) |\n";
cout<<"| -dd <file> : download discription (requires:-d)\n"; cout<<"| -dn <file> : download firmware to file (requires:-d) |\n";
cout<<"| -d <number Of Device : (requires: -p or -dn)\n"; // cout<<"| -dd <file> : download discription (requires:-d) |\n";
cout<<"| -w <description> : (requires: -p)\n"; cout<<"| -d <number Of Device : (requires: -p or -dn) |\n";
cout<<"| -ca <file> : compares byte by byte current firmware with file\n"; cout<<"| -w <description> : (requires: -p) |\n";
cout<<"| -ch <file> : compares hash of current firmware with file\n"; cout<<"| -ca <file> : compares byte by byte current firmware with file|\n";
cout<<"| -s : requests status of device\n"; cout<<"| -ch <file> : compares hash of current firmware with file |\n";
cout<<"| -s : requests status of device |\n";
cout<<"| -r : resets the device |\n";
cout<<"| -j : exits bootloader and jumps to user FW |\n";
cout<<"| -debug : prints debug information |\n";
cout<<"| |\n";
cout<<"| examples: |\n";
cout<<"| |\n";
cout<<"| program and verify device #1 |\n";
cout<<"| OPUploadTool -p c:/OpenPilot.bin -w \"Openpilot Firmware\" -v -d 1 |\n";
cout<<"| |\n";
cout<<"| Perform a quick compare of FW in file with FW in device #2 |\n";
cout<<"| OPUploadTool -ch c:/OpenPilot.bin -d 2 |\n";
cout<<"|________________________________________________________________________|\n";
return 0; return 0;
} }
else if(args.contains(DOWNDESCRIPTION)) // else if(args.contains(DOWNDESCRIPTION))
{ // {
action=OP_DFU::downdesc; // action=OP_DFU::downdesc;
if(args.contains(DEVICE)) // if(args.contains(DEVICE))
{ // {
if(args.indexOf(DEVICE)+1<args.length()) // if(args.indexOf(DEVICE)+1<args.length())
{ // {
device=(args[args.indexOf(DEVICE)+1]).toInt(); // device=(args[args.indexOf(DEVICE)+1]).toInt();
} // }
} // }
} // }
else if(args.contains(PROGRAMFW)) else if(args.contains(PROGRAMFW))
{ {
if(args.contains(VERIFY)) if(args.contains(VERIFY))
@ -66,7 +82,7 @@ int main(int argc, char *argv[])
} }
} }
action=OP_DFU::program; action=OP_DFU::actionProgram;
if(args.contains(DEVICE)) if(args.contains(DEVICE))
{ {
if(args.indexOf(DEVICE)+1<args.length()) if(args.indexOf(DEVICE)+1<args.length())
@ -84,7 +100,7 @@ int main(int argc, char *argv[])
return -1; return -1;
} }
if(args.contains(VERIFY)) if(args.contains(VERIFY))
action=OP_DFU::programandverify; action=OP_DFU::actionProgramAndVerify;
} }
else if(args.contains(COMPAREHASH) || args.contains(COMPAREALL)) else if(args.contains(COMPAREHASH) || args.contains(COMPAREALL))
{ {
@ -92,12 +108,12 @@ int main(int argc, char *argv[])
if(args.contains(COMPAREHASH)) if(args.contains(COMPAREHASH))
{ {
index=args.indexOf(COMPAREHASH); index=args.indexOf(COMPAREHASH);
action=OP_DFU::comparehash; action=OP_DFU::actionCompareHash;
} }
else else
{ {
index=args.indexOf(COMPAREALL); index=args.indexOf(COMPAREALL);
action=OP_DFU::compareall; action=OP_DFU::actionCompareAll;
} }
if(args.contains(DEVICE)) if(args.contains(DEVICE))
{ {
@ -120,7 +136,7 @@ int main(int argc, char *argv[])
{ {
int index; int index;
index=args.indexOf(DOWNLOAD); index=args.indexOf(DOWNLOAD);
action=OP_DFU::download; action=OP_DFU::actionDownload;
if(args.contains(DEVICE)) if(args.contains(DEVICE))
{ {
@ -141,32 +157,46 @@ int main(int argc, char *argv[])
} }
else if(args.contains(STATUSREQUEST)) else if(args.contains(STATUSREQUEST))
{ {
action=OP_DFU::statusreq; action=OP_DFU::actionStatusReq;
} }
else if(args.contains(RESET))
{
action=OP_DFU::actionReset;
}
else if(args.contains(JUMP))
{
action=OP_DFU::actionJump;
}
else if(args.contains(LISTDEVICES)) else if(args.contains(LISTDEVICES))
{ {
action=OP_DFU::listdevs; action=OP_DFU::actionListDevs;
} }
if((file.isEmpty()|| device==-1) && action!=OP_DFU::downdesc && action!=OP_DFU::statusreq && action!=OP_DFU::listdevs) if((file.isEmpty()|| device==-1) && action!=OP_DFU::actionReset && action!=OP_DFU::actionStatusReq && action!=OP_DFU::actionListDevs&& action!=OP_DFU::actionJump)
{ {
cout<<"wrong parameters"; cout<<"wrong parameters";
return -1; return -1;
} }
if(debug)
{
qDebug()<<"Action="<<(int)action;
qDebug()<<"File="<<file;
qDebug()<<"Device="<<device;
qDebug()<<"Action="<<action;
qDebug()<<"Desctription"<<description;
}
// qDebug()<<"Action="<<(int)action; ///////////////////////////////////ACTIONS START///////////////////////////////////////////////////
// qDebug()<<"File="<<file;
// qDebug()<<"Device="<<device;
// qDebug()<<"Action="<<action;
OP_DFU dfu(debug); OP_DFU dfu(debug);
if(!dfu.enterDFU(0)) if(!dfu.enterDFU(0))
{ {
cout<<"Could not enter DFU mode\n"; cout<<"Could not enter DFU mode\n";
return -1; return -1;
} }
if(action!=OP_DFU::statusreq) if(!(action==OP_DFU::actionStatusReq || action==OP_DFU::actionReset || action== OP_DFU::actionJump))
{ {
dfu.findDevices(); dfu.findDevices();
if(action==OP_DFU::listdevs) if(action==OP_DFU::actionListDevs)
{ {
cout<<"Found "<<dfu.numberOfDevices<<"\n"; cout<<"Found "<<dfu.numberOfDevices<<"\n";
for(int x=0;x<dfu.numberOfDevices;++x) for(int x=0;x<dfu.numberOfDevices;++x)
@ -184,8 +214,6 @@ int main(int argc, char *argv[])
} }
return 0; return 0;
} }
else if (action==OP_DFU::program)
{
if(device>dfu.numberOfDevices) if(device>dfu.numberOfDevices)
{ {
cout<<"Error:Invalid Device"; cout<<"Error:Invalid Device";
@ -196,6 +224,8 @@ int main(int argc, char *argv[])
cout<<"Error:Could not enter DFU mode\n"; cout<<"Error:Could not enter DFU mode\n";
return -1; return -1;
} }
if (action==OP_DFU::actionProgram)
{
OP_DFU::Status retstatus=dfu.UploadFirmware(file.toAscii(),verify); OP_DFU::Status retstatus=dfu.UploadFirmware(file.toAscii(),verify);
if(retstatus!=OP_DFU::Last_operation_Success) if(retstatus!=OP_DFU::Last_operation_Success)
{ {
@ -213,30 +243,42 @@ int main(int argc, char *argv[])
} }
cout<<"Uploading Succeded!\n"; cout<<"Uploading Succeded!\n";
} }
else if (action==OP_DFU::download) else if (action==OP_DFU::actionDownload)
{ {
if(device>dfu.numberOfDevices)
{
cout<<"Error:Invalid Device";
return -1;
}
if(!dfu.enterDFU(device))
{
cout<<"Error:Could not enter DFU mode\n";
return -1;
}
qint32 size=((OP_DFU::device)dfu.devices[device]).SizeOfCode; qint32 size=((OP_DFU::device)dfu.devices[device]).SizeOfCode;
bool ret=dfu.SaveByteArrayToFile(file.toAscii(),dfu.StartDownload(size,OP_DFU::FW)); bool ret=dfu.SaveByteArrayToFile(file.toAscii(),dfu.StartDownload(size,OP_DFU::FW));
return ret; return ret;
} }
else if(action==OP_DFU::downdesc) // else if(action==OP_DFU::downdesc)
// {
// int size=((OP_DFU::device)dfu.devices[device]).SizeOfDesc;
// cout<<"Description:"<<dfu.DownloadDescription(size).toLatin1().data()<<"\n";
// }
else if(action==OP_DFU::actionCompareHash)
{ {
int size=((OP_DFU::device)dfu.devices[device]).SizeOfDesc; dfu.CompareFirmware(file.toAscii(),OP_DFU::hashcompare);
cout<<"Description:"<<dfu.DownloadDescription(size).toLatin1().data(); return 1;
}
else if(action==OP_DFU::actionCompareAll)
{
dfu.CompareFirmware(file.toAscii(),OP_DFU::bytetobytecompare);
return 1;
} }
} }
else if(action==OP_DFU::actionStatusReq)
{
cout<<"Current device status="<<dfu.StatusToString(dfu.StatusRequest()).toLatin1().data()<<"\n";
}
else if(action==OP_DFU::actionReset)
{
dfu.ResetDevice();
}
else if(action== OP_DFU::actionJump)
{
dfu.JumpToApp();
}
return 0; return 0;
} }

View File

@ -499,6 +499,56 @@ OP_DFU::Status OP_DFU::UploadFirmware(const QString &sfile, const bool &verify)
} }
return ret; return ret;
} }
OP_DFU::Status OP_DFU::CompareFirmware(const QString &sfile, const CompareType &type)
{
cout<<"Starting Firmware Compare...\n";
QFile file(sfile);
if (!file.open(QIODevice::ReadOnly))
{
if(debug)
qDebug()<<"Cant open file";
return OP_DFU::abort;
}
QByteArray arr=file.readAll();
QByteArray hash=QCryptographicHash::hash(arr,QCryptographicHash::Sha1);
if(debug)
qDebug()<<"hash size="<<hash.length()<<" -"<<hash;
if(debug)
qDebug()<<"Bytes Loaded="<<arr.length();
if(arr.length()%4!=0)
{
int pad=arr.length()/4;
++pad;
pad=pad*4;
pad=pad-arr.length();
arr.append(QByteArray(pad,255));
}
if(type==OP_DFU::hashcompare)
{
if(hash==StartDownload(hash.length(),OP_DFU::Hash))
{
cout<<"Compare Successfull Hashes MATCH!\n";
}
else
{
cout<<"Compare failed Hashes DONT MATCH!\n";
}
return StatusRequest();
}
else
{
if(arr==StartDownload(arr.length(),OP_DFU::FW))
{
cout<<"Compare Successfull ALL Bytes MATCH!\n";
}
else
{
cout<<"Compare failed Bytes DONT MATCH!\n";
}
return StatusRequest();
}
}
void OP_DFU::CopyWords(char *source, char *destination, int count) void OP_DFU::CopyWords(char *source, char *destination, int count)
{ {
for (int x=0;x<count;x=x+4) for (int x=0;x<count;x=x+4)

View File

@ -14,15 +14,16 @@ using namespace std;
//Command Line Options //Command Line Options
#define DOWNLOAD "-dn" //done #define DOWNLOAD "-dn" //done
#define DEVICE "-d" //done #define DEVICE "-d" //done
#define DOWNDESCRIPTION "-dd" //done //#define DOWNDESCRIPTION "-dd" //done
#define PROGRAMFW "-p" //done #define PROGRAMFW "-p" //done
#define PROGRAMDESC "-w" //done #define PROGRAMDESC "-w" //done
#define VERIFY "-v" #define VERIFY "-v" //done
#define COMPAREHASH "-ch" #define COMPAREHASH "-ch"
#define COMPAREALL "-ca" #define COMPAREALL "-ca"
#define STATUSREQUEST "-s" //done #define STATUSREQUEST "-s" //done
#define LISTDEVICES "-ls" //done #define LISTDEVICES "-ls" //done
#define RESET "-r"
#define JUMP "-j"
class OP_DFU class OP_DFU
{ {
@ -33,6 +34,12 @@ public:
Hash, Hash,
Descript Descript
}; };
enum CompareType
{
hashcompare,
bytetobytecompare
};
enum Status enum Status
{ {
DFUidle, DFUidle,
@ -50,14 +57,15 @@ public:
}; };
enum Actions enum Actions
{ {
program, actionProgram,
programandverify, actionProgramAndVerify,
download, actionDownload,
compareall, actionCompareAll,
comparehash, actionCompareHash,
listdevs, actionListDevs,
statusreq, actionStatusReq,
downdesc actionReset,
actionJump
}; };
enum Commands enum Commands
@ -107,6 +115,7 @@ public:
QList<device> devices; QList<device> devices;
int numberOfDevices; int numberOfDevices;
QString StatusToString(OP_DFU::Status); QString StatusToString(OP_DFU::Status);
OP_DFU::Status CompareFirmware(const QString &sfile, const CompareType &type);
private: private:
bool debug; bool debug;
int RWFlags; int RWFlags;