2
* Copyright (c) International Business Machines Corp., 2006
4
* SPDX-License-Identifier: GPL-2.0+
6
* Author: Artem Bityutskiy (Битюцкий Артём)
9
/* Here we keep miscellaneous functions which are used all over the UBI code */
11
#include <ubi_uboot.h>
15
* calc_data_len - calculate how much real data is stored in a buffer.
16
* @ubi: UBI device description object
17
* @buf: a buffer with the contents of the physical eraseblock
18
* @length: the buffer length
20
* This function calculates how much "real data" is stored in @buf and returnes
21
* the length. Continuous 0xFF bytes at the end of the buffer are not
22
* considered as "real data".
24
int ubi_calc_data_len(const struct ubi_device *ubi, const void *buf,
29
ubi_assert(!(length & (ubi->min_io_size - 1)));
31
for (i = length - 1; i >= 0; i--)
32
if (((const uint8_t *)buf)[i] != 0xFF)
35
/* The resulting length must be aligned to the minimum flash I/O size */
36
length = ALIGN(i + 1, ubi->min_io_size);
41
* ubi_check_volume - check the contents of a static volume.
42
* @ubi: UBI device description object
43
* @vol_id: ID of the volume to check
45
* This function checks if static volume @vol_id is corrupted by fully reading
46
* it and checking data CRC. This function returns %0 if the volume is not
47
* corrupted, %1 if it is corrupted and a negative error code in case of
48
* failure. Dynamic volumes are not checked and zero is returned immediately.
50
int ubi_check_volume(struct ubi_device *ubi, int vol_id)
54
struct ubi_volume *vol = ubi->volumes[vol_id];
56
if (vol->vol_type != UBI_STATIC_VOLUME)
59
buf = vmalloc(vol->usable_leb_size);
63
for (i = 0; i < vol->used_ebs; i++) {
66
if (i == vol->used_ebs - 1)
67
size = vol->last_eb_bytes;
69
size = vol->usable_leb_size;
71
err = ubi_eba_read_leb(ubi, vol, i, buf, 0, size, 1);
73
if (mtd_is_eccerr(err))
84
* ubi_calculate_rsvd_pool - calculate how many PEBs must be reserved for bad
85
* eraseblock handling.
86
* @ubi: UBI device description object
88
void ubi_calculate_reserved(struct ubi_device *ubi)
90
ubi->beb_rsvd_level = ubi->good_peb_count/100;
91
ubi->beb_rsvd_level *= CONFIG_MTD_UBI_BEB_RESERVE;
92
if (ubi->beb_rsvd_level < MIN_RESEVED_PEBS)
93
ubi->beb_rsvd_level = MIN_RESEVED_PEBS;