^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1) // SPDX-License-Identifier: GPL-2.0-or-later
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3) * Copyright (c) International Business Machines Corp., 2006
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5) * Author: Artem Bityutskiy (Битюцкий Артём)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 6) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 7)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 8) /* Here we keep miscellaneous functions which are used all over the UBI code */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 9)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 10) #include "ubi.h"
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 11)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 12) /**
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 13) * calc_data_len - calculate how much real data is stored in a buffer.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 14) * @ubi: UBI device description object
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 15) * @buf: a buffer with the contents of the physical eraseblock
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 16) * @length: the buffer length
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 17) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 18) * This function calculates how much "real data" is stored in @buf and returnes
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 19) * the length. Continuous 0xFF bytes at the end of the buffer are not
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 20) * considered as "real data".
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 21) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 22) int ubi_calc_data_len(const struct ubi_device *ubi, const void *buf,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 23) int length)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 24) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 25) int i;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 26)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 27) ubi_assert(!(length & (ubi->min_io_size - 1)));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 28)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 29) for (i = length - 1; i >= 0; i--)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 30) if (((const uint8_t *)buf)[i] != 0xFF)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 31) break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 32)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 33) /* The resulting length must be aligned to the minimum flash I/O size */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 34) length = ALIGN(i + 1, ubi->min_io_size);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 35) return length;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 36) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 37)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 38) /**
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 39) * ubi_check_volume - check the contents of a static volume.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 40) * @ubi: UBI device description object
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 41) * @vol_id: ID of the volume to check
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 42) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 43) * This function checks if static volume @vol_id is corrupted by fully reading
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 44) * it and checking data CRC. This function returns %0 if the volume is not
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 45) * corrupted, %1 if it is corrupted and a negative error code in case of
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 46) * failure. Dynamic volumes are not checked and zero is returned immediately.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 47) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 48) int ubi_check_volume(struct ubi_device *ubi, int vol_id)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 49) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 50) void *buf;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 51) int err = 0, i;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 52) struct ubi_volume *vol = ubi->volumes[vol_id];
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 53)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 54) if (vol->vol_type != UBI_STATIC_VOLUME)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 55) return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 56)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 57) buf = vmalloc(vol->usable_leb_size);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 58) if (!buf)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 59) return -ENOMEM;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 60)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 61) for (i = 0; i < vol->used_ebs; i++) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 62) int size;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 63)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 64) cond_resched();
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 65)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 66) if (i == vol->used_ebs - 1)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 67) size = vol->last_eb_bytes;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 68) else
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 69) size = vol->usable_leb_size;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 70)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 71) err = ubi_eba_read_leb(ubi, vol, i, buf, 0, size, 1);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 72) if (err) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 73) if (mtd_is_eccerr(err))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 74) err = 1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 75) break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 76) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 77) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 78)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 79) vfree(buf);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 80) return err;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 81) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 82)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 83) /**
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 84) * ubi_update_reserved - update bad eraseblock handling accounting data.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 85) * @ubi: UBI device description object
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 86) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 87) * This function calculates the gap between current number of PEBs reserved for
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 88) * bad eraseblock handling and the required level of PEBs that must be
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 89) * reserved, and if necessary, reserves more PEBs to fill that gap, according
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 90) * to availability. Should be called with ubi->volumes_lock held.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 91) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 92) void ubi_update_reserved(struct ubi_device *ubi)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 93) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 94) int need = ubi->beb_rsvd_level - ubi->beb_rsvd_pebs;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 95)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 96) if (need <= 0 || ubi->avail_pebs == 0)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 97) return;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 98)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 99) need = min_t(int, need, ubi->avail_pebs);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 100) ubi->avail_pebs -= need;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 101) ubi->rsvd_pebs += need;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 102) ubi->beb_rsvd_pebs += need;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 103) ubi_msg(ubi, "reserved more %d PEBs for bad PEB handling", need);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 104) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 105)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 106) /**
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 107) * ubi_calculate_reserved - calculate how many PEBs must be reserved for bad
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 108) * eraseblock handling.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 109) * @ubi: UBI device description object
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 110) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 111) void ubi_calculate_reserved(struct ubi_device *ubi)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 112) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 113) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 114) * Calculate the actual number of PEBs currently needed to be reserved
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 115) * for future bad eraseblock handling.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 116) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 117) ubi->beb_rsvd_level = ubi->bad_peb_limit - ubi->bad_peb_count;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 118) if (ubi->beb_rsvd_level < 0) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 119) ubi->beb_rsvd_level = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 120) 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)",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 121) ubi->bad_peb_count, ubi->bad_peb_limit);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 122) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 123) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 124)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 125) /**
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 126) * ubi_check_pattern - check if buffer contains only a certain byte pattern.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 127) * @buf: buffer to check
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 128) * @patt: the pattern to check
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 129) * @size: buffer size in bytes
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 130) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 131) * This function returns %1 in there are only @patt bytes in @buf, and %0 if
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 132) * something else was also found.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 133) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 134) int ubi_check_pattern(const void *buf, uint8_t patt, int size)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 135) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 136) int i;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 137)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 138) for (i = 0; i < size; i++)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 139) if (((const uint8_t *)buf)[i] != patt)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 140) return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 141) return 1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 142) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 143)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 144) /* Normal UBI messages */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 145) void ubi_msg(const struct ubi_device *ubi, const char *fmt, ...)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 146) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 147) struct va_format vaf;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 148) va_list args;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 149)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 150) va_start(args, fmt);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 151)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 152) vaf.fmt = fmt;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 153) vaf.va = &args;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 154)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 155) pr_notice(UBI_NAME_STR "%d: %pV\n", ubi->ubi_num, &vaf);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 156)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 157) va_end(args);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 158) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 159)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 160) /* UBI warning messages */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 161) void ubi_warn(const struct ubi_device *ubi, const char *fmt, ...)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 162) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 163) struct va_format vaf;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 164) va_list args;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 165)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 166) va_start(args, fmt);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 167)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 168) vaf.fmt = fmt;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 169) vaf.va = &args;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 170)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 171) pr_warn(UBI_NAME_STR "%d warning: %ps: %pV\n",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 172) ubi->ubi_num, __builtin_return_address(0), &vaf);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 173)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 174) va_end(args);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 175) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 176)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 177) /* UBI error messages */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 178) void ubi_err(const struct ubi_device *ubi, const char *fmt, ...)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 179) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 180) struct va_format vaf;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 181) va_list args;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 182)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 183) va_start(args, fmt);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 184)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 185) vaf.fmt = fmt;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 186) vaf.va = &args;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 187)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 188) pr_err(UBI_NAME_STR "%d error: %ps: %pV\n",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 189) ubi->ubi_num, __builtin_return_address(0), &vaf);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 190) va_end(args);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 191) }