<?xml version="1.0" encoding="utf-8" standalone="yes"?><rss version="2.0" xmlns:atom="http://www.w3.org/2005/Atom" xmlns:content="http://purl.org/rss/1.0/modules/content/"><channel><title>MCUboot on Hoa Nguyen — Embedded Software Architect</title><link>https://hoanguyen.de/tags/mcuboot/</link><description>Recent content in MCUboot on Hoa Nguyen — Embedded Software Architect</description><generator>Hugo</generator><language>en-us</language><lastBuildDate>Tue, 10 Nov 2026 00:00:00 +0000</lastBuildDate><atom:link href="https://hoanguyen.de/tags/mcuboot/index.xml" rel="self" type="application/rss+xml"/><item><title>From Board Bring-Up to Production: 5 Lessons from an i.MX RT Control Unit</title><link>https://hoanguyen.de/posts/bringup-to-production-imx-rt-lessons/</link><pubDate>Tue, 10 Nov 2026 00:00:00 +0000</pubDate><guid>https://hoanguyen.de/posts/bringup-to-production-imx-rt-lessons/</guid><description>Five lessons from taking an i.MX RT control unit from first boot toward production: the bootloader that hides your SDRAM setup, the flash size nobody measured, writing the flash you execute from, firmware updates as a protocol, and surviving the power cut.</description><content:encoded><![CDATA[<p>Getting a board to blink is the easy 10 %. The hard part is everything between &ldquo;it boots on
my desk&rdquo; and &ldquo;it can leave the building&rdquo;: a bootloader in front of the application, a flash
layout that still fits next year, field updates that never leave a device half-updated, and
a power cut at the worst possible moment.</p>
<p>None of the five problems below is exotic. Each one costs days when it goes unnoticed,
because a hidden assumption looks true on the bench until the moment it isn&rsquo;t.</p>
<p>In this article I walk through five lessons from taking an i.MX RT (Cortex-M7) control unit
along that path, in the order a product meets them: the mechanism behind each one, and the
rule that keeps it from coming back.</p>
<figure class="diagram">
  <div class="diagram-svg" role="img" aria-label="Five lessons from bring-up to production, each with the hidden assumption and the rule that replaces it: SDRAM is set up by the boot ROM&#39;s DCD, so the bootloader must take over; the flash size is a measurement, so one header owns the map and CI checks it; writing XIP flash makes the flash vanish, so the write path runs from RAM; an update is a protocol, so only the new version number counts as success; hold-up time is a measurement, so the power-fail path never starts a flash operation"><svg xmlns="http://www.w3.org/2000/svg" viewBox="0 0 760 610">
