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 12506C433F5 for ; Fri, 1 Apr 2022 18:46:45 +0000 (UTC) Received: from h2850616.stratoserver.net (localhost [IPv6:::1]) by phobos.denx.de (Postfix) with ESMTP id 24B62842FB; Fri, 1 Apr 2022 20:46:44 +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="bC8YcKAd"; dkim-atps=neutral Received: by phobos.denx.de (Postfix, from userid 109) id CDD5F842F3; Fri, 1 Apr 2022 20:46:42 +0200 (CEST) Received: from EUR01-VE1-obe.outbound.protection.outlook.com (mail-ve1eur01on062d.outbound.protection.outlook.com [IPv6:2a01:111:f400:fe1f::62d]) (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 E6CA3842FB for ; Fri, 1 Apr 2022 20:46:25 +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=kx0ZsJVLUjpHw1IGQaRfq1oUUytZphQaailDebSiWBxy3VFSWaNEhtQ6Vio4o+7AIQXOrlnCtVQCZE0Sxdxe2PfJlJNpqR7wCD4FcMF7LNjVfBEpX/cwTUICdDPTqBRBpvWRWx7S3ShkqoTeUV2caFBi3CNHvjrtZLTNQGvHbZToipgkTqaP43N1RM0mxthjTGumdXy1wlCSJDXj+pfdDYTAv+aIm464VkeTTBl84QmujM3hIBZ7QusJytYVxJHKaRgSkUeIy9HwRLoXYRsaN37WQblUxvZ99Hp9KtEsk7hUJV9pGVUE1+r69P5F97o8SKBkkdrr7RyZ+VqAn0nzzw== 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=bBv8ECbgWoTU6xV7g1cXUjQSf0adFmcWSmDPCVRZQ0U=; b=X3/fIR92Ypm7AQMVpPs0SVl6zvWw5xNktWYKkSBvdrPbiK1Al/umXmVaMq1k+JMU/5E72a278vuK4pqjLYyB5XJLMlG4hLp7eCLlFLBTNr00SAtbzy+GTjzPGOT3DLpQyuglrmbA0oiAxWHC2bRdusUCGcUFimAECkUfS3vwG9+KXPpscJ9qQJTgMXxz2LSft2Be5/KkQALZAmLoF1aetCjuJ/zoZYwIXx7Vcory79DLwOU14rxMDjId8HYBRR6n5v+aH58dNk7HRS7FDfKGR8iFC3JV6+cqw+ekFXvo5XWQ00bzgyatxVhINrr/1akcO78v1X2w5Y5xMnsSK5np4g== 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=bBv8ECbgWoTU6xV7g1cXUjQSf0adFmcWSmDPCVRZQ0U=; b=bC8YcKAdU5OHsb7w2ay6FgtXEONYzoVC6za/zzujTfctlhwfUNK3Aj5j8sehsU1W9nr+v/SKYIy4d+6GLZuXjvQOIyqldUH3gjwMSuJpGsI8PonU/Q91aKXV4wUUjGo95q0wStcRa2yuI7nL5jVU21OupggOLijlphtWaanOi5toygRO6xzA8P//s/sLUlOVF3a3NEpmatk3wfxrRphNlVnoxLSzOVw/0Bs3jMZCVyjIgAbqGDaGpIJdb09fZMWlVProUjUwZzA9j6vWbYzP1urcRX1E8e3joYxRF5iNqb7DPTegg2wXQGoK2LqORGLglbuBfRzVXoGbOpUivETbOw== 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:46:24 +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:46:24 +0000 From: Sean Anderson To: U-Boot Mailing List , Michael Trimarchi , Dario Binacchi , Michael Trimarchi , Tim Harvey , Tom Rini , Lokesh Vutla Subject: The contradictory nature of spl_nand_fit_read Message-ID: Date: Fri, 1 Apr 2022 14:46:20 -0400 User-Agent: Mozilla/5.0 (X11; Linux x86_64; rv:68.0) Gecko/20100101 Thunderbird/68.10.0 Content-Type: text/plain; charset=utf-8 Content-Language: en-US Content-Transfer-Encoding: 7bit X-ClientProxiedBy: BLAPR03CA0046.namprd03.prod.outlook.com (2603:10b6:208:32d::21) 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: c6affc66-fc39-429e-716d-08da140febc7 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: v3ExoEEwd04bndZ90eIHyooYp/YNiY4wOd88mMtiS07Te0XJGWs5VYWPiqy37ddu9KfvJpIwvZhZ1TjxanNtMo7Zw2AVyLgBktduEHtN9k2LrGu0lTfYAzt1XFvTCZUjD+goCJjJc5bMPbZ2kNmqSdsqs58HPvziKxYZ0AitMLBldMIFqinU7MrhS3KwvZxaPIhSQeCZuuvVf8UrrS7dRoSfqmX9TqEdg4uI/PZmjajsJbODM6ckwzeBPki60yAIf7zlIJ0TIzhIpJhQiGRFWKCdN2Jirp8Zkyr0jg70QXQ3gfY/5F91EtyOU9sfVYQ2l9GAae++sK0bGWJ2hOtidrQU8XyEmLCds0Mg4dg0scLaCNZJr5cMsLy+hMAXZQlS7WiS6fIDbV3MDCADutydleFUlG+KKhKM3/D5eKzsMOiJWSD69YA8NEUV65I40G29VnTL0h3oiPjthU7tbZtzc2CN6ifJgGP+mvwSE7juebA9IuhveL1cVTfth/Wpx+YaGIOtXlFue7jzkAbxVFHMo07KpIKQAjcGy2XooIZmkg2kW48Bb3/+JnXnDcdWtvMRkZ6Cg5TUcD2tZYA7XfJulR0MMRtqcISWM5Y04ocXe1CpKmOEGjljqCfH+1AqKSVs04rcvEUxZh/jlqQzYzvTJTmzaL9q1uB+JN2H20l900k0h4nuDj1q3WCIwKW9E7Q6nIvqjlPrdrD8BaDegtP6YCSVoNaWL0hBFfglGltIq3Qq56KshFoVmFyBETO7Uvlrj9sCrdgQFogVxXeiGCvweg== 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)(6486002)(186003)(26005)(36756003)(66556008)(66946007)(66476007)(2906002)(8676002)(2616005)(83380400001)(316002)(6512007)(86362001)(6666004)(6506007)(31696002)(31686004)(38100700002)(52116002)(38350700002)(110136005)(5660300002)(44832011)(8936002)(508600001)(43740500002)(45980500001); DIR:OUT; SFP:1101; X-MS-Exchange-AntiSpam-MessageData-ChunkCount: 1 X-MS-Exchange-AntiSpam-MessageData-0: =?utf-8?B?RXo1dDRKSVBkRkFMci9CTlp2S1Z5L3FKbjhQNTM0SVJ4U1d3ODhNL1ZqeDl0?= =?utf-8?B?U1JybHc3dFd1cEF5SWpEenlTVnQ0bkJ0OVRoNGE2RmlXSXVNaEs4ZFpnVEhr?= =?utf-8?B?K2Jna0p3Y1Jiak5Fb2gxMVJuRlFJQldBS3Y0dVdYSld5ZEp0andJb250dGxE?= =?utf-8?B?S2JhcjdsRGNESFJaNUNSbFdkRTRtVVNUa1Y4VE5UZjJIWVVrMXdiNmErY3RL?= =?utf-8?B?UlhTdmdDNElLSlFpUnpEWXIySnd5OHgxZ210bmtES3kyc1AvbGZ0dWltQ3pQ?= =?utf-8?B?bkRKSzVhMFdZcEFuRWxwd1ZXZHBXQXdKYTVzcDVGSzNCaFFJYit2T3dhR0s0?= =?utf-8?B?bm9rSHFOZEFmY3gvbjlnQlM2dUlyVmVGZy9nb1pTb2tzbVFINTNFZFFrY04x?= =?utf-8?B?aGRBMDVTdEV5ek9XbTk0ZVdxTzZBeU91NkM4QmdVc2xKbkx0S2FrSWNuZ0g4?= =?utf-8?B?cVh2YVU1MG11d3NxcXBpWXZNUkRtaURDTlZxTUpGQVZ1NjBmU0lZc1Z0YnZl?= =?utf-8?B?VTZWSm84TndFN2gvdHVzTHhjNGxBdHdPOGFWNDJrc05iN3NOUUFLVG5JR3Bl?= =?utf-8?B?YXFoZm1wdEVaM04zUmJSNy83anhCSzFrOG9EaDQ2YmdKSG5majZlc2pTaHc1?= =?utf-8?B?UmlIaW1xalJDTGJaUFpSaUNOZGZFc2RmOThja1ZycmtFZ1JidGxhdU0wSGxJ?= =?utf-8?B?KzgzK2lnanUyY09nWmh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U3Y1ODNrb3RrN29rbTZJMndUaW5yQUtsWW1KRmh1MzNUT09sWTNYb29h?= =?utf-8?B?NExkcktXeEt2Q0JKR1J3cWp5emxjNnA1Z2MvTlU1N1ptTVE1YWlqMitXSHR6?= =?utf-8?B?WVZwYmdzd2hVYzBpSUNjQTROUU5VYW5yZWVnYWRUd0NtY1puUWNqZlNaQzEr?= =?utf-8?B?N1Jhbnd1RnNPeU0zQTJSRm80bVc2Uml0STcxS2hTdEFGdGxZemVYajc5TnRa?= =?utf-8?B?ZHVZdzQxQnFTM3RuaWZmZldXME1TYk5LdjBxbUJ1SmVTbmtsK09FVEoyb1BR?= =?utf-8?B?VWJMcGdMdjk1R1RZcWg1ME03Rk81TmZMeHRVeGxrOFQ4R3FrYTlyM015amtK?= =?utf-8?B?am56YUtndURGdUphL2d2QWVBcm1zRXAxTk5nZnNNR2lZcjZFQ1V2VTJubjNo?= =?utf-8?B?MHgyUVc5YWw1MWdrUThtcFRJbDNnZ1lYaG8xdzBZZHV3bkFqR2NRR0NaRnFz?= =?utf-8?B?ejFicEJDcmk2Qks4ME92WGs2MXAyWUZjZWFZeDI4WHlsdk1WYWQwMG5OU28z?= =?utf-8?B?cithVzVSMVprU3BZbWt6bkNGYWJWbTd4b2Z4YXBoOEplS1N1ckkxbWN3dVl5?= =?utf-8?B?Wkw5MG9hVG13c085VE5aR0lBVXZyMjYzRmI4VHo4b0ZtdDVUbXhTdDRSKzQ1?= =?utf-8?B?TDNFZEI1WGo1THpXejMrUUt0d2t1S1FkWmt0UmRLRjFvS0VRei8vdVU3YXRq?= =?utf-8?Q?cFSGZ3vRxLCGMG2s=3D?= X-OriginatorOrg: seco.com X-MS-Exchange-CrossTenant-Network-Message-Id: c6affc66-fc39-429e-716d-08da140febc7 X-MS-Exchange-CrossTenant-AuthSource: DB7PR03MB4972.eurprd03.prod.outlook.com X-MS-Exchange-CrossTenant-AuthAs: Internal X-MS-Exchange-CrossTenant-OriginalArrivalTime: 01 Apr 2022 18:46:24.0221 (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: rxahFjCeApIqvfA4L8eGXvtbNMzqAGBvVTfN4EoXYRAWDsj01Rhhpsv34/T6RRw9mBDRKAde6Ug4Eq7NvxTJ1w== 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 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); 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 > #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