diff --git a/src/Makefile b/src/Makefile index 2bab43fc557..d3fa4b0f2f2 100644 --- a/src/Makefile +++ b/src/Makefile @@ -61,6 +61,7 @@ SRC = \ stm32l4.c \ stm32g0.c \ target.c \ + target_probes.c include $(PLATFORM_DIR)/Makefile.inc diff --git a/src/target/adiv5.c b/src/target/adiv5.c index af6251a7612..73da60bc86a 100644 --- a/src/target/adiv5.c +++ b/src/target/adiv5.c @@ -28,6 +28,7 @@ #include "general.h" #include "target.h" #include "target_internal.h" +#include "target_probes.h" #include "adiv5.h" #include "cortexm.h" #include "exception.h" @@ -261,8 +262,6 @@ static const struct { {0xfff, 0x00, 0, aa_end, cidc_unknown, PIDR_PN_BIT_STRINGS("end", "end")} }; -extern bool cortexa_probe(ADIv5_AP_t *apb, uint32_t debug_base); - void adiv5_ap_ref(ADIv5_AP_t *ap) { if (ap->refcnt == 0) @@ -663,7 +662,7 @@ static void rp_rescue_setup(ADIv5_DP_t *dp) } memset(ap, 0, sizeof(ADIv5_AP_t)); ap->dp = dp; - extern void rp_rescue_probe(ADIv5_AP_t *); + rp_rescue_probe(ap); return; } @@ -796,13 +795,9 @@ void adiv5_dp_init(ADIv5_DP_t *dp) return; } last_base = ap->base; - extern void kinetis_mdm_probe(ADIv5_AP_t *); - kinetis_mdm_probe(ap); - extern void nrf51_mdm_probe(ADIv5_AP_t *); + kinetis_mdm_probe(ap); nrf51_mdm_probe(ap); - - extern void efm32_aap_probe(ADIv5_AP_t *); efm32_aap_probe(ap); /* Halt the device and release from reset if reset is active!*/ diff --git a/src/target/cortexm.c b/src/target/cortexm.c index a73820520bb..6d151768ecc 100644 --- a/src/target/cortexm.c +++ b/src/target/cortexm.c @@ -32,6 +32,7 @@ #include "adiv5.h" #include "target.h" #include "target_internal.h" +#include "target_probes.h" #include "cortexm.h" #include "command.h" #include "gdb_packet.h" @@ -378,6 +379,7 @@ bool cortexm_probe(ADIv5_AP_t *ap) } else { target_check_error(t); } + #define PROBE(x) \ do { if ((x)(t)) {return true;} else target_check_error(t); } while (0) diff --git a/src/target/cortexm.h b/src/target/cortexm.h index 8bd95d368f3..d41e231cdaf 100644 --- a/src/target/cortexm.h +++ b/src/target/cortexm.h @@ -185,7 +185,6 @@ enum cortexm_types { #define CPUID_REVISION_MASK 0x00f00000 #define CPUID_PATCH_MASK 0xf -bool cortexm_probe(ADIv5_AP_t *ap); ADIv5_AP_t *cortexm_ap(target *t); bool cortexm_attach(target *t); diff --git a/src/target/efm32.c b/src/target/efm32.c index 4589c00b28e..f0fc7e28717 100644 --- a/src/target/efm32.c +++ b/src/target/efm32.c @@ -986,13 +986,13 @@ struct efm32_aap_priv_s { char aap_driver_string[42]; }; -void efm32_aap_probe(ADIv5_AP_t *ap) +bool efm32_aap_probe(ADIv5_AP_t *ap) { if ((ap->idr & EFM32_APP_IDR_MASK) == EFM32_AAP_IDR) { /* It's an EFM32 AAP! */ DEBUG_INFO("EFM32: Found EFM32 AAP\n"); } else { - return; + return false; } /* Both revsion 1 and revision 2 devices seen in the wild */ @@ -1017,6 +1017,8 @@ void efm32_aap_probe(ADIv5_AP_t *ap) t->regs_size = 4; target_add_commands(t, efm32_aap_cmd_list, t->driver); + + return true; } static bool efm32_aap_cmd_device_erase(target *t, int argc, const char **argv) diff --git a/src/target/kinetis.c b/src/target/kinetis.c index 5e44bfe8f14..c271c14fb8d 100644 --- a/src/target/kinetis.c +++ b/src/target/kinetis.c @@ -524,19 +524,19 @@ enum target_halt_reason mdm_halt_poll(target *t, target_addr *watch) return TARGET_HALT_REQUEST; } -void kinetis_mdm_probe(ADIv5_AP_t *ap) +bool kinetis_mdm_probe(ADIv5_AP_t *ap) { switch(ap->idr) { case KINETIS_MDM_IDR_KZ03: /* Also valid for KE04, no way to check! */ case KINETIS_MDM_IDR_K22F: break; default: - return; + return false; } target *t = target_new(); if (!t) { - return; + return false; } adiv5_ap_ref(ap); @@ -546,6 +546,8 @@ void kinetis_mdm_probe(ADIv5_AP_t *ap) t->driver = "Kinetis Recovery (MDM-AP)"; t->regs_size = 4; target_add_commands(t, kinetis_mdm_cmd_list, t->driver); + + return true; } #define MDM_STATUS ADIV5_AP_REG(0x00) diff --git a/src/target/nrf51.c b/src/target/nrf51.c index 4021c0a9d8a..10c9ad84569 100644 --- a/src/target/nrf51.c +++ b/src/target/nrf51.c @@ -411,18 +411,18 @@ const struct command_s nrf51_mdm_cmd_list[] = { #define MDM_CONTROL ADIV5_AP_REG(0x04) #define MDM_PROT_EN ADIV5_AP_REG(0x0C) -void nrf51_mdm_probe(ADIv5_AP_t *ap) +bool nrf51_mdm_probe(ADIv5_AP_t *ap) { switch(ap->idr) { case NRF52_MDM_IDR: break; default: - return; + return false; } target *t = target_new(); if (!t) { - return; + return false; } adiv5_ap_ref(ap); @@ -437,6 +437,8 @@ void nrf51_mdm_probe(ADIv5_AP_t *ap) t->driver = "Nordic nRF52 Access Port (protected)"; t->regs_size = 4; target_add_commands(t, nrf51_mdm_cmd_list, t->driver); + + return true; } static bool nrf51_mdm_cmd_erase_mass(target *t, int argc, const char **argv) diff --git a/src/target/rp.c b/src/target/rp.c index 649c3b6423a..7043562ffa5 100644 --- a/src/target/rp.c +++ b/src/target/rp.c @@ -427,11 +427,11 @@ static bool rp_rescue_do_reset(target *t) * * Attach to this DP will do the reset, but will fail to attach! */ -void rp_rescue_probe(ADIv5_AP_t *ap) +bool rp_rescue_probe(ADIv5_AP_t *ap) { target *t = target_new(); if (!t) { - return; + return false; } adiv5_ap_ref(ap); @@ -439,4 +439,6 @@ void rp_rescue_probe(ADIv5_AP_t *ap) t->priv = ap; t->priv_free = (void*)adiv5_ap_unref; t->driver = "Raspberry RP2040 Rescue(Attach to reset!)"; + + return true; } diff --git a/src/target/target_internal.h b/src/target/target_internal.h index 883d3b57313..bfc1e2524df 100644 --- a/src/target/target_internal.h +++ b/src/target/target_internal.h @@ -172,33 +172,4 @@ int tc_gettimeofday(target *t, target_addr tv, target_addr tz); int tc_isatty(target *t, int fd); int tc_system(target *t, target_addr cmd, size_t cmdlen); -/* Probe for various targets. - * Actual functions implemented in their respective drivers. - */ -bool ch32f1_probe(target *t); // will catch all the clones -bool gd32f1_probe(target *t); -bool stm32f1_probe(target *t); -bool stm32f4_probe(target *t); -bool stm32h7_probe(target *t); -bool stm32l0_probe(target *t); -bool stm32l1_probe(target *t); -bool stm32l4_probe(target *t); -bool stm32g0_probe(target *t); -bool lmi_probe(target *t); -bool lpc11xx_probe(target *t); -bool lpc15xx_probe(target *t); -bool lpc17xx_probe(target *t); -bool lpc43xx_probe(target *t); -bool lpc546xx_probe(target *t); -bool samx7x_probe(target *t); -bool sam3x_probe(target *t); -bool sam4l_probe(target *t); -bool nrf51_probe(target *t); -bool samd_probe(target *t); -bool samx5x_probe(target *t); -bool kinetis_probe(target *t); -bool efm32_probe(target *t); -bool msp432_probe(target *t); -bool ke04_probe(target *t); -bool rp_probe(target *t); #endif diff --git a/src/target/target_probes.c b/src/target/target_probes.c new file mode 100644 index 00000000000..b7cb4cfdbd7 --- /dev/null +++ b/src/target/target_probes.c @@ -0,0 +1,87 @@ +/* + * This file is part of the Black Magic Debug project. + * + * Copyright (C) 2022 Rafael Silva + * + * 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, either version 3 of the License, or + * (at your option) any later version. + * + * 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, see . + */ + +#include "target_probes.h" + +#define CORTEXA_PROBE_DUMMY __attribute__((weak, alias("_cortexa_probe_dummy"))) +#define CORTEXM_PROBE_DUMMY __attribute__((weak, alias("_cortexm_probe_dummy"))) +#define TARGET_PROBE_DUMMY __attribute__((weak, alias("_target_probe_dummy"))) + +static inline bool _target_probe_dummy(target *t) +{ + (void)t; + + return false; +} + +static inline bool _cortexa_probe_dummy(ADIv5_AP_t *apb, uint32_t debug_base) +{ + (void)apb; + (void)debug_base; + + return false; +} + +static inline bool _cortexm_probe_dummy(ADIv5_AP_t *ap) +{ + (void)ap; + + return false; +} + +/* + * "dummy" alias functions to allow suport for these targets + * to be disabled by not compiling them in. + */ + +bool cortexa_probe(ADIv5_AP_t *apb, uint32_t debug_base) CORTEXA_PROBE_DUMMY; + +bool cortexm_probe(ADIv5_AP_t *ap) CORTEXM_PROBE_DUMMY; + +bool kinetis_mdm_probe(ADIv5_AP_t *ap) CORTEXM_PROBE_DUMMY; +bool nrf51_mdm_probe(ADIv5_AP_t *ap) CORTEXM_PROBE_DUMMY; +bool efm32_aap_probe(ADIv5_AP_t *ap) CORTEXM_PROBE_DUMMY; +bool rp_rescue_probe(ADIv5_AP_t *ap) CORTEXM_PROBE_DUMMY; + +bool ch32f1_probe(target *t) TARGET_PROBE_DUMMY; // will catch all the clones +bool gd32f1_probe(target *t) TARGET_PROBE_DUMMY; +bool stm32f1_probe(target *t) TARGET_PROBE_DUMMY; +bool stm32f4_probe(target *t) TARGET_PROBE_DUMMY; +bool stm32h7_probe(target *t) TARGET_PROBE_DUMMY; +bool stm32l0_probe(target *t) TARGET_PROBE_DUMMY; +bool stm32l1_probe(target *t) TARGET_PROBE_DUMMY; +bool stm32l4_probe(target *t) TARGET_PROBE_DUMMY; +bool stm32g0_probe(target *t) TARGET_PROBE_DUMMY; +bool lmi_probe(target *t) TARGET_PROBE_DUMMY; +bool lpc11xx_probe(target *t) TARGET_PROBE_DUMMY; +bool lpc15xx_probe(target *t) TARGET_PROBE_DUMMY; +bool lpc17xx_probe(target *t) TARGET_PROBE_DUMMY; +bool lpc43xx_probe(target *t) TARGET_PROBE_DUMMY; +bool lpc546xx_probe(target *t) TARGET_PROBE_DUMMY; +bool samx7x_probe(target *t) TARGET_PROBE_DUMMY; +bool sam3x_probe(target *t) TARGET_PROBE_DUMMY; +bool sam4l_probe(target *t) TARGET_PROBE_DUMMY; +bool nrf51_probe(target *t) TARGET_PROBE_DUMMY; +bool samd_probe(target *t) TARGET_PROBE_DUMMY; +bool samx5x_probe(target *t) TARGET_PROBE_DUMMY; +bool kinetis_probe(target *t) TARGET_PROBE_DUMMY; +bool efm32_probe(target *t) TARGET_PROBE_DUMMY; +bool msp432_probe(target *t) TARGET_PROBE_DUMMY; +bool ke04_probe(target *t) TARGET_PROBE_DUMMY; +bool rp_probe(target *t) TARGET_PROBE_DUMMY; diff --git a/src/target/target_probes.h b/src/target/target_probes.h new file mode 100644 index 00000000000..aa76054796c --- /dev/null +++ b/src/target/target_probes.h @@ -0,0 +1,66 @@ +/* + * This file is part of the Black Magic Debug project. + * + * Copyright (C) 2022 Rafael Silva + * + * 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, either version 3 of the License, or + * (at your option) any later version. + * + * 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, see . + */ + +#ifndef __TARGET_PROBE_H +#define __TARGET_PROBE_H + +#include "target.h" +#include "adiv5.h" + +/* Probe for various targets. + * Actual functions implemented in their respective drivers. + */ + +bool cortexa_probe(ADIv5_AP_t *apb, uint32_t debug_base); + +bool cortexm_probe(ADIv5_AP_t *ap); + +bool kinetis_mdm_probe(ADIv5_AP_t *ap); +bool nrf51_mdm_probe(ADIv5_AP_t *ap); +bool efm32_aap_probe(ADIv5_AP_t *ap); +bool rp_rescue_probe(ADIv5_AP_t *ap); + +bool ch32f1_probe(target *t); // will catch all the clones +bool gd32f1_probe(target *t); +bool stm32f1_probe(target *t); +bool stm32f4_probe(target *t); +bool stm32h7_probe(target *t); +bool stm32l0_probe(target *t); +bool stm32l1_probe(target *t); +bool stm32l4_probe(target *t); +bool stm32g0_probe(target *t); +bool lmi_probe(target *t); +bool lpc11xx_probe(target *t); +bool lpc15xx_probe(target *t); +bool lpc17xx_probe(target *t); +bool lpc43xx_probe(target *t); +bool lpc546xx_probe(target *t); +bool samx7x_probe(target *t); +bool sam3x_probe(target *t); +bool sam4l_probe(target *t); +bool nrf51_probe(target *t); +bool samd_probe(target *t); +bool samx5x_probe(target *t); +bool kinetis_probe(target *t); +bool efm32_probe(target *t); +bool msp432_probe(target *t); +bool ke04_probe(target *t); +bool rp_probe(target *t); + +#endif /* __TARGET_PROBE_H */