<defs><marker id="m_cur" viewBox="0 0 10 10" refX="9" refY="5" markerWidth="7" markerHeight="7" orient="auto-start-reverse"><path d="M0,0 L10,5 L0,10 z" fill="currentColor"/></marker></defs>
<text x="66" y="26" font-family="-apple-system, 'Segoe UI', Roboto, Helvetica, Arial, sans-serif" font-size="13" font-weight="bold" text-anchor="start" fill="currentColor" fill-opacity="0.7">Phase</text>
<text x="232" y="26" font-family="-apple-system, 'Segoe UI', Roboto, Helvetica, Arial, sans-serif" font-size="13" font-weight="bold" text-anchor="start" fill="#f59e0b">The hidden assumption</text>
<text x="492" y="26" font-family="-apple-system, 'Segoe UI', Roboto, Helvetica, Arial, sans-serif" font-size="13" font-weight="bold" text-anchor="start" fill="#10b981">The rule instead</text>
<rect x="10" y="42" width="740" height="104" rx="10" fill="#94a3b8" fill-opacity="0.06"/>
<circle cx="36" cy="94.0" r="15" fill="#3b82f6"/>
<text x="36" y="99.0" font-family="-apple-system, 'Segoe UI', Roboto, Helvetica, Arial, sans-serif" font-size="14" font-weight="bold" text-anchor="middle" fill="#fefefe">1</text>
<text x="62" y="90.0" font-family="-apple-system, 'Segoe UI', Roboto, Helvetica, Arial, sans-serif" font-size="15" font-weight="bold" text-anchor="start" fill="currentColor">Bring-up</text>
<text x="62" y="110.0" font-family="-apple-system, 'Segoe UI', Roboto, Helvetica, Arial, sans-serif" font-size="12.5" font-weight="normal" text-anchor="start" fill="currentColor" fill-opacity="0.7">behind a bootloader</text>
<rect x="222" y="54" width="236" height="80" rx="8" fill="#f59e0b" fill-opacity="0.12" stroke="#f59e0b" stroke-width="1.5"/>
<text x="234" y="89.5" font-family="-apple-system, 'Segoe UI', Roboto, Helvetica, Arial, sans-serif" font-size="13.5" font-weight="normal" text-anchor="start" fill="currentColor">The application sets up</text>
<text x="234" y="108.5" font-family="-apple-system, 'Segoe UI', Roboto, Helvetica, Arial, sans-serif" font-size="13.5" font-weight="normal" text-anchor="start" fill="currentColor">its own SDRAM</text>
<line x1="462" y1="94.0" x2="478" y2="94.0" stroke="currentColor" stroke-width="1.6" stroke-opacity="0.6" marker-end="url(#m_cur)"/>
<rect x="482" y="54" width="262" height="80" rx="8" fill="#10b981" fill-opacity="0.12" stroke="#10b981" stroke-width="1.5"/>
<text x="494" y="80.0" font-family="-apple-system, 'Segoe UI', Roboto, Helvetica, Arial, sans-serif" font-size="13.5" font-weight="normal" text-anchor="start" fill="currentColor">The boot ROM did it via the DCD.</text>
<text x="494" y="99.0" font-family="-apple-system, 'Segoe UI', Roboto, Helvetica, Arial, sans-serif" font-size="13.5" font-weight="normal" text-anchor="start" fill="currentColor">The bootloader must take over</text>
<text x="494" y="118.0" font-family="-apple-system, 'Segoe UI', Roboto, Helvetica, Arial, sans-serif" font-size="13.5" font-weight="normal" text-anchor="start" fill="currentColor">SDRAM and clocks</text>
<rect x="10" y="154" width="740" height="104" rx="10" fill="#94a3b8" fill-opacity="0.06"/>
<circle cx="36" cy="206.0" r="15" fill="#8b5cf6"/>
<text x="36" y="211.0" font-family="-apple-system, 'Segoe UI', Roboto, Helvetica, Arial, sans-serif" font-size="14" font-weight="bold" text-anchor="middle" fill="#fefefe">2</text>
<text x="62" y="202.0" font-family="-apple-system, 'Segoe UI', Roboto, Helvetica, Arial, sans-serif" font-size="15" font-weight="bold" text-anchor="start" fill="currentColor">Flash layout</text>
<text x="62" y="222.0" font-family="-apple-system, 'Segoe UI', Roboto, Helvetica, Arial, sans-serif" font-size="12.5" font-weight="normal" text-anchor="start" fill="currentColor" fill-opacity="0.7">partition map</text>
<rect x="222" y="166" width="236" height="80" rx="8" fill="#f59e0b" fill-opacity="0.12" stroke="#f59e0b" stroke-width="1.5"/>
<text x="234" y="192.0" font-family="-apple-system, 'Segoe UI', Roboto, Helvetica, Arial, sans-serif" font-size="13.5" font-weight="normal" text-anchor="start" fill="currentColor">The size in the linker</text>
<text x="234" y="211.0" font-family="-apple-system, 'Segoe UI', Roboto, Helvetica, Arial, sans-serif" font-size="13.5" font-weight="normal" text-anchor="start" fill="currentColor">script and the ticket</text>
<text x="234" y="230.0" font-family="-apple-system, 'Segoe UI', Roboto, Helvetica, Arial, sans-serif" font-size="13.5" font-weight="normal" text-anchor="start" fill="currentColor">is right</text>
<line x1="462" y1="206.0" x2="478" y2="206.0" stroke="currentColor" stroke-width="1.6" stroke-opacity="0.6" marker-end="url(#m_cur)"/>
<rect x="482" y="166" width="262" height="80" rx="8" fill="#10b981" fill-opacity="0.12" stroke="#10b981" stroke-width="1.5"/>
<text x="494" y="192.0" font-family="-apple-system, 'Segoe UI', Roboto, Helvetica, Arial, sans-serif" font-size="13.5" font-weight="normal" text-anchor="start" fill="currentColor">Probe the part. One header owns</text>
<text x="494" y="211.0" font-family="-apple-system, 'Segoe UI', Roboto, Helvetica, Arial, sans-serif" font-size="13.5" font-weight="normal" text-anchor="start" fill="currentColor">the map; an assert and a CI</text>
<text x="494" y="230.0" font-family="-apple-system, 'Segoe UI', Roboto, Helvetica, Arial, sans-serif" font-size="13.5" font-weight="normal" text-anchor="start" fill="currentColor">test keep it true</text>
<rect x="10" y="266" width="740" height="104" rx="10" fill="#94a3b8" fill-opacity="0.06"/>
<circle cx="36" cy="318.0" r="15" fill="#14b8a6"/>
<text x="36" y="323.0" font-family="-apple-system, 'Segoe UI', Roboto, Helvetica, Arial, sans-serif" font-size="14" font-weight="bold" text-anchor="middle" fill="#fefefe">3</text>
<text x="62" y="314.0" font-family="-apple-system, 'Segoe UI', Roboto, Helvetica, Arial, sans-serif" font-size="15" font-weight="bold" text-anchor="start" fill="currentColor">Field update</text>
<text x="62" y="334.0" font-family="-apple-system, 'Segoe UI', Roboto, Helvetica, Arial, sans-serif" font-size="12.5" font-weight="normal" text-anchor="start" fill="currentColor" fill-opacity="0.7">writing the XIP flash</text>
<rect x="222" y="278" width="236" height="80" rx="8" fill="#f59e0b" fill-opacity="0.12" stroke="#f59e0b" stroke-width="1.5"/>
<text x="234" y="313.5" font-family="-apple-system, 'Segoe UI', Roboto, Helvetica, Arial, sans-serif" font-size="13.5" font-weight="normal" text-anchor="start" fill="currentColor">Writing the flash we run</text>
<text x="234" y="332.5" font-family="-apple-system, 'Segoe UI', Roboto, Helvetica, Arial, sans-serif" font-size="13.5" font-weight="normal" text-anchor="start" fill="currentColor">from is just slow</text>
<line x1="462" y1="318.0" x2="478" y2="318.0" stroke="currentColor" stroke-width="1.6" stroke-opacity="0.6" marker-end="url(#m_cur)"/>
<rect x="482" y="278" width="262" height="80" rx="8" fill="#10b981" fill-opacity="0.12" stroke="#10b981" stroke-width="1.5"/>
<text x="494" y="304.0" font-family="-apple-system, 'Segoe UI', Roboto, Helvetica, Arial, sans-serif" font-size="13.5" font-weight="normal" text-anchor="start" fill="currentColor">While busy, the flash is gone:</text>
<text x="494" y="323.0" font-family="-apple-system, 'Segoe UI', Roboto, Helvetica, Arial, sans-serif" font-size="13.5" font-weight="normal" text-anchor="start" fill="currentColor">write path in RAM, IRQs masked,</text>
<text x="494" y="342.0" font-family="-apple-system, 'Segoe UI', Roboto, Helvetica, Arial, sans-serif" font-size="13.5" font-weight="normal" text-anchor="start" fill="currentColor">I-cache invalidated after</text>
<rect x="10" y="378" width="740" height="104" rx="10" fill="#94a3b8" fill-opacity="0.06"/>
<circle cx="36" cy="430.0" r="15" fill="#f59e0b"/>
<text x="36" y="435.0" font-family="-apple-system, 'Segoe UI', Roboto, Helvetica, Arial, sans-serif" font-size="14" font-weight="bold" text-anchor="middle" fill="#fefefe">4</text>
<text x="62" y="426.0" font-family="-apple-system, 'Segoe UI', Roboto, Helvetica, Arial, sans-serif" font-size="15" font-weight="bold" text-anchor="start" fill="currentColor">Update protocol</text>
<text x="62" y="446.0" font-family="-apple-system, 'Segoe UI', Roboto, Helvetica, Arial, sans-serif" font-size="12.5" font-weight="normal" text-anchor="start" fill="currentColor" fill-opacity="0.7">proof of success</text>
<rect x="222" y="390" width="236" height="80" rx="8" fill="#f59e0b" fill-opacity="0.12" stroke="#f59e0b" stroke-width="1.5"/>
<text x="234" y="425.5" font-family="-apple-system, 'Segoe UI', Roboto, Helvetica, Arial, sans-serif" font-size="13.5" font-weight="normal" text-anchor="start" fill="currentColor">"Update done" means</text>
<text x="234" y="444.5" font-family="-apple-system, 'Segoe UI', Roboto, Helvetica, Arial, sans-serif" font-size="13.5" font-weight="normal" text-anchor="start" fill="currentColor">the update worked</text>
<line x1="462" y1="430.0" x2="478" y2="430.0" stroke="currentColor" stroke-width="1.6" stroke-opacity="0.6" marker-end="url(#m_cur)"/>
<rect x="482" y="390" width="262" height="80" rx="8" fill="#10b981" fill-opacity="0.12" stroke="#10b981" stroke-width="1.5"/>
<text x="494" y="416.0" font-family="-apple-system, 'Segoe UI', Roboto, Helvetica, Arial, sans-serif" font-size="13.5" font-weight="normal" text-anchor="start" fill="currentColor">One outcome per request; only</text>
<text x="494" y="435.0" font-family="-apple-system, 'Segoe UI', Roboto, Helvetica, Arial, sans-serif" font-size="13.5" font-weight="normal" text-anchor="start" fill="currentColor">the new version number counts</text>
<text x="494" y="454.0" font-family="-apple-system, 'Segoe UI', Roboto, Helvetica, Arial, sans-serif" font-size="13.5" font-weight="normal" text-anchor="start" fill="currentColor">as success</text>
<rect x="10" y="490" width="740" height="104" rx="10" fill="#94a3b8" fill-opacity="0.06"/>
<circle cx="36" cy="542.0" r="15" fill="#ef4444"/>
<text x="36" y="547.0" font-family="-apple-system, 'Segoe UI', Roboto, Helvetica, Arial, sans-serif" font-size="14" font-weight="bold" text-anchor="middle" fill="#fefefe">5</text>
<text x="62" y="538.0" font-family="-apple-system, 'Segoe UI', Roboto, Helvetica, Arial, sans-serif" font-size="15" font-weight="bold" text-anchor="start" fill="currentColor">Power cut</text>
<text x="62" y="558.0" font-family="-apple-system, 'Segoe UI', Roboto, Helvetica, Arial, sans-serif" font-size="12.5" font-weight="normal" text-anchor="start" fill="currentColor" fill-opacity="0.7">hold-up time</text>
<rect x="222" y="502" width="236" height="80" rx="8" fill="#f59e0b" fill-opacity="0.12" stroke="#f59e0b" stroke-width="1.5"/>
<text x="234" y="537.5" font-family="-apple-system, 'Segoe UI', Roboto, Helvetica, Arial, sans-serif" font-size="13.5" font-weight="normal" text-anchor="start" fill="currentColor">The capacitors keep the</text>
<text x="234" y="556.5" font-family="-apple-system, 'Segoe UI', Roboto, Helvetica, Arial, sans-serif" font-size="13.5" font-weight="normal" text-anchor="start" fill="currentColor">board alive long enough</text>
<line x1="462" y1="542.0" x2="478" y2="542.0" stroke="currentColor" stroke-width="1.6" stroke-opacity="0.6" marker-end="url(#m_cur)"/>
<rect x="482" y="502" width="262" height="80" rx="8" fill="#10b981" fill-opacity="0.12" stroke="#10b981" stroke-width="1.5"/>
<text x="494" y="528.0" font-family="-apple-system, 'Segoe UI', Roboto, Helvetica, Arial, sans-serif" font-size="13.5" font-weight="normal" text-anchor="start" fill="currentColor">Measure hold-up first. If an erase</text>
<text x="494" y="547.0" font-family="-apple-system, 'Segoe UI', Roboto, Helvetica, Arial, sans-serif" font-size="13.5" font-weight="normal" text-anchor="start" fill="currentColor">can outlast it, the power-fail</text>
<text x="494" y="566.0" font-family="-apple-system, 'Segoe UI', Roboto, Helvetica, Arial, sans-serif" font-size="13.5" font-weight="normal" text-anchor="start" fill="currentColor">path never starts one</text>
</svg>
</div>
  <figcaption>Five hidden assumptions, and the rule that replaces each one.</figcaption>
