Creating UBIFS Images on AM335x Under Linux
This document explains how to use MTD tools to create a UBIFS image, with parameters varying based on the specific NAND Flash used.
1. Analyzing the NAND Flash
Taking Samsung's 256MB K9F2G08U0M NAND Flash as an example:
· Page Write Size: 2KB
· Block Erase Size: 64 pages = 128KB
· Device Size: 2048 blocks = 2112 Mbits
· Physical Erase Block (PEB): Physical block
· Logical Erase Block (LEB): Logical block, actual usable space
Step 1: Creating the UBIFS Image
Run the following command:
mkfs.ubifs -r filesystem/ -F -o ubifs.img -m 2048 -e 126976 -c 1580
-m 2KiB (or 2048): Page size
-e 124KiB (or 126976): Erase block size
Erase Block Size:
UBI requires at least two IO page units in each logical block (PEB) for overhead: one to store erase count information and one to store Volume ID information. Since PEB is 128KB in size, the usable logical block is calculated as follows:
-c 1580: Maximum number of LEBs, calculated as shown below.
-r filesystem: Specifies the filesystem to be packaged.
-F: Indicates to fill free space during the first mount (see Linux MTD FAQ).
-o ubifs.img: Output file name.
Step 2: Creating UBI Image
Run the following command:
-o ubi.img: Output file.
-m 2KiB (or 2048): Page size.
-p 128KiB: Physical logical block size.
-O 2048: Offset if the VID header is at the beginning of the physical logical block.
After executing this command, the generated ubi.img
will be the required image.
UBI Calculations
Usable Size Calculation
UBI reserves some blocks for managing bad blocks (PEB):
2 PEBs for storing the UBI volume table.
1 PEB reserved for wear-leveling purposes.
1 PEB reserved for atomic LEB change operations.
A percentage of PEBs reserved for handling bad blocks (default for NAND is 1%).
UBI stores erase count (EC) and volume ID (VID); these two pages are at the header of each block (PEB).
Symbol | Meaning | Value for XO test case |
SP | PEB Size | 128KiB |
SL | LEB Size | 128KiB - 2 * 2KiB = 124 KiB |
P | MTD | 200MiB / 128KiB = 1600 |
B | Badblock size | 1% of P = 16 |
O | EC and VID headers i.e. O = SP - SL | 4KiB |
UBI Overhead Calculation
The UBI overhead is calculated as:
(
Substituting the values:
=
This translates to approximately 69.375 PEBs (rounded to 69).
(B + 4) \times SP accounts for bad blocks and the 4 blocks reserved for UBI system use, multiplied by the PEB size.
O \times (P - B - 4) accounts for the space wasted by each block occupying 4KiB
Thus, the remaining usable PEBs for data storage are:
1600−69=1531 PEBs or 195968KiB1600 - 69 = 1531 \text{ PEBs} \text{ or } 195968KiB1600−69=1531 PEBs or 195968KiB
The reason for using -c 1580 in mkfs.ubifs is that c refers to the maximum number of filesystem blocks, calculated based on the LEB size of 124KiB, not the PEB size of 128KiB:
195968KiB/124KiB≈1580.39 (rounded to 1580).195968KiB / 124KiB \approx 1580.39 \text{ (rounded to 1580)}.195968KiB/124KiB≈1580.39 (rounded to 1580).
Volume Size: 195968KiB (~192MiB)
weathink AM335x SOM:https://www.weathink.com/products/hexinban/2.html