libubi.c 30 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404
  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 Bityutskiy
  19. *
  20. * UBI (Unsorted Block Images) library.
  21. */
  22. #define PROGRAM_NAME "libubi"
  23. #include <sys/sysmacros.h>
  24. #include <stdlib.h>
  25. #include <stdio.h>
  26. #include <string.h>
  27. #include <fcntl.h>
  28. #include <dirent.h>
  29. #include <unistd.h>
  30. #include <limits.h>
  31. #include <sys/ioctl.h>
  32. #include <sys/stat.h>
  33. #include <sys/types.h>
  34. #include "libubi.h"
  35. #include "libubi_int.h"
  36. /**
  37. * mkpath - compose full path from 2 given components.
  38. * @path: the first component
  39. * @name: the second component
  40. *
  41. * This function returns the resulting path in case of success and %NULL in
  42. * case of failure.
  43. */
  44. static char *mkpath(const char *path, const char *name)
  45. {
  46. char *n;
  47. int len1 = strlen(path);
  48. int len2 = strlen(name);
  49. n = malloc(len1 + len2 + 2);
  50. if (!n) {
  51. sys_errmsg("cannot allocate %d bytes", len1 + len2 + 2);
  52. return NULL;
  53. }
  54. memcpy(n, path, len1);
  55. if (n[len1 - 1] != '/')
  56. n[len1++] = '/';
  57. memcpy(n + len1, name, len2 + 1);
  58. return n;
  59. }
  60. /**
  61. * read_positive_ll - read a positive 'long long' value from a file.
  62. * @file: the file to read from
  63. * @value: the result is stored here
  64. *
  65. * This function reads file @file and interprets its contents as a positive
  66. * 'long long' integer. If this is not true, it fails with %EINVAL error code.
  67. * Returns %0 in case of success and %-1 in case of failure.
  68. */
  69. static int read_positive_ll(const char *file, long long *value)
  70. {
  71. int fd, rd;
  72. char buf[50];
  73. fd = open(file, O_RDONLY);
  74. if (fd == -1)
  75. return -1;
  76. rd = read(fd, buf, sizeof(buf));
  77. if (rd == -1) {
  78. sys_errmsg("cannot read \"%s\"", file);
  79. goto out_error;
  80. }
  81. if (rd == sizeof(buf)) {
  82. errmsg("contents of \"%s\" is too long", file);
  83. errno = EINVAL;
  84. goto out_error;
  85. }
  86. buf[rd] = '\0';
  87. if (sscanf(buf, "%lld\n", value) != 1) {
  88. errmsg("cannot read integer from \"%s\"\n", file);
  89. errno = EINVAL;
  90. goto out_error;
  91. }
  92. if (*value < 0) {
  93. errmsg("negative value %lld in \"%s\"", *value, file);
  94. errno = EINVAL;
  95. goto out_error;
  96. }
  97. if (close(fd)) {
  98. sys_errmsg("close failed on \"%s\"", file);
  99. return -1;
  100. }
  101. return 0;
  102. out_error:
  103. close(fd);
  104. return -1;
  105. }
  106. /**
  107. * read_positive_int - read a positive 'int' value from a file.
  108. * @file: the file to read from
  109. * @value: the result is stored here
  110. *
  111. * This function is the same as 'read_positive_ll()', but it reads an 'int'
  112. * value, not 'long long'.
  113. */
  114. static int read_positive_int(const char *file, int *value)
  115. {
  116. long long res;
  117. if (read_positive_ll(file, &res))
  118. return -1;
  119. /* Make sure the value is not too big */
  120. if (res > INT_MAX) {
  121. errmsg("value %lld read from file \"%s\" is out of range",
  122. res, file);
  123. errno = EINVAL;
  124. return -1;
  125. }
  126. *value = res;
  127. return 0;
  128. }
  129. /**
  130. * read_data - read data from a file.
  131. * @file: the file to read from
  132. * @buf: the buffer to read to
  133. * @buf_len: buffer length
  134. *
  135. * This function returns number of read bytes in case of success and %-1 in
  136. * case of failure. Note, if the file contains more then @buf_len bytes of
  137. * date, this function fails with %EINVAL error code.
  138. */
  139. static int read_data(const char *file, void *buf, int buf_len)
  140. {
  141. int fd, rd, tmp, tmp1;
  142. fd = open(file, O_RDONLY);
  143. if (fd == -1)
  144. return -1;
  145. rd = read(fd, buf, buf_len);
  146. if (rd == -1) {
  147. sys_errmsg("cannot read \"%s\"", file);
  148. goto out_error;
  149. }
  150. if (rd == buf_len) {
  151. errmsg("contents of \"%s\" is too long", file);
  152. errno = EINVAL;
  153. goto out_error;
  154. }
  155. ((char *)buf)[rd] = '\0';
  156. /* Make sure all data is read */
  157. tmp1 = read(fd, &tmp, 4);
  158. if (tmp1 < 0) {
  159. sys_errmsg("cannot read \"%s\"", file);
  160. goto out_error;
  161. }
  162. if (tmp1) {
  163. errmsg("file \"%s\" contains too much data (> %d bytes)",
  164. file, buf_len);
  165. errno = EINVAL;
  166. goto out_error;
  167. }
  168. if (close(fd)) {
  169. sys_errmsg("close failed on \"%s\"", file);
  170. return -1;
  171. }
  172. return rd;
  173. out_error:
  174. close(fd);
  175. return -1;
  176. }
  177. /**
  178. * read_major - read major and minor numbers from a file.
  179. * @file: name of the file to read from
  180. * @major: major number is returned here
  181. * @minor: minor number is returned here
  182. *
  183. * This function returns % in case of succes, and %-1 in case of failure.
  184. */
  185. static int read_major(const char *file, int *major, int *minor)
  186. {
  187. int ret;
  188. char buf[50];
  189. ret = read_data(file, buf, 50);
  190. if (ret < 0)
  191. return ret;
  192. ret = sscanf(buf, "%d:%d\n", major, minor);
  193. if (ret != 2) {
  194. errno = EINVAL;
  195. errmsg("\"%s\" does not have major:minor format", file);
  196. return -1;
  197. }
  198. if (*major < 0 || *minor < 0) {
  199. errno = EINVAL;
  200. errmsg("bad major:minor %d:%d in \"%s\"",
  201. *major, *minor, file);
  202. return -1;
  203. }
  204. return 0;
  205. }
  206. /**
  207. * dev_read_int - read a positive 'int' value from an UBI device sysfs file.
  208. * @patt: file pattern to read from
  209. * @dev_num: UBI device number
  210. * @value: the result is stored here
  211. *
  212. * This function returns %0 in case of success and %-1 in case of failure.
  213. */
  214. static int dev_read_int(const char *patt, int dev_num, int *value)
  215. {
  216. char file[strlen(patt) + 50];
  217. sprintf(file, patt, dev_num);
  218. return read_positive_int(file, value);
  219. }
  220. /**
  221. * vol_read_int - read a positive 'int' value from an UBI volume sysfs file.
  222. * @patt: file pattern to read from
  223. * @dev_num: UBI device number
  224. * @vol_id: volume ID
  225. * @value: the result is stored here
  226. *
  227. * This function returns %0 in case of success and %-1 in case of failure.
  228. */
  229. static int vol_read_int(const char *patt, int dev_num, int vol_id, int *value)
  230. {
  231. char file[strlen(patt) + 100];
  232. sprintf(file, patt, dev_num, vol_id);
  233. return read_positive_int(file, value);
  234. }
  235. /**
  236. * dev_read_ll - read a positive 'long long' value from an UBI device sysfs file.
  237. * @patt: file pattern to read from
  238. * @dev_num: UBI device number
  239. * @value: the result is stored here
  240. *
  241. * This function returns %0 in case of success and %-1 in case of failure.
  242. */
  243. static int dev_read_ll(const char *patt, int dev_num, long long *value)
  244. {
  245. char file[strlen(patt) + 50];
  246. sprintf(file, patt, dev_num);
  247. return read_positive_ll(file, value);
  248. }
  249. /**
  250. * vol_read_ll - read a positive 'long long' value from an UBI volume sysfs file.
  251. * @patt: file pattern to read from
  252. * @dev_num: UBI device number
  253. * @vol_id: volume ID
  254. * @value: the result is stored here
  255. *
  256. * This function returns %0 in case of success and %-1 in case of failure.
  257. */
  258. static int vol_read_ll(const char *patt, int dev_num, int vol_id,
  259. long long *value)
  260. {
  261. char file[strlen(patt) + 100];
  262. sprintf(file, patt, dev_num, vol_id);
  263. return read_positive_ll(file, value);
  264. }
  265. /**
  266. * vol_read_data - read data from an UBI volume's sysfs file.
  267. * @patt: file pattern to read from
  268. * @dev_num: UBI device number
  269. * @vol_id: volume ID
  270. * @buf: buffer to read to
  271. * @buf_len: buffer length
  272. *
  273. * This function returns number of read bytes in case of success and %-1 in
  274. * case of failure.
  275. */
  276. static int vol_read_data(const char *patt, int dev_num, int vol_id, void *buf,
  277. int buf_len)
  278. {
  279. char file[strlen(patt) + 100];
  280. sprintf(file, patt, dev_num, vol_id);
  281. return read_data(file, buf, buf_len);
  282. }
  283. /**
  284. * dev_get_major - get major and minor numbers of an UBI device.
  285. * @lib: libubi descriptor
  286. * @dev_num: UBI device number
  287. * @major: major number is returned here
  288. * @minor: minor number is returned here
  289. *
  290. * This function returns zero in case of succes and %-1 in case of failure.
  291. */
  292. static int dev_get_major(struct libubi *lib, int dev_num, int *major, int *minor)
  293. {
  294. char file[strlen(lib->dev_dev) + 50];
  295. sprintf(file, lib->dev_dev, dev_num);
  296. return read_major(file, major, minor);
  297. }
  298. /**
  299. * vol_get_major - get major and minor numbers of an UBI volume.
  300. * @lib: libubi descriptor
  301. * @dev_num: UBI device number
  302. * @vol_id: volume ID
  303. * @major: major number is returned here
  304. * @minor: minor number is returned here
  305. *
  306. * This function returns zero in case of succes and %-1 in case of failure.
  307. */
  308. static int vol_get_major(struct libubi *lib, int dev_num, int vol_id,
  309. int *major, int *minor)
  310. {
  311. char file[strlen(lib->vol_dev) + 100];
  312. sprintf(file, lib->vol_dev, dev_num, vol_id);
  313. return read_major(file, major, minor);
  314. }
  315. /**
  316. * vol_node2nums - find UBI device number and volume ID by volume device node
  317. * file.
  318. * @lib: UBI library descriptor
  319. * @node: UBI character device node name
  320. * @dev_num: UBI device number is returned here
  321. * @vol_id: volume ID is returned hers
  322. *
  323. * This function returns zero in case of succes and %-1 in case of failure.
  324. */
  325. static int vol_node2nums(struct libubi *lib, const char *node, int *dev_num,
  326. int *vol_id)
  327. {
  328. struct stat st;
  329. struct ubi_info info;
  330. int i, fd, major, minor;
  331. char file[strlen(lib->ubi_vol) + 100];
  332. if (stat(node, &st)) {
  333. sys_errmsg("cannot get information about \"%s\"",
  334. node);
  335. return -1;
  336. }
  337. if (!S_ISCHR(st.st_mode)) {
  338. errno = EINVAL;
  339. errmsg("\"%s\" is not a character device", node);
  340. return -1;
  341. }
  342. major = major(st.st_rdev);
  343. minor = minor(st.st_rdev);
  344. if (minor == 0) {
  345. errno = EINVAL;
  346. errmsg("\"%s\" is not a volume character device", node);
  347. return -1;
  348. }
  349. if (ubi_get_info((libubi_t *)lib, &info))
  350. return -1;
  351. for (i = info.lowest_dev_num; i <= info.highest_dev_num; i++) {
  352. int major1, minor1, ret;
  353. ret = dev_get_major(lib, i, &major1, &minor1);
  354. if (ret) {
  355. if (errno == ENOENT)
  356. continue;
  357. return -1;
  358. }
  359. if (major1 == major)
  360. break;
  361. }
  362. if (i > info.highest_dev_num) {
  363. errno = ENODEV;
  364. return -1;
  365. }
  366. /* Make sure this UBI volume exists */
  367. sprintf(file, lib->ubi_vol, i, minor - 1);
  368. fd = open(file, O_RDONLY);
  369. if (fd < 0) {
  370. errno = ENODEV;
  371. return -1;
  372. }
  373. close(fd);
  374. *dev_num = i;
  375. *vol_id = minor - 1;
  376. errno = 0;
  377. return 0;
  378. }
  379. /**
  380. * dev_node2num - find UBI device number by its character device node.
  381. * @lib: UBI library descriptor
  382. * @node: UBI character device node name
  383. * @dev_num: UBI device number is returned here
  384. *
  385. * This function returns %0 in case of success and %-1 in case of failure.
  386. */
  387. static int dev_node2num(struct libubi *lib, const char *node, int *dev_num)
  388. {
  389. struct stat st;
  390. struct ubi_info info;
  391. int i, major, minor;
  392. if (stat(node, &st)) {
  393. sys_errmsg("cannot get information about \"%s\"", node);
  394. return -1;
  395. }
  396. if (!S_ISCHR(st.st_mode)) {
  397. errno = EINVAL;
  398. errmsg("\"%s\" is not a character device", node);
  399. return -1;
  400. }
  401. major = major(st.st_rdev);
  402. minor = minor(st.st_rdev);
  403. if (minor != 0) {
  404. errno = EINVAL;
  405. errmsg("\"%s\" is not an UBI character device", node);
  406. return -1;
  407. }
  408. if (ubi_get_info((libubi_t *)lib, &info))
  409. return -1;
  410. for (i = info.lowest_dev_num; i <= info.highest_dev_num; i++) {
  411. int major1, minor1, ret;
  412. ret = dev_get_major(lib, i, &major1, &minor1);
  413. if (ret) {
  414. if (errno == ENOENT)
  415. continue;
  416. return -1;
  417. }
  418. if (major1 == major) {
  419. if (minor1 != 0) {
  420. errmsg("UBI character device minor number is "
  421. "%d, but must be 0", minor1);
  422. errno = EINVAL;
  423. return -1;
  424. }
  425. errno = 0;
  426. *dev_num = i;
  427. return 0;
  428. }
  429. }
  430. errno = ENODEV;
  431. return -1;
  432. }
  433. int mtd_num2ubi_dev(libubi_t desc, int mtd_num, int *dev_num)
  434. {
  435. struct ubi_info info;
  436. int i, ret, mtd_num1;
  437. struct libubi *lib = desc;
  438. if (ubi_get_info(desc, &info))
  439. return -1;
  440. for (i = info.lowest_dev_num; i <= info.highest_dev_num; i++) {
  441. ret = dev_read_int(lib->dev_mtd_num, i, &mtd_num1);
  442. if (ret) {
  443. if (errno == ENOENT)
  444. continue;
  445. return -1;
  446. }
  447. if (mtd_num1 == mtd_num) {
  448. errno = 0;
  449. *dev_num = i;
  450. return 0;
  451. }
  452. }
  453. errno = 0;
  454. return -1;
  455. }
  456. libubi_t libubi_open(void)
  457. {
  458. int fd, version;
  459. struct libubi *lib;
  460. lib = calloc(1, sizeof(struct libubi));
  461. if (!lib)
  462. return NULL;
  463. lib->sysfs_ctrl = mkpath("/sys", SYSFS_CTRL);
  464. if (!lib->sysfs_ctrl)
  465. goto out_error;
  466. lib->ctrl_dev = mkpath(lib->sysfs_ctrl, CTRL_DEV);
  467. if (!lib->ctrl_dev)
  468. goto out_error;
  469. lib->sysfs_ubi = mkpath("/sys", SYSFS_UBI);
  470. if (!lib->sysfs_ubi)
  471. goto out_error;
  472. /* Make sure UBI is present */
  473. fd = open(lib->sysfs_ubi, O_RDONLY);
  474. if (fd == -1) {
  475. errno = 0;
  476. goto out_error;
  477. }
  478. if (close(fd)) {
  479. sys_errmsg("close failed on \"%s\"", lib->sysfs_ubi);
  480. goto out_error;
  481. }
  482. lib->ubi_dev = mkpath(lib->sysfs_ubi, UBI_DEV_NAME_PATT);
  483. if (!lib->ubi_dev)
  484. goto out_error;
  485. lib->ubi_version = mkpath(lib->sysfs_ubi, UBI_VER);
  486. if (!lib->ubi_version)
  487. goto out_error;
  488. lib->dev_dev = mkpath(lib->ubi_dev, DEV_DEV);
  489. if (!lib->dev_dev)
  490. goto out_error;
  491. lib->dev_avail_ebs = mkpath(lib->ubi_dev, DEV_AVAIL_EBS);
  492. if (!lib->dev_avail_ebs)
  493. goto out_error;
  494. lib->dev_total_ebs = mkpath(lib->ubi_dev, DEV_TOTAL_EBS);
  495. if (!lib->dev_total_ebs)
  496. goto out_error;
  497. lib->dev_bad_count = mkpath(lib->ubi_dev, DEV_BAD_COUNT);
  498. if (!lib->dev_bad_count)
  499. goto out_error;
  500. lib->dev_eb_size = mkpath(lib->ubi_dev, DEV_EB_SIZE);
  501. if (!lib->dev_eb_size)
  502. goto out_error;
  503. lib->dev_max_ec = mkpath(lib->ubi_dev, DEV_MAX_EC);
  504. if (!lib->dev_max_ec)
  505. goto out_error;
  506. lib->dev_bad_rsvd = mkpath(lib->ubi_dev, DEV_MAX_RSVD);
  507. if (!lib->dev_bad_rsvd)
  508. goto out_error;
  509. lib->dev_max_vols = mkpath(lib->ubi_dev, DEV_MAX_VOLS);
  510. if (!lib->dev_max_vols)
  511. goto out_error;
  512. lib->dev_min_io_size = mkpath(lib->ubi_dev, DEV_MIN_IO_SIZE);
  513. if (!lib->dev_min_io_size)
  514. goto out_error;
  515. lib->dev_mtd_num = mkpath(lib->ubi_dev, DEV_MTD_NUM);
  516. if (!lib->dev_mtd_num)
  517. goto out_error;
  518. lib->ubi_vol = mkpath(lib->sysfs_ubi, UBI_VOL_NAME_PATT);
  519. if (!lib->ubi_vol)
  520. goto out_error;
  521. lib->vol_type = mkpath(lib->ubi_vol, VOL_TYPE);
  522. if (!lib->vol_type)
  523. goto out_error;
  524. lib->vol_dev = mkpath(lib->ubi_vol, VOL_DEV);
  525. if (!lib->vol_dev)
  526. goto out_error;
  527. lib->vol_alignment = mkpath(lib->ubi_vol, VOL_ALIGNMENT);
  528. if (!lib->vol_alignment)
  529. goto out_error;
  530. lib->vol_data_bytes = mkpath(lib->ubi_vol, VOL_DATA_BYTES);
  531. if (!lib->vol_data_bytes)
  532. goto out_error;
  533. lib->vol_rsvd_ebs = mkpath(lib->ubi_vol, VOL_RSVD_EBS);
  534. if (!lib->vol_rsvd_ebs)
  535. goto out_error;
  536. lib->vol_eb_size = mkpath(lib->ubi_vol, VOL_EB_SIZE);
  537. if (!lib->vol_eb_size)
  538. goto out_error;
  539. lib->vol_corrupted = mkpath(lib->ubi_vol, VOL_CORRUPTED);
  540. if (!lib->vol_corrupted)
  541. goto out_error;
  542. lib->vol_name = mkpath(lib->ubi_vol, VOL_NAME);
  543. if (!lib->vol_name)
  544. goto out_error;
  545. if (read_positive_int(lib->ubi_version, &version))
  546. goto out_error;
  547. if (version != LIBUBI_UBI_VERSION) {
  548. errmsg("this library was made for UBI version %d, but UBI "
  549. "version %d is detected\n", LIBUBI_UBI_VERSION, version);
  550. goto out_error;
  551. }
  552. return lib;
  553. out_error:
  554. libubi_close((libubi_t)lib);
  555. return NULL;
  556. }
  557. void libubi_close(libubi_t desc)
  558. {
  559. struct libubi *lib = (struct libubi *)desc;
  560. free(lib->vol_name);
  561. free(lib->vol_corrupted);
  562. free(lib->vol_eb_size);
  563. free(lib->vol_rsvd_ebs);
  564. free(lib->vol_data_bytes);
  565. free(lib->vol_alignment);
  566. free(lib->vol_dev);
  567. free(lib->vol_type);
  568. free(lib->ubi_vol);
  569. free(lib->dev_mtd_num);
  570. free(lib->dev_min_io_size);
  571. free(lib->dev_max_vols);
  572. free(lib->dev_bad_rsvd);
  573. free(lib->dev_max_ec);
  574. free(lib->dev_eb_size);
  575. free(lib->dev_bad_count);
  576. free(lib->dev_total_ebs);
  577. free(lib->dev_avail_ebs);
  578. free(lib->dev_dev);
  579. free(lib->ubi_version);
  580. free(lib->ubi_dev);
  581. free(lib->sysfs_ubi);
  582. free(lib->ctrl_dev);
  583. free(lib->sysfs_ctrl);
  584. free(lib);
  585. }
  586. /**
  587. * do_attach - perform the actual attach operation.
  588. * @node: name of the UBI control character device node
  589. * @r: attach request
  590. *
  591. * This function performs the actual UBI attach operation. Returns %0 in case of
  592. * success and %-1 in case of failure. @r->ubi_num contains newly created UBI
  593. * device number.
  594. */
  595. static int do_attach(const char *node, const struct ubi_attach_req *r)
  596. {
  597. int fd, ret;
  598. fd = open(node, O_RDONLY);
  599. if (fd == -1) {
  600. sys_errmsg("cannot open \"%s\"", node);
  601. return -1;
  602. }
  603. ret = ioctl(fd, UBI_IOCATT, r);
  604. close(fd);
  605. if (ret == -1)
  606. return -1;
  607. #ifdef UDEV_SETTLE_HACK
  608. // if (system("udevsettle") == -1)
  609. // return -1;
  610. usleep(100000);
  611. #endif
  612. return ret;
  613. }
  614. #ifndef MTD_CHAR_MAJOR
  615. /*
  616. * This is taken from kernel <linux/mtd/mtd.h> and is unlikely to change anytime
  617. * soon.
  618. */
  619. #define MTD_CHAR_MAJOR 90
  620. #endif
  621. /**
  622. * mtd_node_to_num - converts device node to MTD number.
  623. * @mtd_dev_node: path to device node to convert
  624. *
  625. * This function converts given @mtd_dev_node to MTD device number.
  626. * @mtd_dev_node should contain path to the MTD device node. Returns MTD device
  627. * number in case of success and %-1 in case of failure (errno is set).
  628. */
  629. static int mtd_node_to_num(const char *mtd_dev_node)
  630. {
  631. int major, minor;
  632. struct stat sb;
  633. if (stat(mtd_dev_node, &sb) < 0) {
  634. sys_errmsg("cannot stat \"%s\"", mtd_dev_node);
  635. return -1;
  636. }
  637. if (!S_ISCHR(sb.st_mode)) {
  638. errno = EINVAL;
  639. sys_errmsg("\"%s\" is not a character device",
  640. mtd_dev_node);
  641. return -1;
  642. }
  643. major = major(sb.st_rdev);
  644. minor = minor(sb.st_rdev);
  645. if (major != MTD_CHAR_MAJOR) {
  646. errno = EINVAL;
  647. sys_errmsg("\"%s\" is not an MTD device", mtd_dev_node);
  648. return -1;
  649. }
  650. return minor / 2;
  651. }
  652. int ubi_attach(libubi_t desc, const char *node, struct ubi_attach_request *req)
  653. {
  654. struct ubi_attach_req r;
  655. int ret;
  656. (void)desc;
  657. if (req->mtd_dev_node) {
  658. /*
  659. * User has passed path to device node. Lets find out MTD
  660. * device number of the device and update req->mtd_num with it
  661. */
  662. req->mtd_num = mtd_node_to_num(req->mtd_dev_node);
  663. if (req->mtd_num == -1)
  664. return -1;
  665. }
  666. memset(&r, 0, sizeof(struct ubi_attach_req));
  667. r.ubi_num = req->dev_num;
  668. r.mtd_num = req->mtd_num;
  669. r.vid_hdr_offset = req->vid_hdr_offset;
  670. if (req->max_beb_per1024) {
  671. /*
  672. * We first have to check if the running kernel supports the
  673. * 'max_beb_per1024' parameter. To do this, we invoke the
  674. * "attach" ioctl 2 times: first with incorrect value %-1 of
  675. * 'max_beb_per1024'.
  676. *
  677. * If the ioctl succeeds, it means that the kernel doesn't
  678. * support the feature and just ignored our 'max_beb_per1024'
  679. * value.
  680. *
  681. * If the ioctl returns -EINVAL, we assume this is because
  682. * 'max_beb_per1024' was set to -1, and we invoke the ioctl for
  683. * the second time with the 'max_beb_per1024' value.
  684. */
  685. r.max_beb_per1024 = -1;
  686. ret = do_attach(node, &r);
  687. if (ret == 0) {
  688. req->dev_num = r.ubi_num;
  689. /*
  690. * The call succeeded. It means that the kernel ignored
  691. * 'max_beb_per1024' parameter.
  692. */
  693. return 1;
  694. } else if (errno != EINVAL)
  695. return ret;
  696. }
  697. r.max_beb_per1024 = req->max_beb_per1024;
  698. ret = do_attach(node, &r);
  699. if (ret == 0)
  700. req->dev_num = r.ubi_num;
  701. return ret;
  702. }
  703. int ubi_detach_mtd(libubi_t desc, const char *node, int mtd_num)
  704. {
  705. int ret, ubi_dev;
  706. ret = mtd_num2ubi_dev(desc, mtd_num, &ubi_dev);
  707. if (ret == -1) {
  708. errno = ENODEV;
  709. return ret;
  710. }
  711. return ubi_remove_dev(desc, node, ubi_dev);
  712. }
  713. int ubi_detach(libubi_t desc, const char *node, const char *mtd_dev_node)
  714. {
  715. int mtd_num;
  716. if (!mtd_dev_node) {
  717. errno = EINVAL;
  718. return -1;
  719. }
  720. mtd_num = mtd_node_to_num(mtd_dev_node);
  721. if (mtd_num == -1)
  722. return -1;
  723. return ubi_detach_mtd(desc, node, mtd_num);
  724. }
  725. int ubi_remove_dev(libubi_t desc, const char *node, int ubi_dev)
  726. {
  727. int fd, ret;
  728. desc = desc;
  729. fd = open(node, O_RDONLY);
  730. if (fd == -1) {
  731. sys_errmsg("cannot open \"%s\"", node);
  732. return -1;
  733. }
  734. ret = ioctl(fd, UBI_IOCFDET, &ubi_dev);
  735. if (ret == -1)
  736. goto out_close;
  737. #ifdef UDEV_SETTLE_HACK
  738. // if (system("udevsettle") == -1)
  739. // return -1;
  740. usleep(100000);
  741. #endif
  742. out_close:
  743. close(fd);
  744. return ret;
  745. }
  746. int ubi_probe_node(libubi_t desc, const char *node)
  747. {
  748. struct stat st;
  749. struct ubi_info info;
  750. int i, fd, major, minor;
  751. struct libubi *lib = (struct libubi *)desc;
  752. char file[strlen(lib->ubi_vol) + 100];
  753. if (stat(node, &st)) {
  754. sys_errmsg("cannot get information about \"%s\"", node);
  755. return -1;
  756. }
  757. if (!S_ISCHR(st.st_mode)) {
  758. errmsg("\"%s\" is not a character device", node);
  759. errno = EINVAL;
  760. return -1;
  761. }
  762. major = major(st.st_rdev);
  763. minor = minor(st.st_rdev);
  764. if (ubi_get_info((libubi_t *)lib, &info))
  765. return -1;
  766. for (i = info.lowest_dev_num; i <= info.highest_dev_num; i++) {
  767. int major1, minor1, ret;
  768. ret = dev_get_major(lib, i, &major1, &minor1);
  769. if (ret) {
  770. if (errno == ENOENT)
  771. continue;
  772. if (!errno)
  773. goto out_not_ubi;
  774. return -1;
  775. }
  776. if (major1 == major)
  777. break;
  778. }
  779. if (i > info.highest_dev_num)
  780. goto out_not_ubi;
  781. if (minor == 0)
  782. return 1;
  783. /* This is supposdely an UBI volume device node */
  784. sprintf(file, lib->ubi_vol, i, minor - 1);
  785. fd = open(file, O_RDONLY);
  786. if (fd < 0)
  787. goto out_not_ubi;
  788. close(fd);
  789. return 2;
  790. out_not_ubi:
  791. errmsg("\"%s\" has major:minor %d:%d, but this does not correspond to "
  792. "any existing UBI device or volume", node, major, minor);
  793. errno = ENODEV;
  794. return -1;
  795. }
  796. int ubi_get_info(libubi_t desc, struct ubi_info *info)
  797. {
  798. DIR *sysfs_ubi;
  799. struct dirent *dirent;
  800. struct libubi *lib = (struct libubi *)desc;
  801. memset(info, 0, sizeof(struct ubi_info));
  802. if (read_major(lib->ctrl_dev, &info->ctrl_major, &info->ctrl_minor)) {
  803. /*
  804. * Older UBI versions did not have control device, so we do not
  805. * panic here for compatibility reasons. May be few years later
  806. * we could return -1 here, but for now just set major:minor to
  807. * -1.
  808. */
  809. info->ctrl_major = info->ctrl_minor = -1;
  810. }
  811. /*
  812. * We have to scan the UBI sysfs directory to identify how many UBI
  813. * devices are present.
  814. */
  815. sysfs_ubi = opendir(lib->sysfs_ubi);
  816. if (!sysfs_ubi)
  817. return -1;
  818. info->lowest_dev_num = INT_MAX;
  819. while (1) {
  820. int dev_num, ret;
  821. char tmp_buf[256];
  822. errno = 0;
  823. dirent = readdir(sysfs_ubi);
  824. if (!dirent)
  825. break;
  826. if (strlen(dirent->d_name) >= 255) {
  827. errmsg("invalid entry in %s: \"%s\"",
  828. lib->sysfs_ubi, dirent->d_name);
  829. errno = EINVAL;
  830. goto out_close;
  831. }
  832. ret = sscanf(dirent->d_name, UBI_DEV_NAME_PATT"%s",
  833. &dev_num, tmp_buf);
  834. if (ret == 1) {
  835. info->dev_count += 1;
  836. if (dev_num > info->highest_dev_num)
  837. info->highest_dev_num = dev_num;
  838. if (dev_num < info->lowest_dev_num)
  839. info->lowest_dev_num = dev_num;
  840. }
  841. }
  842. if (!dirent && errno) {
  843. sys_errmsg("readdir failed on \"%s\"", lib->sysfs_ubi);
  844. goto out_close;
  845. }
  846. if (closedir(sysfs_ubi)) {
  847. sys_errmsg("closedir failed on \"%s\"", lib->sysfs_ubi);
  848. return -1;
  849. }
  850. if (info->lowest_dev_num == INT_MAX)
  851. info->lowest_dev_num = 0;
  852. if (read_positive_int(lib->ubi_version, &info->version))
  853. return -1;
  854. return 0;
  855. out_close:
  856. closedir(sysfs_ubi);
  857. return -1;
  858. }
  859. int ubi_mkvol(libubi_t desc, const char *node, struct ubi_mkvol_request *req)
  860. {
  861. int fd, ret;
  862. struct ubi_mkvol_req r;
  863. size_t n;
  864. memset(&r, 0, sizeof(struct ubi_mkvol_req));
  865. desc = desc;
  866. r.vol_id = req->vol_id;
  867. r.alignment = req->alignment;
  868. r.bytes = req->bytes;
  869. r.vol_type = req->vol_type;
  870. n = strlen(req->name);
  871. if (n > UBI_MAX_VOLUME_NAME)
  872. return -1;
  873. strncpy(r.name, req->name, UBI_MAX_VOLUME_NAME + 1);
  874. r.name_len = n;
  875. desc = desc;
  876. fd = open(node, O_RDONLY);
  877. if (fd == -1) {
  878. sys_errmsg("cannot open \"%s\"", node);
  879. return -1;
  880. }
  881. ret = ioctl(fd, UBI_IOCMKVOL, &r);
  882. if (ret == -1) {
  883. close(fd);
  884. return ret;
  885. }
  886. close(fd);
  887. req->vol_id = r.vol_id;
  888. #ifdef UDEV_SETTLE_HACK
  889. // if (system("udevsettle") == -1)
  890. // return -1;
  891. usleep(100000);
  892. #endif
  893. return 0;
  894. }
  895. int ubi_rmvol(libubi_t desc, const char *node, int vol_id)
  896. {
  897. int fd, ret;
  898. desc = desc;
  899. fd = open(node, O_RDONLY);
  900. if (fd == -1) {
  901. sys_errmsg("cannot open \"%s\"", node);
  902. return -1;
  903. }
  904. ret = ioctl(fd, UBI_IOCRMVOL, &vol_id);
  905. if (ret == -1) {
  906. close(fd);
  907. return ret;
  908. }
  909. close(fd);
  910. #ifdef UDEV_SETTLE_HACK
  911. // if (system("udevsettle") == -1)
  912. // return -1;
  913. usleep(100000);
  914. #endif
  915. return 0;
  916. }
  917. int ubi_rnvols(libubi_t desc, const char *node, struct ubi_rnvol_req *rnvol)
  918. {
  919. int fd, ret;
  920. desc = desc;
  921. fd = open(node, O_RDONLY);
  922. if (fd == -1)
  923. return -1;
  924. ret = ioctl(fd, UBI_IOCRNVOL, rnvol);
  925. if (ret == -1) {
  926. close(fd);
  927. return ret;
  928. }
  929. close(fd);
  930. #ifdef UDEV_SETTLE_HACK
  931. // if (system("udevsettle") == -1)
  932. // return -1;
  933. usleep(100000);
  934. #endif
  935. return 0;
  936. }
  937. int ubi_rsvol(libubi_t desc, const char *node, int vol_id, long long bytes)
  938. {
  939. int fd, ret;
  940. struct ubi_rsvol_req req;
  941. desc = desc;
  942. fd = open(node, O_RDONLY);
  943. if (fd == -1) {
  944. sys_errmsg("cannot open \"%s\"", node);
  945. return -1;
  946. }
  947. req.bytes = bytes;
  948. req.vol_id = vol_id;
  949. ret = ioctl(fd, UBI_IOCRSVOL, &req);
  950. close(fd);
  951. return ret;
  952. }
  953. int ubi_update_start(libubi_t desc, int fd, long long bytes)
  954. {
  955. desc = desc;
  956. if (ioctl(fd, UBI_IOCVOLUP, &bytes))
  957. return -1;
  958. return 0;
  959. }
  960. int ubi_leb_change_start(libubi_t desc, int fd, int lnum, int bytes)
  961. {
  962. struct ubi_leb_change_req req;
  963. desc = desc;
  964. memset(&req, 0, sizeof(struct ubi_leb_change_req));
  965. req.lnum = lnum;
  966. req.bytes = bytes;
  967. req.dtype = 3;
  968. if (ioctl(fd, UBI_IOCEBCH, &req))
  969. return -1;
  970. return 0;
  971. }
  972. int ubi_dev_present(libubi_t desc, int dev_num)
  973. {
  974. struct stat st;
  975. struct libubi *lib = (struct libubi *)desc;
  976. char file[strlen(lib->ubi_dev) + 50];
  977. sprintf(file, lib->ubi_dev, dev_num);
  978. return !stat(file, &st);
  979. }
  980. int ubi_get_dev_info1(libubi_t desc, int dev_num, struct ubi_dev_info *info)
  981. {
  982. DIR *sysfs_ubi;
  983. struct dirent *dirent;
  984. struct libubi *lib = (struct libubi *)desc;
  985. memset(info, 0, sizeof(struct ubi_dev_info));
  986. info->dev_num = dev_num;
  987. if (!ubi_dev_present(desc, dev_num))
  988. return -1;
  989. sysfs_ubi = opendir(lib->sysfs_ubi);
  990. if (!sysfs_ubi)
  991. return -1;
  992. info->lowest_vol_id = INT_MAX;
  993. while (1) {
  994. int vol_id, ret, devno;
  995. char tmp_buf[256];
  996. errno = 0;
  997. dirent = readdir(sysfs_ubi);
  998. if (!dirent)
  999. break;
  1000. if (strlen(dirent->d_name) >= 255) {
  1001. errmsg("invalid entry in %s: \"%s\"",
  1002. lib->sysfs_ubi, dirent->d_name);
  1003. goto out_close;
  1004. }
  1005. ret = sscanf(dirent->d_name, UBI_VOL_NAME_PATT"%s", &devno, &vol_id, tmp_buf);
  1006. if (ret == 2 && devno == dev_num) {
  1007. info->vol_count += 1;
  1008. if (vol_id > info->highest_vol_id)
  1009. info->highest_vol_id = vol_id;
  1010. if (vol_id < info->lowest_vol_id)
  1011. info->lowest_vol_id = vol_id;
  1012. }
  1013. }
  1014. if (!dirent && errno) {
  1015. sys_errmsg("readdir failed on \"%s\"", lib->sysfs_ubi);
  1016. goto out_close;
  1017. }
  1018. if (closedir(sysfs_ubi)) {
  1019. sys_errmsg("closedir failed on \"%s\"", lib->sysfs_ubi);
  1020. return -1;
  1021. }
  1022. if (info->lowest_vol_id == INT_MAX)
  1023. info->lowest_vol_id = 0;
  1024. if (dev_get_major(lib, dev_num, &info->major, &info->minor))
  1025. return -1;
  1026. if (dev_read_int(lib->dev_mtd_num, dev_num, &info->mtd_num))
  1027. return -1;
  1028. if (dev_read_int(lib->dev_avail_ebs, dev_num, &info->avail_lebs))
  1029. return -1;
  1030. if (dev_read_int(lib->dev_total_ebs, dev_num, &info->total_lebs))
  1031. return -1;
  1032. if (dev_read_int(lib->dev_bad_count, dev_num, &info->bad_count))
  1033. return -1;
  1034. if (dev_read_int(lib->dev_eb_size, dev_num, &info->leb_size))
  1035. return -1;
  1036. if (dev_read_int(lib->dev_bad_rsvd, dev_num, &info->bad_rsvd))
  1037. return -1;
  1038. if (dev_read_ll(lib->dev_max_ec, dev_num, &info->max_ec))
  1039. return -1;
  1040. if (dev_read_int(lib->dev_max_vols, dev_num, &info->max_vol_count))
  1041. return -1;
  1042. if (dev_read_int(lib->dev_min_io_size, dev_num, &info->min_io_size))
  1043. return -1;
  1044. info->avail_bytes = (long long)info->avail_lebs * info->leb_size;
  1045. info->total_bytes = (long long)info->total_lebs * info->leb_size;
  1046. return 0;
  1047. out_close:
  1048. closedir(sysfs_ubi);
  1049. return -1;
  1050. }
  1051. int ubi_get_dev_info(libubi_t desc, const char *node, struct ubi_dev_info *info)
  1052. {
  1053. int err, dev_num = 0;
  1054. struct libubi *lib = (struct libubi *)desc;
  1055. err = ubi_probe_node(desc, node);
  1056. if (err != 1) {
  1057. if (err == 2)
  1058. errno = ENODEV;
  1059. return -1;
  1060. }
  1061. if (dev_node2num(lib, node, &dev_num))
  1062. return -1;
  1063. return ubi_get_dev_info1(desc, dev_num, info);
  1064. }
  1065. int ubi_get_vol_info1(libubi_t desc, int dev_num, int vol_id,
  1066. struct ubi_vol_info *info)
  1067. {
  1068. int ret;
  1069. struct libubi *lib = (struct libubi *)desc;
  1070. char buf[50];
  1071. memset(info, 0, sizeof(struct ubi_vol_info));
  1072. info->dev_num = dev_num;
  1073. info->vol_id = vol_id;
  1074. if (vol_get_major(lib, dev_num, vol_id, &info->major, &info->minor))
  1075. return -1;
  1076. ret = vol_read_data(lib->vol_type, dev_num, vol_id, buf, 50);
  1077. if (ret < 0)
  1078. return -1;
  1079. if (strncmp(buf, "static\n", ret) == 0)
  1080. info->type = UBI_STATIC_VOLUME;
  1081. else if (strncmp(buf, "dynamic\n", ret) == 0)
  1082. info->type = UBI_DYNAMIC_VOLUME;
  1083. else {
  1084. errmsg("bad value at \"%s\"", buf);
  1085. errno = EINVAL;
  1086. return -1;
  1087. }
  1088. ret = vol_read_int(lib->vol_alignment, dev_num, vol_id,
  1089. &info->alignment);
  1090. if (ret)
  1091. return -1;
  1092. ret = vol_read_ll(lib->vol_data_bytes, dev_num, vol_id,
  1093. &info->data_bytes);
  1094. if (ret)
  1095. return -1;
  1096. ret = vol_read_int(lib->vol_rsvd_ebs, dev_num, vol_id, &info->rsvd_lebs);
  1097. if (ret)
  1098. return -1;
  1099. ret = vol_read_int(lib->vol_eb_size, dev_num, vol_id, &info->leb_size);
  1100. if (ret)
  1101. return -1;
  1102. ret = vol_read_int(lib->vol_corrupted, dev_num, vol_id,
  1103. &info->corrupted);
  1104. if (ret)
  1105. return -1;
  1106. info->rsvd_bytes = (long long)info->leb_size * info->rsvd_lebs;
  1107. ret = vol_read_data(lib->vol_name, dev_num, vol_id, &info->name,
  1108. UBI_VOL_NAME_MAX + 1);
  1109. if (ret < 0)
  1110. return -1;
  1111. info->name[ret - 1] = '\0';
  1112. return 0;
  1113. }
  1114. int ubi_get_vol_info(libubi_t desc, const char *node, struct ubi_vol_info *info)
  1115. {
  1116. int err, vol_id = 0, dev_num = 0;
  1117. struct libubi *lib = (struct libubi *)desc;
  1118. err = ubi_probe_node(desc, node);
  1119. if (err != 2) {
  1120. if (err == 1)
  1121. errno = ENODEV;
  1122. return -1;
  1123. }
  1124. if (vol_node2nums(lib, node, &dev_num, &vol_id))
  1125. return -1;
  1126. return ubi_get_vol_info1(desc, dev_num, vol_id, info);
  1127. }
  1128. int ubi_get_vol_info1_nm(libubi_t desc, int dev_num, const char *name,
  1129. struct ubi_vol_info *info)
  1130. {
  1131. int i, err;
  1132. unsigned int nlen = strlen(name);
  1133. struct ubi_dev_info dev_info;
  1134. if (nlen == 0) {
  1135. errmsg("bad \"name\" input parameter");
  1136. errno = EINVAL;
  1137. return -1;
  1138. }
  1139. err = ubi_get_dev_info1(desc, dev_num, &dev_info);
  1140. if (err)
  1141. return err;
  1142. for (i = dev_info.lowest_vol_id;
  1143. i <= dev_info.highest_vol_id; i++) {
  1144. err = ubi_get_vol_info1(desc, dev_num, i, info);
  1145. if (err == -1) {
  1146. if (errno == ENOENT)
  1147. continue;
  1148. return -1;
  1149. }
  1150. if (nlen == strlen(info->name) && !strcmp(name, info->name))
  1151. return 0;
  1152. }
  1153. errno = ENOENT;
  1154. return -1;
  1155. }
  1156. int ubi_set_property(int fd, uint8_t property, uint64_t value)
  1157. {
  1158. struct ubi_set_vol_prop_req r;
  1159. memset(&r, 0, sizeof(struct ubi_set_vol_prop_req));
  1160. r.property = property;
  1161. r.value = value;
  1162. return ioctl(fd, UBI_IOCSETVOLPROP, &r);
  1163. }
  1164. int ubi_leb_unmap(int fd, int lnum)
  1165. {
  1166. return ioctl(fd, UBI_IOCEBUNMAP, &lnum);
  1167. }
  1168. int ubi_is_mapped(int fd, int lnum)
  1169. {
  1170. return ioctl(fd, UBI_IOCEBISMAP, &lnum);
  1171. }