legacy-mtd-utils.c 1.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899
  1. // SPDX-License-Identifier: GPL-2.0+
  2. #include <common.h>
  3. #include <jffs2/jffs2.h>
  4. #include <linux/mtd/mtd.h>
  5. #include <linux/mtd/partitions.h>
  6. #include <linux/string.h>
  7. #include <mtd.h>
  8. static int get_part(const char *partname, int *idx, loff_t *off, loff_t *size,
  9. loff_t *maxsize, int devtype)
  10. {
  11. #ifdef CONFIG_CMD_MTDPARTS
  12. struct mtd_device *dev;
  13. struct part_info *part;
  14. u8 pnum;
  15. int ret;
  16. ret = mtdparts_init();
  17. if (ret)
  18. return ret;
  19. ret = find_dev_and_part(partname, &dev, &pnum, &part);
  20. if (ret)
  21. return ret;
  22. if (dev->id->type != devtype) {
  23. printf("not same typ %d != %d\n", dev->id->type, devtype);
  24. return -1;
  25. }
  26. *off = part->offset;
  27. *size = part->size;
  28. *maxsize = part->size;
  29. *idx = dev->id->num;
  30. return 0;
  31. #else
  32. puts("mtdparts support missing.\n");
  33. return -1;
  34. #endif
  35. }
  36. int mtd_arg_off(const char *arg, int *idx, loff_t *off, loff_t *size,
  37. loff_t *maxsize, int devtype, uint64_t chipsize)
  38. {
  39. if (!str2off(arg, off))
  40. return get_part(arg, idx, off, size, maxsize, devtype);
  41. if (*off >= chipsize) {
  42. puts("Offset exceeds device limit\n");
  43. return -1;
  44. }
  45. *maxsize = chipsize - *off;
  46. *size = *maxsize;
  47. return 0;
  48. }
  49. int mtd_arg_off_size(int argc, char *const argv[], int *idx, loff_t *off,
  50. loff_t *size, loff_t *maxsize, int devtype,
  51. uint64_t chipsize)
  52. {
  53. int ret;
  54. if (argc == 0) {
  55. *off = 0;
  56. *size = chipsize;
  57. *maxsize = *size;
  58. goto print;
  59. }
  60. ret = mtd_arg_off(argv[0], idx, off, size, maxsize, devtype,
  61. chipsize);
  62. if (ret)
  63. return ret;
  64. if (argc == 1)
  65. goto print;
  66. if (!str2off(argv[1], size)) {
  67. printf("'%s' is not a number\n", argv[1]);
  68. return -1;
  69. }
  70. if (*size > *maxsize) {
  71. puts("Size exceeds partition or device limit\n");
  72. return -1;
  73. }
  74. print:
  75. printf("device %d ", *idx);
  76. if (*size == chipsize)
  77. puts("whole chip\n");
  78. else
  79. printf("offset 0x%llx, size 0x%llx\n",
  80. (unsigned long long)*off, (unsigned long long)*size);
  81. return 0;
  82. }