xref: /openbmc/u-boot/drivers/mtd/ubi/misc.c (revision 9b914727)
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 
21 /* Here we keep miscellaneous functions which are used all over the UBI code */
22 
23 #include <ubi_uboot.h>
24 #include "ubi.h"
25 
26 /**
27  * calc_data_len - calculate how much real data is stored in a buffer.
28  * @ubi: UBI device description object
29  * @buf: a buffer with the contents of the physical eraseblock
30  * @length: the buffer length
31  *
32  * This function calculates how much "real data" is stored in @buf and returnes
33  * the length. Continuous 0xFF bytes at the end of the buffer are not
34  * considered as "real data".
35  */
36 int ubi_calc_data_len(const struct ubi_device *ubi, const void *buf,
37 		      int length)
38 {
39 	int i;
40 
41 	ubi_assert(!(length & (ubi->min_io_size - 1)));
42 
43 	for (i = length - 1; i >= 0; i--)
44 		if (((const uint8_t *)buf)[i] != 0xFF)
45 			break;
46 
47 	/* The resulting length must be aligned to the minimum flash I/O size */
48 	length = ALIGN(i + 1, ubi->min_io_size);
49 	return length;
50 }
51 
52 /**
53  * ubi_check_volume - check the contents of a static volume.
54  * @ubi: UBI device description object
55  * @vol_id: ID of the volume to check
56  *
57  * This function checks if static volume @vol_id is corrupted by fully reading
58  * it and checking data CRC. This function returns %0 if the volume is not
59  * corrupted, %1 if it is corrupted and a negative error code in case of
60  * failure. Dynamic volumes are not checked and zero is returned immediately.
61  */
62 int ubi_check_volume(struct ubi_device *ubi, int vol_id)
63 {
64 	void *buf;
65 	int err = 0, i;
66 	struct ubi_volume *vol = ubi->volumes[vol_id];
67 
68 	if (vol->vol_type != UBI_STATIC_VOLUME)
69 		return 0;
70 
71 	buf = vmalloc(vol->usable_leb_size);
72 	if (!buf)
73 		return -ENOMEM;
74 
75 	for (i = 0; i < vol->used_ebs; i++) {
76 		int size;
77 
78 		if (i == vol->used_ebs - 1)
79 			size = vol->last_eb_bytes;
80 		else
81 			size = vol->usable_leb_size;
82 
83 		err = ubi_eba_read_leb(ubi, vol, i, buf, 0, size, 1);
84 		if (err) {
85 			if (err == -EBADMSG)
86 				err = 1;
87 			break;
88 		}
89 	}
90 
91 	vfree(buf);
92 	return err;
93 }
94 
95 /**
96  * ubi_calculate_rsvd_pool - calculate how many PEBs must be reserved for bad
97  * eraseblock handling.
98  * @ubi: UBI device description object
99  */
100 void ubi_calculate_reserved(struct ubi_device *ubi)
101 {
102 	ubi->beb_rsvd_level = ubi->good_peb_count/100;
103 	ubi->beb_rsvd_level *= CONFIG_MTD_UBI_BEB_RESERVE;
104 	if (ubi->beb_rsvd_level < MIN_RESEVED_PEBS)
105 		ubi->beb_rsvd_level = MIN_RESEVED_PEBS;
106 }
107