2020-04-17 19:23:28 -04:00
|
|
|
/*
|
|
|
|
* This file is part of the MicroPython project, http://micropython.org/
|
|
|
|
*
|
|
|
|
* The MIT License (MIT)
|
|
|
|
*
|
2020-06-03 18:40:05 -04:00
|
|
|
* SPDX-FileCopyrightText: Copyright (c) 2013, 2014 Damien P. George
|
2020-04-17 19:23:28 -04:00
|
|
|
* Copyright (c) 2019 Lucian Copeland for Adafruit Industries
|
|
|
|
*
|
|
|
|
* Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
|
|
* of this software and associated documentation files (the "Software"), to deal
|
|
|
|
* in the Software without restriction, including without limitation the rights
|
|
|
|
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
|
|
* copies of the Software, and to permit persons to whom the Software is
|
|
|
|
* furnished to do so, subject to the following conditions:
|
|
|
|
*
|
|
|
|
* The above copyright notice and this permission notice shall be included in
|
|
|
|
* all copies or substantial portions of the Software.
|
|
|
|
*
|
|
|
|
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
|
|
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
|
|
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
|
|
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
|
|
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
|
|
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
|
|
|
|
* THE SOFTWARE.
|
|
|
|
*/
|
2022-10-05 10:35:47 -04:00
|
|
|
|
2020-04-17 19:23:28 -04:00
|
|
|
#include "supervisor/internal_flash.h"
|
|
|
|
|
|
|
|
#include <stdint.h>
|
|
|
|
#include <string.h>
|
|
|
|
#include <stdbool.h>
|
|
|
|
|
|
|
|
#include "extmod/vfs.h"
|
|
|
|
#include "extmod/vfs_fat.h"
|
2022-10-05 10:35:47 -04:00
|
|
|
|
2020-04-17 19:23:28 -04:00
|
|
|
#include "py/mphal.h"
|
|
|
|
#include "py/obj.h"
|
|
|
|
#include "py/runtime.h"
|
|
|
|
|
2022-10-04 10:02:56 -04:00
|
|
|
#include "esp_ota_ops.h"
|
|
|
|
#include "esp_partition.h"
|
2020-04-17 19:23:28 -04:00
|
|
|
|
2022-10-05 10:35:47 -04:00
|
|
|
#include "supervisor/filesystem.h"
|
2021-11-10 12:07:45 -05:00
|
|
|
#include "supervisor/flash.h"
|
2020-04-17 19:23:28 -04:00
|
|
|
#include "supervisor/usb.h"
|
|
|
|
|
2022-10-05 10:35:47 -04:00
|
|
|
#define OP_READ 0
|
|
|
|
#define OP_WRITE 1
|
2020-04-17 19:23:28 -04:00
|
|
|
|
2020-08-10 20:08:33 -04:00
|
|
|
// TODO: Split the caching out of supervisor/shared/external_flash so we can use it.
|
|
|
|
#define SECTOR_SIZE 4096
|
|
|
|
STATIC uint8_t _cache[SECTOR_SIZE];
|
2020-08-11 14:19:40 -04:00
|
|
|
STATIC uint32_t _cache_lba = 0xffffffff;
|
2020-08-10 20:08:33 -04:00
|
|
|
|
2022-10-05 10:35:47 -04:00
|
|
|
#if CIRCUITPY_STORAGE_EXTEND
|
|
|
|
#if FF_MAX_SS == FF_MIN_SS
|
|
|
|
#define SECSIZE(fs) (FF_MIN_SS)
|
|
|
|
#else
|
|
|
|
#define SECSIZE(fs) ((fs)->ssize)
|
|
|
|
#endif // FF_MAX_SS == FF_MIN_SS
|
|
|
|
STATIC DWORD fatfs_bytes(void) {
|
|
|
|
FATFS *fatfs = filesystem_circuitpy();
|
|
|
|
return (fatfs->csize * SECSIZE(fatfs)) * (fatfs->n_fatent - 2);
|
|
|
|
}
|
|
|
|
STATIC bool storage_extended = true;
|
|
|
|
STATIC const esp_partition_t *_partition[2];
|
|
|
|
#else
|
|
|
|
STATIC const esp_partition_t *_partition[1];
|
|
|
|
#endif // CIRCUITPY_STORAGE_EXTEND
|
|
|
|
|
2020-04-17 19:23:28 -04:00
|
|
|
void supervisor_flash_init(void) {
|
2022-10-05 10:35:47 -04:00
|
|
|
if (_partition[0] != NULL) {
|
|
|
|
return;
|
|
|
|
}
|
2022-10-04 07:55:48 -04:00
|
|
|
_partition[0] = esp_partition_find_first(ESP_PARTITION_TYPE_DATA,
|
2021-03-15 09:57:36 -04:00
|
|
|
ESP_PARTITION_SUBTYPE_DATA_FAT,
|
|
|
|
NULL);
|
2022-10-04 07:56:28 -04:00
|
|
|
#if CIRCUITPY_STORAGE_EXTEND
|
2022-10-04 10:02:56 -04:00
|
|
|
_partition[1] = esp_ota_get_next_update_partition(NULL);
|
2022-10-04 07:56:28 -04:00
|
|
|
#endif
|
2020-04-17 19:23:28 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
uint32_t supervisor_flash_get_block_size(void) {
|
|
|
|
return FILESYSTEM_BLOCK_SIZE;
|
|
|
|
}
|
|
|
|
|
|
|
|
uint32_t supervisor_flash_get_block_count(void) {
|
2022-10-04 07:56:28 -04:00
|
|
|
#if CIRCUITPY_STORAGE_EXTEND
|
2022-10-05 10:35:47 -04:00
|
|
|
return ((storage_extended) ? (_partition[0]->size + _partition[1]->size) : _partition[0]->size) / FILESYSTEM_BLOCK_SIZE;
|
2022-10-04 07:56:28 -04:00
|
|
|
#else
|
|
|
|
return _partition[0]->size / FILESYSTEM_BLOCK_SIZE;
|
|
|
|
#endif
|
2020-04-17 19:23:28 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
void port_internal_flash_flush(void) {
|
|
|
|
}
|
|
|
|
|
2022-10-05 10:35:47 -04:00
|
|
|
STATIC void single_partition_rw(const esp_partition_t *partition, uint8_t *data,
|
|
|
|
const uint32_t offset, const uint32_t size_total, const bool op) {
|
|
|
|
if (op == OP_READ) {
|
|
|
|
esp_partition_read(partition, offset, data, size_total);
|
|
|
|
} else {
|
|
|
|
esp_partition_erase_range(partition, offset, size_total);
|
|
|
|
esp_partition_write(partition, offset, _cache, size_total);
|
|
|
|
}
|
|
|
|
}
|
2022-10-04 07:55:48 -04:00
|
|
|
|
2022-10-05 10:35:47 -04:00
|
|
|
#if CIRCUITPY_STORAGE_EXTEND
|
|
|
|
STATIC void multi_partition_rw(uint8_t *data,
|
|
|
|
const uint32_t offset, const uint32_t size_total, const bool op) {
|
2022-10-04 07:55:48 -04:00
|
|
|
if (offset > _partition[0]->size) {
|
2022-10-05 10:35:47 -04:00
|
|
|
// only r/w partition 1
|
|
|
|
single_partition_rw(_partition[1], data, (offset - _partition[0]->size), size_total, op);
|
|
|
|
} else if ((offset + size_total) > _partition[0]->size) {
|
|
|
|
// first r/w partition 0, then partition 1
|
|
|
|
uint32_t size_0 = _partition[0]->size - offset;
|
|
|
|
uint32_t size_1 = size_total - size_0;
|
|
|
|
if (op == OP_READ) {
|
|
|
|
esp_partition_read(_partition[0], offset, data, size_0);
|
|
|
|
esp_partition_read(_partition[1], 0, (data + size_0), size_1);
|
|
|
|
} else {
|
|
|
|
esp_partition_erase_range(_partition[0], offset, size_0);
|
|
|
|
esp_partition_write(_partition[0], offset, _cache, size_0);
|
|
|
|
esp_partition_erase_range(_partition[1], 0, size_1);
|
|
|
|
esp_partition_write(_partition[1], 0, (_cache + size_0), size_1);
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
// only r/w partition 0
|
|
|
|
single_partition_rw(_partition[0], data, offset, size_total, op);
|
2022-10-04 07:55:48 -04:00
|
|
|
}
|
2022-10-05 10:35:47 -04:00
|
|
|
}
|
|
|
|
#endif
|
2022-10-04 07:55:48 -04:00
|
|
|
|
2022-10-05 10:35:47 -04:00
|
|
|
mp_uint_t supervisor_flash_read_blocks(uint8_t *dest, uint32_t block, uint32_t num_blocks) {
|
|
|
|
const uint32_t offset = block * FILESYSTEM_BLOCK_SIZE;
|
|
|
|
const uint32_t read_total = num_blocks * FILESYSTEM_BLOCK_SIZE;
|
|
|
|
#if CIRCUITPY_STORAGE_EXTEND
|
|
|
|
multi_partition_rw(dest, offset, read_total, OP_READ);
|
|
|
|
#else
|
|
|
|
single_partition_rw(_partition[0], dest, offset, read_total, OP_READ);
|
|
|
|
#endif
|
|
|
|
return 0; // success
|
2020-04-17 19:23:28 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
mp_uint_t supervisor_flash_write_blocks(const uint8_t *src, uint32_t lba, uint32_t num_blocks) {
|
|
|
|
uint32_t blocks_per_sector = SECTOR_SIZE / FILESYSTEM_BLOCK_SIZE;
|
|
|
|
uint32_t block = 0;
|
|
|
|
while (block < num_blocks) {
|
|
|
|
uint32_t block_address = lba + block;
|
|
|
|
uint32_t sector_offset = block_address / blocks_per_sector * SECTOR_SIZE;
|
|
|
|
uint8_t block_offset = block_address % blocks_per_sector;
|
|
|
|
if (_cache_lba != block_address) {
|
2022-10-04 07:55:48 -04:00
|
|
|
supervisor_flash_read_blocks(_cache, sector_offset / FILESYSTEM_BLOCK_SIZE, blocks_per_sector);
|
2020-04-17 19:23:28 -04:00
|
|
|
_cache_lba = sector_offset;
|
|
|
|
}
|
|
|
|
for (uint8_t b = block_offset; b < blocks_per_sector; b++) {
|
2020-05-28 18:43:55 -04:00
|
|
|
// Stop copying after the last block.
|
|
|
|
if (block >= num_blocks) {
|
2021-03-15 09:57:36 -04:00
|
|
|
break;
|
2020-05-28 18:43:55 -04:00
|
|
|
}
|
2020-04-17 19:23:28 -04:00
|
|
|
memcpy(_cache + b * FILESYSTEM_BLOCK_SIZE,
|
2021-03-15 09:57:36 -04:00
|
|
|
src + block * FILESYSTEM_BLOCK_SIZE,
|
|
|
|
FILESYSTEM_BLOCK_SIZE);
|
2020-04-17 19:23:28 -04:00
|
|
|
block++;
|
|
|
|
}
|
2022-10-04 07:56:28 -04:00
|
|
|
#if CIRCUITPY_STORAGE_EXTEND
|
2022-10-05 10:35:47 -04:00
|
|
|
multi_partition_rw(_cache, sector_offset, SECTOR_SIZE, OP_WRITE);
|
|
|
|
#else
|
|
|
|
single_partition_rw(_partition[0], _cache, sector_offset, SECTOR_SIZE, OP_READ);
|
2022-10-04 07:56:28 -04:00
|
|
|
#endif
|
2020-04-17 19:23:28 -04:00
|
|
|
}
|
|
|
|
return 0; // success
|
|
|
|
}
|
|
|
|
|
|
|
|
void supervisor_flash_release_cache(void) {
|
|
|
|
}
|
2022-10-05 10:35:47 -04:00
|
|
|
|
|
|
|
void supervisor_flash_set_extended(bool extended) {
|
|
|
|
#if CIRCUITPY_STORAGE_EXTEND
|
|
|
|
storage_extended = extended;
|
|
|
|
#endif
|
|
|
|
}
|
|
|
|
|
|
|
|
bool supervisor_flash_get_extended(void) {
|
|
|
|
#if CIRCUITPY_STORAGE_EXTEND
|
|
|
|
return storage_extended;
|
|
|
|
#else
|
|
|
|
return false;
|
|
|
|
#endif
|
|
|
|
}
|
|
|
|
|
|
|
|
void supervisor_flash_update_extended(void) {
|
|
|
|
#if CIRCUITPY_STORAGE_EXTEND
|
|
|
|
storage_extended = (_partition[0]->size < fatfs_bytes());
|
|
|
|
#endif
|
|
|
|
}
|