加入收藏 | 设为首页 | 会员中心 | 我要投稿 辽源站长网 (https://www.0437zz.com/)- 云专线、云连接、智能数据、边缘计算、数据安全!
当前位置: 首页 > 站长资讯 > 外闻 > 正文

如何在STM32上移植Linux?超详细的实操经验分享

发布时间:2018-10-18 17:40:09 所属栏目:外闻 来源:21ic电子网
导读:【51CTO技术沙龙】10月27日,让我们共同探索AI场景化应用实现之道 刚从硬件跳槽为嵌软时,没有任何一丝的准备。一入职,领导就交代了一项特难的任务在stm32上移植linux! 瞬间我就懵了,没办法硬着头皮上吧,先搜集资料,我之前跑的是ok6410的板子上运行的li

这四种给rootfs提供内容的方式都有一个共同点:在kernel启动时,一系列的文件被解压到rootfs,如果kernel能在其中找到可执行的文件“/init”,kernel就会运行它;这意味着,kernel不会再去理会“root=”是指向哪里的。

此外,一旦initramfs里面的init 进程运行起来,kernel就会认为启动已经完成。接下来,init将掌控整个宇宙!它拥有霹雳无敌的专门为它预留的Process ID #1,整个系统接下来的所有都将由它来创造!还有,它的地位将是不可剥夺的,嗯哼,PID 1 退出的话,系统会panic的。

接下来我会介绍其他一些,在rootfs中,init程序可以做的事。

Footnote 1: The kernel doesn’t allow rootfs to be unmounted for the same reason the same reason it won’t let the first process (PID 1, generally running init) be killed. The fact the lists of mounts and processes are never empty simplifies the kernel’s implementation.

Footnote 2: The cpio format is another way of combining files together, like tar and zip. It’s an older and simpler storage format that dates back to the original unix, and it’s the storage format used inside RPM packages. It’s not as widely used as tar or zip because the command line syntax of the cpio command is unnecessarily complicated (type “man 1 cpio” at a Linux or Cygwin command line if you have a strong stomach). Luckily, we don’t need to use this command.

Footnote 3: The kernel will always panic if PID 1 exits; this is unrelated to initramfs. All of the signals that might kill init are blocked, even “kill -9” which will reliably kill any other process. But init can still call the exit() syscall itself, and the kernel panics if this happens in PID 1. Avoiding it here is mostly a cosmetic issue: we don’t want the panic scrolling our “Hello World!” message off the top of the screen.

Footnote 4: Statically linking programs against glibc produces enormous, bloated binaries. Yes, this is expected to be over 400k for a hello world proram. You can try using the “strip” command on the resulting binary, but it won’t help much. This sort of bloat is why uClibc exists.

Footnote 5: Older 2.6 kernels had a bug where they would append to duplicate files rather than overwriting. Test your kernel version before depending on this behavior.

Footnote 6:User Mode Linux or QEMU can be very helpful testing out initramfs, but are beyond the scope of this article.

Footnote 7: Well, sort of. The default one is probably meant to be empty, but due to a small bug (gen_initramfs_list.sh spits out an example file when run with no arguments) the version in the 2.6.16 kernel actually contains a “/dev/console” node and a “/root” directory, which aren’t used for anything. It gzips down to about 135 bytes, and might as well actually be empty. On Intel you can run “readelf -S vmlinux” and look for section “.init.ramfs” to see the cpio.gz archive linked into a 2.6 kernel. Elf section names might vary a bit on other platforms.

显然stm32f103使用的是第三种方法,里面那些指令无非是设置文件权限、设置软连接等等操作,期间还学了一点bash shell,又有点收获,明白了道理之后就好办了,直接照猫画虎,我采用的是第二种方法:

1、先创建rootfs这个文件夹,再在这个文件夹下面分别创建bin、dev、etc、proc、sys等目录

2、编译busybox,把生成的bin文件复制到rootfs/bin下

3、新建linuxrc文件,设置权限chmod 777,然后在u-boot传给内核参数中一定要加上init=/linuxrc

4、在dev目录下,加设备节点,不然会没有输出信息哦!

· 1 mknod -m 666 console c 5 1

· 2 mknod -m 666 null c 1 3

5、在内核make menuconfig上CONFIG_INITRAMFS_SOURCE=“你刚刚构建的文件夹的绝对路径”

编译内核,initramfs直接和内核编译在一起,不用另外分出一个bin文件拷,这比较方便,启动,在串口调试助手中可看到相关显示信息:

如何在STM32上移植Linux?超详细的实操经验分享

最后说一下自己的感想,用initramfs根文件系统虽然方便实用,但是有弊端就是它只读不可写,这对开发很不利,领导说会加个spi flash再在里面挂载个根文件系统(spi flash能挂jfss2吗?),那是以后的事了以后再说,目前这种情况以我的技术水平只能做到这个份上了,加了根文件之后,stm32内部flash还有200多k的存储空间,应该可以添加些驱动和应用程序,那下面我的任务是编写简单的驱动与应用程序,好吧,又得继续学习,努力工作了。。。(本文摘自:http://blog.csdn.net/jccg89104)

(编辑:辽源站长网)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

推荐文章
    热点阅读