blob: 123c2f344de152597f107d22f38e22fc33f4a4df [file] [log] [blame]
Tom Rini10e47792018-05-06 17:58:06 -04001// SPDX-License-Identifier: GPL-2.0+
Kyungmin Park7f88f002008-11-19 16:28:06 +01002/*
3 * Copyright (c) International Business Machines Corp., 2006
4 * Copyright (c) Nokia Corporation, 2006, 2007
5 *
Kyungmin Park7f88f002008-11-19 16:28:06 +01006 * Author: Artem Bityutskiy (Битюцкий Артём)
7 */
8
9/*
10 * This file includes volume table manipulation code. The volume table is an
11 * on-flash table containing volume meta-data like name, number of reserved
12 * physical eraseblocks, type, etc. The volume table is stored in the so-called
13 * "layout volume".
14 *
15 * The layout volume is an internal volume which is organized as follows. It
16 * consists of two logical eraseblocks - LEB 0 and LEB 1. Each logical
17 * eraseblock stores one volume table copy, i.e. LEB 0 and LEB 1 duplicate each
18 * other. This redundancy guarantees robustness to unclean reboots. The volume
19 * table is basically an array of volume table records. Each record contains
Heiko Schocher94b66de2015-10-22 06:19:21 +020020 * full information about the volume and protected by a CRC checksum. Note,
21 * nowadays we use the atomic LEB change operation when updating the volume
22 * table, so we do not really need 2 LEBs anymore, but we preserve the older
23 * design for the backward compatibility reasons.
Kyungmin Park7f88f002008-11-19 16:28:06 +010024 *
Heiko Schocher94b66de2015-10-22 06:19:21 +020025 * When the volume table is changed, it is first changed in RAM. Then LEB 0 is
Kyungmin Park7f88f002008-11-19 16:28:06 +010026 * erased, and the updated volume table is written back to LEB 0. Then same for
27 * LEB 1. This scheme guarantees recoverability from unclean reboots.
28 *
29 * In this UBI implementation the on-flash volume table does not contain any
Heiko Schocherf5895d12014-06-24 10:10:04 +020030 * information about how much data static volumes contain.
Kyungmin Park7f88f002008-11-19 16:28:06 +010031 *
32 * But it would still be beneficial to store this information in the volume
33 * table. For example, suppose we have a static volume X, and all its physical
34 * eraseblocks became bad for some reasons. Suppose we are attaching the
Heiko Schocherf5895d12014-06-24 10:10:04 +020035 * corresponding MTD device, for some reason we find no logical eraseblocks
Kyungmin Park7f88f002008-11-19 16:28:06 +010036 * corresponding to the volume X. According to the volume table volume X does
37 * exist. So we don't know whether it is just empty or all its physical
Heiko Schocherf5895d12014-06-24 10:10:04 +020038 * eraseblocks went bad. So we cannot alarm the user properly.
Kyungmin Park7f88f002008-11-19 16:28:06 +010039 *
40 * The volume table also stores so-called "update marker", which is used for
41 * volume updates. Before updating the volume, the update marker is set, and
42 * after the update operation is finished, the update marker is cleared. So if
43 * the update operation was interrupted (e.g. by an unclean reboot) - the
44 * update marker is still there and we know that the volume's contents is
45 * damaged.
46 */
47
Heiko Schocherf5895d12014-06-24 10:10:04 +020048#ifndef __UBOOT__
Simon Glassd66c5f72020-02-03 07:36:15 -070049#include <dm/devres.h>
Kyungmin Park7f88f002008-11-19 16:28:06 +010050#include <linux/crc32.h>
51#include <linux/err.h>
Heiko Schocherf5895d12014-06-24 10:10:04 +020052#include <linux/slab.h>
Kyungmin Park7f88f002008-11-19 16:28:06 +010053#include <asm/div64.h>
Simon Glass48b6c6b2019-11-14 12:57:16 -070054#include <u-boot/crc.h>
Heiko Schocherf5895d12014-06-24 10:10:04 +020055#else
56#include <ubi_uboot.h>
Kyungmin Park7f88f002008-11-19 16:28:06 +010057#endif
58
Heiko Schocherf5895d12014-06-24 10:10:04 +020059#include <linux/err.h>
Kyungmin Park7f88f002008-11-19 16:28:06 +010060#include "ubi.h"
61
Heiko Schocherf5895d12014-06-24 10:10:04 +020062static void self_vtbl_check(const struct ubi_device *ubi);
Kyungmin Park7f88f002008-11-19 16:28:06 +010063
64/* Empty volume table record */
65static struct ubi_vtbl_record empty_vtbl_record;
66
67/**
Heiko Schocher94b66de2015-10-22 06:19:21 +020068 * ubi_update_layout_vol - helper for updatting layout volumes on flash
69 * @ubi: UBI device description object
70 */
71static int ubi_update_layout_vol(struct ubi_device *ubi)
72{
73 struct ubi_volume *layout_vol;
74 int i, err;
75
76 layout_vol = ubi->volumes[vol_id2idx(ubi, UBI_LAYOUT_VOLUME_ID)];
77 for (i = 0; i < UBI_LAYOUT_VOLUME_EBS; i++) {
78 err = ubi_eba_atomic_leb_change(ubi, layout_vol, i, ubi->vtbl,
79 ubi->vtbl_size);
80 if (err)
81 return err;
82 }
83
84 return 0;
85}
86
87/**
Kyungmin Park7f88f002008-11-19 16:28:06 +010088 * ubi_change_vtbl_record - change volume table record.
89 * @ubi: UBI device description object
90 * @idx: table index to change
91 * @vtbl_rec: new volume table record
92 *
93 * This function changes volume table record @idx. If @vtbl_rec is %NULL, empty
94 * volume table record is written. The caller does not have to calculate CRC of
95 * the record as it is done by this function. Returns zero in case of success
96 * and a negative error code in case of failure.
97 */
98int ubi_change_vtbl_record(struct ubi_device *ubi, int idx,
99 struct ubi_vtbl_record *vtbl_rec)
100{
Heiko Schocher94b66de2015-10-22 06:19:21 +0200101 int err;
Kyungmin Park7f88f002008-11-19 16:28:06 +0100102 uint32_t crc;
Kyungmin Park7f88f002008-11-19 16:28:06 +0100103
104 ubi_assert(idx >= 0 && idx < ubi->vtbl_slots);
Kyungmin Park7f88f002008-11-19 16:28:06 +0100105
106 if (!vtbl_rec)
107 vtbl_rec = &empty_vtbl_record;
108 else {
109 crc = crc32(UBI_CRC32_INIT, vtbl_rec, UBI_VTBL_RECORD_SIZE_CRC);
110 vtbl_rec->crc = cpu_to_be32(crc);
111 }
112
113 memcpy(&ubi->vtbl[idx], vtbl_rec, sizeof(struct ubi_vtbl_record));
Heiko Schocher94b66de2015-10-22 06:19:21 +0200114 err = ubi_update_layout_vol(ubi);
Heiko Schocherf5895d12014-06-24 10:10:04 +0200115
116 self_vtbl_check(ubi);
Heiko Schocher94b66de2015-10-22 06:19:21 +0200117 return err ? err : 0;
Heiko Schocherf5895d12014-06-24 10:10:04 +0200118}
119
120/**
121 * ubi_vtbl_rename_volumes - rename UBI volumes in the volume table.
122 * @ubi: UBI device description object
123 * @rename_list: list of &struct ubi_rename_entry objects
124 *
125 * This function re-names multiple volumes specified in @req in the volume
126 * table. Returns zero in case of success and a negative error code in case of
127 * failure.
128 */
129int ubi_vtbl_rename_volumes(struct ubi_device *ubi,
130 struct list_head *rename_list)
131{
Heiko Schocherf5895d12014-06-24 10:10:04 +0200132 struct ubi_rename_entry *re;
Heiko Schocherf5895d12014-06-24 10:10:04 +0200133
134 list_for_each_entry(re, rename_list, list) {
135 uint32_t crc;
136 struct ubi_volume *vol = re->desc->vol;
137 struct ubi_vtbl_record *vtbl_rec = &ubi->vtbl[vol->vol_id];
138
139 if (re->remove) {
140 memcpy(vtbl_rec, &empty_vtbl_record,
141 sizeof(struct ubi_vtbl_record));
142 continue;
143 }
144
145 vtbl_rec->name_len = cpu_to_be16(re->new_name_len);
146 memcpy(vtbl_rec->name, re->new_name, re->new_name_len);
147 memset(vtbl_rec->name + re->new_name_len, 0,
148 UBI_VOL_NAME_MAX + 1 - re->new_name_len);
149 crc = crc32(UBI_CRC32_INIT, vtbl_rec,
150 UBI_VTBL_RECORD_SIZE_CRC);
151 vtbl_rec->crc = cpu_to_be32(crc);
152 }
153
Heiko Schocher94b66de2015-10-22 06:19:21 +0200154 return ubi_update_layout_vol(ubi);
Kyungmin Park7f88f002008-11-19 16:28:06 +0100155}
156
157/**
Heiko Schocherf5895d12014-06-24 10:10:04 +0200158 * vtbl_check - check if volume table is not corrupted and sensible.
Kyungmin Park7f88f002008-11-19 16:28:06 +0100159 * @ubi: UBI device description object
160 * @vtbl: volume table
161 *
162 * This function returns zero if @vtbl is all right, %1 if CRC is incorrect,
163 * and %-EINVAL if it contains inconsistent data.
164 */
165static int vtbl_check(const struct ubi_device *ubi,
166 const struct ubi_vtbl_record *vtbl)
167{
168 int i, n, reserved_pebs, alignment, data_pad, vol_type, name_len;
169 int upd_marker, err;
170 uint32_t crc;
171 const char *name;
172
173 for (i = 0; i < ubi->vtbl_slots; i++) {
174 cond_resched();
175
176 reserved_pebs = be32_to_cpu(vtbl[i].reserved_pebs);
177 alignment = be32_to_cpu(vtbl[i].alignment);
178 data_pad = be32_to_cpu(vtbl[i].data_pad);
179 upd_marker = vtbl[i].upd_marker;
180 vol_type = vtbl[i].vol_type;
181 name_len = be16_to_cpu(vtbl[i].name_len);
Heiko Schocherf5895d12014-06-24 10:10:04 +0200182 name = &vtbl[i].name[0];
Kyungmin Park7f88f002008-11-19 16:28:06 +0100183
184 crc = crc32(UBI_CRC32_INIT, &vtbl[i], UBI_VTBL_RECORD_SIZE_CRC);
185 if (be32_to_cpu(vtbl[i].crc) != crc) {
Heiko Schocher94b66de2015-10-22 06:19:21 +0200186 ubi_err(ubi, "bad CRC at record %u: %#08x, not %#08x",
Kyungmin Park7f88f002008-11-19 16:28:06 +0100187 i, crc, be32_to_cpu(vtbl[i].crc));
Heiko Schocherf5895d12014-06-24 10:10:04 +0200188 ubi_dump_vtbl_record(&vtbl[i], i);
Kyungmin Park7f88f002008-11-19 16:28:06 +0100189 return 1;
190 }
191
192 if (reserved_pebs == 0) {
193 if (memcmp(&vtbl[i], &empty_vtbl_record,
194 UBI_VTBL_RECORD_SIZE)) {
195 err = 2;
196 goto bad;
197 }
198 continue;
199 }
200
201 if (reserved_pebs < 0 || alignment < 0 || data_pad < 0 ||
202 name_len < 0) {
203 err = 3;
204 goto bad;
205 }
206
207 if (alignment > ubi->leb_size || alignment == 0) {
208 err = 4;
209 goto bad;
210 }
211
212 n = alignment & (ubi->min_io_size - 1);
213 if (alignment != 1 && n) {
214 err = 5;
215 goto bad;
216 }
217
218 n = ubi->leb_size % alignment;
219 if (data_pad != n) {
Heiko Schocher94b66de2015-10-22 06:19:21 +0200220 ubi_err(ubi, "bad data_pad, has to be %d", n);
Kyungmin Park7f88f002008-11-19 16:28:06 +0100221 err = 6;
222 goto bad;
223 }
224
225 if (vol_type != UBI_VID_DYNAMIC && vol_type != UBI_VID_STATIC) {
226 err = 7;
227 goto bad;
228 }
229
230 if (upd_marker != 0 && upd_marker != 1) {
231 err = 8;
232 goto bad;
233 }
234
235 if (reserved_pebs > ubi->good_peb_count) {
Heiko Schocher94b66de2015-10-22 06:19:21 +0200236 ubi_err(ubi, "too large reserved_pebs %d, good PEBs %d",
Heiko Schocherf5895d12014-06-24 10:10:04 +0200237 reserved_pebs, ubi->good_peb_count);
Kyungmin Park7f88f002008-11-19 16:28:06 +0100238 err = 9;
239 goto bad;
240 }
241
242 if (name_len > UBI_VOL_NAME_MAX) {
243 err = 10;
244 goto bad;
245 }
246
247 if (name[0] == '\0') {
248 err = 11;
249 goto bad;
250 }
251
252 if (name_len != strnlen(name, name_len + 1)) {
253 err = 12;
254 goto bad;
255 }
256 }
257
258 /* Checks that all names are unique */
259 for (i = 0; i < ubi->vtbl_slots - 1; i++) {
260 for (n = i + 1; n < ubi->vtbl_slots; n++) {
261 int len1 = be16_to_cpu(vtbl[i].name_len);
262 int len2 = be16_to_cpu(vtbl[n].name_len);
263
264 if (len1 > 0 && len1 == len2 &&
Heiko Schocherf5895d12014-06-24 10:10:04 +0200265#ifndef __UBOOT__
266 !strncmp(vtbl[i].name, vtbl[n].name, len1)) {
267#else
268 !strncmp((char *)vtbl[i].name, vtbl[n].name, len1)) {
269#endif
Heiko Schocher94b66de2015-10-22 06:19:21 +0200270 ubi_err(ubi, "volumes %d and %d have the same name \"%s\"",
Heiko Schocherf5895d12014-06-24 10:10:04 +0200271 i, n, vtbl[i].name);
272 ubi_dump_vtbl_record(&vtbl[i], i);
273 ubi_dump_vtbl_record(&vtbl[n], n);
Kyungmin Park7f88f002008-11-19 16:28:06 +0100274 return -EINVAL;
275 }
276 }
277 }
278
279 return 0;
280
281bad:
Heiko Schocher94b66de2015-10-22 06:19:21 +0200282 ubi_err(ubi, "volume table check failed: record %d, error %d", i, err);
Heiko Schocherf5895d12014-06-24 10:10:04 +0200283 ubi_dump_vtbl_record(&vtbl[i], i);
Kyungmin Park7f88f002008-11-19 16:28:06 +0100284 return -EINVAL;
285}
286
287/**
288 * create_vtbl - create a copy of volume table.
289 * @ubi: UBI device description object
Heiko Schocherf5895d12014-06-24 10:10:04 +0200290 * @ai: attaching information
Kyungmin Park7f88f002008-11-19 16:28:06 +0100291 * @copy: number of the volume table copy
292 * @vtbl: contents of the volume table
293 *
294 * This function returns zero in case of success and a negative error code in
295 * case of failure.
296 */
Heiko Schocherf5895d12014-06-24 10:10:04 +0200297static int create_vtbl(struct ubi_device *ubi, struct ubi_attach_info *ai,
Kyungmin Park7f88f002008-11-19 16:28:06 +0100298 int copy, void *vtbl)
299{
300 int err, tries = 0;
Heiko Schocherf5895d12014-06-24 10:10:04 +0200301 struct ubi_vid_hdr *vid_hdr;
302 struct ubi_ainf_peb *new_aeb;
Kyungmin Park7f88f002008-11-19 16:28:06 +0100303
Heiko Schocherf5895d12014-06-24 10:10:04 +0200304 dbg_gen("create volume table (copy #%d)", copy + 1);
Kyungmin Park7f88f002008-11-19 16:28:06 +0100305
306 vid_hdr = ubi_zalloc_vid_hdr(ubi, GFP_KERNEL);
307 if (!vid_hdr)
308 return -ENOMEM;
309
Kyungmin Park7f88f002008-11-19 16:28:06 +0100310retry:
Heiko Schocherf5895d12014-06-24 10:10:04 +0200311 new_aeb = ubi_early_get_peb(ubi, ai);
312 if (IS_ERR(new_aeb)) {
313 err = PTR_ERR(new_aeb);
Kyungmin Park7f88f002008-11-19 16:28:06 +0100314 goto out_free;
315 }
316
Heiko Schocherf5895d12014-06-24 10:10:04 +0200317 vid_hdr->vol_type = UBI_LAYOUT_VOLUME_TYPE;
Kyungmin Park7f88f002008-11-19 16:28:06 +0100318 vid_hdr->vol_id = cpu_to_be32(UBI_LAYOUT_VOLUME_ID);
319 vid_hdr->compat = UBI_LAYOUT_VOLUME_COMPAT;
320 vid_hdr->data_size = vid_hdr->used_ebs =
321 vid_hdr->data_pad = cpu_to_be32(0);
322 vid_hdr->lnum = cpu_to_be32(copy);
Heiko Schocherf5895d12014-06-24 10:10:04 +0200323 vid_hdr->sqnum = cpu_to_be64(++ai->max_sqnum);
Kyungmin Park7f88f002008-11-19 16:28:06 +0100324
325 /* The EC header is already there, write the VID header */
Heiko Schocherf5895d12014-06-24 10:10:04 +0200326 err = ubi_io_write_vid_hdr(ubi, new_aeb->pnum, vid_hdr);
Kyungmin Park7f88f002008-11-19 16:28:06 +0100327 if (err)
328 goto write_error;
329
330 /* Write the layout volume contents */
Heiko Schocherf5895d12014-06-24 10:10:04 +0200331 err = ubi_io_write_data(ubi, vtbl, new_aeb->pnum, 0, ubi->vtbl_size);
Kyungmin Park7f88f002008-11-19 16:28:06 +0100332 if (err)
333 goto write_error;
334
335 /*
Heiko Schocherf5895d12014-06-24 10:10:04 +0200336 * And add it to the attaching information. Don't delete the old version
337 * of this LEB as it will be deleted and freed in 'ubi_add_to_av()'.
Kyungmin Park7f88f002008-11-19 16:28:06 +0100338 */
Heiko Schocherf5895d12014-06-24 10:10:04 +0200339 err = ubi_add_to_av(ubi, ai, new_aeb->pnum, new_aeb->ec, vid_hdr, 0);
340 kmem_cache_free(ai->aeb_slab_cache, new_aeb);
Kyungmin Park7f88f002008-11-19 16:28:06 +0100341 ubi_free_vid_hdr(ubi, vid_hdr);
342 return err;
343
344write_error:
345 if (err == -EIO && ++tries <= 5) {
346 /*
347 * Probably this physical eraseblock went bad, try to pick
348 * another one.
349 */
Heiko Schocherf5895d12014-06-24 10:10:04 +0200350 list_add(&new_aeb->u.list, &ai->erase);
Kyungmin Park7f88f002008-11-19 16:28:06 +0100351 goto retry;
352 }
Heiko Schocherf5895d12014-06-24 10:10:04 +0200353 kmem_cache_free(ai->aeb_slab_cache, new_aeb);
Kyungmin Park7f88f002008-11-19 16:28:06 +0100354out_free:
355 ubi_free_vid_hdr(ubi, vid_hdr);
356 return err;
357
358}
359
360/**
361 * process_lvol - process the layout volume.
362 * @ubi: UBI device description object
Heiko Schocherf5895d12014-06-24 10:10:04 +0200363 * @ai: attaching information
364 * @av: layout volume attaching information
Kyungmin Park7f88f002008-11-19 16:28:06 +0100365 *
366 * This function is responsible for reading the layout volume, ensuring it is
367 * not corrupted, and recovering from corruptions if needed. Returns volume
368 * table in case of success and a negative error code in case of failure.
369 */
370static struct ubi_vtbl_record *process_lvol(struct ubi_device *ubi,
Heiko Schocherf5895d12014-06-24 10:10:04 +0200371 struct ubi_attach_info *ai,
372 struct ubi_ainf_volume *av)
Kyungmin Park7f88f002008-11-19 16:28:06 +0100373{
374 int err;
375 struct rb_node *rb;
Heiko Schocherf5895d12014-06-24 10:10:04 +0200376 struct ubi_ainf_peb *aeb;
Kyungmin Park7f88f002008-11-19 16:28:06 +0100377 struct ubi_vtbl_record *leb[UBI_LAYOUT_VOLUME_EBS] = { NULL, NULL };
378 int leb_corrupted[UBI_LAYOUT_VOLUME_EBS] = {1, 1};
379
380 /*
381 * UBI goes through the following steps when it changes the layout
382 * volume:
383 * a. erase LEB 0;
384 * b. write new data to LEB 0;
385 * c. erase LEB 1;
386 * d. write new data to LEB 1.
387 *
388 * Before the change, both LEBs contain the same data.
389 *
390 * Due to unclean reboots, the contents of LEB 0 may be lost, but there
391 * should LEB 1. So it is OK if LEB 0 is corrupted while LEB 1 is not.
392 * Similarly, LEB 1 may be lost, but there should be LEB 0. And
393 * finally, unclean reboots may result in a situation when neither LEB
394 * 0 nor LEB 1 are corrupted, but they are different. In this case, LEB
395 * 0 contains more recent information.
396 *
397 * So the plan is to first check LEB 0. Then
Heiko Schocherf5895d12014-06-24 10:10:04 +0200398 * a. if LEB 0 is OK, it must be containing the most recent data; then
Kyungmin Park7f88f002008-11-19 16:28:06 +0100399 * we compare it with LEB 1, and if they are different, we copy LEB
400 * 0 to LEB 1;
401 * b. if LEB 0 is corrupted, but LEB 1 has to be OK, and we copy LEB 1
402 * to LEB 0.
403 */
404
Heiko Schocherf5895d12014-06-24 10:10:04 +0200405 dbg_gen("check layout volume");
Kyungmin Park7f88f002008-11-19 16:28:06 +0100406
407 /* Read both LEB 0 and LEB 1 into memory */
Heiko Schocherf5895d12014-06-24 10:10:04 +0200408 ubi_rb_for_each_entry(rb, aeb, &av->root, u.rb) {
409 leb[aeb->lnum] = vzalloc(ubi->vtbl_size);
410 if (!leb[aeb->lnum]) {
Kyungmin Park7f88f002008-11-19 16:28:06 +0100411 err = -ENOMEM;
412 goto out_free;
413 }
Kyungmin Park7f88f002008-11-19 16:28:06 +0100414
Heiko Schocherf5895d12014-06-24 10:10:04 +0200415 err = ubi_io_read_data(ubi, leb[aeb->lnum], aeb->pnum, 0,
Kyungmin Park7f88f002008-11-19 16:28:06 +0100416 ubi->vtbl_size);
Sergey Lapin3a38a552013-01-14 03:46:50 +0000417 if (err == UBI_IO_BITFLIPS || mtd_is_eccerr(err))
Kyungmin Park7f88f002008-11-19 16:28:06 +0100418 /*
419 * Scrub the PEB later. Note, -EBADMSG indicates an
420 * uncorrectable ECC error, but we have our own CRC and
421 * the data will be checked later. If the data is OK,
422 * the PEB will be scrubbed (because we set
Heiko Schocherf5895d12014-06-24 10:10:04 +0200423 * aeb->scrub). If the data is not OK, the contents of
Kyungmin Park7f88f002008-11-19 16:28:06 +0100424 * the PEB will be recovered from the second copy, and
Heiko Schocherf5895d12014-06-24 10:10:04 +0200425 * aeb->scrub will be cleared in
426 * 'ubi_add_to_av()'.
Kyungmin Park7f88f002008-11-19 16:28:06 +0100427 */
Heiko Schocherf5895d12014-06-24 10:10:04 +0200428 aeb->scrub = 1;
Kyungmin Park7f88f002008-11-19 16:28:06 +0100429 else if (err)
430 goto out_free;
431 }
432
433 err = -EINVAL;
434 if (leb[0]) {
435 leb_corrupted[0] = vtbl_check(ubi, leb[0]);
436 if (leb_corrupted[0] < 0)
437 goto out_free;
438 }
439
440 if (!leb_corrupted[0]) {
441 /* LEB 0 is OK */
442 if (leb[1])
Heiko Schocherf5895d12014-06-24 10:10:04 +0200443 leb_corrupted[1] = memcmp(leb[0], leb[1],
444 ubi->vtbl_size);
Kyungmin Park7f88f002008-11-19 16:28:06 +0100445 if (leb_corrupted[1]) {
Heiko Schocher94b66de2015-10-22 06:19:21 +0200446 ubi_warn(ubi, "volume table copy #2 is corrupted");
Heiko Schocherf5895d12014-06-24 10:10:04 +0200447 err = create_vtbl(ubi, ai, 1, leb[0]);
Kyungmin Park7f88f002008-11-19 16:28:06 +0100448 if (err)
449 goto out_free;
Heiko Schocher94b66de2015-10-22 06:19:21 +0200450 ubi_msg(ubi, "volume table was restored");
Kyungmin Park7f88f002008-11-19 16:28:06 +0100451 }
452
453 /* Both LEB 1 and LEB 2 are OK and consistent */
454 vfree(leb[1]);
455 return leb[0];
456 } else {
457 /* LEB 0 is corrupted or does not exist */
458 if (leb[1]) {
459 leb_corrupted[1] = vtbl_check(ubi, leb[1]);
460 if (leb_corrupted[1] < 0)
461 goto out_free;
462 }
463 if (leb_corrupted[1]) {
464 /* Both LEB 0 and LEB 1 are corrupted */
Heiko Schocher94b66de2015-10-22 06:19:21 +0200465 ubi_err(ubi, "both volume tables are corrupted");
Kyungmin Park7f88f002008-11-19 16:28:06 +0100466 goto out_free;
467 }
468
Heiko Schocher94b66de2015-10-22 06:19:21 +0200469 ubi_warn(ubi, "volume table copy #1 is corrupted");
Heiko Schocherf5895d12014-06-24 10:10:04 +0200470 err = create_vtbl(ubi, ai, 0, leb[1]);
Kyungmin Park7f88f002008-11-19 16:28:06 +0100471 if (err)
472 goto out_free;
Heiko Schocher94b66de2015-10-22 06:19:21 +0200473 ubi_msg(ubi, "volume table was restored");
Kyungmin Park7f88f002008-11-19 16:28:06 +0100474
475 vfree(leb[0]);
476 return leb[1];
477 }
478
479out_free:
480 vfree(leb[0]);
481 vfree(leb[1]);
482 return ERR_PTR(err);
483}
484
485/**
486 * create_empty_lvol - create empty layout volume.
487 * @ubi: UBI device description object
Heiko Schocherf5895d12014-06-24 10:10:04 +0200488 * @ai: attaching information
Kyungmin Park7f88f002008-11-19 16:28:06 +0100489 *
490 * This function returns volume table contents in case of success and a
491 * negative error code in case of failure.
492 */
493static struct ubi_vtbl_record *create_empty_lvol(struct ubi_device *ubi,
Heiko Schocherf5895d12014-06-24 10:10:04 +0200494 struct ubi_attach_info *ai)
Kyungmin Park7f88f002008-11-19 16:28:06 +0100495{
496 int i;
497 struct ubi_vtbl_record *vtbl;
498
Heiko Schocherf5895d12014-06-24 10:10:04 +0200499 vtbl = vzalloc(ubi->vtbl_size);
Kyungmin Park7f88f002008-11-19 16:28:06 +0100500 if (!vtbl)
501 return ERR_PTR(-ENOMEM);
Kyungmin Park7f88f002008-11-19 16:28:06 +0100502
503 for (i = 0; i < ubi->vtbl_slots; i++)
504 memcpy(&vtbl[i], &empty_vtbl_record, UBI_VTBL_RECORD_SIZE);
505
506 for (i = 0; i < UBI_LAYOUT_VOLUME_EBS; i++) {
507 int err;
508
Heiko Schocherf5895d12014-06-24 10:10:04 +0200509 err = create_vtbl(ubi, ai, i, vtbl);
Kyungmin Park7f88f002008-11-19 16:28:06 +0100510 if (err) {
511 vfree(vtbl);
512 return ERR_PTR(err);
513 }
514 }
515
516 return vtbl;
517}
518
519/**
520 * init_volumes - initialize volume information for existing volumes.
521 * @ubi: UBI device description object
Heiko Schocherf5895d12014-06-24 10:10:04 +0200522 * @ai: scanning information
Kyungmin Park7f88f002008-11-19 16:28:06 +0100523 * @vtbl: volume table
524 *
525 * This function allocates volume description objects for existing volumes.
526 * Returns zero in case of success and a negative error code in case of
527 * failure.
528 */
Heiko Schocherf5895d12014-06-24 10:10:04 +0200529static int init_volumes(struct ubi_device *ubi,
530 const struct ubi_attach_info *ai,
Kyungmin Park7f88f002008-11-19 16:28:06 +0100531 const struct ubi_vtbl_record *vtbl)
532{
533 int i, reserved_pebs = 0;
Heiko Schocherf5895d12014-06-24 10:10:04 +0200534 struct ubi_ainf_volume *av;
Kyungmin Park7f88f002008-11-19 16:28:06 +0100535 struct ubi_volume *vol;
536
537 for (i = 0; i < ubi->vtbl_slots; i++) {
538 cond_resched();
539
540 if (be32_to_cpu(vtbl[i].reserved_pebs) == 0)
541 continue; /* Empty record */
542
543 vol = kzalloc(sizeof(struct ubi_volume), GFP_KERNEL);
544 if (!vol)
545 return -ENOMEM;
546
547 vol->reserved_pebs = be32_to_cpu(vtbl[i].reserved_pebs);
548 vol->alignment = be32_to_cpu(vtbl[i].alignment);
549 vol->data_pad = be32_to_cpu(vtbl[i].data_pad);
Peter Horton3ca9a932010-06-12 10:11:56 +0900550 vol->upd_marker = vtbl[i].upd_marker;
Kyungmin Park7f88f002008-11-19 16:28:06 +0100551 vol->vol_type = vtbl[i].vol_type == UBI_VID_DYNAMIC ?
552 UBI_DYNAMIC_VOLUME : UBI_STATIC_VOLUME;
553 vol->name_len = be16_to_cpu(vtbl[i].name_len);
554 vol->usable_leb_size = ubi->leb_size - vol->data_pad;
555 memcpy(vol->name, vtbl[i].name, vol->name_len);
556 vol->name[vol->name_len] = '\0';
557 vol->vol_id = i;
558
Quentin Schulza88049b2019-09-12 16:41:01 +0200559 if (vtbl[i].flags & UBI_VTBL_SKIP_CRC_CHECK_FLG)
560 vol->skip_check = 1;
561
Kyungmin Park7f88f002008-11-19 16:28:06 +0100562 if (vtbl[i].flags & UBI_VTBL_AUTORESIZE_FLG) {
563 /* Auto re-size flag may be set only for one volume */
564 if (ubi->autoresize_vol_id != -1) {
Heiko Schocher94b66de2015-10-22 06:19:21 +0200565 ubi_err(ubi, "more than one auto-resize volume (%d and %d)",
Heiko Schocherf5895d12014-06-24 10:10:04 +0200566 ubi->autoresize_vol_id, i);
Kyungmin Park7f88f002008-11-19 16:28:06 +0100567 kfree(vol);
568 return -EINVAL;
569 }
570
571 ubi->autoresize_vol_id = i;
572 }
573
574 ubi_assert(!ubi->volumes[i]);
575 ubi->volumes[i] = vol;
576 ubi->vol_count += 1;
577 vol->ubi = ubi;
578 reserved_pebs += vol->reserved_pebs;
579
580 /*
581 * In case of dynamic volume UBI knows nothing about how many
582 * data is stored there. So assume the whole volume is used.
583 */
584 if (vol->vol_type == UBI_DYNAMIC_VOLUME) {
585 vol->used_ebs = vol->reserved_pebs;
586 vol->last_eb_bytes = vol->usable_leb_size;
587 vol->used_bytes =
588 (long long)vol->used_ebs * vol->usable_leb_size;
589 continue;
590 }
591
592 /* Static volumes only */
Heiko Schocherf5895d12014-06-24 10:10:04 +0200593 av = ubi_find_av(ai, i);
Heiko Schocher94b66de2015-10-22 06:19:21 +0200594 if (!av || !av->leb_count) {
Kyungmin Park7f88f002008-11-19 16:28:06 +0100595 /*
596 * No eraseblocks belonging to this volume found. We
597 * don't actually know whether this static volume is
598 * completely corrupted or just contains no data. And
599 * we cannot know this as long as data size is not
600 * stored on flash. So we just assume the volume is
601 * empty. FIXME: this should be handled.
602 */
603 continue;
604 }
605
Heiko Schocherf5895d12014-06-24 10:10:04 +0200606 if (av->leb_count != av->used_ebs) {
Kyungmin Park7f88f002008-11-19 16:28:06 +0100607 /*
608 * We found a static volume which misses several
609 * eraseblocks. Treat it as corrupted.
610 */
Heiko Schocher94b66de2015-10-22 06:19:21 +0200611 ubi_warn(ubi, "static volume %d misses %d LEBs - corrupted",
Heiko Schocherf5895d12014-06-24 10:10:04 +0200612 av->vol_id, av->used_ebs - av->leb_count);
Kyungmin Park7f88f002008-11-19 16:28:06 +0100613 vol->corrupted = 1;
614 continue;
615 }
616
Heiko Schocherf5895d12014-06-24 10:10:04 +0200617 vol->used_ebs = av->used_ebs;
Kyungmin Park7f88f002008-11-19 16:28:06 +0100618 vol->used_bytes =
619 (long long)(vol->used_ebs - 1) * vol->usable_leb_size;
Heiko Schocherf5895d12014-06-24 10:10:04 +0200620 vol->used_bytes += av->last_data_size;
621 vol->last_eb_bytes = av->last_data_size;
Kyungmin Park7f88f002008-11-19 16:28:06 +0100622 }
623
624 /* And add the layout volume */
625 vol = kzalloc(sizeof(struct ubi_volume), GFP_KERNEL);
626 if (!vol)
627 return -ENOMEM;
628
629 vol->reserved_pebs = UBI_LAYOUT_VOLUME_EBS;
Heiko Schocherf5895d12014-06-24 10:10:04 +0200630 vol->alignment = UBI_LAYOUT_VOLUME_ALIGN;
Kyungmin Park7f88f002008-11-19 16:28:06 +0100631 vol->vol_type = UBI_DYNAMIC_VOLUME;
632 vol->name_len = sizeof(UBI_LAYOUT_VOLUME_NAME) - 1;
633 memcpy(vol->name, UBI_LAYOUT_VOLUME_NAME, vol->name_len + 1);
634 vol->usable_leb_size = ubi->leb_size;
635 vol->used_ebs = vol->reserved_pebs;
636 vol->last_eb_bytes = vol->reserved_pebs;
637 vol->used_bytes =
638 (long long)vol->used_ebs * (ubi->leb_size - vol->data_pad);
639 vol->vol_id = UBI_LAYOUT_VOLUME_ID;
640 vol->ref_count = 1;
641
642 ubi_assert(!ubi->volumes[i]);
643 ubi->volumes[vol_id2idx(ubi, vol->vol_id)] = vol;
644 reserved_pebs += vol->reserved_pebs;
645 ubi->vol_count += 1;
646 vol->ubi = ubi;
647
Heiko Schocherf5895d12014-06-24 10:10:04 +0200648 if (reserved_pebs > ubi->avail_pebs) {
Heiko Schocher94b66de2015-10-22 06:19:21 +0200649 ubi_err(ubi, "not enough PEBs, required %d, available %d",
Kyungmin Park7f88f002008-11-19 16:28:06 +0100650 reserved_pebs, ubi->avail_pebs);
Heiko Schocherf5895d12014-06-24 10:10:04 +0200651 if (ubi->corr_peb_count)
Heiko Schocher94b66de2015-10-22 06:19:21 +0200652 ubi_err(ubi, "%d PEBs are corrupted and not used",
Heiko Schocherf5895d12014-06-24 10:10:04 +0200653 ubi->corr_peb_count);
654 }
Kyungmin Park7f88f002008-11-19 16:28:06 +0100655 ubi->rsvd_pebs += reserved_pebs;
656 ubi->avail_pebs -= reserved_pebs;
657
658 return 0;
659}
660
661/**
Heiko Schocherf5895d12014-06-24 10:10:04 +0200662 * check_av - check volume attaching information.
Kyungmin Park7f88f002008-11-19 16:28:06 +0100663 * @vol: UBI volume description object
Heiko Schocherf5895d12014-06-24 10:10:04 +0200664 * @av: volume attaching information
Kyungmin Park7f88f002008-11-19 16:28:06 +0100665 *
Heiko Schocherf5895d12014-06-24 10:10:04 +0200666 * This function returns zero if the volume attaching information is consistent
Kyungmin Park7f88f002008-11-19 16:28:06 +0100667 * to the data read from the volume tabla, and %-EINVAL if not.
668 */
Heiko Schocherf5895d12014-06-24 10:10:04 +0200669static int check_av(const struct ubi_volume *vol,
670 const struct ubi_ainf_volume *av)
Kyungmin Park7f88f002008-11-19 16:28:06 +0100671{
672 int err;
673
Heiko Schocherf5895d12014-06-24 10:10:04 +0200674 if (av->highest_lnum >= vol->reserved_pebs) {
Kyungmin Park7f88f002008-11-19 16:28:06 +0100675 err = 1;
676 goto bad;
677 }
Heiko Schocherf5895d12014-06-24 10:10:04 +0200678 if (av->leb_count > vol->reserved_pebs) {
Kyungmin Park7f88f002008-11-19 16:28:06 +0100679 err = 2;
680 goto bad;
681 }
Heiko Schocherf5895d12014-06-24 10:10:04 +0200682 if (av->vol_type != vol->vol_type) {
Kyungmin Park7f88f002008-11-19 16:28:06 +0100683 err = 3;
684 goto bad;
685 }
Heiko Schocherf5895d12014-06-24 10:10:04 +0200686 if (av->used_ebs > vol->reserved_pebs) {
Kyungmin Park7f88f002008-11-19 16:28:06 +0100687 err = 4;
688 goto bad;
689 }
Heiko Schocherf5895d12014-06-24 10:10:04 +0200690 if (av->data_pad != vol->data_pad) {
Kyungmin Park7f88f002008-11-19 16:28:06 +0100691 err = 5;
692 goto bad;
693 }
694 return 0;
695
696bad:
Heiko Schocher94b66de2015-10-22 06:19:21 +0200697 ubi_err(vol->ubi, "bad attaching information, error %d", err);
Heiko Schocherf5895d12014-06-24 10:10:04 +0200698 ubi_dump_av(av);
699 ubi_dump_vol_info(vol);
Kyungmin Park7f88f002008-11-19 16:28:06 +0100700 return -EINVAL;
701}
702
703/**
Heiko Schocherf5895d12014-06-24 10:10:04 +0200704 * check_attaching_info - check that attaching information.
Kyungmin Park7f88f002008-11-19 16:28:06 +0100705 * @ubi: UBI device description object
Heiko Schocherf5895d12014-06-24 10:10:04 +0200706 * @ai: attaching information
Kyungmin Park7f88f002008-11-19 16:28:06 +0100707 *
708 * Even though we protect on-flash data by CRC checksums, we still don't trust
Heiko Schocherf5895d12014-06-24 10:10:04 +0200709 * the media. This function ensures that attaching information is consistent to
710 * the information read from the volume table. Returns zero if the attaching
Kyungmin Park7f88f002008-11-19 16:28:06 +0100711 * information is OK and %-EINVAL if it is not.
712 */
Heiko Schocherf5895d12014-06-24 10:10:04 +0200713static int check_attaching_info(const struct ubi_device *ubi,
714 struct ubi_attach_info *ai)
Kyungmin Park7f88f002008-11-19 16:28:06 +0100715{
716 int err, i;
Heiko Schocherf5895d12014-06-24 10:10:04 +0200717 struct ubi_ainf_volume *av;
Kyungmin Park7f88f002008-11-19 16:28:06 +0100718 struct ubi_volume *vol;
719
Heiko Schocherf5895d12014-06-24 10:10:04 +0200720 if (ai->vols_found > UBI_INT_VOL_COUNT + ubi->vtbl_slots) {
Heiko Schocher94b66de2015-10-22 06:19:21 +0200721 ubi_err(ubi, "found %d volumes while attaching, maximum is %d + %d",
Heiko Schocherf5895d12014-06-24 10:10:04 +0200722 ai->vols_found, UBI_INT_VOL_COUNT, ubi->vtbl_slots);
Kyungmin Park7f88f002008-11-19 16:28:06 +0100723 return -EINVAL;
724 }
725
Heiko Schocherf5895d12014-06-24 10:10:04 +0200726 if (ai->highest_vol_id >= ubi->vtbl_slots + UBI_INT_VOL_COUNT &&
727 ai->highest_vol_id < UBI_INTERNAL_VOL_START) {
Heiko Schocher94b66de2015-10-22 06:19:21 +0200728 ubi_err(ubi, "too large volume ID %d found",
729 ai->highest_vol_id);
Kyungmin Park7f88f002008-11-19 16:28:06 +0100730 return -EINVAL;
731 }
732
733 for (i = 0; i < ubi->vtbl_slots + UBI_INT_VOL_COUNT; i++) {
734 cond_resched();
735
Heiko Schocherf5895d12014-06-24 10:10:04 +0200736 av = ubi_find_av(ai, i);
Kyungmin Park7f88f002008-11-19 16:28:06 +0100737 vol = ubi->volumes[i];
738 if (!vol) {
Heiko Schocherf5895d12014-06-24 10:10:04 +0200739 if (av)
740 ubi_remove_av(ai, av);
Kyungmin Park7f88f002008-11-19 16:28:06 +0100741 continue;
742 }
743
744 if (vol->reserved_pebs == 0) {
745 ubi_assert(i < ubi->vtbl_slots);
746
Heiko Schocherf5895d12014-06-24 10:10:04 +0200747 if (!av)
Kyungmin Park7f88f002008-11-19 16:28:06 +0100748 continue;
749
750 /*
Heiko Schocherf5895d12014-06-24 10:10:04 +0200751 * During attaching we found a volume which does not
Kyungmin Park7f88f002008-11-19 16:28:06 +0100752 * exist according to the information in the volume
753 * table. This must have happened due to an unclean
754 * reboot while the volume was being removed. Discard
755 * these eraseblocks.
756 */
Heiko Schocher94b66de2015-10-22 06:19:21 +0200757 ubi_msg(ubi, "finish volume %d removal", av->vol_id);
Heiko Schocherf5895d12014-06-24 10:10:04 +0200758 ubi_remove_av(ai, av);
759 } else if (av) {
760 err = check_av(vol, av);
Kyungmin Park7f88f002008-11-19 16:28:06 +0100761 if (err)
762 return err;
763 }
764 }
765
766 return 0;
767}
768
769/**
Heiko Schocherf5895d12014-06-24 10:10:04 +0200770 * ubi_read_volume_table - read the volume table.
Kyungmin Park7f88f002008-11-19 16:28:06 +0100771 * @ubi: UBI device description object
Heiko Schocherf5895d12014-06-24 10:10:04 +0200772 * @ai: attaching information
Kyungmin Park7f88f002008-11-19 16:28:06 +0100773 *
774 * This function reads volume table, checks it, recover from errors if needed,
775 * or creates it if needed. Returns zero in case of success and a negative
776 * error code in case of failure.
777 */
Heiko Schocherf5895d12014-06-24 10:10:04 +0200778int ubi_read_volume_table(struct ubi_device *ubi, struct ubi_attach_info *ai)
Kyungmin Park7f88f002008-11-19 16:28:06 +0100779{
780 int i, err;
Heiko Schocherf5895d12014-06-24 10:10:04 +0200781 struct ubi_ainf_volume *av;
Kyungmin Park7f88f002008-11-19 16:28:06 +0100782
783 empty_vtbl_record.crc = cpu_to_be32(0xf116c36b);
784
785 /*
786 * The number of supported volumes is limited by the eraseblock size
787 * and by the UBI_MAX_VOLUMES constant.
788 */
789 ubi->vtbl_slots = ubi->leb_size / UBI_VTBL_RECORD_SIZE;
790 if (ubi->vtbl_slots > UBI_MAX_VOLUMES)
791 ubi->vtbl_slots = UBI_MAX_VOLUMES;
792
793 ubi->vtbl_size = ubi->vtbl_slots * UBI_VTBL_RECORD_SIZE;
794 ubi->vtbl_size = ALIGN(ubi->vtbl_size, ubi->min_io_size);
795
Heiko Schocherf5895d12014-06-24 10:10:04 +0200796 av = ubi_find_av(ai, UBI_LAYOUT_VOLUME_ID);
797 if (!av) {
Kyungmin Park7f88f002008-11-19 16:28:06 +0100798 /*
799 * No logical eraseblocks belonging to the layout volume were
800 * found. This could mean that the flash is just empty. In
801 * this case we create empty layout volume.
802 *
803 * But if flash is not empty this must be a corruption or the
804 * MTD device just contains garbage.
805 */
Heiko Schocherf5895d12014-06-24 10:10:04 +0200806 if (ai->is_empty) {
807 ubi->vtbl = create_empty_lvol(ubi, ai);
Kyungmin Park7f88f002008-11-19 16:28:06 +0100808 if (IS_ERR(ubi->vtbl))
809 return PTR_ERR(ubi->vtbl);
810 } else {
Heiko Schocher94b66de2015-10-22 06:19:21 +0200811 ubi_err(ubi, "the layout volume was not found");
Kyungmin Park7f88f002008-11-19 16:28:06 +0100812 return -EINVAL;
813 }
814 } else {
Heiko Schocherf5895d12014-06-24 10:10:04 +0200815 if (av->leb_count > UBI_LAYOUT_VOLUME_EBS) {
Kyungmin Park7f88f002008-11-19 16:28:06 +0100816 /* This must not happen with proper UBI images */
Heiko Schocher94b66de2015-10-22 06:19:21 +0200817 ubi_err(ubi, "too many LEBs (%d) in layout volume",
Heiko Schocherf5895d12014-06-24 10:10:04 +0200818 av->leb_count);
Kyungmin Park7f88f002008-11-19 16:28:06 +0100819 return -EINVAL;
820 }
821
Heiko Schocherf5895d12014-06-24 10:10:04 +0200822 ubi->vtbl = process_lvol(ubi, ai, av);
Kyungmin Park7f88f002008-11-19 16:28:06 +0100823 if (IS_ERR(ubi->vtbl))
824 return PTR_ERR(ubi->vtbl);
825 }
826
Heiko Schocherf5895d12014-06-24 10:10:04 +0200827 ubi->avail_pebs = ubi->good_peb_count - ubi->corr_peb_count;
Kyungmin Park7f88f002008-11-19 16:28:06 +0100828
829 /*
830 * The layout volume is OK, initialize the corresponding in-RAM data
831 * structures.
832 */
Heiko Schocherf5895d12014-06-24 10:10:04 +0200833 err = init_volumes(ubi, ai, ubi->vtbl);
Kyungmin Park7f88f002008-11-19 16:28:06 +0100834 if (err)
835 goto out_free;
836
837 /*
Heiko Schocherf5895d12014-06-24 10:10:04 +0200838 * Make sure that the attaching information is consistent to the
Kyungmin Park7f88f002008-11-19 16:28:06 +0100839 * information stored in the volume table.
840 */
Heiko Schocherf5895d12014-06-24 10:10:04 +0200841 err = check_attaching_info(ubi, ai);
Kyungmin Park7f88f002008-11-19 16:28:06 +0100842 if (err)
843 goto out_free;
844
845 return 0;
846
847out_free:
848 vfree(ubi->vtbl);
Heiko Schocherf5895d12014-06-24 10:10:04 +0200849 for (i = 0; i < ubi->vtbl_slots + UBI_INT_VOL_COUNT; i++) {
850 kfree(ubi->volumes[i]);
851 ubi->volumes[i] = NULL;
852 }
Kyungmin Park7f88f002008-11-19 16:28:06 +0100853 return err;
854}
855
Kyungmin Park7f88f002008-11-19 16:28:06 +0100856/**
Heiko Schocherf5895d12014-06-24 10:10:04 +0200857 * self_vtbl_check - check volume table.
Kyungmin Park7f88f002008-11-19 16:28:06 +0100858 * @ubi: UBI device description object
859 */
Heiko Schocherf5895d12014-06-24 10:10:04 +0200860static void self_vtbl_check(const struct ubi_device *ubi)
Kyungmin Park7f88f002008-11-19 16:28:06 +0100861{
Heiko Schocherf5895d12014-06-24 10:10:04 +0200862 if (!ubi_dbg_chk_gen(ubi))
863 return;
864
Kyungmin Park7f88f002008-11-19 16:28:06 +0100865 if (vtbl_check(ubi, ubi->vtbl)) {
Heiko Schocher94b66de2015-10-22 06:19:21 +0200866 ubi_err(ubi, "self-check failed");
Kyungmin Park7f88f002008-11-19 16:28:06 +0100867 BUG();
868 }
869}