</figure>

<h2 id="1-the-board-boots-on-its-own-then-dies-behind-the-bootloader">1. The board boots on its own, then dies behind the bootloader</h2>
<p><strong>The mental model: the boot ROM was quietly doing your setup for you.</strong></p>
<p>A stand-alone application image on the i.MX RT works because the boot ROM reads the image&rsquo;s
boot header and runs its DCD (Device Configuration Data). That is where external SDRAM gets
configured. You never have to think about it.</p>
<p>Then you put a bootloader such as MCUboot in front of the application. A signed application
image behind a bootloader has no boot header of its own, so nobody runs its DCD. The first
stack push or the first <code>.bss</code> clear in SDRAM ends in a bus fault, long before <code>main()</code>.</p>
<p>The fix has two parts:</p>
<ol>
<li>Move the SDRAM setup (the DCD) into the <strong>bootloader</strong>, which the ROM still boots
normally.</li>
<li>Make the application <strong>skip the clock (PLL) re-initialisation</strong>, because the
bootloader has already done it.</li>
</ol>
<p>A related trap: in MCUboot&rsquo;s direct-XIP-with-revert mode, a slot without the image
trailer&rsquo;s magic value is erased at reset. The symptom is <em>the application disappears after
a power cycle</em>. Sign the image with the trailer included.</p>
<div class="callout callout--tip">
  <p class="callout__title"><span class="callout__icon" aria-hidden="true">💡</span>What to check on your own board</p>
  <div class="callout__body">
