feat(spi_flash): Modify partition driver

Use "sys/queue.h" as default queue header file.
This commit is contained in:
Dong Heng
2018-06-05 17:07:26 +08:00
committed by Wu Jian Gang
parent 55bad94c43
commit 06bbb9e4bf
7 changed files with 56 additions and 243 deletions

View File

@ -27,11 +27,10 @@
#include <stdint.h> #include <stdint.h>
#include <stdbool.h> #include <stdbool.h>
#include <sys/queue.h>
#include "esp_wifi.h" #include "esp_wifi.h"
#include "queue.h"
#ifdef __cplusplus #ifdef __cplusplus
extern "C" { extern "C" {
#endif #endif

View File

@ -27,8 +27,8 @@
#include <stdbool.h> #include <stdbool.h>
#include <stdint.h> #include <stdint.h>
#include <sys/queue.h>
#include "queue.h"
#include "esp_wifi.h" #include "esp_wifi.h"
#ifdef __cplusplus #ifdef __cplusplus

View File

@ -1,236 +0,0 @@
/*
* ESPRSSIF MIT License
*
* Copyright (c) 2015 <ESPRESSIF SYSTEMS (SHANGHAI) PTE LTD>
*
* Permission is hereby granted for use on ESPRESSIF SYSTEMS ESP8266 only, in which case,
* it is 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.
*
*/
#ifndef _SYS_QUEUE_H_
#define _SYS_QUEUE_H_
#ifdef __cplusplus
extern "C" {
#endif
#define QMD_SAVELINK(name, link)
#define TRASHIT(x)
/*
* Singly-linked List declarations.
*/
#define SLIST_HEAD(name, type) \
struct name { \
struct type *slh_first; /* first element */ \
}
#define SLIST_HEAD_INITIALIZER(head) \
{ NULL }
#define SLIST_ENTRY(type) \
struct { \
struct type *sle_next; /* next element */ \
}
/*
* Singly-linked List functions.
*/
#define SLIST_EMPTY(head) ((head)->slh_first == NULL)
#define SLIST_FIRST(head) ((head)->slh_first)
#define SLIST_FOREACH(var, head, field) \
for ((var) = SLIST_FIRST((head)); \
(var); \
(var) = SLIST_NEXT((var), field))
#define SLIST_FOREACH_SAFE(var, head, field, tvar) \
for ((var) = SLIST_FIRST((head)); \
(var) && ((tvar) = SLIST_NEXT((var), field), 1); \
(var) = (tvar))
#define SLIST_FOREACH_PREVPTR(var, varp, head, field) \
for ((varp) = &SLIST_FIRST((head)); \
((var) = *(varp)) != NULL; \
(varp) = &SLIST_NEXT((var), field))
#define SLIST_INIT(head) do { \
SLIST_FIRST((head)) = NULL; \
} while (0)
#define SLIST_INSERT_AFTER(slistelm, elm, field) do { \
SLIST_NEXT((elm), field) = SLIST_NEXT((slistelm), field); \
SLIST_NEXT((slistelm), field) = (elm); \
} while (0)
#define SLIST_INSERT_HEAD(head, elm, field) do { \
SLIST_NEXT((elm), field) = SLIST_FIRST((head)); \
SLIST_FIRST((head)) = (elm); \
} while (0)
#define SLIST_NEXT(elm, field) ((elm)->field.sle_next)
#define SLIST_REMOVE(head, elm, type, field) do { \
QMD_SAVELINK(oldnext, (elm)->field.sle_next); \
if (SLIST_FIRST((head)) == (elm)) { \
SLIST_REMOVE_HEAD((head), field); \
} \
else { \
struct type *curelm = SLIST_FIRST((head)); \
while (SLIST_NEXT(curelm, field) != (elm)) \
curelm = SLIST_NEXT(curelm, field); \
SLIST_REMOVE_AFTER(curelm, field); \
} \
TRASHIT(*oldnext); \
} while (0)
#define SLIST_REMOVE_AFTER(elm, field) do { \
SLIST_NEXT(elm, field) = \
SLIST_NEXT(SLIST_NEXT(elm, field), field); \
} while (0)
#define SLIST_REMOVE_HEAD(head, field) do { \
SLIST_FIRST((head)) = SLIST_NEXT(SLIST_FIRST((head)), field); \
} while (0)
/*
* Singly-linked Tail queue declarations.
*/
#define STAILQ_HEAD(name, type) \
struct name { \
struct type *stqh_first; /* first element */ \
struct type **stqh_last; /* addr of last next element */ \
}
#define STAILQ_HEAD_INITIALIZER(head) \
{ NULL, &(head).stqh_first }
#define STAILQ_ENTRY(type) \
struct { \
struct type *stqe_next; /* next element */ \
}
/*
* Singly-linked Tail queue functions.
*/
#define STAILQ_CONCAT(head1, head2) do { \
if (!STAILQ_EMPTY((head2))) { \
*(head1)->stqh_last = (head2)->stqh_first; \
(head1)->stqh_last = (head2)->stqh_last; \
STAILQ_INIT((head2)); \
} \
} while (0)
#define STAILQ_EMPTY(head) ((head)->stqh_first == NULL)
#define STAILQ_FIRST(head) ((head)->stqh_first)
#define STAILQ_FOREACH(var, head, field) \
for((var) = STAILQ_FIRST((head)); \
(var); \
(var) = STAILQ_NEXT((var), field))
#define STAILQ_FOREACH_SAFE(var, head, field, tvar) \
for ((var) = STAILQ_FIRST((head)); \
(var) && ((tvar) = STAILQ_NEXT((var), field), 1); \
(var) = (tvar))
#define STAILQ_INIT(head) do { \
STAILQ_FIRST((head)) = NULL; \
(head)->stqh_last = &STAILQ_FIRST((head)); \
} while (0)
#define STAILQ_INSERT_AFTER(head, tqelm, elm, field) do { \
if ((STAILQ_NEXT((elm), field) = STAILQ_NEXT((tqelm), field)) == NULL)\
(head)->stqh_last = &STAILQ_NEXT((elm), field); \
STAILQ_NEXT((tqelm), field) = (elm); \
} while (0)
#define STAILQ_INSERT_HEAD(head, elm, field) do { \
if ((STAILQ_NEXT((elm), field) = STAILQ_FIRST((head))) == NULL) \
(head)->stqh_last = &STAILQ_NEXT((elm), field); \
STAILQ_FIRST((head)) = (elm); \
} while (0)
#define STAILQ_INSERT_TAIL(head, elm, field) do { \
STAILQ_NEXT((elm), field) = NULL; \
*(head)->stqh_last = (elm); \
(head)->stqh_last = &STAILQ_NEXT((elm), field); \
} while (0)
#define STAILQ_LAST(head, type, field) \
(STAILQ_EMPTY((head))? \
NULL : \
((struct type *)(void *)\
((char *)((head)->stqh_last) - __offsetof(struct type, field))))
#define STAILQ_NEXT(elm, field) ((elm)->field.stqe_next)
#define STAILQ_REMOVE(head, elm, type, field) do { \
QMD_SAVELINK(oldnext, (elm)->field.stqe_next); \
if (STAILQ_FIRST((head)) == (elm)) { \
STAILQ_REMOVE_HEAD((head), field); \
} \
else { \
struct type *curelm = STAILQ_FIRST((head)); \
while (STAILQ_NEXT(curelm, field) != (elm)) \
curelm = STAILQ_NEXT(curelm, field); \
STAILQ_REMOVE_AFTER(head, curelm, field); \
} \
TRASHIT(*oldnext); \
} while (0)
#define STAILQ_REMOVE_HEAD(head, field) do { \
if ((STAILQ_FIRST((head)) = \
STAILQ_NEXT(STAILQ_FIRST((head)), field)) == NULL) \
(head)->stqh_last = &STAILQ_FIRST((head)); \
} while (0)
#define STAILQ_REMOVE_AFTER(head, elm, field) do { \
if ((STAILQ_NEXT(elm, field) = \
STAILQ_NEXT(STAILQ_NEXT(elm, field), field)) == NULL) \
(head)->stqh_last = &STAILQ_NEXT((elm), field); \
} while (0)
#define STAILQ_SWAP(head1, head2, type) do { \
struct type *swap_first = STAILQ_FIRST(head1); \
struct type **swap_last = (head1)->stqh_last; \
STAILQ_FIRST(head1) = STAILQ_FIRST(head2); \
(head1)->stqh_last = (head2)->stqh_last; \
STAILQ_FIRST(head2) = swap_first; \
(head2)->stqh_last = swap_last; \
if (STAILQ_EMPTY(head1)) \
(head1)->stqh_last = &STAILQ_FIRST(head1); \
if (STAILQ_EMPTY(head2)) \
(head2)->stqh_last = &STAILQ_FIRST(head2); \
} while (0)
#define STAILQ_INSERT_CHAIN_HEAD(head, elm_chead, elm_ctail, field) do { \
if ((STAILQ_NEXT(elm_ctail, field) = STAILQ_FIRST(head)) == NULL ) { \
(head)->stqh_last = &STAILQ_NEXT(elm_ctail, field); \
} \
STAILQ_FIRST(head) = (elm_chead); \
} while (0)
#ifdef __cplusplus
}
#endif
#endif /* !_SYS_QUEUE_H_ */

