[Pdbg] [PATCH] libpdbg: Add api for attribute get/set

Amitay Isaacs amitay at ozlabs.org
Wed Mar 4 15:25:47 AEDT 2020


Signed-off-by: Amitay Isaacs <amitay at ozlabs.org>
---
 libpdbg/device.c  | 89 +++++++++++++++++++++++++++++++++++++++++++++++
 libpdbg/libpdbg.h | 34 ++++++++++++++++++
 2 files changed, 123 insertions(+)

diff --git a/libpdbg/device.c b/libpdbg/device.c
index cd8a459..69a64f9 100644
--- a/libpdbg/device.c
+++ b/libpdbg/device.c
@@ -755,3 +755,92 @@ struct pdbg_target *pdbg_target_root(void)
 {
 	return pdbg_dt_root;
 }
+
+bool pdbg_target_set_attribute(struct pdbg_target *target, const char *name, const void *val, uint32_t count, uint32_t size)
+{
+	void *buf;
+	size_t total_size = count * size;
+	uint32_t i;
+	bool ok;
+
+	buf = malloc(total_size);
+	if (!buf)
+		return false;
+
+	if (size == 1) {
+		memcpy(buf, val, total_size);
+
+	} else if (size == 2) {
+		uint16_t *b = (uint16_t *)buf;
+		uint16_t *v = (uint16_t *)val;
+
+		for (i = 0; i < count; i++)
+			b[i] = htobe16(v[i]);
+
+	} else if (size == 4) {
+		uint32_t *b = (uint32_t *)buf;
+		uint32_t *v = (uint32_t *)val;
+
+		for (i = 0; i < count; i++)
+			b[i] = htobe32(v[i]);
+
+	} else if (size == 8) {
+		uint64_t *b = (uint64_t *)buf;
+		uint64_t *v = (uint64_t *)val;
+
+		for (i = 0; i < count; i++)
+			b[i] = htobe64(v[i]);
+
+	} else {
+		return false;
+	}
+
+	ok = pdbg_target_set_property(target, name, buf, total_size);
+	free(buf);
+
+	return ok;
+}
+
+bool pdbg_target_get_attribute(struct pdbg_target *target, const char *name, void *val, uint32_t count, uint32_t size)
+{
+	const void *buf;
+	size_t total_size;
+	uint32_t i;
+
+	buf = pdbg_target_property(target, name, &total_size);
+	if (!buf)
+		return false;
+
+	if (total_size != count * size)
+		return false;
+
+	if (size == 1) {
+		memcpy(val, buf, total_size);
+
+	} else if (size == 2) {
+		uint16_t *b = (uint16_t *)buf;
+		uint16_t *v = (uint16_t *)val;
+
+		for (i = 0; i < count; i++)
+			v[i] = be16toh(b[i]);
+
+	} else if (size == 4) {
+		uint32_t *b = (uint32_t *)buf;
+		uint32_t *v = (uint32_t *)val;
+
+		for (i = 0; i < count; i++)
+			v[i] = be32toh(b[i]);
+
+	} else if (size == 8) {
+		uint64_t *b = (uint64_t *)buf;
+		uint64_t *v = (uint64_t *)val;
+
+		for (i = 0; i < count; i++)
+			v[i] = be64toh(b[i]);
+
+	} else {
+		return false;
+	}
+
+	return true;
+}
diff --git a/libpdbg/libpdbg.h b/libpdbg/libpdbg.h
index 1fc7ef4..9c234cb 100644
--- a/libpdbg/libpdbg.h
+++ b/libpdbg/libpdbg.h
@@ -312,6 +312,40 @@ bool pdbg_target_set_property(struct pdbg_target *target, const char *name, cons
  */
 const void *pdbg_target_property(struct pdbg_target *target, const char *name, size_t *size);
 
+/**
+ * @brief Overwrite the value of given attribute in device tree
+ *
+ * The attributes are treated as 1, 2, 4, or 8 byte integer arrays.
+ *
+ * @param[in] target pdbg_target to set the attribute on
+ * @param[in] name name of the attribute to set
+ * @param[in] val value of the attribute to set
+ * @param[in] count Number of elements in the buffer
+ * @param[in] size Size of element
+ * @return true on success, false on failure
+ *
+ * This function will update the attribute value provided the count of
+ * elements and the size of element matches.
+ */
+bool pdbg_target_set_attribute(struct pdbg_target *target, const char *name, const void *val, uint32_t count, uint32_t size);
+
+/**
+ * @brief Get the value of the given attribute from device tree
+ *
+ * The attributes are treated as 1, 2, 4, or 8 byte integer arrays.
+ *
+ * @param[in] target pdbg_target to get the attribute from
+ * @param[in] name name of the attribute to get
+ * @param[out] val value of the attribute
+ * @param[in] count Number of elements in the buffer
+ * @param[in] size Size of element
+ * @return true on success, false on failure
+ *
+ * This function will copy the attribute value in the given buffer, provided
+ * the count of elements and the size of element matches.
+ */
+bool pdbg_target_get_attribute(struct pdbg_target *target, const char *name, void *val, uint32_t count, uint32_t size);
+
 /**
  * @brief Get the given property value as a uint32_t
  * @param[in] target pdbg_target to get the property from
-- 
2.24.1



More information about the Pdbg mailing list