List everything the boot ROM does for a stand-alone image (DCD, clocks, memory) and ask who
does it once a bootloader sits in front. Anything nobody owns will fault before <code>main()</code>.
  </div>
</div>

<h2 id="2-measure-the-chip-not-the-documents">2. Measure the chip, not the documents</h2>
<p><strong>The mental model: the flash size is a measurement, not a setting.</strong></p>
<p>The flash size in a linker script, a ticket or a code comment is often copied from an
earlier board. Several documents agreeing with each other proves nothing if they all come
from the same assumption. Read the part number on the board, and probe the memory with a
debugger before you plan a partition map around it.</p>
<p>Then make the answer structural, so it cannot drift again:</p>
<ol>
<li><strong>One header</strong> owns the flash partition map.</li>
<li><strong>A start-up assert</strong> checks it against the linker symbols.</li>
<li><strong>A host test</strong> parses the linker scripts, so any drift fails in CI rather than on a
customer&rsquo;s device.</li>
</ol>
<p>The map matters most once you plan field updates. A dual-slot layout needs room for <strong>two
complete images</strong>, next to the bootloader and the data partition, and images grow over a
product&rsquo;s life.</p>
<figure class="diagram">
  <div class="diagram-svg" role="img" aria-label="Flash budget for dual-slot updates: bootloader, slot 1 with the running image, slot 2 for the incoming update, and a data partition must all fit in the part; each slot is sized for the worst-case build plus room to grow; above 16 MiB the flash needs 4-byte addressing; the layout is owned by one header and checked in CI"><svg xmlns="http://www.w3.org/2000/svg" viewBox="0 0 760 372">
