Skip to content

Commit 8485eb1

Browse files
committed
transport: validate the transport id's from the driver
Verify that it contains only valid transports. While JTAG and SWD are the more permissive transports, the respective 'dapdirect' versions are slightly limited, and the respective 'hla' versions are even more limited. A driver should not provide two version of the same transport. Verify that only one JTAG and only one SWD transport is present. Verify that the preferred transport is valid too. Change-Id: Iace2f881dd65fc763e81b33e6a7113961a7008af Signed-off-by: Antonio Borneo <[email protected]> Reviewed-on: https://review.openocd.org/c/openocd/+/8676 Tested-by: jenkins Reviewed-by: zapb <[email protected]> Reviewed-by: Jan Matyas <[email protected]>
1 parent a500b2c commit 8485eb1

File tree

1 file changed

+26
-10
lines changed

1 file changed

+26
-10
lines changed

src/transport/transport.c

Lines changed: 26 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -125,21 +125,37 @@ static int transport_select(struct command_context *ctx, const char *name)
125125
int allow_transports(struct command_context *ctx, unsigned int transport_ids,
126126
unsigned int transport_preferred_id)
127127
{
128-
/* NOTE: caller is required to provide only a list
129-
* of *valid* transports
130-
*
131-
* REVISIT should we validate that? and insist there's
132-
* at least one valid element in that list?
133-
*
134-
* ... allow removals, e.g. external strapping prevents use
135-
* of one transport; C code should be definitive about what
136-
* can be used when all goes well.
137-
*/
138128
if (allowed_transports || session) {
139129
LOG_ERROR("Can't modify the set of allowed transports.");
140130
return ERROR_FAIL;
141131
}
142132

133+
/* validate the values in transport_ids and transport_preferred_id */
134+
if (transport_ids == 0 || (transport_ids & ~TRANSPORT_VALID_MASK) != 0) {
135+
LOG_ERROR("BUG: Unknown transport IDs %lu", transport_ids & ~TRANSPORT_VALID_MASK);
136+
return ERROR_FAIL;
137+
}
138+
139+
if ((transport_ids & transport_preferred_id) == 0
140+
|| !IS_PWR_OF_2(transport_preferred_id)) {
141+
LOG_ERROR("BUG: Invalid adapter transport_preferred_id");
142+
return ERROR_FAIL;
143+
}
144+
145+
unsigned int mask = transport_ids &
146+
(TRANSPORT_JTAG | TRANSPORT_HLA_JTAG | TRANSPORT_DAPDIRECT_JTAG);
147+
if (mask && !IS_PWR_OF_2(mask)) {
148+
LOG_ERROR("BUG: Multiple JTAG transports");
149+
return ERROR_FAIL;
150+
}
151+
152+
mask = transport_ids &
153+
(TRANSPORT_SWD | TRANSPORT_HLA_SWD | TRANSPORT_DAPDIRECT_SWD);
154+
if (mask && !IS_PWR_OF_2(mask)) {
155+
LOG_ERROR("BUG: Multiple SWD transports");
156+
return ERROR_FAIL;
157+
}
158+
143159
allowed_transports = transport_ids;
144160
preferred_transport = transport_preferred_id;
145161

0 commit comments

Comments
 (0)