Posts Tagged ‘recover chip hex’

PostHeaderIcon Recover Chip PIC12C509A Firmware: A Stack Two Levels Deep Is a Test, Not a Limitation

Every architecture has one number that constrains how code is written and, read backwards, constrains how a recovered image can be checked. On the PIC12C509A that number is two. The hardware stack holds two return addresses, a call from inside a called routine is legal but a call from inside that one is not, and the compiler or the assembly author who wrote the firmware knew it every time they wrote a CALL.

That makes the stack a free verification instrument. Once the image is out, the call graph can be built from the instruction stream, and any path that needs three levels is evidence of a bad read rather than evidence of a firmware bug. Very few parts offer so clean a test.

The rest of the device is unremarkable in the best sense: 1024 twelve-bit words of one-time-programmable program memory arranged as two pages of 512, 41 bytes of RAM, six I/O pins in an eight-pin package, thirty-three instructions, a watchdog, and a factory-trimmed internal oscillator whose calibration word sits at the top of the map.

The part in numbers

item PIC12C509A
program memory 1024 by 12 bits, OTP
pages 2, selected by PA0 in the status register
return stack 2 levels, not readable by software
data RAM 41 bytes
I/O 6 pins, one input-only
interrupts none
entry points exactly one, address zero
calibration word top program address

The stack row deserves emphasis because it is not observable from inside the part. There is no register that reports the stack pointer, no overflow flag, and no instruction that pushes or pops for data purposes. A program that overflows it does not trap; it wraps, and the return goes to whatever address was there two calls ago.

What a depth-three path means in a dump

When a recovered image shows a chain of calls three deep, exactly three explanations exist, and they can be separated.

The first is that the read is wrong at one instruction. A single bit error can turn a MOVF into a CALL, which manufactures a nesting level that the firmware never had. The check is positional: if the offending word sits at an address whose neighbours are inconsistent with it, the word is corrupted.

The second is that a computed jump was disassembled as a call. The twelve-bit core jumps with GOTO into the middle of a routine for skip tables, and a tool that mislabels the target as a subroutine entry will build a chain that does not exist in the machine.

The third is that the code is executing data. This is the interesting one and it is common on parts this small, because a 41-byte RAM budget pushes designers to store lookup tables in program memory and read them with CALL and RETLW. Those tables are legitimate and they are reached by call, but they are one level deep by construction. A table reached by call from inside a routine that was itself called is at level two, which is fine; a table reached from a table is impossible.

Data that decodes as a call

The RETLW table is the idiom that makes a 12-bit part feel larger than it is. A block of words, each one RETLW with a different literal, is indexed by adding an offset to the program counter and calling into the middle of the block. The return value is the literal.

For a disassembler this block is a trap in both directions. Read as code it is a subroutine that always returns a constant and never falls through, which looks plausible. Read as data it is a table of byte values, which is what the firmware intended. Telling the two apart needs the surrounding code: the CALL that reaches the block usually has its address computed a few instructions earlier from an index register.

The practical consequence for a delivery is that a raw disassembly of this part should not be presented as source. It should be presented as an image plus a listing, with the table blocks marked as tables. Customers who receive a flat listing where the tables are rendered as forty consecutive subroutines tend to conclude the recovery was scrambled.

Two pages and one status bit

1024 words is two pages of 512, and the page is chosen by a single bit in the status register. The bit is not restored automatically after a call returns, which is the detail that catches hand-written code and catches disassemblers even more often: a routine that switches page, calls within the new page and returns leaves the caller in the wrong page unless the caller put it back.

The stack and the page interact in exactly one way worth knowing. A CALL at the last address of page zero stores the return address correctly and pushes the programme counter into page one or into the call target, depending on how the bit was set. There is no hardware protection against a call whose target page does not match the bit. If the recovered image shows a call into the upper half and the page bit was clear at that point, either the bit was set by an instruction the disassembly skipped, or the word is a misread.

Using the constraint as a check

A verification pass on this part needs three steps and no simulator.

Build the call graph from the reset vector, following GOTO as a jump and CALL as an edge, and stopping at RETLW and RETURN. Record the maximum depth reached on any path. If it exceeds two, the image has a false edge, and the address of that edge is where to re-read first.

Then check that every CALL target lands inside 1024 words and, on the assumption that the design is ordinary code, that the target is a plausible entry rather than a word in the middle of a table. Entry points in hand-written assembly tend to be preceded by a RETLW or a GOTO, which is a weak signal but a useful one.

Finally check address zero. With one entry point and no interrupts, the reset vector must decode to a GOTO that lands in the lower page, or to a MOVLW that begins an oscillator-trim sequence. An image that fails this test is off by an offset, and no amount of downstream analysis repairs it.

For the sibling with half the array and no page bit, PIC12C508A firmware reversal shows where the simplification helps. For the same core in a twenty-pin package where paging runs to four pages, PIC16C57C firmware carries the argument further. And for a mid-range part where the stack is eight levels and the constraint disappears entirely, PIC16F72 secured data is the contrast.

FAQ

Does the part trap on stack overflow? No. The two-level stack is a ring of registers with no detection logic. An overflow returns to the wrong address and the program continues from there.

Can the stack contents be read? Not through any instruction or programming command. The return addresses are only observable indirectly, by watching where a program goes.

Why does a recovered listing show more subroutines than the firmware has? Because lookup tables written with RETLW are reached by CALL and look exactly like subroutines. They are data with a calling convention.

The short version

Two levels of stack, one entry point, one page bit, and a calibration word at the top. The stack is the free test: build the call graph, and any path that reaches three levels is a bad read rather than a bad design. Mark the RETLW tables as tables, verify address zero, and this part verifies more cleanly than most parts ten times its size.