This is an automated email from Gerrit.

"Tomas Vanek <[email protected]>" just uploaded a new patch set to Gerrit, which 
you can find at https://review.openocd.org/c/openocd/+/9178

-- gerrit

commit fbdb1c6379ca23dd48b3dcb9f97c359c44f5e026
Author: Tomas Vanek <[email protected]>
Date:   Wed Oct 22 21:35:39 2025 +0200

    target: introduce target_memory_ready() test
    
    Use the new test in target memory access functions
    instead of target_was_examined()
    
    Drop the test from target_read/write_u8/16/32/64() helpers
    as they directly call a memory access function which does
    the test again.
    
    Change-Id: Ic1753e461d2a4b91ce3a3e1bf3e86eb2be743d46
    Signed-off-by: Tomas Vanek <[email protected]>

diff --git a/src/target/target.c b/src/target/target.c
index bdf0ff244d..75dd01e2cc 100644
--- a/src/target/target.c
+++ b/src/target/target.c
@@ -1244,15 +1244,23 @@ int target_run_read_async_algorithm(struct target 
*target,
        return retval;
 }
 
+bool target_memory_ready(struct target *target)
+{
+       if (target->type->memory_ready)
+               return target->type->memory_ready(target);
+
+       return target_was_examined(target);
+}
+
 int target_read_memory(struct target *target,
                target_addr_t address, uint32_t size, uint32_t count, uint8_t 
*buffer)
 {
-       if (!target_was_examined(target)) {
-               LOG_ERROR("Target not examined yet");
+       if (!target_memory_ready(target)) {
+               LOG_TARGET_ERROR(target, "Memory not ready");
                return ERROR_FAIL;
        }
        if (!target->type->read_memory) {
-               LOG_ERROR("Target %s doesn't support read_memory", 
target_name(target));
+               LOG_TARGET_ERROR(target, "doesn't support read_memory");
                return ERROR_FAIL;
        }
        return target->type->read_memory(target, address, size, count, buffer);
@@ -1261,12 +1269,12 @@ int target_read_memory(struct target *target,
 int target_read_phys_memory(struct target *target,
                target_addr_t address, uint32_t size, uint32_t count, uint8_t 
*buffer)
 {
-       if (!target_was_examined(target)) {
-               LOG_ERROR("Target not examined yet");
+       if (!target_memory_ready(target)) {
+               LOG_TARGET_ERROR(target, "Memory not ready");
                return ERROR_FAIL;
        }
        if (!target->type->read_phys_memory) {
-               LOG_ERROR("Target %s doesn't support read_phys_memory", 
target_name(target));
+               LOG_TARGET_ERROR(target, "doesn't support read_phys_memory");
                return ERROR_FAIL;
        }
        return target->type->read_phys_memory(target, address, size, count, 
buffer);
@@ -1275,12 +1283,12 @@ int target_read_phys_memory(struct target *target,
 int target_write_memory(struct target *target,
                target_addr_t address, uint32_t size, uint32_t count, const 
uint8_t *buffer)
 {
-       if (!target_was_examined(target)) {
-               LOG_ERROR("Target not examined yet");
+       if (!target_memory_ready(target)) {
+               LOG_TARGET_ERROR(target, "Memory not ready");
                return ERROR_FAIL;
        }
        if (!target->type->write_memory) {
-               LOG_ERROR("Target %s doesn't support write_memory", 
target_name(target));
+               LOG_TARGET_ERROR(target, "doesn't support write_memory");
                return ERROR_FAIL;
        }
        return target->type->write_memory(target, address, size, count, buffer);
@@ -1289,12 +1297,12 @@ int target_write_memory(struct target *target,
 int target_write_phys_memory(struct target *target,
                target_addr_t address, uint32_t size, uint32_t count, const 
uint8_t *buffer)
 {
-       if (!target_was_examined(target)) {
-               LOG_ERROR("Target not examined yet");
+       if (!target_memory_ready(target)) {
+               LOG_TARGET_ERROR(target, "Memory not ready");
                return ERROR_FAIL;
        }
        if (!target->type->write_phys_memory) {
-               LOG_ERROR("Target %s doesn't support write_phys_memory", 
target_name(target));
+               LOG_TARGET_ERROR(target, "doesn't support write_phys_memory");
                return ERROR_FAIL;
        }
        return target->type->write_phys_memory(target, address, size, count, 
buffer);
@@ -2353,8 +2361,8 @@ int target_write_buffer(struct target *target, 
target_addr_t address, uint32_t s
        LOG_DEBUG("writing buffer of %" PRIu32 " byte at " TARGET_ADDR_FMT,
                          size, address);
 
-       if (!target_was_examined(target)) {
-               LOG_ERROR("Target not examined yet");
+       if (!target_memory_ready(target)) {
+               LOG_TARGET_ERROR(target, "Memory not ready");
                return ERROR_FAIL;
        }
 
@@ -2418,8 +2426,8 @@ int target_read_buffer(struct target *target, 
target_addr_t address, uint32_t si
        LOG_DEBUG("reading buffer of %" PRIu32 " byte at " TARGET_ADDR_FMT,
                          size, address);
 
-       if (!target_was_examined(target)) {
-               LOG_ERROR("Target not examined yet");
+       if (!target_memory_ready(target)) {
+               LOG_TARGET_ERROR(target, "Memory not ready");
                return ERROR_FAIL;
        }
 
@@ -2535,10 +2543,6 @@ int target_blank_check_memory(struct target *target,
 int target_read_u64(struct target *target, target_addr_t address, uint64_t 
*value)
 {
        uint8_t value_buf[8];
-       if (!target_was_examined(target)) {
-               LOG_ERROR("Target not examined yet");
-               return ERROR_FAIL;
-       }
 
        int retval = target_read_memory(target, address, 8, 1, value_buf);
 
@@ -2559,10 +2563,6 @@ int target_read_u64(struct target *target, target_addr_t 
address, uint64_t *valu
 int target_read_u32(struct target *target, target_addr_t address, uint32_t 
*value)
 {
        uint8_t value_buf[4];
-       if (!target_was_examined(target)) {
-               LOG_ERROR("Target not examined yet");
-               return ERROR_FAIL;
-       }
 
        int retval = target_read_memory(target, address, 4, 1, value_buf);
 
@@ -2583,10 +2583,6 @@ int target_read_u32(struct target *target, target_addr_t 
address, uint32_t *valu
 int target_read_u16(struct target *target, target_addr_t address, uint16_t 
*value)
 {
        uint8_t value_buf[2];
-       if (!target_was_examined(target)) {
-               LOG_ERROR("Target not examined yet");
-               return ERROR_FAIL;
-       }
 
        int retval = target_read_memory(target, address, 2, 1, value_buf);
 
@@ -2606,11 +2602,6 @@ int target_read_u16(struct target *target, target_addr_t 
address, uint16_t *valu
 
 int target_read_u8(struct target *target, target_addr_t address, uint8_t 
*value)
 {
-       if (!target_was_examined(target)) {
-               LOG_ERROR("Target not examined yet");
-               return ERROR_FAIL;
-       }
-
        int retval = target_read_memory(target, address, 1, 1, value);
 
        if (retval == ERROR_OK) {
@@ -2630,10 +2621,6 @@ int target_write_u64(struct target *target, 
target_addr_t address, uint64_t valu
 {
        int retval;
        uint8_t value_buf[8];
-       if (!target_was_examined(target)) {
-               LOG_ERROR("Target not examined yet");
-               return ERROR_FAIL;
-       }
 
        LOG_DEBUG("address: " TARGET_ADDR_FMT ", value: 0x%16.16" PRIx64,
                          address,
@@ -2651,10 +2638,6 @@ int target_write_u32(struct target *target, 
target_addr_t address, uint32_t valu
 {
        int retval;
        uint8_t value_buf[4];
-       if (!target_was_examined(target)) {
-               LOG_ERROR("Target not examined yet");
-               return ERROR_FAIL;
-       }
 
        LOG_DEBUG("address: " TARGET_ADDR_FMT ", value: 0x%8.8" PRIx32,
                          address,
@@ -2672,10 +2655,6 @@ int target_write_u16(struct target *target, 
target_addr_t address, uint16_t valu
 {
        int retval;
        uint8_t value_buf[2];
-       if (!target_was_examined(target)) {
-               LOG_ERROR("Target not examined yet");
-               return ERROR_FAIL;
-       }
 
        LOG_DEBUG("address: " TARGET_ADDR_FMT ", value: 0x%8.8" PRIx16,
                          address,
@@ -2692,10 +2671,6 @@ int target_write_u16(struct target *target, 
target_addr_t address, uint16_t valu
 int target_write_u8(struct target *target, target_addr_t address, uint8_t 
value)
 {
        int retval;
-       if (!target_was_examined(target)) {
-               LOG_ERROR("Target not examined yet");
-               return ERROR_FAIL;
-       }
 
        LOG_DEBUG("address: " TARGET_ADDR_FMT ", value: 0x%2.2" PRIx8,
                          address, value);
@@ -2711,10 +2686,6 @@ int target_write_phys_u64(struct target *target, 
target_addr_t address, uint64_t
 {
        int retval;
        uint8_t value_buf[8];
-       if (!target_was_examined(target)) {
-               LOG_ERROR("Target not examined yet");
-               return ERROR_FAIL;
-       }
 
        LOG_DEBUG("address: " TARGET_ADDR_FMT ", value: 0x%16.16" PRIx64,
                          address,
@@ -2732,10 +2703,6 @@ int target_write_phys_u32(struct target *target, 
target_addr_t address, uint32_t
 {
        int retval;
        uint8_t value_buf[4];
-       if (!target_was_examined(target)) {
-               LOG_ERROR("Target not examined yet");
-               return ERROR_FAIL;
-       }
 
        LOG_DEBUG("address: " TARGET_ADDR_FMT ", value: 0x%8.8" PRIx32,
                          address,
@@ -2753,10 +2720,6 @@ int target_write_phys_u16(struct target *target, 
target_addr_t address, uint16_t
 {
        int retval;
        uint8_t value_buf[2];
-       if (!target_was_examined(target)) {
-               LOG_ERROR("Target not examined yet");
-               return ERROR_FAIL;
-       }
 
        LOG_DEBUG("address: " TARGET_ADDR_FMT ", value: 0x%8.8" PRIx16,
                          address,
@@ -2773,10 +2736,6 @@ int target_write_phys_u16(struct target *target, 
target_addr_t address, uint16_t
 int target_write_phys_u8(struct target *target, target_addr_t address, uint8_t 
value)
 {
        int retval;
-       if (!target_was_examined(target)) {
-               LOG_ERROR("Target not examined yet");
-               return ERROR_FAIL;
-       }
 
        LOG_DEBUG("address: " TARGET_ADDR_FMT ", value: 0x%2.2" PRIx8,
                          address, value);
diff --git a/src/target/target.h b/src/target/target.h
index b850b49cf3..ab3b1e59d0 100644
--- a/src/target/target.h
+++ b/src/target/target.h
@@ -590,6 +590,15 @@ int target_run_read_async_algorithm(struct target *target,
                uint32_t entry_point, uint32_t exit_point,
                void *arch_info);
 
+/**
+ * Returns true if target memory is ready to read/write.
+ *
+ * This routine is a wrapper for target->type->memory_ready.
+ * If the target specific check is not implemented,
+ * returns target_was_examined()
+ */
+bool target_memory_ready(struct target *target);
+
 /**
  * Read @a count items of @a size bytes from the memory of @a target at
  * the @a address given.
diff --git a/src/target/target_type.h b/src/target/target_type.h
index ccbe03a476..219417409c 100644
--- a/src/target/target_type.h
+++ b/src/target/target_type.h
@@ -111,6 +111,12 @@ struct target_type {
        * count: number of items of <size>
        */
 
+       /**
+        * Returns true if target memory is read to read/write.
+        * Do @b not call this function
+        * directly, use target_memory_ready() instead.
+        */
+       bool (*memory_ready)(struct target *target);
        /**
         * Target memory read callback.  Do @b not call this function
         * directly, use target_read_memory() instead.

-- 

Reply via email to