summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorUlrich Ölmann <u.oelmann@pengutronix.de>2018-12-18 14:57:55 +0100
committerRoland Hieber <rhi@pengutronix.de>2019-01-07 15:47:56 +0100
commitea60eb9e5a7d08f4faba6ff170cc93f2faaa2bdc (patch)
tree968c675f4f0f3fbe75b18f7481ed0cb8f4e5c058
parent2157caeffeb08a18a4971a05807e37de701ad41b (diff)
downloaddt-utils-ea60eb9e5a7d08f4faba6ff170cc93f2faaa2bdc.tar.gz
dt-utils-ea60eb9e5a7d08f4faba6ff170cc93f2faaa2bdc.tar.xz
state: Add property to protect existing data
This ports the following barebox commit: | commit 67c1c0853fa06bcbc1725e93b6ad9942e80a1e56 | Author: Daniel Schultz <d.schultz@phytec.de> | Date: Thu Apr 12 11:13:01 2018 +0200 | | common: state: Add property to protect existing data | | After an update to a newer barebox version with an enabled state | framework, existing data in storage memories could be overwritten. | | Add a new property to check in front of every write task, if the meta | magic field only contains the magic number, zeros or ones. | | Signed-off-by: Daniel Schultz <d.schultz@phytec.de> | Signed-off-by: Sascha Hauer <s.hauer@pengutronix.de> Signed-off-by: Ulrich Ölmann <u.oelmann@pengutronix.de> Signed-off-by: Roland Hieber <rhi@pengutronix.de>
-rw-r--r--src/barebox-state/backend_bucket_circular.c8
-rw-r--r--src/barebox-state/backend_bucket_direct.c2
-rw-r--r--src/barebox-state/state.c19
-rw-r--r--src/barebox-state/state.h2
4 files changed, 27 insertions, 4 deletions
diff --git a/src/barebox-state/backend_bucket_circular.c b/src/barebox-state/backend_bucket_circular.c
index 2a0e34d..ec78748 100644
--- a/src/barebox-state/backend_bucket_circular.c
+++ b/src/barebox-state/backend_bucket_circular.c
@@ -397,11 +397,13 @@ static int state_backend_bucket_circular_init(
meta = (struct state_backend_storage_bucket_circular_meta *)
(buf + sub_offset + circ->writesize - sizeof(*meta));
- if (meta->magic != circular_magic)
+ if (meta->magic != circular_magic) {
written_length = 0;
- else
+ if (meta->magic != ~0 && !!meta->magic)
+ bucket->wrong_magic = 1;
+ } else {
written_length = meta->written_length;
-
+ }
break;
}
}
diff --git a/src/barebox-state/backend_bucket_direct.c b/src/barebox-state/backend_bucket_direct.c
index 4465ed0..dc00de0 100644
--- a/src/barebox-state/backend_bucket_direct.c
+++ b/src/barebox-state/backend_bucket_direct.c
@@ -69,6 +69,8 @@ static int state_backend_bucket_direct_read(struct state_backend_storage_bucket
if (meta.magic == direct_magic) {
read_len = meta.written_length;
} else {
+ if (meta.magic != ~0 && !!meta.magic)
+ bucket->wrong_magic = 1;
read_len = direct->max_size;
ret = lseek(direct->fd, direct->offset, SEEK_SET);
if (ret < 0) {
diff --git a/src/barebox-state/state.c b/src/barebox-state/state.c
index 0ec5664..eb2b3eb 100644
--- a/src/barebox-state/state.c
+++ b/src/barebox-state/state.c
@@ -43,6 +43,8 @@ int state_save(struct state *state)
void *buf;
ssize_t len;
int ret;
+ struct state_backend_storage_bucket *bucket;
+ struct state_backend_storage *storage;
if (!state->dirty)
return 0;
@@ -54,7 +56,19 @@ int state_save(struct state *state)
return ret;
}
- ret = state_storage_write(&state->storage, buf, len);
+ storage = &state->storage;
+ if (state->keep_prev_content) {
+ bool has_content = 0;
+ list_for_each_entry(bucket, &storage->buckets, bucket_list)
+ has_content |= bucket->wrong_magic;
+ if (has_content) {
+ dev_err(&state->dev, "Found foreign content on backend, won't overwrite.\n");
+ ret = -EPERM;
+ goto out;
+ }
+ }
+
+ ret = state_storage_write(storage, buf, len);
if (ret) {
dev_err(&state->dev, "Failed to write packed state, %d\n", ret);
goto out;
@@ -628,6 +642,9 @@ struct state *state_new_from_node(struct device_node *node, bool readonly)
of_property_read_string(node, "backend-storage-type", &storage_type);
+ state->keep_prev_content = of_property_read_bool(node,
+ "keep-previous-content");
+
ret = state_format_init(state, backend_type, node, alias);
if (ret)
goto out_release_state;
diff --git a/src/barebox-state/state.h b/src/barebox-state/state.h
index 6670523..3a0662f 100644
--- a/src/barebox-state/state.h
+++ b/src/barebox-state/state.h
@@ -46,6 +46,7 @@ struct state_backend_storage_bucket {
void *buf;
ssize_t len;
bool needs_refresh;
+ bool wrong_magic;
};
/**
@@ -105,6 +106,7 @@ struct state {
char *of_path;
const char *name;
uint32_t magic;
+ bool keep_prev_content;
struct list_head variables; /* Sorted list of variables */