[coreboot-gerrit] Change in coreboot[master]: device_tree/fit: Constify data structures

Philipp Deppenwiese (Code Review) gerrit at coreboot.org
Fri Aug 24 13:38:34 CEST 2018


Philipp Deppenwiese has submitted this change and it was merged. ( https://review.coreboot.org/28267 )

Change subject: device_tree/fit: Constify data structures
......................................................................

device_tree/fit: Constify data structures

* Add const quailifier to arguments and elements.
* Add casts where necessary in cn81xx/soc.

Tested on Cavium CN81xx EVB SFF.

Change-Id: Id27966427fb97457fe883be32685d1397fb0781f
Signed-off-by: Patrick Rudolph <patrick.rudolph at 9elements.com>
Reviewed-on: https://review.coreboot.org/28267
Tested-by: build bot (Jenkins) <no-reply at coreboot.org>
Reviewed-by: Julius Werner <jwerner at chromium.org>
Reviewed-by: Paul Menzel <paulepanter at users.sourceforge.net>
---
M src/include/device_tree.h
M src/include/fit.h
M src/lib/device_tree.c
M src/lib/fit.c
M src/lib/fit_payload.c
M src/soc/cavium/cn81xx/soc.c
6 files changed, 75 insertions(+), 64 deletions(-)

Approvals:
  build bot (Jenkins): Verified
  Paul Menzel: Looks good to me, but someone else must approve
  Julius Werner: Looks good to me, approved



diff --git a/src/include/device_tree.h b/src/include/device_tree.h
index 4c1a37c..14be832 100644
--- a/src/include/device_tree.h
+++ b/src/include/device_tree.h
@@ -50,7 +50,7 @@
 struct fdt_property
 {
 	const char *name;
-	void *data;
+	const void *data;
 	uint32_t size;
 };
 
@@ -88,7 +88,7 @@
 
 struct device_tree
 {
-	void *header;
+	const void *header;
 	uint32_t header_size;
 
 	struct list_node reserve_map;
@@ -104,17 +104,18 @@
  */
 
 // Read the property, if any, at offset offset.
-int fdt_next_property(void *blob, uint32_t offset, struct fdt_property *prop);
+int fdt_next_property(const void *blob, uint32_t offset,
+		      struct fdt_property *prop);
 // Read the name of the node, if any, at offset offset.
-int fdt_node_name(void *blob, uint32_t offset, const char **name);
+int fdt_node_name(const void *blob, uint32_t offset, const char **name);
 
-void fdt_print_node(void *blob, uint32_t offset);
-int fdt_skip_node(void *blob, uint32_t offset);
+void fdt_print_node(const void *blob, uint32_t offset);
+int fdt_skip_node(const void *blob, uint32_t offset);
 
 // Read a flattened device tree into a heirarchical structure which refers to
 // the contents of the flattened tree in place. Modifying the flat tree
 // invalidates the unflattened one.
-struct device_tree *fdt_unflatten(void *blob);
+struct device_tree *fdt_unflatten(const void *blob);
 
 
 
@@ -123,12 +124,13 @@
  */
 
 // Figure out how big a device tree would be if it were flattened.
-uint32_t dt_flat_size(struct device_tree *tree);
+uint32_t dt_flat_size(const struct device_tree *tree);
 // Flatten a device tree into the buffer pointed to by dest.
-void dt_flatten(struct device_tree *tree, void *dest);
-void dt_print_node(struct device_tree_node *node);
+void dt_flatten(const struct device_tree *tree, void *dest);
+void dt_print_node(const struct device_tree_node *node);
 // Read #address-cells and #size-cells properties from a node.
-void dt_read_cell_props(struct device_tree_node *node, u32 *addrcp, u32 *sizecp);
+void dt_read_cell_props(const struct device_tree_node *node, u32 *addrcp,
+			u32 *sizecp);
 // Look up or create a node relative to a parent node, through its path
 // represented as an array of strings.
 struct device_tree_node *dt_find_node(struct device_tree_node *parent, const char **path,
@@ -148,15 +150,16 @@
 struct device_tree_node *dt_find_prop_value(struct device_tree_node *parent, const char *name,
 				   void *data, size_t size);
 // Return the phandle
-uint32_t dt_get_phandle(struct device_tree_node *node);
+uint32_t dt_get_phandle(const struct device_tree_node *node);
 // Write src into *dest as a 'length'-byte big-endian integer.
 void dt_write_int(u8 *dest, u64 src, size_t length);
 // Delete a property
 void dt_delete_prop(struct device_tree_node *node, const char *name);
 // Add different kinds of properties to a node, or update existing ones.
-void dt_add_bin_prop(struct device_tree_node *node, const char *name, void *data,
-		     size_t size);
-void dt_add_string_prop(struct device_tree_node *node, const char *name, char *str);
+void dt_add_bin_prop(struct device_tree_node *node, const char *name,
+		     const void *data, size_t size);
+void dt_add_string_prop(struct device_tree_node *node, const char *name,
+			const char *str);
 void dt_add_u32_prop(struct device_tree_node *node, const char *name, u32 val);
 void dt_add_u64_prop(struct device_tree_node *node, const char *name, u64 val);
 void dt_add_reg_prop(struct device_tree_node *node, u64 *addrs, u64 *sizes,
@@ -164,9 +167,10 @@
 int dt_set_bin_prop_by_path(struct device_tree *tree, const char *path,
 			    void *data, size_t size, int create);
 
-void dt_find_bin_prop(struct device_tree_node *node, const char *name, void **data,
-		      size_t *size);
-const char *dt_find_string_prop(struct device_tree_node *node, const char *name);
+void dt_find_bin_prop(const struct device_tree_node *node, const char *name,
+		      const void **data, size_t *size);
+const char *dt_find_string_prop(const struct device_tree_node *node,
+				const char *name);
 
 /*
  * Fixups to apply to a kernel's device tree before booting it.
diff --git a/src/include/fit.h b/src/include/fit.h
index eb51b50..6e0667f 100644
--- a/src/include/fit.h
+++ b/src/include/fit.h
@@ -27,7 +27,7 @@
 struct fit_image_node
 {
 	const char *name;
-	void *data;
+	const void *data;
 	uint32_t size;
 	int compression;
 
@@ -54,7 +54,7 @@
 /*
  * Updates the cmdline in the devicetree.
  */
-void fit_update_chosen(struct device_tree *tree, char *cmd_line);
+void fit_update_chosen(struct device_tree *tree, const char *cmd_line);
 
 /*
  * Add a compat string to the list of supported board ids.
diff --git a/src/lib/device_tree.c b/src/lib/device_tree.c
index 89387ff..79561a6 100644
--- a/src/lib/device_tree.c
+++ b/src/lib/device_tree.c
@@ -28,7 +28,8 @@
  * Functions for picking apart flattened trees.
  */
 
-int fdt_next_property(void *blob, uint32_t offset, struct fdt_property *prop)
+int fdt_next_property(const void *blob, uint32_t offset,
+		      struct fdt_property *prop)
 {
 	struct fdt_header *header = (struct fdt_header *)blob;
 	uint32_t *ptr = (uint32_t *)(((uint8_t *)blob) + offset);
@@ -52,7 +53,7 @@
 	return index * sizeof(uint32_t);
 }
 
-int fdt_node_name(void *blob, uint32_t offset, const char **name)
+int fdt_node_name(const void *blob, uint32_t offset, const char **name)
 {
 	uint8_t *ptr = ((uint8_t *)blob) + offset;
 	if (be32toh(*(uint32_t *)ptr) != FDT_TOKEN_BEGIN_NODE)
@@ -76,7 +77,7 @@
 		printk(BIOS_DEBUG, "  ");
 }
 
-static void print_property(struct fdt_property *prop, int depth)
+static void print_property(const struct fdt_property *prop, int depth)
 {
 	print_indent(depth);
 	printk(BIOS_DEBUG, "prop \"%s\" (%d bytes).\n", prop->name, prop->size);
@@ -89,7 +90,7 @@
 	printk(BIOS_DEBUG, "\n");
 }
 
-static int print_flat_node(void *blob, uint32_t start_offset, int depth)
+static int print_flat_node(const void *blob, uint32_t start_offset, int depth)
 {
 	int offset = start_offset;
 	const char *name;
@@ -116,7 +117,7 @@
 	return offset - start_offset + sizeof(uint32_t);
 }
 
-void fdt_print_node(void *blob, uint32_t offset)
+void fdt_print_node(const void *blob, uint32_t offset)
 {
 	print_flat_node(blob, offset, 0);
 }
@@ -127,7 +128,7 @@
  * A utility function to skip past nodes in flattened trees.
  */
 
-int fdt_skip_node(void *blob, uint32_t start_offset)
+int fdt_skip_node(const void *blob, uint32_t start_offset)
 {
 	int offset = start_offset;
 	int size;
@@ -171,7 +172,7 @@
 	return buf;
 }
 
-static int fdt_unflatten_node(void *blob, uint32_t start_offset,
+static int fdt_unflatten_node(const void *blob, uint32_t start_offset,
 			      struct device_tree_node **new_node)
 {
 	struct list_node *last;
@@ -216,12 +217,12 @@
 	return offset - start_offset + sizeof(uint32_t);
 }
 
-static int fdt_unflatten_map_entry(void *blob, uint32_t offset,
+static int fdt_unflatten_map_entry(const void *blob, uint32_t offset,
 				   struct device_tree_reserve_map_entry **new)
 {
-	uint64_t *ptr = (uint64_t *)(((uint8_t *)blob) + offset);
-	uint64_t start = be64toh(ptr[0]);
-	uint64_t size = be64toh(ptr[1]);
+	const uint64_t *ptr = (const uint64_t *)(((uint8_t *)blob) + offset);
+	const uint64_t start = be64toh(ptr[0]);
+	const uint64_t size = be64toh(ptr[1]);
 
 	if (!size)
 		return 0;
@@ -237,10 +238,10 @@
 	return sizeof(uint64_t) * 2;
 }
 
-struct device_tree *fdt_unflatten(void *blob)
+struct device_tree *fdt_unflatten(const void *blob)
 {
 	struct device_tree *tree = malloc(sizeof(*tree));
-	struct fdt_header *header = (struct fdt_header *)blob;
+	const struct fdt_header *header = (const struct fdt_header *)blob;
 	if (!tree)
 		return NULL;
 	memset(tree, 0, sizeof(*tree));
@@ -315,7 +316,7 @@
 	*struct_size += sizeof(uint32_t);
 }
 
-uint32_t dt_flat_size(struct device_tree *tree)
+uint32_t dt_flat_size(const struct device_tree *tree)
 {
 	uint32_t size = tree->header_size;
 	struct device_tree_reserve_map_entry *entry;
@@ -377,8 +378,9 @@
 	*strings_start = dstrings;
 }
 
-static void dt_flatten_node(struct device_tree_node *node, void **struct_start,
-			    void *strings_base, void **strings_start)
+static void dt_flatten_node(const struct device_tree_node *node,
+			    void **struct_start, void *strings_base,
+			    void **strings_start)
 {
 	uint8_t *dstruct = (uint8_t *)*struct_start;
 	uint8_t *dstrings = (uint8_t *)*strings_start;
@@ -406,7 +408,7 @@
 	*strings_start = dstrings;
 }
 
-void dt_flatten(struct device_tree *tree, void *start_dest)
+void dt_flatten(const struct device_tree *tree, void *start_dest)
 {
 	uint8_t *dest = (uint8_t *)start_dest;
 
@@ -449,7 +451,7 @@
  * Functions for printing a non-flattened device tree.
  */
 
-static void print_node(struct device_tree_node *node, int depth)
+static void print_node(const struct device_tree_node *node, int depth)
 {
 	print_indent(depth);
 	printk(BIOS_DEBUG, "name = %s\n", node->name);
@@ -463,7 +465,7 @@
 		print_node(child, depth + 1);
 }
 
-void dt_print_node(struct device_tree_node *node)
+void dt_print_node(const struct device_tree_node *node)
 {
 	print_node(node, 0);
 }
@@ -481,7 +483,8 @@
  * @param addrcp	Pointer to store #address-cells in, skipped if NULL.
  * @param sizecp	Pointer to store #size-cells in, skipped if NULL.
  */
-void dt_read_cell_props(struct device_tree_node *node, u32 *addrcp, u32 *sizecp)
+void dt_read_cell_props(const struct device_tree_node *node, u32 *addrcp,
+			u32 *sizecp)
 {
 	struct device_tree_property *prop;
 	list_for_each(prop, node->properties, list_node) {
@@ -706,7 +709,7 @@
 	list_for_each(prop, parent->properties, list_node) {
 		if (!strcmp(name, prop->prop.name)) {
 			size_t bytes = prop->prop.size;
-			void *prop_data = prop->prop.data;
+			const void *prop_data = prop->prop.data;
 			if (size != bytes)
 				break;
 			if (!memcmp(data, prop_data, size))
@@ -731,16 +734,16 @@
  * @param node Pointer to node containing the phandle
  * @return Zero on error, the phandle on success
  */
-uint32_t dt_get_phandle(struct device_tree_node *node)
+uint32_t dt_get_phandle(const struct device_tree_node *node)
 {
-	uint32_t *phandle;
+	const uint32_t *phandle;
 	size_t len;
 
-	dt_find_bin_prop(node, "phandle", (void **)&phandle, &len);
+	dt_find_bin_prop(node, "phandle", (const void **)&phandle, &len);
 	if (phandle != NULL && len == sizeof(*phandle))
 		return be32_to_cpu(*phandle);
 
-	dt_find_bin_prop(node, "linux,phandle", (void **)&phandle, &len);
+	dt_find_bin_prop(node, "linux,phandle", (const void **)&phandle, &len);
 	if (phandle != NULL && len == sizeof(*phandle))
 		return be32_to_cpu(*phandle);
 
@@ -789,7 +792,7 @@
  * @param size		The size of data in bytes.
  */
 void dt_add_bin_prop(struct device_tree_node *node, const char *name,
-		     void *data, size_t size)
+		     const void *data, size_t size)
 {
 	struct device_tree_property *prop;
 
@@ -817,9 +820,10 @@
  * @param name		The name of the property.
  * @return		The found string, or NULL.
  */
-const char *dt_find_string_prop(struct device_tree_node *node, const char *name)
+const char *dt_find_string_prop(const struct device_tree_node *node,
+				const char *name)
 {
-	void *content;
+	const void *content;
 	size_t size;
 
 	dt_find_bin_prop(node, name, &content, &size);
@@ -835,8 +839,8 @@
  * @param data		Pointer to return raw data blob in the property.
  * @param size		Pointer to return the size of data in bytes.
  */
-void dt_find_bin_prop(struct device_tree_node *node, const char *name,
-		      void **data, size_t *size)
+void dt_find_bin_prop(const struct device_tree_node *node, const char *name,
+		      const void **data, size_t *size)
 {
 	struct device_tree_property *prop;
 
@@ -860,7 +864,7 @@
  * @param str		The zero-terminated string to be stored in the property.
  */
 void dt_add_string_prop(struct device_tree_node *node, const char *name,
-			char *str)
+			const char *str)
 {
 	dt_add_bin_prop(node, name, str, strlen(str) + 1);
 }
diff --git a/src/lib/fit.c b/src/lib/fit.c
index 68f5bed..da55072 100644
--- a/src/lib/fit.c
+++ b/src/lib/fit.c
@@ -158,7 +158,7 @@
 	return NULL;
 }
 
-static int fdt_find_compat(void *blob, uint32_t start_offset,
+static int fdt_find_compat(const void *blob, uint32_t start_offset,
 			   struct fdt_property *prop)
 {
 	int offset = start_offset;
@@ -196,7 +196,7 @@
 	return -1;
 }
 
-void fit_update_chosen(struct device_tree *tree, char *cmd_line)
+void fit_update_chosen(struct device_tree *tree, const char *cmd_line)
 {
 	const char *path[] = { "chosen", NULL };
 	struct device_tree_node *node;
@@ -388,10 +388,12 @@
  * @param fdt_blob Pointer to FDT
  * @param config The current config node to operate on
  */
-static void fit_update_compat(void *fdt_blob, struct fit_config_node *config)
+static void fit_update_compat(const void *fdt_blob,
+			      struct fit_config_node *config)
 {
 	struct compat_string_entry *compat_node;
-	struct fdt_header *fdt_header = (struct fdt_header *)fdt_blob;
+	const struct fdt_header *fdt_header =
+		(const struct fdt_header *)fdt_blob;
 	uint32_t fdt_offset = be32_to_cpu(fdt_header->structure_offset);
 	size_t i = 0;
 
diff --git a/src/lib/fit_payload.c b/src/lib/fit_payload.c
index ec947c0..3e1819d 100644
--- a/src/lib/fit_payload.c
+++ b/src/lib/fit_payload.c
@@ -119,7 +119,7 @@
 	struct device_tree_node *coreboot_node = dt_find_node(firmware_node,
 		coreboot_path, &addr_cells, &size_cells, 1);
 
-	dt_add_string_prop(coreboot_node, "compatible", strdup("coreboot"));
+	dt_add_string_prop(coreboot_node, "compatible", "coreboot");
 
 	/* Fetch CB tables from cbmem */
 	void *cbtable = cbmem_find(CBMEM_ID_CBTABLE);
diff --git a/src/soc/cavium/cn81xx/soc.c b/src/soc/cavium/cn81xx/soc.c
index 8efcb13..dfb06e0 100644
--- a/src/soc/cavium/cn81xx/soc.c
+++ b/src/soc/cavium/cn81xx/soc.c
@@ -64,9 +64,9 @@
 static void dt_platform_fixup_phy(struct device_tree_node *node, char *path,
 				  int64_t phy_address, bdk_qlm_modes_t qlm_mode)
 {
-	char *data = NULL;
+	const char *data = NULL;
 	size_t size = 0;
-	dt_find_bin_prop(node, "qlm-mode", (void **)&data, &size);
+	dt_find_bin_prop(node, "qlm-mode", (const void **)&data, &size);
 
 	if (!data || strncmp(data, path, 6) != 0)
 		return; /* No key prefix match. */
@@ -127,10 +127,10 @@
 static void dt_platform_fixup_mac(struct device_tree_node *node)
 {
 	const char *name = "local-mac-address";
-	u64 *localmac = NULL;
+	const u64 *localmac = NULL;
 	size_t size = 0;
 
-	dt_find_bin_prop(node, name, (void **)&localmac, &size);
+	dt_find_bin_prop(node, name, (const void **)&localmac, &size);
 
 	if (!localmac)
 		return;
@@ -150,8 +150,8 @@
 		if (*localmac)
 			return;
 		if (used_mac < num_free_mac_addresses) {
-			*localmac = next_free_mac_address + used_mac;
-			dt_add_bin_prop(node, name, (void *)&localmac, 6);
+			const u64 genmac = next_free_mac_address + used_mac;
+			dt_add_bin_prop(node, name, &genmac, 6);
 			used_mac++;
 			return;
 		}
@@ -232,9 +232,10 @@
 			       __func__);
 			continue;
 		}
-		u32 *data = NULL;
+		const u32 *data = NULL;
 		size_t size = 0;
-		dt_find_bin_prop(dt_node, "mmu-masters", (void **)&data, &size);
+		dt_find_bin_prop(dt_node, "mmu-masters", (const void **)&data,
+				 &size);
 		if (!size) {
 			printk(BIOS_ERR, "%s: mmu-masters entry not found\n",
 			       __func__);

-- 
To view, visit https://review.coreboot.org/28267
To unsubscribe, or for help writing mail filters, visit https://review.coreboot.org/settings

Gerrit-Project: coreboot
Gerrit-Branch: master
Gerrit-MessageType: merged
Gerrit-Change-Id: Id27966427fb97457fe883be32685d1397fb0781f
Gerrit-Change-Number: 28267
Gerrit-PatchSet: 3
Gerrit-Owner: Patrick Rudolph <patrick.rudolph at 9elements.com>
Gerrit-Reviewer: Julius Werner <jwerner at chromium.org>
Gerrit-Reviewer: Patrick Rudolph <patrick.rudolph at 9elements.com>
Gerrit-Reviewer: Paul Menzel <paulepanter at users.sourceforge.net>
Gerrit-Reviewer: Philipp Deppenwiese <zaolin.daisuki at gmail.com>
Gerrit-Reviewer: build bot (Jenkins) <no-reply at coreboot.org>
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.coreboot.org/pipermail/coreboot-gerrit/attachments/20180824/bb89e507/attachment.html>


More information about the coreboot-gerrit mailing list