Skip to content

Commit

Permalink
target: add weak aliasing to allow disabling targets
Browse files Browse the repository at this point in the history
Signed-off-by: Rafael Silva <[email protected]>
  • Loading branch information
perigoso committed Jun 8, 2022
1 parent 483e51d commit 2e53e38
Show file tree
Hide file tree
Showing 11 changed files with 177 additions and 48 deletions.
1 change: 1 addition & 0 deletions src/Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,7 @@ SRC = \
stm32l4.c \
stm32g0.c \
target.c \
target_probes.c


include $(PLATFORM_DIR)/Makefile.inc
Expand Down
11 changes: 3 additions & 8 deletions src/target/adiv5.c
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down Expand Up @@ -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)
Expand Down Expand Up @@ -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;
}
Expand Down Expand Up @@ -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!*/
Expand Down
2 changes: 2 additions & 0 deletions src/target/cortexm.c
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down Expand Up @@ -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)

Expand Down
1 change: 0 additions & 1 deletion src/target/cortexm.h
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down
6 changes: 4 additions & 2 deletions src/target/efm32.c
Original file line number Diff line number Diff line change
Expand Up @@ -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 */
Expand All @@ -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)
Expand Down
8 changes: 5 additions & 3 deletions src/target/kinetis.c
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand All @@ -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)
Expand Down
8 changes: 5 additions & 3 deletions src/target/nrf51.c
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand All @@ -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)
Expand Down
6 changes: 4 additions & 2 deletions src/target/rp.c
Original file line number Diff line number Diff line change
Expand Up @@ -427,16 +427,18 @@ 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);
t->attach = (void*)rp_rescue_do_reset;
t->priv = ap;
t->priv_free = (void*)adiv5_ap_unref;
t->driver = "Raspberry RP2040 Rescue(Attach to reset!)";

return true;
}
29 changes: 0 additions & 29 deletions src/target/target_internal.h
Original file line number Diff line number Diff line change
Expand Up @@ -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
87 changes: 87 additions & 0 deletions src/target/target_probes.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,87 @@
/*
* This file is part of the Black Magic Debug project.
*
* Copyright (C) 2022 Rafael Silva <[email protected]>
*
* 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 <http://www.gnu.org/licenses/>.
*/

#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;
66 changes: 66 additions & 0 deletions src/target/target_probes.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,66 @@
/*
* This file is part of the Black Magic Debug project.
*
* Copyright (C) 2022 Rafael Silva <[email protected]>
*
* 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 <http://www.gnu.org/licenses/>.
*/

#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 */

0 comments on commit 2e53e38

Please sign in to comment.