summaryrefslogtreecommitdiff
path: root/src/commonlib
diff options
context:
space:
mode:
Diffstat (limited to 'src/commonlib')
-rw-r--r--src/commonlib/include/commonlib/region.h24
1 files changed, 20 insertions, 4 deletions
diff --git a/src/commonlib/include/commonlib/region.h b/src/commonlib/include/commonlib/region.h
index 25efcc8724..45a3b8d7b7 100644
--- a/src/commonlib/include/commonlib/region.h
+++ b/src/commonlib/include/commonlib/region.h
@@ -6,6 +6,7 @@
#include <sys/types.h>
#include <stddef.h>
#include <stdbool.h>
+#include <commonlib/bsd/cb_err.h>
#include <commonlib/bsd/helpers.h>
#include <commonlib/mem_pool.h>
@@ -94,10 +95,20 @@ struct region_device {
}, \
}
-/* Helper to dynamically initialize region device. */
-void region_device_init(struct region_device *rdev,
- const struct region_device_ops *ops, size_t offset,
- size_t size);
+static inline struct region region_create(size_t offset, size_t size)
+{
+ assert(offset + size - 1 >= offset);
+ return (struct region){ .offset = offset, .size = size };
+}
+
+static inline enum cb_err region_create_untrusted(
+ struct region *r, unsigned long long offset, unsigned long long size)
+{
+ if (offset > SIZE_MAX || size > SIZE_MAX || (size_t)(offset + size - 1) < offset)
+ return CB_ERR_ARG;
+ *r = (struct region){ .offset = offset, .size = size };
+ return CB_SUCCESS;
+}
/* Return 1 if child is subregion of parent, else 0. */
int region_is_subregion(const struct region *p, const struct region *c);
@@ -123,6 +134,11 @@ static inline bool region_overlap(const struct region *r1, const struct region *
(region_offset(r1) < region_end(r2));
}
+/* Helper to dynamically initialize region device. */
+void region_device_init(struct region_device *rdev,
+ const struct region_device_ops *ops, size_t offset,
+ size_t size);
+
static inline const struct region *region_device_region(
const struct region_device *rdev)
{