io_basic.c 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182
  1. /*
  2. * Copyright (c) International Business Machines Corp., 2006
  3. *
  4. * This program is free software; you can redistribute it and/or modify
  5. * it under the terms of the GNU General Public License as published by
  6. * the Free Software Foundation; either version 2 of the License, or
  7. * (at your option) any later version.
  8. *
  9. * This program is distributed in the hope that it will be useful,
  10. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  11. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See
  12. * the GNU General Public License for more details.
  13. *
  14. * You should have received a copy of the GNU General Public License
  15. * along with this program; if not, write to the Free Software
  16. * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
  17. *
  18. * Author: Artem B. Bityutskiy
  19. *
  20. * Test basic UBI volume I/O capabilities.
  21. */
  22. #include <stdio.h>
  23. #include <stdlib.h>
  24. #include <errno.h>
  25. #include <string.h>
  26. #include <unistd.h>
  27. #include <sys/types.h>
  28. #include <sys/stat.h>
  29. #include <fcntl.h>
  30. #include "libubi.h"
  31. #define PROGRAM_NAME "io_basic"
  32. #include "common.h"
  33. #include "helpers.h"
  34. static libubi_t libubi;
  35. static struct ubi_dev_info dev_info;
  36. const char *node;
  37. /**
  38. * test_basic - check basic volume read and update capabilities.
  39. *
  40. * @type volume type (%UBI_DYNAMIC_VOLUME or %UBI_STATIC_VOLUME)
  41. *
  42. * Thus function returns %0 in case of success and %-1 in case of failure.
  43. */
  44. static int test_basic(int type)
  45. {
  46. struct ubi_mkvol_request req;
  47. const char *name = PROGRAM_NAME ":test_basic()";
  48. char vol_node[strlen(UBI_VOLUME_PATTERN) + 100];
  49. req.vol_id = UBI_VOL_NUM_AUTO;
  50. req.alignment = 1;
  51. req.bytes = dev_info.avail_bytes;
  52. req.vol_type = type;
  53. req.name = name;
  54. req.flags = 0;
  55. if (ubi_mkvol(libubi, node, &req)) {
  56. failed("ubi_mkvol");
  57. return -1;
  58. }
  59. sprintf(vol_node, UBI_VOLUME_PATTERN, dev_info.dev_num, req.vol_id);
  60. /* Make sure newly created volume contains only 0xFF bytes */
  61. if (check_vol_patt(vol_node, 0xFF))
  62. goto remove;
  63. /* Write 0xA5 bytes to the volume */
  64. if (update_vol_patt(vol_node, dev_info.avail_bytes, 0xA5))
  65. goto remove;
  66. if (check_vol_patt(vol_node, 0xA5))
  67. goto remove;
  68. if (ubi_rmvol(libubi, node, req.vol_id)) {
  69. failed("ubi_rmvol");
  70. return -1;
  71. }
  72. return 0;
  73. remove:
  74. ubi_rmvol(libubi, node, req.vol_id);
  75. return -1;
  76. }
  77. /**
  78. * test_aligned - test volume alignment feature.
  79. *
  80. * @type volume type (%UBI_DYNAMIC_VOLUME or %UBI_STATIC_VOLUME)
  81. *
  82. * Thus function returns %0 in case of success and %-1 in case of failure.
  83. */
  84. static int test_aligned(int type)
  85. {
  86. unsigned int i, ebsz;
  87. struct ubi_mkvol_request req;
  88. const char *name = PROGRAM_NAME ":test_aligned()";
  89. char vol_node[strlen(UBI_VOLUME_PATTERN) + 100];
  90. int alignments[] = ALIGNMENTS(dev_info.leb_size);
  91. req.vol_type = type;
  92. req.name = name;
  93. req.flags = 0;
  94. for (i = 0; i < sizeof(alignments)/sizeof(int); i++) {
  95. req.vol_id = UBI_VOL_NUM_AUTO;
  96. req.alignment = alignments[i];
  97. req.alignment -= req.alignment % dev_info.min_io_size;
  98. if (req.alignment == 0)
  99. req.alignment = dev_info.min_io_size;
  100. ebsz = dev_info.leb_size - dev_info.leb_size % req.alignment;
  101. req.bytes = MIN_AVAIL_EBS * ebsz;
  102. if (ubi_mkvol(libubi, node, &req)) {
  103. failed("ubi_mkvol");
  104. return -1;
  105. }
  106. sprintf(vol_node, UBI_VOLUME_PATTERN, dev_info.dev_num, req.vol_id);
  107. /* Make sure newly created volume contains only 0xFF bytes */
  108. if (check_vol_patt(vol_node, 0xFF))
  109. goto remove;
  110. /* Write 0xA5 bytes to the volume */
  111. if (update_vol_patt(vol_node, req.bytes, 0xA5))
  112. goto remove;
  113. if (check_vol_patt(vol_node, 0xA5))
  114. goto remove;
  115. if (ubi_rmvol(libubi, node, req.vol_id)) {
  116. failed("ubi_rmvol");
  117. return -1;
  118. }
  119. }
  120. return 0;
  121. remove:
  122. ubi_rmvol(libubi, node, req.vol_id);
  123. return -1;
  124. }
  125. int main(int argc, char * const argv[])
  126. {
  127. if (initial_check(argc, argv))
  128. return 1;
  129. node = argv[1];
  130. libubi = libubi_open();
  131. if (libubi == NULL) {
  132. failed("libubi_open");
  133. return 1;
  134. }
  135. if (ubi_get_dev_info(libubi, node, &dev_info)) {
  136. failed("ubi_get_dev_info");
  137. goto close;
  138. }
  139. if (test_basic(UBI_DYNAMIC_VOLUME))
  140. goto close;
  141. if (test_basic(UBI_STATIC_VOLUME))
  142. goto close;
  143. if (test_aligned(UBI_DYNAMIC_VOLUME))
  144. goto close;
  145. if (test_aligned(UBI_STATIC_VOLUME))
  146. goto close;
  147. libubi_close(libubi);
  148. return 0;
  149. close:
  150. libubi_close(libubi);
  151. return 1;
  152. }