misc.c 3.9 KB

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