<p>Philipp Deppenwiese <strong>merged</strong> this change.</p><p><a href="https://review.coreboot.org/c/coreboot/+/29532">View Change</a></p><div style="white-space:pre-wrap">Approvals:
  build bot (Jenkins): Verified
  Philipp Deppenwiese: Looks good to me, approved

</div><pre style="font-family: monospace,monospace; white-space: pre-wrap;">mb/opencellular/elgon: Enable write protection<br><br>* Verify the flash write protection on each boot<br>* Program non-volatile write protection on first boot<br><br>Tested using I715791b8ae5d1db1ef587321ae5c9daa10eb7dbc.<br><br>The bootblock is write-protected as long as the #WP pin is asserted low:<br>* Reprogramming of the status register fails.<br>* Trying to write to WP_RO region fails.<br><br>Programming the WP_RO is only possible if #WP pin is high.<br><br>Change-Id: I6a940c69ecb1dfd9704b2101c263570bebc5540e<br>Signed-off-by: Patrick Rudolph <patrick.rudolph@9elements.com><br>Reviewed-on: https://review.coreboot.org/c/29532<br>Tested-by: build bot (Jenkins) <no-reply@coreboot.org><br>Reviewed-by: Philipp Deppenwiese <zaolin.daisuki@gmail.com><br>---<br>M src/mainboard/opencellular/elgon/bootblock.c<br>1 file changed, 44 insertions(+), 1 deletion(-)<br><br></pre><pre style="font-family: monospace,monospace; white-space: pre-wrap;"><span>diff --git a/src/mainboard/opencellular/elgon/bootblock.c b/src/mainboard/opencellular/elgon/bootblock.c</span><br><span>index e6109f1..9dfd1b8 100644</span><br><span>--- a/src/mainboard/opencellular/elgon/bootblock.c</span><br><span>+++ b/src/mainboard/opencellular/elgon/bootblock.c</span><br><span>@@ -18,6 +18,9 @@</span><br><span> #include <soc/spi.h></span><br><span> #include <soc/uart.h></span><br><span> #include <soc/gpio.h></span><br><span style="color: hsl(120, 100%, 40%);">+#include <spi_flash.h></span><br><span style="color: hsl(120, 100%, 40%);">+#include <console/console.h></span><br><span style="color: hsl(120, 100%, 40%);">+#include <fmap.h></span><br><span> #include "mainboard.h"</span><br><span> </span><br><span> void bootblock_mainboard_early_init(void)</span><br><span>@@ -49,8 +52,48 @@</span><br><span>    gpio_output(ELGON_GPIO_SPI_MUX, 1);</span><br><span> }</span><br><span> </span><br><span style="color: hsl(120, 100%, 40%);">+/**</span><br><span style="color: hsl(120, 100%, 40%);">+ * Handle flash write protection.</span><br><span style="color: hsl(120, 100%, 40%);">+ * This code verifies the write-protection on each boot.</span><br><span style="color: hsl(120, 100%, 40%);">+ * Enabling the write protection does only run on the first boot.</span><br><span style="color: hsl(120, 100%, 40%);">+ * An error is fatal as it breaks the Chain Of Trust.</span><br><span style="color: hsl(120, 100%, 40%);">+ */</span><br><span style="color: hsl(120, 100%, 40%);">+static void protect_ro_rgn_spi_flash(void)</span><br><span style="color: hsl(120, 100%, 40%);">+{</span><br><span style="color: hsl(120, 100%, 40%);">+  const struct spi_flash *flash = boot_device_spi_flash();</span><br><span style="color: hsl(120, 100%, 40%);">+      const char *fmapname = "WP_RO";</span><br><span style="color: hsl(120, 100%, 40%);">+     struct region ro_rgn;</span><br><span style="color: hsl(120, 100%, 40%);">+</span><br><span style="color: hsl(120, 100%, 40%);">+       if (fmap_locate_area(fmapname, &ro_rgn)) {</span><br><span style="color: hsl(120, 100%, 40%);">+                printk(BIOS_ERR, "%s: No %s FMAP section.\n", __func__,</span><br><span style="color: hsl(120, 100%, 40%);">+                     fmapname);</span><br><span style="color: hsl(120, 100%, 40%);">+            die("Can't verify flash protections!");</span><br><span style="color: hsl(120, 100%, 40%);">+ }</span><br><span style="color: hsl(120, 100%, 40%);">+</span><br><span style="color: hsl(120, 100%, 40%);">+   u8 reg8 = 0;</span><br><span style="color: hsl(120, 100%, 40%);">+  spi_flash_status(flash, &reg8);</span><br><span style="color: hsl(120, 100%, 40%);">+</span><br><span style="color: hsl(120, 100%, 40%);">+ /* Check if SRP0 is set and RO region is protected */</span><br><span style="color: hsl(120, 100%, 40%);">+ if (!(reg8 & 0x80) ||</span><br><span style="color: hsl(120, 100%, 40%);">+         spi_flash_is_write_protected(flash, &ro_rgn) != 1) {</span><br><span style="color: hsl(120, 100%, 40%);">+          printk(BIOS_WARNING, "%s: FMAP section %s is not write-protected\n",</span><br><span style="color: hsl(120, 100%, 40%);">+                         __func__, fmapname);</span><br><span style="color: hsl(120, 100%, 40%);">+</span><br><span style="color: hsl(120, 100%, 40%);">+               /*</span><br><span style="color: hsl(120, 100%, 40%);">+            * Need to protect flash region :</span><br><span style="color: hsl(120, 100%, 40%);">+              * WP_RO read only and use /WP pin</span><br><span style="color: hsl(120, 100%, 40%);">+             * non-volatile programming</span><br><span style="color: hsl(120, 100%, 40%);">+            */</span><br><span style="color: hsl(120, 100%, 40%);">+            if (spi_flash_set_write_protected(flash, &ro_rgn, 1,</span><br><span style="color: hsl(120, 100%, 40%);">+                  SPI_WRITE_PROTECTION_PIN) != 0)</span><br><span style="color: hsl(120, 100%, 40%);">+                   die("Failed to write-protect WP_RO region!");</span><br><span style="color: hsl(120, 100%, 40%);">+       }</span><br><span style="color: hsl(120, 100%, 40%);">+     printk(BIOS_INFO, "%s: FMAP section %s is write-protected\n",</span><br><span style="color: hsl(120, 100%, 40%);">+              __func__, fmapname);</span><br><span style="color: hsl(120, 100%, 40%);">+}</span><br><span style="color: hsl(120, 100%, 40%);">+</span><br><span> void bootblock_mainboard_init(void)</span><br><span> {</span><br><span>         configure_spi_flash();</span><br><span style="color: hsl(0, 100%, 40%);">-  // FIXME: Check SPI flash WP bits</span><br><span style="color: hsl(120, 100%, 40%);">+     protect_ro_rgn_spi_flash();</span><br><span> }</span><br><span></span><br></pre><p>To view, visit <a href="https://review.coreboot.org/c/coreboot/+/29532">change 29532</a>. To unsubscribe, or for help writing mail filters, visit <a href="https://review.coreboot.org/settings">settings</a>.</p><div itemscope itemtype="http://schema.org/EmailMessage"><div itemscope itemprop="action" itemtype="http://schema.org/ViewAction"><link itemprop="url" href="https://review.coreboot.org/c/coreboot/+/29532"/><meta itemprop="name" content="View Change"/></div></div>

<div style="display:none"> Gerrit-Project: coreboot </div>
<div style="display:none"> Gerrit-Branch: master </div>
<div style="display:none"> Gerrit-Change-Id: I6a940c69ecb1dfd9704b2101c263570bebc5540e </div>
<div style="display:none"> Gerrit-Change-Number: 29532 </div>
<div style="display:none"> Gerrit-PatchSet: 3 </div>
<div style="display:none"> Gerrit-Owner: Patrick Rudolph <patrick.rudolph@9elements.com> </div>
<div style="display:none"> Gerrit-Reviewer: David Hendricks <david.hendricks@gmail.com> </div>
<div style="display:none"> Gerrit-Reviewer: Patrick Rudolph <patrick.rudolph@9elements.com> </div>
<div style="display:none"> Gerrit-Reviewer: Philipp Deppenwiese <zaolin.daisuki@gmail.com> </div>
<div style="display:none"> Gerrit-Reviewer: build bot (Jenkins) <no-reply@coreboot.org> </div>
<div style="display:none"> Gerrit-MessageType: merged </div>