Posts Tagged ‘recover mcu locked hex’

PostHeaderIcon ATmega2560: RAMPZ, ELPM, and the Tables That Live Above 64 KB

A 256 KB flash array sounds like one memory, and for instruction fetch it largely behaves like one. For constant data it does not. The ordinary load-from-program-memory instruction takes a sixteen-bit pointer, which reaches sixty-five thousand five hundred and thirty-six bytes — a quarter of the array. Everything above that is reachable, but only through a different instruction and only if a particular register holds the right value.

That asymmetry is the single most common reason a recovered image appears to be missing its tables. The tables are there. The code that reads them is using an address form that a quick disassembly has rendered as something else.

Two limits that are not the same limit

Instruction fetch and constant data access are separate paths with separate widths, and on this part they diverge:

path pointer width reach limitation
instruction fetch program counter, extended to 17 bits the whole 128K words indirect jumps need an extension register
ordinary constant load 16-bit register pair 64 KB of bytes, a quarter of the array cannot see the upper three quarters
extended constant load register pair plus a high-byte register the whole array the high-byte register must be correct

The middle row is the trap. A design with a font, a waveform table or a large string set placed in the upper part of the array reads that table with the extended instruction, and a disassembly that does not recognise the idiom will show the table as unreferenced and the reader will conclude it was never used.

The high-byte register is global state

The register that supplies the upper address bits for the extended load is not part of the pointer. It is a single global register that stays where it was last written. That produces a code pattern with no equivalent on smaller parts: before every extended load, the register is set, and after the load, if the code is careful, it is restored.

Careful is the operative word. A routine that performs an extended load and returns without restoring the register will corrupt the next extended load performed by its caller. The bug is real, it is common in hand-written assembly, and it appears in recovered images as a routine that sets the register at entry and never restores it.

For a reader the practical consequence is that the register’s value at any given instruction cannot be determined locally. It has to be tracked along each path, which is exactly the kind of analysis a disassembler does not do by default.

Interrupts make it worse, and the code knows

An interrupt can occur between setting the register and using it. If the service routine also performs an extended load, it overwrites the value and the interrupted code resumes with the wrong high byte — reading a table address in the wrong bank, which usually means reading plausible-looking bytes from somewhere else entirely.

The fix is to save and restore the register in the service routine, and that save appears in the image as a pair of instructions around the handler’s prologue that look redundant until the mechanism is understood. Seeing them is a good sign: it means the firmware was written by somebody who understood the part.

symptom in the field mechanism what to look for in the image
occasional wrong lookup values under interrupt load high-byte register clobbered by a handler handlers that omit the save and restore
a table that reads correctly in a test loop and wrongly in production same, with the test loop not enabling interrupts the enable instruction’s position relative to the table read
data that looks shifted by a fixed offset wrong bank selected, reading a neighbouring region the constant written to the register before the load

Three-byte pointers in the data structures

Constant pointers on this part are three bytes wide, not two, because the address space needs eighteen bits. That shows up in structures stored in flash or in SRAM: arrays of pointers are three bytes per entry, and a structure containing a program-memory pointer has different padding from the same structure on a 128 KB part.

Reading such a table by hand is straightforward once the width is known and hopeless if it is assumed to be two. The characteristic error is an array that decodes as alternating sensible and nonsensical values, which is what happens when a two-byte stride is applied to three-byte records: every third entry lands correctly.

The routine that reads the whole array

A bootloader that verifies the application before jumping to it has to read all 256 KB, and it cannot do that with the ordinary load. Such a routine is often the largest single user of the extended instruction in the image, and it is a useful landmark because it walks the address space linearly with the high-byte register incrementing at each 64 KB boundary.

Finding it answers a question that matters for a recovery: whether the device would have refused to run a modified image. A design with a checksum over the whole array will not boot after a partial write, which is why some «dead» units are not dead at all.

What the deliverable states

The return set is the flash image at the correct length, the EEPROM separately, the fuse and lock state as read, a disassembly in which the extended load is annotated with the bank it selects, and a map of where the large tables sit relative to the 64 KB byte boundary.

That map is the part of the work that prevents a wrong conclusion downstream. An engineer who knows that the font lives at an address requiring the extended load will not spend a day looking for where it is read.

The control-flow side of the same part — the extension register for indirect jumps and the three-byte return address — is covered in ATmega2560V firmware recovery. The part that stops one size below this boundary is in ATmega1280V program recovery, and the same table-versus-code distinction on a smaller array in ATtiny261 code recovery.

