mailarchive of the pengutronix oss-tools mailing list
 help / color / mirror / Atom feed
From: Marco Felsch <m.felsch@pengutronix.de>
To: oss-tools@pengutronix.de
Subject: [OSS-Tools] [PATCH dt-utils 5/6] state: make uninitialized state less verbose
Date: Tue, 27 Feb 2024 15:26:10 +0100	[thread overview]
Message-ID: <20240227142611.2421711-6-m.felsch@pengutronix.de> (raw)
In-Reply-To: <20240227142611.2421711-1-m.felsch@pengutronix.de>

This patch ports barebox commits:

| commit 22d06d7d72d781d16db7f8292181d51752e2a860
| Author: Ahmad Fatoum <a.fatoum@pengutronix.de>
| Date:   Thu Nov 25 17:10:36 2021 +0100
|
|     state: mark state init errors specially
|
|     First boot with uninitialized state is needlessly verbose. As
|     preparation for making boot less noisy on fresh state, mark all the
|     error messages with newly introduced dev_err_state_init().
|
|     Signed-off-by: Ahmad Fatoum <a.fatoum@pengutronix.de>
|     Link: https://lore.barebox.org/20211125161042.3829996-2-a.fatoum@pengutronix.de
|     Signed-off-by: Sascha Hauer <s.hauer@pengutronix.de>

| commit 863a2251393e5ee5ecdd6d696ee0e23c3b945f9a
| Author: Ahmad Fatoum <a.fatoum@pengutronix.de>
| Date:   Thu Nov 25 17:10:37 2021 +0100
|
|     state: make first boot less verbose
|
|     First boot with uninitialized state is needlessly verbose:
|
|       state: New state registered 'state'
|       state: Detected old on-storage format
|       ERROR: state: Error, invalid header crc in raw format, calculated 0x7bd5c66f, found 0x00000000
|       state: Ignoring broken bucket 0@0x00000000...
|       state: Detected old on-storage format
|       ERROR: state: Error, invalid header crc in raw format, calculated 0x7bd5c66f, found 0x00000000
|       state: Ignoring broken bucket 1@0x00040000...
|       state: Detected old on-storage format
|       ERROR: state: Error, invalid header crc in raw format, calculated 0x7bd5c66f, found 0x00000000
|       state: Ignoring broken bucket 2@0x00080000...
|       ERROR: state: Failed to find any valid state copy in any bucket
|       ERROR: state: Failed to read state with format raw, -2
|
|     This has confused barebox-state novices more than once. Let's handle
|     the zeroed state case specially and reduce output in that case, so
|     it now looks like this:
|
|       state: New state registered 'state'
|       state: Detected old on-storage format
|       state: Detected old on-storage format
|       state: Detected old on-storage format
|       state state.of: Fresh state detected, continuing with defaults
|
|     This is only the output when CRC is zero (hinting at zeroed state
|     partition). If crc != zero, then output is a little more verbose than
|     before:
|
|       state: New state registered 'state'
|       state: Detected old on-storage format
|       ERROR: state: init error: Invalid argument: header crc in raw format, calculated 0x7bd5c66f, found 0x00000000
|       state: Ignoring broken bucket 0@0x00000000...
|       state: Detected old on-storage format
|       ERROR: state: init error: Invalid argument: header crc in raw format, calculated 0x7bd5c66f, found 0x00000000
|       state: Ignoring broken bucket 1@0x00040000...
|       state: Detected old on-storage format
|       ERROR: state: init error: Invalid argument: header crc in raw format, calculated 0x7bd5c66f, found 0x00000000
|       state: Ignoring broken bucket 2@0x00080000...
|       ERROR: state: init error: No such file or directory: no valid state copy in any bucket
|       ERROR: state: init error: No such file or directory: format raw read failed
|       WARNING: state state.of: Failed to load persistent state, continuing with defaults, -2
|
|     Signed-off-by: Ahmad Fatoum <a.fatoum@pengutronix.de>
|     Link: https://lore.barebox.org/20211125161042.3829996-3-a.fatoum@pengutronix.de
|     Signed-off-by: Sascha Hauer <s.hauer@pengutronix.de>

