Posts Tagged ‘uv eprom mcu firmware extract’

PostHeaderIcon Section Layout: Why a Full Read Is Not a Complete Image

An extraction that reads every address from the start of flash to the end produces a byte-for-byte copy of the array. It is not necessarily a firmware image, and the difference is what makes a delivery that “looks complete” fail on the customer’s bench.

The reason is that a program’s memory layout is defined by a linker script, and the linker script places things in several regions with different rules. The flash holds some of them, the RAM holds others, and some are not stored anywhere at all until the program creates them.

What the flash actually contains

The flash contains four kinds of content, and they are not all program.

The code is the obvious part, including the vector table and the startup routine. Read-only data — constant tables, strings, lookup arrays — sits alongside it.

The third kind is the initial values of initialised variables. A variable declared with a value is stored in RAM at run time and its initial value is stored in flash, because RAM loses its contents at power-off. The startup code copies that block from flash to RAM before main runs.

Content Run-time location Stored in flash
Code and vectors Flash Yes
Read-only data Flash Yes
Initialised data (.data) RAM Yes, as a source block
Zero-initialised data (.bss) RAM No
Stack and heap RAM No
Configuration A separate area Usually not in the array

The fourth row is the one that catches people. A .bss region occupies no flash at all; it is created by the startup code zeroing a RAM range. An image that “contains” the .bss region is an image whose author has confused the RAM map with the flash map.

The address range is not the image

One more distinction is worth making explicit because it causes a specific and common error.

A read produces bytes from a range of addresses. An image is a file whose contents correspond to those addresses starting at a defined base. The two are the same only when the base is recorded, and the base is not always zero.

Read range Image base Result of assuming zero
0x0000 to end 0x0000 Correct
0x08000000 to end 0x08000000 Every address is wrong by 128 MB
0x0000 to end 0x0000, but the image starts at 0x1000 The vector table is found at the wrong offset
0x1000 to end 0x1000 A bootloader is missing from the image

The second row is the reason ARM images are frequently mis-based. A Cortex-M part maps flash at 0x08000000 and aliases it at 0x00000000, so a read of the alias and a read of the real address produce identical bytes at different addresses. An image built from the alias with a base of zero will run on that part and will not link against anything.

The third row is subtler. Some parts reserve the first few kilobytes for a bootloader or a configuration block, and the application’s vector table sits at 0x1000. An image that assumes the vector table is at zero will look as though it has no valid entry point, which reads as a damaged image rather than as a base error.

How the startup code defines the boundaries

The startup routine is the authoritative description of the layout, and reading it tells you what a complete image must contain.

It begins by loading the initial stack pointer from the first word of the vector table. It then copies a block from a flash source address to a RAM destination address, and the two addresses and the length are either in the code as immediates or in the vector table’s neighbourhood as a small structure. Finally it zeroes a RAM range, again defined by a base and a length.

Startup element What it defines Where the values are
Initial stack pointer Top of stack Word 0 of the image
Copy loop source Start of .data in flash Immediate or a literal
Copy loop destination Start of .data in RAM Immediate or a literal
Copy length Size of .data Immediate or a literal
Zero loop base and length .bss range Immediate or a literal

Those five values are the layout, and they are the reason a dumped image can be checked for completeness without any other information. If the copy source points outside the dumped range, the image is incomplete. If the copy length is larger than the region between the source and the end of the dump, the image is truncated.

What is not in the array at all

Three things a running device needs are stored outside the program array, and an extraction that ignores them produces an image that is complete as code and unusable as a device.

Configuration bytes — option bytes, fuses, calibration — live in a separate address range with a separate access mechanism. They are not part of the flash array, they are not covered by the read command that reads the array, and they are frequently the difference between a device that runs and one that does not.

The factory calibration page is a region of flash that is written at test and protected against modification. It is in the array on some parts and in a separate area on others, and its contents are per-unit.

A bootloader is a separate program in a separate region. An application image extracted without it is a program that cannot be programmed.

Item Location Included in an array read
Configuration bytes Separate range No
Factory calibration A protected page, sometimes in the array Sometimes
Bootloader A separate region of the array Yes, if the read covers it
Signature or checksum End of the application region Yes

The layout question a customer is really asking

When a customer asks whether the extraction is complete, the useful answer is a statement about the three regions: code, initialised data and configuration.

A complete extraction has the code from the vector table to the end of the last function, the initialised data block with its source address inside the dumped range, and the configuration bytes read separately and delivered as a separate file with a note on how they were obtained.

Region Complete if How to check
Code Ends with erased space or a known boundary The boundary analysis
Initialised data Copy source inside the dump The startup routine’s immediates
Configuration Read separately and delivered A second read of a different range
Calibration Read from the page or from a reference Compare two units

Why this matters for the deliverable

An image that is missing its initialised data block will boot and misbehave in a specific way: every global variable that had an initial value is zero instead. The symptom is a program that works until it relies on a configured default, which may be at startup or may be hours later.

An image that is missing its configuration bytes will not boot at all on a new device, because the clock source and the boot mode are set there.

Missing component Symptom When it appears
.data initial values Globals are zero When a default is used
Configuration bytes Device does not start Immediately
Calibration Measurements are wrong When a measurement is taken
Bootloader Cannot be reprogrammed When an update is attempted

The first row is the dangerous one because it appears late. A device that boots, runs and fails an hour later is a device that will be diagnosed as a hardware fault.

How the service delivers

We deliver the image, the layout map derived from the startup code, and any configuration or calibration read separately. The layout map lists each region with its address range, its run-time location and whether it was included.

Where a region could not be included — a calibration page that is write-protected and read-protected, for example — the map says so and says what that absence implies for the customer’s intended use. That is more useful than a complete-looking file with a silent gap.

The analysis that follows, which turns the image into something readable, is in MCU break. The read that produces the dump is in read MCU code, and the confidence labelling that applies when the read is imperfect is in restoring MCU code.

Where the customer’s need is a running replacement rather than a faithful copy, the missing pieces are not sections but conditions, and the six of them are set out in cloning IC firmware.