misc.c 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106
  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_uboot.h>
  22. #include "ubi.h"
  23. /**
  24. * calc_data_len - calculate how much real data is stored in a buffer.
  25. * @ubi: UBI device description object
  26. * @buf: a buffer with the contents of the physical eraseblock
  27. * @length: the buffer length
  28. *
  29. * This function calculates how much "real data" is stored in @buf and returnes
  30. * the length. Continuous 0xFF bytes at the end of the buffer are not
  31. * considered as "real data".
  32. */
  33. int ubi_calc_data_len(const struct ubi_device *ubi, const void *buf,
  34. int length)
  35. {
  36. int i;
  37. ubi_assert(!(length & (ubi->min_io_size - 1)));
  38. for (i = length - 1; i >= 0; i--)
  39. if (((const uint8_t *)buf)[i] != 0xFF)
  40. break;
  41. /* The resulting length must be aligned to the minimum flash I/O size */
  42. length = ALIGN(i + 1, ubi->min_io_size);
  43. return length;
  44. }
  45. /**
  46. * ubi_check_volume - check the contents of a static volume.
  47. * @ubi: UBI device description object
  48. * @vol_id: ID of the volume to check
  49. *
  50. * This function checks if static volume @vol_id is corrupted by fully reading
  51. * it and checking data CRC. This function returns %0 if the volume is not
  52. * corrupted, %1 if it is corrupted and a negative error code in case of
  53. * failure. Dynamic volumes are not checked and zero is returned immediately.
  54. */
  55. int ubi_check_volume(struct ubi_device *ubi, int vol_id)
  56. {
  57. void *buf;
  58. int err = 0, i;
  59. struct ubi_volume *vol = ubi->volumes[vol_id];
  60. if (vol->vol_type != UBI_STATIC_VOLUME)
  61. return 0;
  62. buf = vmalloc(vol->usable_leb_size);
  63. if (!buf)
  64. return -ENOMEM;
  65. for (i = 0; i < vol->used_ebs; i++) {
  66. int size;
  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 (err == -EBADMSG)
  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. }