From patchwork Sun May 15 09:35:31 2022 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Michael Nazzareno Trimarchi X-Patchwork-Id: 1952 Return-Path: X-Original-To: linux-amarula@patchwork.amarulasolutions.com Delivered-To: linux-amarula@patchwork.amarulasolutions.com Received: from mail-ed1-f71.google.com (mail-ed1-f71.google.com [209.85.208.71]) by ganimede.amarulasolutions.com (Postfix) with ESMTPS id 952A83F1F5 for ; Sun, 15 May 2022 11:35:46 +0200 (CEST) Received: by mail-ed1-f71.google.com with SMTP id h7-20020a056402094700b00425a52983dfsf7825421edz.8 for ; Sun, 15 May 2022 02:35:46 -0700 (PDT) ARC-Seal: i=2; a=rsa-sha256; t=1652607346; cv=pass; d=google.com; s=arc-20160816; b=gwF8xwJWovOdvSXzvMGCXOSX4EMvFdIELf9AI7rf+/v+SO5YN0qwB3qw7TlUJntrgf DWie0uAHkttkGdAqYbGVw1wIQolvnolvGrTULWBQMBRjlSjH8a39vU8mkX1JUDHPJfgF 2wD+R7sktayBcPmQwVqHwnMVbSKI8ZUSILKyX2oF1FLK0SXycvj9EgBUMEkK0qODXAAq STRdYltxxa41Aq7mOgV7mk8Hb5n/IJ5N2CkOiHl3BYL9A89N315F9JgphJzRpRiT76gv tYXVeBN0kzfteQzscHjrhc/ZfAreZPE7OuvgpaaVshbdBkjjF1osw4mLWRatp5n86bGT qFbw== ARC-Message-Signature: i=2; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-unsubscribe:list-archive:list-help:list-post:list-id :mailing-list:precedence:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:dkim-signature; bh=z2sBDyFkNrTp4iPUVnHLwp1l0cReaDzCIdIQXSnKWBc=; b=hQPQaUnKGSxatyMo30a2xzrq5x6sQWXZzOpcj9FXm/61hho0MoXd6BNc+PimsVXflB ET/6kd4VWJqS5zVS8shyxAt9Syb0T+EA/F32jnwXexUIePcEnA8fwxX0LcvcHrW+x+zu Nyrp7lbd2n7wrHuLpDp2qtrV/PQP0QHkMqXlV+lib7aZX+e5CE7gdVojQFMBfyvWw/zY lM+bVFc/dg0aaR5/4RZM9tbliO0ve6aGscol5+dYKQv96NnUg4aqtfgaGzdBOTcvDXya RKbOYzDcSCuwQ9I9rHuUeFoqUlhswZ6UpV3nXHxEPYjbST2SZ+KQF5xwryT+VcYsaeFH FASw== ARC-Authentication-Results: i=2; mx.google.com; dkim=pass header.i=@amarulasolutions.com header.s=google header.b=pgGeQV7n; spf=pass (google.com: domain of michael@amarulasolutions.com designates 209.85.220.41 as permitted sender) smtp.mailfrom=michael@amarulasolutions.com; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=amarulasolutions.com DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=amarulasolutions.com; s=google; h=from:to:cc:subject:date:message-id:in-reply-to:references :mime-version:x-original-sender:x-original-authentication-results :precedence:mailing-list:list-id:list-post:list-help:list-archive :list-unsubscribe; bh=z2sBDyFkNrTp4iPUVnHLwp1l0cReaDzCIdIQXSnKWBc=; b=dXqM8dWI522X4T1MoL124D+KTwn38r8DEMyVpyjOmCk+3LY6izBUPR41hcTPJlWn3r Lfy6dWB6BE9jTNX9Zp6rhh/05JzRBoEtX57maXqW+4Wz5vOZgcYrThjxN2bUlJ4I5Qtu xGXGezxImFKU14GSkJqfDWk3SLUgZI0jcp0HY= X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20210112; h=x-gm-message-state:from:to:cc:subject:date:message-id:in-reply-to :references:mime-version:x-original-sender :x-original-authentication-results:precedence:mailing-list:list-id :x-spam-checked-in-group:list-post:list-help:list-archive :list-unsubscribe; bh=z2sBDyFkNrTp4iPUVnHLwp1l0cReaDzCIdIQXSnKWBc=; b=YSUXiPCdM6az7kzxUdbU7kaqW0DprpoHP74kXPpYbMTo9YXSZVGHmq3q9+2pK2NOzW FUZJZE98IXPfIQsGG/Km254irR8hyA2vg9UHsUF18vxuzFZe2+F+EGkH8MIxAahBHUnE f1eAG2uy3EX99ehdZ2+25AjrbYdkLQoyKiXMxZJiVolZ0eTm41ViQU75l5+5xljAt49M xTJTQLdbrhQjniuJX60L6ifKClHClsbRz3+zK4FRhdV0tBNNRQ35HZ8iOQWRpRpU15mI JoQl2d+aKQZGm7+LIUGD2nwE+1L8SOMohyDIlyGv/epz0WjIxI9++8R8uOqkGK4d8hxj wDSw== X-Gm-Message-State: AOAM531z+UB1hjp81yNDssPdBGYVc/1Vqpvqspu3jaWQh214w2KmHLfp QmFgkl+GafEl0dVhATptRidOAuRe X-Google-Smtp-Source: ABdhPJx+9+bKqE4iVySxa+6WT2nOZ74FCZ0rSC9hYYjcU4jRBHvwE/Ozczu60EY5woccL64Hhr+XIw== X-Received: by 2002:a50:fe1a:0:b0:425:e276:5adf with SMTP id f26-20020a50fe1a000000b00425e2765adfmr7521919edt.284.1652607346209; Sun, 15 May 2022 02:35:46 -0700 (PDT) X-BeenThere: linux-amarula@amarulasolutions.com Received: by 2002:a05:6402:270b:b0:427:d070:5bfe with SMTP id y11-20020a056402270b00b00427d0705bfels3101996edd.0.gmail; Sun, 15 May 2022 02:35:45 -0700 (PDT) X-Received: by 2002:a05:6402:43c4:b0:41d:9403:8dca with SMTP id p4-20020a05640243c400b0041d94038dcamr7305153edc.184.1652607344913; Sun, 15 May 2022 02:35:44 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1652607344; cv=none; d=google.com; s=arc-20160816; b=yCP1tGAPpaGDqKc1N0iKYK84H/YeUF/wY8jXyaAa4k2ScHO4NPlC1D3xYC5tLmCnit aBjTasvxozYJHHoJvvmgbzSeIM8V+zMiW4exCIKqYu9GA01USj+HQVpvgAB95lsmdzXB ttvnER5VVAcU0ypXJG7/1lEz0L1Vez+hSx1nUfYyMTF+urCBGmBi4AG3btCRMJtn8Vux F4fQnBP2KeldzFSYlvYY6GGEhPtKwcMXHCmC37HWJ3mqrWtg6mE1PUmBC9N6e+oHBqq3 kknaJobiaVf/BuJxchXmiq1NmhpF6A5bM0tUf3cQEIxaRb2N1Lh02HPBXRFuKNiHP6tT YfIg== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:dkim-signature; bh=YWmXIADIm9JSh8WtxwkqZljfFa3HcMwbtQZcEBjjleo=; b=hO+rkGkvUgK6a8WrXOndFPSZz1KkmkIq0ltu0A72TAzkIqRl9T6kUNkQmqyone9h4a a5cURjvzUTQ6sX6CshlxrB9gX6f0joam3RBqexydXgmc2ZhY4NkPX4Go1hQfl4cbJVJr EC1ABgSoXRWJW9LO0x0w4EJ2mb/MfyRyBKdTkX6LP1KxFyTpqWF4cyoXOEjMgSqxYWpK 7QOOMG63yxtbdC3s8PAG7ZhtjUCusArfeX0gq2SlC6aFSDaDdEQo4yxZiWdXdwM4BqFL P8gjyC6+7JoE8MOS2Ubhb45H7Ux0lnssoh2wuSoYStRYQVM7ZatwHustBiwxtmvwtj1H cNEQ== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@amarulasolutions.com header.s=google header.b=pgGeQV7n; spf=pass (google.com: domain of michael@amarulasolutions.com designates 209.85.220.41 as permitted sender) smtp.mailfrom=michael@amarulasolutions.com; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=amarulasolutions.com Received: from mail-sor-f41.google.com (mail-sor-f41.google.com. [209.85.220.41]) by mx.google.com with SMTPS id 22-20020a170906309600b006f4c2b0dbb8sor2581311ejv.67.2022.05.15.02.35.44 for (Google Transport Security); Sun, 15 May 2022 02:35:44 -0700 (PDT) Received-SPF: pass (google.com: domain of michael@amarulasolutions.com designates 209.85.220.41 as permitted sender) client-ip=209.85.220.41; X-Received: by 2002:a17:906:c114:b0:6f5:db6f:71a1 with SMTP id do20-20020a170906c11400b006f5db6f71a1mr10866183ejc.338.1652607344534; Sun, 15 May 2022 02:35:44 -0700 (PDT) Received: from localhost.localdomain (mob-109-118-140-232.net.vodafone.it. [109.118.140.232]) by smtp.gmail.com with ESMTPSA id p6-20020a17090664c600b006f3ef214de8sm2516582ejn.78.2022.05.15.02.35.42 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Sun, 15 May 2022 02:35:43 -0700 (PDT) From: Michael Trimarchi To: Han Xu , U-Boot-Denx Cc: Ye Li , Stefano Babic , Miquel Raynal , Fabio Estevam , Dario Binacchi , Sean Anderson , linux-amarula@amarulasolutions.com, Jagan Teki , Ariel D'Alessandro , Tom Rini , Fabio Estevam , Tim Harvey Subject: [PATCH V4 2/5] mtd: nand: mxs_nand_spl: Fix bad block skipping Date: Sun, 15 May 2022 11:35:31 +0200 Message-Id: <20220515093534.1737172-3-michael@amarulasolutions.com> X-Mailer: git-send-email 2.25.1 In-Reply-To: <20220515093534.1737172-1-michael@amarulasolutions.com> References: <20220515093534.1737172-1-michael@amarulasolutions.com> MIME-Version: 1.0 X-Original-Sender: michael@amarulasolutions.com X-Original-Authentication-Results: mx.google.com; dkim=pass header.i=@amarulasolutions.com header.s=google header.b=pgGeQV7n; spf=pass (google.com: domain of michael@amarulasolutions.com designates 209.85.220.41 as permitted sender) smtp.mailfrom=michael@amarulasolutions.com; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=amarulasolutions.com Content-Type: text/plain; charset="UTF-8" Precedence: list Mailing-list: list linux-amarula@amarulasolutions.com; contact linux-amarula+owners@amarulasolutions.com List-ID: X-Spam-Checked-In-Group: linux-amarula@amarulasolutions.com X-Google-Group-Id: 476853432473 List-Post: , List-Help: , List-Archive: List-Unsubscribe: , The specific implementation was having bug. Those bugs are since the beginning of the implementation. Some manufactures can already experience this bug in their SPL code. This bug can be more visible on architecture that has complicated boot process like imx8mn. Older version of uboot can be affected if the bad block appear in correspoding of the beginning of u-boot image. In order to adjust the function we scan from the first erase block. 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. This code is used by other architecures like imx6 and imx8mm Cc: Han Xu Cc: Fabio Estevam Acked-by: Han Xu Tested-By: Tim Harvey Signed-off-by: Michael Trimarchi --- V3->V4: - none V2->V3: - Add tested-by from Tim - Add ack from Han Xu - Rework english of commit message 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; }