bmputil 1.1.0

Black Magic Probe companion utility
Documentation
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
// SPDX-License-Identifier: MIT OR Apache-2.0
// SPDX-FileCopyrightText: 2025 1BitSquared <info@1bitsquared.com>
// SPDX-FileContributor: Written by Rachel Mant <git@dragonmux.network>
// SPDX-FileContributor: Modified by P-Storm <pauldeman@gmail.com>

use std::cmp::Ordering;
use std::fmt::{Display, Formatter};

use color_eyre::eyre::{Report, Result, eyre};

use crate::metadata::structs::Probe;

const BMP_PRODUCT_STRING: &str = "Black Magic Probe";
const BMP_BOOT_STRING: &str = "Black Magic Probe DFU";
const DRAGON_BOOT_STRING: &str = "dragonBoot DFU bootloader";
const BMP_NATIVE: &str = "native";

#[derive(PartialEq, Eq)]
pub struct ProbeIdentity
{
	kind: DeviceKind,
	pub version: VersionNumber,
}

#[derive(PartialEq, Eq)]
pub enum DeviceKind
{
	Probe(Probe),
	Bootloader(String),
}

enum ParseNameError
{
	OpeningParenthesisAfterClosingParenthesis,
	FoundNotMatchedParenthesis,
}

#[derive(Debug)]
enum ParseVersionError
{
	FormattingPatternError,
	EmptyOrWhitespaceVersion,
}

#[derive(Debug, PartialEq, Eq)]
pub enum VersionNumber
{
	Unknown,
	Invalid,
	GitHash(String),
	FullVersion(VersionParts),
}

#[derive(Debug, PartialEq, Eq)]
pub struct VersionParts
{
	major: usize,
	minor: usize,
	revision: usize,
	kind: VersionKind,
	dirty: bool,
}

#[derive(Debug, PartialEq, Eq)]
pub enum VersionKind
{
	Release,
	ReleaseCandidate(usize),
	Development(GitVersion),
}

#[derive(Debug, PartialEq, Eq)]
pub struct GitVersion
{
	release_candidate: Option<usize>,
	commits: usize,
	hash: String,
}

impl Display for ParseNameError
{
	fn fmt(&self, f: &mut Formatter<'_>) -> std::fmt::Result
	{
		match self {
			ParseNameError::OpeningParenthesisAfterClosingParenthesis => {
				write!(f, "A '(' parenthesis is found after a ')'.")
			},
			ParseNameError::FoundNotMatchedParenthesis => write!(f, "Not a matching pair of parenthesis found."),
		}
	}
}

impl Display for ParseVersionError
{
	fn fmt(&self, f: &mut Formatter<'_>) -> std::fmt::Result
	{
		match self {
			ParseVersionError::FormattingPatternError => write!(
				f,
				"The version failed to match the pattern of '{} (<version number>)'.",
				BMP_PRODUCT_STRING
			),
			ParseVersionError::EmptyOrWhitespaceVersion => write!(f, "The extracted version is empty or whitespace"),
		}
	}
}

fn parse_name_from_identity_string(input: &str) -> Result<&str, ParseNameError>
{
	let opening_paren = input.find('(');
	let closing_paren = input.find(')');

	match (opening_paren, closing_paren) {
		(None, None) => Ok(BMP_NATIVE),
		(Some(opening_paren), Some(closing_paren)) => {
			if opening_paren > closing_paren {
				Err(ParseNameError::OpeningParenthesisAfterClosingParenthesis)
			} else {
				Ok(&input[opening_paren + 1..closing_paren])
			}
		},
		(Some(_), None) => Err(ParseNameError::FoundNotMatchedParenthesis),
		(None, Some(_)) => Err(ParseNameError::FoundNotMatchedParenthesis),
	}
}

fn parse_version_from_identity_string(input: &str) -> Result<&str, ParseVersionError>
{
	let start_index = input.rfind(' ').ok_or(ParseVersionError::FormattingPatternError)?;
	let version = &input[start_index + 1..];

	if version.trim().is_empty() {
		return Err(ParseVersionError::EmptyOrWhitespaceVersion);
	}

	Ok(version)
}

impl TryFrom<&str> for ProbeIdentity
{
	type Error = Report;

