From 657aa14900f8c830e3a39aff7521f911d9de57bb Mon Sep 17 00:00:00 2001
From: Zygmunt Krynicki <zygmunt.krynicki@huawei.com>
Date: Wed, 13 Oct 2021 22:40:11 +0200
Subject: [PATCH] ota/raucadapter: fix handling of pristine boot configuration

Instead of ignoring the requests entirely, assume slot A is active
and _carry out_ the requests. This fixes updates.

Tested-By: Zygmunt Krynicki <zygmunt.krynicki@huawei.com>
Signed-off-by: Zygmunt Krynicki <zygmunt.krynicki@huawei.com>
---
 ota/raucadapter/raucadapter.go | 39 ++++++++++++----------------------
 1 file changed, 13 insertions(+), 26 deletions(-)

diff --git a/ota/raucadapter/raucadapter.go b/ota/raucadapter/raucadapter.go
index bcc2240..7810f02 100644
--- a/ota/raucadapter/raucadapter.go
+++ b/ota/raucadapter/raucadapter.go
@@ -94,12 +94,11 @@ func (adapter *Adapter) SetPrimarySlot(slot boot.Slot) error {
 	activeSlot, err := adapter.proto.QueryActive()
 	if err != nil {
 		if errors.Is(err, boot.ErrPristineBootConfig) && adapter.bootModeHolder.BootMode() == boot.Normal {
-			fmt.Printf("Ignoring RAUC set-primary %s while in pristine boot configuration\n", slot)
-
-			return nil
+			fmt.Printf("System is in pristine boot configuration mode, pretending slot A is active\n")
+			activeSlot = boot.SlotA
+		} else {
+			return err
 		}
-
-		return err
 	}
 
 	switch adapter.bootModeHolder.BootMode() {
@@ -149,21 +148,12 @@ func (adapter *Adapter) SlotState(slot boot.Slot) (boot.SlotState, error) {
 
 	active, err := adapter.proto.QueryActive()
 	if err != nil {
-		if errors.Is(err, boot.ErrPristineBootConfig) {
-			fmt.Printf("Forging RAUC get-state %s while in pristine boot configuration\n", slot)
-
-			if slot == boot.SlotA {
-				return boot.GoodSlot, nil
-			}
-
-			return boot.BadSlot, nil
+		if errors.Is(err, boot.ErrPristineBootConfig) && adapter.bootModeHolder.BootMode() == boot.Normal {
+			fmt.Printf("System is in pristine boot configuration mode, pretending slot A is active\n")
+			active = boot.SlotA
+		} else {
+			return boot.InvalidSlotState, err
 		}
-
-		return boot.InvalidSlotState, err
-	}
-
-	if err != nil {
-		return boot.InvalidSlotState, err
 	}
 
 	if slot == active {
@@ -210,14 +200,11 @@ func (adapter *Adapter) SetSlotState(slot boot.Slot, state boot.SlotState) error
 	active, err := adapter.proto.QueryActive()
 	if err != nil {
 		if errors.Is(err, boot.ErrPristineBootConfig) && adapter.bootModeHolder.BootMode() == boot.Normal {
-			if (slot == boot.SlotA && state == boot.GoodSlot) || (slot == boot.SlotB && state == boot.BadSlot) {
-				fmt.Printf("Ignoring RAUC set-state %s %s while in pristine boot configuration\n", slot, state)
-
-				return nil
-			}
+			fmt.Printf("System is in pristine boot configuration mode, pretending slot A is active\n")
+			active = boot.SlotA
+		} else {
+			return err
 		}
-
-		return err
 	}
 
 	inactive := boot.SynthesizeInactiveSlot(active)
-- 
GitLab