]> granicus.if.org Git - esp-idf/commitdiff
feature/fota_ops_api: add ota core api implement
authorTian Zhong Xing <tianzhongxing@espressif.com>
Mon, 28 Nov 2016 05:53:03 +0000 (13:53 +0800)
committertzx <tianzhongxing@espressif.com>
Mon, 28 Nov 2016 09:12:57 +0000 (17:12 +0800)
components/app_update/component.mk [new file with mode: 0755]
components/app_update/esp_ota_ops.c [new file with mode: 0644]
components/app_update/include/esp_ota_ops.h [new file with mode: 0755]
components/spi_flash/flash_ops.c
components/spi_flash/partition.c
docs/Doxyfile
docs/api/ota.rst [new file with mode: 0644]
docs/index.rst

diff --git a/components/app_update/component.mk b/components/app_update/component.mk
new file mode 100755 (executable)
index 0000000..c2c4c03
--- /dev/null
@@ -0,0 +1,5 @@
+#
+# Component Makefile
+#
+# (Uses default behaviour of compiling all source files in directory, adding 'include' to include path.)
+
diff --git a/components/app_update/esp_ota_ops.c b/components/app_update/esp_ota_ops.c
new file mode 100644 (file)
index 0000000..14b1c8c
--- /dev/null
@@ -0,0 +1,381 @@
+// Copyright 2015-2016 Espressif Systems (Shanghai) PTE LTD
+//
+// Licensed under the Apache License, Version 2.0 (the "License");
+// you may not use this file except in compliance with the License.
+// You may obtain a copy of the License at
+
+//     http://www.apache.org/licenses/LICENSE-2.0
+//
+// Unless required by applicable law or agreed to in writing, software
+// distributed under the License is distributed on an "AS IS" BASIS,
+// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+// See the License for the specific language governing permissions and
+// limitations under the License.
+
+#include <stdint.h>
+#include <stdbool.h>
+#include <stddef.h>
+#include <stdlib.h>
+#include <stdio.h>
+#include <string.h>
+#include <assert.h>
+#include <freertos/FreeRTOS.h>
+#include <freertos/task.h>
+
+#include "esp_err.h"
+#include "esp_partition.h"
+#include "esp_spi_flash.h"
+#include "esp_image_format.h"
+#include "esp_secure_boot.h"
+#include "sdkconfig.h"
+
+#include "esp_ota_ops.h"
+#include "rom/queue.h"
+#include "rom/crc.h"
+#include "esp_log.h"
+
+
+#define OTA_MAX(a,b) ((a) >= (b) ? (a) : (b)) 
+#define OTA_MIN(a,b) ((a) <= (b) ? (a) : (b)) 
+#define SUB_TYPE_ID(i) (i & 0x0F) 
+
+typedef struct ota_ops_entry_ {
+    uint32_t handle;
+    esp_partition_t part;
+    uint32_t erased_size;
+    uint32_t wrote_size;
+    LIST_ENTRY(ota_ops_entry_) entries;
+} ota_ops_entry_t;
+
+/* OTA selection structure (two copies in the OTA data partition.)
+   Size of 32 bytes is friendly to flash encryption */
+typedef struct {
+    uint32_t ota_seq;
+    uint8_t  seq_label[24];
+    uint32_t crc;                /* CRC32 of ota_seq field only */
+} ota_select;
+
+static LIST_HEAD(ota_ops_entries_head, ota_ops_entry_) s_ota_ops_entries_head =
+    LIST_HEAD_INITIALIZER(s_ota_ops_entries_head);
+
+static uint32_t s_ota_ops_last_handle = 0;
+static ota_select s_ota_select[2];
+
+const static char *TAG = "esp_ota_ops";
+
+esp_err_t esp_ota_begin(const esp_partition_t *partition, size_t image_size, esp_ota_handle_t *out_handle)
+{
+    esp_err_t ret = ESP_OK;
+
+    if ((partition == NULL) || (out_handle == NULL)) {
+        return ESP_ERR_INVALID_ARG;
+    }
+
+    ota_ops_entry_t *new_entry = (ota_ops_entry_t *) calloc(sizeof(ota_ops_entry_t), 1);
+
+    if (new_entry == 0) {
+        return ESP_ERR_NO_MEM;
+    }
+
+    // if input image size is 0 or OTA_SIZE_UNKNOWN, will erase all areas in this partition
+    if ((image_size == 0) || (image_size == OTA_SIZE_UNKNOWN)) {
+        ret = esp_partition_erase_range(partition, 0, partition->size);
+    } else {
+        ret = esp_partition_erase_range(partition, 0, (image_size / SPI_FLASH_SEC_SIZE + 1) * SPI_FLASH_SEC_SIZE);
+    }
+
+    if (ret != ESP_OK) {
+        free(new_entry);
+        new_entry = NULL;
+        return ret;
+    }
+
+    LIST_INSERT_HEAD(&s_ota_ops_entries_head, new_entry, entries);
+
+    if ((image_size == 0) || (image_size == OTA_SIZE_UNKNOWN)) {
+        new_entry->erased_size = partition->size;
+    } else {
+        new_entry->erased_size = image_size;
+    }
+
+    memcpy(&new_entry->part, partition, sizeof(esp_partition_t));
+    new_entry->handle = ++s_ota_ops_last_handle;
+    *out_handle = new_entry->handle;
+    return ESP_OK;
+}
+
+esp_err_t esp_ota_write(esp_ota_handle_t handle, const void *data, size_t size)
+{
+    esp_err_t ret;
+    ota_ops_entry_t *it;
+
+    if (data == NULL) {
+        ESP_LOGE(TAG, "write data is invalid");
+        return ESP_ERR_INVALID_ARG;
+    }
+
+    // find ota handle in linked list
+    for (it = LIST_FIRST(&s_ota_ops_entries_head); it != NULL; it = LIST_NEXT(it, entries)) {
+        if (it->handle == handle) {
+            // must erase the partition before writing to it
+            assert(it->erased_size > 0 && "must erase the partition before writing to it");
+            ret = esp_partition_write(&it->part, it->wrote_size, data, size);
+            if(ret == ESP_OK){
+                it->wrote_size += size;
+            }
+            return ret;
+        }
+    }
+
+    //if go to here ,means don't find the handle
+    ESP_LOGE(TAG,"not found the handle")
+    return ESP_ERR_INVALID_ARG;
+}
+
+esp_err_t esp_ota_end(esp_ota_handle_t handle)
+{
+    esp_err_t ret;
+    ota_ops_entry_t *it;
+    size_t image_size;
+    for (it = LIST_FIRST(&s_ota_ops_entries_head); it != NULL; it = LIST_NEXT(it, entries)) {
+        if (it->handle == handle) {
+            // an ota handle need to be ended after erased and wrote data in it
+            if ((it->erased_size == 0) || (it->wrote_size == 0)) {
+                return ESP_ERR_INVALID_ARG;
+            }
+
+#ifdef CONFIG_SECUREBOOTLOADER
+            if (esp_image_basic_verify(it->part.address, &image_size) != ESP_OK) {
+                return ESP_ERR_OTA_VALIDATE_FAILED;
+            }
+            ret = esp_secure_boot_verify_signature(it->part.address, image_size);
+            if (ret != ESP_OK) {
+                return ESP_ERR_OTA_VALIDATE_FAILED;
+            }
+#endif
+
+            LIST_REMOVE(it, entries);
+            break;
+        }
+    }
+
+    if (it == NULL) {
+        return ESP_ERR_NOT_FOUND;
+    }
+
+    free(it);
+    return ESP_OK;
+}
+
+static uint32_t ota_select_crc(const ota_select *s)
+{
+    return crc32_le(UINT32_MAX, (uint8_t *)&s->ota_seq, 4);
+}
+
+static bool ota_select_valid(const ota_select *s)
+{
+    return s->ota_seq != UINT32_MAX && s->crc == ota_select_crc(s);
+}
+
+static esp_err_t rewrite_ota_seq(uint32_t seq, uint8_t sec_id, const esp_partition_t *ota_data_partition)
+{
+    esp_err_t ret;
+
+    if (sec_id == 0 || sec_id == 1) {
+        s_ota_select[sec_id].ota_seq = seq;
+        s_ota_select[sec_id].crc = ota_select_crc(&s_ota_select[sec_id]);
+        ret = spi_flash_erase_range(ota_data_partition->address, SPI_FLASH_SEC_SIZE);
+        if (ret != ESP_OK) {
+            return ret;
+        } else {
+            return esp_partition_write(ota_data_partition, SPI_FLASH_SEC_SIZE * sec_id, &s_ota_select[sec_id].ota_seq, sizeof(ota_select));
+        }
+    } else {
+        return ESP_ERR_INVALID_ARG;
+    }
+}
+
+static uint8_t get_ota_partition_count(void)
+{
+    uint16_t ota_app_count = 0;
+    while (esp_partition_find_first(ESP_PARTITION_TYPE_APP, ESP_PARTITION_SUBTYPE_APP_OTA_MIN + ota_app_count, NULL) != NULL) {
+            assert(ota_app_count < 16 && "must erase the partition before writing to it");
+            ota_app_count++;
+    }
+    return ota_app_count;
+}
+
+static esp_err_t esp_rewrite_ota_data(esp_partition_subtype_t subtype)
+{
+    esp_err_t ret;
+    const esp_partition_t *find_partition = NULL;
+    uint16_t ota_app_count = 0;
+    uint32_t i = 0;
+    uint32_t seq;
+    static spi_flash_mmap_memory_t ota_data_map;
+    const void *result = NULL;
+
+    find_partition = esp_partition_find_first(ESP_PARTITION_TYPE_DATA, ESP_PARTITION_SUBTYPE_DATA_OTA, NULL);
+    if (find_partition != NULL) {
+        ota_app_count = get_ota_partition_count();
+        //esp32_idf use two sector for store information about which partition is running
+        //it defined the two sector as ota data partition,two structure ota_select is saved in the two sector
+        //named data in first sector as s_ota_select[0], second sector data as s_ota_select[1]
+        //e.g.
+        //if s_ota_select[0].ota_seq == s_ota_select[1].ota_seq == 0xFFFFFFFF,means ota info partition is in init status
+        //so it will boot factory application(if there is),if there's no factory application,it will boot ota[0] application 
+        //if s_ota_select[0].ota_seq != 0 and s_ota_select[1].ota_seq != 0,it will choose a max seq ,and get value of max_seq%max_ota_app_number
+        //and boot a subtype (mask 0x0F) value is (max_seq - 1)%max_ota_app_number,so if want switch to run ota[x],can use next formulas.
+        //for example, if s_ota_select[0].ota_seq = 4, s_ota_select[1].ota_seq = 5, and there are 8 ota application, 
+        //current running is (5-1)%8 = 4,running ota[4],so if we want to switch to run ota[7],
+        //we should add s_ota_select[0].ota_seq (is 4) to 4 ,(8-1)%8=7,then it will boot ota[7]
+        //if      A=(B - C)%D
+        //then    B=(A + C)%D + D*n ,n= (0,1,2...)
+        //so current ota app sub type id is x , dest bin subtype is y,total ota app count is n
+        //seq will add (x + n*1 + 1 - seq)%n
+        if (SUB_TYPE_ID(subtype) >= ota_app_count) {
+            return ESP_ERR_NOT_FOUND;
+        }
+
+        ret = esp_partition_mmap(find_partition, 0, find_partition->size, SPI_FLASH_MMAP_DATA, &result, &ota_data_map);
+        if (ret != ESP_OK) {
+            result = NULL;
+            return ret;
+        } else {
+            memcpy(&s_ota_select[0], result, sizeof(ota_select));
+            memcpy(&s_ota_select[1], result + SPI_FLASH_SEC_SIZE, sizeof(ota_select));
+            spi_flash_munmap(ota_data_map);
+        }
+
+        if (ota_select_valid(&s_ota_select[0]) && ota_select_valid(&s_ota_select[1])) {
+            seq = OTA_MAX(s_ota_select[0].ota_seq, s_ota_select[1].ota_seq);
+            while (seq > (SUB_TYPE_ID(subtype) + 1) % ota_app_count + i * ota_app_count) {
+                i++;
+            }
+
+            if (s_ota_select[0].ota_seq >= s_ota_select[1].ota_seq) {
+                return rewrite_ota_seq((SUB_TYPE_ID(subtype) + 1) % ota_app_count + i * ota_app_count, 0, find_partition);
+            } else {
+                return rewrite_ota_seq((SUB_TYPE_ID(subtype) + 1) % ota_app_count + i * ota_app_count, 1, find_partition);
+            }
+
+        } else if (ota_select_valid(&s_ota_select[0])) {
+            while (s_ota_select[0].ota_seq > (SUB_TYPE_ID(subtype) + 1) % ota_app_count + i * ota_app_count) {
+                i++;
+            }
+            return rewrite_ota_seq((SUB_TYPE_ID(subtype) + 1) % ota_app_count + i * ota_app_count, 1, find_partition);
+
+        } else if (ota_select_valid(&s_ota_select[1])) {
+            while (s_ota_select[1].ota_seq > (SUB_TYPE_ID(subtype) + 1) % ota_app_count + i * ota_app_count) {
+                i++;
+            }
+            return rewrite_ota_seq((SUB_TYPE_ID(subtype) + 1) % ota_app_count + i * ota_app_count, 0, find_partition);
+
+        } else if (s_ota_select[0].ota_seq == 0xFFFFFFFF && s_ota_select[1].ota_seq == 0xFFFFFFFF) {
+            return rewrite_ota_seq(SUB_TYPE_ID(subtype) + 1, 0, find_partition);
+
+        } else {
+            return ESP_ERR_OTA_SELECT_INFO_INVALID;
+        }
+
+    } else {
+        return ESP_ERR_NOT_FOUND;
+    }
+}
+
+esp_err_t esp_ota_set_boot_partition(const esp_partition_t *partition)
+{
+    const esp_partition_t *find_partition = NULL;
+    size_t image_size;
+    if (partition == NULL) {
+        return ESP_ERR_INVALID_ARG;
+    }
+
+#ifdef CONFIG_SECUREBOOTLOADER
+    if (esp_image_basic_verify(partition->address, &image_size) != ESP_OK) {
+        return ESP_ERR_OTA_VALIDATE_FAILED;
+    }
+    ret = esp_secure_boot_verify_signature(partition->address, image_size);
+    if (ret != ESP_OK) {
+        return ESP_ERR_OTA_VALIDATE_FAILED;
+    }
+#endif
+    // if set boot partition to factory bin ,just format ota info partition
+    if (partition->type == ESP_PARTITION_TYPE_APP) {
+        if (partition->subtype == ESP_PARTITION_SUBTYPE_APP_FACTORY) {
+            find_partition = esp_partition_find_first(ESP_PARTITION_TYPE_DATA, ESP_PARTITION_SUBTYPE_DATA_OTA, NULL);
+            if (find_partition != NULL) {
+                return esp_partition_erase_range(find_partition, find_partition->address, find_partition->size);
+            } else {
+                return ESP_ERR_NOT_FOUND;
+            }
+        } else {
+            // try to find this partition in flash,if not find it ,return error
+            find_partition = esp_partition_find_first(ESP_PARTITION_TYPE_DATA, ESP_PARTITION_SUBTYPE_DATA_OTA, NULL);
+            if (find_partition != NULL) {
+                return esp_rewrite_ota_data(partition->subtype);
+            } else {
+                return ESP_ERR_NOT_FOUND;
+            }
+        }
+    } else {
+        return ESP_ERR_INVALID_ARG;
+    }
+}
+
+const esp_partition_t *esp_ota_get_boot_partition(void)
+{
+    esp_err_t ret;
+    const esp_partition_t *find_partition = NULL;
+    static spi_flash_mmap_memory_t ota_data_map;
+    const void *result = NULL;
+    uint16_t ota_app_count = 0;
+    find_partition = esp_partition_find_first(ESP_PARTITION_TYPE_DATA, ESP_PARTITION_SUBTYPE_DATA_OTA, NULL);
+
+    if (find_partition == NULL) {
+        ESP_LOGE(TAG, "not found ota data");
+        return NULL;
+    }
+
+    ret = esp_partition_mmap(find_partition, 0, find_partition->size, SPI_FLASH_MMAP_DATA, &result, &ota_data_map);
+    if (ret != ESP_OK) {
+        spi_flash_munmap(ota_data_map);
+        ESP_LOGE(TAG, "mmap ota data filed");
+        return NULL;
+    } else {
+        memcpy(&s_ota_select[0], result, sizeof(ota_select));
+        memcpy(&s_ota_select[1], result + 0x1000, sizeof(ota_select));
+        spi_flash_munmap(ota_data_map);
+    }
+    ota_app_count = get_ota_partition_count();
+
+    ESP_LOGD(TAG, "found ota bin max = %d", ota_app_count);
+    if (s_ota_select[0].ota_seq == 0xFFFFFFFF && s_ota_select[1].ota_seq == 0xFFFFFFFF) {
+        ESP_LOGD(TAG, "finding factory bin......");
+
+        return esp_partition_find_first(ESP_PARTITION_TYPE_APP, ESP_PARTITION_SUBTYPE_APP_FACTORY, NULL);
+    } else if (ota_select_valid(&s_ota_select[0]) && ota_select_valid(&s_ota_select[1])) {
+        ESP_LOGD(TAG, "finding ota_%d bin......", \
+                 ESP_PARTITION_SUBTYPE_APP_OTA_MIN + ((OTA_MAX(s_ota_select[0].ota_seq, s_ota_select[1].ota_seq) - 1) % ota_app_count));
+
+        return esp_partition_find_first(ESP_PARTITION_TYPE_APP, \
+                                        ESP_PARTITION_SUBTYPE_APP_OTA_MIN + ((OTA_MAX(s_ota_select[0].ota_seq, s_ota_select[1].ota_seq) - 1) % ota_app_count), NULL);
+    } else if (ota_select_valid(&s_ota_select[0])) {
+        ESP_LOGD(TAG, "finding ota_%d bin......", \
+                 ESP_PARTITION_SUBTYPE_APP_OTA_MIN + (s_ota_select[0].ota_seq - 1) % ota_app_count);
+
+        return esp_partition_find_first(ESP_PARTITION_TYPE_APP, \
+                                        ESP_PARTITION_SUBTYPE_APP_OTA_MIN + (s_ota_select[0].ota_seq - 1) % ota_app_count, NULL);
+
+    } else if (ota_select_valid(&s_ota_select[1])) {
+        ESP_LOGD(TAG, "finding ota_%d bin......", \
+                 ESP_PARTITION_SUBTYPE_APP_OTA_MIN + (s_ota_select[1].ota_seq - 1) % ota_app_count);
+
+        return esp_partition_find_first(ESP_PARTITION_TYPE_APP, \
+                                        ESP_PARTITION_SUBTYPE_APP_OTA_MIN + (s_ota_select[1].ota_seq - 1) % ota_app_count, NULL);
+
+    } else {
+        ESP_LOGE(TAG, "not found current bin");
+        return NULL;
+    }
+}
\ No newline at end of file
diff --git a/components/app_update/include/esp_ota_ops.h b/components/app_update/include/esp_ota_ops.h
new file mode 100755 (executable)
index 0000000..614ac18
--- /dev/null
@@ -0,0 +1,106 @@
+// Copyright 2015-2016 Espressif Systems (Shanghai) PTE LTD
+//
+// Licensed under the Apache License, Version 2.0 (the "License");
+// you may not use this file except in compliance with the License.
+// You may obtain a copy of the License at
+
+//     http://www.apache.org/licenses/LICENSE-2.0
+//
+// Unless required by applicable law or agreed to in writing, software
+// distributed under the License is distributed on an "AS IS" BASIS,
+// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+// See the License for the specific language governing permissions and
+// limitations under the License.
+
+#ifndef _OTA_OPS_H
+#define _OTA_OPS_H
+
+#include <stdint.h>
+#include <stdbool.h>
+#include <stddef.h>
+#include "esp_err.h"
+#include "esp_partition.h"
+#include "esp_spi_flash.h"
+
+#ifdef __cplusplus
+extern "C"
+{
+#endif
+
+#define OTA_SIZE_UNKNOWN 0xffffffff
+
+#define ESP_ERR_OTA_BASE                         0x1500                     /*!< base error code for ota_ops api */
+#define ESP_ERR_OTA_PARTITION_CONFLICT           (ESP_ERR_OTA_BASE + 0x01)  /*!< want to write or erase current running partition */
+#define ESP_ERR_OTA_SELECT_INFO_INVALID          (ESP_ERR_OTA_BASE + 0x02)  /*!< ota data partition info is error */
+#define ESP_ERR_OTA_VALIDATE_FAILED              (ESP_ERR_OTA_BASE + 0x03)  /*!< validate ota image failed */
+
+/**
+ * @brief Opaque handle for application update obtained from app_ops.
+ */
+typedef uint32_t esp_ota_handle_t;
+
+/**
+ * @brief   format input partition in flash to 0xFF as input image size,
+ *          if unkown image size ,pass 0x0 or 0xFFFFFFFF, it will erase all the
+ *          partition ,Otherwise, erase the required range
+ *
+ * @param   partition Pointer to partition structure which need to be updated
+ *            Must be non-NULL.
+ * @param   image_size size of image need to be updated
+ * @param   out_handle handle which should be used for esp_ota_write or esp_ota_end call
+
+ * @return: ESP_OK if format ota image OK  
+ *          ESP_ERR_OTA_PARTITION_CONFLICT  operate current running bin  
+ *          ESP_ERR_OTA_SELECT_INFO_INVALID ota bin select info invalid
+ */
+esp_err_t esp_ota_begin(const esp_partition_t* partition, size_t image_size, esp_ota_handle_t* out_handle);
+
+/**
+ * @brief   Write data to input input partition
+ *
+ * @param   handle  Handle obtained from esp_ota_begin
+ * @param   data  Pointer to data write to flash
+ * @param   size  data size of recieved data
+ *
+ * @return: ESP_OK if write flash data OK 
+ *          ESP_ERR_OTA_PARTITION_CONFLICT  operate current running bin  
+ *          ESP_ERR_OTA_SELECT_INFO_INVALID ota bin select info invalid
+ */
+esp_err_t esp_ota_write(esp_ota_handle_t handle, const void* data, size_t size);
+/**
+ * @brief   Finish the update and validate written data
+ *
+ * @param   handle  Handle obtained from esp_ota_begin 
+ *
+ * @return: ESP_OK if validate ota image pass
+ *          ESP_ERR_OTA_VALIDATE_FAILED  validate the ota image is invalid
+ */
+esp_err_t esp_ota_end(esp_ota_handle_t handle);
+
+/**
+ * @brief   Set next boot partition, call system_restart() will switch to run it
+ *
+ * @note    if you want switch to run a bin file 
+ *          has never been checked before,please validate it's signature firstly
+ *
+ * @param   partition Pointer to partition structure which need to boot
+ *
+ * @return: ESP_OK if set next boot partition OK
+ *          ESP_ERR_OTA_SELECT_INFO_INVALID ota bin select info invalid
+ */
+esp_err_t esp_ota_set_boot_partition(const esp_partition_t* partition);
+
+/**
+ * @brief   Get partition info of current running image
+ * 
+ * @return  pointer to esp_partition_t structure, or NULL if no partition is found or 
+ *          operate flash failed,This pointer is valid for the lifetime of the application.
+ */
+const esp_partition_t* esp_ota_get_boot_partition(void);
+
+#ifdef __cplusplus
+}
+#endif
+
+#endif /* OTA_OPS_H */
index 72ed1336c643c35e28591eb21793d0ddf3a111ef..db3ac042f0bf5c35b5d018d2bbd6d23ac19dc309 100644 (file)
@@ -132,34 +132,90 @@ esp_err_t IRAM_ATTR spi_flash_write(size_t dest_addr, const void *src, size_t si
     // Destination alignment is also checked in ROM code, but we can give
     // better error code here
     // TODO: add handling of unaligned destinations
-    if (dest_addr % 4 != 0) {
-        return ESP_ERR_INVALID_ARG;
-    }
-    if (size % 4 != 0) {
-        return ESP_ERR_INVALID_SIZE;
-    }
-    if ((uint32_t) src < 0x3ff00000) {
-        // if source address is in DROM, we won't be able to read it
-        // from within SPIWrite
-        // TODO: consider buffering source data using heap and writing it anyway?
-        return ESP_ERR_INVALID_ARG;
-    }
+    uint8_t *temp_write_buf = NULL;
+    uint8_t pad_head = 0;
+    uint8_t pad_end = 0;
+    SpiFlashOpResult rc;
     // Out of bound writes are checked in ROM code, but we can give better
     // error code here
     if (dest_addr + size > g_rom_flashchip.chip_size) {
         return ESP_ERR_INVALID_SIZE;
     }
-    COUNTER_START();
-    spi_flash_disable_interrupts_caches_and_other_cpu();
-    SpiFlashOpResult rc;
-    rc = spi_flash_unlock();
-    if (rc == SPI_FLASH_RESULT_OK) {
-        rc = SPIWrite((uint32_t) dest_addr, (const uint32_t*) src, (int32_t) size);
-        COUNTER_ADD_BYTES(write, size);
+
+    while(size >= 1024) {
+        // max need pad byte num for 1024 is 4
+        temp_write_buf = (uint8_t*)malloc(1024 + 4);
+        if(temp_write_buf == NULL) {
+            return ESP_ERR_NO_MEM;
+        }
+
+        if(dest_addr%4 != 0) {
+            pad_head = dest_addr%4;
+            pad_end = 4 - pad_head;
+        }
+        memset(temp_write_buf,0xFF,pad_head);
+        memcpy(temp_write_buf + pad_head ,src,1024);
+        memset(temp_write_buf + pad_head + 1024, 0xFF,pad_end);
+        COUNTER_START();
+        spi_flash_disable_interrupts_caches_and_other_cpu();
+        rc = spi_flash_unlock();
+        if (rc == SPI_FLASH_RESULT_OK) {
+            rc = SPIWrite((uint32_t) (dest_addr - pad_head), (const uint32_t*) temp_write_buf, (int32_t) (1024 + pad_head + pad_end));
+            COUNTER_ADD_BYTES(write, 1024 + pad_head + pad_end);
+        }
+        COUNTER_STOP(write);
+        spi_flash_enable_interrupts_caches_and_other_cpu();  
+        if(rc != ESP_OK) {
+            free(temp_write_buf);
+            temp_write_buf = NULL;
+            return spi_flash_translate_rc(rc);
+        }
+
+        free(temp_write_buf);
+        temp_write_buf = NULL;
+        size -= 1024;
+        dest_addr += 1024;
+        src = (uint8_t*)src + 1024;
     }
-    spi_flash_enable_interrupts_caches_and_other_cpu();
-    COUNTER_STOP(write);
-    return spi_flash_translate_rc(rc);
+    if(size > 0) {
+        // max need pad byte num for rand size is 6
+        temp_write_buf = (uint8_t*)malloc(size + 6);
+        if(temp_write_buf == NULL) {
+            return ESP_ERR_NO_MEM;
+        }
+        if(dest_addr%4 != 0) {
+            pad_head = dest_addr%4;
+        }
+        if ((pad_head + size)%4 != 0){
+            pad_end = 4 - (pad_head + size) % 4;  
+        }
+        memset(temp_write_buf,0xFF,pad_head);
+        memcpy(temp_write_buf + pad_head, src, size);
+        memset(temp_write_buf + pad_head + size, 0xFF,pad_end);
+        COUNTER_START();
+        spi_flash_disable_interrupts_caches_and_other_cpu();
+        rc = spi_flash_unlock();
+        if (rc == SPI_FLASH_RESULT_OK) {
+            rc = SPIWrite((uint32_t) (dest_addr - pad_head), (const uint32_t*) temp_write_buf, (int32_t) (size + pad_head + pad_end));
+            COUNTER_ADD_BYTES(write, size + pad_head + pad_end);
+        }
+        COUNTER_STOP(write);
+        spi_flash_enable_interrupts_caches_and_other_cpu();
+        if(rc != ESP_OK) {
+            free(temp_write_buf);
+            temp_write_buf = NULL;
+            return spi_flash_translate_rc(rc);
+        }
+
+        free(temp_write_buf);
+        temp_write_buf = NULL;
+        size = 0;
+        dest_addr += size;
+        src = (uint8_t*)src + size;
+        return spi_flash_translate_rc(rc);
+    } 
+    return spi_flash_translate_rc(SPI_FLASH_RESULT_OK);
+
 }
 
 esp_err_t IRAM_ATTR spi_flash_read(size_t src_addr, void *dest, size_t size)
index 21013d96fa775477372c37f6183653d0c066c449..c54da24caa5c85479ecb796c92b421b784b8ebed 100644 (file)
@@ -208,6 +208,10 @@ esp_err_t esp_partition_write(const esp_partition_t* partition,
                              size_t dst_offset, const void* src, size_t size)
 {
     assert(partition != NULL);
+    //todo : need add ecrypt write support ,size must be 32-bytes align 
+    if(partition->encrypted == true) {
+        return ESP_FAIL;
+    }
     if (dst_offset > partition->size) {
         return ESP_ERR_INVALID_ARG;
     }
index eeb4fe740d5b3c7f2ae8aa6d4582977c89ab6f88..2a408981971d3ec21168e195f0dcb6c21211f77f 100644 (file)
@@ -23,7 +23,8 @@ INPUT = ../components/esp32/include/esp_wifi.h \
        ../components/vfs/include \
        ../components/spi_flash/include \
        ../components/esp32/include/esp_int_wdt.h \
-       ../components/esp32/include/esp_task_wdt.h
+       ../components/esp32/include/esp_task_wdt.h \
+       ../components/app_update/include/esp_ota_ops.h 
 
 ## Get warnings for functions that have no documentation for their parameters or return value 
 ##
diff --git a/docs/api/ota.rst b/docs/api/ota.rst
new file mode 100644 (file)
index 0000000..cb181d3
--- /dev/null
@@ -0,0 +1,46 @@
+OTA
+===
+
+Overview
+--------
+
+`Instructions`_
+
+Application Example
+-------------------
+
+`Instructions`_
+
+API Reference
+-------------
+
+`Instructions`_
+
+.. _Instructions: template.html
+
+Header Files
+^^^^^^^^^^^^
+
+  * `app_update/include/esp_ota_ops.h <https://github.com/espressif/esp-idf/blob/master/components/app_update/include/esp_ota_ops.h>`_
+
+Macros
+------
+
+.. doxygendefine:: ESP_ERR_OTA_BASE
+.. doxygendefine:: ESP_ERR_OTA_PARTITION_CONFLICT
+.. doxygendefine:: ESP_ERR_OTA_SELECT_INFO_INVALID
+.. doxygendefine:: ESP_ERR_OTA_VALIDATE_FAILED
+
+Type Definitions
+^^^^^^^^^^^^^^^^
+
+.. doxygentypedef:: esp_ota_handle_t
+
+Functions
+^^^^^^^^^
+
+.. doxygenfunction:: esp_ota_begin
+.. doxygenfunction:: esp_ota_write
+.. doxygenfunction:: esp_ota_end
+.. doxygenfunction:: esp_ota_set_boot_partition
+.. doxygenfunction:: esp_ota_get_boot_partition
index a643a17836a61da11d17dd4e28cb81f90e1bd163..da11f322379bc16e40652ae7d7b0aa77b22708fe 100644 (file)
@@ -97,6 +97,7 @@ Contents:
    Wi-Fi <api/esp_wifi>
    Bluetooth <api/bt>
    Watchdogs <api/wdts>
+   OTA <api/ota>
    GPIO <api/gpio>
    UART <api/uart>
    LED Control <api/ledc>