ccollins476ad closed pull request #1301: kernel/os: Add debug mode syscfg
setting
URL: https://github.com/apache/mynewt-core/pull/1301
This is a PR merged from a forked repository.
As GitHub hides the original diff on merge, it is displayed below for
the sake of provenance:
As this is a foreign pull request (from a fork), the diff is supplied
below (as it won't show otherwise due to GitHub magic):
diff --git a/hw/hal/src/hal_flash.c b/hw/hal/src/hal_flash.c
index 8822910599..35ff78d6e0 100644
--- a/hw/hal/src/hal_flash.c
+++ b/hw/hal/src/hal_flash.c
@@ -16,8 +16,10 @@
* specific language governing permissions and limitations
* under the License.
*/
+
#include <inttypes.h>
#include <assert.h>
+#include <string.h>
#include "hal/hal_bsp.h"
#include "hal/hal_flash.h"
@@ -91,11 +93,98 @@ hal_flash_read(uint8_t id, uint32_t address, void *dst,
uint32_t num_bytes)
return hf->hf_itf->hff_read(hf, address, dst, num_bytes);
}
+#if MYNEWT_VAL(HAL_FLASH_VERIFY_ERASES)
+/**
+ * Verifies that the specified range of flash is erased.
+ *
+ * @return 0 on success;
+ * nonzero on error or unexpected contents.
+ */
+static int
+hal_flash_cmp_erased(const struct hal_flash *hf, uint32_t address,
+ uint32_t num_bytes)
+{
+ uint8_t buf[MYNEWT_VAL(HAL_FLASH_VERIFY_BUF_SZ)];
+
+ uint32_t off;
+ uint32_t rem;
+ int chunk_sz;
+ int rc;
+ int i;
+
+ for (off = 0; off < num_bytes; off += sizeof buf) {
+ rem = num_bytes - off;
+ if (rem >= sizeof buf) {
+ chunk_sz = sizeof buf;
+ } else {
+ chunk_sz = rem;
+ }
+
+ rc = hf->hf_itf->hff_read(hf, address + off, buf, chunk_sz);
+ if (rc != 0) {
+ return rc;
+ }
+
+ for (i = 0; i < chunk_sz; i++) {
+ if (buf[i] != 0xff) {
+ return -1;
+ }
+ }
+ }
+
+ return 0;
+}
+#endif
+
+#if MYNEWT_VAL(HAL_FLASH_VERIFY_WRITES)
+/**
+ * Verifies that the specified range of flash contains the given contents.
+ *
+ * @return 0 on success;
+ * nonzero on error or unexpected contents.
+ */
+static int
+hal_flash_cmp(const struct hal_flash *hf, uint32_t address, const void *val,
+ uint32_t num_bytes)
+{
+ uint8_t buf[MYNEWT_VAL(HAL_FLASH_VERIFY_BUF_SZ)];
+
+ const uint8_t *u8p;
+ uint32_t off;
+ uint32_t rem;
+ int chunk_sz;
+ int rc;
+
+ u8p = val;
+
+ for (off = 0; off < num_bytes; off += sizeof buf) {
+ rem = num_bytes - off;
+ if (rem >= sizeof buf) {
+ chunk_sz = sizeof buf;
+ } else {
+ chunk_sz = rem;
+ }
+
+ rc = hf->hf_itf->hff_read(hf, address + off, buf, chunk_sz);
+ if (rc != 0) {
+ return rc;
+ }
+
+ if (memcmp(buf, u8p + off, chunk_sz) != 0) {
+ return -1;
+ }
+ }
+
+ return 0;
+}
+#endif
+
int
hal_flash_write(uint8_t id, uint32_t address, const void *src,
uint32_t num_bytes)
{
const struct hal_flash *hf;
+ int rc;
hf = hal_bsp_flash_dev(id);
if (!hf) {
@@ -105,13 +194,31 @@ hal_flash_write(uint8_t id, uint32_t address, const void
*src,
hal_flash_check_addr(hf, address + num_bytes)) {
return -1;
}
- return hf->hf_itf->hff_write(hf, address, src, num_bytes);
+
+ rc = hf->hf_itf->hff_write(hf, address, src, num_bytes);
+ if (rc != 0) {
+ return rc;
+ }
+
+#if MYNEWT_VAL(HAL_FLASH_VERIFY_WRITES)
+ assert(hal_flash_cmp(hf, address, src, num_bytes) == 0);
+#endif
+
+ return 0;
}
int
hal_flash_erase_sector(uint8_t id, uint32_t sector_address)
{
const struct hal_flash *hf;
+ uint32_t start;
+ uint32_t size;
+ int rc;
+ int i;
+
+ (void) start;
+ (void) size;
+ (void) i;
hf = hal_bsp_flash_dev(id);
if (!hf) {
@@ -120,7 +227,26 @@ hal_flash_erase_sector(uint8_t id, uint32_t sector_address)
if (hal_flash_check_addr(hf, sector_address)) {
return -1;
}
- return hf->hf_itf->hff_erase_sector(hf, sector_address);
+
+ rc = hf->hf_itf->hff_erase_sector(hf, sector_address);
+ if (rc != 0) {
+ return rc;
+ }
+
+#if MYNEWT_VAL(HAL_FLASH_VERIFY_ERASES)
+ /* Find the sector bounds so we can verify the erase. */
+ for (i = 0; i < hf->hf_sector_cnt; i++) {
+ rc = hf->hf_itf->hff_sector_info(hf, i, &start, &size);
+ assert(rc == 0);
+
+ if (sector_address == start) {
+ assert(hal_flash_cmp_erased(hf, start, size) == 0);
+ break;
+ }
+ }
+#endif
+
+ return 0;
}
int
@@ -162,6 +288,10 @@ hal_flash_erase(uint8_t id, uint32_t address, uint32_t
num_bytes)
if (hf->hf_itf->hff_erase_sector(hf, start)) {
return -1;
}
+
+#if MYNEWT_VAL(HAL_FLASH_VERIFY_ERASES)
+ assert(hal_flash_cmp_erased(hf, start, size) == 0);
+#endif
}
}
return 0;
diff --git a/hw/hal/syscfg.yml b/hw/hal/syscfg.yml
new file mode 100644
index 0000000000..f49dff1054
--- /dev/null
+++ b/hw/hal/syscfg.yml
@@ -0,0 +1,39 @@
+# Licensed to the Apache Software Foundation (ASF) under one
+# or more contributor license agreements. See the NOTICE file
+# distributed with this work for additional information
+# regarding copyright ownership. The ASF licenses this file
+# to you 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.
+#
+
+syscfg.defs:
+ HAL_FLASH_VERIFY_WRITES:
+ description: >
+ If enabled, flash contents are read back and verified after each
+ write.
+ value: 0
+ HAL_FLASH_VERIFY_ERASES:
+ description: >
+ If enabled, flash contents are read back and verified after each
+ erase.
+ value: 0
+ HAL_FLASH_VERIFY_BUF_SZ:
+ description: >
+ The buffer size to use when verifying writes and erases. One
+ buffer of this size is allocated on the stack during verify
+ operations.
+ value: 16
+
+syscfg.vals.OS_DEBUG_MODE:
+ HAL_FLASH_VERIFY_WRITES: 1
+ HAL_FLASH_VERIFY_ERASES: 1
diff --git a/kernel/os/syscfg.yml b/kernel/os/syscfg.yml
index d64245b1b9..bee5e78c90 100644
--- a/kernel/os/syscfg.yml
+++ b/kernel/os/syscfg.yml
@@ -107,3 +107,15 @@ syscfg.defs:
description: >
Enable tracing os_sem APIs by SystemView
value: 1
+
+ OS_DEBUG_MODE:
+ description: >
+ Enables various runtime error checks. A failed assert is triggered
+ on error detection. Enabling this setting increases stack usage.
+ value: 0
+
+syscfg.vals.OS_DEBUG_MODE:
+ OS_CRASH_STACKTRACE: 1
+ OS_CTX_SW_STACK_CHECK: 1
+ OS_MEMPOOL_CHECK: 1
+ OS_MEMPOOL_POISON: 1
----------------------------------------------------------------
This is an automated message from the Apache Git Service.
To respond to the message, please log on GitHub and use the
URL above to go to the specific comment.
For queries about this service, please contact Infrastructure at:
[email protected]
With regards,
Apache Git Services