cmd_ubi.c 14 KB

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