<text x="20" y="72" font-family="-apple-system, 'Segoe UI', Roboto, Helvetica, Arial, sans-serif" font-size="14" font-weight="bold" text-anchor="start" fill="currentColor">At design time</text>
<text x="20" y="90" font-family="-apple-system, 'Segoe UI', Roboto, Helvetica, Arial, sans-serif" font-size="12" font-weight="normal" text-anchor="start" fill="currentColor" fill-opacity="0.7">everything fits</text>
<rect x="170" y="52" width="50" height="44" rx="4" fill="#94a3b8" fill-opacity="0.3" stroke="#94a3b8" stroke-width="1.5"/>
<text x="195.0" y="79" font-family="-apple-system, 'Segoe UI', Roboto, Helvetica, Arial, sans-serif" font-size="13" font-weight="bold" text-anchor="middle" fill="currentColor">boot</text>
<rect x="220" y="52" width="170" height="44" rx="4" fill="#3b82f6" fill-opacity="0.2" stroke="#3b82f6" stroke-width="1.5"/>
<text x="305.0" y="72" font-family="-apple-system, 'Segoe UI', Roboto, Helvetica, Arial, sans-serif" font-size="13" font-weight="bold" text-anchor="middle" fill="currentColor">slot 1</text>
<text x="305.0" y="88" font-family="-apple-system, 'Segoe UI', Roboto, Helvetica, Arial, sans-serif" font-size="11.5" font-weight="normal" text-anchor="middle" fill="currentColor" fill-opacity="0.8">running image</text>
<rect x="390" y="52" width="170" height="44" rx="4" fill="#3b82f6" fill-opacity="0.2" stroke="#3b82f6" stroke-width="1.5"/>
<text x="475.0" y="72" font-family="-apple-system, 'Segoe UI', Roboto, Helvetica, Arial, sans-serif" font-size="13" font-weight="bold" text-anchor="middle" fill="currentColor">slot 2</text>
<text x="475.0" y="88" font-family="-apple-system, 'Segoe UI', Roboto, Helvetica, Arial, sans-serif" font-size="11.5" font-weight="normal" text-anchor="middle" fill="currentColor" fill-opacity="0.8">incoming update</text>
<rect x="560" y="52" width="90" height="44" rx="4" fill="#14b8a6" fill-opacity="0.2" stroke="#14b8a6" stroke-width="1.5"/>
<text x="605.0" y="72" font-family="-apple-system, 'Segoe UI', Roboto, Helvetica, Arial, sans-serif" font-size="13" font-weight="bold" text-anchor="middle" fill="currentColor">data</text>
<text x="605.0" y="88" font-family="-apple-system, 'Segoe UI', Roboto, Helvetica, Arial, sans-serif" font-size="11.5" font-weight="normal" text-anchor="middle" fill="currentColor" fill-opacity="0.8">file system</text>
<text x="20" y="162" font-family="-apple-system, 'Segoe UI', Roboto, Helvetica, Arial, sans-serif" font-size="14" font-weight="bold" text-anchor="start" fill="currentColor">Images grow</text>
<text x="20" y="180" font-family="-apple-system, 'Segoe UI', Roboto, Helvetica, Arial, sans-serif" font-size="12" font-weight="normal" text-anchor="start" fill="currentColor" fill-opacity="0.7">both slots grow</text>
<rect x="170" y="142" width="50" height="44" rx="4" fill="#94a3b8" fill-opacity="0.3" stroke="#94a3b8" stroke-width="1.5"/>
<text x="195.0" y="169" font-family="-apple-system, 'Segoe UI', Roboto, Helvetica, Arial, sans-serif" font-size="13" font-weight="bold" text-anchor="middle" fill="currentColor">boot</text>
<rect x="220" y="142" width="215" height="44" rx="4" fill="#3b82f6" fill-opacity="0.2" stroke="#3b82f6" stroke-width="1.5"/>
<text x="327.5" y="162" font-family="-apple-system, 'Segoe UI', Roboto, Helvetica, Arial, sans-serif" font-size="13" font-weight="bold" text-anchor="middle" fill="currentColor">slot 1</text>
<text x="327.5" y="178" font-family="-apple-system, 'Segoe UI', Roboto, Helvetica, Arial, sans-serif" font-size="11.5" font-weight="normal" text-anchor="middle" fill="currentColor" fill-opacity="0.8">bigger image</text>
<rect x="435" y="142" width="215" height="44" rx="4" fill="#3b82f6" fill-opacity="0.2" stroke="#3b82f6" stroke-width="1.5"/>
<text x="542.5" y="162" font-family="-apple-system, 'Segoe UI', Roboto, Helvetica, Arial, sans-serif" font-size="13" font-weight="bold" text-anchor="middle" fill="currentColor">slot 2</text>
<text x="542.5" y="178" font-family="-apple-system, 'Segoe UI', Roboto, Helvetica, Arial, sans-serif" font-size="11.5" font-weight="normal" text-anchor="middle" fill="currentColor" fill-opacity="0.8">same size again</text>
<rect x="650" y="142" width="90" height="44" rx="4" fill="#ef4444" fill-opacity="0.22" stroke="#ef4444" stroke-width="1.5" stroke-dasharray="4 3"/>
<text x="695.0" y="162" font-family="-apple-system, 'Segoe UI', Roboto, Helvetica, Arial, sans-serif" font-size="13" font-weight="bold" text-anchor="middle" fill="currentColor">data</text>
<text x="695.0" y="178" font-family="-apple-system, 'Segoe UI', Roboto, Helvetica, Arial, sans-serif" font-size="11.5" font-weight="normal" text-anchor="middle" fill="currentColor" fill-opacity="0.8">no room</text>
<line x1="650" y1="38" x2="650" y2="200" stroke="currentColor" stroke-width="1.6" stroke-dasharray="5 4" stroke-opacity="0.7"/>
<text x="650" y="30" font-family="-apple-system, 'Segoe UI', Roboto, Helvetica, Arial, sans-serif" font-size="12" font-weight="normal" text-anchor="middle" fill="currentColor" fill-opacity="0.8">end of the part</text>
<text x="170" y="30" font-family="-apple-system, 'Segoe UI', Roboto, Helvetica, Arial, sans-serif" font-size="12" font-weight="normal" text-anchor="start" fill="currentColor" fill-opacity="0.8">start of flash</text>
<rect x="20" y="222" width="720" height="136" rx="8" fill="#8b5cf6" fill-opacity="0.08" stroke="#8b5cf6" stroke-width="1"/>
<text x="36" y="248" font-family="-apple-system, 'Segoe UI', Roboto, Helvetica, Arial, sans-serif" font-size="14" font-weight="bold" text-anchor="start" fill="currentColor">Rules for the partition map</text>
<text x="36" y="274" font-family="-apple-system, 'Segoe UI', Roboto, Helvetica, Arial, sans-serif" font-size="13.5" font-weight="normal" text-anchor="start" fill="currentColor">Size each slot for the worst-case build (debug, with tracing), plus room to grow.</text>
<text x="36" y="295" font-family="-apple-system, 'Segoe UI', Roboto, Helvetica, Arial, sans-serif" font-size="13.5" font-weight="normal" text-anchor="start" fill="currentColor">Two slots mean two complete images: an image that grows costs twice its growth.</text>
<text x="36" y="316" font-family="-apple-system, 'Segoe UI', Roboto, Helvetica, Arial, sans-serif" font-size="13.5" font-weight="normal" text-anchor="start" fill="currentColor">Above 16 MiB the flash needs 4-byte addressing: boot config, driver commands, size limit.</text>
<text x="36" y="337" font-family="-apple-system, 'Segoe UI', Roboto, Helvetica, Arial, sans-serif" font-size="13.5" font-weight="normal" text-anchor="start" fill="currentColor">One header owns the map; a start-up assert and a CI test check it against the linker.</text>
</svg>
</div>
  <figcaption>What a dual-slot layout has to fit, and the rules for sizing it.</figcaption>
</figure>

<table>
  <thead>
      <tr>
          <th>Budget question</th>
          <th>What to do</th>
      </tr>
  </thead>
  <tbody>
      <tr>
          <td>Which build do I size against?</td>
          <td>The worst case: the debug build with tracing, not the release build.</td>
      </tr>
      <tr>
          <td>Does reflashing prove the layout?</td>
          <td>No. Debug probes often skip unchanged sectors, so a reflash test can prove nothing.</td>
      </tr>
      <tr>
          <td>What changes above 16 MiB?</td>
          <td>4-byte addressing. On the i.MX RT it has to be set consistently in the FlexSPI configuration block, the flash driver&rsquo;s commands (also used for XIP reads) and the driver&rsquo;s size limit.</td>
      </tr>
  </tbody>
