ubi.c 15 KB

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