PostHeaderIcon PIC18F26K40: The Boot Block Has a Configurable Size

On small parts, the boot block is a fixed region at the bottom of the array and its size is documented in a table. On the K40 generation it is a configuration choice: the vendor selects a size when the device is programmed, and everything above the boot block belongs to the application.

That single field reorganises the memory map. It decides where the application’s reset vector lives, where the interrupt vectors live if they are relocated, and which addresses the programming tool treats as protected. A rebuild that ignores the field produces a device that programs cleanly and starts at the wrong place.

What the size field does

Selecting a boot block size reserves the bottom of the array for bootloader code. The reset vector is inside that region, so it always executes first; the interrupt vectors may reside there too, or be relocated into the application region depending on a separate setting.

Boot block size Application starts at What lives below
Smallest Just above the small block Vectors only
Medium Higher Loader plus vectors
Largest Higher still A full loader with protocol code

The important consequence: the address of “the start of the application” is a function of configuration, not of documentation. Two devices with the same part number, programmed by different vendors, can have application code starting at different addresses.

Why this matters when reading

A recovered image begins at address zero, which is the boot block. On a device with no bootloader, address zero is the application’s reset vector and the disassembly starts sensibly. On a device with a large boot block, address zero is a loader whose entry point branches into the application above it.

A reader who does not distinguish the two will decode a loader as application start-up and conclude the firmware does something it does not. The tell is that the code at address zero does very little before a long branch — loaders are small, and they hand off early.

The relocation interaction

If the interrupt vectors are relocated into the application region, the hardware fetches vectors from an address computed from the configuration. The vector table in that case is not at the base of the array, and searching for it there yields data that decodes as instructions and is not.

Finding the real table requires the configuration. This is the third time in this series that the same advice applies, and it remains the right advice: read the configuration before interpreting the image.

Protection, in this arrangement

The boot block has its own protection bits, and the application blocks have theirs. That allows the configuration a vendor actually wants: a bootloader that is readable or writable only by the vendor’s tooling, and an application the customer can update.

Because the boot block size is configurable, the protection boundaries move with it. A device whose protection was applied for a small boot block, then reprogrammed with a larger one, can end up with part of the loader outside the protected region — a subtle misconfiguration that is invisible until someone examines the configuration exactly.

Reading a K40 device in practice

  1. Read the configuration and interpret the boot block size field first.
  2. Compute where the application begins.
  3. Decode the boot block separately from the application; they are different programs with different purposes.
  4. Locate the vector table from the configuration, not by search.
  5. For the application, proceed as usual: entry points, references, tables.

Step three is worth doing properly. Bootloaders on this generation are frequently the vendor’s own code, well-structured, with a protocol implementation and a verification routine. Reading one is often easier than reading the application, and it reveals the update format — which in turn reveals the application’s expected layout.

What a loader tells you about the application

A bootloader written for this part must know:

  • where the application begins, since it jumps there;
  • what the application’s image format is;
  • how to verify the image, if it verifies;
  • whether to preserve the application’s data array during an update.

Every one of those is a fact about the application, stated by code that is often less protected than the application itself. This asymmetry is common and underused: the loader is the part of the design that must be functional for field updates, and functionality and secrecy are frequently traded against each other.

Limits

  • The configuration tells you the layout; it does not tell you what the code does.
  • A protected boot block with an open application is the usual arrangement, and it is the good case.
  • A fully protected device yields configuration and nothing else.

Related reading

FAQ

Is the boot block always the same size? No. It is a configuration field, set at programming time.

Can the application start at different addresses on identical parts? Yes, if the boot block size differs.

Where is the vector table? At the base, unless relocation is configured. The configuration is the authority, not convention.

Does the boot block have separate protection? Yes, with its own read and write bits, distinct from the application blocks.

Why read the loader first? Because it is usually less protected and it states the application’s layout and update format.

Conclusion

On the K40 generation the memory map is a configuration rather than a constant. The boot block size moves the start of the application and the vectors, and the protection boundaries move with them. Reading the configuration first is not a habit here; it is the only way to know what address the hardware is actually fetching from.

Comments are closed.