mtd_uboot.c 1.9 KB

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