xref: /kernel/linux/linux-5.10/drivers/mtd/ubi/misc.c (revision 8c2ecf20)
18c2ecf20Sopenharmony_ci// SPDX-License-Identifier: GPL-2.0-or-later
28c2ecf20Sopenharmony_ci/*
38c2ecf20Sopenharmony_ci * Copyright (c) International Business Machines Corp., 2006
48c2ecf20Sopenharmony_ci *
58c2ecf20Sopenharmony_ci * Author: Artem Bityutskiy (Битюцкий Артём)
68c2ecf20Sopenharmony_ci */
78c2ecf20Sopenharmony_ci
88c2ecf20Sopenharmony_ci/* Here we keep miscellaneous functions which are used all over the UBI code */
98c2ecf20Sopenharmony_ci
108c2ecf20Sopenharmony_ci#include "ubi.h"
118c2ecf20Sopenharmony_ci
128c2ecf20Sopenharmony_ci/**
138c2ecf20Sopenharmony_ci * calc_data_len - calculate how much real data is stored in a buffer.
148c2ecf20Sopenharmony_ci * @ubi: UBI device description object
158c2ecf20Sopenharmony_ci * @buf: a buffer with the contents of the physical eraseblock
168c2ecf20Sopenharmony_ci * @length: the buffer length
178c2ecf20Sopenharmony_ci *
188c2ecf20Sopenharmony_ci * This function calculates how much "real data" is stored in @buf and returnes
198c2ecf20Sopenharmony_ci * the length. Continuous 0xFF bytes at the end of the buffer are not
208c2ecf20Sopenharmony_ci * considered as "real data".
218c2ecf20Sopenharmony_ci */
228c2ecf20Sopenharmony_ciint ubi_calc_data_len(const struct ubi_device *ubi, const void *buf,
238c2ecf20Sopenharmony_ci		      int length)
248c2ecf20Sopenharmony_ci{
258c2ecf20Sopenharmony_ci	int i;
268c2ecf20Sopenharmony_ci
278c2ecf20Sopenharmony_ci	ubi_assert(!(length & (ubi->min_io_size - 1)));
288c2ecf20Sopenharmony_ci
298c2ecf20Sopenharmony_ci	for (i = length - 1; i >= 0; i--)
308c2ecf20Sopenharmony_ci		if (((const uint8_t *)buf)[i] != 0xFF)
318c2ecf20Sopenharmony_ci			break;
328c2ecf20Sopenharmony_ci
338c2ecf20Sopenharmony_ci	/* The resulting length must be aligned to the minimum flash I/O size */
348c2ecf20Sopenharmony_ci	length = ALIGN(i + 1, ubi->min_io_size);
358c2ecf20Sopenharmony_ci	return length;
368c2ecf20Sopenharmony_ci}
378c2ecf20Sopenharmony_ci
388c2ecf20Sopenharmony_ci/**
398c2ecf20Sopenharmony_ci * ubi_check_volume - check the contents of a static volume.
408c2ecf20Sopenharmony_ci * @ubi: UBI device description object
418c2ecf20Sopenharmony_ci * @vol_id: ID of the volume to check
428c2ecf20Sopenharmony_ci *
438c2ecf20Sopenharmony_ci * This function checks if static volume @vol_id is corrupted by fully reading
448c2ecf20Sopenharmony_ci * it and checking data CRC. This function returns %0 if the volume is not
458c2ecf20Sopenharmony_ci * corrupted, %1 if it is corrupted and a negative error code in case of
468c2ecf20Sopenharmony_ci * failure. Dynamic volumes are not checked and zero is returned immediately.
478c2ecf20Sopenharmony_ci */
488c2ecf20Sopenharmony_ciint ubi_check_volume(struct ubi_device *ubi, int vol_id)
498c2ecf20Sopenharmony_ci{
508c2ecf20Sopenharmony_ci	void *buf;
518c2ecf20Sopenharmony_ci	int err = 0, i;
528c2ecf20Sopenharmony_ci	struct ubi_volume *vol = ubi->volumes[vol_id];
538c2ecf20Sopenharmony_ci
548c2ecf20Sopenharmony_ci	if (vol->vol_type != UBI_STATIC_VOLUME)
558c2ecf20Sopenharmony_ci		return 0;
568c2ecf20Sopenharmony_ci
578c2ecf20Sopenharmony_ci	buf = vmalloc(vol->usable_leb_size);
588c2ecf20Sopenharmony_ci	if (!buf)
598c2ecf20Sopenharmony_ci		return -ENOMEM;
608c2ecf20Sopenharmony_ci
618c2ecf20Sopenharmony_ci	for (i = 0; i < vol->used_ebs; i++) {
628c2ecf20Sopenharmony_ci		int size;
638c2ecf20Sopenharmony_ci
648c2ecf20Sopenharmony_ci		cond_resched();
658c2ecf20Sopenharmony_ci
668c2ecf20Sopenharmony_ci		if (i == vol->used_ebs - 1)
678c2ecf20Sopenharmony_ci			size = vol->last_eb_bytes;
688c2ecf20Sopenharmony_ci		else
698c2ecf20Sopenharmony_ci			size = vol->usable_leb_size;
708c2ecf20Sopenharmony_ci
718c2ecf20Sopenharmony_ci		err = ubi_eba_read_leb(ubi, vol, i, buf, 0, size, 1);
728c2ecf20Sopenharmony_ci		if (err) {
738c2ecf20Sopenharmony_ci			if (mtd_is_eccerr(err))
748c2ecf20Sopenharmony_ci				err = 1;
758c2ecf20Sopenharmony_ci			break;
768c2ecf20Sopenharmony_ci		}
778c2ecf20Sopenharmony_ci	}
788c2ecf20Sopenharmony_ci
798c2ecf20Sopenharmony_ci	vfree(buf);
808c2ecf20Sopenharmony_ci	return err;
818c2ecf20Sopenharmony_ci}
828c2ecf20Sopenharmony_ci
838c2ecf20Sopenharmony_ci/**
848c2ecf20Sopenharmony_ci * ubi_update_reserved - update bad eraseblock handling accounting data.
858c2ecf20Sopenharmony_ci * @ubi: UBI device description object
868c2ecf20Sopenharmony_ci *
878c2ecf20Sopenharmony_ci * This function calculates the gap between current number of PEBs reserved for
888c2ecf20Sopenharmony_ci * bad eraseblock handling and the required level of PEBs that must be
898c2ecf20Sopenharmony_ci * reserved, and if necessary, reserves more PEBs to fill that gap, according
908c2ecf20Sopenharmony_ci * to availability. Should be called with ubi->volumes_lock held.
918c2ecf20Sopenharmony_ci */
928c2ecf20Sopenharmony_civoid ubi_update_reserved(struct ubi_device *ubi)
938c2ecf20Sopenharmony_ci{
948c2ecf20Sopenharmony_ci	int need = ubi->beb_rsvd_level - ubi->beb_rsvd_pebs;
958c2ecf20Sopenharmony_ci
968c2ecf20Sopenharmony_ci	if (need <= 0 || ubi->avail_pebs == 0)
978c2ecf20Sopenharmony_ci		return;
988c2ecf20Sopenharmony_ci
998c2ecf20Sopenharmony_ci	need = min_t(int, need, ubi->avail_pebs);
1008c2ecf20Sopenharmony_ci	ubi->avail_pebs -= need;
1018c2ecf20Sopenharmony_ci	ubi->rsvd_pebs += need;
1028c2ecf20Sopenharmony_ci	ubi->beb_rsvd_pebs += need;
1038c2ecf20Sopenharmony_ci	ubi_msg(ubi, "reserved more %d PEBs for bad PEB handling", need);
1048c2ecf20Sopenharmony_ci}
1058c2ecf20Sopenharmony_ci
1068c2ecf20Sopenharmony_ci/**
1078c2ecf20Sopenharmony_ci * ubi_calculate_reserved - calculate how many PEBs must be reserved for bad
1088c2ecf20Sopenharmony_ci * eraseblock handling.
1098c2ecf20Sopenharmony_ci * @ubi: UBI device description object
1108c2ecf20Sopenharmony_ci */
1118c2ecf20Sopenharmony_civoid ubi_calculate_reserved(struct ubi_device *ubi)
1128c2ecf20Sopenharmony_ci{
1138c2ecf20Sopenharmony_ci	/*
1148c2ecf20Sopenharmony_ci	 * Calculate the actual number of PEBs currently needed to be reserved
1158c2ecf20Sopenharmony_ci	 * for future bad eraseblock handling.
1168c2ecf20Sopenharmony_ci	 */
1178c2ecf20Sopenharmony_ci	ubi->beb_rsvd_level = ubi->bad_peb_limit - ubi->bad_peb_count;
1188c2ecf20Sopenharmony_ci	if (ubi->beb_rsvd_level < 0) {
1198c2ecf20Sopenharmony_ci		ubi->beb_rsvd_level = 0;
1208c2ecf20Sopenharmony_ci		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)",
1218c2ecf20Sopenharmony_ci			 ubi->bad_peb_count, ubi->bad_peb_limit);
1228c2ecf20Sopenharmony_ci	}
1238c2ecf20Sopenharmony_ci}
1248c2ecf20Sopenharmony_ci
1258c2ecf20Sopenharmony_ci/**
1268c2ecf20Sopenharmony_ci * ubi_check_pattern - check if buffer contains only a certain byte pattern.
1278c2ecf20Sopenharmony_ci * @buf: buffer to check
1288c2ecf20Sopenharmony_ci * @patt: the pattern to check
1298c2ecf20Sopenharmony_ci * @size: buffer size in bytes
1308c2ecf20Sopenharmony_ci *
1318c2ecf20Sopenharmony_ci * This function returns %1 in there are only @patt bytes in @buf, and %0 if
1328c2ecf20Sopenharmony_ci * something else was also found.
1338c2ecf20Sopenharmony_ci */
1348c2ecf20Sopenharmony_ciint ubi_check_pattern(const void *buf, uint8_t patt, int size)
1358c2ecf20Sopenharmony_ci{
1368c2ecf20Sopenharmony_ci	int i;
1378c2ecf20Sopenharmony_ci
1388c2ecf20Sopenharmony_ci	for (i = 0; i < size; i++)
1398c2ecf20Sopenharmony_ci		if (((const uint8_t *)buf)[i] != patt)
1408c2ecf20Sopenharmony_ci			return 0;
1418c2ecf20Sopenharmony_ci	return 1;
1428c2ecf20Sopenharmony_ci}
1438c2ecf20Sopenharmony_ci
1448c2ecf20Sopenharmony_ci/* Normal UBI messages */
1458c2ecf20Sopenharmony_civoid ubi_msg(const struct ubi_device *ubi, const char *fmt, ...)
1468c2ecf20Sopenharmony_ci{
1478c2ecf20Sopenharmony_ci	struct va_format vaf;
1488c2ecf20Sopenharmony_ci	va_list args;
1498c2ecf20Sopenharmony_ci
1508c2ecf20Sopenharmony_ci	va_start(args, fmt);
1518c2ecf20Sopenharmony_ci
1528c2ecf20Sopenharmony_ci	vaf.fmt = fmt;
1538c2ecf20Sopenharmony_ci	vaf.va = &args;
1548c2ecf20Sopenharmony_ci
1558c2ecf20Sopenharmony_ci	pr_notice(UBI_NAME_STR "%d: %pV\n", ubi->ubi_num, &vaf);
1568c2ecf20Sopenharmony_ci
1578c2ecf20Sopenharmony_ci	va_end(args);
1588c2ecf20Sopenharmony_ci}
1598c2ecf20Sopenharmony_ci
1608c2ecf20Sopenharmony_ci/* UBI warning messages */
1618c2ecf20Sopenharmony_civoid ubi_warn(const struct ubi_device *ubi, const char *fmt, ...)
1628c2ecf20Sopenharmony_ci{
1638c2ecf20Sopenharmony_ci	struct va_format vaf;
1648c2ecf20Sopenharmony_ci	va_list args;
1658c2ecf20Sopenharmony_ci
1668c2ecf20Sopenharmony_ci	va_start(args, fmt);
1678c2ecf20Sopenharmony_ci
1688c2ecf20Sopenharmony_ci	vaf.fmt = fmt;
1698c2ecf20Sopenharmony_ci	vaf.va = &args;
1708c2ecf20Sopenharmony_ci
1718c2ecf20Sopenharmony_ci	pr_warn(UBI_NAME_STR "%d warning: %ps: %pV\n",
1728c2ecf20Sopenharmony_ci		ubi->ubi_num, __builtin_return_address(0), &vaf);
1738c2ecf20Sopenharmony_ci
1748c2ecf20Sopenharmony_ci	va_end(args);
1758c2ecf20Sopenharmony_ci}
1768c2ecf20Sopenharmony_ci
1778c2ecf20Sopenharmony_ci/* UBI error messages */
1788c2ecf20Sopenharmony_civoid ubi_err(const struct ubi_device *ubi, const char *fmt, ...)
1798c2ecf20Sopenharmony_ci{
1808c2ecf20Sopenharmony_ci	struct va_format vaf;
1818c2ecf20Sopenharmony_ci	va_list args;
1828c2ecf20Sopenharmony_ci
1838c2ecf20Sopenharmony_ci	va_start(args, fmt);
1848c2ecf20Sopenharmony_ci
1858c2ecf20Sopenharmony_ci	vaf.fmt = fmt;
1868c2ecf20Sopenharmony_ci	vaf.va = &args;
1878c2ecf20Sopenharmony_ci
1888c2ecf20Sopenharmony_ci	pr_err(UBI_NAME_STR "%d error: %ps: %pV\n",
1898c2ecf20Sopenharmony_ci	       ubi->ubi_num, __builtin_return_address(0), &vaf);
1908c2ecf20Sopenharmony_ci	va_end(args);
1918c2ecf20Sopenharmony_ci}
192