</table>
<h2 id="3-writing-the-flash-you-execute-from">3. Writing the flash you execute from</h2>
<p><strong>The mental model: while the flash is busy, it does not exist.</strong></p>
<p>On the i.MX RT the code usually runs straight out of QSPI NOR (execute in place, XIP). A
firmware update writes to the same chip. From the moment you send a program or erase
command until the flash clears its busy bit (WIP), <strong>nothing</strong> may be fetched from it: not
the interrupt handler, not the vector table, not a helper function the compiler decided
not to inline.</p>
<p>The failure is intermittent, which makes it nasty. The update crashes at a different block
on every run, and every bench session looks like a different bug.</p>
<figure class="diagram">
  <div class="diagram-svg" role="img" aria-label="Timeline of one flash write while executing in place: running from flash, then a busy window where nothing may be fetched from flash and the ISR, vectors and helpers run from RAM with interrupts masked, then invalidate the instruction cache, then resume; a page program is short, a sector erase takes tens of milliseconds and many times longer in the worst case"><svg xmlns="http://www.w3.org/2000/svg" viewBox="0 0 760 340">
<defs><marker id="m_cur" viewBox="0 0 10 10" refX="9" refY="5" markerWidth="7" markerHeight="7" orient="auto-start-reverse"><path d="M0,0 L10,5 L0,10 z" fill="currentColor"/></marker></defs>
<rect x="20" y="70" width="170" height="92" rx="8" fill="#10b981" fill-opacity="0.15" stroke="#10b981" stroke-width="1.5"/>
<text x="34" y="108" font-family="-apple-system, 'Segoe UI', Roboto, Helvetica, Arial, sans-serif" font-size="14" font-weight="bold" text-anchor="start" fill="currentColor">Running from</text>
<text x="34" y="127" font-family="-apple-system, 'Segoe UI', Roboto, Helvetica, Arial, sans-serif" font-size="14" font-weight="bold" text-anchor="start" fill="currentColor">flash (XIP)</text>
<rect x="196" y="70" width="330" height="92" rx="8" fill="#ef4444" fill-opacity="0.15" stroke="#ef4444" stroke-width="2"/>
<text x="210" y="96" font-family="-apple-system, 'Segoe UI', Roboto, Helvetica, Arial, sans-serif" font-size="14" font-weight="bold" text-anchor="start" fill="#ef4444">Flash busy (WIP = 1): it does not exist</text>
<text x="210" y="120" font-family="-apple-system, 'Segoe UI', Roboto, Helvetica, Arial, sans-serif" font-size="13" font-weight="normal" text-anchor="start" fill="currentColor">No fetch from flash at all: ISR, vector</text>
<text x="210" y="138" font-family="-apple-system, 'Segoe UI', Roboto, Helvetica, Arial, sans-serif" font-size="13" font-weight="normal" text-anchor="start" fill="currentColor">table and helpers must run from RAM;</text>
<text x="210" y="156" font-family="-apple-system, 'Segoe UI', Roboto, Helvetica, Arial, sans-serif" font-size="13" font-weight="normal" text-anchor="start" fill="currentColor">interrupts masked</text>
<rect x="532" y="70" width="102" height="92" rx="8" fill="#f59e0b" fill-opacity="0.15" stroke="#f59e0b" stroke-width="1.5"/>
<text x="544" y="108" font-family="-apple-system, 'Segoe UI', Roboto, Helvetica, Arial, sans-serif" font-size="14" font-weight="bold" text-anchor="start" fill="currentColor">Invalidate</text>
<text x="544" y="127" font-family="-apple-system, 'Segoe UI', Roboto, Helvetica, Arial, sans-serif" font-size="14" font-weight="bold" text-anchor="start" fill="currentColor">I-cache</text>
<rect x="640" y="70" width="100" height="92" rx="8" fill="#10b981" fill-opacity="0.15" stroke="#10b981" stroke-width="1.5"/>
<text x="652" y="108" font-family="-apple-system, 'Segoe UI', Roboto, Helvetica, Arial, sans-serif" font-size="14" font-weight="bold" text-anchor="start" fill="currentColor">Resume</text>
<text x="652" y="127" font-family="-apple-system, 'Segoe UI', Roboto, Helvetica, Arial, sans-serif" font-size="14" font-weight="bold" text-anchor="start" fill="currentColor">XIP</text>
<line x1="196" y1="40" x2="196" y2="68" stroke="currentColor" stroke-width="1.6" stroke-opacity="0.7" marker-end="url(#m_cur)"/>
<text x="196" y="34" font-family="-apple-system, 'Segoe UI', Roboto, Helvetica, Arial, sans-serif" font-size="12.5" font-weight="normal" text-anchor="middle" fill="currentColor">program / erase command</text>
<line x1="526" y1="40" x2="526" y2="68" stroke="currentColor" stroke-width="1.6" stroke-opacity="0.7" marker-end="url(#m_cur)"/>
<text x="560" y="34" font-family="-apple-system, 'Segoe UI', Roboto, Helvetica, Arial, sans-serif" font-size="12.5" font-weight="normal" text-anchor="middle" fill="currentColor">busy bit clears</text>
<text x="210" y="192" font-family="-apple-system, 'Segoe UI', Roboto, Helvetica, Arial, sans-serif" font-size="13" font-weight="bold" text-anchor="start" fill="currentColor">How long is the window? Check your datasheet:</text>
<text x="210" y="212" font-family="-apple-system, 'Segoe UI', Roboto, Helvetica, Arial, sans-serif" font-size="13" font-weight="bold" text-anchor="start" fill="currentColor">page program</text>
<text x="310" y="212" font-family="-apple-system, 'Segoe UI', Roboto, Helvetica, Arial, sans-serif" font-size="13" font-weight="normal" text-anchor="start" fill="currentColor">short</text>
<text x="210" y="230" font-family="-apple-system, 'Segoe UI', Roboto, Helvetica, Arial, sans-serif" font-size="13" font-weight="bold" text-anchor="start" fill="currentColor">sector erase</text>
<text x="310" y="230" font-family="-apple-system, 'Segoe UI', Roboto, Helvetica, Arial, sans-serif" font-size="13" font-weight="normal" text-anchor="start" fill="currentColor">tens of ms, typical</text>
<text x="210" y="248" font-family="-apple-system, 'Segoe UI', Roboto, Helvetica, Arial, sans-serif" font-size="13" font-weight="bold" text-anchor="start" fill="currentColor">worst case</text>
<text x="310" y="248" font-family="-apple-system, 'Segoe UI', Roboto, Helvetica, Arial, sans-serif" font-size="13" font-weight="normal" text-anchor="start" fill="currentColor">many times the typical value</text>
<text x="540" y="192" font-family="-apple-system, 'Segoe UI', Roboto, Helvetica, Arial, sans-serif" font-size="12.5" font-weight="normal" text-anchor="start" fill="currentColor" fill-opacity="0.8">Cortex-M7 speculative</text>
<text x="540" y="209" font-family="-apple-system, 'Segoe UI', Roboto, Helvetica, Arial, sans-serif" font-size="12.5" font-weight="normal" text-anchor="start" fill="currentColor" fill-opacity="0.8">fetches may have pulled</text>
<text x="540" y="226" font-family="-apple-system, 'Segoe UI', Roboto, Helvetica, Arial, sans-serif" font-size="12.5" font-weight="normal" text-anchor="start" fill="currentColor" fill-opacity="0.8">stale lines in the cache</text>
<rect x="20" y="286" width="720" height="38" rx="8" fill="#ef4444" fill-opacity="0.12" stroke="#ef4444" stroke-width="1.5"/>
<text x="36" y="310" font-family="-apple-system, 'Segoe UI', Roboto, Helvetica, Arial, sans-serif" font-size="13.5" font-weight="bold" text-anchor="start" fill="currentColor">Without this: a crash at a random block, different on every run</text>
</svg>
</div>
  <figcaption>One write to the flash you execute from. While the busy bit is set, the flash does not exist for the CPU.</figcaption>
