misc.c 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191
  1. // SPDX-License-Identifier: GPL-2.0-or-later
  2. /*
  3. * Copyright (c) International Business Machines Corp., 2006
  4. *
  5. * Author: Artem Bityutskiy (Битюцкий Артём)
  6. */
  7. /* Here we keep miscellaneous functions which are used all over the UBI code */
  8. #include "ubi.h"
  9. /**
  10. * calc_data_len - calculate how much real data is stored in a buffer.
  11. * @ubi: UBI device description object
  12. * @buf: a buffer with the contents of the physical eraseblock
  13. * @length: the buffer length
  14. *
  15. * This function calculates how much "real data" is stored in @buf and returnes
  16. * the length. Continuous 0xFF bytes at the end of the buffer are not
  17. * considered as "real data".
  18. */
  19. int ubi_calc_data_len(const struct ubi_device *ubi, const void *buf,
  20. int length)
  21. {
  22. int i;
  23. ubi_assert(!(length & (ubi->min_io_size - 1)));
  24. for (i = length - 1; i >= 0; i--)
  25. if (((const uint8_t *)buf)[i] != 0xFF)
  26. break;
  27. /* The resulting length must be aligned to the minimum flash I/O size */
  28. length = ALIGN(i + 1, ubi->min_io_size);
  29. return length;
  30. }
  31. /**
  32. * ubi_check_volume - check the contents of a static volume.
  33. * @ubi: UBI device description object
  34. * @vol_id: ID of the volume to check
  35. *
  36. * This function checks if static volume @vol_id is corrupted by fully reading
  37. * it and checking data CRC. This function returns %0 if the volume is not
  38. * corrupted, %1 if it is corrupted and a negative error code in case of
  39. * failure. Dynamic volumes are not checked and zero is returned immediately.
  40. */
  41. int ubi_check_volume(struct ubi_device *ubi, int vol_id)
  42. {
  43. void *buf;
  44. int err = 0, i;
  45. struct ubi_volume *vol = ubi->volumes[vol_id];
  46. if (vol->vol_type != UBI_STATIC_VOLUME)
  47. return 0;
  48. buf = vmalloc(vol->usable_leb_size);
  49. if (!buf)
  50. return -ENOMEM;
  51. for (i = 0; i < vol->used_ebs; i++) {
  52. int size;
  53. cond_resched();
  54. if (i == vol->used_ebs - 1)
  55. size = vol->last_eb_bytes;
  56. else
  57. size = vol->usable_leb_size;
  58. err = ubi_eba_read_leb(ubi, vol, i, buf, 0, size, 1);
  59. if (err) {
  60. if (mtd_is_eccerr(err))
  61. err = 1;
  62. break;
  63. }
  64. }
  65. vfree(buf);
  66. return err;
  67. }
  68. /**
  69. * ubi_update_reserved - update bad eraseblock handling accounting data.
  70. * @ubi: UBI device description object
  71. *
  72. * This function calculates the gap between current number of PEBs reserved for
  73. * bad eraseblock handling and the required level of PEBs that must be
  74. * reserved, and if necessary, reserves more PEBs to fill that gap, according
  75. * to availability. Should be called with ubi->volumes_lock held.
  76. */
  77. void ubi_update_reserved(struct ubi_device *ubi)
  78. {
  79. int need = ubi->beb_rsvd_level - ubi->beb_rsvd_pebs;
  80. if (need <= 0 || ubi->avail_pebs == 0)
  81. return;
  82. need = min_t(int, need, ubi->avail_pebs);
  83. ubi->avail_pebs -= need;
  84. ubi->rsvd_pebs += need;
  85. ubi->beb_rsvd_pebs += need;
  86. ubi_msg(ubi, "reserved more %d PEBs for bad PEB handling", need);
  87. }
  88. /**
  89. * ubi_calculate_reserved - calculate how many PEBs must be reserved for bad
  90. * eraseblock handling.
  91. * @ubi: UBI device description object
  92. */
  93. void ubi_calculate_reserved(struct ubi_device *ubi)
  94. {
  95. /*
  96. * Calculate the actual number of PEBs currently needed to be reserved
  97. * for future bad eraseblock handling.
  98. */
  99. ubi->beb_rsvd_level = ubi->bad_peb_limit - ubi->bad_peb_count;
  100. if (ubi->beb_rsvd_level < 0) {
  101. ubi->beb_rsvd_level = 0;
  102. ubi_warn(ubi, "number of bad PEBs (%d) is above the expected limit (%d), not reserving any PEBs for bad PEB handling, will use available PEBs (if any)",
  103. ubi->bad_peb_count, ubi->bad_peb_limit);
  104. }
  105. }
  106. /**
  107. * ubi_check_pattern - check if buffer contains only a certain byte pattern.
  108. * @buf: buffer to check
  109. * @patt: the pattern to check
  110. * @size: buffer size in bytes
  111. *
  112. * This function returns %1 in there are only @patt bytes in @buf, and %0 if
  113. * something else was also found.
  114. */
  115. int ubi_check_pattern(const void *buf, uint8_t patt, int size)
  116. {
  117. int i;
  118. for (i = 0; i < size; i++)
  119. if (((const uint8_t *)buf)[i] != patt)
  120. return 0;
  121. return 1;
  122. }
  123. /* Normal UBI messages */
  124. void ubi_msg(const struct ubi_device *ubi, const char *fmt, ...)
  125. {
  126. struct va_format vaf;
  127. va_list args;
  128. va_start(args, fmt);
  129. vaf.fmt = fmt;
  130. vaf.va = &args;
  131. pr_notice(UBI_NAME_STR "%d: %pV\n", ubi->ubi_num, &vaf);
  132. va_end(args);
  133. }
  134. /* UBI warning messages */
  135. void ubi_warn(const struct ubi_device *ubi, const char *fmt, ...)
  136. {
  137. struct va_format vaf;
  138. va_list args;
  139. va_start(args, fmt);
  140. vaf.fmt = fmt;
  141. vaf.va = &args;
  142. pr_warn(UBI_NAME_STR "%d warning: %ps: %pV\n",
  143. ubi->ubi_num, __builtin_return_address(0), &vaf);
  144. va_end(args);
  145. }
  146. /* UBI error messages */
  147. void ubi_err(const struct ubi_device *ubi, const char *fmt, ...)
  148. {
  149. struct va_format vaf;
  150. va_list args;
  151. va_start(args, fmt);
  152. vaf.fmt = fmt;
  153. vaf.va = &args;
  154. pr_err(UBI_NAME_STR "%d error: %ps: %pV\n",
  155. ubi->ubi_num, __builtin_return_address(0), &vaf);
  156. va_end(args);
  157. }