nlebouthillier
Posts: 1
Joined: Wed May 22, 2019 6:00 pm

Fast boot on Pi-Zero with C library and GPIO access

Wed May 22, 2019 6:12 pm

Hi,

I scoured the internet for an answer/how to and did not find what I needed.

Simply put I need to boot as fast as possible. All I need is the C libraries and access to the GPIO with the SPI turned on and maybe UART for debug.

I'm not experienced enough to re-compile the kernel so let's not go there if possible.

I don't need video, network, audio, ete, etc.

I basically need the pi to act as an embedded system. Boot time right now is closer to 40s... anything close to 10 would be great.

A basic list of services that I don't need or any other little tricks would be greatly appreciated or even just pointers on where to start.

I'm currently running the latest version of Raspbian Stretch Lite but I willing to install any os.

Thanks!

hippy
Posts: 5942
Joined: Fri Sep 09, 2011 10:34 pm
Location: UK

Re: Fast boot on Pi-Zero with C library and GPIO access

Wed May 22, 2019 10:54 pm

You haven't completely explained what you do need to support, exactly what needing "the C libraries " amounts to. You could look at BuildRoot and similar, bare metal, Ultibo, or even coding for the VC4 and running in the GPU.

User avatar
TimG
Posts: 291
Joined: Tue Apr 03, 2012 12:15 am
Location: Switzerland

Re: Fast boot on Pi-Zero with C library and GPIO access

Thu May 23, 2019 3:29 am

It's been a while since I worked on fast-booting, but on a Pi Zero it's possible to start running code within 2 - 2.5 seconds.

I used a systemd service file to launch my script as early as possible, like this:

Code: Select all

# example.service
# run as early as possible

[Unit]
Description=Example Service
DefaultDependencies=no

[Service]
ExecStart=/usr/local/bin/example

[Install]
#WantedBy=basic.target
WantedBy=sysinit.target
I disabled all non-essential services:

Code: Select all

systemctl set-default basic.target
systemctl disable console-setup kbd keyboard-setup networking
systemctl mask plymouth-read-write plymouth-start systemd-fsck-root
systemctl enable example
...and I think that's about it. No kernel recompilation. According to my notes the kernel took 1.3-1.5s to start, and my service was run 0.8-0.9s later. The complete boot took 5.6-6.2s, depending on what was attached to the USB port.

For testing I switched the on-board LED on at power-on with config.txt:

Code: Select all

boot_delay 0
enable_uart=0
dtparam=act_led_trigger=default-on
dtparam=act_led_activelow=on
and off again to indicate when my script was running:

Code: Select all

#!/bin/sh
#/usr/local/bin/example
# just an example service to do something
echo 0 >/sys/class/leds/led0/brightness # LED-off = service started
Note that undoing these changes is awkward, since you've disabled networking and keyboard. I wrote a second service script to restore normal operation:

Code: Select all

# restore.service
# run when boot is complete

[Unit]
Description=Restore normal boot
After=default.target

[Service]
ExecStart=/usr/local/bin/restore

[Install]
WantedBy=default.target

Code: Select all

#!/bin/sh
#/usr/local/bin/restore

# --- log boot performance
{
systemctl list-dependencies
systemd-analyze critical-chain --fuzz 30
systemd-analyze blame
systemd-analyze critical-chain example.service
systemd-analyze
} >/boot/bootlog.txt
systemd-analyze plot >/boot/bootlog.svg

systemctl set-default multi-user.target
systemctl enable console-setup kbd keyboard-setup networking
systemctl unmask plymouth-read-write plymouth-start systemd-fsck-root
systemctl disable restore

Return to “Advanced users”