300-support-env-in-ubivol-chardev.patch 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163
  1. From 6e2630a0fc872d0db34157972f6dc3941f6d66dd Mon Sep 17 00:00:00 2001
  2. From: Daniel Golle <daniel@makrotopia.org>
  3. Date: Mon, 19 May 2014 21:38:01 +0200
  4. Subject: [PATCH] tools/env: add support for env in ubi volume chardev
  5. Signed-off-by: Daniel Golle <daniel@makrotopia.org>
  6. ---
  7. tools/env/Makefile | 5 ++++
  8. tools/env/fw_env.c | 76 +++++++++++++++++++++++++++++++++++++++++++++++-------
  9. 2 files changed, 71 insertions(+), 10 deletions(-)
  10. --- a/tools/env/Makefile
  11. +++ b/tools/env/Makefile
  12. @@ -24,6 +24,13 @@ ifeq ($(MTD_VERSION),old)
  13. HOST_EXTRACFLAGS += -DMTD_OLD
  14. endif
  15. +ifeq ($(UBI),y)
  16. +HOST_EXTRACFLAGS += -DUBI
  17. +HOST_LOADLIBES = "-Wl,--gc-sections,-lubi-utils"
  18. +else
  19. +HOST_LOADLIBES = "-Wl,--gc-sections"
  20. +endif
  21. +
  22. always := fw_printenv
  23. hostprogs-y := fw_printenv
  24. --- a/tools/env/fw_env.c
  25. +++ b/tools/env/fw_env.c
  26. @@ -31,6 +31,9 @@
  27. # include <mtd/mtd-user.h>
  28. #endif
  29. +#ifdef UBI
  30. +# include <libubi.h>
  31. +#endif
  32. #include "fw_env.h"
  33. #include <aes.h>
  34. @@ -811,6 +814,11 @@ static int flash_write_buf (int dev, int
  35. off_t top_of_range; /* end of the last block we may use */
  36. loff_t blockstart; /* running start of the current block -
  37. MEMGETBADBLOCK needs 64 bits */
  38. +#ifdef UBI
  39. + libubi_t *libubi = NULL;/* pointer to libubi struct */
  40. +#else
  41. + void *libubi = NULL;
  42. +#endif
  43. int rc;
  44. /*
  45. @@ -916,7 +924,30 @@ static int flash_write_buf (int dev, int
  46. continue;
  47. }
  48. - if (mtd_type != MTD_ABSENT) {
  49. +#ifdef UBI
  50. + if (mtd_type == MTD_UBIVOLUME) {
  51. + struct ubi_vol_info volinfo;
  52. + libubi = libubi_open();
  53. + if (libubi)
  54. + rc = ubi_get_vol_info(libubi,
  55. + DEVNAME(dev_current), &volinfo);
  56. + if (libubi && !rc) {
  57. + erasesize = volinfo.leb_size;
  58. + int leb = blockstart / erasesize;
  59. + if (volinfo.type != UBI_STATIC_VOLUME)
  60. + rc = ubi_leb_change_start(libubi, fd,
  61. + leb, erasesize);
  62. + else
  63. + rc = ubi_update_start(libubi, fd,
  64. + erasesize);
  65. + }
  66. + if (libubi && rc) {
  67. + libubi_close(libubi);
  68. + libubi = NULL;
  69. + }
  70. + }
  71. +#endif
  72. + if (!libubi && mtd_type != MTD_ABSENT) {
  73. erase.start = blockstart;
  74. ioctl(fd, MEMUNLOCK, &erase);
  75. /* These do not need an explicit erase cycle */
  76. @@ -933,7 +964,8 @@ static int flash_write_buf (int dev, int
  77. fprintf (stderr,
  78. "Seek error on %s: %s\n",
  79. DEVNAME (dev), strerror (errno));
  80. - return -1;
  81. + processed = -1;
  82. + goto out;
  83. }
  84. #ifdef DEBUG
  85. @@ -943,10 +975,11 @@ static int flash_write_buf (int dev, int
  86. if (write (fd, data + processed, erasesize) != erasesize) {
  87. fprintf (stderr, "Write error on %s: %s\n",
  88. DEVNAME (dev), strerror (errno));
  89. - return -1;
  90. + processed = -1;
  91. + goto out;
  92. }
  93. - if (mtd_type != MTD_ABSENT)
  94. + if (!libubi && mtd_type != MTD_ABSENT)
  95. ioctl(fd, MEMLOCK, &erase);
  96. processed += erasesize;
  97. @@ -957,6 +990,11 @@ static int flash_write_buf (int dev, int
  98. if (write_total > count)
  99. free (data);
  100. +out:
  101. +#ifdef UBI
  102. + if (libubi)
  103. + libubi_close(libubi);
  104. +#endif
  105. return processed;
  106. }
  107. @@ -1068,12 +1106,8 @@ static int flash_read (int fd)
  108. if (S_ISCHR(st.st_mode)) {
  109. rc = ioctl(fd, MEMGETINFO, &mtdinfo);
  110. - if (rc < 0) {
  111. - fprintf(stderr, "Cannot get MTD information for %s\n",
  112. - DEVNAME(dev_current));
  113. - return -1;
  114. - }
  115. - if (mtdinfo.type != MTD_NORFLASH &&
  116. + if (!rc &&
  117. + mtdinfo.type != MTD_NORFLASH &&
  118. mtdinfo.type != MTD_NANDFLASH &&
  119. mtdinfo.type != MTD_DATAFLASH &&
  120. mtdinfo.type != MTD_UBIVOLUME) {
  121. @@ -1081,6 +1115,28 @@ static int flash_read (int fd)
  122. mtdinfo.type, DEVNAME(dev_current));
  123. return -1;
  124. }
  125. +#ifdef UBI
  126. + if (rc) {
  127. + libubi_t *libubi;
  128. + struct ubi_vol_info volinfo;
  129. + libubi = libubi_open();
  130. + if (!libubi)
  131. + return -ENOMEM;
  132. +
  133. + rc = ubi_get_vol_info(libubi, DEVNAME(dev_current),
  134. + &volinfo);
  135. + if (rc) {
  136. + libubi_close(libubi);
  137. + return -ENODEV;
  138. + }
  139. + memset(&mtdinfo, 0, sizeof(mtdinfo));
  140. + mtdinfo.type = MTD_UBIVOLUME;
  141. + mtdinfo.size = volinfo.data_bytes;
  142. + mtdinfo.erasesize = volinfo.leb_size;
  143. + mtdinfo.writesize = volinfo.leb_size;
  144. + libubi_close(libubi);
  145. + }
  146. +#endif
  147. } else {
  148. memset(&mtdinfo, 0, sizeof(mtdinfo));
  149. mtdinfo.type = MTD_ABSENT;