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 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 smtp.lore.kernel.org (Postfix) with ESMTPS id 69E9DC47089 for ; Wed, 7 Dec 2022 09:01:06 +0000 (UTC) Received: from h2850616.stratoserver.net (localhost [IPv6:::1]) by phobos.denx.de (Postfix) with ESMTP id 5696D85645; Wed, 7 Dec 2022 09:56:08 +0100 (CET) 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="I1I8LtP9"; dkim-atps=neutral Received: by phobos.denx.de (Postfix, from userid 109) id D18C1855A4; Wed, 7 Dec 2022 09:55:54 +0100 (CET) Received: from mail-pj1-x1030.google.com (mail-pj1-x1030.google.com [IPv6:2607:f8b0:4864:20::1030]) (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 0B0B885652 for ; Wed, 7 Dec 2022 09:55:51 +0100 (CET) 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-pj1-x1030.google.com with SMTP id w15-20020a17090a380f00b0021873113cb4so958605pjb.0 for ; Wed, 07 Dec 2022 00:55:50 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=chromium.org; s=google; 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; bh=FmQY7i5s8e/a8UF/BwRY5mJj64t77ByAXTKC5A7TQBM=; b=I1I8LtP9dPddAqDBnGA42uz5HMNtSMIq+4j1H4VAd7utA8sq7+FDvKRVB9w5jL1ugE Krr5XIFGSRAZKT+9YtS1wlzlzb4j00FEDzvwIzebn9CoLyu/ZSwu7DJWZFZagtK6XMI/ 42GucgSz7M1ob+7dVF4aYLmmGS2ZfJS4x95uQ= X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20210112; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:x-gm-message-state:from:to:cc :subject:date:message-id:reply-to; bh=FmQY7i5s8e/a8UF/BwRY5mJj64t77ByAXTKC5A7TQBM=; b=gd2RSQwxiiQW+upfgEZIJGD4pmLJM16KK5kyuRKmcjBYD9EdBsZu43tsPzvMJN+fLH vVvgZpi5/61fgW8a8NLEm0roTl5pgMbDvlPFBoWeqIfiatd56kq0JZGGqQQ4taUOL1WB UpUqkAA8aWkdAWyoCIklmhqOptmwFO6wGj24w6hr+ICMocqv7f4ZXBR7LX3CDnTZM6W7 maNGLZFVji0GqRKUttgeUrDMLMEmkbB9ukVbpD6JgGJ4cydUcf5tSY1TS/U/GEWj6ELz Mc76apKOOTtzlRF/o8K81G8labu27GJf8t/FhAtGEkUn6hkdeboVWuaKhQ4sZKPeVraD 5/nw== X-Gm-Message-State: ANoB5pmKEvQ+yPtx9hpiV9LNBAGbvN780k9JR26KpfQtGW8HD4L+N3CW A8FLKkzL2h9Zlyaitq6bPaT+48mbBvDLBJSQ X-Google-Smtp-Source: AA0mqf4LHnzbt2FVnbjQxWiU6pO0WPmAPlEqELeoLdpDlLyW+yn3UPzmkqLrxkapTwRn8Oqa8hESPw== X-Received: by 2002:a17:902:7682:b0:182:6c84:7ff4 with SMTP id m2-20020a170902768200b001826c847ff4mr81789705pll.25.1670403349158; Wed, 07 Dec 2022 00:55:49 -0800 (PST) Received: from localhost.localdomain ([27.110.126.54]) by smtp.gmail.com with ESMTPSA id l16-20020a170903121000b0018157b415dbsm14145514plh.63.2022.12.07.00.55.46 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Wed, 07 Dec 2022 00:55:48 -0800 (PST) From: Simon Glass To: U-Boot Mailing List Cc: U-Boot Custodians , Simon Glass Subject: [PATCH 33/71] bootstd: Only scan bootable partitions Date: Wed, 7 Dec 2022 21:50:59 +1300 Message-Id: <20221207085137.445249-34-sjg@chromium.org> X-Mailer: git-send-email 2.39.0.rc0.267.gcb52ba06e7-goog In-Reply-To: <20221207085137.445249-1-sjg@chromium.org> References: <20221207085137.445249-1-sjg@chromium.org> MIME-Version: 1.0 Content-Transfer-Encoding: 8bit X-BeenThere: u-boot@lists.denx.de X-Mailman-Version: 2.1.39 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.6 at phobos.denx.de X-Virus-Status: Clean At present all partitions are scanned, whether marked bootable or not. Use only bootable partitions, defaulting to partition 1 if none is found. Signed-off-by: Simon Glass --- boot/bootdev-uclass.c | 10 +++++++++- include/bootflow.h | 2 ++ test/boot/bootdev.c | 37 +++++++++++++++++++++++++++++++++++++ test/boot/bootflow.c | 4 ++-- 4 files changed, 50 insertions(+), 3 deletions(-) diff --git a/boot/bootdev-uclass.c b/boot/bootdev-uclass.c index 081b94ce332..3dcf317c150 100644 --- a/boot/bootdev-uclass.c +++ b/boot/bootdev-uclass.c @@ -163,7 +163,15 @@ int bootdev_find_in_blk(struct udevice *dev, struct udevice *blk, */ iter->max_part = MAX_PART_PER_BOOTDEV; - if (iter->part) { + /* If this is the whole disk, check if we have bootable partitions */ + if (!iter->part) { + iter->first_bootable = part_get_bootable(desc); + log_debug("checking bootable=%d\n", iter->first_bootable); + + /* if there are bootable partitions, scan only those */ + } else if (iter->first_bootable ? !info.bootable : iter->part != 1) { + return log_msg_ret("boot", -EINVAL); + } else { ret = fs_set_blk_dev_with_part(desc, bflow->part); bflow->state = BOOTFLOWST_PART; diff --git a/include/bootflow.h b/include/bootflow.h index 32dbbbbe261..7d50ca2a45d 100644 --- a/include/bootflow.h +++ b/include/bootflow.h @@ -112,6 +112,7 @@ enum bootflow_flags_t { * @method: Current bootmeth * @max_part: Maximum hardware partition number in @dev, 0 if there is no * partition table + * @first_bootable: First bootable partition, or 0 if none * @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 @@ -133,6 +134,7 @@ struct bootflow_iter { int part; struct udevice *method; int max_part; + int first_bootable; int err; int num_devs; int cur_dev; diff --git a/test/boot/bootdev.c b/test/boot/bootdev.c index 32a31c44609..db0e0ca20fa 100644 --- a/test/boot/bootdev.c +++ b/test/boot/bootdev.c @@ -301,3 +301,40 @@ static int bootdev_test_cmd_hunt(struct unit_test_state *uts) } BOOTSTD_TEST(bootdev_test_cmd_hunt, UT_TESTF_DM | UT_TESTF_SCAN_FDT | UT_TESTF_ETH_BOOTDEV); + +/* Check that only bootable partitions are processed */ +static int bootdev_test_bootable(struct unit_test_state *uts) +{ + struct bootflow_iter iter; + struct bootflow bflow; + struct udevice *blk; + + memset(&iter, '\0', sizeof(iter)); + memset(&bflow, '\0', sizeof(bflow)); + iter.part = 0; + ut_assertok(uclass_get_device_by_name(UCLASS_BLK, "mmc1.blk", &blk)); + iter.dev = blk; + ut_assertok(device_find_next_child(&iter.dev)); + uclass_first_device(UCLASS_BOOTMETH, &bflow.method); + + /* + * initially we don't have any knowledge of which partitions are + * bootable, but mmc1 has two partitions, with the first one being + * bootable + */ + iter.part = 2; + ut_asserteq(-EINVAL, bootdev_find_in_blk(iter.dev, blk, &iter, &bflow)); + ut_asserteq(0, iter.first_bootable); + + /* scan with part == 0 to get the partition info */ + iter.part = 0; + ut_asserteq(-ENOENT, bootdev_find_in_blk(iter.dev, blk, &iter, &bflow)); + ut_asserteq(1, iter.first_bootable); + + /* now it will refuse to use non-bootable partitions */ + iter.part = 2; + ut_asserteq(-EINVAL, bootdev_find_in_blk(iter.dev, blk, &iter, &bflow)); + + return 0; +} +BOOTSTD_TEST(bootdev_test_bootable, UT_TESTF_DM | UT_TESTF_SCAN_FDT); diff --git a/test/boot/bootflow.c b/test/boot/bootflow.c index 54649f077c7..9e9a3e4061c 100644 --- a/test/boot/bootflow.c +++ b/test/boot/bootflow.c @@ -306,14 +306,14 @@ static int bootflow_iter(struct unit_test_state *uts) bootflow_free(&bflow); /* Then more to partition 2 which exists but is not bootable */ - ut_asserteq(-EPERM, bootflow_scan_next(&iter, &bflow)); + ut_asserteq(-EINVAL, bootflow_scan_next(&iter, &bflow)); ut_asserteq(2, iter.num_methods); ut_asserteq(0, iter.cur_method); ut_asserteq(2, iter.part); ut_asserteq(0x1e, iter.max_part); ut_asserteq_str("syslinux", iter.method->name); ut_asserteq(0, bflow.err); - ut_asserteq(BOOTFLOWST_PART, bflow.state); + ut_asserteq(BOOTFLOWST_MEDIA, bflow.state); bootflow_free(&bflow); bootflow_iter_uninit(&iter); -- 2.39.0.rc0.267.gcb52ba06e7-goog