ubi.c 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698
  1. /*
  2. * Unsorted Block Image commands
  3. *
  4. * Copyright (C) 2008 Samsung Electronics
  5. * Kyungmin Park <kyungmin.park@samsung.com>
  6. *
  7. * Copyright 2008-2009 Stefan Roese <sr@denx.de>, DENX Software Engineering
  8. *
  9. * This program is free software; you can redistribute it and/or modify
  10. * it under the terms of the GNU General Public License version 2 as
  11. * published by the Free Software Foundation.
  12. */
  13. #include <common.h>
  14. #include <command.h>
  15. #include <env.h>
  16. #include <exports.h>
  17. #include <memalign.h>
  18. #include <mtd.h>
  19. #include <nand.h>
  20. #include <onenand_uboot.h>
  21. #include <linux/mtd/mtd.h>
  22. #include <linux/mtd/partitions.h>
  23. #include <linux/err.h>
  24. #include <ubi_uboot.h>
  25. #include <linux/errno.h>
  26. #include <jffs2/load_kernel.h>
  27. #undef ubi_msg
  28. #define ubi_msg(fmt, ...) printf("UBI: " fmt "\n", ##__VA_ARGS__)
  29. /* Private own data */
  30. static struct ubi_device *ubi;
  31. #ifdef CONFIG_CMD_UBIFS
  32. #include <ubifs_uboot.h>
  33. #endif
  34. static void display_volume_info(struct ubi_device *ubi)
  35. {
  36. int i;
  37. for (i = 0; i < (ubi->vtbl_slots + 1); i++) {
  38. if (!ubi->volumes[i])
  39. continue; /* Empty record */
  40. ubi_dump_vol_info(ubi->volumes[i]);
  41. }
  42. }
  43. static void display_ubi_info(struct ubi_device *ubi)
  44. {
  45. ubi_msg("MTD device name: \"%s\"", ubi->mtd->name);
  46. ubi_msg("MTD device size: %llu MiB", ubi->flash_size >> 20);
  47. ubi_msg("physical eraseblock size: %d bytes (%d KiB)",
  48. ubi->peb_size, ubi->peb_size >> 10);
  49. ubi_msg("logical eraseblock size: %d bytes", ubi->leb_size);
  50. ubi_msg("number of good PEBs: %d", ubi->good_peb_count);
  51. ubi_msg("number of bad PEBs: %d", ubi->bad_peb_count);
  52. ubi_msg("smallest flash I/O unit: %d", ubi->min_io_size);
  53. ubi_msg("VID header offset: %d (aligned %d)",
  54. ubi->vid_hdr_offset, ubi->vid_hdr_aloffset);
  55. ubi_msg("data offset: %d", ubi->leb_start);
  56. ubi_msg("max. allowed volumes: %d", ubi->vtbl_slots);
  57. ubi_msg("wear-leveling threshold: %d", CONFIG_MTD_UBI_WL_THRESHOLD);
  58. ubi_msg("number of internal volumes: %d", UBI_INT_VOL_COUNT);
  59. ubi_msg("number of user volumes: %d",
  60. ubi->vol_count - UBI_INT_VOL_COUNT);
  61. ubi_msg("available PEBs: %d", ubi->avail_pebs);
  62. ubi_msg("total number of reserved PEBs: %d", ubi->rsvd_pebs);
  63. ubi_msg("number of PEBs reserved for bad PEB handling: %d",
  64. ubi->beb_rsvd_pebs);
  65. ubi_msg("max/mean erase counter: %d/%d", ubi->max_ec, ubi->mean_ec);
  66. }
  67. static int ubi_info(int layout)
  68. {
  69. if (layout)
  70. display_volume_info(ubi);
  71. else
  72. display_ubi_info(ubi);
  73. return 0;
  74. }
  75. static int ubi_check_volumename(const struct ubi_volume *vol, char *name)
  76. {
  77. return strcmp(vol->name, name);
  78. }
  79. static int ubi_check(char *name)
  80. {
  81. int i;
  82. for (i = 0; i < (ubi->vtbl_slots + 1); i++) {
  83. if (!ubi->volumes[i])
  84. continue; /* Empty record */
  85. if (!ubi_check_volumename(ubi->volumes[i], name))
  86. return 0;
  87. }
  88. return 1;
  89. }
  90. static int verify_mkvol_req(const struct ubi_device *ubi,
  91. const struct ubi_mkvol_req *req)
  92. {
  93. int n, err = EINVAL;
  94. if (req->bytes < 0 || req->alignment < 0 || req->vol_type < 0 ||
  95. req->name_len < 0)
  96. goto bad;
  97. if ((req->vol_id < 0 || req->vol_id >= ubi->vtbl_slots) &&
  98. req->vol_id != UBI_VOL_NUM_AUTO)
  99. goto bad;
  100. if (req->alignment == 0)
  101. goto bad;
  102. if (req->bytes == 0) {
  103. printf("No space left in UBI device!\n");
  104. err = ENOMEM;
  105. goto bad;
  106. }
  107. if (req->vol_type != UBI_DYNAMIC_VOLUME &&
  108. req->vol_type != UBI_STATIC_VOLUME)
  109. goto bad;
  110. if (req->alignment > ubi->leb_size)
  111. goto bad;
  112. n = req->alignment % ubi->min_io_size;
  113. if (req->alignment != 1 && n)
  114. goto bad;
  115. if (req->name_len > UBI_VOL_NAME_MAX) {
  116. printf("Name too long!\n");
  117. err = ENAMETOOLONG;
  118. goto bad;
  119. }
  120. return 0;
  121. bad:
  122. return err;
  123. }
  124. static int ubi_create_vol(char *volume, int64_t size, int dynamic, int vol_id,
  125. bool skipcheck)
  126. {
  127. struct ubi_mkvol_req req;
  128. int err;
  129. if (dynamic)
  130. req.vol_type = UBI_DYNAMIC_VOLUME;
  131. else
  132. req.vol_type = UBI_STATIC_VOLUME;
  133. req.vol_id = vol_id;
  134. req.alignment = 1;
  135. req.bytes = size;
  136. strcpy(req.name, volume);
  137. req.name_len = strlen(volume);
  138. req.name[req.name_len] = '\0';
  139. req.flags = 0;
  140. if (skipcheck)
  141. req.flags |= UBI_VOL_SKIP_CRC_CHECK_FLG;
  142. /* It's duplicated at drivers/mtd/ubi/cdev.c */
  143. err = verify_mkvol_req(ubi, &req);
  144. if (err) {
  145. printf("verify_mkvol_req failed %d\n", err);
  146. return err;
  147. }
  148. printf("Creating %s volume %s of size %lld\n",
  149. dynamic ? "dynamic" : "static", volume, size);
  150. /* Call real ubi create volume */
  151. return ubi_create_volume(ubi, &req);
  152. }
  153. static struct ubi_volume *ubi_find_volume(char *volume)
  154. {
  155. struct ubi_volume *vol = NULL;
  156. int i;
  157. for (i = 0; i < ubi->vtbl_slots; i++) {
  158. vol = ubi->volumes[i];
  159. if (vol && !strcmp(vol->name, volume))
  160. return vol;
  161. }
  162. printf("Volume %s not found!\n", volume);
  163. return NULL;
  164. }
  165. static int ubi_remove_vol(char *volume)
  166. {
  167. int err, reserved_pebs, i;
  168. struct ubi_volume *vol;
  169. vol = ubi_find_volume(volume);
  170. if (vol == NULL)
  171. return ENODEV;
  172. printf("Remove UBI volume %s (id %d)\n", vol->name, vol->vol_id);
  173. if (ubi->ro_mode) {
  174. printf("It's read-only mode\n");
  175. err = EROFS;
  176. goto out_err;
  177. }
  178. err = ubi_change_vtbl_record(ubi, vol->vol_id, NULL);
  179. if (err) {
  180. printf("Error changing Vol tabel record err=%x\n", err);
  181. goto out_err;
  182. }
  183. reserved_pebs = vol->reserved_pebs;
  184. for (i = 0; i < vol->reserved_pebs; i++) {
  185. err = ubi_eba_unmap_leb(ubi, vol, i);
  186. if (err)
  187. goto out_err;
  188. }
  189. kfree(vol->eba_tbl);
  190. ubi->volumes[vol->vol_id]->eba_tbl = NULL;
  191. ubi->volumes[vol->vol_id] = NULL;
  192. ubi->rsvd_pebs -= reserved_pebs;
  193. ubi->avail_pebs += reserved_pebs;
  194. i = ubi->beb_rsvd_level - ubi->beb_rsvd_pebs;
  195. if (i > 0) {
  196. i = ubi->avail_pebs >= i ? i : ubi->avail_pebs;
  197. ubi->avail_pebs -= i;
  198. ubi->rsvd_pebs += i;
  199. ubi->beb_rsvd_pebs += i;
  200. if (i > 0)
  201. ubi_msg("reserve more %d PEBs", i);
  202. }
  203. ubi->vol_count -= 1;
  204. return 0;
  205. out_err:
  206. ubi_err(ubi, "cannot remove volume %s, error %d", volume, err);
  207. if (err < 0)
  208. err = -err;
  209. return err;
  210. }
  211. static int ubi_volume_continue_write(char *volume, void *buf, size_t size)
  212. {
  213. int err = 1;
  214. struct ubi_volume *vol;
  215. vol = ubi_find_volume(volume);
  216. if (vol == NULL)
  217. return ENODEV;
  218. err = ubi_more_update_data(ubi, vol, buf, size);
  219. if (err < 0) {
  220. printf("Couldnt or partially wrote data\n");
  221. return -err;
  222. }
  223. if (err) {
  224. size = err;
  225. err = ubi_check_volume(ubi, vol->vol_id);
  226. if (err < 0)
  227. return -err;
  228. if (err) {
  229. ubi_warn(ubi, "volume %d on UBI device %d is corrupt",
  230. vol->vol_id, ubi->ubi_num);
  231. vol->corrupted = 1;
  232. }
  233. vol->checked = 1;
  234. ubi_gluebi_updated(vol);
  235. }
  236. return 0;
  237. }
  238. int ubi_volume_begin_write(char *volume, void *buf, size_t size,
  239. size_t full_size)
  240. {
  241. int err = 1;
  242. int rsvd_bytes = 0;
  243. struct ubi_volume *vol;
  244. vol = ubi_find_volume(volume);
  245. if (vol == NULL)
  246. return ENODEV;
  247. rsvd_bytes = vol->reserved_pebs * (ubi->leb_size - vol->data_pad);
  248. if (size > rsvd_bytes) {
  249. printf("size > volume size! Aborting!\n");
  250. return EINVAL;
  251. }
  252. err = ubi_start_update(ubi, vol, full_size);
  253. if (err < 0) {
  254. printf("Cannot start volume update\n");
  255. return -err;
  256. }
  257. return ubi_volume_continue_write(volume, buf, size);
  258. }
  259. int ubi_volume_write(char *volume, void *buf, size_t size)
  260. {
  261. return ubi_volume_begin_write(volume, buf, size, size);
  262. }
  263. int ubi_volume_read(char *volume, char *buf, size_t size)
  264. {
  265. int err, lnum, off, len, tbuf_size;
  266. void *tbuf;
  267. unsigned long long tmp;
  268. struct ubi_volume *vol;
  269. loff_t offp = 0;
  270. size_t len_read;
  271. vol = ubi_find_volume(volume);
  272. if (vol == NULL)
  273. return ENODEV;
  274. if (vol->updating) {
  275. printf("updating");
  276. return EBUSY;
  277. }
  278. if (vol->upd_marker) {
  279. printf("damaged volume, update marker is set");
  280. return EBADF;
  281. }
  282. if (offp == vol->used_bytes)
  283. return 0;
  284. if (size == 0) {
  285. printf("No size specified -> Using max size (%lld)\n", vol->used_bytes);
  286. size = vol->used_bytes;
  287. }
  288. printf("Read %zu bytes from volume %s to %p\n", size, volume, buf);
  289. if (vol->corrupted)
  290. printf("read from corrupted volume %d", vol->vol_id);
  291. if (offp + size > vol->used_bytes)
  292. size = vol->used_bytes - offp;
  293. tbuf_size = vol->usable_leb_size;
  294. if (size < tbuf_size)
  295. tbuf_size = ALIGN(size, ubi->min_io_size);
  296. tbuf = malloc_cache_aligned(tbuf_size);
  297. if (!tbuf) {
  298. printf("NO MEM\n");
  299. return ENOMEM;
  300. }
  301. len = size > tbuf_size ? tbuf_size : size;
  302. tmp = offp;
  303. off = do_div(tmp, vol->usable_leb_size);
  304. lnum = tmp;
  305. len_read = size;
  306. do {
  307. if (off + len >= vol->usable_leb_size)
  308. len = vol->usable_leb_size - off;
  309. err = ubi_eba_read_leb(ubi, vol, lnum, tbuf, off, len, 0);
  310. if (err) {
  311. printf("read err %x\n", err);
  312. err = -err;
  313. break;
  314. }
  315. off += len;
  316. if (off == vol->usable_leb_size) {
  317. lnum += 1;
  318. off -= vol->usable_leb_size;
  319. }
  320. size -= len;
  321. offp += len;
  322. memcpy(buf, tbuf, len);
  323. buf += len;
  324. len = size > tbuf_size ? tbuf_size : size;
  325. } while (size);
  326. if (!size)
  327. env_set_hex("filesize", len_read);
  328. free(tbuf);
  329. return err;
  330. }
  331. static int ubi_dev_scan(struct mtd_info *info, const char *vid_header_offset)
  332. {
  333. char ubi_mtd_param_buffer[80];
  334. int err;
  335. if (!vid_header_offset)
  336. sprintf(ubi_mtd_param_buffer, "%s", info->name);
  337. else
  338. sprintf(ubi_mtd_param_buffer, "%s,%s", info->name,
  339. vid_header_offset);
  340. err = ubi_mtd_param_parse(ubi_mtd_param_buffer, NULL);
  341. if (err)
  342. return -err;
  343. err = ubi_init();
  344. if (err)
  345. return -err;
  346. return 0;
  347. }
  348. static int ubi_set_skip_check(char *volume, bool skip_check)
  349. {
  350. struct ubi_vtbl_record vtbl_rec;
  351. struct ubi_volume *vol;
  352. vol = ubi_find_volume(volume);
  353. if (!vol)
  354. return ENODEV;
  355. printf("%sing skip_check on volume %s\n",
  356. skip_check ? "Sett" : "Clear", volume);
  357. vtbl_rec = ubi->vtbl[vol->vol_id];
  358. if (skip_check) {
  359. vtbl_rec.flags |= UBI_VTBL_SKIP_CRC_CHECK_FLG;
  360. vol->skip_check = 1;
  361. } else {
  362. vtbl_rec.flags &= ~UBI_VTBL_SKIP_CRC_CHECK_FLG;
  363. vol->skip_check = 0;
  364. }
  365. return ubi_change_vtbl_record(ubi, vol->vol_id, &vtbl_rec);
  366. }
  367. static int ubi_detach(void)
  368. {
  369. #ifdef CONFIG_CMD_UBIFS
  370. /*
  371. * Automatically unmount UBIFS partition when user
  372. * changes the UBI device. Otherwise the following
  373. * UBIFS commands will crash.
  374. */
  375. if (ubifs_is_mounted())
  376. cmd_ubifs_umount();
  377. #endif
  378. /*
  379. * Call ubi_exit() before re-initializing the UBI subsystem
  380. */
  381. if (ubi)
  382. ubi_exit();
  383. ubi = NULL;
  384. return 0;
  385. }
  386. int ubi_part(char *part_name, const char *vid_header_offset)
  387. {
  388. struct mtd_info *mtd;
  389. int err = 0;
  390. ubi_detach();
  391. mtd_probe_devices();
  392. mtd = get_mtd_device_nm(part_name);
  393. if (IS_ERR(mtd)) {
  394. printf("Partition %s not found!\n", part_name);
  395. return 1;
  396. }
  397. put_mtd_device(mtd);
  398. err = ubi_dev_scan(mtd, vid_header_offset);
  399. if (err) {
  400. printf("UBI init error %d\n", err);
  401. printf("Please check, if the correct MTD partition is used (size big enough?)\n");
  402. return err;
  403. }
  404. ubi = ubi_devices[0];
  405. return 0;
  406. }
  407. static int do_ubi(cmd_tbl_t *cmdtp, int flag, int argc, char * const argv[])
  408. {
  409. int64_t size = 0;
  410. ulong addr = 0;
  411. bool skipcheck = false;
  412. if (argc < 2)
  413. return CMD_RET_USAGE;
  414. if (strcmp(argv[1], "detach") == 0)
  415. return ubi_detach();
  416. if (strcmp(argv[1], "part") == 0) {
  417. const char *vid_header_offset = NULL;
  418. /* Print current partition */
  419. if (argc == 2) {
  420. if (!ubi) {
  421. printf("Error, no UBI device selected!\n");
  422. return 1;
  423. }
  424. printf("Device %d: %s, MTD partition %s\n",
  425. ubi->ubi_num, ubi->ubi_name, ubi->mtd->name);
  426. return 0;
  427. }
  428. if (argc < 3)
  429. return CMD_RET_USAGE;
  430. if (argc > 3)
  431. vid_header_offset = argv[3];
  432. return ubi_part(argv[2], vid_header_offset);
  433. }
  434. if ((strcmp(argv[1], "part") != 0) && !ubi) {
  435. printf("Error, no UBI device selected!\n");
  436. return 1;
  437. }
  438. if (strcmp(argv[1], "info") == 0) {
  439. int layout = 0;
  440. if (argc > 2 && !strncmp(argv[2], "l", 1))
  441. layout = 1;
  442. return ubi_info(layout);
  443. }
  444. if (strcmp(argv[1], "check") == 0) {
  445. if (argc > 2)
  446. return ubi_check(argv[2]);
  447. printf("Error, no volume name passed\n");
  448. return 1;
  449. }
  450. if (strncmp(argv[1], "create", 6) == 0) {
  451. int dynamic = 1; /* default: dynamic volume */
  452. int id = UBI_VOL_NUM_AUTO;
  453. /* Use maximum available size */
  454. size = 0;
  455. /* E.g., create volume with "skipcheck" bit set */
  456. if (argc == 7) {
  457. skipcheck = strncmp(argv[6], "--skipcheck", 11) == 0;
  458. argc--;
  459. }
  460. /* E.g., create volume size type vol_id */
  461. if (argc == 6) {
  462. id = simple_strtoull(argv[5], NULL, 16);
  463. argc--;
  464. }
  465. /* E.g., create volume size type */
  466. if (argc == 5) {
  467. if (strncmp(argv[4], "s", 1) == 0)
  468. dynamic = 0;
  469. else if (strncmp(argv[4], "d", 1) != 0) {
  470. printf("Incorrect type\n");
  471. return 1;
  472. }
  473. argc--;
  474. }
  475. /* E.g., create volume size */
  476. if (argc == 4) {
  477. if (argv[3][0] != '-')
  478. size = simple_strtoull(argv[3], NULL, 16);
  479. argc--;
  480. }
  481. /* Use maximum available size */
  482. if (!size) {
  483. size = (int64_t)ubi->avail_pebs * ubi->leb_size;
  484. printf("No size specified -> Using max size (%lld)\n", size);
  485. }
  486. /* E.g., create volume */
  487. if (argc == 3) {
  488. return ubi_create_vol(argv[2], size, dynamic, id,
  489. skipcheck);
  490. }
  491. }
  492. if (strncmp(argv[1], "remove", 6) == 0) {
  493. /* E.g., remove volume */
  494. if (argc == 3)
  495. return ubi_remove_vol(argv[2]);
  496. }
  497. if (strncmp(argv[1], "skipcheck", 9) == 0) {
  498. /* E.g., change skip_check flag */
  499. if (argc == 4) {
  500. skipcheck = strncmp(argv[3], "on", 2) == 0;
  501. return ubi_set_skip_check(argv[2], skipcheck);
  502. }
  503. }
  504. if (strncmp(argv[1], "write", 5) == 0) {
  505. int ret;
  506. if (argc < 5) {
  507. printf("Please see usage\n");
  508. return 1;
  509. }
  510. addr = simple_strtoul(argv[2], NULL, 16);
  511. size = simple_strtoul(argv[4], NULL, 16);
  512. if (strlen(argv[1]) == 10 &&
  513. strncmp(argv[1] + 5, ".part", 5) == 0) {
  514. if (argc < 6) {
  515. ret = ubi_volume_continue_write(argv[3],
  516. (void *)addr, size);
  517. } else {
  518. size_t full_size;
  519. full_size = simple_strtoul(argv[5], NULL, 16);
  520. ret = ubi_volume_begin_write(argv[3],
  521. (void *)addr, size, full_size);
  522. }
  523. } else {
  524. ret = ubi_volume_write(argv[3], (void *)addr, size);
  525. }
  526. if (!ret) {
  527. printf("%lld bytes written to volume %s\n", size,
  528. argv[3]);
  529. }
  530. return ret;
  531. }
  532. if (strncmp(argv[1], "read", 4) == 0) {
  533. size = 0;
  534. /* E.g., read volume size */
  535. if (argc == 5) {
  536. size = simple_strtoul(argv[4], NULL, 16);
  537. argc--;
  538. }
  539. /* E.g., read volume */
  540. if (argc == 4) {
  541. addr = simple_strtoul(argv[2], NULL, 16);
  542. argc--;
  543. }
  544. if (argc == 3) {
  545. return ubi_volume_read(argv[3], (char *)addr, size);
  546. }
  547. }
  548. printf("Please see usage\n");
  549. return 1;
  550. }
  551. U_BOOT_CMD(
  552. ubi, 7, 1, do_ubi,
  553. "ubi commands",
  554. "detach"
  555. " - detach ubi from a mtd partition\n"
  556. "ubi part [part] [offset]\n"
  557. " - Show or set current partition (with optional VID"
  558. " header offset)\n"
  559. "ubi info [l[ayout]]"
  560. " - Display volume and ubi layout information\n"
  561. "ubi check volumename"
  562. " - check if volumename exists\n"
  563. "ubi create[vol] volume [size] [type] [id] [--skipcheck]\n"
  564. " - create volume name with size ('-' for maximum"
  565. " available size)\n"
  566. "ubi write[vol] address volume size"
  567. " - Write volume from address with size\n"
  568. "ubi write.part address volume size [fullsize]\n"
  569. " - Write part of a volume from address\n"
  570. "ubi read[vol] address volume [size]"
  571. " - Read volume to address with size\n"
  572. "ubi remove[vol] volume"
  573. " - Remove volume\n"
  574. "ubi skipcheck volume on/off - Set or clear skip_check flag in volume header\n"
  575. "[Legends]\n"
  576. " volume: character name\n"
  577. " size: specified in bytes\n"
  578. " type: s[tatic] or d[ynamic] (default=dynamic)"
  579. );