| commit 818d180da1c709f3d2789925662222019b3cf439
| Author: Ahmad Fatoum <ahmad@a3f.at>
| Date:   Mon Nov 29 08:22:35 2021 +0100
|
|     state: make first boot with uninitialized legacy state less verbose
|
|     With non-legacy state, barebox checks a header for a specific magic
|     signature. This can be all zero on an uninitialized state, so in that
|     particular case, bump down the log message severity.
|
|     Signed-off-by: Ahmad Fatoum <ahmad@a3f.at>
|     Link: https://lore.barebox.org/20211129072235.2016324-1-ahmad@a3f.at
|     Signed-off-by: Sascha Hauer <s.hauer@pengutronix.de>

Signed-off-by: Marco Felsch <m.felsch@pengutronix.de>
---
 src/barebox-state.c                       |  4 +++-
 src/barebox-state/backend_bucket_direct.c |  4 ++--
 src/barebox-state/backend_format_raw.c    |  7 +++----
 src/barebox-state/backend_storage.c       | 24 ++++++++++++++---------
 src/barebox-state/state.c                 |  4 ++--
 src/barebox-state/state.h                 |  7 +++++++
 6 files changed, 32 insertions(+), 18 deletions(-)

diff --git a/src/barebox-state.c b/src/barebox-state.c
index e74ba1992286..1a515d30dde4 100644
--- a/src/barebox-state.c
+++ b/src/barebox-state.c
@@ -409,7 +409,9 @@ struct state *state_get(const char *name, const char *filename, bool readonly, b
 	else
 		ret = state_load_no_auth(state);
 
-	if (ret)
+	if (ret == -ENOMEDIUM)
+		pr_info("Fresh state detected, continuing with defaults\n");
+	else if (ret)
 		pr_err("Failed to load persistent state, continuing with defaults, %d\n", ret);
 
 	return state;
diff --git a/src/barebox-state/backend_bucket_direct.c b/src/barebox-state/backend_bucket_direct.c
index cf77c3f6a0df..bf060ebed4c0 100644
--- a/src/barebox-state/backend_bucket_direct.c
+++ b/src/barebox-state/backend_bucket_direct.c
@@ -77,9 +77,9 @@ static int state_backend_bucket_direct_read(struct state_backend_storage_bucket
 		if (meta.magic != ~0 && !!meta.magic)
 			bucket->wrong_magic = 1;
 		if (!IS_ENABLED(CONFIG_STATE_BACKWARD_COMPATIBLE)) {
-			dev_err(direct->dev, "No meta data header found\n");
 			dev_dbg(direct->dev, "Enable backward compatibility or increase stride size\n");
-			return -EINVAL;
+			return dev_err_state_init(direct->dev, meta.magic ? -EINVAL : -ENOMEDIUM,
+						  "No meta data header found\n");
 		}
 		read_len = direct->max_size;
 		if (lseek(direct->fd, direct->offset, SEEK_SET) !=
diff --git a/src/barebox-state/backend_format_raw.c b/src/barebox-state/backend_format_raw.c
index 8127a27e5732..85ea539657dc 100644
--- a/src/barebox-state/backend_format_raw.c
+++ b/src/barebox-state/backend_format_raw.c
@@ -114,11 +114,10 @@ static int backend_format_raw_verify(struct state_backend_format *format,
 
 	header = (struct backend_raw_header *)buf;
 	crc = crc32(0, header, sizeof(*header) - sizeof(uint32_t));
-	if (crc != header->header_crc) {
-		dev_err(backend_raw->dev, "Error, invalid header crc in raw format, calculated 0x%08x, found 0x%08x\n",
+	if (crc != header->header_crc)
+		return dev_err_state_init(backend_raw->dev, header->header_crc ? -EINVAL : -ENOMEDIUM,
+			"header crc in raw format, calculated 0x%08x, found 0x%08x\n",
 			crc, header->header_crc);
-		return -EINVAL;
-	}
 
 	if (magic && magic != header->magic) {
 		dev_err(backend_raw->dev, "Error, invalid magic in raw format 0x%08x, should be 0x%08x\n",
diff --git a/src/barebox-state/backend_storage.c b/src/barebox-state/backend_storage.c
index 6d659c1fd85a..135c7aad0e81 100644
--- a/src/barebox-state/backend_storage.c
+++ b/src/barebox-state/backend_storage.c
@@ -145,6 +145,7 @@ int state_storage_read(struct state_backend_storage *storage,
 		       enum state_flags flags)
 {
 	struct state_backend_storage_bucket *bucket, *bucket_used = NULL;
+	int zerobuckets = 0, totalbuckets = 0;
 	int ret;
 
 	dev_dbg(storage->dev, "Checking redundant buckets...\n");
@@ -153,30 +154,35 @@ int state_storage_read(struct state_backend_storage *storage,
 	 * one we want to use.
 	 */
 	list_for_each_entry(bucket, &storage->buckets, bucket_list) {
+		totalbuckets++;
+
 		ret = bucket->read(bucket, &bucket->buf, &bucket->len);
-		if (ret == -EUCLEAN)
+		if (ret == -EUCLEAN) {
 			bucket->needs_refresh = 1;
-		else if (ret)
+		} else if (ret) {
+			if (ret == -ENOMEDIUM)
+				zerobuckets++;
 			continue;
+		}
 
 		/*
 		 * Verify the buffer crcs. The buffer length is passed in the len argument,
 		 * .verify overwrites it with the length actually used.
 		 */
 		ret = format->verify(format, magic, bucket->buf, &bucket->len, flags);
-		if (!ret && !bucket_used)
+		if (ret == -ENOMEDIUM)
+			zerobuckets++;
+		else if (!ret && !bucket_used)
 			bucket_used = bucket;
-		if (ret)
+		else if (ret)
 			dev_info(storage->dev, "Ignoring broken bucket %d@0x%08llx...\n", bucket->num, (long long) bucket->offset);
 	}
 
 	dev_dbg(storage->dev, "Checking redundant buckets finished.\n");
 
-	if (!bucket_used) {
-		dev_err(storage->dev, "Failed to find any valid state copy in any bucket\n");
-
-		return -ENOENT;
-	}
+	if (!bucket_used)
+		return dev_err_state_init(storage->dev, zerobuckets == totalbuckets ? -ENOMEDIUM : -ENOENT,
+					  "no valid state copy in any bucket\n");
 
 	dev_info(storage->dev, "Using bucket %d@0x%08llx\n", bucket_used->num, (long long) bucket_used->offset);
 
diff --git a/src/barebox-state/state.c b/src/barebox-state/state.c
index df76407d4d33..d842bf1de0ec 100644
--- a/src/barebox-state/state.c
+++ b/src/barebox-state/state.c
@@ -102,8 +102,8 @@ static int state_do_load(struct state *state, enum state_flags flags)
 	ret = state_storage_read(&state->storage, state->format,
 				 state->magic, &buf, &len, flags);
 	if (ret) {
-		dev_err(&state->dev, "Failed to read state with format %s, %d\n",
-			state->format->name, ret);
+		dev_err_state_init(&state->dev, ret, "format %s read failed\n",
+				   state->format->name);
 		goto out;
 	}
 
diff --git a/src/barebox-state/state.h b/src/barebox-state/state.h
index c8217a382070..734899b075b1 100644
--- a/src/barebox-state/state.h
+++ b/src/barebox-state/state.h
@@ -290,3 +290,10 @@ static inline int open_exclusive(const char *path, int flags)
 
 	return fd;
 }
+
+#define dev_err_state_init(dev, ret, fmt, ...) ({ \
+	int __ret = (ret); \
+	dev_printf(__ret == -ENOMEDIUM ? 7 : 3, \
+		   (dev), "init error: %pe: " fmt, ERR_PTR(__ret), ##__VA_ARGS__); \
+	__ret; \
+})
-- 
2.39.2




  parent reply	other threads:[~2024-02-27 14:26 UTC|newest]

Thread overview: 7+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2024-02-27 14:26 [OSS-Tools] [PATCH dt-utils 0/6] barebox-state sync Marco Felsch
2024-02-27 14:26 ` [OSS-Tools] [PATCH dt-utils 1/6] state: align state_backend_bucket_direct_create with barebox Marco Felsch
2024-02-27 14:26 ` [OSS-Tools] [PATCH dt-utils 2/6] state: don't mix goto labels and statements on same line Marco Felsch
2024-02-27 14:26 ` [OSS-Tools] [PATCH dt-utils 3/6] state: drop null pointer checks around of_delete_node Marco Felsch
2024-02-27 14:26 ` [OSS-Tools] [PATCH dt-utils 4/6] treewide: rename struct device_d to device Marco Felsch
2024-02-27 14:26 ` Marco Felsch [this message]
2024-02-27 14:26 ` [OSS-Tools] [PATCH dt-utils 6/6] state: backend_bucket_direct: add debug prints on read/write Marco Felsch

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=20240227142611.2421711-6-m.felsch@pengutronix.de \
    --to=m.felsch@pengutronix.de \
    --cc=oss-tools@pengutronix.de \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox