Introduce the new build time option '--enable-state-backward-compatibility' to
port the following barebox commit.

NOTE: This changes barebox-state's default behaviour.

| commit 480cde1b22831febacc2a8ab91dfe99d2e5be8e9
| Author: Juergen Borleis <j...@pengutronix.de>
| Date:   Tue Aug 15 15:46:31 2017 +0200
|
|     state: keep backward compatibility
|
|     Previous 'state' variable set variants do not know and use metadata. The
|     'direct' storage backend's read function honors this, but not its
|     counterpart the write function. This makes an update of the 'state'
|     variable set impossible.
|     This change makes backward compatibility explicit, else it complains in
|     the read function as well. With some more debug output it helps the
|     developer to do things right.
|
|     Signed-off-by: Juergen Borleis <j...@pengutronix.de>
|     Signed-off-by: Sascha Hauer <s.ha...@pengutronix.de>

Signed-off-by: Ulrich Ölmann <u.oelm...@pengutronix.de>
---
 configure.ac                              | 28 +++++++++++++++--------
 src/barebox-state.c                       |  2 ++
 src/barebox-state/backend_bucket_direct.c | 28 +++++++++++++++--------
 3 files changed, 40 insertions(+), 18 deletions(-)

diff --git a/configure.ac b/configure.ac
index 777f4956ba5f..04c2226625c1 100644
--- a/configure.ac
+++ b/configure.ac
@@ -32,6 +32,15 @@ AS_IF([test "x$enable_debug" = "xyes"], [
         AC_DEFINE(DEBUG, [1], [Debug messages.])
 ])
 
+AC_ARG_ENABLE([state-backward-compatibility],
+        AS_HELP_STRING([--enable-state-backward-compatibility], [keep the 
'direct' storage backend backward compatible @<:@default=disabled@:>@]),
+        [], [enable_state_backward_compatibility=no])
+AS_IF([test "x${enable_state_backward_compatibility}" = "xyes"], [
+        AC_DEFINE(CONFIG_STATE_BACKWARD_COMPATIBLE, [1], ['direct' storage 
backend backward compatibility.])
+], [
+        AC_DEFINE(CONFIG_STATE_BACKWARD_COMPATIBLE, [0])
+])
+
 AC_CHECK_FUNCS([__secure_getenv secure_getenv])
 
 my_CFLAGS="-Wall \
@@ -53,15 +62,16 @@ AC_MSG_RESULT([
         $PACKAGE $VERSION
         =====
 
-        prefix:                 ${prefix}
-        sysconfdir:             ${sysconfdir}
-        libdir:                 ${libdir}
-        includedir:             ${includedir}
+        prefix:                         ${prefix}
+        sysconfdir:                     ${sysconfdir}
+        libdir:                         ${libdir}
+        includedir:                     ${includedir}
 
-        compiler:               ${CC}
-        cflags:                 ${CFLAGS}
-        ldflags:                ${LDFLAGS}
+        compiler:                       ${CC}
+        cflags:                         ${CFLAGS}
+        ldflags:                        ${LDFLAGS}
 
-        logging:                ${enable_logging}
-        debug:                  ${enable_debug}
+        logging:                        ${enable_logging}
+        debug:                          ${enable_debug}
+        state-backward-compatibility:   ${enable_state_backward_compatibility}
 ])
diff --git a/src/barebox-state.c b/src/barebox-state.c
index 946a8dba6d8c..6b166bfe6e02 100644
--- a/src/barebox-state.c
+++ b/src/barebox-state.c
@@ -439,6 +439,8 @@ int main(int argc, char *argv[])
                        exit(0);
                case OPT_VERSION:
                        printf(PACKAGE_STRING "\n");
+                       printf("Configured with build-time option 
'--%s-state-backward-compatibility'.\n",
+                              (CONFIG_STATE_BACKWARD_COMPATIBLE) ? "enable" : 
"disable");
                        exit(0);
                case 'g':
                        sg = xzalloc(sizeof(*sg));
diff --git a/src/barebox-state/backend_bucket_direct.c 
b/src/barebox-state/backend_bucket_direct.c
index 5b5506be002e..4522f0170f3d 100644
--- a/src/barebox-state/backend_bucket_direct.c
+++ b/src/barebox-state/backend_bucket_direct.c
@@ -75,6 +75,11 @@ static int state_backend_bucket_direct_read(struct 
state_backend_storage_bucket
        } else {
                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;
+               }
                read_len = direct->max_size;
                if (lseek(direct->fd, direct->offset, SEEK_SET) !=
                    direct->offset) {
@@ -110,20 +115,25 @@ static int state_backend_bucket_direct_write(struct 
state_backend_storage_bucket
        int ret;
        struct state_backend_storage_bucket_direct_meta meta;
 
-       if (len > direct->max_size - sizeof(meta))
-               return -E2BIG;
-
        if (lseek(direct->fd, direct->offset, SEEK_SET) != direct->offset) {
                dev_err(direct->dev, "Failed to seek file, %d\n", -errno);
                return -errno;
        }
 
-       meta.magic = direct_magic;
-       meta.written_length = len;
-       ret = write_full(direct->fd, &meta, sizeof(meta));
-       if (ret < 0) {
-               dev_err(direct->dev, "Failed to write metadata to file, %d\n", 
ret);
-               return ret;
+       /* write the meta data only if there is head room */
+       if (len <= direct->max_size - sizeof(meta)) {
+               meta.magic = direct_magic;
+               meta.written_length = len;
+               ret = write_full(direct->fd, &meta, sizeof(meta));
+               if (ret < 0) {
+                       dev_err(direct->dev, "Failed to write metadata to file, 
%d\n", ret);
+                       return ret;
+               }
+       } else {
+               if (!IS_ENABLED(CONFIG_STATE_BACKWARD_COMPATIBLE)) {
+                       dev_dbg(direct->dev, "Too small stride size: must skip 
metadata! Increase stride size\n");
+                       return -EINVAL;
+               }
        }
 
        ret = write_full(direct->fd, buf, len);
-- 
2.23.0


_______________________________________________
OSS-Tools mailing list
OSS-Tools@pengutronix.de

Reply via email to