1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
// tolino-sys is the one program of the tolino's own userland: a multicall
// binary dispatching on argv[0] (or on its first argument), so that the init
// stages and every s6 service are small Go functions instead of shell
// scripts. See ../../tolino.7 and ../DESIGN.md. The initramfs /init is not
// here but in ../stage1, so that the boot image does not change with it.
//
//	rc-init    s6-linux-init's stage 2 (rc.init): start s6-rc.
//	rc-shutdown s6-linux-init's rc.shutdown: bring s6-rc down.
//	modules    s6-rc oneshot: load the vendor kernel modules.
//	mount-tmp  s6-rc oneshot: tmpfs on /tmp (the root is read-only), devpts,
//	           debugfs.
//	sysctl     s6-rc oneshot: kernel settings (sysrq off).
//	pstore     s6-rc oneshot: save the previous kernel's pstore records to
//	           /data/tolino/pstore.
//	usb-gadget s6-rc oneshot: configfs gadget with a functionfs for adb.
//	adbd       s6 longrun: a small adb daemon (shell, push/pull, reboot).
//	koreader   s6 longrun: set up and exec KOReader.
//	koreader-finish  its finish: power off or reboot when KOReader asked.
package main

import (
	"fmt"
	"os"
	"path/filepath"
	"strings"
)

type applet func(args []string) error

var applets = map[string]applet{
	"rc-init":         rcInit,
	"rc-shutdown":     rcShutdown,
	"runlevel":        runlevel,
	"modules":         loadModules,
	"display":         displaySetup,
	"koreader":        koreader,
	"koreader-finish": koreaderFinish,
	"mount-tmp":       mountTmp,
	"sysctl":          sysctl,
	"pstore":          pstoreSaveApplet,
	"usb-gadget":      usbGadget,
	"adbd":            adbd,
	"kexec":           kexecBoot,
	"kexec-jump":      kexecJump,
}

func main() {
	name := filepath.Base(os.Args[0])
	args := os.Args[1:]
	if _, ok := applets[name]; !ok {
		if len(args) == 0 {
			usage()
		}
		name, args = args[0], args[1:]
	}
	a, ok := applets[name]
	if !ok {
		usage()
	}
	if err := a(args); err != nil {
		fmt.Fprintf(os.Stderr, "tolino-sys %s: %v\n", name, err)
		os.Exit(1)
	}
}

func usage() {
	names := make([]string, 0, len(applets))
	for n := range applets {
		names = append(names, n)
	}
	fmt.Fprintf(os.Stderr, "usage: tolino-sys <%s> [args...]\n", strings.Join(names, "|"))
	os.Exit(100)
}