+uint8_t
+ao_storage_is_erased(uint32_t pos)
+{
+ uint32_t read_pos;
+ uint32_t read_len;
+ uint32_t i;
+ uint8_t ret = 1;
+
+ ao_storage_setup();
+ ao_mutex_get(&storage_mutex);
+ read_pos = pos;
+ read_len = ao_storage_block;
+ while (read_len) {
+ uint32_t this_time = AO_STORAGE_DATA_SIZE;
+ if (this_time > read_len)
+ this_time = read_len;
+ if (!ao_storage_read(read_pos, storage_data, this_time)) {
+ ret = 0;
+ goto done;
+ }
+ for (i = 0; i < this_time; i++)
+ if (storage_data[i] != 0xff) {
+ ret = 0;
+ goto done;
+ }
+ read_pos += this_time;
+ read_len -= this_time;
+ }
+done:
+ ao_mutex_put(&storage_mutex);
+ return ret;
+}
+
+uint8_t
+ao_storage_erase(uint32_t start_pos, uint32_t len)
+{
+ /* Round 'len' up to ao_storage_block units */
+ len = ((len + ao_storage_block - 1) / ao_storage_block) * ao_storage_block;
+
+ /*
+ * Start at the end of the area to erase so that the
+ * last block cleared is the first block; this will ensure
+ * that partially erased flight logs still appear in the list
+ * and can be re-erased.
+ */
+ uint32_t pos = start_pos + len - ao_storage_block;
+ while (len) {
+ int tries;
+
+#define MAX_TRIES 4 /* needs to be at least 2 */
+ for (tries = 0; tries < MAX_TRIES; tries++) {
+ if (ao_storage_is_erased(pos))
+ break;
+ if (!ao_storage_device_erase(pos))
+ return 0;
+ }
+ if (tries == MAX_TRIES)
+ return 0;
+ pos -= ao_storage_block;
+ len -= ao_storage_block;
+ }
+ return 1;
+}