libubi.c 30 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402
  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, 1);
  158. if (tmp1 == 1) {
  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 == -1) {
  370. errno = ENODEV;
  371. return -1;
  372. }
  373. *dev_num = i;
  374. *vol_id = minor - 1;
  375. errno = 0;
  376. return 0;
  377. }
  378. /**
  379. * dev_node2num - find UBI device number by its character device node.
  380. * @lib: UBI library descriptor
  381. * @node: UBI character device node name
  382. * @dev_num: UBI device number is returned here
  383. *
  384. * This function returns %0 in case of success and %-1 in case of failure.
  385. */
  386. static int dev_node2num(struct libubi *lib, const char *node, int *dev_num)
  387. {
  388. struct stat st;
  389. struct ubi_info info;
  390. int i, major, minor;
  391. if (stat(node, &st)) {
  392. sys_errmsg("cannot get information about \"%s\"", node);
  393. return -1;
  394. }
  395. if (!S_ISCHR(st.st_mode)) {
  396. errno = EINVAL;
  397. errmsg("\"%s\" is not a character device", node);
  398. return -1;
  399. }
  400. major = major(st.st_rdev);
  401. minor = minor(st.st_rdev);
  402. if (minor != 0) {
  403. errno = EINVAL;
  404. errmsg("\"%s\" is not an UBI character device", node);
  405. return -1;
  406. }
  407. if (ubi_get_info((libubi_t *)lib, &info))
  408. return -1;
  409. for (i = info.lowest_dev_num; i <= info.highest_dev_num; i++) {
  410. int major1, minor1, ret;
  411. ret = dev_get_major(lib, i, &major1, &minor1);
  412. if (ret) {
  413. if (errno == ENOENT)
  414. continue;
  415. return -1;
  416. }
  417. if (major1 == major) {
  418. if (minor1 != 0) {
  419. errmsg("UBI character device minor number is "
  420. "%d, but must be 0", minor1);
  421. errno = EINVAL;
  422. return -1;
  423. }
  424. errno = 0;
  425. *dev_num = i;
  426. return 0;
  427. }
  428. }
  429. errno = ENODEV;
  430. return -1;
  431. }
  432. int mtd_num2ubi_dev(libubi_t desc, int mtd_num, int *dev_num)
  433. {
  434. struct ubi_info info;
  435. int i, ret, mtd_num1;
  436. struct libubi *lib = desc;
  437. if (ubi_get_info(desc, &info))
  438. return -1;
  439. for (i = info.lowest_dev_num; i <= info.highest_dev_num; i++) {
  440. ret = dev_read_int(lib->dev_mtd_num, i, &mtd_num1);
  441. if (ret) {
  442. if (errno == ENOENT)
  443. continue;
  444. return -1;
  445. }
  446. if (mtd_num1 == mtd_num) {
  447. errno = 0;
  448. *dev_num = i;
  449. return 0;
  450. }
  451. }
  452. errno = 0;
  453. return -1;
  454. }
  455. libubi_t libubi_open(void)
  456. {
  457. int fd, version;
  458. struct libubi *lib;
  459. lib = calloc(1, sizeof(struct libubi));
  460. if (!lib)
  461. return NULL;
  462. lib->sysfs_ctrl = mkpath("/sys", SYSFS_CTRL);
  463. if (!lib->sysfs_ctrl)
  464. goto out_error;
  465. lib->ctrl_dev = mkpath(lib->sysfs_ctrl, CTRL_DEV);
  466. if (!lib->ctrl_dev)
  467. goto out_error;
  468. lib->sysfs_ubi = mkpath("/sys", SYSFS_UBI);
  469. if (!lib->sysfs_ubi)
  470. goto out_error;
  471. /* Make sure UBI is present */
  472. fd = open(lib->sysfs_ubi, O_RDONLY);
  473. if (fd == -1) {
  474. errno = 0;
  475. goto out_error;
  476. }
  477. if (close(fd)) {
  478. sys_errmsg("close failed on \"%s\"", lib->sysfs_ubi);
  479. goto out_error;
  480. }
  481. lib->ubi_dev = mkpath(lib->sysfs_ubi, UBI_DEV_NAME_PATT);
  482. if (!lib->ubi_dev)
  483. goto out_error;
  484. lib->ubi_version = mkpath(lib->sysfs_ubi, UBI_VER);
  485. if (!lib->ubi_version)
  486. goto out_error;
  487. lib->dev_dev = mkpath(lib->ubi_dev, DEV_DEV);
  488. if (!lib->dev_dev)
  489. goto out_error;
  490. lib->dev_avail_ebs = mkpath(lib->ubi_dev, DEV_AVAIL_EBS);
  491. if (!lib->dev_avail_ebs)
  492. goto out_error;
  493. lib->dev_total_ebs = mkpath(lib->ubi_dev, DEV_TOTAL_EBS);
  494. if (!lib->dev_total_ebs)
  495. goto out_error;
  496. lib->dev_bad_count = mkpath(lib->ubi_dev, DEV_BAD_COUNT);
  497. if (!lib->dev_bad_count)
  498. goto out_error;
  499. lib->dev_eb_size = mkpath(lib->ubi_dev, DEV_EB_SIZE);
  500. if (!lib->dev_eb_size)
  501. goto out_error;
  502. lib->dev_max_ec = mkpath(lib->ubi_dev, DEV_MAX_EC);
  503. if (!lib->dev_max_ec)
  504. goto out_error;
  505. lib->dev_bad_rsvd = mkpath(lib->ubi_dev, DEV_MAX_RSVD);
  506. if (!lib->dev_bad_rsvd)
  507. goto out_error;
  508. lib->dev_max_vols = mkpath(lib->ubi_dev, DEV_MAX_VOLS);
  509. if (!lib->dev_max_vols)
  510. goto out_error;
  511. lib->dev_min_io_size = mkpath(lib->ubi_dev, DEV_MIN_IO_SIZE);
  512. if (!lib->dev_min_io_size)
  513. goto out_error;
  514. lib->dev_mtd_num = mkpath(lib->ubi_dev, DEV_MTD_NUM);
  515. if (!lib->dev_mtd_num)
  516. goto out_error;
  517. lib->ubi_vol = mkpath(lib->sysfs_ubi, UBI_VOL_NAME_PATT);
  518. if (!lib->ubi_vol)
  519. goto out_error;
  520. lib->vol_type = mkpath(lib->ubi_vol, VOL_TYPE);
  521. if (!lib->vol_type)
  522. goto out_error;
  523. lib->vol_dev = mkpath(lib->ubi_vol, VOL_DEV);
  524. if (!lib->vol_dev)
  525. goto out_error;
  526. lib->vol_alignment = mkpath(lib->ubi_vol, VOL_ALIGNMENT);
  527. if (!lib->vol_alignment)
  528. goto out_error;
  529. lib->vol_data_bytes = mkpath(lib->ubi_vol, VOL_DATA_BYTES);
  530. if (!lib->vol_data_bytes)
  531. goto out_error;
  532. lib->vol_rsvd_ebs = mkpath(lib->ubi_vol, VOL_RSVD_EBS);
  533. if (!lib->vol_rsvd_ebs)
  534. goto out_error;
  535. lib->vol_eb_size = mkpath(lib->ubi_vol, VOL_EB_SIZE);
  536. if (!lib->vol_eb_size)
  537. goto out_error;
  538. lib->vol_corrupted = mkpath(lib->ubi_vol, VOL_CORRUPTED);
  539. if (!lib->vol_corrupted)
  540. goto out_error;
  541. lib->vol_name = mkpath(lib->ubi_vol, VOL_NAME);
  542. if (!lib->vol_name)
  543. goto out_error;
  544. if (read_positive_int(lib->ubi_version, &version))
  545. goto out_error;
  546. if (version != LIBUBI_UBI_VERSION) {
  547. errmsg("this library was made for UBI version %d, but UBI "
  548. "version %d is detected\n", LIBUBI_UBI_VERSION, version);
  549. goto out_error;
  550. }
  551. return lib;
  552. out_error:
  553. libubi_close((libubi_t)lib);
  554. return NULL;
  555. }
  556. void libubi_close(libubi_t desc)
  557. {
  558. struct libubi *lib = (struct libubi *)desc;
  559. free(lib->vol_name);
  560. free(lib->vol_corrupted);
  561. free(lib->vol_eb_size);
  562. free(lib->vol_rsvd_ebs);
  563. free(lib->vol_data_bytes);
  564. free(lib->vol_alignment);
  565. free(lib->vol_dev);
  566. free(lib->vol_type);
  567. free(lib->ubi_vol);
  568. free(lib->dev_mtd_num);
  569. free(lib->dev_min_io_size);
  570. free(lib->dev_max_vols);
  571. free(lib->dev_bad_rsvd);
  572. free(lib->dev_max_ec);
  573. free(lib->dev_eb_size);
  574. free(lib->dev_bad_count);
  575. free(lib->dev_total_ebs);
  576. free(lib->dev_avail_ebs);
  577. free(lib->dev_dev);
  578. free(lib->ubi_version);
  579. free(lib->ubi_dev);
  580. free(lib->sysfs_ubi);
  581. free(lib->ctrl_dev);
  582. free(lib->sysfs_ctrl);
  583. free(lib);
  584. }
  585. /**
  586. * do_attach - perform the actual attach operation.
  587. * @node: name of the UBI control character device node
  588. * @r: attach request
  589. *
  590. * This function performs the actual UBI attach operation. Returns %0 in case of
  591. * success and %-1 in case of failure. @r->ubi_num contains newly created UBI
  592. * device number.
  593. */
  594. static int do_attach(const char *node, const struct ubi_attach_req *r)
  595. {
  596. int fd, ret;
  597. fd = open(node, O_RDONLY);
  598. if (fd == -1) {
  599. sys_errmsg("cannot open \"%s\"", node);
  600. return -1;
  601. }
  602. ret = ioctl(fd, UBI_IOCATT, r);
  603. close(fd);
  604. if (ret == -1)
  605. return -1;
  606. #ifdef UDEV_SETTLE_HACK
  607. // if (system("udevsettle") == -1)
  608. // return -1;
  609. usleep(100000);
  610. #endif
  611. return ret;
  612. }
  613. #ifndef MTD_CHAR_MAJOR
  614. /*
  615. * This is taken from kernel <linux/mtd/mtd.h> and is unlikely to change anytime
  616. * soon.
  617. */
  618. #define MTD_CHAR_MAJOR 90
  619. #endif
  620. /**
  621. * mtd_node_to_num - converts device node to MTD number.
  622. * @mtd_dev_node: path to device node to convert
  623. *
  624. * This function converts given @mtd_dev_node to MTD device number.
  625. * @mtd_dev_node should contain path to the MTD device node. Returns MTD device
  626. * number in case of success and %-1 in case of failure (errno is set).
  627. */
  628. static int mtd_node_to_num(const char *mtd_dev_node)
  629. {
  630. int major, minor;
  631. struct stat sb;
  632. if (stat(mtd_dev_node, &sb) < 0) {
  633. sys_errmsg("cannot stat \"%s\"", mtd_dev_node);
  634. return -1;
  635. }
  636. if (!S_ISCHR(sb.st_mode)) {
  637. errno = EINVAL;
  638. sys_errmsg("\"%s\" is not a character device",
  639. mtd_dev_node);
  640. return -1;
  641. }
  642. major = major(sb.st_rdev);
  643. minor = minor(sb.st_rdev);
  644. if (major != MTD_CHAR_MAJOR) {
  645. errno = EINVAL;
  646. sys_errmsg("\"%s\" is not an MTD device", mtd_dev_node);
  647. return -1;
  648. }
  649. return minor / 2;
  650. }
  651. int ubi_attach(libubi_t desc, const char *node, struct ubi_attach_request *req)
  652. {
  653. struct ubi_attach_req r;
  654. int ret;
  655. (void)desc;
  656. if (req->mtd_dev_node) {
  657. /*
  658. * User has passed path to device node. Lets find out MTD
  659. * device number of the device and update req->mtd_num with it
  660. */
  661. req->mtd_num = mtd_node_to_num(req->mtd_dev_node);
  662. if (req->mtd_num == -1)
  663. return -1;
  664. }
  665. memset(&r, 0, sizeof(struct ubi_attach_req));
  666. r.ubi_num = req->dev_num;
  667. r.mtd_num = req->mtd_num;
  668. r.vid_hdr_offset = req->vid_hdr_offset;
  669. if (req->max_beb_per1024) {
  670. /*
  671. * We first have to check if the running kernel supports the
  672. * 'max_beb_per1024' parameter. To do this, we invoke the
  673. * "attach" ioctl 2 times: first with incorrect value %-1 of
  674. * 'max_beb_per1024'.
  675. *
  676. * If the ioctl succeeds, it means that the kernel doesn't
  677. * support the feature and just ignored our 'max_beb_per1024'
  678. * value.
  679. *
  680. * If the ioctl returns -EINVAL, we assume this is because
  681. * 'max_beb_per1024' was set to -1, and we invoke the ioctl for
  682. * the second time with the 'max_beb_per1024' value.
  683. */
  684. r.max_beb_per1024 = -1;
  685. ret = do_attach(node, &r);
  686. if (ret == 0) {
  687. req->dev_num = r.ubi_num;
  688. /*
  689. * The call succeeded. It means that the kernel ignored
  690. * 'max_beb_per1024' parameter.
  691. */
  692. return 1;
  693. } else if (errno != EINVAL)
  694. return ret;
  695. }
  696. r.max_beb_per1024 = req->max_beb_per1024;
  697. ret = do_attach(node, &r);
  698. if (ret == 0)
  699. req->dev_num = r.ubi_num;
  700. return ret;
  701. }
  702. int ubi_detach_mtd(libubi_t desc, const char *node, int mtd_num)
  703. {
  704. int ret, ubi_dev;
  705. ret = mtd_num2ubi_dev(desc, mtd_num, &ubi_dev);
  706. if (ret == -1) {
  707. errno = ENODEV;
  708. return ret;
  709. }
  710. return ubi_remove_dev(desc, node, ubi_dev);
  711. }
  712. int ubi_detach(libubi_t desc, const char *node, const char *mtd_dev_node)
  713. {
  714. int mtd_num;
  715. if (!mtd_dev_node) {
  716. errno = EINVAL;
  717. return -1;
  718. }
  719. mtd_num = mtd_node_to_num(mtd_dev_node);
  720. if (mtd_num == -1)
  721. return -1;
  722. return ubi_detach_mtd(desc, node, mtd_num);
  723. }
  724. int ubi_remove_dev(libubi_t desc, const char *node, int ubi_dev)
  725. {
  726. int fd, ret;
  727. desc = desc;
  728. fd = open(node, O_RDONLY);
  729. if (fd == -1) {
  730. sys_errmsg("cannot open \"%s\"", node);
  731. return -1;
  732. }
  733. ret = ioctl(fd, UBI_IOCFDET, &ubi_dev);
  734. if (ret == -1)
  735. goto out_close;
  736. #ifdef UDEV_SETTLE_HACK
  737. // if (system("udevsettle") == -1)
  738. // return -1;
  739. usleep(100000);
  740. #endif
  741. out_close:
  742. close(fd);
  743. return ret;
  744. }
  745. int ubi_probe_node(libubi_t desc, const char *node)
  746. {
  747. struct stat st;
  748. struct ubi_info info;
  749. int i, fd, major, minor;
  750. struct libubi *lib = (struct libubi *)desc;
  751. char file[strlen(lib->ubi_vol) + 100];
  752. if (stat(node, &st)) {
  753. sys_errmsg("cannot get information about \"%s\"", node);
  754. return -1;
  755. }
  756. if (!S_ISCHR(st.st_mode)) {
  757. errmsg("\"%s\" is not a character device", node);
  758. errno = EINVAL;
  759. return -1;
  760. }
  761. major = major(st.st_rdev);
  762. minor = minor(st.st_rdev);
  763. if (ubi_get_info((libubi_t *)lib, &info))
  764. return -1;
  765. for (i = info.lowest_dev_num; i <= info.highest_dev_num; i++) {
  766. int major1, minor1, ret;
  767. ret = dev_get_major(lib, i, &major1, &minor1);
  768. if (ret) {
  769. if (errno == ENOENT)
  770. continue;
  771. if (!errno)
  772. goto out_not_ubi;
  773. return -1;
  774. }
  775. if (major1 == major)
  776. break;
  777. }
  778. if (i > info.highest_dev_num)
  779. goto out_not_ubi;
  780. if (minor == 0)
  781. return 1;
  782. /* This is supposdely an UBI volume device node */
  783. sprintf(file, lib->ubi_vol, i, minor - 1);
  784. fd = open(file, O_RDONLY);
  785. if (fd == -1)
  786. goto out_not_ubi;
  787. return 2;
  788. out_not_ubi:
  789. errmsg("\"%s\" has major:minor %d:%d, but this does not correspond to "
  790. "any existing UBI device or volume", node, major, minor);
  791. errno = ENODEV;
  792. return -1;
  793. }
  794. int ubi_get_info(libubi_t desc, struct ubi_info *info)
  795. {
  796. DIR *sysfs_ubi;
  797. struct dirent *dirent;
  798. struct libubi *lib = (struct libubi *)desc;
  799. memset(info, 0, sizeof(struct ubi_info));
  800. if (read_major(lib->ctrl_dev, &info->ctrl_major, &info->ctrl_minor)) {
  801. /*
  802. * Older UBI versions did not have control device, so we do not
  803. * panic here for compatibility reasons. May be few years later
  804. * we could return -1 here, but for now just set major:minor to
  805. * -1.
  806. */
  807. info->ctrl_major = info->ctrl_minor = -1;
  808. }
  809. /*
  810. * We have to scan the UBI sysfs directory to identify how many UBI
  811. * devices are present.
  812. */
  813. sysfs_ubi = opendir(lib->sysfs_ubi);
  814. if (!sysfs_ubi)
  815. return -1;
  816. info->lowest_dev_num = INT_MAX;
  817. while (1) {
  818. int dev_num, ret;
  819. char tmp_buf[256];
  820. errno = 0;
  821. dirent = readdir(sysfs_ubi);
  822. if (!dirent)
  823. break;
  824. if (strlen(dirent->d_name) >= 255) {
  825. errmsg("invalid entry in %s: \"%s\"",
  826. lib->sysfs_ubi, dirent->d_name);
  827. errno = EINVAL;
  828. goto out_close;
  829. }
  830. ret = sscanf(dirent->d_name, UBI_DEV_NAME_PATT"%s",
  831. &dev_num, tmp_buf);
  832. if (ret == 1) {
  833. info->dev_count += 1;
  834. if (dev_num > info->highest_dev_num)
  835. info->highest_dev_num = dev_num;
  836. if (dev_num < info->lowest_dev_num)
  837. info->lowest_dev_num = dev_num;
  838. }
  839. }
  840. if (!dirent && errno) {
  841. sys_errmsg("readdir failed on \"%s\"", lib->sysfs_ubi);
  842. goto out_close;
  843. }
  844. if (closedir(sysfs_ubi)) {
  845. sys_errmsg("closedir failed on \"%s\"", lib->sysfs_ubi);
  846. return -1;
  847. }
  848. if (info->lowest_dev_num == INT_MAX)
  849. info->lowest_dev_num = 0;
  850. if (read_positive_int(lib->ubi_version, &info->version))
  851. return -1;
  852. return 0;
  853. out_close:
  854. closedir(sysfs_ubi);
  855. return -1;
  856. }
  857. int ubi_mkvol(libubi_t desc, const char *node, struct ubi_mkvol_request *req)
  858. {
  859. int fd, ret;
  860. struct ubi_mkvol_req r;
  861. size_t n;
  862. memset(&r, 0, sizeof(struct ubi_mkvol_req));
  863. desc = desc;
  864. r.vol_id = req->vol_id;
  865. r.alignment = req->alignment;
  866. r.bytes = req->bytes;
  867. r.vol_type = req->vol_type;
  868. n = strlen(req->name);
  869. if (n > UBI_MAX_VOLUME_NAME)
  870. return -1;
  871. strncpy(r.name, req->name, UBI_MAX_VOLUME_NAME + 1);
  872. r.name_len = n;
  873. desc = desc;
  874. fd = open(node, O_RDONLY);
  875. if (fd == -1) {
  876. sys_errmsg("cannot open \"%s\"", node);
  877. return -1;
  878. }
  879. ret = ioctl(fd, UBI_IOCMKVOL, &r);
  880. if (ret == -1) {
  881. close(fd);
  882. return ret;
  883. }
  884. close(fd);
  885. req->vol_id = r.vol_id;
  886. #ifdef UDEV_SETTLE_HACK
  887. // if (system("udevsettle") == -1)
  888. // return -1;
  889. usleep(100000);
  890. #endif
  891. return 0;
  892. }
  893. int ubi_rmvol(libubi_t desc, const char *node, int vol_id)
  894. {
  895. int fd, ret;
  896. desc = desc;
  897. fd = open(node, O_RDONLY);
  898. if (fd == -1) {
  899. sys_errmsg("cannot open \"%s\"", node);
  900. return -1;
  901. }
  902. ret = ioctl(fd, UBI_IOCRMVOL, &vol_id);
  903. if (ret == -1) {
  904. close(fd);
  905. return ret;
  906. }
  907. close(fd);
  908. #ifdef UDEV_SETTLE_HACK
  909. // if (system("udevsettle") == -1)
  910. // return -1;
  911. usleep(100000);
  912. #endif
  913. return 0;
  914. }
  915. int ubi_rnvols(libubi_t desc, const char *node, struct ubi_rnvol_req *rnvol)
  916. {
  917. int fd, ret;
  918. desc = desc;
  919. fd = open(node, O_RDONLY);
  920. if (fd == -1)
  921. return -1;
  922. ret = ioctl(fd, UBI_IOCRNVOL, rnvol);
  923. if (ret == -1) {
  924. close(fd);
  925. return ret;
  926. }
  927. close(fd);
  928. #ifdef UDEV_SETTLE_HACK
  929. // if (system("udevsettle") == -1)
  930. // return -1;
  931. usleep(100000);
  932. #endif
  933. return 0;
  934. }
  935. int ubi_rsvol(libubi_t desc, const char *node, int vol_id, long long bytes)
  936. {
  937. int fd, ret;
  938. struct ubi_rsvol_req req;
  939. desc = desc;
  940. fd = open(node, O_RDONLY);
  941. if (fd == -1) {
  942. sys_errmsg("cannot open \"%s\"", node);
  943. return -1;
  944. }
  945. req.bytes = bytes;
  946. req.vol_id = vol_id;
  947. ret = ioctl(fd, UBI_IOCRSVOL, &req);
  948. close(fd);
  949. return ret;
  950. }
  951. int ubi_update_start(libubi_t desc, int fd, long long bytes)
  952. {
  953. desc = desc;
  954. if (ioctl(fd, UBI_IOCVOLUP, &bytes))
  955. return -1;
  956. return 0;
  957. }
  958. int ubi_leb_change_start(libubi_t desc, int fd, int lnum, int bytes)
  959. {
  960. struct ubi_leb_change_req req;
  961. desc = desc;
  962. memset(&req, 0, sizeof(struct ubi_leb_change_req));
  963. req.lnum = lnum;
  964. req.bytes = bytes;
  965. req.dtype = 3;
  966. if (ioctl(fd, UBI_IOCEBCH, &req))
  967. return -1;
  968. return 0;
  969. }
  970. int ubi_dev_present(libubi_t desc, int dev_num)
  971. {
  972. struct stat st;
  973. struct libubi *lib = (struct libubi *)desc;
  974. char file[strlen(lib->ubi_dev) + 50];
  975. sprintf(file, lib->ubi_dev, dev_num);
  976. return !stat(file, &st);
  977. }
  978. int ubi_get_dev_info1(libubi_t desc, int dev_num, struct ubi_dev_info *info)
  979. {
  980. DIR *sysfs_ubi;
  981. struct dirent *dirent;
  982. struct libubi *lib = (struct libubi *)desc;
  983. memset(info, 0, sizeof(struct ubi_dev_info));
  984. info->dev_num = dev_num;
  985. if (!ubi_dev_present(desc, dev_num))
  986. return -1;
  987. sysfs_ubi = opendir(lib->sysfs_ubi);
  988. if (!sysfs_ubi)
  989. return -1;
  990. info->lowest_vol_id = INT_MAX;
  991. while (1) {
  992. int vol_id, ret, devno;
  993. char tmp_buf[256];
  994. errno = 0;
  995. dirent = readdir(sysfs_ubi);
  996. if (!dirent)
  997. break;
  998. if (strlen(dirent->d_name) >= 255) {
  999. errmsg("invalid entry in %s: \"%s\"",
  1000. lib->sysfs_ubi, dirent->d_name);
  1001. goto out_close;
  1002. }
  1003. ret = sscanf(dirent->d_name, UBI_VOL_NAME_PATT"%s", &devno, &vol_id, tmp_buf);
  1004. if (ret == 2 && devno == dev_num) {
  1005. info->vol_count += 1;
  1006. if (vol_id > info->highest_vol_id)
  1007. info->highest_vol_id = vol_id;
  1008. if (vol_id < info->lowest_vol_id)
  1009. info->lowest_vol_id = vol_id;
  1010. }
  1011. }
  1012. if (!dirent && errno) {
  1013. sys_errmsg("readdir failed on \"%s\"", lib->sysfs_ubi);
  1014. goto out_close;
  1015. }
  1016. if (closedir(sysfs_ubi)) {
  1017. sys_errmsg("closedir failed on \"%s\"", lib->sysfs_ubi);
  1018. return -1;
  1019. }
  1020. if (info->lowest_vol_id == INT_MAX)
  1021. info->lowest_vol_id = 0;
  1022. if (dev_get_major(lib, dev_num, &info->major, &info->minor))
  1023. return -1;
  1024. if (dev_read_int(lib->dev_mtd_num, dev_num, &info->mtd_num))
  1025. return -1;
  1026. if (dev_read_int(lib->dev_avail_ebs, dev_num, &info->avail_lebs))
  1027. return -1;
  1028. if (dev_read_int(lib->dev_total_ebs, dev_num, &info->total_lebs))
  1029. return -1;
  1030. if (dev_read_int(lib->dev_bad_count, dev_num, &info->bad_count))
  1031. return -1;
  1032. if (dev_read_int(lib->dev_eb_size, dev_num, &info->leb_size))
  1033. return -1;
  1034. if (dev_read_int(lib->dev_bad_rsvd, dev_num, &info->bad_rsvd))
  1035. return -1;
  1036. if (dev_read_ll(lib->dev_max_ec, dev_num, &info->max_ec))
  1037. return -1;
  1038. if (dev_read_int(lib->dev_max_vols, dev_num, &info->max_vol_count))
  1039. return -1;
  1040. if (dev_read_int(lib->dev_min_io_size, dev_num, &info->min_io_size))
  1041. return -1;
  1042. info->avail_bytes = (long long)info->avail_lebs * info->leb_size;
  1043. info->total_bytes = (long long)info->total_lebs * info->leb_size;
  1044. return 0;
  1045. out_close:
  1046. closedir(sysfs_ubi);
  1047. return -1;
  1048. }
  1049. int ubi_get_dev_info(libubi_t desc, const char *node, struct ubi_dev_info *info)
  1050. {
  1051. int err, dev_num = 0;
  1052. struct libubi *lib = (struct libubi *)desc;
  1053. err = ubi_probe_node(desc, node);
  1054. if (err != 1) {
  1055. if (err == 2)
  1056. errno = ENODEV;
  1057. return -1;
  1058. }
  1059. if (dev_node2num(lib, node, &dev_num))
  1060. return -1;
  1061. return ubi_get_dev_info1(desc, dev_num, info);
  1062. }
  1063. int ubi_get_vol_info1(libubi_t desc, int dev_num, int vol_id,
  1064. struct ubi_vol_info *info)
  1065. {
  1066. int ret;
  1067. struct libubi *lib = (struct libubi *)desc;
  1068. char buf[50];
  1069. memset(info, 0, sizeof(struct ubi_vol_info));
  1070. info->dev_num = dev_num;
  1071. info->vol_id = vol_id;
  1072. if (vol_get_major(lib, dev_num, vol_id, &info->major, &info->minor))
  1073. return -1;
  1074. ret = vol_read_data(lib->vol_type, dev_num, vol_id, buf, 50);
  1075. if (ret < 0)
  1076. return -1;
  1077. if (strncmp(buf, "static\n", ret) == 0)
  1078. info->type = UBI_STATIC_VOLUME;
  1079. else if (strncmp(buf, "dynamic\n", ret) == 0)
  1080. info->type = UBI_DYNAMIC_VOLUME;
  1081. else {
  1082. errmsg("bad value at \"%s\"", buf);
  1083. errno = EINVAL;
  1084. return -1;
  1085. }
  1086. ret = vol_read_int(lib->vol_alignment, dev_num, vol_id,
  1087. &info->alignment);
  1088. if (ret)
  1089. return -1;
  1090. ret = vol_read_ll(lib->vol_data_bytes, dev_num, vol_id,
  1091. &info->data_bytes);
  1092. if (ret)
  1093. return -1;
  1094. ret = vol_read_int(lib->vol_rsvd_ebs, dev_num, vol_id, &info->rsvd_lebs);
  1095. if (ret)
  1096. return -1;
  1097. ret = vol_read_int(lib->vol_eb_size, dev_num, vol_id, &info->leb_size);
  1098. if (ret)
  1099. return -1;
  1100. ret = vol_read_int(lib->vol_corrupted, dev_num, vol_id,
  1101. &info->corrupted);
  1102. if (ret)
  1103. return -1;
  1104. info->rsvd_bytes = (long long)info->leb_size * info->rsvd_lebs;
  1105. ret = vol_read_data(lib->vol_name, dev_num, vol_id, &info->name,
  1106. UBI_VOL_NAME_MAX + 2);
  1107. if (ret < 0)
  1108. return -1;
  1109. info->name[ret - 1] = '\0';
  1110. return 0;
  1111. }
  1112. int ubi_get_vol_info(libubi_t desc, const char *node, struct ubi_vol_info *info)
  1113. {
  1114. int err, vol_id = 0, dev_num = 0;
  1115. struct libubi *lib = (struct libubi *)desc;
  1116. err = ubi_probe_node(desc, node);
  1117. if (err != 2) {
  1118. if (err == 1)
  1119. errno = ENODEV;
  1120. return -1;
  1121. }
  1122. if (vol_node2nums(lib, node, &dev_num, &vol_id))
  1123. return -1;
  1124. return ubi_get_vol_info1(desc, dev_num, vol_id, info);
  1125. }
  1126. int ubi_get_vol_info1_nm(libubi_t desc, int dev_num, const char *name,
  1127. struct ubi_vol_info *info)
  1128. {
  1129. int i, err;
  1130. unsigned int nlen = strlen(name);
  1131. struct ubi_dev_info dev_info;
  1132. if (nlen == 0) {
  1133. errmsg("bad \"name\" input parameter");
  1134. errno = EINVAL;
  1135. return -1;
  1136. }
  1137. err = ubi_get_dev_info1(desc, dev_num, &dev_info);
  1138. if (err)
  1139. return err;
  1140. for (i = dev_info.lowest_vol_id;
  1141. i <= dev_info.highest_vol_id; i++) {
  1142. err = ubi_get_vol_info1(desc, dev_num, i, info);
  1143. if (err == -1) {
  1144. if (errno == ENOENT)
  1145. continue;
  1146. return -1;
  1147. }
  1148. if (nlen == strlen(info->name) && !strcmp(name, info->name))
  1149. return 0;
  1150. }
  1151. errno = ENOENT;
  1152. return -1;
  1153. }
  1154. int ubi_set_property(int fd, uint8_t property, uint64_t value)
  1155. {
  1156. struct ubi_set_vol_prop_req r;
  1157. memset(&r, 0, sizeof(struct ubi_set_vol_prop_req));
  1158. r.property = property;
  1159. r.value = value;
  1160. return ioctl(fd, UBI_IOCSETVOLPROP, &r);
  1161. }
  1162. int ubi_leb_unmap(int fd, int lnum)
  1163. {
  1164. return ioctl(fd, UBI_IOCEBUNMAP, &lnum);
  1165. }
  1166. int ubi_is_mapped(int fd, int lnum)
  1167. {
  1168. return ioctl(fd, UBI_IOCEBISMAP, &lnum);
  1169. }