helpers.c 8.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365
  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. * The stuff which is common for many tests.
  21. */
  22. #include <stdarg.h>
  23. #include <stdlib.h>
  24. #include <stdio.h>
  25. #include <errno.h>
  26. #include <string.h>
  27. #include <unistd.h>
  28. #include <limits.h>
  29. #include <sys/types.h>
  30. #include <sys/stat.h>
  31. #include <sys/time.h>
  32. #include <fcntl.h>
  33. #include "libubi.h"
  34. #include "helpers.h"
  35. /**
  36. * __initial_check - check that common prerequisites which are required to run
  37. * tests.
  38. *
  39. * @test test name
  40. * @argc count of command-line arguments
  41. * @argv command-line arguments
  42. *
  43. * This function returns %0 if all is fine and test may be run and %-1 if not.
  44. */
  45. int __initial_check(const char *test, int argc, char * const argv[])
  46. {
  47. libubi_t libubi;
  48. struct ubi_dev_info dev_info;
  49. /*
  50. * All tests require UBI character device name as the first parameter,
  51. * check this.
  52. */
  53. if (argc < 2) {
  54. __errorm(test, __func__, __LINE__,
  55. "UBI character device node is not specified");
  56. return -1;
  57. }
  58. libubi = libubi_open();
  59. if (libubi == NULL) {
  60. __failed(test, __func__, __LINE__, "libubi_open");
  61. return -1;
  62. }
  63. if (ubi_get_dev_info(libubi, argv[1], &dev_info)) {
  64. __failed(test, __func__, __LINE__, "ubi_get_dev_info");
  65. goto close;
  66. }
  67. if (dev_info.avail_lebs < MIN_AVAIL_EBS) {
  68. __errorm(test, __func__, __LINE__,
  69. "insufficient available eraseblocks %d on UBI "
  70. "device, required %d",
  71. dev_info.avail_lebs, MIN_AVAIL_EBS);
  72. goto close;
  73. }
  74. if (dev_info.vol_count != 0) {
  75. __errorm(test, __func__, __LINE__,
  76. "device %s is not empty", argv[1]);
  77. goto close;
  78. }
  79. libubi_close(libubi);
  80. return 0;
  81. close:
  82. libubi_close(libubi);
  83. return -1;
  84. }
  85. /**
  86. * __errorm - print a message to stderr.
  87. *
  88. * @test test name
  89. * @func function name
  90. * @line line number
  91. * @fmt format string
  92. */
  93. void __errorm(const char *test, const char *func, int line,
  94. const char *fmt, ...)
  95. {
  96. va_list args;
  97. fprintf(stderr, "[%s] %s():%d: ", test, func, line);
  98. va_start(args, fmt);
  99. vfprintf(stderr, fmt, args);
  100. fprintf(stderr, "\n");
  101. va_end(args);
  102. }
  103. /**
  104. * __failed - print function fail message.
  105. *
  106. * @test test name
  107. * @func calling function name
  108. * @line line number
  109. * @failed failed function name
  110. */
  111. void __failed(const char *test, const char *func, int line,
  112. const char *failed)
  113. {
  114. fprintf(stderr, "[%s] %s():%d: function %s() failed with error %d (%s)\n",
  115. test, func, line, failed, errno, strerror(errno));
  116. }
  117. /**
  118. * __check_volume - check volume information.
  119. *
  120. * @libubi libubi descriptor
  121. * @dev_info UBI device description
  122. * @test test name
  123. * @func function name
  124. * @line line number
  125. * @vol_id ID of existing volume to check
  126. * @req volume creation request to compare with
  127. *
  128. * This function checks if a volume created using @req request has exactly the
  129. * requested characteristics. Returns 0 in case of success and %-1 in case of
  130. * error.
  131. */
  132. int __check_volume(libubi_t libubi, struct ubi_dev_info *dev_info,
  133. const char *test, const char *func, int line, int vol_id,
  134. const struct ubi_mkvol_request *req)
  135. {
  136. int ret;
  137. struct ubi_vol_info vol_info;
  138. int leb_size;
  139. long long rsvd_bytes;
  140. ret = ubi_get_vol_info1(libubi, dev_info->dev_num, vol_id, &vol_info);
  141. if (ret) {
  142. __failed(test, func, line, "ubi_get_vol_info");
  143. return -1;
  144. }
  145. if (req->alignment != vol_info.alignment) {
  146. __errorm(test, func, line,
  147. "bad alignment: requested %d, got %d",
  148. req->alignment, vol_info.alignment);
  149. return -1;
  150. }
  151. if (req->vol_type != vol_info.type) {
  152. __errorm(test, func, line, "bad type: requested %d, got %d",
  153. req->vol_type, vol_info.type);
  154. return -1;
  155. }
  156. if (strlen(req->name) != strlen(vol_info.name) ||
  157. strcmp(req->name, vol_info.name) != 0) {
  158. __errorm(test, func, line,
  159. "bad name: requested \"%s\", got \"%s\"",
  160. req->name, vol_info.name);
  161. return -1;
  162. }
  163. if (vol_info.corrupted) {
  164. __errorm(test, func, line, "corrupted new volume");
  165. return -1;
  166. }
  167. leb_size = dev_info->leb_size - (dev_info->leb_size % req->alignment);
  168. if (leb_size != vol_info.leb_size) {
  169. __errorm(test, func, line,
  170. "bad usable LEB size %d, should be %d",
  171. vol_info.leb_size, leb_size);
  172. return -1;
  173. }
  174. rsvd_bytes = req->bytes;
  175. if (rsvd_bytes % leb_size)
  176. rsvd_bytes += leb_size - (rsvd_bytes % leb_size);
  177. if (rsvd_bytes != vol_info.rsvd_bytes) {
  178. __errorm(test, func, line,
  179. "bad reserved bytes %lld, should be %lld",
  180. vol_info.rsvd_bytes, rsvd_bytes);
  181. return -1;
  182. }
  183. return 0;
  184. }
  185. /**
  186. * __check_vol_patt - check that volume contains certain data
  187. *
  188. * @libubi libubi descriptor
  189. * @test test name
  190. * @func function name
  191. * @line line number
  192. * @node volume character device node
  193. * @byte data pattern to check
  194. *
  195. * This function returns %0 if the volume contains only @byte bytes, and %-1 if
  196. * not.
  197. */
  198. int __check_vol_patt(libubi_t libubi, const char *test, const char *func,
  199. int line, const char *node, uint8_t byte)
  200. {
  201. int ret, fd;
  202. long long bytes = 0;
  203. struct ubi_vol_info vol_info;
  204. unsigned char buf[512];
  205. fd = open(node, O_RDONLY);
  206. if (fd == -1) {
  207. __failed(test, func, line, "open");
  208. __errorm(test, func, line, "cannot open \"%s\"\n", node);
  209. return -1;
  210. }
  211. ret = ubi_get_vol_info(libubi, node, &vol_info);
  212. if (ret) {
  213. __failed(test, func, line, "ubi_get_vol_info");
  214. goto close;
  215. }
  216. while (bytes < vol_info.data_bytes) {
  217. int i;
  218. memset(buf, ~byte, 512);
  219. ret = read(fd, buf, 512);
  220. if (ret == -1) {
  221. __failed(test, func, line, "read");
  222. __errorm(test, func, line, "bytes = %lld, ret = %d",
  223. bytes, ret);
  224. goto close;
  225. }
  226. if (ret == 0 && bytes + ret < vol_info.data_bytes) {
  227. __errorm(test, func, line,
  228. "EOF, but read only %lld bytes of %lld",
  229. bytes + ret, vol_info.data_bytes);
  230. goto close;
  231. }
  232. for (i = 0; i < ret; i++)
  233. if (buf[i] != byte) {
  234. __errorm(test, func, line,
  235. "byte at %lld is not %#x but %#x",
  236. bytes + i, byte, (int)buf[i]);
  237. goto close;
  238. }
  239. bytes += ret;
  240. }
  241. close(fd);
  242. return 0;
  243. close:
  244. close(fd);
  245. return -1;
  246. }
  247. /**
  248. * __update_vol_patt - update volume using a certain byte pattern
  249. *
  250. * @libubi libubi descriptor
  251. * @dev_info UBI device description
  252. * @test test name
  253. * @func function name
  254. * @line line number
  255. * @node volume character device node
  256. * @byte data pattern to check
  257. *
  258. * This function returns %0 in case of success, and %-1 if in case of failure.
  259. */
  260. int __update_vol_patt(libubi_t libubi, const char *test, const char *func,
  261. int line, const char *node, long long bytes, uint8_t byte)
  262. {
  263. int ret, fd;
  264. long long written = 0;
  265. unsigned char buf[512];
  266. fd = open(node, O_RDWR);
  267. if (fd == -1) {
  268. __failed(test, func, line, "open");
  269. __errorm(test, func, line, "cannot open \"%s\"\n", node);
  270. return -1;
  271. }
  272. if (ubi_update_start(libubi, fd, bytes)) {
  273. __failed(test, func, line, "ubi_update_start");
  274. __errorm(test, func, line, "bytes = %lld", bytes);
  275. goto close;
  276. }
  277. memset(buf, byte, 512);
  278. while (written != bytes) {
  279. ret = write(fd, buf, 512);
  280. if (ret == -1) {
  281. __failed(test, func, line, "write");
  282. __errorm(test, func, line, "written = %lld, ret = %d",
  283. written, ret);
  284. goto close;
  285. }
  286. written += ret;
  287. if (written > bytes) {
  288. __errorm(test, func, line, "update length %lld bytes, "
  289. "but %lld bytes are already written",
  290. bytes, written);
  291. goto close;
  292. }
  293. }
  294. close(fd);
  295. return 0;
  296. close:
  297. close(fd);
  298. return -1;
  299. }
  300. /**
  301. * seed_random_generator - randomly seed the standard pseudo-random generator.
  302. *
  303. * This helper function seeds the standard libc pseudo-random generator with a
  304. * more or less random value to make sure the 'rand()' call does not return the
  305. * same sequence every time UBI utilities run. Returns the random seed in case
  306. * of success and a %-1 in case of error.
  307. */
  308. int seed_random_generator(void)
  309. {
  310. struct timeval tv;
  311. struct timezone tz;
  312. int seed;
  313. /*
  314. * Just assume that a combination of the PID + current time is a
  315. * reasonably random number.
  316. */
  317. if (gettimeofday(&tv, &tz))
  318. return -1;
  319. seed = (unsigned int)tv.tv_sec;
  320. seed += (unsigned int)tv.tv_usec;
  321. seed *= getpid();
  322. seed %= INT_MAX;
  323. srand(seed);
  324. return seed;
  325. }