cmd_ubi.c 14 KB

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