misc.c 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153
  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. if (i == vol->used_ebs - 1)
  67. size = vol->last_eb_bytes;
  68. else
  69. size = vol->usable_leb_size;
  70. err = ubi_eba_read_leb(ubi, vol, i, buf, 0, size, 1);
  71. if (err) {
  72. if (mtd_is_eccerr(err))
  73. err = 1;
  74. break;
  75. }
  76. }
  77. vfree(buf);
  78. return err;
  79. }
  80. /**
  81. * ubi_update_reserved - update bad eraseblock handling accounting data.
  82. * @ubi: UBI device description object
  83. *
  84. * This function calculates the gap between current number of PEBs reserved for
  85. * bad eraseblock handling and the required level of PEBs that must be
  86. * reserved, and if necessary, reserves more PEBs to fill that gap, according
  87. * to availability. Should be called with ubi->volumes_lock held.
  88. */
  89. void ubi_update_reserved(struct ubi_device *ubi)
  90. {
  91. int need = ubi->beb_rsvd_level - ubi->beb_rsvd_pebs;
  92. if (need <= 0 || ubi->avail_pebs == 0)
  93. return;
  94. need = min_t(int, need, ubi->avail_pebs);
  95. ubi->avail_pebs -= need;
  96. ubi->rsvd_pebs += need;
  97. ubi->beb_rsvd_pebs += need;
  98. ubi_msg("reserved more %d PEBs for bad PEB handling", need);
  99. }
  100. /**
  101. * ubi_calculate_reserved - calculate how many PEBs must be reserved for bad
  102. * eraseblock handling.
  103. * @ubi: UBI device description object
  104. */
  105. void ubi_calculate_reserved(struct ubi_device *ubi)
  106. {
  107. /*
  108. * Calculate the actual number of PEBs currently needed to be reserved
  109. * for future bad eraseblock handling.
  110. */
  111. ubi->beb_rsvd_level = ubi->bad_peb_limit - ubi->bad_peb_count;
  112. if (ubi->beb_rsvd_level < 0) {
  113. ubi->beb_rsvd_level = 0;
  114. ubi_warn("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)",
  115. ubi->bad_peb_count, ubi->bad_peb_limit);
  116. }
  117. }
  118. /**
  119. * ubi_check_pattern - check if buffer contains only a certain byte pattern.
  120. * @buf: buffer to check
  121. * @patt: the pattern to check
  122. * @size: buffer size in bytes
  123. *
  124. * This function returns %1 in there are only @patt bytes in @buf, and %0 if
  125. * something else was also found.
  126. */
  127. int ubi_check_pattern(const void *buf, uint8_t patt, int size)
  128. {
  129. int i;
  130. for (i = 0; i < size; i++)
  131. if (((const uint8_t *)buf)[i] != patt)
  132. return 0;
  133. return 1;
  134. }