[PATCH 46/67] upl: Factor out part of encode_reg() to new function
Simon Glass
sjg at chromium.org
Wed Jan 1 23:09:32 CET 2025
This function performs the same operation on each list element. Move
this processing into a separate function so that it can be called
without an alist
Signed-off-by: Simon Glass <sjg at chromium.org>
---
boot/upl_write.c | 54 ++++++++++++++++++++++++++++++++++++------------
1 file changed, 41 insertions(+), 13 deletions(-)
diff --git a/boot/upl_write.c b/boot/upl_write.c
index eed84832a42..a1ecb809cbc 100644
--- a/boot/upl_write.c
+++ b/boot/upl_write.c
@@ -143,6 +143,42 @@ static int ofnode_write_value(ofnode node, const char *prop,
return 0;
}
+/**
+ * encode_addr_size() - Write an address/size pair
+ *
+ * Writes an address and size into a buffer suitable for placing in a devicetree
+ * 'reg' property. This uses upl->addr/size_cells to determine the number of
+ * cells for each value
+ *
+ * @upl: UPL state
+ * @buf: Buffer to write to
+ * @size: Buffer size in bytes
+ * @reg: Region to process
+ * Returns: Number of bytes written, or -ENOSPC if the buffer is too small
+ */
+static int encode_addr_size(const struct upl *upl, char *buf, uint size,
+ const struct memregion *reg)
+{
+ char *ptr = buf;
+
+ if (sizeof(fdt32_t) * (upl->addr_cells + upl->size_cells) > size)
+ return log_msg_ret("eas", -ENOSPC);
+
+ if (upl->addr_cells == 1)
+ *(u32 *)ptr = cpu_to_fdt32(reg->base);
+ else
+ *(u64 *)ptr = cpu_to_fdt64(reg->base);
+ ptr += upl->addr_cells * sizeof(u32);
+
+ if (upl->size_cells == 1)
+ *(u32 *)ptr = cpu_to_fdt32(reg->size);
+ else
+ *(u64 *)ptr = cpu_to_fdt64(reg->size);
+ ptr += upl->size_cells * sizeof(u32);
+
+ return ptr - buf;
+}
+
/**
* encode_reg() - Generate a set of addr/size pairs
*
@@ -166,20 +202,12 @@ static int encode_reg(const struct upl *upl, char *buf, int size,
for (i = 0; i < num_regions; i++) {
const struct memregion *reg = alist_get(region, i,
struct memregion);
+ int ret;
- if (upl->addr_cells == 1)
- *(u32 *)ptr = cpu_to_fdt32(reg->base);
- else
- *(u64 *)ptr = cpu_to_fdt64(reg->base);
- ptr += upl->addr_cells * sizeof(u32);
-
- if (upl->size_cells == 1)
- *(u32 *)ptr = cpu_to_fdt32(reg->size);
- else
- *(u64 *)ptr = cpu_to_fdt64(reg->size);
- ptr += upl->size_cells * sizeof(u32);
- if (ptr > end)
- return log_msg_ret("uer", -ENOSPC);
+ ret = encode_addr_size(upl, ptr, end - ptr, reg);
+ if (ret < 0)
+ return log_msg_ret("uer", ret);
+ ptr += ret;
}
return ptr - buf;
--
2.43.0
More information about the U-Boot
mailing list