From patchwork Fri May 6 12:50:13 2016 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: 619291 Return-Path: X-Original-To: incoming@patchwork.ozlabs.org Delivered-To: patchwork-incoming@bilbo.ozlabs.org Received: from bombadil.infradead.org (bombadil.infradead.org [IPv6:2001:1868:205::9]) (using TLSv1.2 with cipher ECDHE-RSA-AES128-GCM-SHA256 (128/128 bits)) (No client certificate requested) by ozlabs.org (Postfix) with ESMTPS id 3r1Wr23Bnxz9t3r for ; Fri, 6 May 2016 22:51:46 +1000 (AEST) Authentication-Results: ozlabs.org; dkim=fail reason="signature verification failed" (2048-bit key; unprotected) header.d=gmail.com header.i=@gmail.com header.b=KkEzN4HR; dkim-atps=neutral Received: from localhost ([127.0.0.1] helo=bombadil.infradead.org) by bombadil.infradead.org with esmtp (Exim 4.80.1 #2 (Red Hat Linux)) id 1ayfDV-0001ME-C2; Fri, 06 May 2016 12:50:49 +0000 Received: from mail-lf0-x22f.google.com ([2a00:1450:4010:c07::22f]) by bombadil.infradead.org with esmtps (Exim 4.80.1 #2 (Red Hat Linux)) id 1ayfDS-0001HH-A4 for lede-dev@lists.infradead.org; Fri, 06 May 2016 12:50:47 +0000 Received: by mail-lf0-x22f.google.com with SMTP id u64so130577074lff.3 for ; Fri, 06 May 2016 05:50:25 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20120113; h=from:to:cc:subject:date:message-id:mime-version :content-transfer-encoding; bh=yeWSB7sHbW6fhCD3ioWK5iqk4XmCXcI4JHmxbrB+bPk=; b=KkEzN4HR8BMCIH9AJLACPVs7DUVhJWEyGv78JL+v2oG8XKRoVh3h/Cs6SaR8JSZw4Q Zhmjso6SVlR6EEtuxxw4c7E1fwBzd5fV0CIpdP484dIa3k44D0pdAu0c+VXVStLNjsUP 7CpJDqNzqssXwL0dRNUvwvq46+HWb6dDrKs/2aNFVVZkXKnovuntvO//2hNEd7n886v3 dft2u1/L+jqZfpi7h2UBrSLfXA7iZiZbJB27NkQ7ClnkOD+VzE1JU9LysEkFVxdUbbVs fAi9TuRJPGe5Thwo/53Xl56l9LahRl/tmYVncm/3kMnkJ51Is3z+VR1vcBnH28b6oRJv O8qQ== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20130820; h=x-gm-message-state:from:to:cc:subject:date:message-id:mime-version :content-transfer-encoding; bh=yeWSB7sHbW6fhCD3ioWK5iqk4XmCXcI4JHmxbrB+bPk=; b=i8X8pFBjSPar3srv2avkzFSNWVpWMth9w0e8HGtsOvmQrRAV/25orl71fHvCC+vTW8 jfELyXOphhpj9gKmqJgxxdz9gvkNIl7leM8lpC8CejcDSbFgbzJqmUDf1hwl9VZr2Sg2 2hHG64LjOj/+/PZdHRAb+zp9u93eecvH5dwbw/8ibYeymcOKtASfkkdYilcbuqCexalA 94nLukzwzMIFLml/pK1iKwP5POlwovdATcZS00sJ2TZRN5VO8RWQ5Yu638opgZt1SqkP OuLH6OyIY/ABaFy6cHJ0recnYripssOvH3ik6anQjloy4COHgjeHi55EID2D3zCkZVOg cHaQ== X-Gm-Message-State: AOPr4FU/uSY2lbsZcvvYLwoZm+VGhhWPdpWZZi2JdzivJK2iR/B+lEECMznQtPtf9OBrTg== X-Received: by 10.112.146.164 with SMTP id td4mr9542285lbb.82.1462539024265; Fri, 06 May 2016 05:50:24 -0700 (PDT) Received: from linux-samsung.lan (ip-194-187-74-233.konfederacka.maverick.com.pl. [194.187.74.233]) by smtp.gmail.com with ESMTPSA id bd5sm2441112lbc.12.2016.05.06.05.50.23 (version=TLS1_2 cipher=ECDHE-RSA-AES128-GCM-SHA256 bits=128/128); Fri, 06 May 2016 05:50:23 -0700 (PDT) From: =?UTF-8?q?Rafa=C5=82=20Mi=C5=82ecki?= To: lede-dev@lists.infradead.org, openwrt-devel@lists.openwrt.org Subject: [PATCH] mtd: add -c option for specifying amount of data to be used for checksum Date: Fri, 6 May 2016 14:50:13 +0200 Message-Id: <1462539013-18188-1-git-send-email-zajec5@gmail.com> X-Mailer: git-send-email 1.8.4.5 MIME-Version: 1.0 X-CRM114-Version: 20100106-BlameMichelson ( TRE 0.8.0 (BSD) ) MR-646709E3 X-CRM114-CacheID: sfid-20160506_055046_741693_7D8856BB X-CRM114-Status: GOOD ( 16.28 ) X-Spam-Score: -2.5 (--) X-Spam-Report: SpamAssassin version 3.4.0 on bombadil.infradead.org summary: Content analysis details: (-2.5 points) pts rule name description ---- ---------------------- -------------------------------------------------- -0.7 RCVD_IN_DNSWL_LOW RBL: Sender listed at http://www.dnswl.org/, low trust [2a00:1450:4010:c07:0:0:0:22f listed in] [list.dnswl.org] -0.0 SPF_PASS SPF: sender matches SPF record 0.2 FREEMAIL_ENVFROM_END_DIGIT Envelope-from freemail username ends in digit (zajec5[at]gmail.com) 0.0 FREEMAIL_FROM Sender email is commonly abused enduser mail provider (zajec5[at]gmail.com) -1.9 BAYES_00 BODY: Bayes spam probability is 0 to 1% [score: 0.0000] 0.1 DKIM_SIGNED Message has a DKIM or DK signature, not necessarily valid -0.1 DKIM_VALID Message has at least one valid DKIM or DK signature -0.1 DKIM_VALID_AU Message has a valid DKIM or DK signature from author's domain X-BeenThere: lede-dev@lists.infradead.org X-Mailman-Version: 2.1.20 Precedence: list List-Id: List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Cc: =?UTF-8?q?Rafa=C5=82=20Mi=C5=82ecki?= Sender: "Lede-dev" Errors-To: lede-dev-bounces+incoming=patchwork.ozlabs.org@lists.infradead.org So far fixtrx was calculating checksum over amount of data matching partition erase size. It was mostly a workaround of checksum problem after changing anything in initial TRX content (e.g. formatting JFFS2). Its main purpose was to make bootloader accept modified TRX. This didn't provide much protection of flash data against corruption. This new option lets caller request calculating checksum over a bigger amount of data. It may be used e.g. to include whole kernel data for checksum and hopefully make bootloader go info failsafe mode if something goes wrong. Signed-off-by: Rafał Miłecki --- package/system/mtd/src/mtd.c | 16 +++++++++++++--- package/system/mtd/src/mtd.h | 2 +- package/system/mtd/src/trx.c | 21 ++++++++++++--------- 3 files changed, 26 insertions(+), 13 deletions(-) diff --git a/package/system/mtd/src/mtd.c b/package/system/mtd/src/mtd.c index dae0514..f206c4f 100644 --- a/package/system/mtd/src/mtd.c +++ b/package/system/mtd/src/mtd.c @@ -737,6 +737,8 @@ static void usage(void) if (mtd_fixtrx) { fprintf(stderr, " -o offset offset of the image header in the partition(for fixtrx)\n"); + fprintf(stderr, + " -c datasize amount of data to be used for checksum calculation (for fixtrx)\n"); } fprintf(stderr, #ifdef FIS_SUPPORT @@ -769,7 +771,7 @@ int main (int argc, char **argv) int ch, i, boot, imagefd = 0, force, unlocked; char *erase[MAX_ARGS], *device = NULL; char *fis_layout = NULL; - size_t offset = 0, part_offset = 0, dump_len = 0; + size_t offset = 0, datasize = 0, part_offset = 0, dump_len = 0; enum { CMD_ERASE, CMD_WRITE, @@ -793,7 +795,7 @@ int main (int argc, char **argv) #ifdef FIS_SUPPORT "F:" #endif - "frnqe:d:s:j:p:o:l:")) != -1) + "frnqe:d:s:j:p:o:c:l:")) != -1) switch (ch) { case 'f': force = 1; @@ -853,6 +855,14 @@ int main (int argc, char **argv) usage(); } break; + case 'c': + errno = 0; + datasize = strtoul(optarg, 0, 0); + if (errno) { + fprintf(stderr, "-d: illegal numeric string\n"); + usage(); + } + break; #ifdef FIS_SUPPORT case 'F': fis_layout = optarg; @@ -967,7 +977,7 @@ int main (int argc, char **argv) break; case CMD_FIXTRX: if (mtd_fixtrx) { - mtd_fixtrx(device, offset); + mtd_fixtrx(device, offset, datasize); } case CMD_RESETBC: if (mtd_resetbc) { diff --git a/package/system/mtd/src/mtd.h b/package/system/mtd/src/mtd.h index 751b0d0..52074fc 100644 --- a/package/system/mtd/src/mtd.h +++ b/package/system/mtd/src/mtd.h @@ -25,7 +25,7 @@ extern void mtd_parse_jffs2data(const char *buf, const char *dir); /* target specific functions */ extern int trx_fixup(int fd, const char *name) __attribute__ ((weak)); extern int trx_check(int imagefd, const char *mtd, char *buf, int *len) __attribute__ ((weak)); -extern int mtd_fixtrx(const char *mtd, size_t offset) __attribute__ ((weak)); +extern int mtd_fixtrx(const char *mtd, size_t offset, size_t datasize) __attribute__ ((weak)); extern int mtd_fixseama(const char *mtd, size_t offset) __attribute__ ((weak)); extern int mtd_resetbc(const char *mtd) __attribute__ ((weak)); #endif /* __mtd_h */ diff --git a/package/system/mtd/src/trx.c b/package/system/mtd/src/trx.c index 00c4d6c..2a41e0f 100644 --- a/package/system/mtd/src/trx.c +++ b/package/system/mtd/src/trx.c @@ -148,7 +148,7 @@ trx_check(int imagefd, const char *mtd, char *buf, int *len) #endif int -mtd_fixtrx(const char *mtd, size_t offset) +mtd_fixtrx(const char *mtd, size_t offset, size_t datasize) { int fd; struct trx_header *trx; @@ -165,22 +165,25 @@ mtd_fixtrx(const char *mtd, size_t offset) exit(1); } + if (!datasize) + datasize = erasesize; + block_offset = offset & ~(erasesize - 1); offset -= block_offset; - if (block_offset + erasesize > mtdsize) { + if (block_offset + datasize > mtdsize) { fprintf(stderr, "Offset too large, device size 0x%x\n", mtdsize); exit(1); } - buf = malloc(erasesize); + buf = malloc(datasize); if (!buf) { perror("malloc"); exit(1); } - res = pread(fd, buf, erasesize, block_offset); - if (res != erasesize) { + res = pread(fd, buf, datasize, block_offset); + if (res != datasize) { perror("pread"); exit(1); } @@ -191,16 +194,16 @@ mtd_fixtrx(const char *mtd, size_t offset) exit(1); } - if (trx->len == STORE32_LE(erasesize - offset)) { + if (trx->len == STORE32_LE(datasize - offset)) { if (quiet < 2) fprintf(stderr, "Header already fixed, exiting\n"); close(fd); return 0; } - trx->len = STORE32_LE(erasesize - offset); + trx->len = STORE32_LE(datasize - offset); - trx->crc32 = STORE32_LE(crc32buf((char*) &trx->flag_version, erasesize - offset - 3*4)); + trx->crc32 = STORE32_LE(crc32buf((char*) &trx->flag_version, datasize - offset - 3*4)); if (mtd_erase_block(fd, block_offset)) { fprintf(stderr, "Can't erease block at 0x%x (%s)\n", block_offset, strerror(errno)); exit(1); @@ -209,7 +212,7 @@ mtd_fixtrx(const char *mtd, size_t offset) if (quiet < 2) fprintf(stderr, "New crc32: 0x%x, rewriting block\n", trx->crc32); - if (pwrite(fd, buf, erasesize, block_offset) != erasesize) { + if (pwrite(fd, buf, datasize, block_offset) != datasize) { fprintf(stderr, "Error writing block (%s)\n", strerror(errno)); exit(1); }