From patchwork Wed Mar 4 19:41:31 2015 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: namnguyen@chromium.org X-Patchwork-Id: 446405 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-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by ozlabs.org (Postfix) with ESMTPS id 38C38140187 for ; Thu, 5 Mar 2015 06:54:25 +1100 (AEDT) Authentication-Results: ozlabs.org; dkim=fail reason="verification failed; unprotected key" header.d=chromium.org header.i=@chromium.org header.b=MWej8mKF; dkim-adsp=none (unprotected policy); 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 1YTFKc-0004xu-0N; Wed, 04 Mar 2015 19:51:46 +0000 Received: from mail-ig0-x234.google.com ([2607:f8b0:4001:c05::234]) by bombadil.infradead.org with esmtps (Exim 4.80.1 #2 (Red Hat Linux)) id 1YTFBa-0004Cn-PE for linux-mtd@lists.infradead.org; Wed, 04 Mar 2015 19:42:28 +0000 Received: by igbhl2 with SMTP id hl2so39665234igb.5 for ; Wed, 04 Mar 2015 11:42:02 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=chromium.org; s=google; h=from:to:cc:subject:date:message-id; bh=HlxS++783Ef/Ya3bKLe59hqQnv3w3s8ymCYxHwJp1N0=; b=MWej8mKFhi52OzyVQlMLRe0ZsrYYnmXxSSaMuiBqw+5trjznpSPs/5NlmanbIItHej spAfh8m98HMhbLvwQWmeUrU34um/YV/ntNE42Dq1FOFzrYTUGrpMco+lO2WUF0CS37jy X/n7TYbqvN720GtxzVJFAL1uOgzhJI8EN9bPM= 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; bh=HlxS++783Ef/Ya3bKLe59hqQnv3w3s8ymCYxHwJp1N0=; b=VMVaTS4JZ4bml7XNAQFPVgLhVXE8UKrYl9KQJYeisAzD5G+itV0q92Lgd7R5Zc72+f baDz/SlJj/jpdDRWVQkRm/XvXnwa82t91+JVnGwMWybJDZnhrv1AKJv/Q2O58VDhOeLV BBynrNi/2DcF2kKMOd2TPEwS/GsMoZB068L/j+W8Ap2MIDSBVXAGWBSbQwCM8/K+1Ko4 x1hKQU6U9LUd9daWaAr/uCnJSBdmq5NSFxzFJvGJP/C33NnjLxDobBC80TZYpQv2av9E rMrJ34aXtExQZgHoyoW97mtmPayrdp0vfLJFTm69mOj4jDziaFw69uqGZYD3h0Yd/TAN 1H7A== X-Gm-Message-State: ALoCoQkbarD+81fmt8POMNpZJd33dJJyv9PqdqPXdfQGZcrojsSA1JNmoYYVDlccWgsEFo82y2mr X-Received: by 10.50.66.235 with SMTP id i11mr14657046igt.40.1425498122410; Wed, 04 Mar 2015 11:42:02 -0800 (PST) Received: from namnguyen.mtv.corp.google.com ([172.22.64.167]) by mx.google.com with ESMTPSA id sd7sm11254718igb.20.2015.03.04.11.42.00 (version=TLSv1.2 cipher=ECDHE-RSA-AES128-SHA bits=128/128); Wed, 04 Mar 2015 11:42:01 -0800 (PST) From: "Nam T. Nguyen" To: linux-mtd@lists.infradead.org Subject: [PATCH v3] mtd-utils: Add mtdpart to add/delete partition Date: Wed, 4 Mar 2015 11:41:31 -0800 Message-Id: <1425498091-3505-1-git-send-email-namnguyen@chromium.org> X-Mailer: git-send-email 2.2.0.rc0.207.ga3a616c X-CRM114-Version: 20100106-BlameMichelson ( TRE 0.8.0 (BSD) ) MR-646709E3 X-CRM114-CacheID: sfid-20150304_114226_925927_F257BCA6 X-CRM114-Status: GOOD ( 16.07 ) X-Spam-Score: -0.8 (/) X-Spam-Report: SpamAssassin version 3.4.0 on bombadil.infradead.org summary: Content analysis details: (-0.8 points) pts rule name description ---- ---------------------- -------------------------------------------------- -0.7 RCVD_IN_DNSWL_LOW RBL: Sender listed at http://www.dnswl.org/, low trust [2607:f8b0:4001:c05:0:0:0:234 listed in] [list.dnswl.org] -0.0 SPF_PASS SPF: sender matches SPF record -0.1 DKIM_VALID_AU Message has a valid DKIM or DK signature from author's domain -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 Cc: "Nam T. Nguyen" , vapier@chromium.org, computersforpeace@gmail.com, dehrenberg@chromium.org X-BeenThere: linux-mtd@lists.infradead.org X-Mailman-Version: 2.1.18-1 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 Add a simple utility to exercise BLKPG ioctl. Signed-off-by: Nam T. Nguyen --- Changes from v2: o Fix coding style o Remove cleanup code since we exit()ing anyway. Makefile | 2 +- mtdpart.c | 187 ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 188 insertions(+), 1 deletion(-) create mode 100644 mtdpart.c diff --git a/Makefile b/Makefile index eade234..f4ce313 100644 --- a/Makefile +++ b/Makefile @@ -20,7 +20,7 @@ MTD_BINS = \ ftl_format flash_erase nanddump doc_loadbios \ ftl_check mkfs.jffs2 flash_lock flash_unlock \ flash_otp_info flash_otp_dump flash_otp_lock flash_otp_write \ - mtd_debug flashcp nandwrite nandtest \ + mtd_debug flashcp nandwrite nandtest mtdpart \ jffs2dump \ nftldump nftl_format docfdisk \ rfddump rfdformat \ diff --git a/mtdpart.c b/mtdpart.c new file mode 100644 index 0000000..b944f49 --- /dev/null +++ b/mtdpart.c @@ -0,0 +1,187 @@ +/* + * mtdpart.c + * + * Copyright 2015 The Chromium OS Authors. + * + * This program is free software; you can redistribute it and/or modify + * it under the terms of the GNU General Public License version 2 as + * published by the Free Software Foundation. + * + * Overview: + * This utility adds or removes a partition from an MTD device. + */ + +#define PROGRAM_NAME "mtdpart" + +#include +#include +#include +#include +#include +#include +#include +#include +#include + +#include "common.h" + +static void display_help(int status) +{ + fprintf(status == EXIT_SUCCESS ? stdout : stderr, +"Usage: %1$s add [OPTION] \n" +" %1$s del [OPTION] \n" +"Adds a partition to an MTD device, or remove an existing partition from it.\n" +"\n" +" -h, --help Display this help and exit\n" +" --version Output version information and exit\n" +"\n" +"START location and SIZE of the partition are in bytes. They should align on\n" +"eraseblock size.\n", + PROGRAM_NAME + ); + exit(status); +} + +static void display_version(void) +{ + printf("%1$s " VERSION "\n" + "\n" + "%1$s comes with NO WARRANTY\n" + "to the extent permitted by law.\n" + "\n" + "You may redistribute copies of %1$s\n" + "under the terms of the GNU General Public Licence.\n" + "See the file `COPYING' for more information.\n", + PROGRAM_NAME); + exit(EXIT_SUCCESS); +} + +/* Command arguments */ + +typedef enum { + COMMAND_ADD, + COMMAND_DEL +} command_type; + +static command_type command; /* add or del */ +static const char *mtddev; /* mtd device name */ +static const char *part_name; /* partition name */ +static int part_no; /* partition number */ +static long long start_addr; /* start address */ +static long long length; /* partition size */ + +static void process_options(int argc, char * const argv[]) +{ + int error = 0; + + for (;;) { + int option_index = 0; + static const char short_options[] = "h"; + static const struct option long_options[] = { + {"version", no_argument, 0, 0}, + {"help", no_argument, 0, 'h'}, + {0, 0, 0, 0}, + }; + + int c = getopt_long(argc, argv, short_options, + long_options, &option_index); + if (c == EOF) { + break; + } + + switch (c) { + case 0: + display_version(); + break; + case 'h': + display_help(EXIT_SUCCESS); + break; + case '?': + error++; + break; + } + } + + if ((argc - optind) < 3 || error) + display_help(EXIT_FAILURE); + + const char *s_command = argv[optind++]; + mtddev = argv[optind++]; + + if (strcmp(s_command, "del") == 0 && (argc - optind) == 1) { + const char *s_part_no = argv[optind++]; + + part_no = simple_strtol(s_part_no, &error); + if (part_no < 0) { + errmsg_die("Can't specify negative partition number: %d", + part_no); + } + + command = COMMAND_DEL; + } else if (strcmp(s_command, "add") == 0 && (argc - optind) == 3) { + part_name = argv[optind++]; + const char *s_start = argv[optind++]; + const char *s_length = argv[optind++]; + + if (strlen(part_name) >= BLKPG_DEVNAMELTH) + errmsg_die("Partition name (%s) should be less than %d characters" + part_name, BLKPG_DEVNAMELTH); + + start_addr = simple_strtoll(s_start, &error); + if (start_addr < 0) + errmsg_die("Can't specify negative start offset: %lld", + start_addr); + + length = simple_strtoll(s_length, &error); + if (length < 0) + errmsg_die("Can't specify negative length: %lld", + length); + + command = COMMAND_ADD; + } else + display_help(EXIT_FAILURE); + + if (error) + display_help(EXIT_FAILURE); +} + + +int main(int argc, char * const argv[]) +{ + int fd; + struct blkpg_partition part; + struct blkpg_ioctl_arg arg; + + process_options(argc, argv); + + fd = open(mtddev, O_RDWR | O_CLOEXEC); + if (fd == -1) + sys_errmsg_die("Cannot open %s", mtddev); + + memset(&part, 0, sizeof(part)); + + memset(&arg, 0, sizeof(arg)); + arg.datalen = sizeof(part); + arg.data = ∂ + + switch (command) { + case COMMAND_ADD: + part.start = start_addr; + part.length = length; + strncpy(part.devname, part_name, sizeof(part.devname)); + arg.op = BLKPG_ADD_PARTITION; + break; + case COMMAND_DEL: + part.pno = part_no; + arg.op = BLKPG_DEL_PARTITION; + break; + } + + if (ioctl(fd, BLKPG, &arg)) + sys_errmsg_die("Failed to issue BLKPG ioctl"); + + close(fd); + + /* Exit happy */ + return EXIT_SUCCESS; +}