From patchwork Fri Nov 13 17:17:31 2026 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Dario Binacchi X-Patchwork-Id: 5019 Delivered-To: michael@amarulasolutions.com Received: by 2002:a05:7301:4187:b0:36a:3487:1c26 with SMTP id ph7csp545407dyb; Fri, 13 Nov 2026 09:17:48 -0800 (PST) X-Forwarded-Encrypted: i=2; AL+hErE19pK+I2vTHWZzKbgdQwSJ+sqlUq2n76aFxpryKe3b7bOCW+pR+aCUkXw7haUjYZdV/gHgdPSZ@amarulasolutions.com X-Received: by 2002:a05:600c:630f:b0:4a1:7ec7:a479 with SMTP id 5b1f17b1804b1-4bb55aa5795mr72705785e9.33.1794590268500; Fri, 13 Nov 2026 09:17:48 -0800 (PST) ARC-Seal: i=1; a=rsa-sha256; t=1794590268; cv=none; d=google.com; s=arc-20260327; b=tCeGU4fd3L3Do5JWnn28pcEzVCHUVyWRI2KIHc9x6ZbZKyUVb7EbNkMumL8P1ub0on 9ZT/7FMlXV8vinc0XJLEdI/+3hddWBZJc1deNN0XkEipGZvOQGupJzpLpJv7vzblaHlA GCt3tZcZk3wDnlAm+uOzeZEG+jalrMuDLAzYn2WDOwy8dDcOuTK2WtO0rWlbwb1KQLb1 Vr2ZEkI/a5g5TxVBRMmaDCFYSKJQ/fSPvA5bQI7Ovxzc2/mn6Y1s8g4oqc/j35MUNZoB eADinV4XnT7magZBJT2aTCN4ruHaLRNIo5uKN/dS/H5ekDRHlesUodCPCx0r2rhuG7Da bxQQ== 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=dZs/XF1VaDgjKn9kGoi2mcCbRECJ52K6X8IdMndoHzM=; fh=9JQobalafcTTDQGhX6OWlBgrxJxdrEhHNBrnKPxafY0=; b=sLHMUIolZgKj1tKy6QPxZrET830RBfSfrPkrTieue0AL1eTyjHK6To/k6dWEZTiVoH hNn33F5ZxCy0nnUHCZiC3q3ZDzBIkodCMN0dDDQ5fabE2+K4tXYe+MrBft+C6znSNIAP 59muSBmb+o+G4NWfd1YtzzF9blMSvLJ9t5bD94zfiasVdTcCtuuDsQhlBnWbv1vjX7sN RM+sqKtuMAZvYf/8g4gzihSGoRjXnMyh2yXtFEvGhYIvN5oY9B2mu3/I0sxsD/YF4yqA fl91Q++hLtBtELRm8jbNuydmvrO3F4mRx0QLXwc5goI305KBpmc1+wFz95eDFBmrwoEV +2NQ==; dara=google.com ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@amarulasolutions.com header.s=google header.b=iqhz3HR+; 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-4bb55a2d783sor5682525e9.0.2026.11.13.09.17.48 for (Google Transport Security); Fri, 13 Nov 2026 09:17:48 -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=iqhz3HR+; 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=1794590268; x=1795195068; 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=dZs/XF1VaDgjKn9kGoi2mcCbRECJ52K6X8IdMndoHzM=; b=iqhz3HR+6cFepGDVFuQnIY9ga/lNCmKACVNuN02GgeF1SORA80hLvhDZv2jGaH9x3K jPz/OvKjAD8Ij6+xWzBdI3kcTnI+b7pYoJAQhHTixV0FOMoHZtfP0vblMt2E+mevdI7u 3ZTlPCCLOroPlM4INPzDcXua/7/I7RH4W+YKY= X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20260707; t=1794590268; x=1795195068; 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=dZs/XF1VaDgjKn9kGoi2mcCbRECJ52K6X8IdMndoHzM=; b=A9nEjgZsBjwlJ5E+cEgwL4Cdj1s+BQbQq8JNcZerkezaevJtMTZC39XqOfp1Ygn+5g rqW+jWHZbsKm0BbdVki+OQtWiFLJ6zPWDCmN+33k1Dg6fDTJEh77aKuwEhlMIV/wXZW5 u/d+Vwe4MWilBpJJd78pJfFya4qMCV4kMLi1oWfdT4LyURQlZ6M9JRda3S+TIbDbQLMm wvXeck0m5+zseda2rf15BJJtBcCosXZCXUEmRRfS85rEYNtk6/q1aX+1b+5WSXlztkO1 MjGn+9d0yp5yl41ajIQz43IqiBTR/gPL7XXVmVU+rnWSAgqFu8Was9s1PheMxgFk1Yq/ FUvQ== X-Forwarded-Encrypted: i=1; AL+hErH8pyBNtpgRtDIFK8bNRngNQhYH0XIWm9ij9pEvyKdMcAMwl+ptRW3AEELT9ttvStAst1b3DvwE@amarulasolutions.com X-Gm-Message-State: APuU4Z8h5riZe2zfVe66cufmQcEiOH4zE3F71fY8TD04mTly2KltsWdg XoydYZORgX16YtwSVKSFKgSO4NnGp5FutapAqC3bQXdYHVQRGGueRGCkvXPYw+hPAy/q9wdXxTW hAzAS9t4= X-Gm-Gg: AYBFou1WAqBDegidRsgDqxsR3qX4BAU4u7sxwErWp2A9MNCKEXE/nQyhN5YYHvtrh5Z 6yE9Q+jyG8AN2bcqcjH/TC4C+tA0GcNKZuF5BbZ8w0VEBoLLFxBk7nQsD5s6T7WyhKg5EJL6Zls vReBdGVZMlAaCxO2NEsLgDkAYsqYKqe//DVONjupfqD/Ul8OTMceGDz3ziV3cIfb5SWyOgfkRxm iS3xkrABIyB/nVrEgCWOqd4xklyREQfZTSgBXDlP6A2y/TicI5q09qTo6ryyAgfmsf8x7vHzZwI 6oiA3EVZ5BdV93wrLWh97vTY6kQyZMuUBpEH5GqbATEFkoZC34A9DvYEodiiQcXTVvqJKUZQg7v MZN7HgiQSbaM4meMS9kqj48h4qsLGQWTZC6nhcxn5dQDWPZCGVAxZMCxgA9GfLnMpU2rXBWFgTF F2zvZi0T/ql2nhk4/ULvsnwUQRWpXBxZmEXST+1gu/jQDJLUnf/zwi4I/sGp9UF72/7yPpjwLB5 BYTSe58zmPlzcxAWIhDrX1MEE3JPX8eH+L7M4C1kstk9nPOfvD9I2whRKqcS+T5LKILYQF84zf3 qCp/jiC2BL9Cz7S+dyR9L+Wqml5qB6+W6hasA838QT1wjh0vJU4xJHdM3ioJ9hU4s/SvlcHQBDl qVWyHRGlYr5ZZJFN8TFSVQ/qp+U8desdPMHVCqPngRExiWVhkTjImWEU25qlNoN1tri9rE9TiVc nQXesxTObCCfS1QLhn/hI28ILTIFKHaMo/2BBti7ZVBI9/Q/s= X-Received: by 2002:a05:600c:a012:b0:4b8:d17e:da27 with SMTP id 5b1f17b1804b1-4bb55a85101mr65184145e9.27.1794590267881; Fri, 13 Nov 2026 09:17:47 -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-4bb55ac0e1esm68545935e9.3.2026.11.13.09.17.46 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Fri, 13 Nov 2026 09:17:47 -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][PATCH v2 2/8] uboot_env: access the storage through callbacks Date: Fri, 13 Nov 2026 18:17:31 +0100 Message-ID: <20261113171737.470894-3-dario.binacchi@amarulasolutions.com> X-Mailer: git-send-email 2.43.0 In-Reply-To: <20261113171737.470894-1-dario.binacchi@amarulasolutions.com> References: <20261113171737.470894-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 --- (no changes since v1) 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__)