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 9E2FDC0015E for ; Thu, 27 Jul 2023 03:02:14 +0000 (UTC) Received: from h2850616.stratoserver.net (localhost [IPv6:::1]) by phobos.denx.de (Postfix) with ESMTP id E25DC867D9; Thu, 27 Jul 2023 05:01:58 +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="Hsek5SyL"; dkim-atps=neutral Received: by phobos.denx.de (Postfix, from userid 109) id C5A93863DB; Thu, 27 Jul 2023 05:01:48 +0200 (CEST) Received: from mail-io1-xd2f.google.com (mail-io1-xd2f.google.com [IPv6:2607:f8b0:4864:20::d2f]) (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 2AA1A86792 for ; Thu, 27 Jul 2023 05:01:45 +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-io1-xd2f.google.com with SMTP id ca18e2360f4ac-78360b822abso16029139f.2 for ; Wed, 26 Jul 2023 20:01:45 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=chromium.org; s=google; t=1690426903; x=1691031703; 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=74zZXuh+yOX7Ytlf7ni10pXNH0ZbSJmN88VNIDgVS0s=; b=Hsek5SyL6uenRqcKmIq2YlVmZOHF+iIrGRqalN/2Ey/Y2oysPl8k/qXQ43PXr8jnnB nNCTeltWFZ96TAktw//rx4kiuFVZkLJxB9zuuyt89HodOst6rmVwuT97IbL3TVobK/rQ QE28kSIaw54aOCioosYSX9G0c69bTqJ3jwOEU= X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20221208; t=1690426903; x=1691031703; 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=74zZXuh+yOX7Ytlf7ni10pXNH0ZbSJmN88VNIDgVS0s=; b=A/6ycuL8SmxEgBEejpB4dlsb/CPCkiioQZGtrGjSEvNHaiE5T0NqfE3ESA4DKZ7nAu dh1nBe4JNBMpK2Rp1D22U7TR+bQC9HgxPSrkbG0VxOcJSx7F/HlE4Z8sPYHbEcTdX4JS UqZJ9yGhOxOX5KRkw83uv6OqYaZiNYYWNqtmTRo03+lrYZJ7sDQYmmebcyRzulv3+1l0 bOBbxHGsP9Qusv9Yi+oLyVLEGEF/YnMZt56E2YIqcRdBDqULNaYPIRIHeElKl5lR10gb VV6qH1S6e+VhrFPEmF6B76euNyRaQoIwG2Owri7a0lld6+Pl5HRQ/WbY1LuZbODsepSs 3O5w== X-Gm-Message-State: ABy/qLYrsDOtUVS+grSGk383qrgxau+qq+9F6hVgtW1mGJ8I54b/jrig qOtJUDirsOsP2CJs5oFBn1rYljBwghcOn27rbLs= X-Google-Smtp-Source: APBJJlHBKi7TsiQpAa55XoB4/8aX6JWN884/u8hbAtFVs8RWKhBkZ+8jXtvR2RjzoztoxMBYYCv0AQ== X-Received: by 2002:a05:6e02:1206:b0:347:7555:b739 with SMTP id a6-20020a056e02120600b003477555b739mr3391828ilq.32.1690426903619; Wed, 26 Jul 2023 20:01:43 -0700 (PDT) Received: from sjg1.lan (c-73-14-173-85.hsd1.co.comcast.net. [73.14.173.85]) by smtp.gmail.com with ESMTPSA id a14-20020a92d58e000000b00348d41ffd13sm207539iln.85.2023.07.26.20.01.43 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Wed, 26 Jul 2023 20:01:43 -0700 (PDT) From: Simon Glass To: U-Boot Mailing List Cc: Tom Rini , Simon Glass , Da Xue , =?UTF-8?q?Vincent=20Stehl=C3=A9?= Subject: [PATCH 3/5] bootstd: Avoid allocating memory for the EFI file Date: Wed, 26 Jul 2023 21:01:23 -0600 Message-ID: <20230727030128.459760-4-sjg@chromium.org> X-Mailer: git-send-email 2.41.0.487.g6d72f3e995-goog In-Reply-To: <20230727030128.459760-1-sjg@chromium.org> References: <20230727030128.459760-1-sjg@chromium.org> MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 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.8 at phobos.denx.de X-Virus-Status: Clean The current bootflow-iteration algorithm reads the bootflow file into an allocated memory buffer so it can be examined. This works well in most cases. However, while the common case is that the first bootflow is immediately booted, it is also possible just to scan for available bootflows, perhaps selecting one to boot later. Even with the common case, EFI bootflows can be quite large. It doesn't make sense to read it into an allocated buffer when we have kernel_addr_t providing a suitable address for it. Even if we do have plenty of malloc() space available, it is a violation of U-Boot's lazy-init principle to read the bootflow before it is needed. So overall it seems better to make a change. Adjust the logic to read just the size of the EFI file at first. Later, when the bootflow is booted, read the rest of the file into the designated kernel buffer. Signed-off-by: Simon Glass Reported-by: Da Xue Reported-by: Vincent Stehlé --- boot/bootmeth_efi.c | 50 ++++++++++++++++++++++++++++++--------------- 1 file changed, 33 insertions(+), 17 deletions(-) diff --git a/boot/bootmeth_efi.c b/boot/bootmeth_efi.c index e88e171ccc1b..bceec0d12ef3 100644 --- a/boot/bootmeth_efi.c +++ b/boot/bootmeth_efi.c @@ -143,13 +143,24 @@ static void set_efi_bootdev(struct blk_desc *desc, struct bootflow *bflow) efi_set_bootdev(dev_name, devnum_str, bflow->fname, bflow->buf, size); } -static int efiload_read_file(struct blk_desc *desc, struct bootflow *bflow) +static int efiload_read_file(struct bootflow *bflow, ulong addr) { + struct blk_desc *desc = NULL; + loff_t bytes_read; int ret; - ret = bootmeth_alloc_file(bflow, 0x2000000, 0x10000); + if (bflow->blk) + desc = dev_get_uclass_plat(bflow->blk); + ret = bootmeth_setup_fs(bflow, desc); + if (ret) + return log_msg_ret("set", ret); + + ret = fs_read(bflow->fname, addr, 0, bflow->size, &bytes_read); if (ret) return log_msg_ret("read", ret); + bflow->buf = map_sysmem(addr, bflow->size); + + set_efi_bootdev(desc, bflow); return 0; } @@ -223,7 +234,18 @@ static int distro_efi_get_fdt_name(char *fname, int size, int seq) return 0; } -static int distro_efi_read_bootflow_file(struct udevice *dev, +/* + * distro_efi_try_bootflow_files() - Check that files are present + * + * This reads any FDT file and checks whether the bootflow file is present, for + * later reading. We avoid reading the bootflow now, since it is likely large, + * it may take a long time and we want to avoid needing to allocate memory for + * it + * + * @dev: bootmeth device to use + * @bflow: bootflow to update + */ +static int distro_efi_try_bootflow_files(struct udevice *dev, struct bootflow *bflow) { struct blk_desc *desc = NULL; @@ -247,9 +269,8 @@ static int distro_efi_read_bootflow_file(struct udevice *dev, if (ret) return log_msg_ret("try", ret); - ret = efiload_read_file(desc, bflow); - if (ret) - return log_msg_ret("read", ret); + /* Since we can access the file, let's call it ready */ + bflow->state = BOOTFLOWST_READY; fdt_addr = env_get_hex("fdt_addr_r", 0); @@ -376,7 +397,7 @@ static int distro_efi_read_bootflow(struct udevice *dev, struct bootflow *bflow) if (ret) return log_msg_ret("net", ret); } else { - ret = distro_efi_read_bootflow_file(dev, bflow); + ret = distro_efi_try_bootflow_files(dev, bflow); if (ret) return log_msg_ret("blk", ret); } @@ -388,17 +409,13 @@ int distro_efi_boot(struct udevice *dev, struct bootflow *bflow) { ulong kernel, fdt; char cmd[50]; + int ret; - /* A non-zero buffer indicates the kernel is there */ + kernel = env_get_hex("kernel_addr_r", 0); if (!bootmeth_uses_network(bflow)) { - /* Set the EFI bootdev again, since reading an FDT loses it! */ - if (bflow->blk) { - struct blk_desc *desc = dev_get_uclass_plat(bflow->blk); - - set_efi_bootdev(desc, bflow); - } - - kernel = (ulong)map_to_sysmem(bflow->buf); + ret = efiload_read_file(bflow, kernel); + if (ret) + return log_msg_ret("read", ret); /* * use the provided device tree if available, else fall back to @@ -417,7 +434,6 @@ int distro_efi_boot(struct udevice *dev, struct bootflow *bflow) * But this is the same behaviour for distro boot, so it can be * fixed here. */ - kernel = env_get_hex("kernel_addr_r", 0); fdt = env_get_hex("fdt_addr_r", 0); } -- 2.41.0.487.g6d72f3e995-goog