-
Notifications
You must be signed in to change notification settings - Fork 514
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
Change DCT read access to lock()/unlock() or read-with-copy #1307
Closed
Closed
Changes from all commits
Commits
Show all changes
6 commits
Select commit
Hold shift + click to select a range
4b0240a
makes FLASH_Unlock()/Lock() functions enforce a critical section arou…
m-mcgowan cc276e6
adds atomic flag mutex, for use with DCD and flash operations on the …
m-mcgowan 870f0b0
add stub functions for concurrency in stm32 bootloaders
m-mcgowan 4651981
Changed DCT access to lock()/unlock() or read-with-copy style
avtolstoy 9ed574c
Fix bootloader build. It still overflows, but since we are going to d…
avtolstoy 7fd45e4
DCT locking fixes
avtolstoy 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 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 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 |
---|---|---|
|
@@ -8,4 +8,4 @@ | |
#define CRC_TYPE uint32_t | ||
#endif | ||
|
||
#include "../hal/src/photon/dct_hal.c" | ||
#include "../hal/src/photon/dct_hal.cpp" |
This file contains 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 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 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 |
---|---|---|
@@ -0,0 +1,12 @@ | ||
|
||
int os_thread_yield() { | ||
return 0; | ||
} | ||
|
||
void __flash_acquire() { | ||
|
||
} | ||
|
||
void __flash_release() { | ||
|
||
} |
This file contains 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 was deleted.
Oops, something went wrong.
This file contains 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 |
---|---|---|
@@ -0,0 +1,79 @@ | ||
#include "dct_hal.h" | ||
#include "wiced_result.h" | ||
#include "wiced_dct_common.h" | ||
#include "waf_platform.h" | ||
#include "wiced_framework.h" | ||
#include "module_info.h" | ||
#include "service_debug.h" | ||
#include "hal_irq_flag.h" | ||
#include "atomic_flag_mutex.h" | ||
|
||
using DCTLock = AtomicFlagMutex<os_result_t, os_thread_yield>; | ||
|
||
static DCTLock dctLock; | ||
|
||
extern uint32_t Compute_CRC32(const uint8_t *pBuffer, uint32_t bufferSize); | ||
|
||
// Compatibility crc32 function for WICED | ||
extern "C" uint32_t crc32(uint8_t* pdata, unsigned int nbytes, uint32_t crc) { | ||
uint32_t crcres = Compute_CRC32(pdata, nbytes); | ||
// We need to XOR computed CRC32 value with 0xffffffff again as it was already xored in Compute_CRC32 | ||
// and apply passed crc value instead. | ||
crcres ^= 0xffffffff; | ||
crcres ^= crc; | ||
return crcres; | ||
} | ||
|
||
int dct_read_app_data_copy(uint32_t offset, void* ptr, size_t size) | ||
{ | ||
void* p = NULL; | ||
int result = 0; | ||
if (ptr && wiced_dct_read_lock(&p, WICED_FALSE, DCT_APP_SECTION, offset, 0) == WICED_SUCCESS) | ||
{ | ||
memcpy(ptr, p, size); | ||
} | ||
else | ||
{ | ||
result = 1; | ||
} | ||
wiced_dct_read_unlock(NULL, WICED_FALSE); | ||
|
||
return result; | ||
} | ||
|
||
const void* dct_read_app_data_lock(uint32_t offset) | ||
{ | ||
void* ptr = NULL; | ||
wiced_dct_read_lock(&ptr, WICED_FALSE, DCT_APP_SECTION, offset, 0); | ||
return (const void*)ptr; | ||
} | ||
|
||
int dct_read_app_data_unlock(uint32_t offset) | ||
{ | ||
return wiced_dct_read_unlock(NULL, WICED_FALSE); | ||
} | ||
|
||
const void* dct_read_app_data(uint32_t offset) { | ||
wiced_dct_lock(); | ||
const char* ptr = ((const char*)wiced_dct_get_current_address(DCT_APP_SECTION) + offset); | ||
wiced_dct_unlock(); | ||
return ptr; | ||
} | ||
|
||
int dct_write_app_data(const void* data, uint32_t offset, uint32_t size) { | ||
int res = wiced_dct_write(data, DCT_APP_SECTION, offset, size); | ||
|
||
return res; | ||
} | ||
|
||
wiced_result_t wiced_dct_lock() | ||
{ | ||
dctLock.lock(); | ||
return WICED_SUCCESS; | ||
} | ||
|
||
wiced_result_t wiced_dct_unlock() | ||
{ | ||
dctLock.unlock(); | ||
return WICED_SUCCESS; | ||
} |
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
This file contains 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 |
---|---|---|
|
@@ -126,8 +126,9 @@ int dns_resolve_query(const char* query) | |
int result = dns_resolve_query_default(query); | ||
if (result<=0) | ||
{ | ||
const char* valid_queries = (const char*) dct_read_app_data(DCT_DNS_RESOLVE_OFFSET); | ||
const char* valid_queries = (const char*) dct_read_app_data_lock(DCT_DNS_RESOLVE_OFFSET); | ||
result = resolve_dns_query(query, valid_queries); | ||
dct_read_app_data_unlock(DCT_DNS_RESOLVE_OFFSET); | ||
} | ||
return result; | ||
} | ||
|
@@ -524,7 +525,9 @@ int decrypt(char* plaintext, int max_plaintext_len, char* hex_encoded_ciphertext | |
hex_decode(buf, len, hex_encoded_ciphertext); | ||
|
||
// reuse the hex encoded buffer | ||
int plaintext_len = decrypt_rsa(buf, fetch_device_private_key(), (uint8_t*)plaintext, max_plaintext_len); | ||
const uint8_t *key = fetch_device_private_key(1); // fetch and lock private key data | ||
int plaintext_len = decrypt_rsa(buf, key, (uint8_t*)plaintext, max_plaintext_len); | ||
fetch_device_private_key(0); // unlock private key data | ||
return plaintext_len; | ||
} | ||
|
||
|
@@ -778,7 +781,7 @@ class PublicKeyCommand : public JSONCommand { | |
void produce_response(Writer& writer, int result) { | ||
// fetch public key | ||
const int length = EXTERNAL_FLASH_SERVER_PUBLIC_KEY_LENGTH; | ||
const uint8_t* data = fetch_device_public_key(); | ||
const uint8_t* data = fetch_device_public_key(1); | ||
write_char(writer, '{'); | ||
if (data) { | ||
writer.write("\"b\":\""); | ||
|
@@ -794,6 +797,8 @@ class PublicKeyCommand : public JSONCommand { | |
result = 1; | ||
} | ||
|
||
fetch_device_public_key(0); | ||
|
||
write_json_int(writer, "r", result); | ||
write_char(writer, '}'); | ||
} | ||
|
@@ -899,34 +904,38 @@ extern "C" bool fetch_or_generate_setup_ssid(wiced_ssid_t* SSID); | |
* @return true if the device code was generated. | ||
*/ | ||
bool fetch_or_generate_device_code(wiced_ssid_t* SSID) { | ||
const uint8_t* suffix = (const uint8_t*)dct_read_app_data(DCT_DEVICE_CODE_OFFSET); | ||
const uint8_t* suffix = (const uint8_t*)dct_read_app_data_lock(DCT_DEVICE_CODE_OFFSET); | ||
int8_t c = (int8_t)*suffix; // check out first byte | ||
bool generate = (!c || c<0); | ||
uint8_t* dest = SSID->value+SSID->length; | ||
SSID->length += DEVICE_CODE_LEN; | ||
if (generate) { | ||
dct_read_app_data_unlock(DCT_DEVICE_CODE_OFFSET); | ||
random_code(dest, DEVICE_CODE_LEN); | ||
dct_write_app_data(dest, DCT_DEVICE_CODE_OFFSET, DEVICE_CODE_LEN); | ||
} | ||
else { | ||
memcpy(dest, suffix, DEVICE_CODE_LEN); | ||
dct_read_app_data_unlock(DCT_DEVICE_CODE_OFFSET); | ||
} | ||
return generate; | ||
} | ||
|
||
const int MAX_SSID_PREFIX_LEN = 25; | ||
|
||
bool fetch_or_generate_ssid_prefix(wiced_ssid_t* SSID) { | ||
const uint8_t* prefix = (const uint8_t*)dct_read_app_data(DCT_SSID_PREFIX_OFFSET); | ||
const uint8_t* prefix = (const uint8_t*)dct_read_app_data_lock(DCT_SSID_PREFIX_OFFSET); | ||
uint8_t len = *prefix; | ||
bool generate = (!len || len>MAX_SSID_PREFIX_LEN); | ||
if (generate) { | ||
dct_read_app_data_unlock(DCT_SSID_PREFIX_OFFSET); | ||
strcpy((char*)SSID->value, "Photon"); | ||
SSID->length = 6; | ||
dct_write_app_data(SSID, DCT_SSID_PREFIX_OFFSET, SSID->length+1); | ||
} | ||
else { | ||
memcpy(SSID, prefix, DCT_SSID_PREFIX_SIZE); | ||
dct_read_app_data_unlock(DCT_SSID_PREFIX_OFFSET); | ||
} | ||
if (SSID->length>MAX_SSID_PREFIX_LEN) | ||
SSID->length = MAX_SSID_PREFIX_LEN; | ||
|
@@ -964,9 +973,11 @@ class SoftAPController { | |
if (result == WICED_SUCCESS) | ||
{ | ||
if (memcmp(&expected, soft_ap, sizeof(expected))) { | ||
wiced_dct_read_unlock( soft_ap, WICED_FALSE ); | ||
result = wiced_dct_write(&expected, DCT_WIFI_CONFIG_SECTION, OFFSETOF(platform_dct_wifi_config_t, soft_ap_settings), sizeof(wiced_config_soft_ap_t)); | ||
} else { | ||
wiced_dct_read_unlock( soft_ap, WICED_FALSE ); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Nice! 👍 I've just checked other places where we use non-copying variant of |
||
} | ||
wiced_dct_read_unlock( soft_ap, WICED_FALSE ); | ||
} | ||
return result; | ||
} | ||
|
This file contains 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
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.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Should we add
HAL_IsISR()
check here?