IMX6ULL驱动开发Linux篇02——移植Rootfs

目录

编译rootfs

添加lib库

创建fstab文件

创建/etc/inittab文件

启动开发板检查一下

测试一下程序能不能跑

测试一下中文


编译rootfs

我们下面做的活就是移植rootfs,咱们打算是从NFS上走(网络文件系统)。笔者使用的rootfs是走的1.29.0的版本,之后要作点魔改。

首先就是取消我们的shell限制,好像这里的源码写的稍微有点问题------就是支持Unicode了但是shell打印字符的时候还是会查ASCII码来比较,如果是纯英文选手那没关系

修改libbb/printable_string.c下的printable_string函数

cpp 复制代码
const char* FAST_FUNC printable_string(uni_stat_t *stats, const char *str)
{
    char *dst;
    const char *s;
​
    s = str;
    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;
        s++;
    }
​
#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 (c < ' ')
                *d = '?';
            d++;
        }
        if (stats) {
            stats->byte_count = (d - dst);
            stats->unicode_count = (d - dst);
            stats->unicode_width = (d - dst);
        }
    }
#endif
    return auto_string(dst);
}

修改libbb/unicode.c

cpp 复制代码
static char* FAST_FUNC unicode_conv_to_printable2(uni_stat_t *stats, const char *src, unsigned width, int flags)
{
    char *dst;
    unsigned dst_len;
    unsigned uni_count;
    unsigned uni_width;
​
    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;
                }
                // 改了这个!!!把后面小于0x7F的部分删掉
                *d++ = (c >= ' ') ? c : '?';
                src++;
            }
            *d = '\0';
        } else {
            d = dst = xstrndup(src, width);
            while (*d) {
                unsigned char c = *d;
                if (c < ' ' || c >= 0x7f)
                    *d = '?';
                d++;
            }
        }
        if (stats) {
            stats->byte_count = (d - dst);
            stats->unicode_count = (d - dst);
            stats->unicode_width = (d - dst);
        }
        return dst;
    }
    ...

然后就是尝试编译了

复制代码
make  ARCH=arm CROSS_COMPILE=arm-linux-gnueabihf- defconfig

我们还需要图形化微操。第一个事情是检查------检查我们是动态库编译:

第二个事情是使用vi风格的命令行

第三个事情是采取比较全面的编译

以及确保我们的mdev是选中的

最后使能Unicode

复制代码
make  ARCH=arm CROSS_COMPILE=arm-linux-gnueabihf- -j12
make  ARCH=arm CROSS_COMPILE=arm-linux-gnueabihf- CONFIG_PREFIX=/home/charliechen/linux/nfs/rootfs install

install的时候,需要安装到我们挂载nfs的位置上

添加lib库

原本的rootfs非常稀少,需要我们自己添加lib,我们把编译器中使用到的库复制粘贴一下:

先进入自己家的arm-gcc下的arm-linux-gnueabihf/libc/lib,拷贝所有的库

复制代码
cp *so* *.a /home/charliechen/linux/nfs/rootfs/lib/ -d 

以及arm-linux-gnueabihf/lib

复制代码
cp *so* *.a /home/charliechen/linux/nfs/rootfs/lib/ -d 

我们在rootfs下的usr目录下新建一个lib目录,然后到arm-linux-gnueabihf/libc/usr/lib下拷贝

复制代码
cp *so* *.a /home/charliechen/linux/nfs/rootfs/usr/lib/ -d 
复制代码
charliechen@ubuntu:~/linux/nfs/rootfs$ du lib usr/lib/ -sh
59M lib
67M usr/lib/

OK,我们下一步就是设置一下板子的bootargs

复制代码
bootargs=console=ttymxc0,115200 root=/dev/nfs nfsroot=192.168.137.10:/home/charliechen/linux/nfs/rootfs,proto=tcp rw ip=192.168.137.4:192.168.137.10:192.168.137.1:255.255.255.0::eth0:off

Documentation/filesystems/nfs/nfsroot.txt下是有告诉你如何如何使用nfs挂载的,笔者这里整理一下:

复制代码
root=/dev/nfs nfsroot=[服务器IP]:[根文件系统路径],[NFS 选项] ip=[客户端IP]:[服务器IP]:[网关IP]:[子网掩码]:[主机名]:[设备]:[自动配置]:[DNS0]:[DNS1]
  • 服务器 IP 地址是存放根文件系统的主机 IP,例如 我的就是Ubuntu嘛!192.168.137.10,麻烦自己在ubuntu主机上ifconfig一下

  • 根文件系统的存放路径例,笔者的是 /home/charliechen/linux/nfs/rootfs。

  • NFS 选项一般不设置,但是这里我们强调使用proto=tcp rw,表达使用TCP协议来完成挂载

  • 客户端 IP 地址是开发板的 IP,需与服务器同一网段,且未被占用,我选择了 192.168.137.4,这个IP有没有效,很简单,跑到主机上ping一下,要求是没有人应答,说明没有人占用这个IP,这个IP就可以被分配到板子上

  • 网关地址,参考我咋搞uboot的,这里沿用 192.168.137.1。

  • 子网掩码例如 255.255.255.0。

  • 主机名一般不设置,可留空。

  • 设备名是网卡名称,例如 eth0、eth1。本例中使用 ENET2,对应 eth0。

  • 自动配置一般设为 off。

  • DNS 服务器 IP 一般不使用,可留空。

