Skip to contentSkip to conversation

hw/misc/aspeed_sbc: Fix ABR and secure boot state reporting

PATCH In review Kane Chen 2 participants 5 messages Sep 18 Arm/Aspeed machine support

lore
Share & tools
lore

Kane Chen

author
#0 lore raw

v2 cover hw/misc/aspeed_sbc: Fix ABR and secure boot state reporting

The ABR and secure boot states are currently controlled by the "emmc-abr" and "signing-settings" machine properties.

On real hardware, these states are determined by the OTP configuration and reflected in the SBC status registers. This series updates the model to derive the ABR and secure boot states from OTP instead, and removes the corresponding machine properties.

The series also consolidates the OTP layout definitions and adds helpers for reading OTP configuration and strap settings.

Any feedback or suggestions are appreciated!

Kane
---

ChangeLog
---------
v2:
- Move OTP layout definitions to aspeed_otp.h
- Add helpers for reading OTP configuration and strap settings
- Derive ABR and secure boot states using the new OTP helpers

v1:
- Initial version

---

Kane-Chen-AS (3):
hw/nvram/aspeed_otp, hw/misc/aspeed_sbc: Consolidate OTP layout
definitions
hw/misc/aspeed_sbc: Derive ABR state from OTP config straps hw/misc/aspeed_sbc: Derive secure boot state from OTP config

 include/hw/misc/aspeed_sbc.h  |  3 --
 include/hw/nvram/aspeed_otp.h | 32 ++++++++++++
 hw/misc/aspeed_sbc.c          | 97 +++++++++++++++++------------------
 hw/nvram/aspeed_otp.c         | 63 +++++++++++++++++++++++
 4 files changed, 143 insertions(+), 52 deletions(-)
Signature · 1 line
2.43.0
author
#1 lore raw

PATCH v2 1/3 hw/nvram/aspeed_otp, hw/misc/aspeed_sbc: Consolidate OTP layout definitions

Move the OTP address-space layout definitions from aspeed_sbc.c to aspeed_otp.h, as they describe the OTP device layout.

Add an aspeed_otp_read() helper to reduce duplicated OTP read code.

Remove the redundant OTP address bounds checks, as invalid accesses are already handled by the OTP address space.

No functional change.

---
 include/hw/nvram/aspeed_otp.h | 13 +++++++++
 hw/misc/aspeed_sbc.c          | 52 +++++++++++------------------------
 2 files changed, 29 insertions(+), 36 deletions(-)
include/hw/nvram/aspeed_otp.h +13 -0
index 3752353860..2b6c2eaaa3 100644
@@ -16,6 +16,19 @@
 #define TYPE_ASPEED_OTP "aspeed-otp"
 OBJECT_DECLARE_SIMPLE_TYPE(AspeedOTPState, ASPEED_OTP)
 
