转:http://wiki.laptop.org/go/NAND_Flash_Bad_Block_Table

OLPC NAND Bad Block Management

Introduction

This document describes the on-FLASH data structures that OLPC uses to maintain NAND FLASH bad-block information. It is a specific subcase of the general NAND FLASH bad-block scheme in the Linux "mtd" (memory technology device) subsystem as of Linux version 2.6.22.

This document focuses on the specific choices that are relevant for OLPC, omitting inapplicable variations of the general scheme. Those choices include:

  • Only large-page (2K page) NAND devices
  • The specific ECC format used by the OLPC "CaFe" NAND controller
  • Handling of multi-chip NAND devices
  • Location of bad block tables

Basic Concepts

  • "Block" means the NAND FLASH erasure unit, e.g. 128 KBytes.
  • "Page" means the write-with-ECC unit, i.e. 2 Kbytes (since we are only dealing with "large page" NAND devices).
  • "OOB" (Out Of Band) means the 64 bytes of extra storage that is associated with each NAND page. Part of OOB is used for hardware-generated ECC, and part is available for software use.

NAND FLASH chips typically have some bad blocks as shipped from the factory, and additional blocks may go bad over time due to "wear" from repeated erasure. A factory-fresh NAND chip is shipped in the "erased" state with every good page and its OOB containing all 0xFF bytes. Factory-detected bad blocks have 0 in the first OOB byte of the first or second page in that block.

