[U-Boot] [PATCH 5/9] fdt_support: refactor fdt_fixup_stdout() function

Masahiro Yamada yamada.m at jp.panasonic.com
Fri Feb 14 14:30:25 CET 2014


 - Do not use a deep indentation. We have only 80-character
   on each line and 1 indentation consumes 8 spaces. Before the
   code moves far to the right, you should consider to
   fix your code. See Linux Documentation/CodingStyle.

 - Add CONFIG_OF_STDOUT_VIA_ALIAS and OF_STDOUT_PATH macros
   only to their definition. Do not add them to both
   callee and caller. This is a tip to avoid using #ifdef
   everywhere.

 - OF_STDOUT_PATH and CONFIG_OF_STDOUT_VIA_ALIAS are exclusive.
   If both are defined, the former takes precedence.
   Do not try to fix-up "linux,stdout-path" property twice.

Signed-off-by: Masahiro Yamada <yamada.m at jp.panasonic.com>
---

 common/fdt_support.c | 85 ++++++++++++++++++++++++++--------------------------
 1 file changed, 42 insertions(+), 43 deletions(-)

diff --git a/common/fdt_support.c b/common/fdt_support.c
index c714ffa..f641e68 100644
--- a/common/fdt_support.c
+++ b/common/fdt_support.c
@@ -123,9 +123,14 @@ static int fdt_find_or_add_subnode(void *fdt, int parentoffset,
 	return offset;
 }
 
-#ifdef CONFIG_OF_STDOUT_VIA_ALIAS
-
-#ifdef CONFIG_CONS_INDEX
+/* rename to CONFIG_OF_STDOUT_PATH ? */
+#if defined(OF_STDOUT_PATH)
+static int fdt_fixup_stdout(void *fdt, int chosenoff)
+{
+	return fdt_setprop(fdt, chosenoff, "linux,stdout-path",
+			      OF_STDOUT_PATH, strlen(OF_STDOUT_PATH) + 1);
+}
+#elif defined(CONFIG_OF_STDOUT_VIA_ALIAS) && defined(CONFIG_CONS_INDEX)
 static void fdt_fill_multisername(char *sername, size_t maxlen)
 {
 	const char *outname = stdio_devices[stdout]->name;
@@ -137,44 +142,48 @@ static void fdt_fill_multisername(char *sername, size_t maxlen)
 	if (strcmp(outname + 1, "serial") > 0)
 		strncpy(sername, outname + 1, maxlen);
 }
-#endif
 
 static int fdt_fixup_stdout(void *fdt, int chosenoff)
 {
-	int err = 0;
-#ifdef CONFIG_CONS_INDEX
-	int node;
+	int err;
+	int aliasoff;
 	char sername[9] = { 0 };
-	const char *path;
+	const void *path;
+	int len;
+	char tmp[256]; /* long enough */
 
 	fdt_fill_multisername(sername, sizeof(sername) - 1);
 	if (!sername[0])
 		sprintf(sername, "serial%d", CONFIG_CONS_INDEX - 1);
 
-	err = node = fdt_path_offset(fdt, "/aliases");
-	if (node >= 0) {
-		int len;
-		path = fdt_getprop(fdt, node, sername, &len);
-		if (path) {
-			char *p = malloc(len);
-			err = -FDT_ERR_NOSPACE;
-			if (p) {
-				memcpy(p, path, len);
-				err = fdt_setprop(fdt, chosenoff,
-					"linux,stdout-path", p, len);
-				free(p);
-			}
-		} else {
-			err = len;
-		}
+	aliasoff = fdt_path_offset(fdt, "/aliases");
+	if (aliasoff < 0) {
+		err = aliasoff;
+		goto error;
 	}
-#endif
+
+	path = fdt_getprop(fdt, aliasoff, sername, &len);
+	if (!path) {
+		err = len;
+		goto error;
+	}
+
+	/* fdt_setprop may break "path" so we copy it to tmp buffer */
+	memcpy(tmp, path, len);
+
+	err = fdt_setprop(fdt, chosenoff, "linux,stdout-path", tmp, len);
+error:
 	if (err < 0)
 		printf("WARNING: could not set linux,stdout-path %s.\n",
-				fdt_strerror(err));
+		       fdt_strerror(err));
 
 	return err;
 }
+#else
+static int fdt_fixup_stdout(void *fdt, int chosenoff)
+{
+	return 0;
+}
 #endif
 
 int fdt_initrd(void *fdt, ulong initrd_start, ulong initrd_end)
@@ -254,27 +263,17 @@ int fdt_chosen(void *fdt)
 		return nodeoffset;
 
 	str = getenv("bootargs");
-	if (str != NULL) {
-		err = fdt_setprop(fdt, nodeoffset,
-				  "bootargs", str, strlen(str)+1);
-		if (err < 0)
+	if (str) {
+		err = fdt_setprop(fdt, nodeoffset, "bootargs", str,
+				  strlen(str) + 1);
+		if (err < 0) {
 			printf("WARNING: could not set bootargs %s.\n",
 			       fdt_strerror(err));
+			return err;
+		}
 	}
 
-#ifdef CONFIG_OF_STDOUT_VIA_ALIAS
-	err = fdt_fixup_stdout(fdt, nodeoffset);
-#endif
-
-#ifdef OF_STDOUT_PATH
-	err = fdt_setprop(fdt, nodeoffset, "linux,stdout-path",
-			  OF_STDOUT_PATH, strlen(OF_STDOUT_PATH)+1);
-	if (err < 0)
-		printf("WARNING: could not set linux,stdout-path %s.\n",
-		       fdt_strerror(err));
-#endif
-
-	return err;
+	return fdt_fixup_stdout(fdt, nodeoffset);
 }
 
 void do_fixup_by_path(void *fdt, const char *path, const char *prop,
-- 
1.8.3.2



More information about the U-Boot mailing list