PostHeaderIcon MSP430G2101: A One-Kilobyte Program You Can Read in Full

The tools and habits of firmware analysis are built for images of tens of kilobytes, where nobody can read everything and the work has to be statistical: find the compiler, locate the library functions by their signatures, identify main from the call graph, and accept that most of the image will remain unexamined.

At one kilobyte none of that applies. The image is a few hundred instructions. There is no library, because there was no room for one. There is no meaningful statistical structure, because a few hundred instructions is not a population. And there is a genuine opportunity that does not exist at larger sizes: reading the entire program and understanding all of it.

What a device this size can and cannot contain

The constraints are severe enough to determine the shape of any program written for it.

There is no room for floating point, so all arithmetic is integer or fixed point. There is no room for formatted output, so any serial protocol is a hand-written byte stream. There is no room for a scheduler, so the program is a loop with a few interrupt handlers. There is no room for a parameter file, so constants are either literals or in the information region.

Those constraints are also a map. When you know what cannot be in the image, the disassembly becomes readable quickly: every function you find is one of a small set of things a program of this size does.

Constraint What it implies about the code
No floating point Numeric handling is integer; fixed-point scaling appears as shifts
No formatted output The serial protocol is byte-at-a-time with explicit framing
No scheduler One main loop plus a handful of vectors
No spare space Configuration is in the information region or in literals

Reading it from the entry point

Start at the reset vector and read forward. On this core that is a small amount of work: the vector points at the startup code, which sets the stack pointer, configures the clock, and then calls main.

Three things to record as you go:

  1. Every call target, which builds the function list without any graph analysis.
  2. Every peripheral register address accessed, which identifies the interfaces the product uses.
  3. Every constant loaded as an immediate, which is where the protocol values and the calibration live.

The third list is the most valuable. In a program of this size, the magic numbers are the specification: the baud divisor, the sampling period, the threshold, the command codes. Extracting them by hand produces a document that describes the product, which is often what the customer actually needed.

The state machine is the program

Small embedded programs are usually state machines, and at this size the state variable is a single byte in RAM that is tested in a chain of comparisons.

Finding it is straightforward: look for a byte that is written in one place and read in several, with a sequence of comparisons against small constants. That is the state variable, and the constants are the states.

Once it is found, the program can be redrawn as a diagram, and the diagram is a better deliverable than the disassembly. It is also the artifact that makes reimplementation practical, because a state machine can be rewritten from its diagram without reproducing the original code.

Verification at this scale

The usual verification approach does not work here. There is not enough image to compute meaningful statistics, and there are no library signatures to match.

What replaces it is direct correspondence: every instruction in the image should be explainable by the behaviour observed on the device. If the device toggles a pin every ten milliseconds, there is a delay loop that produces ten milliseconds, and the constant in it can be checked against the clock.

That is a stronger verification than any statistical method, and it is only available because the whole program fits in a person’s head. A discrepancy at this scale is not a rounding error, it is a line of code that does not do what the device does, which means either the read is wrong or the understanding is.

The information region matters more, proportionally

Because the program is so small, the information region is a larger fraction of the total content than on a bigger device. Calibration constants, any stored parameters, and the device descriptor may together be a meaningful share of what the device holds.

That has a practical consequence for a restoration: the main array alone is less likely to be sufficient. Check the information region before declaring the recovery complete, and check whether the program reads from it, which a program this small will do by direct address.

When there is no loader

The entry-level parts of this family are the ones most likely to lack a bootstrap loader, and at this density the absence is not a great loss. The loader’s value is in moving a large image over a slow interface; here the image is small enough that the same transfer can be done by a short monitor program loaded over the debug interface.

That is the practical route on a device of this size with the protection absent: load a monitor, move a kilobyte out over whatever serial interface the board provides, and be done in less time than it takes to set up a loader connection on a larger part.

What to hand over

For a program this small the useful deliverable is not the binary. It is:

  • the annotated disassembly, with every function named,
  • the state machine diagram,
  • the parameter table, with each constant identified,
  • the memory map, including the information region,
  • the behaviour specification, as measured and as read from the code.

A customer who receives that can maintain the product. A customer who receives a hex file and no explanation has gained very little, because the file cannot be modified without the understanding that the annotation provides.

Related reading

FAQ

Is a kilobyte really readable by hand? A few hundred instructions, yes. That is an afternoon, not a week.

Why is there no library in the image? There is no room. Any support code was written specifically for the product.

Is the state machine always a single byte? Usually, and usually a byte that is written once and tested many times.

Does the small size make extraction easier? It makes understanding easier. The protection does not care about image size.

Conclusion

A one-kilobyte program is not a small version of a large one. It has no library, no statistics and no depth, and in exchange it can be read completely. Take that exchange: read every instruction, find the state variable, extract the constants, and deliver the specification rather than the file.

Comments are closed.