2023-03-28 08:39:00 -04:00
|
|
|
/*
|
|
|
|
* Copyright (C) 2015 Jared Boone, ShareBrained Technology, Inc.
|
|
|
|
* Copyright (C) 2023 Bernd Herzog
|
|
|
|
*
|
|
|
|
* This file is part of PortaPack.
|
|
|
|
*
|
|
|
|
* This program is free software; you can redistribute it and/or modify
|
|
|
|
* it under the terms of the GNU General Public License as published by
|
|
|
|
* the Free Software Foundation; either version 2, or (at your option)
|
|
|
|
* any later version.
|
|
|
|
*
|
|
|
|
* This program is distributed in the hope that it will be useful,
|
|
|
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
* GNU General Public License for more details.
|
|
|
|
*
|
|
|
|
* You should have received a copy of the GNU General Public License
|
|
|
|
* along with this program; see the file COPYING. If not, write to
|
|
|
|
* the Free Software Foundation, Inc., 51 Franklin Street,
|
|
|
|
* Boston, MA 02110-1301, USA.
|
|
|
|
*/
|
|
|
|
|
|
|
|
#include "ui_flash_utility.hpp"
|
|
|
|
#include "portapack_shared_memory.hpp"
|
2024-03-25 03:44:49 -04:00
|
|
|
#include "file_path.hpp"
|
2023-03-28 08:39:00 -04:00
|
|
|
|
|
|
|
namespace ui {
|
|
|
|
|
2024-02-18 13:02:42 -05:00
|
|
|
// Firmware image validation
|
|
|
|
static const char* hackrf_magic = "HACKRFFW";
|
|
|
|
#define FIRMWARE_INFO_AREA_OFFSET 0x400
|
|
|
|
#define FIRST_CHECKSUM_NIGHTLY 240125
|
|
|
|
|
2023-05-18 16:16:05 -04:00
|
|
|
Thread* FlashUtilityView::thread{nullptr};
|
2023-03-28 08:39:00 -04:00
|
|
|
static constexpr size_t max_filename_length = 26;
|
|
|
|
|
2024-01-24 17:37:21 -05:00
|
|
|
bool valid_firmware_file(std::filesystem::path::string_type path) {
|
|
|
|
File firmware_file;
|
2024-02-18 13:02:42 -05:00
|
|
|
bool require_checksum{false};
|
2024-01-24 17:37:21 -05:00
|
|
|
uint32_t read_buffer[128];
|
2024-02-18 13:02:42 -05:00
|
|
|
uint32_t checksum{FLASH_CHECKSUM_ERROR}; // initializing to invalid checksum in case file can't be read
|
|
|
|
|
|
|
|
static_assert((FIRMWARE_INFO_AREA_OFFSET % sizeof(read_buffer)) == 0, "Read buffer size must divide evenly into FIRMWARE_INFO_AREA_OFFSET");
|
2024-01-24 17:37:21 -05:00
|
|
|
|
|
|
|
// test read of the whole file just to validate checksum (baseband flash code will re-read when flashing)
|
|
|
|
auto result = firmware_file.open(path.c_str());
|
|
|
|
if (!result.is_valid()) {
|
|
|
|
checksum = 0;
|
2024-02-18 13:02:42 -05:00
|
|
|
for (uint32_t offset = 0; offset < FLASH_ROM_SIZE; offset += sizeof(read_buffer)) {
|
2024-01-24 17:37:21 -05:00
|
|
|
auto readResult = firmware_file.read(&read_buffer, sizeof(read_buffer));
|
|
|
|
|
|
|
|
if ((!readResult) || (readResult.value() != sizeof(read_buffer))) {
|
2024-02-18 13:02:42 -05:00
|
|
|
// File was smaller than 1MB:
|
|
|
|
// If version is such that the file SHOULD have been 1MB, call it a checksum error (otherwise say it's OK).
|
|
|
|
checksum = (require_checksum) ? FLASH_CHECKSUM_ERROR : FLASH_EXPECTED_CHECKSUM;
|
2024-01-24 17:37:21 -05:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
2024-02-18 13:02:42 -05:00
|
|
|
// Did we just read the firmware info area?
|
|
|
|
if (offset == FIRMWARE_INFO_AREA_OFFSET) {
|
|
|
|
// If there's no info area (missing HACKRFFW signature), it could be an ancient FW version (so skipping check)
|
|
|
|
if (memcmp(read_buffer, hackrf_magic, 8) == 0) {
|
|
|
|
char* version_string = (char*)&read_buffer[4];
|
|
|
|
|
|
|
|
// Require a 1MB firmware image with a valid checksum if release version >=v2.x or nightly >n_240125
|
|
|
|
if (((version_string[0] == 'v') && (std::atoi(&version_string[1]) >= 2)) ||
|
|
|
|
((version_string[0] == 'n') && (version_string[1] == '_') && (std::atoi(&version_string[2]) >= FIRST_CHECKSUM_NIGHTLY)))
|
|
|
|
require_checksum = true;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2024-01-24 17:37:21 -05:00
|
|
|
checksum += simple_checksum((uint32_t)read_buffer, sizeof(read_buffer));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return (checksum == FLASH_EXPECTED_CHECKSUM);
|
|
|
|
}
|
|
|
|
|
2023-05-18 16:16:05 -04:00
|
|
|
FlashUtilityView::FlashUtilityView(NavigationView& nav)
|
|
|
|
: nav_(nav) {
|
|
|
|
add_children({&labels,
|
|
|
|
&menu_view});
|
2023-03-28 08:39:00 -04:00
|
|
|
|
2023-05-18 16:16:05 -04:00
|
|
|
menu_view.set_parent_rect({0, 3 * 8, 240, 33 * 8});
|
2023-03-28 08:39:00 -04:00
|
|
|
|
2024-05-20 03:02:29 -04:00
|
|
|
ensure_directory(apps_dir);
|
2024-03-25 03:44:49 -04:00
|
|
|
ensure_directory(firmware_dir);
|
2024-03-14 23:56:55 -04:00
|
|
|
|
|
|
|
auto add_firmware_items = [&](
|
|
|
|
const std::filesystem::path& folder_path,
|
|
|
|
const std::filesystem::path& wild,
|
|
|
|
ui::Color color) {
|
|
|
|
for (const auto& entry : std::filesystem::directory_iterator(folder_path, wild)) {
|
|
|
|
auto filename = entry.path().filename();
|
|
|
|
auto path = entry.path().native();
|
|
|
|
|
|
|
|
menu_view.add_item({filename.string().substr(0, max_filename_length),
|
|
|
|
color,
|
2024-12-07 06:38:15 -05:00
|
|
|
&bitmap_icon_peripherals_details,
|
2024-03-14 23:56:55 -04:00
|
|
|
[this, path](KeyEvent) {
|
|
|
|
this->firmware_selected(path);
|
|
|
|
}});
|
|
|
|
}
|
|
|
|
};
|
2023-06-26 13:44:01 -04:00
|
|
|
|
2024-05-27 15:02:52 -04:00
|
|
|
add_firmware_items(firmware_dir, u"*.bin", ui::Theme::getInstance()->fg_red->foreground);
|
|
|
|
add_firmware_items(firmware_dir, u"*.tar", ui::Theme::getInstance()->fg_cyan->foreground);
|
2023-03-28 08:39:00 -04:00
|
|
|
|
2024-05-27 15:02:52 -04:00
|
|
|
// add_firmware_items(user_firmware_folder,u"*.bin", ui::Theme::getInstance()->fg_cyan->foreground);
|
2023-03-28 08:39:00 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
void FlashUtilityView::firmware_selected(std::filesystem::path::string_type path) {
|
2023-05-18 16:16:05 -04:00
|
|
|
nav_.push<ModalMessageView>(
|
|
|
|
"Warning!",
|
|
|
|
"This will replace your\ncurrent firmware.\n\nIf things go wrong you are\nrequired to flash manually\nwith dfu.",
|
|
|
|
YESNO,
|
|
|
|
[this, path](bool choice) {
|
|
|
|
if (choice) {
|
2024-03-25 03:44:49 -04:00
|
|
|
std::filesystem::path::string_type full_path = firmware_dir.native() + u"/" + path;
|
2023-05-18 16:16:05 -04:00
|
|
|
this->flash_firmware(full_path);
|
|
|
|
}
|
|
|
|
});
|
2023-03-28 08:39:00 -04:00
|
|
|
}
|
|
|
|
|
2024-01-03 08:05:24 -05:00
|
|
|
bool FlashUtilityView::endsWith(const std::u16string& str, const std::u16string& suffix) {
|
|
|
|
if (str.length() >= suffix.length()) {
|
|
|
|
std::u16string endOfString = str.substr(str.length() - suffix.length());
|
|
|
|
return endOfString == suffix;
|
|
|
|
} else {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2024-01-29 14:52:35 -05:00
|
|
|
std::filesystem::path FlashUtilityView::extract_tar(std::filesystem::path::string_type path, ui::Painter& painter) {
|
2024-01-03 08:05:24 -05:00
|
|
|
//
|
|
|
|
painter.fill_rectangle(
|
|
|
|
{0, 0, portapack::display.width(), portapack::display.height()},
|
2024-05-27 15:02:52 -04:00
|
|
|
Theme::getInstance()->bg_darkest->background);
|
2024-01-03 08:05:24 -05:00
|
|
|
painter.draw_string({12, 24}, this->nav_.style(), "Unpacking TAR file...");
|
|
|
|
|
|
|
|
auto res = UnTar::untar(path, [this](const std::string fileName) {
|
|
|
|
ui::Painter painter;
|
2024-05-27 15:02:52 -04:00
|
|
|
painter.fill_rectangle({0, 50, portapack::display.width(), 90}, Theme::getInstance()->bg_darkest->background);
|
2024-01-03 08:05:24 -05:00
|
|
|
painter.draw_string({0, 60}, this->nav_.style(), fileName);
|
|
|
|
});
|
|
|
|
return res;
|
|
|
|
}
|
|
|
|
|
2024-01-29 14:52:35 -05:00
|
|
|
bool FlashUtilityView::flash_firmware(std::filesystem::path::string_type path) {
|
|
|
|
ui::Painter painter;
|
2024-01-18 14:59:44 -05:00
|
|
|
if (endsWith(path, u".tar")) {
|
2024-01-03 08:05:24 -05:00
|
|
|
// extract, then update
|
2024-01-29 14:52:35 -05:00
|
|
|
path = extract_tar(u'/' + path, painter).native();
|
2024-01-03 08:05:24 -05:00
|
|
|
}
|
2024-01-24 17:37:21 -05:00
|
|
|
|
2024-01-27 07:29:24 -05:00
|
|
|
if (path.empty() || !valid_firmware_file(path.c_str())) {
|
2024-05-27 15:02:52 -04:00
|
|
|
painter.fill_rectangle({0, 50, portapack::display.width(), 90}, Theme::getInstance()->bg_darkest->background);
|
|
|
|
painter.draw_string({0, 60}, *Theme::getInstance()->fg_red, "BAD FIRMWARE FILE OR W/R ERR");
|
2024-01-27 07:29:24 -05:00
|
|
|
chThdSleepMilliseconds(5000);
|
2024-01-29 14:52:35 -05:00
|
|
|
return false;
|
2024-01-27 07:29:24 -05:00
|
|
|
}
|
2023-05-18 16:16:05 -04:00
|
|
|
painter.fill_rectangle(
|
|
|
|
{0, 0, portapack::display.width(), portapack::display.height()},
|
2024-05-27 15:02:52 -04:00
|
|
|
Theme::getInstance()->bg_darkest->background);
|
2023-03-28 08:39:00 -04:00
|
|
|
|
2023-05-18 16:16:05 -04:00
|
|
|
painter.draw_string({12, 24}, this->nav_.style(), "This will take 15 seconds.");
|
2024-04-29 16:29:46 -04:00
|
|
|
painter.draw_string({12, 64}, this->nav_.style(), "Please wait while LED RX");
|
|
|
|
painter.draw_string({12, 84}, this->nav_.style(), "is on and TX is flashing.");
|
2024-01-04 12:47:45 -05:00
|
|
|
painter.draw_string({12, 124}, this->nav_.style(), "Device will then restart.");
|
2023-03-28 08:39:00 -04:00
|
|
|
|
2023-05-18 16:16:05 -04:00
|
|
|
std::memcpy(&shared_memory.bb_data.data[0], path.c_str(), (path.length() + 1) * 2);
|
|
|
|
m4_init(portapack::spi_flash::image_tag_flash_utility, portapack::memory::map::m4_code, false);
|
|
|
|
m0_halt();
|
2024-01-29 19:07:32 -05:00
|
|
|
return true; // fixes compiler warning (line should not be reached due to halt)
|
2023-03-28 08:39:00 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
void FlashUtilityView::focus() {
|
2023-05-18 16:16:05 -04:00
|
|
|
menu_view.focus();
|
2023-03-28 08:39:00 -04:00
|
|
|
}
|
|
|
|
|
2024-03-14 23:56:55 -04:00
|
|
|
} /* namespace ui */
|