调试过程记录,尽可能用最简洁的话把原理说清楚。

mkfs.fat移植和使用

一、引言

1.1、背景

客户需求,需要在arm linux中格式化sd卡,格式化成fat格式。

1.2、文章目的

总结归档,输出快速可移植、可操作、可执行的指引文档。

如果不想看更多的内容,而是直接拿到板子上手使用,请移步到 使用指南 。

二、项目概述

2.1、功能简介

正常使用这个命令。

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
mkfs.fat --help
mkfs.fat 4.2 (2021-01-31)
Usage: mkfs.fat [OPTIONS] TARGET [BLOCKS]
Create FAT filesystem in TARGET, which can be a block device or file. Use only
up to BLOCKS 1024 byte blocks if specified. With the -C option, file TARGET will be
created with a size of 1024 bytes times BLOCKS, which must be specified.

Options:
-a Disable alignment of data structures
-A Toggle Atari variant of the filesystem
-b SECTOR Select SECTOR as location of the FAT32 backup boot sector
-c Check device for bad blocks before creating the filesystem
-C Create file TARGET then create filesystem in it
-D NUMBER Write BIOS drive number NUMBER to boot sector
-f COUNT Create COUNT file allocation tables
-F SIZE Select FAT size SIZE (12, 16 or 32)
-g GEOM Select disk geometry: heads/sectors_per_track
-h NUMBER Write hidden sectors NUMBER to boot sector
-i VOLID Set volume ID to VOLID (a 32 bit hexadecimal number)
-I Ignore and disable safety checks
-l FILENAME Read bad blocks list from FILENAME
-m FILENAME Replace default error message in boot block with contents of FILENAME
-M TYPE Set media type in boot sector to TYPE
--mbr[=y|n|a] Fill (fake) MBR table with one partition which spans whole disk
-n LABEL Set volume name to LABEL (up to 11 characters long)
--codepage=N use DOS codepage N to encode label (default: 850)
-r COUNT Make room for at least COUNT entries in the root directory
-R COUNT Set minimal number of reserved sectors to COUNT
-s COUNT Set number of sectors per cluster to COUNT
-S SIZE Select a sector size of SIZE (a power of two, at least 512)
-v Verbose execution
--variant=TYPE Select variant TYPE of filesystem (standard or Atari)

--invariant Use constants for randomly generated or time based values
--offset=SECTOR Write the filesystem at a specific sector into the device file.
--help Show this help message and exit

三、环境准备

3.1、产品需求

支持 mkfs.fat这条命令

3.2、硬件配置

1、任意ss928的板子,本次使用的是安防S4主板。

2、通过网口完成数据传输及命令行交互

3.3、软件配置

1、secureCRT工具(可选)

2、ssh等一些常用的网络调试工具

四、实现要点

4.1、关键步骤

1)下载源码交叉编译

详细请看如下命令行交互,所有操作都是在编译主机:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
wget https://github.com/dosfstools/dosfstools/archive/refs/tags/v4.2.tar.gz -O dosfstools-4.2.tar.gz

tar -xf dosfstools-4.2.tar.gz
cd dosfstools-4.2

mkdir __install

./autogen.sh
./configure --host=aarch64-linux-gnu CC=aarch64-mix210-linux-gcc --prefix=$(pwd)/__install

make
make install

scp -r __install root@192.168.0.224:/

2)板端更新文件

1
2
3
4
5
6
cd /
cd __install/
chmod -R 777 sbin/
cp sbin/* /usr/sbin/

mkfs.fat --help

3)板端使用这个工具

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
mkfs.fat -v
mkfs.fat 4.2 (2021-01-31)
No device specified.
Usage: mkfs.fat [OPTIONS] TARGET [BLOCKS]
Create FAT filesystem in TARGET, which can be a block device or file. Use only
up to BLOCKS 1024 byte blocks if specified. With the -C option, file TARGET will be
created with a size of 1024 bytes times BLOCKS, which must be specified.

Options:
-a Disable alignment of data structures
-A Toggle Atari variant of the filesystem
-b SECTOR Select SECTOR as location of the FAT32 backup boot sector
-c Check device for bad blocks before creating the filesystem
-C Create file TARGET then create filesystem in it
-D NUMBER Write BIOS drive number NUMBER to boot sector
-f COUNT Create COUNT file allocation tables
-F SIZE Select FAT size SIZE (12, 16 or 32)
-g GEOM Select disk geometry: heads/sectors_per_track
-h NUMBER Write hidden sectors NUMBER to boot sector
-i VOLID Set volume ID to VOLID (a 32 bit hexadecimal number)
-I Ignore and disable safety checks
-l FILENAME Read bad blocks list from FILENAME
-m FILENAME Replace default error message in boot block with contents of FILENAME
-M TYPE Set media type in boot sector to TYPE
--mbr[=y|n|a] Fill (fake) MBR table with one partition which spans whole disk
-n LABEL Set volume name to LABEL (up to 11 characters long)
--codepage=N use DOS codepage N to encode label (default: 850)
-r COUNT Make room for at least COUNT entries in the root directory
-R COUNT Set minimal number of reserved sectors to COUNT
-s COUNT Set number of sectors per cluster to COUNT
-S SIZE Select a sector size of SIZE (a power of two, at least 512)
-v Verbose execution
--variant=TYPE Select variant TYPE of filesystem (standard or Atari)

--invariant Use constants for randomly generated or time based values
--offset=SECTOR Write the filesystem at a specific sector into the device file.
--help Show this help message and exit

五、使用方法

快速上手指南

5.1、PC端下载工具

1
2
3
4
git clone http://10.10.10.7/system/app/rtu_server.git
cd rtu_server/misc_tools/dostools/__install

scp -r __install root@192.168.0.224:/

5.2、板端更新文件

1
2
3
4
5
6
cd /
cd __install/
chmod -R 777 sbin/
cp sbin/* /usr/sbin/

mkfs.fat --help

5.3、实例,格式化sd卡成FAT32

1)插入sdcard到板端并键入命令

1
2
3
mkfs.fat -F 32 -n MYSDCARD /dev/mmcblk1p1 
mkfs.fat 4.2 (2021-01-31)
mount /dev/mmcblk1p1 /sdcard/

特别注意:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
找到sd卡的设备挂载点

fdisk -l

Disk /dev/mmcblk0: 7456 MB, 7818182656 bytes, 15269888 sectors
238592 cylinders, 4 heads, 16 sectors/track
Units: sectors of 1 * 512 = 512 bytes

Disk /dev/mmcblk0 doesn't contain a valid partition table
Disk /dev/mmcblk0p1: 8 MB, 8388608 bytes, 16384 sectors
256 cylinders, 4 heads, 16 sectors/track
Units: sectors of 1 * 512 = 512 bytes

Disk /dev/mmcblk0p1 doesn't contain a valid partition table
Disk /dev/mmcblk1: 58 GB, 62075699200 bytes, 121241600 sectors

这里就是 /dev/mmcblk1p1 就是sd卡的挂载点

2)把卡插回电脑查看

image

image

测试用例(略)

六、总结