2020-11-29 22:48:20 -04:00
|
|
|
#include "Storage.h"
|
|
|
|
|
|
|
|
#include <AP_Vehicle/AP_Vehicle_Type.h>
|
2015-08-11 03:28:43 -03:00
|
|
|
#include <AP_HAL/AP_HAL.h>
|
2021-06-10 23:30:38 -03:00
|
|
|
#include "AP_HAL_SITL.h"
|
2012-12-12 18:04:27 -04:00
|
|
|
|
|
|
|
#include <assert.h>
|
|
|
|
#include <sys/types.h>
|
|
|
|
#include <sys/stat.h>
|
|
|
|
#include <fcntl.h>
|
|
|
|
#include <unistd.h>
|
2019-08-27 10:20:49 -03:00
|
|
|
#include <stdio.h>
|
|
|
|
|
2020-11-29 22:48:20 -04:00
|
|
|
#ifndef HAL_STORAGE_FILE
|
|
|
|
#if APM_BUILD_TYPE(APM_BUILD_Replay)
|
|
|
|
#define HAL_STORAGE_FILE "eeprom-replay.bin"
|
2020-12-28 10:33:56 -04:00
|
|
|
#elif APM_BUILD_TYPE(APM_BUILD_AP_Periph)
|
|
|
|
#define HAL_STORAGE_FILE "eeprom-periph.bin"
|
2020-11-29 22:48:20 -04:00
|
|
|
#else
|
|
|
|
#define HAL_STORAGE_FILE "eeprom.bin"
|
|
|
|
#endif
|
|
|
|
#endif
|
|
|
|
|
2015-05-04 03:15:12 -03:00
|
|
|
using namespace HALSITL;
|
2012-12-12 18:04:27 -04:00
|
|
|
|
2021-10-05 03:29:45 -03:00
|
|
|
extern HAL_SITL& hal;
|
2019-01-21 00:21:42 -04:00
|
|
|
|
2020-04-10 20:32:15 -03:00
|
|
|
/*
|
|
|
|
emulate flash sector sizes
|
|
|
|
*/
|
|
|
|
#ifndef HAL_FLASH_SECTOR_SIZE
|
|
|
|
#if HAL_STORAGE_SIZE <= 16384
|
|
|
|
#define HAL_FLASH_SECTOR_SIZE (16*1024)
|
|
|
|
#elif HAL_STORAGE_SIZE <= 32768
|
|
|
|
#define HAL_FLASH_SECTOR_SIZE (32*1024)
|
|
|
|
#else
|
|
|
|
#define HAL_FLASH_SECTOR_SIZE (128*1024)
|
|
|
|
#endif
|
|
|
|
#endif
|
|
|
|
|
2020-04-11 23:16:40 -03:00
|
|
|
#ifndef HAL_FLASH_MIN_WRITE_SIZE
|
|
|
|
#define HAL_FLASH_MIN_WRITE_SIZE 1
|
|
|
|
#endif
|
|
|
|
|
|
|
|
#ifndef HAL_FLASH_ALLOW_UPDATE
|
|
|
|
#define HAL_FLASH_ALLOW_UPDATE 1
|
|
|
|
#endif
|
|
|
|
|
2019-01-21 00:21:42 -04:00
|
|
|
void Storage::_storage_open(void)
|
|
|
|
{
|
2021-06-10 23:30:38 -03:00
|
|
|
if (_initialisedType != StorageBackend::None) {
|
|
|
|
// don't reinit
|
2019-01-21 00:21:42 -04:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
_dirty_mask.clearall();
|
|
|
|
|
2021-07-12 22:41:18 -03:00
|
|
|
#define HAL_RAMTRON_ALLOW_FALLBACK 0
|
|
|
|
|
|
|
|
#if STORAGE_USE_FRAM
|
|
|
|
if (hal.get_storage_fram_enabled()) {
|
|
|
|
if (fram.init() && fram.read(0, _buffer, HAL_STORAGE_SIZE)) {
|
|
|
|
// _save_backup(); // FIXME
|
|
|
|
_initialisedType = StorageBackend::FRAM;
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
#if !HAL_RAMTRON_ALLOW_FALLBACK
|
|
|
|
AP_HAL::panic("Unable to init RAMTRON storage");
|
|
|
|
#endif
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
|
2019-01-21 00:21:42 -04:00
|
|
|
#if STORAGE_USE_FLASH
|
2021-10-05 03:29:45 -03:00
|
|
|
if (hal.get_storage_flash_enabled()) {
|
2021-06-10 23:30:38 -03:00
|
|
|
// load from storage backend
|
|
|
|
_flash_load();
|
|
|
|
_initialisedType = StorageBackend::Flash;
|
2019-01-21 00:21:42 -04:00
|
|
|
return;
|
|
|
|
}
|
2021-06-10 23:30:38 -03:00
|
|
|
#endif // STORAGE_USE_FLASH
|
2019-12-01 22:01:31 -04:00
|
|
|
|
2021-06-10 23:30:38 -03:00
|
|
|
#if STORAGE_USE_POSIX
|
2021-10-05 03:29:45 -03:00
|
|
|
if (hal.get_storage_posix_enabled()) {
|
2021-06-10 23:30:38 -03:00
|
|
|
// if we have failed filesystem init don't try again (this is
|
|
|
|
// initialised to zero in the constructor)
|
|
|
|
if (log_fd == -1) {
|
|
|
|
return;
|
|
|
|
}
|
2019-12-01 22:01:31 -04:00
|
|
|
|
2021-06-10 23:30:38 -03:00
|
|
|
log_fd = open(HAL_STORAGE_FILE, O_RDWR|O_CREAT, 0644);
|
|
|
|
if (log_fd == -1) {
|
|
|
|
hal.console->printf("open failed of " HAL_STORAGE_FILE "\n");
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
fcntl(log_fd, F_SETFD, FD_CLOEXEC);
|
|
|
|
|
|
|
|
int ret = read(log_fd, _buffer, HAL_STORAGE_SIZE);
|
|
|
|
if (ret < 0) {
|
|
|
|
hal.console->printf("read failed for " HAL_STORAGE_FILE "\n");
|
|
|
|
close(log_fd);
|
|
|
|
log_fd = -1;
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
// pre-fill to full size
|
|
|
|
if (lseek(log_fd, ret, SEEK_SET) != ret ||
|
|
|
|
write(log_fd, &_buffer[ret], HAL_STORAGE_SIZE-ret) != HAL_STORAGE_SIZE-ret) {
|
|
|
|
hal.console->printf("setup failed for " HAL_STORAGE_FILE "\n");
|
|
|
|
close(log_fd);
|
|
|
|
log_fd = -1;
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
_initialisedType = StorageBackend::SDCard; // AKA POSIX
|
2019-01-21 00:21:42 -04:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
|
2021-06-10 23:30:38 -03:00
|
|
|
// ::printf("No storage backend enabled");
|
2019-01-21 00:21:42 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
mark some lines as dirty. Note that there is no attempt to avoid
|
|
|
|
the race condition between this code and the _timer_tick() code
|
|
|
|
below, which both update _dirty_mask. If we lose the race then the
|
|
|
|
result is that a line is written more than once, but it won't result
|
|
|
|
in a line not being written.
|
|
|
|
*/
|
|
|
|
void Storage::_mark_dirty(uint16_t loc, uint16_t length)
|
|
|
|
{
|
2020-01-30 02:09:29 -04:00
|
|
|
if (length == 0) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
uint16_t end = loc + length - 1;
|
2019-01-21 00:21:42 -04:00
|
|
|
for (uint16_t line=loc>>STORAGE_LINE_SHIFT;
|
|
|
|
line <= end>>STORAGE_LINE_SHIFT;
|
|
|
|
line++) {
|
|
|
|
_dirty_mask.set(line);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void Storage::read_block(void *dst, uint16_t loc, size_t n)
|
2012-12-12 18:04:27 -04:00
|
|
|
{
|
2019-01-21 00:21:42 -04:00
|
|
|
if (loc >= sizeof(_buffer)-(n-1)) {
|
|
|
|
return;
|
2015-05-04 21:59:07 -03:00
|
|
|
}
|
2019-01-21 00:21:42 -04:00
|
|
|
_storage_open();
|
|
|
|
memcpy(dst, &_buffer[loc], n);
|
2012-12-12 18:04:27 -04:00
|
|
|
}
|
|
|
|
|
2019-01-21 00:21:42 -04:00
|
|
|
void Storage::write_block(uint16_t loc, const void *src, size_t n)
|
2012-12-12 18:04:27 -04:00
|
|
|
{
|
2019-01-21 00:21:42 -04:00
|
|
|
if (loc >= sizeof(_buffer)-(n-1)) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
if (memcmp(src, &_buffer[loc], n) != 0) {
|
|
|
|
_storage_open();
|
|
|
|
memcpy(&_buffer[loc], src, n);
|
|
|
|
_mark_dirty(loc, n);
|
|
|
|
}
|
2012-12-12 18:04:27 -04:00
|
|
|
}
|
|
|
|
|
2019-01-21 00:21:42 -04:00
|
|
|
void Storage::_timer_tick(void)
|
2012-12-12 18:04:27 -04:00
|
|
|
{
|
2021-06-10 23:30:38 -03:00
|
|
|
if (_initialisedType == StorageBackend::None) {
|
2019-01-21 00:21:42 -04:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
if (_dirty_mask.empty()) {
|
|
|
|
_last_empty_ms = AP_HAL::millis();
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
// write out the first dirty line. We don't write more
|
|
|
|
// than one to keep the latency of this call to a minimum
|
|
|
|
uint16_t i;
|
|
|
|
for (i=0; i<STORAGE_NUM_LINES; i++) {
|
|
|
|
if (_dirty_mask.get(i)) {
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if (i == STORAGE_NUM_LINES) {
|
|
|
|
// this shouldn't be possible
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2021-07-12 22:41:18 -03:00
|
|
|
#if STORAGE_USE_FRAM
|
|
|
|
if (fram.write(STORAGE_LINE_SIZE*i, &_buffer[STORAGE_LINE_SIZE*i], STORAGE_LINE_SIZE)) {
|
|
|
|
_dirty_mask.clear(i);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
|
2019-01-21 00:21:42 -04:00
|
|
|
#if STORAGE_USE_POSIX
|
2021-10-05 03:29:45 -03:00
|
|
|
if (hal.get_storage_posix_enabled()) {
|
2021-06-10 23:30:38 -03:00
|
|
|
if (log_fd != -1) {
|
|
|
|
const off_t offset = STORAGE_LINE_SIZE*i;
|
|
|
|
if (lseek(log_fd, offset, SEEK_SET) != offset) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
if (write(log_fd, &_buffer[offset], STORAGE_LINE_SIZE) != STORAGE_LINE_SIZE) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
_dirty_mask.clear(i);
|
2019-01-21 00:21:42 -04:00
|
|
|
return;
|
|
|
|
}
|
2021-06-10 23:30:38 -03:00
|
|
|
}
|
2019-01-21 00:21:42 -04:00
|
|
|
#endif
|
2021-06-10 23:30:38 -03:00
|
|
|
|
2019-01-21 00:21:42 -04:00
|
|
|
#if STORAGE_USE_FLASH
|
2021-10-05 03:29:45 -03:00
|
|
|
if (hal.get_storage_flash_enabled()) {
|
2021-06-10 23:30:38 -03:00
|
|
|
// save to storage backend
|
|
|
|
_flash_write(i);
|
|
|
|
return;
|
|
|
|
}
|
2019-01-21 00:21:42 -04:00
|
|
|
#endif
|
2012-12-12 18:04:27 -04:00
|
|
|
}
|
|
|
|
|
2021-06-10 23:30:38 -03:00
|
|
|
#if STORAGE_USE_FLASH
|
|
|
|
|
2019-01-21 00:21:42 -04:00
|
|
|
/*
|
|
|
|
load all data from flash
|
|
|
|
*/
|
|
|
|
void Storage::_flash_load(void)
|
|
|
|
{
|
|
|
|
if (!_flash.init()) {
|
|
|
|
AP_HAL::panic("unable to init flash storage");
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
write one storage line. This also updates _dirty_mask.
|
|
|
|
*/
|
|
|
|
void Storage::_flash_write(uint16_t line)
|
|
|
|
{
|
|
|
|
if (_flash.write(line*STORAGE_LINE_SIZE, STORAGE_LINE_SIZE)) {
|
|
|
|
// mark the line clean
|
|
|
|
_dirty_mask.clear(line);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/*
|
|
|
|
emulate writing to flash
|
|
|
|
*/
|
|
|
|
static int flash_fd = -1;
|
|
|
|
|
|
|
|
static uint32_t sitl_flash_getpageaddr(uint32_t page)
|
|
|
|
{
|
2020-04-10 20:32:15 -03:00
|
|
|
return page * HAL_FLASH_SECTOR_SIZE;
|
2019-01-21 00:21:42 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
static void sitl_flash_open(void)
|
|
|
|
{
|
|
|
|
if (flash_fd == -1) {
|
|
|
|
flash_fd = open("flash.dat", O_RDWR, 0644);
|
|
|
|
if (flash_fd == -1) {
|
|
|
|
flash_fd = open("flash.dat", O_RDWR|O_CREAT, 0644);
|
|
|
|
if (flash_fd == -1) {
|
|
|
|
AP_HAL::panic("Failed to open flash.dat");
|
|
|
|
}
|
2020-04-10 20:32:15 -03:00
|
|
|
if (ftruncate(flash_fd, 2*HAL_FLASH_SECTOR_SIZE) != 0) {
|
2019-01-21 00:21:42 -04:00
|
|
|
AP_HAL::panic("Failed to create flash.dat");
|
|
|
|
}
|
2020-04-10 20:32:15 -03:00
|
|
|
uint8_t fill[HAL_FLASH_SECTOR_SIZE*2];
|
2019-01-21 00:21:42 -04:00
|
|
|
memset(fill, 0xff, sizeof(fill));
|
2021-06-11 02:51:31 -03:00
|
|
|
if (pwrite(flash_fd, fill, sizeof(fill), 0) != (ssize_t)sizeof(fill)) {
|
|
|
|
AP_HAL::panic("Failed to fill flash.dat");
|
|
|
|
}
|
2019-01-21 00:21:42 -04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
static bool sitl_flash_write(uint32_t addr, const uint8_t *data, uint32_t length)
|
|
|
|
{
|
|
|
|
sitl_flash_open();
|
|
|
|
uint8_t old[length];
|
2021-06-11 02:51:31 -03:00
|
|
|
if (pread(flash_fd, old, length, addr) != (ssize_t)length) {
|
2020-04-10 20:32:15 -03:00
|
|
|
AP_HAL::panic("Failed to read flash.dat at %u len=%u", unsigned(addr), unsigned(length));
|
2019-01-21 00:21:42 -04:00
|
|
|
}
|
2020-04-11 23:16:40 -03:00
|
|
|
#if defined(HAL_FLASH_MIN_WRITE_SIZE) && HAL_FLASH_MIN_WRITE_SIZE == 32
|
|
|
|
if ((length % HAL_FLASH_MIN_WRITE_SIZE) != 0 || (addr % HAL_FLASH_MIN_WRITE_SIZE) != 0) {
|
2024-12-13 05:15:44 -04:00
|
|
|
AP_HAL::panic("Attempt to write flash at %u length %u", addr, length);
|
2020-04-11 23:16:40 -03:00
|
|
|
}
|
|
|
|
// emulate H7 requirement that writes be to untouched bytes
|
|
|
|
for (uint32_t i=0; i<length; i+= 32) {
|
|
|
|
if (memcmp(&old[i], &data[i], 32) == 0) {
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
for (uint32_t j=0; j<32; j++) {
|
|
|
|
if (old[i+j] != 0xFF) {
|
2024-12-13 05:15:44 -04:00
|
|
|
AP_HAL::panic("Attempt to write modified flash at %u length %u", addr+i+j, length);
|
2020-04-11 23:16:40 -03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
#endif
|
2019-01-21 00:21:42 -04:00
|
|
|
// check that we are only ever clearing bits (real flash storage can only ever clear bits,
|
|
|
|
// except for an erase
|
|
|
|
for (uint32_t i=0; i<length; i++) {
|
2020-04-11 23:16:40 -03:00
|
|
|
#if HAL_FLASH_ALLOW_UPDATE
|
|
|
|
// emulating flash that allows setting any bit from 1 to 0
|
2019-01-21 00:21:42 -04:00
|
|
|
if (data[i] & ~old[i]) {
|
2024-12-13 05:15:44 -04:00
|
|
|
AP_HAL::panic("Attempt to set flash byte 0x%02x from 0x%02x at %u", data[i], old[i], addr+i);
|
2019-01-21 00:21:42 -04:00
|
|
|
}
|
2020-04-11 23:16:40 -03:00
|
|
|
#else
|
|
|
|
// emulating flash that only allows update if whole byte is 0xFF
|
|
|
|
if (data[i] != old[i] && old[i] != 0xFF) {
|
2024-12-13 05:15:44 -04:00
|
|
|
AP_HAL::panic("Attempt to set flash byte 0x%02x from 0x%02x at %u", data[i], old[i], addr+i);
|
2020-04-11 23:16:40 -03:00
|
|
|
}
|
|
|
|
#endif
|
2019-01-21 00:21:42 -04:00
|
|
|
}
|
2021-06-11 02:51:31 -03:00
|
|
|
return pwrite(flash_fd, data, length, addr) == (ssize_t)length;
|
2019-01-21 00:21:42 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
static bool sitl_flash_read(uint32_t addr, uint8_t *data, uint32_t length)
|
|
|
|
{
|
|
|
|
sitl_flash_open();
|
2021-06-11 02:51:31 -03:00
|
|
|
return pread(flash_fd, data, length, addr) == (ssize_t)length;
|
2019-01-21 00:21:42 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
static bool sitl_flash_erasepage(uint32_t page)
|
|
|
|
{
|
2020-04-10 20:32:15 -03:00
|
|
|
uint8_t fill[HAL_FLASH_SECTOR_SIZE];
|
2019-01-21 00:21:42 -04:00
|
|
|
memset(fill, 0xff, sizeof(fill));
|
|
|
|
sitl_flash_open();
|
2020-04-10 20:32:15 -03:00
|
|
|
bool ret = pwrite(flash_fd, fill, sizeof(fill), page * HAL_FLASH_SECTOR_SIZE) == sizeof(fill);
|
2019-01-21 00:21:42 -04:00
|
|
|
printf("erase %u -> %u\n", page, ret);
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
callback to write data to flash
|
|
|
|
*/
|
|
|
|
bool Storage::_flash_write_data(uint8_t sector, uint32_t offset, const uint8_t *data, uint16_t length)
|
|
|
|
{
|
|
|
|
size_t base_address = sitl_flash_getpageaddr(sector);
|
|
|
|
bool ret = sitl_flash_write(base_address+offset, data, length);
|
|
|
|
if (!ret && _flash_erase_ok()) {
|
|
|
|
// we are getting flash write errors while disarmed. Try
|
|
|
|
// re-writing all of flash
|
|
|
|
uint32_t now = AP_HAL::millis();
|
|
|
|
if (now - _last_re_init_ms > 5000) {
|
|
|
|
_last_re_init_ms = now;
|
|
|
|
bool ok = _flash.re_initialise();
|
|
|
|
hal.console->printf("Storage: failed at %u:%u for %u - re-init %u\n",
|
|
|
|
(unsigned)sector, (unsigned)offset, (unsigned)length, (unsigned)ok);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
callback to read data from flash
|
|
|
|
*/
|
|
|
|
bool Storage::_flash_read_data(uint8_t sector, uint32_t offset, uint8_t *data, uint16_t length)
|
|
|
|
{
|
|
|
|
size_t base_address = sitl_flash_getpageaddr(sector);
|
|
|
|
return sitl_flash_read(base_address+offset, data, length);
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
callback to erase flash sector
|
|
|
|
*/
|
|
|
|
bool Storage::_flash_erase_sector(uint8_t sector)
|
|
|
|
{
|
|
|
|
return sitl_flash_erasepage(sector);
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
callback to check if erase is allowed
|
|
|
|
*/
|
|
|
|
bool Storage::_flash_erase_ok(void)
|
|
|
|
{
|
|
|
|
// only allow erase while disarmed
|
|
|
|
return !hal.util->get_soft_armed();
|
|
|
|
}
|
2021-06-10 23:30:38 -03:00
|
|
|
|
2019-01-21 00:21:42 -04:00
|
|
|
#endif // STORAGE_USE_FLASH
|
|
|
|
|
|
|
|
/*
|
|
|
|
consider storage healthy if we have nothing to write sometime in the
|
|
|
|
last 2 seconds
|
|
|
|
*/
|
|
|
|
bool Storage::healthy(void)
|
|
|
|
{
|
2021-06-10 23:30:38 -03:00
|
|
|
if (_initialisedType == StorageBackend::None) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
return AP_HAL::millis() - _last_empty_ms < 2000;
|
2019-01-21 00:21:42 -04:00
|
|
|
}
|
|
|
|
|
2021-11-08 10:21:31 -04:00
|
|
|
/*
|
|
|
|
get storage size and ptr
|
|
|
|
*/
|
|
|
|
bool Storage::get_storage_ptr(void *&ptr, size_t &size)
|
|
|
|
{
|
|
|
|
if (_initialisedType==StorageBackend::None) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
ptr = _buffer;
|
|
|
|
size = sizeof(_buffer);
|
|
|
|
return true;
|
|
|
|
}
|