9
0
Fork 0
barebox/commands/partition.c

207 lines
4.6 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.
*
* You should have received a copy of the GNU General Public License
* along with this program; if not, write to the Free Software
* Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
*/
/**
* @file
* @brief partition handling and addpart and delpart command
*/
2007-10-19 09:50:58 +00:00
#ifdef CONFIG_ENABLE_PARTITION_NOISE
# define DEBUG
#endif
2007-07-05 16:02:13 +00:00
#include <common.h>
#include <command.h>
#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>
2007-07-05 16:02:13 +00:00
#define SIZE_REMAINING ((ulong)-1)
static int mtd_part_do_parse_one(char *devname, const char *partstr,
char **endp, unsigned long *offset,
off_t devsize, size_t *retsize)
2007-07-05 16:02:13 +00:00
{
2007-07-14 13:20:43 +00:00
ulong size;
char *end;
char buf[PATH_MAX];
unsigned long flags = 0;
int ret;
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 = strtoul_suffix(partstr, &end, 0);
2007-07-05 16:02:13 +00:00
}
if (*end == '@')
*offset = strtoul_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;
}
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 |= 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
ret = devfs_add_partition(devname, *offset, size, flags, buf);
if (ret)
printf("cannot create %s: %s\n", buf, strerror(-ret));
return ret;
2007-07-05 16:02:13 +00:00
}
static int do_addpart(struct command * cmdtp, int argc, char *argv[])
2007-07-05 16:02:13 +00:00
{
char *devname;
2007-07-14 13:20:43 +00:00
char *endp;
unsigned long offset = 0;
off_t devsize;
struct stat s;
2007-07-14 13:20:43 +00:00
if (argc != 3)
return COMMAND_ERROR_USAGE;
2007-07-05 16:02:13 +00:00
if (stat(argv[1], &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[1]);
2007-07-05 16:02:13 +00:00
endp = argv[2];
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))
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)
BAREBOX_CMD_HELP_USAGE("addpart <device> <part_desc>\n")
BAREBOX_CMD_HELP_SHORT("Add a partition description to a device.\n")
BAREBOX_CMD_HELP_OPT ("<device>", "device being worked on\n")
BAREBOX_CMD_HELP_OPT ("<part_desc>", "size1[@offset1](name1)[ro],size2[@offset2](name2)[ro],...\n")
BAREBOX_CMD_HELP_END
/**
* @page addpart_command
Size and offset can be given in decimal 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,
.usage = "adds a partition table to a device",
BAREBOX_CMD_HELP(cmd_addpart_help)
BAREBOX_CMD_END
2007-07-05 16:02:13 +00:00
static int do_delpart(struct command * cmdtp, 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
}
static const __maybe_unused char cmd_delpart_help[] =
"Usage: delpart FILE...\n"
"Delete partitions previously added to a device with addpart.\n";
2007-07-05 16:02:13 +00:00
BAREBOX_CMD_START(delpart)
2007-07-14 13:20:43 +00:00
.cmd = do_delpart,
.usage = "delete partition(s)",
BAREBOX_CMD_HELP(cmd_delpart_help)
BAREBOX_CMD_END
2007-07-14 13:20:43 +00:00
/** @page delpart_command delpart Delete a partition
*
* Usage is: delpart \<partions>
*
* Delete a partition previously added to a device with addpart.
*/