From patchwork Mon Jul 31 22:42:54 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Sean Anderson X-Patchwork-Id: 1815291 X-Patchwork-Delegate: trini@ti.com Return-Path: X-Original-To: incoming@patchwork.ozlabs.org Delivered-To: patchwork-incoming@legolas.ozlabs.org Authentication-Results: legolas.ozlabs.org; spf=pass (sender SPF authorized) smtp.mailfrom=lists.denx.de (client-ip=2a01:238:438b:c500:173d:9f52:ddab:ee01; helo=phobos.denx.de; envelope-from=u-boot-bounces@lists.denx.de; receiver=) Authentication-Results: legolas.ozlabs.org; dkim=pass (2048-bit key; unprotected) header.d=seco.com header.i=@seco.com header.a=rsa-sha256 header.s=selector1 header.b=ZVtuGsyq; dkim=pass (2048-bit key) header.d=seco.com header.i=@seco.com header.a=rsa-sha256 header.s=selector1 header.b=ZVtuGsyq; dkim-atps=neutral Received: from phobos.denx.de (phobos.denx.de [IPv6:2a01:238:438b:c500:173d:9f52:ddab:ee01]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange X25519 server-signature ECDSA (P-384)) (No client certificate requested) by legolas.ozlabs.org (Postfix) with ESMTPS id 4RFCvv1Cnsz1yfC for ; Tue, 1 Aug 2023 08:43:55 +1000 (AEST) Received: from h2850616.stratoserver.net (localhost [IPv6:::1]) by phobos.denx.de (Postfix) with ESMTP id 2C1DF86ADA; Tue, 1 Aug 2023 00:43:32 +0200 (CEST) Authentication-Results: phobos.denx.de; dmarc=pass (p=reject dis=none) header.from=seco.com Authentication-Results: phobos.denx.de; spf=pass smtp.mailfrom=u-boot-bounces@lists.denx.de Authentication-Results: phobos.denx.de; dkim=pass (2048-bit key; unprotected) header.d=seco.com header.i=@seco.com header.b="ZVtuGsyq"; dkim=pass (2048-bit key) header.d=seco.com header.i=@seco.com header.b="ZVtuGsyq"; dkim-atps=neutral Received: by phobos.denx.de (Postfix, from userid 109) id 4D7038682B; Tue, 1 Aug 2023 00:43:30 +0200 (CEST) X-Spam-Checker-Version: SpamAssassin 3.4.2 (2018-09-13) on phobos.denx.de X-Spam-Level: X-Spam-Status: No, score=-2.1 required=5.0 tests=BAYES_00,DKIM_SIGNED, DKIM_VALID,DKIM_VALID_AU,DKIM_VALID_EF,SPF_HELO_PASS,SPF_PASS, T_SCC_BODY_TEXT_LINE autolearn=unavailable autolearn_force=no version=3.4.2 Received: from EUR04-VI1-obe.outbound.protection.outlook.com (mail-vi1eur04on0615.outbound.protection.outlook.com [IPv6:2a01:111:f400:fe0e::615]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by phobos.denx.de (Postfix) with ESMTPS id 9FB6986AB4 for ; Tue, 1 Aug 2023 00:43:27 +0200 (CEST) Authentication-Results: phobos.denx.de; dmarc=pass (p=reject dis=none) header.from=seco.com Authentication-Results: phobos.denx.de; spf=pass smtp.mailfrom=sean.anderson@seco.com DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=seco.com; s=selector1; h=From:Date:Subject:Message-ID:Content-Type:MIME-Version:X-MS-Exchange-SenderADCheck; bh=H6uE5P9lXzch3qUspdKEJ3MjgWwoYF6KLdni7AFbku8=; b=ZVtuGsyqy7HzSPWYyzjucAUI73jR3pHabl6iJngyb7WvIAwxLPX36DZx+3Wq5Q1eBXPRtXGo9nVAqUgwQ66Zll/G9H4NiziyQDi4FGFa/fJQLLrVlNr2B9R5JfPgtAaacCj1kDNrM2C/BB7mPzUkfqdqJ05QAHN6aWK3oVOKLyZQKYWMtzOxhEt98e1NhHzvQGarhgOtZuJMBOpZYuFNlYDj8qUV5xA3qxsSuDnyzW7sgX2p36Xu/hvNbvgFd5zZvn6ncjI3RnfjWGLFkni+BA3rJ6hrTRoVBvrlGLBLVdTI5oD4EkIIASLaKZblAqiDxkydxkixXYeMm5XBOHF8JQ== Received: from DU2P250CA0011.EURP250.PROD.OUTLOOK.COM (2603:10a6:10:231::16) by DB3PR03MB10129.eurprd03.prod.outlook.com (2603:10a6:10:436::8) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id 15.20.6631.43; Mon, 31 Jul 2023 22:43:25 +0000 Received: from DB8EUR05FT068.eop-eur05.prod.protection.outlook.com (2603:10a6:10:231:cafe::56) by DU2P250CA0011.outlook.office365.com (2603:10a6:10:231::16) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id 15.20.6631.43 via Frontend Transport; Mon, 31 Jul 2023 22:43:25 +0000 X-MS-Exchange-Authentication-Results: spf=pass (sender IP is 20.160.56.84) smtp.mailfrom=seco.com; dkim=pass (signature was verified) header.d=seco.com;dmarc=pass action=none header.from=seco.com; Received-SPF: Pass (protection.outlook.com: domain of seco.com designates 20.160.56.84 as permitted sender) receiver=protection.outlook.com; client-ip=20.160.56.84; helo=inpost-eu.tmcas.trendmicro.com; pr=C Received: from inpost-eu.tmcas.trendmicro.com (20.160.56.84) by DB8EUR05FT068.mail.protection.outlook.com (10.233.238.69) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id 15.20.6631.44 via Frontend Transport; Mon, 31 Jul 2023 22:43:24 +0000 Received: from outmta (unknown [192.168.82.132]) by inpost-eu.tmcas.trendmicro.com (Trend Micro CAS) with ESMTP id A17732008008B; Mon, 31 Jul 2023 22:43:24 +0000 (UTC) Received: from EUR05-VI1-obe.outbound.protection.outlook.com (unknown [104.47.17.171]) by repre.tmcas.trendmicro.com (Trend Micro CAS) with ESMTPS id A4D3B2008006E; Mon, 31 Jul 2023 22:43:18 +0000 (UTC) ARC-Seal: i=1; a=rsa-sha256; s=arcselector9901; d=microsoft.com; cv=none; b=DLUoaqI4/qqfXyz1muJu0Gsxklj4PDmPaQEtXMEC4tPBieIFN6ZCip6ONClwAzKoxmjJJnaUQFBjxm5ssC1o4Qid2tBSSKTeV16Gqn8C3g9NBd1tPEaZOtJtUxgNhfk9HI5Xo2E4bHuYeV2TX/T8kHoQW7dq0+i2SAhBcLmvqFlkzqkpnVEX3ZYp4Cj4jXKoLb7uwog58UlIoWnTHhVSrs6WQfKZer17QkOD1IqePMcoxNlGgXLdY1P0GjpdNFCQCg4Rku5Uug6syNccUh6yCnBeMJU2flwn2KLKfXAsG6kh/5G3ls/24E60o7NQjNNgbEUKJB7YCSCzPG5+7oQghA== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=microsoft.com; s=arcselector9901; h=From:Date:Subject:Message-ID:Content-Type:MIME-Version:X-MS-Exchange-AntiSpam-MessageData-ChunkCount:X-MS-Exchange-AntiSpam-MessageData-0:X-MS-Exchange-AntiSpam-MessageData-1; bh=H6uE5P9lXzch3qUspdKEJ3MjgWwoYF6KLdni7AFbku8=; b=D452iC4GPSpjrn8ovQ8iRJavuQMIiJXb1KJ2oXW3nr36A97lXmsIkdYAp4NaAa2UI2eDmimUPQYryX5oDc9uFF2IjqahpTKxacE4LreQXvEGn4XrNQPb16cbKWTlm9nBFUCta5jVLIaabsgREuInunCCNWpwTnNloAu7LccrPIPpwBazaC774dug6bMFTtGXUYOnOFcNNobqeqYgJLP4CGIU4qfDr5MiqPJTiwz4FBChHYZrSpBsNKVyDTm2MzZneawQEAXzXGomniWkn2ErgmOev9XzPKUIWgqXoH2KTaSND8msrqj8oZSIEo6Dz2lM3KDJeY+CrVCv2kIMzpg34A== ARC-Authentication-Results: i=1; mx.microsoft.com 1; spf=pass smtp.mailfrom=seco.com; dmarc=pass action=none header.from=seco.com; dkim=pass header.d=seco.com; arc=none DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=seco.com; s=selector1; h=From:Date:Subject:Message-ID:Content-Type:MIME-Version:X-MS-Exchange-SenderADCheck; bh=H6uE5P9lXzch3qUspdKEJ3MjgWwoYF6KLdni7AFbku8=; b=ZVtuGsyqy7HzSPWYyzjucAUI73jR3pHabl6iJngyb7WvIAwxLPX36DZx+3Wq5Q1eBXPRtXGo9nVAqUgwQ66Zll/G9H4NiziyQDi4FGFa/fJQLLrVlNr2B9R5JfPgtAaacCj1kDNrM2C/BB7mPzUkfqdqJ05QAHN6aWK3oVOKLyZQKYWMtzOxhEt98e1NhHzvQGarhgOtZuJMBOpZYuFNlYDj8qUV5xA3qxsSuDnyzW7sgX2p36Xu/hvNbvgFd5zZvn6ncjI3RnfjWGLFkni+BA3rJ6hrTRoVBvrlGLBLVdTI5oD4EkIIASLaKZblAqiDxkydxkixXYeMm5XBOHF8JQ== Authentication-Results-Original: dkim=none (message not signed) header.d=none;dmarc=none action=none header.from=seco.com; Received: from DB9PR03MB8847.eurprd03.prod.outlook.com (2603:10a6:10:3dd::13) by PAVPR03MB9163.eurprd03.prod.outlook.com (2603:10a6:102:32f::19) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id 15.20.6631.42; Mon, 31 Jul 2023 22:43:17 +0000 Received: from DB9PR03MB8847.eurprd03.prod.outlook.com ([fe80::65d7:d7d8:711b:2514]) by DB9PR03MB8847.eurprd03.prod.outlook.com ([fe80::65d7:d7d8:711b:2514%5]) with mapi id 15.20.6631.043; Mon, 31 Jul 2023 22:43:17 +0000 From: Sean Anderson To: Tom Rini , u-boot@lists.denx.de Cc: =?utf-8?q?Pali_Roh=C3=A1r?= , Stefan Roese , =?utf-8?q?Marek_Beh=C3=BAn?= , Simon Glass , Xavier Drudis Ferran , Heinrich Schuchardt , Marek Vasut , Sean Anderson Subject: [PATCH v5 02/11] spl: Add generic spl_load function Date: Mon, 31 Jul 2023 18:42:54 -0400 Message-Id: <20230731224304.111081-3-sean.anderson@seco.com> X-Mailer: git-send-email 2.40.1 In-Reply-To: <20230731224304.111081-1-sean.anderson@seco.com> References: <20230731224304.111081-1-sean.anderson@seco.com> X-ClientProxiedBy: MN2PR15CA0013.namprd15.prod.outlook.com (2603:10b6:208:1b4::26) To DB9PR03MB8847.eurprd03.prod.outlook.com (2603:10a6:10:3dd::13) MIME-Version: 1.0 X-MS-TrafficTypeDiagnostic: DB9PR03MB8847:EE_|PAVPR03MB9163:EE_|DB8EUR05FT068:EE_|DB3PR03MB10129:EE_ X-MS-Office365-Filtering-Correlation-Id: c6a241ea-acd9-4db3-025e-08db92178cf9 X-TrendMicro-CAS-OUT-LOOP-IDENTIFIER: 656f966764b7fb185830381c646b41a1 X-MS-Exchange-SenderADCheck: 1 X-MS-Exchange-AntiSpam-Relay: 0 X-Microsoft-Antispam-Untrusted: BCL:0; X-Microsoft-Antispam-Message-Info-Original: wfJUGDDEuU9GCpUpyc5u48Jj/OkEcBmmF8THAYBazhfMPBn3ubR6IaWTZVC458A4cIgeqIUR2ulVC5YA/dnF0mCtRSuc5kXEar6yT2QLmUNXb5R6Ly2kDPsWFajAKcMTWHYUMDT6Az031YKkhoZVFZQUsvBrz5wkONY983m3Q96i8bIQISOZI/eT6wg0XbKBmJzLBni8PykQl8tQNflge7dfgFS6NDOcgNAZL5fY8ItNQ3TGfQHRtCB3Y3i+jkkxDSKya7X1q6b3zVpz2LAEcfhMuE9pF4LhgVKWmwxeT8qVf2mAi0X0zQxK3codvMaouOPv3a+g4LDxsd2DCsX9FEg8AleXn84HA+wnFpkC6BtpKrO/YbBu4mp/M7he0mS3z+5MbbyjqBZfvBifxcuDIsktRqDiT1OJHsQZVa4Yh+WXGfnGZ8O37zaontKCnA46HEQXlK6tSMYb4mGJFKwU6whs8XIrxc9K/loFuqj4xENQZiCIgliP3ykT0y5kwrGaD7f5ntCx4qPiUEyiqGXEj2yeM/yiP9Bg8A6J0mOqTFWYiist76SCtZc6IkBKIX+7gwmnuAh5jW8knZpNu9U4HD0LbyNc5xdLcp0jAZ+USOMwCO0YRPsFiJBEZQn2RkHtweKT8X4MaNbqm4kG2dn0kA== X-Forefront-Antispam-Report-Untrusted: CIP:255.255.255.255; CTRY:; LANG:en; SCL:1; SRV:; IPV:NLI; SFV:NSPM; H:DB9PR03MB8847.eurprd03.prod.outlook.com; PTR:; CAT:NONE; SFS:(13230028)(39850400004)(136003)(396003)(366004)(376002)(346002)(451199021)(36756003)(478600001)(38350700002)(38100700002)(86362001)(6512007)(52116002)(6486002)(6666004)(2616005)(186003)(107886003)(8676002)(1076003)(26005)(8936002)(6506007)(5660300002)(44832011)(4326008)(2906002)(66476007)(66946007)(66556008)(54906003)(41300700001)(316002)(83380400001)(41533002); DIR:OUT; SFP:1101; X-MS-Exchange-Transport-CrossTenantHeadersStamped: PAVPR03MB9163 X-EOPAttributedMessage: 0 X-MS-Exchange-Transport-CrossTenantHeadersStripped: DB8EUR05FT068.eop-eur05.prod.protection.outlook.com X-MS-PublicTrafficType: Email X-MS-Office365-Filtering-Correlation-Id-Prvs: 5a7a0330-843f-4442-5b67-08db92178852 X-Microsoft-Antispam: BCL:0; X-Microsoft-Antispam-Message-Info: uBl2hfDXfwDAqEkIi4BVkhmJWGq2cQdIgxLx0ofmA/9mJV/1c4xGYf+bMII1ZgEbFl/74kpD5wKWV5XwFlD/tphhV5Vvth7PNv44lTs+3Fxt2kORuKZ/tW3BcTiYx+9mag1oZdiTy5S8flFGfSCGEsTT2EOoYquC+ZGIZ9wYZ4cr+XQvlirLVfQd0cGvx1kIaroAkadbLmO3hpJW/BR6ot7BsoicCOXn0LXrGWssrdilFSv2BkMuKQ34bepUm7XZIGMqoyR9k/KNou2A6qI5Bn2/J0bzjF13wKPaCJ2GNJHquFVbBKVvb3S8neT/HxRd95HjD1ybzRdsxVo3dUNEfcXLZUn6jLRlJj9g/12ZhaGeL6y8S6pOGMomGPDLMxXgK8x3qCLyYKWm0WqtXOtJIkQ6oJnh8EnC6VsfZz5kaxjiRfmsvmcgu7wF0VnK98h4owo2+5E57mfOmydjU7ghO/e+Q+nzW93Xkg1canfc7RBn93uSdCR0ZyTxgICdlCrOfKcCYfGVaS3drBi0glYi/LX70z8M2DhM2JMSrkY9muQOOIbKTZ55NUwlgSYM3MbYpZwN5enP15bJ6wY43ck7AUY0zZr+fXiFjO9+r4OKYsucmmMs4OPI7V1z8plI/exdjtTTNHfzhG29q0QCqL5zHZ9saF3T32+L7Li9x7l0m0+05/kAyN05Iv87lFd6pPP3vbYimhA15+pSsznFaceCfpQOQePXJ+fNuhIlPoEDV2Rfx0exsJmsuGSxx/72kVLuz6hTy83sRrMbWPjKaVifRQ== X-Forefront-Antispam-Report: CIP:20.160.56.84; CTRY:NL; LANG:en; SCL:1; SRV:; IPV:CAL; SFV:NSPM; H:inpost-eu.tmcas.trendmicro.com; PTR:inpost-eu.tmcas.trendmicro.com; CAT:NONE; SFS:(13230028)(346002)(396003)(376002)(136003)(39850400004)(82310400008)(451199021)(36840700001)(46966006)(44832011)(6512007)(47076005)(36860700001)(6486002)(36756003)(2616005)(40480700001)(6506007)(26005)(1076003)(83380400001)(107886003)(336012)(186003)(70206006)(70586007)(7636003)(54906003)(356005)(7596003)(82740400003)(41300700001)(86362001)(316002)(5660300002)(4326008)(8676002)(8936002)(2906002)(6666004)(34070700002)(478600001)(41533002); DIR:OUT; SFP:1101; X-OriginatorOrg: seco.com X-MS-Exchange-CrossTenant-OriginalArrivalTime: 31 Jul 2023 22:43:24.9152 (UTC) X-MS-Exchange-CrossTenant-Network-Message-Id: c6a241ea-acd9-4db3-025e-08db92178cf9 X-MS-Exchange-CrossTenant-Id: bebe97c3-6438-442e-ade3-ff17aa50e733 X-MS-Exchange-CrossTenant-OriginalAttributedTenantConnectingIp: TenantId=bebe97c3-6438-442e-ade3-ff17aa50e733; Ip=[20.160.56.84]; Helo=[inpost-eu.tmcas.trendmicro.com] X-MS-Exchange-CrossTenant-AuthSource: DB8EUR05FT068.eop-eur05.prod.protection.outlook.com X-MS-Exchange-CrossTenant-AuthAs: Anonymous X-MS-Exchange-CrossTenant-FromEntityHeader: HybridOnPrem X-MS-Exchange-Transport-CrossTenantHeadersStamped: DB3PR03MB10129 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 Implementers of SPL_LOAD_IMAGE_METHOD have to correctly determine what type of image is being loaded and then call the appropriate image load function correctly. This is tricky, because some image load functions expect the whole image to already be loaded (CONFIG_SPL_LOAD_FIT_FULL), some will load the image automatically using spl_load_info.read() (CONFIG_SPL_LOAD_FIT/CONFIG_SPL_LOAD_IMX_CONTAINER), and some just parse the header and expect the caller to do the actual loading afterwards (legacy/raw images). Load methods often only support a subset of the above methods, meaning that not all image types can be used with all load methods. Further, the code to invoke these functions is duplicated between different load functions. To address this problem, this commit introduces a "spl_load" function. It aims to handle image detection and correct invocation of each of the parse/load functions. spl_simple_read is a wrapper around spl_load_info.read with get_aligned_image* functions inlined for size purposes. Additionally, we assume that bl_len is a power of 2 so we can do bitshifts instead of divisions (which is smaller and faster). spl_load_fit_image tries to DMA-align buf, but we don't. This is because there may be conflicting alignment requirements for buf and offset. For example, if the block and DMA-alignments are both 8, and we are passed buf=80 and offset=20, then we cannot adjust buf and offset so they are both aligned. Just focus on ensuring block alignment, and warn if we don't end up with something DMA-aligned. Signed-off-by: Sean Anderson Reviewed-by: Stefan Roese --- Changes in v5: - Load the header in spl_load as well - Don't bother trying to DMA-align the buffer, since we can't really fix it. Changes in v4: - Fix format specifiers in debug prints - Reword/fix some of the doc comments for spl_load Changes in v3: - Fix using ffs instead of fls - Fix using not initializing bl_len when info->filename was NULL Changes in v2: - Use reverse-xmas-tree style for locals in spl_simple_read. This is not complete, since overhead depends on bl_mask. common/spl/spl.c | 83 ++++++++++++++++++++++++++++++++++++++++++++++++ include/spl.h | 27 +++++++++++++++- 2 files changed, 109 insertions(+), 1 deletion(-) diff --git a/common/spl/spl.c b/common/spl/spl.c index b98a9a062a..5374eddc89 100644 --- a/common/spl/spl.c +++ b/common/spl/spl.c @@ -450,6 +450,89 @@ int spl_parse_image_header(struct spl_image_info *spl_image, return 0; } +static int spl_simple_read(struct spl_load_info *info, void *buf, size_t size, + size_t offset) +{ + size_t bl_mask = info->bl_len - 1; + size_t overhead = offset & bl_mask; + size_t bl_shift = fls(bl_mask); + int ret; + + debug("%s: buf=%p size=%lx offset=%lx\n", __func__, buf, (long)size, + (long)offset); + debug("%s: bl_len=%lx bl_mask=%lx bl_shift=%lx\n", __func__, + (long)info->bl_len, (long)bl_mask, (long)bl_shift); + + /* + * Convert offset and size to units of bl_len. In order for the final + * value of buf to be DMA-aligned, both buf and offset must already be + * DMA-aligned. A bounce buffer is required to correct improper + * alignment if min(bl_len, ARCH_DMA_MINALIGN) > 1, so we don't bother. + */ + buf -= overhead; + size = (size + overhead + bl_mask) >> bl_shift; + offset = offset >> bl_shift; + + if (CONFIG_IS_ENABLED(SHOW_ERRORS) && + !IS_ALIGNED((uintptr_t)buf, ARCH_DMA_MINALIGN)) + printf("Warning: loading to misaligned address %p\n", buf); + + debug("info->read(info, %lx, %lx, %p)\n", (long)offset, (long)size, + buf); + ret = info->read(info, offset, size, buf); + return ret == size ? 0 : -EIO; +} + +int spl_load(struct spl_image_info *spl_image, + const struct spl_boot_device *bootdev, struct spl_load_info *info, + size_t size, size_t sector) +{ + struct legacy_img_hdr *header = + spl_get_load_buffer(-sizeof(*header), sizeof(*header)); + size_t offset = sector * info->bl_len; + int ret; + + ret = spl_simple_read(info, header, sizeof(*header), offset); + if (ret) + return ret; + + if (image_get_magic(header) == FDT_MAGIC) { + if (IS_ENABLED(CONFIG_SPL_LOAD_FIT_FULL)) { + void *buf; + + /* + * In order to support verifying images in the FIT, we + * need to load the whole FIT into memory. Try and + * guess how much we need to load by using the total + * size. This will fail for FITs with external data, + * but there's not much we can do about that. + */ + if (!size) + size = roundup(fdt_totalsize(header), 4); + buf = spl_get_load_buffer(0, size); + ret = spl_simple_read(info, buf, size, offset); + if (ret) + return ret; + + return spl_parse_image_header(spl_image, bootdev, buf); + } + + if (IS_ENABLED(CONFIG_SPL_LOAD_FIT)) + return spl_load_simple_fit(spl_image, info, sector, + header); + } + + if (IS_ENABLED(CONFIG_SPL_LOAD_IMX_CONTAINER)) + return spl_load_imx_container(spl_image, info, sector); + + ret = spl_parse_image_header(spl_image, bootdev, header); + if (ret) + return ret; + + return spl_simple_read(info, (void *)spl_image->load_addr, + spl_image->size, offset + spl_image->offset); +} + __weak void __noreturn jump_to_image_no_args(struct spl_image_info *spl_image) { typedef void __noreturn (*image_entry_noargs_t)(void); diff --git a/include/spl.h b/include/spl.h index 658d36481d..47ce16aabf 100644 --- a/include/spl.h +++ b/include/spl.h @@ -267,7 +267,7 @@ struct spl_image_info { * * @dev: Pointer to the device, e.g. struct mmc * * @priv: Private data for the device - * @bl_len: Block length for reading in bytes + * @bl_len: Block length for reading in bytes; must be a power of 2 * @filename: Name of the fit image file. * @read: Function to call to read from the device */ @@ -676,6 +676,31 @@ int spl_blk_load_image(struct spl_image_info *spl_image, struct spl_boot_device *bootdev, enum uclass_id uclass_id, int devnum, int partnum); +/** + * spl_load() - Parse a header and load the image + * @spl_image: Image data which will be filled in by this function + * @bootdev: The device to load from + * @info: Describes how to load additional information from @bootdev. At the + * minimum, read() and bl_len must be populated. + * @size: The size of the image, in bytes, if it is known in advance. Some boot + * devices (such as filesystems) know how big an image is before parsing + * the header. If 0, then the size will be determined from the header. + * @sectors: The offset from the start of @bootdev, in units of @info->bl_len. + * This should have the offset @header was loaded from. It will be + * added to any offsets passed to @info->read(). + * + * This function determines the image type (FIT, legacy, i.MX, raw, etc), calls + * the appropriate parsing function, determines the load address, and the loads + * the image from storage. It is designed to replace ad-hoc image loading which + * may not support all image types (especially when config options are + * involved). + * + * Return: 0 on success, or a negative error on failure + */ +int spl_load(struct spl_image_info *spl_image, + const struct spl_boot_device *bootdev, struct spl_load_info *info, + size_t size, size_t sector); + /** * spl_early_init() - Set up device tree and driver model in SPL if enabled *