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