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 7E50BC433F5 for ; Mon, 4 Apr 2022 16:27:10 +0000 (UTC) Received: from h2850616.stratoserver.net (localhost [IPv6:::1]) by phobos.denx.de (Postfix) with ESMTP id 3EFF48396B; Mon, 4 Apr 2022 18:27:07 +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="aTk+Jdj4"; dkim-atps=neutral Received: by phobos.denx.de (Postfix, from userid 109) id 4D9EB83988; Mon, 4 Apr 2022 18:27:05 +0200 (CEST) Received: from EUR04-DB3-obe.outbound.protection.outlook.com (mail-db3eur04on0600.outbound.protection.outlook.com [IPv6:2a01:111:f400:fe0c::600]) (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 BB69183639 for ; Mon, 4 Apr 2022 18:26:59 +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=KriXqemDjycCavSsujSMDNCVIAeZgmaENzsh1+VnNM8uQupgwBispVgHXH5E3BU98QVZYGRNpZ4rHHM3HvPNne3+lmBMRFsZGf++EluorOQl3+3gXv1/EgeRi0+G49ONcmuI7x5m4NR12D9yZn4byhXi8koz03PqO16mpXGaD4lSPQJeRAWJTPOxShPRT4rXLPSgs2SuWQqZJVb6QovTb0o9yj2mn5dZqfT2+pGqdGtbAkQDD7Ci9UZt2KRqEg6qIWGY/lmGC6y/6mZQ4Eo+tRSZNDt+lpGz4y8dFVRyrY0XzwLBmh0757CzI8vGRCUYCu2B4/Huie+UexdAPZy+cg== 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=iCw+mqizyWQDZ5JicPKPAPx/t8F8BWTT/qkcR6MZVTY=; b=eNGpVXynhQsmqWh4pHiSIGYLNkaeSJE10OfvIziT202SbofJXN49fniSnFoCIwfbUrq/hH6uNK5co0OjWShbIfFsKb33ZSE6NVfM4+3pER0vPxfmVc6bA6LJFiLJ5ZraWSHP4cJVOXsV2GOcY0u0wXTLTq+Sn93WqhTGc67Jo5ywjOyZWeK76xZuBl8MzvsZAaV3pOMjpRlHJgh1201R/3viZnsCFyND7eRb+hdCHxc0BBn8tdS1cNWLvTsksl1vhztx3/nAU3+EKlj/xXJnxGF09eyAM9Bv2bYEsIMcPGBN84+UVNld3HmzVkImVcxdsOqICvsbuC2Pf0ptpoaoAA== 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=iCw+mqizyWQDZ5JicPKPAPx/t8F8BWTT/qkcR6MZVTY=; b=aTk+Jdj4gGZUjfvtqab81zISJexBKGtsTqnNAe8KmA3Lwy+dQwYk4NohVxEgI0rDKUBZzjRLcsnhypg/TVh3jqP0F8kbj2npDfgoS8I9zIvMSv6f2IldMToo/6QB7mF/10jBPceS6WFOOF2ZUhVW66OZ5yOX/ndsapQ/KJtamqq21wVgnhSj1vdvWrEOQxjBtITKC+H2PWG/qft7/RbJMuxsUs+alQRKSUwur+X/BeyNrIz7sZaX0INxEVPjuVcC8hRyQRuaifzW7W6WIGXZdoeyXAb6CF3smKSNfIYeU4OOfrUJef7pLcPvKFmEENUOaZvdz3favl7VM2uxw6YHHA== 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 AM6PR03MB5800.eurprd03.prod.outlook.com (2603:10a6:20b:e5::23) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id 15.20.5123.31; Mon, 4 Apr 2022 16:26:57 +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.030; Mon, 4 Apr 2022 16:26:57 +0000 Subject: Re: The contradictory nature of spl_nand_fit_read To: Dario Binacchi , Michael Nazzareno Trimarchi Cc: U-Boot Mailing List , Tim Harvey , Tom Rini , Lokesh Vutla References: <96acebf8-5eda-c8fc-82d2-049def9fdd32@seco.com> <979408354.1065083.1649001394901@mail1.libero.it> From: Sean Anderson Message-ID: Date: Mon, 4 Apr 2022 12:26:51 -0400 User-Agent: Mozilla/5.0 (X11; Linux x86_64; rv:68.0) Gecko/20100101 Thunderbird/68.10.0 In-Reply-To: <979408354.1065083.1649001394901@mail1.libero.it> Content-Type: text/plain; charset=utf-8 Content-Language: en-US Content-Transfer-Encoding: 7bit X-ClientProxiedBy: MN2PR16CA0023.namprd16.prod.outlook.com (2603:10b6:208:134::36) 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: 7e383f60-789e-4286-16de-08da1657ef78 X-MS-TrafficTypeDiagnostic: AM6PR03MB5800: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: XWMzLQd2GxaZF1iTvolQmThusjUdtkz0m+wkUE/J5N4P4ENO/uJBwrui/jzcY+rtx6AYMr2jG9872vJFsBD/jVvh2f0zP6u9bZi9kKPkZWZAz6kmLzWixyj8+AyK/yWlvdJnpjFqQTKEqYrJ4OjXMNCo+klhl/DXQmOCZBNYtBLKMO84Hqq1s+4hZBKx/xll7C6kouJdWWZgrPmQAIFfNh5P2L0mpjw6YhESiVWM3Nt9cyQNmrmwQ2+iQ2fcDvGiRKcB4sQRVojoSYgv6ACurVmUOr6FXVaMHs+oeJwRWeG0xfjmsveUAhwLybHYeYgpYZMjX7xSo6mb7clVMCjA8jxF4kAiM05LJBECO9ACRMh15AtLkbhsKNQhDwjZd2cAupeKIgIeWTZpAzZqJ4PPpCBhLXWt9r0ukReRhTcDetlfofM4fTkvB5XbUrdxq63L5J5A4YUzjJCpXmi7DlrzdmqoZbPuWl1QguMt9zB4s5rmYZ8++jI6NH3B8kDDp5zch/9AbG7/B4m4Zk9fw7w42QsczUeto/IwiYMqnXnsasBXrmMfS5ORA+z6vRqoeCvl8iurhVD96v2kVcw8ZVj7ngsznfD94A2ZOKgc7RMcD/DzXCPtHSQPkWRL+TZSCsRqCA6rJsDA/CtFSc5Xw7nu5FIUYvEVXOKM9RVU5biRK7UoL3sW+ESrPAST9z3IbQkdhP0CkLX/lBR2Z9l+MerCajegdlOWrmYPBosNGMiDr02e03vc+althS1ssXJD7oZd+kyEEraA4oZEPXop8CQ4rw== 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)(66946007)(66476007)(66556008)(316002)(26005)(31696002)(8676002)(4326008)(110136005)(86362001)(2616005)(6666004)(54906003)(6512007)(2906002)(83380400001)(38350700002)(38100700002)(5660300002)(31686004)(8936002)(52116002)(53546011)(44832011)(6506007)(36756003)(186003)(508600001)(6486002)(45980500001)(43740500002); DIR:OUT; SFP:1101; X-MS-Exchange-AntiSpam-MessageData-ChunkCount: 1 X-MS-Exchange-AntiSpam-MessageData-0: =?utf-8?B?UmlTNm1DaWJaeHRGK3RKTlhJZWJ5L2hpS3dmQjhubk5mV2tkNFVjOWQ0R3Bt?= =?utf-8?B?VGJFYmlHOVlDRVF3NSthWHhTRzZJaFNmL0JGMGJhZUZVLzB3d3EyWktKeUhp?= =?utf-8?B?S0R0K3ZxQUp5N1JNdmU0WlM0NGdsb1FMemxrS3JkdWhHeGxldEhSWkoxN1lT?= =?utf-8?B?TTQ2QzZ1UHFXeW8rL1RPR25aRklyTW5CT1VmY3E4eFRwWmtHbUpQVzg1bXhI?= =?utf-8?B?d0RLN3VOWnFGMTlZeTQxdG51QzkzRDJGTjJpM1IwVllOQnhYTE5jR1pTcldI?= =?utf-8?B?TUwwVlA3cDVyVzV4Y1FiWGdKZ0E2aVBadkpFNERZNlRZVlFGeUpaMUM0dHI4?= =?utf-8?B?cFZwNTJkTzZYVVBSM1BDeEdTQXJWT04wK04yOCtjbXArM2RiTnlyVWJyNVBW?= =?utf-8?B?d21lQ2cwRjFOV2RZRHlIMU9wdDFBTVJXSTVKYXRaZkN4UEJZZVQzbXl5dnly?= =?utf-8?B?dFNXSWtPcXA2dHZ0OG5BOWw2Rjdmc3FQZU1HaDJFTHpwV2hyUkpPdXZSeU5E?= =?utf-8?B?aHI4Tkd5RkpnYUVBWW5EekJ1Sy9QcUI3VXJ5MU54QUZZdTM2blBVUUdhVFps?= =?utf-8?B?S0xuNW1LSE43ejdnejM5OS9KQTNxRmJjNEdPZmcyeC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ange-CrossTenant-AuthSource: DB7PR03MB4972.eurprd03.prod.outlook.com X-MS-Exchange-CrossTenant-AuthAs: Internal X-MS-Exchange-CrossTenant-OriginalArrivalTime: 04 Apr 2022 16:26:57.3186 (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: tTyqa1+TiuxGNj8OGwuLGN/443osNQZ2tFy8XACZvQAP/bVepcI4YpEp3BA2bDXi7ASAXb5o/Wcy/wzQa4OUsA== X-MS-Exchange-Transport-CrossTenantHeadersStamped: AM6PR03MB5800 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/3/22 11:56 AM, Dario Binacchi wrote: > Hi Sean, > >> Il 01/04/2022 23:43 Michael Nazzareno Trimarchi ha scritto: >> >> >> Hi Sean >> >> On Fri, Apr 1, 2022 at 8:53 PM Sean Anderson wrote: >> > >> > >> > >> > 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); >> >> The spl_nand_fit_read was called from info->read that was in units of sectors >> count = info->read(info, sector, sectors, fit); >> debug("fit read sector %lx, sectors=%d, dst=%p, count=%lu, size=0x%lx\n", >> sector, sectors, fit, count, size); >> >> >> load.dev = NULL; >> load.priv = &offset; >> load.filename = NULL; >> load.bl_len = 1; Ah, here is the problem. If bl_len is always 1, then there is no problem going back and forth between units. But aa0032f672 ("spl: fit: nand: allow for non-page-aligned elements") means that bl_len is not always 1, so now the conversion matters. So I suppose my question is now for Tim: how did you test your patch? >> load.read = spl_nand_fit_read; >> >> static ulong spl_nand_fit_read(struct spl_load_info *load, ulong offs, >> ulong size, void *dst) >> { >> ulong sector; >> int ret; >> >> sector = *(int *)load->priv; >> offs = sector + nand_spl_adjust_offset(sector, offs - sector); This line is ONLY ok if bl_len = 1 >> ret = nand_spl_load_image(offs, size, dst); >> if (!ret) >> return size; >> else >> return 0; >> } >> >> Sorry it's a bit of time and I think that we are discussing about this >> commit at the time was introduced >> Michael >> >> > >> > 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? > > The commit 9f6a14c47f only added the code strictly necessary to skip a bad block > that was in that memory area. I heavily tested it on a custom board that presented > this problem and that without it it would not have been usable. The patch itself > was also tested on SOCs that did not have bad block, therefore backward compatible. > I think that your doubts arise from the patches following mine and that, however, > I have not had the opportunity to test. Have you tested v2021.07 or later? --Sean >> > > >> > >> 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 >> > >