Posts Tagged ‘copy mcu source code’

PostHeaderIcon Copy Failures: Four Causes That Are Not in the Code

The image is byte-identical to the original. The new device boots, initialises, and does roughly what it should. Then it reads two percent high, or its clock is five percent off, or it refuses to authenticate, or a feature works on one unit and not the next.

This is the standard outcome of copying code without copying the device state that the code depends on. The code is a program; the device is a program plus a set of values that were established at manufacture and that the program reads rather than computes.

The four dependencies outside the image

Four things outside the program determine whether a copy behaves like the original.

Calibration values are measured per unit at test and written into the device. They compensate for the tolerance of the internal oscillator, the offset of the analog comparator, the gain error of the ADC and the reference voltage. Without them, the device runs at the nominal frequency rather than its own, and every analog measurement inherits an error that was designed out.

A unique identifier is burned into a protected area and is different on every unit. Firmware uses it to derive keys, generate serial numbers, bind a licence, or enforce a one-device-per-licence rule. A copy that carries the original’s firmware but not the original’s ID will either fail the derivation or, worse, present the original’s identity.

Dependency Where it lives Written by
Calibration A dedicated flash page or OTP Factory test
Unique ID Protected OTP or a system area Wafer or package test
Option bytes A configuration area Production programmer
Revision-specific code The application image itself The compiler and the developer

The fourth row is different in kind: it is in the image, but it is conditional on which silicon revision is present. A silicon bug that the code works around is a bug that the workaround only fixes on the revision it was written for, and a copy onto a different revision can behave differently with identical code.

The symptom map

The value of the four-way split is that each cause produces a recognisable symptom, and the symptom points at the cause.

A device that runs at the wrong frequency, or whose UART baud rate is wrong, or whose timing loops are consistently off by a few percent, is missing its oscillator calibration. The error is systematic across units because it is the nominal-versus-actual difference of the internal oscillator, which is several percent on a part without a crystal.

A device whose analog readings are offset or scaled is missing its ADC calibration. The error is linear and repeatable, which distinguishes it from a noise problem.

Symptom Most likely cause Test
Frequency or baud rate off by percent Oscillator calibration Measure a clock output
Analog readings offset or scaled ADC calibration Apply a known reference
Authentication fails, serial number wrong Unique ID Read the ID from both units
Boots to the wrong mode Option bytes Read the configuration area
Works on one unit, not another Silicon revision Compare device revision codes

The third row is the one that produces the most confusing reports, because the device appears to work and the failure happens elsewhere — at a server, in a licensing check, in a log. A device that presents the original serial number is a device that has copied an identity, and the consequences extend beyond the hardware.

Finding the missing dependency

The diagnostic method is comparison, and it requires two devices of the same type: the original and a blank.

Reading the two devices’ full address spaces and subtracting them yields the regions that differ. On two blank parts of the same type there are usually a handful of differences, all in a small region near the top of the address space: the calibration page, the identifier and the option bytes.

Region Typical offset Contents
Calibration Top of flash or a dedicated page Oscillator trim, ADC gain and offset
Unique identifier A fixed system address 64 to 128 bits, different per unit
Option bytes A configuration block Clock source, BOR, boot mode
Device signature A read-only location Part number and revision

The fourth row is not a dependency to copy but a fact to read, and reading it is what prevents the fourth cause. A device that reports a different revision from the one the image was written for may need a different workaround, and the difference is not visible in the code.

Once the differing regions are identified, the copy procedure includes them. That means reading the original’s calibration and writing it to the new device, which works only if the new device’s analog characteristics are close enough that the original’s trim values are valid — true for parts from the same batch and not necessarily true across batches.

Why the batch matters

The calibration values are valid only within the range over which the underlying characteristics vary, and that range is set by process variation. Parts from the same wafer differ from each other by a little; parts from different lots differ by more.

This is why a copy that works on the engineer’s bench can fail in production. The bench unit happens to be close enough to the original that the original’s trim values are within range. The production units come from a later lot, and the same trim values push the oscillator outside its specified tolerance.

Source of parts Calibration transfer Expected outcome
Same lot, adjacent wafers Usually valid Works
Same lot, different wafers Often valid Works, with a check
Different lot Marginal Some units out of specification
Different revision Invalid Systematic error

The practical rule is that a copied calibration is a hypothesis to be tested per lot, not a value to be trusted. The test is one measurement of the clock frequency and one of an analog channel, on a sample from each lot, which is an hour of work and prevents a production run that has to be reworked.

When the values cannot be copied

Three situations make the copy impossible rather than merely incomplete.

The calibration is stored in a region that cannot be written. Some parts protect the factory page against any write, including by the debug interface, and a blank part’s factory page contains the blank part’s own calibration rather than the original’s. Copying the original’s values is then not permitted by the hardware.

The identifier is used as a key input. If the firmware derives an encryption key from the unique ID, then a copy onto a different ID produces a different key and the encrypted data on the device cannot be read. This is a deliberate anti-copy design and it works.

The revision differs and the code has no workaround for the new one. This is not a copying failure at all but a compatibility failure, and it appears as a behaviour that the original never exhibited.

Obstruction Result Workaround
Factory page write-protected Original trim unavailable Re-trim the new unit
ID used as key input Data unreadable on the copy Key extraction from the original
Revision mismatch Behaviour differs Patch the workaround
Batch variation Trim values out of range Characterise and re-trim

The first row’s workaround is the one that turns a copy into a small production process: measure each new unit’s oscillator and ADC against a reference and write its own trim values. That is what the factory does, and doing it manually for a handful of units is a few hours of bench work.

What a copy engagement actually involves

The image is the easy part. The engagement is the image plus the four dependencies plus a test that confirms the copy behaves within specification.

The test is the deliverable that customers remember, because it is the one that catches the failures. It consists of the measurements that the dependencies affect: a clock output for the oscillator trim, a known reference for the ADC, an authentication exchange for the identity, and a boot-mode check for the option bytes.

Check Instrument Pass criterion
Clock frequency Frequency counter on a clock pin Within the datasheet tolerance
Analog accuracy Reference voltage at two points Within the specified linearity
Identity Read the ID and the serial number Matches the intended value
Boot mode Observe the startup behaviour Same as the original

Where this fits

The copy work sits downstream of the extraction, and it depends on the region map from finding the code to know which regions are program and which are factory data.

Where the missing dependency is the identifier and it is used as a key, the route becomes the key recovery described in where the secret key lives, because copying is no longer sufficient. Where the goal is a production run rather than a handful of units, the additional sources of distortion in volume are set out in copying an IC program, and the full list of conditions a clone must satisfy is in cloning IC firmware.

What we will not do is hand over an image and call the job complete. An image without the calibration page is a program that runs at the wrong speed, and the customer discovers it after the boards are built.