blob: f913d701a5b3d96222ca9eeb71007c6ee982e209 [file] [log] [blame]
Artem B. Bityutskiy801c1352006-06-27 12:22:22 +04001/*
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.h"
24
25/**
26 * calc_data_len - calculate how much real data is stored in a buffer.
27 * @ubi: UBI device description object
28 * @buf: a buffer with the contents of the physical eraseblock
29 * @length: the buffer length
30 *
31 * This function calculates how much "real data" is stored in @buf and returnes
32 * the length. Continuous 0xFF bytes at the end of the buffer are not
33 * considered as "real data".
34 */
35int ubi_calc_data_len(const struct ubi_device *ubi, const void *buf,
36 int length)
37{
38 int i;
39
Kyungmin Parkcadb40c2008-05-22 10:32:18 +090040 ubi_assert(!(length & (ubi->min_io_size - 1)));
Artem B. Bityutskiy801c1352006-06-27 12:22:22 +040041
42 for (i = length - 1; i >= 0; i--)
43 if (((const uint8_t *)buf)[i] != 0xFF)
44 break;
45
46 /* The resulting length must be aligned to the minimum flash I/O size */
47 length = ALIGN(i + 1, ubi->min_io_size);
48 return length;
49}
50
51/**
52 * ubi_check_volume - check the contents of a static volume.
53 * @ubi: UBI device description object
54 * @vol_id: ID of the volume to check
55 *
56 * This function checks if static volume @vol_id is corrupted by fully reading
57 * it and checking data CRC. This function returns %0 if the volume is not
58 * corrupted, %1 if it is corrupted and a negative error code in case of
59 * failure. Dynamic volumes are not checked and zero is returned immediately.
60 */
61int ubi_check_volume(struct ubi_device *ubi, int vol_id)
62{
63 void *buf;
64 int err = 0, i;
65 struct ubi_volume *vol = ubi->volumes[vol_id];
66
67 if (vol->vol_type != UBI_STATIC_VOLUME)
68 return 0;
69
Artem Bityutskiy92ad8f32007-05-06 16:12:54 +030070 buf = vmalloc(vol->usable_leb_size);
Artem B. Bityutskiy801c1352006-06-27 12:22:22 +040071 if (!buf)
72 return -ENOMEM;
73
74 for (i = 0; i < vol->used_ebs; i++) {
75 int size;
76
77 if (i == vol->used_ebs - 1)
78 size = vol->last_eb_bytes;
79 else
80 size = vol->usable_leb_size;
81
Artem Bityutskiy89b96b62007-12-16 20:00:38 +020082 err = ubi_eba_read_leb(ubi, vol, i, buf, 0, size, 1);
Artem B. Bityutskiy801c1352006-06-27 12:22:22 +040083 if (err) {
Brian Norrisd57f40542011-09-20 18:34:25 -070084 if (mtd_is_eccerr(err))
Artem B. Bityutskiy801c1352006-06-27 12:22:22 +040085 err = 1;
86 break;
87 }
88 }
89
Artem Bityutskiy92ad8f32007-05-06 16:12:54 +030090 vfree(buf);
Artem B. Bityutskiy801c1352006-06-27 12:22:22 +040091 return err;
92}
93
94/**
Shmulik Ladkani87e773c2012-07-04 11:06:04 +030095 * ubi_update_reserved - update bad eraseblock handling accounting data.
96 * @ubi: UBI device description object
97 *
98 * This function calculates the gap between current number of PEBs reserved for
99 * bad eraseblock handling and the required level of PEBs that must be
100 * reserved, and if necessary, reserves more PEBs to fill that gap, according
101 * to availability. Should be called with ubi->volumes_lock held.
102 */
103void ubi_update_reserved(struct ubi_device *ubi)
104{
105 int need = ubi->beb_rsvd_level - ubi->beb_rsvd_pebs;
106
107 if (need <= 0 || ubi->avail_pebs == 0)
108 return;
109
110 need = min_t(int, need, ubi->avail_pebs);
111 ubi->avail_pebs -= need;
112 ubi->rsvd_pebs += need;
113 ubi->beb_rsvd_pebs += need;
114 ubi_msg("reserved more %d PEBs for bad PEB handling", need);
115}
116
117/**
Shmulik Ladkani5c669a52012-07-04 11:06:03 +0300118 * ubi_calculate_reserved - calculate how many PEBs must be reserved for bad
Artem B. Bityutskiy801c1352006-06-27 12:22:22 +0400119 * eraseblock handling.
120 * @ubi: UBI device description object
121 */
122void ubi_calculate_reserved(struct ubi_device *ubi)
123{
Shmulik Ladkani37f758a2012-07-04 11:06:01 +0300124 /*
125 * Calculate the actual number of PEBs currently needed to be reserved
126 * for future bad eraseblock handling.
127 */
128 ubi->beb_rsvd_level = ubi->bad_peb_limit - ubi->bad_peb_count;
129 if (ubi->beb_rsvd_level < 0) {
130 ubi->beb_rsvd_level = 0;
Artem Bityutskiy049333c2012-08-27 14:43:54 +0300131 ubi_warn("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)",
Shmulik Ladkani37f758a2012-07-04 11:06:01 +0300132 ubi->bad_peb_count, ubi->bad_peb_limit);
133 }
Artem B. Bityutskiy801c1352006-06-27 12:22:22 +0400134}
Artem Bityutskiybb00e182010-07-31 09:37:34 +0300135
136/**
137 * ubi_check_pattern - check if buffer contains only a certain byte pattern.
138 * @buf: buffer to check
139 * @patt: the pattern to check
140 * @size: buffer size in bytes
141 *
142 * This function returns %1 in there are only @patt bytes in @buf, and %0 if
143 * something else was also found.
144 */
145int ubi_check_pattern(const void *buf, uint8_t patt, int size)
146{
147 int i;
148
149 for (i = 0; i < size; i++)
150 if (((const uint8_t *)buf)[i] != patt)
151 return 0;
152 return 1;
153}