Skip to content

Commit

Permalink
Merge branch 'bugfix/swupdate_swu_file_check' into 'main'
Browse files Browse the repository at this point in the history
Fix for Issue #297 Add checks to ensure swu file is available for swupdate flash command

See merge request syntron/support/csr/ifm3d/ifm3d!412
  • Loading branch information
NikitaS20 committed Nov 18, 2024
2 parents f43f6c7 + cd6fbc1 commit aa440a8
Show file tree
Hide file tree
Showing 2 changed files with 81 additions and 65 deletions.
144 changes: 80 additions & 64 deletions modules/tools/src/libifm3d_tools/common/swupdater/flash_sw_app.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,9 @@
# include <fcntl.h>
#endif

const std::string ASCII_FORMAT_MAGIC_NUMBER = "070701";
const std::string CRC_FORMAT_MAGIC_NUMBER = "070702";

ifm3d::FlashSWApp::~FlashSWApp() {}

void
Expand Down Expand Up @@ -84,72 +87,85 @@ ifm3d::FlashSWApp::Execute(CLI::App* app)
});
}

{
// Read the file in
if (!swu_file.empty())
auto validateSwuFileHeader = [](std::string& swu_file) -> bool {
std::ifstream file(swu_file, std::ios::binary);

if (file.good())
{
char magic[6];
file.read(magic, 6);
std::string magicStr(magic, 6);

return (magicStr == ASCII_FORMAT_MAGIC_NUMBER ||
magicStr == CRC_FORMAT_MAGIC_NUMBER);
}
else
{
//// Check if provided swu_file exists
// const std::filesystem::path infile =
// std::filesystem::u8path(swu_file);

// if (!std::filesystem::exists(infile) ||
// infile.extension().wstring() != L".swu")
// {
// std::cerr << "ifm3d error: File not found or invalid file.\n";
// return;
// }

// Reboot to recovery if not already in recovery
if (!swupdater->WaitForRecovery(-1))
{
if (!quiet)
{
std::cout << "Rebooting device to recovery mode..."
<< std::endl;
}
swupdater->RebootToRecovery();
if (!swupdater->WaitForRecovery(get_remaining_timeout()))
{
if (!quiet)
{
std::cout << "Timed out waiting for recovery mode"
<< std::endl;
}
return;
}
}

if (!swupdater->FlashFirmware(swu_file, get_remaining_timeout()))
{
if (!quiet)
{
std::cout << "Timed out waiting for flashing to complete"
<< std::endl;
}
return;
}

swupdater->RebootToProductive();
if (!quiet)
{
std::cout << "Update successful, waiting for device to reboot..."
<< std::endl;
}
if (!swupdater->WaitForProductive(get_remaining_timeout()))
{
if (!quiet)
{
std::cout << "Timed out waiting for productive mode"
<< std::endl;
}
return;
}
if (!quiet)
{
std::cout << "SWUpdate Complete." << std::endl;
}
return false;
}
}
};

// Read the file in
if (!swu_file.empty())
{
// Check if provided swu_file exists
const std::fstream infile{swu_file};

if ((swu_file != "-") && !validateSwuFileHeader(swu_file))
{
std::cerr << "ifm3d error: File not found or invalid file.\n";
return;
}

// Reboot to recovery if not already in recovery
if (!swupdater->WaitForRecovery(-1))
{
if (!quiet)
{
std::cout << "Rebooting device to recovery mode..." << std::endl;
}
swupdater->RebootToRecovery();
if (!swupdater->WaitForRecovery(get_remaining_timeout()))
{
if (!quiet)
{
std::cout << "Timed out waiting for recovery mode"
<< std::endl;
}
return;
}
}

if (!swupdater->FlashFirmware(swu_file, get_remaining_timeout()))
{
if (!quiet)
{
std::cout << "Timed out waiting for flashing to complete"
<< std::endl;
}
return;
}

swupdater->RebootToProductive();
if (!quiet)
{
std::cout << "Update successful, waiting for device to reboot..."
<< std::endl;
}
if (!swupdater->WaitForProductive(get_remaining_timeout()))
{
if (!quiet)
{
std::cout << "Timed out waiting for productive mode"
<< std::endl;
}
return;
}
if (!quiet)
{
std::cout << "SWUpdate Complete." << std::endl;
}
}
}

CLI::App*
Expand Down
2 changes: 1 addition & 1 deletion third-party/CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
set(CMAKE_CXX_STANDARD 14)
set(CMAKE_CXX_STANDARD 17)

include(FetchContent)

Expand Down

0 comments on commit aa440a8

Please sign in to comment.