Skip to content
Snippets Groups Projects
Commit 14f92377 authored by schneider's avatar schneider
Browse files

recovery: select image for writing

parent c4e9c913
No related branches found
No related tags found
No related merge requests found
...@@ -34,6 +34,12 @@ static menu_t _erasedone_menu; ...@@ -34,6 +34,12 @@ static menu_t _erasedone_menu;
static menu_entry_t _list_menu_entries[]; static menu_entry_t _list_menu_entries[];
static bool _usb_initialized = false; static bool _usb_initialized = false;
typedef struct {
char path[64];
char label[64];
} image_entry_t;
static image_entry_t image_list[64];
static void _main_reboot(void) { esp_restart(); } static void _main_reboot(void) { esp_restart(); }
...@@ -89,26 +95,24 @@ static void _list_exit(void) { ...@@ -89,26 +95,24 @@ static void _list_exit(void) {
} }
static void _list_select(void) { static void _list_select(void) {
// TODO: copy selected image over to app partition
_cur_menu = &_main_menu;
_cur_menu->selected = 0;
// app, app, ota_0, 0x90000, 0x300000, // app, app, ota_0, 0x90000, 0x300000,
const esp_partition_t *p = esp_partition_find_first( const esp_partition_t *p = esp_partition_find_first(
ESP_PARTITION_TYPE_APP, ESP_PARTITION_SUBTYPE_APP_OTA_0, "app"); ESP_PARTITION_TYPE_APP, ESP_PARTITION_SUBTYPE_APP_OTA_0, "app");
if (p == NULL) { if (p == NULL) {
ESP_LOGE(TAG, "app partition not found"); rec_fatal("App partition not found");
return;
} }
printf("erase size: %lu\n", p->erase_size); uint8_t buf[p->erase_size]; // Erase size of internal flash is 4096
uint8_t buf[p->erase_size];
size_t n; size_t n;
size_t offset = 0; size_t offset = 0;
char path[128] = "/flash/hello.bin"; ESP_LOGI(TAG, "Opening %s", image_list[_cur_menu->selected].path);
FILE *f = fopen(path, "rb"); FILE *f = fopen(image_list[_cur_menu->selected].path, "rb");
if (f == NULL) {
ESP_LOGW(TAG, "fopen failed: %s", strerror(errno));
rec_fatal("Could not open file :(");
}
do { do {
n = fread(buf, 1, sizeof(buf), f); n = fread(buf, 1, sizeof(buf), f);
...@@ -116,11 +120,14 @@ static void _list_select(void) { ...@@ -116,11 +120,14 @@ static void _list_select(void) {
esp_partition_erase_range(p, offset, sizeof(buf)); esp_partition_erase_range(p, offset, sizeof(buf));
esp_partition_write(p, offset, buf, n); esp_partition_write(p, offset, buf, n);
offset += n; offset += n;
printf("wrote %u bytes\n", offset); ESP_LOGI(TAG, "wrote %u bytes", offset);
} }
} while (n == sizeof(buf)); } while (n == sizeof(buf));
fclose(f); fclose(f);
_cur_menu = &_main_menu;
_cur_menu->selected = 0;
} }
typedef struct { typedef struct {
...@@ -133,15 +140,15 @@ static void _main_list(void) { ...@@ -133,15 +140,15 @@ static void _main_list(void) {
_cur_menu = &_list_menu; _cur_menu = &_list_menu;
_cur_menu->selected = 0; _cur_menu->selected = 0;
int entries = 1; int entry = 1;
struct dirent *pDirent; struct dirent *pDirent;
DIR *pDir; DIR *pDir;
// TODO: also consider the SD card // TODO(schneider): also consider the SD card
pDir = opendir("/flash"); pDir = opendir("/flash");
if (pDir != NULL) { if (pDir != NULL) {
while (((pDirent = readdir(pDir)) != NULL) && (entries < 64)) { while (((pDirent = readdir(pDir)) != NULL) && (entry < 64)) {
ESP_LOGI(TAG, "Checking header of %s", pDirent->d_name); ESP_LOGI(TAG, "Checking header of %s", pDirent->d_name);
char *s = pDirent->d_name; char *s = pDirent->d_name;
...@@ -150,7 +157,7 @@ static void _main_list(void) { ...@@ -150,7 +157,7 @@ static void _main_list(void) {
if (l > 4 && strcmp(".bin", &s[l - 4]) == 0) { if (l > 4 && strcmp(".bin", &s[l - 4]) == 0) {
esp32_standard_header_t hdr; esp32_standard_header_t hdr;
char path[128]; char path[64];
snprintf(path, sizeof(path) / sizeof(char), "/flash/%s", snprintf(path, sizeof(path) / sizeof(char), "/flash/%s",
pDirent->d_name); pDirent->d_name);
FILE *f = fopen(path, "rb"); FILE *f = fopen(path, "rb");
...@@ -172,23 +179,21 @@ static void _main_list(void) { ...@@ -172,23 +179,21 @@ static void _main_list(void) {
continue; continue;
} }
// TODO have this in a global list, so the select function image_entry_t *ie = &image_list[entry];
// can get the actual path snprintf(ie->label, sizeof(ie->label), "%s: %s",
size_t n = strlen(pDirent->d_name) + 2 + pDirent->d_name, hdr.app.project_name);
strlen(hdr.app.project_name) + 1; snprintf(ie->path, sizeof(ie->path), "%s", path);
char *label = malloc(n);
snprintf(label, n, "%s: %s", pDirent->d_name, _list_menu_entries[entry].label = ie->label;
hdr.app.project_name); _list_menu_entries[entry].enter = _list_select;
_list_menu_entries[entries].label = label; entry++;
_list_menu_entries[entries].enter = _list_select;
entries++;
} }
} }
closedir(pDir); closedir(pDir);
} else { } else {
ESP_LOGE(TAG, "Opening /flash failed"); ESP_LOGE(TAG, "Opening /flash failed");
} }
_list_menu.entries_count = entries; _list_menu.entries_count = entry;
} }
static menu_entry_t _main_menu_entries[] = { static menu_entry_t _main_menu_entries[] = {
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment