[Pdbg] [PATCH 28/29] main: Attempt to automatically discover target
Cyril Bur
cyrilbur at gmail.com
Fri Feb 9 15:38:56 AEDT 2018
Signed-off-by: Cyril Bur <cyrilbur at gmail.com>
---
.gitignore | 1 +
Makefile.am | 3 +-
configure.ac | 8 +++++
src/main.c | 20 +++++++++++-
src/main.h | 2 ++
src/options.h | 30 ++++++++++++++++++
src/options_arm.c | 95 +++++++++++++++++++++++++++++++++++++++++++++++++++++++
src/options_def.c | 39 +++++++++++++++++++++++
src/options_ppc.c | 76 ++++++++++++++++++++++++++++++++++++++++++++
9 files changed, 272 insertions(+), 2 deletions(-)
create mode 100644 src/options.h
create mode 100644 src/options_arm.c
create mode 100644 src/options_def.c
create mode 100644 src/options_ppc.c
diff --git a/.gitignore b/.gitignore
index ec823d4..a996610 100644
--- a/.gitignore
+++ b/.gitignore
@@ -26,6 +26,7 @@ libpdbg.a
.libs
config.guess
config.sub
+src/options.c
*.la
*.lo
ltmain.sh
diff --git a/Makefile.am b/Makefile.am
index 58666aa..6ac51d1 100644
--- a/Makefile.am
+++ b/Makefile.am
@@ -12,7 +12,8 @@ AM_CFLAGS = -I$(top_srcdir)/ccan/array_size -Wall -Werror
pdbg_SOURCES = \
src/main.c src/cfam.c src/scom.c src/reg.c src/mem.c src/thread.c \
- src/htm.c
+ src/htm.c src/options.c
+
pdbg_LDADD = fake.dtb.o p8-fsi.dtb.o p8-i2c.dtb.o p9w-fsi.dtb.o p8-host.dtb.o \
p9z-fsi.dtb.o p9r-fsi.dtb.o p9-kernel.dtb.o libpdbg.la libfdt.la \
p9-host.dtb.o \
diff --git a/configure.ac b/configure.ac
index 43348d7..7fb4393 100644
--- a/configure.ac
+++ b/configure.ac
@@ -8,4 +8,12 @@ AC_CONFIG_MACRO_DIR([m4])
AC_CONFIG_HEADERS([config.h])
AC_CONFIG_FILES([Makefile])
AC_LANG(C)
+
+case "$host" in
+ arm*-*-*) ARCH="arm" ;;
+ powerpc*-*-*) ARCH="ppc" ;;
+ *) ARCH="def" ;;
+esac
+AC_CONFIG_LINKS([src/options.c:src/options_$ARCH.c])
+
AC_OUTPUT
diff --git a/src/main.c b/src/main.c
index 11b578e..33e77b9 100644
--- a/src/main.c
+++ b/src/main.c
@@ -32,6 +32,7 @@
#include <libpdbg.h>
+#include "main.h"
#include "bitutils.h"
#include "cfam.h"
#include "scom.h"
@@ -39,6 +40,7 @@
#include "mem.h"
#include "thread.h"
#include "htm.h"
+#include "options.h"
#undef PR_DEBUG
#define PR_DEBUG(...)
@@ -47,7 +49,6 @@
#define THREADS_PER_CORE 8
-enum backend { FSI, I2C, KERNEL, FAKE, HOST };
static enum backend backend = KERNEL;
static char const *device_node;
@@ -462,9 +463,26 @@ int main(int argc, char *argv[])
{
int i, rc = 0;
+ backend = default_backend();
+ device_node = default_target(backend);
+
if (parse_options(argc, argv))
return 1;
+ if (!backend_is_possible(backend)) {
+ fprintf(stderr, "Backend not possible\n");
+ /* Probs say something about bad backend */
+ print_usage(argv[0]);
+ return 1;
+ }
+
+ if (!target_is_possible(backend, device_node)) {
+ fprintf(stderr, "Target not possible\n");
+ /* Probs say something about bad target */
+ print_usage(argv[0]);
+ return 1;
+ }
+
if (optind >= argc) {
print_usage(argv[0]);
return 1;
diff --git a/src/main.h b/src/main.h
index 1bfdd99..1849d3f 100644
--- a/src/main.h
+++ b/src/main.h
@@ -17,6 +17,8 @@
#include <target.h>
+enum backend { FSI, I2C, KERNEL, FAKE, HOST };
+
static inline bool target_is_disabled(struct pdbg_target *target)
{
return pdbg_target_index(target) == PDBG_TARGET_DISABLED;
diff --git a/src/options.h b/src/options.h
new file mode 100644
index 0000000..7624c20
--- /dev/null
+++ b/src/options.h
@@ -0,0 +1,30 @@
+/* Copyright 2017 IBM Corp.
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or
+ * implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+/* Default backend on this platform */
+enum backend default_backend(void);
+
+/* Is this backend possible on this platform */
+bool backend_is_possible(enum backend backend);
+
+/* The default (perhaps only) target for this backend */
+const char *default_target(enum backend backend);
+
+/*
+ * Does this platform backend support this target,
+ * there is an implied check of is_backend_possible()
+ */
+bool target_is_possible(enum backend backend, const char *target);
diff --git a/src/options_arm.c b/src/options_arm.c
new file mode 100644
index 0000000..689424b
--- /dev/null
+++ b/src/options_arm.c
@@ -0,0 +1,95 @@
+/* Copyright 2017 IBM Corp.
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or
+ * implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+#include <stdio.h>
+#include <string.h>
+#include <unistd.h>
+
+#include "main.h"
+
+#define AMI_BMC "/proc/ractrends/Helper/FwInfo"
+#define OPENFSI_BMC "/sys/bus/platform/devices/gpio-fsi/fsi0/"
+
+static const char witherspoon[] = "witherspoon";
+static const char romulus[] = "romulus";
+static const char zaius[] = "zaius";
+
+enum backend default_backend(void)
+{
+ int rc;
+ rc = access(AMI_BMC, F_OK);
+ if (rc == 0) /* AMI BMC */
+ return I2C;
+
+ rc = access(OPENFSI_BMC, F_OK);
+ if (rc == 0) /* Kernel interface! OpenBMC */
+ return KERNEL;
+
+ /*
+ * "This should never be the default" - Apopple 2017
+ * Unfortunately if both of those files don't exist we don't
+ * know what to do, we'll have to try this and hope
+ */
+ fprintf(stderr, "Couldn't locate a good backend, using fallback!\n");
+ return FSI;
+}
+
+bool backend_is_possible(enum backend backend)
+{
+ if (backend == I2C && access(AMI_BMC, F_OK) == 0)
+ return true;
+ if (backend == KERNEL && access(OPENFSI_BMC, F_OK) == 0)
+ return true;
+
+ return backend == FSI;
+}
+
+const char *default_target(enum backend backend)
+{
+ FILE *dt_compatible;
+ char line[256];
+
+ if (backend == I2C || backend == KERNEL) /* No target nessesary */
+ return NULL;
+
+ dt_compatible = fopen("/proc/device-tree/compatible", "r");
+ if (!dt_compatible)
+ return NULL;
+
+ fgets(line, sizeof(line), dt_compatible);
+ fclose(dt_compatible);
+
+ if (strstr(witherspoon, line))
+ return witherspoon;
+
+ if (strstr(romulus, line))
+ return romulus;
+
+ if (strstr(zaius, line))
+ return zaius;
+
+ return NULL;
+}
+
+bool target_is_possible(enum backend backend, const char *target)
+{
+ const char *def;
+
+ if (!backend_is_possible(backend))
+ return false;
+
+ def = default_target(backend);
+ return strcmp(def, target) == 0;
+}
diff --git a/src/options_def.c b/src/options_def.c
new file mode 100644
index 0000000..a7179e6
--- /dev/null
+++ b/src/options_def.c
@@ -0,0 +1,39 @@
+/* Copyright 2017 IBM Corp.
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or
+ * implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+#include "main.h"
+
+enum backend default_backend(void)
+{
+ return FAKE;
+}
+
+bool backend_is_possible(enum backend backend)
+{
+ return backend == FAKE;
+}
+
+/* Theres no target for FAKE backend */
+const char *default_target(enum backend backend)
+{
+ return NULL;
+}
+
+/* Theres no device for FAKE backend */
+bool target_is_possible(enum backend backend, const char *target)
+{
+ return target == NULL;
+}
diff --git a/src/options_ppc.c b/src/options_ppc.c
new file mode 100644
index 0000000..f9223e6
--- /dev/null
+++ b/src/options_ppc.c
@@ -0,0 +1,76 @@
+/* Copyright 2017 IBM Corp.
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or
+ * implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+#include <stdio.h>
+#include <string.h>
+
+#include "main.h"
+
+static const char p8[] = "p8";
+static const char p9[] = "p9";
+
+enum backend default_backend(void)
+{
+ return HOST;
+}
+
+bool backend_is_possible(enum backend backend)
+{
+ /* TODO */
+ return backend == HOST;
+}
+
+const char *default_target(enum backend backend)
+{
+ const char *pos = NULL;
+ char line[256];
+ FILE *cpuinfo;
+
+ cpuinfo = fopen("/proc/cpuinfo", "r");
+ if (!cpuinfo)
+ return NULL;
+
+ while (fgets(line, sizeof(line), cpuinfo))
+ if (strncmp(line, "cpu", 3) == 0)
+ break;
+ fclose(cpuinfo);
+
+ pos = strchr(line, ':');
+ if (!pos)
+ return NULL;
+
+ if (*(pos + 1) == '\0')
+ return NULL;
+ pos += 2;
+
+ if (strncmp(pos, "POWER8", 6) == 0)
+ return p8;
+
+ if (strncmp(pos, "POWER9", 6) == 0)
+ return p9;
+
+ return NULL;
+}
+
+bool target_is_possible(enum backend backend, const char *target)
+{
+ const char *def;
+
+ if (!backend_is_possible(backend))
+ return false;
+
+ def = default_target(backend);
+ return strcmp(def, target) == 0;
+}
--
2.16.1
More information about the Pdbg
mailing list