View File

@ -3,3 +3,5 @@
# #
COMPONENT_SRCDIRS := src COMPONENT_SRCDIRS := src
CFLAGS += -DPARTITION_QUEUE_HEADER=\"sys/queue.h\"

View File

@ -42,11 +42,11 @@ typedef struct {
* See docs/partition_tables.rst for more information about individual fields. * See docs/partition_tables.rst for more information about individual fields.
*/ */
typedef struct { typedef struct {
uint16_t magic; uint16_t magic;
uint8_t type; uint8_t type;
uint8_t subtype; uint8_t subtype;
esp_partition_pos_t pos; esp_partition_pos_t pos;
uint8_t label[16]; uint8_t label[16];
uint32_t flags; uint32_t flags;
} esp_partition_info_t; } esp_partition_info_t;

View File

@ -257,6 +257,7 @@ esp_err_t esp_partition_write(const esp_partition_t* partition,
esp_err_t esp_partition_erase_range(const esp_partition_t* partition, esp_err_t esp_partition_erase_range(const esp_partition_t* partition,
uint32_t start_addr, uint32_t size); uint32_t start_addr, uint32_t size);
#ifdef CONFIG_ENABLE_FLASH_MMAP
/** /**
* @brief Configure MMU to map partition into data memory * @brief Configure MMU to map partition into data memory
* *
@ -286,6 +287,7 @@ esp_err_t esp_partition_mmap(const esp_partition_t* partition, uint32_t offset,
spi_flash_mmap_memory_t memory, spi_flash_mmap_memory_t memory,
const void** out_ptr, spi_flash_mmap_handle_t* out_handle); const void** out_ptr, spi_flash_mmap_handle_t* out_handle);
#endif /* CONFIG_ENABLE_FLASH_MMAP */
#ifdef __cplusplus #ifdef __cplusplus
} }

