From mboxrd@z Thu Jan 1 00:00:00 1970 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 A2166C433EF for ; Fri, 6 May 2022 14:41:26 +0000 (UTC) Received: from h2850616.stratoserver.net (localhost [IPv6:::1]) by phobos.denx.de (Postfix) with ESMTP id 4BF1E84135; Fri, 6 May 2022 16:41:24 +0200 (CEST) Authentication-Results: phobos.denx.de; dmarc=pass (p=none dis=none) header.from=nxp.com 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=nxp.com header.i=@nxp.com header.b="lyWmbz75"; dkim-atps=neutral Received: by phobos.denx.de (Postfix, from userid 109) id 7FF208405A; Fri, 6 May 2022 16:41:22 +0200 (CEST) Received: from EUR05-DB8-obe.outbound.protection.outlook.com (mail-db8eur05on2060e.outbound.protection.outlook.com [IPv6:2a01:111:f400:7e1a::60e]) (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 7E53E84166 for ; Fri, 6 May 2022 16:41:18 +0200 (CEST) Authentication-Results: phobos.denx.de; dmarc=pass (p=none dis=none) header.from=nxp.com Authentication-Results: phobos.denx.de; spf=pass smtp.mailfrom=han.xu@nxp.com ARC-Seal: i=1; a=rsa-sha256; s=arcselector9901; d=microsoft.com; cv=none; b=mkKSJBR7RXJf0gLCRmRnWHnW2Av6Q101NyrLrZX7Netc75iycXzi3UYr/aRck82yOhbGvzQRtVmQk7JKb2LwyWypL1wGgINCijmLWIkJ0decUFP4b+g1KfQfm9eAt5kYq0pHcZeVzpYN5yHIL4zmv6SJ4yfL9DOHx2yliPfQNIJ43DmWxvQcp0SjqXAvvXRVS07772dyxqBDVXB7MEjkhABvJbrVNkmywMWTw5OSvqx/ufVcwTQB6qXoBjtZYTPqJzYtS8vMDOiCB7yhx6pm+7CF3Mzu2FCD0rtY9WhS4mngyocx/V5Jeov8s0ku8vOQa0GL/LL7PmDMNu4ZKWw4Hg== 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=lQHtst03foIqf6OFg/Sx7Rs2GWpSwwSKaJVaT7I1oAs=; b=RDXubyjtEXLmohVIZKkXhBoPMs/gps5SSW/VkOlzpJpPHaQUvVrHDlsCrZEomJIHZotWOvvfra3zQSgXlmadqlbveh0Z1679m9Fr9zFaKixjVjUOYN6qwDYfFFWEUXw6TpQfpm6/RN7f6plc7Lh9/UCRrZG/eath4OmODo+6OEb79OtSNlzeGiNzz+lSJ/QyJkBcwDtTbAIWjKbLgizuyZbDS1pm2J7ue09Q9E8Zjd89G4pvfhWHkbDl8Ps2kJrBS9mlhSktid3Y9yj+CVpJ9yT2p9JZHWwPqkSZLjZ0Di+gEQxWvqSAwqQLo7WliInvZt7IcCS1AymFaJC7kDGzSg== ARC-Authentication-Results: i=1; mx.microsoft.com 1; spf=pass smtp.mailfrom=nxp.com; dmarc=pass action=none header.from=nxp.com; dkim=pass header.d=nxp.com; arc=none DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=nxp.com; s=selector2; h=From:Date:Subject:Message-ID:Content-Type:MIME-Version:X-MS-Exchange-SenderADCheck; bh=lQHtst03foIqf6OFg/Sx7Rs2GWpSwwSKaJVaT7I1oAs=; b=lyWmbz75PBCfV8o04V8bGJ2xh7jbXR7gMANsdFjUhm38cEeVfMWBfnSHJbxdrIZEdIPLyYUNk2zs4G8YOhp76qiyQpChgaVszu8XSY7roJNDcskHL/HIb2kZ3ltd0tlNWTqp5lhH2qg5L/9ulisDmIjLOl9VBu9fDXLY/QGGqm0= Authentication-Results: dkim=none (message not signed) header.d=none;dmarc=none action=none header.from=nxp.com; Received: from DU2PR04MB8774.eurprd04.prod.outlook.com (2603:10a6:10:2e1::21) by VI1PR04MB6256.eurprd04.prod.outlook.com (2603:10a6:803:f5::21) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id 15.20.5206.27; Fri, 6 May 2022 14:41:16 +0000 Received: from DU2PR04MB8774.eurprd04.prod.outlook.com ([fe80::47b:6049:acff:ce06]) by DU2PR04MB8774.eurprd04.prod.outlook.com ([fe80::47b:6049:acff:ce06%3]) with mapi id 15.20.5206.027; Fri, 6 May 2022 14:41:16 +0000 Date: Fri, 6 May 2022 09:41:10 -0500 From: Han Xu To: Michael Trimarchi Cc: U-Boot-Denx , Ye Li , Stefano Babic , Miquel Raynal , Fabio Estevam , Dario Binacchi , Sean Anderson , linux-kernel@amarulasolutions.com, Jagan Teki , Ariel D'Alessandro , Fabio Estevam Subject: Re: [PATCH V2 2/4] mtd: nand: mxs_nand_spl: Fix bad block skipping Message-ID: <20220506144110.d4sthydoa7e5yzv5@umbrella> References: <20220427055025.231586-1-michael@amarulasolutions.com> <20220427055025.231586-3-michael@amarulasolutions.com> Content-Type: text/plain; charset=us-ascii Content-Disposition: inline In-Reply-To: <20220427055025.231586-3-michael@amarulasolutions.com> User-Agent: NeoMutt/20171215 X-ClientProxiedBy: SJ0PR03CA0367.namprd03.prod.outlook.com (2603:10b6:a03:3a1::12) To DU2PR04MB8774.eurprd04.prod.outlook.com (2603:10a6:10:2e1::21) MIME-Version: 1.0 X-MS-PublicTrafficType: Email X-MS-Office365-Filtering-Correlation-Id: f700fbea-f52c-4a15-ac67-08da2f6e79e6 X-MS-TrafficTypeDiagnostic: VI1PR04MB6256:EE_ X-LD-Processed: 686ea1d3-bc2b-4c6f-a92c-d99c5c301635,ExtAddr 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: jSC8S6/mUEfAfXaOrkC5SZr9oxk9doUghMNqKDhIq+TWr+9d71K+wXagSxMZCQseBLqPE5gljG/LeFHyMxGws7w8KNVyUiEmvjL4me8SsYIwm5Vfs/wJntiuh0Gp/+9CwspHkpPgYR8p+V5DfAJT6bZh6zfZ9VFl69FWXlWO3ZETwnVAPVU/Ey9Ct+T8o3um77p7uzLZNpkk+us7R9+g0QQF6ltQFKKk4LU+3BJ+iamauLuTjaJ1OpagLBWm7Isj8GEnKxn9qTbxu4f7cdwY8HT9TbIbEiBhJMW6D+YFD+idGm7gkTrncYZHZ8Moyoh1SbqgRm/k5NiAvmXTSf9Hf6tSx5IOROnMuiOqIdp8AQB5iY2ZFokPNU2L4KnOuAkitCGkmLKwk2LsjNwstftIfjB8MbyvjZ3Evfsukef6BmcgxPVX+A3hZPp2cExot4jxV6wm91KUpM4H/VEuFplibiS1wba0kdPXIx5D0UZjMS16Ids/OP26ca3HCnEGVxfNO9WlIrTfV15LUNE2QMa19dUYI+CLMIgFuSO7bMRNLV87felfyuSDOZrAxN4nz2cFYrOca7WWPt4yPBHJz2aMWNniWySW8SaaVUs8+vSR2f93ZKBMQiizOejqnMHNuP2+nNhRflQr92qM9jQgOLq7j26i81B+qGQoIVr4uTJFRfCwOD4Qw4xa8AWzkCKD+zOMqdX/rXA13lIe0SEhx+7AOQ== X-Forefront-Antispam-Report: CIP:255.255.255.255; CTRY:; LANG:en; SCL:1; SRV:; IPV:NLI; SFV:NSPM; H:DU2PR04MB8774.eurprd04.prod.outlook.com; PTR:; CAT:NONE; SFS:(13230001)(4636009)(7916004)(366004)(83380400001)(33716001)(316002)(8936002)(44832011)(66946007)(8676002)(4326008)(6512007)(66476007)(6486002)(508600001)(54906003)(7416002)(6916009)(5660300002)(6666004)(52116002)(6506007)(9686003)(2906002)(26005)(1076003)(186003)(66556008)(38350700002)(38100700002)(86362001); DIR:OUT; SFP:1101; X-MS-Exchange-AntiSpam-MessageData-ChunkCount: 1 X-MS-Exchange-AntiSpam-MessageData-0: =?us-ascii?Q?FcAvFxqVWHRwJXIk6730vXiCOUk+WL6nnLFH2eaqAqO08rn1+zW6yE0g+Wmo?= =?us-ascii?Q?2twNBCgd+a7eqdvbWmttPjeo5pQGDSrdOGEbLz/LxMGmw98vmPrcQM/Qbh+e?= =?us-ascii?Q?aP9EItso9okEdFZuA9MJbTRTB4xQWIHiycXtkXF8+/dizjBiGhsF+m9rUTn0?= =?us-ascii?Q?LSbBQ5LApX7NIUnTG+3fTKIESoijNvQiLWHL17El2jTR9ZJqyvfvOErq5xPp?= =?us-ascii?Q?EghPnQTLANS0WJ90e2ggYsBldXkokwmmhNrftT8XGlbSSHUkmW7Rg6+j4gUn?= =?us-ascii?Q?t8/VxG/F53HjoLNHOrcLNkIjVagdpIePbhAL0nHaAzQXvxtKFyNZ9E0xodux?= =?us-ascii?Q?yrOql3EV9mt7jochz8muMoJ4SZiTQXzNo9UIWLVN94qmuYjiYQMWRAwadQOd?= =?us-ascii?Q?6kJT7jiA++aw46xkYkUM90RLX3VFk9dJgorGEz2H6QfJhGYG/B+7GlPpw+s6?= =?us-ascii?Q?NS6ADs1jfY54iowEipv2oO5mmSX6BzXOfte68QgSARFGWCj4CzWovuHSGxZV?= =?us-ascii?Q?PaxNXUucbb7x1qgS7wmg11LMex6ikTYH02BNcmCGJYrytKJ9qAOTmM5kM+RF?= =?us-ascii?Q?L2/w0Um7JY5ySDc5rOh73UtPjGJBneKV94D6hoalbYORQ7PO/zbgvD4p8QWw?= =?us-ascii?Q?j2wG/RrnQ72m6QVV5KWeJ4EQI+uiktYBfcMFhvbX92vPyIfNiTFewMovP2Lz?= =?us-ascii?Q?CDZwzF8FxLuNwjRcYriDZ/W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LI5Pe4uN9lhXLwrEqpj9od8VG6XvdGkFI23oNRlAz7UjNCbyeRgtpXRH0yj?= =?us-ascii?Q?OhbPSCoakYVy38NJcha01CAQ3W9R4jqUKfZyKeQ+q0BYUBEVMCLwlszAZs/l?= =?us-ascii?Q?nIXNT0ueaA=3D=3D?= X-OriginatorOrg: nxp.com X-MS-Exchange-CrossTenant-Network-Message-Id: f700fbea-f52c-4a15-ac67-08da2f6e79e6 X-MS-Exchange-CrossTenant-AuthSource: DU2PR04MB8774.eurprd04.prod.outlook.com X-MS-Exchange-CrossTenant-AuthAs: Internal X-MS-Exchange-CrossTenant-OriginalArrivalTime: 06 May 2022 14:41:16.7070 (UTC) X-MS-Exchange-CrossTenant-FromEntityHeader: Hosted X-MS-Exchange-CrossTenant-Id: 686ea1d3-bc2b-4c6f-a92c-d99c5c301635 X-MS-Exchange-CrossTenant-MailboxType: HOSTED X-MS-Exchange-CrossTenant-UserPrincipalName: 2DwWZ4mKzDDLl+2AAKbdvPqcNnWnz5iTu0RiVUA48uv+q6G7OZT1TjSBk1IpTBqi X-MS-Exchange-Transport-CrossTenantHeadersStamped: VI1PR04MB6256 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 22/04/27 07:50AM, Michael Trimarchi wrote: > The specific implementation was having bug. Those bugs are since > the beginning of the implementation. Some manufactures can receive > this bug in their SPL code. This bug start to be more visible on > architecture that has complicated boot process like imx8mn. Older > version of uboot has the same problem only if the bad block > appear in correspoding of befine of u-boot image. In order to > adjust the function we scan from the first block. The logic > is not changed to have a simple way to fix without get regression. > > The problematic part of old code was in this part: > > while (is_badblock(mtd, offs, 1)) { > page = page + nand_page_per_block; > /* Check i we've reached the end of flash. */ > if (page >= mtd->size >> chip->page_shift) { > free(page_buf); > return -ENOMEM; > } > } > > Even we fix it adding increment of the offset of one erase block size > we don't fix the problem, because the first erase block where the > image start is not checked. The code was tested on an imx8mn where > the boot rom api was not able to skip it. Apart of that other > architecure are using this code and all boards that has nand as boot > device can be affected > > Cc: Han Xu > Cc: Fabio Estevam > Signed-off-by: Michael Trimarchi Acked-by: Han Xu > --- > V1->V2: > - Adjust the commit message > - Add Cc Han Xu and Fabio > - fix size >= 0 to > 0 > --- > drivers/mtd/nand/raw/mxs_nand_spl.c | 90 ++++++++++++++++------------- > 1 file changed, 49 insertions(+), 41 deletions(-) > > diff --git a/drivers/mtd/nand/raw/mxs_nand_spl.c b/drivers/mtd/nand/raw/mxs_nand_spl.c > index 59a67ee414..2bfb181007 100644 > --- a/drivers/mtd/nand/raw/mxs_nand_spl.c > +++ b/drivers/mtd/nand/raw/mxs_nand_spl.c > @@ -218,14 +218,14 @@ void nand_init(void) > mxs_nand_setup_ecc(mtd); > } > > -int nand_spl_load_image(uint32_t offs, unsigned int size, void *buf) > +int nand_spl_load_image(uint32_t offs, unsigned int size, void *dst) > { > - struct nand_chip *chip; > - unsigned int page; > + unsigned int sz; > + unsigned int block, lastblock; > + unsigned int page, page_offset; > unsigned int nand_page_per_block; > - unsigned int sz = 0; > + struct nand_chip *chip; > u8 *page_buf = NULL; > - u32 page_off; > > chip = mtd_to_nand(mtd); > if (!chip->numchips) > @@ -235,47 +235,42 @@ int nand_spl_load_image(uint32_t offs, unsigned int size, void *buf) > if (!page_buf) > return -ENOMEM; > > - page = offs >> chip->page_shift; > - page_off = offs & (mtd->writesize - 1); > + /* offs has to be aligned to a page address! */ > + block = offs / mtd->erasesize; > + lastblock = (offs + size - 1) / mtd->erasesize; > + page = (offs % mtd->erasesize) / mtd->writesize; > + page_offset = offs % mtd->writesize; > nand_page_per_block = mtd->erasesize / mtd->writesize; > > - debug("%s offset:0x%08x len:%d page:%x\n", __func__, offs, size, page); > - > - while (size) { > - if (mxs_read_page_ecc(mtd, page_buf, page) < 0) > - return -1; > - > - if (size > (mtd->writesize - page_off)) > - sz = (mtd->writesize - page_off); > - else > - sz = size; > - > - memcpy(buf, page_buf + page_off, sz); > - > - offs += mtd->writesize; > - page++; > - buf += (mtd->writesize - page_off); > - page_off = 0; > - size -= sz; > - > - /* > - * Check if we have crossed a block boundary, and if so > - * check for bad block. > - */ > - if (!(page % nand_page_per_block)) { > - /* > - * Yes, new block. See if this block is good. If not, > - * loop until we find a good block. > - */ > - while (is_badblock(mtd, offs, 1)) { > - page = page + nand_page_per_block; > - /* Check i we've reached the end of flash. */ > - if (page >= mtd->size >> chip->page_shift) { > + while (block <= lastblock && size > 0) { > + if (!is_badblock(mtd, mtd->erasesize * block, 1)) { > + /* Skip bad blocks */ > + while (page < nand_page_per_block) { > + int curr_page = nand_page_per_block * block + page; > + > + if (mxs_read_page_ecc(mtd, page_buf, curr_page) < 0) { > free(page_buf); > - return -ENOMEM; > + return -EIO; > } > + > + if (size > (mtd->writesize - page_offset)) > + sz = (mtd->writesize - page_offset); > + else > + sz = size; > + > + memcpy(dst, page_buf + page_offset, sz); > + dst += sz; > + size -= sz; > + page_offset = 0; > + page++; > } > + > + page = 0; > + } else { > + lastblock++; > } > + > + block++; > } > > free(page_buf); > @@ -294,6 +289,19 @@ void nand_deselect(void) > > u32 nand_spl_adjust_offset(u32 sector, u32 offs) > { > - /* Handle the offset adjust in nand_spl_load_image,*/ > + unsigned int block, lastblock; > + > + block = sector / mtd->erasesize; > + lastblock = (sector + offs) / mtd->erasesize; > + > + while (block <= lastblock) { > + if (is_badblock(mtd, block * mtd->erasesize, 1)) { > + offs += mtd->erasesize; > + lastblock++; > + } > + > + block++; > + } > + > return offs; > } > -- > 2.25.1 >