* Copyright (C) 2008 Samsung Electronics
* Kyungmin Park <kyungmin.park@samsung.com>
*
- * Copyright 2008 Stefan Roese <sr@denx.de>, DENX Software Engineering
+ * Copyright 2008-2009 Stefan Roese <sr@denx.de>, DENX Software Engineering
*
* 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
static int ubi_initialized;
struct selected_dev {
- char dev_name[32]; /* NAND/OneNAND etc */
char part_name[80];
- int type;
+ int selected;
int nr;
struct mtd_info *mtd_info;
};
static struct selected_dev ubi_dev;
+#ifdef CONFIG_CMD_UBIFS
+int ubifs_is_mounted(void);
+void cmd_ubifs_umount(void);
+#endif
+
static void ubi_dump_vol_info(const struct ubi_volume *vol)
{
ubi_msg("volume information dump:");
static int verify_mkvol_req(const struct ubi_device *ubi,
const struct ubi_mkvol_req *req)
{
- int n, err = -EINVAL;
+ int n, err = EINVAL;
if (req->bytes < 0 || req->alignment < 0 || req->vol_type < 0 ||
req->name_len < 0)
if (req->alignment == 0)
goto bad;
- if (req->bytes == 0)
+ if (req->bytes == 0) {
+ printf("No space left in UBI device!\n");
+ err = ENOMEM;
goto bad;
+ }
if (req->vol_type != UBI_DYNAMIC_VOLUME &&
req->vol_type != UBI_STATIC_VOLUME)
goto bad;
if (req->name_len > UBI_VOL_NAME_MAX) {
- err = -ENAMETOOLONG;
+ printf("Name too long!\n");
+ err = ENAMETOOLONG;
goto bad;
}
return 0;
bad:
- printf("bad volume creation request");
return err;
}
return ubi_create_volume(ubi, &req);
}
-static int ubi_remove_vol(char *volume)
+static struct ubi_volume *ubi_find_volume(char *volume)
{
- int i, err, reserved_pebs;
- int found = 0, vol_id = 0;
- struct ubi_volume *vol;
+ struct ubi_volume *vol = NULL;
+ int i;
for (i = 0; i < ubi->vtbl_slots; i++) {
vol = ubi->volumes[i];
- if (vol && !strcmp(vol->name, volume)) {
- printf("Volume %s found at valid %d\n", volume, i);
- vol_id = i;
- found = 1;
- break;
- }
- }
- if (!found) {
- printf("%s volume not found\n", volume);
- return -ENODEV;
+ if (vol && !strcmp(vol->name, volume))
+ return vol;
}
- printf("remove UBI volume %s (id %d)\n", vol->name, vol->vol_id);
+
+ printf("Volume %s not found!\n", volume);
+ return NULL;
+}
+
+static int ubi_remove_vol(char *volume)
+{
+ int err, reserved_pebs, i;
+ struct ubi_volume *vol;
+
+ vol = ubi_find_volume(volume);
+ if (vol == NULL)
+ return ENODEV;
+
+ printf("Remove UBI volume %s (id %d)\n", vol->name, vol->vol_id);
if (ubi->ro_mode) {
printf("It's read-only mode\n");
- err = -EROFS;
+ err = EROFS;
goto out_err;
}
- err = ubi_change_vtbl_record(ubi, vol_id, NULL);
+ err = ubi_change_vtbl_record(ubi, vol->vol_id, NULL);
if (err) {
printf("Error changing Vol tabel record err=%x\n", err);
goto out_err;
}
kfree(vol->eba_tbl);
- ubi->volumes[vol_id]->eba_tbl = NULL;
- ubi->volumes[vol_id] = NULL;
+ ubi->volumes[vol->vol_id]->eba_tbl = NULL;
+ ubi->volumes[vol->vol_id] = NULL;
ubi->rsvd_pebs -= reserved_pebs;
ubi->avail_pebs += reserved_pebs;
return 0;
out_err:
- ubi_err("cannot remove volume %d, error %d", vol_id, err);
+ ubi_err("cannot remove volume %s, error %d", volume, err);
+ if (err < 0)
+ err = -err;
return err;
}
static int ubi_volume_write(char *volume, void *buf, size_t size)
{
- int i = 0, err = -1;
+ int err = 1;
int rsvd_bytes = 0;
- int found = 0;
struct ubi_volume *vol;
- for (i = 0; i < ubi->vtbl_slots; i++) {
- vol = ubi->volumes[i];
- if (vol && !strcmp(vol->name, volume)) {
- printf("Volume \"%s\" found at volume id %d\n", volume, i);
- found = 1;
- break;
- }
- }
- if (!found) {
- printf("%s volume not found\n", volume);
- return 1;
- }
+ vol = ubi_find_volume(volume);
+ if (vol == NULL)
+ return ENODEV;
+
rsvd_bytes = vol->reserved_pebs * (ubi->leb_size - vol->data_pad);
if (size < 0 || size > rsvd_bytes) {
- printf("rsvd_bytes=%d vol->reserved_pebs=%d ubi->leb_size=%d\n",
- rsvd_bytes, vol->reserved_pebs, ubi->leb_size);
- printf("vol->data_pad=%d\n", vol->data_pad);
- printf("Size > volume size !!\n");
- return 1;
+ printf("size > volume size! Aborting!\n");
+ return EINVAL;
}
err = ubi_start_update(ubi, vol, size);
if (err < 0) {
printf("Cannot start volume update\n");
- return err;
+ return -err;
}
err = ubi_more_update_data(ubi, vol, buf, size);
if (err < 0) {
- printf("Couldnt or partially wrote data \n");
- return err;
+ printf("Couldnt or partially wrote data\n");
+ return -err;
}
if (err) {
size = err;
err = ubi_check_volume(ubi, vol->vol_id);
- if ( err < 0 )
- return err;
+ if (err < 0)
+ return -err;
if (err) {
ubi_warn("volume %d on UBI device %d is corrupted",
ubi_gluebi_updated(vol);
}
+ printf("%d bytes written to volume %s\n", size, volume);
+
return 0;
}
static int ubi_volume_read(char *volume, char *buf, size_t size)
{
- int err, lnum, off, len, tbuf_size, i = 0;
+ int err, lnum, off, len, tbuf_size;
size_t count_save = size;
void *tbuf;
unsigned long long tmp;
- struct ubi_volume *vol = NULL;
+ struct ubi_volume *vol;
loff_t offp = 0;
- for (i = 0; i < ubi->vtbl_slots; i++) {
- vol = ubi->volumes[i];
- if (vol && !strcmp(vol->name, volume)) {
- printf("Volume %s found at volume id %d\n",
- volume, vol->vol_id);
- break;
- }
- }
- if (i == ubi->vtbl_slots) {
- printf("%s volume not found\n", volume);
- return 0;
- }
+ vol = ubi_find_volume(volume);
+ if (vol == NULL)
+ return ENODEV;
- printf("read %i bytes from volume %d to %x(buf address)\n",
- (int) size, vol->vol_id, (unsigned)buf);
+ printf("Read %d bytes from volume %s to %p\n", size, volume, buf);
if (vol->updating) {
printf("updating");
- return -EBUSY;
+ return EBUSY;
}
if (vol->upd_marker) {
printf("damaged volume, update marker is set");
- return -EBADF;
+ return EBADF;
}
if (offp == vol->used_bytes)
return 0;
if (size == 0) {
- printf("Read [%lu] bytes\n", (unsigned long) vol->used_bytes);
+ printf("No size specified -> Using max size (%lld)\n", vol->used_bytes);
size = vol->used_bytes;
}
tbuf = malloc(tbuf_size);
if (!tbuf) {
printf("NO MEM\n");
- return -ENOMEM;
+ return ENOMEM;
}
len = size > tbuf_size ? tbuf_size : size;
err = ubi_eba_read_leb(ubi, vol, lnum, tbuf, off, len, 0);
if (err) {
printf("read err %x\n", err);
+ err = -err;
break;
}
off += len;
} while (size);
free(tbuf);
- return err ? err : count_save - size;
+ return err;
}
-static int ubi_dev_scan(struct mtd_info *info, char *ubidev)
+static int ubi_dev_scan(struct mtd_info *info, char *ubidev,
+ const char *vid_header_offset)
{
struct mtd_device *dev;
struct part_info *part;
struct mtd_partition mtd_part;
+ char ubi_mtd_param_buffer[80];
u8 pnum;
int err;
- if (mtdparts_init() != 0)
- return 1;
-
if (find_dev_and_part(ubidev, &dev, &pnum, &part) != 0)
return 1;
mtd_part.offset = part->offset;
add_mtd_partitions(info, &mtd_part, 1);
- err = ubi_mtd_param_parse(buffer, NULL);
+ strcpy(ubi_mtd_param_buffer, buffer);
+ if (vid_header_offset)
+ sprintf(ubi_mtd_param_buffer, "mtd=%d,%s", pnum,
+ vid_header_offset);
+ err = ubi_mtd_param_parse(ubi_mtd_param_buffer, NULL);
if (err) {
del_mtd_partitions(info);
- return err;
+ return -err;
}
err = ubi_init();
if (err) {
del_mtd_partitions(info);
- return err;
+ return -err;
}
ubi_initialized = 1;
return 0;
}
-static int do_ubi(cmd_tbl_t * cmdtp, int flag, int argc, char *argv[])
+static int do_ubi(cmd_tbl_t * cmdtp, int flag, int argc, char * const argv[])
{
size_t size = 0;
ulong addr = 0;
int err = 0;
- if (argc < 2) {
- cmd_usage(cmdtp);
+ if (argc < 2)
+ return cmd_usage(cmdtp);
+
+ if (mtdparts_init() != 0) {
+ printf("Error initializing mtdparts!\n");
return 1;
}
if (strcmp(argv[1], "part") == 0) {
+ char mtd_dev[16];
+ struct mtd_device *dev;
+ struct part_info *part;
+ const char *vid_header_offset = NULL;
+ u8 pnum;
+
/* Print current partition */
if (argc == 2) {
- if (ubi_dev.type == DEV_TYPE_NONE) {
+ if (!ubi_dev.selected) {
printf("Error, no UBI device/partition selected!\n");
return 1;
}
- printf("%s Device %d: %s, partition %s\n", ubi_dev.dev_name,
+ printf("Device %d: %s, partition %s\n",
ubi_dev.nr, ubi_dev.mtd_info->name, ubi_dev.part_name);
return 0;
}
- if (argc < 4) {
- cmd_usage(cmdtp);
- return 1;
- }
+ if (argc < 3)
+ return cmd_usage(cmdtp);
+
+#ifdef CONFIG_CMD_UBIFS
+ /*
+ * Automatically unmount UBIFS partition when user
+ * changes the UBI device. Otherwise the following
+ * UBIFS commands will crash.
+ */
+ if (ubifs_is_mounted())
+ cmd_ubifs_umount();
+#endif
/* todo: get dev number for NAND... */
ubi_dev.nr = 0;
}
/*
- * Check for nand|onenand selection
+ * Search the mtd device number where this partition
+ * is located
*/
-#if defined(CONFIG_CMD_NAND)
- if (strcmp(argv[2], "nand") == 0) {
- strcpy(ubi_dev.dev_name, "NAND");
- ubi_dev.type = DEV_TYPE_NAND;
- ubi_dev.mtd_info = &nand_info[ubi_dev.nr];
- }
-#endif
-#if defined(CONFIG_FLASH_CFI_MTD)
- if (strcmp(argv[2], "nor") == 0) {
- strcpy(ubi_dev.dev_name, "NOR");
- ubi_dev.type = DEV_TYPE_NOR;
- ubi_dev.mtd_info = get_mtd_device_nm(CFI_MTD_DEV_NAME);
- }
-#endif
-#if defined(CONFIG_CMD_ONENAND)
- if (strcmp(argv[2], "onenand") == 0) {
- strcpy(ubi_dev.dev_name, "OneNAND");
- ubi_dev.type = DEV_TYPE_ONENAND;
- ubi_dev.mtd_info = &onenand_mtd;
+ if (find_dev_and_part(argv[2], &dev, &pnum, &part)) {
+ printf("Partition %s not found!\n", argv[2]);
+ return 1;
}
-#endif
-
- if (ubi_dev.type == DEV_TYPE_NONE) {
- printf("Error, no UBI device/partition selected!\n");
+ sprintf(mtd_dev, "%s%d", MTD_DEV_TYPE(dev->id->type), dev->id->num);
+ ubi_dev.mtd_info = get_mtd_device_nm(mtd_dev);
+ if (IS_ERR(ubi_dev.mtd_info)) {
+ printf("Partition %s not found on device %s!\n", argv[2], mtd_dev);
return 1;
}
- strcpy(ubi_dev.part_name, argv[3]);
- err = ubi_dev_scan(ubi_dev.mtd_info, ubi_dev.part_name);
+ ubi_dev.selected = 1;
+
+ if (argc > 3)
+ vid_header_offset = argv[3];
+ strcpy(ubi_dev.part_name, argv[2]);
+ err = ubi_dev_scan(ubi_dev.mtd_info, ubi_dev.part_name,
+ vid_header_offset);
if (err) {
printf("UBI init error %d\n", err);
- ubi_dev.type = DEV_TYPE_NONE;
+ ubi_dev.selected = 0;
return err;
}
return 0;
}
- if ((strcmp(argv[1], "part") != 0) && (ubi_dev.type == DEV_TYPE_NONE)) {
+ if ((strcmp(argv[1], "part") != 0) && (!ubi_dev.selected)) {
printf("Error, no UBI device/partition selected!\n");
return 1;
}
argc--;
}
/* Use maximum available size */
- if (!size)
+ if (!size) {
size = ubi->avail_pebs * ubi->leb_size;
+ printf("No size specified -> Using max size (%u)\n", size);
+ }
/* E.g., create volume */
if (argc == 3)
return ubi_create_vol(argv[2], size, dynamic);
}
printf("Please see usage\n");
- return -1;
+ return 1;
}
-U_BOOT_CMD(ubi, 6, 1, do_ubi,
- "ubi - ubi commands\n",
- "part [nand|nor|onenand] [part]"
- " - Show or set current partition\n"
+U_BOOT_CMD(
+ ubi, 6, 1, do_ubi,
+ "ubi commands",
+ "part [part] [offset]\n"
+ " - Show or set current partition (with optional VID"
+ " header offset)\n"
"ubi info [l[ayout]]"
" - Display volume and ubi layout information\n"
"ubi create[vol] volume [size] [type]"
"ubi remove[vol] volume"
" - Remove volume\n"
"[Legends]\n"
- " volume: charater name\n"
- " size: KiB, MiB, GiB, and bytes\n"
- " type: s[tatic] or d[ynamic] (default=dynamic)\n"
+ " volume: character name\n"
+ " size: specified in bytes\n"
+ " type: s[tatic] or d[ynamic] (default=dynamic)"
);