summaryrefslogtreecommitdiff
path: root/cmd/ubi.c
diff options
context:
space:
mode:
Diffstat (limited to 'cmd/ubi.c')
-rw-r--r--cmd/ubi.c78
1 files changed, 72 insertions, 6 deletions
diff --git a/cmd/ubi.c b/cmd/ubi.c
index 92998af2b0..0e62e44932 100644
--- a/cmd/ubi.c
+++ b/cmd/ubi.c
@@ -423,18 +423,84 @@ int ubi_volume_begin_write(char *volume, void *buf, size_t size,
return ubi_volume_continue_write(volume, buf, size);
}
-int ubi_volume_write(char *volume, void *buf, size_t size)
+static int ubi_volume_offset_write(char *volume, void *buf, loff_t offset,
+ size_t size)
{
- return ubi_volume_begin_write(volume, buf, size, size);
+ int len, tbuf_size, ret;
+ u64 lnum;
+ struct ubi_volume *vol;
+ loff_t off = offset;
+ void *tbuf;
+
+ vol = ubi_find_volume(volume);
+ if (!vol)
+ return -ENODEV;
+
+ if (size > vol->reserved_pebs * (ubi->leb_size - vol->data_pad))
+ return -EINVAL;
+
+ tbuf_size = vol->usable_leb_size;
+ tbuf = malloc_cache_aligned(tbuf_size);
+ if (!tbuf)
+ return -ENOMEM;
+
+ lnum = off;
+ off = do_div(lnum, vol->usable_leb_size);
+
+ do {
+ struct ubi_volume_desc desc = {
+ .vol = vol,
+ .mode = UBI_READWRITE,
+ };
+
+ len = size > tbuf_size ? tbuf_size : size;
+ if (off + len >= vol->usable_leb_size)
+ len = vol->usable_leb_size - off;
+
+ ret = ubi_read(&desc, (int)lnum, tbuf, 0, tbuf_size);
+ if (ret) {
+ pr_err("Failed to read leb %lld (%d)\n", lnum, ret);
+ goto exit;
+ }
+
+ memcpy(tbuf + off, buf, len);
+
+ ret = ubi_leb_change(&desc, (int)lnum, tbuf, tbuf_size);
+ if (ret) {
+ pr_err("Failed to write leb %lld (%d)\n", lnum, ret);
+ goto exit;
+ }
+
+ off += len;
+ if (off >= vol->usable_leb_size) {
+ lnum++;
+ off -= vol->usable_leb_size;
+ }
+
+ buf += len;
+ size -= len;
+ } while (size);
+
+exit:
+ free(tbuf);
+ return ret;
+}
+
+int ubi_volume_write(char *volume, void *buf, loff_t offset, size_t size)
+{
+ if (!offset)
+ return ubi_volume_begin_write(volume, buf, size, size);
+
+ return ubi_volume_offset_write(volume, buf, offset, size);
}
-int ubi_volume_read(char *volume, char *buf, size_t size)
+int ubi_volume_read(char *volume, char *buf, loff_t offset, size_t size)
{
int err, lnum, off, len, tbuf_size;
void *tbuf;
unsigned long long tmp;
struct ubi_volume *vol;
- loff_t offp = 0;
+ loff_t offp = offset;
size_t len_read;
vol = ubi_find_volume(volume);
@@ -769,7 +835,7 @@ static int do_ubi(struct cmd_tbl *cmdtp, int flag, int argc, char *const argv[])
(void *)addr, size, full_size);
}
} else {
- ret = ubi_volume_write(argv[3], (void *)addr, size);
+ ret = ubi_volume_write(argv[3], (void *)addr, 0, size);
}
if (!ret) {
printf("%lld bytes written to volume %s\n", size,
@@ -795,7 +861,7 @@ static int do_ubi(struct cmd_tbl *cmdtp, int flag, int argc, char *const argv[])
}
if (argc == 3) {
- return ubi_volume_read(argv[3], (char *)addr, size);
+ return ubi_volume_read(argv[3], (char *)addr, 0, size);
}
}