From patchwork Fri Mar 18 15:53:04 2022 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: 552620 Delivered-To: patch@linaro.org Received: by 2002:a05:7000:6713:0:0:0:0 with SMTP id u19csp2188364mag; Fri, 18 Mar 2022 08:54:23 -0700 (PDT) X-Google-Smtp-Source: ABdhPJwT3F4iGUHMXEyc21oEsoaeOVS+0K5AGeNTTywUAFa7t5UatKTxDXn7Wnb0urJCr1JpajC+ X-Received: by 2002:a05:6870:d112:b0:dd:a85c:3b32 with SMTP id e18-20020a056870d11200b000dda85c3b32mr3967483oac.60.1647618863213; Fri, 18 Mar 2022 08:54:23 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1647618863; cv=none; d=google.com; s=arc-20160816; b=T3W93IpTmD+0cCTLauMybwjH9XyBwcpOtlNynLf/i2zgPHyLjh6c0xfs5ZbkIgiPZT aV+4Wpbtv/e4PpjSRJb1Vj6ilf/ICEua31JJhfbHEO8H825YwI21E0ZiX8fdUOl6RAWE D3C/dhxWK6kKT5xWYn8plKGvu7GdVl4tDZD6hNeWor82j/bSr+Fl51sRUmoAw0Yi+n3A irgReuLlhd70bpMbE9qkXN6BJ2j3Uf8mvmqOTEwYLLVmTZu7JjncQuAS8f5bqfJokFzM g8v9dwzVzHPn5Grmbdp/pdB0pfPyTVsEoqyYYD3i3LCZwUp6nx0QglypQnzvYrQ5GnrM a5Qg== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=errors-to:sender:content-transfer-encoding:list-subscribe:list-help :list-post:list-archive:list-unsubscribe:list-id:precedence :mime-version:message-id:date:subject:cc:to:from:dkim-signature :dkim-signature; bh=zNN3j/TKcWyqgd2Yt0AXp1ghiFZP69NKxW6avVUnUeU=; b=HDH5nty744zqcAmVBMWAI0IdPIR4IgeN4hN5WFbibUUXv5uDH1j4Gm6Zxm9p4sBi2S PAdi570u19diFv8kkzuryNxGQPWmr1CS6cmXG38nuTGUTAZZxX4Ugj+JFef0FNA5PRQK mg/TogifOSyhAZzjixpSf5yD0L5YJYZ0UDxr8j80gf4nK/rfj5cdNXR5zQsrIN8DIsPs Bo3dzopxu3Dq2yuqkpfSscpkd3bNZnpiZSIJKRfKH6TlyjuB9ocYept5v5EMJjtOJurl MsIMkaTF4U/tKZgpLNJ4O/5XZCTi1NtEIc/wx5R/vFlQxdFXdrtyMLXDLib05AkK/Fm1 /Xpg== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@lists.infradead.org header.s=bombadil.20210309 header.b=cvV1pb6j; dkim=neutral (body hash did not verify) header.i=@gmail.com header.s=20210112 header.b=AzKXqqaN; spf=pass (google.com: best guess record for domain of linux-mtd-bounces+patch=linaro.org@lists.infradead.org designates 2607:7c80:54:e::133 as permitted sender) smtp.mailfrom="linux-mtd-bounces+patch=linaro.org@lists.infradead.org"; dmarc=fail (p=NONE sp=QUARANTINE dis=NONE) header.from=gmail.com Return-Path: Received: from bombadil.infradead.org (bombadil.infradead.org. [2607:7c80:54:e::133]) by mx.google.com with ESMTPS id z25-20020a056830129900b005c925150860si3708454otp.111.2022.03.18.08.54.23 for (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Fri, 18 Mar 2022 08:54:23 -0700 (PDT) Received-SPF: pass (google.com: best guess record for domain of linux-mtd-bounces+patch=linaro.org@lists.infradead.org designates 2607:7c80:54:e::133 as permitted sender) client-ip=2607:7c80:54:e::133; Authentication-Results: mx.google.com; dkim=pass header.i=@lists.infradead.org header.s=bombadil.20210309 header.b=cvV1pb6j; dkim=neutral (body hash did not verify) header.i=@gmail.com header.s=20210112 header.b=AzKXqqaN; spf=pass (google.com: best guess record for domain of linux-mtd-bounces+patch=linaro.org@lists.infradead.org designates 2607:7c80:54:e::133 as permitted sender) smtp.mailfrom="linux-mtd-bounces+patch=linaro.org@lists.infradead.org"; dmarc=fail (p=NONE sp=QUARANTINE dis=NONE) header.from=gmail.com DKIM-Signature: v=1; a=rsa-sha256; q=dns/txt; c=relaxed/relaxed; d=lists.infradead.org; s=bombadil.20210309; h=Sender: Content-Transfer-Encoding:Content-Type:List-Subscribe:List-Help:List-Post: List-Archive:List-Unsubscribe:List-Id:MIME-Version:Message-Id:Date:Subject:Cc :To:From:Reply-To:Content-ID:Content-Description:Resent-Date:Resent-From: Resent-Sender:Resent-To:Resent-Cc:Resent-Message-ID:In-Reply-To:References: List-Owner; bh=zNN3j/TKcWyqgd2Yt0AXp1ghiFZP69NKxW6avVUnUeU=; b=cvV1pb6j4zFf9Q TxSi/MrZlOIojPXKe/y35bmn3EfqciJLfGqXr2Ld+WwaF3WXqH+RcsVPdjn/S/nyGZU+mXMyYSiNA MJP1gBjZpb3TvldLczZhMpUIAWif8nX3Wa5+Y0F+Y1NW9ezlWrQotoKz0oQOkSfVjWC3ORlIpcrzq /a5UzEh56H+NxHZMP70LR3HIbunDOPplmMB4u9JxeKp2occsTf5QSEyJiFkKGxT6NpfYiUhzEmrKZ c3gKwSmoKaE150FPpGvA0zsf2Nn4sXL4HdLm6+Wcr0DRgPcThFA58qgh6p3VRkC2p9HTMzl7zEDS0 7j//Jc5DCFEcrYcRA6pg==; Received: from localhost ([::1] helo=bombadil.infradead.org) by bombadil.infradead.org with esmtp (Exim 4.94.2 #2 (Red Hat Linux)) id 1nVEun-002Jd9-My; Fri, 18 Mar 2022 15:53:21 +0000 Received: from mail-lj1-x22a.google.com ([2a00:1450:4864:20::22a]) by bombadil.infradead.org with esmtps (Exim 4.94.2 #2 (Red Hat Linux)) id 1nVEuk-002JcW-7K for linux-mtd@lists.infradead.org; Fri, 18 Mar 2022 15:53:20 +0000 Received: by mail-lj1-x22a.google.com with SMTP id r21so4624304ljp.8 for ; Fri, 18 Mar 2022 08:53:17 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20210112; h=from:to:cc:subject:date:message-id:mime-version :content-transfer-encoding; bh=kOpTv9lkGIFBP7PcQ+dEeu2k+vwLtZ1TaYm7R8svoP4=; b=AzKXqqaNiXsEsgD2uDia08mzqE7ZS7//xHv1NTY3BCH2my2GckJdnaeYTFGQbYYm/t swSiDLCy7D7nqlZTuarRqi/UcATmCkYjw6bi12owriDDX8eOE+TqSWikTNmAv03dS0un BgeJOA85jRqoP0YvsUvAOb5WTt3CDKlAyARN1xmtrE2PVfzP2bfCry0Uy1gtdijF0p5z pwfLIPnjv00j9nbxl50uFOuGj3jxFo3MeLRQGFjra8/BnfLw2n3jUVMqn1RjYXhTa1VR BfNp8NEFjxS/Bj/RHPQ/zHWL2ILdzrhxGmuy0+0uGU5fcsB3wulVFw/uCqtvwPNU6CZz 7VBQ== 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:mime-version :content-transfer-encoding; bh=kOpTv9lkGIFBP7PcQ+dEeu2k+vwLtZ1TaYm7R8svoP4=; b=dr4lJP2YQkANykIJzdwtyp72XlyG53k3uaBz1B5zQaVAIa73URHYGPG9fQg5qYbnrL x6YL8TUFEwb0GPJH9dF562ZjbbTQxxg2vOq3LQoSYpSlY1IoLiHaQJzQrv1Dy4O/0mdS /z4KIV+Fg+1cFEkuswg5lPRHh6nX9Y36mQlBKWF1q55eFVnLZdiQWYlls0aiKQWQStch EzYAgC5Dd4Cj1sUA0iq3jGnrMNJefacw0XhajVu0FUGejwVufoMIlyUSeeoSHVF8rhrF g4Me6jn9468zv7O0AKCKhSzL+nFWfAw6zgqYgxZyAKEkFap520lPzMmlVi1SKxlrlE9g VpVg== X-Gm-Message-State: AOAM531Ctt688AbZhPR024hz07yKajVTmQ8cTwsPHHAzdGte6rwd1qsZ EXDIy6HxgFpsds3ZH8BmolN/809LZmc= X-Received: by 2002:a2e:bf01:0:b0:247:dfe7:62dc with SMTP id c1-20020a2ebf01000000b00247dfe762dcmr6508421ljr.365.1647618794948; Fri, 18 Mar 2022 08:53:14 -0700 (PDT) Received: from localhost.lan (ip-194-187-74-233.konfederacka.maverick.com.pl. [194.187.74.233]) by smtp.gmail.com with ESMTPSA id bp19-20020a056512159300b00448b37c6f4csm899403lfb.12.2022.03.18.08.53.13 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Fri, 18 Mar 2022 08:53:14 -0700 (PDT) From: =?utf-8?b?UmFmYcWCIE1pxYJlY2tp?= To: Richard Weinberger Cc: linux-mtd@lists.infradead.org, =?utf-8?b?UmFmYcWCIE1pxYJlY2tp?= Subject: [PATCH] UBI: support EOF block marking end of ubinized image Date: Fri, 18 Mar 2022 16:53:04 +0100 Message-Id: <20220318155304.14916-1-zajec5@gmail.com> X-Mailer: git-send-email 2.34.1 MIME-Version: 1.0 X-CRM114-Version: 20100106-BlameMichelson ( TRE 0.8.0 (BSD) ) MR-646709E3 X-CRM114-CacheID: sfid-20220318_085318_290419_C60269E7 X-CRM114-Status: GOOD ( 16.56 ) X-Spam-Score: 0.1 (/) X-Spam-Report: =?utf-8?q?Spam_detection_software=2C_running_on_the_system_?= =?utf-8?q?=22bombadil=2Einfradead=2Eorg=22=2C?= =?utf-8?q?_has_NOT_identified_this_incoming_email_as_spam=2E__The_original?= =?utf-8?q?_message_has_been_attached_to_this_so_you_can_view_it_or_label?= =?utf-8?q?_similar_future_email=2E__If_you_have_any_questions=2C_see?= =?utf-8?q?_the_administrator_of_that_system_for_details=2E?= =?utf-8?q?_?= =?utf-8?q?_Content_preview=3A__From=3A_Rafa=C3=85=C2=82_Mi=C3=85=C2=82ecki_O?= =?utf-8?q?n_home_routers_UBI_is_often_used_with?= =?utf-8?q?_UBI_unaware_bootloaders=2E_In_such_case_an_ubinized_image_needs_t?= =?utf-8?q?o_be_created?= =?utf-8?q?_=26_flashed=2E_The_problem_is_many_such_bootloaders_are_poorly_wr?= =?utf-8?q?itten_and_they?= =?utf-8?q?_don=27t_erase_whole_flash_=28partition=29_before_writing_an_image?= =?utf-8?q?=2E_Only_part_of?= =?utf-8?q?_flash_required_to_fit_new_image_gets_erased=2E_It_results_in_UB_?= =?utf-8?q?=5B=2E=2E=2E=5D_?= =?utf-8?q?_?= =?utf-8?q?_Content_analysis_details=3A___=280=2E1_points=2C_5=2E0_required?= =?utf-8?q?=29?= =?utf-8?q?_?= =?utf-8?q?_pts_rule_name______________description?= =?utf-8?q?_----_----------------------_-------------------------------------?= =?utf-8?q?-------------?= =?utf-8?q?_-0=2E0_SPF=5FPASS_______________SPF=3A_sender_matches_SPF_record?= =?utf-8?q?_0=2E0_SPF=5FHELO=5FNONE__________SPF=3A_HELO_does_not_publish_an_?= =?utf-8?q?SPF_Record?= =?utf-8?q?_0=2E0_FREEMAIL=5FFROM__________Sender_email_is_commonly_abused_en?= =?utf-8?q?duser_mail?= =?utf-8?q?_provider?= =?utf-8?q?_=5Bzajec5=5Bat=5Dgmail=2Ecom=5D?= =?utf-8?q?_0=2E2_FREEMAIL=5FENVFROM=5FEND=5FDIGIT_Envelope-from_freemail_use?= =?utf-8?q?rname_ends?= =?utf-8?q?_in_digit?= =?utf-8?q?_=5Bzajec5=5Bat=5Dgmail=2Ecom=5D?= =?utf-8?q?_-0=2E0_RCVD=5FIN=5FDNSWL=5FNONE_____RBL=3A_Sender_listed_at_https?= =?utf-8?q?=3A//www=2Ednswl=2Eorg/=2C?= =?utf-8?q?_no_trust?= =?utf-8?b?IFsyYTAwOjE0NTA6NDg2NDoyMDowOjA6MDoyMmEgbGlzdGVkIGluXQ==?= =?utf-8?b?IFtsaXN0LmRuc3dsLm9yZ10=?= =?utf-8?q?_-0=2E1_DKIM=5FVALID=5FAU__________Message_has_a_valid_DKIM_or_DK_?= =?utf-8?q?signature_from?= =?utf-8?q?_author=27s_domain?= =?utf-8?q?_-0=2E1_DKIM=5FVALID_____________Message_has_at_least_one_valid_DK?= =?utf-8?q?IM_or_DK_signature?= =?utf-8?q?_0=2E1_DKIM=5FSIGNED____________Message_has_a_DKIM_or_DK_signature?= =?utf-8?q?=2C_not_necessarily?= =?utf-8?q?_valid?= =?utf-8?q?_-0=2E1_DKIM=5FVALID=5FEF__________Message_has_a_valid_DKIM_or_DK_?= =?utf-8?q?signature_from?= =?utf-8?q?_envelope-from_domain?= X-BeenThere: linux-mtd@lists.infradead.org X-Mailman-Version: 2.1.34 Precedence: list List-Id: Linux MTD discussion mailing list List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Sender: "linux-mtd" Errors-To: linux-mtd-bounces+patch=linaro.org@lists.infradead.org From: Rafał Miłecki On home routers UBI is often used with UBI unaware bootloaders. In such case an ubinized image needs to be created & flashed. The problem is many such bootloaders are poorly written and they don't erase whole flash (partition) before writing an image. Only part of flash required to fit new image gets erased. It results in UBI complaining about flash garbage content, e.g.: ubi0: attaching mtd2 ubi0 error: ubi_attach: bad image sequence number 1646161998 in PEB 119, expected 524503983 Erase counter header dump: magic 0x55424923 version 1 ec 1 vid_hdr_offset 2048 data_offset 4096 image_seq 1646161998 hdr_crc 0x1eb28994 erase counter header hexdump: ubi0 error: ubi_attach_mtd_dev: failed to attach mtd2, error -22 UBI error: cannot attach mtd2 To fix up such flashed images it's required to identify the end of ubinized image on flash and erase remaining blocks. Idea of extending on-disk UBI format to mark end of ubinized image was rejected, see: [PATCH RFC 2/2] ubi: add support for UBI_EC_FLAG_ERASE_FROM_HERE https://patchwork.ozlabs.org/project/linux-mtd/patch/20161230171151.13448-2-zajec5@gmail.com/ This patch implements support for a simple "EOF" text content (block) appended to the ubinized image instead. It doesn't require changing on-disk format and still allows simple integration into ubi code. Signed-off-by: Rafał Miłecki --- drivers/mtd/ubi/attach.c | 19 +++++++++++++++++++ drivers/mtd/ubi/ubi.h | 1 + 2 files changed, 20 insertions(+) diff --git a/drivers/mtd/ubi/attach.c b/drivers/mtd/ubi/attach.c index ae5abe492b52..8bea4bbcfbd9 100644 --- a/drivers/mtd/ubi/attach.c +++ b/drivers/mtd/ubi/attach.c @@ -958,9 +958,28 @@ static int scan_peb(struct ubi_device *ubi, struct ubi_attach_info *ai, return 0; } + if (ai->eof_found) { + ai->empty_peb_count += 1; + return add_to_list(ai, pnum, UBI_UNKNOWN, UBI_UNKNOWN, + UBI_UNKNOWN, 0, &ai->erase); + } + err = ubi_io_read_ec_hdr(ubi, pnum, ech, 0); if (err < 0) return err; + + if (err == UBI_IO_BAD_HDR) { + uint8_t eof[] = { 'E', 'O', 'F' }; + uint8_t *data = (uint8_t *)ech; + + if (!memcmp(data, eof, sizeof(eof))) { + ai->eof_found = true; + ai->empty_peb_count += 1; + return add_to_list(ai, pnum, UBI_UNKNOWN, UBI_UNKNOWN, + UBI_UNKNOWN, 0, &ai->erase); + } + } + switch (err) { case 0: break; diff --git a/drivers/mtd/ubi/ubi.h b/drivers/mtd/ubi/ubi.h index 7c083ad58274..218c7dfedfc0 100644 --- a/drivers/mtd/ubi/ubi.h +++ b/drivers/mtd/ubi/ubi.h @@ -783,6 +783,7 @@ struct ubi_attach_info { struct kmem_cache *aeb_slab_cache; struct ubi_ec_hdr *ech; struct ubi_vid_io_buf *vidb; + bool eof_found; }; /**