From patchwork Tue Jun 1 07:34:53 2021 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Harvey Wu X-Patchwork-Id: 1485905 X-Patchwork-Delegate: david.oberhollenzer@sigma-star.at Return-Path: X-Original-To: incoming@patchwork.ozlabs.org Delivered-To: patchwork-incoming@bilbo.ozlabs.org Authentication-Results: ozlabs.org; spf=none (no SPF record) smtp.mailfrom=lists.infradead.org (client-ip=2607:7c80:54:e::133; helo=bombadil.infradead.org; envelope-from=linux-mtd-bounces+incoming=patchwork.ozlabs.org@lists.infradead.org; receiver=) Authentication-Results: ozlabs.org; dkim=pass (2048-bit key; secure) header.d=lists.infradead.org header.i=@lists.infradead.org header.a=rsa-sha256 header.s=bombadil.20210309 header.b=4nLiXvq0; dkim=fail reason="signature verification failed" (2048-bit key; unprotected) header.d=gmail.com header.i=@gmail.com header.a=rsa-sha256 header.s=20161025 header.b=FrHRsHFY; dkim-atps=neutral Received: from bombadil.infradead.org (bombadil.infradead.org [IPv6:2607:7c80:54:e::133]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange X25519 server-signature RSA-PSS (4096 bits) server-digest SHA256) (No client certificate requested) by ozlabs.org (Postfix) with ESMTPS id 4FvP9l5PHSz9sSn for ; Tue, 1 Jun 2021 17:36:43 +1000 (AEST) 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:MIME-Version:List-Subscribe:List-Help: List-Post:List-Archive:List-Unsubscribe:List-Id: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=bjGoe3uJVbN9iO0cpxZ0azHHhIrYeDWp64/5E+ChSvg=; b=4nLiXvq0BAPnS3 yVyQ/W/tKNhlpVa7KXpf6r2RbJbw8Vs72C+1WuOgJmGHG4DqkD91JBdgd5p8QhvhGlcHqr2mcMBzb nK+m6tmYGSlFRqVQP/FkltW5OtMV4pPJESvAw2O2DLaQ7ik5fURmUJcBBlgHV8Jpg0UYN7cuLKHb8 mvixfA39aiUfEgUW94jhSKE+GOqEjnnJaFXc/58dfX5vM/keZ//lneWRjcgsW5DJ7esUrFaxtFlgw +OIV5F746kHe33LNdYOeXlu5ByRMh+jNKezjbr4e3ZVdaoJhDqwTTGJPcIIdkVbaiij6gxvtVeR2Y JJa4T4lNrtBeHs5SmHBw==; Received: from localhost ([::1] helo=bombadil.infradead.org) by bombadil.infradead.org with esmtp (Exim 4.94.2 #2 (Red Hat Linux)) id 1lnywY-00FAR6-FX; Tue, 01 Jun 2021 07:36:06 +0000 Received: from mail-pl1-x629.google.com ([2607:f8b0:4864:20::629]) by bombadil.infradead.org with esmtps (Exim 4.94.2 #2 (Red Hat Linux)) id 1lnywU-00FAQO-HO for linux-mtd@lists.infradead.org; Tue, 01 Jun 2021 07:36:04 +0000 Received: by mail-pl1-x629.google.com with SMTP id x10so5486380plg.3 for ; Tue, 01 Jun 2021 00:36:01 -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; bh=PlEp80zz59Htslj8ZEa/0YU3v6/zPl7KR0xZI0weEsM=; b=FrHRsHFYFMlZMFGwKCyYacgJzHZoHYbYt21qesl2w2GcmvRbUNGi5QK1HucfZPVoeh 0oXJRtVJyO8AgI7rTsUQ8AjYJyK9HzB67vGWUoLCsXjG5BsElP+VyNCNMsVDY6MM6sQS OFH5MoGXen02mgcQwbPB3swL3+szLDNl8q8FbNVIPOW13fh4TFkJ8Q5nJ6G2zi/7igSK YiFKneGdFkvmnqfMOOcvMD4qE1Q3gx6/cnDi7mBQopvfQpcP7T44nLuhS/KByVpwuIni kxtEdW8yV/vUUOUdO9UWVs+GkXWCGThOitdhBLEzvNx7VOTaSh2G3S91tGTZfueNIj5B +axQ== 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; bh=PlEp80zz59Htslj8ZEa/0YU3v6/zPl7KR0xZI0weEsM=; b=afG8iph+THrd65hBO6UxuEyO9Rx7DHPHOv9Y2pUMoHEYtIYAGBi1P6Ze5nbWpWRYKs bhMUL28u9QsJ/Pth7vSY5I2UTWrEwX1sJ7KtFDOQFhlJavtMNdGk0A0aUSe2EonkyuHG r5oTiKJIBF9lDL2GnjxlGVzTyAtblT4xmJL2tmb8PDMiKXI4tIRSRrL2J6nteT4iopBV inJe8qRmTKzva0RUr2eBeh/3cA0ejM8gcm74D1CMHahJ8kf2QE3gTS4WwYtKkt3yncno 5Uh9sAlB9EeafzHoevbgjyYYjj5/y1wMC0oe5BainQkKRQ5GAE4icrPTsBi+HxcnRPbb 6tNg== X-Gm-Message-State: AOAM530lw9coM/zmcg36+dXdGP9AxLMVZaKLnhoZBTxcXj/hvZxGf5yG fpq2UYglRYVWiaD+I/l9cmdDONmUnDdLNA== X-Google-Smtp-Source: ABdhPJxT6miczoWt6q4P1jLBK2UtCvc+2kfr8UsOI/bQORHIwG0mOeKQ7K25nSI8BGuWmyxJb717Xw== X-Received: by 2002:a17:90a:ad8e:: with SMTP id s14mr12182712pjq.198.1622532960848; Tue, 01 Jun 2021 00:36:00 -0700 (PDT) Received: from harvey-System-Product-Name.dhcpserver.local (220-135-135-179.HINET-IP.hinet.net. [220.135.135.179]) by smtp.gmail.com with ESMTPSA id b23sm12424970pfi.34.2021.06.01.00.35.59 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Tue, 01 Jun 2021 00:36:00 -0700 (PDT) From: Harvey Wu To: linux-mtd@lists.infradead.org Cc: Harvey.Wu@quantatw.com Subject: [PATCH] misc-utils: flashcp: Add new function that copy only different blocks Date: Tue, 1 Jun 2021 15:34:53 +0800 Message-Id: <20210601073453.28252-1-harveywu95@gmail.com> X-Mailer: git-send-email 2.17.1 X-CRM114-Version: 20100106-BlameMichelson ( TRE 0.8.0 (BSD) ) MR-646709E3 X-CRM114-CacheID: sfid-20210601_003602_617746_E3FEADCE X-CRM114-Status: GOOD ( 19.17 ) X-Spam-Score: 0.1 (/) X-Spam-Report: Spam detection software, running on the system "bombadil.infradead.org", has NOT identified this incoming email as spam. The original message has been attached to this so you can view it or label similar future email. If you have any questions, see the administrator of that system for details. Content preview: - The original flashcp process is erase, write and verify all blocks in one time from file to device. This patch will add a function that only copy different block data from file to device. The functi [...] Content analysis details: (0.1 points, 5.0 required) pts rule name description ---- ---------------------- -------------------------------------------------- -0.0 RCVD_IN_DNSWL_NONE RBL: Sender listed at https://www.dnswl.org/, no trust [2607:f8b0:4864:20:0:0:0:629 listed in] [list.dnswl.org] 0.0 SPF_HELO_NONE SPF: HELO does not publish an SPF Record 0.2 FREEMAIL_ENVFROM_END_DIGIT Envelope-from freemail username ends in digit [harveywu95[at]gmail.com] -0.0 SPF_PASS SPF: sender matches SPF record 0.0 FREEMAIL_FROM Sender email is commonly abused enduser mail provider [harveywu95[at]gmail.com] -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 0.1 DKIM_SIGNED Message has a DKIM or DK signature, not necessarily valid -0.1 DKIM_VALID_EF Message has a valid DKIM or DK signature from 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: , MIME-Version: 1.0 Sender: "linux-mtd" Errors-To: linux-mtd-bounces+incoming=patchwork.ozlabs.org@lists.infradead.org - The original flashcp process is erase, write and verify all blocks in one time from file to device. This patch will add a function that only copy different block data from file to device. The function will compare blocks by block between file and device, then erase and write block data from file to device if found different block. Signed-off-by: Harvey Wu --- misc-utils/flashcp.c | 93 +++++++++++++++++++++++++++++++++++++++++++- 1 file changed, 92 insertions(+), 1 deletion(-) diff --git a/misc-utils/flashcp.c b/misc-utils/flashcp.c index d7b0a59..341c210 100644 --- a/misc-utils/flashcp.c +++ b/misc-utils/flashcp.c @@ -67,6 +67,7 @@ #define FLAG_FILENAME 0x04 #define FLAG_DEVICE 0x08 #define FLAG_ERASE_ALL 0x10 +#define FLAG_PARTITION 0x20 /* error levels */ #define LOG_NORMAL 1 @@ -96,6 +97,7 @@ static NORETURN void showusage(bool error) "\n" " -h | --help Show this help message\n" " -v | --verbose Show progress reports\n" + " -p | --partition Only copy different block from file to device\n" " -A | --erase-all Erases the whole device regardless of the image size\n" " -V | --version Show version information and exit\n" " File which you want to copy to flash\n" @@ -181,10 +183,11 @@ int main (int argc,char *argv[]) for (;;) { int option_index = 0; - static const char *short_options = "hvAV"; + static const char *short_options = "hvpAV"; static const struct option long_options[] = { {"help", no_argument, 0, 'h'}, {"verbose", no_argument, 0, 'v'}, + {"partition", no_argument, 0, 'p'}, {"erase-all", no_argument, 0, 'A'}, {"version", no_argument, 0, 'V'}, {0, 0, 0, 0}, @@ -205,6 +208,10 @@ int main (int argc,char *argv[]) flags |= FLAG_VERBOSE; DEBUG("Got FLAG_VERBOSE\n"); break; + case 'p': + flags |= FLAG_PARTITION; + DEBUG("Got FLAG_PARTITION"); + break; case 'A': flags |= FLAG_ERASE_ALL; DEBUG("Got FLAG_ERASE_ALL\n"); @@ -257,6 +264,12 @@ int main (int argc,char *argv[]) exit (EXIT_FAILURE); } + /* diff block flashcp */ + if (flags & FLAG_PARTITION) + { + goto DIFF_BLOCKS; + } + /***************************************************** * erase enough blocks so that we can write the file * *****************************************************/ @@ -403,4 +416,82 @@ int main (int argc,char *argv[]) DEBUG("Verified %d / %lluk bytes\n",written,(unsigned long long)filestat.st_size); exit (EXIT_SUCCESS); + + /********************************************* + * Copy different blocks from file to device * + ********************************************/ +DIFF_BLOCKS: + safe_rewind (fil_fd,filename); + safe_rewind (dev_fd,device); + size = filestat.st_size; + i = mtd.erasesize; + erase.start = 0; + erase.length = (filestat.st_size + mtd.erasesize - 1) / mtd.erasesize; + erase.length *= mtd.erasesize; + written = 0; + unsigned long current_dev_block = 0; + int diffBlock = 0; + int blocks = erase.length / mtd.erasesize; + erase.length = mtd.erasesize; + + if (flags & FLAG_VERBOSE) + log_printf (LOG_NORMAL, + "\rProcessing blocks: 0/%d (%d%%)", blocks, PERCENTAGE (0,blocks)); + for (int s = 1; s <= blocks; s++) + { + if (size < mtd.erasesize) i = size; + if (flags & FLAG_VERBOSE) + log_printf (LOG_NORMAL, + "\rProcessing blocks: %d/%d (%d%%)", s, blocks, PERCENTAGE (s,blocks)); + + /* read from filename */ + safe_read (fil_fd,filename,src,i,flags & FLAG_VERBOSE); + + /* read from device */ + current_dev_block = lseek(dev_fd, 0, SEEK_CUR); + safe_read (dev_fd,device,dest,i,flags & FLAG_VERBOSE); + + /* compare buffers, if not the same, erase and write the block */ + if (memcmp (src,dest,i)) + { + diffBlock++; + /* erase block */ + lseek(dev_fd, current_dev_block, SEEK_SET); + if (ioctl (dev_fd,MEMERASE,&erase) < 0) + { + log_printf (LOG_NORMAL,"\n"); + log_printf (LOG_ERROR, + "While erasing blocks 0x%.8x-0x%.8x on %s: %m\n", + (unsigned int) erase.start,(unsigned int) (erase.start + erase.length),device); + exit (EXIT_FAILURE); + } + + /* write to device */ + lseek(dev_fd, current_dev_block, SEEK_SET); + result = write (dev_fd,src,i); + if (i != result) + { + if (flags & FLAG_VERBOSE) log_printf (LOG_NORMAL,"\n"); + if (result < 0) + { + log_printf (LOG_ERROR, + "While writing data to 0x%.8lx-0x%.8lx on %s: %m\n", + written,written + i,device); + exit (EXIT_FAILURE); + } + log_printf (LOG_ERROR, + "Short write count returned while writing to x%.8zx-0x%.8zx on %s: %zu/%llu bytes written to flash\n", + written,written + i,device,written + result,(unsigned long long)filestat.st_size); + exit (EXIT_FAILURE); + } + } + + erase.start += i; + written += i; + size -= i; + } + + if (flags & FLAG_VERBOSE) log_printf (LOG_NORMAL, "\ndiff blocks: %d\n", diffBlock); + + exit (EXIT_SUCCESS); }