diff mbox series

[v1,29/30] discover: Add platform-arm64

Message ID dbf6bf73fc3c521cdef5b7e63cb0bf23e580c688.1532469861.git.geoff@infradead.org
State Superseded
Headers show
Series [v1,01/30] docker: Add libfdt-dev | expand

Commit Message

Geoff Levand July 24, 2018, 10:15 p.m. UTC
From: Ge Song <ge.song@hxt-semitech.com>

Signed-off-by: Ge Song <ge.song@hxt-semitech.com>
[Split from a larger patch and cleaned up]
Signed-off-by: Geoff Levand <geoff@infradead.org>
---
 configure.ac              |  12 +-
 discover/Makefile.am      |   4 +
 discover/platform-arm64.c | 278 ++++++++++++++++++++++++++++++++++++++++++++++
 3 files changed, 293 insertions(+), 1 deletion(-)
 create mode 100644 discover/platform-arm64.c
diff mbox series

Patch

diff --git a/configure.ac b/configure.ac
index c92ef1d..952c4af 100644
--- a/configure.ac
+++ b/configure.ac
@@ -313,7 +313,7 @@  AC_ARG_ENABLE(
 )
 AS_IF(
 	[test "x$enable_platform_all" = "xyes"],
-	[enable_platform_powerpc="yes"; enable_platform_ps3="yes"]
+	[enable_platform_arm64="yes"; enable_platform_powerpc="yes"; enable_platform_ps3="yes"]
 )
 
 AC_ARG_ENABLE(
@@ -328,10 +328,20 @@  AC_ARG_ENABLE(
 AS_IF(
 	[test "x$enable_platform_auto" = "xyes"],
 	[AS_CASE([$host],
+		[aarch64-*-*],   [enable_platform_arm64="yes"],
 		[powerpc*-*-*],  [enable_platform_powerpc="yes"],
 	)]
 )
 
+AC_ARG_ENABLE(
+	[platform-arm64],
+	[AS_HELP_STRING(
+		[--enable-platform-arm64],
+		[build support for arm64 platforms [default=no]]
+	)]
+)
+AM_CONDITIONAL([PLATFORM_ARM64], [test "x$enable_platform_arm64" = "xyes"])
+
 AC_ARG_ENABLE(
 	[platform-powerpc],
 	[AS_HELP_STRING(
diff --git a/discover/Makefile.am b/discover/Makefile.am
index 2b7c794..f5b799d 100644
--- a/discover/Makefile.am
+++ b/discover/Makefile.am
@@ -82,6 +82,10 @@  discover_platform_ro_SOURCES = \
 	discover/dt.h \
 	discover/hostboot.h
 
+if PLATFORM_ARM64
+discover_platform_ro_SOURCES += discover/platform-arm64.c
+endif
+
 if PLATFORM_POWERPC
 discover_platform_ro_SOURCES += discover/platform-powerpc.c
 endif
diff --git a/discover/platform-arm64.c b/discover/platform-arm64.c
new file mode 100644
index 0000000..9cd6641
--- /dev/null
+++ b/discover/platform-arm64.c
@@ -0,0 +1,278 @@ 
+/*
+ *  This program is free software; you can redistribute it and/or modify
+ *  it under the terms of the GNU General Public License as published by
+ *  the Free Software Foundation; version 2 of the License.
+ *
+ *  This program is distributed in the hope that it will be useful,
+ *  but WITHOUT ANY WARRANTY; without even the implied warranty of
+ *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+ *  GNU General Public License for more details.
+ *
+ *  You should have received a copy of the GNU General Public License
+ *  along with this program; if not, write to the Free Software
+ *  Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
+ *
+ *  Copyright (C) 2018 Huaxintong Semiconductor Technology Co.,Ltd. All rights
+ *  reserved.
+ *  Author: Ge Song <ge.song@hxt-semitech.com>
+ */
+
+#include <ctype.h>
+#include <errno.h>
+#include <limits.h>
+#include <stdlib.h>
+#include <string.h>
+#include <sys/statfs.h>
+
+#include "efi/efivar.h"
+#include "file/file.h"
+#include "log/log.h"
+#include <process/process.h>
+#include <system/system.h>
+#include "talloc/talloc.h"
+#include "types/types.h"
+
+
+#include "ipmi.h"
+#include "platform.h"
+
+static const char *efi_vars_guid = "fb78ab4b-bd43-41a0-99a2-4e74bef9169b";
+
+struct platform_arm64 {
+	struct param_list params;
+	struct ipmi *ipmi;
+};
+
+static inline struct platform_arm64 *to_platform_arm64(struct platform *p)
+{
+	return (struct platform_arm64 *)(p->platform_data);
+}
+
+static void parse_nvram(struct param_list *pl)
+{
+	const char** known;
+
+	param_list_for_each_known_param(pl, known) {
+		struct efi_data *efi_data;
+
+		if (efi_get_variable(NULL, efi_vars_guid, *known, &efi_data))
+			continue;
+
+		param_list_set(pl, *known, efi_data->data,
+			true);
+
+		talloc_free(efi_data);
+	}
+}
+
+static void write_nvram(const struct param_list *pl)
+{
+	struct efi_data efi_data;
+	struct param *param;
+
+	efi_data.attributes =
+		EFI_VARIABLE_NON_VOLATILE |
+		EFI_VARIABLE_RUNTIME_ACCESS |
+		EFI_VARIABLE_BOOTSERVICE_ACCESS;
+
+	param_list_for_each(pl, param) {
+		if (!param->modified)
+			continue;
+
+		efi_data.data = param->value;
+		efi_data.data_size = strlen(param->value) + 1;
+		efi_set_variable(efi_vars_guid, param->name, &efi_data);
+	}
+}
+
+static void get_active_consoles(struct config *config)
+{
+	config->n_consoles = 0;
+	config->consoles = talloc_array(config, char *, 3);
+	if (!config->consoles)
+		return;
+
+	config->consoles[0] = talloc_asprintf(config,
+					"/dev/hvc0 [IPMI / Serial]");
+	config->consoles[1] = talloc_asprintf(config,
+					"/dev/ttyAMA0 [Serial]");
+	config->consoles[2] = talloc_asprintf(config,
+					"/dev/tty1 [VGA]");
+	config->n_consoles = 3;
+}
+
+static int load_config(struct platform *p, struct config *config)
+{
+	struct param_list *pl = &to_platform_arm64(p)->params;
+
+	parse_nvram(pl);
+	config_populate_all(config, pl);
+	get_active_consoles(config);
+
+	return 0;
+}
+
+static char *stdout_cleaner(struct process_stdout *out)
+{
+	char *p;
+	const char *const end = out->buf + out->len;
+
+	for (p = out->buf; *p && p < end; p++) {
+		if (*p == '\n' || *p == '\r') {
+			*p = 0;
+		} else if (!isprint(*p)) {
+			*p = '.';
+		} else if (*p == '\t') {
+			*p = ' ';
+		}
+	}
+	return out->buf;
+}
+
+static int get_sysinfo(struct platform *p, struct system_info *sysinfo)
+{
+	struct platform_arm64 *platform = to_platform_arm64(p);
+	struct process_stdout *out1, *out2;
+
+	/* Use dmidecode to get sysinfo type and identifier. */
+
+	process_get_stdout(NULL, &out1, pb_system_apps.dmidecode,
+		"--string=system-manufacturer",
+		NULL);
+	process_get_stdout(NULL, &out2, pb_system_apps.dmidecode,
+		"--string=system-product-name",
+		NULL);
+
+	if (out1 || out2)
+		sysinfo->type = talloc_asprintf(sysinfo, "%s %s",
+			out1 ? stdout_cleaner(out1) : "Unknown",
+			out2 ? stdout_cleaner(out2) : "Unknown");
+	talloc_free(out1);
+	talloc_free(out2);
+
+	process_get_stdout(NULL, &out1, pb_system_apps.dmidecode,
+		"--string=system-uuid",
+		NULL);
+
+	if (out1)
+		sysinfo->identifier = talloc_asprintf(sysinfo, "%s",
+			stdout_cleaner(out1));
+	talloc_free(out1);
+
+	sysinfo->bmc_mac = talloc_zero_size(sysinfo, HWADDR_SIZE);
+
+	if (platform->ipmi) {
+		ipmi_get_bmc_mac(platform->ipmi, sysinfo->bmc_mac);
+		ipmi_get_bmc_versions(platform->ipmi, sysinfo);
+	}
+
+	pb_debug_fn("type:       '%s'\n", sysinfo->type);
+	pb_debug_fn("identifier: '%s'\n", sysinfo->identifier);
+	pb_debug_fn("bmc_mac:    '%s'\n", sysinfo->bmc_mac);
+
+	return 0;
+}
+
+static void params_update_all(struct param_list *pl,
+	const struct config *config, const struct config *defaults)
+{
+	char *tmp = NULL;
+	const char *val;
+
+	if (config->autoboot_enabled == defaults->autoboot_enabled)
+		val = "";
+	else
+		val = config->autoboot_enabled ? "true" : "false";
+
+	param_list_set_non_empty(pl, "auto-boot?", val, true);
+
+	if (config->autoboot_timeout_sec == defaults->autoboot_timeout_sec)
+		val = "";
+	else
+		val = tmp = talloc_asprintf(pl, "%d",
+			config->autoboot_timeout_sec);
+
+	param_list_set_non_empty(pl, "petitboot,timeout", val, true);
+	if (tmp)
+		talloc_free(tmp);
+
+	val = config->lang ?: "";
+	param_list_set_non_empty(pl, "petitboot,language", val, true);
+
+	if (config->allow_writes == defaults->allow_writes)
+		val = "";
+	else
+		val = config->allow_writes ? "true" : "false";
+	param_list_set_non_empty(pl, "petitboot,write?", val, true);
+
+	if (!config->manual_console) {
+		val = config->boot_console ?: "";
+		param_list_set_non_empty(pl, "petitboot,console", val, true);
+	}
+
+	val = config->http_proxy ?: "";
+	param_list_set_non_empty(pl, "petitboot,http_proxy", val, true);
+	val = config->https_proxy ?: "";
+	param_list_set_non_empty(pl, "petitboot,https_proxy", val, true);
+
+	params_update_network_values(pl, "petitboot,network", config);
+	params_update_bootdev_values(pl, "petitboot,bootdevs", config);
+}
+
+static int save_config(struct platform *p, struct config *config)
+{
+	struct param_list *pl = &to_platform_arm64(p)->params;
+	struct config *defaults;
+
+	defaults = talloc_zero(NULL, struct config);
+	config_set_defaults(defaults);
+
+	params_update_all(pl, config, defaults);
+	talloc_free(defaults);
+
+	write_nvram(pl);
+	
+	return 0;
+}
+
+static bool probe(struct platform *p, void *ctx)
+{
+	static const char *efivars = "/sys/firmware/efi/efivars";
+	struct platform_arm64 *platform;
+	struct statfs buf;
+
+	if (access(efivars, R_OK | W_OK)) {
+		pb_debug_fn("Can't access %s\n", efivars);
+		return false;
+	}
+
+	memset(&buf, '\0', sizeof(buf));
+	if (statfs(efivars, &buf)) {
+		pb_debug_fn("statfs failed: %s: %s\n", efivars,
+			strerror(errno));
+		return false;
+	}
+
+	if (buf.f_type != EFIVARFS_MAGIC) {
+		pb_debug_fn("Bad magic = 0x%lx\n", (unsigned long)buf.f_type);
+		return false;
+	}
+
+	platform = talloc_zero(ctx, struct platform_arm64);
+	param_list_init(&platform->params, common_known_params());
+
+	p->platform_data = platform;
+
+	return true;
+}
+
+static struct platform platform_arm64 = {
+	.name			= "arm64",
+	.dhcp_arch_id		= 0x000d,
+	.probe			= probe,
+	.load_config		= load_config,
+	.save_config		= save_config,
+	.get_sysinfo		= get_sysinfo,
+};
+
+register_platform(platform_arm64);