ubi.c 15 KB

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