misc.c 3.9 KB

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