	fn try_from(identity: &str) -> Result<Self>
	{
		// BMD product strings are in one of the following forms:
		// Recent: Black Magic Probe v2.0.0-rc2
		//       : Black Magic Probe (ST-Link/v2) v1.10.0-1273-g2b1ce9aee
		//    Old: Black Magic Probe
		// From this we want to extract two main things: version (if available), and probe variety
		// (probe variety meaning alternative platform kind if not a BMP itself)
		//
		// NB: Bootloaders do not necessarily follow this pattern and can use other string formats.
		// They are no less valid(!) and must be represented somehow by a ProbeIdentity.
		// Examples from the wild include:
		// - Black Magic Probe DFU v1.9.0
		// - dragonBoot DFU bootloader
		// and ST's boot ROM bootloader
		//
		// We mark these additional strings as bootloader strings for which we cannot determine the
		// probe kind that they run on as the strings fail to encode that information.

		// Bootloaders should either start with the project DFU base string, or one of a small set of
		// known alternatives like dragonBoot's signature string. Handle them here first.
		if identity.starts_with(BMP_BOOT_STRING) || identity.starts_with(DRAGON_BOOT_STRING) {
			return Ok(ProbeIdentity {
				kind: DeviceKind::Bootloader(identity.to_string()),
				version: VersionNumber::Unknown,
			});
		}

		// Every probe identity should start with 'Black Magic Probe'
		if !identity.starts_with(BMP_PRODUCT_STRING) {
			return Err(eyre!("Product string doesn't start with '{}'", BMP_PRODUCT_STRING));
		}

		// If it is exactly 'Black Magic Probe', then it is an old identity, with an unknown version.
		if identity == BMP_PRODUCT_STRING {
			return Ok(ProbeIdentity {
				kind: DeviceKind::Probe(Probe::Native),
				version: VersionNumber::Unknown,
			});
		}

		// Removes the first length from the identity, because we know it starts with the 'Black Magic Probe'
		let parse_slice = &identity[BMP_PRODUCT_STRING.len()..];
		let probe = parse_name_from_identity_string(parse_slice)
			.map_err(|error| eyre!("Error while parsing probe string: {}", error))?
			.to_lowercase();

		let version = parse_version_from_identity_string(parse_slice)
			.map_err(|error| eyre!("Error while parsing version string: {}", error))?;

		Ok(ProbeIdentity {
			kind: DeviceKind::Probe(probe.try_into()?),
			version: version.into(),
		})
	}
}

impl TryFrom<String> for ProbeIdentity
{
	type Error = Report;

	fn try_from(identity: String) -> Result<Self>
	{
		identity.as_str().try_into()
	}
}

impl Display for ProbeIdentity
{
	fn fmt(&self, f: &mut Formatter<'_>) -> std::fmt::Result
	{
		match &self.kind {
			DeviceKind::Probe(probe) => {
				// Probe names always use the product string as a prefix
				write!(f, "{}", BMP_PRODUCT_STRING)?;
				// If it's not a native probe, display the variant name
				if probe != &Probe::Native {
					write!(f, " ({})", probe.to_string())?;
				}
				// Translate the version string as best as possible to a readable form
				match &self.version {
					VersionNumber::Unknown => Ok(()),
					VersionNumber::Invalid => write!(f, " <invalid version>"),
					VersionNumber::GitHash(hash) => write!(f, " {}", hash),
					VersionNumber::FullVersion(version_parts) => write!(f, " {}", version_parts.to_string()),
				}
			},
			DeviceKind::Bootloader(ident) => write!(f, "{ident}"),
		}
	}
}

impl ProbeIdentity
{
	pub fn variant(&self) -> Option<Probe>
	{
		match self.kind {
			DeviceKind::Probe(probe) => Some(probe),
			_ => None,
		}
	}

	pub fn is_bootloader(&self) -> bool
	{
		matches!(self.kind, DeviceKind::Bootloader(_))
	}
}

impl TryFrom<String> for Probe
{
	type Error = Report;

	fn try_from(value: String) -> Result<Self>
	{
		match value.as_str() {
			"96b carbon" => Ok(Probe::_96bCarbon),
			"blackpill-f401cc" => Ok(Probe::BlackpillF401CC),
			"blackpill-f401ce" => Ok(Probe::BlackpillF401CE),
			"blackpill-f411ce" => Ok(Probe::BlackpillF411CE),
			"ctxlink" => Ok(Probe::CtxLink),
			"f072-if" => Ok(Probe::F072),
			"f3-if" => Ok(Probe::F3),
			"f4discovery" => Ok(Probe::F4Discovery),
			"hydrabus" => Ok(Probe::HydraBus),
			"launchpad icdi" => Ok(Probe::LaunchpadICDI),
			BMP_NATIVE => Ok(Probe::Native),
			"st-link/v2" => Ok(Probe::Stlink),
			"st-link v3" => Ok(Probe::Stlinkv3),
			"swlink" => Ok(Probe::Swlink),
			_ => Err(eyre!("Probe with unknown product string encountered")),
		}
	}
}

impl From<&str> for VersionNumber
{
	fn from<'a>(value: &str) -> Self
	{
		// Check what the version string starts with - if it starts with a 'g', it's a GitHash, 'v' is a version,
		// anything else is invalid and unknown.
		if let Some(value) = value.strip_prefix("g") {
			VersionNumber::GitHash(value.to_string())
		} else if let Some(value) = value.strip_prefix("v") {
			// Try to convert the version number into parts
			let version_parts = VersionParts::try_from(value);
			match version_parts {
				// If that succeeds return a fully versioned object
				Ok(version_parts) => VersionNumber::FullVersion(version_parts),
				// Otherwise it's an invalid version, so chuck back an invalid version object
				Err(_) => VersionNumber::Invalid,
			}
		} else {
			VersionNumber::Invalid
		}
	}
}

impl From<&String> for VersionNumber
{
	fn from(value: &String) -> Self
	{
		value.as_str().into()
	}
}

impl From<String> for VersionNumber
{
	fn from(value: String) -> Self
	{
		value.as_str().into()
	}
}

impl Display for VersionNumber
{
	fn fmt(&self, f: &mut Formatter<'_>) -> std::fmt::Result
	{
		match &self {
			VersionNumber::Unknown => write!(f, "<unknown>"),
			VersionNumber::Invalid => write!(f, "<invalid version>"),
			VersionNumber::GitHash(hash) => write!(f, "{}", hash),
			VersionNumber::FullVersion(version_parts) => write!(f, "{}", version_parts.to_string()),
		}
	}
}

impl PartialOrd for VersionNumber
{
	fn partial_cmp(&self, other: &Self) -> Option<Ordering>
	{
		// There's no ordering invalid version numbers, or Git hash only ones beyond equality
		match self {
			Self::Unknown => None,
			Self::Invalid => None,
			Self::GitHash(lhs) => {
				match other {
					// If the other number is also a GitHash, check if they're equal.
					// For everything else, there's no way to compare
					Self::GitHash(rhs) => {
						if lhs == rhs {
							Some(Ordering::Equal)
						} else {
							None
						}
					},
					_ => None,
				}
			},
			Self::FullVersion(lhs) => {
				// If we're a full version though then if the RHS is also a full version, apply full
				// partial comparison - everything else cannot be ordered however.
				match other {
					Self::Unknown | Self::Invalid | Self::GitHash(_) => None,
					Self::FullVersion(rhs) => lhs.partial_cmp(rhs),
				}
			},
		}
	}
}

impl VersionParts
{
	pub fn from_parts(major: usize, minor: usize, revision: usize, kind: VersionKind, dirty: bool) -> Self
	{
		Self {
			major,
			minor,
			revision,
			kind,
			dirty,
		}
	}
}

impl TryFrom<&str> for VersionParts
{
	type Error = Report;

