ubi.c 15 KB

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