View File

@ -22,7 +22,9 @@
#include "esp_flash_data_types.h" #include "esp_flash_data_types.h"
#include "esp_spi_flash.h" #include "esp_spi_flash.h"
#include "esp_partition.h" #include "esp_partition.h"
#ifdef CONFIG_ENABLE_FLASH_ENCRYPT
#include "esp_flash_encrypt.h" #include "esp_flash_encrypt.h"
#endif
#include "esp_log.h" #include "esp_log.h"
@ -30,7 +32,11 @@
// Enable built-in checks in queue.h in debug builds // Enable built-in checks in queue.h in debug builds
#define INVARIANTS #define INVARIANTS
#endif #endif
#if defined(PARTITION_QUEUE_HEADER)
#include PARTITION_QUEUE_HEADER
#else
#include "rom/queue.h" #include "rom/queue.h"
#endif
typedef struct partition_list_item_ { typedef struct partition_list_item_ {
@ -142,7 +148,9 @@ static esp_partition_iterator_opaque_t* iterator_create(esp_partition_type_t typ
// This function is called only once, with s_partition_list_lock taken. // This function is called only once, with s_partition_list_lock taken.
static esp_err_t load_partitions() static esp_err_t load_partitions()
{ {
#ifdef CONFIG_ENABLE_FLASH_MMAP
const uint32_t* ptr; const uint32_t* ptr;
spi_flash_mmap_handle_t handle; spi_flash_mmap_handle_t handle;
// map 64kB block where partition table is located // map 64kB block where partition table is located
esp_err_t err = spi_flash_mmap(ESP_PARTITION_TABLE_ADDR & 0xffff0000, esp_err_t err = spi_flash_mmap(ESP_PARTITION_TABLE_ADDR & 0xffff0000,
@ -150,9 +158,26 @@ static esp_err_t load_partitions()
if (err != ESP_OK) { if (err != ESP_OK) {
return err; return err;
} }
// calculate partition address within mmap-ed region // calculate partition address within mmap-ed region
const esp_partition_info_t* it = (const esp_partition_info_t*) const esp_partition_info_t* it = (const esp_partition_info_t*)
(ptr + (ESP_PARTITION_TABLE_ADDR & 0xffff) / sizeof(*ptr)); (ptr + (ESP_PARTITION_TABLE_ADDR & 0xffff) / sizeof(*ptr));
#else
esp_err_t ret;
uint32_t *ptr;
const size_t read_size = SPI_FLASH_SEC_SIZE;
ptr = malloc(read_size);
if (!ptr)
return ESP_ERR_NO_MEM;
ret = spi_flash_read(ESP_PARTITION_TABLE_ADDR, ptr, read_size);
if (ret != ESP_OK) {
free(ptr);
return ret;
}
// calculate partition address within mmap-ed region
const esp_partition_info_t* it = (const esp_partition_info_t*)ptr;
#endif
const esp_partition_info_t* end = it + SPI_FLASH_SEC_SIZE / sizeof(*it); const esp_partition_info_t* end = it + SPI_FLASH_SEC_SIZE / sizeof(*it);
// tail of the linked list of partitions // tail of the linked list of partitions
partition_list_item_t* last = NULL; partition_list_item_t* last = NULL;
@ -166,6 +191,7 @@ static esp_err_t load_partitions()
item->info.size = it->pos.size; item->info.size = it->pos.size;
item->info.type = it->type; item->info.type = it->type;
item->info.subtype = it->subtype; item->info.subtype = it->subtype;
#ifdef CONFIG_ENABLE_FLASH_ENCRYPT
item->info.encrypted = it->flags & PART_FLAG_ENCRYPTED; item->info.encrypted = it->flags & PART_FLAG_ENCRYPTED;
if (esp_flash_encryption_enabled() && ( if (esp_flash_encryption_enabled() && (
it->type == PART_TYPE_APP it->type == PART_TYPE_APP
@ -174,6 +200,9 @@ static esp_err_t load_partitions()
are always encrypted */ are always encrypted */
item->info.encrypted = true; item->info.encrypted = true;
} }
else
item->info.encrypted = false;
#endif
// it->label may not be zero-terminated // it->label may not be zero-terminated
strncpy(item->info.label, (const char*) it->label, sizeof(it->label)); strncpy(item->info.label, (const char*) it->label, sizeof(it->label));
@ -186,7 +215,11 @@ static esp_err_t load_partitions()
} }
last = item; last = item;
} }
#ifdef CONFIG_ENABLE_FLASH_MMAP
spi_flash_munmap(handle); spi_flash_munmap(handle);
#else
free(ptr);
#endif
return ESP_OK; return ESP_OK;
} }
@ -214,7 +247,10 @@ const esp_partition_t *esp_partition_verify(const esp_partition_t *partition)
/* Can't memcmp() whole structure here as padding contents may be different */ /* Can't memcmp() whole structure here as padding contents may be different */
if (p->address == partition->address if (p->address == partition->address
&& partition->size == p->size && partition->size == p->size
&& partition->encrypted == p->encrypted) { #ifdef CONFIG_ENABLE_FLASH_ENCRYPT
&& partition->encrypted == p->encrypted
#endif
) {
esp_partition_iterator_release(it); esp_partition_iterator_release(it);
return p; return p;
} }
@ -235,6 +271,7 @@ esp_err_t esp_partition_read(const esp_partition_t* partition,
return ESP_ERR_INVALID_SIZE; return ESP_ERR_INVALID_SIZE;
} }
#ifdef CONFIG_ENABLE_FLASH_ENCRYPT
if (!partition->encrypted) { if (!partition->encrypted) {
return spi_flash_read(partition->address + src_offset, dst, size); return spi_flash_read(partition->address + src_offset, dst, size);
} else { } else {
@ -252,6 +289,9 @@ esp_err_t esp_partition_read(const esp_partition_t* partition,
spi_flash_munmap(handle); spi_flash_munmap(handle);
return ESP_OK; return ESP_OK;
} }
#else
return spi_flash_read(partition->address + src_offset, dst, size);
#endif
} }
esp_err_t esp_partition_write(const esp_partition_t* partition, esp_err_t esp_partition_write(const esp_partition_t* partition,
@ -265,11 +305,15 @@ esp_err_t esp_partition_write(const esp_partition_t* partition,
return ESP_ERR_INVALID_SIZE; return ESP_ERR_INVALID_SIZE;
} }
dst_offset = partition->address + dst_offset; dst_offset = partition->address + dst_offset;
#ifdef CONFIG_ENABLE_FLASH_ENCRYPT
if (partition->encrypted) { if (partition->encrypted) {
return spi_flash_write_encrypted(dst_offset, src, size); return spi_flash_write_encrypted(dst_offset, src, size);
} else { } else {
return spi_flash_write(dst_offset, src, size); return spi_flash_write(dst_offset, src, size);
} }
#else
return spi_flash_write(dst_offset, src, size);
#endif
} }
esp_err_t esp_partition_erase_range(const esp_partition_t* partition, esp_err_t esp_partition_erase_range(const esp_partition_t* partition,
@ -300,6 +344,7 @@ esp_err_t esp_partition_erase_range(const esp_partition_t* partition,
* we can add esp_partition_mmapv which will accept an array of offsets and sizes, and return array of * we can add esp_partition_mmapv which will accept an array of offsets and sizes, and return array of
* mmaped pointers, and a single handle for all these regions. * mmaped pointers, and a single handle for all these regions.
*/ */
#ifdef CONFIG_ENABLE_FLASH_MMAP
esp_err_t esp_partition_mmap(const esp_partition_t* partition, uint32_t offset, uint32_t size, esp_err_t esp_partition_mmap(const esp_partition_t* partition, uint32_t offset, uint32_t size,
spi_flash_mmap_memory_t memory, spi_flash_mmap_memory_t memory,
const void** out_ptr, spi_flash_mmap_handle_t* out_handle) const void** out_ptr, spi_flash_mmap_handle_t* out_handle)
@ -322,3 +367,4 @@ esp_err_t esp_partition_mmap(const esp_partition_t* partition, uint32_t offset,
} }
return rc; return rc;
} }
#endif