<p>Philipp Deppenwiese has uploaded this change for <strong>review</strong>.</p><p><a href="https://review.coreboot.org/25283">View Change</a></p><pre style="font-family: monospace,monospace; white-space: pre-wrap;">security/flash: Refactor flash protection logic<br><br>Change-Id: Ic0a859e6ce9aba32278f666a38a952ec8b4c1b4d<br>Signed-off-by: zaolin <zaolin@das-labor.org><br>---<br>M src/drivers/mrc_cache/mrc_cache.c<br>M src/drivers/spi/spi_flash.c<br>M src/security/flash/Makefile.inc<br>M src/security/flash/flash.c<br>M src/security/flash/flash.h<br>A src/security/flash/wp.c<br>M src/security/vboot/vboot_handoff.c<br>7 files changed, 94 insertions(+), 33 deletions(-)<br><br></pre><pre style="font-family: monospace,monospace; white-space: pre-wrap;">git pull ssh://review.coreboot.org:29418/coreboot refs/changes/83/25283/1</pre><pre style="font-family: monospace,monospace; white-space: pre-wrap;"><span>diff --git a/src/drivers/mrc_cache/mrc_cache.c b/src/drivers/mrc_cache/mrc_cache.c</span><br><span>index 1850034..2a1c48c 100644</span><br><span>--- a/src/drivers/mrc_cache/mrc_cache.c</span><br><span>+++ b/src/drivers/mrc_cache/mrc_cache.c</span><br><span>@@ -446,13 +446,21 @@</span><br><span> /* Read flash status register to determine if write protect is active */</span><br><span> static int nvm_is_write_protected(void)</span><br><span> {</span><br><span style="color: hsl(120, 100%, 40%);">+     int result;</span><br><span style="color: hsl(120, 100%, 40%);">+</span><br><span>        if (!IS_ENABLED(CONFIG_CHROMEOS))</span><br><span>            return 0;</span><br><span> </span><br><span>        if (!IS_ENABLED(CONFIG_BOOT_DEVICE_SPI_FLASH))</span><br><span>               return 0;</span><br><span> </span><br><span style="color: hsl(0, 100%, 40%);">-   return get_write_protect_state();</span><br><span style="color: hsl(120, 100%, 40%);">+     result = get_write_protect_state(boot_device_spi_flash());</span><br><span style="color: hsl(120, 100%, 40%);">+    if (result < 0)</span><br><span style="color: hsl(120, 100%, 40%);">+            printk(BIOS_ERR, "SPI flash protection failed!\n");</span><br><span style="color: hsl(120, 100%, 40%);">+ else</span><br><span style="color: hsl(120, 100%, 40%);">+          printk(BIOS_DEBUG, "SPI flash protection: WP=%d\n", result);</span><br><span style="color: hsl(120, 100%, 40%);">+</span><br><span style="color: hsl(120, 100%, 40%);">+      return result;</span><br><span> }</span><br><span> </span><br><span> /* Apply protection to a range of flash */</span><br><span>diff --git a/src/drivers/spi/spi_flash.c b/src/drivers/spi/spi_flash.c</span><br><span>index d20773f..0e269dd 100644</span><br><span>--- a/src/drivers/spi/spi_flash.c</span><br><span>+++ b/src/drivers/spi/spi_flash.c</span><br><span>@@ -419,7 +419,7 @@</span><br><span>         if (!region_is_subregion(&flash_region, region))</span><br><span>                 return -1;</span><br><span> </span><br><span style="color: hsl(0, 100%, 40%);">-  if (flash->ops->get_write_protection) {</span><br><span style="color: hsl(120, 100%, 40%);">+ if (!flash->ops->get_write_protection) {</span><br><span>               printk(BIOS_WARNING, "SPI: Write-protection gathering not "</span><br><span>                       "implemented for this vendor.\n");</span><br><span>          return 0;</span><br><span>@@ -442,7 +442,7 @@</span><br><span>      if (!region_is_subregion(&flash_region, region))</span><br><span>                 return -1;</span><br><span> </span><br><span style="color: hsl(0, 100%, 40%);">-  if (flash->ops->set_write_protection) {</span><br><span style="color: hsl(120, 100%, 40%);">+ if (!flash->ops->set_write_protection) {</span><br><span>               printk(BIOS_WARNING, "SPI: Setting write-protection is not "</span><br><span>                                    "implemented for this vendor.\n");</span><br><span>            wp_result = -1;</span><br><span>@@ -450,7 +450,7 @@</span><br><span>                wp_result = flash->ops->set_write_protection(flash, region);</span><br><span>   }</span><br><span> </span><br><span style="color: hsl(0, 100%, 40%);">-   if (flash->ops->lock_status_register) {</span><br><span style="color: hsl(120, 100%, 40%);">+ if (!flash->ops->lock_status_register) {</span><br><span>               printk(BIOS_WARNING, "SPI: Locking status register is not "</span><br><span>                                     "implemented for this vendor.\n");</span><br><span>            st_result = 0;</span><br><span>diff --git a/src/security/flash/Makefile.inc b/src/security/flash/Makefile.inc</span><br><span>index 50d4a7a..f4bb413 100644</span><br><span>--- a/src/security/flash/Makefile.inc</span><br><span>+++ b/src/security/flash/Makefile.inc</span><br><span>@@ -1,5 +1,7 @@</span><br><span> ## flash</span><br><span> </span><br><span style="color: hsl(0, 100%, 40%);">-verstage-y += flash.c</span><br><span style="color: hsl(0, 100%, 40%);">-romstage-y += flash.c</span><br><span> ramstage-y += flash.c</span><br><span style="color: hsl(120, 100%, 40%);">+</span><br><span style="color: hsl(120, 100%, 40%);">+ramstage-y += wp.c</span><br><span style="color: hsl(120, 100%, 40%);">+romstage-y += wp.c</span><br><span style="color: hsl(120, 100%, 40%);">+verstage-y += wp.c</span><br><span>diff --git a/src/security/flash/flash.c b/src/security/flash/flash.c</span><br><span>index 49a2980..608d6d2 100644</span><br><span>--- a/src/security/flash/flash.c</span><br><span>+++ b/src/security/flash/flash.c</span><br><span>@@ -13,44 +13,37 @@</span><br><span>  * GNU General Public License for more details.</span><br><span>  */</span><br><span> </span><br><span style="color: hsl(120, 100%, 40%);">+#include <bootstate.h></span><br><span> #include <commonlib/region.h></span><br><span style="color: hsl(120, 100%, 40%);">+#include <console/console.h></span><br><span> #include <fmap.h></span><br><span> #include <security/flash/flash.h></span><br><span style="color: hsl(0, 100%, 40%);">-#include <spi_flash.h></span><br><span> #include <string.h></span><br><span> </span><br><span> #define FMAP_VBOOT_RO_REGION "WP_RO"</span><br><span> #define FMAP_BIOS_REGION "BIOS"</span><br><span> #define FMAP_FLASH_REGION "FLASH"</span><br><span> </span><br><span style="color: hsl(0, 100%, 40%);">-__attribute__((weak)) int get_spi_wp_pin_state(void) { return 0; }</span><br><span style="color: hsl(0, 100%, 40%);">-</span><br><span style="color: hsl(0, 100%, 40%);">-int set_write_protect_enabled(void)</span><br><span style="color: hsl(120, 100%, 40%);">+int set_write_protect_enabled(const struct spi_flash *flash)</span><br><span> {</span><br><span>    int result = -1;</span><br><span>     struct region region;</span><br><span> </span><br><span>    if (IS_ENABLED(CONFIG_FLASH_SPI_PROTECTIONS)) {</span><br><span style="color: hsl(0, 100%, 40%);">-         struct spi_flash flash;</span><br><span style="color: hsl(0, 100%, 40%);">-</span><br><span style="color: hsl(0, 100%, 40%);">-         spi_init();</span><br><span style="color: hsl(0, 100%, 40%);">-             if (spi_flash_probe(0, 0, &flash))</span><br><span style="color: hsl(0, 100%, 40%);">-                  return result;</span><br><span style="color: hsl(0, 100%, 40%);">-</span><br><span>               if (IS_ENABLED(CONFIG_FLASH_MODE_VBOOT)) {</span><br><span>                   if (fmap_locate_area(FMAP_VBOOT_RO_REGION, &region) ==</span><br><span>                       0) {</span><br><span style="color: hsl(0, 100%, 40%);">-                                result = spi_flash_set_write_protected(&flash,</span><br><span style="color: hsl(120, 100%, 40%);">+                            result = spi_flash_set_write_protected(flash,</span><br><span>                                                                       &region);</span><br><span>                         }</span><br><span>            } else if (IS_ENABLED(CONFIG_FLASH_MODE_BIOS)) {</span><br><span>                     if (fmap_locate_area(FMAP_BIOS_REGION, &region) == 0) {</span><br><span style="color: hsl(0, 100%, 40%);">-                             result = spi_flash_set_write_protected(&flash,</span><br><span style="color: hsl(120, 100%, 40%);">+                            result = spi_flash_set_write_protected(flash,</span><br><span>                                                                       &region);</span><br><span>                         }</span><br><span>            } else if (IS_ENABLED(CONFIG_FLASH_MODE_EVERYTHING)) {</span><br><span>                       if (fmap_locate_area(FMAP_FLASH_REGION, &region) == 0) {</span><br><span style="color: hsl(0, 100%, 40%);">-                            result = spi_flash_set_write_protected(&flash,</span><br><span style="color: hsl(120, 100%, 40%);">+                            result = spi_flash_set_write_protected(flash,</span><br><span>                                                                       &region);</span><br><span>                         }</span><br><span>            }</span><br><span>@@ -60,33 +53,27 @@</span><br><span>      return result;</span><br><span> }</span><br><span> </span><br><span style="color: hsl(0, 100%, 40%);">-int get_write_protect_state(void)</span><br><span style="color: hsl(120, 100%, 40%);">+int get_write_protect_state(const struct spi_flash *flash)</span><br><span> {</span><br><span>    int result = -1;</span><br><span>     struct region region;</span><br><span> </span><br><span>    if (IS_ENABLED(CONFIG_FLASH_SPI_PROTECTIONS)) {</span><br><span style="color: hsl(0, 100%, 40%);">-         struct spi_flash flash;</span><br><span style="color: hsl(0, 100%, 40%);">-</span><br><span style="color: hsl(0, 100%, 40%);">-         spi_init();</span><br><span style="color: hsl(0, 100%, 40%);">-             if (spi_flash_probe(0, 0, &flash))</span><br><span style="color: hsl(0, 100%, 40%);">-                  return result;</span><br><span style="color: hsl(0, 100%, 40%);">-</span><br><span>               if (IS_ENABLED(CONFIG_FLASH_MODE_VBOOT)) {</span><br><span>                   if (fmap_locate_area(FMAP_VBOOT_RO_REGION, &region) ==</span><br><span>                       0) {</span><br><span style="color: hsl(0, 100%, 40%);">-                                result = spi_flash_is_write_protected(&flash,</span><br><span style="color: hsl(120, 100%, 40%);">+                             result = spi_flash_is_write_protected(flash,</span><br><span>                                                                       &region);</span><br><span style="color: hsl(0, 100%, 40%);">-                             result &= get_spi_wp_pin_state();</span><br><span style="color: hsl(120, 100%, 40%);">+                         result = result && get_spi_wp_pin_state();</span><br><span>                   }</span><br><span>            } else if (IS_ENABLED(CONFIG_FLASH_MODE_BIOS)) {</span><br><span>                     if (fmap_locate_area(FMAP_BIOS_REGION, &region) == 0) {</span><br><span style="color: hsl(0, 100%, 40%);">-                             result = spi_flash_is_write_protected(&flash,</span><br><span style="color: hsl(120, 100%, 40%);">+                             result = spi_flash_is_write_protected(flash,</span><br><span>                                                                       &region);</span><br><span>                  }</span><br><span>            } else if (IS_ENABLED(CONFIG_FLASH_MODE_EVERYTHING)) {</span><br><span>                       if (fmap_locate_area(FMAP_FLASH_REGION, &region) == 0) {</span><br><span style="color: hsl(0, 100%, 40%);">-                            result = spi_flash_is_write_protected(&flash,</span><br><span style="color: hsl(120, 100%, 40%);">+                             result = spi_flash_is_write_protected(flash,</span><br><span>                                                                       &region);</span><br><span>                  }</span><br><span>            }</span><br><span>@@ -95,3 +82,47 @@</span><br><span> </span><br><span>   return result;</span><br><span> }</span><br><span style="color: hsl(120, 100%, 40%);">+</span><br><span style="color: hsl(120, 100%, 40%);">+static void init_flash_write_protection(void *unused)</span><br><span style="color: hsl(120, 100%, 40%);">+{</span><br><span style="color: hsl(120, 100%, 40%);">+       if (IS_ENABLED(CONFIG_BOOT_DEVICE_SPI_FLASH)) {</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%);">+</span><br><span style="color: hsl(120, 100%, 40%);">+            int result = get_write_protect_state(flash);</span><br><span style="color: hsl(120, 100%, 40%);">+          if (result == 0) {</span><br><span style="color: hsl(120, 100%, 40%);">+                    printk(</span><br><span style="color: hsl(120, 100%, 40%);">+                           BIOS_INFO,</span><br><span style="color: hsl(120, 100%, 40%);">+                            "FLASH: Write protection disable, activating..\n");</span><br><span style="color: hsl(120, 100%, 40%);">+                     set_write_protect_enabled(flash);</span><br><span style="color: hsl(120, 100%, 40%);">+             } else if (result < 0) {</span><br><span style="color: hsl(120, 100%, 40%);">+                   printk(BIOS_ERR, "FLASH: Error getting WP state.\n");</span><br><span style="color: hsl(120, 100%, 40%);">+               } else {</span><br><span style="color: hsl(120, 100%, 40%);">+                      printk(BIOS_INFO,</span><br><span style="color: hsl(120, 100%, 40%);">+                            "FLASH: Write protection already enabled.\n");</span><br><span style="color: hsl(120, 100%, 40%);">+               }</span><br><span style="color: hsl(120, 100%, 40%);">+     } else {</span><br><span style="color: hsl(120, 100%, 40%);">+              struct spi_flash flash;</span><br><span style="color: hsl(120, 100%, 40%);">+</span><br><span style="color: hsl(120, 100%, 40%);">+             spi_init();</span><br><span style="color: hsl(120, 100%, 40%);">+           if (spi_flash_probe(0, 0, &flash))</span><br><span style="color: hsl(120, 100%, 40%);">+                        return;</span><br><span style="color: hsl(120, 100%, 40%);">+</span><br><span style="color: hsl(120, 100%, 40%);">+             int result = get_write_protect_state(flash);</span><br><span style="color: hsl(120, 100%, 40%);">+          if (result == 0) {</span><br><span style="color: hsl(120, 100%, 40%);">+                    printk(</span><br><span style="color: hsl(120, 100%, 40%);">+                           BIOS_INFO,</span><br><span style="color: hsl(120, 100%, 40%);">+                            "FLASH: Write protection disable, activating..\n");</span><br><span style="color: hsl(120, 100%, 40%);">+                     set_write_protect_enabled(&flash);</span><br><span style="color: hsl(120, 100%, 40%);">+                } else if (result < 0) {</span><br><span style="color: hsl(120, 100%, 40%);">+                   printk(BIOS_ERR, "FLASH: Error getting WP state.\n")</span><br><span style="color: hsl(120, 100%, 40%);">+                } else {</span><br><span style="color: hsl(120, 100%, 40%);">+                      printk(BIOS_INFO,</span><br><span style="color: hsl(120, 100%, 40%);">+                            "FLASH: Write protection already enabled.\n");</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%);">+}</span><br><span style="color: hsl(120, 100%, 40%);">+</span><br><span style="color: hsl(120, 100%, 40%);">+BOOT_STATE_INIT_ENTRY(BS_POST_DEVICE, BS_ON_ENTRY, init_flash_write_protection,</span><br><span style="color: hsl(120, 100%, 40%);">+                     NULL);</span><br><span style="color: hsl(120, 100%, 40%);">+BOOT_STATE_INIT_ENTRY(BS_OS_RESUME, BS_ON_ENTRY, init_flash_write_protection,</span><br><span style="color: hsl(120, 100%, 40%);">+                 NULL);</span><br><span>diff --git a/src/security/flash/flash.h b/src/security/flash/flash.h</span><br><span>index 0d113c8..4c83495 100644</span><br><span>--- a/src/security/flash/flash.h</span><br><span>+++ b/src/security/flash/flash.h</span><br><span>@@ -16,9 +16,11 @@</span><br><span> #ifndef FLASH_H_</span><br><span> #define FLASH_H_</span><br><span> </span><br><span style="color: hsl(120, 100%, 40%);">+#include <spi_flash.h></span><br><span style="color: hsl(120, 100%, 40%);">+</span><br><span> int get_spi_wp_pin_state(void);</span><br><span> </span><br><span style="color: hsl(0, 100%, 40%);">-int set_write_protect_enabled(void);</span><br><span style="color: hsl(0, 100%, 40%);">-int get_write_protect_state(void);</span><br><span style="color: hsl(120, 100%, 40%);">+int set_write_protect_enabled(const struct spi_flash *flash);</span><br><span style="color: hsl(120, 100%, 40%);">+int get_write_protect_state(const struct spi_flash *flash);</span><br><span> </span><br><span> #endif /* FLASH_H_ */</span><br><span>diff --git a/src/security/flash/wp.c b/src/security/flash/wp.c</span><br><span>new file mode 100644</span><br><span>index 0000000..87d84f7</span><br><span>--- /dev/null</span><br><span>+++ b/src/security/flash/wp.c</span><br><span>@@ -0,0 +1,18 @@</span><br><span style="color: hsl(120, 100%, 40%);">+/*</span><br><span style="color: hsl(120, 100%, 40%);">+ * This file is part of the coreboot project.</span><br><span style="color: hsl(120, 100%, 40%);">+ *</span><br><span style="color: hsl(120, 100%, 40%);">+ * Copyright 2018 Facebook Inc.</span><br><span style="color: hsl(120, 100%, 40%);">+ *</span><br><span style="color: hsl(120, 100%, 40%);">+ * This program is free software; you can redistribute it and/or modify</span><br><span style="color: hsl(120, 100%, 40%);">+ * it under the terms of the GNU General Public License as published by</span><br><span style="color: hsl(120, 100%, 40%);">+ * the Free Software Foundation; version 2 of the License.</span><br><span style="color: hsl(120, 100%, 40%);">+ *</span><br><span style="color: hsl(120, 100%, 40%);">+ * This program is distributed in the hope that it will be useful,</span><br><span style="color: hsl(120, 100%, 40%);">+ * but WITHOUT ANY WARRANTY; without even the implied warranty of</span><br><span style="color: hsl(120, 100%, 40%);">+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the</span><br><span style="color: hsl(120, 100%, 40%);">+ * GNU General Public License for more details.</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%);">+#include <security/flash/flash.h></span><br><span style="color: hsl(120, 100%, 40%);">+</span><br><span style="color: hsl(120, 100%, 40%);">+__attribute__((weak)) int get_spi_wp_pin_state(void) { return 0; }</span><br><span>diff --git a/src/security/vboot/vboot_handoff.c b/src/security/vboot/vboot_handoff.c</span><br><span>index 3190b7d..b0bf7f8 100644</span><br><span>--- a/src/security/vboot/vboot_handoff.c</span><br><span>+++ b/src/security/vboot/vboot_handoff.c</span><br><span>@@ -61,7 +61,7 @@</span><br><span>  vb_sd->data_used = sizeof(VbSharedDataHeader);</span><br><span>    vb_sd->fw_version_tpm = vb2_sd->fw_version_secdata;</span><br><span> </span><br><span style="color: hsl(0, 100%, 40%);">-   if (get_write_protect_state())</span><br><span style="color: hsl(120, 100%, 40%);">+        if (get_spi_wp_pin_state())</span><br><span>          vb_sd->flags |= VBSD_BOOT_FIRMWARE_WP_ENABLED;</span><br><span> </span><br><span>        if (vb2_sd->recovery_reason) {</span><br><span></span><br></pre><p>To view, visit <a href="https://review.coreboot.org/25283">change 25283</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/25283"/><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-MessageType: newchange </div>
<div style="display:none"> Gerrit-Change-Id: Ic0a859e6ce9aba32278f666a38a952ec8b4c1b4d </div>
<div style="display:none"> Gerrit-Change-Number: 25283 </div>
<div style="display:none"> Gerrit-PatchSet: 1 </div>
<div style="display:none"> Gerrit-Owner: Philipp Deppenwiese <zaolin.daisuki@gmail.com> </div>