1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23#include "ubi.h"
24
25
26
27
28
29
30
31
32
33
34
35int ubi_calc_data_len(const struct ubi_device *ubi, const void *buf,
36 int length)
37{
38 int i;
39
40 ubi_assert(!(length & (ubi->min_io_size - 1)));
41
42 for (i = length - 1; i >= 0; i--)
43 if (((const uint8_t *)buf)[i] != 0xFF)
44 break;
45
46
47 length = ALIGN(i + 1, ubi->min_io_size);
48 return length;
49}
50
51
52
53
54
55
56
57
58
59
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
70 buf = vmalloc(vol->usable_leb_size);
71 if (!buf)
72 return -ENOMEM;
73
74 for (i = 0; i < vol->used_ebs; i++) {
75 int size;
76
77 cond_resched();
78
79 if (i == vol->used_ebs - 1)
80 size = vol->last_eb_bytes;
81 else
82 size = vol->usable_leb_size;
83
84 err = ubi_eba_read_leb(ubi, vol, i, buf, 0, size, 1);
85 if (err) {
86 if (mtd_is_eccerr(err))
87 err = 1;
88 break;
89 }
90 }
91
92 vfree(buf);
93 return err;
94}
95
96
97
98
99
100
101
102
103
104
105void ubi_update_reserved(struct ubi_device *ubi)
106{
107 int need = ubi->beb_rsvd_level - ubi->beb_rsvd_pebs;
108
109 if (need <= 0 || ubi->avail_pebs == 0)
110 return;
111
112 need = min_t(int, need, ubi->avail_pebs);
113 ubi->avail_pebs -= need;
114 ubi->rsvd_pebs += need;
115 ubi->beb_rsvd_pebs += need;
116 ubi_msg(ubi, "reserved more %d PEBs for bad PEB handling", need);
117}
118
119
120
121
122
123
124void ubi_calculate_reserved(struct ubi_device *ubi)
125{
126
127
128
129
130 ubi->beb_rsvd_level = ubi->bad_peb_limit - ubi->bad_peb_count;
131 if (ubi->beb_rsvd_level < 0) {
132 ubi->beb_rsvd_level = 0;
133 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)",
134 ubi->bad_peb_count, ubi->bad_peb_limit);
135 }
136}
137
138
139
140
141
142
143
144
145
146
147int ubi_check_pattern(const void *buf, uint8_t patt, int size)
148{
149 int i;
150
151 for (i = 0; i < size; i++)
152 if (((const uint8_t *)buf)[i] != patt)
153 return 0;
154 return 1;
155}
156
157
158void ubi_msg(const struct ubi_device *ubi, const char *fmt, ...)
159{
160 struct va_format vaf;
161 va_list args;
162
163 va_start(args, fmt);
164
165 vaf.fmt = fmt;
166 vaf.va = &args;
167
168 pr_notice(UBI_NAME_STR "%d: %pV\n", ubi->ubi_num, &vaf);
169
170 va_end(args);
171}
172
173
174void ubi_warn(const struct ubi_device *ubi, const char *fmt, ...)
175{
176 struct va_format vaf;
177 va_list args;
178
179 va_start(args, fmt);
180
181 vaf.fmt = fmt;
182 vaf.va = &args;
183
184 pr_warn(UBI_NAME_STR "%d warning: %ps: %pV\n",
185 ubi->ubi_num, __builtin_return_address(0), &vaf);
186
187 va_end(args);
188}
189
190
191void ubi_err(const struct ubi_device *ubi, const char *fmt, ...)
192{
193 struct va_format vaf;
194 va_list args;
195
196 va_start(args, fmt);
197
198 vaf.fmt = fmt;
199 vaf.va = &args;
200
201 pr_err(UBI_NAME_STR "%d error: %ps: %pV\n",
202 ubi->ubi_num, __builtin_return_address(0), &vaf);
203 va_end(args);
204}
205