How to Port BusyBox, the Swiss Army Knife of Linux Tools, to RT-Thread Smart?

How to Port BusyBox, the Swiss Army Knife of Linux Tools, to RT-Thread Smart?

RT-Thread Smart Series Serialization

Serial Number Content
1 When “Raspberry Pi” Meets RT-Thread Smart – Introduction to Application Programming
2 RT-Thread Smart and Raspberry Pi: wget & cURL Network Client
3 “How to Port BusyBox, the Swiss Army Knife of Linux Tools, to RT-Thread Smart?”
4 sdl graphic class application
5

dropbear and ssh server application

BusyBox is software that integrates hundreds of commonly used Linux/Unix commands and tools. BusyBox contains some simple tools, such as ls, cat, and echo, as well as larger, more complex tools like grep, find, mount, and telnet. Some people refer to BusyBox as the Swiss Army knife of Linux tools. Simply put, BusyBox is like a big toolbox that integrates and compresses many tools and commands of Linux and also includes the shell provided by the Android system.

This article will introduce how to port BusyBox to RT-Thread Smart.

Preparation Work

Taking busybox-1.32.0 as an example, download the source package:

1$ mkdir -p userapps/gnu-apps/busybox
2$ wget https://busybox.net/downloads/busybox-1.32.0.tar.bz2

Porting Script

Unlike the previous article, BusyBox comes with a Makefile, so some variables in the Makefile need to be replaced with cross-compilation environment variables, which will be achieved through a patch file.

First, create a simple build.sh script:

 1#!/bin/sh
 2
 3APP_NAME="busybox"
 4VERSION="1.32.0"
 5APP_DIR=
{APP_NAME}-${VERSION}
 6
 7# userapps is ROOTDIR
 8ROOTDIR=${PWD}/../..
 9
10# set env
11export RTT_EXEC_PATH=${ROOTDIR}/../tools/gnu_gcc/arm-linux-musleabi_for_x86_64-pc-linux-gnu/bin
12export PATH=$PATH:$RTT_EXEC_PATH:$RTT_EXEC_PATH/../arm-linux-musleabi/bin
13
14# get src
15tar xjf ${APP_DIR}.tar.bz2
16cd ${APP_DIR}
17
18# patch Makefile
19patch -Np1 -i ../makefile.patch
20
21# get default config
22cp ../def_config .config
23
24make V=1

makefile.patch file is as follows:

 1--- busybox-1.32.0/Makefile.orig        2020-12-24 12:51:40.752730739 +0800
 2+++ busybox-1.32.0/Makefile     2020-12-24 12:55:44.162093866 +0800
 3@@ -4,6 +4,21 @@
 4 EXTRAVERSION =
 5 NAME = Unnamed
 6
 7+
 8+CROSS_COMPILE= arm-linux-musleabi-
 9+
10+PWD := $(shell pwd)
11+# userapp dir
12+ROOTDIR := $(PWD)/../../..
13+RT_DIR=$(ROOTDIR)/sdk/rt-thread
14+INC_DIR=${ROOTDIR}/sdk/include
15+LIB_DIR=${ROOTDIR}/sdk/lib
16+
17+CFLAGS = -march=armv7-a -marm -msoft-float -D__RTTHREAD__ -Wall -O2 -n --static -I. -I$(RT_DIR)/include -I$(RT_DIR)/components/dfs -I$(RT_DIR)/components/drivers -I$(RT_DIR)/components/finsh -I$(RT_DIR)/components/net -I${INC_DIR}
18+
19+LDFLAGS = -L. -L${LIB_DIR} -T $(ROOTDIR)/linker_scripts/arm/cortex-a/link.lds -march=armv7-a -marm -msoft-float -L$(RT_DIR)/lib -Wl,--whole-archive -lrtthread -Wl,--no-whole-archive -n --static -Wl,--start-group -lrtthread -Wl,--end-group
20+
21+
22 # *DOCUMENTATION*
23 # To see a list of typical targets execute "make help"
24 # More info can be located in ./README    

It is best to configure the commands to be compiled through “make menuconfig” before compiling BusyBox. For convenience, a configured def_config file is provided, which will be used in the above build.sh script.

The file is quite long, please copy the following link to an external browser to open it, and you can directly download the file:

https://gitee.com/rtthread/rt-smart-notes/tree/master/examples/busybox

Compilation:

1userapps/gnu-apps/busybox$ ls
2build.sh  busybox-1.32.0.tar.bz2  def_config  makefile.patch
3userapps/gnu-apps/busybox$ sh build.sh

Using BusyBox

When using BusyBox on Linux, the general method is to create symbolic links, for example, linking ls to busybox through symbolic links, so that when ls is called, it will actually call busybox. However, for main(int argc, char** argv), argv[0] is ‘ls’.

Of course, busybox also supports directly calling busybox with commands appended afterwards, for example:

1msh /> busybox.elf ls -l

Since RT-Thread and RT-Thread Smart do not support symbolic links, only the latter form can be used.

BusyBox contains many commands, and you can configure the commands to be compiled by executing “make menuconfig”.

How to Port BusyBox, the Swiss Army Knife of Linux Tools, to RT-Thread Smart?

To view the currently available commands in busybox, you can use the `–help’ option:

How to Port BusyBox, the Swiss Army Knife of Linux Tools, to RT-Thread Smart?

To view the usage help for a specific command, such as ls :

How to Port BusyBox, the Swiss Army Knife of Linux Tools, to RT-Thread Smart?

More Commands

To ensure that some specific commands run normally, you need to enable devices like null, random, zero in the kernel configuration; and ensure that the /etc/resolv.com and /etc/hosts files exist.

1msh /> busybox.elf cat /etc/resolv.conf
2nameserver 114.114.114.114
3
4msh /> busybox.elf cat /etc/hosts
5127.0.0.1    localhost

busybox is a very compact toolset, and the commands that have been verified on rt-smart include (but are not limited to):

  • Archiving tools, such as tar/unzip/bzip2/bunzip2/bzcat/gzip/gunzip/zcat/xz/unxz/xzcat, etc.;

  • Core tools, such as cat/cp/dd/echo/head/ls/mv/mkdir/pwd/rm/tail/uname/wc, etc.;

  • Editing tools, such as vi/diff/cmp/awk/sed, etc.;

  • Network tools, such as ftpd/tcpsvd/ftpgt/ftpput/tftp/nslookup/telnet/httpd/wget, etc.

How to Port BusyBox, the Swiss Army Knife of Linux Tools, to RT-Thread Smart?

How to Port BusyBox, the Swiss Army Knife of Linux Tools, to RT-Thread Smart?

You can add WeChat 17775982065 as a friend, indicating: Company + Name, to be added to the RT-Thread official WeChat group!

How to Port BusyBox, the Swiss Army Knife of Linux Tools, to RT-Thread Smart?

👇 Click to read the original text to enter the RT-Thread official website

Leave a Comment

×