Angel Pons has uploaded this change for review. ( https://review.coreboot.org/c/coreboot/+/48693 )
Change subject: sconfig: Allow base devicetrees to provide a header ......................................................................
sconfig: Allow base devicetrees to provide a header
To allow mainboards to define macros for use in devicetree, check if the base devicetree has a corresponding header, and include it from static.c if it does. Else, do nothing (behaviour remains unchanged).
This assumes that devicetrees end with `.cb` and that their path starts with `src/`. The header is assumed to not exist if this is not the case.
Change-Id: If9b9908155e64cdc140bb759e1dcc96acb8018b6 Signed-off-by: Angel Pons th3fanbus@gmail.com --- M util/sconfig/main.c 1 file changed, 52 insertions(+), 2 deletions(-)
git pull ssh://review.coreboot.org:29418/coreboot refs/changes/93/48693/1
diff --git a/util/sconfig/main.c b/util/sconfig/main.c index 815bfc7..6904a1c 100644 --- a/util/sconfig/main.c +++ b/util/sconfig/main.c @@ -1733,6 +1733,55 @@ override_devicetree(&base_root_bus, dev->bus); }
+/* Write an include directive if a header for devtree exists, else do nothing */ +static void emit_devtree_header(FILE *f, const char *devtree) +{ + const char prefix[] = "src/"; + const char old_ext[] = ".cb"; + const char new_ext[] = ".h"; + _Static_assert(sizeof(old_ext) >= sizeof(new_ext)); + + const char *header = NULL; + char *path = NULL; + + if (!devtree) + return; + + const size_t old_ext_len = strlen(old_ext); + const size_t devtree_len = strlen(devtree); + const size_t prefix_len = strlen(prefix); + + /* Ensure preconditions are met */ + if (devtree_len <= prefix_len + old_ext_len) + return; + + path = strdup(devtree); + if (!path) + return; + + /* Find and replace suffix */ + char *ext = &path[devtree_len - old_ext_len]; + if (strncmp(ext, old_ext, old_ext_len)) + goto out; + + strcpy(ext, new_ext); + assert(strlen(path) - strlen(new_ext) == devtree_len - old_ext_len); + + /* Check if file actually exists */ + struct stat st; + if ((stat(path, &st) == -1) && (errno == ENOENT)) + goto out; + + /* Remove the prefix */ + if (strncmp(path, prefix, prefix_len)) + goto out; + + fprintf(f, "#include <%s>\n", &path[prefix_len]); + +out: + free(path); +} + static void generate_outputh(FILE *f, const char *fw_conf_header, const char *device_header) { fprintf(f, "#ifndef __STATIC_DEVICE_TREE_H\n"); @@ -1744,12 +1793,13 @@ fprintf(f, "\n#endif /* __STATIC_DEVICE_TREE_H */\n"); }
-static void generate_outputc(FILE *f, const char *static_header) +static void generate_outputc(FILE *f, const char *static_header, const char *base_devtree) { fprintf(f, "#include <device/device.h>\n"); fprintf(f, "#include <device/pci.h>\n"); fprintf(f, "#include <fw_config.h>\n"); fprintf(f, "#include <%s>\n", static_header); + emit_devtree_header(f, base_devtree); emit_chip_headers(f, chip_header.next); fprintf(f, "\n#define STORAGE static __unused DEVTREE_CONST\n\n");
@@ -1895,7 +1945,7 @@ const char *static_header = basename(h);
generate_outputh(autohead, fw_conf_header, device_header); - generate_outputc(autogen, static_header); + generate_outputc(autogen, static_header, base_devtree); generate_outputd(autogen, autodev); generate_outputf(autofwconf);