Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org Received: from mail.kernel.org (mail.kernel.org [198.145.29.99]) by smtp.lore.kernel.org (Postfix) with ESMTP id CF40BC433F5 for ; Sat, 23 Oct 2021 23:29:47 +0000 (UTC) Received: from phobos.denx.de (phobos.denx.de [85.214.62.61]) (using TLSv1.2 with cipher ECDHE-RSA-AES128-GCM-SHA256 (128/128 bits)) (No client certificate requested) by mail.kernel.org (Postfix) with ESMTPS id 304B360E9C for ; Sat, 23 Oct 2021 23:29:47 +0000 (UTC) DMARC-Filter: OpenDMARC Filter v1.4.1 mail.kernel.org 304B360E9C Authentication-Results: mail.kernel.org; dmarc=fail (p=none dis=none) header.from=chromium.org Authentication-Results: mail.kernel.org; spf=pass smtp.mailfrom=lists.denx.de Received: from h2850616.stratoserver.net (localhost [IPv6:::1]) by phobos.denx.de (Postfix) with ESMTP id 073EF83537; Sun, 24 Oct 2021 01:29:07 +0200 (CEST) Authentication-Results: phobos.denx.de; dmarc=pass (p=none dis=none) header.from=chromium.org Authentication-Results: phobos.denx.de; spf=pass smtp.mailfrom=u-boot-bounces@lists.denx.de Authentication-Results: phobos.denx.de; dkim=pass (1024-bit key; unprotected) header.d=chromium.org header.i=@chromium.org header.b="feCnGBXf"; dkim-atps=neutral Received: by phobos.denx.de (Postfix, from userid 109) id 7814383303; Sun, 24 Oct 2021 01:27:41 +0200 (CEST) Received: from mail-ot1-x333.google.com (mail-ot1-x333.google.com [IPv6:2607:f8b0:4864:20::333]) (using TLSv1.3 with cipher TLS_AES_128_GCM_SHA256 (128/128 bits)) (No client certificate requested) by phobos.denx.de (Postfix) with ESMTPS id 282DE83303 for ; Sun, 24 Oct 2021 01:27:07 +0200 (CEST) Authentication-Results: phobos.denx.de; dmarc=pass (p=none dis=none) header.from=chromium.org Authentication-Results: phobos.denx.de; spf=pass smtp.mailfrom=sjg@chromium.org Received: by mail-ot1-x333.google.com with SMTP id x27-20020a9d459b000000b0055303520cc4so9260974ote.13 for ; Sat, 23 Oct 2021 16:27:07 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=chromium.org; s=google; h=from:to:cc:subject:date:message-id:in-reply-to:references :mime-version:content-transfer-encoding; bh=PwJTVLEjYgr/2HgGkhNv10Ne7ULfxjmpxbpQkQiaXsY=; b=feCnGBXfQoOeiF9NXDF3sz6mrPqVYeoCRKGVXRkG+a1onKh5wTpzva0CFujh39IsLS PZSGECcKQYV8bRnqmkXyeZoKQhWED+bCv8EBZN6UCst1+Pjo1Me91MDvi3XtLbeDzx7o /zEv4PVuBNcgr8deV3XysBlScShChIEzF9cPM= X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20210112; h=x-gm-message-state:from:to:cc:subject:date:message-id:in-reply-to :references:mime-version:content-transfer-encoding; bh=PwJTVLEjYgr/2HgGkhNv10Ne7ULfxjmpxbpQkQiaXsY=; b=Mek+vyCrkwg6zhwM0u7mg/CEHLJWU+5do6A5ogCVh5KpQIp8chZNaSU0+AGjD+SNSP Mj52ku7NY0D2Zeu3EileVwiaRu4PhkAZIH7mQ74yyz6To6tszIdni7VAYaGMLXo4tk2T xKV6FAfCicyOVAml5P8VMD2AInkvkVW4A1lqDpiTRiODgItAOL8SelaW7tUpK97SYjjW lrjt7lOxLt46qX3T7o96bzrMAAbeFDsp9klEmbxZB9eads6czMdWFGGX07tU4itfpwo4 y7EGQNwzZbfV0K+RTv20PTjVYZO2qoYtJJhiftceJKNQ6Y3PRjBg635zTXlEON6wwzRO WPRA== X-Gm-Message-State: AOAM531Bo+eD2mjAW1UCaIAYcyqdJqyF5yaxCtiC+XbiXAdMQWufGqDK 2/UGvh5tUDEZWK2+KEUnUFT7jVtDWjqttw== X-Google-Smtp-Source: ABdhPJxE3ksrsX22x0aV4RcIzuMfkc395iyTjF83aUXgqnrSSjfvbU4v2FdqO4L1gkOO7+8/12p1Ag== X-Received: by 2002:a9d:109:: with SMTP id 9mr6777383otu.20.1635031625484; Sat, 23 Oct 2021 16:27:05 -0700 (PDT) Received: from kiwi.bld.corp.google.com (c-67-190-101-114.hsd1.co.comcast.net. [67.190.101.114]) by smtp.gmail.com with ESMTPSA id l24sm2253885oop.4.2021.10.23.16.27.04 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Sat, 23 Oct 2021 16:27:05 -0700 (PDT) From: Simon Glass To: U-Boot Mailing List Cc: Michal Simek , Heinrich Schuchardt , Tom Rini , Ilias Apalodimas , Daniel Schwierzeck , Steffen Jaeckel , =?UTF-8?q?Marek=20Beh=C3=BAn?= , Lukas Auer , Dennis Gilmore , Simon Glass Subject: [PATCH v2 19/41] bootstd: Add the concept of a bootflow Date: Sat, 23 Oct 2021 17:26:13 -0600 Message-Id: <20211023232635.9195-18-sjg@chromium.org> X-Mailer: git-send-email 2.33.0.1079.g6e70778dc9-goog In-Reply-To: <20211023232635.9195-1-sjg@chromium.org> References: <20211023232635.9195-1-sjg@chromium.org> MIME-Version: 1.0 Content-Transfer-Encoding: 8bit X-BeenThere: u-boot@lists.denx.de X-Mailman-Version: 2.1.34 Precedence: list List-Id: U-Boot discussion List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Errors-To: u-boot-bounces@lists.denx.de Sender: "U-Boot" X-Virus-Scanned: clamav-milter 0.103.2 at phobos.denx.de X-Virus-Status: Clean A bootflow encapsulates the process used to boot an operating system. It typically has a control file (such as extlinux.conf) and information about which 'bootdev' it came from. Add the header file for this first, since it is needed by all other files. Signed-off-by: Simon Glass --- (no changes since v1) MAINTAINERS | 5 + include/bootflow.h | 254 +++++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 259 insertions(+) create mode 100644 include/bootflow.h diff --git a/MAINTAINERS b/MAINTAINERS index 5069f188065..8f4211baf1f 100644 --- a/MAINTAINERS +++ b/MAINTAINERS @@ -633,6 +633,11 @@ M: Simon Glass S: Maintained F: tools/binman/ +BOOTDEVICE +M: Simon Glass +S: Maintained +F: include/bootflow.h + BTRFS M: Marek Behun R: Qu Wenruo diff --git a/include/bootflow.h b/include/bootflow.h new file mode 100644 index 00000000000..56ff101a78a --- /dev/null +++ b/include/bootflow.h @@ -0,0 +1,254 @@ +/* SPDX-License-Identifier: GPL-2.0+ */ +/* + * Copyright 2021 Google LLC + * Written by Simon Glass + */ + +#ifndef __bootflow_h +#define __bootflow_h + +#include + +/** + * enum bootflow_state_t - states that a particular bootflow can be in + * + * Only bootflows in state BOOTFLOWST_READY can be used to boot. + */ +enum bootflow_state_t { + BOOTFLOWST_BASE, /**< Nothing known yet */ + BOOTFLOWST_MEDIA, /**< Media exists */ + BOOTFLOWST_PART, /**< Partition exists */ + BOOTFLOWST_FS, /**< Filesystem exists */ + BOOTFLOWST_FILE, /**< Bootflow file exists */ + BOOTFLOWST_READY, /**< Bootflow file loaded */ + + BOOTFLOWST_COUNT +}; + +/** + * struct bootflow - information about a bootflow + * + * This is connected into two separate linked lists: + * + * bm_sibling - links all bootflows in the same bootdev + * glob_sibling - links all bootflows in all bootdevs + * + * @bm_node: Points to siblings in the same bootdev + * @glob_node: Points to siblings in the global list (all bootdev) + * @dev: Bootdevice device which produced this bootflow + * @blk: Block device which contains this bootflow, NULL if this is a network + * device + * @part: Partition number (0 for whole device) + * @method: Bootmethod device used to perform the boot and read files + * @name: Name of bootflow (allocated) + * @state: Current state (enum bootflow_state_t) + * @subdir: Subdirectory to fetch files from (with trailing /), or NULL if none + * @fname: Filename of bootflow file (allocated) + * @buf: Bootflow file contents (allocated) + * @size: Size of bootflow file in bytes + * @err: Error number received (0 if OK) + */ +struct bootflow { + struct list_head bm_node; + struct list_head glob_node; + struct udevice *dev; + struct udevice *blk; + int part; + struct udevice *method; + char *name; + enum bootflow_state_t state; + char *subdir; + char *fname; + char *buf; + int size; + int err; +}; + +/** + * enum bootflow_flags_t - flags for the bootflow iterator + * + * @BOOTFLOWF_FIXED: Only used fixed/internal media + * @BOOTFLOWF_SHOW: Show each bootdev before scanning it + * @BOOTFLOWF_ALL: Return bootflows with errors as well + * @BOOTFLOWF_SINGLE_DEV: Just scan one bootmeth + */ +enum bootflow_flags_t { + BOOTFLOWF_FIXED = 1 << 0, + BOOTFLOWF_SHOW = 1 << 1, + BOOTFLOWF_ALL = 1 << 2, + BOOTFLOWF_SINGLE_DEV = 1 << 3, +}; + +/** + * struct bootflow_iter - state for iterating through bootflows + * + * This starts at with the first bootdev/partition/bootmeth and can be used to + * iterate through all of them. + * + * Iteration starts with the bootdev. The first partition (0, i.e. whole device) + * is scanned first. For partition 0, it iterates through all the available + * bootmeths to see which one(s) can provide a bootflow. Then it moves to + * parition 1 (if there is one) and the process continues. Once all partitions + * are examined, it moves to the next bootdev. + * + * Initially @max_part is 0, meaning that only the whole device (@part=0) can be + * used. During scanning, if a partition table is found, then @max_part is + * updated to a larger value, no less than the number of available partitions. + * This ensures that iteration works through all partitions on the bootdev. + * + * @flags: Flags to use (see enum bootflow_flags_t) + * @dev: Current bootdev + * @part: Current partition number (0 for whole device) + * @method: Current bootmeth + * @max_part: Maximum hardware partition number in @dev, 0 if there is no + * partition table + * @err: Error obtained from checking the last iteration. This is used to skip + * forward (e.g. to skip the current partition because it is not valid) + * -ESHUTDOWN: try next bootdev + * @num_devs: Number of bootdevs in @dev_order + * @cur_dev: Current bootdev number, an index into @dev_order[] + * @dev_order: List of bootdevs to scan, in order of priority. The scan starts + * with the first one on the list + * @num_methods: Number of bootmeth devices in @method_order + * @cur_method: Current method number, an index into @method_order + * @method_order: List of bootmeth devices to use, in order + */ +struct bootflow_iter { + int flags; + struct udevice *dev; + int part; + struct udevice *method; + int max_part; + int err; + int num_devs; + int cur_dev; + struct udevice **dev_order; + int num_methods; + int cur_method; + struct udevice **method_order; +}; + +/** + * bootflow_iter_init() - Reset a bootflow iterator + * + * This sets everything to the starting point, ready for use. + * + * @iter: Place to store private info (inited by this call) + * @flags: Flags to use (see enum bootflow_flags_t) + */ +void bootflow_iter_init(struct bootflow_iter *iter, int flags); + +/** + * bootflow_iter_uninit() - Free memory used by an interator + * + * @iter: Iterator to free + */ +void bootflow_iter_uninit(struct bootflow_iter *iter); + +/** + * bootflow_scan_bootdev() - find the first bootflow in a bootdev + * + * If @flags includes BOOTFLOWF_ALL then bootflows with errors are returned too + * + * @dev: Boot device to scan, NULL to work through all of them until it + * finds one that * can supply a bootflow + * @iter: Place to store private info (inited by this call) + * @flags: Flags for bootdev (enum bootflow_flags_t) + * @bflow: Place to put the bootflow if found + * @return 0 if found, -ENODEV if no device, other -ve on other error + * (iteration can continue) + */ +int bootflow_scan_bootdev(struct udevice *dev, struct bootflow_iter *iter, + int flags, struct bootflow *bflow); + +/** + * bootflow_scan_first() - find the first bootflow + * + * This works through the available bootdev devices until it finds one that + * can supply a bootflow. It then returns that + * + * If @flags includes BOOTFLOWF_ALL then bootflows with errors are returned too + * + * @iter: Place to store private info (inited by this call), with + * @flags: Flags for bootdev (enum bootflow_flags_t) + * @bflow: Place to put the bootflow if found + * @return 0 if found, -ENODEV if no device, other -ve on other error (iteration + * can continue) + */ +int bootflow_scan_first(struct bootflow_iter *iter, int flags, + struct bootflow *bflow); + +/** + * bootflow_scan_next() - find the next bootflow + * + * This works through the available bootdev devices until it finds one that + * can supply a bootflow. It then returns that bootflow + * + * @iter: Private info (as set up by bootflow_scan_first()) + * @bflow: Place to put the bootflow if found + * @return 0 if found, -ENODEV if no device, -ESHUTDOWN if no more bootflows, + * other -ve on other error (iteration can continue) + */ +int bootflow_scan_next(struct bootflow_iter *iter, struct bootflow *bflow); + +/** + * bootflow_first_glob() - Get the first bootflow from the global list + * + * Returns the first bootflow in the global list, no matter what bootflow it is + * attached to + * + * @bflowp: Returns a pointer to the bootflow + * @return 0 if found, -ENOENT if there are no bootflows + */ +int bootflow_first_glob(struct bootflow **bflowp); + +/** + * bootflow_next_glob() - Get the next bootflow from the global list + * + * Returns the next bootflow in the global list, no matter what bootflow it is + * attached to + * + * @bflowp: On entry, the last bootflow returned , e.g. from + * bootflow_first_glob() + * @return 0 if found, -ENOENT if there are no more bootflows + */ +int bootflow_next_glob(struct bootflow **bflowp); + +/** + * bootflow_free() - Free memory used by a bootflow + * + * This frees fields within @bflow, but not the @bflow pointer itself + */ +void bootflow_free(struct bootflow *bflow); + +/** + * bootflow_boot() - boot a bootflow + * + * @bflow: Bootflow to boot + * @return -EPROTO if bootflow has not been loaded, -ENOSYS if the bootflow + * type is not supported, -EFAULT if the boot returned without an error + * when we are expecting it to boot, -ENOTSUPP if trying method resulted in + * finding out that is not actually supported for this boot and should not + * be tried again unless something changes + */ +int bootflow_boot(struct bootflow *bflow); + +/** + * bootflow_run_boot() - Try to boot a bootflow + * + * @iter: Current iteration (or NULL if none). Used to disable a bootmeth if the + * boot returns -ENOTSUPP + * @bflow: Bootflow to boot + * @return result of trying to boot + */ +int bootflow_run_boot(struct bootflow_iter *iter, struct bootflow *bflow); + +/** + * bootflow_state_get_name() - Get the name of a bootflow state + * + * @state: State to check + * @return name, or "?" if invalid + */ +const char *bootflow_state_get_name(enum bootflow_state_t state); + +#endif -- 2.33.0.1079.g6e70778dc9-goog