Posts Tagged ‘decrypt locked mcu data’

PostHeaderIcon Decrypt Locked MCU LPC2103FBD48 Heximal: The Word at 0x14

Every recovery job has a moment where the content is in hand, the file looks correct, and the device still does nothing. On an ARM7 part from this family, that moment is almost always the same four bytes.

The LPC2103FBD48 has eight exception vectors at the bottom of the array. Seven of them are addresses the core will jump to when something happens. The eighth, at 0x00000014, is reserved, and the boot loader uses it as a checksum slot: the sum of all eight words must be zero for the image to be considered valid. Get the other seven right and the eighth wrong, and the loader concludes that no program exists.

Why a boot loader needs an opinion about your code

The loader has to decide, in the first few hundred microseconds after reset, whether to start the application or to wait for a command on the UART. There is no configuration pin for that decision and no flag stored elsewhere; the flash itself has to carry the answer.

A blank array is unambiguously invalid, because erased flash reads as all ones. The problem is a partly written array — a device that lost power during programming, or one whose first sector was corrupted. Those bytes look plausible. A checksum over the vector table is a cheap test that catches the common failure and costs the loader nothing but eight additions.

The rule is arithmetic: the eight words from 0x00 to 0x1C must sum to zero. Since seven of them are addresses the compiler chose, the eighth is whatever makes the total wrap to zero. Any build that produces a bootable binary writes that word automatically, and any process that rebuilds the table by hand must compute it.

What the failure looks like

The symptom is quiet. Power on the board and nothing happens on the application pins, no banner appears on the port the product normally speaks on, and current draw sits at the idle figure instead of rising through the boot sequence.

A debugger attached with the core halted sees an image that looks entirely reasonable: a plausible reset vector at 0x00 pointing into the middle of the array, plausible handler addresses, plausible code at the destination. Nothing in the listing says the checksum is wrong, because the checksum is not a property of the code — it is a property of the table.

The giveaway is the failure mode itself. If the UART answers a question mark after reset, the loader is running, which means it decided the image was invalid. That is a checksum verdict, not a hardware fault.

Three ways to get the word wrong

The first is the simplest: the image was reassembled from a partial read, and the bottom of the array was rebuilt while the reserved slot was left as erased flash. All ones do not sum to zero with any real vector table.

The second is more subtle. Some builds place a jump instruction at 0x00 and others place the address of the reset routine. Both are legal on this core, and a table assembled from one convention and a checksum computed for the other will be wrong by exactly the difference between the two interpretations.

The third is a genuine modification. When the recovered code is adjusted — a peripheral initialisation removed, an address patched to make a board variant work — the vector table changes and the checksum must be recomputed. Forgetting that step produces an image that runs on the bench under a debugger, because a debugger does not ask the loader for permission, and fails completely when the device boots on its own.

Offset Word Meaning Touched by a rebuild
0x00 reset vector first instruction or address yes
0x04 undefined instruction handler address rarely
0x08 software interrupt handler address rarely
0x0C prefetch abort handler address rarely
0x10 data abort handler address rarely
0x14 checksum makes the sum zero must be recomputed
0x18 IRQ handler address sometimes
0x1C FIQ handler address sometimes

MEMMAP changes where the vectors are

There is a second mechanism layered on top, and it matters when the firmware runs code out of SRAM.

The memory map register selects which memory appears at address zero: the boot loader’s own block, the user flash, or the user RAM. A product that copies a routine into SRAM and executes it there — a flash programming routine, a fast interrupt path, a self-modifying decoder — typically switches the map to SRAM so that the vectors it installed at the base of RAM are the ones the core actually uses.

A recovered image therefore sometimes contains two vector tables: the one in flash that the loader checks, and the one the application builds in RAM at run time to redirect interrupts. Only the first is protected by the checksum rule. The second is created by code, and if the code is incomplete, the second table may be missing entries that nothing in the static image reveals.

Reading the dump as a product description

Once the checksum rule is handled, the vector table becomes the most useful page in the file. The IRQ entry is a dispatcher, and the dispatcher’s structure tells you how the firmware organises its work: a table of function pointers indexed by an interrupt source, a chain of comparisons, or a hardware vector controller whose registers were programmed at start-up.

Counting the pointers in that dispatcher gives the number of interrupt sources the product actually uses, which is often far fewer than the silicon supports and is a direct clue about what the board does. On a device with a locked array and a working board, this is frequently the fastest route to understanding the product.

Delivering a hex that boots

A heximal handed to a customer is only useful if the device starts from it. That means the file must carry the checksum, must match the memory map the product was built for, and must not include the region the loader occupies at the top of the array.

Verification is by behaviour, not by byte comparison: write the image, release reset with the entry pin in its normal state, and confirm the first output the product normally produces. If the port stays silent and a question mark gets an answer, the image was rejected — go back to 0x14.

Two related pieces explain the surrounding mechanics: LPC2101FBD48 protection states covers the control word that decides whether the loader will serve you at all, and LPC2104FBD48 mass erase behaviour covers what happens when an erase removes the table you were relying on. For a device where the same boot-time validity check exists under a different vendor’s rules, STM32F101C4 binary recovery is a useful contrast, because there the check is a protection level rather than a checksum.

A short procedure

