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 42506C433EF for ; Fri, 1 Apr 2022 18:54:30 +0000 (UTC) Received: from h2850616.stratoserver.net (localhost [IPv6:::1]) by phobos.denx.de (Postfix) with ESMTP id D0A69842F1; Fri, 1 Apr 2022 20:54: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=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="b6eBQXOB"; dkim-atps=neutral Received: by phobos.denx.de (Postfix, from userid 109) id EECDB842F1; Fri, 1 Apr 2022 20:54:25 +0200 (CEST) Received: from EUR04-DB3-obe.outbound.protection.outlook.com (mail-db3eur04on0624.outbound.protection.outlook.com [IPv6:2a01:111:f400:fe0c::624]) (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 B18E1842E9 for ; Fri, 1 Apr 2022 20:53:56 +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 ARC-Seal: i=1; a=rsa-sha256; s=arcselector9901; d=microsoft.com; cv=none; b=mQ2YggHalUx24mWFFdPygr/CWBlCUr8QVVlR8+KokM/bDBmsNiPBSJvpGBZK9VUPkWCF6D8W2UOgvW95iWQsPApMuj2H7Ju7BhkD/iZbwCe4ovmvkyGWHhNd+/AIGCKY7InI+GLihtoeChbd1HcBsMqpKlXf1jnpB9evLPR9duK0ASkwKagvHr7RWLMxywArMtL1eKZePFfNhWGuiADy34TlAc5Dy/8V35dKI03jleSjm4Uh1FulJ9lBVZB3WPtHO6f4XYeE1/9d+hGfTDpmlq4AoWrdWiC44DQ6F0gyi2u6QxYrjt2UKI6E9v5WgsGBzGNHoB1W4sh/cHAvFDhrkQ== 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=JtLxjaoEBPeTztS/F3UYW90P23bKxNFzz+kXfn9MGkc=; b=BvdFjkwrHxgfqNAZ3rQi/bhMiVHwNWA9GS963pJO/z+JvjCcM+7Mtj+b8l0y6EBpbscP7CYT8DWiklfjaTdWBu6x3lnEuUpZm2RSoDgREAVgAeqNJnfMfI6aguvP3qaK8HEeaWDoEJSeVCuKDqZtVZjAzi3rr9QJsZp4RAdBEhI/fWvkEYH8x4DDzDDUNMOwVl1IQdiIr+ee2im77FsaVfBoLqSBoOK/d75d235E+xogyLRtA1Mmms1wqIgyW0CzkuZG3fpL8cQbsPlbRznf6wzJziyGVRQwUQmgfHEuIwktFTJ5w9n6Ipd5LMbgigj7ch6OKEozqfHrmpOMIl4IQg== 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=JtLxjaoEBPeTztS/F3UYW90P23bKxNFzz+kXfn9MGkc=; b=b6eBQXOBfqEqCzY8eTLSX56uQplqEc/0Sv2lWXuXBn/Y1Yer+x93th3ei5V1or9RkSNaFtE+9sdkx7d8BR0M4zpABq505J0NSuW0SzDFsgh2jSWMPemdhNqXqfWQZQCNUPWkdR5UMfD0PHlo15jSiUN0ak8k9akqvdyOZjNE74rVx4+yKK6Of9HsaORvZQ/iw8eqJZ/GCrVSZgxdCPclLqDZinw0nX6Lf+TPxEkWB7FI8LJFdgAZc9pMramQQxkEIKziA+7So9SXkDn/8P8NEK0pCTpz9LsI/0uRJPnvOjyF8mOLtIGatEDUQD8UlMY0vJcAuAz8KfsYjGjkCMHgHA== Authentication-Results: dkim=none (message not signed) header.d=none;dmarc=none action=none header.from=seco.com; Received: from DB7PR03MB4972.eurprd03.prod.outlook.com (2603:10a6:10:7d::22) by AM0PR03MB3873.eurprd03.prod.outlook.com (2603:10a6:208:6f::14) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id 15.20.5123.25; Fri, 1 Apr 2022 18:53:54 +0000 Received: from DB7PR03MB4972.eurprd03.prod.outlook.com ([fe80::356c:789f:d20f:92bc]) by DB7PR03MB4972.eurprd03.prod.outlook.com ([fe80::356c:789f:d20f:92bc%7]) with mapi id 15.20.5123.025; Fri, 1 Apr 2022 18:53:54 +0000 Subject: Re: The contradictory nature of spl_nand_fit_read From: Sean Anderson To: U-Boot Mailing List , Michael Trimarchi , Dario Binacchi , Michael Trimarchi , Tim Harvey , Tom Rini , Lokesh Vutla References: Message-ID: <96acebf8-5eda-c8fc-82d2-049def9fdd32@seco.com> Date: Fri, 1 Apr 2022 14:53:50 -0400 User-Agent: Mozilla/5.0 (X11; Linux x86_64; rv:68.0) Gecko/20100101 Thunderbird/68.10.0 In-Reply-To: Content-Type: text/plain; charset=utf-8 Content-Language: en-US Content-Transfer-Encoding: 7bit X-ClientProxiedBy: MN2PR08CA0015.namprd08.prod.outlook.com (2603:10b6:208:239::20) To DB7PR03MB4972.eurprd03.prod.outlook.com (2603:10a6:10:7d::22) MIME-Version: 1.0 X-MS-PublicTrafficType: Email X-MS-Office365-Filtering-Correlation-Id: cccfcf1b-6359-4132-b48d-08da1410f861 X-MS-TrafficTypeDiagnostic: AM0PR03MB3873:EE_ X-Microsoft-Antispam-PRVS: X-MS-Exchange-SenderADCheck: 1 X-MS-Exchange-AntiSpam-Relay: 0 X-Microsoft-Antispam: BCL:0; X-Microsoft-Antispam-Message-Info: NiANYICtn/9NSk2JYFYvKIDv18B3TZblpsI+5QIBO6N1bddvM20KQWjeGSxOTXLIfXuSaE0WMS5/Pn7K9kngVJpr4YHAlntd/9GcTqg3aHRO/BUwgcm/hV+DprXgAyGdEZMs3qpRjjvsBGgK0lvLeLU7t7KGjRKkyR6YSoDhQbWN5xHAUf5DRoTjXLUkNY1S58SKOBB3dnEM0sTBbVySbFZp7xotp3FtxLB10g7fJx+p7U/dHJRib1hIbGOlLGF8PSYi5HAbnU6N8uGUOQcnWyleAi3aGnXEpPbcF5suVIArgd8evy3t2C+LSiygDoXTz+O2JCdJ/YbeqhRULNdQ0Llhod1mEDe5oXwHbxZFzrgqYT7vucEDgi2wC/hwmOcYp82Eho6DQ46oD+TGrT8/uV8fxh3iF8HG5Wzejy91E+QnDbl1ph7I7koCWX4lCPAQZmuclBr1qxVkPgQbpwyPqIFhM2h7LCyRRvGPK8DD4zaPDSwYyFNl0e1DTI6sAtwKdSnIChk0fOU8S16qS9/DTf1y9yIrnrQ5MbfBuAFeJKRb4M1CjUPjusydypudZJwhij/SMOzgI8An2lZUEbjwqGe7QpXmHf7BDAvsmXPqZVuyU7iBol1R/exP8qCKSfp3pyrAKiOnwOugDfXJugOsoVhaNiuU+uycY2mRNUo8KqRrOJgblYZ7V4bSfAR86FMMHZo1LMgAMB8xo9xsq9mX5zr5rgXDDwWQ/EeONQi1P6JuPN7Ucm6mO2jZydSMMaiWv4/ULZ9TeyYBkOjHBu+T9A== X-Forefront-Antispam-Report: CIP:255.255.255.255; CTRY:; LANG:en; SCL:1; SRV:; IPV:NLI; SFV:NSPM; H:DB7PR03MB4972.eurprd03.prod.outlook.com; PTR:; CAT:NONE; SFS:(13230001)(366004)(31686004)(38100700002)(52116002)(31696002)(6512007)(86362001)(6666004)(6506007)(8936002)(44832011)(508600001)(38350700002)(53546011)(5660300002)(110136005)(66556008)(66946007)(66476007)(2906002)(6486002)(36756003)(186003)(26005)(2616005)(83380400001)(316002)(8676002)(45980500001)(43740500002); DIR:OUT; SFP:1101; X-MS-Exchange-AntiSpam-MessageData-ChunkCount: 1 X-MS-Exchange-AntiSpam-MessageData-0: =?utf-8?B?QkZMZVNqQzlTd1ZDSGE0K3ZlZmNSYko3QW11THhrUXVOVEh5SjdGR3B4OTQv?= =?utf-8?B?azF0VW1QWFpSV09OT2E3dm5oWjcyNHFEczJaN05vV3RqSjRuUGpKbVdzVnlV?= =?utf-8?B?RFFwNWpQNDhwOWgwL3AzL1FhVW9KR0E3UDZSNEVHc0oySzhuT21zVE5mZUVF?= =?utf-8?B?YUc5SjNqL0hNam91dU4zNXRjYW16WHhPZ2lwL2JteGl5WTdUVmZZMDU1azZM?= =?utf-8?B?SVg3RmdiNittL3dSUzZybFc0TFl6RTRlUFU4Vm9JbkMwdFI4ekJPaElrNlhC?= =?utf-8?B?dDNHWVl1RjRtRzhFUHdrTk9JbjJCcm5HS1hpeDQ5b2JZdlVPSlpYMEhIbUtO?= =?utf-8?B?WnlGU2krSW5yRDBuUzU1RDgyK1B5R2JPQ0dWVmNidWhXekxPZHdMMkpWbmJq?= =?utf-8?B?QlZEZFJyRGUzV3dxQ1hGNkZRMEVqSTkrQ2QxaXZRc1gxZ1RCWHNTbzV1Q2F6?= =?utf-8?B?emVsbklRYURCbmRlZFVCQXE4U1pJZnRreHE0S251MlBYemQ0bkUzYzNqdXFU?= =?utf-8?B?VDdOVFdKb3hFR2tKVFEwWmE2WnlIMDNKV0k4WWg5TXFCdjZOMnN3YzAzcllu?= =?utf-8?B?M0psZkRwMTFGM2pxdmZvWGpSZkw4c2oyMElJYy96d3VZbDdoaG92RzFaY21R?= =?utf-8?B?TElmSHlqQ3FSbWs4MDZmaitKVUh6dHZ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MB4972.eurprd03.prod.outlook.com X-MS-Exchange-CrossTenant-AuthAs: Internal X-MS-Exchange-CrossTenant-OriginalArrivalTime: 01 Apr 2022 18:53:54.6312 (UTC) X-MS-Exchange-CrossTenant-FromEntityHeader: Hosted X-MS-Exchange-CrossTenant-Id: bebe97c3-6438-442e-ade3-ff17aa50e733 X-MS-Exchange-CrossTenant-MailboxType: HOSTED X-MS-Exchange-CrossTenant-UserPrincipalName: B7AgTSHbaiec9Ei2nN0fXoPcPneCa3qpCqtBLBZwTPIJtCtOJxs78B06UsE9+OuqucX/VtaBraqANE2Q2VEzHw== X-MS-Exchange-Transport-CrossTenantHeadersStamped: AM0PR03MB3873 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.5 at phobos.denx.de X-Virus-Status: Clean On 4/1/22 2:46 PM, Sean Anderson wrote: > Hi all, > > I don't understand how spl_nand_fit_read is supposed to work. This > function has been seemingly hacked together by multiple people over the > years, and it (presumably) (somehow) works despite mass confusion of > units. I tried to do some refactoring, but the contradictions make it > very difficult to determine what is a bug and what is intentional. > > Lets start with the basics. spl_nand_fit_read is used to implement > spl_load_info.load. I've reproduced the documentation for this function > below: > >> read() - Read from device >> >> @load: Information about the load state >> @sector: Sector number to read from (each @load->bl_len bytes) >> @count: Number of sectors to read >> @buf: Buffer to read into >> @return number of sectors read, 0 on error > > In particular, both @sector and @count are in units of load.bl_len. In > addition, the return value should be @count on success. > >> static ulong spl_nand_fit_read(struct spl_load_info *load, ulong offs, >> ulong size, void *dst) >> { >> int err; >> #ifdef CONFIG_SYS_NAND_BLOCK_SIZE > > The following logic was introduced in commit 9f6a14c47f ("spl: fit: > nand: fix fit loading in case of bad blocks"). However, at the time it > was not guarded by the above ifdef. nand_spl_adjust_offset is not > defined for all nand drivers. It is defined for at least mxs, am335x, > atmel, and simple. Additionally, some drivers (such as rockchip) > implement bad block skipping in nand_spl_load_image. > > It's not at all clear to me that there is common config which determines > whether nand_spl_adjust_offset is implemented. Nevertheless, in commit > aa0032f672 ("spl: fit: nand: allow for non-page-aligned elements"), the > above directive selects different code if CONFIG_SYS_NAND_BLOCK_SIZE is > defined. > >> ulong sector; >> >> sector = *(int *)load->priv; > > This is the offset passed to nand_spl_load_image. This > offset is in units of bytes, and is aligned to the block size. However, > it is *not* set if CONFIG_SPL_LOAD_IMX_CONTAINER is enabled. Oops. > >> offs = sector + nand_spl_adjust_offset(sector, offs - sector); I forgot to mention this, but this also adds sector to offs, but offs already includes the sector: > return spl_load_simple_fit(spl_image, &load, offset / bl_len, header); Which means that we add in sector twice (but with different units!) > The documentation for this this function is as follows > >> nand_spl_adjust_offset - Adjust offset from a starting sector >> @sector: Address of the sector >> @offs: Offset starting from @sector >> >> If one or more bad blocks are in the address space between @sector >> and @sector + @offs, @offs is increased by the NAND block size for >> each bad block found. > > In particular, note that offs is in units of bytes. However, we know > from above that at this point in the function, offs is in units of > bl_len. Oops. > >> #else >> offs *= load->bl_len; >> size *= load->bl_len; > > This code path adjusts both offs and size to be in units of *units of bytes >> #endif >> err = nand_spl_load_image(offs, size, dst); > > So by the time we get here, one code path is in units of bytes, and the > other is in units of bl_len. nand_spl_load_image seems to expect bytes > (though there is no documentation, so I can't be sure). Which of course > begs the question: how did the code introduced in 9f6a14c47f ever work? > >> if (err) >> return 0; >> >> return size / load->bl_len; > > And of course, this should only be done if size is in units of bytes. > >> } > > I have some patches for what I think are bugs, but the logic of this > function is so confused that I am unable to determine if they actually > are bugs. I would greatly appreciate if some of the authors of the > mentioned commits could comment on their intent, and what they thought > the units of offs and size were. > > --Sean >