| 428 | } |
| 429 | |
| 430 | bool launcherPartitionWriteGeneratedTable(const LauncherPartitionTable &table, String *error) { |
| 431 | if (!launcherPartitionBuild(table, gPartitionTableRaw, sizeof(gPartitionTableRaw), error)) return false; |
| 432 | |
| 433 | if (gPartitionTableRaw[0] != 0xAA || gPartitionTableRaw[1] != 0x50) { |
| 434 | setError(error, "Generated partition table has invalid magic"); |
| 435 | return false; |
| 436 | } |
| 437 | |
| 438 | constexpr size_t kWriteChunk = 256; |
| 439 | for (uint8_t attempt = 0; attempt < 3; ++attempt) { |
| 440 | esp_err_t err = |
| 441 | esp_flash_erase_region(nullptr, LAUNCHER_PARTITION_TABLE_OFFSET, LAUNCHER_PARTITION_TABLE_SIZE); |
| 442 | if (err != ESP_OK) { |
| 443 | setError(error, "Could not erase partition table sector"); |
| 444 | continue; |
| 445 | } |
| 446 | |
| 447 | bool writeOk = true; |
| 448 | for (size_t offset = 0; offset < sizeof(gPartitionTableRaw); offset += kWriteChunk) { |
| 449 | size_t len = std::min(kWriteChunk, sizeof(gPartitionTableRaw) - offset); |
| 450 | err = esp_flash_write( |
| 451 | nullptr, gPartitionTableRaw + offset, LAUNCHER_PARTITION_TABLE_OFFSET + offset, len |
| 452 | ); |
| 453 | if (err != ESP_OK) { |
| 454 | writeOk = false; |
| 455 | break; |
| 456 | } |
| 457 | } |
| 458 | if (!writeOk) { |
| 459 | setError(error, "Could not write generated partition table"); |
| 460 | continue; |
| 461 | } |
| 462 | |
| 463 | err = esp_flash_read( |
| 464 | nullptr, gPartitionTableVerify, LAUNCHER_PARTITION_TABLE_OFFSET, sizeof(gPartitionTableVerify) |
| 465 | ); |
| 466 | if (err == ESP_OK && |
| 467 | memcmp(gPartitionTableRaw, gPartitionTableVerify, sizeof(gPartitionTableRaw)) == 0) { |
| 468 | return true; |
| 469 | } |
| 470 | setError(error, "Partition table verify failed"); |
| 471 | } |
| 472 | |
| 473 | return false; |
| 474 | } |
| 475 | |
| 476 | LauncherPartitionEntry *launcherPartitionFindByLabel(LauncherPartitionTable &table, const char *label) { |
| 477 | if (!label) return nullptr; |
no test coverage detected