From patchwork Sun Nov 8 13:36:13 2026 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Dario Binacchi X-Patchwork-Id: 5011 Delivered-To: michael@amarulasolutions.com Received: by 2002:a05:7300:aca6:b0:36a:3487:1c26 with SMTP id cr38csp1709600dyb; Sun, 8 Nov 2026 05:36:28 -0800 (PST) X-Forwarded-Encrypted: i=2; AL+hErEpvGrw+YJTIpOEGUaR2ty0cXkdiVSElZ8qzkcuaCkp84ptcpbh9RkeuMSJH9xd5sqCYFtw6CeC@amarulasolutions.com X-Received: by 2002:a05:600c:4583:b0:4b0:b9ff:1e7c with SMTP id 5b1f17b1804b1-4b963c77f6cmr37987105e9.15.1794144987913; Sun, 08 Nov 2026 05:36:27 -0800 (PST) ARC-Seal: i=1; a=rsa-sha256; t=1794144987; cv=none; d=google.com; s=arc-20260327; b=oDyg5njxiN7l2VVE1fAmciI9fm4ddhiTaMZwrycbYW1O5/mNPnM7/2fh+17SIw+jPd 1AH2u8x4tjxcD3dAhPoIAeSDlGAIO/0L7gJe+AeRNYErmAftJcC2EANOH5quF1VdtVnb Z5B7Rh6b9dUwCEW1rLKJP5rr1UpRRuVtL1/luIB+mAfqkk+SZ0zbh6OGQWLjvdeSm2Oj zAArtO34BKeW7tOs7G5DhBqTQAoAZnWN47LpsldyyJJmwAG3kO0k0TeWP6dmLxWbB76c /l0OpydG0ro9M0q06b3b46NxebtreHMDNkwv3cM5UtxLpJ5xQOekmKPb4VGz7Ry9858h 6kdw== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20260327; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:dkim-signature; bh=wfio/WkVUEuyhNfQzX1UqpxV8BD2T387459hUdPuQ/I=; fh=mLYy+orUZSyOwUgz96dpSbEJuMVftc2/brdI0X+6IZM=; b=hCAH6zq1XTa/hzuBt9rshEv0a+6CgmiLfnyJSFC/yPOalkqraS7okjldLu2oIKPwxF dUE5Kin0lXoLCLHwXQ660OQDYuCgJhDXDvT+6eEiMnzhxiYqD1nCt9yuomxChSjo/Rb/ ZZ8C9ajn5qyW/pzKZMWR3bhGSrwQ2539xfBSPYlW2+95FH3qtIjb8RzRokq9dp4fX4Qe A/5tXCB9Lv1lsNg9PRSwCywUy1Zh9P5hVFk3Md1mqG1557nrFcXIyP+ncUF2hNtyRBci Z8s1Aipc6jWY+XoITxcnC08JfGje2nQKdWq6R1oSw5M8E+pAPPuOZGYVmh9AuJuBawXk vrcA==; dara=google.com ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@amarulasolutions.com header.s=google header.b=EotZiCfw; spf=pass (google.com: domain of dario.binacchi@amarulasolutions.com designates 209.85.220.41 as permitted sender) smtp.mailfrom=dario.binacchi@amarulasolutions.com; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=amarulasolutions.com; dara=neutral header.i=@amarulasolutions.com Return-Path: Received: from mail-sor-f41.google.com (mail-sor-f41.google.com. [209.85.220.41]) by mx.google.com with SMTPS id 5b1f17b1804b1-4b90ee11b7esor2176095e9.3.2026.11.08.05.36.27 for (Google Transport Security); Sun, 08 Nov 2026 05:36:27 -0800 (PST) Received-SPF: pass (google.com: domain of dario.binacchi@amarulasolutions.com designates 209.85.220.41 as permitted sender) client-ip=209.85.220.41; Authentication-Results: mx.google.com; dkim=pass header.i=@amarulasolutions.com header.s=google header.b=EotZiCfw; spf=pass (google.com: domain of dario.binacchi@amarulasolutions.com designates 209.85.220.41 as permitted sender) smtp.mailfrom=dario.binacchi@amarulasolutions.com; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=amarulasolutions.com; dara=neutral header.i=@amarulasolutions.com DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=amarulasolutions.com; s=google; t=1794144987; x=1794749787; darn=amarulasolutions.com; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:from:to:cc:subject:date :message-id:reply-to:content-type; bh=wfio/WkVUEuyhNfQzX1UqpxV8BD2T387459hUdPuQ/I=; b=EotZiCfwLz9K6N+yD1WEStzDR30+srD/2Bx5qFnLiHFs4N9yUfUVcsttxZZEQ/4pFV 1dJh+gsKhorW2STuHVHCvM/BJideLDxn+N5ZYB2mPVCgBJXCwUPEQGKz903eLRp6R/dD r1xwU08ozksP67L7qo4dfWhWZtypc6ysgewo8= X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20260707; t=1794144987; x=1794749787; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:x-gm-gg:x-gm-message-state:from :to:cc:subject:date:message-id:reply-to:content-type; bh=wfio/WkVUEuyhNfQzX1UqpxV8BD2T387459hUdPuQ/I=; b=AVWs2or2KZUWgFlws2MXjzvu+eslTgHmmWH/bUtiD+BAwfByb1vzjeJpp3w8n6TOVt gtVk5A2YnEMPJDSjnTArsu1ATaU8n9iRj6islWdZCiG2Cr4Bvs2f3ie5OJw0NeUZh46V f3tRRUGAGblHbHxw2LTuf4Y8VCjGQIL89VODhqh7Ys3jYLnHE6vyfDDfUqmFVUQcbpM0 uAT4yh30SNISFrhxOezVV5SDmFcag3yy+rtnluWCy5iFsWUqnXvGpHG6kFxcgWdoB8u6 dt0kz2y14Vi7IlJ6JBGYjPL/ezQ8JhA5NJASqjularAUPienLY5RMVEwxJpopmQf2Zhm 8KbA== X-Forwarded-Encrypted: i=1; AL+hErH0oewyBjSYFXVWFfKlnq5UZI5ttmxntuko3UjGTrPcp6CIHEJMTvFLQv1kP9Umh84ioyvBtr5E@amarulasolutions.com X-Gm-Message-State: APuU4Z8AAkagnjdqWn8QQcKwoTJVXeuMyr5d5+lGNl1ex9dbgGFJna+f 2nZ9zYJiZX5/sf6ADzJ59rEGGdU3tawYfKfSLHEHOhD6g+zoORcqirn7aL1+tHt2EAsaTA== X-Gm-Gg: AYBFou2qeBH0J60k+/TU/9Hzhf0C0QgGSiE8z5fK2T1hU6IErNG3nbqS4EUq51lDiNy AJXNhS03Pyf4DT6gAvA90G5AOSo+tISw6FzSEqWAbnENO3fVPnDcXvAsOSHJwnjND9qPxDKJWBE ksA4KEBiLEK+xBQTinedK6WA5urZ7wn/pghkbSSFdok4J8qF56e9VlOeG4FS1trL7Z3fKpZu2zn aMSONTGzuGZDYoDr7Uy+cDnr0dC2pFLnVeh5Dz9ZBZJ0peY0tT1WkTvxlwJje4r+VztK7KczgYg 7Mgh2UVWIpFNWFqhsyauIKxVHos7L/MdOy7bPnlzWntHLNzp/NXUFeYDySD8+OXRkhPNAOgnFVZ FnQGbtyiwvOEC6YpBGYl+Xu+BTwzHlyttcuVcSHoN7ZbinnXPoO7EnCEBxmSaJaSBaxHRvAQxO2 J6BoF6GXrlsvBsW+8rMVI7o5KhlwXhjZbB1LQY6u0wZ06S+O88YHT6maAAPlp5AI5/R3YWMenwz /DC/t36b/XhmCQwjTY9LSt9LL/+fzL+43Cq39JajKgwbzwDAp/YjWe8kgC0ybefIIUy97jrI/9n mz+o0I6K1r1vwOTI/2ZmLwNgu+fEkVi+x4p046UXfIv88Y+VPsNyJonKxjyylXPxHCZief33i1h PXDJeyOz9QjdGDT3m6IanwGdVnqLdcjIU6Cnl1zvJN0BaEdy0WQxYo4fpFMTWK51622skux0ABL 2PdF+hIln4/5iQ2eqsOEQd57civA== X-Received: by 2002:a05:600c:4747:b0:4a1:c6f5:2450 with SMTP id 5b1f17b1804b1-4b963c7e7e1mr31943545e9.17.1794144987296; Sun, 08 Nov 2026 05:36:27 -0800 (PST) Return-Path: Received: from dario-ThinkPad-P14s-Gen-5.homenet.telecomitalia.it (host-79-14-29-58.business.telecomitalia.it. [79.14.29.58]) by smtp.gmail.com with ESMTPSA id 5b1f17b1804b1-4b76cb76697sm92901945e9.0.2026.11.08.05.36.26 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Sun, 08 Nov 2026 05:36:26 -0800 (PST) From: Dario Binacchi To: swupdate@googlegroups.com Cc: stefano.babic@swupdate.org, linux-amarula@amarulasolutions.com, michael@amarulasolutions.com, Dario Binacchi Subject: [libubootenv][RFC PATCH 2/4] uboot_env: access the storage through callbacks Date: Sun, 8 Nov 2026 14:36:13 +0100 Message-ID: <20261108133615.2483762-3-dario.binacchi@amarulasolutions.com> X-Mailer: git-send-email 2.43.0 In-Reply-To: <20261108133615.2483762-1-dario.binacchi@amarulasolutions.com> References: <20261108133615.2483762-1-dario.binacchi@amarulasolutions.com> MIME-Version: 1.0 Load and store the environment through callbacks selected by the format of the namespace, with the U-Boot environment as the default. This allows other storage formats to be added without changing the API. No functional change. Suggested-by: Stefano Babic Link: https://groups.google.com/g/swupdate/c/kYr2Hy-f3us Signed-off-by: Dario Binacchi --- src/uboot_env.c | 35 ++++++++++++++++++++++++++++------- src/uboot_private.h | 27 +++++++++++++++++++++++++++ 2 files changed, 55 insertions(+), 7 deletions(-) -- 2.43.0 diff --git a/src/uboot_env.c b/src/uboot_env.c index d09cb8ead142..16aa416da20f 100644 --- a/src/uboot_env.c +++ b/src/uboot_env.c @@ -530,7 +530,7 @@ const struct uboot_version_info *libuboot_version_info(void) return &libinfo; } -int libuboot_env_store(struct uboot_ctx *ctx) +static int uboot_env_store(struct uboot_ctx *ctx, const struct env_helpers *h) { struct var_entry *entry; void *image; @@ -619,7 +619,7 @@ int libuboot_env_store(struct uboot_ctx *ctx) *(uint32_t *)image = crc32(0, (uint8_t *)data, ctx->size - offsetdata); copy = ctx->redundant ? (ctx->current ? 0 : 1) : 0; - ret = devwrite(ctx, copy, image); + ret = h->write(ctx, copy, image); free(image); if (ret == ctx->size) @@ -636,7 +636,7 @@ int libuboot_env_store(struct uboot_ctx *ctx) return ret; } -static int libuboot_load(struct uboot_ctx *ctx) +static int uboot_env_load(struct uboot_ctx *ctx, const struct env_helpers *h) { int ret, i; int copies = 1; @@ -676,7 +676,7 @@ static int libuboot_load(struct uboot_ctx *ctx) uint32_t crc; dev = &ctx->envdevs[i]; - ret = devread(ctx, i, buf[i]); + ret = h->read(ctx, i, buf[i]); if (ret != ctx->size) { free(buf[0]); return -EIO; @@ -761,7 +761,7 @@ static int libuboot_load(struct uboot_ctx *ctx) if (!strcmp(line, ".flags")) flagsvar = strdup(value); else - __libuboot_set_env(ctx, line, value, NULL); + h->set_env(ctx, line, value, NULL); } } @@ -790,7 +790,7 @@ static int libuboot_load(struct uboot_ctx *ctx) else *pnext++ = '\0'; - entry = __libuboot_get_env(&ctx->varlist, pvar); + entry = h->get_env(&ctx->varlist, pvar); set_var_access_type(entry, pval); pvar = pnext; } @@ -802,6 +802,27 @@ static int libuboot_load(struct uboot_ctx *ctx) return ctx->valid ? 0 : -ENODATA; } +static const struct env_helpers helpers = { + .read = devread, + .write = devwrite, + .set_env = __libuboot_set_env, + .get_env = __libuboot_get_env, +}; + +static const struct env_storage uboot_env_storage = { + .load = uboot_env_load, + .store = uboot_env_store, +}; + +static const struct env_storage *storages[] = { + [FORMAT_UBOOT] = &uboot_env_storage, +}; + +int libuboot_env_store(struct uboot_ctx *ctx) +{ + return storages[ctx->format]->store(ctx, &helpers); +} + #define LINE_LENGTH 2048 int libuboot_load_file(struct uboot_ctx *ctx, const char *filename) { @@ -1139,7 +1160,7 @@ int libuboot_open(struct uboot_ctx *ctx) { if (ret < 0) return ret; - return libuboot_load(ctx); + return storages[ctx->format]->load(ctx, &helpers); } void libuboot_close(struct uboot_ctx *ctx) { diff --git a/src/uboot_private.h b/src/uboot_private.h index fd6f43197da0..9fd8c5f996c9 100644 --- a/src/uboot_private.h +++ b/src/uboot_private.h @@ -85,6 +85,10 @@ enum flags_type { FLAGS_INCREMENTAL }; +enum env_format { + FORMAT_UBOOT, /* default */ +}; + enum device_type { DEVICE_NONE, DEVICE_FILE, @@ -185,6 +189,29 @@ struct uboot_ctx { int nelem; /** private pointer to list */ struct uboot_ctx *ctxlist; + /** storage format of the namespace */ + enum env_format format; +}; + +/** Services of the core available to a storage format */ +struct env_helpers { + /** read a copy of the storage into data */ + int (*read)(struct uboot_ctx *ctx, unsigned int copy, void *data); + /** write data to a copy of the storage */ + int (*write)(struct uboot_ctx *ctx, unsigned int copy, void *data); + /** set a variable, applying the attributes of validate if not NULL */ + int (*set_env)(struct uboot_ctx *ctx, const char *varname, + const char *value, struct var_entry *validate); + /** look up a variable */ + struct var_entry *(*get_env)(struct vars *envs, const char *varname); +}; + +/** Access to the storage of a namespace */ +struct env_storage { + /** read the storage and fill the variable list */ + int (*load)(struct uboot_ctx *ctx, const struct env_helpers *h); + /** write the variable list to the storage */ + int (*store)(struct uboot_ctx *ctx, const struct env_helpers *h); }; #if defined(__FreeBSD__)