ubi.c 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652
  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. {
  126. struct ubi_mkvol_req req;
  127. int err;
  128. if (dynamic)
  129. req.vol_type = UBI_DYNAMIC_VOLUME;
  130. else
  131. req.vol_type = UBI_STATIC_VOLUME;
  132. req.vol_id = vol_id;
  133. req.alignment = 1;
  134. req.bytes = size;
  135. strcpy(req.name, volume);
  136. req.name_len = strlen(volume);
  137. req.name[req.name_len] = '\0';
  138. req.padding1 = 0;
  139. /* It's duplicated at drivers/mtd/ubi/cdev.c */
  140. err = verify_mkvol_req(ubi, &req);
  141. if (err) {
  142. printf("verify_mkvol_req failed %d\n", err);
  143. return err;
  144. }
  145. printf("Creating %s volume %s of size %lld\n",
  146. dynamic ? "dynamic" : "static", volume, size);
  147. /* Call real ubi create volume */
  148. return ubi_create_volume(ubi, &req);
  149. }
  150. static struct ubi_volume *ubi_find_volume(char *volume)
  151. {
  152. struct ubi_volume *vol = NULL;
  153. int i;
  154. for (i = 0; i < ubi->vtbl_slots; i++) {
  155. vol = ubi->volumes[i];
  156. if (vol && !strcmp(vol->name, volume))
  157. return vol;
  158. }
  159. printf("Volume %s not found!\n", volume);
  160. return NULL;
  161. }
  162. static int ubi_remove_vol(char *volume)
  163. {
  164. int err, reserved_pebs, i;
  165. struct ubi_volume *vol;
  166. vol = ubi_find_volume(volume);
  167. if (vol == NULL)
  168. return ENODEV;
  169. printf("Remove UBI volume %s (id %d)\n", vol->name, vol->vol_id);
  170. if (ubi->ro_mode) {
  171. printf("It's read-only mode\n");
  172. err = EROFS;
  173. goto out_err;
  174. }
  175. err = ubi_change_vtbl_record(ubi, vol->vol_id, NULL);
  176. if (err) {
  177. printf("Error changing Vol tabel record err=%x\n", err);
  178. goto out_err;
  179. }
  180. reserved_pebs = vol->reserved_pebs;
  181. for (i = 0; i < vol->reserved_pebs; i++) {
  182. err = ubi_eba_unmap_leb(ubi, vol, i);
  183. if (err)
  184. goto out_err;
  185. }
  186. kfree(vol->eba_tbl);
  187. ubi->volumes[vol->vol_id]->eba_tbl = NULL;
  188. ubi->volumes[vol->vol_id] = NULL;
  189. ubi->rsvd_pebs -= reserved_pebs;
  190. ubi->avail_pebs += reserved_pebs;
  191. i = ubi->beb_rsvd_level - ubi->beb_rsvd_pebs;
  192. if (i > 0) {
  193. i = ubi->avail_pebs >= i ? i : ubi->avail_pebs;
  194. ubi->avail_pebs -= i;
  195. ubi->rsvd_pebs += i;
  196. ubi->beb_rsvd_pebs += i;
  197. if (i > 0)
  198. ubi_msg("reserve more %d PEBs", i);
  199. }
  200. ubi->vol_count -= 1;
  201. return 0;
  202. out_err:
  203. ubi_err(ubi, "cannot remove volume %s, error %d", volume, err);
  204. if (err < 0)
  205. err = -err;
  206. return err;
  207. }
  208. static int ubi_volume_continue_write(char *volume, void *buf, size_t size)
  209. {
  210. int err = 1;
  211. struct ubi_volume *vol;
  212. vol = ubi_find_volume(volume);
  213. if (vol == NULL)
  214. return ENODEV;
  215. err = ubi_more_update_data(ubi, vol, buf, size);
  216. if (err < 0) {
  217. printf("Couldnt or partially wrote data\n");
  218. return -err;
  219. }
  220. if (err) {
  221. size = err;
  222. err = ubi_check_volume(ubi, vol->vol_id);
  223. if (err < 0)
  224. return -err;
  225. if (err) {
  226. ubi_warn(ubi, "volume %d on UBI device %d is corrupt",
  227. vol->vol_id, ubi->ubi_num);
  228. vol->corrupted = 1;
  229. }
  230. vol->checked = 1;
  231. ubi_gluebi_updated(vol);
  232. }
  233. return 0;
  234. }
  235. int ubi_volume_begin_write(char *volume, void *buf, size_t size,
  236. size_t full_size)
  237. {
  238. int err = 1;
  239. int rsvd_bytes = 0;
  240. struct ubi_volume *vol;
  241. vol = ubi_find_volume(volume);
  242. if (vol == NULL)
  243. return ENODEV;
  244. rsvd_bytes = vol->reserved_pebs * (ubi->leb_size - vol->data_pad);
  245. if (size > rsvd_bytes) {
  246. printf("size > volume size! Aborting!\n");
  247. return EINVAL;
  248. }
  249. err = ubi_start_update(ubi, vol, full_size);
  250. if (err < 0) {
  251. printf("Cannot start volume update\n");
  252. return -err;
  253. }
  254. return ubi_volume_continue_write(volume, buf, size);
  255. }
  256. int ubi_volume_write(char *volume, void *buf, size_t size)
  257. {
  258. return ubi_volume_begin_write(volume, buf, size, size);
  259. }
  260. int ubi_volume_read(char *volume, char *buf, size_t size)
  261. {
  262. int err, lnum, off, len, tbuf_size;
  263. void *tbuf;
  264. unsigned long long tmp;
  265. struct ubi_volume *vol;
  266. loff_t offp = 0;
  267. size_t len_read;
  268. vol = ubi_find_volume(volume);
  269. if (vol == NULL)
  270. return ENODEV;
  271. if (vol->updating) {
  272. printf("updating");
  273. return EBUSY;
  274. }
  275. if (vol->upd_marker) {
  276. printf("damaged volume, update marker is set");
  277. return EBADF;
  278. }
  279. if (offp == vol->used_bytes)
  280. return 0;
  281. if (size == 0) {
  282. printf("No size specified -> Using max size (%lld)\n", vol->used_bytes);
  283. size = vol->used_bytes;
  284. }
  285. printf("Read %zu bytes from volume %s to %p\n", size, volume, buf);
  286. if (vol->corrupted)
  287. printf("read from corrupted volume %d", vol->vol_id);
  288. if (offp + size > vol->used_bytes)
  289. size = vol->used_bytes - offp;
  290. tbuf_size = vol->usable_leb_size;
  291. if (size < tbuf_size)
  292. tbuf_size = ALIGN(size, ubi->min_io_size);
  293. tbuf = malloc_cache_aligned(tbuf_size);
  294. if (!tbuf) {
  295. printf("NO MEM\n");
  296. return ENOMEM;
  297. }
  298. len = size > tbuf_size ? tbuf_size : size;
  299. tmp = offp;
  300. off = do_div(tmp, vol->usable_leb_size);
  301. lnum = tmp;
  302. len_read = size;
  303. do {
  304. if (off + len >= vol->usable_leb_size)
  305. len = vol->usable_leb_size - off;
  306. err = ubi_eba_read_leb(ubi, vol, lnum, tbuf, off, len, 0);
  307. if (err) {
  308. printf("read err %x\n", err);
  309. err = -err;
  310. break;
  311. }
  312. off += len;
  313. if (off == vol->usable_leb_size) {
  314. lnum += 1;
  315. off -= vol->usable_leb_size;
  316. }
  317. size -= len;
  318. offp += len;
  319. memcpy(buf, tbuf, len);
  320. buf += len;
  321. len = size > tbuf_size ? tbuf_size : size;
  322. } while (size);
  323. if (!size)
  324. env_set_hex("filesize", len_read);
  325. free(tbuf);
  326. return err;
  327. }
  328. static int ubi_dev_scan(struct mtd_info *info, const char *vid_header_offset)
  329. {
  330. char ubi_mtd_param_buffer[80];
  331. int err;
  332. if (!vid_header_offset)
  333. sprintf(ubi_mtd_param_buffer, "%s", info->name);
  334. else
  335. sprintf(ubi_mtd_param_buffer, "%s,%s", info->name,
  336. vid_header_offset);
  337. err = ubi_mtd_param_parse(ubi_mtd_param_buffer, NULL);
  338. if (err)
  339. return -err;
  340. err = ubi_init();
  341. if (err)
  342. return -err;
  343. return 0;
  344. }
  345. static int ubi_detach(void)
  346. {
  347. #ifdef CONFIG_CMD_UBIFS
  348. /*
  349. * Automatically unmount UBIFS partition when user
  350. * changes the UBI device. Otherwise the following
  351. * UBIFS commands will crash.
  352. */
  353. if (ubifs_is_mounted())
  354. cmd_ubifs_umount();
  355. #endif
  356. /*
  357. * Call ubi_exit() before re-initializing the UBI subsystem
  358. */
  359. if (ubi)
  360. ubi_exit();
  361. ubi = NULL;
  362. return 0;
  363. }
  364. int ubi_part(char *part_name, const char *vid_header_offset)
  365. {
  366. struct mtd_info *mtd;
  367. int err = 0;
  368. ubi_detach();
  369. mtd_probe_devices();
  370. mtd = get_mtd_device_nm(part_name);
  371. if (IS_ERR(mtd)) {
  372. printf("Partition %s not found!\n", part_name);
  373. return 1;
  374. }
  375. put_mtd_device(mtd);
  376. err = ubi_dev_scan(mtd, vid_header_offset);
  377. if (err) {
  378. printf("UBI init error %d\n", err);
  379. printf("Please check, if the correct MTD partition is used (size big enough?)\n");
  380. return err;
  381. }
  382. ubi = ubi_devices[0];
  383. return 0;
  384. }
  385. static int do_ubi(cmd_tbl_t *cmdtp, int flag, int argc, char * const argv[])
  386. {
  387. int64_t size = 0;
  388. ulong addr = 0;
  389. if (argc < 2)
  390. return CMD_RET_USAGE;
  391. if (strcmp(argv[1], "detach") == 0)
  392. return ubi_detach();
  393. if (strcmp(argv[1], "part") == 0) {
  394. const char *vid_header_offset = NULL;
  395. /* Print current partition */
  396. if (argc == 2) {
  397. if (!ubi) {
  398. printf("Error, no UBI device selected!\n");
  399. return 1;
  400. }
  401. printf("Device %d: %s, MTD partition %s\n",
  402. ubi->ubi_num, ubi->ubi_name, ubi->mtd->name);
  403. return 0;
  404. }
  405. if (argc < 3)
  406. return CMD_RET_USAGE;
  407. if (argc > 3)
  408. vid_header_offset = argv[3];
  409. return ubi_part(argv[2], vid_header_offset);
  410. }
  411. if ((strcmp(argv[1], "part") != 0) && !ubi) {
  412. printf("Error, no UBI device selected!\n");
  413. return 1;
  414. }
  415. if (strcmp(argv[1], "info") == 0) {
  416. int layout = 0;
  417. if (argc > 2 && !strncmp(argv[2], "l", 1))
  418. layout = 1;
  419. return ubi_info(layout);
  420. }
  421. if (strcmp(argv[1], "check") == 0) {
  422. if (argc > 2)
  423. return ubi_check(argv[2]);
  424. printf("Error, no volume name passed\n");
  425. return 1;
  426. }
  427. if (strncmp(argv[1], "create", 6) == 0) {
  428. int dynamic = 1; /* default: dynamic volume */
  429. int id = UBI_VOL_NUM_AUTO;
  430. /* Use maximum available size */
  431. size = 0;
  432. /* E.g., create volume size type vol_id */
  433. if (argc == 6) {
  434. id = simple_strtoull(argv[5], NULL, 16);
  435. argc--;
  436. }
  437. /* E.g., create volume size type */
  438. if (argc == 5) {
  439. if (strncmp(argv[4], "s", 1) == 0)
  440. dynamic = 0;
  441. else if (strncmp(argv[4], "d", 1) != 0) {
  442. printf("Incorrect type\n");
  443. return 1;
  444. }
  445. argc--;
  446. }
  447. /* E.g., create volume size */
  448. if (argc == 4) {
  449. if (argv[3][0] != '-')
  450. size = simple_strtoull(argv[3], NULL, 16);
  451. argc--;
  452. }
  453. /* Use maximum available size */
  454. if (!size) {
  455. size = (int64_t)ubi->avail_pebs * ubi->leb_size;
  456. printf("No size specified -> Using max size (%lld)\n", size);
  457. }
  458. /* E.g., create volume */
  459. if (argc == 3)
  460. return ubi_create_vol(argv[2], size, dynamic, id);
  461. }
  462. if (strncmp(argv[1], "remove", 6) == 0) {
  463. /* E.g., remove volume */
  464. if (argc == 3)
  465. return ubi_remove_vol(argv[2]);
  466. }
  467. if (strncmp(argv[1], "write", 5) == 0) {
  468. int ret;
  469. if (argc < 5) {
  470. printf("Please see usage\n");
  471. return 1;
  472. }
  473. addr = simple_strtoul(argv[2], NULL, 16);
  474. size = simple_strtoul(argv[4], NULL, 16);
  475. if (strlen(argv[1]) == 10 &&
  476. strncmp(argv[1] + 5, ".part", 5) == 0) {
  477. if (argc < 6) {
  478. ret = ubi_volume_continue_write(argv[3],
  479. (void *)addr, size);
  480. } else {
  481. size_t full_size;
  482. full_size = simple_strtoul(argv[5], NULL, 16);
  483. ret = ubi_volume_begin_write(argv[3],
  484. (void *)addr, size, full_size);
  485. }
  486. } else {
  487. ret = ubi_volume_write(argv[3], (void *)addr, size);
  488. }
  489. if (!ret) {
  490. printf("%lld bytes written to volume %s\n", size,
  491. argv[3]);
  492. }
  493. return ret;
  494. }
  495. if (strncmp(argv[1], "read", 4) == 0) {
  496. size = 0;
  497. /* E.g., read volume size */
  498. if (argc == 5) {
  499. size = simple_strtoul(argv[4], NULL, 16);
  500. argc--;
  501. }
  502. /* E.g., read volume */
  503. if (argc == 4) {
  504. addr = simple_strtoul(argv[2], NULL, 16);
  505. argc--;
  506. }
  507. if (argc == 3) {
  508. return ubi_volume_read(argv[3], (char *)addr, size);
  509. }
  510. }
  511. printf("Please see usage\n");
  512. return 1;
  513. }
  514. U_BOOT_CMD(
  515. ubi, 6, 1, do_ubi,
  516. "ubi commands",
  517. "detach"
  518. " - detach ubi from a mtd partition\n"
  519. "ubi part [part] [offset]\n"
  520. " - Show or set current partition (with optional VID"
  521. " header offset)\n"
  522. "ubi info [l[ayout]]"
  523. " - Display volume and ubi layout information\n"
  524. "ubi check volumename"
  525. " - check if volumename exists\n"
  526. "ubi create[vol] volume [size] [type] [id]\n"
  527. " - create volume name with size ('-' for maximum"
  528. " available size)\n"
  529. "ubi write[vol] address volume size"
  530. " - Write volume from address with size\n"
  531. "ubi write.part address volume size [fullsize]\n"
  532. " - Write part of a volume from address\n"
  533. "ubi read[vol] address volume [size]"
  534. " - Read volume to address with size\n"
  535. "ubi remove[vol] volume"
  536. " - Remove volume\n"
  537. "[Legends]\n"
  538. " volume: character name\n"
  539. " size: specified in bytes\n"
  540. " type: s[tatic] or d[ynamic] (default=dynamic)"
  541. );