cmd_ubi.c 15 KB

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