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