</figure>

<p>What fixes it:</p>
<ol>
<li>Run the erase and program routines, and everything they call, from RAM.</li>
<li>Keep interrupts masked while the flash is busy. Check the datasheet for how long that
is: a page program is short, but a sector erase takes tens of milliseconds, and the
worst case is many times the typical value.</li>
<li><strong>Invalidate the instruction cache</strong> afterwards. The Cortex-M7 fetches speculatively and
can have pulled in stale or garbage lines during the write.</li>
</ol>
<div class="callout callout--warn">
  <p class="callout__title"><span class="callout__icon" aria-hidden="true">⚠️</span>An unknown opcode fails silently</p>
  <div class="callout__body">
A NOR flash ignores a command it does not support. Send the wrong erase opcode and the erase
&ldquo;succeeds&rdquo; instantly. If an erase finishes far faster than the datasheet&rsquo;s typical time,
suspect that the command never ran.
  </div>
</div>

<h2 id="4-treat-the-field-update-as-a-protocol">4. Treat the field update as a protocol</h2>
<p><strong>The mental model: every update request ends in exactly one outcome, and the new version
number is the only proof.</strong></p>
<p>A device that reports &ldquo;update done&rdquo; has proven nothing. It might reboot into the old image,
or not reboot at all. Update logic only gets robust once you treat it as a protocol with
explicit rules:</p>
<ul>
<li>Every request ends in <strong>exactly one</strong> outcome: success, a classified failure, or a
timeout. Never silence, never two answers.</li>
<li>After the restart, poll the device for its version with a fixed interval and a hard
limit. Only the new version string counts as success.</li>
<li>Retries must fit the device&rsquo;s own limits. If the target closes an idle session after a
timeout, all your retries together must finish inside it, or the retries themselves
break the session.</li>
</ul>
<p>The bench catches what unit tests cannot: a bootloader step on the target that takes far
longer than you assumed, or a frame buffer that does not fit on the task stack you gave it.
Measure step times on hardware and check stack high-water marks before you trust the
timeouts.</p>
<h2 id="5-production-ready-means-surviving-the-power-cut">5. Production-ready means surviving the power cut</h2>
<p><strong>The mental model: measure the hold-up time before you design the software around it.</strong></p>
<p>When the supply drops, the board runs a little longer on its capacitors. That hold-up time
is a hardware property: measure it on a real board, in the worst-case load state, before
you design the power-fail path. One wrong part in the hold-up circuit can bring it close
to zero.</p>
<p>That number decides the software design. If the worst-case flash erase (lesson 3) is
longer than the hold-up time, <strong>the emergency path must never start a flash operation</strong>. It
only finishes or abandons what is already running, and closes the file system.</p>
<figure class="diagram">
  <div class="diagram-svg" role="img" aria-label="Power-fail timeline: the supply drops, the power-fail interrupt fires and the measured hold-up window starts; closing the file system and a typical erase fit in the window, a worst-case erase can outlast it; so the emergency path never starts a flash operation"><svg xmlns="http://www.w3.org/2000/svg" viewBox="0 0 760 372">
