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
74
75
76
77
78
79
80
81
|
// Builder: the initramfs, a gzipped newc cpio holding only tolino-stage1 as
// /init, the mountpoints it needs, and /dev/console. Written directly, no
// cpio(1): the format is simple, and only a hand-written archive can hold
// a device node built without root.
package main
import (
"compress/gzip"
"fmt"
"io"
"os"
"drv"
)
type entry struct {
name string
mode uint32 // including type bits
data []byte
rmaj, rmin uint32 // for device nodes
}
func main() {
initBin, err := os.ReadFile(drv.Env("stage1") + "/bin/tolino-stage1")
drv.Check(err, "reading tolino-stage1")
const dir, file, chr = 0o040000, 0o100000, 0o020000
entries := []entry{
{name: "data", mode: dir | 0o755},
{name: "dev", mode: dir | 0o755},
// The kernel opens /dev/console as /init's stdin, stdout and stderr
// before /init runs, so before devtmpfs is mounted. Without the node
// /init starts with no fds 0-2 and its log output goes nowhere.
{name: "dev/console", mode: chr | 0o600, rmaj: 5, rmin: 1},
{name: "newroot", mode: dir | 0o755},
{name: "proc", mode: dir | 0o755},
{name: "sys", mode: dir | 0o755},
{name: "init", mode: file | 0o755, data: initBin},
}
f, err := os.Create(drv.Out())
drv.Check(err, "creating output")
gz, err := gzip.NewWriterLevel(f, gzip.BestCompression)
drv.Check(err)
for i, e := range entries {
writeEntry(gz, uint32(i+1), e)
}
writeEntry(gz, 0, entry{name: "TRAILER!!!"})
drv.Check(gz.Close(), "gzip")
drv.Check(f.Close(), "close")
}
// writeEntry writes one newc ("070701") record: a 110-byte ASCII header,
// the NUL-terminated name, padding to 4 bytes, the data, padding to 4.
// Fields: ino mode uid gid nlink mtime filesize devmajor devminor
// rdevmajor rdevminor namesize check.
func writeEntry(w io.Writer, ino uint32, e entry) {
name := e.name + "\x00"
nlink := uint32(1)
if e.mode&0o170000 == 0o040000 {
nlink = 2
}
hdr := fmt.Sprintf("070701%08X%08X%08X%08X%08X%08X%08X%08X%08X%08X%08X%08X%08X",
ino, e.mode, 0, 0, nlink, 0, len(e.data), 0, 0, e.rmaj, e.rmin, len(name), 0)
write(w, []byte(hdr))
write(w, []byte(name))
pad(w, len(hdr)+len(name))
write(w, e.data)
pad(w, len(e.data))
}
func write(w io.Writer, b []byte) {
_, err := w.Write(b)
drv.Check(err, "writing cpio")
}
func pad(w io.Writer, n int) {
if r := n % 4; r != 0 {
write(w, make([]byte, 4-r))
}
}
|