libubi.c 30 KB

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