Improve multi (#7136)
[opentx.git] / radio / src / io / bootloader_flash.cpp
blob0df79800f0d761af7c2cb19cef55862573359c4f
1 /*
2 * Copyright (C) OpenTX
4 * Based on code named
5 * th9x - http://code.google.com/p/th9x
6 * er9x - http://code.google.com/p/er9x
7 * gruvin9x - http://code.google.com/p/gruvin9x
9 * License GPLv2: http://www.gnu.org/licenses/gpl-2.0.html
11 * This program is free software; you can redistribute it and/or modify
12 * it under the terms of the GNU General Public License version 2 as
13 * published by the Free Software Foundation.
15 * This program is distributed in the hope that it will be useful,
16 * but WITHOUT ANY WARRANTY; without even the implied warranty of
17 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18 * GNU General Public License for more details.
21 #include "opentx.h"
23 bool isBootloader(const char * filename)
25 FIL file;
26 f_open(&file, filename, FA_READ);
27 uint8_t buffer[1024];
28 UINT count;
30 if (f_read(&file, buffer, sizeof(buffer), &count) != FR_OK || count != sizeof(buffer)) {
31 return false;
34 return isBootloaderStart(buffer);
37 void bootloaderFlash(const char * filename)
39 FIL file;
40 f_open(&file, filename, FA_READ);
41 uint8_t buffer[1024];
42 UINT count;
44 static uint8_t unlocked = 0;
45 if (!unlocked) {
46 unlocked = 1;
47 unlockFlash();
50 for (int i = 0; i < BOOTLOADER_SIZE; i += 1024) {
51 watchdogSuspend(1000/*10s*/);
52 if (f_read(&file, buffer, sizeof(buffer), &count) != FR_OK || count != sizeof(buffer)) {
53 POPUP_WARNING(STR_SDCARD_ERROR);
54 break;
56 if (i == 0 && !isBootloaderStart(buffer)) {
57 POPUP_WARNING(STR_INCOMPATIBLE);
58 break;
60 for (int j = 0; j < 1024; j += FLASH_PAGESIZE) {
61 flashWrite(CONVERT_UINT_PTR(FIRMWARE_ADDRESS + i + j), CONVERT_UINT_PTR(buffer + j));
63 drawProgressScreen("Bootloader", STR_WRITING, i, BOOTLOADER_SIZE);
64 #if defined(SIMU)
65 // add an artificial delay and check for simu quit
66 if (SIMU_SLEEP_OR_EXIT_MS(30))
67 break;
68 #endif
71 watchdogSuspend(0);
73 if (unlocked) {
74 lockFlash();
75 unlocked = 0;
78 f_close(&file);