完事,启动一下

Oh,出现这个问题了,需要我们补充一下rcS

复制代码
#!/bin/sh
​
PATH=/sbin:/bin:/usr/sbin:/usr/bin:$PATH
LD_LIBRARY_PATH=$LD_LIBRARY_PATH:/lib:/usr/lib
export PATH LD_LIBRARY_PATH
mount -a
mkdir -p /dev/pts
mount -t devpts devpts /dev/pts
​
echo /sbin/mdev > /proc/sys/kernel/hotplug
mdev -s

我们很快还有了新的错误,一个个来

复制代码
mount: can't read '/etc/fstab': No such file or directory
/etc/init.d/rcS: line 10: can't create /proc/sys/kernel/hotplug: nonexistent directory
mdev: /sys/dev: No such file or directory
​

创建fstab文件

这个笔者跟之有交集是很早的事情了,当时沉迷Arch,就因为这个文件把系统搞崩过,这个文件实际上就是告知自动挂载的分区

创建/etc/fstab文件

复制代码
proc    /proc   proc    defaults    0   0
tmpfs   /tmp    tmpfs   defaults    0   0
sysfs   /sys    sysfs   defaults    0   0

创建/etc/inittab文件

init 程序会读取/etc/inittab这个文件做一些事情,我们也要这样做。

复制代码
::sysinit:/etc/init.d/rcS
console::askfirst:-/bin/sh
::restart:/sbin/init
::ctrlaltdel:/sbin/reboot
::shutdown:/bin/umount -a -r
::shutdown:/sbin/swapoff -a

启动开发板检查一下

复制代码
VFS: Mounted root (nfs filesystem) on device 0:15.
devtmpfs: mounted
Freeing unused kernel memory: 400K (8090e000 - 80972000)
​
Please press Enter to activate this console. 
/ # 
​

没有问题,我们下面做其他测试

测试一下程序能不能跑

复制代码
#include <stdio.h> 
   
int main(void) 
{ 
    while(1) { 
        printf("Hello!!! My Really Tiny Linux!!!\r\n"); 
        sleep(2); 
    } 
    return 0; 
}
复制代码
charliechen@ubuntu:~/IMX6ULL_Programming_Code/test/rfs$ arm-linux-gnueabihf-gcc hello.c -o hello
charliechen@ubuntu:~/IMX6ULL_Programming_Code/test/rfs$ ls
hello  hello.c
charliechen@ubuntu:~/IMX6ULL_Programming_Code/test/rfs$ file hello
hello: ELF 32-bit LSB executable, ARM, EABI5 version 1 (SYSV), dynamically linked, interpreter /lib/ld-linux-armhf.so.3, for GNU/Linux 2.6.31, BuildID[sha1]=93e70461bd94c25c5d7aa8a1663265cbc753f128, not stripped

测试一下

复制代码
/ # ls
bin      etc      lib      mnt      root     sys      usr
dev      hello    linuxrc  proc     sbin     tmp
/ # ./hello 
Hello!!! My Really Tiny Linux!!!
Hello!!! My Really Tiny Linux!!!
Hello!!! My Really Tiny Linux!!!
Hello!!! My Really Tiny Linux!!!
Hello!!! My Really Tiny Linux!!!
Hello!!! My Really Tiny Linux!!!
Hello!!! My Really Tiny Linux!!!
Hello!!! My Really Tiny Linux!!!
Hello!!! My Really Tiny Linux!!!
Hello!!! My Really Tiny Linux!!!
Hello!!! My Really Tiny Linux!!!
Hello!!! My Really Tiny Linux!!!
Hello!!! My Really Tiny Linux!!!
​

测试一下中文

复制代码
/ # mkdir 你好!
/ # ls
bin        etc        linuxrc    proc       sbin       tmp        你好!
dev        lib        mnt        root       sys        usr
/ 
/ # cd 你好!
/你好! # ls
/你好! # cd ..
/ # ls
bin        etc        linuxrc    proc       sbin       tmp        你好!
dev        lib        mnt        root       sys        usr

OK,基本完事了!

相关推荐
伊H1 小时前
C语言main的参数;argc与argv
linux·c语言·算法
husertuo1 小时前
Linux下的网络管理配置
linux·云计算
liqingdi4372 小时前
WSL+Ubuntu+miniconda环境配置
linux·windows·ubuntu
luoqice2 小时前
通过 Samba 服务实现 Ubuntu 和 Windows 之间互传文件
linux
Non importa3 小时前
【初阶数据结构】树——二叉树(上)
c语言·数据结构·学习·算法
Rousson3 小时前
硬件学习笔记--57 MCU相关资源介绍
笔记·单片机·mcu·学习
h汉堡4 小时前
C++入门基础
开发语言·c++·学习
小斌的Debug日记5 小时前
SpringBoot和微服务学习记录Day3
spring boot·学习·微服务
路有瑶台5 小时前
EXCEL学习
学习·excel
计算机视觉与OpenCV6 小时前
自动驾驶与机器人算法学习
学习·机器人·自动驾驶