ubi.c 14 KB

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