<line x1="230" y1="40" x2="230" y2="254" stroke="#f59e0b" stroke-width="2"/>
<text x="230" y="22" font-family="-apple-system, 'Segoe UI', Roboto, Helvetica, Arial, sans-serif" font-size="12.5" font-weight="bold" text-anchor="middle" fill="currentColor">power-fail interrupt</text>
<line x1="590" y1="40" x2="590" y2="254" stroke="#ef4444" stroke-width="2" stroke-dasharray="5 4"/>
<text x="590" y="22" font-family="-apple-system, 'Segoe UI', Roboto, Helvetica, Arial, sans-serif" font-size="12.5" font-weight="bold" text-anchor="middle" fill="currentColor">brown-out: CPU stops</text>
<rect x="230" y="48" width="360" height="34" rx="8" fill="#10b981" fill-opacity="0.18" stroke="#10b981" stroke-width="1.5"/>
<text x="410.0" y="70" font-family="-apple-system, 'Segoe UI', Roboto, Helvetica, Arial, sans-serif" font-size="13" font-weight="bold" text-anchor="middle" fill="currentColor">hold-up time: measure it on the real board</text>
<text x="20" y="70" font-family="-apple-system, 'Segoe UI', Roboto, Helvetica, Arial, sans-serif" font-size="13.5" font-weight="bold" text-anchor="start" fill="currentColor">Capacitors</text>
<text x="20" y="124" font-family="-apple-system, 'Segoe UI', Roboto, Helvetica, Arial, sans-serif" font-size="13.5" font-weight="normal" text-anchor="start" fill="currentColor">Close the file system</text>
<rect x="230" y="108" width="30" height="24" rx="3" fill="#3b82f6" fill-opacity="0.35" stroke="#3b82f6" stroke-width="1.5"/>
<text x="270" y="125" font-family="-apple-system, 'Segoe UI', Roboto, Helvetica, Arial, sans-serif" font-size="12.5" font-weight="bold" text-anchor="start" fill="#10b981">fits</text>
<text x="20" y="172" font-family="-apple-system, 'Segoe UI', Roboto, Helvetica, Arial, sans-serif" font-size="13.5" font-weight="normal" text-anchor="start" fill="currentColor">Typical sector erase</text>
<rect x="230" y="156" width="120" height="24" rx="3" fill="#f59e0b" fill-opacity="0.35" stroke="#f59e0b" stroke-width="1.5"/>
<text x="360" y="173" font-family="-apple-system, 'Segoe UI', Roboto, Helvetica, Arial, sans-serif" font-size="12.5" font-weight="bold" text-anchor="start" fill="#10b981">fits</text>
<text x="20" y="220" font-family="-apple-system, 'Segoe UI', Roboto, Helvetica, Arial, sans-serif" font-size="13.5" font-weight="bold" text-anchor="start" fill="currentColor">Worst-case sector erase</text>
<rect x="230" y="204" width="470" height="24" rx="3" fill="#ef4444" fill-opacity="0.35" stroke="#ef4444" stroke-width="1.5"/>
<text x="690" y="221" font-family="-apple-system, 'Segoe UI', Roboto, Helvetica, Arial, sans-serif" font-size="12.5" font-weight="bold" text-anchor="end" fill="currentColor">outlasts the hold-up</text>
<text x="745" y="254" font-family="-apple-system, 'Segoe UI', Roboto, Helvetica, Arial, sans-serif" font-size="11.5" font-weight="normal" text-anchor="end" fill="currentColor" fill-opacity="0.6" font-style="italic">not to scale</text>
<rect x="20" y="270" width="720" height="88" rx="8" fill="#ef4444" fill-opacity="0.08" stroke="#ef4444" stroke-width="1"/>
<text x="36" y="294" font-family="-apple-system, 'Segoe UI', Roboto, Helvetica, Arial, sans-serif" font-size="14" font-weight="bold" text-anchor="start" fill="currentColor">If a worst-case erase can outlast the hold-up time:</text>
<text x="36" y="318" font-family="-apple-system, 'Segoe UI', Roboto, Helvetica, Arial, sans-serif" font-size="13.5" font-weight="normal" text-anchor="start" fill="currentColor">the power-fail path never starts a flash operation. It only finishes or abandons</text>
<text x="36" y="338" font-family="-apple-system, 'Segoe UI', Roboto, Helvetica, Arial, sans-serif" font-size="13.5" font-weight="normal" text-anchor="start" fill="currentColor">what is already running, and closes the file system.</text>
</svg>
</div>
  <figcaption>The hold-up window against flash timing. A worst-case erase may not fit, so the power-fail path never starts one.</figcaption>
</figure>

<p>Then test it properly. Cut power repeatedly while the firmware writes continuously, and
check after each cut that the file system is consistent and the last committed version of
each file survived.</p>
<div class="callout callout--check">
  <p class="callout__title"><span class="callout__icon" aria-hidden="true">✅</span>Test the test</p>
  <div class="callout__body">
A power-cut test that never cuts during a write proves nothing. Count how many cuts actually
landed mid-write before you trust the pass rate.
  </div>
</div>

<h2 id="summary">Summary</h2>
<ol>
<li><strong>Own what the ROM did for you.</strong> Once a bootloader sits in front, it has to set up
memory and clocks.</li>
<li><strong>Measure, then make it structural.</strong> Flash size, step timings and hold-up time are
measurements; a header, an assert and a CI test keep them true.</li>
<li><strong>Design for the worst case.</strong> The flash write path runs from RAM, updates end in exactly
one outcome, and the power-fail path never starts an erase.</li>
</ol>
<div class="callout callout--note">
  <p class="callout__title"><span class="callout__icon" aria-hidden="true">📝</span>Sources</p>
  <div class="callout__body">
<ul>
<li>NXP, <em>i.MX RT1060 Processor Reference Manual</em>: boot ROM, IVT/DCD, FlexSPI.</li>
<li>MCUboot documentation: image trailer and the direct-XIP / revert modes.</li>
<li>The datasheet of your QSPI NOR, for its typical and maximum program and erase times.</li>
</ul>

  </div>
</div>

<p>💬 Questions, corrections, or your own war story? Leave a comment below.</p>
]]></content:encoded></item></channel></rss>