Call the flasher app when flashing from serial (#1825)

This commit is contained in:
Totoo 2024-01-29 20:52:35 +01:00 committed by GitHub
parent c36fe78bd5
commit 8b2598fdac
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
3 changed files with 15 additions and 47 deletions

View File

@ -110,9 +110,8 @@ bool FlashUtilityView::endsWith(const std::u16string& str, const std::u16string&
} }
} }
std::filesystem::path FlashUtilityView::extract_tar(std::filesystem::path::string_type path) { std::filesystem::path FlashUtilityView::extract_tar(std::filesystem::path::string_type path, ui::Painter& painter) {
// //
ui::Painter painter;
painter.fill_rectangle( painter.fill_rectangle(
{0, 0, portapack::display.width(), portapack::display.height()}, {0, 0, portapack::display.width(), portapack::display.height()},
ui::Color::black()); ui::Color::black());
@ -126,21 +125,19 @@ std::filesystem::path FlashUtilityView::extract_tar(std::filesystem::path::strin
return res; return res;
} }
void FlashUtilityView::flash_firmware(std::filesystem::path::string_type path) { bool FlashUtilityView::flash_firmware(std::filesystem::path::string_type path) {
ui::Painter painter;
if (endsWith(path, u".tar")) { if (endsWith(path, u".tar")) {
// extract, then update // extract, then update
path = extract_tar(u'/' + path).native(); path = extract_tar(u'/' + path, painter).native();
} }
if (path.empty() || !valid_firmware_file(path.c_str())) { if (path.empty() || !valid_firmware_file(path.c_str())) {
ui::Painter painter;
painter.fill_rectangle({0, 50, portapack::display.width(), 90}, ui::Color::black()); painter.fill_rectangle({0, 50, portapack::display.width(), 90}, ui::Color::black());
painter.draw_string({0, 60}, Styles::red, "BAD FIRMWARE FILE"); painter.draw_string({0, 60}, Styles::red, "BAD FIRMWARE FILE");
chThdSleepMilliseconds(5000); chThdSleepMilliseconds(5000);
return; return false;
} }
ui::Painter painter;
painter.fill_rectangle( painter.fill_rectangle(
{0, 0, portapack::display.width(), portapack::display.height()}, {0, 0, portapack::display.width(), portapack::display.height()},
ui::Color::black()); ui::Color::black());

View File

@ -47,6 +47,7 @@ class FlashUtilityView : public View {
void focus() override; void focus() override;
std::string title() const override { return "Flash Utility"; }; std::string title() const override { return "Flash Utility"; };
bool flash_firmware(std::filesystem::path::string_type path);
private: private:
NavigationView& nav_; NavigationView& nav_;
@ -61,9 +62,9 @@ class FlashUtilityView : public View {
{0, 2 * 8, 240, 26 * 8}, {0, 2 * 8, 240, 26 * 8},
true}; true};
std::filesystem::path extract_tar(std::filesystem::path::string_type path); // extracts the tar file, and returns the firmware.bin path from it. empty string if no fw std::filesystem::path extract_tar(std::filesystem::path::string_type path, ui::Painter& painter); // extracts the tar file, and returns the firmware.bin path from it. empty string if no fw
void firmware_selected(std::filesystem::path::string_type path); void firmware_selected(std::filesystem::path::string_type path);
void flash_firmware(std::filesystem::path::string_type path);
bool endsWith(const std::u16string& str, const std::u16string& suffix); bool endsWith(const std::u16string& str, const std::u16string& suffix);
}; };

View File

@ -152,45 +152,15 @@ static void cmd_flash(BaseSequentialStream* chp, int argc, char* argv[]) {
if (!top_widget) return; if (!top_widget) return;
auto nav = static_cast<ui::SystemView*>(top_widget)->get_navigation_view(); auto nav = static_cast<ui::SystemView*>(top_widget)->get_navigation_view();
if (!nav) return; if (!nav) return;
nav->display_modal("Flashing", "Flashing from serial.\r\nPlease wait!\r\nDevice will restart."); nav->home(false);
// check file extensions
if (strEndsWith(path.native(), u".ppfw.tar")) {
// extract tar
chprintf(chp, "Extracting TAR file.\r\n");
auto res = UnTar::untar(
path.native(), [chp](const std::string fileName) {
chprintf(chp, fileName.c_str());
chprintf(chp, "\r\n");
});
if (res.empty()) {
chprintf(chp, "error bad TAR file.\r\n");
nav->pop();
return;
}
path = res; // it will contain the last bin file in tar
} else if (strEndsWith(path.native(), u".bin")) {
// nothing to do for this case yet.
} else {
chprintf(chp, "error only .bin or .ppfw.tar files can be flashed.\r\n");
nav->pop();
return;
}
if (!ui::valid_firmware_file(path.native().c_str())) { // call nav with flash
chprintf(chp, "error corrupt firmware file.\r\n"); auto open_view = nav->push<ui::FlashUtilityView>();
nav->pop(); chprintf(chp, "Flashing started\r\n");
return; chThdSleepMilliseconds(150); // to give display some time to paint the screen
if (!open_view->flash_firmware(path.native())) {
chprintf(chp, "error\r\n");
} }
chprintf(chp, "Flashing: ");
chprintf(chp, path.string().c_str());
chprintf(chp, "\r\n");
chThdSleepMilliseconds(50);
std::memcpy(&shared_memory.bb_data.data[0], path.native().c_str(), (path.native().length() + 1) * 2);
m4_request_shutdown();
chThdSleepMilliseconds(50);
m4_init(portapack::spi_flash::image_tag_flash_utility, portapack::memory::map::m4_code, false);
m0_halt();
} }
static void cmd_screenshot(BaseSequentialStream* chp, int argc, char* argv[]) { static void cmd_screenshot(BaseSequentialStream* chp, int argc, char* argv[]) {