misc.c 3.9 KB

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