The factory bad block information must be copied elsewhere before the first time that the device is written, because writes to other blocks could thus make them appear to be bad. (In particular, the CaFe chip uses the first OOB byte for hardware-generated CRC, so it's not feasible to reserve that byte to mean "this block is bad".) "Bad block table" means the storage for the bad block information after it has been copied from the factory locations (and later amended to include blocks that have gone bad from wear).

The various software components that access the NAND FLASH, including the boot firmware and OS drivers, must understand the bad block table, using it to avoid preexisting bad blocks and updating it with newly-detected bad blocks.

The bad block table is a critical resource whose integrity is essential for the system to function properly, so updates must be done carefully to prevent loss of its information if an update is interrupted (perhaps due to power loss at a bad time). In general, it's not possible to fully recover the bad block information by re-scanning the device. The bad block storage format is redundant, and there is a prescribed procedure for updating it safely.

Multi-Chip Devices

The totality of onboard NAND FLASH storage on an OLPC system can be arranged in several ways. There might a single silicon chip in a single package, two chips in one package, or multiple packages each with one or two chips inside. The driver software "hides" this internal organization, so the totality of onboard NAND FLASH appears as a single contiguous array.

The implication for this bad block table specification is that a single bad block table (with its redundant mirror copy) encompasses all of the chips and packages, instead of the bad block information being "per chip".

Bad Block Table Location

The bad block table consists of two subtables, a primary copy and a mirror copy. Each subtable is in a separate block, beginning in the first page of its block. The bad block table blocks are located somewhere within the last four blocks of NAND FLASH. Only two blocks would be necessary in the absence of bad blocks within the last four; the range of four provides a little slack in case one or two of those blocks is bad.

When software scans to find the bad block tables, it starts at the end of NAND FLASH and scans backwards until either both tables (primary and mirror) are found, or until 4 blocks have been scanned.

A NAND FLASH device with 3 or more factory bad blocks in the last 4 cannot be used. This is a rare occurence that reduces factory yield insignificantly. Furthermore, blocks in that region should wear out quite infrequently, with an insignificant effect on field failure rate.

Bad Block Table Format

A bad block subtable consists of an identifying header and a bitmap describing the dispostion of all the device's blocks.

The header is present in the OOB of each occupied page in the block that contains the bad block subtable. The bitmap is stored in the page data area, spanning as many pages as necessary to map the entire NAND FLASH.

The bitmap has two bits for each block, so each 2 KB page can map 2 K * 4 = 8K blocks = 1 GB. A 4 GB FLASH thus requires 4 pages. The format works for NAND FLASH sizes up to 128 GB - assuming that the block size remains 128 KB. (That assumption is not necessarily a good one, as larger FLASH devices might have larger block sizes.)

The header includes a signature that identifies the block as either a primary or a mirror bad block subtable and an incrementing version number that is used to detect and recover from interrupted updates.

Header Format

The bad block subtable header is in the OOB of each page containing bad block information.

The signature is at OOB offset 0x0e-0x11 (bytes 0-0xd are used by the controller chip's hardware ECC).

Primary signature: 0xe: 'B' 0xf: 'b' 0x10: 't' 0x11: '0'

Mirror signature: 0xe: '1' 0xf: 't' 0x10: 'b' 0x11: 'B'

The version number is one byte at OOB offset 0x12. The version number increments on each bad block table update, wrapping from 0xff back to 0. (Updates are typically very infrequent.) Version number comparison must be done with "circular arithmetic modulo 256" as described in #Update Procedure.

Bitmap Format

The bad block bitmap begins at the start of the first page in the block and spans as many pages as necessary.

The bitmap has two bits for each block on the NAND FLASH device, encoded as:

  • 11 - good block
  • 10 - reserved - most software should interpret as "don't use this block"
  • 01 - block went bad during use
  • 00 - block was marked bad by the factory

Bitmap entries are stored in little-endian order - the two least-significant bits of a byte are for block numbers equal to (0 mod 4), the next two bits are for blocks (1 mod 4), etc. In C:

 uint_8 bitmap[], byte;
byte = bitmap[block_number / 4]; // 4 entries per byte
bitshift = (block_number % 4) * 2; // 4 entries per byte, 2 bits per entry
bits = (byte >> bitshift) & 3; // Shift and extract 2 bits

The total number of bytes in the bitmap is total_blocks/4 .

Consistency Checks

For the bad block table to be consistent, all of the following must be true:

  • There must be a good primary subtable and a good mirror subtable
  • The version numbers of the primary and the mirror must be the same
  • The primary and secondary bitmaps must be the same

For a primary or secondary subtable to be good:

  • The right number of pages must be occupied, i.e. they mustAll the pages must have the same version

    • Read with no uncorrectable errors
    • Have the same signature (either primary or mirror)

Safe Update Procedure

This safe update procedure prevents loss of the bad block information if a bad block table update is interrupted, by ensuring that at least one copy of the information is always present.

Assumed starting point: there is a primary bad block table and a mirror table that are consistent, i.e. with the same data and the same version number N.

  1. Add new bad blocks to a RAM copy of the bad block table.
  2. Erase the block containing the primary bad block table.
  3. Write the new bad block table to that block. For each page thus occupied, set the header in the OOB area to the primary signature, with version number N+1 (mod 256).
  4. Erase the block containing the secondary bad block table.
  5. Write the new bad block table to that block. For each page thus occupied, set the header in the OOB area to the mirror signature, with version number N+1 (mod 256).

If the all steps complete without error, a scan at a later time will find a self-consistent bad block table.

If a later scan finds an inconsistent table (see #Consistency_Checks), the #Recovery_Procedure should be used to restore the table.

Recovery Procedure

Recovery from Interrupted Write

If the update process is interrupted (e.g. by loss of power at a bad time), a subsequent scan will detect an inconsistent table. There are several cases:

  • If the interruption happened during or just after update step 2, there will be a mirror but no primary. Recover by re-erasing the next available free block in the bad block table region, then writing a copy of the mirror table to that block, with a primary signature and the same version number as the mirror. (The information about the bad blocks that were added during the update will be lost, but the previous information will be intact.)
  • If the interruption happened during step 3, the mirror will be good, and the primary will be partially present but not completely good (e.g. some pages may be unwritten or may have read errors). Recover by re-erasing the partial primary block, then writing a copy of the mirror table to that block, with a primary signature and the same version number as the mirror. (The information about the bad blocks that were added during the update will be lost, but the previous information will be intact.)
  • If the interruption happened just after step 3, both the mirror and the primary will be good, but the primary version number will be greater than then mirror version number. Recover by erasing the mirror block, then writing a copy of the primary table to that block, with a mirror signature and the same version as the primary. (This is a full recovery without loss of information.)
  • If the interruption happened during or just after step 4, the mirror will be nonexistent. Recover by re-erasing the next available free block in the bad block table region, then writing a copy of the primary table to that block, with a mirror signature and the same version number as the primary. (This is a full recovery without loss of information.)
  • If the interruption happened during step 5, the mirror will be partially present, but not completely good. Recover by re-erasing the mirror block, then writing a copy of the primary table to that block, with a mirror signature and the same version number as the primary. (This is a full recovery without loss of information.)
  • There is a further possible inconsistency that is not covered by any of the above cases - the primary and mirror might both be good, with the same version, but with different bitmap data. It's unclear how this could happen, but if it does, the recovery procedure is to form a new in-memory bitmap as the logical AND of the primary and mirror, then perform update steps 2-5 with the new data. This has the effect of merging the two bitmaps to include any bad blocks reported in either.

Recovery from Bad Write

A write or erase operation could fail during a bad block table update - not an interrupted write caused by an external event like loss of power, but rather a write failure that is detected by the driver while the system is still running. This could happen if the block used for the bad block table wears out.

To recover from this, that block must be removed from use and the bad block data placed in a different block. To find a suitable block, we use the following:

  • The last 4 NAND blocks are reserved for use by the bad block table.
  • Blocks in that range, if written at all, are always written with either a primary or a mirror signature in the first page's OOB.
  • Therefore, if a block in that range contains 0 in the first OOB byte and does not contain a primary or mirror signature, it is a bad block.
  • If a block in that range goes bad during a table update, force it to look like a factory bad block by writing all 0's into its first page's OOB, thus taking it out of service.
  • Choose another block in that range to write the data to.

This algorithm isn't foolproof, but it works almost all the time, and the probability of those blocks wearing out is already low because of infrequent bad block table updates, so the algorithm is good enough.

Version Comparision

Version numbers, which are 8 bits wide, must be compared with circular arithmetic modulo 256, to give the correct answer when the version number increments from 255 to 0. This can be done in C with:

  signed char va, vb;
if (va - vb > 0) {
// va is greater than vb
} else {
// va is less than or equal to vb
}

NAND Flash Bad Block Table的更多相关文章

  1. 编程器NAND Flash 技术入门

    NAND Flash分类 SLC(Single-Level Cell)架构:单一储存单元(Cell)可储存1bit data MLC(Multi-Level Cell)架构:单一储存单元(Cell)可 ...

  2. NAND Flash vs NOR Flash

    Avinash Aravindan reference:https://www.embedded.com/design/prototyping-and-development/4460910/2/Fl ...

  3. nand flash详解及驱动编写

    https://www.crifan.com/files/doc/docbook/linux_nand_driver/release/html/linux_nand_driver.html#nand_ ...

  4. Nand Flash基础知识与坏块管理机制的研究

    概述 Flash名称的由来,Flash的擦除操作是以block块为单位的,与此相对应的是其他很多存储设备,是以bit位为最小读取/写入的单位,Flash是一次性地擦除整个块:在发送一个擦除命令后,一次 ...

  5. JZ2440 裸机驱动 第8章 NAND Flash控制器

    本章目标  了解NAND Flash 芯片的接口 掌握通过NAND Flash控制器访问NAND Flash的方法 8.1 NAND Flash介绍和NAND Flash控制器使用     NAND ...

  6. 说说NAND FLASH以及相关ECC校验方法

    Flash名称的由来,Flash的擦除操作是以block块为单位的,与此相对应的是其他很多存储设备,是以bit位为最小读取/写入的单位,Flash是一次性地擦除整个块:在发送一个擦除命令后,一次性地将 ...

  7. WinCE NAND flash - FAL

    http://blog.csdn.net/renpine/article/details/4572347 http://msdn.microsoft.com/en-US/library/ee48203 ...

  8. 【转】nand flash坏块管理OOB,BBT,ECC

    0.NAND的操作管理方式      NAND FLASH的管理方式:以三星FLASH为例,一片Nand flash为一个设备(device),1 (Device) = xxxx (Blocks),1 ...

  9. Samsung K9F1G08U0D SLC NAND FLASH简介(待整理)

    Samsung  K9F1G08U0D,数据存储容量为128M,采用块页式存储管理.8个I/O引脚充当数据.地址.命令的复用端口.详细:http://www.linux-mtd.infradead.o ...

随机推荐

  1. android 在自定义的listview(有刷新加载项)列表中,数据过少时不能铺满整个屏幕时,header和footer同时显示问题

    android  在自定义的listview(有刷新加载项)列表中,数据过少时,当刷新时,加载项也会显示,这是很头疼的一个问题,查阅了一些资料,总结了一个比较不错的方法: 原来代码: @Overrid ...

  2. 剑指offer:二维数组中的查找

    目录 题目 解题思路 具体代码 题目 题目链接 剑指offer:二维数组中的查找 题目描述 在一个二维数组中(每个一维数组的长度相同),每一行都按照从左到右递增的顺序排序,每一列都按照从上到下递增的顺 ...

  3. Android中如何为自定义控件增加状态?

    在android开发中我们常常需要对控件进行相关操作,虽然网上已有很多对控件酷炫的操作,但小编今天给大家分享的纯属实用出发.在查看了一些列安卓教程和文档后,发现了一位大牛分享的非常不错的有关andro ...

  4. [洛谷P1903][国家集训队]数颜色

    题目大意:有$n$支画笔,有两个操作 $Q\;l\;r:$询问$[l,r]$中有几种颜色 $R\;p\;Col:$把第$p$支画笔的颜色改成$Col$ 题解:带修莫队,分为$n^{\frac{1}{3 ...

  5. [luogu3676] 小清新数据结构题 [树链剖分+线段树]

    题面 传送门 思路 本来以为这道题可以LCT维护子树信息直接做的,后来发现这样会因为splay形态改变影响子树权值平方和,是splay本身的局限性导致的 所以只能另辟蹊径 首先,我们考虑询问点都在1的 ...

  6. [hdu] 5696 区间的价值 || 序列分治

    原题 我们定义"区间的价值"为一段区间的最大值*最小值. 一个区间左端点在L,右端点在R,那么该区间的长度为(R−L+1). 求长度分别为1-n的区间的最大价值. 保证数据随机 因 ...

  7. POJ 2243 [SDOI2011]染色 | 树链剖分+线段树

    原题链接 肯定是树链剖分的题啦 树剖怎么做可以看我上一篇博客 如果我们已经剖完了: 然后考虑怎么维护重链和查询 用线段树维护的时候当前区间的区间颜色个数应该等于左儿子+右儿子,但是当左儿子的右端点和右 ...

  8. POJ -1679(次小生成树)模板

    The Unique MST Time Limit: 1000MS   Memory Limit: 10000K Total Submissions:34617   Accepted: 12637 D ...

  9. JSONP原理小记

    大家都知道JSONP(JSON with padding参数式JSON)是跨域传输数据的方法,jq等很多类库都封装了JSONP的方法,但是他的原理是怎样的呢?下面举个我认为最浅显的栗子,大家看过了都会 ...

  10. hdu6188&&百度之星初赛(B) T5

    度度熊的交易计划 Problem Description 度度熊参与了喵哈哈村的商业大会,但是这次商业大会遇到了一个难题: 喵哈哈村以及周围的村庄可以看做是一共由n个片区,m条公路组成的地区. 由于生 ...