9
0
Fork 0
barebox/commands/partition.c

240 lines
5.5 KiB
C
Raw Normal View History

2007-07-05 16:02:19 +00:00
/*
* partition.c - parse a linux-like mtd partition definition and register
* the new partitions
*
* Copyright (c) 2007 Sascha Hauer <s.hauer@pengutronix.de>, Pengutronix
*
* See file CREDITS for list of people who contributed to this
* project.
*
* 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.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
*/
/**
* @file
* @brief partition handling and addpart and delpart command
*/
2007-07-05 16:02:13 +00:00
#include <common.h>
#include <command.h>
#include <complete.h>
2007-07-05 16:02:13 +00:00
#include <driver.h>
#include <malloc.h>
#include <partition.h>
#include <errno.h>
#include <xfuncs.h>
#include <fs.h>
#include <linux/stat.h>
#include <libgen.h>
#include <getopt.h>
#include <linux/err.h>
2007-07-05 16:02:13 +00:00
#define SIZE_REMAINING ((ulong)-1)
#define PART_ADD_DEVNAME (1 << 0)
static int mtd_part_do_parse_one(char *devname, const char *partstr,
char **endp, loff_t *offset,
loff_t devsize, size_t *retsize,
unsigned int pflags)
2007-07-05 16:02:13 +00:00
{
loff_t size;
2007-07-14 13:20:43 +00:00
char *end;
char buf[PATH_MAX] = {};
unsigned long flags = 0;
int ret = 0;
struct cdev *cdev;
2007-07-05 16:02:13 +00:00
memset(buf, 0, PATH_MAX);
2007-07-05 16:02:13 +00:00
if (*partstr == '-') {
size = SIZE_REMAINING;
end = (char *)partstr + 1;
2007-07-14 13:20:43 +00:00
} else {
size = strtoull_suffix(partstr, &end, 0);
2007-07-05 16:02:13 +00:00
}
if (*end == '@')
*offset = strtoull_suffix(end+1, &end, 0);
if (size == SIZE_REMAINING)
size = devsize - *offset;
partstr = end;
2007-07-05 16:02:13 +00:00
if (*partstr == '(') {
partstr++;
end = strchr((char *) partstr, ')');
if (!end) {
2007-07-14 13:20:43 +00:00
printf("could not find matching ')'\n");
return -EINVAL;
}
if (pflags & PART_ADD_DEVNAME)
sprintf(buf, "%s.", devname);
memcpy(buf + strlen(buf), partstr, end - partstr);
end++;
2007-07-14 13:20:43 +00:00
}
if (size + *offset > devsize) {
printf("%s: partition end is beyond device\n", buf);
return -EINVAL;
}
partstr = end;
if (*partstr == 'r' && *(partstr + 1) == 'o') {
flags |= DEVFS_PARTITION_READONLY;
end = (char *)(partstr + 2);
2007-07-14 13:20:43 +00:00
}
if (endp)
*endp = end;
*retsize = size;
2007-07-14 13:20:43 +00:00
cdev = devfs_add_partition(devname, *offset, size, flags, buf);
if (IS_ERR(cdev)) {
ret = PTR_ERR(cdev);
printf("cannot create %s: %s\n", buf, strerror(-ret));
}
return ret;
2007-07-05 16:02:13 +00:00
}
static int do_addpart(int argc, char *argv[])
2007-07-05 16:02:13 +00:00
{
char *devname;
2007-07-14 13:20:43 +00:00
char *endp;
loff_t offset = 0;
loff_t devsize;
struct stat s;
int opt;
unsigned int flags = PART_ADD_DEVNAME;
while ((opt = getopt(argc, argv, "n")) > 0) {
switch (opt) {
case 'n':
flags &= ~PART_ADD_DEVNAME;
break;
}
}
2007-07-14 13:20:43 +00:00
if (argc != optind + 2)
return COMMAND_ERROR_USAGE;
2007-07-05 16:02:13 +00:00
if (stat(argv[optind], &s)) {
perror("addpart");
2007-07-14 13:20:43 +00:00
return 1;
}
devsize = s.st_size;
2007-07-05 16:02:13 +00:00
devname = basename(argv[optind]);
2007-07-05 16:02:13 +00:00
endp = argv[optind + 1];
2007-07-14 13:20:43 +00:00
while (1) {
size_t size = 0;
2007-07-05 16:02:13 +00:00
if (mtd_part_do_parse_one(devname, endp, &endp, &offset,
devsize, &size, flags))
return 1;
2007-07-05 16:02:13 +00:00
offset += size;
2007-07-05 16:02:13 +00:00
2007-07-14 13:20:43 +00:00
if (!*endp)
break;
2007-07-14 13:20:43 +00:00
if (*endp != ',') {
printf("parse error\n");
return 1;
2007-07-14 13:20:43 +00:00
}
endp++;
}
2007-07-05 16:02:13 +00:00
2007-07-14 13:20:43 +00:00
return 0;
2007-07-05 16:02:13 +00:00
}
BAREBOX_CMD_HELP_START(addpart)
commands: harmonize in-barebox documentation This patch does probably too much, but it's hard (and very cumbersome/time consuming) to break it out. What is does is this: * each command has one short description, e.g. "list MUX configuration" * made sure the short descriptions start lowercase * each command has one usage. That string contains just the options, e.g. "[-npn]". It's not part of the long help text. * that is, it doesn't say "[OPTIONS]" anymore, every usable option is listed by character in this (short) option string (the long description is in the long help text, as before) * help texts have been reworked, to make them - sometimes smaller - sometimes describe the options better - more often present themselves in a nicer format * all long help texts are now created with BUSYBOX_CMD_HELP_ macros, no more 'static const __maybe_unused char cmd_foobar_help[]' * made sure the long help texts starts uppercase * because cmdtp->name and cmdtp->opts together provide the new usage, all "Usage: foobar" texts have been removed from the long help texts * BUSYBOX_CMD_HELP_TEXT() provides the trailing newline by itself, this is nicer in the source code * BUSYBOX_CMD_HELP_OPT() provides the trailing newline by itself * made sure no line gets longer than 77 characters * delibertely renamed cmdtp->usage, so that we can get compile-time errors (e.g. in out-of-tree modules that use register_command() * the 'help' command can now always emit the usage, even without compiled long help texts * 'help -v' gives a list of commands with their short description, this is similar like the old "help" command before my patchset * 'help -a' gives out help of all commands Signed-off-by: Holger Schurig <holgerschurig@gmail.com> Signed-off-by: Sascha Hauer <s.hauer@pengutronix.de>
2014-05-13 08:28:42 +00:00
BAREBOX_CMD_HELP_TEXT("The size and the offset can be given in decimal (without any prefix) and")
BAREBOX_CMD_HELP_TEXT("in hex (prefixed with 0x). Both can have an optional suffix K, M or G.")
BAREBOX_CMD_HELP_TEXT("The size of the last partition can be specified as '-' for the remaining")
BAREBOX_CMD_HELP_TEXT("space on the device. This format is the same as used by the Linux")
BAREBOX_CMD_HELP_TEXT("kernel or cmdline mtd partitions.")
BAREBOX_CMD_HELP_TEXT("")
BAREBOX_CMD_HELP_TEXT("Options:")
BAREBOX_CMD_HELP_OPT ("-n", "do not use the device name as prefix of the partition name")
BAREBOX_CMD_HELP_OPT ("DEVICE", "device being worked on")
BAREBOX_CMD_HELP_OPT ("PART", "SIZE1[@OFFSET1](NAME1)[RO],SIZE2[@OFFSET2](NAME2)[RO],...")
BAREBOX_CMD_HELP_END
/**
* @page addpart_command
The size and the offset can be given in decimal (without any prefix) and
in hex (prefixed with 0x). Both can have an optional suffix K, M or G.
The size of the last partition can be specified as '-' for the remaining
space on the device. This format is the same as used by the Linux
kernel or cmdline mtd partitions.
\todo This command has to be reworked and will probably change it's API.
*/
2007-07-05 16:02:13 +00:00
BAREBOX_CMD_START(addpart)
2007-07-14 13:20:43 +00:00
.cmd = do_addpart,
commands: harmonize in-barebox documentation This patch does probably too much, but it's hard (and very cumbersome/time consuming) to break it out. What is does is this: * each command has one short description, e.g. "list MUX configuration" * made sure the short descriptions start lowercase * each command has one usage. That string contains just the options, e.g. "[-npn]". It's not part of the long help text. * that is, it doesn't say "[OPTIONS]" anymore, every usable option is listed by character in this (short) option string (the long description is in the long help text, as before) * help texts have been reworked, to make them - sometimes smaller - sometimes describe the options better - more often present themselves in a nicer format * all long help texts are now created with BUSYBOX_CMD_HELP_ macros, no more 'static const __maybe_unused char cmd_foobar_help[]' * made sure the long help texts starts uppercase * because cmdtp->name and cmdtp->opts together provide the new usage, all "Usage: foobar" texts have been removed from the long help texts * BUSYBOX_CMD_HELP_TEXT() provides the trailing newline by itself, this is nicer in the source code * BUSYBOX_CMD_HELP_OPT() provides the trailing newline by itself * made sure no line gets longer than 77 characters * delibertely renamed cmdtp->usage, so that we can get compile-time errors (e.g. in out-of-tree modules that use register_command() * the 'help' command can now always emit the usage, even without compiled long help texts * 'help -v' gives a list of commands with their short description, this is similar like the old "help" command before my patchset * 'help -a' gives out help of all commands Signed-off-by: Holger Schurig <holgerschurig@gmail.com> Signed-off-by: Sascha Hauer <s.hauer@pengutronix.de>
2014-05-13 08:28:42 +00:00
BAREBOX_CMD_DESC("add a partition description to a device")
BAREBOX_CMD_OPTS("[-n] DEVICE PART")
BAREBOX_CMD_GROUP(CMD_GRP_PART)
BAREBOX_CMD_HELP(cmd_addpart_help)
BAREBOX_CMD_END
2007-07-05 16:02:13 +00:00
static int do_delpart(int argc, char *argv[])
2007-07-05 16:02:13 +00:00
{
int i, err;
2007-07-05 16:02:13 +00:00
for (i = 1; i < argc; i++) {
err = devfs_del_partition(basename(argv[i]));
if (err) {
printf("cannot delete %s: %s\n", argv[i], strerror(-err));
break;
}
2007-07-14 13:20:43 +00:00
}
2007-07-05 16:02:13 +00:00
return 1;
2007-07-05 16:02:13 +00:00
}
BAREBOX_CMD_HELP_START(delpart)
commands: harmonize in-barebox documentation This patch does probably too much, but it's hard (and very cumbersome/time consuming) to break it out. What is does is this: * each command has one short description, e.g. "list MUX configuration" * made sure the short descriptions start lowercase * each command has one usage. That string contains just the options, e.g. "[-npn]". It's not part of the long help text. * that is, it doesn't say "[OPTIONS]" anymore, every usable option is listed by character in this (short) option string (the long description is in the long help text, as before) * help texts have been reworked, to make them - sometimes smaller - sometimes describe the options better - more often present themselves in a nicer format * all long help texts are now created with BUSYBOX_CMD_HELP_ macros, no more 'static const __maybe_unused char cmd_foobar_help[]' * made sure the long help texts starts uppercase * because cmdtp->name and cmdtp->opts together provide the new usage, all "Usage: foobar" texts have been removed from the long help texts * BUSYBOX_CMD_HELP_TEXT() provides the trailing newline by itself, this is nicer in the source code * BUSYBOX_CMD_HELP_OPT() provides the trailing newline by itself * made sure no line gets longer than 77 characters * delibertely renamed cmdtp->usage, so that we can get compile-time errors (e.g. in out-of-tree modules that use register_command() * the 'help' command can now always emit the usage, even without compiled long help texts * 'help -v' gives a list of commands with their short description, this is similar like the old "help" command before my patchset * 'help -a' gives out help of all commands Signed-off-by: Holger Schurig <holgerschurig@gmail.com> Signed-off-by: Sascha Hauer <s.hauer@pengutronix.de>
2014-05-13 08:28:42 +00:00
BAREBOX_CMD_HELP_TEXT("Delete partitions previously added to a device with addpart.")
BAREBOX_CMD_HELP_END
/**
* @page delpart_command
Partitions are created by adding their description with the addpart
command. If you want to get rid of a partition again, use delpart. The
argument list is taken as a list of partitions to be deleted.
\todo Add an example
*/
2007-07-05 16:02:13 +00:00
BAREBOX_CMD_START(delpart)
2007-07-14 13:20:43 +00:00
.cmd = do_delpart,
commands: harmonize in-barebox documentation This patch does probably too much, but it's hard (and very cumbersome/time consuming) to break it out. What is does is this: * each command has one short description, e.g. "list MUX configuration" * made sure the short descriptions start lowercase * each command has one usage. That string contains just the options, e.g. "[-npn]". It's not part of the long help text. * that is, it doesn't say "[OPTIONS]" anymore, every usable option is listed by character in this (short) option string (the long description is in the long help text, as before) * help texts have been reworked, to make them - sometimes smaller - sometimes describe the options better - more often present themselves in a nicer format * all long help texts are now created with BUSYBOX_CMD_HELP_ macros, no more 'static const __maybe_unused char cmd_foobar_help[]' * made sure the long help texts starts uppercase * because cmdtp->name and cmdtp->opts together provide the new usage, all "Usage: foobar" texts have been removed from the long help texts * BUSYBOX_CMD_HELP_TEXT() provides the trailing newline by itself, this is nicer in the source code * BUSYBOX_CMD_HELP_OPT() provides the trailing newline by itself * made sure no line gets longer than 77 characters * delibertely renamed cmdtp->usage, so that we can get compile-time errors (e.g. in out-of-tree modules that use register_command() * the 'help' command can now always emit the usage, even without compiled long help texts * 'help -v' gives a list of commands with their short description, this is similar like the old "help" command before my patchset * 'help -a' gives out help of all commands Signed-off-by: Holger Schurig <holgerschurig@gmail.com> Signed-off-by: Sascha Hauer <s.hauer@pengutronix.de>
2014-05-13 08:28:42 +00:00
BAREBOX_CMD_DESC("delete partition(s)")
BAREBOX_CMD_OPTS("PART...")
BAREBOX_CMD_GROUP(CMD_GRP_PART)
BAREBOX_CMD_HELP(cmd_delpart_help)
BAREBOX_CMD_COMPLETE(devfs_partition_complete)
BAREBOX_CMD_END
2007-07-14 13:20:43 +00:00