From patchwork Sun Mar 22 23:57:04 2026 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Simon Glass X-Patchwork-Id: 2038 Return-Path: X-Original-To: u-boot-concept@u-boot.org Delivered-To: u-boot-concept@u-boot.org DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=u-boot.org; s=default; t=1774223869; bh=K0NR2ztMBpA9ROwLbPSvCtdfeyAeoHh1loSHGpwiLLo=; h=From:To:Date:In-Reply-To:References:CC:Subject:List-Id: List-Archive:List-Help:List-Owner:List-Post:List-Subscribe: List-Unsubscribe:From; b=tlXNK/YLUVjL7LLZCVYBuK6Gk9lgf2vFHadCK+ArsRp07U3vXMe+Ohx1TFF1+FTTp FHOOwXHvDdSWbhisI3x3JGR6/8oqxRSuVwzBCrIAf+iK778/Aav03yu5C0GkDSjVvm 4YI54MbfZltVVpGzE7p5bA1fRHsjKA0BLOOD9/T9uwnUOyFbrW0QndQ2Vpl8SAncFK GL+p0N1pYYiBL+vH9/kEXcUH4KDhQsDYycolooNU3m7gOW2G3srVHfnZciFLfBURsV +VmvkNFj54Uy/Ar0Q2zaJnNtaSo43tPTWLZkOHJUH116rK3tLQED7tqhrJiqYyAqdf qy3cug5ciItNQ== Received: from localhost (localhost [127.0.0.1]) by mail.u-boot.org (Postfix) with ESMTP id 1A60B6A1DA for ; Sun, 22 Mar 2026 17:57:49 -0600 (MDT) X-Virus-Scanned: Debian amavis at Received: from mail.u-boot.org ([127.0.0.1]) by localhost (mail.u-boot.org [127.0.0.1]) (amavis, port 10024) with ESMTP id c8DXbVHE7rGY for ; Sun, 22 Mar 2026 17:57:49 -0600 (MDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=u-boot.org; s=default; t=1774223867; bh=K0NR2ztMBpA9ROwLbPSvCtdfeyAeoHh1loSHGpwiLLo=; h=From:To:Date:In-Reply-To:References:CC:Subject:List-Id: List-Archive:List-Help:List-Owner:List-Post:List-Subscribe: List-Unsubscribe:From; b=jSphjzyQN8IPTA7BJzwPu8135y3MWsGcmTuWFoGnRl6rwO2cE/geqJuWH9HG/tmuZ KznocDGABiyyQ3ZjuqhF96wHokUgfP8X/VO50MiR0dZhyFqvkfg6a/FhnGoBNEXQpC 6kBuO6LEsAsaLJAJpqhs6Tfdqke5NLMfj1bqS6j9WHtY8IFyCsPUx4eT2GCxcxlbQI p8M6jVyYLGuqwMahSckJYZkpwYk214GbNoUrMJQCEeCq1d57x93u5QF32dCGR177Uk jTAPwb5B/VETpkGe5Uhe5NklUU3HRvQcqbFfJBdyYNOHq56EvfbpcgaQPFYqhsjYyO zLcapVojIcDZg== Received: from mail.u-boot.org (localhost [127.0.0.1]) by mail.u-boot.org (Postfix) with ESMTP id D88356A1F1 for ; Sun, 22 Mar 2026 17:57:47 -0600 (MDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=u-boot.org; s=default; t=1774223861; bh=dB2jdvJBdDH8oBkxF1Y7hpe9ocn4d3asudNk0lK012w=; h=From:To:Cc:Subject:Date:In-Reply-To:References:From; b=r+iaJcDIR104FKPHF9yz1CiqQktXmqsIfdWAhFipyorbWm4uN5wBiZE4y5NxOnbXk vq/1Q50n8aBd2cWZRI9VHfABTQM3z9uuqeXKfh5FBIdiJR5H6EzJc0UbJwbqX2Fc8k l0y8w0uu9LejEKl10UA/stMfOFSVN3b1b+sThXdisIbYzC/ncg/BcCvtW0qZp/15Lz MPHp/QZlNlbL3w2BOLhdIlMvTUN4XTKNcnQUxFYNrEIpWlad2vg/fMZ2zpmGb82YFm 4hZRPyXQl5RHCTQyQq9jEKU0xWbtxqFQ4j4Pb5tfrKB8JWcXpS6R/UKnz7FDLn2gA1 5xBDc0IUHO+Hw== Received: from localhost (localhost [127.0.0.1]) by mail.u-boot.org (Postfix) with ESMTP id 2FEB766F0C; Sun, 22 Mar 2026 17:57:41 -0600 (MDT) X-Virus-Scanned: Debian amavis at Received: from mail.u-boot.org ([127.0.0.1]) by localhost (mail.u-boot.org [127.0.0.1]) (amavis, port 10026) with ESMTP id OsSmQLU0S3Jo; Sun, 22 Mar 2026 17:57:41 -0600 (MDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=u-boot.org; s=default; t=1774223860; bh=hG5i7Goa4o4v+M7YkqCFkzpckEE6eYjKC408Vl/CM+8=; h=From:To:Cc:Subject:Date:In-Reply-To:References:From; b=beQJpnXJXXty5M5yIIQi2QfE6Lrmi4gRO9b/iHlYeRyUuip1d1XLdNGnxY9zrjCXY pMMLVK8OnxFimKyJtmDoxCYaN5lE9LM/pCM77X/HDhj/FuxDdpLCyw9plmJI+1LO/v ZyQX7yfKnm6CfuxclFrgjO4ltfUFwsLdH1Fl63QCxTMGypMDN7yjPlr44T/IvvurgY MJpTDz7J10CNFto26TN/xpc4C3yUUK9Lup7+a4Z3KVpgiWpuuzEZL0u4FftbMUlPQn 13nmDGIUXtRb7ADOuQN68ZtPXyKchjtERE2iFac90IXnk3tCQyFgsmr4VBwtFYcIHV PfdHkbptMjAXQ== Received: from u-boot.org (unknown [73.34.74.121]) by mail.u-boot.org (Postfix) with ESMTPSA id 6F2896A1D7; Sun, 22 Mar 2026 17:57:40 -0600 (MDT) From: Simon Glass To: U-Boot Concept Date: Sun, 22 Mar 2026 17:57:04 -0600 Message-ID: <20260322235719.1729267-7-sjg@u-boot.org> X-Mailer: git-send-email 2.43.0 In-Reply-To: <20260322235719.1729267-1-sjg@u-boot.org> References: <20260322235719.1729267-1-sjg@u-boot.org> MIME-Version: 1.0 Message-ID-Hash: YTCOEPOWOIJXLZJFFF65HLDTBFPLTFIW X-Message-ID-Hash: YTCOEPOWOIJXLZJFFF65HLDTBFPLTFIW X-MailFrom: sjg@u-boot.org X-Mailman-Rule-Misses: dmarc-mitigation; no-senders; approved; loop; banned-address; emergency; member-moderation; nonmember-moderation; administrivia; implicit-dest; max-recipients; max-size; news-moderation; no-subject; digests; suspicious-header CC: Simon Glass X-Mailman-Version: 3.3.10 Precedence: list Subject: [Concept] [PATCH 06/14] bootstd: Add free_bootflow() method for bootmeths List-Id: Discussion and patches related to U-Boot Concept Archived-At: List-Archive: List-Help: List-Owner: List-Post: List-Subscribe: List-Unsubscribe: From: Simon Glass Add a free_bootflow() op to struct bootmeth_ops so bootmeths can free internal allocations within bflow->bootmeth_priv. The bootmeth_free_bootflow() stub calls the op if provided, then always frees bootmeth_priv itself and sets it to NULL. This means drivers only need to free sub-allocations, not the priv struct. Update bootflow_free() to use the stub when a method is set. Document the bootmeth_priv lifecycle in overview.rst and bootflow.h. No drivers implement the op yet; the fallback to free() preserves the existing behaviour. Signed-off-by: Simon Glass --- boot/bootflow.c | 4 +++- boot/bootmeth-uclass.c | 10 ++++++++++ doc/develop/bootstd/overview.rst | 7 +++++++ include/bootflow.h | 4 +++- include/bootmeth.h | 24 ++++++++++++++++++++++++ 5 files changed, 47 insertions(+), 2 deletions(-) diff --git a/boot/bootflow.c b/boot/bootflow.c index 4a55cc637fa..d6549b5ce9a 100644 --- a/boot/bootflow.c +++ b/boot/bootflow.c @@ -726,7 +726,9 @@ void bootflow_free(struct bootflow *bflow) free(bflow->buf); free(bflow->os_name); free(bflow->fdt_fname); - free(bflow->bootmeth_priv); + /* bootmeth_priv is only set when method is set */ + if (bflow->method) + bootmeth_free_bootflow(bflow->method, bflow); alist_for_each(img, &bflow->images) free(img->fname); diff --git a/boot/bootmeth-uclass.c b/boot/bootmeth-uclass.c index 5e0badfc7a9..0ca3e8fca32 100644 --- a/boot/bootmeth-uclass.c +++ b/boot/bootmeth-uclass.c @@ -84,6 +84,16 @@ int bootmeth_boot(struct udevice *dev, struct bootflow *bflow) return ops->boot(dev, bflow); } +void bootmeth_free_bootflow(struct udevice *dev, struct bootflow *bflow) +{ + const struct bootmeth_ops *ops = bootmeth_get_ops(dev); + + if (ops->free_bootflow) + ops->free_bootflow(dev, bflow); + free(bflow->bootmeth_priv); + bflow->bootmeth_priv = NULL; +} + int bootmeth_read_file(struct udevice *dev, struct bootflow *bflow, const char *file_path, ulong *addrp, ulong align, enum bootflow_img_t type, ulong *sizep) diff --git a/doc/develop/bootstd/overview.rst b/doc/develop/bootstd/overview.rst index 3e3b502460d..0ff4868ba44 100644 --- a/doc/develop/bootstd/overview.rst +++ b/doc/develop/bootstd/overview.rst @@ -855,6 +855,13 @@ list of scanned bootflows just for that device. The bootflow itself is documented in bootflow_h_. It includes various bits of information about the bootflow and a buffer to hold the file. +The ``bootmeth_priv`` field allows a bootmeth to attach private data to each +bootflow, such as parsed configuration state. When the bootflow is freed, +``bootmeth_free_bootflow()`` calls the bootmeth's ``free_bootflow()`` op (if +provided) to free internal allocations, then frees ``bootmeth_priv`` itself. +Bootmeths that only store a flat struct in ``bootmeth_priv`` do not need to +implement the op. + Future ------ diff --git a/include/bootflow.h b/include/bootflow.h index 6c6f07db97d..65aebefd3b3 100644 --- a/include/bootflow.h +++ b/include/bootflow.h @@ -98,7 +98,9 @@ enum bootflow_flags_t { * @luks_version: LUKS version (1 or 2) if BOOTFLOWF_ENCRYPTED is set, else 0 * @cmdline: OS command line, or NULL if not known (allocated) * @x86_setup: Pointer to x86 setup block inside @buf, NULL if not present - * @bootmeth_priv: Private data for the bootmeth + * @bootmeth_priv: Private data for the bootmeth (allocated). Freed by + * bootmeth_free_bootflow() which calls the bootmeth's free_bootflow() op + * for internal cleanup, then frees the pointer itself. * @images: List of loaded images (struct bootstd_img) */ struct bootflow { diff --git a/include/bootmeth.h b/include/bootmeth.h index b5288843d03..2cc8b690bbf 100644 --- a/include/bootmeth.h +++ b/include/bootmeth.h @@ -155,6 +155,19 @@ struct bootmeth_ops { */ int (*boot)(struct udevice *dev, struct bootflow *bflow); + /** + * free_bootflow() - free bootmeth-private data in a bootflow + * + * This is called from bootmeth_free_bootflow() to allow the bootmeth + * to free any internal allocations within bflow->bootmeth_priv. The + * caller handles free(bflow->bootmeth_priv) afterwards, so the op + * should not free the priv struct itself. + * + * @dev: Bootmethod device + * @bflow: Bootflow being freed + */ + void (*free_bootflow)(struct udevice *dev, struct bootflow *bflow); + /** * set_property() - set the bootmeth property * @@ -309,6 +322,17 @@ int bootmeth_boot(struct udevice *dev, struct bootflow *bflow); */ int bootmeth_setup_iter_order(struct bootflow_iter *iter, bool include_global); +/** + * bootmeth_free_bootflow() - free bootmeth-private data in a bootflow + * + * Calls the bootmeth's free_bootflow() op if provided to free internal + * allocations, then frees bflow->bootmeth_priv and sets it to NULL. + * + * @dev: Bootmethod device + * @bflow: Bootflow being freed + */ +void bootmeth_free_bootflow(struct udevice *dev, struct bootflow *bflow); + /** * bootmeth_set_order() - Set the bootmeth order *