blob: 49530b7448f9df8d7653658682b1a5dafe53b7ce [file] [log] [blame]
Kyungmin Park2d262c42008-11-19 16:26:54 +01001/*
2 * Copyright (c) International Business Machines Corp., 2006
3 *
Wolfgang Denk1a459662013-07-08 09:37:19 +02004 * SPDX-License-Identifier: GPL-2.0+
Kyungmin Park2d262c42008-11-19 16:26:54 +01005 *
6 * Author: Artem Bityutskiy (Битюцкий Артём)
7 */
8
9/* Here we keep miscellaneous functions which are used all over the UBI code */
10
11#include <ubi_uboot.h>
12#include "ubi.h"
13
14/**
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
19 *
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".
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 /* The resulting length must be aligned to the minimum flash I/O size */
36 length = ALIGN(i + 1, ubi->min_io_size);
37 return length;
38}
39
40/**
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
44 *
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.
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 if (i == vol->used_ebs - 1)
67 size = vol->last_eb_bytes;
68 else
69 size = vol->usable_leb_size;
70
71 err = ubi_eba_read_leb(ubi, vol, i, buf, 0, size, 1);
72 if (err) {
Sergey Lapindfe64e22013-01-14 03:46:50 +000073 if (mtd_is_eccerr(err))
Kyungmin Park2d262c42008-11-19 16:26:54 +010074 err = 1;
75 break;
76 }
77 }
78
79 vfree(buf);
80 return err;
81}
82
83/**
Heiko Schocherff94bc42014-06-24 10:10:04 +020084 * ubi_update_reserved - update bad eraseblock handling accounting data.
85 * @ubi: UBI device description object
86 *
87 * This function calculates the gap between current number of PEBs reserved for
88 * bad eraseblock handling and the required level of PEBs that must be
89 * reserved, and if necessary, reserves more PEBs to fill that gap, according
90 * to availability. Should be called with ubi->volumes_lock held.
91 */
92void ubi_update_reserved(struct ubi_device *ubi)
93{
94 int need = ubi->beb_rsvd_level - ubi->beb_rsvd_pebs;
95
96 if (need <= 0 || ubi->avail_pebs == 0)
97 return;
98
99 need = min_t(int, need, ubi->avail_pebs);
100 ubi->avail_pebs -= need;
101 ubi->rsvd_pebs += need;
102 ubi->beb_rsvd_pebs += need;
103 ubi_msg("reserved more %d PEBs for bad PEB handling", need);
104}
105
106/**
107 * ubi_calculate_reserved - calculate how many PEBs must be reserved for bad
Kyungmin Park2d262c42008-11-19 16:26:54 +0100108 * eraseblock handling.
109 * @ubi: UBI device description object
110 */
111void ubi_calculate_reserved(struct ubi_device *ubi)
112{
Heiko Schocherff94bc42014-06-24 10:10:04 +0200113 /*
114 * Calculate the actual number of PEBs currently needed to be reserved
115 * for future bad eraseblock handling.
116 */
117 ubi->beb_rsvd_level = ubi->bad_peb_limit - ubi->bad_peb_count;
118 if (ubi->beb_rsvd_level < 0) {
119 ubi->beb_rsvd_level = 0;
120 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)",
121 ubi->bad_peb_count, ubi->bad_peb_limit);
122 }
123}
124
125/**
126 * ubi_check_pattern - check if buffer contains only a certain byte pattern.
127 * @buf: buffer to check
128 * @patt: the pattern to check
129 * @size: buffer size in bytes
130 *
131 * This function returns %1 in there are only @patt bytes in @buf, and %0 if
132 * something else was also found.
133 */
134int ubi_check_pattern(const void *buf, uint8_t patt, int size)
135{
136 int i;
137
138 for (i = 0; i < size; i++)
139 if (((const uint8_t *)buf)[i] != patt)
140 return 0;
141 return 1;
Kyungmin Park2d262c42008-11-19 16:26:54 +0100142}