misc.c 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155
  1. /*
  2. * Copyright (c) International Business Machines Corp., 2006
  3. *
  4. * This program is free software; you can redistribute it and/or modify
  5. * it under the terms of the GNU General Public License as published by
  6. * the Free Software Foundation; either version 2 of the License, or
  7. * (at your option) any later version.
  8. *
  9. * This program is distributed in the hope that it will be useful,
  10. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  11. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See
  12. * the GNU General Public License for more details.
  13. *
  14. * You should have received a copy of the GNU General Public License
  15. * along with this program; if not, write to the Free Software
  16. * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
  17. *
  18. * Author: Artem Bityutskiy (Битюцкий Артём)
  19. */
  20. /* Here we keep miscellaneous functions which are used all over the UBI code */
  21. #include "ubi.h"
  22. /**
  23. * calc_data_len - calculate how much real data is stored in a buffer.
  24. * @ubi: UBI device description object
  25. * @buf: a buffer with the contents of the physical eraseblock
  26. * @length: the buffer length
  27. *
  28. * This function calculates how much "real data" is stored in @buf and returnes
  29. * the length. Continuous 0xFF bytes at the end of the buffer are not
  30. * considered as "real data".
  31. */
  32. int ubi_calc_data_len(const struct ubi_device *ubi, const void *buf,
  33. int length)
  34. {
  35. int i;
  36. ubi_assert(!(length & (ubi->min_io_size - 1)));
  37. for (i = length - 1; i >= 0; i--)
  38. if (((const uint8_t *)buf)[i] != 0xFF)
  39. break;
  40. /* The resulting length must be aligned to the minimum flash I/O size */
  41. length = ALIGN(i + 1, ubi->min_io_size);
  42. return length;
  43. }
  44. /**
  45. * ubi_check_volume - check the contents of a static volume.
  46. * @ubi: UBI device description object
  47. * @vol_id: ID of the volume to check
  48. *
  49. * This function checks if static volume @vol_id is corrupted by fully reading
  50. * it and checking data CRC. This function returns %0 if the volume is not
  51. * corrupted, %1 if it is corrupted and a negative error code in case of
  52. * failure. Dynamic volumes are not checked and zero is returned immediately.
  53. */
  54. int ubi_check_volume(struct ubi_device *ubi, int vol_id)
  55. {
  56. void *buf;
  57. int err = 0, i;
  58. struct ubi_volume *vol = ubi->volumes[vol_id];
  59. if (vol->vol_type != UBI_STATIC_VOLUME)
  60. return 0;
  61. buf = vmalloc(vol->usable_leb_size);
  62. if (!buf)
  63. return -ENOMEM;
  64. for (i = 0; i < vol->used_ebs; i++) {
  65. int size;
  66. cond_resched();
  67. if (i == vol->used_ebs - 1)
  68. size = vol->last_eb_bytes;
  69. else
  70. size = vol->usable_leb_size;
  71. err = ubi_eba_read_leb(ubi, vol, i, buf, 0, size, 1);
  72. if (err) {
  73. if (mtd_is_eccerr(err))
  74. err = 1;
  75. break;
  76. }
  77. }
  78. vfree(buf);
  79. return err;
  80. }
  81. /**
  82. * ubi_update_reserved - update bad eraseblock handling accounting data.
  83. * @ubi: UBI device description object
  84. *
  85. * This function calculates the gap between current number of PEBs reserved for
  86. * bad eraseblock handling and the required level of PEBs that must be
  87. * reserved, and if necessary, reserves more PEBs to fill that gap, according
  88. * to availability. Should be called with ubi->volumes_lock held.
  89. */
  90. void ubi_update_reserved(struct ubi_device *ubi)
  91. {
  92. int need = ubi->beb_rsvd_level - ubi->beb_rsvd_pebs;
  93. if (need <= 0 || ubi->avail_pebs == 0)
  94. return;
  95. need = min_t(int, need, ubi->avail_pebs);
  96. ubi->avail_pebs -= need;
  97. ubi->rsvd_pebs += need;
  98. ubi->beb_rsvd_pebs += need;
  99. ubi_msg(ubi, "reserved more %d PEBs for bad PEB handling", need);
  100. }
  101. /**
  102. * ubi_calculate_reserved - calculate how many PEBs must be reserved for bad
  103. * eraseblock handling.
  104. * @ubi: UBI device description object
  105. */
  106. void ubi_calculate_reserved(struct ubi_device *ubi)
  107. {
  108. /*
  109. * Calculate the actual number of PEBs currently needed to be reserved
  110. * for future bad eraseblock handling.
  111. */
  112. ubi->beb_rsvd_level = ubi->bad_peb_limit - ubi->bad_peb_count;
  113. if (ubi->beb_rsvd_level < 0) {
  114. ubi->beb_rsvd_level = 0;
  115. 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)",
  116. ubi->bad_peb_count, ubi->bad_peb_limit);
  117. }
  118. }
  119. /**
  120. * ubi_check_pattern - check if buffer contains only a certain byte pattern.
  121. * @buf: buffer to check
  122. * @patt: the pattern to check
  123. * @size: buffer size in bytes
  124. *
  125. * This function returns %1 in there are only @patt bytes in @buf, and %0 if
  126. * something else was also found.
  127. */
  128. int ubi_check_pattern(const void *buf, uint8_t patt, int size)
  129. {
  130. int i;
  131. for (i = 0; i < size; i++)
  132. if (((const uint8_t *)buf)[i] != patt)
  133. return 0;
  134. return 1;
  135. }