void user_init(void) { uart_set_baud(0, 115200); rboot_config conf = rboot_get_config(); printf("esp-gizmo. Running on flash slot %d / %d\n", conf.current_rom, conf.count); key_task_init(); load_driver_init(); mqtt_task_init(); struct sdk_station_config config = { .ssid = WIFI_SSID, .password = WIFI_PASS, }; /* required to call wifi_set_opmode before station_set_config */ sdk_wifi_set_opmode(STATION_MODE); sdk_wifi_station_set_config(&config); printf("Starting TFTP server..."); ota_tftp_init_server(TFTP_PORT); xTaskCreate(main_task, "main", 1024, NULL, 1, NULL); xTaskCreate(health_state, "health_state", 512, NULL, 1, NULL); }
void otaUpdateHandler() { uint8 slot; rboot_config bootconf; Debug.println("Updating..."); // need a clean object, otherwise if run before and failed will not run again if (otaUpdater) delete otaUpdater; otaUpdater = new rBootHttpUpdate(); // select rom slot to flash bootconf = rboot_get_config(); slot = bootconf.current_rom; if (slot == 0) slot = 1; else slot = 0; // flash rom to position indicated in the rBoot config rom table otaUpdater->addItem(bootconf.roms[slot], otaBaseUrl + ROM_0_FNAME); #ifndef DISABLE_SPIFFS // use user supplied values (defaults for 4mb flash in makefile) if (slot == 0) { otaUpdater->addItem(RBOOT_SPIFFS_0, otaBaseUrl + SPIFFS_FNAME); } else { otaUpdater->addItem(RBOOT_SPIFFS_1, otaBaseUrl + SPIFFS_FNAME); } #endif // set a callback (called on failure or success without switching requested) otaUpdater->setCallback(otaUpdateDelegate(otaUpdate_CallBack)); // start update otaUpdater->start(); }
// set current boot rom bool ICACHE_FLASH_ATTR rboot_set_current_rom(uint8 rom) { rboot_config conf; conf = rboot_get_config(); if (rom >= conf.count) return false; conf.current_rom = rom; return rboot_set_config(&conf); }
rboot_config *get_rboot_config() { static rboot_config *cfg = NULL; if (cfg == NULL) { cfg = malloc(sizeof(*cfg)); *cfg = rboot_get_config(); } return cfg; }
void OtaUpdate() { uint8 slot; rboot_config bootconf; Serial.println("Updating..."); hardwareTimer.stop(); reportTimer.stop(); sendToClients("Firmware ota update started..."); // need a clean object, otherwise if run before and failed will not run again if (otaUpdater) delete otaUpdater; otaUpdater = new rBootHttpUpdate(); sendToClients("Firmware ota update started...1"); // select rom slot to flash bootconf = rboot_get_config(); slot = bootconf.current_rom; if (slot == 0) slot = 1; else slot = 0; #ifndef RBOOT_TWO_ROMS // flash rom to position indicated in the rBoot config rom table otaUpdater->addItem(bootconf.roms[slot], ROM_0_URL); #else // flash appropriate rom if (slot == 0) { otaUpdater->addItem(bootconf.roms[slot], ROM_0_URL); } else { otaUpdater->addItem(bootconf.roms[slot], ROM_1_URL); } #endif #ifndef DISABLE_SPIFFS // use user supplied values (defaults for 4mb flash in makefile) if (slot == 0) { otaUpdater->addItem(RBOOT_SPIFFS_0, SPIFFS_URL); } else { otaUpdater->addItem(RBOOT_SPIFFS_1, SPIFFS_URL); } #endif sendToClients("Firmware ota update started...2"); // request switch and reboot on success otaUpdater->switchToRom(slot); // and/or set a callback (called on failure or success without switching requested) otaUpdater->setCallback(OtaUpdate_CallBack); // start update sendToClients("Firmware ota update started...3"); otaUpdater->start(); }
void ota_update() { uint8 slot; rboot_config bootconf; PRINTF_INFO("Updating...\n"); // need a clean object, otherwise if run before and failed will not run again if (otaUpdater) delete otaUpdater; otaUpdater = new rBootHttpUpdate(); // select rom slot to flash bootconf = rboot_get_config(); slot = bootconf.current_rom; if (slot == 0) slot = 1; else slot = 0; #ifndef RBOOT_TWO_ROMS // flash rom to position indicated in the rBoot config rom table otaUpdater->addItem(bootconf.roms[slot], ROM_0_URL); #else // flash appropriate rom if (slot == 0) { otaUpdater->addItem(bootconf.roms[slot], ROM_0_URL); } else { otaUpdater->addItem(bootconf.roms[slot], ROM_1_URL); } #endif #ifndef DISABLE_SPIFFS // use user supplied values (defaults for 4mb flash in makefile) if (slot == 0) { otaUpdater->addItem(RBOOT_SPIFFS_0, SPIFFS_URL); } else { otaUpdater->addItem(RBOOT_SPIFFS_1, SPIFFS_URL); } #endif // request switch and reboot on success //otaUpdater->switchToRom(slot); // and/or set a callback (called on failure or success without switching requested) otaUpdater->setCallback(ota_onUpdate); // start update otaUpdater->start(); }
irom static app_action_t ota_start(const string_t *src, string_t *dst, bool verify) { rboot_config rcfg = rboot_get_config(); if(string_size(&buffer_4k) < 0x1000) { string_format(dst, "OTA: string write buffer too small: %d\n", string_size(&buffer_4k)); return(app_action_error); } if((rcfg.magic != BOOT_CONFIG_MAGIC) || (rcfg.count != 2) || (rcfg.current_rom > 1)) { string_cat(dst, "OTA: rboot config invalid\n"); return(app_action_error); } if(parse_int(1, src, &remote_file_length, 0) != parse_ok) { string_cat(dst, "OTA: invalid/missing file length\n"); return(app_action_error); } if(wlan_scan_active()) { string_cat(dst, "OTA: wlan scan active\n"); return(app_action_error); } ota_state = verify ? state_verify : state_write; flash_slot = rcfg.current_rom == 0 ? 1 : 0; flash_sector = rcfg.roms[flash_slot] / 0x1000; received = 0; written = 0; skipped = 0; string_clear(&buffer_4k); string_crc32_init(); MD5Init(&md5); string_format(dst, "%s %d %d\n", verify ? "VERIFY" : "WRITE", flash_slot, flash_sector); return(app_action_normal); }
void user_init(void) { uart_set_baud(0, 115200); midea_ir_init(&ir, 14); config_init(); rboot_config conf = rboot_get_config(); printf("Currently running on flash slot %d / %d\n", conf.current_rom, conf.count); printf("Image addresses in flash:\n"); for(int i = 0; i <conf.count; i++) { printf("%c%d: offset 0x%08x\n", i == conf.current_rom ? '*':' ', i, conf.roms[i]); } start_config_server(); xTaskCreate(main_task, (signed char *)"main", 512, NULL, 4, NULL); /* xTaskCreate(test_task, (signed char *)"test", 512, NULL, 2, NULL); */ ota_tftp_init_server(TFTP_PORT); }
// start the ota process, with user supplied options bool ICACHE_FLASH_ATTR rboot_ota_start(ota_callback callback) { uint8 slot; rboot_config bootconf; err_t result; // check not already updating if (system_upgrade_flag_check() == UPGRADE_FLAG_START) { return false; } // create upgrade status structure upgrade = (upgrade_status*)os_zalloc(sizeof(upgrade_status)); if (!upgrade) { CHATFABRIC_DEBUG(_GLOBAL_DEBUG, "No ram!\r\n"); return false; } // store the callback upgrade->callback = callback; // get details of rom slot to update bootconf = rboot_get_config(); slot = bootconf.current_rom; if (slot == 0) slot = 1; else slot = 0; upgrade->rom_slot = slot; // flash to rom slot upgrade->write_status = rboot_write_init(bootconf.roms[upgrade->rom_slot]); // to flash a file (e.g. containing a filesystem) to an arbitrary location // (e.g. 0x40000 bytes after the start of the rom) use code this like instead: // Note: address must be start of a sector (multiple of 4k)! //upgrade->write_status = rboot_write_init(bootconf.roms[upgrade->rom_slot] + 0x40000); //upgrade->rom_slot = FLASH_BY_ADDR; // create connection upgrade->conn = (struct espconn *)os_zalloc(sizeof(struct espconn)); if (!upgrade->conn) { CHATFABRIC_DEBUG(_GLOBAL_DEBUG, "No ram!\r\n"); os_free(upgrade); return false; } upgrade->conn->proto.tcp = (esp_tcp *)os_zalloc(sizeof(esp_tcp)); if (!upgrade->conn->proto.tcp) { CHATFABRIC_DEBUG(_GLOBAL_DEBUG, "No ram!\r\n"); os_free(upgrade->conn); os_free(upgrade); return false; } // set update flag system_upgrade_flag_set(UPGRADE_FLAG_START); // dns lookup result = espconn_gethostbyname(upgrade->conn, OTA_HOST, &upgrade->ip, upgrade_resolved); if (result == ESPCONN_OK) { // hostname is already cached or is actually a dotted decimal ip address upgrade_resolved(0, &upgrade->ip, upgrade->conn); } else if (result == ESPCONN_INPROGRESS) { // lookup taking place, will call upgrade_resolved on completion } else { CHATFABRIC_DEBUG(_GLOBAL_DEBUG, "DNS error!\r\n"); os_free(upgrade->conn->proto.tcp); os_free(upgrade->conn); os_free(upgrade); return false; } return true; }
// get current boot rom uint8 ICACHE_FLASH_ATTR rboot_get_current_rom(void) { rboot_config conf; conf = rboot_get_config(); return conf.current_rom; }
uint32_t rboot_get_slot_offset(uint8_t slot) { rboot_config conf; conf = rboot_get_config(); if (slot >= conf.count) return (uint32_t)-1; return conf.roms[slot]; }