Startup Program with Buildroot

From www.chip-community.org
Jump to: navigation, search

This is a guide to create a startup program on the buildroot image using init.d as of 17 February 2017:

  • Make a source folder that includes the C code, a makefile, and an init script.
  • Make a new package in CHIP-buildroot
  • Let buildroot know about your package and enable it for the current build.
  • Flash the new image. Your program should start running by itself.


This guide requires CHIP-buildroot, CHIP-tools, and sunxi-tools to be installed. Refer to Flashing Buildroot Image from Ubuntu for setup.
If you have trouble with this guide, make a post in the forum here: https://bbs.nextthing.co/t/guide-on-making-startup-program-with-buildroot/15068


Make a source folder somewhere on your system. In this source folder create an init script named something like helloworld-init. It should look like this:

#!/bin/sh
case "$1" in
  start)
    /usr/bin/helloworld
    ;;
  stop)
    exit 1
    ;;
  *)
    exit 1
    ;;
esac

exit 0

We will tell buildroot to place this init script in /etc/init.d on the CHIP buildroot filesystem. For this to work, we also need to tell buildroot to place the helloworld executable in /usr/bin.

"helloworld" is a simple C program that prints "hello world" once per second after starting:

#include <stdio.h>
#include <unistd.h>

int main(int argc, int *argv[])
{
	while(1)
	{
		printf("Hello world\n");
		sleep(1);
	}
	return 0;
}

In this source directory create a Makefile like this:

OBJS=helloworld.o

all:	helloworld

helloworld:	$(OBJS)
	$(CC) $(LDFLAGS) -o helloworld $(OBJS)

$(OBJS): %.o:	%.c
	$(CC) $(CFLAGS) -c -o $@ $<

$(CC) will be replaced with the correct cross compiler for the chip.

Now make a new buildroot package for our helloworld program. Make a new folder under ~/CHIP-buildroot/package. Name this folder for your application, for instance, helloworld. Create a Config.in (Make sure it is spelled with a capital C at the beginning) in this folder. It should look like this:

config BR2_PACKAGE_HELLOWORLD
    bool "helloworld"
    help
      hello world package

Create a helloworld.mk file. It should look something like this:

HELLOWORLD_VERSION = 1.0.0
HELLOWORLD_SITE = $(TOPDIR)/../hello_source
HELLOWORLD_SITE_METHOD = local

define HELLOWORLD_BUILD_CMDS
	$(MAKE) $(TARGET_CONFIGURE_OPTS) -C $(@D)
endef

define HELLOWORLD_INSTALL_TARGET_CMDS
	$(INSTALL) -D -m 0755 $(@D)/helloworld $(TARGET_DIR)/usr/bin/helloworld

	$(INSTALL) -D -m 0755 $(@D)/helloworld-init $(TARGET_DIR)/etc/init.d/S90helloworld	
endef

$(eval $(generic-package))

HELLOWORLD_SITE is the location of the helloworld.c, init script, and makefile.
$(TOPDIR) is the top directory, in our case ~/CHIP-buildroot.
$(@D) is the build directory. This is where the result of cross compiling the helloworld.c program goes. The install command takes this cross compiled executable and places it in the chip file system. This is done again to place the hello-init script in the correct location under /etc/inti.d. It needs to be named S followed by a number for init.d to use it.

Now you need to let buildroot know about your new package: Under ~/CHIP-buildroot/package there should be a Config.in file. Edit this file, and under "menu "CHIP specific" add:

source "package/helloworld/Config.in"

Save this file and open a terminal at ~/CHIP-buildroot and type:

make menuconfig

(If you want to you can also create a new defconfig under ~/CHIP-buildroot/configs to include your helloworld program by copying chippro_defconfig and including your new package.)

In menuconfig, navigate to

Target packages > CHIP specific

And you should see helloworld. Enable it by pressing the spacebar. Then use the left arrowkeys to select<Save>. Save this as CHIP-buildroot/.config.

Now build this new image. This can take > 30 minutes.

make

After this is built you should see the rootfs.tar and some other boot files under ~/CHIP-buildroot/output/images. If you ever need to make a change to a package, instead of calling make clean and make again, you can go under CHIP-buildroot/build/ and find the package you want rebuild. Remove the folder of the package, and then call make again. This saves time because you do not need to rebuild everything.

Now use the CHIP-tools scripts:

cd ~/CHIP-tools
sudo ./chip-create-nand-images.sh ../CHIP-buildroot/output/build/uboot-nextthing_2016.01_next ../CHIP-buildroot/output/images/rootfs.tar helloworld-image

After this is done you will end up with a folder in CHIP-tools that should contain a rootfs.tar and a bunch of ubi, bin, and sparse files as well as some other things. However if you call ls -l in this folder you will see that everything is owned by root. Make this owned by your current user by calling:

sudo chown -R username:username helloworld-image

Your login username should be used in place of "username".

Now call:

sudo ./chip-flash-nand-images.sh helloworld-image

Hold down the FEL button on the chippro, plug it into USB, and then run the chip-flash-nand-images.sh command. Flashing can take a few minutes.