misc.c 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127
  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_calculate_rsvd_pool - calculate how many PEBs must be reserved for bad
  83. * eraseblock handling.
  84. * @ubi: UBI device description object
  85. */
  86. void ubi_calculate_reserved(struct ubi_device *ubi)
  87. {
  88. ubi->beb_rsvd_level = ubi->good_peb_count/100;
  89. ubi->beb_rsvd_level *= CONFIG_MTD_UBI_BEB_RESERVE;
  90. if (ubi->beb_rsvd_level < MIN_RESEVED_PEBS)
  91. ubi->beb_rsvd_level = MIN_RESEVED_PEBS;
  92. }
  93. /**
  94. * ubi_check_pattern - check if buffer contains only a certain byte pattern.
  95. * @buf: buffer to check
  96. * @patt: the pattern to check
  97. * @size: buffer size in bytes
  98. *
  99. * This function returns %1 in there are only @patt bytes in @buf, and %0 if
  100. * something else was also found.
  101. */
  102. int ubi_check_pattern(const void *buf, uint8_t patt, int size)
  103. {
  104. int i;
  105. for (i = 0; i < size; i++)
  106. if (((const uint8_t *)buf)[i] != patt)
  107. return 0;
  108. return 1;
  109. }