From 6cd3111d79d62bd603778accf2e9bcd7a450ab29 Mon Sep 17 00:00:00 2001 From: Johny Mattsson Date: Thu, 22 Sep 2016 13:09:56 +1000 Subject: [PATCH] Platform interface to flash partition info. --- components/platform/include/platform.h | 45 +++++++++++----- components/platform/platform_partition.c | 68 ++++++++++++++++++++++++ 2 files changed, 101 insertions(+), 12 deletions(-) create mode 100644 components/platform/platform_partition.c diff --git a/components/platform/include/platform.h b/components/platform/include/platform.h index a49df62e..8852b4e2 100644 --- a/components/platform/include/platform.h +++ b/components/platform/include/platform.h @@ -2,6 +2,7 @@ #define _PLATFORM_H_ #include +#include #include #include "sdkconfig.h" #include "cpu_esp32.h" @@ -17,8 +18,21 @@ enum PLATFORM_UNDERFLOW = -1 }; -int platform_init (void); +#if CONFIG_NODE_DEBUG +# define NODE_DBG printf +#else +# define NODE_DBG(...) do{}while(0) +#endif + +#if CONFIG_NODE_ERR +# define NODE_ERR printf +#else +# define NODE_ERR(...) do{}while(0) +#endif + + +int platform_init (void); // ***************************************************************************** // UART subsection @@ -54,7 +68,6 @@ void platform_uart_send( unsigned id, uint8_t data ); int platform_uart_set_flow_control( unsigned id, int type ); - // Internal flash erase/write functions uint32_t platform_flash_get_first_free_block_address( uint32_t *psect ); @@ -76,17 +89,25 @@ int platform_flash_erase_sector( uint32_t sector_id ); */ uint32_t platform_flash_mapped2phys (uint32_t mapped_addr); -#if CONFIG_NODE_DEBUG -# define NODE_DBG printf -#else -# define NODE_DBG(...) do{}while(0) -#endif -#if CONFIG_NODE_ERR -# define NODE_ERR printf -#else -# define NODE_ERR(...) do{}while(0) -#endif +// Internal flash partitions +typedef struct { + uint8_t label[16]; + uint32_t offs; + uint32_t size; + uint8_t type; + uint8_t subtype; +} platform_partition_t; + +/** + * Obtain partition information for the internal flash. + * @param idx Which partition index to load info for. + * @param info Buffer to store the info in. + * @returns True if the partition info was loaded, false if not (e.g. no such + * partition idx). + */ +bool platform_partition_info (uint8_t idx, platform_partition_t *info); + // ***************************************************************************** diff --git a/components/platform/platform_partition.c b/components/platform/platform_partition.c new file mode 100644 index 00000000..66c6d9e9 --- /dev/null +++ b/components/platform/platform_partition.c @@ -0,0 +1,68 @@ +/* + * Copyright 2016 Dius Computing Pty Ltd. All rights reserved. + * + * Redistribution and use in source and binary forms, with or without + * modification, are permitted provided that the following conditions + * are met: + * + * - Redistributions of source code must retain the above copyright + * notice, this list of conditions and the following disclaimer. + * - Redistributions in binary form must reproduce the above copyright + * notice, this list of conditions and the following disclaimer in the + * documentation and/or other materials provided with the + * distribution. + * - Neither the name of the copyright holders nor the names of + * its contributors may be used to endorse or promote products derived + * from this software without specific prior written permission. + * + * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS + * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT + * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS + * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL + * THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, + * INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES + * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR + * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) + * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, + * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) + * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED + * OF THE POSSIBILITY OF SUCH DAMAGE. + * + * @author Johny Mattsson + */ + +#include "platform.h" + +#include +#include "../../bootloader/src/main/bootloader_config.h" +#include "esp_spi_flash.h" + +static inline bool possible_idx (uint8_t idx) +{ + return ((idx +1) * sizeof (partition_info_t)) < SPI_FLASH_SEC_SIZE; +} + + +bool platform_partition_info (uint8_t idx, platform_partition_t *info) +{ + if (!possible_idx (idx)) + return false; + + partition_info_t pi; + esp_err_t err = spi_flash_read ( + PARTITION_ADD + idx * sizeof(pi), (uint32_t *)&pi, sizeof (pi)); + if (err != ESP_OK) + return false; + + if (pi.magic != PARTITION_MAGIC) + return false; + + memcpy (info->label, pi.label, sizeof (info->label)); + info->offs = pi.pos.offset; + info->size = pi.pos.size; + info->type = pi.type; + info->subtype = pi.subtype; + + return true; +} +