diff mbox series

[v2,1/2] libpdbg: Refactor code to get chip id

Message ID 20200625062544.306406-1-amitay@ozlabs.org
State Superseded
Headers show
Series [v2,1/2] libpdbg: Refactor code to get chip id | expand

Commit Message

Amitay Isaacs June 25, 2020, 6:25 a.m. UTC
Signed-off-by: Amitay Isaacs <amitay@ozlabs.org>
---
 libpdbg/dtb.c | 58 +++++++++++++++++++++++++++++----------------------
 1 file changed, 33 insertions(+), 25 deletions(-)
diff mbox series

Patch

diff --git a/libpdbg/dtb.c b/libpdbg/dtb.c
index 6d9b182..3006cda 100644
--- a/libpdbg/dtb.c
+++ b/libpdbg/dtb.c
@@ -173,55 +173,63 @@  static void ppc_target(struct pdbg_dtb *dtb)
  	}
 }
 
-static void bmc_target(struct pdbg_dtb *dtb)
+static uint32_t get_chipid(void)
 {
 	FILE *cfam_id_file;
 	char *path;
-	uint32_t cfam_id = 0;
-	uint32_t chip_id = 0;
+	uint32_t cfam_id = 0, chip_id;
 	int rc;
 
-	if (pdbg_backend_option) {
-		if (!strcmp(pdbg_backend_option, "p8")) {
-			if (!dtb->backend.fdt)
-				dtb->backend.fdt = &_binary_p8_kernel_dtb_o_start;
-			if (!dtb->system.fdt)
-				dtb->system.fdt = &_binary_p8_dtb_o_start;
-		} else if (!strcmp(pdbg_backend_option, "p9")) {
-			if (!dtb->backend.fdt)
-				dtb->backend.fdt = &_binary_bmc_kernel_dtb_o_start;
-			if (!dtb->system.fdt)
-				dtb->system.fdt = &_binary_p9_dtb_o_start;
-		} else {
-			pdbg_log(PDBG_ERROR, "Invalid system type %s\n", pdbg_backend_option);
-			pdbg_log(PDBG_ERROR, "Use 'p8' or 'p9'\n");
-		}
-
-		return;
-	}
-
 	/* Try and determine the correct device type */
 	rc = asprintf(&path, "%s/fsi0/slave@00:00/cfam_id", kernel_get_fsi_path());
 	if (rc < 0) {
 		pdbg_log(PDBG_ERROR, "Unable create fsi path");
-		return;
+		return 0;
 	}
 
 	cfam_id_file = fopen(path, "r");
 	free(path);
 	if (!cfam_id_file) {
 		pdbg_log(PDBG_ERROR, "Unabled to open CFAM ID file\n");
-		return;
+		return 0;
 	}
 
 	rc = fscanf(cfam_id_file, "0x%" PRIx32, &cfam_id);
 	if (rc != 1) {
 		pdbg_log(PDBG_ERROR, "Unable to read CFAM ID: %s", strerror(errno));
 		fclose(cfam_id_file);
-		return;
+		return 0;
 	}
 	fclose(cfam_id_file);
+
 	chip_id = (cfam_id >> 4) & 0xff;
+	return chip_id;
+}
+
+static void bmc_target(struct pdbg_dtb *dtb)
+{
+	uint32_t chip_id;
+
+	if (pdbg_backend_option) {
+		if (!strcmp(pdbg_backend_option, "p8")) {
+			if (!dtb->backend.fdt)
+				dtb->backend.fdt = &_binary_p8_kernel_dtb_o_start;
+			if (!dtb->system.fdt)
+				dtb->system.fdt = &_binary_p8_dtb_o_start;
+		} else if (!strcmp(pdbg_backend_option, "p9")) {
+			if (!dtb->backend.fdt)
+				dtb->backend.fdt = &_binary_bmc_kernel_dtb_o_start;
+			if (!dtb->system.fdt)
+				dtb->system.fdt = &_binary_p9_dtb_o_start;
+		} else {
+			pdbg_log(PDBG_ERROR, "Invalid system type %s\n", pdbg_backend_option);
+			pdbg_log(PDBG_ERROR, "Use 'p8' or 'p9'\n");
+		}
+
+		return;
+	}
+
+	chip_id = get_chipid();
 
 	switch(chip_id) {
 	case CHIP_ID_P9: