From b9d286eb4344e219c933c0ec2614305b1b1b8664 Mon Sep 17 00:00:00 2001 From: Nav Date: Mon, 7 Jun 2021 00:15:25 +0100 Subject: [PATCH] AVR8 signature extraction and validation (from TDFs) --- .../AVR8/Avr8TargetDescriptionFile.php | 33 +++++++++++++++++++ .../TargetDescriptionFiles/AVR8/Signature.php | 21 ++++++++++++ 2 files changed, 54 insertions(+) create mode 100644 build/scripts/TargetDescriptionFiles/AVR8/Signature.php diff --git a/build/scripts/TargetDescriptionFiles/AVR8/Avr8TargetDescriptionFile.php b/build/scripts/TargetDescriptionFiles/AVR8/Avr8TargetDescriptionFile.php index 88886113..75a196d9 100644 --- a/build/scripts/TargetDescriptionFiles/AVR8/Avr8TargetDescriptionFile.php +++ b/build/scripts/TargetDescriptionFiles/AVR8/Avr8TargetDescriptionFile.php @@ -4,6 +4,7 @@ namespace Bloom\BuildScripts\TargetDescriptionFiles\Avr8; use Bloom\BuildScripts\TargetDescriptionFiles\TargetDescriptionFile; require_once __DIR__ . "/../TargetDescriptionFile.php"; +require_once __DIR__ . "/Signature.php"; class Avr8TargetDescriptionFile extends TargetDescriptionFile { @@ -20,6 +21,7 @@ class Avr8TargetDescriptionFile extends TargetDescriptionFile const AVR8_PHYSICAL_INTERFACE_UPDI = 'UPDI'; const AVR8_PHYSICAL_INTERFACE_DEBUG_WIRE = 'debugWire'; + public ?Signature $signature = null; public ?string $family = null; public array $debugPhysicalInterface = []; @@ -109,6 +111,29 @@ class Avr8TargetDescriptionFile extends TargetDescriptionFile $this->debugPhysicalInterface[] = self::AVR8_PHYSICAL_INTERFACE_JTAG; } + $signaturePropertyGroup = $this->propertyGroupsByName['signatures'] ?? null; + if (!empty($signaturePropertyGroup)) { + $this->signature = new Signature(); + + if (isset($signaturePropertyGroup->propertiesMappedByName['signature0'])) { + $this->signature->byteZero = $this->rawValueToInt( + $signaturePropertyGroup->propertiesMappedByName['signature0']->value + ); + } + + if (isset($signaturePropertyGroup->propertiesMappedByName['signature1'])) { + $this->signature->byteOne = $this->rawValueToInt( + $signaturePropertyGroup->propertiesMappedByName['signature1']->value + ); + } + + if (isset($signaturePropertyGroup->propertiesMappedByName['signature2'])) { + $this->signature->byteTwo = $this->rawValueToInt( + $signaturePropertyGroup->propertiesMappedByName['signature2']->value + ); + } + } + $progAddressSpace = $this->addressSpacesById['prog'] ?? null; if (!empty($progAddressSpace)) { $flashMemorySegment = $progAddressSpace->memorySegmentsByTypeAndName['flash']['flash'] @@ -344,6 +369,14 @@ class Avr8TargetDescriptionFile extends TargetDescriptionFile { $failures = parent::validate(); + if (is_null($this->signature) + || is_null($this->signature->byteZero) + || is_null($this->signature->byteOne) + || is_null($this->signature->byteTwo) + ) { + $failures[] = "Missing or incomplete AVR signature."; + } + if (is_null($this->debugPhysicalInterface)) { $failures[] = 'Target does not support any known AVR8 debug interface - the TDF will need to be deleted.' . ' Aborting validation.'; diff --git a/build/scripts/TargetDescriptionFiles/AVR8/Signature.php b/build/scripts/TargetDescriptionFiles/AVR8/Signature.php new file mode 100644 index 00000000..bd88ad85 --- /dev/null +++ b/build/scripts/TargetDescriptionFiles/AVR8/Signature.php @@ -0,0 +1,21 @@ +byteZero) || is_null($this->byteOne) || is_null($this->byteTwo)) { + throw new \Exception("Cannot generate hex string of incomplete AVR8 target signature."); + } + + return '0x' . substr('0' . dechex($this->byteZero), -2) + . substr('0' . dechex($this->byteOne), -2) + . substr('0' . dechex($this->byteTwo), -2) + ; + } +}