-
-
Notifications
You must be signed in to change notification settings - Fork 97
[realtek-ambz2] Add OTA implementation for RTL8720CF #344
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from 4 commits
Commits
Show all changes
5 commits
Select commit
Hold shift + click to select a range
5614d25
[realtek-ambz2] Implement OTA
prokoma 47404de
Fix uf2 filename
bdraco a038683
Fix encode_for_define for Windows compatibility
bdraco 9f36a59
Format lt_ota.c with clang-format
bdraco ede22fe
Update cores/realtek-ambz2/base/api/lt_ota.c
kuba2k2 File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,24 +1,133 @@ | ||
| /* Copyright (c) Kuba Szczodrzyński 2023-05-22. */ | ||
|
|
||
| #include <device_lock.h> | ||
| #include <libretiny.h> | ||
| #include <osdep_service.h> | ||
| #include <sdk_private.h> | ||
|
|
||
| // from SDK | ||
| extern uint32_t sys_update_ota_get_curr_fw_idx(void); | ||
|
|
||
| #define FLASH_SECTOR_SIZE 0x1000 | ||
| // IMAGE_PUBLIC_KEY is defined by the build script as an array initializer | ||
| #define IMAGE_PUBLIC_KEY_OFFSET 32 | ||
| #define IMAGE_PUBLIC_KEY_LENGTH 32 | ||
|
|
||
| static const uint8_t lt_image_public_key[] = IMAGE_PUBLIC_KEY; | ||
|
|
||
| typedef enum { | ||
| INVALID = 0, | ||
| DISABLED = 1, | ||
| ENABLED = 2, | ||
| } lt_ota_image_state_t; | ||
|
|
||
| static bool lt_ota_get_image_offset(uint8_t index, uint32_t *offset) { | ||
| switch (index) { | ||
| case 1: | ||
| *offset = FLASH_OTA1_OFFSET; | ||
| break; | ||
| case 2: | ||
| *offset = FLASH_OTA2_OFFSET; | ||
| break; | ||
| default: | ||
| return false; | ||
| } | ||
| return true; | ||
| } | ||
|
|
||
| static uint8_t lt_ota_get_other_index(uint8_t index) { | ||
| return index ^ 0b11; // 1 -> 2, 2 -> 1 | ||
| } | ||
|
|
||
| static lt_ota_image_state_t lt_ota_get_image_state(uint8_t index) { | ||
| uint32_t offset; | ||
| if (!lt_ota_get_image_offset(index, &offset)) | ||
| return INVALID; | ||
|
|
||
| uint8_t public_key[IMAGE_PUBLIC_KEY_LENGTH]; | ||
| uint32_t num_read = lt_flash_read(offset + IMAGE_PUBLIC_KEY_OFFSET, public_key, sizeof(public_key)); | ||
| if (num_read != sizeof(public_key)) | ||
| return INVALID; | ||
|
|
||
| if (memcmp(public_key, lt_image_public_key, sizeof(public_key)) == 0) | ||
| return ENABLED; | ||
|
|
||
| public_key[0] = ~(public_key[0]); | ||
| if (memcmp(public_key, lt_image_public_key, sizeof(public_key)) == 0) | ||
| return DISABLED; | ||
|
|
||
| return INVALID; | ||
| } | ||
|
|
||
| static bool lt_ota_set_image_enabled(uint8_t index, bool new_enabled) { | ||
| uint32_t offset; | ||
| if (!lt_ota_get_image_offset(index, &offset)) | ||
| return false; | ||
|
|
||
| _irqL irqL; | ||
| uint8_t *header = (uint8_t *)malloc(FLASH_SECTOR_SIZE); | ||
|
|
||
| rtw_enter_critical(NULL, &irqL); | ||
| device_mutex_lock(RT_DEV_LOCK_FLASH); | ||
| flash_stream_read(<_flash_obj, offset, FLASH_SECTOR_SIZE, header); | ||
|
|
||
| bool enabled = header[IMAGE_PUBLIC_KEY_OFFSET] == lt_image_public_key[0]; | ||
| if (enabled != new_enabled) { | ||
| // negate first byte of OTA signature | ||
| header[0] = ~(header[0]); | ||
| // negate first byte of public key | ||
| header[IMAGE_PUBLIC_KEY_OFFSET] = ~(header[IMAGE_PUBLIC_KEY_OFFSET]); | ||
|
|
||
| // write to flash | ||
| hal_flash_sector_erase(lt_flash_obj.phal_spic_adaptor, offset); | ||
| hal_flash_burst_write(lt_flash_obj.phal_spic_adaptor, FLASH_SECTOR_SIZE, offset, header); | ||
| } | ||
|
|
||
| device_mutex_unlock(RT_DEV_LOCK_FLASH); | ||
| rtw_exit_critical(NULL, &irqL); | ||
| free(header); | ||
|
|
||
| return true; | ||
| } | ||
|
|
||
| // public interface implementation | ||
|
|
||
| lt_ota_type_t lt_ota_get_type() { | ||
| return OTA_TYPE_DUAL; | ||
| } | ||
|
|
||
| bool lt_ota_is_valid(uint8_t index) { | ||
| return false; | ||
| return lt_ota_get_image_state(index) != INVALID; | ||
| } | ||
|
|
||
| uint8_t lt_ota_dual_get_current() { | ||
| return 0; | ||
| // ambz2 uses virtual memory, so we can't use function address to determine active image | ||
| // use the SDK instead | ||
| return sys_update_ota_get_curr_fw_idx(); | ||
| } | ||
|
|
||
| uint8_t lt_ota_dual_get_stored() { | ||
| return 0; | ||
| // bootloader prioritizes FW1 if both are valid | ||
| return lt_ota_get_image_state(1) == ENABLED ? 1 : 2; | ||
| } | ||
|
|
||
| bool lt_ota_switch(bool revert) { | ||
| return false; | ||
| uint8_t current = lt_ota_dual_get_current(); | ||
| uint8_t stored = lt_ota_dual_get_stored(); | ||
| if ((current == stored) == revert) | ||
| return true; | ||
|
|
||
| uint8_t to_enable = lt_ota_get_other_index(stored); | ||
| uint8_t to_disable = stored; | ||
|
|
||
| if (!lt_ota_is_valid(to_enable)) | ||
| return false; | ||
|
|
||
| // enable first, so there is always at least one enabled image | ||
| if (!lt_ota_set_image_enabled(to_enable, true)) | ||
| return false; | ||
| if (!lt_ota_set_image_enabled(to_disable, false)) | ||
| return false; | ||
|
|
||
| return true; | ||
| } | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -7,4 +7,5 @@ | |
| #define LT_HAS_LWIP2 1 | ||
| #define LT_HAS_MBEDTLS 1 | ||
| #define LT_HAS_PRINTF 1 | ||
| #define LT_HAS_OTA 1 | ||
| #define LT_HW_BLE 1 | ||
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.