ubi.c 6.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337
  1. /*
  2. * Copyright (C) 2014 John Crispin <blogic@openwrt.org>
  3. *
  4. * This program is free software; you can redistribute it and/or modify
  5. * it under the terms of the GNU Lesser General Public License version 2.1
  6. * as published by the Free Software Foundation
  7. *
  8. * This program is distributed in the hope that it will be useful,
  9. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  10. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  11. * GNU General Public License for more details.
  12. */
  13. #include <sys/types.h>
  14. #include <sys/stat.h>
  15. #include <fcntl.h>
  16. #include <stdio.h>
  17. #include <stdint.h>
  18. #include <getopt.h>
  19. #include <stdlib.h>
  20. #include <string.h>
  21. #include <unistd.h>
  22. #include <libubox/ulog.h>
  23. #include "libubi/libubi-tiny.h"
  24. static int print_usage(void)
  25. {
  26. printf("ubi info\n");
  27. printf("ubi detach kernel|rootfs\n");
  28. printf("ubi kernel <image.kernel.ubi>\n");
  29. printf("ubi rootfs <image.rootfs.ubi>\n");
  30. printf("ubi overlay <image.rootfs-overlay.ubi>\n");
  31. return -1;
  32. }
  33. static int mtd_find_index(char *name)
  34. {
  35. FILE *fp = fopen("/proc/mtd", "r");
  36. char line[256];
  37. char *index = NULL;
  38. if (!fp)
  39. return -1;
  40. while (!index && fgets(line, sizeof(line), fp)) {
  41. if (strstr(line, name)) {
  42. char *eol = strstr(line, ":");
  43. if (!eol)
  44. continue;
  45. *eol = '\0';
  46. index = &line[3];
  47. }
  48. }
  49. fclose(fp);
  50. if (!index)
  51. return -1;
  52. return atoi(index);
  53. }
  54. static int mtd_find(char *name, char *ret)
  55. {
  56. int index = mtd_find_index(name);
  57. if (index < 0)
  58. return -1;
  59. sprintf(ret, "/dev/mtd%d", index);
  60. return 0;
  61. }
  62. static int ubi_find(libubi_t libubi, char *name, char *ret)
  63. {
  64. int index = mtd_find_index(name);
  65. int ubi = 0;
  66. while (ubi_dev_present(libubi, ubi))
  67. {
  68. struct ubi_dev_info info;
  69. if (ubi_get_dev_info1(libubi, ubi++, &info))
  70. continue;
  71. if (info.mtd_num != index)
  72. continue;
  73. sprintf(ret, "/dev/ubi%d", info.dev_num);
  74. return 0;
  75. }
  76. return -1;
  77. }
  78. static int volume_find(libubi_t libubi, char *name, char *ret)
  79. {
  80. int index = mtd_find_index(name);
  81. struct ubi_vol_info vol;
  82. int ubi = 0;
  83. if (index < 0)
  84. return -1;
  85. if (mtd_num2ubi_dev(libubi, index, &ubi)) {
  86. ULOG_ERR("failed to get ubi node for %s\n", name);
  87. return -1;
  88. }
  89. if (ubi_get_vol_info1_nm(libubi, ubi, name, &vol)) {
  90. ULOG_ERR("failed to get ubi volume info for %s\n", name);
  91. return -1;
  92. }
  93. sprintf(ret, "/dev/ubi%d_%d", ubi, vol.vol_id);
  94. return 0;
  95. }
  96. static int main_detach(char *type)
  97. {
  98. libubi_t libubi;
  99. char mtd[64];
  100. int err;
  101. if (!strcmp(type, "kernel"))
  102. err = mtd_find("kernel_ubi", mtd);
  103. else if (!strcmp(type, "rootfs"))
  104. err = mtd_find("rootfs_ubi", mtd);
  105. else
  106. return print_usage();
  107. if (err) {
  108. ULOG_ERR("MTD partition '%s_ubi' not found\n", type);
  109. return -1;
  110. }
  111. libubi = libubi_open();
  112. if (!libubi) {
  113. ULOG_ERR("cannot open libubi");
  114. return -1;
  115. }
  116. err = ubidetach(libubi, mtd);
  117. if (err) {
  118. ULOG_ERR("cannot detach \"%s\"", mtd);
  119. libubi_close(libubi);
  120. return -1;
  121. }
  122. libubi_close(libubi);
  123. return 0;
  124. }
  125. static int main_image(char *partition, char *image, char *overlay)
  126. {
  127. libubi_t libubi;
  128. struct stat s;
  129. int err;
  130. char mtd[64];
  131. char _part[64];
  132. char node[64];
  133. char volume[64];
  134. char _data[64];
  135. char *data = NULL;
  136. if (stat(image, &s)) {
  137. ULOG_ERR("image not found %s\n", image);
  138. return -1;
  139. }
  140. if (!strcmp(partition, "kernel"))
  141. err = mtd_find("kernel", _part);
  142. else
  143. err = mtd_find("rootfs", _part);
  144. if (overlay && !mtd_find(overlay, _data))
  145. data = _data;
  146. libubi = libubi_open();
  147. if (!libubi) {
  148. ULOG_ERR("cannot open libubi");
  149. return -1;
  150. }
  151. if (!strcmp(partition, "kernel"))
  152. err = mtd_find("kernel_ubi", mtd);
  153. else
  154. err = mtd_find("rootfs_ubi", mtd);
  155. if (err) {
  156. ULOG_ERR("MTD partition '%s_ubi' not found\n", partition);
  157. libubi_close(libubi);
  158. return -1;
  159. }
  160. if (!strcmp(partition, "kernel"))
  161. err = ubi_find(libubi, "kernel_ubi", node);
  162. else
  163. err = ubi_find(libubi, "rootfs_ubi", node);
  164. if (err) {
  165. ULOG_ERR("UBI volume '%s' not found\n", partition);
  166. libubi_close(libubi);
  167. return -1;
  168. }
  169. err = ubidetach(libubi, mtd);
  170. if (err) {
  171. ULOG_ERR("cannot detach \"%s\"", mtd);
  172. libubi_close(libubi);
  173. return -1;
  174. }
  175. err = ubiattach(libubi, mtd);
  176. if (err) {
  177. ULOG_ERR("cannot attach \"%s\"", mtd);
  178. libubi_close(libubi);
  179. return -1;
  180. }
  181. if (data) {
  182. err = ubirmvol(libubi, node, overlay);
  183. if (err) {
  184. ULOG_ERR("cannot remove \"%s\"", node);
  185. libubi_close(libubi);
  186. return -1;
  187. }
  188. }
  189. if (volume_find(libubi, partition, volume) < 0) {
  190. ULOG_ERR("UBI volume '%s' not found\n", partition);
  191. libubi_close(libubi);
  192. return -1;
  193. }
  194. err = ubirsvol(libubi, node, partition, s.st_size);
  195. if (err) {
  196. ULOG_ERR("cannot resize \"%s\"", partition);
  197. libubi_close(libubi);
  198. return -1;
  199. }
  200. err = ubiupdatevol(libubi, volume, image);
  201. if (err) {
  202. ULOG_ERR("cannot update \"%s\"", volume);
  203. libubi_close(libubi);
  204. return -1;
  205. }
  206. if (overlay) {
  207. err = ubimkvol(libubi, node, overlay, 1);
  208. if (err) {
  209. ULOG_ERR("cannot make \"%s\"", overlay);
  210. libubi_close(libubi);
  211. return -1;
  212. }
  213. }
  214. libubi_close(libubi);
  215. return err;
  216. }
  217. static int main_info(void)
  218. {
  219. struct ubi_info info;
  220. libubi_t libubi;
  221. int i;
  222. libubi = libubi_open();
  223. if (!libubi) {
  224. ULOG_ERR("cannot open libubi");
  225. return -1;
  226. }
  227. if (ubi_get_info(libubi, &info)) {
  228. ULOG_ERR("failed to get info\n");
  229. libubi_close(libubi);
  230. return -1;
  231. }
  232. for (i = info.lowest_dev_num; i <= info.highest_dev_num; i++) {
  233. struct ubi_dev_info dinfo;
  234. char ubi[64];
  235. int j;
  236. sprintf(ubi, "/dev/ubi%d", i);
  237. if (ubi_get_dev_info(libubi, ubi, &dinfo))
  238. continue;
  239. printf("device - %s\n size: %lldBytes\n bad blocks: %d\n",
  240. &ubi[5], dinfo.total_bytes, dinfo.bad_count);
  241. for (j = dinfo.lowest_vol_id; j <= dinfo.highest_vol_id; j++) {
  242. struct ubi_vol_info vinfo;
  243. sprintf(ubi, "/dev/ubi%d_%d", i, j);
  244. if (ubi_get_vol_info(libubi, ubi, &vinfo))
  245. continue;
  246. printf(" volume - %s\n", &ubi[5]);
  247. printf("\tname: %s\n", vinfo.name);
  248. printf("\tsize: %lld\n", vinfo.data_bytes);
  249. }
  250. }
  251. libubi_close(libubi);
  252. return 0;
  253. }
  254. int main(int argc, char **argv)
  255. {
  256. if (argc > 1 && !strcmp(argv[1], "info"))
  257. return main_info();
  258. if (argc < 3)
  259. return print_usage();
  260. if (!strcmp(argv[1], "kernel")) {
  261. return main_image("kernel", argv[2], NULL);
  262. } else if (!strcmp(argv[1], "rootfs")) {
  263. return main_image("rootfs", argv[2], NULL);
  264. } else if (!strcmp(argv[1], "overlay")) {
  265. return main_image("rootfs", argv[2], "rootfs_data");
  266. } else if (!strcmp(argv[1], "detach")) {
  267. return main_detach(argv[2]);
  268. }
  269. return -1;
  270. }