FAQ

Why can the ordinary load only reach a quarter of the array? Because it takes a sixteen-bit byte address, and the array is 256 KB. The extension register supplies the bits above sixteen.

Do the tables disappear in a bad read? No. They are present in the image. What fails is the disassembly’s recognition of how they are addressed.

Is the high-byte register saved by the compiler? Compilers that support this part do save and restore it around functions that use the extended load. Hand-written assembly often does not.

Conclusion

On a quarter-megabyte array the interesting problems are on the data side. One register holds the high address bits, it is global state, interrupts can clobber it, and pointers that reach the upper part of the array are three bytes wide. Read those four facts into the disassembly and the missing tables reappear where they always were.

PostHeaderIcon ATtiny861V: The Internal Reference That Drifts With the Rail

Every ADC answer is a ratio: the input voltage divided by a reference. Change the reference and every reading changes with it, proportionally and silently. On a low-voltage part the available references include the supply itself, which by definition moves.

This is not a recovery problem in the narrow sense. It is the reason a recovered image can be byte-perfect and still produce a device that measures wrongly, and the reason a firmware’s constants have to be understood rather than copied.

The reference choices and what they imply

Reference Depends on supply Typical use
Supply voltage yes, directly Ratio measurements, battery monitoring
Internal fixed reference weakly Absolute measurements
External reference on a pin no Precision work

The first row is more common than it looks, because measuring the supply against itself is exactly how a battery monitor works: the reading is a ratio and the ratio is what matters. In that design, a drifting reference is not an error, it is the measurement.

The second row is where trouble appears. A firmware that switches to the internal reference to measure an absolute voltage assumes that reference is stable. It is more stable than the supply and it is not perfectly stable, particularly at the bottom of a low-voltage part’s range.

What this means for a recovered image

Two patterns appear in firmware that uses the ADC, and they have different implications:

Ratios throughout. The code compares a reading against a threshold expressed in counts. There is no absolute voltage anywhere, and the design is insensitive to the reference. Reproduction is straightforward because the constants are dimensionless.

Absolute values. The code converts counts to millivolts with a constant, then acts on the result. That constant encodes both the reference value and the divider ratio, and it is only correct at the reference the designer assumed.

Telling the two apart is straightforward: a ratio design has thresholds like 512 or 700, an absolute design has constants in the hundreds or thousands with a multiplication nearby.

Why the V suffix changes the arithmetic

A low-voltage part is specified to run down to a rail where a standard part stops. At that rail the internal reference may be at the edge of its specified accuracy, and the ADC’s own linearity degrades as the input approaches the rails.

A design built and validated at three volts and deployed at 1.8 is therefore working with a different error budget, even though nothing in the firmware changed. The constants are the same; the physics is not.

That is a real and frequently observed field failure, and it arrives as “the readings drift when the battery is low” rather than as anything resembling a software fault.

Reading the analogue path out of a dump

The analogue configuration is a handful of register writes, all of which appear early and all of which are identifiable:

  • Reference selection.
  • Prescaler, which sets the conversion clock and therefore the accuracy.
  • Channel selection, which implies which pins are inputs.
  • Conversion start and completion polling, which reveals whether the code blocks or uses an interrupt.

The prescaler is worth attention. An ADC clock that is too fast for the part’s specification produces readings that are noisy rather than wrong, and the noise looks like sensor behaviour. A recovered design with an aggressive prescaler is a design that traded accuracy for speed, and that is a fact about the product rather than about the recovery.

What a flash image cannot tell you

It cannot tell you what the reference actually was on the bench. The register setting says which source was selected; the accuracy of that source at that voltage and temperature is a property of the silicon, not of the code.

So a reconstruction that reproduces the code faithfully will reproduce the design’s intent, not its accuracy. If the product’s accuracy matters, the constants have to be re-derived against the actual reference — which is a measurement, not a disassembly.

Practical order

  1. Signature, fuses, lock byte.
  2. Array read, verified.
  3. Locate the analogue configuration block.
  4. Classify the design as ratio-based or absolute.
  5. If absolute, record the constant and the reference assumption as a pair.

Step 5 is the one that makes the reconstruction usable. A constant without its reference assumption is a number that will be copied into a design and be wrong by an amount nobody can calculate.

Related notes

FAQ

Is the internal reference accurate enough to rely on? It is specified to a tolerance that is adequate for many purposes and not for precision measurement.

