avionic design with actual uboot and tooling
submodule of avionic design uboot bootloader and with included tools to get you started , read readme.md and readme-tk1-loader.md
This commit is contained in:
11
u-boot/drivers/mtd/ubi/Makefile
Normal file
11
u-boot/drivers/mtd/ubi/Makefile
Normal file
@@ -0,0 +1,11 @@
|
||||
#
|
||||
# (C) Copyright 2006
|
||||
# Wolfgang Denk, DENX Software Engineering, wd@denx.de.
|
||||
#
|
||||
# SPDX-License-Identifier: GPL-2.0+
|
||||
#
|
||||
|
||||
obj-y += attach.o build.o vtbl.o vmt.o upd.o kapi.o eba.o io.o wl.o crc32.o
|
||||
obj-$(CONFIG_MTD_UBI_FASTMAP) += fastmap.o
|
||||
obj-y += misc.o
|
||||
obj-y += debug.o
|
||||
1764
u-boot/drivers/mtd/ubi/attach.c
Normal file
1764
u-boot/drivers/mtd/ubi/attach.c
Normal file
File diff suppressed because it is too large
Load Diff
1561
u-boot/drivers/mtd/ubi/build.c
Normal file
1561
u-boot/drivers/mtd/ubi/build.c
Normal file
File diff suppressed because it is too large
Load Diff
510
u-boot/drivers/mtd/ubi/crc32.c
Normal file
510
u-boot/drivers/mtd/ubi/crc32.c
Normal file
@@ -0,0 +1,510 @@
|
||||
/*
|
||||
* Oct 15, 2000 Matt Domsch <Matt_Domsch@dell.com>
|
||||
* Nicer crc32 functions/docs submitted by linux@horizon.com. Thanks!
|
||||
* Code was from the public domain, copyright abandoned. Code was
|
||||
* subsequently included in the kernel, thus was re-licensed under the
|
||||
* GNU GPL v2.
|
||||
*
|
||||
* Oct 12, 2000 Matt Domsch <Matt_Domsch@dell.com>
|
||||
* Same crc32 function was used in 5 other places in the kernel.
|
||||
* I made one version, and deleted the others.
|
||||
* There are various incantations of crc32(). Some use a seed of 0 or ~0.
|
||||
* Some xor at the end with ~0. The generic crc32() function takes
|
||||
* seed as an argument, and doesn't xor at the end. Then individual
|
||||
* users can do whatever they need.
|
||||
* drivers/net/smc9194.c uses seed ~0, doesn't xor with ~0.
|
||||
* fs/jffs2 uses seed 0, doesn't xor with ~0.
|
||||
* fs/partitions/efi.c uses seed ~0, xor's with ~0.
|
||||
*
|
||||
* This source code is licensed under the GNU General Public License,
|
||||
* Version 2. See the file COPYING for more details.
|
||||
*/
|
||||
|
||||
#ifndef __UBOOT__
|
||||
#include <linux/crc32.h>
|
||||
#include <linux/kernel.h>
|
||||
#include <linux/module.h>
|
||||
#include <linux/compiler.h>
|
||||
#endif
|
||||
#include <linux/types.h>
|
||||
|
||||
#include <asm/byteorder.h>
|
||||
|
||||
#ifndef __UBOOT__
|
||||
#include <linux/slab.h>
|
||||
#include <linux/init.h>
|
||||
#include <asm/atomic.h>
|
||||
#endif
|
||||
#include "crc32defs.h"
|
||||
#define CRC_LE_BITS 8
|
||||
|
||||
#if CRC_LE_BITS == 8
|
||||
#define tole(x) cpu_to_le32(x)
|
||||
#define tobe(x) cpu_to_be32(x)
|
||||
#else
|
||||
#define tole(x) (x)
|
||||
#define tobe(x) (x)
|
||||
#endif
|
||||
#include "crc32table.h"
|
||||
#ifndef __UBOOT__
|
||||
MODULE_AUTHOR("Matt Domsch <Matt_Domsch@dell.com>");
|
||||
MODULE_DESCRIPTION("Ethernet CRC32 calculations");
|
||||
MODULE_LICENSE("GPL");
|
||||
#endif
|
||||
/**
|
||||
* crc32_le() - Calculate bitwise little-endian Ethernet AUTODIN II CRC32
|
||||
* @crc: seed value for computation. ~0 for Ethernet, sometimes 0 for
|
||||
* other uses, or the previous crc32 value if computing incrementally.
|
||||
* @p: pointer to buffer over which CRC is run
|
||||
* @len: length of buffer @p
|
||||
*/
|
||||
u32 crc32_le(u32 crc, unsigned char const *p, size_t len);
|
||||
|
||||
#if CRC_LE_BITS == 1
|
||||
/*
|
||||
* In fact, the table-based code will work in this case, but it can be
|
||||
* simplified by inlining the table in ?: form.
|
||||
*/
|
||||
|
||||
u32 crc32_le(u32 crc, unsigned char const *p, size_t len)
|
||||
{
|
||||
int i;
|
||||
while (len--) {
|
||||
crc ^= *p++;
|
||||
for (i = 0; i < 8; i++)
|
||||
crc = (crc >> 1) ^ ((crc & 1) ? CRCPOLY_LE : 0);
|
||||
}
|
||||
return crc;
|
||||
}
|
||||
#else /* Table-based approach */
|
||||
|
||||
u32 crc32_le(u32 crc, unsigned char const *p, size_t len)
|
||||
{
|
||||
# if CRC_LE_BITS == 8
|
||||
const u32 *b =(u32 *)p;
|
||||
const u32 *tab = crc32table_le;
|
||||
|
||||
# ifdef __LITTLE_ENDIAN
|
||||
# define DO_CRC(x) crc = tab[ (crc ^ (x)) & 255 ] ^ (crc>>8)
|
||||
# else
|
||||
# define DO_CRC(x) crc = tab[ ((crc >> 24) ^ (x)) & 255] ^ (crc<<8)
|
||||
# endif
|
||||
/* printf("Crc32_le crc=%x\n",crc); */
|
||||
crc = __cpu_to_le32(crc);
|
||||
/* Align it */
|
||||
if((((long)b)&3 && len)){
|
||||
do {
|
||||
u8 *p = (u8 *)b;
|
||||
DO_CRC(*p++);
|
||||
b = (void *)p;
|
||||
} while ((--len) && ((long)b)&3 );
|
||||
}
|
||||
if((len >= 4)){
|
||||
/* load data 32 bits wide, xor data 32 bits wide. */
|
||||
size_t save_len = len & 3;
|
||||
len = len >> 2;
|
||||
--b; /* use pre increment below(*++b) for speed */
|
||||
do {
|
||||
crc ^= *++b;
|
||||
DO_CRC(0);
|
||||
DO_CRC(0);
|
||||
DO_CRC(0);
|
||||
DO_CRC(0);
|
||||
} while (--len);
|
||||
b++; /* point to next byte(s) */
|
||||
len = save_len;
|
||||
}
|
||||
/* And the last few bytes */
|
||||
if(len){
|
||||
do {
|
||||
u8 *p = (u8 *)b;
|
||||
DO_CRC(*p++);
|
||||
b = (void *)p;
|
||||
} while (--len);
|
||||
}
|
||||
|
||||
return __le32_to_cpu(crc);
|
||||
#undef ENDIAN_SHIFT
|
||||
#undef DO_CRC
|
||||
|
||||
# elif CRC_LE_BITS == 4
|
||||
while (len--) {
|
||||
crc ^= *p++;
|
||||
crc = (crc >> 4) ^ crc32table_le[crc & 15];
|
||||
crc = (crc >> 4) ^ crc32table_le[crc & 15];
|
||||
}
|
||||
return crc;
|
||||
# elif CRC_LE_BITS == 2
|
||||
while (len--) {
|
||||
crc ^= *p++;
|
||||
crc = (crc >> 2) ^ crc32table_le[crc & 3];
|
||||
crc = (crc >> 2) ^ crc32table_le[crc & 3];
|
||||
crc = (crc >> 2) ^ crc32table_le[crc & 3];
|
||||
crc = (crc >> 2) ^ crc32table_le[crc & 3];
|
||||
}
|
||||
return crc;
|
||||
# endif
|
||||
}
|
||||
#endif
|
||||
#ifndef __UBOOT__
|
||||
/**
|
||||
* crc32_be() - Calculate bitwise big-endian Ethernet AUTODIN II CRC32
|
||||
* @crc: seed value for computation. ~0 for Ethernet, sometimes 0 for
|
||||
* other uses, or the previous crc32 value if computing incrementally.
|
||||
* @p: pointer to buffer over which CRC is run
|
||||
* @len: length of buffer @p
|
||||
*/
|
||||
u32 __attribute_pure__ crc32_be(u32 crc, unsigned char const *p, size_t len);
|
||||
|
||||
#if CRC_BE_BITS == 1
|
||||
/*
|
||||
* In fact, the table-based code will work in this case, but it can be
|
||||
* simplified by inlining the table in ?: form.
|
||||
*/
|
||||
|
||||
u32 __attribute_pure__ crc32_be(u32 crc, unsigned char const *p, size_t len)
|
||||
{
|
||||
int i;
|
||||
while (len--) {
|
||||
crc ^= *p++ << 24;
|
||||
for (i = 0; i < 8; i++)
|
||||
crc =
|
||||
(crc << 1) ^ ((crc & 0x80000000) ? CRCPOLY_BE :
|
||||
0);
|
||||
}
|
||||
return crc;
|
||||
}
|
||||
|
||||
#else /* Table-based approach */
|
||||
u32 __attribute_pure__ crc32_be(u32 crc, unsigned char const *p, size_t len)
|
||||
{
|
||||
# if CRC_BE_BITS == 8
|
||||
const u32 *b =(u32 *)p;
|
||||
const u32 *tab = crc32table_be;
|
||||
|
||||
# ifdef __LITTLE_ENDIAN
|
||||
# define DO_CRC(x) crc = tab[ (crc ^ (x)) & 255 ] ^ (crc>>8)
|
||||
# else
|
||||
# define DO_CRC(x) crc = tab[ ((crc >> 24) ^ (x)) & 255] ^ (crc<<8)
|
||||
# endif
|
||||
|
||||
crc = __cpu_to_be32(crc);
|
||||
/* Align it */
|
||||
if(unlikely(((long)b)&3 && len)){
|
||||
do {
|
||||
u8 *p = (u8 *)b;
|
||||
DO_CRC(*p++);
|
||||
b = (u32 *)p;
|
||||
} while ((--len) && ((long)b)&3 );
|
||||
}
|
||||
if(likely(len >= 4)){
|
||||
/* load data 32 bits wide, xor data 32 bits wide. */
|
||||
size_t save_len = len & 3;
|
||||
len = len >> 2;
|
||||
--b; /* use pre increment below(*++b) for speed */
|
||||
do {
|
||||
crc ^= *++b;
|
||||
DO_CRC(0);
|
||||
DO_CRC(0);
|
||||
DO_CRC(0);
|
||||
DO_CRC(0);
|
||||
} while (--len);
|
||||
b++; /* point to next byte(s) */
|
||||
len = save_len;
|
||||
}
|
||||
/* And the last few bytes */
|
||||
if(len){
|
||||
do {
|
||||
u8 *p = (u8 *)b;
|
||||
DO_CRC(*p++);
|
||||
b = (void *)p;
|
||||
} while (--len);
|
||||
}
|
||||
return __be32_to_cpu(crc);
|
||||
#undef ENDIAN_SHIFT
|
||||
#undef DO_CRC
|
||||
|
||||
# elif CRC_BE_BITS == 4
|
||||
while (len--) {
|
||||
crc ^= *p++ << 24;
|
||||
crc = (crc << 4) ^ crc32table_be[crc >> 28];
|
||||
crc = (crc << 4) ^ crc32table_be[crc >> 28];
|
||||
}
|
||||
return crc;
|
||||
# elif CRC_BE_BITS == 2
|
||||
while (len--) {
|
||||
crc ^= *p++ << 24;
|
||||
crc = (crc << 2) ^ crc32table_be[crc >> 30];
|
||||
crc = (crc << 2) ^ crc32table_be[crc >> 30];
|
||||
crc = (crc << 2) ^ crc32table_be[crc >> 30];
|
||||
crc = (crc << 2) ^ crc32table_be[crc >> 30];
|
||||
}
|
||||
return crc;
|
||||
# endif
|
||||
}
|
||||
#endif
|
||||
|
||||
EXPORT_SYMBOL(crc32_le);
|
||||
EXPORT_SYMBOL(crc32_be);
|
||||
#endif
|
||||
/*
|
||||
* A brief CRC tutorial.
|
||||
*
|
||||
* A CRC is a long-division remainder. You add the CRC to the message,
|
||||
* and the whole thing (message+CRC) is a multiple of the given
|
||||
* CRC polynomial. To check the CRC, you can either check that the
|
||||
* CRC matches the recomputed value, *or* you can check that the
|
||||
* remainder computed on the message+CRC is 0. This latter approach
|
||||
* is used by a lot of hardware implementations, and is why so many
|
||||
* protocols put the end-of-frame flag after the CRC.
|
||||
*
|
||||
* It's actually the same long division you learned in school, except that
|
||||
* - We're working in binary, so the digits are only 0 and 1, and
|
||||
* - When dividing polynomials, there are no carries. Rather than add and
|
||||
* subtract, we just xor. Thus, we tend to get a bit sloppy about
|
||||
* the difference between adding and subtracting.
|
||||
*
|
||||
* A 32-bit CRC polynomial is actually 33 bits long. But since it's
|
||||
* 33 bits long, bit 32 is always going to be set, so usually the CRC
|
||||
* is written in hex with the most significant bit omitted. (If you're
|
||||
* familiar with the IEEE 754 floating-point format, it's the same idea.)
|
||||
*
|
||||
* Note that a CRC is computed over a string of *bits*, so you have
|
||||
* to decide on the endianness of the bits within each byte. To get
|
||||
* the best error-detecting properties, this should correspond to the
|
||||
* order they're actually sent. For example, standard RS-232 serial is
|
||||
* little-endian; the most significant bit (sometimes used for parity)
|
||||
* is sent last. And when appending a CRC word to a message, you should
|
||||
* do it in the right order, matching the endianness.
|
||||
*
|
||||
* Just like with ordinary division, the remainder is always smaller than
|
||||
* the divisor (the CRC polynomial) you're dividing by. Each step of the
|
||||
* division, you take one more digit (bit) of the dividend and append it
|
||||
* to the current remainder. Then you figure out the appropriate multiple
|
||||
* of the divisor to subtract to being the remainder back into range.
|
||||
* In binary, it's easy - it has to be either 0 or 1, and to make the
|
||||
* XOR cancel, it's just a copy of bit 32 of the remainder.
|
||||
*
|
||||
* When computing a CRC, we don't care about the quotient, so we can
|
||||
* throw the quotient bit away, but subtract the appropriate multiple of
|
||||
* the polynomial from the remainder and we're back to where we started,
|
||||
* ready to process the next bit.
|
||||
*
|
||||
* A big-endian CRC written this way would be coded like:
|
||||
* for (i = 0; i < input_bits; i++) {
|
||||
* multiple = remainder & 0x80000000 ? CRCPOLY : 0;
|
||||
* remainder = (remainder << 1 | next_input_bit()) ^ multiple;
|
||||
* }
|
||||
* Notice how, to get at bit 32 of the shifted remainder, we look
|
||||
* at bit 31 of the remainder *before* shifting it.
|
||||
*
|
||||
* But also notice how the next_input_bit() bits we're shifting into
|
||||
* the remainder don't actually affect any decision-making until
|
||||
* 32 bits later. Thus, the first 32 cycles of this are pretty boring.
|
||||
* Also, to add the CRC to a message, we need a 32-bit-long hole for it at
|
||||
* the end, so we have to add 32 extra cycles shifting in zeros at the
|
||||
* end of every message,
|
||||
*
|
||||
* So the standard trick is to rearrage merging in the next_input_bit()
|
||||
* until the moment it's needed. Then the first 32 cycles can be precomputed,
|
||||
* and merging in the final 32 zero bits to make room for the CRC can be
|
||||
* skipped entirely.
|
||||
* This changes the code to:
|
||||
* for (i = 0; i < input_bits; i++) {
|
||||
* remainder ^= next_input_bit() << 31;
|
||||
* multiple = (remainder & 0x80000000) ? CRCPOLY : 0;
|
||||
* remainder = (remainder << 1) ^ multiple;
|
||||
* }
|
||||
* With this optimization, the little-endian code is simpler:
|
||||
* for (i = 0; i < input_bits; i++) {
|
||||
* remainder ^= next_input_bit();
|
||||
* multiple = (remainder & 1) ? CRCPOLY : 0;
|
||||
* remainder = (remainder >> 1) ^ multiple;
|
||||
* }
|
||||
*
|
||||
* Note that the other details of endianness have been hidden in CRCPOLY
|
||||
* (which must be bit-reversed) and next_input_bit().
|
||||
*
|
||||
* However, as long as next_input_bit is returning the bits in a sensible
|
||||
* order, we can actually do the merging 8 or more bits at a time rather
|
||||
* than one bit at a time:
|
||||
* for (i = 0; i < input_bytes; i++) {
|
||||
* remainder ^= next_input_byte() << 24;
|
||||
* for (j = 0; j < 8; j++) {
|
||||
* multiple = (remainder & 0x80000000) ? CRCPOLY : 0;
|
||||
* remainder = (remainder << 1) ^ multiple;
|
||||
* }
|
||||
* }
|
||||
* Or in little-endian:
|
||||
* for (i = 0; i < input_bytes; i++) {
|
||||
* remainder ^= next_input_byte();
|
||||
* for (j = 0; j < 8; j++) {
|
||||
* multiple = (remainder & 1) ? CRCPOLY : 0;
|
||||
* remainder = (remainder << 1) ^ multiple;
|
||||
* }
|
||||
* }
|
||||
* If the input is a multiple of 32 bits, you can even XOR in a 32-bit
|
||||
* word at a time and increase the inner loop count to 32.
|
||||
*
|
||||
* You can also mix and match the two loop styles, for example doing the
|
||||
* bulk of a message byte-at-a-time and adding bit-at-a-time processing
|
||||
* for any fractional bytes at the end.
|
||||
*
|
||||
* The only remaining optimization is to the byte-at-a-time table method.
|
||||
* Here, rather than just shifting one bit of the remainder to decide
|
||||
* in the correct multiple to subtract, we can shift a byte at a time.
|
||||
* This produces a 40-bit (rather than a 33-bit) intermediate remainder,
|
||||
* but again the multiple of the polynomial to subtract depends only on
|
||||
* the high bits, the high 8 bits in this case.
|
||||
*
|
||||
* The multile we need in that case is the low 32 bits of a 40-bit
|
||||
* value whose high 8 bits are given, and which is a multiple of the
|
||||
* generator polynomial. This is simply the CRC-32 of the given
|
||||
* one-byte message.
|
||||
*
|
||||
* Two more details: normally, appending zero bits to a message which
|
||||
* is already a multiple of a polynomial produces a larger multiple of that
|
||||
* polynomial. To enable a CRC to detect this condition, it's common to
|
||||
* invert the CRC before appending it. This makes the remainder of the
|
||||
* message+crc come out not as zero, but some fixed non-zero value.
|
||||
*
|
||||
* The same problem applies to zero bits prepended to the message, and
|
||||
* a similar solution is used. Instead of starting with a remainder of
|
||||
* 0, an initial remainder of all ones is used. As long as you start
|
||||
* the same way on decoding, it doesn't make a difference.
|
||||
*/
|
||||
|
||||
#ifdef UNITTEST
|
||||
|
||||
#include <stdlib.h>
|
||||
#include <stdio.h>
|
||||
|
||||
#ifndef __UBOOT__
|
||||
static void
|
||||
buf_dump(char const *prefix, unsigned char const *buf, size_t len)
|
||||
{
|
||||
fputs(prefix, stdout);
|
||||
while (len--)
|
||||
printf(" %02x", *buf++);
|
||||
putchar('\n');
|
||||
|
||||
}
|
||||
#endif
|
||||
|
||||
static void bytereverse(unsigned char *buf, size_t len)
|
||||
{
|
||||
while (len--) {
|
||||
unsigned char x = bitrev8(*buf);
|
||||
*buf++ = x;
|
||||
}
|
||||
}
|
||||
|
||||
static void random_garbage(unsigned char *buf, size_t len)
|
||||
{
|
||||
while (len--)
|
||||
*buf++ = (unsigned char) random();
|
||||
}
|
||||
|
||||
#ifndef __UBOOT__
|
||||
static void store_le(u32 x, unsigned char *buf)
|
||||
{
|
||||
buf[0] = (unsigned char) x;
|
||||
buf[1] = (unsigned char) (x >> 8);
|
||||
buf[2] = (unsigned char) (x >> 16);
|
||||
buf[3] = (unsigned char) (x >> 24);
|
||||
}
|
||||
#endif
|
||||
|
||||
static void store_be(u32 x, unsigned char *buf)
|
||||
{
|
||||
buf[0] = (unsigned char) (x >> 24);
|
||||
buf[1] = (unsigned char) (x >> 16);
|
||||
buf[2] = (unsigned char) (x >> 8);
|
||||
buf[3] = (unsigned char) x;
|
||||
}
|
||||
|
||||
/*
|
||||
* This checks that CRC(buf + CRC(buf)) = 0, and that
|
||||
* CRC commutes with bit-reversal. This has the side effect
|
||||
* of bytewise bit-reversing the input buffer, and returns
|
||||
* the CRC of the reversed buffer.
|
||||
*/
|
||||
static u32 test_step(u32 init, unsigned char *buf, size_t len)
|
||||
{
|
||||
u32 crc1, crc2;
|
||||
size_t i;
|
||||
|
||||
crc1 = crc32_be(init, buf, len);
|
||||
store_be(crc1, buf + len);
|
||||
crc2 = crc32_be(init, buf, len + 4);
|
||||
if (crc2)
|
||||
printf("\nCRC cancellation fail: 0x%08x should be 0\n",
|
||||
crc2);
|
||||
|
||||
for (i = 0; i <= len + 4; i++) {
|
||||
crc2 = crc32_be(init, buf, i);
|
||||
crc2 = crc32_be(crc2, buf + i, len + 4 - i);
|
||||
if (crc2)
|
||||
printf("\nCRC split fail: 0x%08x\n", crc2);
|
||||
}
|
||||
|
||||
/* Now swap it around for the other test */
|
||||
|
||||
bytereverse(buf, len + 4);
|
||||
init = bitrev32(init);
|
||||
crc2 = bitrev32(crc1);
|
||||
if (crc1 != bitrev32(crc2))
|
||||
printf("\nBit reversal fail: 0x%08x -> 0x%08x -> 0x%08x\n",
|
||||
crc1, crc2, bitrev32(crc2));
|
||||
crc1 = crc32_le(init, buf, len);
|
||||
if (crc1 != crc2)
|
||||
printf("\nCRC endianness fail: 0x%08x != 0x%08x\n", crc1,
|
||||
crc2);
|
||||
crc2 = crc32_le(init, buf, len + 4);
|
||||
if (crc2)
|
||||
printf("\nCRC cancellation fail: 0x%08x should be 0\n",
|
||||
crc2);
|
||||
|
||||
for (i = 0; i <= len + 4; i++) {
|
||||
crc2 = crc32_le(init, buf, i);
|
||||
crc2 = crc32_le(crc2, buf + i, len + 4 - i);
|
||||
if (crc2)
|
||||
printf("\nCRC split fail: 0x%08x\n", crc2);
|
||||
}
|
||||
|
||||
return crc1;
|
||||
}
|
||||
|
||||
#define SIZE 64
|
||||
#define INIT1 0
|
||||
#define INIT2 0
|
||||
|
||||
int main(void)
|
||||
{
|
||||
unsigned char buf1[SIZE + 4];
|
||||
unsigned char buf2[SIZE + 4];
|
||||
unsigned char buf3[SIZE + 4];
|
||||
int i, j;
|
||||
u32 crc1, crc2, crc3;
|
||||
|
||||
for (i = 0; i <= SIZE; i++) {
|
||||
printf("\rTesting length %d...", i);
|
||||
fflush(stdout);
|
||||
random_garbage(buf1, i);
|
||||
random_garbage(buf2, i);
|
||||
for (j = 0; j < i; j++)
|
||||
buf3[j] = buf1[j] ^ buf2[j];
|
||||
|
||||
crc1 = test_step(INIT1, buf1, i);
|
||||
crc2 = test_step(INIT2, buf2, i);
|
||||
/* Now check that CRC(buf1 ^ buf2) = CRC(buf1) ^ CRC(buf2) */
|
||||
crc3 = test_step(INIT1 ^ INIT2, buf3, i);
|
||||
if (crc3 != (crc1 ^ crc2))
|
||||
printf("CRC XOR fail: 0x%08x != 0x%08x ^ 0x%08x\n",
|
||||
crc3, crc1, crc2);
|
||||
}
|
||||
printf("\nAll test complete. No failures expected.\n");
|
||||
return 0;
|
||||
}
|
||||
|
||||
#endif /* UNITTEST */
|
||||
32
u-boot/drivers/mtd/ubi/crc32defs.h
Normal file
32
u-boot/drivers/mtd/ubi/crc32defs.h
Normal file
@@ -0,0 +1,32 @@
|
||||
/*
|
||||
* There are multiple 16-bit CRC polynomials in common use, but this is
|
||||
* *the* standard CRC-32 polynomial, first popularized by Ethernet.
|
||||
* x^32+x^26+x^23+x^22+x^16+x^12+x^11+x^10+x^8+x^7+x^5+x^4+x^2+x^1+x^0
|
||||
*/
|
||||
#define CRCPOLY_LE 0xedb88320
|
||||
#define CRCPOLY_BE 0x04c11db7
|
||||
|
||||
/* How many bits at a time to use. Requires a table of 4<<CRC_xx_BITS bytes. */
|
||||
/* For less performance-sensitive, use 4 */
|
||||
#ifndef CRC_LE_BITS
|
||||
# define CRC_LE_BITS 8
|
||||
#endif
|
||||
#ifndef CRC_BE_BITS
|
||||
# define CRC_BE_BITS 8
|
||||
#endif
|
||||
|
||||
/*
|
||||
* Little-endian CRC computation. Used with serial bit streams sent
|
||||
* lsbit-first. Be sure to use cpu_to_le32() to append the computed CRC.
|
||||
*/
|
||||
#if CRC_LE_BITS > 8 || CRC_LE_BITS < 1 || CRC_LE_BITS & CRC_LE_BITS-1
|
||||
# error CRC_LE_BITS must be a power of 2 between 1 and 8
|
||||
#endif
|
||||
|
||||
/*
|
||||
* Big-endian CRC computation. Used with serial bit streams sent
|
||||
* msbit-first. Be sure to use cpu_to_be32() to append the computed CRC.
|
||||
*/
|
||||
#if CRC_BE_BITS > 8 || CRC_BE_BITS < 1 || CRC_BE_BITS & CRC_BE_BITS-1
|
||||
# error CRC_BE_BITS must be a power of 2 between 1 and 8
|
||||
#endif
|
||||
136
u-boot/drivers/mtd/ubi/crc32table.h
Normal file
136
u-boot/drivers/mtd/ubi/crc32table.h
Normal file
@@ -0,0 +1,136 @@
|
||||
/* this file is generated - do not edit */
|
||||
|
||||
static const u32 crc32table_le[] = {
|
||||
tole(0x00000000L), tole(0x77073096L), tole(0xee0e612cL), tole(0x990951baL),
|
||||
tole(0x076dc419L), tole(0x706af48fL), tole(0xe963a535L), tole(0x9e6495a3L),
|
||||
tole(0x0edb8832L), tole(0x79dcb8a4L), tole(0xe0d5e91eL), tole(0x97d2d988L),
|
||||
tole(0x09b64c2bL), tole(0x7eb17cbdL), tole(0xe7b82d07L), tole(0x90bf1d91L),
|
||||
tole(0x1db71064L), tole(0x6ab020f2L), tole(0xf3b97148L), tole(0x84be41deL),
|
||||
tole(0x1adad47dL), tole(0x6ddde4ebL), tole(0xf4d4b551L), tole(0x83d385c7L),
|
||||
tole(0x136c9856L), tole(0x646ba8c0L), tole(0xfd62f97aL), tole(0x8a65c9ecL),
|
||||
tole(0x14015c4fL), tole(0x63066cd9L), tole(0xfa0f3d63L), tole(0x8d080df5L),
|
||||
tole(0x3b6e20c8L), tole(0x4c69105eL), tole(0xd56041e4L), tole(0xa2677172L),
|
||||
tole(0x3c03e4d1L), tole(0x4b04d447L), tole(0xd20d85fdL), tole(0xa50ab56bL),
|
||||
tole(0x35b5a8faL), tole(0x42b2986cL), tole(0xdbbbc9d6L), tole(0xacbcf940L),
|
||||
tole(0x32d86ce3L), tole(0x45df5c75L), tole(0xdcd60dcfL), tole(0xabd13d59L),
|
||||
tole(0x26d930acL), tole(0x51de003aL), tole(0xc8d75180L), tole(0xbfd06116L),
|
||||
tole(0x21b4f4b5L), tole(0x56b3c423L), tole(0xcfba9599L), tole(0xb8bda50fL),
|
||||
tole(0x2802b89eL), tole(0x5f058808L), tole(0xc60cd9b2L), tole(0xb10be924L),
|
||||
tole(0x2f6f7c87L), tole(0x58684c11L), tole(0xc1611dabL), tole(0xb6662d3dL),
|
||||
tole(0x76dc4190L), tole(0x01db7106L), tole(0x98d220bcL), tole(0xefd5102aL),
|
||||
tole(0x71b18589L), tole(0x06b6b51fL), tole(0x9fbfe4a5L), tole(0xe8b8d433L),
|
||||
tole(0x7807c9a2L), tole(0x0f00f934L), tole(0x9609a88eL), tole(0xe10e9818L),
|
||||
tole(0x7f6a0dbbL), tole(0x086d3d2dL), tole(0x91646c97L), tole(0xe6635c01L),
|
||||
tole(0x6b6b51f4L), tole(0x1c6c6162L), tole(0x856530d8L), tole(0xf262004eL),
|
||||
tole(0x6c0695edL), tole(0x1b01a57bL), tole(0x8208f4c1L), tole(0xf50fc457L),
|
||||
tole(0x65b0d9c6L), tole(0x12b7e950L), tole(0x8bbeb8eaL), tole(0xfcb9887cL),
|
||||
tole(0x62dd1ddfL), tole(0x15da2d49L), tole(0x8cd37cf3L), tole(0xfbd44c65L),
|
||||
tole(0x4db26158L), tole(0x3ab551ceL), tole(0xa3bc0074L), tole(0xd4bb30e2L),
|
||||
tole(0x4adfa541L), tole(0x3dd895d7L), tole(0xa4d1c46dL), tole(0xd3d6f4fbL),
|
||||
tole(0x4369e96aL), tole(0x346ed9fcL), tole(0xad678846L), tole(0xda60b8d0L),
|
||||
tole(0x44042d73L), tole(0x33031de5L), tole(0xaa0a4c5fL), tole(0xdd0d7cc9L),
|
||||
tole(0x5005713cL), tole(0x270241aaL), tole(0xbe0b1010L), tole(0xc90c2086L),
|
||||
tole(0x5768b525L), tole(0x206f85b3L), tole(0xb966d409L), tole(0xce61e49fL),
|
||||
tole(0x5edef90eL), tole(0x29d9c998L), tole(0xb0d09822L), tole(0xc7d7a8b4L),
|
||||
tole(0x59b33d17L), tole(0x2eb40d81L), tole(0xb7bd5c3bL), tole(0xc0ba6cadL),
|
||||
tole(0xedb88320L), tole(0x9abfb3b6L), tole(0x03b6e20cL), tole(0x74b1d29aL),
|
||||
tole(0xead54739L), tole(0x9dd277afL), tole(0x04db2615L), tole(0x73dc1683L),
|
||||
tole(0xe3630b12L), tole(0x94643b84L), tole(0x0d6d6a3eL), tole(0x7a6a5aa8L),
|
||||
tole(0xe40ecf0bL), tole(0x9309ff9dL), tole(0x0a00ae27L), tole(0x7d079eb1L),
|
||||
tole(0xf00f9344L), tole(0x8708a3d2L), tole(0x1e01f268L), tole(0x6906c2feL),
|
||||
tole(0xf762575dL), tole(0x806567cbL), tole(0x196c3671L), tole(0x6e6b06e7L),
|
||||
tole(0xfed41b76L), tole(0x89d32be0L), tole(0x10da7a5aL), tole(0x67dd4accL),
|
||||
tole(0xf9b9df6fL), tole(0x8ebeeff9L), tole(0x17b7be43L), tole(0x60b08ed5L),
|
||||
tole(0xd6d6a3e8L), tole(0xa1d1937eL), tole(0x38d8c2c4L), tole(0x4fdff252L),
|
||||
tole(0xd1bb67f1L), tole(0xa6bc5767L), tole(0x3fb506ddL), tole(0x48b2364bL),
|
||||
tole(0xd80d2bdaL), tole(0xaf0a1b4cL), tole(0x36034af6L), tole(0x41047a60L),
|
||||
tole(0xdf60efc3L), tole(0xa867df55L), tole(0x316e8eefL), tole(0x4669be79L),
|
||||
tole(0xcb61b38cL), tole(0xbc66831aL), tole(0x256fd2a0L), tole(0x5268e236L),
|
||||
tole(0xcc0c7795L), tole(0xbb0b4703L), tole(0x220216b9L), tole(0x5505262fL),
|
||||
tole(0xc5ba3bbeL), tole(0xb2bd0b28L), tole(0x2bb45a92L), tole(0x5cb36a04L),
|
||||
tole(0xc2d7ffa7L), tole(0xb5d0cf31L), tole(0x2cd99e8bL), tole(0x5bdeae1dL),
|
||||
tole(0x9b64c2b0L), tole(0xec63f226L), tole(0x756aa39cL), tole(0x026d930aL),
|
||||
tole(0x9c0906a9L), tole(0xeb0e363fL), tole(0x72076785L), tole(0x05005713L),
|
||||
tole(0x95bf4a82L), tole(0xe2b87a14L), tole(0x7bb12baeL), tole(0x0cb61b38L),
|
||||
tole(0x92d28e9bL), tole(0xe5d5be0dL), tole(0x7cdcefb7L), tole(0x0bdbdf21L),
|
||||
tole(0x86d3d2d4L), tole(0xf1d4e242L), tole(0x68ddb3f8L), tole(0x1fda836eL),
|
||||
tole(0x81be16cdL), tole(0xf6b9265bL), tole(0x6fb077e1L), tole(0x18b74777L),
|
||||
tole(0x88085ae6L), tole(0xff0f6a70L), tole(0x66063bcaL), tole(0x11010b5cL),
|
||||
tole(0x8f659effL), tole(0xf862ae69L), tole(0x616bffd3L), tole(0x166ccf45L),
|
||||
tole(0xa00ae278L), tole(0xd70dd2eeL), tole(0x4e048354L), tole(0x3903b3c2L),
|
||||
tole(0xa7672661L), tole(0xd06016f7L), tole(0x4969474dL), tole(0x3e6e77dbL),
|
||||
tole(0xaed16a4aL), tole(0xd9d65adcL), tole(0x40df0b66L), tole(0x37d83bf0L),
|
||||
tole(0xa9bcae53L), tole(0xdebb9ec5L), tole(0x47b2cf7fL), tole(0x30b5ffe9L),
|
||||
tole(0xbdbdf21cL), tole(0xcabac28aL), tole(0x53b39330L), tole(0x24b4a3a6L),
|
||||
tole(0xbad03605L), tole(0xcdd70693L), tole(0x54de5729L), tole(0x23d967bfL),
|
||||
tole(0xb3667a2eL), tole(0xc4614ab8L), tole(0x5d681b02L), tole(0x2a6f2b94L),
|
||||
tole(0xb40bbe37L), tole(0xc30c8ea1L), tole(0x5a05df1bL), tole(0x2d02ef8dL)
|
||||
};
|
||||
#ifndef __UBOOT__
|
||||
static const u32 crc32table_be[] = {
|
||||
tobe(0x00000000L), tobe(0x04c11db7L), tobe(0x09823b6eL), tobe(0x0d4326d9L),
|
||||
tobe(0x130476dcL), tobe(0x17c56b6bL), tobe(0x1a864db2L), tobe(0x1e475005L),
|
||||
tobe(0x2608edb8L), tobe(0x22c9f00fL), tobe(0x2f8ad6d6L), tobe(0x2b4bcb61L),
|
||||
tobe(0x350c9b64L), tobe(0x31cd86d3L), tobe(0x3c8ea00aL), tobe(0x384fbdbdL),
|
||||
tobe(0x4c11db70L), tobe(0x48d0c6c7L), tobe(0x4593e01eL), tobe(0x4152fda9L),
|
||||
tobe(0x5f15adacL), tobe(0x5bd4b01bL), tobe(0x569796c2L), tobe(0x52568b75L),
|
||||
tobe(0x6a1936c8L), tobe(0x6ed82b7fL), tobe(0x639b0da6L), tobe(0x675a1011L),
|
||||
tobe(0x791d4014L), tobe(0x7ddc5da3L), tobe(0x709f7b7aL), tobe(0x745e66cdL),
|
||||
tobe(0x9823b6e0L), tobe(0x9ce2ab57L), tobe(0x91a18d8eL), tobe(0x95609039L),
|
||||
tobe(0x8b27c03cL), tobe(0x8fe6dd8bL), tobe(0x82a5fb52L), tobe(0x8664e6e5L),
|
||||
tobe(0xbe2b5b58L), tobe(0xbaea46efL), tobe(0xb7a96036L), tobe(0xb3687d81L),
|
||||
tobe(0xad2f2d84L), tobe(0xa9ee3033L), tobe(0xa4ad16eaL), tobe(0xa06c0b5dL),
|
||||
tobe(0xd4326d90L), tobe(0xd0f37027L), tobe(0xddb056feL), tobe(0xd9714b49L),
|
||||
tobe(0xc7361b4cL), tobe(0xc3f706fbL), tobe(0xceb42022L), tobe(0xca753d95L),
|
||||
tobe(0xf23a8028L), tobe(0xf6fb9d9fL), tobe(0xfbb8bb46L), tobe(0xff79a6f1L),
|
||||
tobe(0xe13ef6f4L), tobe(0xe5ffeb43L), tobe(0xe8bccd9aL), tobe(0xec7dd02dL),
|
||||
tobe(0x34867077L), tobe(0x30476dc0L), tobe(0x3d044b19L), tobe(0x39c556aeL),
|
||||
tobe(0x278206abL), tobe(0x23431b1cL), tobe(0x2e003dc5L), tobe(0x2ac12072L),
|
||||
tobe(0x128e9dcfL), tobe(0x164f8078L), tobe(0x1b0ca6a1L), tobe(0x1fcdbb16L),
|
||||
tobe(0x018aeb13L), tobe(0x054bf6a4L), tobe(0x0808d07dL), tobe(0x0cc9cdcaL),
|
||||
tobe(0x7897ab07L), tobe(0x7c56b6b0L), tobe(0x71159069L), tobe(0x75d48ddeL),
|
||||
tobe(0x6b93dddbL), tobe(0x6f52c06cL), tobe(0x6211e6b5L), tobe(0x66d0fb02L),
|
||||
tobe(0x5e9f46bfL), tobe(0x5a5e5b08L), tobe(0x571d7dd1L), tobe(0x53dc6066L),
|
||||
tobe(0x4d9b3063L), tobe(0x495a2dd4L), tobe(0x44190b0dL), tobe(0x40d816baL),
|
||||
tobe(0xaca5c697L), tobe(0xa864db20L), tobe(0xa527fdf9L), tobe(0xa1e6e04eL),
|
||||
tobe(0xbfa1b04bL), tobe(0xbb60adfcL), tobe(0xb6238b25L), tobe(0xb2e29692L),
|
||||
tobe(0x8aad2b2fL), tobe(0x8e6c3698L), tobe(0x832f1041L), tobe(0x87ee0df6L),
|
||||
tobe(0x99a95df3L), tobe(0x9d684044L), tobe(0x902b669dL), tobe(0x94ea7b2aL),
|
||||
tobe(0xe0b41de7L), tobe(0xe4750050L), tobe(0xe9362689L), tobe(0xedf73b3eL),
|
||||
tobe(0xf3b06b3bL), tobe(0xf771768cL), tobe(0xfa325055L), tobe(0xfef34de2L),
|
||||
tobe(0xc6bcf05fL), tobe(0xc27dede8L), tobe(0xcf3ecb31L), tobe(0xcbffd686L),
|
||||
tobe(0xd5b88683L), tobe(0xd1799b34L), tobe(0xdc3abdedL), tobe(0xd8fba05aL),
|
||||
tobe(0x690ce0eeL), tobe(0x6dcdfd59L), tobe(0x608edb80L), tobe(0x644fc637L),
|
||||
tobe(0x7a089632L), tobe(0x7ec98b85L), tobe(0x738aad5cL), tobe(0x774bb0ebL),
|
||||
tobe(0x4f040d56L), tobe(0x4bc510e1L), tobe(0x46863638L), tobe(0x42472b8fL),
|
||||
tobe(0x5c007b8aL), tobe(0x58c1663dL), tobe(0x558240e4L), tobe(0x51435d53L),
|
||||
tobe(0x251d3b9eL), tobe(0x21dc2629L), tobe(0x2c9f00f0L), tobe(0x285e1d47L),
|
||||
tobe(0x36194d42L), tobe(0x32d850f5L), tobe(0x3f9b762cL), tobe(0x3b5a6b9bL),
|
||||
tobe(0x0315d626L), tobe(0x07d4cb91L), tobe(0x0a97ed48L), tobe(0x0e56f0ffL),
|
||||
tobe(0x1011a0faL), tobe(0x14d0bd4dL), tobe(0x19939b94L), tobe(0x1d528623L),
|
||||
tobe(0xf12f560eL), tobe(0xf5ee4bb9L), tobe(0xf8ad6d60L), tobe(0xfc6c70d7L),
|
||||
tobe(0xe22b20d2L), tobe(0xe6ea3d65L), tobe(0xeba91bbcL), tobe(0xef68060bL),
|
||||
tobe(0xd727bbb6L), tobe(0xd3e6a601L), tobe(0xdea580d8L), tobe(0xda649d6fL),
|
||||
tobe(0xc423cd6aL), tobe(0xc0e2d0ddL), tobe(0xcda1f604L), tobe(0xc960ebb3L),
|
||||
tobe(0xbd3e8d7eL), tobe(0xb9ff90c9L), tobe(0xb4bcb610L), tobe(0xb07daba7L),
|
||||
tobe(0xae3afba2L), tobe(0xaafbe615L), tobe(0xa7b8c0ccL), tobe(0xa379dd7bL),
|
||||
tobe(0x9b3660c6L), tobe(0x9ff77d71L), tobe(0x92b45ba8L), tobe(0x9675461fL),
|
||||
tobe(0x8832161aL), tobe(0x8cf30badL), tobe(0x81b02d74L), tobe(0x857130c3L),
|
||||
tobe(0x5d8a9099L), tobe(0x594b8d2eL), tobe(0x5408abf7L), tobe(0x50c9b640L),
|
||||
tobe(0x4e8ee645L), tobe(0x4a4ffbf2L), tobe(0x470cdd2bL), tobe(0x43cdc09cL),
|
||||
tobe(0x7b827d21L), tobe(0x7f436096L), tobe(0x7200464fL), tobe(0x76c15bf8L),
|
||||
tobe(0x68860bfdL), tobe(0x6c47164aL), tobe(0x61043093L), tobe(0x65c52d24L),
|
||||
tobe(0x119b4be9L), tobe(0x155a565eL), tobe(0x18197087L), tobe(0x1cd86d30L),
|
||||
tobe(0x029f3d35L), tobe(0x065e2082L), tobe(0x0b1d065bL), tobe(0x0fdc1becL),
|
||||
tobe(0x3793a651L), tobe(0x3352bbe6L), tobe(0x3e119d3fL), tobe(0x3ad08088L),
|
||||
tobe(0x2497d08dL), tobe(0x2056cd3aL), tobe(0x2d15ebe3L), tobe(0x29d4f654L),
|
||||
tobe(0xc5a92679L), tobe(0xc1683bceL), tobe(0xcc2b1d17L), tobe(0xc8ea00a0L),
|
||||
tobe(0xd6ad50a5L), tobe(0xd26c4d12L), tobe(0xdf2f6bcbL), tobe(0xdbee767cL),
|
||||
tobe(0xe3a1cbc1L), tobe(0xe760d676L), tobe(0xea23f0afL), tobe(0xeee2ed18L),
|
||||
tobe(0xf0a5bd1dL), tobe(0xf464a0aaL), tobe(0xf9278673L), tobe(0xfde69bc4L),
|
||||
tobe(0x89b8fd09L), tobe(0x8d79e0beL), tobe(0x803ac667L), tobe(0x84fbdbd0L),
|
||||
tobe(0x9abc8bd5L), tobe(0x9e7d9662L), tobe(0x933eb0bbL), tobe(0x97ffad0cL),
|
||||
tobe(0xafb010b1L), tobe(0xab710d06L), tobe(0xa6322bdfL), tobe(0xa2f33668L),
|
||||
tobe(0xbcb4666dL), tobe(0xb8757bdaL), tobe(0xb5365d03L), tobe(0xb1f740b4L)
|
||||
};
|
||||
#endif
|
||||
560
u-boot/drivers/mtd/ubi/debug.c
Normal file
560
u-boot/drivers/mtd/ubi/debug.c
Normal file
@@ -0,0 +1,560 @@
|
||||
/*
|
||||
* Copyright (c) International Business Machines Corp., 2006
|
||||
*
|
||||
* SPDX-License-Identifier: GPL-2.0+
|
||||
*
|
||||
* Author: Artem Bityutskiy (Битюцкий Артём)
|
||||
*/
|
||||
|
||||
#include <ubi_uboot.h>
|
||||
#include "ubi.h"
|
||||
#ifndef __UBOOT__
|
||||
#include <linux/debugfs.h>
|
||||
#include <linux/uaccess.h>
|
||||
#include <linux/module.h>
|
||||
#endif
|
||||
|
||||
/**
|
||||
* ubi_dump_flash - dump a region of flash.
|
||||
* @ubi: UBI device description object
|
||||
* @pnum: the physical eraseblock number to dump
|
||||
* @offset: the starting offset within the physical eraseblock to dump
|
||||
* @len: the length of the region to dump
|
||||
*/
|
||||
void ubi_dump_flash(struct ubi_device *ubi, int pnum, int offset, int len)
|
||||
{
|
||||
int err;
|
||||
size_t read;
|
||||
void *buf;
|
||||
loff_t addr = (loff_t)pnum * ubi->peb_size + offset;
|
||||
|
||||
buf = vmalloc(len);
|
||||
if (!buf)
|
||||
return;
|
||||
err = mtd_read(ubi->mtd, addr, len, &read, buf);
|
||||
if (err && err != -EUCLEAN) {
|
||||
ubi_err(ubi, "err %d while reading %d bytes from PEB %d:%d, read %zd bytes",
|
||||
err, len, pnum, offset, read);
|
||||
goto out;
|
||||
}
|
||||
|
||||
ubi_msg(ubi, "dumping %d bytes of data from PEB %d, offset %d",
|
||||
len, pnum, offset);
|
||||
print_hex_dump(KERN_DEBUG, "", DUMP_PREFIX_OFFSET, 32, 1, buf, len, 1);
|
||||
out:
|
||||
vfree(buf);
|
||||
return;
|
||||
}
|
||||
|
||||
/**
|
||||
* ubi_dump_ec_hdr - dump an erase counter header.
|
||||
* @ec_hdr: the erase counter header to dump
|
||||
*/
|
||||
void ubi_dump_ec_hdr(const struct ubi_ec_hdr *ec_hdr)
|
||||
{
|
||||
pr_err("Erase counter header dump:\n");
|
||||
pr_err("\tmagic %#08x\n", be32_to_cpu(ec_hdr->magic));
|
||||
pr_err("\tversion %d\n", (int)ec_hdr->version);
|
||||
pr_err("\tec %llu\n", (long long)be64_to_cpu(ec_hdr->ec));
|
||||
pr_err("\tvid_hdr_offset %d\n", be32_to_cpu(ec_hdr->vid_hdr_offset));
|
||||
pr_err("\tdata_offset %d\n", be32_to_cpu(ec_hdr->data_offset));
|
||||
pr_err("\timage_seq %d\n", be32_to_cpu(ec_hdr->image_seq));
|
||||
pr_err("\thdr_crc %#08x\n", be32_to_cpu(ec_hdr->hdr_crc));
|
||||
pr_err("erase counter header hexdump:\n");
|
||||
print_hex_dump(KERN_DEBUG, "", DUMP_PREFIX_OFFSET, 32, 1,
|
||||
ec_hdr, UBI_EC_HDR_SIZE, 1);
|
||||
}
|
||||
|
||||
/**
|
||||
* ubi_dump_vid_hdr - dump a volume identifier header.
|
||||
* @vid_hdr: the volume identifier header to dump
|
||||
*/
|
||||
void ubi_dump_vid_hdr(const struct ubi_vid_hdr *vid_hdr)
|
||||
{
|
||||
pr_err("Volume identifier header dump:\n");
|
||||
pr_err("\tmagic %08x\n", be32_to_cpu(vid_hdr->magic));
|
||||
pr_err("\tversion %d\n", (int)vid_hdr->version);
|
||||
pr_err("\tvol_type %d\n", (int)vid_hdr->vol_type);
|
||||
pr_err("\tcopy_flag %d\n", (int)vid_hdr->copy_flag);
|
||||
pr_err("\tcompat %d\n", (int)vid_hdr->compat);
|
||||
pr_err("\tvol_id %d\n", be32_to_cpu(vid_hdr->vol_id));
|
||||
pr_err("\tlnum %d\n", be32_to_cpu(vid_hdr->lnum));
|
||||
pr_err("\tdata_size %d\n", be32_to_cpu(vid_hdr->data_size));
|
||||
pr_err("\tused_ebs %d\n", be32_to_cpu(vid_hdr->used_ebs));
|
||||
pr_err("\tdata_pad %d\n", be32_to_cpu(vid_hdr->data_pad));
|
||||
pr_err("\tsqnum %llu\n",
|
||||
(unsigned long long)be64_to_cpu(vid_hdr->sqnum));
|
||||
pr_err("\thdr_crc %08x\n", be32_to_cpu(vid_hdr->hdr_crc));
|
||||
pr_err("Volume identifier header hexdump:\n");
|
||||
print_hex_dump(KERN_DEBUG, "", DUMP_PREFIX_OFFSET, 32, 1,
|
||||
vid_hdr, UBI_VID_HDR_SIZE, 1);
|
||||
}
|
||||
|
||||
/**
|
||||
* ubi_dump_vol_info - dump volume information.
|
||||
* @vol: UBI volume description object
|
||||
*/
|
||||
void ubi_dump_vol_info(const struct ubi_volume *vol)
|
||||
{
|
||||
printf("Volume information dump:\n");
|
||||
printf("\tvol_id %d\n", vol->vol_id);
|
||||
printf("\treserved_pebs %d\n", vol->reserved_pebs);
|
||||
printf("\talignment %d\n", vol->alignment);
|
||||
printf("\tdata_pad %d\n", vol->data_pad);
|
||||
printf("\tvol_type %d\n", vol->vol_type);
|
||||
printf("\tname_len %d\n", vol->name_len);
|
||||
printf("\tusable_leb_size %d\n", vol->usable_leb_size);
|
||||
printf("\tused_ebs %d\n", vol->used_ebs);
|
||||
printf("\tused_bytes %lld\n", vol->used_bytes);
|
||||
printf("\tlast_eb_bytes %d\n", vol->last_eb_bytes);
|
||||
printf("\tcorrupted %d\n", vol->corrupted);
|
||||
printf("\tupd_marker %d\n", vol->upd_marker);
|
||||
|
||||
if (vol->name_len <= UBI_VOL_NAME_MAX &&
|
||||
strnlen(vol->name, vol->name_len + 1) == vol->name_len) {
|
||||
printf("\tname %s\n", vol->name);
|
||||
} else {
|
||||
printf("\t1st 5 characters of name: %c%c%c%c%c\n",
|
||||
vol->name[0], vol->name[1], vol->name[2],
|
||||
vol->name[3], vol->name[4]);
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* ubi_dump_vtbl_record - dump a &struct ubi_vtbl_record object.
|
||||
* @r: the object to dump
|
||||
* @idx: volume table index
|
||||
*/
|
||||
void ubi_dump_vtbl_record(const struct ubi_vtbl_record *r, int idx)
|
||||
{
|
||||
int name_len = be16_to_cpu(r->name_len);
|
||||
|
||||
pr_err("Volume table record %d dump:\n", idx);
|
||||
pr_err("\treserved_pebs %d\n", be32_to_cpu(r->reserved_pebs));
|
||||
pr_err("\talignment %d\n", be32_to_cpu(r->alignment));
|
||||
pr_err("\tdata_pad %d\n", be32_to_cpu(r->data_pad));
|
||||
pr_err("\tvol_type %d\n", (int)r->vol_type);
|
||||
pr_err("\tupd_marker %d\n", (int)r->upd_marker);
|
||||
pr_err("\tname_len %d\n", name_len);
|
||||
|
||||
if (r->name[0] == '\0') {
|
||||
pr_err("\tname NULL\n");
|
||||
return;
|
||||
}
|
||||
|
||||
if (name_len <= UBI_VOL_NAME_MAX &&
|
||||
strnlen(&r->name[0], name_len + 1) == name_len) {
|
||||
pr_err("\tname %s\n", &r->name[0]);
|
||||
} else {
|
||||
pr_err("\t1st 5 characters of name: %c%c%c%c%c\n",
|
||||
r->name[0], r->name[1], r->name[2], r->name[3],
|
||||
r->name[4]);
|
||||
}
|
||||
pr_err("\tcrc %#08x\n", be32_to_cpu(r->crc));
|
||||
}
|
||||
|
||||
/**
|
||||
* ubi_dump_av - dump a &struct ubi_ainf_volume object.
|
||||
* @av: the object to dump
|
||||
*/
|
||||
void ubi_dump_av(const struct ubi_ainf_volume *av)
|
||||
{
|
||||
pr_err("Volume attaching information dump:\n");
|
||||
pr_err("\tvol_id %d\n", av->vol_id);
|
||||
pr_err("\thighest_lnum %d\n", av->highest_lnum);
|
||||
pr_err("\tleb_count %d\n", av->leb_count);
|
||||
pr_err("\tcompat %d\n", av->compat);
|
||||
pr_err("\tvol_type %d\n", av->vol_type);
|
||||
pr_err("\tused_ebs %d\n", av->used_ebs);
|
||||
pr_err("\tlast_data_size %d\n", av->last_data_size);
|
||||
pr_err("\tdata_pad %d\n", av->data_pad);
|
||||
}
|
||||
|
||||
/**
|
||||
* ubi_dump_aeb - dump a &struct ubi_ainf_peb object.
|
||||
* @aeb: the object to dump
|
||||
* @type: object type: 0 - not corrupted, 1 - corrupted
|
||||
*/
|
||||
void ubi_dump_aeb(const struct ubi_ainf_peb *aeb, int type)
|
||||
{
|
||||
pr_err("eraseblock attaching information dump:\n");
|
||||
pr_err("\tec %d\n", aeb->ec);
|
||||
pr_err("\tpnum %d\n", aeb->pnum);
|
||||
if (type == 0) {
|
||||
pr_err("\tlnum %d\n", aeb->lnum);
|
||||
pr_err("\tscrub %d\n", aeb->scrub);
|
||||
pr_err("\tsqnum %llu\n", aeb->sqnum);
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* ubi_dump_mkvol_req - dump a &struct ubi_mkvol_req object.
|
||||
* @req: the object to dump
|
||||
*/
|
||||
void ubi_dump_mkvol_req(const struct ubi_mkvol_req *req)
|
||||
{
|
||||
char nm[17];
|
||||
|
||||
pr_err("Volume creation request dump:\n");
|
||||
pr_err("\tvol_id %d\n", req->vol_id);
|
||||
pr_err("\talignment %d\n", req->alignment);
|
||||
pr_err("\tbytes %lld\n", (long long)req->bytes);
|
||||
pr_err("\tvol_type %d\n", req->vol_type);
|
||||
pr_err("\tname_len %d\n", req->name_len);
|
||||
|
||||
memcpy(nm, req->name, 16);
|
||||
nm[16] = 0;
|
||||
pr_err("\t1st 16 characters of name: %s\n", nm);
|
||||
}
|
||||
|
||||
#ifndef __UBOOT__
|
||||
/*
|
||||
* Root directory for UBI stuff in debugfs. Contains sub-directories which
|
||||
* contain the stuff specific to particular UBI devices.
|
||||
*/
|
||||
static struct dentry *dfs_rootdir;
|
||||
|
||||
/**
|
||||
* ubi_debugfs_init - create UBI debugfs directory.
|
||||
*
|
||||
* Create UBI debugfs directory. Returns zero in case of success and a negative
|
||||
* error code in case of failure.
|
||||
*/
|
||||
int ubi_debugfs_init(void)
|
||||
{
|
||||
if (!IS_ENABLED(CONFIG_DEBUG_FS))
|
||||
return 0;
|
||||
|
||||
dfs_rootdir = debugfs_create_dir("ubi", NULL);
|
||||
if (IS_ERR_OR_NULL(dfs_rootdir)) {
|
||||
int err = dfs_rootdir ? -ENODEV : PTR_ERR(dfs_rootdir);
|
||||
|
||||
pr_err("UBI error: cannot create \"ubi\" debugfs directory, error %d\n",
|
||||
err);
|
||||
return err;
|
||||
}
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
||||
/**
|
||||
* ubi_debugfs_exit - remove UBI debugfs directory.
|
||||
*/
|
||||
void ubi_debugfs_exit(void)
|
||||
{
|
||||
if (IS_ENABLED(CONFIG_DEBUG_FS))
|
||||
debugfs_remove(dfs_rootdir);
|
||||
}
|
||||
|
||||
/* Read an UBI debugfs file */
|
||||
static ssize_t dfs_file_read(struct file *file, char __user *user_buf,
|
||||
size_t count, loff_t *ppos)
|
||||
{
|
||||
unsigned long ubi_num = (unsigned long)file->private_data;
|
||||
struct dentry *dent = file->f_path.dentry;
|
||||
struct ubi_device *ubi;
|
||||
struct ubi_debug_info *d;
|
||||
char buf[8];
|
||||
int val;
|
||||
|
||||
ubi = ubi_get_device(ubi_num);
|
||||
if (!ubi)
|
||||
return -ENODEV;
|
||||
d = &ubi->dbg;
|
||||
|
||||
if (dent == d->dfs_chk_gen)
|
||||
val = d->chk_gen;
|
||||
else if (dent == d->dfs_chk_io)
|
||||
val = d->chk_io;
|
||||
else if (dent == d->dfs_chk_fastmap)
|
||||
val = d->chk_fastmap;
|
||||
else if (dent == d->dfs_disable_bgt)
|
||||
val = d->disable_bgt;
|
||||
else if (dent == d->dfs_emulate_bitflips)
|
||||
val = d->emulate_bitflips;
|
||||
else if (dent == d->dfs_emulate_io_failures)
|
||||
val = d->emulate_io_failures;
|
||||
else if (dent == d->dfs_emulate_power_cut) {
|
||||
snprintf(buf, sizeof(buf), "%u\n", d->emulate_power_cut);
|
||||
count = simple_read_from_buffer(user_buf, count, ppos,
|
||||
buf, strlen(buf));
|
||||
goto out;
|
||||
} else if (dent == d->dfs_power_cut_min) {
|
||||
snprintf(buf, sizeof(buf), "%u\n", d->power_cut_min);
|
||||
count = simple_read_from_buffer(user_buf, count, ppos,
|
||||
buf, strlen(buf));
|
||||
goto out;
|
||||
} else if (dent == d->dfs_power_cut_max) {
|
||||
snprintf(buf, sizeof(buf), "%u\n", d->power_cut_max);
|
||||
count = simple_read_from_buffer(user_buf, count, ppos,
|
||||
buf, strlen(buf));
|
||||
goto out;
|
||||
}
|
||||
else {
|
||||
count = -EINVAL;
|
||||
goto out;
|
||||
}
|
||||
|
||||
if (val)
|
||||
buf[0] = '1';
|
||||
else
|
||||
buf[0] = '0';
|
||||
buf[1] = '\n';
|
||||
buf[2] = 0x00;
|
||||
|
||||
count = simple_read_from_buffer(user_buf, count, ppos, buf, 2);
|
||||
|
||||
out:
|
||||
ubi_put_device(ubi);
|
||||
return count;
|
||||
}
|
||||
|
||||
/* Write an UBI debugfs file */
|
||||
static ssize_t dfs_file_write(struct file *file, const char __user *user_buf,
|
||||
size_t count, loff_t *ppos)
|
||||
{
|
||||
unsigned long ubi_num = (unsigned long)file->private_data;
|
||||
struct dentry *dent = file->f_path.dentry;
|
||||
struct ubi_device *ubi;
|
||||
struct ubi_debug_info *d;
|
||||
size_t buf_size;
|
||||
char buf[8] = {0};
|
||||
int val;
|
||||
|
||||
ubi = ubi_get_device(ubi_num);
|
||||
if (!ubi)
|
||||
return -ENODEV;
|
||||
d = &ubi->dbg;
|
||||
|
||||
buf_size = min_t(size_t, count, (sizeof(buf) - 1));
|
||||
if (copy_from_user(buf, user_buf, buf_size)) {
|
||||
count = -EFAULT;
|
||||
goto out;
|
||||
}
|
||||
|
||||
if (dent == d->dfs_power_cut_min) {
|
||||
if (kstrtouint(buf, 0, &d->power_cut_min) != 0)
|
||||
count = -EINVAL;
|
||||
goto out;
|
||||
} else if (dent == d->dfs_power_cut_max) {
|
||||
if (kstrtouint(buf, 0, &d->power_cut_max) != 0)
|
||||
count = -EINVAL;
|
||||
goto out;
|
||||
} else if (dent == d->dfs_emulate_power_cut) {
|
||||
if (kstrtoint(buf, 0, &val) != 0)
|
||||
count = -EINVAL;
|
||||
d->emulate_power_cut = val;
|
||||
goto out;
|
||||
}
|
||||
|
||||
if (buf[0] == '1')
|
||||
val = 1;
|
||||
else if (buf[0] == '0')
|
||||
val = 0;
|
||||
else {
|
||||
count = -EINVAL;
|
||||
goto out;
|
||||
}
|
||||
|
||||
if (dent == d->dfs_chk_gen)
|
||||
d->chk_gen = val;
|
||||
else if (dent == d->dfs_chk_io)
|
||||
d->chk_io = val;
|
||||
else if (dent == d->dfs_chk_fastmap)
|
||||
d->chk_fastmap = val;
|
||||
else if (dent == d->dfs_disable_bgt)
|
||||
d->disable_bgt = val;
|
||||
else if (dent == d->dfs_emulate_bitflips)
|
||||
d->emulate_bitflips = val;
|
||||
else if (dent == d->dfs_emulate_io_failures)
|
||||
d->emulate_io_failures = val;
|
||||
else
|
||||
count = -EINVAL;
|
||||
|
||||
out:
|
||||
ubi_put_device(ubi);
|
||||
return count;
|
||||
}
|
||||
|
||||
/* File operations for all UBI debugfs files */
|
||||
static const struct file_operations dfs_fops = {
|
||||
.read = dfs_file_read,
|
||||
.write = dfs_file_write,
|
||||
.open = simple_open,
|
||||
.llseek = no_llseek,
|
||||
.owner = THIS_MODULE,
|
||||
};
|
||||
|
||||
/**
|
||||
* ubi_debugfs_init_dev - initialize debugfs for an UBI device.
|
||||
* @ubi: UBI device description object
|
||||
*
|
||||
* This function creates all debugfs files for UBI device @ubi. Returns zero in
|
||||
* case of success and a negative error code in case of failure.
|
||||
*/
|
||||
int ubi_debugfs_init_dev(struct ubi_device *ubi)
|
||||
{
|
||||
int err, n;
|
||||
unsigned long ubi_num = ubi->ubi_num;
|
||||
const char *fname;
|
||||
struct dentry *dent;
|
||||
struct ubi_debug_info *d = &ubi->dbg;
|
||||
|
||||
if (!IS_ENABLED(CONFIG_DEBUG_FS))
|
||||
return 0;
|
||||
|
||||
n = snprintf(d->dfs_dir_name, UBI_DFS_DIR_LEN + 1, UBI_DFS_DIR_NAME,
|
||||
ubi->ubi_num);
|
||||
if (n == UBI_DFS_DIR_LEN) {
|
||||
/* The array size is too small */
|
||||
fname = UBI_DFS_DIR_NAME;
|
||||
dent = ERR_PTR(-EINVAL);
|
||||
goto out;
|
||||
}
|
||||
|
||||
fname = d->dfs_dir_name;
|
||||
dent = debugfs_create_dir(fname, dfs_rootdir);
|
||||
if (IS_ERR_OR_NULL(dent))
|
||||
goto out;
|
||||
d->dfs_dir = dent;
|
||||
|
||||
fname = "chk_gen";
|
||||
dent = debugfs_create_file(fname, S_IWUSR, d->dfs_dir, (void *)ubi_num,
|
||||
&dfs_fops);
|
||||
if (IS_ERR_OR_NULL(dent))
|
||||
goto out_remove;
|
||||
d->dfs_chk_gen = dent;
|
||||
|
||||
fname = "chk_io";
|
||||
dent = debugfs_create_file(fname, S_IWUSR, d->dfs_dir, (void *)ubi_num,
|
||||
&dfs_fops);
|
||||
if (IS_ERR_OR_NULL(dent))
|
||||
goto out_remove;
|
||||
d->dfs_chk_io = dent;
|
||||
|
||||
fname = "chk_fastmap";
|
||||
dent = debugfs_create_file(fname, S_IWUSR, d->dfs_dir, (void *)ubi_num,
|
||||
&dfs_fops);
|
||||
if (IS_ERR_OR_NULL(dent))
|
||||
goto out_remove;
|
||||
d->dfs_chk_fastmap = dent;
|
||||
|
||||
fname = "tst_disable_bgt";
|
||||
dent = debugfs_create_file(fname, S_IWUSR, d->dfs_dir, (void *)ubi_num,
|
||||
&dfs_fops);
|
||||
if (IS_ERR_OR_NULL(dent))
|
||||
goto out_remove;
|
||||
d->dfs_disable_bgt = dent;
|
||||
|
||||
fname = "tst_emulate_bitflips";
|
||||
dent = debugfs_create_file(fname, S_IWUSR, d->dfs_dir, (void *)ubi_num,
|
||||
&dfs_fops);
|
||||
if (IS_ERR_OR_NULL(dent))
|
||||
goto out_remove;
|
||||
d->dfs_emulate_bitflips = dent;
|
||||
|
||||
fname = "tst_emulate_io_failures";
|
||||
dent = debugfs_create_file(fname, S_IWUSR, d->dfs_dir, (void *)ubi_num,
|
||||
&dfs_fops);
|
||||
if (IS_ERR_OR_NULL(dent))
|
||||
goto out_remove;
|
||||
d->dfs_emulate_io_failures = dent;
|
||||
|
||||
fname = "tst_emulate_power_cut";
|
||||
dent = debugfs_create_file(fname, S_IWUSR, d->dfs_dir, (void *)ubi_num,
|
||||
&dfs_fops);
|
||||
if (IS_ERR_OR_NULL(dent))
|
||||
goto out_remove;
|
||||
d->dfs_emulate_power_cut = dent;
|
||||
|
||||
fname = "tst_emulate_power_cut_min";
|
||||
dent = debugfs_create_file(fname, S_IWUSR, d->dfs_dir, (void *)ubi_num,
|
||||
&dfs_fops);
|
||||
if (IS_ERR_OR_NULL(dent))
|
||||
goto out_remove;
|
||||
d->dfs_power_cut_min = dent;
|
||||
|
||||
fname = "tst_emulate_power_cut_max";
|
||||
dent = debugfs_create_file(fname, S_IWUSR, d->dfs_dir, (void *)ubi_num,
|
||||
&dfs_fops);
|
||||
if (IS_ERR_OR_NULL(dent))
|
||||
goto out_remove;
|
||||
d->dfs_power_cut_max = dent;
|
||||
|
||||
return 0;
|
||||
|
||||
out_remove:
|
||||
debugfs_remove_recursive(d->dfs_dir);
|
||||
out:
|
||||
err = dent ? PTR_ERR(dent) : -ENODEV;
|
||||
ubi_err(ubi, "cannot create \"%s\" debugfs file or directory, error %d\n",
|
||||
fname, err);
|
||||
return err;
|
||||
}
|
||||
|
||||
/**
|
||||
* dbg_debug_exit_dev - free all debugfs files corresponding to device @ubi
|
||||
* @ubi: UBI device description object
|
||||
*/
|
||||
void ubi_debugfs_exit_dev(struct ubi_device *ubi)
|
||||
{
|
||||
if (IS_ENABLED(CONFIG_DEBUG_FS))
|
||||
debugfs_remove_recursive(ubi->dbg.dfs_dir);
|
||||
}
|
||||
|
||||
/**
|
||||
* ubi_dbg_power_cut - emulate a power cut if it is time to do so
|
||||
* @ubi: UBI device description object
|
||||
* @caller: Flags set to indicate from where the function is being called
|
||||
*
|
||||
* Returns non-zero if a power cut was emulated, zero if not.
|
||||
*/
|
||||
int ubi_dbg_power_cut(struct ubi_device *ubi, int caller)
|
||||
{
|
||||
unsigned int range;
|
||||
|
||||
if ((ubi->dbg.emulate_power_cut & caller) == 0)
|
||||
return 0;
|
||||
|
||||
if (ubi->dbg.power_cut_counter == 0) {
|
||||
ubi->dbg.power_cut_counter = ubi->dbg.power_cut_min;
|
||||
|
||||
if (ubi->dbg.power_cut_max > ubi->dbg.power_cut_min) {
|
||||
range = ubi->dbg.power_cut_max - ubi->dbg.power_cut_min;
|
||||
ubi->dbg.power_cut_counter += prandom_u32() % range;
|
||||
}
|
||||
return 0;
|
||||
}
|
||||
|
||||
ubi->dbg.power_cut_counter--;
|
||||
if (ubi->dbg.power_cut_counter)
|
||||
return 0;
|
||||
|
||||
ubi_msg(ubi, "XXXXXXXXXXXXXXX emulating a power cut XXXXXXXXXXXXXXXX");
|
||||
ubi_ro_mode(ubi);
|
||||
return 1;
|
||||
}
|
||||
#else
|
||||
int ubi_debugfs_init(void)
|
||||
{
|
||||
return 0;
|
||||
}
|
||||
|
||||
void ubi_debugfs_exit(void)
|
||||
{
|
||||
}
|
||||
|
||||
int ubi_debugfs_init_dev(struct ubi_device *ubi)
|
||||
{
|
||||
return 0;
|
||||
}
|
||||
|
||||
void ubi_debugfs_exit_dev(struct ubi_device *ubi)
|
||||
{
|
||||
}
|
||||
|
||||
int ubi_dbg_power_cut(struct ubi_device *ubi, int caller)
|
||||
{
|
||||
return 0;
|
||||
}
|
||||
#endif
|
||||
132
u-boot/drivers/mtd/ubi/debug.h
Normal file
132
u-boot/drivers/mtd/ubi/debug.h
Normal file
@@ -0,0 +1,132 @@
|
||||
/*
|
||||
* Copyright (c) International Business Machines Corp., 2006
|
||||
*
|
||||
* SPDX-License-Identifier: GPL-2.0+
|
||||
*
|
||||
* Author: Artem Bityutskiy (Битюцкий Артём)
|
||||
*/
|
||||
|
||||
#ifndef __UBI_DEBUG_H__
|
||||
#define __UBI_DEBUG_H__
|
||||
|
||||
void ubi_dump_flash(struct ubi_device *ubi, int pnum, int offset, int len);
|
||||
void ubi_dump_ec_hdr(const struct ubi_ec_hdr *ec_hdr);
|
||||
void ubi_dump_vid_hdr(const struct ubi_vid_hdr *vid_hdr);
|
||||
|
||||
#ifndef __UBOOT__
|
||||
#include <linux/random.h>
|
||||
#endif
|
||||
|
||||
#define ubi_assert(expr) do { \
|
||||
if (unlikely(!(expr))) { \
|
||||
pr_crit("UBI assert failed in %s at %u (pid %d)\n", \
|
||||
__func__, __LINE__, current->pid); \
|
||||
dump_stack(); \
|
||||
} \
|
||||
} while (0)
|
||||
|
||||
#define ubi_dbg_print_hex_dump(l, ps, pt, r, g, b, len, a) \
|
||||
print_hex_dump(l, ps, pt, r, g, b, len, a)
|
||||
|
||||
#define ubi_dbg_msg(type, fmt, ...) \
|
||||
pr_debug("UBI DBG " type " (pid %d): " fmt "\n", current->pid, \
|
||||
##__VA_ARGS__)
|
||||
|
||||
/* General debugging messages */
|
||||
#define dbg_gen(fmt, ...) ubi_dbg_msg("gen", fmt, ##__VA_ARGS__)
|
||||
/* Messages from the eraseblock association sub-system */
|
||||
#define dbg_eba(fmt, ...) ubi_dbg_msg("eba", fmt, ##__VA_ARGS__)
|
||||
/* Messages from the wear-leveling sub-system */
|
||||
#define dbg_wl(fmt, ...) ubi_dbg_msg("wl", fmt, ##__VA_ARGS__)
|
||||
/* Messages from the input/output sub-system */
|
||||
#define dbg_io(fmt, ...) ubi_dbg_msg("io", fmt, ##__VA_ARGS__)
|
||||
/* Initialization and build messages */
|
||||
#define dbg_bld(fmt, ...) ubi_dbg_msg("bld", fmt, ##__VA_ARGS__)
|
||||
|
||||
void ubi_dump_vol_info(const struct ubi_volume *vol);
|
||||
void ubi_dump_vtbl_record(const struct ubi_vtbl_record *r, int idx);
|
||||
void ubi_dump_av(const struct ubi_ainf_volume *av);
|
||||
void ubi_dump_aeb(const struct ubi_ainf_peb *aeb, int type);
|
||||
void ubi_dump_mkvol_req(const struct ubi_mkvol_req *req);
|
||||
int ubi_self_check_all_ff(struct ubi_device *ubi, int pnum, int offset,
|
||||
int len);
|
||||
int ubi_debugfs_init(void);
|
||||
void ubi_debugfs_exit(void);
|
||||
int ubi_debugfs_init_dev(struct ubi_device *ubi);
|
||||
void ubi_debugfs_exit_dev(struct ubi_device *ubi);
|
||||
|
||||
/**
|
||||
* ubi_dbg_is_bgt_disabled - if the background thread is disabled.
|
||||
* @ubi: UBI device description object
|
||||
*
|
||||
* Returns non-zero if the UBI background thread is disabled for testing
|
||||
* purposes.
|
||||
*/
|
||||
static inline int ubi_dbg_is_bgt_disabled(const struct ubi_device *ubi)
|
||||
{
|
||||
return ubi->dbg.disable_bgt;
|
||||
}
|
||||
|
||||
/**
|
||||
* ubi_dbg_is_bitflip - if it is time to emulate a bit-flip.
|
||||
* @ubi: UBI device description object
|
||||
*
|
||||
* Returns non-zero if a bit-flip should be emulated, otherwise returns zero.
|
||||
*/
|
||||
static inline int ubi_dbg_is_bitflip(const struct ubi_device *ubi)
|
||||
{
|
||||
if (ubi->dbg.emulate_bitflips)
|
||||
return !(prandom_u32() % 200);
|
||||
return 0;
|
||||
}
|
||||
|
||||
/**
|
||||
* ubi_dbg_is_write_failure - if it is time to emulate a write failure.
|
||||
* @ubi: UBI device description object
|
||||
*
|
||||
* Returns non-zero if a write failure should be emulated, otherwise returns
|
||||
* zero.
|
||||
*/
|
||||
static inline int ubi_dbg_is_write_failure(const struct ubi_device *ubi)
|
||||
{
|
||||
if (ubi->dbg.emulate_io_failures)
|
||||
return !(prandom_u32() % 500);
|
||||
return 0;
|
||||
}
|
||||
|
||||
/**
|
||||
* ubi_dbg_is_erase_failure - if its time to emulate an erase failure.
|
||||
* @ubi: UBI device description object
|
||||
*
|
||||
* Returns non-zero if an erase failure should be emulated, otherwise returns
|
||||
* zero.
|
||||
*/
|
||||
static inline int ubi_dbg_is_erase_failure(const struct ubi_device *ubi)
|
||||
{
|
||||
if (ubi->dbg.emulate_io_failures)
|
||||
return !(prandom_u32() % 400);
|
||||
return 0;
|
||||
}
|
||||
|
||||
static inline int ubi_dbg_chk_io(const struct ubi_device *ubi)
|
||||
{
|
||||
return ubi->dbg.chk_io;
|
||||
}
|
||||
|
||||
static inline int ubi_dbg_chk_gen(const struct ubi_device *ubi)
|
||||
{
|
||||
return ubi->dbg.chk_gen;
|
||||
}
|
||||
|
||||
static inline int ubi_dbg_chk_fastmap(const struct ubi_device *ubi)
|
||||
{
|
||||
return ubi->dbg.chk_fastmap;
|
||||
}
|
||||
|
||||
static inline void ubi_enable_dbg_chk_fastmap(struct ubi_device *ubi)
|
||||
{
|
||||
ubi->dbg.chk_fastmap = 1;
|
||||
}
|
||||
|
||||
int ubi_dbg_power_cut(struct ubi_device *ubi, int caller);
|
||||
#endif /* !__UBI_DEBUG_H__ */
|
||||
1472
u-boot/drivers/mtd/ubi/eba.c
Normal file
1472
u-boot/drivers/mtd/ubi/eba.c
Normal file
File diff suppressed because it is too large
Load Diff
372
u-boot/drivers/mtd/ubi/fastmap-wl.c
Normal file
372
u-boot/drivers/mtd/ubi/fastmap-wl.c
Normal file
@@ -0,0 +1,372 @@
|
||||
/*
|
||||
* Copyright (c) 2012 Linutronix GmbH
|
||||
* Copyright (c) 2014 sigma star gmbh
|
||||
* Author: Richard Weinberger <richard@nod.at>
|
||||
*
|
||||
* SPDX-License-Identifier: GPL-2.0+
|
||||
*
|
||||
*/
|
||||
|
||||
/**
|
||||
* update_fastmap_work_fn - calls ubi_update_fastmap from a work queue
|
||||
* @wrk: the work description object
|
||||
*/
|
||||
#ifndef __UBOOT__
|
||||
static void update_fastmap_work_fn(struct work_struct *wrk)
|
||||
#else
|
||||
void update_fastmap_work_fn(struct ubi_device *ubi)
|
||||
#endif
|
||||
{
|
||||
#ifndef __UBOOT__
|
||||
struct ubi_device *ubi = container_of(wrk, struct ubi_device, fm_work);
|
||||
#endif
|
||||
|
||||
ubi_update_fastmap(ubi);
|
||||
spin_lock(&ubi->wl_lock);
|
||||
ubi->fm_work_scheduled = 0;
|
||||
spin_unlock(&ubi->wl_lock);
|
||||
}
|
||||
|
||||
/**
|
||||
* find_anchor_wl_entry - find wear-leveling entry to used as anchor PEB.
|
||||
* @root: the RB-tree where to look for
|
||||
*/
|
||||
static struct ubi_wl_entry *find_anchor_wl_entry(struct rb_root *root)
|
||||
{
|
||||
struct rb_node *p;
|
||||
struct ubi_wl_entry *e, *victim = NULL;
|
||||
int max_ec = UBI_MAX_ERASECOUNTER;
|
||||
|
||||
ubi_rb_for_each_entry(p, e, root, u.rb) {
|
||||
if (e->pnum < UBI_FM_MAX_START && e->ec < max_ec) {
|
||||
victim = e;
|
||||
max_ec = e->ec;
|
||||
}
|
||||
}
|
||||
|
||||
return victim;
|
||||
}
|
||||
|
||||
/**
|
||||
* return_unused_pool_pebs - returns unused PEB to the free tree.
|
||||
* @ubi: UBI device description object
|
||||
* @pool: fastmap pool description object
|
||||
*/
|
||||
static void return_unused_pool_pebs(struct ubi_device *ubi,
|
||||
struct ubi_fm_pool *pool)
|
||||
{
|
||||
int i;
|
||||
struct ubi_wl_entry *e;
|
||||
|
||||
for (i = pool->used; i < pool->size; i++) {
|
||||
e = ubi->lookuptbl[pool->pebs[i]];
|
||||
wl_tree_add(e, &ubi->free);
|
||||
ubi->free_count++;
|
||||
}
|
||||
}
|
||||
|
||||
static int anchor_pebs_avalible(struct rb_root *root)
|
||||
{
|
||||
struct rb_node *p;
|
||||
struct ubi_wl_entry *e;
|
||||
|
||||
ubi_rb_for_each_entry(p, e, root, u.rb)
|
||||
if (e->pnum < UBI_FM_MAX_START)
|
||||
return 1;
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
||||
/**
|
||||
* ubi_wl_get_fm_peb - find a physical erase block with a given maximal number.
|
||||
* @ubi: UBI device description object
|
||||
* @anchor: This PEB will be used as anchor PEB by fastmap
|
||||
*
|
||||
* The function returns a physical erase block with a given maximal number
|
||||
* and removes it from the wl subsystem.
|
||||
* Must be called with wl_lock held!
|
||||
*/
|
||||
struct ubi_wl_entry *ubi_wl_get_fm_peb(struct ubi_device *ubi, int anchor)
|
||||
{
|
||||
struct ubi_wl_entry *e = NULL;
|
||||
|
||||
if (!ubi->free.rb_node || (ubi->free_count - ubi->beb_rsvd_pebs < 1))
|
||||
goto out;
|
||||
|
||||
if (anchor)
|
||||
e = find_anchor_wl_entry(&ubi->free);
|
||||
else
|
||||
e = find_mean_wl_entry(ubi, &ubi->free);
|
||||
|
||||
if (!e)
|
||||
goto out;
|
||||
|
||||
self_check_in_wl_tree(ubi, e, &ubi->free);
|
||||
|
||||
/* remove it from the free list,
|
||||
* the wl subsystem does no longer know this erase block */
|
||||
rb_erase(&e->u.rb, &ubi->free);
|
||||
ubi->free_count--;
|
||||
out:
|
||||
return e;
|
||||
}
|
||||
|
||||
/**
|
||||
* ubi_refill_pools - refills all fastmap PEB pools.
|
||||
* @ubi: UBI device description object
|
||||
*/
|
||||
void ubi_refill_pools(struct ubi_device *ubi)
|
||||
{
|
||||
struct ubi_fm_pool *wl_pool = &ubi->fm_wl_pool;
|
||||
struct ubi_fm_pool *pool = &ubi->fm_pool;
|
||||
struct ubi_wl_entry *e;
|
||||
int enough;
|
||||
|
||||
spin_lock(&ubi->wl_lock);
|
||||
|
||||
return_unused_pool_pebs(ubi, wl_pool);
|
||||
return_unused_pool_pebs(ubi, pool);
|
||||
|
||||
wl_pool->size = 0;
|
||||
pool->size = 0;
|
||||
|
||||
for (;;) {
|
||||
enough = 0;
|
||||
if (pool->size < pool->max_size) {
|
||||
if (!ubi->free.rb_node)
|
||||
break;
|
||||
|
||||
e = wl_get_wle(ubi);
|
||||
if (!e)
|
||||
break;
|
||||
|
||||
pool->pebs[pool->size] = e->pnum;
|
||||
pool->size++;
|
||||
} else
|
||||
enough++;
|
||||
|
||||
if (wl_pool->size < wl_pool->max_size) {
|
||||
if (!ubi->free.rb_node ||
|
||||
(ubi->free_count - ubi->beb_rsvd_pebs < 5))
|
||||
break;
|
||||
|
||||
e = find_wl_entry(ubi, &ubi->free, WL_FREE_MAX_DIFF);
|
||||
self_check_in_wl_tree(ubi, e, &ubi->free);
|
||||
rb_erase(&e->u.rb, &ubi->free);
|
||||
ubi->free_count--;
|
||||
|
||||
wl_pool->pebs[wl_pool->size] = e->pnum;
|
||||
wl_pool->size++;
|
||||
} else
|
||||
enough++;
|
||||
|
||||
if (enough == 2)
|
||||
break;
|
||||
}
|
||||
|
||||
wl_pool->used = 0;
|
||||
pool->used = 0;
|
||||
|
||||
spin_unlock(&ubi->wl_lock);
|
||||
}
|
||||
|
||||
/**
|
||||
* ubi_wl_get_peb - get a physical eraseblock.
|
||||
* @ubi: UBI device description object
|
||||
*
|
||||
* This function returns a physical eraseblock in case of success and a
|
||||
* negative error code in case of failure.
|
||||
* Returns with ubi->fm_eba_sem held in read mode!
|
||||
*/
|
||||
int ubi_wl_get_peb(struct ubi_device *ubi)
|
||||
{
|
||||
int ret, retried = 0;
|
||||
struct ubi_fm_pool *pool = &ubi->fm_pool;
|
||||
struct ubi_fm_pool *wl_pool = &ubi->fm_wl_pool;
|
||||
|
||||
again:
|
||||
down_read(&ubi->fm_eba_sem);
|
||||
spin_lock(&ubi->wl_lock);
|
||||
|
||||
/* We check here also for the WL pool because at this point we can
|
||||
* refill the WL pool synchronous. */
|
||||
if (pool->used == pool->size || wl_pool->used == wl_pool->size) {
|
||||
spin_unlock(&ubi->wl_lock);
|
||||
up_read(&ubi->fm_eba_sem);
|
||||
ret = ubi_update_fastmap(ubi);
|
||||
if (ret) {
|
||||
ubi_msg(ubi, "Unable to write a new fastmap: %i", ret);
|
||||
down_read(&ubi->fm_eba_sem);
|
||||
return -ENOSPC;
|
||||
}
|
||||
down_read(&ubi->fm_eba_sem);
|
||||
spin_lock(&ubi->wl_lock);
|
||||
}
|
||||
|
||||
if (pool->used == pool->size) {
|
||||
spin_unlock(&ubi->wl_lock);
|
||||
if (retried) {
|
||||
ubi_err(ubi, "Unable to get a free PEB from user WL pool");
|
||||
ret = -ENOSPC;
|
||||
goto out;
|
||||
}
|
||||
retried = 1;
|
||||
up_read(&ubi->fm_eba_sem);
|
||||
goto again;
|
||||
}
|
||||
|
||||
ubi_assert(pool->used < pool->size);
|
||||
ret = pool->pebs[pool->used++];
|
||||
prot_queue_add(ubi, ubi->lookuptbl[ret]);
|
||||
spin_unlock(&ubi->wl_lock);
|
||||
out:
|
||||
return ret;
|
||||
}
|
||||
|
||||
/* get_peb_for_wl - returns a PEB to be used internally by the WL sub-system.
|
||||
*
|
||||
* @ubi: UBI device description object
|
||||
*/
|
||||
static struct ubi_wl_entry *get_peb_for_wl(struct ubi_device *ubi)
|
||||
{
|
||||
struct ubi_fm_pool *pool = &ubi->fm_wl_pool;
|
||||
int pnum;
|
||||
|
||||
if (pool->used == pool->size) {
|
||||
#ifndef __UBOOT__
|
||||
/* We cannot update the fastmap here because this
|
||||
* function is called in atomic context.
|
||||
* Let's fail here and refill/update it as soon as possible. */
|
||||
if (!ubi->fm_work_scheduled) {
|
||||
ubi->fm_work_scheduled = 1;
|
||||
schedule_work(&ubi->fm_work);
|
||||
}
|
||||
return NULL;
|
||||
#else
|
||||
/*
|
||||
* No work queues in U-Boot, we must do this immediately
|
||||
*/
|
||||
update_fastmap_work_fn(ubi);
|
||||
#endif
|
||||
}
|
||||
|
||||
pnum = pool->pebs[pool->used++];
|
||||
return ubi->lookuptbl[pnum];
|
||||
}
|
||||
|
||||
/**
|
||||
* ubi_ensure_anchor_pebs - schedule wear-leveling to produce an anchor PEB.
|
||||
* @ubi: UBI device description object
|
||||
*/
|
||||
int ubi_ensure_anchor_pebs(struct ubi_device *ubi)
|
||||
{
|
||||
struct ubi_work *wrk;
|
||||
|
||||
spin_lock(&ubi->wl_lock);
|
||||
if (ubi->wl_scheduled) {
|
||||
spin_unlock(&ubi->wl_lock);
|
||||
return 0;
|
||||
}
|
||||
ubi->wl_scheduled = 1;
|
||||
spin_unlock(&ubi->wl_lock);
|
||||
|
||||
wrk = kmalloc(sizeof(struct ubi_work), GFP_NOFS);
|
||||
if (!wrk) {
|
||||
spin_lock(&ubi->wl_lock);
|
||||
ubi->wl_scheduled = 0;
|
||||
spin_unlock(&ubi->wl_lock);
|
||||
return -ENOMEM;
|
||||
}
|
||||
|
||||
wrk->anchor = 1;
|
||||
wrk->func = &wear_leveling_worker;
|
||||
schedule_ubi_work(ubi, wrk);
|
||||
return 0;
|
||||
}
|
||||
|
||||
/**
|
||||
* ubi_wl_put_fm_peb - returns a PEB used in a fastmap to the wear-leveling
|
||||
* sub-system.
|
||||
* see: ubi_wl_put_peb()
|
||||
*
|
||||
* @ubi: UBI device description object
|
||||
* @fm_e: physical eraseblock to return
|
||||
* @lnum: the last used logical eraseblock number for the PEB
|
||||
* @torture: if this physical eraseblock has to be tortured
|
||||
*/
|
||||
int ubi_wl_put_fm_peb(struct ubi_device *ubi, struct ubi_wl_entry *fm_e,
|
||||
int lnum, int torture)
|
||||
{
|
||||
struct ubi_wl_entry *e;
|
||||
int vol_id, pnum = fm_e->pnum;
|
||||
|
||||
dbg_wl("PEB %d", pnum);
|
||||
|
||||
ubi_assert(pnum >= 0);
|
||||
ubi_assert(pnum < ubi->peb_count);
|
||||
|
||||
spin_lock(&ubi->wl_lock);
|
||||
e = ubi->lookuptbl[pnum];
|
||||
|
||||
/* This can happen if we recovered from a fastmap the very
|
||||
* first time and writing now a new one. In this case the wl system
|
||||
* has never seen any PEB used by the original fastmap.
|
||||
*/
|
||||
if (!e) {
|
||||
e = fm_e;
|
||||
ubi_assert(e->ec >= 0);
|
||||
ubi->lookuptbl[pnum] = e;
|
||||
}
|
||||
|
||||
spin_unlock(&ubi->wl_lock);
|
||||
|
||||
vol_id = lnum ? UBI_FM_DATA_VOLUME_ID : UBI_FM_SB_VOLUME_ID;
|
||||
return schedule_erase(ubi, e, vol_id, lnum, torture);
|
||||
}
|
||||
|
||||
/**
|
||||
* ubi_is_erase_work - checks whether a work is erase work.
|
||||
* @wrk: The work object to be checked
|
||||
*/
|
||||
int ubi_is_erase_work(struct ubi_work *wrk)
|
||||
{
|
||||
return wrk->func == erase_worker;
|
||||
}
|
||||
|
||||
static void ubi_fastmap_close(struct ubi_device *ubi)
|
||||
{
|
||||
int i;
|
||||
|
||||
#ifndef __UBOOT__
|
||||
flush_work(&ubi->fm_work);
|
||||
#else
|
||||
update_fastmap_work_fn(ubi);
|
||||
#endif
|
||||
return_unused_pool_pebs(ubi, &ubi->fm_pool);
|
||||
return_unused_pool_pebs(ubi, &ubi->fm_wl_pool);
|
||||
|
||||
if (ubi->fm) {
|
||||
for (i = 0; i < ubi->fm->used_blocks; i++)
|
||||
kfree(ubi->fm->e[i]);
|
||||
}
|
||||
kfree(ubi->fm);
|
||||
}
|
||||
|
||||
/**
|
||||
* may_reserve_for_fm - tests whether a PEB shall be reserved for fastmap.
|
||||
* See find_mean_wl_entry()
|
||||
*
|
||||
* @ubi: UBI device description object
|
||||
* @e: physical eraseblock to return
|
||||
* @root: RB tree to test against.
|
||||
*/
|
||||
static struct ubi_wl_entry *may_reserve_for_fm(struct ubi_device *ubi,
|
||||
struct ubi_wl_entry *e,
|
||||
struct rb_root *root) {
|
||||
if (e && !ubi->fm_disabled && !ubi->fm &&
|
||||
e->pnum < UBI_FM_MAX_START)
|
||||
e = rb_entry(rb_next(root->rb_node),
|
||||
struct ubi_wl_entry, u.rb);
|
||||
|
||||
return e;
|
||||
}
|
||||
1654
u-boot/drivers/mtd/ubi/fastmap.c
Normal file
1654
u-boot/drivers/mtd/ubi/fastmap.c
Normal file
File diff suppressed because it is too large
Load Diff
1428
u-boot/drivers/mtd/ubi/io.c
Normal file
1428
u-boot/drivers/mtd/ubi/io.c
Normal file
File diff suppressed because it is too large
Load Diff
864
u-boot/drivers/mtd/ubi/kapi.c
Normal file
864
u-boot/drivers/mtd/ubi/kapi.c
Normal file
@@ -0,0 +1,864 @@
|
||||
/*
|
||||
* Copyright (c) International Business Machines Corp., 2006
|
||||
*
|
||||
* SPDX-License-Identifier: GPL-2.0+
|
||||
*
|
||||
* Author: Artem Bityutskiy (Битюцкий Артём)
|
||||
*/
|
||||
|
||||
/* This file mostly implements UBI kernel API functions */
|
||||
|
||||
#ifndef __UBOOT__
|
||||
#include <linux/module.h>
|
||||
#include <linux/slab.h>
|
||||
#include <linux/namei.h>
|
||||
#include <linux/fs.h>
|
||||
#include <asm/div64.h>
|
||||
#else
|
||||
#include <ubi_uboot.h>
|
||||
#endif
|
||||
#include <linux/err.h>
|
||||
|
||||
#include "ubi.h"
|
||||
|
||||
/**
|
||||
* ubi_do_get_device_info - get information about UBI device.
|
||||
* @ubi: UBI device description object
|
||||
* @di: the information is stored here
|
||||
*
|
||||
* This function is the same as 'ubi_get_device_info()', but it assumes the UBI
|
||||
* device is locked and cannot disappear.
|
||||
*/
|
||||
void ubi_do_get_device_info(struct ubi_device *ubi, struct ubi_device_info *di)
|
||||
{
|
||||
di->ubi_num = ubi->ubi_num;
|
||||
di->leb_size = ubi->leb_size;
|
||||
di->leb_start = ubi->leb_start;
|
||||
di->min_io_size = ubi->min_io_size;
|
||||
di->max_write_size = ubi->max_write_size;
|
||||
di->ro_mode = ubi->ro_mode;
|
||||
#ifndef __UBOOT__
|
||||
di->cdev = ubi->cdev.dev;
|
||||
#endif
|
||||
}
|
||||
EXPORT_SYMBOL_GPL(ubi_do_get_device_info);
|
||||
|
||||
/**
|
||||
* ubi_get_device_info - get information about UBI device.
|
||||
* @ubi_num: UBI device number
|
||||
* @di: the information is stored here
|
||||
*
|
||||
* This function returns %0 in case of success, %-EINVAL if the UBI device
|
||||
* number is invalid, and %-ENODEV if there is no such UBI device.
|
||||
*/
|
||||
int ubi_get_device_info(int ubi_num, struct ubi_device_info *di)
|
||||
{
|
||||
struct ubi_device *ubi;
|
||||
|
||||
if (ubi_num < 0 || ubi_num >= UBI_MAX_DEVICES)
|
||||
return -EINVAL;
|
||||
ubi = ubi_get_device(ubi_num);
|
||||
if (!ubi)
|
||||
return -ENODEV;
|
||||
ubi_do_get_device_info(ubi, di);
|
||||
ubi_put_device(ubi);
|
||||
return 0;
|
||||
}
|
||||
EXPORT_SYMBOL_GPL(ubi_get_device_info);
|
||||
|
||||
/**
|
||||
* ubi_do_get_volume_info - get information about UBI volume.
|
||||
* @ubi: UBI device description object
|
||||
* @vol: volume description object
|
||||
* @vi: the information is stored here
|
||||
*/
|
||||
void ubi_do_get_volume_info(struct ubi_device *ubi, struct ubi_volume *vol,
|
||||
struct ubi_volume_info *vi)
|
||||
{
|
||||
vi->vol_id = vol->vol_id;
|
||||
vi->ubi_num = ubi->ubi_num;
|
||||
vi->size = vol->reserved_pebs;
|
||||
vi->used_bytes = vol->used_bytes;
|
||||
vi->vol_type = vol->vol_type;
|
||||
vi->corrupted = vol->corrupted;
|
||||
vi->upd_marker = vol->upd_marker;
|
||||
vi->alignment = vol->alignment;
|
||||
vi->usable_leb_size = vol->usable_leb_size;
|
||||
vi->name_len = vol->name_len;
|
||||
vi->name = vol->name;
|
||||
vi->cdev = vol->cdev.dev;
|
||||
}
|
||||
|
||||
/**
|
||||
* ubi_get_volume_info - get information about UBI volume.
|
||||
* @desc: volume descriptor
|
||||
* @vi: the information is stored here
|
||||
*/
|
||||
void ubi_get_volume_info(struct ubi_volume_desc *desc,
|
||||
struct ubi_volume_info *vi)
|
||||
{
|
||||
ubi_do_get_volume_info(desc->vol->ubi, desc->vol, vi);
|
||||
}
|
||||
EXPORT_SYMBOL_GPL(ubi_get_volume_info);
|
||||
|
||||
/**
|
||||
* ubi_open_volume - open UBI volume.
|
||||
* @ubi_num: UBI device number
|
||||
* @vol_id: volume ID
|
||||
* @mode: open mode
|
||||
*
|
||||
* The @mode parameter specifies if the volume should be opened in read-only
|
||||
* mode, read-write mode, or exclusive mode. The exclusive mode guarantees that
|
||||
* nobody else will be able to open this volume. UBI allows to have many volume
|
||||
* readers and one writer at a time.
|
||||
*
|
||||
* If a static volume is being opened for the first time since boot, it will be
|
||||
* checked by this function, which means it will be fully read and the CRC
|
||||
* checksum of each logical eraseblock will be checked.
|
||||
*
|
||||
* This function returns volume descriptor in case of success and a negative
|
||||
* error code in case of failure.
|
||||
*/
|
||||
struct ubi_volume_desc *ubi_open_volume(int ubi_num, int vol_id, int mode)
|
||||
{
|
||||
int err;
|
||||
struct ubi_volume_desc *desc;
|
||||
struct ubi_device *ubi;
|
||||
struct ubi_volume *vol;
|
||||
|
||||
dbg_gen("open device %d, volume %d, mode %d", ubi_num, vol_id, mode);
|
||||
|
||||
if (ubi_num < 0 || ubi_num >= UBI_MAX_DEVICES)
|
||||
return ERR_PTR(-EINVAL);
|
||||
|
||||
if (mode != UBI_READONLY && mode != UBI_READWRITE &&
|
||||
mode != UBI_EXCLUSIVE && mode != UBI_METAONLY)
|
||||
return ERR_PTR(-EINVAL);
|
||||
|
||||
/*
|
||||
* First of all, we have to get the UBI device to prevent its removal.
|
||||
*/
|
||||
ubi = ubi_get_device(ubi_num);
|
||||
if (!ubi)
|
||||
return ERR_PTR(-ENODEV);
|
||||
|
||||
if (vol_id < 0 || vol_id >= ubi->vtbl_slots) {
|
||||
err = -EINVAL;
|
||||
goto out_put_ubi;
|
||||
}
|
||||
|
||||
desc = kmalloc(sizeof(struct ubi_volume_desc), GFP_KERNEL);
|
||||
if (!desc) {
|
||||
err = -ENOMEM;
|
||||
goto out_put_ubi;
|
||||
}
|
||||
|
||||
err = -ENODEV;
|
||||
if (!try_module_get(THIS_MODULE))
|
||||
goto out_free;
|
||||
|
||||
spin_lock(&ubi->volumes_lock);
|
||||
vol = ubi->volumes[vol_id];
|
||||
if (!vol)
|
||||
goto out_unlock;
|
||||
|
||||
err = -EBUSY;
|
||||
switch (mode) {
|
||||
case UBI_READONLY:
|
||||
if (vol->exclusive)
|
||||
goto out_unlock;
|
||||
vol->readers += 1;
|
||||
break;
|
||||
|
||||
case UBI_READWRITE:
|
||||
if (vol->exclusive || vol->writers > 0)
|
||||
goto out_unlock;
|
||||
vol->writers += 1;
|
||||
break;
|
||||
|
||||
case UBI_EXCLUSIVE:
|
||||
if (vol->exclusive || vol->writers || vol->readers ||
|
||||
vol->metaonly)
|
||||
goto out_unlock;
|
||||
vol->exclusive = 1;
|
||||
break;
|
||||
|
||||
case UBI_METAONLY:
|
||||
if (vol->metaonly || vol->exclusive)
|
||||
goto out_unlock;
|
||||
vol->metaonly = 1;
|
||||
break;
|
||||
}
|
||||
get_device(&vol->dev);
|
||||
vol->ref_count += 1;
|
||||
spin_unlock(&ubi->volumes_lock);
|
||||
|
||||
desc->vol = vol;
|
||||
desc->mode = mode;
|
||||
|
||||
mutex_lock(&ubi->ckvol_mutex);
|
||||
if (!vol->checked) {
|
||||
/* This is the first open - check the volume */
|
||||
err = ubi_check_volume(ubi, vol_id);
|
||||
if (err < 0) {
|
||||
mutex_unlock(&ubi->ckvol_mutex);
|
||||
ubi_close_volume(desc);
|
||||
return ERR_PTR(err);
|
||||
}
|
||||
if (err == 1) {
|
||||
ubi_warn(ubi, "volume %d on UBI device %d is corrupted",
|
||||
vol_id, ubi->ubi_num);
|
||||
vol->corrupted = 1;
|
||||
}
|
||||
vol->checked = 1;
|
||||
}
|
||||
mutex_unlock(&ubi->ckvol_mutex);
|
||||
|
||||
return desc;
|
||||
|
||||
out_unlock:
|
||||
spin_unlock(&ubi->volumes_lock);
|
||||
module_put(THIS_MODULE);
|
||||
out_free:
|
||||
kfree(desc);
|
||||
out_put_ubi:
|
||||
ubi_put_device(ubi);
|
||||
ubi_err(ubi, "cannot open device %d, volume %d, error %d",
|
||||
ubi_num, vol_id, err);
|
||||
return ERR_PTR(err);
|
||||
}
|
||||
EXPORT_SYMBOL_GPL(ubi_open_volume);
|
||||
|
||||
/**
|
||||
* ubi_open_volume_nm - open UBI volume by name.
|
||||
* @ubi_num: UBI device number
|
||||
* @name: volume name
|
||||
* @mode: open mode
|
||||
*
|
||||
* This function is similar to 'ubi_open_volume()', but opens a volume by name.
|
||||
*/
|
||||
struct ubi_volume_desc *ubi_open_volume_nm(int ubi_num, const char *name,
|
||||
int mode)
|
||||
{
|
||||
int i, vol_id = -1, len;
|
||||
struct ubi_device *ubi;
|
||||
struct ubi_volume_desc *ret;
|
||||
|
||||
dbg_gen("open device %d, volume %s, mode %d", ubi_num, name, mode);
|
||||
|
||||
if (!name)
|
||||
return ERR_PTR(-EINVAL);
|
||||
|
||||
len = strnlen(name, UBI_VOL_NAME_MAX + 1);
|
||||
if (len > UBI_VOL_NAME_MAX)
|
||||
return ERR_PTR(-EINVAL);
|
||||
|
||||
if (ubi_num < 0 || ubi_num >= UBI_MAX_DEVICES)
|
||||
return ERR_PTR(-EINVAL);
|
||||
|
||||
ubi = ubi_get_device(ubi_num);
|
||||
if (!ubi)
|
||||
return ERR_PTR(-ENODEV);
|
||||
|
||||
spin_lock(&ubi->volumes_lock);
|
||||
/* Walk all volumes of this UBI device */
|
||||
for (i = 0; i < ubi->vtbl_slots; i++) {
|
||||
struct ubi_volume *vol = ubi->volumes[i];
|
||||
|
||||
if (vol && len == vol->name_len && !strcmp(name, vol->name)) {
|
||||
vol_id = i;
|
||||
break;
|
||||
}
|
||||
}
|
||||
spin_unlock(&ubi->volumes_lock);
|
||||
|
||||
if (vol_id >= 0)
|
||||
ret = ubi_open_volume(ubi_num, vol_id, mode);
|
||||
else
|
||||
ret = ERR_PTR(-ENODEV);
|
||||
|
||||
/*
|
||||
* We should put the UBI device even in case of success, because
|
||||
* 'ubi_open_volume()' took a reference as well.
|
||||
*/
|
||||
ubi_put_device(ubi);
|
||||
return ret;
|
||||
}
|
||||
EXPORT_SYMBOL_GPL(ubi_open_volume_nm);
|
||||
|
||||
#ifndef __UBOOT__
|
||||
/**
|
||||
* ubi_open_volume_path - open UBI volume by its character device node path.
|
||||
* @pathname: volume character device node path
|
||||
* @mode: open mode
|
||||
*
|
||||
* This function is similar to 'ubi_open_volume()', but opens a volume the path
|
||||
* to its character device node.
|
||||
*/
|
||||
struct ubi_volume_desc *ubi_open_volume_path(const char *pathname, int mode)
|
||||
{
|
||||
int error, ubi_num, vol_id, mod;
|
||||
struct inode *inode;
|
||||
struct path path;
|
||||
|
||||
dbg_gen("open volume %s, mode %d", pathname, mode);
|
||||
|
||||
if (!pathname || !*pathname)
|
||||
return ERR_PTR(-EINVAL);
|
||||
|
||||
error = kern_path(pathname, LOOKUP_FOLLOW, &path);
|
||||
if (error)
|
||||
return ERR_PTR(error);
|
||||
|
||||
inode = d_backing_inode(path.dentry);
|
||||
mod = inode->i_mode;
|
||||
ubi_num = ubi_major2num(imajor(inode));
|
||||
vol_id = iminor(inode) - 1;
|
||||
path_put(&path);
|
||||
|
||||
if (!S_ISCHR(mod))
|
||||
return ERR_PTR(-EINVAL);
|
||||
if (vol_id >= 0 && ubi_num >= 0)
|
||||
return ubi_open_volume(ubi_num, vol_id, mode);
|
||||
return ERR_PTR(-ENODEV);
|
||||
}
|
||||
EXPORT_SYMBOL_GPL(ubi_open_volume_path);
|
||||
#endif
|
||||
|
||||
/**
|
||||
* ubi_close_volume - close UBI volume.
|
||||
* @desc: volume descriptor
|
||||
*/
|
||||
void ubi_close_volume(struct ubi_volume_desc *desc)
|
||||
{
|
||||
struct ubi_volume *vol = desc->vol;
|
||||
struct ubi_device *ubi = vol->ubi;
|
||||
|
||||
dbg_gen("close device %d, volume %d, mode %d",
|
||||
ubi->ubi_num, vol->vol_id, desc->mode);
|
||||
|
||||
spin_lock(&ubi->volumes_lock);
|
||||
switch (desc->mode) {
|
||||
case UBI_READONLY:
|
||||
vol->readers -= 1;
|
||||
break;
|
||||
case UBI_READWRITE:
|
||||
vol->writers -= 1;
|
||||
break;
|
||||
case UBI_EXCLUSIVE:
|
||||
vol->exclusive = 0;
|
||||
break;
|
||||
case UBI_METAONLY:
|
||||
vol->metaonly = 0;
|
||||
break;
|
||||
}
|
||||
vol->ref_count -= 1;
|
||||
spin_unlock(&ubi->volumes_lock);
|
||||
|
||||
kfree(desc);
|
||||
put_device(&vol->dev);
|
||||
ubi_put_device(ubi);
|
||||
module_put(THIS_MODULE);
|
||||
}
|
||||
EXPORT_SYMBOL_GPL(ubi_close_volume);
|
||||
|
||||
/**
|
||||
* leb_read_sanity_check - does sanity checks on read requests.
|
||||
* @desc: volume descriptor
|
||||
* @lnum: logical eraseblock number to read from
|
||||
* @offset: offset within the logical eraseblock to read from
|
||||
* @len: how many bytes to read
|
||||
*
|
||||
* This function is used by ubi_leb_read() and ubi_leb_read_sg()
|
||||
* to perform sanity checks.
|
||||
*/
|
||||
static int leb_read_sanity_check(struct ubi_volume_desc *desc, int lnum,
|
||||
int offset, int len)
|
||||
{
|
||||
struct ubi_volume *vol = desc->vol;
|
||||
struct ubi_device *ubi = vol->ubi;
|
||||
int vol_id = vol->vol_id;
|
||||
|
||||
if (vol_id < 0 || vol_id >= ubi->vtbl_slots || lnum < 0 ||
|
||||
lnum >= vol->used_ebs || offset < 0 || len < 0 ||
|
||||
offset + len > vol->usable_leb_size)
|
||||
return -EINVAL;
|
||||
|
||||
if (vol->vol_type == UBI_STATIC_VOLUME) {
|
||||
if (vol->used_ebs == 0)
|
||||
/* Empty static UBI volume */
|
||||
return 0;
|
||||
if (lnum == vol->used_ebs - 1 &&
|
||||
offset + len > vol->last_eb_bytes)
|
||||
return -EINVAL;
|
||||
}
|
||||
|
||||
if (vol->upd_marker)
|
||||
return -EBADF;
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
||||
/**
|
||||
* ubi_leb_read - read data.
|
||||
* @desc: volume descriptor
|
||||
* @lnum: logical eraseblock number to read from
|
||||
* @buf: buffer where to store the read data
|
||||
* @offset: offset within the logical eraseblock to read from
|
||||
* @len: how many bytes to read
|
||||
* @check: whether UBI has to check the read data's CRC or not.
|
||||
*
|
||||
* This function reads data from offset @offset of logical eraseblock @lnum and
|
||||
* stores the data at @buf. When reading from static volumes, @check specifies
|
||||
* whether the data has to be checked or not. If yes, the whole logical
|
||||
* eraseblock will be read and its CRC checksum will be checked (i.e., the CRC
|
||||
* checksum is per-eraseblock). So checking may substantially slow down the
|
||||
* read speed. The @check argument is ignored for dynamic volumes.
|
||||
*
|
||||
* In case of success, this function returns zero. In case of failure, this
|
||||
* function returns a negative error code.
|
||||
*
|
||||
* %-EBADMSG error code is returned:
|
||||
* o for both static and dynamic volumes if MTD driver has detected a data
|
||||
* integrity problem (unrecoverable ECC checksum mismatch in case of NAND);
|
||||
* o for static volumes in case of data CRC mismatch.
|
||||
*
|
||||
* If the volume is damaged because of an interrupted update this function just
|
||||
* returns immediately with %-EBADF error code.
|
||||
*/
|
||||
int ubi_leb_read(struct ubi_volume_desc *desc, int lnum, char *buf, int offset,
|
||||
int len, int check)
|
||||
{
|
||||
struct ubi_volume *vol = desc->vol;
|
||||
struct ubi_device *ubi = vol->ubi;
|
||||
int err, vol_id = vol->vol_id;
|
||||
|
||||
dbg_gen("read %d bytes from LEB %d:%d:%d", len, vol_id, lnum, offset);
|
||||
|
||||
err = leb_read_sanity_check(desc, lnum, offset, len);
|
||||
if (err < 0)
|
||||
return err;
|
||||
|
||||
if (len == 0)
|
||||
return 0;
|
||||
|
||||
err = ubi_eba_read_leb(ubi, vol, lnum, buf, offset, len, check);
|
||||
if (err && mtd_is_eccerr(err) && vol->vol_type == UBI_STATIC_VOLUME) {
|
||||
ubi_warn(ubi, "mark volume %d as corrupted", vol_id);
|
||||
vol->corrupted = 1;
|
||||
}
|
||||
|
||||
return err;
|
||||
}
|
||||
EXPORT_SYMBOL_GPL(ubi_leb_read);
|
||||
|
||||
#ifndef __UBOOT__
|
||||
/**
|
||||
* ubi_leb_read_sg - read data into a scatter gather list.
|
||||
* @desc: volume descriptor
|
||||
* @lnum: logical eraseblock number to read from
|
||||
* @buf: buffer where to store the read data
|
||||
* @offset: offset within the logical eraseblock to read from
|
||||
* @len: how many bytes to read
|
||||
* @check: whether UBI has to check the read data's CRC or not.
|
||||
*
|
||||
* This function works exactly like ubi_leb_read_sg(). But instead of
|
||||
* storing the read data into a buffer it writes to an UBI scatter gather
|
||||
* list.
|
||||
*/
|
||||
int ubi_leb_read_sg(struct ubi_volume_desc *desc, int lnum, struct ubi_sgl *sgl,
|
||||
int offset, int len, int check)
|
||||
{
|
||||
struct ubi_volume *vol = desc->vol;
|
||||
struct ubi_device *ubi = vol->ubi;
|
||||
int err, vol_id = vol->vol_id;
|
||||
|
||||
dbg_gen("read %d bytes from LEB %d:%d:%d", len, vol_id, lnum, offset);
|
||||
|
||||
err = leb_read_sanity_check(desc, lnum, offset, len);
|
||||
if (err < 0)
|
||||
return err;
|
||||
|
||||
if (len == 0)
|
||||
return 0;
|
||||
|
||||
err = ubi_eba_read_leb_sg(ubi, vol, sgl, lnum, offset, len, check);
|
||||
if (err && mtd_is_eccerr(err) && vol->vol_type == UBI_STATIC_VOLUME) {
|
||||
ubi_warn(ubi, "mark volume %d as corrupted", vol_id);
|
||||
vol->corrupted = 1;
|
||||
}
|
||||
|
||||
return err;
|
||||
}
|
||||
EXPORT_SYMBOL_GPL(ubi_leb_read_sg);
|
||||
#endif
|
||||
|
||||
/**
|
||||
* ubi_leb_write - write data.
|
||||
* @desc: volume descriptor
|
||||
* @lnum: logical eraseblock number to write to
|
||||
* @buf: data to write
|
||||
* @offset: offset within the logical eraseblock where to write
|
||||
* @len: how many bytes to write
|
||||
*
|
||||
* This function writes @len bytes of data from @buf to offset @offset of
|
||||
* logical eraseblock @lnum.
|
||||
*
|
||||
* This function takes care of physical eraseblock write failures. If write to
|
||||
* the physical eraseblock write operation fails, the logical eraseblock is
|
||||
* re-mapped to another physical eraseblock, the data is recovered, and the
|
||||
* write finishes. UBI has a pool of reserved physical eraseblocks for this.
|
||||
*
|
||||
* If all the data were successfully written, zero is returned. If an error
|
||||
* occurred and UBI has not been able to recover from it, this function returns
|
||||
* a negative error code. Note, in case of an error, it is possible that
|
||||
* something was still written to the flash media, but that may be some
|
||||
* garbage.
|
||||
*
|
||||
* If the volume is damaged because of an interrupted update this function just
|
||||
* returns immediately with %-EBADF code.
|
||||
*/
|
||||
int ubi_leb_write(struct ubi_volume_desc *desc, int lnum, const void *buf,
|
||||
int offset, int len)
|
||||
{
|
||||
struct ubi_volume *vol = desc->vol;
|
||||
struct ubi_device *ubi = vol->ubi;
|
||||
int vol_id = vol->vol_id;
|
||||
|
||||
dbg_gen("write %d bytes to LEB %d:%d:%d", len, vol_id, lnum, offset);
|
||||
|
||||
if (vol_id < 0 || vol_id >= ubi->vtbl_slots)
|
||||
return -EINVAL;
|
||||
|
||||
if (desc->mode == UBI_READONLY || vol->vol_type == UBI_STATIC_VOLUME)
|
||||
return -EROFS;
|
||||
|
||||
if (lnum < 0 || lnum >= vol->reserved_pebs || offset < 0 || len < 0 ||
|
||||
offset + len > vol->usable_leb_size ||
|
||||
offset & (ubi->min_io_size - 1) || len & (ubi->min_io_size - 1))
|
||||
return -EINVAL;
|
||||
|
||||
if (vol->upd_marker)
|
||||
return -EBADF;
|
||||
|
||||
if (len == 0)
|
||||
return 0;
|
||||
|
||||
return ubi_eba_write_leb(ubi, vol, lnum, buf, offset, len);
|
||||
}
|
||||
EXPORT_SYMBOL_GPL(ubi_leb_write);
|
||||
|
||||
/*
|
||||
* ubi_leb_change - change logical eraseblock atomically.
|
||||
* @desc: volume descriptor
|
||||
* @lnum: logical eraseblock number to change
|
||||
* @buf: data to write
|
||||
* @len: how many bytes to write
|
||||
*
|
||||
* This function changes the contents of a logical eraseblock atomically. @buf
|
||||
* has to contain new logical eraseblock data, and @len - the length of the
|
||||
* data, which has to be aligned. The length may be shorter than the logical
|
||||
* eraseblock size, ant the logical eraseblock may be appended to more times
|
||||
* later on. This function guarantees that in case of an unclean reboot the old
|
||||
* contents is preserved. Returns zero in case of success and a negative error
|
||||
* code in case of failure.
|
||||
*/
|
||||
int ubi_leb_change(struct ubi_volume_desc *desc, int lnum, const void *buf,
|
||||
int len)
|
||||
{
|
||||
struct ubi_volume *vol = desc->vol;
|
||||
struct ubi_device *ubi = vol->ubi;
|
||||
int vol_id = vol->vol_id;
|
||||
|
||||
dbg_gen("atomically write %d bytes to LEB %d:%d", len, vol_id, lnum);
|
||||
|
||||
if (vol_id < 0 || vol_id >= ubi->vtbl_slots)
|
||||
return -EINVAL;
|
||||
|
||||
if (desc->mode == UBI_READONLY || vol->vol_type == UBI_STATIC_VOLUME)
|
||||
return -EROFS;
|
||||
|
||||
if (lnum < 0 || lnum >= vol->reserved_pebs || len < 0 ||
|
||||
len > vol->usable_leb_size || len & (ubi->min_io_size - 1))
|
||||
return -EINVAL;
|
||||
|
||||
if (vol->upd_marker)
|
||||
return -EBADF;
|
||||
|
||||
if (len == 0)
|
||||
return 0;
|
||||
|
||||
return ubi_eba_atomic_leb_change(ubi, vol, lnum, buf, len);
|
||||
}
|
||||
EXPORT_SYMBOL_GPL(ubi_leb_change);
|
||||
|
||||
/**
|
||||
* ubi_leb_erase - erase logical eraseblock.
|
||||
* @desc: volume descriptor
|
||||
* @lnum: logical eraseblock number
|
||||
*
|
||||
* This function un-maps logical eraseblock @lnum and synchronously erases the
|
||||
* correspondent physical eraseblock. Returns zero in case of success and a
|
||||
* negative error code in case of failure.
|
||||
*
|
||||
* If the volume is damaged because of an interrupted update this function just
|
||||
* returns immediately with %-EBADF code.
|
||||
*/
|
||||
int ubi_leb_erase(struct ubi_volume_desc *desc, int lnum)
|
||||
{
|
||||
struct ubi_volume *vol = desc->vol;
|
||||
struct ubi_device *ubi = vol->ubi;
|
||||
int err;
|
||||
|
||||
dbg_gen("erase LEB %d:%d", vol->vol_id, lnum);
|
||||
|
||||
if (desc->mode == UBI_READONLY || vol->vol_type == UBI_STATIC_VOLUME)
|
||||
return -EROFS;
|
||||
|
||||
if (lnum < 0 || lnum >= vol->reserved_pebs)
|
||||
return -EINVAL;
|
||||
|
||||
if (vol->upd_marker)
|
||||
return -EBADF;
|
||||
|
||||
err = ubi_eba_unmap_leb(ubi, vol, lnum);
|
||||
if (err)
|
||||
return err;
|
||||
|
||||
return ubi_wl_flush(ubi, vol->vol_id, lnum);
|
||||
}
|
||||
EXPORT_SYMBOL_GPL(ubi_leb_erase);
|
||||
|
||||
/**
|
||||
* ubi_leb_unmap - un-map logical eraseblock.
|
||||
* @desc: volume descriptor
|
||||
* @lnum: logical eraseblock number
|
||||
*
|
||||
* This function un-maps logical eraseblock @lnum and schedules the
|
||||
* corresponding physical eraseblock for erasure, so that it will eventually be
|
||||
* physically erased in background. This operation is much faster than the
|
||||
* erase operation.
|
||||
*
|
||||
* Unlike erase, the un-map operation does not guarantee that the logical
|
||||
* eraseblock will contain all 0xFF bytes when UBI is initialized again. For
|
||||
* example, if several logical eraseblocks are un-mapped, and an unclean reboot
|
||||
* happens after this, the logical eraseblocks will not necessarily be
|
||||
* un-mapped again when this MTD device is attached. They may actually be
|
||||
* mapped to the same physical eraseblocks again. So, this function has to be
|
||||
* used with care.
|
||||
*
|
||||
* In other words, when un-mapping a logical eraseblock, UBI does not store
|
||||
* any information about this on the flash media, it just marks the logical
|
||||
* eraseblock as "un-mapped" in RAM. If UBI is detached before the physical
|
||||
* eraseblock is physically erased, it will be mapped again to the same logical
|
||||
* eraseblock when the MTD device is attached again.
|
||||
*
|
||||
* The main and obvious use-case of this function is when the contents of a
|
||||
* logical eraseblock has to be re-written. Then it is much more efficient to
|
||||
* first un-map it, then write new data, rather than first erase it, then write
|
||||
* new data. Note, once new data has been written to the logical eraseblock,
|
||||
* UBI guarantees that the old contents has gone forever. In other words, if an
|
||||
* unclean reboot happens after the logical eraseblock has been un-mapped and
|
||||
* then written to, it will contain the last written data.
|
||||
*
|
||||
* This function returns zero in case of success and a negative error code in
|
||||
* case of failure. If the volume is damaged because of an interrupted update
|
||||
* this function just returns immediately with %-EBADF code.
|
||||
*/
|
||||
int ubi_leb_unmap(struct ubi_volume_desc *desc, int lnum)
|
||||
{
|
||||
struct ubi_volume *vol = desc->vol;
|
||||
struct ubi_device *ubi = vol->ubi;
|
||||
|
||||
dbg_gen("unmap LEB %d:%d", vol->vol_id, lnum);
|
||||
|
||||
if (desc->mode == UBI_READONLY || vol->vol_type == UBI_STATIC_VOLUME)
|
||||
return -EROFS;
|
||||
|
||||
if (lnum < 0 || lnum >= vol->reserved_pebs)
|
||||
return -EINVAL;
|
||||
|
||||
if (vol->upd_marker)
|
||||
return -EBADF;
|
||||
|
||||
return ubi_eba_unmap_leb(ubi, vol, lnum);
|
||||
}
|
||||
EXPORT_SYMBOL_GPL(ubi_leb_unmap);
|
||||
|
||||
/**
|
||||
* ubi_leb_map - map logical eraseblock to a physical eraseblock.
|
||||
* @desc: volume descriptor
|
||||
* @lnum: logical eraseblock number
|
||||
*
|
||||
* This function maps an un-mapped logical eraseblock @lnum to a physical
|
||||
* eraseblock. This means, that after a successful invocation of this
|
||||
* function the logical eraseblock @lnum will be empty (contain only %0xFF
|
||||
* bytes) and be mapped to a physical eraseblock, even if an unclean reboot
|
||||
* happens.
|
||||
*
|
||||
* This function returns zero in case of success, %-EBADF if the volume is
|
||||
* damaged because of an interrupted update, %-EBADMSG if the logical
|
||||
* eraseblock is already mapped, and other negative error codes in case of
|
||||
* other failures.
|
||||
*/
|
||||
int ubi_leb_map(struct ubi_volume_desc *desc, int lnum)
|
||||
{
|
||||
struct ubi_volume *vol = desc->vol;
|
||||
struct ubi_device *ubi = vol->ubi;
|
||||
|
||||
dbg_gen("unmap LEB %d:%d", vol->vol_id, lnum);
|
||||
|
||||
if (desc->mode == UBI_READONLY || vol->vol_type == UBI_STATIC_VOLUME)
|
||||
return -EROFS;
|
||||
|
||||
if (lnum < 0 || lnum >= vol->reserved_pebs)
|
||||
return -EINVAL;
|
||||
|
||||
if (vol->upd_marker)
|
||||
return -EBADF;
|
||||
|
||||
if (vol->eba_tbl[lnum] >= 0)
|
||||
return -EBADMSG;
|
||||
|
||||
return ubi_eba_write_leb(ubi, vol, lnum, NULL, 0, 0);
|
||||
}
|
||||
EXPORT_SYMBOL_GPL(ubi_leb_map);
|
||||
|
||||
/**
|
||||
* ubi_is_mapped - check if logical eraseblock is mapped.
|
||||
* @desc: volume descriptor
|
||||
* @lnum: logical eraseblock number
|
||||
*
|
||||
* This function checks if logical eraseblock @lnum is mapped to a physical
|
||||
* eraseblock. If a logical eraseblock is un-mapped, this does not necessarily
|
||||
* mean it will still be un-mapped after the UBI device is re-attached. The
|
||||
* logical eraseblock may become mapped to the physical eraseblock it was last
|
||||
* mapped to.
|
||||
*
|
||||
* This function returns %1 if the LEB is mapped, %0 if not, and a negative
|
||||
* error code in case of failure. If the volume is damaged because of an
|
||||
* interrupted update this function just returns immediately with %-EBADF error
|
||||
* code.
|
||||
*/
|
||||
int ubi_is_mapped(struct ubi_volume_desc *desc, int lnum)
|
||||
{
|
||||
struct ubi_volume *vol = desc->vol;
|
||||
|
||||
dbg_gen("test LEB %d:%d", vol->vol_id, lnum);
|
||||
|
||||
if (lnum < 0 || lnum >= vol->reserved_pebs)
|
||||
return -EINVAL;
|
||||
|
||||
if (vol->upd_marker)
|
||||
return -EBADF;
|
||||
|
||||
return vol->eba_tbl[lnum] >= 0;
|
||||
}
|
||||
EXPORT_SYMBOL_GPL(ubi_is_mapped);
|
||||
|
||||
/**
|
||||
* ubi_sync - synchronize UBI device buffers.
|
||||
* @ubi_num: UBI device to synchronize
|
||||
*
|
||||
* The underlying MTD device may cache data in hardware or in software. This
|
||||
* function ensures the caches are flushed. Returns zero in case of success and
|
||||
* a negative error code in case of failure.
|
||||
*/
|
||||
int ubi_sync(int ubi_num)
|
||||
{
|
||||
struct ubi_device *ubi;
|
||||
|
||||
ubi = ubi_get_device(ubi_num);
|
||||
if (!ubi)
|
||||
return -ENODEV;
|
||||
|
||||
mtd_sync(ubi->mtd);
|
||||
ubi_put_device(ubi);
|
||||
return 0;
|
||||
}
|
||||
EXPORT_SYMBOL_GPL(ubi_sync);
|
||||
|
||||
/**
|
||||
* ubi_flush - flush UBI work queue.
|
||||
* @ubi_num: UBI device to flush work queue
|
||||
* @vol_id: volume id to flush for
|
||||
* @lnum: logical eraseblock number to flush for
|
||||
*
|
||||
* This function executes all pending works for a particular volume id / logical
|
||||
* eraseblock number pair. If either value is set to %UBI_ALL, then it acts as
|
||||
* a wildcard for all of the corresponding volume numbers or logical
|
||||
* eraseblock numbers. It returns zero in case of success and a negative error
|
||||
* code in case of failure.
|
||||
*/
|
||||
int ubi_flush(int ubi_num, int vol_id, int lnum)
|
||||
{
|
||||
struct ubi_device *ubi;
|
||||
int err = 0;
|
||||
|
||||
ubi = ubi_get_device(ubi_num);
|
||||
if (!ubi)
|
||||
return -ENODEV;
|
||||
|
||||
err = ubi_wl_flush(ubi, vol_id, lnum);
|
||||
ubi_put_device(ubi);
|
||||
return err;
|
||||
}
|
||||
EXPORT_SYMBOL_GPL(ubi_flush);
|
||||
|
||||
#ifndef __UBOOT__
|
||||
BLOCKING_NOTIFIER_HEAD(ubi_notifiers);
|
||||
|
||||
/**
|
||||
* ubi_register_volume_notifier - register a volume notifier.
|
||||
* @nb: the notifier description object
|
||||
* @ignore_existing: if non-zero, do not send "added" notification for all
|
||||
* already existing volumes
|
||||
*
|
||||
* This function registers a volume notifier, which means that
|
||||
* 'nb->notifier_call()' will be invoked when an UBI volume is created,
|
||||
* removed, re-sized, re-named, or updated. The first argument of the function
|
||||
* is the notification type. The second argument is pointer to a
|
||||
* &struct ubi_notification object which describes the notification event.
|
||||
* Using UBI API from the volume notifier is prohibited.
|
||||
*
|
||||
* This function returns zero in case of success and a negative error code
|
||||
* in case of failure.
|
||||
*/
|
||||
int ubi_register_volume_notifier(struct notifier_block *nb,
|
||||
int ignore_existing)
|
||||
{
|
||||
int err;
|
||||
|
||||
err = blocking_notifier_chain_register(&ubi_notifiers, nb);
|
||||
if (err != 0)
|
||||
return err;
|
||||
if (ignore_existing)
|
||||
return 0;
|
||||
|
||||
/*
|
||||
* We are going to walk all UBI devices and all volumes, and
|
||||
* notify the user about existing volumes by the %UBI_VOLUME_ADDED
|
||||
* event. We have to lock the @ubi_devices_mutex to make sure UBI
|
||||
* devices do not disappear.
|
||||
*/
|
||||
mutex_lock(&ubi_devices_mutex);
|
||||
ubi_enumerate_volumes(nb);
|
||||
mutex_unlock(&ubi_devices_mutex);
|
||||
|
||||
return err;
|
||||
}
|
||||
EXPORT_SYMBOL_GPL(ubi_register_volume_notifier);
|
||||
|
||||
/**
|
||||
* ubi_unregister_volume_notifier - unregister the volume notifier.
|
||||
* @nb: the notifier description object
|
||||
*
|
||||
* This function unregisters volume notifier @nm and returns zero in case of
|
||||
* success and a negative error code in case of failure.
|
||||
*/
|
||||
int ubi_unregister_volume_notifier(struct notifier_block *nb)
|
||||
{
|
||||
return blocking_notifier_chain_unregister(&ubi_notifiers, nb);
|
||||
}
|
||||
EXPORT_SYMBOL_GPL(ubi_unregister_volume_notifier);
|
||||
#endif
|
||||
144
u-boot/drivers/mtd/ubi/misc.c
Normal file
144
u-boot/drivers/mtd/ubi/misc.c
Normal file
@@ -0,0 +1,144 @@
|
||||
/*
|
||||
* Copyright (c) International Business Machines Corp., 2006
|
||||
*
|
||||
* SPDX-License-Identifier: GPL-2.0+
|
||||
*
|
||||
* Author: Artem Bityutskiy (Битюцкий Артём)
|
||||
*/
|
||||
|
||||
/* Here we keep miscellaneous functions which are used all over the UBI code */
|
||||
|
||||
#include <ubi_uboot.h>
|
||||
#include "ubi.h"
|
||||
|
||||
/**
|
||||
* calc_data_len - calculate how much real data is stored in a buffer.
|
||||
* @ubi: UBI device description object
|
||||
* @buf: a buffer with the contents of the physical eraseblock
|
||||
* @length: the buffer length
|
||||
*
|
||||
* This function calculates how much "real data" is stored in @buf and returnes
|
||||
* the length. Continuous 0xFF bytes at the end of the buffer are not
|
||||
* considered as "real data".
|
||||
*/
|
||||
int ubi_calc_data_len(const struct ubi_device *ubi, const void *buf,
|
||||
int length)
|
||||
{
|
||||
int i;
|
||||
|
||||
ubi_assert(!(length & (ubi->min_io_size - 1)));
|
||||
|
||||
for (i = length - 1; i >= 0; i--)
|
||||
if (((const uint8_t *)buf)[i] != 0xFF)
|
||||
break;
|
||||
|
||||
/* The resulting length must be aligned to the minimum flash I/O size */
|
||||
length = ALIGN(i + 1, ubi->min_io_size);
|
||||
return length;
|
||||
}
|
||||
|
||||
/**
|
||||
* ubi_check_volume - check the contents of a static volume.
|
||||
* @ubi: UBI device description object
|
||||
* @vol_id: ID of the volume to check
|
||||
*
|
||||
* This function checks if static volume @vol_id is corrupted by fully reading
|
||||
* it and checking data CRC. This function returns %0 if the volume is not
|
||||
* corrupted, %1 if it is corrupted and a negative error code in case of
|
||||
* failure. Dynamic volumes are not checked and zero is returned immediately.
|
||||
*/
|
||||
int ubi_check_volume(struct ubi_device *ubi, int vol_id)
|
||||
{
|
||||
void *buf;
|
||||
int err = 0, i;
|
||||
struct ubi_volume *vol = ubi->volumes[vol_id];
|
||||
|
||||
if (vol->vol_type != UBI_STATIC_VOLUME)
|
||||
return 0;
|
||||
|
||||
buf = vmalloc(vol->usable_leb_size);
|
||||
if (!buf)
|
||||
return -ENOMEM;
|
||||
|
||||
for (i = 0; i < vol->used_ebs; i++) {
|
||||
int size;
|
||||
|
||||
cond_resched();
|
||||
|
||||
if (i == vol->used_ebs - 1)
|
||||
size = vol->last_eb_bytes;
|
||||
else
|
||||
size = vol->usable_leb_size;
|
||||
|
||||
err = ubi_eba_read_leb(ubi, vol, i, buf, 0, size, 1);
|
||||
if (err) {
|
||||
if (mtd_is_eccerr(err))
|
||||
err = 1;
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
vfree(buf);
|
||||
return err;
|
||||
}
|
||||
|
||||
/**
|
||||
* ubi_update_reserved - update bad eraseblock handling accounting data.
|
||||
* @ubi: UBI device description object
|
||||
*
|
||||
* This function calculates the gap between current number of PEBs reserved for
|
||||
* bad eraseblock handling and the required level of PEBs that must be
|
||||
* reserved, and if necessary, reserves more PEBs to fill that gap, according
|
||||
* to availability. Should be called with ubi->volumes_lock held.
|
||||
*/
|
||||
void ubi_update_reserved(struct ubi_device *ubi)
|
||||
{
|
||||
int need = ubi->beb_rsvd_level - ubi->beb_rsvd_pebs;
|
||||
|
||||
if (need <= 0 || ubi->avail_pebs == 0)
|
||||
return;
|
||||
|
||||
need = min_t(int, need, ubi->avail_pebs);
|
||||
ubi->avail_pebs -= need;
|
||||
ubi->rsvd_pebs += need;
|
||||
ubi->beb_rsvd_pebs += need;
|
||||
ubi_msg(ubi, "reserved more %d PEBs for bad PEB handling", need);
|
||||
}
|
||||
|
||||
/**
|
||||
* ubi_calculate_reserved - calculate how many PEBs must be reserved for bad
|
||||
* eraseblock handling.
|
||||
* @ubi: UBI device description object
|
||||
*/
|
||||
void ubi_calculate_reserved(struct ubi_device *ubi)
|
||||
{
|
||||
/*
|
||||
* Calculate the actual number of PEBs currently needed to be reserved
|
||||
* for future bad eraseblock handling.
|
||||
*/
|
||||
ubi->beb_rsvd_level = ubi->bad_peb_limit - ubi->bad_peb_count;
|
||||
if (ubi->beb_rsvd_level < 0) {
|
||||
ubi->beb_rsvd_level = 0;
|
||||
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)",
|
||||
ubi->bad_peb_count, ubi->bad_peb_limit);
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* ubi_check_pattern - check if buffer contains only a certain byte pattern.
|
||||
* @buf: buffer to check
|
||||
* @patt: the pattern to check
|
||||
* @size: buffer size in bytes
|
||||
*
|
||||
* This function returns %1 in there are only @patt bytes in @buf, and %0 if
|
||||
* something else was also found.
|
||||
*/
|
||||
int ubi_check_pattern(const void *buf, uint8_t patt, int size)
|
||||
{
|
||||
int i;
|
||||
|
||||
for (i = 0; i < size; i++)
|
||||
if (((const uint8_t *)buf)[i] != patt)
|
||||
return 0;
|
||||
return 1;
|
||||
}
|
||||
505
u-boot/drivers/mtd/ubi/ubi-media.h
Normal file
505
u-boot/drivers/mtd/ubi/ubi-media.h
Normal file
@@ -0,0 +1,505 @@
|
||||
/*
|
||||
* Copyright (c) International Business Machines Corp., 2006
|
||||
*
|
||||
* SPDX-License-Identifier: GPL-2.0+
|
||||
*
|
||||
* Authors: Artem Bityutskiy (Битюцкий Артём)
|
||||
* Thomas Gleixner
|
||||
* Frank Haverkamp
|
||||
* Oliver Lohmann
|
||||
* Andreas Arnez
|
||||
*/
|
||||
|
||||
/*
|
||||
* This file defines the layout of UBI headers and all the other UBI on-flash
|
||||
* data structures.
|
||||
*/
|
||||
|
||||
#ifndef __UBI_MEDIA_H__
|
||||
#define __UBI_MEDIA_H__
|
||||
|
||||
#include <asm/byteorder.h>
|
||||
|
||||
/* The version of UBI images supported by this implementation */
|
||||
#define UBI_VERSION 1
|
||||
|
||||
/* The highest erase counter value supported by this implementation */
|
||||
#define UBI_MAX_ERASECOUNTER 0x7FFFFFFF
|
||||
|
||||
/* The initial CRC32 value used when calculating CRC checksums */
|
||||
#define UBI_CRC32_INIT 0xFFFFFFFFU
|
||||
|
||||
/* Erase counter header magic number (ASCII "UBI#") */
|
||||
#define UBI_EC_HDR_MAGIC 0x55424923
|
||||
/* Volume identifier header magic number (ASCII "UBI!") */
|
||||
#define UBI_VID_HDR_MAGIC 0x55424921
|
||||
|
||||
/*
|
||||
* Volume type constants used in the volume identifier header.
|
||||
*
|
||||
* @UBI_VID_DYNAMIC: dynamic volume
|
||||
* @UBI_VID_STATIC: static volume
|
||||
*/
|
||||
enum {
|
||||
UBI_VID_DYNAMIC = 1,
|
||||
UBI_VID_STATIC = 2
|
||||
};
|
||||
|
||||
/*
|
||||
* Volume flags used in the volume table record.
|
||||
*
|
||||
* @UBI_VTBL_AUTORESIZE_FLG: auto-resize this volume
|
||||
*
|
||||
* %UBI_VTBL_AUTORESIZE_FLG flag can be set only for one volume in the volume
|
||||
* table. UBI automatically re-sizes the volume which has this flag and makes
|
||||
* the volume to be of largest possible size. This means that if after the
|
||||
* initialization UBI finds out that there are available physical eraseblocks
|
||||
* present on the device, it automatically appends all of them to the volume
|
||||
* (the physical eraseblocks reserved for bad eraseblocks handling and other
|
||||
* reserved physical eraseblocks are not taken). So, if there is a volume with
|
||||
* the %UBI_VTBL_AUTORESIZE_FLG flag set, the amount of available logical
|
||||
* eraseblocks will be zero after UBI is loaded, because all of them will be
|
||||
* reserved for this volume. Note, the %UBI_VTBL_AUTORESIZE_FLG bit is cleared
|
||||
* after the volume had been initialized.
|
||||
*
|
||||
* The auto-resize feature is useful for device production purposes. For
|
||||
* example, different NAND flash chips may have different amount of initial bad
|
||||
* eraseblocks, depending of particular chip instance. Manufacturers of NAND
|
||||
* chips usually guarantee that the amount of initial bad eraseblocks does not
|
||||
* exceed certain percent, e.g. 2%. When one creates an UBI image which will be
|
||||
* flashed to the end devices in production, he does not know the exact amount
|
||||
* of good physical eraseblocks the NAND chip on the device will have, but this
|
||||
* number is required to calculate the volume sized and put them to the volume
|
||||
* table of the UBI image. In this case, one of the volumes (e.g., the one
|
||||
* which will store the root file system) is marked as "auto-resizable", and
|
||||
* UBI will adjust its size on the first boot if needed.
|
||||
*
|
||||
* Note, first UBI reserves some amount of physical eraseblocks for bad
|
||||
* eraseblock handling, and then re-sizes the volume, not vice-versa. This
|
||||
* means that the pool of reserved physical eraseblocks will always be present.
|
||||
*/
|
||||
enum {
|
||||
UBI_VTBL_AUTORESIZE_FLG = 0x01,
|
||||
};
|
||||
|
||||
/*
|
||||
* Compatibility constants used by internal volumes.
|
||||
*
|
||||
* @UBI_COMPAT_DELETE: delete this internal volume before anything is written
|
||||
* to the flash
|
||||
* @UBI_COMPAT_RO: attach this device in read-only mode
|
||||
* @UBI_COMPAT_PRESERVE: preserve this internal volume - do not touch its
|
||||
* physical eraseblocks, don't allow the wear-leveling
|
||||
* sub-system to move them
|
||||
* @UBI_COMPAT_REJECT: reject this UBI image
|
||||
*/
|
||||
enum {
|
||||
UBI_COMPAT_DELETE = 1,
|
||||
UBI_COMPAT_RO = 2,
|
||||
UBI_COMPAT_PRESERVE = 4,
|
||||
UBI_COMPAT_REJECT = 5
|
||||
};
|
||||
|
||||
/* Sizes of UBI headers */
|
||||
#define UBI_EC_HDR_SIZE sizeof(struct ubi_ec_hdr)
|
||||
#define UBI_VID_HDR_SIZE sizeof(struct ubi_vid_hdr)
|
||||
|
||||
/* Sizes of UBI headers without the ending CRC */
|
||||
#define UBI_EC_HDR_SIZE_CRC (UBI_EC_HDR_SIZE - sizeof(__be32))
|
||||
#define UBI_VID_HDR_SIZE_CRC (UBI_VID_HDR_SIZE - sizeof(__be32))
|
||||
|
||||
/**
|
||||
* struct ubi_ec_hdr - UBI erase counter header.
|
||||
* @magic: erase counter header magic number (%UBI_EC_HDR_MAGIC)
|
||||
* @version: version of UBI implementation which is supposed to accept this
|
||||
* UBI image
|
||||
* @padding1: reserved for future, zeroes
|
||||
* @ec: the erase counter
|
||||
* @vid_hdr_offset: where the VID header starts
|
||||
* @data_offset: where the user data start
|
||||
* @image_seq: image sequence number
|
||||
* @padding2: reserved for future, zeroes
|
||||
* @hdr_crc: erase counter header CRC checksum
|
||||
*
|
||||
* The erase counter header takes 64 bytes and has a plenty of unused space for
|
||||
* future usage. The unused fields are zeroed. The @version field is used to
|
||||
* indicate the version of UBI implementation which is supposed to be able to
|
||||
* work with this UBI image. If @version is greater than the current UBI
|
||||
* version, the image is rejected. This may be useful in future if something
|
||||
* is changed radically. This field is duplicated in the volume identifier
|
||||
* header.
|
||||
*
|
||||
* The @vid_hdr_offset and @data_offset fields contain the offset of the the
|
||||
* volume identifier header and user data, relative to the beginning of the
|
||||
* physical eraseblock. These values have to be the same for all physical
|
||||
* eraseblocks.
|
||||
*
|
||||
* The @image_seq field is used to validate a UBI image that has been prepared
|
||||
* for a UBI device. The @image_seq value can be any value, but it must be the
|
||||
* same on all eraseblocks. UBI will ensure that all new erase counter headers
|
||||
* also contain this value, and will check the value when attaching the flash.
|
||||
* One way to make use of @image_seq is to increase its value by one every time
|
||||
* an image is flashed over an existing image, then, if the flashing does not
|
||||
* complete, UBI will detect the error when attaching the media.
|
||||
*/
|
||||
struct ubi_ec_hdr {
|
||||
__be32 magic;
|
||||
__u8 version;
|
||||
__u8 padding1[3];
|
||||
__be64 ec; /* Warning: the current limit is 31-bit anyway! */
|
||||
__be32 vid_hdr_offset;
|
||||
__be32 data_offset;
|
||||
__be32 image_seq;
|
||||
__u8 padding2[32];
|
||||
__be32 hdr_crc;
|
||||
} __packed;
|
||||
|
||||
/**
|
||||
* struct ubi_vid_hdr - on-flash UBI volume identifier header.
|
||||
* @magic: volume identifier header magic number (%UBI_VID_HDR_MAGIC)
|
||||
* @version: UBI implementation version which is supposed to accept this UBI
|
||||
* image (%UBI_VERSION)
|
||||
* @vol_type: volume type (%UBI_VID_DYNAMIC or %UBI_VID_STATIC)
|
||||
* @copy_flag: if this logical eraseblock was copied from another physical
|
||||
* eraseblock (for wear-leveling reasons)
|
||||
* @compat: compatibility of this volume (%0, %UBI_COMPAT_DELETE,
|
||||
* %UBI_COMPAT_IGNORE, %UBI_COMPAT_PRESERVE, or %UBI_COMPAT_REJECT)
|
||||
* @vol_id: ID of this volume
|
||||
* @lnum: logical eraseblock number
|
||||
* @padding1: reserved for future, zeroes
|
||||
* @data_size: how many bytes of data this logical eraseblock contains
|
||||
* @used_ebs: total number of used logical eraseblocks in this volume
|
||||
* @data_pad: how many bytes at the end of this physical eraseblock are not
|
||||
* used
|
||||
* @data_crc: CRC checksum of the data stored in this logical eraseblock
|
||||
* @padding2: reserved for future, zeroes
|
||||
* @sqnum: sequence number
|
||||
* @padding3: reserved for future, zeroes
|
||||
* @hdr_crc: volume identifier header CRC checksum
|
||||
*
|
||||
* The @sqnum is the value of the global sequence counter at the time when this
|
||||
* VID header was created. The global sequence counter is incremented each time
|
||||
* UBI writes a new VID header to the flash, i.e. when it maps a logical
|
||||
* eraseblock to a new physical eraseblock. The global sequence counter is an
|
||||
* unsigned 64-bit integer and we assume it never overflows. The @sqnum
|
||||
* (sequence number) is used to distinguish between older and newer versions of
|
||||
* logical eraseblocks.
|
||||
*
|
||||
* There are 2 situations when there may be more than one physical eraseblock
|
||||
* corresponding to the same logical eraseblock, i.e., having the same @vol_id
|
||||
* and @lnum values in the volume identifier header. Suppose we have a logical
|
||||
* eraseblock L and it is mapped to the physical eraseblock P.
|
||||
*
|
||||
* 1. Because UBI may erase physical eraseblocks asynchronously, the following
|
||||
* situation is possible: L is asynchronously erased, so P is scheduled for
|
||||
* erasure, then L is written to,i.e. mapped to another physical eraseblock P1,
|
||||
* so P1 is written to, then an unclean reboot happens. Result - there are 2
|
||||
* physical eraseblocks P and P1 corresponding to the same logical eraseblock
|
||||
* L. But P1 has greater sequence number, so UBI picks P1 when it attaches the
|
||||
* flash.
|
||||
*
|
||||
* 2. From time to time UBI moves logical eraseblocks to other physical
|
||||
* eraseblocks for wear-leveling reasons. If, for example, UBI moves L from P
|
||||
* to P1, and an unclean reboot happens before P is physically erased, there
|
||||
* are two physical eraseblocks P and P1 corresponding to L and UBI has to
|
||||
* select one of them when the flash is attached. The @sqnum field says which
|
||||
* PEB is the original (obviously P will have lower @sqnum) and the copy. But
|
||||
* it is not enough to select the physical eraseblock with the higher sequence
|
||||
* number, because the unclean reboot could have happen in the middle of the
|
||||
* copying process, so the data in P is corrupted. It is also not enough to
|
||||
* just select the physical eraseblock with lower sequence number, because the
|
||||
* data there may be old (consider a case if more data was added to P1 after
|
||||
* the copying). Moreover, the unclean reboot may happen when the erasure of P
|
||||
* was just started, so it result in unstable P, which is "mostly" OK, but
|
||||
* still has unstable bits.
|
||||
*
|
||||
* UBI uses the @copy_flag field to indicate that this logical eraseblock is a
|
||||
* copy. UBI also calculates data CRC when the data is moved and stores it at
|
||||
* the @data_crc field of the copy (P1). So when UBI needs to pick one physical
|
||||
* eraseblock of two (P or P1), the @copy_flag of the newer one (P1) is
|
||||
* examined. If it is cleared, the situation* is simple and the newer one is
|
||||
* picked. If it is set, the data CRC of the copy (P1) is examined. If the CRC
|
||||
* checksum is correct, this physical eraseblock is selected (P1). Otherwise
|
||||
* the older one (P) is selected.
|
||||
*
|
||||
* There are 2 sorts of volumes in UBI: user volumes and internal volumes.
|
||||
* Internal volumes are not seen from outside and are used for various internal
|
||||
* UBI purposes. In this implementation there is only one internal volume - the
|
||||
* layout volume. Internal volumes are the main mechanism of UBI extensions.
|
||||
* For example, in future one may introduce a journal internal volume. Internal
|
||||
* volumes have their own reserved range of IDs.
|
||||
*
|
||||
* The @compat field is only used for internal volumes and contains the "degree
|
||||
* of their compatibility". It is always zero for user volumes. This field
|
||||
* provides a mechanism to introduce UBI extensions and to be still compatible
|
||||
* with older UBI binaries. For example, if someone introduced a journal in
|
||||
* future, he would probably use %UBI_COMPAT_DELETE compatibility for the
|
||||
* journal volume. And in this case, older UBI binaries, which know nothing
|
||||
* about the journal volume, would just delete this volume and work perfectly
|
||||
* fine. This is similar to what Ext2fs does when it is fed by an Ext3fs image
|
||||
* - it just ignores the Ext3fs journal.
|
||||
*
|
||||
* The @data_crc field contains the CRC checksum of the contents of the logical
|
||||
* eraseblock if this is a static volume. In case of dynamic volumes, it does
|
||||
* not contain the CRC checksum as a rule. The only exception is when the
|
||||
* data of the physical eraseblock was moved by the wear-leveling sub-system,
|
||||
* then the wear-leveling sub-system calculates the data CRC and stores it in
|
||||
* the @data_crc field. And of course, the @copy_flag is %in this case.
|
||||
*
|
||||
* The @data_size field is used only for static volumes because UBI has to know
|
||||
* how many bytes of data are stored in this eraseblock. For dynamic volumes,
|
||||
* this field usually contains zero. The only exception is when the data of the
|
||||
* physical eraseblock was moved to another physical eraseblock for
|
||||
* wear-leveling reasons. In this case, UBI calculates CRC checksum of the
|
||||
* contents and uses both @data_crc and @data_size fields. In this case, the
|
||||
* @data_size field contains data size.
|
||||
*
|
||||
* The @used_ebs field is used only for static volumes and indicates how many
|
||||
* eraseblocks the data of the volume takes. For dynamic volumes this field is
|
||||
* not used and always contains zero.
|
||||
*
|
||||
* The @data_pad is calculated when volumes are created using the alignment
|
||||
* parameter. So, effectively, the @data_pad field reduces the size of logical
|
||||
* eraseblocks of this volume. This is very handy when one uses block-oriented
|
||||
* software (say, cramfs) on top of the UBI volume.
|
||||
*/
|
||||
struct ubi_vid_hdr {
|
||||
__be32 magic;
|
||||
__u8 version;
|
||||
__u8 vol_type;
|
||||
__u8 copy_flag;
|
||||
__u8 compat;
|
||||
__be32 vol_id;
|
||||
__be32 lnum;
|
||||
__u8 padding1[4];
|
||||
__be32 data_size;
|
||||
__be32 used_ebs;
|
||||
__be32 data_pad;
|
||||
__be32 data_crc;
|
||||
__u8 padding2[4];
|
||||
__be64 sqnum;
|
||||
__u8 padding3[12];
|
||||
__be32 hdr_crc;
|
||||
} __packed;
|
||||
|
||||
/* Internal UBI volumes count */
|
||||
#define UBI_INT_VOL_COUNT 1
|
||||
|
||||
/*
|
||||
* Starting ID of internal volumes: 0x7fffefff.
|
||||
* There is reserved room for 4096 internal volumes.
|
||||
*/
|
||||
#define UBI_INTERNAL_VOL_START (0x7FFFFFFF - 4096)
|
||||
|
||||
/* The layout volume contains the volume table */
|
||||
|
||||
#define UBI_LAYOUT_VOLUME_ID UBI_INTERNAL_VOL_START
|
||||
#define UBI_LAYOUT_VOLUME_TYPE UBI_VID_DYNAMIC
|
||||
#define UBI_LAYOUT_VOLUME_ALIGN 1
|
||||
#define UBI_LAYOUT_VOLUME_EBS 2
|
||||
#define UBI_LAYOUT_VOLUME_NAME "layout volume"
|
||||
#define UBI_LAYOUT_VOLUME_COMPAT UBI_COMPAT_REJECT
|
||||
|
||||
/* The maximum number of volumes per one UBI device */
|
||||
#define UBI_MAX_VOLUMES 128
|
||||
|
||||
/* The maximum volume name length */
|
||||
#define UBI_VOL_NAME_MAX 127
|
||||
|
||||
/* Size of the volume table record */
|
||||
#define UBI_VTBL_RECORD_SIZE sizeof(struct ubi_vtbl_record)
|
||||
|
||||
/* Size of the volume table record without the ending CRC */
|
||||
#define UBI_VTBL_RECORD_SIZE_CRC (UBI_VTBL_RECORD_SIZE - sizeof(__be32))
|
||||
|
||||
/**
|
||||
* struct ubi_vtbl_record - a record in the volume table.
|
||||
* @reserved_pebs: how many physical eraseblocks are reserved for this volume
|
||||
* @alignment: volume alignment
|
||||
* @data_pad: how many bytes are unused at the end of the each physical
|
||||
* eraseblock to satisfy the requested alignment
|
||||
* @vol_type: volume type (%UBI_DYNAMIC_VOLUME or %UBI_STATIC_VOLUME)
|
||||
* @upd_marker: if volume update was started but not finished
|
||||
* @name_len: volume name length
|
||||
* @name: the volume name
|
||||
* @flags: volume flags (%UBI_VTBL_AUTORESIZE_FLG)
|
||||
* @padding: reserved, zeroes
|
||||
* @crc: a CRC32 checksum of the record
|
||||
*
|
||||
* The volume table records are stored in the volume table, which is stored in
|
||||
* the layout volume. The layout volume consists of 2 logical eraseblock, each
|
||||
* of which contains a copy of the volume table (i.e., the volume table is
|
||||
* duplicated). The volume table is an array of &struct ubi_vtbl_record
|
||||
* objects indexed by the volume ID.
|
||||
*
|
||||
* If the size of the logical eraseblock is large enough to fit
|
||||
* %UBI_MAX_VOLUMES records, the volume table contains %UBI_MAX_VOLUMES
|
||||
* records. Otherwise, it contains as many records as it can fit (i.e., size of
|
||||
* logical eraseblock divided by sizeof(struct ubi_vtbl_record)).
|
||||
*
|
||||
* The @upd_marker flag is used to implement volume update. It is set to %1
|
||||
* before update and set to %0 after the update. So if the update operation was
|
||||
* interrupted, UBI knows that the volume is corrupted.
|
||||
*
|
||||
* The @alignment field is specified when the volume is created and cannot be
|
||||
* later changed. It may be useful, for example, when a block-oriented file
|
||||
* system works on top of UBI. The @data_pad field is calculated using the
|
||||
* logical eraseblock size and @alignment. The alignment must be multiple to the
|
||||
* minimal flash I/O unit. If @alignment is 1, all the available space of
|
||||
* the physical eraseblocks is used.
|
||||
*
|
||||
* Empty records contain all zeroes and the CRC checksum of those zeroes.
|
||||
*/
|
||||
struct ubi_vtbl_record {
|
||||
__be32 reserved_pebs;
|
||||
__be32 alignment;
|
||||
__be32 data_pad;
|
||||
__u8 vol_type;
|
||||
__u8 upd_marker;
|
||||
__be16 name_len;
|
||||
#ifndef __UBOOT__
|
||||
__u8 name[UBI_VOL_NAME_MAX+1];
|
||||
#else
|
||||
char name[UBI_VOL_NAME_MAX+1];
|
||||
#endif
|
||||
__u8 flags;
|
||||
__u8 padding[23];
|
||||
__be32 crc;
|
||||
} __packed;
|
||||
|
||||
/* UBI fastmap on-flash data structures */
|
||||
|
||||
#define UBI_FM_SB_VOLUME_ID (UBI_LAYOUT_VOLUME_ID + 1)
|
||||
#define UBI_FM_DATA_VOLUME_ID (UBI_LAYOUT_VOLUME_ID + 2)
|
||||
|
||||
/* fastmap on-flash data structure format version */
|
||||
#define UBI_FM_FMT_VERSION 1
|
||||
|
||||
#define UBI_FM_SB_MAGIC 0x7B11D69F
|
||||
#define UBI_FM_HDR_MAGIC 0xD4B82EF7
|
||||
#define UBI_FM_VHDR_MAGIC 0xFA370ED1
|
||||
#define UBI_FM_POOL_MAGIC 0x67AF4D08
|
||||
#define UBI_FM_EBA_MAGIC 0xf0c040a8
|
||||
|
||||
/* A fastmap supber block can be located between PEB 0 and
|
||||
* UBI_FM_MAX_START */
|
||||
#define UBI_FM_MAX_START 64
|
||||
|
||||
/* A fastmap can use up to UBI_FM_MAX_BLOCKS PEBs */
|
||||
#define UBI_FM_MAX_BLOCKS 32
|
||||
|
||||
/* 5% of the total number of PEBs have to be scanned while attaching
|
||||
* from a fastmap.
|
||||
* But the size of this pool is limited to be between UBI_FM_MIN_POOL_SIZE and
|
||||
* UBI_FM_MAX_POOL_SIZE */
|
||||
#define UBI_FM_MIN_POOL_SIZE 8
|
||||
#define UBI_FM_MAX_POOL_SIZE 256
|
||||
|
||||
/**
|
||||
* struct ubi_fm_sb - UBI fastmap super block
|
||||
* @magic: fastmap super block magic number (%UBI_FM_SB_MAGIC)
|
||||
* @version: format version of this fastmap
|
||||
* @data_crc: CRC over the fastmap data
|
||||
* @used_blocks: number of PEBs used by this fastmap
|
||||
* @block_loc: an array containing the location of all PEBs of the fastmap
|
||||
* @block_ec: the erase counter of each used PEB
|
||||
* @sqnum: highest sequence number value at the time while taking the fastmap
|
||||
*
|
||||
*/
|
||||
struct ubi_fm_sb {
|
||||
__be32 magic;
|
||||
__u8 version;
|
||||
__u8 padding1[3];
|
||||
__be32 data_crc;
|
||||
__be32 used_blocks;
|
||||
__be32 block_loc[UBI_FM_MAX_BLOCKS];
|
||||
__be32 block_ec[UBI_FM_MAX_BLOCKS];
|
||||
__be64 sqnum;
|
||||
__u8 padding2[32];
|
||||
} __packed;
|
||||
|
||||
/**
|
||||
* struct ubi_fm_hdr - header of the fastmap data set
|
||||
* @magic: fastmap header magic number (%UBI_FM_HDR_MAGIC)
|
||||
* @free_peb_count: number of free PEBs known by this fastmap
|
||||
* @used_peb_count: number of used PEBs known by this fastmap
|
||||
* @scrub_peb_count: number of to be scrubbed PEBs known by this fastmap
|
||||
* @bad_peb_count: number of bad PEBs known by this fastmap
|
||||
* @erase_peb_count: number of bad PEBs which have to be erased
|
||||
* @vol_count: number of UBI volumes known by this fastmap
|
||||
*/
|
||||
struct ubi_fm_hdr {
|
||||
__be32 magic;
|
||||
__be32 free_peb_count;
|
||||
__be32 used_peb_count;
|
||||
__be32 scrub_peb_count;
|
||||
__be32 bad_peb_count;
|
||||
__be32 erase_peb_count;
|
||||
__be32 vol_count;
|
||||
__u8 padding[4];
|
||||
} __packed;
|
||||
|
||||
/* struct ubi_fm_hdr is followed by two struct ubi_fm_scan_pool */
|
||||
|
||||
/**
|
||||
* struct ubi_fm_scan_pool - Fastmap pool PEBs to be scanned while attaching
|
||||
* @magic: pool magic numer (%UBI_FM_POOL_MAGIC)
|
||||
* @size: current pool size
|
||||
* @max_size: maximal pool size
|
||||
* @pebs: an array containing the location of all PEBs in this pool
|
||||
*/
|
||||
struct ubi_fm_scan_pool {
|
||||
__be32 magic;
|
||||
__be16 size;
|
||||
__be16 max_size;
|
||||
__be32 pebs[UBI_FM_MAX_POOL_SIZE];
|
||||
__be32 padding[4];
|
||||
} __packed;
|
||||
|
||||
/* ubi_fm_scan_pool is followed by nfree+nused struct ubi_fm_ec records */
|
||||
|
||||
/**
|
||||
* struct ubi_fm_ec - stores the erase counter of a PEB
|
||||
* @pnum: PEB number
|
||||
* @ec: ec of this PEB
|
||||
*/
|
||||
struct ubi_fm_ec {
|
||||
__be32 pnum;
|
||||
__be32 ec;
|
||||
} __packed;
|
||||
|
||||
/**
|
||||
* struct ubi_fm_volhdr - Fastmap volume header
|
||||
* it identifies the start of an eba table
|
||||
* @magic: Fastmap volume header magic number (%UBI_FM_VHDR_MAGIC)
|
||||
* @vol_id: volume id of the fastmapped volume
|
||||
* @vol_type: type of the fastmapped volume
|
||||
* @data_pad: data_pad value of the fastmapped volume
|
||||
* @used_ebs: number of used LEBs within this volume
|
||||
* @last_eb_bytes: number of bytes used in the last LEB
|
||||
*/
|
||||
struct ubi_fm_volhdr {
|
||||
__be32 magic;
|
||||
__be32 vol_id;
|
||||
__u8 vol_type;
|
||||
__u8 padding1[3];
|
||||
__be32 data_pad;
|
||||
__be32 used_ebs;
|
||||
__be32 last_eb_bytes;
|
||||
__u8 padding2[8];
|
||||
} __packed;
|
||||
|
||||
/* struct ubi_fm_volhdr is followed by one struct ubi_fm_eba records */
|
||||
|
||||
/**
|
||||
* struct ubi_fm_eba - denotes an association beween a PEB and LEB
|
||||
* @magic: EBA table magic number
|
||||
* @reserved_pebs: number of table entries
|
||||
* @pnum: PEB number of LEB (LEB is the index)
|
||||
*/
|
||||
struct ubi_fm_eba {
|
||||
__be32 magic;
|
||||
__be32 reserved_pebs;
|
||||
__be32 pnum[0];
|
||||
} __packed;
|
||||
#endif /* !__UBI_MEDIA_H__ */
|
||||
1124
u-boot/drivers/mtd/ubi/ubi.h
Normal file
1124
u-boot/drivers/mtd/ubi/ubi.h
Normal file
File diff suppressed because it is too large
Load Diff
432
u-boot/drivers/mtd/ubi/upd.c
Normal file
432
u-boot/drivers/mtd/ubi/upd.c
Normal file
@@ -0,0 +1,432 @@
|
||||
/*
|
||||
* Copyright (c) International Business Machines Corp., 2006
|
||||
* Copyright (c) Nokia Corporation, 2006
|
||||
*
|
||||
* SPDX-License-Identifier: GPL-2.0+
|
||||
*
|
||||
* Author: Artem Bityutskiy (Битюцкий Артём)
|
||||
*
|
||||
* Jan 2007: Alexander Schmidt, hacked per-volume update.
|
||||
*/
|
||||
|
||||
/*
|
||||
* This file contains implementation of the volume update and atomic LEB change
|
||||
* functionality.
|
||||
*
|
||||
* The update operation is based on the per-volume update marker which is
|
||||
* stored in the volume table. The update marker is set before the update
|
||||
* starts, and removed after the update has been finished. So if the update was
|
||||
* interrupted by an unclean re-boot or due to some other reasons, the update
|
||||
* marker stays on the flash media and UBI finds it when it attaches the MTD
|
||||
* device next time. If the update marker is set for a volume, the volume is
|
||||
* treated as damaged and most I/O operations are prohibited. Only a new update
|
||||
* operation is allowed.
|
||||
*
|
||||
* Note, in general it is possible to implement the update operation as a
|
||||
* transaction with a roll-back capability.
|
||||
*/
|
||||
|
||||
#ifndef __UBOOT__
|
||||
#include <linux/uaccess.h>
|
||||
#else
|
||||
#include <div64.h>
|
||||
#include <ubi_uboot.h>
|
||||
#endif
|
||||
#include <linux/err.h>
|
||||
#include <linux/math64.h>
|
||||
|
||||
#include "ubi.h"
|
||||
|
||||
/**
|
||||
* set_update_marker - set update marker.
|
||||
* @ubi: UBI device description object
|
||||
* @vol: volume description object
|
||||
*
|
||||
* This function sets the update marker flag for volume @vol. Returns zero
|
||||
* in case of success and a negative error code in case of failure.
|
||||
*/
|
||||
static int set_update_marker(struct ubi_device *ubi, struct ubi_volume *vol)
|
||||
{
|
||||
int err;
|
||||
struct ubi_vtbl_record vtbl_rec;
|
||||
|
||||
dbg_gen("set update marker for volume %d", vol->vol_id);
|
||||
|
||||
if (vol->upd_marker) {
|
||||
ubi_assert(ubi->vtbl[vol->vol_id].upd_marker);
|
||||
dbg_gen("already set");
|
||||
return 0;
|
||||
}
|
||||
|
||||
vtbl_rec = ubi->vtbl[vol->vol_id];
|
||||
vtbl_rec.upd_marker = 1;
|
||||
|
||||
mutex_lock(&ubi->device_mutex);
|
||||
err = ubi_change_vtbl_record(ubi, vol->vol_id, &vtbl_rec);
|
||||
vol->upd_marker = 1;
|
||||
mutex_unlock(&ubi->device_mutex);
|
||||
return err;
|
||||
}
|
||||
|
||||
/**
|
||||
* clear_update_marker - clear update marker.
|
||||
* @ubi: UBI device description object
|
||||
* @vol: volume description object
|
||||
* @bytes: new data size in bytes
|
||||
*
|
||||
* This function clears the update marker for volume @vol, sets new volume
|
||||
* data size and clears the "corrupted" flag (static volumes only). Returns
|
||||
* zero in case of success and a negative error code in case of failure.
|
||||
*/
|
||||
static int clear_update_marker(struct ubi_device *ubi, struct ubi_volume *vol,
|
||||
long long bytes)
|
||||
{
|
||||
int err;
|
||||
struct ubi_vtbl_record vtbl_rec;
|
||||
|
||||
dbg_gen("clear update marker for volume %d", vol->vol_id);
|
||||
|
||||
vtbl_rec = ubi->vtbl[vol->vol_id];
|
||||
ubi_assert(vol->upd_marker && vtbl_rec.upd_marker);
|
||||
vtbl_rec.upd_marker = 0;
|
||||
|
||||
if (vol->vol_type == UBI_STATIC_VOLUME) {
|
||||
vol->corrupted = 0;
|
||||
vol->used_bytes = bytes;
|
||||
vol->used_ebs = div_u64_rem(bytes, vol->usable_leb_size,
|
||||
&vol->last_eb_bytes);
|
||||
if (vol->last_eb_bytes)
|
||||
vol->used_ebs += 1;
|
||||
else
|
||||
vol->last_eb_bytes = vol->usable_leb_size;
|
||||
}
|
||||
|
||||
mutex_lock(&ubi->device_mutex);
|
||||
err = ubi_change_vtbl_record(ubi, vol->vol_id, &vtbl_rec);
|
||||
vol->upd_marker = 0;
|
||||
mutex_unlock(&ubi->device_mutex);
|
||||
return err;
|
||||
}
|
||||
|
||||
/**
|
||||
* ubi_start_update - start volume update.
|
||||
* @ubi: UBI device description object
|
||||
* @vol: volume description object
|
||||
* @bytes: update bytes
|
||||
*
|
||||
* This function starts volume update operation. If @bytes is zero, the volume
|
||||
* is just wiped out. Returns zero in case of success and a negative error code
|
||||
* in case of failure.
|
||||
*/
|
||||
int ubi_start_update(struct ubi_device *ubi, struct ubi_volume *vol,
|
||||
long long bytes)
|
||||
{
|
||||
int i, err;
|
||||
|
||||
dbg_gen("start update of volume %d, %llu bytes", vol->vol_id, bytes);
|
||||
ubi_assert(!vol->updating && !vol->changing_leb);
|
||||
vol->updating = 1;
|
||||
|
||||
vol->upd_buf = vmalloc(ubi->leb_size);
|
||||
if (!vol->upd_buf)
|
||||
return -ENOMEM;
|
||||
|
||||
err = set_update_marker(ubi, vol);
|
||||
if (err)
|
||||
return err;
|
||||
|
||||
/* Before updating - wipe out the volume */
|
||||
for (i = 0; i < vol->reserved_pebs; i++) {
|
||||
err = ubi_eba_unmap_leb(ubi, vol, i);
|
||||
if (err)
|
||||
return err;
|
||||
}
|
||||
|
||||
if (bytes == 0) {
|
||||
err = ubi_wl_flush(ubi, UBI_ALL, UBI_ALL);
|
||||
if (err)
|
||||
return err;
|
||||
|
||||
err = clear_update_marker(ubi, vol, 0);
|
||||
if (err)
|
||||
return err;
|
||||
|
||||
vfree(vol->upd_buf);
|
||||
vol->updating = 0;
|
||||
return 0;
|
||||
}
|
||||
|
||||
vol->upd_ebs = div_u64(bytes + vol->usable_leb_size - 1,
|
||||
vol->usable_leb_size);
|
||||
vol->upd_bytes = bytes;
|
||||
vol->upd_received = 0;
|
||||
return 0;
|
||||
}
|
||||
|
||||
/**
|
||||
* ubi_start_leb_change - start atomic LEB change.
|
||||
* @ubi: UBI device description object
|
||||
* @vol: volume description object
|
||||
* @req: operation request
|
||||
*
|
||||
* This function starts atomic LEB change operation. Returns zero in case of
|
||||
* success and a negative error code in case of failure.
|
||||
*/
|
||||
int ubi_start_leb_change(struct ubi_device *ubi, struct ubi_volume *vol,
|
||||
const struct ubi_leb_change_req *req)
|
||||
{
|
||||
ubi_assert(!vol->updating && !vol->changing_leb);
|
||||
|
||||
dbg_gen("start changing LEB %d:%d, %u bytes",
|
||||
vol->vol_id, req->lnum, req->bytes);
|
||||
if (req->bytes == 0)
|
||||
return ubi_eba_atomic_leb_change(ubi, vol, req->lnum, NULL, 0);
|
||||
|
||||
vol->upd_bytes = req->bytes;
|
||||
vol->upd_received = 0;
|
||||
vol->changing_leb = 1;
|
||||
vol->ch_lnum = req->lnum;
|
||||
|
||||
vol->upd_buf = vmalloc(req->bytes);
|
||||
if (!vol->upd_buf)
|
||||
return -ENOMEM;
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
||||
/**
|
||||
* write_leb - write update data.
|
||||
* @ubi: UBI device description object
|
||||
* @vol: volume description object
|
||||
* @lnum: logical eraseblock number
|
||||
* @buf: data to write
|
||||
* @len: data size
|
||||
* @used_ebs: how many logical eraseblocks will this volume contain (static
|
||||
* volumes only)
|
||||
*
|
||||
* This function writes update data to corresponding logical eraseblock. In
|
||||
* case of dynamic volume, this function checks if the data contains 0xFF bytes
|
||||
* at the end. If yes, the 0xFF bytes are cut and not written. So if the whole
|
||||
* buffer contains only 0xFF bytes, the LEB is left unmapped.
|
||||
*
|
||||
* The reason why we skip the trailing 0xFF bytes in case of dynamic volume is
|
||||
* that we want to make sure that more data may be appended to the logical
|
||||
* eraseblock in future. Indeed, writing 0xFF bytes may have side effects and
|
||||
* this PEB won't be writable anymore. So if one writes the file-system image
|
||||
* to the UBI volume where 0xFFs mean free space - UBI makes sure this free
|
||||
* space is writable after the update.
|
||||
*
|
||||
* We do not do this for static volumes because they are read-only. But this
|
||||
* also cannot be done because we have to store per-LEB CRC and the correct
|
||||
* data length.
|
||||
*
|
||||
* This function returns zero in case of success and a negative error code in
|
||||
* case of failure.
|
||||
*/
|
||||
static int write_leb(struct ubi_device *ubi, struct ubi_volume *vol, int lnum,
|
||||
void *buf, int len, int used_ebs)
|
||||
{
|
||||
int err;
|
||||
|
||||
if (vol->vol_type == UBI_DYNAMIC_VOLUME) {
|
||||
int l = ALIGN(len, ubi->min_io_size);
|
||||
|
||||
memset(buf + len, 0xFF, l - len);
|
||||
len = ubi_calc_data_len(ubi, buf, l);
|
||||
if (len == 0) {
|
||||
dbg_gen("all %d bytes contain 0xFF - skip", len);
|
||||
return 0;
|
||||
}
|
||||
|
||||
err = ubi_eba_write_leb(ubi, vol, lnum, buf, 0, len);
|
||||
} else {
|
||||
/*
|
||||
* When writing static volume, and this is the last logical
|
||||
* eraseblock, the length (@len) does not have to be aligned to
|
||||
* the minimal flash I/O unit. The 'ubi_eba_write_leb_st()'
|
||||
* function accepts exact (unaligned) length and stores it in
|
||||
* the VID header. And it takes care of proper alignment by
|
||||
* padding the buffer. Here we just make sure the padding will
|
||||
* contain zeros, not random trash.
|
||||
*/
|
||||
memset(buf + len, 0, vol->usable_leb_size - len);
|
||||
err = ubi_eba_write_leb_st(ubi, vol, lnum, buf, len, used_ebs);
|
||||
}
|
||||
|
||||
return err;
|
||||
}
|
||||
|
||||
/**
|
||||
* ubi_more_update_data - write more update data.
|
||||
* @ubi: UBI device description object
|
||||
* @vol: volume description object
|
||||
* @buf: write data (user-space memory buffer)
|
||||
* @count: how much bytes to write
|
||||
*
|
||||
* This function writes more data to the volume which is being updated. It may
|
||||
* be called arbitrary number of times until all the update data arriveis. This
|
||||
* function returns %0 in case of success, number of bytes written during the
|
||||
* last call if the whole volume update has been successfully finished, and a
|
||||
* negative error code in case of failure.
|
||||
*/
|
||||
int ubi_more_update_data(struct ubi_device *ubi, struct ubi_volume *vol,
|
||||
const void __user *buf, int count)
|
||||
{
|
||||
#ifndef __UBOOT__
|
||||
int lnum, offs, err = 0, len, to_write = count;
|
||||
#else
|
||||
int lnum, err = 0, len, to_write = count;
|
||||
u32 offs;
|
||||
#endif
|
||||
|
||||
dbg_gen("write %d of %lld bytes, %lld already passed",
|
||||
count, vol->upd_bytes, vol->upd_received);
|
||||
|
||||
if (ubi->ro_mode)
|
||||
return -EROFS;
|
||||
|
||||
lnum = div_u64_rem(vol->upd_received, vol->usable_leb_size, &offs);
|
||||
if (vol->upd_received + count > vol->upd_bytes)
|
||||
to_write = count = vol->upd_bytes - vol->upd_received;
|
||||
|
||||
/*
|
||||
* When updating volumes, we accumulate whole logical eraseblock of
|
||||
* data and write it at once.
|
||||
*/
|
||||
if (offs != 0) {
|
||||
/*
|
||||
* This is a write to the middle of the logical eraseblock. We
|
||||
* copy the data to our update buffer and wait for more data or
|
||||
* flush it if the whole eraseblock is written or the update
|
||||
* is finished.
|
||||
*/
|
||||
|
||||
len = vol->usable_leb_size - offs;
|
||||
if (len > count)
|
||||
len = count;
|
||||
|
||||
err = copy_from_user(vol->upd_buf + offs, buf, len);
|
||||
if (err)
|
||||
return -EFAULT;
|
||||
|
||||
if (offs + len == vol->usable_leb_size ||
|
||||
vol->upd_received + len == vol->upd_bytes) {
|
||||
int flush_len = offs + len;
|
||||
|
||||
/*
|
||||
* OK, we gathered either the whole eraseblock or this
|
||||
* is the last chunk, it's time to flush the buffer.
|
||||
*/
|
||||
ubi_assert(flush_len <= vol->usable_leb_size);
|
||||
err = write_leb(ubi, vol, lnum, vol->upd_buf, flush_len,
|
||||
vol->upd_ebs);
|
||||
if (err)
|
||||
return err;
|
||||
}
|
||||
|
||||
vol->upd_received += len;
|
||||
count -= len;
|
||||
buf += len;
|
||||
lnum += 1;
|
||||
}
|
||||
|
||||
/*
|
||||
* If we've got more to write, let's continue. At this point we know we
|
||||
* are starting from the beginning of an eraseblock.
|
||||
*/
|
||||
while (count) {
|
||||
if (count > vol->usable_leb_size)
|
||||
len = vol->usable_leb_size;
|
||||
else
|
||||
len = count;
|
||||
|
||||
err = copy_from_user(vol->upd_buf, buf, len);
|
||||
if (err)
|
||||
return -EFAULT;
|
||||
|
||||
if (len == vol->usable_leb_size ||
|
||||
vol->upd_received + len == vol->upd_bytes) {
|
||||
err = write_leb(ubi, vol, lnum, vol->upd_buf,
|
||||
len, vol->upd_ebs);
|
||||
if (err)
|
||||
break;
|
||||
}
|
||||
|
||||
vol->upd_received += len;
|
||||
count -= len;
|
||||
lnum += 1;
|
||||
buf += len;
|
||||
}
|
||||
|
||||
ubi_assert(vol->upd_received <= vol->upd_bytes);
|
||||
if (vol->upd_received == vol->upd_bytes) {
|
||||
err = ubi_wl_flush(ubi, UBI_ALL, UBI_ALL);
|
||||
if (err)
|
||||
return err;
|
||||
/* The update is finished, clear the update marker */
|
||||
err = clear_update_marker(ubi, vol, vol->upd_bytes);
|
||||
if (err)
|
||||
return err;
|
||||
vol->updating = 0;
|
||||
err = to_write;
|
||||
vfree(vol->upd_buf);
|
||||
}
|
||||
|
||||
return err;
|
||||
}
|
||||
|
||||
/**
|
||||
* ubi_more_leb_change_data - accept more data for atomic LEB change.
|
||||
* @ubi: UBI device description object
|
||||
* @vol: volume description object
|
||||
* @buf: write data (user-space memory buffer)
|
||||
* @count: how much bytes to write
|
||||
*
|
||||
* This function accepts more data to the volume which is being under the
|
||||
* "atomic LEB change" operation. It may be called arbitrary number of times
|
||||
* until all data arrives. This function returns %0 in case of success, number
|
||||
* of bytes written during the last call if the whole "atomic LEB change"
|
||||
* operation has been successfully finished, and a negative error code in case
|
||||
* of failure.
|
||||
*/
|
||||
int ubi_more_leb_change_data(struct ubi_device *ubi, struct ubi_volume *vol,
|
||||
const void __user *buf, int count)
|
||||
{
|
||||
int err;
|
||||
|
||||
dbg_gen("write %d of %lld bytes, %lld already passed",
|
||||
count, vol->upd_bytes, vol->upd_received);
|
||||
|
||||
if (ubi->ro_mode)
|
||||
return -EROFS;
|
||||
|
||||
if (vol->upd_received + count > vol->upd_bytes)
|
||||
count = vol->upd_bytes - vol->upd_received;
|
||||
|
||||
err = copy_from_user(vol->upd_buf + vol->upd_received, buf, count);
|
||||
if (err)
|
||||
return -EFAULT;
|
||||
|
||||
vol->upd_received += count;
|
||||
|
||||
if (vol->upd_received == vol->upd_bytes) {
|
||||
int len = ALIGN((int)vol->upd_bytes, ubi->min_io_size);
|
||||
|
||||
memset(vol->upd_buf + vol->upd_bytes, 0xFF,
|
||||
len - vol->upd_bytes);
|
||||
len = ubi_calc_data_len(ubi, vol->upd_buf, len);
|
||||
err = ubi_eba_atomic_leb_change(ubi, vol, vol->ch_lnum,
|
||||
vol->upd_buf, len);
|
||||
if (err)
|
||||
return err;
|
||||
}
|
||||
|
||||
ubi_assert(vol->upd_received <= vol->upd_bytes);
|
||||
if (vol->upd_received == vol->upd_bytes) {
|
||||
vol->changing_leb = 0;
|
||||
err = count;
|
||||
vfree(vol->upd_buf);
|
||||
}
|
||||
|
||||
return err;
|
||||
}
|
||||
809
u-boot/drivers/mtd/ubi/vmt.c
Normal file
809
u-boot/drivers/mtd/ubi/vmt.c
Normal file
@@ -0,0 +1,809 @@
|
||||
/*
|
||||
* Copyright (c) International Business Machines Corp., 2006
|
||||
*
|
||||
* SPDX-License-Identifier: GPL-2.0+
|
||||
*
|
||||
* Author: Artem Bityutskiy (Битюцкий Артём)
|
||||
*/
|
||||
|
||||
/*
|
||||
* This file contains implementation of volume creation, deletion, updating and
|
||||
* resizing.
|
||||
*/
|
||||
|
||||
#ifndef __UBOOT__
|
||||
#include <linux/err.h>
|
||||
#include <linux/slab.h>
|
||||
#include <linux/export.h>
|
||||
#else
|
||||
#include <div64.h>
|
||||
#include <ubi_uboot.h>
|
||||
#endif
|
||||
#include <linux/math64.h>
|
||||
|
||||
#include "ubi.h"
|
||||
|
||||
static int self_check_volumes(struct ubi_device *ubi);
|
||||
|
||||
#ifndef __UBOOT__
|
||||
static ssize_t vol_attribute_show(struct device *dev,
|
||||
struct device_attribute *attr, char *buf);
|
||||
|
||||
/* Device attributes corresponding to files in '/<sysfs>/class/ubi/ubiX_Y' */
|
||||
static struct device_attribute attr_vol_reserved_ebs =
|
||||
__ATTR(reserved_ebs, S_IRUGO, vol_attribute_show, NULL);
|
||||
static struct device_attribute attr_vol_type =
|
||||
__ATTR(type, S_IRUGO, vol_attribute_show, NULL);
|
||||
static struct device_attribute attr_vol_name =
|
||||
__ATTR(name, S_IRUGO, vol_attribute_show, NULL);
|
||||
static struct device_attribute attr_vol_corrupted =
|
||||
__ATTR(corrupted, S_IRUGO, vol_attribute_show, NULL);
|
||||
static struct device_attribute attr_vol_alignment =
|
||||
__ATTR(alignment, S_IRUGO, vol_attribute_show, NULL);
|
||||
static struct device_attribute attr_vol_usable_eb_size =
|
||||
__ATTR(usable_eb_size, S_IRUGO, vol_attribute_show, NULL);
|
||||
static struct device_attribute attr_vol_data_bytes =
|
||||
__ATTR(data_bytes, S_IRUGO, vol_attribute_show, NULL);
|
||||
static struct device_attribute attr_vol_upd_marker =
|
||||
__ATTR(upd_marker, S_IRUGO, vol_attribute_show, NULL);
|
||||
|
||||
/*
|
||||
* "Show" method for files in '/<sysfs>/class/ubi/ubiX_Y/'.
|
||||
*
|
||||
* Consider a situation:
|
||||
* A. process 1 opens a sysfs file related to volume Y, say
|
||||
* /<sysfs>/class/ubi/ubiX_Y/reserved_ebs;
|
||||
* B. process 2 removes volume Y;
|
||||
* C. process 1 starts reading the /<sysfs>/class/ubi/ubiX_Y/reserved_ebs file;
|
||||
*
|
||||
* In this situation, this function will return %-ENODEV because it will find
|
||||
* out that the volume was removed from the @ubi->volumes array.
|
||||
*/
|
||||
static ssize_t vol_attribute_show(struct device *dev,
|
||||
struct device_attribute *attr, char *buf)
|
||||
{
|
||||
int ret;
|
||||
struct ubi_volume *vol = container_of(dev, struct ubi_volume, dev);
|
||||
struct ubi_device *ubi;
|
||||
|
||||
ubi = ubi_get_device(vol->ubi->ubi_num);
|
||||
if (!ubi)
|
||||
return -ENODEV;
|
||||
|
||||
spin_lock(&ubi->volumes_lock);
|
||||
if (!ubi->volumes[vol->vol_id]) {
|
||||
spin_unlock(&ubi->volumes_lock);
|
||||
ubi_put_device(ubi);
|
||||
return -ENODEV;
|
||||
}
|
||||
/* Take a reference to prevent volume removal */
|
||||
vol->ref_count += 1;
|
||||
spin_unlock(&ubi->volumes_lock);
|
||||
|
||||
if (attr == &attr_vol_reserved_ebs)
|
||||
ret = sprintf(buf, "%d\n", vol->reserved_pebs);
|
||||
else if (attr == &attr_vol_type) {
|
||||
const char *tp;
|
||||
|
||||
if (vol->vol_type == UBI_DYNAMIC_VOLUME)
|
||||
tp = "dynamic";
|
||||
else
|
||||
tp = "static";
|
||||
ret = sprintf(buf, "%s\n", tp);
|
||||
} else if (attr == &attr_vol_name)
|
||||
ret = sprintf(buf, "%s\n", vol->name);
|
||||
else if (attr == &attr_vol_corrupted)
|
||||
ret = sprintf(buf, "%d\n", vol->corrupted);
|
||||
else if (attr == &attr_vol_alignment)
|
||||
ret = sprintf(buf, "%d\n", vol->alignment);
|
||||
else if (attr == &attr_vol_usable_eb_size)
|
||||
ret = sprintf(buf, "%d\n", vol->usable_leb_size);
|
||||
else if (attr == &attr_vol_data_bytes)
|
||||
ret = sprintf(buf, "%lld\n", vol->used_bytes);
|
||||
else if (attr == &attr_vol_upd_marker)
|
||||
ret = sprintf(buf, "%d\n", vol->upd_marker);
|
||||
else
|
||||
/* This must be a bug */
|
||||
ret = -EINVAL;
|
||||
|
||||
/* We've done the operation, drop volume and UBI device references */
|
||||
spin_lock(&ubi->volumes_lock);
|
||||
vol->ref_count -= 1;
|
||||
ubi_assert(vol->ref_count >= 0);
|
||||
spin_unlock(&ubi->volumes_lock);
|
||||
ubi_put_device(ubi);
|
||||
return ret;
|
||||
}
|
||||
|
||||
static struct attribute *volume_dev_attrs[] = {
|
||||
&attr_vol_reserved_ebs.attr,
|
||||
&attr_vol_type.attr,
|
||||
&attr_vol_name.attr,
|
||||
&attr_vol_corrupted.attr,
|
||||
&attr_vol_alignment.attr,
|
||||
&attr_vol_usable_eb_size.attr,
|
||||
&attr_vol_data_bytes.attr,
|
||||
&attr_vol_upd_marker.attr,
|
||||
NULL
|
||||
};
|
||||
ATTRIBUTE_GROUPS(volume_dev);
|
||||
#endif
|
||||
|
||||
/* Release method for volume devices */
|
||||
static void vol_release(struct device *dev)
|
||||
{
|
||||
struct ubi_volume *vol = container_of(dev, struct ubi_volume, dev);
|
||||
|
||||
kfree(vol->eba_tbl);
|
||||
kfree(vol);
|
||||
}
|
||||
|
||||
/**
|
||||
* ubi_create_volume - create volume.
|
||||
* @ubi: UBI device description object
|
||||
* @req: volume creation request
|
||||
*
|
||||
* This function creates volume described by @req. If @req->vol_id id
|
||||
* %UBI_VOL_NUM_AUTO, this function automatically assign ID to the new volume
|
||||
* and saves it in @req->vol_id. Returns zero in case of success and a negative
|
||||
* error code in case of failure. Note, the caller has to have the
|
||||
* @ubi->device_mutex locked.
|
||||
*/
|
||||
int ubi_create_volume(struct ubi_device *ubi, struct ubi_mkvol_req *req)
|
||||
{
|
||||
int i, err, vol_id = req->vol_id, do_free = 1;
|
||||
struct ubi_volume *vol;
|
||||
struct ubi_vtbl_record vtbl_rec;
|
||||
dev_t dev;
|
||||
|
||||
if (ubi->ro_mode)
|
||||
return -EROFS;
|
||||
|
||||
vol = kzalloc(sizeof(struct ubi_volume), GFP_KERNEL);
|
||||
if (!vol)
|
||||
return -ENOMEM;
|
||||
|
||||
spin_lock(&ubi->volumes_lock);
|
||||
if (vol_id == UBI_VOL_NUM_AUTO) {
|
||||
/* Find unused volume ID */
|
||||
dbg_gen("search for vacant volume ID");
|
||||
for (i = 0; i < ubi->vtbl_slots; i++)
|
||||
if (!ubi->volumes[i]) {
|
||||
vol_id = i;
|
||||
break;
|
||||
}
|
||||
|
||||
if (vol_id == UBI_VOL_NUM_AUTO) {
|
||||
ubi_err(ubi, "out of volume IDs");
|
||||
err = -ENFILE;
|
||||
goto out_unlock;
|
||||
}
|
||||
req->vol_id = vol_id;
|
||||
}
|
||||
|
||||
dbg_gen("create device %d, volume %d, %llu bytes, type %d, name %s",
|
||||
ubi->ubi_num, vol_id, (unsigned long long)req->bytes,
|
||||
(int)req->vol_type, req->name);
|
||||
|
||||
/* Ensure that this volume does not exist */
|
||||
err = -EEXIST;
|
||||
if (ubi->volumes[vol_id]) {
|
||||
ubi_err(ubi, "volume %d already exists", vol_id);
|
||||
goto out_unlock;
|
||||
}
|
||||
|
||||
/* Ensure that the name is unique */
|
||||
for (i = 0; i < ubi->vtbl_slots; i++)
|
||||
if (ubi->volumes[i] &&
|
||||
ubi->volumes[i]->name_len == req->name_len &&
|
||||
!strcmp(ubi->volumes[i]->name, req->name)) {
|
||||
ubi_err(ubi, "volume \"%s\" exists (ID %d)",
|
||||
req->name, i);
|
||||
goto out_unlock;
|
||||
}
|
||||
|
||||
/* Calculate how many eraseblocks are requested */
|
||||
vol->usable_leb_size = ubi->leb_size - ubi->leb_size % req->alignment;
|
||||
vol->reserved_pebs = div_u64(req->bytes + vol->usable_leb_size - 1,
|
||||
vol->usable_leb_size);
|
||||
|
||||
/* Reserve physical eraseblocks */
|
||||
if (vol->reserved_pebs > ubi->avail_pebs) {
|
||||
ubi_err(ubi, "not enough PEBs, only %d available",
|
||||
ubi->avail_pebs);
|
||||
if (ubi->corr_peb_count)
|
||||
ubi_err(ubi, "%d PEBs are corrupted and not used",
|
||||
ubi->corr_peb_count);
|
||||
err = -ENOSPC;
|
||||
goto out_unlock;
|
||||
}
|
||||
ubi->avail_pebs -= vol->reserved_pebs;
|
||||
ubi->rsvd_pebs += vol->reserved_pebs;
|
||||
spin_unlock(&ubi->volumes_lock);
|
||||
|
||||
vol->vol_id = vol_id;
|
||||
vol->alignment = req->alignment;
|
||||
vol->data_pad = ubi->leb_size % vol->alignment;
|
||||
vol->vol_type = req->vol_type;
|
||||
vol->name_len = req->name_len;
|
||||
memcpy(vol->name, req->name, vol->name_len);
|
||||
vol->ubi = ubi;
|
||||
|
||||
/*
|
||||
* Finish all pending erases because there may be some LEBs belonging
|
||||
* to the same volume ID.
|
||||
*/
|
||||
err = ubi_wl_flush(ubi, vol_id, UBI_ALL);
|
||||
if (err)
|
||||
goto out_acc;
|
||||
|
||||
vol->eba_tbl = kmalloc(vol->reserved_pebs * sizeof(int), GFP_KERNEL);
|
||||
if (!vol->eba_tbl) {
|
||||
err = -ENOMEM;
|
||||
goto out_acc;
|
||||
}
|
||||
|
||||
for (i = 0; i < vol->reserved_pebs; i++)
|
||||
vol->eba_tbl[i] = UBI_LEB_UNMAPPED;
|
||||
|
||||
if (vol->vol_type == UBI_DYNAMIC_VOLUME) {
|
||||
vol->used_ebs = vol->reserved_pebs;
|
||||
vol->last_eb_bytes = vol->usable_leb_size;
|
||||
vol->used_bytes =
|
||||
(long long)vol->used_ebs * vol->usable_leb_size;
|
||||
} else {
|
||||
vol->used_ebs = div_u64_rem(vol->used_bytes,
|
||||
vol->usable_leb_size,
|
||||
&vol->last_eb_bytes);
|
||||
if (vol->last_eb_bytes != 0)
|
||||
vol->used_ebs += 1;
|
||||
else
|
||||
vol->last_eb_bytes = vol->usable_leb_size;
|
||||
}
|
||||
|
||||
/* Register character device for the volume */
|
||||
cdev_init(&vol->cdev, &ubi_vol_cdev_operations);
|
||||
vol->cdev.owner = THIS_MODULE;
|
||||
dev = MKDEV(MAJOR(ubi->cdev.dev), vol_id + 1);
|
||||
err = cdev_add(&vol->cdev, dev, 1);
|
||||
if (err) {
|
||||
ubi_err(ubi, "cannot add character device");
|
||||
goto out_mapping;
|
||||
}
|
||||
|
||||
vol->dev.release = vol_release;
|
||||
vol->dev.parent = &ubi->dev;
|
||||
vol->dev.devt = dev;
|
||||
#ifndef __UBOOT__
|
||||
vol->dev.class = &ubi_class;
|
||||
vol->dev.groups = volume_dev_groups;
|
||||
#endif
|
||||
|
||||
dev_set_name(&vol->dev, "%s_%d", ubi->ubi_name, vol->vol_id);
|
||||
err = device_register(&vol->dev);
|
||||
if (err) {
|
||||
ubi_err(ubi, "cannot register device");
|
||||
goto out_cdev;
|
||||
}
|
||||
|
||||
/* Fill volume table record */
|
||||
memset(&vtbl_rec, 0, sizeof(struct ubi_vtbl_record));
|
||||
vtbl_rec.reserved_pebs = cpu_to_be32(vol->reserved_pebs);
|
||||
vtbl_rec.alignment = cpu_to_be32(vol->alignment);
|
||||
vtbl_rec.data_pad = cpu_to_be32(vol->data_pad);
|
||||
vtbl_rec.name_len = cpu_to_be16(vol->name_len);
|
||||
if (vol->vol_type == UBI_DYNAMIC_VOLUME)
|
||||
vtbl_rec.vol_type = UBI_VID_DYNAMIC;
|
||||
else
|
||||
vtbl_rec.vol_type = UBI_VID_STATIC;
|
||||
memcpy(vtbl_rec.name, vol->name, vol->name_len);
|
||||
|
||||
err = ubi_change_vtbl_record(ubi, vol_id, &vtbl_rec);
|
||||
if (err)
|
||||
goto out_sysfs;
|
||||
|
||||
spin_lock(&ubi->volumes_lock);
|
||||
ubi->volumes[vol_id] = vol;
|
||||
ubi->vol_count += 1;
|
||||
spin_unlock(&ubi->volumes_lock);
|
||||
|
||||
ubi_volume_notify(ubi, vol, UBI_VOLUME_ADDED);
|
||||
self_check_volumes(ubi);
|
||||
return err;
|
||||
|
||||
out_sysfs:
|
||||
/*
|
||||
* We have registered our device, we should not free the volume
|
||||
* description object in this function in case of an error - it is
|
||||
* freed by the release function.
|
||||
*
|
||||
* Get device reference to prevent the release function from being
|
||||
* called just after sysfs has been closed.
|
||||
*/
|
||||
do_free = 0;
|
||||
get_device(&vol->dev);
|
||||
device_unregister(&vol->dev);
|
||||
out_cdev:
|
||||
cdev_del(&vol->cdev);
|
||||
out_mapping:
|
||||
if (do_free)
|
||||
kfree(vol->eba_tbl);
|
||||
out_acc:
|
||||
spin_lock(&ubi->volumes_lock);
|
||||
ubi->rsvd_pebs -= vol->reserved_pebs;
|
||||
ubi->avail_pebs += vol->reserved_pebs;
|
||||
out_unlock:
|
||||
spin_unlock(&ubi->volumes_lock);
|
||||
if (do_free)
|
||||
kfree(vol);
|
||||
else
|
||||
put_device(&vol->dev);
|
||||
ubi_err(ubi, "cannot create volume %d, error %d", vol_id, err);
|
||||
return err;
|
||||
}
|
||||
|
||||
/**
|
||||
* ubi_remove_volume - remove volume.
|
||||
* @desc: volume descriptor
|
||||
* @no_vtbl: do not change volume table if not zero
|
||||
*
|
||||
* This function removes volume described by @desc. The volume has to be opened
|
||||
* in "exclusive" mode. Returns zero in case of success and a negative error
|
||||
* code in case of failure. The caller has to have the @ubi->device_mutex
|
||||
* locked.
|
||||
*/
|
||||
int ubi_remove_volume(struct ubi_volume_desc *desc, int no_vtbl)
|
||||
{
|
||||
struct ubi_volume *vol = desc->vol;
|
||||
struct ubi_device *ubi = vol->ubi;
|
||||
int i, err, vol_id = vol->vol_id, reserved_pebs = vol->reserved_pebs;
|
||||
|
||||
dbg_gen("remove device %d, volume %d", ubi->ubi_num, vol_id);
|
||||
ubi_assert(desc->mode == UBI_EXCLUSIVE);
|
||||
ubi_assert(vol == ubi->volumes[vol_id]);
|
||||
|
||||
if (ubi->ro_mode)
|
||||
return -EROFS;
|
||||
|
||||
spin_lock(&ubi->volumes_lock);
|
||||
if (vol->ref_count > 1) {
|
||||
/*
|
||||
* The volume is busy, probably someone is reading one of its
|
||||
* sysfs files.
|
||||
*/
|
||||
err = -EBUSY;
|
||||
goto out_unlock;
|
||||
}
|
||||
ubi->volumes[vol_id] = NULL;
|
||||
spin_unlock(&ubi->volumes_lock);
|
||||
|
||||
if (!no_vtbl) {
|
||||
err = ubi_change_vtbl_record(ubi, vol_id, NULL);
|
||||
if (err)
|
||||
goto out_err;
|
||||
}
|
||||
|
||||
for (i = 0; i < vol->reserved_pebs; i++) {
|
||||
err = ubi_eba_unmap_leb(ubi, vol, i);
|
||||
if (err)
|
||||
goto out_err;
|
||||
}
|
||||
|
||||
cdev_del(&vol->cdev);
|
||||
device_unregister(&vol->dev);
|
||||
|
||||
spin_lock(&ubi->volumes_lock);
|
||||
ubi->rsvd_pebs -= reserved_pebs;
|
||||
ubi->avail_pebs += reserved_pebs;
|
||||
ubi_update_reserved(ubi);
|
||||
ubi->vol_count -= 1;
|
||||
spin_unlock(&ubi->volumes_lock);
|
||||
|
||||
ubi_volume_notify(ubi, vol, UBI_VOLUME_REMOVED);
|
||||
if (!no_vtbl)
|
||||
self_check_volumes(ubi);
|
||||
|
||||
return err;
|
||||
|
||||
out_err:
|
||||
ubi_err(ubi, "cannot remove volume %d, error %d", vol_id, err);
|
||||
spin_lock(&ubi->volumes_lock);
|
||||
ubi->volumes[vol_id] = vol;
|
||||
out_unlock:
|
||||
spin_unlock(&ubi->volumes_lock);
|
||||
return err;
|
||||
}
|
||||
|
||||
/**
|
||||
* ubi_resize_volume - re-size volume.
|
||||
* @desc: volume descriptor
|
||||
* @reserved_pebs: new size in physical eraseblocks
|
||||
*
|
||||
* This function re-sizes the volume and returns zero in case of success, and a
|
||||
* negative error code in case of failure. The caller has to have the
|
||||
* @ubi->device_mutex locked.
|
||||
*/
|
||||
int ubi_resize_volume(struct ubi_volume_desc *desc, int reserved_pebs)
|
||||
{
|
||||
int i, err, pebs, *new_mapping;
|
||||
struct ubi_volume *vol = desc->vol;
|
||||
struct ubi_device *ubi = vol->ubi;
|
||||
struct ubi_vtbl_record vtbl_rec;
|
||||
int vol_id = vol->vol_id;
|
||||
|
||||
if (ubi->ro_mode)
|
||||
return -EROFS;
|
||||
|
||||
dbg_gen("re-size device %d, volume %d to from %d to %d PEBs",
|
||||
ubi->ubi_num, vol_id, vol->reserved_pebs, reserved_pebs);
|
||||
|
||||
if (vol->vol_type == UBI_STATIC_VOLUME &&
|
||||
reserved_pebs < vol->used_ebs) {
|
||||
ubi_err(ubi, "too small size %d, %d LEBs contain data",
|
||||
reserved_pebs, vol->used_ebs);
|
||||
return -EINVAL;
|
||||
}
|
||||
|
||||
/* If the size is the same, we have nothing to do */
|
||||
if (reserved_pebs == vol->reserved_pebs)
|
||||
return 0;
|
||||
|
||||
new_mapping = kmalloc(reserved_pebs * sizeof(int), GFP_KERNEL);
|
||||
if (!new_mapping)
|
||||
return -ENOMEM;
|
||||
|
||||
for (i = 0; i < reserved_pebs; i++)
|
||||
new_mapping[i] = UBI_LEB_UNMAPPED;
|
||||
|
||||
spin_lock(&ubi->volumes_lock);
|
||||
if (vol->ref_count > 1) {
|
||||
spin_unlock(&ubi->volumes_lock);
|
||||
err = -EBUSY;
|
||||
goto out_free;
|
||||
}
|
||||
spin_unlock(&ubi->volumes_lock);
|
||||
|
||||
/* Reserve physical eraseblocks */
|
||||
pebs = reserved_pebs - vol->reserved_pebs;
|
||||
if (pebs > 0) {
|
||||
spin_lock(&ubi->volumes_lock);
|
||||
if (pebs > ubi->avail_pebs) {
|
||||
ubi_err(ubi, "not enough PEBs: requested %d, available %d",
|
||||
pebs, ubi->avail_pebs);
|
||||
if (ubi->corr_peb_count)
|
||||
ubi_err(ubi, "%d PEBs are corrupted and not used",
|
||||
ubi->corr_peb_count);
|
||||
spin_unlock(&ubi->volumes_lock);
|
||||
err = -ENOSPC;
|
||||
goto out_free;
|
||||
}
|
||||
ubi->avail_pebs -= pebs;
|
||||
ubi->rsvd_pebs += pebs;
|
||||
for (i = 0; i < vol->reserved_pebs; i++)
|
||||
new_mapping[i] = vol->eba_tbl[i];
|
||||
kfree(vol->eba_tbl);
|
||||
vol->eba_tbl = new_mapping;
|
||||
spin_unlock(&ubi->volumes_lock);
|
||||
}
|
||||
|
||||
/* Change volume table record */
|
||||
vtbl_rec = ubi->vtbl[vol_id];
|
||||
vtbl_rec.reserved_pebs = cpu_to_be32(reserved_pebs);
|
||||
err = ubi_change_vtbl_record(ubi, vol_id, &vtbl_rec);
|
||||
if (err)
|
||||
goto out_acc;
|
||||
|
||||
if (pebs < 0) {
|
||||
for (i = 0; i < -pebs; i++) {
|
||||
err = ubi_eba_unmap_leb(ubi, vol, reserved_pebs + i);
|
||||
if (err)
|
||||
goto out_acc;
|
||||
}
|
||||
spin_lock(&ubi->volumes_lock);
|
||||
ubi->rsvd_pebs += pebs;
|
||||
ubi->avail_pebs -= pebs;
|
||||
ubi_update_reserved(ubi);
|
||||
for (i = 0; i < reserved_pebs; i++)
|
||||
new_mapping[i] = vol->eba_tbl[i];
|
||||
kfree(vol->eba_tbl);
|
||||
vol->eba_tbl = new_mapping;
|
||||
spin_unlock(&ubi->volumes_lock);
|
||||
}
|
||||
|
||||
vol->reserved_pebs = reserved_pebs;
|
||||
if (vol->vol_type == UBI_DYNAMIC_VOLUME) {
|
||||
vol->used_ebs = reserved_pebs;
|
||||
vol->last_eb_bytes = vol->usable_leb_size;
|
||||
vol->used_bytes =
|
||||
(long long)vol->used_ebs * vol->usable_leb_size;
|
||||
}
|
||||
|
||||
ubi_volume_notify(ubi, vol, UBI_VOLUME_RESIZED);
|
||||
self_check_volumes(ubi);
|
||||
return err;
|
||||
|
||||
out_acc:
|
||||
if (pebs > 0) {
|
||||
spin_lock(&ubi->volumes_lock);
|
||||
ubi->rsvd_pebs -= pebs;
|
||||
ubi->avail_pebs += pebs;
|
||||
spin_unlock(&ubi->volumes_lock);
|
||||
}
|
||||
out_free:
|
||||
kfree(new_mapping);
|
||||
return err;
|
||||
}
|
||||
|
||||
/**
|
||||
* ubi_rename_volumes - re-name UBI volumes.
|
||||
* @ubi: UBI device description object
|
||||
* @rename_list: list of &struct ubi_rename_entry objects
|
||||
*
|
||||
* This function re-names or removes volumes specified in the re-name list.
|
||||
* Returns zero in case of success and a negative error code in case of
|
||||
* failure.
|
||||
*/
|
||||
int ubi_rename_volumes(struct ubi_device *ubi, struct list_head *rename_list)
|
||||
{
|
||||
int err;
|
||||
struct ubi_rename_entry *re;
|
||||
|
||||
err = ubi_vtbl_rename_volumes(ubi, rename_list);
|
||||
if (err)
|
||||
return err;
|
||||
|
||||
list_for_each_entry(re, rename_list, list) {
|
||||
if (re->remove) {
|
||||
err = ubi_remove_volume(re->desc, 1);
|
||||
if (err)
|
||||
break;
|
||||
} else {
|
||||
struct ubi_volume *vol = re->desc->vol;
|
||||
|
||||
spin_lock(&ubi->volumes_lock);
|
||||
vol->name_len = re->new_name_len;
|
||||
memcpy(vol->name, re->new_name, re->new_name_len + 1);
|
||||
spin_unlock(&ubi->volumes_lock);
|
||||
ubi_volume_notify(ubi, vol, UBI_VOLUME_RENAMED);
|
||||
}
|
||||
}
|
||||
|
||||
if (!err)
|
||||
self_check_volumes(ubi);
|
||||
return err;
|
||||
}
|
||||
|
||||
/**
|
||||
* ubi_add_volume - add volume.
|
||||
* @ubi: UBI device description object
|
||||
* @vol: volume description object
|
||||
*
|
||||
* This function adds an existing volume and initializes all its data
|
||||
* structures. Returns zero in case of success and a negative error code in
|
||||
* case of failure.
|
||||
*/
|
||||
int ubi_add_volume(struct ubi_device *ubi, struct ubi_volume *vol)
|
||||
{
|
||||
int err, vol_id = vol->vol_id;
|
||||
dev_t dev;
|
||||
|
||||
dbg_gen("add volume %d", vol_id);
|
||||
|
||||
/* Register character device for the volume */
|
||||
cdev_init(&vol->cdev, &ubi_vol_cdev_operations);
|
||||
vol->cdev.owner = THIS_MODULE;
|
||||
dev = MKDEV(MAJOR(ubi->cdev.dev), vol->vol_id + 1);
|
||||
err = cdev_add(&vol->cdev, dev, 1);
|
||||
if (err) {
|
||||
ubi_err(ubi, "cannot add character device for volume %d, error %d",
|
||||
vol_id, err);
|
||||
return err;
|
||||
}
|
||||
|
||||
vol->dev.release = vol_release;
|
||||
vol->dev.parent = &ubi->dev;
|
||||
vol->dev.devt = dev;
|
||||
#ifndef __UBOOT__
|
||||
vol->dev.class = &ubi_class;
|
||||
vol->dev.groups = volume_dev_groups;
|
||||
#endif
|
||||
dev_set_name(&vol->dev, "%s_%d", ubi->ubi_name, vol->vol_id);
|
||||
err = device_register(&vol->dev);
|
||||
if (err)
|
||||
goto out_cdev;
|
||||
|
||||
self_check_volumes(ubi);
|
||||
return err;
|
||||
|
||||
out_cdev:
|
||||
cdev_del(&vol->cdev);
|
||||
return err;
|
||||
}
|
||||
|
||||
/**
|
||||
* ubi_free_volume - free volume.
|
||||
* @ubi: UBI device description object
|
||||
* @vol: volume description object
|
||||
*
|
||||
* This function frees all resources for volume @vol but does not remove it.
|
||||
* Used only when the UBI device is detached.
|
||||
*/
|
||||
void ubi_free_volume(struct ubi_device *ubi, struct ubi_volume *vol)
|
||||
{
|
||||
dbg_gen("free volume %d", vol->vol_id);
|
||||
|
||||
ubi->volumes[vol->vol_id] = NULL;
|
||||
cdev_del(&vol->cdev);
|
||||
device_unregister(&vol->dev);
|
||||
}
|
||||
|
||||
/**
|
||||
* self_check_volume - check volume information.
|
||||
* @ubi: UBI device description object
|
||||
* @vol_id: volume ID
|
||||
*
|
||||
* Returns zero if volume is all right and a a negative error code if not.
|
||||
*/
|
||||
static int self_check_volume(struct ubi_device *ubi, int vol_id)
|
||||
{
|
||||
int idx = vol_id2idx(ubi, vol_id);
|
||||
int reserved_pebs, alignment, data_pad, vol_type, name_len, upd_marker;
|
||||
const struct ubi_volume *vol;
|
||||
long long n;
|
||||
const char *name;
|
||||
|
||||
spin_lock(&ubi->volumes_lock);
|
||||
reserved_pebs = be32_to_cpu(ubi->vtbl[vol_id].reserved_pebs);
|
||||
vol = ubi->volumes[idx];
|
||||
|
||||
if (!vol) {
|
||||
if (reserved_pebs) {
|
||||
ubi_err(ubi, "no volume info, but volume exists");
|
||||
goto fail;
|
||||
}
|
||||
spin_unlock(&ubi->volumes_lock);
|
||||
return 0;
|
||||
}
|
||||
|
||||
if (vol->reserved_pebs < 0 || vol->alignment < 0 || vol->data_pad < 0 ||
|
||||
vol->name_len < 0) {
|
||||
ubi_err(ubi, "negative values");
|
||||
goto fail;
|
||||
}
|
||||
if (vol->alignment > ubi->leb_size || vol->alignment == 0) {
|
||||
ubi_err(ubi, "bad alignment");
|
||||
goto fail;
|
||||
}
|
||||
|
||||
n = vol->alignment & (ubi->min_io_size - 1);
|
||||
if (vol->alignment != 1 && n) {
|
||||
ubi_err(ubi, "alignment is not multiple of min I/O unit");
|
||||
goto fail;
|
||||
}
|
||||
|
||||
n = ubi->leb_size % vol->alignment;
|
||||
if (vol->data_pad != n) {
|
||||
ubi_err(ubi, "bad data_pad, has to be %lld", n);
|
||||
goto fail;
|
||||
}
|
||||
|
||||
if (vol->vol_type != UBI_DYNAMIC_VOLUME &&
|
||||
vol->vol_type != UBI_STATIC_VOLUME) {
|
||||
ubi_err(ubi, "bad vol_type");
|
||||
goto fail;
|
||||
}
|
||||
|
||||
if (vol->upd_marker && vol->corrupted) {
|
||||
ubi_err(ubi, "update marker and corrupted simultaneously");
|
||||
goto fail;
|
||||
}
|
||||
|
||||
if (vol->reserved_pebs > ubi->good_peb_count) {
|
||||
ubi_err(ubi, "too large reserved_pebs");
|
||||
goto fail;
|
||||
}
|
||||
|
||||
n = ubi->leb_size - vol->data_pad;
|
||||
if (vol->usable_leb_size != ubi->leb_size - vol->data_pad) {
|
||||
ubi_err(ubi, "bad usable_leb_size, has to be %lld", n);
|
||||
goto fail;
|
||||
}
|
||||
|
||||
if (vol->name_len > UBI_VOL_NAME_MAX) {
|
||||
ubi_err(ubi, "too long volume name, max is %d",
|
||||
UBI_VOL_NAME_MAX);
|
||||
goto fail;
|
||||
}
|
||||
|
||||
n = strnlen(vol->name, vol->name_len + 1);
|
||||
if (n != vol->name_len) {
|
||||
ubi_err(ubi, "bad name_len %lld", n);
|
||||
goto fail;
|
||||
}
|
||||
|
||||
n = (long long)vol->used_ebs * vol->usable_leb_size;
|
||||
if (vol->vol_type == UBI_DYNAMIC_VOLUME) {
|
||||
if (vol->corrupted) {
|
||||
ubi_err(ubi, "corrupted dynamic volume");
|
||||
goto fail;
|
||||
}
|
||||
if (vol->used_ebs != vol->reserved_pebs) {
|
||||
ubi_err(ubi, "bad used_ebs");
|
||||
goto fail;
|
||||
}
|
||||
if (vol->last_eb_bytes != vol->usable_leb_size) {
|
||||
ubi_err(ubi, "bad last_eb_bytes");
|
||||
goto fail;
|
||||
}
|
||||
if (vol->used_bytes != n) {
|
||||
ubi_err(ubi, "bad used_bytes");
|
||||
goto fail;
|
||||
}
|
||||
} else {
|
||||
if (vol->used_ebs < 0 || vol->used_ebs > vol->reserved_pebs) {
|
||||
ubi_err(ubi, "bad used_ebs");
|
||||
goto fail;
|
||||
}
|
||||
if (vol->last_eb_bytes < 0 ||
|
||||
vol->last_eb_bytes > vol->usable_leb_size) {
|
||||
ubi_err(ubi, "bad last_eb_bytes");
|
||||
goto fail;
|
||||
}
|
||||
if (vol->used_bytes < 0 || vol->used_bytes > n ||
|
||||
vol->used_bytes < n - vol->usable_leb_size) {
|
||||
ubi_err(ubi, "bad used_bytes");
|
||||
goto fail;
|
||||
}
|
||||
}
|
||||
|
||||
alignment = be32_to_cpu(ubi->vtbl[vol_id].alignment);
|
||||
data_pad = be32_to_cpu(ubi->vtbl[vol_id].data_pad);
|
||||
name_len = be16_to_cpu(ubi->vtbl[vol_id].name_len);
|
||||
upd_marker = ubi->vtbl[vol_id].upd_marker;
|
||||
name = &ubi->vtbl[vol_id].name[0];
|
||||
if (ubi->vtbl[vol_id].vol_type == UBI_VID_DYNAMIC)
|
||||
vol_type = UBI_DYNAMIC_VOLUME;
|
||||
else
|
||||
vol_type = UBI_STATIC_VOLUME;
|
||||
|
||||
if (alignment != vol->alignment || data_pad != vol->data_pad ||
|
||||
upd_marker != vol->upd_marker || vol_type != vol->vol_type ||
|
||||
name_len != vol->name_len || strncmp(name, vol->name, name_len)) {
|
||||
ubi_err(ubi, "volume info is different");
|
||||
goto fail;
|
||||
}
|
||||
|
||||
spin_unlock(&ubi->volumes_lock);
|
||||
return 0;
|
||||
|
||||
fail:
|
||||
ubi_err(ubi, "self-check failed for volume %d", vol_id);
|
||||
if (vol)
|
||||
ubi_dump_vol_info(vol);
|
||||
ubi_dump_vtbl_record(&ubi->vtbl[vol_id], vol_id);
|
||||
dump_stack();
|
||||
spin_unlock(&ubi->volumes_lock);
|
||||
return -EINVAL;
|
||||
}
|
||||
|
||||
/**
|
||||
* self_check_volumes - check information about all volumes.
|
||||
* @ubi: UBI device description object
|
||||
*
|
||||
* Returns zero if volumes are all right and a a negative error code if not.
|
||||
*/
|
||||
static int self_check_volumes(struct ubi_device *ubi)
|
||||
{
|
||||
int i, err = 0;
|
||||
|
||||
if (!ubi_dbg_chk_gen(ubi))
|
||||
return 0;
|
||||
|
||||
for (i = 0; i < ubi->vtbl_slots; i++) {
|
||||
err = self_check_volume(ubi, i);
|
||||
if (err)
|
||||
break;
|
||||
}
|
||||
|
||||
return err;
|
||||
}
|
||||
865
u-boot/drivers/mtd/ubi/vtbl.c
Normal file
865
u-boot/drivers/mtd/ubi/vtbl.c
Normal file
@@ -0,0 +1,865 @@
|
||||
/*
|
||||
* Copyright (c) International Business Machines Corp., 2006
|
||||
* Copyright (c) Nokia Corporation, 2006, 2007
|
||||
*
|
||||
* SPDX-License-Identifier: GPL-2.0+
|
||||
*
|
||||
* Author: Artem Bityutskiy (Битюцкий Артём)
|
||||
*/
|
||||
|
||||
/*
|
||||
* This file includes volume table manipulation code. The volume table is an
|
||||
* on-flash table containing volume meta-data like name, number of reserved
|
||||
* physical eraseblocks, type, etc. The volume table is stored in the so-called
|
||||
* "layout volume".
|
||||
*
|
||||
* The layout volume is an internal volume which is organized as follows. It
|
||||
* consists of two logical eraseblocks - LEB 0 and LEB 1. Each logical
|
||||
* eraseblock stores one volume table copy, i.e. LEB 0 and LEB 1 duplicate each
|
||||
* other. This redundancy guarantees robustness to unclean reboots. The volume
|
||||
* table is basically an array of volume table records. Each record contains
|
||||
* full information about the volume and protected by a CRC checksum. Note,
|
||||
* nowadays we use the atomic LEB change operation when updating the volume
|
||||
* table, so we do not really need 2 LEBs anymore, but we preserve the older
|
||||
* design for the backward compatibility reasons.
|
||||
*
|
||||
* When the volume table is changed, it is first changed in RAM. Then LEB 0 is
|
||||
* erased, and the updated volume table is written back to LEB 0. Then same for
|
||||
* LEB 1. This scheme guarantees recoverability from unclean reboots.
|
||||
*
|
||||
* In this UBI implementation the on-flash volume table does not contain any
|
||||
* information about how much data static volumes contain.
|
||||
*
|
||||
* But it would still be beneficial to store this information in the volume
|
||||
* table. For example, suppose we have a static volume X, and all its physical
|
||||
* eraseblocks became bad for some reasons. Suppose we are attaching the
|
||||
* corresponding MTD device, for some reason we find no logical eraseblocks
|
||||
* corresponding to the volume X. According to the volume table volume X does
|
||||
* exist. So we don't know whether it is just empty or all its physical
|
||||
* eraseblocks went bad. So we cannot alarm the user properly.
|
||||
*
|
||||
* The volume table also stores so-called "update marker", which is used for
|
||||
* volume updates. Before updating the volume, the update marker is set, and
|
||||
* after the update operation is finished, the update marker is cleared. So if
|
||||
* the update operation was interrupted (e.g. by an unclean reboot) - the
|
||||
* update marker is still there and we know that the volume's contents is
|
||||
* damaged.
|
||||
*/
|
||||
|
||||
#ifndef __UBOOT__
|
||||
#include <linux/crc32.h>
|
||||
#include <linux/err.h>
|
||||
#include <linux/slab.h>
|
||||
#include <asm/div64.h>
|
||||
#else
|
||||
#include <ubi_uboot.h>
|
||||
#endif
|
||||
|
||||
#include <linux/err.h>
|
||||
#include "ubi.h"
|
||||
|
||||
static void self_vtbl_check(const struct ubi_device *ubi);
|
||||
|
||||
/* Empty volume table record */
|
||||
static struct ubi_vtbl_record empty_vtbl_record;
|
||||
|
||||
/**
|
||||
* ubi_update_layout_vol - helper for updatting layout volumes on flash
|
||||
* @ubi: UBI device description object
|
||||
*/
|
||||
static int ubi_update_layout_vol(struct ubi_device *ubi)
|
||||
{
|
||||
struct ubi_volume *layout_vol;
|
||||
int i, err;
|
||||
|
||||
layout_vol = ubi->volumes[vol_id2idx(ubi, UBI_LAYOUT_VOLUME_ID)];
|
||||
for (i = 0; i < UBI_LAYOUT_VOLUME_EBS; i++) {
|
||||
err = ubi_eba_atomic_leb_change(ubi, layout_vol, i, ubi->vtbl,
|
||||
ubi->vtbl_size);
|
||||
if (err)
|
||||
return err;
|
||||
}
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
||||
/**
|
||||
* ubi_change_vtbl_record - change volume table record.
|
||||
* @ubi: UBI device description object
|
||||
* @idx: table index to change
|
||||
* @vtbl_rec: new volume table record
|
||||
*
|
||||
* This function changes volume table record @idx. If @vtbl_rec is %NULL, empty
|
||||
* volume table record is written. The caller does not have to calculate CRC of
|
||||
* the record as it is done by this function. Returns zero in case of success
|
||||
* and a negative error code in case of failure.
|
||||
*/
|
||||
int ubi_change_vtbl_record(struct ubi_device *ubi, int idx,
|
||||
struct ubi_vtbl_record *vtbl_rec)
|
||||
{
|
||||
int err;
|
||||
uint32_t crc;
|
||||
|
||||
ubi_assert(idx >= 0 && idx < ubi->vtbl_slots);
|
||||
|
||||
if (!vtbl_rec)
|
||||
vtbl_rec = &empty_vtbl_record;
|
||||
else {
|
||||
crc = crc32(UBI_CRC32_INIT, vtbl_rec, UBI_VTBL_RECORD_SIZE_CRC);
|
||||
vtbl_rec->crc = cpu_to_be32(crc);
|
||||
}
|
||||
|
||||
memcpy(&ubi->vtbl[idx], vtbl_rec, sizeof(struct ubi_vtbl_record));
|
||||
err = ubi_update_layout_vol(ubi);
|
||||
|
||||
self_vtbl_check(ubi);
|
||||
return err ? err : 0;
|
||||
}
|
||||
|
||||
/**
|
||||
* ubi_vtbl_rename_volumes - rename UBI volumes in the volume table.
|
||||
* @ubi: UBI device description object
|
||||
* @rename_list: list of &struct ubi_rename_entry objects
|
||||
*
|
||||
* This function re-names multiple volumes specified in @req in the volume
|
||||
* table. Returns zero in case of success and a negative error code in case of
|
||||
* failure.
|
||||
*/
|
||||
int ubi_vtbl_rename_volumes(struct ubi_device *ubi,
|
||||
struct list_head *rename_list)
|
||||
{
|
||||
struct ubi_rename_entry *re;
|
||||
|
||||
list_for_each_entry(re, rename_list, list) {
|
||||
uint32_t crc;
|
||||
struct ubi_volume *vol = re->desc->vol;
|
||||
struct ubi_vtbl_record *vtbl_rec = &ubi->vtbl[vol->vol_id];
|
||||
|
||||
if (re->remove) {
|
||||
memcpy(vtbl_rec, &empty_vtbl_record,
|
||||
sizeof(struct ubi_vtbl_record));
|
||||
continue;
|
||||
}
|
||||
|
||||
vtbl_rec->name_len = cpu_to_be16(re->new_name_len);
|
||||
memcpy(vtbl_rec->name, re->new_name, re->new_name_len);
|
||||
memset(vtbl_rec->name + re->new_name_len, 0,
|
||||
UBI_VOL_NAME_MAX + 1 - re->new_name_len);
|
||||
crc = crc32(UBI_CRC32_INIT, vtbl_rec,
|
||||
UBI_VTBL_RECORD_SIZE_CRC);
|
||||
vtbl_rec->crc = cpu_to_be32(crc);
|
||||
}
|
||||
|
||||
return ubi_update_layout_vol(ubi);
|
||||
}
|
||||
|
||||
/**
|
||||
* vtbl_check - check if volume table is not corrupted and sensible.
|
||||
* @ubi: UBI device description object
|
||||
* @vtbl: volume table
|
||||
*
|
||||
* This function returns zero if @vtbl is all right, %1 if CRC is incorrect,
|
||||
* and %-EINVAL if it contains inconsistent data.
|
||||
*/
|
||||
static int vtbl_check(const struct ubi_device *ubi,
|
||||
const struct ubi_vtbl_record *vtbl)
|
||||
{
|
||||
int i, n, reserved_pebs, alignment, data_pad, vol_type, name_len;
|
||||
int upd_marker, err;
|
||||
uint32_t crc;
|
||||
const char *name;
|
||||
|
||||
for (i = 0; i < ubi->vtbl_slots; i++) {
|
||||
cond_resched();
|
||||
|
||||
reserved_pebs = be32_to_cpu(vtbl[i].reserved_pebs);
|
||||
alignment = be32_to_cpu(vtbl[i].alignment);
|
||||
data_pad = be32_to_cpu(vtbl[i].data_pad);
|
||||
upd_marker = vtbl[i].upd_marker;
|
||||
vol_type = vtbl[i].vol_type;
|
||||
name_len = be16_to_cpu(vtbl[i].name_len);
|
||||
name = &vtbl[i].name[0];
|
||||
|
||||
crc = crc32(UBI_CRC32_INIT, &vtbl[i], UBI_VTBL_RECORD_SIZE_CRC);
|
||||
if (be32_to_cpu(vtbl[i].crc) != crc) {
|
||||
ubi_err(ubi, "bad CRC at record %u: %#08x, not %#08x",
|
||||
i, crc, be32_to_cpu(vtbl[i].crc));
|
||||
ubi_dump_vtbl_record(&vtbl[i], i);
|
||||
return 1;
|
||||
}
|
||||
|
||||
if (reserved_pebs == 0) {
|
||||
if (memcmp(&vtbl[i], &empty_vtbl_record,
|
||||
UBI_VTBL_RECORD_SIZE)) {
|
||||
err = 2;
|
||||
goto bad;
|
||||
}
|
||||
continue;
|
||||
}
|
||||
|
||||
if (reserved_pebs < 0 || alignment < 0 || data_pad < 0 ||
|
||||
name_len < 0) {
|
||||
err = 3;
|
||||
goto bad;
|
||||
}
|
||||
|
||||
if (alignment > ubi->leb_size || alignment == 0) {
|
||||
err = 4;
|
||||
goto bad;
|
||||
}
|
||||
|
||||
n = alignment & (ubi->min_io_size - 1);
|
||||
if (alignment != 1 && n) {
|
||||
err = 5;
|
||||
goto bad;
|
||||
}
|
||||
|
||||
n = ubi->leb_size % alignment;
|
||||
if (data_pad != n) {
|
||||
ubi_err(ubi, "bad data_pad, has to be %d", n);
|
||||
err = 6;
|
||||
goto bad;
|
||||
}
|
||||
|
||||
if (vol_type != UBI_VID_DYNAMIC && vol_type != UBI_VID_STATIC) {
|
||||
err = 7;
|
||||
goto bad;
|
||||
}
|
||||
|
||||
if (upd_marker != 0 && upd_marker != 1) {
|
||||
err = 8;
|
||||
goto bad;
|
||||
}
|
||||
|
||||
if (reserved_pebs > ubi->good_peb_count) {
|
||||
ubi_err(ubi, "too large reserved_pebs %d, good PEBs %d",
|
||||
reserved_pebs, ubi->good_peb_count);
|
||||
err = 9;
|
||||
goto bad;
|
||||
}
|
||||
|
||||
if (name_len > UBI_VOL_NAME_MAX) {
|
||||
err = 10;
|
||||
goto bad;
|
||||
}
|
||||
|
||||
if (name[0] == '\0') {
|
||||
err = 11;
|
||||
goto bad;
|
||||
}
|
||||
|
||||
if (name_len != strnlen(name, name_len + 1)) {
|
||||
err = 12;
|
||||
goto bad;
|
||||
}
|
||||
}
|
||||
|
||||
/* Checks that all names are unique */
|
||||
for (i = 0; i < ubi->vtbl_slots - 1; i++) {
|
||||
for (n = i + 1; n < ubi->vtbl_slots; n++) {
|
||||
int len1 = be16_to_cpu(vtbl[i].name_len);
|
||||
int len2 = be16_to_cpu(vtbl[n].name_len);
|
||||
|
||||
if (len1 > 0 && len1 == len2 &&
|
||||
#ifndef __UBOOT__
|
||||
!strncmp(vtbl[i].name, vtbl[n].name, len1)) {
|
||||
#else
|
||||
!strncmp((char *)vtbl[i].name, vtbl[n].name, len1)) {
|
||||
#endif
|
||||
ubi_err(ubi, "volumes %d and %d have the same name \"%s\"",
|
||||
i, n, vtbl[i].name);
|
||||
ubi_dump_vtbl_record(&vtbl[i], i);
|
||||
ubi_dump_vtbl_record(&vtbl[n], n);
|
||||
return -EINVAL;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
return 0;
|
||||
|
||||
bad:
|
||||
ubi_err(ubi, "volume table check failed: record %d, error %d", i, err);
|
||||
ubi_dump_vtbl_record(&vtbl[i], i);
|
||||
return -EINVAL;
|
||||
}
|
||||
|
||||
/**
|
||||
* create_vtbl - create a copy of volume table.
|
||||
* @ubi: UBI device description object
|
||||
* @ai: attaching information
|
||||
* @copy: number of the volume table copy
|
||||
* @vtbl: contents of the volume table
|
||||
*
|
||||
* This function returns zero in case of success and a negative error code in
|
||||
* case of failure.
|
||||
*/
|
||||
static int create_vtbl(struct ubi_device *ubi, struct ubi_attach_info *ai,
|
||||
int copy, void *vtbl)
|
||||
{
|
||||
int err, tries = 0;
|
||||
struct ubi_vid_hdr *vid_hdr;
|
||||
struct ubi_ainf_peb *new_aeb;
|
||||
|
||||
dbg_gen("create volume table (copy #%d)", copy + 1);
|
||||
|
||||
vid_hdr = ubi_zalloc_vid_hdr(ubi, GFP_KERNEL);
|
||||
if (!vid_hdr)
|
||||
return -ENOMEM;
|
||||
|
||||
retry:
|
||||
new_aeb = ubi_early_get_peb(ubi, ai);
|
||||
if (IS_ERR(new_aeb)) {
|
||||
err = PTR_ERR(new_aeb);
|
||||
goto out_free;
|
||||
}
|
||||
|
||||
vid_hdr->vol_type = UBI_LAYOUT_VOLUME_TYPE;
|
||||
vid_hdr->vol_id = cpu_to_be32(UBI_LAYOUT_VOLUME_ID);
|
||||
vid_hdr->compat = UBI_LAYOUT_VOLUME_COMPAT;
|
||||
vid_hdr->data_size = vid_hdr->used_ebs =
|
||||
vid_hdr->data_pad = cpu_to_be32(0);
|
||||
vid_hdr->lnum = cpu_to_be32(copy);
|
||||
vid_hdr->sqnum = cpu_to_be64(++ai->max_sqnum);
|
||||
|
||||
/* The EC header is already there, write the VID header */
|
||||
err = ubi_io_write_vid_hdr(ubi, new_aeb->pnum, vid_hdr);
|
||||
if (err)
|
||||
goto write_error;
|
||||
|
||||
/* Write the layout volume contents */
|
||||
err = ubi_io_write_data(ubi, vtbl, new_aeb->pnum, 0, ubi->vtbl_size);
|
||||
if (err)
|
||||
goto write_error;
|
||||
|
||||
/*
|
||||
* And add it to the attaching information. Don't delete the old version
|
||||
* of this LEB as it will be deleted and freed in 'ubi_add_to_av()'.
|
||||
*/
|
||||
err = ubi_add_to_av(ubi, ai, new_aeb->pnum, new_aeb->ec, vid_hdr, 0);
|
||||
kmem_cache_free(ai->aeb_slab_cache, new_aeb);
|
||||
ubi_free_vid_hdr(ubi, vid_hdr);
|
||||
return err;
|
||||
|
||||
write_error:
|
||||
if (err == -EIO && ++tries <= 5) {
|
||||
/*
|
||||
* Probably this physical eraseblock went bad, try to pick
|
||||
* another one.
|
||||
*/
|
||||
list_add(&new_aeb->u.list, &ai->erase);
|
||||
goto retry;
|
||||
}
|
||||
kmem_cache_free(ai->aeb_slab_cache, new_aeb);
|
||||
out_free:
|
||||
ubi_free_vid_hdr(ubi, vid_hdr);
|
||||
return err;
|
||||
|
||||
}
|
||||
|
||||
/**
|
||||
* process_lvol - process the layout volume.
|
||||
* @ubi: UBI device description object
|
||||
* @ai: attaching information
|
||||
* @av: layout volume attaching information
|
||||
*
|
||||
* This function is responsible for reading the layout volume, ensuring it is
|
||||
* not corrupted, and recovering from corruptions if needed. Returns volume
|
||||
* table in case of success and a negative error code in case of failure.
|
||||
*/
|
||||
static struct ubi_vtbl_record *process_lvol(struct ubi_device *ubi,
|
||||
struct ubi_attach_info *ai,
|
||||
struct ubi_ainf_volume *av)
|
||||
{
|
||||
int err;
|
||||
struct rb_node *rb;
|
||||
struct ubi_ainf_peb *aeb;
|
||||
struct ubi_vtbl_record *leb[UBI_LAYOUT_VOLUME_EBS] = { NULL, NULL };
|
||||
int leb_corrupted[UBI_LAYOUT_VOLUME_EBS] = {1, 1};
|
||||
|
||||
/*
|
||||
* UBI goes through the following steps when it changes the layout
|
||||
* volume:
|
||||
* a. erase LEB 0;
|
||||
* b. write new data to LEB 0;
|
||||
* c. erase LEB 1;
|
||||
* d. write new data to LEB 1.
|
||||
*
|
||||
* Before the change, both LEBs contain the same data.
|
||||
*
|
||||
* Due to unclean reboots, the contents of LEB 0 may be lost, but there
|
||||
* should LEB 1. So it is OK if LEB 0 is corrupted while LEB 1 is not.
|
||||
* Similarly, LEB 1 may be lost, but there should be LEB 0. And
|
||||
* finally, unclean reboots may result in a situation when neither LEB
|
||||
* 0 nor LEB 1 are corrupted, but they are different. In this case, LEB
|
||||
* 0 contains more recent information.
|
||||
*
|
||||
* So the plan is to first check LEB 0. Then
|
||||
* a. if LEB 0 is OK, it must be containing the most recent data; then
|
||||
* we compare it with LEB 1, and if they are different, we copy LEB
|
||||
* 0 to LEB 1;
|
||||
* b. if LEB 0 is corrupted, but LEB 1 has to be OK, and we copy LEB 1
|
||||
* to LEB 0.
|
||||
*/
|
||||
|
||||
dbg_gen("check layout volume");
|
||||
|
||||
/* Read both LEB 0 and LEB 1 into memory */
|
||||
ubi_rb_for_each_entry(rb, aeb, &av->root, u.rb) {
|
||||
leb[aeb->lnum] = vzalloc(ubi->vtbl_size);
|
||||
if (!leb[aeb->lnum]) {
|
||||
err = -ENOMEM;
|
||||
goto out_free;
|
||||
}
|
||||
|
||||
err = ubi_io_read_data(ubi, leb[aeb->lnum], aeb->pnum, 0,
|
||||
ubi->vtbl_size);
|
||||
if (err == UBI_IO_BITFLIPS || mtd_is_eccerr(err))
|
||||
/*
|
||||
* Scrub the PEB later. Note, -EBADMSG indicates an
|
||||
* uncorrectable ECC error, but we have our own CRC and
|
||||
* the data will be checked later. If the data is OK,
|
||||
* the PEB will be scrubbed (because we set
|
||||
* aeb->scrub). If the data is not OK, the contents of
|
||||
* the PEB will be recovered from the second copy, and
|
||||
* aeb->scrub will be cleared in
|
||||
* 'ubi_add_to_av()'.
|
||||
*/
|
||||
aeb->scrub = 1;
|
||||
else if (err)
|
||||
goto out_free;
|
||||
}
|
||||
|
||||
err = -EINVAL;
|
||||
if (leb[0]) {
|
||||
leb_corrupted[0] = vtbl_check(ubi, leb[0]);
|
||||
if (leb_corrupted[0] < 0)
|
||||
goto out_free;
|
||||
}
|
||||
|
||||
if (!leb_corrupted[0]) {
|
||||
/* LEB 0 is OK */
|
||||
if (leb[1])
|
||||
leb_corrupted[1] = memcmp(leb[0], leb[1],
|
||||
ubi->vtbl_size);
|
||||
if (leb_corrupted[1]) {
|
||||
ubi_warn(ubi, "volume table copy #2 is corrupted");
|
||||
err = create_vtbl(ubi, ai, 1, leb[0]);
|
||||
if (err)
|
||||
goto out_free;
|
||||
ubi_msg(ubi, "volume table was restored");
|
||||
}
|
||||
|
||||
/* Both LEB 1 and LEB 2 are OK and consistent */
|
||||
vfree(leb[1]);
|
||||
return leb[0];
|
||||
} else {
|
||||
/* LEB 0 is corrupted or does not exist */
|
||||
if (leb[1]) {
|
||||
leb_corrupted[1] = vtbl_check(ubi, leb[1]);
|
||||
if (leb_corrupted[1] < 0)
|
||||
goto out_free;
|
||||
}
|
||||
if (leb_corrupted[1]) {
|
||||
/* Both LEB 0 and LEB 1 are corrupted */
|
||||
ubi_err(ubi, "both volume tables are corrupted");
|
||||
goto out_free;
|
||||
}
|
||||
|
||||
ubi_warn(ubi, "volume table copy #1 is corrupted");
|
||||
err = create_vtbl(ubi, ai, 0, leb[1]);
|
||||
if (err)
|
||||
goto out_free;
|
||||
ubi_msg(ubi, "volume table was restored");
|
||||
|
||||
vfree(leb[0]);
|
||||
return leb[1];
|
||||
}
|
||||
|
||||
out_free:
|
||||
vfree(leb[0]);
|
||||
vfree(leb[1]);
|
||||
return ERR_PTR(err);
|
||||
}
|
||||
|
||||
/**
|
||||
* create_empty_lvol - create empty layout volume.
|
||||
* @ubi: UBI device description object
|
||||
* @ai: attaching information
|
||||
*
|
||||
* This function returns volume table contents in case of success and a
|
||||
* negative error code in case of failure.
|
||||
*/
|
||||
static struct ubi_vtbl_record *create_empty_lvol(struct ubi_device *ubi,
|
||||
struct ubi_attach_info *ai)
|
||||
{
|
||||
int i;
|
||||
struct ubi_vtbl_record *vtbl;
|
||||
|
||||
vtbl = vzalloc(ubi->vtbl_size);
|
||||
if (!vtbl)
|
||||
return ERR_PTR(-ENOMEM);
|
||||
|
||||
for (i = 0; i < ubi->vtbl_slots; i++)
|
||||
memcpy(&vtbl[i], &empty_vtbl_record, UBI_VTBL_RECORD_SIZE);
|
||||
|
||||
for (i = 0; i < UBI_LAYOUT_VOLUME_EBS; i++) {
|
||||
int err;
|
||||
|
||||
err = create_vtbl(ubi, ai, i, vtbl);
|
||||
if (err) {
|
||||
vfree(vtbl);
|
||||
return ERR_PTR(err);
|
||||
}
|
||||
}
|
||||
|
||||
return vtbl;
|
||||
}
|
||||
|
||||
/**
|
||||
* init_volumes - initialize volume information for existing volumes.
|
||||
* @ubi: UBI device description object
|
||||
* @ai: scanning information
|
||||
* @vtbl: volume table
|
||||
*
|
||||
* This function allocates volume description objects for existing volumes.
|
||||
* Returns zero in case of success and a negative error code in case of
|
||||
* failure.
|
||||
*/
|
||||
static int init_volumes(struct ubi_device *ubi,
|
||||
const struct ubi_attach_info *ai,
|
||||
const struct ubi_vtbl_record *vtbl)
|
||||
{
|
||||
int i, reserved_pebs = 0;
|
||||
struct ubi_ainf_volume *av;
|
||||
struct ubi_volume *vol;
|
||||
|
||||
for (i = 0; i < ubi->vtbl_slots; i++) {
|
||||
cond_resched();
|
||||
|
||||
if (be32_to_cpu(vtbl[i].reserved_pebs) == 0)
|
||||
continue; /* Empty record */
|
||||
|
||||
vol = kzalloc(sizeof(struct ubi_volume), GFP_KERNEL);
|
||||
if (!vol)
|
||||
return -ENOMEM;
|
||||
|
||||
vol->reserved_pebs = be32_to_cpu(vtbl[i].reserved_pebs);
|
||||
vol->alignment = be32_to_cpu(vtbl[i].alignment);
|
||||
vol->data_pad = be32_to_cpu(vtbl[i].data_pad);
|
||||
vol->upd_marker = vtbl[i].upd_marker;
|
||||
vol->vol_type = vtbl[i].vol_type == UBI_VID_DYNAMIC ?
|
||||
UBI_DYNAMIC_VOLUME : UBI_STATIC_VOLUME;
|
||||
vol->name_len = be16_to_cpu(vtbl[i].name_len);
|
||||
vol->usable_leb_size = ubi->leb_size - vol->data_pad;
|
||||
memcpy(vol->name, vtbl[i].name, vol->name_len);
|
||||
vol->name[vol->name_len] = '\0';
|
||||
vol->vol_id = i;
|
||||
|
||||
if (vtbl[i].flags & UBI_VTBL_AUTORESIZE_FLG) {
|
||||
/* Auto re-size flag may be set only for one volume */
|
||||
if (ubi->autoresize_vol_id != -1) {
|
||||
ubi_err(ubi, "more than one auto-resize volume (%d and %d)",
|
||||
ubi->autoresize_vol_id, i);
|
||||
kfree(vol);
|
||||
return -EINVAL;
|
||||
}
|
||||
|
||||
ubi->autoresize_vol_id = i;
|
||||
}
|
||||
|
||||
ubi_assert(!ubi->volumes[i]);
|
||||
ubi->volumes[i] = vol;
|
||||
ubi->vol_count += 1;
|
||||
vol->ubi = ubi;
|
||||
reserved_pebs += vol->reserved_pebs;
|
||||
|
||||
/*
|
||||
* In case of dynamic volume UBI knows nothing about how many
|
||||
* data is stored there. So assume the whole volume is used.
|
||||
*/
|
||||
if (vol->vol_type == UBI_DYNAMIC_VOLUME) {
|
||||
vol->used_ebs = vol->reserved_pebs;
|
||||
vol->last_eb_bytes = vol->usable_leb_size;
|
||||
vol->used_bytes =
|
||||
(long long)vol->used_ebs * vol->usable_leb_size;
|
||||
continue;
|
||||
}
|
||||
|
||||
/* Static volumes only */
|
||||
av = ubi_find_av(ai, i);
|
||||
if (!av || !av->leb_count) {
|
||||
/*
|
||||
* No eraseblocks belonging to this volume found. We
|
||||
* don't actually know whether this static volume is
|
||||
* completely corrupted or just contains no data. And
|
||||
* we cannot know this as long as data size is not
|
||||
* stored on flash. So we just assume the volume is
|
||||
* empty. FIXME: this should be handled.
|
||||
*/
|
||||
continue;
|
||||
}
|
||||
|
||||
if (av->leb_count != av->used_ebs) {
|
||||
/*
|
||||
* We found a static volume which misses several
|
||||
* eraseblocks. Treat it as corrupted.
|
||||
*/
|
||||
ubi_warn(ubi, "static volume %d misses %d LEBs - corrupted",
|
||||
av->vol_id, av->used_ebs - av->leb_count);
|
||||
vol->corrupted = 1;
|
||||
continue;
|
||||
}
|
||||
|
||||
vol->used_ebs = av->used_ebs;
|
||||
vol->used_bytes =
|
||||
(long long)(vol->used_ebs - 1) * vol->usable_leb_size;
|
||||
vol->used_bytes += av->last_data_size;
|
||||
vol->last_eb_bytes = av->last_data_size;
|
||||
}
|
||||
|
||||
/* And add the layout volume */
|
||||
vol = kzalloc(sizeof(struct ubi_volume), GFP_KERNEL);
|
||||
if (!vol)
|
||||
return -ENOMEM;
|
||||
|
||||
vol->reserved_pebs = UBI_LAYOUT_VOLUME_EBS;
|
||||
vol->alignment = UBI_LAYOUT_VOLUME_ALIGN;
|
||||
vol->vol_type = UBI_DYNAMIC_VOLUME;
|
||||
vol->name_len = sizeof(UBI_LAYOUT_VOLUME_NAME) - 1;
|
||||
memcpy(vol->name, UBI_LAYOUT_VOLUME_NAME, vol->name_len + 1);
|
||||
vol->usable_leb_size = ubi->leb_size;
|
||||
vol->used_ebs = vol->reserved_pebs;
|
||||
vol->last_eb_bytes = vol->reserved_pebs;
|
||||
vol->used_bytes =
|
||||
(long long)vol->used_ebs * (ubi->leb_size - vol->data_pad);
|
||||
vol->vol_id = UBI_LAYOUT_VOLUME_ID;
|
||||
vol->ref_count = 1;
|
||||
|
||||
ubi_assert(!ubi->volumes[i]);
|
||||
ubi->volumes[vol_id2idx(ubi, vol->vol_id)] = vol;
|
||||
reserved_pebs += vol->reserved_pebs;
|
||||
ubi->vol_count += 1;
|
||||
vol->ubi = ubi;
|
||||
|
||||
if (reserved_pebs > ubi->avail_pebs) {
|
||||
ubi_err(ubi, "not enough PEBs, required %d, available %d",
|
||||
reserved_pebs, ubi->avail_pebs);
|
||||
if (ubi->corr_peb_count)
|
||||
ubi_err(ubi, "%d PEBs are corrupted and not used",
|
||||
ubi->corr_peb_count);
|
||||
}
|
||||
ubi->rsvd_pebs += reserved_pebs;
|
||||
ubi->avail_pebs -= reserved_pebs;
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
||||
/**
|
||||
* check_av - check volume attaching information.
|
||||
* @vol: UBI volume description object
|
||||
* @av: volume attaching information
|
||||
*
|
||||
* This function returns zero if the volume attaching information is consistent
|
||||
* to the data read from the volume tabla, and %-EINVAL if not.
|
||||
*/
|
||||
static int check_av(const struct ubi_volume *vol,
|
||||
const struct ubi_ainf_volume *av)
|
||||
{
|
||||
int err;
|
||||
|
||||
if (av->highest_lnum >= vol->reserved_pebs) {
|
||||
err = 1;
|
||||
goto bad;
|
||||
}
|
||||
if (av->leb_count > vol->reserved_pebs) {
|
||||
err = 2;
|
||||
goto bad;
|
||||
}
|
||||
if (av->vol_type != vol->vol_type) {
|
||||
err = 3;
|
||||
goto bad;
|
||||
}
|
||||
if (av->used_ebs > vol->reserved_pebs) {
|
||||
err = 4;
|
||||
goto bad;
|
||||
}
|
||||
if (av->data_pad != vol->data_pad) {
|
||||
err = 5;
|
||||
goto bad;
|
||||
}
|
||||
return 0;
|
||||
|
||||
bad:
|
||||
ubi_err(vol->ubi, "bad attaching information, error %d", err);
|
||||
ubi_dump_av(av);
|
||||
ubi_dump_vol_info(vol);
|
||||
return -EINVAL;
|
||||
}
|
||||
|
||||
/**
|
||||
* check_attaching_info - check that attaching information.
|
||||
* @ubi: UBI device description object
|
||||
* @ai: attaching information
|
||||
*
|
||||
* Even though we protect on-flash data by CRC checksums, we still don't trust
|
||||
* the media. This function ensures that attaching information is consistent to
|
||||
* the information read from the volume table. Returns zero if the attaching
|
||||
* information is OK and %-EINVAL if it is not.
|
||||
*/
|
||||
static int check_attaching_info(const struct ubi_device *ubi,
|
||||
struct ubi_attach_info *ai)
|
||||
{
|
||||
int err, i;
|
||||
struct ubi_ainf_volume *av;
|
||||
struct ubi_volume *vol;
|
||||
|
||||
if (ai->vols_found > UBI_INT_VOL_COUNT + ubi->vtbl_slots) {
|
||||
ubi_err(ubi, "found %d volumes while attaching, maximum is %d + %d",
|
||||
ai->vols_found, UBI_INT_VOL_COUNT, ubi->vtbl_slots);
|
||||
return -EINVAL;
|
||||
}
|
||||
|
||||
if (ai->highest_vol_id >= ubi->vtbl_slots + UBI_INT_VOL_COUNT &&
|
||||
ai->highest_vol_id < UBI_INTERNAL_VOL_START) {
|
||||
ubi_err(ubi, "too large volume ID %d found",
|
||||
ai->highest_vol_id);
|
||||
return -EINVAL;
|
||||
}
|
||||
|
||||
for (i = 0; i < ubi->vtbl_slots + UBI_INT_VOL_COUNT; i++) {
|
||||
cond_resched();
|
||||
|
||||
av = ubi_find_av(ai, i);
|
||||
vol = ubi->volumes[i];
|
||||
if (!vol) {
|
||||
if (av)
|
||||
ubi_remove_av(ai, av);
|
||||
continue;
|
||||
}
|
||||
|
||||
if (vol->reserved_pebs == 0) {
|
||||
ubi_assert(i < ubi->vtbl_slots);
|
||||
|
||||
if (!av)
|
||||
continue;
|
||||
|
||||
/*
|
||||
* During attaching we found a volume which does not
|
||||
* exist according to the information in the volume
|
||||
* table. This must have happened due to an unclean
|
||||
* reboot while the volume was being removed. Discard
|
||||
* these eraseblocks.
|
||||
*/
|
||||
ubi_msg(ubi, "finish volume %d removal", av->vol_id);
|
||||
ubi_remove_av(ai, av);
|
||||
} else if (av) {
|
||||
err = check_av(vol, av);
|
||||
if (err)
|
||||
return err;
|
||||
}
|
||||
}
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
||||
/**
|
||||
* ubi_read_volume_table - read the volume table.
|
||||
* @ubi: UBI device description object
|
||||
* @ai: attaching information
|
||||
*
|
||||
* This function reads volume table, checks it, recover from errors if needed,
|
||||
* or creates it if needed. Returns zero in case of success and a negative
|
||||
* error code in case of failure.
|
||||
*/
|
||||
int ubi_read_volume_table(struct ubi_device *ubi, struct ubi_attach_info *ai)
|
||||
{
|
||||
int i, err;
|
||||
struct ubi_ainf_volume *av;
|
||||
|
||||
empty_vtbl_record.crc = cpu_to_be32(0xf116c36b);
|
||||
|
||||
/*
|
||||
* The number of supported volumes is limited by the eraseblock size
|
||||
* and by the UBI_MAX_VOLUMES constant.
|
||||
*/
|
||||
ubi->vtbl_slots = ubi->leb_size / UBI_VTBL_RECORD_SIZE;
|
||||
if (ubi->vtbl_slots > UBI_MAX_VOLUMES)
|
||||
ubi->vtbl_slots = UBI_MAX_VOLUMES;
|
||||
|
||||
ubi->vtbl_size = ubi->vtbl_slots * UBI_VTBL_RECORD_SIZE;
|
||||
ubi->vtbl_size = ALIGN(ubi->vtbl_size, ubi->min_io_size);
|
||||
|
||||
av = ubi_find_av(ai, UBI_LAYOUT_VOLUME_ID);
|
||||
if (!av) {
|
||||
/*
|
||||
* No logical eraseblocks belonging to the layout volume were
|
||||
* found. This could mean that the flash is just empty. In
|
||||
* this case we create empty layout volume.
|
||||
*
|
||||
* But if flash is not empty this must be a corruption or the
|
||||
* MTD device just contains garbage.
|
||||
*/
|
||||
if (ai->is_empty) {
|
||||
ubi->vtbl = create_empty_lvol(ubi, ai);
|
||||
if (IS_ERR(ubi->vtbl))
|
||||
return PTR_ERR(ubi->vtbl);
|
||||
} else {
|
||||
ubi_err(ubi, "the layout volume was not found");
|
||||
return -EINVAL;
|
||||
}
|
||||
} else {
|
||||
if (av->leb_count > UBI_LAYOUT_VOLUME_EBS) {
|
||||
/* This must not happen with proper UBI images */
|
||||
ubi_err(ubi, "too many LEBs (%d) in layout volume",
|
||||
av->leb_count);
|
||||
return -EINVAL;
|
||||
}
|
||||
|
||||
ubi->vtbl = process_lvol(ubi, ai, av);
|
||||
if (IS_ERR(ubi->vtbl))
|
||||
return PTR_ERR(ubi->vtbl);
|
||||
}
|
||||
|
||||
ubi->avail_pebs = ubi->good_peb_count - ubi->corr_peb_count;
|
||||
|
||||
/*
|
||||
* The layout volume is OK, initialize the corresponding in-RAM data
|
||||
* structures.
|
||||
*/
|
||||
err = init_volumes(ubi, ai, ubi->vtbl);
|
||||
if (err)
|
||||
goto out_free;
|
||||
|
||||
/*
|
||||
* Make sure that the attaching information is consistent to the
|
||||
* information stored in the volume table.
|
||||
*/
|
||||
err = check_attaching_info(ubi, ai);
|
||||
if (err)
|
||||
goto out_free;
|
||||
|
||||
return 0;
|
||||
|
||||
out_free:
|
||||
vfree(ubi->vtbl);
|
||||
for (i = 0; i < ubi->vtbl_slots + UBI_INT_VOL_COUNT; i++) {
|
||||
kfree(ubi->volumes[i]);
|
||||
ubi->volumes[i] = NULL;
|
||||
}
|
||||
return err;
|
||||
}
|
||||
|
||||
/**
|
||||
* self_vtbl_check - check volume table.
|
||||
* @ubi: UBI device description object
|
||||
*/
|
||||
static void self_vtbl_check(const struct ubi_device *ubi)
|
||||
{
|
||||
if (!ubi_dbg_chk_gen(ubi))
|
||||
return;
|
||||
|
||||
if (vtbl_check(ubi, ubi->vtbl)) {
|
||||
ubi_err(ubi, "self-check failed");
|
||||
BUG();
|
||||
}
|
||||
}
|
||||
1858
u-boot/drivers/mtd/ubi/wl.c
Normal file
1858
u-boot/drivers/mtd/ubi/wl.c
Normal file
File diff suppressed because it is too large
Load Diff
34
u-boot/drivers/mtd/ubi/wl.h
Normal file
34
u-boot/drivers/mtd/ubi/wl.h
Normal file
@@ -0,0 +1,34 @@
|
||||
#ifndef UBI_WL_H
|
||||
#define UBI_WL_H
|
||||
#ifdef CONFIG_MTD_UBI_FASTMAP
|
||||
static int anchor_pebs_avalible(struct rb_root *root);
|
||||
#ifndef __UBOOT__
|
||||
static void update_fastmap_work_fn(struct work_struct *wrk);
|
||||
#else
|
||||
void update_fastmap_work_fn(struct ubi_device *ubi);
|
||||
#endif
|
||||
static struct ubi_wl_entry *find_anchor_wl_entry(struct rb_root *root);
|
||||
static struct ubi_wl_entry *get_peb_for_wl(struct ubi_device *ubi);
|
||||
static void ubi_fastmap_close(struct ubi_device *ubi);
|
||||
static inline void ubi_fastmap_init(struct ubi_device *ubi, int *count)
|
||||
{
|
||||
/* Reserve enough LEBs to store two fastmaps. */
|
||||
*count += (ubi->fm_size / ubi->leb_size) * 2;
|
||||
#ifndef __UBOOT__
|
||||
INIT_WORK(&ubi->fm_work, update_fastmap_work_fn);
|
||||
#endif
|
||||
}
|
||||
static struct ubi_wl_entry *may_reserve_for_fm(struct ubi_device *ubi,
|
||||
struct ubi_wl_entry *e,
|
||||
struct rb_root *root);
|
||||
#else /* !CONFIG_MTD_UBI_FASTMAP */
|
||||
static struct ubi_wl_entry *get_peb_for_wl(struct ubi_device *ubi);
|
||||
static inline void ubi_fastmap_close(struct ubi_device *ubi) { }
|
||||
static inline void ubi_fastmap_init(struct ubi_device *ubi, int *count) { }
|
||||
static struct ubi_wl_entry *may_reserve_for_fm(struct ubi_device *ubi,
|
||||
struct ubi_wl_entry *e,
|
||||
struct rb_root *root) {
|
||||
return e;
|
||||
}
|
||||
#endif /* CONFIG_MTD_UBI_FASTMAP */
|
||||
#endif /* UBI_WL_H */
|
||||
Reference in New Issue
Block a user