2023-08-11 15:57:32 +00:00
|
|
|
#include "translations.h"
|
|
|
|
#include <assert.h>
|
|
|
|
#include <stdbool.h>
|
|
|
|
#include <string.h>
|
|
|
|
#include "common.h"
|
|
|
|
#include "flash.h"
|
|
|
|
#include "model.h"
|
2024-09-07 11:50:10 +00:00
|
|
|
#include "mpu.h"
|
2023-08-11 15:57:32 +00:00
|
|
|
|
2024-08-13 05:47:59 +00:00
|
|
|
#ifdef KERNEL_MODE
|
|
|
|
|
2023-08-11 15:57:32 +00:00
|
|
|
bool translations_write(const uint8_t* data, uint32_t offset, uint32_t len) {
|
|
|
|
uint32_t size = translations_area_bytesize();
|
|
|
|
if (offset > size || size - offset < len) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2024-09-07 11:50:10 +00:00
|
|
|
mpu_mode_t mpu_mode = mpu_reconfig(MPU_MODE_ASSETS);
|
|
|
|
|
2023-08-11 15:57:32 +00:00
|
|
|
ensure(flash_unlock_write(), "translations_write unlock");
|
2024-03-13 15:33:33 +00:00
|
|
|
// todo consider alignment
|
2024-09-26 14:35:59 +00:00
|
|
|
ensure(flash_area_write_data_padded(&ASSETS_AREA, offset, data, len, 0xFF,
|
|
|
|
FLASH_ALIGN(len)),
|
2024-03-13 15:33:33 +00:00
|
|
|
"translations_write write");
|
2023-08-11 15:57:32 +00:00
|
|
|
ensure(flash_lock_write(), "translations_write lock");
|
2024-09-07 11:50:10 +00:00
|
|
|
|
|
|
|
mpu_restore(mpu_mode);
|
|
|
|
|
2023-08-11 15:57:32 +00:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
const uint8_t* translations_read(uint32_t* len, uint32_t offset) {
|
2024-09-26 14:35:59 +00:00
|
|
|
// TODO: _Static_assert was not happy with ASSETS_AREA.num_subareas == 1
|
2023-08-11 15:57:32 +00:00
|
|
|
// error: expression in static assertion is not constant
|
2024-09-26 14:35:59 +00:00
|
|
|
assert(ASSETS_AREA.num_subareas == 1);
|
|
|
|
*len = flash_area_get_size(&ASSETS_AREA) - offset;
|
|
|
|
return flash_area_get_address(&ASSETS_AREA, offset, 0);
|
2023-08-11 15:57:32 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void translations_erase(void) {
|
2024-09-07 11:50:10 +00:00
|
|
|
mpu_mode_t mpu_mode = mpu_reconfig(MPU_MODE_ASSETS);
|
2024-09-26 14:35:59 +00:00
|
|
|
ensure(flash_area_erase(&ASSETS_AREA, NULL), "translations erase");
|
2024-09-07 11:50:10 +00:00
|
|
|
mpu_restore(mpu_mode);
|
2023-08-11 15:57:32 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
uint32_t translations_area_bytesize(void) {
|
2024-09-26 14:35:59 +00:00
|
|
|
return flash_area_get_size(&ASSETS_AREA);
|
2023-08-11 15:57:32 +00:00
|
|
|
}
|
2024-08-13 05:47:59 +00:00
|
|
|
|
|
|
|
#endif // KERNEL_MODE
|