int ubi_check_volume(struct ubi_device *ubi, int vol_id) { void *buf; int err = 0, i; struct ubi_volume *vol = ubi->volumes[vol_id]; if (vol->vol_type != UBI_STATIC_VOLUME) return 0; buf = vmalloc(vol->usable_leb_size); if (!buf) return -ENOMEM; for (i = 0; i < vol->used_ebs; i++) { int size; if (i == vol->used_ebs - 1) size = vol->last_eb_bytes; else size = vol->usable_leb_size; err = ubi_eba_read_leb(ubi, vol, i, buf, 0, size, 1); if (err) { if (err == -EBADMSG) err = 1; break; } } vfree(buf); return err; }
static ssize_t ubi_volume_cdev_read(struct cdev *cdev, void *buf, size_t size, loff_t offset, unsigned long flags) { struct ubi_volume_cdev_priv *priv = cdev->priv; struct ubi_volume *vol = priv->vol; struct ubi_device *ubi = priv->ubi; int err, lnum, off, len; size_t count_save = size; unsigned long long tmp; loff_t offp = offset; int usable_leb_size = vol->usable_leb_size; ubi_debug("%s: %zd @ 0x%08llx", __func__, size, offset); len = size > usable_leb_size ? usable_leb_size : size; tmp = offp; off = do_div(tmp, usable_leb_size); lnum = tmp; do { if (off + len >= usable_leb_size) len = usable_leb_size - off; err = ubi_eba_read_leb(ubi, vol, lnum, buf, off, len, 0); if (err) { ubi_err(ubi, "read error: %s", strerror(-err)); break; } off += len; if (off == usable_leb_size) { lnum += 1; off -= usable_leb_size; } size -= len; offp += len; buf += len; len = size > usable_leb_size ? usable_leb_size : size; } while (size); return count_save; }
/** * gluebi_read - read operation of emulated MTD devices. * @mtd: MTD device description object * @from: absolute offset from where to read * @len: how many bytes to read * @retlen: count of read bytes is returned here * @buf: buffer to store the read data * * This function returns zero in case of success and a negative error code in * case of failure. */ static int gluebi_read(struct mtd_info *mtd, loff_t from, size_t len, size_t *retlen, unsigned char *buf) { int err = 0, lnum, offs, total_read; struct ubi_volume *vol; struct ubi_device *ubi; uint64_t tmp = from; dbg_msg("read %zd bytes from offset %lld", len, from); if (len < 0 || from < 0 || from + len > mtd->size) return -EINVAL; vol = container_of(mtd, struct ubi_volume, gluebi_mtd); ubi = vol->ubi; offs = do_div(tmp, mtd->erasesize); lnum = tmp; total_read = len; while (total_read) { size_t to_read = mtd->erasesize - offs; if (to_read > total_read) to_read = total_read; err = ubi_eba_read_leb(ubi, vol->vol_id, lnum, buf, offs, to_read, 0); if (err) break; lnum += 1; offs = 0; total_read -= to_read; buf += to_read; } *retlen = len - total_read; return err; }
/** * ubi_leb_read - read data. * @desc: volume descriptor * @lnum: logical eraseblock number to read from * @buf: buffer where to store the read data * @offset: offset within the logical eraseblock to read from * @len: how many bytes to read * @check: whether UBI has to check the read data's CRC or not. * * This function reads data from offset @offset of logical eraseblock @lnum and * stores the data at @buf. When reading from static volumes, @check specifies * whether the data has to be checked or not. If yes, the whole logical * eraseblock will be read and its CRC checksum will be checked (i.e., the CRC * checksum is per-eraseblock). So checking may substantially slow down the * read speed. The @check argument is ignored for dynamic volumes. * * In case of success, this function returns zero. In case of failure, this * function returns a negative error code. * * %-EBADMSG error code is returned: * o for both static and dynamic volumes if MTD driver has detected a data * integrity problem (unrecoverable ECC checksum mismatch in case of NAND); * o for static volumes in case of data CRC mismatch. * * If the volume is damaged because of an interrupted update this function just * returns immediately with %-EBADF error code. */ int ubi_leb_read(struct ubi_volume_desc *desc, int lnum, char *buf, int offset, int len, int check) { struct ubi_volume *vol = desc->vol; struct ubi_device *ubi = vol->ubi; int err, vol_id = vol->vol_id; dbg_gen("read %d bytes from LEB %d:%d:%d", len, vol_id, lnum, offset); if (vol_id < 0 || vol_id >= ubi->vtbl_slots || lnum < 0 || lnum >= vol->used_ebs || offset < 0 || len < 0 || offset + len > vol->usable_leb_size) return -EINVAL; if (vol->vol_type == UBI_STATIC_VOLUME) { if (vol->used_ebs == 0) /* Empty static UBI volume */ return 0; if (lnum == vol->used_ebs - 1 && offset + len > vol->last_eb_bytes) return -EINVAL; } if (vol->upd_marker) return -EBADF; if (len == 0) return 0; err = ubi_eba_read_leb(ubi, vol, lnum, buf, offset, len, check); if (err && err == -EBADMSG && vol->vol_type == UBI_STATIC_VOLUME) { ubi_warn("mark volume %d as corrupted", vol_id); vol->corrupted = 1; } return err; }
/** * ubi_leb_read - read data. * @desc: volume descriptor * @lnum: logical eraseblock number to read from * @buf: buffer where to store the read data * @offset: offset within the logical eraseblock to read from * @len: how many bytes to read * @check: whether UBI has to check the read data's CRC or not. * * This function reads data from offset @offset of logical eraseblock @lnum and * stores the data at @buf. When reading from static volumes, @check specifies * whether the data has to be checked or not. If yes, the whole logical * eraseblock will be read and its CRC checksum will be checked (i.e., the CRC * checksum is per-eraseblock). So checking may substantially slow down the * read speed. The @check argument is ignored for dynamic volumes. * * In case of success, this function returns zero. In case of failure, this * function returns a negative error code. * * %-EBADMSG error code is returned: * o for both static and dynamic volumes if MTD driver has detected a data * integrity problem (unrecoverable ECC checksum mismatch in case of NAND); * o for static volumes in case of data CRC mismatch. * * If the volume is damaged because of an interrupted update this function just * returns immediately with %-EBADF error code. */ int ubi_leb_read(struct ubi_volume_desc *desc, int lnum, char *buf, int offset, int len, int check) { struct ubi_volume *vol = desc->vol; struct ubi_device *ubi = vol->ubi; int err, vol_id = vol->vol_id; dbg_gen("read %d bytes from LEB %d:%d:%d", len, vol_id, lnum, offset); err = leb_read_sanity_check(desc, lnum, offset, len); if (err < 0) return err; if (len == 0) return 0; err = ubi_eba_read_leb(ubi, vol, lnum, buf, offset, len, check); if (err && mtd_is_eccerr(err) && vol->vol_type == UBI_STATIC_VOLUME) { ubi_warn(ubi, "mark volume %d as corrupted", vol_id); vol->corrupted = 1; } return err; }
static ssize_t vol_cdev_read(struct file *file, __user char *buf, size_t count, loff_t *offp) { struct ubi_volume_desc *desc = file->private_data; struct ubi_volume *vol = desc->vol; struct ubi_device *ubi = vol->ubi; int err, lnum, off, len, tbuf_size; size_t count_save = count; void *tbuf; uint64_t tmp; dbg_msg("read %zd bytes from offset %lld of volume %d", count, *offp, vol->vol_id); if (vol->updating) { dbg_err("updating"); return -EBUSY; } if (vol->upd_marker) { dbg_err("damaged volume, update marker is set"); return -EBADF; } if (*offp == vol->used_bytes || count == 0) return 0; if (vol->corrupted) dbg_msg("read from corrupted volume %d", vol->vol_id); if (*offp + count > vol->used_bytes) count_save = count = vol->used_bytes - *offp; tbuf_size = vol->usable_leb_size; if (count < tbuf_size) tbuf_size = ALIGN(count, ubi->min_io_size); tbuf = vmalloc(tbuf_size); if (!tbuf) return -ENOMEM; len = count > tbuf_size ? tbuf_size : count; tmp = *offp; off = do_div(tmp, vol->usable_leb_size); lnum = tmp; do { cond_resched(); if (off + len >= vol->usable_leb_size) len = vol->usable_leb_size - off; err = ubi_eba_read_leb(ubi, vol, lnum, tbuf, off, len, 0); if (err) break; off += len; if (off == vol->usable_leb_size) { lnum += 1; off -= vol->usable_leb_size; } count -= len; *offp += len; err = copy_to_user(buf, tbuf, len); if (err) { err = -EFAULT; break; } buf += len; len = count > tbuf_size ? tbuf_size : count; } while (count); vfree(tbuf); return err ? err : count_save - count; }
static int ubi_volume_read(char *volume, char *buf, size_t size) { int err, lnum, off, len, tbuf_size, i = 0; size_t count_save = size; void *tbuf; unsigned long long tmp; struct ubi_volume *vol = NULL; 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; } printf("read %i bytes from volume %d to %x(buf address)\n", (int) size, vol->vol_id, (unsigned)buf); if (vol->updating) { printf("updating"); return -EBUSY; } if (vol->upd_marker) { printf("damaged volume, update marker is set"); return -EBADF; } if (offp == vol->used_bytes) return 0; if (size == 0) { printf("Read [%lu] bytes\n", (unsigned long) vol->used_bytes); size = vol->used_bytes; } if (vol->corrupted) printf("read from corrupted volume %d", vol->vol_id); if (offp + size > vol->used_bytes) count_save = size = vol->used_bytes - offp; tbuf_size = vol->usable_leb_size; if (size < tbuf_size) tbuf_size = ALIGN(size, ubi->min_io_size); tbuf = malloc(tbuf_size); if (!tbuf) { printf("NO MEM\n"); return -ENOMEM; } len = size > tbuf_size ? tbuf_size : size; tmp = offp; off = do_div(tmp, vol->usable_leb_size); lnum = tmp; do { if (off + len >= vol->usable_leb_size) len = vol->usable_leb_size - off; err = ubi_eba_read_leb(ubi, vol, lnum, tbuf, off, len, 0); if (err) { printf("read err %x\n", err); break; } off += len; if (off == vol->usable_leb_size) { lnum += 1; off -= vol->usable_leb_size; } size -= len; offp += len; memcpy(buf, tbuf, len); buf += len; len = size > tbuf_size ? tbuf_size : size; } while (size); free(tbuf); return err ? err : count_save - size; }
static int ubi_volume_read(char *volume, char *buf, size_t size, size_t offset) { int err, lnum, off, len, tbuf_size; void *tbuf; unsigned long long tmp; struct ubi_volume *vol; loff_t offp = 0; vol = ubi_find_volume(volume); if (vol == NULL) return ENODEV; offp = offset; printf("Read 0x%x bytes from volume [%s] offset 0x%x to %p\n", size, volume, offset, buf); if (vol->updating) { printf("updating"); return EBUSY; } if (vol->upd_marker) { printf("damaged volume, update marker is set"); return EBADF; } if (offp == vol->used_bytes) return 0; if (size == 0) { printf("No size specified -> Using max size (0x%lx)\n", (long)vol->used_bytes); size = vol->used_bytes; } if (vol->corrupted) printf("read from corrupted volume %d", vol->vol_id); if (offp + size > vol->used_bytes) size = vol->used_bytes - offp; tbuf_size = vol->usable_leb_size; if (size < tbuf_size) tbuf_size = ALIGN(size, ubi->min_io_size); tbuf = malloc(tbuf_size); if (!tbuf) { printf("NO MEM\n"); return ENOMEM; } len = size > tbuf_size ? tbuf_size : size; tmp = offp; off = do_div(tmp, vol->usable_leb_size); lnum = tmp; do { if (off + len >= vol->usable_leb_size) len = vol->usable_leb_size - off; 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; if (off == vol->usable_leb_size) { lnum += 1; off -= vol->usable_leb_size; } size -= len; offp += len; memcpy(buf, tbuf, len); buf += len; len = size > tbuf_size ? tbuf_size : size; } while (size); free(tbuf); debug("\n0x%x bytes read from volume %s\n", size, volume); return err; }