Does the reference change with temperature? Yes, and the datasheet gives a curve rather than a single number.

Can a recovered constant be used as-is? Only if the reference it assumed matches the reference in the new design.

Why do readings drift as the battery drains? Because either the reference or the input divider is supply-dependent, or both.

Conclusion

The analogue path is where a faithful reconstruction most easily produces a wrong device, because the error is a ratio rather than a bug. Classifying the design as ratio-based or absolute, and recording each constant with the reference it assumed, is what keeps a byte-perfect image from becoming a product that measures confidently and incorrectly.

PostHeaderIcon ATtiny261V: Reading a Marginal Array Without Fooling Yourself

A read either succeeds or fails, except when it does neither. The interface completes, the buffer fills, and a handful of bytes are wrong in a way that is invisible until something downstream refuses to assemble.

On a low-voltage part the margin for that failure is narrower than on its standard sibling, and the mechanism is worth understanding because the fix is procedural rather than technical.

Why the margin exists at all

Three independent tolerances combine:

The target’s oscillator. The serial interface is synchronous, but the target samples on edges derived from its own clock. A slow oscillator eats sampling margin.

The supply. Sense amplifiers resolve a cell against a reference. A low rail narrows the difference they have to detect.

The programmer’s clock rate. Higher rates leave less time for the bus to settle before sampling.

None of the three is at its limit normally. Put all three near their limits at once and the result is bytes that resolve differently on consecutive reads.

The single test that catches it

Read every page twice and compare. That is the whole method, and it is sufficient because a marginal read is not stable — it depends on noise, temperature, and the exact timing of the preceding operation, none of which repeat identically.

Two reads Interpretation Action
Identical Margin adequate; treat as data None
Differ in a few bytes Margin inadequate Lower clock, raise supply, retry
Differ everywhere Interface fundamentally wrong Check wiring and clock source
Second read empty Entry lost between passes Check reset line, socket contact

The second row is the one that saves the most time, because the natural reaction to a single differing byte is to assume damage. It is almost always margin.

The compare must be per page

A whole-file comparison tells you the read was unstable. A per-page comparison tells you which page, and that localises the cause: one scattered page suggests a contact issue, a contiguous run suggests a supply sag during the middle of the session, and the last few pages suggest the supply collapsed at the end.

The pattern is more informative than the count, and it is free once the comparison is per page.

Erase, and the same margin problem

Erase loads the supply in a way reads do not. A charge pump drives the array, current is drawn in bursts, and at the low end of a V part’s range the rail dips.

The consequence is a partially erased page that no command reports. The check is to read the array back after the erase and confirm it is uniformly FF. That single step validates the supply, the socket, and the pump at the moment of peak load — the most demanding test available without instruments.

Skipping it means the first sign of trouble appears later, as a page that will not program correctly, with no record of why.

What to record with the image

A recovered image without context is a file. Four items make it evidence:

  • The signature, which identifies the member and therefore the size of the map.
  • The fuse byte, which explains clock behaviour and interface availability.
  • The lock byte, which records the protection state as the hardware reported it.
  • The number of pages that required a second read to verify.

The fourth is unusual and useful. It is a direct measurement of how marginal the session was, and it tells the next person whether to trust the file without re-reading.

The order that avoids rework

  1. Power at the design rail, measured at the socket, with local decoupling.
  2. Signature and fuses before memory.
  3. Lock byte.
  4. Page-by-page read, each page twice, logging any page that needs a retry.
  5. Post-erase verification if an erase is part of the plan.

Steps 2 and 3 cost seconds and answer the questions that determine whether steps 4 and 5 are even possible. Doing them last is the most common procedural error on a small AVR, and it is expensive because the conclusions drawn from an unverified image propagate.

Related notes

FAQ

Is a single wrong byte a sign of a damaged die? Almost never. It is a sign of inadequate margin, and re-reading at a lower clock usually resolves it.

Does reading twice damage anything? No. Reads do not wear the array.

Why verify after an erase? Because erase is the operation that loads the supply most, and a partial erase is silent.

Can the margin be measured rather than assumed? Indirectly, by counting how many pages need a retry. That count is the measurement.

Conclusion

A marginal read is the most expensive failure because it looks like success. Comparing each page against itself, logging the retries, and verifying after any erase converts a silent risk into a recorded measurement — and on a low-voltage part, that record is the difference between an image that can be trusted and one that merely looks complete.