Posts Tagged ‘microcontroller locked code recovery’

PostHeaderIcon Recovery Microcontroller PIC16F872 Program: Reachability Is the Program

Two kilobytes of words looks like a manageable image until you notice that not all of it is program. Some of it is constant data that the instruction stream reads but never executes, some is the residue of an earlier revision that nobody deleted, and some — on a part with computed jumps — is code that only a table can reach. Telling those apart is what turns a dump into a program.

The PIC16F872 is a twenty-eight-pin flash part with 2048 by 14 bits of program memory, 128 bytes of data EEPROM, and a ten-bit converter with five channels. It is the model of a mid-range controller from the era when a design might run for fifteen years and accumulate four firmware revisions, each leaving something behind.

The reachability graph

The analysis starts at word zero and follows every control transfer.

Transfer type How to follow it Risk
call or goto with an absolute target direct none
conditional skip followed by a jump both paths none
computed jump through a table requires resolving the table an unresolved entry looks unreachable
jump through a modified program counter requires data-flow analysis the usual source of error
return from a table returns to the caller, not a new block misread as an entry point

The third and fourth rows are where naive tools fail. On this core, an indexed jump is done by adding an offset to the program counter register, which means the target is computed from a value that may have been loaded several instructions earlier. A disassembler that does not track that value treats the jump as falling through, and the entire block it reaches is then marked unreachable.

Three kinds of unreachable code

Once the graph is built, the unvisited regions fall into recognisable classes, and each means something different.

Removed features. A block that begins with a test of a flag nobody sets, followed by a complete routine. It was a feature that was disabled rather than deleted, usually because the customer might want it back.

Superseded revisions. Two routines doing the same thing, one called and one not. The dead one is the earlier attempt; the live one is the fix. Comparing them often reveals what the bug was.

Unresolved computed entries. A block reached only by a table whose entries the analysis did not resolve. This is not dead code, it is a failure of the analysis, and it has to be closed before the other two classes can be trusted.

The ordering matters. Classify the third first, because every unresolved entry contaminates the classification of everything it reaches.

Resolving computed jumps

Three patterns cover almost all of them on this part.

A table of retlw instructions read through a call, with the value used as data or as an offset into a second table. The first table is constants; the second is a jump table.

A block that loads a literal into the working register, adds an offset, and moves the result into the program counter. The added offset is a base address, and the resulting target is a small range of addresses immediately after the addition.

A state machine dispatch, where the state variable indexes a table of goto instructions. This one is easy to resolve because the state variable is written with small literals in a small number of places.

Each of these has a signature in the listing, and once they have been resolved the graph becomes dense: on a typical design of this vintage, fewer than one word in twenty remains unreachable.

Why the converter matters for the reading

Five channels of ten-bit conversion is the reason this part was chosen for sensor products, and the conversion logic is a reliable guide to what the code is doing.

The channel selection written at initialisation states how many sensors the product has and which pins they use. The sampling routine — a delay after channel selection, then a conversion start, then a wait — appears once and is called from everywhere, so it is a good anchor for identifying the code’s organisation.

The scaling arithmetic after the conversion is where the product’s range lives, and it is usually a shift-and-add sequence with one or two literals. Reading those literals gives the counts-to-units conversion without needing the sensor’s datasheet.

Where the graph goes wrong

Three failure modes are worth checking against any reachability result before it is published.

A jump into the middle of an instruction. On a core where instructions are one word each this is possible if the target is computed from a value that was not what the analysis assumed. The symptom is a block that decodes into instructions which reference registers the design never uses.

A table whose entries point outside the array. That is either a transcription error in the table or a dispatch that the analysis resolved with the wrong base address, and both are worth resolving rather than classifying as unreachable.

A region that reads as code but is never targeted and contains no return. Functions end with a return; data does not. A run of instructions with no return at the end is either a block that falls through into the next one — which means it is not a separate function — or a block whose terminator was misread.

Each of those is cheap to check and each of them, left unchecked, turns into a wrong statement in a delivery.

Delivering a program, not an image

Artefact Content Who uses it
image full array, stated convention production
call graph every routine with its callers engineering
reachability map reachable, residue, unresolved engineering
channel and scaling note inputs, ranges, units systems
configuration decoded production and engineering

The third item is the one that makes the second trustworthy. A call graph without a reachability statement presents dead code as if it were live, and an engineer who changes a routine that is no longer called will wonder why nothing happened.

Where a forty-pin part in the same family shows how the extra pins are used, PIC16C662 heximal reads the port structure. Where the sibling without the converter requires a different reading, PIC16F870 binary covers the case. Where units of one design from different production years disagree, PIC16F871 software explains why the difference is usually revision rather than damage. And where the file packing rather than the graph is the risk, PIC16C642 binary deals with offsets.

The short version

The program is the reachable part. Build the graph from word zero, resolve the computed jumps first, classify what remains as feature-removed or revision-residue, and deliver the map alongside the image so nobody edits dead code.