diff --git a/common/misc.c b/common/misc.c index c5d3704..0888f1f 100644 --- a/common/misc.c +++ b/common/misc.c @@ -187,6 +187,13 @@ } EXPORT_SYMBOL(barebox_get_hostname); +void barebox_set_hostname_no_overwrite(const char *__hostname) +{ + if (!barebox_get_hostname()) + barebox_set_hostname(__hostname); +} +EXPORT_SYMBOL(barebox_set_hostname_no_overwrite); + BAREBOX_MAGICVAR_NAMED(global_hostname, global.hostname, "shortname of the board. Also used as hostname for DHCP requests"); diff --git a/drivers/of/base.c b/drivers/of/base.c index eabbf3d..6a58217 100644 --- a/drivers/of/base.c +++ b/drivers/of/base.c @@ -2360,3 +2360,35 @@ return available; } EXPORT_SYMBOL(of_graph_port_is_available); + +/** + * of_get_machine_compatible - get first compatible string from the root node. + * + * Returns the string or NULL. + */ +const char *of_get_machine_compatible(void) +{ + struct property *prop; + const char *name, *p; + + if (!root_node) + return NULL; + + prop = of_find_property(root_node, "compatible", NULL); + name = of_prop_next_string(prop, NULL); + + p = strchr(name, ','); + return p ? p + 1 : name; +} +EXPORT_SYMBOL(of_get_machine_compatible); + +static int of_init_hostname(void) +{ + const char *name; + + name = of_get_machine_compatible(); + barebox_set_hostname_no_overwrite(name ?: "barebox"); + + return 0; +} +late_initcall(of_init_hostname); diff --git a/include/common.h b/include/common.h index 54c76d4..60e5005 100644 --- a/include/common.h +++ b/include/common.h @@ -137,6 +137,7 @@ void barebox_set_model(const char *); const char *barebox_get_hostname(void); void barebox_set_hostname(const char *); +void barebox_set_hostname_no_overwrite(const char *); #if defined(CONFIG_MIPS) #include diff --git a/include/of.h b/include/of.h index 9bdbbb5e..1b9719d 100644 --- a/include/of.h +++ b/include/of.h @@ -148,6 +148,7 @@ const struct device_node *other); extern void of_delete_node(struct device_node *node); +extern const char *of_get_machine_compatible(void); extern int of_machine_is_compatible(const char *compat); extern int of_device_is_compatible(const struct device_node *device, const char *compat);