	fn try_from(value: &str) -> Result<Self>
	{
		// The caller already chopped the leading `v` off, so..
		// Start by extracting each of the components, one dot at a time.
		// Look for the first '.' and extract the major version number
		let major_end = value.find('.').unwrap_or(value.len());
		let major = value[..major_end].parse::<usize>()?;

		let mut value = if major_end == value.len() {
			&value[major_end..]
		} else {
			&value[major_end + 1..]
		};

		// Next, find another dot if possible and extract the minor
		let minor_end = value.find('.').unwrap_or(value.len());
		let minor = value[..minor_end].parse::<usize>()?;

		value = if minor_end == value.len() {
			&value[minor_end..]
		} else {
			&value[minor_end + 1..]
		};

		// And one more time - this time for the revision number, and look for a '-'
		let revision_end = value.find('-').unwrap_or(value.len());
		let revision = value[..revision_end].parse::<usize>()?;

		value = if revision_end == value.len() {
			&value[revision_end..]
		} else {
			&value[revision_end + 1..]
		};

		// Now look from the end for another '-', this time to see if the dirty marker is set
		let dirty_begin = value.rfind('-').map(|value| value + 1).unwrap_or(0);
		let dirty = &value[dirty_begin..] == "dirty";
		// If the marker was present, remove it from the string
		if dirty {
			value = &value[..dirty_begin];
			if value.ends_with('-') {
				value = &value[..value.len() - 1];
			}
		}

		// Depending on how much string is left, we need to do different things here..
		// If there is no string left, the kind is a release and we're done
		let kind = if value.is_empty() {
			VersionKind::Release
		} else {
			// More to come? okay.. let's see if this is a release candidate next then
			let candidate = if value.starts_with("rc") {
				let rc_end = value.find('-').unwrap_or(value.len());
				let rc_number = value[2..rc_end].parse::<usize>()?;

				value = if rc_end == value.len() {
					&value[rc_end..]
				} else {
					&value[rc_end + 1..]
				};

				Some(rc_number)
			} else {
				None
			};
			// If there's anything left, we now have Git version information.
			// First comes the number of commits since the tag this is referenced to was made
			if !value.is_empty() {
				// Find the middle '-' and parse the first part as a number
				let commits_end = value
					.find('-')
					.ok_or_else(|| eyre!("Version string has invalid form of Git version tag {:?}", value))?;
				let commits = value[..commits_end].parse::<usize>()?;
				// Now take everything after the '-' as a hash
				let hash = value[commits_end + 1..].to_string();
				VersionKind::Development(GitVersion {
					commits,
					hash,
					release_candidate: candidate,
				})
			} else {
				candidate.map(VersionKind::ReleaseCandidate).unwrap()
			}
		};

		Ok(Self {
			major,
			minor,
			revision,
			kind,
			dirty,
		})
	}
}

#[allow(clippy::to_string_trait_impl)]
impl ToString for VersionParts
{
	fn to_string(&self) -> String
	{
		// Build out the base version string
		let mut version = format!("{}.{}.{}", self.major, self.minor, self.revision);
		// Now flatten out the kind value
		version += &self.kind.to_string();
		// And finally, if the version represents a dirty build, add that before we return
		if self.dirty {
			version += "-dirty";
		}
		version
	}
}

impl PartialOrd for VersionParts
{
	fn partial_cmp(&self, other: &Self) -> Option<Ordering>
	{
		Some(self.cmp(other))
	}
}

impl Ord for VersionParts
{
	fn cmp(&self, other: &Self) -> Ordering
	{
		// First, check to see if the major is larger or smaller than the other
		if self.major < other.major {
			return Ordering::Less;
		} else if self.major > other.major {
			return Ordering::Greater;
		}

		// Next check the minor in the same way
		if self.minor < other.minor {
			return Ordering::Less;
		} else if self.minor > other.minor {
			return Ordering::Greater;
		}

		// Now check the revision number
		if self.revision < other.revision {
			return Ordering::Less;
		} else if self.revision > other.revision {
			return Ordering::Greater;
		}

		// If we got here, the major, minor, and revision numbers all match.. so,
		// we can properly check the ordering on the kind as we're comparing all the same base numbers
		if self.kind < other.kind {
			return Ordering::Less;
		} else if self.kind > other.kind {
			return Ordering::Greater;
		}

		// If the version given is `-dirty`, but other is not, we are a higher version number
		// (and likewise the other way around - other is the higher then). If they're equal,
		// then the version numbers are equivilent.
		if self.dirty && !other.dirty {
			Ordering::Greater
		} else if !self.dirty && other.dirty {
			Ordering::Less
		} else {
			Ordering::Equal
		}
	}
}

#[allow(clippy::to_string_trait_impl)]
impl ToString for VersionKind
{
	fn to_string(&self) -> String
	{
		match self {
			Self::Release => "".into(),
			Self::ReleaseCandidate(rc_number) => format!("-rc{}", rc_number),
			Self::Development(git_version) => git_version.to_string(),
		}
	}
}

impl PartialOrd for VersionKind
{
	/// NB: These orderings are only true IFF we are comparing the same base versions in VersionParts.
	/// a release candidate comes before a release, but development builds come after that release(candidate).
	fn partial_cmp(&self, other: &Self) -> Option<Ordering>
	{
		Some(self.cmp(other))
	}
}

impl Ord for VersionKind
{
	fn cmp(&self, other: &Self) -> Ordering
	{
		match self {
			Self::Release => {
				// A release comes after a release candidate but before its development builds
				match other {
					Self::Release => Ordering::Equal,
					Self::ReleaseCandidate(_) => Ordering::Greater,
					Self::Development(_) => Ordering::Less,
				}
			},
			Self::ReleaseCandidate(lhs) => {
				// A release candidate comes before a release and its development builds, but candidates
				// a strongly ordered relative to each other for a given release
				match other {
					Self::Release => Ordering::Less,
					Self::ReleaseCandidate(rhs) => lhs.cmp(rhs),
					Self::Development(_) => Ordering::Less,
				}
			},
			Self::Development(lhs) => {
				// Development builds come after everything else, but are strongly ordered relative to each other
				match other {
					Self::Development(rhs) => lhs.partial_cmp(rhs).unwrap(),
					_ => Ordering::Greater,
				}
			},
		}
	}
}

impl GitVersion
{
	pub fn from_parts(release_candidate: Option<usize>, commits: usize, hash: String) -> Self
	{
		Self {
			release_candidate,
			commits,
			hash,
		}
	}
}

#[allow(clippy::to_string_trait_impl)]
impl ToString for GitVersion
{
	fn to_string(&self) -> String
	{
		let base_version = match self.release_candidate {
			None => "".into(),
			Some(rc_number) => format!("-rc{}", rc_number),
		};
		let git_version = format!("-{}-{}", self.commits, self.hash);
		base_version + &git_version
	}
}

impl PartialOrd for GitVersion
{
	fn partial_cmp(&self, other: &Self) -> Option<Ordering>
	{
		// Check if this is part of a release candidate-based Git version build
		match self.release_candidate {
			Some(lhs_rc_number) => {
				// It is, so check to see what the other Git version build represents
				match other.release_candidate {
					// If they're both release candidates, start by checking if they're based on the
					// same candidate (if they're not, we're done already)
					Some(rhs_rc_number) => {
						if lhs_rc_number != rhs_rc_number {
							return lhs_rc_number.partial_cmp(&rhs_rc_number);
						}
					},
					// Otherwise, if the other is a release, we're already done -
					// release candidates come before releases
					None => return Some(Ordering::Less),
				}
			},
			None => {
				// It is not a release candidate, so check the other to see what that is
				if other.release_candidate.is_some() {
					// If thee other is a release candidate, we're done - RC's come before releases
					return Some(Ordering::Greater);
				}
				// Otherwise both represent the same base release, continue
			},
		}

		// If the release candidate logic all passed then we should check how many commits different
		// the two are and if the hashes match
		if self.commits < other.commits {
			Some(Ordering::Less)
		} else if self.commits > other.commits {
			Some(Ordering::Greater)
		} else if self.hash == other.hash {
			Some(Ordering::Equal)
		} else {
			None
		}
	}
}