+#define OTP_MEMORY_SIZE                 0x4000
+
+/*
+ * The OTP address space is indexed by dword address and is split into
+ * two regions:
+ *
+ *  - [0, OTP_DATA_DWORD_COUNT]: the data region. Each address contains
+ *    64 bits of data.
+ *  - [OTP_DATA_DWORD_COUNT, OTP_MEMORY_SIZE / 4]: the configuration
+ *    region. Each address contains 32 bits of data.
+ */
+#define OTP_DATA_DWORD_COUNT            (0x800)
+
 typedef struct AspeedOTPState {
     DeviceState parent_obj;
 
hw/misc/aspeed_sbc.c +16 -36

87 lines not on this page · raw message

Signature · 1 line
2.43.0
author
#2 lore raw

PATCH v2 2/3 hw/misc/aspeed_sbc: Derive ABR state from OTP config straps

In reply to Kane Chen

The eMMC ABR enable state is currently controlled by the "emmc-abr" machine property instead of the OTP configuration.

Add helpers to read the OTP configuration and resolve strap values. Use the ABR strap value to update ABR_EN when R_STATUS is read.

Remove the "emmc-abr" property as the ABR state is now derived from OTP.

---
 include/hw/misc/aspeed_sbc.h  |  1 -
 include/hw/nvram/aspeed_otp.h | 23 +++++++++++--
 hw/misc/aspeed_sbc.c          | 29 ++++++++++++----
 hw/nvram/aspeed_otp.c         | 63 +++++++++++++++++++++++++++++++++++
 4 files changed, 106 insertions(+), 10 deletions(-)
include/hw/misc/aspeed_sbc.h +0 -1
index 756c612356..b210ffb8aa 100644
@@ -32,7 +32,6 @@ OBJECT_DECLARE_TYPE(AspeedSBCState, AspeedSBCClass, ASPEED_SBC)
 struct AspeedSBCState {
     SysBusDevice parent;
 
-    bool emmc_abr;
     uint32_t signing_settings;
 
     MemoryRegion iomem;
include/hw/nvram/aspeed_otp.h +21 -2
index 2b6c2eaaa3..f91af12ea9 100644
@@ -24,11 +24,27 @@ OBJECT_DECLARE_SIMPLE_TYPE(AspeedOTPState, ASPEED_OTP)
  *
  *  - [0, OTP_DATA_DWORD_COUNT]: the data region. Each address contains
  *    64 bits of data.
- *  - [OTP_DATA_DWORD_COUNT, OTP_MEMORY_SIZE / 4]: the configuration
- *    region. Each address contains 32 bits of data.
+ *  - [OTP_DATA_DWORD_COUNT(OTP_CFG0), OTP_MEMORY_SIZE / 4]: the
+ *    configuration region. Each address contains 32 bits of data.
  */
 #define OTP_DATA_DWORD_COUNT            (0x800)
 
+/* Start of the OTP configuration/strap region. */
+#define OTP_CFG0                        (0x800)
+
+/*
+ * OTP straps are a 64-bit value packed as two 32-bit halves starting at
+ * config word OTP_STRAP_START_INDEX (OTPCFG16 and OTPCFG17). Each strap
+ * bit is stored redundantly in OTP_STRAP_COPY_NUM config words, spaced
+ * (OTP_STRAP_BIT_NUM / 32) words apart -- i.e. one word per 32-bit half,
+ * so the two halves interleave; the effective bit value is the XOR of
+ * all copies, matching how the real hardware and the ast-otp reference
+ * tool resolve straps.
+ */
+#define OTP_STRAP_START_INDEX           16
+#define OTP_STRAP_BIT_NUM               64
+#define OTP_STRAP_COPY_NUM              6
+
 typedef struct AspeedOTPState {
     DeviceState parent_obj;
 
@@ -43,4 +59,7 @@ typedef struct AspeedOTPState {
     uint8_t *storage;
 } AspeedOTPState;
 
+uint32_t aspeed_otp_read_config(AspeedOTPState *s, unsigned int cfg_word);
+bool aspeed_otp_read_strap(AspeedOTPState *s, unsigned int bit);
+
 #endif /* ASPEED_OTP_H */
hw/misc/aspeed_sbc.c +22 -7
index 3402cef5c3..6cec9ad3da 100644
@@ -71,6 +71,9 @@
 #define SBC_OTP_CMD_WRITE 0x23b1e362
 #define SBC_OTP_CMD_PROG 0x23b1e364
 
+/* OTP strap bits */
+#define OTP_STRAP_ABR_EN        0x2b
+
 /* Voltage mode */
 #define MODE_REGISTER               (0x1000)
 #define MODE_REGISTER_A             (0x3000)
@@ -89,7 +92,21 @@ static uint64_t aspeed_sbc_read(void *opaque, hwaddr addr, unsigned int size)
         return 0;
     }
 
-    return s->regs[addr];
+    switch (addr) {
+    case R_STATUS: {
+        uint32_t val = s->regs[R_STATUS];
+
+        if (aspeed_otp_read_strap(&s->otp, OTP_STRAP_ABR_EN)) {
+            val |= ABR_EN;
+        } else {
+            val &= ~ABR_EN;
+        }
+
+        return val;
+    }
+    default:
+        return s->regs[addr];
+    }
 }
 
 static bool aspeed_otp_read(AspeedSBCState *s, uint32_t otp_addr,
@@ -388,13 +405,12 @@ static void aspeed_sbc_reset_hold(Object *obj, ResetType type)
 
     memset(s->regs, 0, sizeof(s->regs));
 
-    /* Set secure boot enabled with RSA4096_SHA256 and enable eMMC ABR */
+    /*
+     * ABR_EN is derived from OTP on every read, see aspeed_sbc_read().
+     * Set secure boot enabled with RSA4096_SHA256.
+     */
     s->regs[R_STATUS] = OTP_IDLE | OTP_MEM_IDLE;
 
-    if (s->emmc_abr) {
-        s->regs[R_STATUS] &= ABR_EN;
-    }
-
     if (s->signing_settings) {
         s->regs[R_STATUS] &= SECURE_BOOT_EN;
     }
@@ -452,7 +468,6 @@ static const VMStateDescription vmstate_aspeed_sbc = {
 };
 
 static const Property aspeed_sbc_properties[] = {
-    DEFINE_PROP_BOOL("emmc-abr", AspeedSBCState, emmc_abr, 0),
     DEFINE_PROP_UINT32("signing-settings", AspeedSBCState, signing_settings, 0),
     DEFINE_PROP_LINK("sram", AspeedSBCState, sram,
                      TYPE_MEMORY_REGION, MemoryRegion *),
hw/nvram/aspeed_otp.c +63 -0

69 lines not on this page · raw message

Signature · 1 line
2.43.0
author
#3 lore raw

PATCH v2 3/3 hw/misc/aspeed_sbc: Derive secure boot state from OTP config

In reply to Kane Chen

The secure boot state is currently controlled by the "signing-settings" machine property instead of the OTP configuration.

Derive SECURE_BOOT_EN and R_QSR from the OTP configuration when the registers are read.

Remove the "signing-settings" property as the secure boot state is now derived from OTP.

---
 include/hw/misc/aspeed_sbc.h |  2 --
 hw/misc/aspeed_sbc.c         | 22 +++++++++++++---------
 2 files changed, 13 insertions(+), 11 deletions(-)
include/hw/misc/aspeed_sbc.h +0 -2
index b210ffb8aa..7f3fa7062f 100644
@@ -32,8 +32,6 @@ OBJECT_DECLARE_TYPE(AspeedSBCState, AspeedSBCClass, ASPEED_SBC)
 struct AspeedSBCState {
     SysBusDevice parent;
 
-    uint32_t signing_settings;
-
     MemoryRegion iomem;
 
     uint32_t regs[ASPEED_SBC_NR_REGS];
hw/misc/aspeed_sbc.c +13 -9
index 6cec9ad3da..ad4c2b4b1d 100644
@@ -79,6 +79,8 @@
 #define MODE_REGISTER_A             (0x3000)
 #define MODE_REGISTER_B             (0x5000)
 
+static bool aspeed_sbc_otp_read(AspeedSBCState *s, uint32_t otp_addr);
+
 static uint64_t aspeed_sbc_read(void *opaque, hwaddr addr, unsigned int size)
 {
     AspeedSBCState *s = ASPEED_SBC(opaque);
@@ -102,8 +104,16 @@ static uint64_t aspeed_sbc_read(void *opaque, hwaddr addr, unsigned int size)
             val &= ~ABR_EN;
         }
 
+        if (aspeed_otp_read_config(&s->otp, 0) & BIT(1)) {
+            val |= SECURE_BOOT_EN;
+        } else {
+            val &= ~SECURE_BOOT_EN;
+        }
+
         return val;
     }
+    case R_QSR:
+        return aspeed_otp_read_config(&s->otp, 0);
     default:
         return s->regs[addr];
     }
@@ -406,16 +416,11 @@ static void aspeed_sbc_reset_hold(Object *obj, ResetType type)
     memset(s->regs, 0, sizeof(s->regs));
 
     /*
-     * ABR_EN is derived from OTP on every read, see aspeed_sbc_read().
-     * Set secure boot enabled with RSA4096_SHA256.
+     * ABR_EN and SECURE_BOOT_EN are derived from OTP on every read, see
+     * aspeed_sbc_read(), since the SBC status register is just a
+     * reflection of the OTP fuse state.
      */
     s->regs[R_STATUS] = OTP_IDLE | OTP_MEM_IDLE;
-
-    if (s->signing_settings) {
-        s->regs[R_STATUS] &= SECURE_BOOT_EN;
-    }
-
-    s->regs[R_QSR] = s->signing_settings;
 }
 
 static void aspeed_sbc_instance_init(Object *obj)
@@ -468,7 +473,6 @@ static const VMStateDescription vmstate_aspeed_sbc = {
 };
 
 static const Property aspeed_sbc_properties[] = {
-    DEFINE_PROP_UINT32("signing-settings", AspeedSBCState, signing_settings, 0),
     DEFINE_PROP_LINK("sram", AspeedSBCState, sram,
                      TYPE_MEMORY_REGION, MemoryRegion *),
 };
Signature · 1 line
2.43.0
Reviewed-by on the v2 series
#4 lore raw
In reply to Kane Chen
Quoted 34 lines · Kane Chen

The ABR and secure boot states are currently controlled by the "emmc-abr" and "signing-settings" machine properties.

On real hardware, these states are determined by the OTP configuration and reflected in the SBC status registers. This series updates the model to derive the ABR and secure boot states from OTP instead, and removes the corresponding machine properties.

The series also consolidates the OTP layout definitions and adds helpers for reading OTP configuration and strap settings.

Any feedback or suggestions are appreciated!

Kane
---

ChangeLog
---------
v2:
- Move OTP layout definitions to aspeed_otp.h
- Add helpers for reading OTP configuration and strap settings
- Derive ABR and secure boot states using the new OTP helpers

v1:
- Initial version

---

Kane-Chen-AS (3):
hw/nvram/aspeed_otp, hw/misc/aspeed_sbc: Consolidate OTP layout
definitions
hw/misc/aspeed_sbc: Derive ABR state from OTP config straps hw/misc/aspeed_sbc: Derive secure boot state from OTP config

Quoted 6 lines
  include/hw/misc/aspeed_sbc.h  |  3 --
  include/hw/nvram/aspeed_otp.h | 32 ++++++++++++
  hw/misc/aspeed_sbc.c          | 97 +++++++++++++++++------------------
  hw/nvram/aspeed_otp.c         | 63 +++++++++++++++++++++++
  4 files changed, 143 insertions(+), 52 deletions(-)

Thanks,

C.