前言:
本文涉及到制作嵌入式Linux文件系统,也涉及到编码种类
继续完善中,待更……
参考链接
UTF、GBK等编码以及嵌入式linux支持中文显示:
https://blog.youkuaiyun.com/weixin_43369409/article/details/90380100
说明1:
Windows的编码方式为GB2312
验证方式:cmd中输入“chcp”,返回值为“活动代码页:936”,如下图所示
说明2:
虚拟机装的Ubuntu的编码方式为UTF-8
验证方式:在终端中输入“locale”,返回的都是“UTF-8”的编码,如下图所示
说明3:
嵌入式Linux开发板的编码方式尽量设置为GB2312,当然也可以为UTF-8.
可以在内核配置中配置的。
说明4:
板子与Windows的编码方式要一致,才能完成在板子上的文件与Windows下的文件通用不乱码。
不要将虚拟机Ubuntu带有中文的文件直接拷贝到板子上,会导致乱码,只允许拷贝ASCII编码的文件(即 纯英文)。
1. Linux内核部分
在虚拟机中切换到内核目录,打开图形化配置界面(在Xshell中打开无法修改Default codepage/iocharset/NLS的编码值)
File systems ---> DOS/FAT/NT Filesystems --->
(437) Default codepage for FAT
(cp936) Default iocharset for FAT 修改
File systems ---> Native language support --->
(GB2312) Default NLS Option
Codepage 437 (United States, Canada) 选中
Simplified Chinese charset (CP936, GB2312) 选中(简体中文编码)
NLS ISO 8859-1 (Latin 1; Western European Languages) 选中
NLS UTF-8 选中(通用码,选中但不用)
2. 开发板中挂载的命令
后面在开发板上挂载外设存储设备的命令需要修改:
# GB2312格式挂载:
mount -t vfat -o iocharset=cp936 /dev/sda1 /mnt/udisk
# UTF-8格式挂载:
mount -t vfat -o iocharset=utf8 /dev/sda1 /mnt/udisk
3. BusyBox
3.1 BusyBox目录:
# 进入生成的目录
cd /XXX/busybox/
3.2 修改BusyBox源码,使显示突破ASCII码0x7F:
源码修改部分:
busybox/libbb/printable_string.c 的 FAST_FUNC printable_string 函数
busybox/libbb/unicode.c 的 FAST_FUNC unicode_conv_to_printable2 函数
笔者使用预处理指令(#if 0 & #else & #endif)屏蔽掉的代码为修改部分
// 修改1 printable_string.c 的 printable_string函数:
while (1) {
unsigned char c = *s;
if (c == '\0') {
/* 99+% of inputs do not need conversion */
if (stats) {
stats->byte_count = (s - str);
stats->unicode_count = (s - str);
stats->unicode_width = (s - str);
}
return str;
}
if (c < ' ')
break;
#if 0
if (c >= 0x7f)
break;
#endif
s++;
}
// 修改2 printable_string.c 的 printable_string函数:
#if ENABLE_UNICODE_SUPPORT
dst = unicode_conv_to_printable(stats, str);
#else
{
char *d = dst = xstrdup(str);
while (1) {
unsigned char c = *d;
if (c == '\0')
break;
#if 0
if (c < ' ' || c >= 0x7f)
*d = '?';
#else
if (c < ' ')
*d = '?';
#endif
d++;
}
if (stats) {
stats->byte_count = (d - dst);
stats->unicode_count = (d - dst);
stats->unicode_width = (d - dst);
}
}
#endif
// 修改3 unicode.c 的 unicode_conv_to_printable2函数:
if (unicode_status != UNICODE_ON) {
char *d;
if (flags & UNI_FLAG_PAD) {
d = dst = xmalloc(width + 1);
while ((int)--width >= 0) {
unsigned char c = *src;
if (c == '\0') {
do
*d++ = ' ';
while ((int)--width >= 0);
break;
}
#if 0
*d++ = (c >= ' ' && c < 0x7f) ? c : '?';
#else
*d++ = (c >= ' ') ? c : '?';
#endif
src++;
}
*d = '\0';
} else {
d = dst = xstrndup(src, width);
while (*d) {
unsigned char c = *d;
#if 0
if (c < ' ' || c >= 0x7f)
*d = '?';
#else
if (c < ' ')
*d = '?';
#endif
d++;
}
}
if (stats) {
stats->byte_count = (d - dst);
stats->unicode_count = (d - dst);
stats->unicode_width = (d - dst);
}
return dst;
}
3.3 修改顶级Makefile:
# 使用vim定位 CROSS_COMPILE 和 ARCH
CROSS_COMPILE ?= arm-linux-gnueabihf-
ARCH ?= arm
3.4 配置BusyBox:
# 运行命令:
make ARCH=arm menuconfig
Busybox Settings --->
Build BusyBox as a static binary (no shared libs) 勾上(把busybox编译成静态链接的可执行文件,不用依赖其他库运行.)
()Cross Compiler prefix(添加交叉编译器arm-linux-gnueabihf-)
(./_install) BusyBox installation prefix (自定义BusyBox的安装路径