Read the bottom four words and the top of the array before anything else, compute the sum of the eight vectors, and record it. If the sum is not zero, the device has been telling you all along why it will not start. Fix the word, write sector 0 only, and confirm the loader accepts the image before touching the rest of the device. The check takes a minute and removes the most common cause of a job that appears to have failed when it has actually succeeded.

PostHeaderIcon Decrypt IC PIC16F505 Flash: There Is No Cipher to Break, and That Changes the Answer

The word decrypt arrives in requests often enough that it deserves a straight answer. The PIC16F505 contains no encryption engine, no key storage, no scrambled address map and no obfuscated instruction set. What it contains is a code-protect bit that disables the read path, and a bulk erase that clears both the bit and everything it was protecting.

That second half of the sentence is the part customers do not expect. On an OTP device, protection is permanent and the array is a physical object that can be probed. On a flash device of this generation, protection is reversible — and reversing it deletes the content. So “decrypt the chip” is not a task that can be completed on the part itself, only on the die. Everything below is about what can be done, and what the word usually means when it is used precisely.

The part in numbers

item PIC16F505
program memory 1024 by 12 bits, flash
pages 2, selected by a status bit
data RAM 72 bytes
I/O 12 pins, 14-pin package
interrupts none
data EEPROM none
oscillator internal RC, two speeds, plus external options
protection code-protect bit in the config word, cleared by bulk erase

The missing data EEPROM row surprises people who are used to mid-range parts. There is nowhere on this device to put calibration data except the program array itself, and that is exactly where designers put it: a block of RETLW instructions near the end of the map, addressed by an index. When a recovered image is being reviewed for “where are the settings”, the answer on this part is usually “inside the code, four hundred words from the top”.

Three things the word decrypt actually asks for

The vocabulary problem is worth separating once, because each meaning has a different deliverable.

what the customer means what it requires what it cannot give
read a protected part die access plus a read of the array a way to make the original part programmable again
turn a binary into something readable disassembly, table identification, annotation original source, variable names, comments
unscramble a dump that looks wrong a corrected read, or a container fix anything if the array was never read correctly

The first row is the one that gets called decryption and is really protection circumvention. The second is often called decryption and is really disassembly. The third is called decryption and is usually a file-format or address-offset problem, which is the cheapest of the three to fix and the most often misdiagnosed.

Why unprotecting a flash part is a one-way deal in practice

The code-protect bit is stored in the configuration word, and a bulk erase clears the configuration word along with the array. This is by design: the bit is not a lock with a key, it is a flag that the erase operation resets.

The consequence is precise. A part can be returned to the blank state, and in that state it is unprotected and programmable. It is also empty. There is no sequence of commands that clears the bit while preserving the array, because the array and the bit are erased by the same operation.

Anyone who has worked only with OTP parts expects the opposite, on the grounds that a fuse cannot be undone and therefore content survives. Both intuitions are wrong in different directions, and both are corrected by the same sentence: protection prevents reading, erasure removes content, and on flash the two are the same event.

What reading a protected 16F505 involves

Because the command path is closed, the work moves to the die: remove the package, expose the array, and read it with a probe rather than a protocol. On a flash part this is not a matter of finding a hidden test mode. The cells hold charge in the ordinary way; the question is access.

Three practical notes apply to this device specifically. The die is small even by the standards of its generation, so the probe landing pad budget is tight. The array is organised in rows that do not correspond to the twelve-bit word a disassembler wants, so a raw bit image has to be de-interleaved before it becomes a program. And the configuration word is stored outside the array, which is convenient: it can often be read without touching the array at all, and it tells you the oscillator setting the design expects, which is otherwise unknowable.

Turning a binary into something maintainable

Once an image exists, the second meaning of decrypt takes over. A 12-bit baseline image is unusually friendly for this, for two structural reasons: there is exactly one entry point, and there are no interrupts. No context to reconstruct, no vector table to reconcile, no asynchronous code paths.

The work is therefore dominated by three passes. Decode every word as an instruction and mark the ones that cannot be instructions — the RETLW tables. Trace from address zero through GOTO and CALL and record the page bit at each entry. Then annotate the delay loops, because on a fixed internal oscillator every timing constant is a statement about the clock, and a maintenance engineer who changes one without knowing the clock will break the product.

That annotation pass is what separates a listing from a usable source. It is also the part that cannot be automated, which is why quotations for “decrypt and give me the source” vary by an order of magnitude across suppliers who are all doing the same physical work.

For the file-format version of the third meaning, PIC16C57 heximal file extraction covers packing and record framing. For the triage that tells you whether a strange image is protection or a bad read, PIC16C56A heximal file gives the signatures. And for the sibling part that shares this core and adds comparators, PIC16F506 firmware recovery continues the family thread.

FAQ

Is there any encryption on this device? None. No key, no cipher, no scrambled addressing. The protection bit blocks the read command and nothing else.

Can the part be unprotected without losing the program? No. The erase that clears the protect bit clears the array in the same operation, by design.

Does that mean a protected 16F505 is unrecoverable by normal means? It means the normal means are exhausted. Recovery becomes a die-level read, which is a different service with a different cost structure.

The short version

No cipher, one protect bit, one erase that clears both bit and content. Decrypt therefore means one of three things, and only the first is physical. Say which one is wanted before quoting, read the configuration word first because it is cheap and it survives, and treat a maintenance-quality source as a separate engagement from a byte-accurate image.