Add helper function for calculating offset to first constituent.

Change-Id: I436d91eb99bbfa178faa30cbb70a895f693aab83
diff --git a/inc/hf/ffa_internal.h b/inc/hf/ffa_internal.h
index 201f04f..3393305 100644
--- a/inc/hf/ffa_internal.h
+++ b/inc/hf/ffa_internal.h
@@ -18,6 +18,8 @@
 
 #include <stdint.h>
 
+#include "hf/check.h"
+
 #include "vmapi/hf/ffa.h"
 
 #define FFA_VERSION_MAJOR 0x1
@@ -30,3 +32,21 @@
 {
 	return (struct ffa_value){.func = FFA_ERROR_32, .arg2 = error_code};
 }
+
+/**
+ * Return the offset to the first constituent within the
+ * `ffa_composite_memory_region` for the given receiver from an
+ * `ffa_memory_region`. The caller must check that the receiver_index is within
+ * bounds, and that it has a composite memory region offset.
+ */
+static inline uint32_t ffa_composite_constituent_offset(
+	struct ffa_memory_region *memory_region, uint32_t receiver_index)
+{
+	CHECK(receiver_index < memory_region->receiver_count);
+	CHECK(memory_region->receivers[receiver_index]
+		      .composite_memory_region_offset != 0);
+
+	return memory_region->receivers[receiver_index]
+		       .composite_memory_region_offset +
+	       sizeof(struct ffa_composite_memory_region);
+}
diff --git a/src/ffa_memory.c b/src/ffa_memory.c
index 6ca8a51..e6ab21e 100644
--- a/src/ffa_memory.c
+++ b/src/ffa_memory.c
@@ -1097,6 +1097,7 @@
 {
 	struct ffa_composite_memory_region *composite;
 	uint32_t receivers_length;
+	uint32_t constituents_offset;
 	uint32_t constituents_length;
 	enum ffa_data_access data_access;
 	enum ffa_instruction_access instruction_access;
@@ -1122,13 +1123,16 @@
 	 */
 	receivers_length = sizeof(struct ffa_memory_access) *
 			   memory_region->receiver_count;
+	constituents_offset =
+		ffa_composite_constituent_offset(memory_region, 0);
 	if (memory_region->receivers[0].composite_memory_region_offset <
 		    sizeof(struct ffa_memory_region) + receivers_length ||
-	    memory_region->receivers[0].composite_memory_region_offset +
-			    sizeof(struct ffa_composite_memory_region) >=
-		    memory_share_length) {
+	    constituents_offset >= memory_share_length) {
 		dlog_verbose(
-			"Invalid composite memory region descriptor offset.\n");
+			"Invalid composite memory region descriptor offset "
+			"%d.\n",
+			memory_region->receivers[0]
+				.composite_memory_region_offset);
 		return ffa_error(FFA_INVALID_PARAMETERS);
 	}
 
@@ -1139,11 +1143,8 @@
 	 */
 	constituents_length = sizeof(struct ffa_memory_region_constituent) *
 			      composite->constituent_count;
-	if (memory_share_length !=
-	    memory_region->receivers[0].composite_memory_region_offset +
-		    sizeof(struct ffa_composite_memory_region) +
-		    constituents_length) {
-		dlog_verbose("Invalid length %d or constituent offset %d.\n",
+	if (memory_share_length != constituents_offset + constituents_length) {
+		dlog_verbose("Invalid length %d or composite offset %d.\n",
 			     memory_share_length,
 			     memory_region->receivers[0]
 				     .composite_memory_region_offset);