1
2
3
4
5
6
7
8
9
10
11#include <ubi_uboot.h>
12#include "ubi.h"
13
14
15
16
17
18
19
20
21
22
23
24int ubi_calc_data_len(const struct ubi_device *ubi, const void *buf,
25 int length)
26{
27 int i;
28
29 ubi_assert(!(length & (ubi->min_io_size - 1)));
30
31 for (i = length - 1; i >= 0; i--)
32 if (((const uint8_t *)buf)[i] != 0xFF)
33 break;
34
35
36 length = ALIGN(i + 1, ubi->min_io_size);
37 return length;
38}
39
40
41
42
43
44
45
46
47
48
49
50int ubi_check_volume(struct ubi_device *ubi, int vol_id)
51{
52 void *buf;
53 int err = 0, i;
54 struct ubi_volume *vol = ubi->volumes[vol_id];
55
56 if (vol->vol_type != UBI_STATIC_VOLUME)
57 return 0;
58
59 buf = vmalloc(vol->usable_leb_size);
60 if (!buf)
61 return -ENOMEM;
62
63 for (i = 0; i < vol->used_ebs; i++) {
64 int size;
65
66 cond_resched();
67
68 if (i == vol->used_ebs - 1)
69 size = vol->last_eb_bytes;
70 else
71 size = vol->usable_leb_size;
72
73 err = ubi_eba_read_leb(ubi, vol, i, buf, 0, size, 1);
74 if (err) {
75 if (mtd_is_eccerr(err))
76 err = 1;
77 break;
78 }
79 }
80
81 vfree(buf);
82 return err;
83}
84
85
86
87
88
89
90
91
92
93
94void ubi_update_reserved(struct ubi_device *ubi)
95{
96 int need = ubi->beb_rsvd_level - ubi->beb_rsvd_pebs;
97
98 if (need <= 0 || ubi->avail_pebs == 0)
99 return;
100
101 need = min_t(int, need, ubi->avail_pebs);
102 ubi->avail_pebs -= need;
103 ubi->rsvd_pebs += need;
104 ubi->beb_rsvd_pebs += need;
105 ubi_msg(ubi, "reserved more %d PEBs for bad PEB handling", need);
106}
107
108
109
110
111
112
113void ubi_calculate_reserved(struct ubi_device *ubi)
114{
115
116
117
118
119 ubi->beb_rsvd_level = ubi->bad_peb_limit - ubi->bad_peb_count;
120 if (ubi->beb_rsvd_level < 0) {
121 ubi->beb_rsvd_level = 0;
122 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 ubi->bad_peb_count, ubi->bad_peb_limit);
124 }
125}
126
127
128
129
130
131
132
133
134
135
136int ubi_check_pattern(const void *buf, uint8_t patt, int size)
137{
138 int i;
139
140 for (i = 0; i < size; i++)
141 if (((const uint8_t *)buf)[i] != patt)
142 return 0;
143 return 1;
144}
145