mtd_uboot.c 1.9 KB

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