123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409 |
- /*
- * Copyright (c) International Business Machines Corp., 2006
- *
- * This program is free software; you can redistribute it and/or modify
- * it under the terms of the GNU General Public License as published by
- * the Free Software Foundation; either version 2 of the License, or
- * (at your option) any later version.
- *
- * 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., 675 Mass Ave, Cambridge, MA 02139, USA.
- *
- * Author: Artem B. Bityutskiy
- *
- * Test UBI volume read.
- */
- #include <stdio.h>
- #include <stdlib.h>
- #include <errno.h>
- #include <string.h>
- #include <unistd.h>
- #include <sys/types.h>
- #include <sys/stat.h>
- #include <fcntl.h>
- #include "libubi.h"
- #define PROGRAM_NAME "io_basic"
- #include "common.h"
- #include "helpers.h"
- static libubi_t libubi;
- static struct ubi_dev_info dev_info;
- const char *node;
- static int fd;
- /* Data lengthes to test, @io - minimal I/O unit size, @s - eraseblock size */
- #define LENGTHES(io, s) \
- {1, (io), (io)+1, 2*(io), 3*(io)-1, 3*(io), \
- MAX_NAND_PAGE_SIZE-1, MAX_NAND_PAGE_SIZE-(io), 2*MAX_NAND_PAGE_SIZE, 2*MAX_NAND_PAGE_SIZE-(io), \
- (s)/2-1, (s)/2, (s)/2+1, (s)-1, (s), (s)+1, 2*(s)-(io), 2*(s), \
- 2*(s)+(io), 3*(s), 3*(s)+(io)};
- /*
- * Offsets to test, @io - minimal I/O unit size, @s - eraseblock size, @sz -
- * volume size.
- */
- #define OFFSETS(io, s, sz) \
- {0, (io)-1, (io), (io)+1, 2*(io)-1, 2*(io), 3*(io)-1, 3*(io), \
- MAX_NAND_PAGE_SIZE-1, MAX_NAND_PAGE_SIZE-(io), 2*MAX_NAND_PAGE_SIZE, 2*MAX_NAND_PAGE_SIZE-(io), \
- (s)/2-1, (s)/2, (s)/2+1, (s)-1, (s), (s)+1, 2*(s)-(io), 2*(s), \
- 2*(s)+(io), 3*(s), (sz)-(s)-1, (sz)-(io)-1, (sz)-MAX_NAND_PAGE_SIZE-1};
- /**
- * test_static - test static volume-specific features.
- *
- * Thus function returns %0 in case of success and %-1 in case of failure.
- */
- static int test_static(void)
- {
- struct ubi_mkvol_request req;
- const char *name = PROGRAM_NAME ":io_basic()";
- char vol_node[strlen(UBI_VOLUME_PATTERN) + 100];
- struct ubi_vol_info vol_info;
- int fd, ret;
- char buf[20];
- req.vol_id = UBI_VOL_NUM_AUTO;
- req.alignment = 1;
- req.bytes = dev_info.avail_bytes;
- req.vol_type = UBI_STATIC_VOLUME;
- req.name = name;
- req.flags = 0;
- if (ubi_mkvol(libubi, node, &req)) {
- failed("ubi_mkvol");
- return -1;
- }
- sprintf(vol_node, UBI_VOLUME_PATTERN, dev_info.dev_num, req.vol_id);
- fd = open(vol_node, O_RDWR);
- if (fd == -1) {
- failed("open");
- errorm("cannot open \"%s\"\n", node);
- goto remove;
- }
- if (ubi_get_vol_info(libubi, vol_node, &vol_info)) {
- failed("ubi_get_vol_info");
- goto close;
- }
- /* Make sure new static volume contains no data */
- if (vol_info.data_bytes != 0) {
- errorm("data_bytes = %lld, not zero", vol_info.data_bytes);
- goto close;
- }
- /* Ensure read returns EOF */
- ret = read(fd, buf, 1);
- if (ret < 0) {
- failed("read");
- goto close;
- }
- if (ret != 0) {
- errorm("read data from free static volume");
- goto close;
- }
- if (ubi_update_start(libubi, fd, 10)) {
- failed("ubi_update_start");
- goto close;
- }
- ret = write(fd, buf, 10);
- if (ret < 0) {
- failed("write");
- goto close;
- }
- if (ret != 10) {
- errorm("written %d bytes", ret);
- goto close;
- }
- if (lseek(fd, 0, SEEK_SET) != 0) {
- failed("seek");
- goto close;
- }
- ret = read(fd, buf, 20);
- if (ret < 0) {
- failed("read");
- goto close;
- }
- if (ret != 10) {
- errorm("read %d bytes", ret);
- goto close;
- }
- close(fd);
- if (ubi_rmvol(libubi, node, req.vol_id)) {
- failed("ubi_rmvol");
- return -1;
- }
- return 0;
- close:
- close(fd);
- remove:
- ubi_rmvol(libubi, node, req.vol_id);
- return -1;
- }
- /*
- * A helper function for test_read2().
- */
- static int test_read3(const struct ubi_vol_info *vol_info, int len, off_t off)
- {
- int i, len1;
- unsigned char *ck_buf = NULL;
- unsigned char *buf = NULL;
- off_t new_off;
- int ret = -1;
- ck_buf = malloc(len);
- buf = malloc(len);
- if (!ck_buf || !buf) {
- failed("malloc");
- goto out;
- }
- if (off + len > vol_info->data_bytes)
- len1 = vol_info->data_bytes - off;
- else
- len1 = len;
- if (lseek(fd, off, SEEK_SET) != off) {
- failed("seek");
- errorm("len = %d", len);
- goto out;
- }
- if (read(fd, buf, len) != len1) {
- failed("read");
- errorm("len = %d", len);
- goto out;
- }
- new_off = lseek(fd, 0, SEEK_CUR);
- if (new_off != off + len1) {
- if (new_off == -1)
- failed("lseek");
- else
- errorm("read %d bytes from %lld, but resulting "
- "offset is %lld", len1, (long long) off, (long long) new_off);
- goto out;
- }
- for (i = 0; i < len1; i++)
- ck_buf[i] = (unsigned char)(off + i);
- if (memcmp(buf, ck_buf, len1)) {
- errorm("incorrect data read from offset %lld",
- (long long)off);
- errorm("len = %d", len);
- goto out;
- }
- ret = 0;
- out:
- free(buf);
- free(ck_buf);
- return ret;
- }
- /*
- * A helper function for test_read1().
- */
- static int test_read2(const struct ubi_vol_info *vol_info, int len)
- {
- int i;
- off_t offsets[] = OFFSETS(dev_info.min_io_size, vol_info->leb_size,
- vol_info->data_bytes);
- for (i = 0; i < sizeof(offsets)/sizeof(off_t); i++) {
- /* Filter invalid offset value */
- if (offsets[i] < 0 || offsets[i] > vol_info->data_bytes)
- continue;
- if (test_read3(vol_info, len, offsets[i])) {
- errorm("offset = %ld", offsets[i]);
- return -1;
- }
- }
- return 0;
- }
- /*
- * A helper function for test_read().
- */
- static int test_read1(struct ubi_vol_info *vol_info)
- {
- int i, written = 0;
- char vol_node[strlen(UBI_VOLUME_PATTERN) + 100];
- int lengthes[] = LENGTHES(dev_info.min_io_size, vol_info->leb_size);
- sprintf(vol_node, UBI_VOLUME_PATTERN, dev_info.dev_num,
- vol_info->vol_id);
- fd = open(vol_node, O_RDWR);
- if (fd == -1) {
- failed("open");
- errorm("cannot open \"%s\"\n", node);
- return -1;
- }
- /* Write some pattern to the volume */
- if (ubi_update_start(libubi, fd, vol_info->rsvd_bytes)) {
- failed("ubi_update_start");
- errorm("bytes = %lld", vol_info->rsvd_bytes);
- goto close;
- }
- while (written < vol_info->rsvd_bytes) {
- int i, ret;
- unsigned char buf[512];
- for (i = 0; i < 512; i++)
- buf[i] = (unsigned char)(written + i);
- ret = write(fd, buf, 512);
- if (ret == -1) {
- failed("write");
- errorm("written = %d, ret = %d", written, ret);
- goto close;
- }
- written += ret;
- }
- close(fd);
- if (ubi_get_vol_info(libubi, vol_node, vol_info)) {
- failed("ubi_get_vol_info");
- return -1;
- }
- fd = open(vol_node, O_RDONLY);
- if (fd == -1) {
- failed("open");
- errorm("cannot open \"%s\"\n", node);
- return -1;
- }
- for (i = 0; i < sizeof(lengthes)/sizeof(int); i++) {
- if (test_read2(vol_info, lengthes[i])) {
- errorm("length = %d", lengthes[i]);
- goto close;
- }
- }
- close(fd);
- return 0;
- close:
- close(fd);
- return -1;
- }
- /**
- * test_read - test UBI volume reading from different offsets.
- *
- * @type volume type (%UBI_DYNAMIC_VOLUME or %UBI_STATIC_VOLUME)
- *
- * Thus function returns %0 in case of success and %-1 in case of failure.
- */
- static int test_read(int type)
- {
- const char *name = PROGRAM_NAME ":test_read()";
- int alignments[] = ALIGNMENTS(dev_info.leb_size);
- char vol_node[strlen(UBI_VOLUME_PATTERN) + 100];
- struct ubi_mkvol_request req;
- int i;
- for (i = 0; i < sizeof(alignments)/sizeof(int); i++) {
- int leb_size;
- struct ubi_vol_info vol_info;
- req.vol_id = UBI_VOL_NUM_AUTO;
- req.vol_type = type;
- req.name = name;
- req.flags = 0;
- req.alignment = alignments[i];
- req.alignment -= req.alignment % dev_info.min_io_size;
- if (req.alignment == 0)
- req.alignment = dev_info.min_io_size;
- leb_size = dev_info.leb_size - dev_info.leb_size % req.alignment;
- req.bytes = MIN_AVAIL_EBS * leb_size;
- if (ubi_mkvol(libubi, node, &req)) {
- failed("ubi_mkvol");
- return -1;
- }
- sprintf(vol_node, UBI_VOLUME_PATTERN, dev_info.dev_num,
- req.vol_id);
- if (ubi_get_vol_info(libubi, vol_node, &vol_info)) {
- failed("ubi_get_vol_info");
- goto remove;
- }
- if (test_read1(&vol_info)) {
- errorm("alignment = %d", req.alignment);
- goto remove;
- }
- if (ubi_rmvol(libubi, node, req.vol_id)) {
- failed("ubi_rmvol");
- return -1;
- }
- }
- return 0;
- remove:
- ubi_rmvol(libubi, node, req.vol_id);
- return -1;
- }
- int main(int argc, char * const argv[])
- {
- if (initial_check(argc, argv))
- return 1;
- node = argv[1];
- libubi = libubi_open();
- if (libubi == NULL) {
- failed("libubi_open");
- return 1;
- }
- if (ubi_get_dev_info(libubi, node, &dev_info)) {
- failed("ubi_get_dev_info");
- goto close;
- }
- if (test_static())
- goto close;
- if (test_read(UBI_DYNAMIC_VOLUME))
- goto close;
- if (test_read(UBI_STATIC_VOLUME))
- goto close;
- libubi_close(libubi);
- return 0;
- close:
- libubi_close(libubi);
- return 1;
- }
|