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
|
|
|
|
*/
|
|
|
|
|
2007-11-09 13:05:23 +00:00
|
|
|
/**
|
|
|
|
* @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>
|
|
|
|
|
2007-10-19 06:45:57 +00:00
|
|
|
static int dev_del_partitions(struct device_d *physdev)
|
2007-07-05 16:02:13 +00:00
|
|
|
{
|
2007-10-19 06:45:57 +00:00
|
|
|
struct device_d *child, *tmp;
|
2008-08-14 07:28:10 +00:00
|
|
|
struct partition *part;
|
2007-10-19 06:45:57 +00:00
|
|
|
int ret;
|
2007-07-14 13:20:43 +00:00
|
|
|
|
2007-10-19 06:45:57 +00:00
|
|
|
device_for_each_child_safe(physdev, tmp, child) {
|
2008-08-14 07:28:10 +00:00
|
|
|
if (child->type != DEVICE_TYPE_PARTITION) {
|
|
|
|
printf("not a partition: %s\n", child->id);
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
|
|
|
part = child->type_data;
|
2007-10-19 06:45:57 +00:00
|
|
|
|
2008-02-16 00:01:20 +00:00
|
|
|
if (part->flags & PARTITION_FIXED) {
|
|
|
|
debug("Skip fixed partition: %s\n", child->id);
|
2007-10-19 06:45:57 +00:00
|
|
|
continue;
|
2008-02-16 00:01:20 +00:00
|
|
|
}
|
2007-10-19 06:45:57 +00:00
|
|
|
|
|
|
|
ret = unregister_device(child);
|
|
|
|
if (ret) {
|
|
|
|
printf("delete partition `%s' failed: %s\n", child->id, errno_str());
|
|
|
|
return errno;
|
|
|
|
}
|
|
|
|
|
2008-02-16 00:01:20 +00:00
|
|
|
debug("deleted partition: %s\n", child->id);
|
|
|
|
|
2007-10-19 06:45:57 +00:00
|
|
|
free(part);
|
2007-07-14 13:20:43 +00:00
|
|
|
}
|
2007-10-19 06:45:57 +00:00
|
|
|
|
|
|
|
return 0;
|
2007-07-05 16:02:13 +00:00
|
|
|
}
|
|
|
|
|
2007-11-27 19:45:36 +00:00
|
|
|
static int dev_check_fixed(struct device_d *physdev, struct partition *new_part)
|
|
|
|
{
|
|
|
|
struct device_d *child;
|
|
|
|
|
|
|
|
device_for_each_child(physdev, child) {
|
|
|
|
struct partition *part = child->type_data;
|
|
|
|
|
2008-02-16 00:01:20 +00:00
|
|
|
debug("check aginst partition: %s -", child->id);
|
2007-11-27 19:45:36 +00:00
|
|
|
|
2008-02-16 00:01:20 +00:00
|
|
|
if (!(part->flags & PARTITION_FIXED)) {
|
|
|
|
debug(" not fixed, ok\n");
|
2007-11-27 19:45:36 +00:00
|
|
|
continue;
|
2008-02-16 00:01:20 +00:00
|
|
|
}
|
2007-11-27 19:45:36 +00:00
|
|
|
|
2008-02-16 00:01:20 +00:00
|
|
|
if (new_part->offset == part->offset && /* new_part is exactly part */
|
|
|
|
((new_part->device.size==0) || (new_part->device.size == part->device.size)) ) {
|
|
|
|
debug(" fixed, but same size, ok\n");
|
2007-11-27 19:45:36 +00:00
|
|
|
continue;
|
2008-02-16 00:01:20 +00:00
|
|
|
}
|
2007-11-27 19:45:36 +00:00
|
|
|
|
|
|
|
if ((new_part->offset >= part->offset &&
|
|
|
|
new_part->offset < part->offset + part->device.size) ||
|
|
|
|
(new_part->offset + new_part->device.size > part->offset &&
|
|
|
|
new_part->offset + new_part->device.size <= part->offset + part->device.size)) {
|
2008-02-16 00:01:20 +00:00
|
|
|
printf(
|
|
|
|
" failed\n"
|
|
|
|
" partition spec %s \n"
|
|
|
|
" violates fixed partition %s\n", new_part->name, child->id);
|
2007-11-27 19:45:36 +00:00
|
|
|
errno = -EINVAL;
|
|
|
|
return errno;
|
|
|
|
}
|
2008-02-16 00:01:20 +00:00
|
|
|
else
|
|
|
|
debug(" fixed and within limit?, ok\n");
|
2007-11-27 19:45:36 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2007-07-14 13:20:43 +00:00
|
|
|
static int mtd_part_do_parse_one(struct partition *part, const char *str,
|
|
|
|
char **endp)
|
2007-07-05 16:02:13 +00:00
|
|
|
{
|
2007-07-14 13:20:43 +00:00
|
|
|
ulong size;
|
|
|
|
char *end;
|
|
|
|
char buf[MAX_DRIVER_NAME];
|
2007-07-05 16:02:13 +00:00
|
|
|
|
|
|
|
memset(buf, 0, MAX_DRIVER_NAME);
|
|
|
|
|
2007-07-14 13:20:43 +00:00
|
|
|
if (*str == '-') {
|
|
|
|
size = part->physdev->size - part->offset;
|
2007-07-05 16:02:13 +00:00
|
|
|
end = (char *)str + 1;
|
2007-07-14 13:20:43 +00:00
|
|
|
} else {
|
2007-07-05 16:02:13 +00:00
|
|
|
size = strtoul_suffix(str, &end, 0);
|
|
|
|
}
|
|
|
|
|
|
|
|
if (size + part->offset > part->physdev->size) {
|
2008-02-16 00:01:20 +00:00
|
|
|
printf("partition %s end is beyond device\n", part->name);
|
2007-07-05 16:02:13 +00:00
|
|
|
return -EINVAL;
|
|
|
|
}
|
|
|
|
|
2007-07-14 13:20:43 +00:00
|
|
|
str = end;
|
2007-07-05 16:02:13 +00:00
|
|
|
|
2007-07-14 13:20:43 +00:00
|
|
|
if (*str == '(') {
|
2007-07-05 16:02:13 +00:00
|
|
|
str++;
|
2007-07-14 13:20:43 +00:00
|
|
|
end = strchr(str, ')');
|
|
|
|
if (!end) {
|
|
|
|
printf("could not find matching ')'\n");
|
|
|
|
return -EINVAL;
|
|
|
|
}
|
|
|
|
if (end - str >= MAX_DRIVER_NAME) {
|
|
|
|
printf("device name too long\n");
|
|
|
|
return -EINVAL;
|
|
|
|
}
|
|
|
|
|
|
|
|
memcpy(part->name, str, end - str);
|
|
|
|
end++;
|
|
|
|
}
|
|
|
|
|
|
|
|
str = end;
|
|
|
|
|
|
|
|
if (*str == 'r' && *(str + 1) == 'o') {
|
2007-10-19 06:45:57 +00:00
|
|
|
part->flags |= PARTITION_READONLY;
|
2007-07-14 13:20:43 +00:00
|
|
|
end = (char *)(str + 2);
|
|
|
|
}
|
|
|
|
|
|
|
|
if (endp)
|
|
|
|
*endp = end;
|
|
|
|
|
|
|
|
strcpy(part->device.name, "partition");
|
|
|
|
part->device.size = size;
|
|
|
|
|
|
|
|
return 0;
|
2007-07-05 16:02:13 +00:00
|
|
|
}
|
|
|
|
|
2007-09-28 08:07:26 +00:00
|
|
|
static int do_addpart(cmd_tbl_t * cmdtp, int argc, char *argv[])
|
2007-07-05 16:02:13 +00:00
|
|
|
{
|
2007-07-14 13:20:43 +00:00
|
|
|
struct partition *part;
|
|
|
|
struct device_d *dev;
|
|
|
|
char *endp;
|
|
|
|
int num = 0;
|
|
|
|
unsigned long offset;
|
|
|
|
|
2007-10-19 06:45:57 +00:00
|
|
|
if (argc != 3) {
|
2008-02-16 00:01:20 +00:00
|
|
|
printf("Usage:\n %s\n", cmdtp->usage);
|
2007-07-05 16:02:13 +00:00
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
2007-10-19 06:45:57 +00:00
|
|
|
dev = get_device_by_path(argv[1]);
|
2007-07-14 13:20:43 +00:00
|
|
|
if (!dev) {
|
|
|
|
printf("no such device: %s\n", argv[1]);
|
|
|
|
return 1;
|
|
|
|
}
|
2007-07-05 16:02:13 +00:00
|
|
|
|
|
|
|
dev_del_partitions(dev);
|
|
|
|
|
2007-07-14 13:20:43 +00:00
|
|
|
offset = 0;
|
2007-07-05 16:02:13 +00:00
|
|
|
|
2007-10-19 06:45:57 +00:00
|
|
|
endp = argv[2];
|
|
|
|
|
2007-07-14 13:20:43 +00:00
|
|
|
while (1) {
|
|
|
|
part = xzalloc(sizeof(struct partition));
|
2007-07-05 16:02:13 +00:00
|
|
|
|
2007-07-14 13:20:43 +00:00
|
|
|
part->offset = offset;
|
|
|
|
part->physdev = dev;
|
|
|
|
part->num = num;
|
|
|
|
part->device.map_base = dev->map_base + offset;
|
2007-11-27 19:45:36 +00:00
|
|
|
part->device.type_data = part;
|
2008-08-14 07:28:10 +00:00
|
|
|
part->device.type = DEVICE_TYPE_PARTITION;
|
2007-07-05 16:02:13 +00:00
|
|
|
|
2007-11-27 19:45:36 +00:00
|
|
|
if (mtd_part_do_parse_one(part, endp, &endp))
|
2007-10-19 06:45:57 +00:00
|
|
|
goto free_out;
|
2007-07-05 16:02:13 +00:00
|
|
|
|
2007-11-27 19:45:36 +00:00
|
|
|
if (dev_check_fixed(dev, part))
|
|
|
|
goto free_out;
|
2007-07-05 16:02:13 +00:00
|
|
|
|
2007-10-19 06:45:57 +00:00
|
|
|
sprintf(part->device.id, "%s.%s", dev->id, part->name);
|
|
|
|
if (register_device(&part->device))
|
|
|
|
goto free_out;
|
|
|
|
|
|
|
|
dev_add_child(dev, &part->device);
|
|
|
|
|
2007-11-27 19:45:36 +00:00
|
|
|
offset += part->device.size;
|
2007-07-14 13:20:43 +00:00
|
|
|
num++;
|
2007-07-05 16:02:13 +00:00
|
|
|
|
2007-07-14 13:20:43 +00:00
|
|
|
if (!*endp)
|
|
|
|
break;
|
2007-10-19 06:45:57 +00:00
|
|
|
|
2007-07-14 13:20:43 +00:00
|
|
|
if (*endp != ',') {
|
|
|
|
printf("parse error\n");
|
2007-10-19 06:45:57 +00:00
|
|
|
goto err_out;
|
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-10-19 06:45:57 +00:00
|
|
|
|
|
|
|
free_out:
|
|
|
|
free(part);
|
|
|
|
err_out:
|
2007-11-27 19:45:36 +00:00
|
|
|
dev_del_partitions(dev);
|
2007-10-19 06:45:57 +00:00
|
|
|
return 1;
|
2007-07-05 16:02:13 +00:00
|
|
|
}
|
|
|
|
|
2008-04-04 09:46:55 +00:00
|
|
|
static const __maybe_unused char cmd_addpart_help[] =
|
2007-10-19 09:23:24 +00:00
|
|
|
"Usage: addpart <device> <partition description>\n"
|
2008-02-16 00:01:20 +00:00
|
|
|
"\n"
|
2007-07-05 16:02:13 +00:00
|
|
|
"addpart adds a partition description to a device. The partition description\n"
|
|
|
|
"has the form\n"
|
2007-10-19 09:23:24 +00:00
|
|
|
"size1(name1)[ro],size2(name2)[ro],...\n"
|
|
|
|
"<device> is the device name under. Size can be given in decimal or if prefixed\n"
|
|
|
|
"with 0x in hex. Sizes can have an optional suffix K,M,G. The size of the last\n"
|
|
|
|
"partition can be specified as '-' for the remaining space of the device.\n"
|
2007-07-05 16:02:13 +00:00
|
|
|
"This format is the same as used in the Linux kernel for cmdline mtd partitions.\n"
|
2008-02-16 00:01:20 +00:00
|
|
|
"\n"
|
|
|
|
"Note: That this command has to be reworked and will probably change it's API.";
|
2007-07-05 16:02:13 +00:00
|
|
|
|
|
|
|
U_BOOT_CMD_START(addpart)
|
2007-10-19 06:45:57 +00:00
|
|
|
.maxargs = 3,
|
2007-07-14 13:20:43 +00:00
|
|
|
.cmd = do_addpart,
|
2008-02-16 00:01:20 +00:00
|
|
|
.usage = "adds a partition table to a device",
|
2007-07-05 16:02:13 +00:00
|
|
|
U_BOOT_CMD_HELP(cmd_addpart_help)
|
|
|
|
U_BOOT_CMD_END
|
|
|
|
|
2007-11-09 13:05:23 +00:00
|
|
|
/** @page addpart_command addpart Add a partition to a device
|
|
|
|
*
|
|
|
|
* Usage is: addpart \<device> \<partition description>
|
|
|
|
*
|
|
|
|
* Adds a partition description to a device. The partition description has the
|
|
|
|
* form
|
|
|
|
*
|
|
|
|
* size1(name1)[ro],size2(name2)[ro],...
|
|
|
|
*
|
|
|
|
* \<device> is the device name under. Sizes can be given in decimal or - if
|
|
|
|
* prefixed with 0x - in hex. Sizes can have an optional suffix K,M,G. The
|
|
|
|
* size of the last partition can be specified as '-' for the remaining space
|
|
|
|
* of the device.
|
|
|
|
*
|
|
|
|
* @note The format is the same as used in the Linux kernel for cmdline mtd
|
|
|
|
* partitions.
|
|
|
|
*
|
|
|
|
* @note This command has to be reworked and will probably change it's API.
|
|
|
|
*/
|
|
|
|
|
2007-09-28 08:07:26 +00:00
|
|
|
static int do_delpart(cmd_tbl_t * cmdtp, int argc, char *argv[])
|
2007-07-05 16:02:13 +00:00
|
|
|
{
|
2007-07-14 13:20:43 +00:00
|
|
|
struct device_d *dev;
|
2007-07-05 16:02:13 +00:00
|
|
|
|
2007-07-14 13:20:43 +00:00
|
|
|
if (argc != 2) {
|
|
|
|
printf("Usage:\n%s\n", cmdtp->usage);
|
2007-07-05 16:02:13 +00:00
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
2007-10-19 06:45:57 +00:00
|
|
|
dev = get_device_by_path(argv[1]);
|
2007-07-14 13:20:43 +00:00
|
|
|
if (!dev) {
|
|
|
|
printf("no such device: %s\n", argv[1]);
|
|
|
|
return 1;
|
|
|
|
}
|
2007-07-05 16:02:13 +00:00
|
|
|
|
|
|
|
dev_del_partitions(dev);
|
|
|
|
|
2007-07-14 13:20:43 +00:00
|
|
|
return 0;
|
2007-07-05 16:02:13 +00:00
|
|
|
}
|
|
|
|
|
2008-04-04 09:46:55 +00:00
|
|
|
static const __maybe_unused char cmd_delpart_help[] =
|
2007-10-19 09:23:24 +00:00
|
|
|
"Usage: delpart <device>\n"
|
|
|
|
"Delete partitions previously added to a device with addpart.\n";
|
2007-07-05 16:02:13 +00:00
|
|
|
|
|
|
|
U_BOOT_CMD_START(delpart)
|
2007-07-14 13:20:43 +00:00
|
|
|
.maxargs = 2,
|
|
|
|
.cmd = do_delpart,
|
|
|
|
.usage = "delete a partition table from a device",
|
2007-07-05 16:02:13 +00:00
|
|
|
U_BOOT_CMD_HELP(cmd_delpart_help)
|
|
|
|
U_BOOT_CMD_END
|
2007-07-14 13:20:43 +00:00
|
|
|
|
2007-11-09 13:05:23 +00:00
|
|
|
/** @page delpart_command delpart Delete a partition
|
|
|
|
*
|
|
|
|
* Usage is: delpart \<device>
|
|
|
|
*
|
|
|
|
* Delete a partition previously added to a device with addpart.
|
|
|
|
*/
|