From patchwork Wed Jul 11 08:52:34 2018 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 8bit X-Patchwork-Submitter: =?utf-8?b?UmFmYcWCIE1pxYJlY2tp?= X-Patchwork-Id: 942367 X-Patchwork-Delegate: zajec5@gmail.com Return-Path: X-Original-To: incoming@patchwork.ozlabs.org Delivered-To: patchwork-incoming@bilbo.ozlabs.org Authentication-Results: ozlabs.org; spf=pass (mailfrom) smtp.mailfrom=lists.openwrt.org (client-ip=2607:7c80:54:e::133; helo=bombadil.infradead.org; envelope-from=openwrt-devel-bounces+incoming=patchwork.ozlabs.org@lists.openwrt.org; receiver=) Authentication-Results: ozlabs.org; dmarc=fail (p=none dis=none) header.from=gmail.com Authentication-Results: ozlabs.org; dkim=pass (2048-bit key; unprotected) header.d=lists.infradead.org header.i=@lists.infradead.org header.b="T81k1RAv"; dkim=fail reason="signature verification failed" (2048-bit key; unprotected) header.d=gmail.com header.i=@gmail.com header.b="ftRwSH3H"; dkim-atps=neutral Received: from bombadil.infradead.org (bombadil.infradead.org [IPv6:2607:7c80:54:e::133]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by ozlabs.org (Postfix) with ESMTPS id 41QXsL3NcJz9s0n for ; Wed, 11 Jul 2018 18:53:10 +1000 (AEST) DKIM-Signature: v=1; a=rsa-sha256; q=dns/txt; c=relaxed/relaxed; d=lists.infradead.org; s=bombadil.20170209; h=Sender: Content-Transfer-Encoding:Content-Type:Cc:List-Subscribe:List-Help:List-Post: List-Archive:List-Unsubscribe:List-Id:Subject:MIME-Version:References: In-Reply-To:Message-Id:Date:To:From:Reply-To:Content-ID:Content-Description: Resent-Date:Resent-From:Resent-Sender:Resent-To:Resent-Cc:Resent-Message-ID: List-Owner; bh=uHkNLhh/RoEI0oCWp5FpvFWEx+9YRNtTv/BAIS/1BsY=; b=T81k1RAvtG+0N/ 3Yp/0FJ+LcFdsyVRAArv0N2KwIHM0zE1u6U1qQDGI0XxldgcnEU3Y630WMz44p+m8m/Gbv/90sYtm GaxCxDe6hxAQgA/itac2oY9XxroJD69btjNkhKGYKGoLjXkf9AjvqvHuQTLzzTysZu5v1623ppmPl zgoXoZgKxOidVddy68iqD5cT6cckIlpypoqiKPDOywz+k5H2g0oLY/py+fynf8V4tBQpB1nujWBJ8 YcAWuSzSKb5Df9QqQQ9Bq4kSn8p7AcILB9NlpZbGowcq/ARsgrFjpuJyFWMB2DMkruNDB/cg/yUcg N21SQesZ+MYEj88GPZAQ==; Received: from localhost ([127.0.0.1] helo=bombadil.infradead.org) by bombadil.infradead.org with esmtp (Exim 4.90_1 #2 (Red Hat Linux)) id 1fdArv-0002fI-Q7; Wed, 11 Jul 2018 08:53:03 +0000 Received: from mail-lf0-x242.google.com ([2a00:1450:4010:c07::242]) by bombadil.infradead.org with esmtps (Exim 4.90_1 #2 (Red Hat Linux)) id 1fdArs-0002eD-NK for openwrt-devel@lists.openwrt.org; Wed, 11 Jul 2018 08:53:02 +0000 Received: by mail-lf0-x242.google.com with SMTP id j8-v6so20641578lfb.4 for ; Wed, 11 Jul 2018 01:52:50 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20161025; h=from:to:cc:subject:date:message-id:in-reply-to:references :mime-version:content-transfer-encoding; bh=FgJmQ5QWEp34uk8/ctf8FJos/YZtw2wKJ+iJ0IXEyUY=; b=ftRwSH3H2PpFExuzfHzeape2qBfEzR2dJwgFvn7NjrhikeVijOQhHwpSYHkLzSsFVm uMzG2K4Qv2wiT+LmHJM3V2wx4Waa6z5arbbV3ZLN83Hl5gsdPjitTn3UbsIeQ0/eCSC4 KejXgQAywwBGL2w90xOsGP0DNZDukxz9dUOUJ+FEdWnR/eOFAMmzdoNwMYS5ADAbKhFd U9ocv0VhQbzZ6j0Gpvo6BnU+ipxNsZrZs5hFgbVk7TlwbRXVEywpGCc5wsOknhCuutUd CpN66mNAE97CJAbkpDgE3YyrGoOhfZPrWKOn0+BMW8SGMCBljHjjX7I0KEzOz737OY0A 7QZw== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; h=x-gm-message-state:from:to:cc:subject:date:message-id:in-reply-to :references:mime-version:content-transfer-encoding; bh=FgJmQ5QWEp34uk8/ctf8FJos/YZtw2wKJ+iJ0IXEyUY=; b=bktzOSUu2x09emUAT1R04KxpLMykPedUNcGGgyun/slyg9NLVHAX+lNfw0dyP+Xrhf RndmaxSFQ6rmQZhta/CLv7k3nET6BloKXQIH+CkfHWcZyWWpHCrMxw/ibzDRMwV6CUy4 eWb8jHIuo4tcWSilLg+PFSWNkkSJU+e3eiAspLZENEFR2cODYHefsOzQYv3gM3vdwUCr MgEBxuUyODcN6i7LFv7Igz7ilK/4CyZmF/1fUNtyQrPaZghFR+e/eOBzIOnhGep8fIAs 7Av03oRunjolN94C3sGCQtj/4MuWA3b9Uiuba07gPMb3gseFZ3QCjVSTDUhymDCBoeoW /ioQ== X-Gm-Message-State: APt69E0fgTefstM+ld0pSWs99t7R1lEEdAQEDu7f+vCi794XxhTJxk+7 n8sWrK1jYzBeUEIzXjvAr5dsuQ== X-Google-Smtp-Source: AAOMgpfJLsWg05KDeGFEgmJ8MpxpkUpajel67gIK7dID0/Sh0GkTNCVKLo5Dj0/Qt3+a/iG3XFXzyg== X-Received: by 2002:a19:f70d:: with SMTP id z13-v6mr5210358lfe.33.1531299168367; Wed, 11 Jul 2018 01:52:48 -0700 (PDT) Received: from linux-veee.lan (ip-194-187-74-233.konfederacka.maverick.com.pl. [194.187.74.233]) by smtp.gmail.com with ESMTPSA id y72-v6sm2296542lfg.74.2018.07.11.01.52.47 (version=TLS1_2 cipher=ECDHE-RSA-AES128-SHA bits=128/128); Wed, 11 Jul 2018 01:52:47 -0700 (PDT) From: =?utf-8?b?UmFmYcWCIE1pxYJlY2tp?= To: openwrt-devel@lists.openwrt.org Date: Wed, 11 Jul 2018 10:52:34 +0200 Message-Id: <20180711085234.1201-1-zajec5@gmail.com> X-Mailer: git-send-email 2.13.7 In-Reply-To: <20180710195915.1856-1-zajec5@gmail.com> References: <20180710195915.1856-1-zajec5@gmail.com> MIME-Version: 1.0 X-CRM114-Version: 20100106-BlameMichelson ( TRE 0.8.0 (BSD) ) MR-646709E3 X-CRM114-CacheID: sfid-20180711_015300_779757_5D9957C7 X-CRM114-Status: GOOD ( 14.17 ) X-Spam-Score: 0.1 (/) X-Spam-Report: SpamAssassin version 3.4.1 on bombadil.infradead.org summary: Content analysis details: (0.1 points) pts rule name description ---- ---------------------- -------------------------------------------------- -0.0 RCVD_IN_DNSWL_NONE RBL: Sender listed at http://www.dnswl.org/, no trust [2a00:1450:4010:c07:0:0:0:242 listed in] [list.dnswl.org] 0.2 FREEMAIL_ENVFROM_END_DIGIT Envelope-from freemail username ends in digit (zajec5[at]gmail.com) -0.0 SPF_PASS SPF: sender matches SPF record 0.0 FREEMAIL_FROM Sender email is commonly abused enduser mail provider (zajec5[at]gmail.com) -0.1 DKIM_VALID Message has at least one valid DKIM or DK signature 0.1 DKIM_SIGNED Message has a DKIM or DK signature, not necessarily valid -0.1 DKIM_VALID_AU Message has a valid DKIM or DK signature from author's domain Subject: [OpenWrt-Devel] [PATCH V2] mtd: support bad blocks within the mtd_fixtrx() X-BeenThere: openwrt-devel@lists.openwrt.org X-Mailman-Version: 2.1.21 Precedence: list List-Id: List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Cc: =?utf-8?b?UmFmYcWCIE1pxYJlY2tp?= Sender: "openwrt-devel" Errors-To: openwrt-devel-bounces+incoming=patchwork.ozlabs.org@lists.openwrt.org From: Rafał Miłecki Reading MTD data with (p)read doesn't return any error when accessing bad block. As the result, with current code, CRC32 covers "data" stored in bad blocks. That behavior doesn't match CFE's one (bootloader simply skips bad blocks) and may result in: 1) Invalid CRC32 2) CFE refusing to boot firmware with a following error: Boot program checksum is invalid Fix that problem by checking every block before reading its content. Signed-off-by: Rafał Miłecki --- V2: Bump PKG_RELEASE Use min() to make code easier to understand Recalculate data_size to make sure old size value (including bad blocks) isn't used. Drop some unneeded helper variables. --- package/system/mtd/Makefile | 2 +- package/system/mtd/src/trx.c | 40 ++++++++++++++++++++++++++++++++++------ 2 files changed, 35 insertions(+), 7 deletions(-) diff --git a/package/system/mtd/Makefile b/package/system/mtd/Makefile index 9ae03a77a9..969e1c711c 100644 --- a/package/system/mtd/Makefile +++ b/package/system/mtd/Makefile @@ -9,7 +9,7 @@ include $(TOPDIR)/rules.mk include $(INCLUDE_DIR)/kernel.mk PKG_NAME:=mtd -PKG_RELEASE:=21 +PKG_RELEASE:=22 PKG_BUILD_DIR := $(KERNEL_BUILD_DIR)/$(PKG_NAME) STAMP_PREPARED := $(STAMP_PREPARED)_$(call confvar,CONFIG_MTD_REDBOOT_PARTS) diff --git a/package/system/mtd/src/trx.c b/package/system/mtd/src/trx.c index 1f5c52914c..d4d486c32b 100644 --- a/package/system/mtd/src/trx.c +++ b/package/system/mtd/src/trx.c @@ -46,6 +46,12 @@ struct trx_header { uint32_t offsets[3]; /* Offsets of partitions from start of header */ }; +#define min(x,y) ({ \ + typeof(x) _x = (x); \ + typeof(y) _y = (y); \ + (void) (&_x == &_y); \ + _x < _y ? _x : _y; }) + #if __BYTE_ORDER == __BIG_ENDIAN #define STORE32_LE(X) ((((X) & 0x000000FF) << 24) | (((X) & 0x0000FF00) << 8) | (((X) & 0x00FF0000) >> 8) | (((X) & 0xFF000000) >> 24)) #elif __BYTE_ORDER == __LITTLE_ENDIAN @@ -156,7 +162,7 @@ mtd_fixtrx(const char *mtd, size_t offset, size_t data_size) int fd; struct trx_header *trx; char *first_block; - char *buf; + char *buf, *to; ssize_t res; size_t block_offset; @@ -214,11 +220,34 @@ mtd_fixtrx(const char *mtd, size_t offset, size_t data_size) exit(1); } - res = pread(fd, buf, data_size, data_offset); - if (res != data_size) { - perror("pread"); - exit(1); + to = buf; + while (data_size) { + size_t read_block_offset = data_offset & ~(erasesize - 1); + size_t read_chunk; + + read_chunk = erasesize - (data_offset & (erasesize - 1)); + read_chunk = min(read_chunk, data_size); + + res = ioctl(fd, MEMGETBADBLOCK, &read_block_offset); + if (res == -1) { + perror("ioctl"); + exit(1); + } + + /* Read from good blocks only to match CFE behavior */ + if (res == 0) { + res = pread(fd, to, read_chunk, data_offset); + if (res != read_chunk) { + perror("pread"); + exit(1); + } + to += read_chunk; + } + + data_offset += read_chunk; + data_size -= read_chunk; } + data_size = to - buf; trx->len = STORE32_LE(data_size + offsetof(struct trx_header, flag_version)); @@ -244,4 +273,3 @@ mtd_fixtrx(const char *mtd, size_t offset, size_t data_size) return 0; } -