blob: ddf8813bd80559e7261156fe2b77c1bbc583f705 [file] [log] [blame]
Lorenz Brunae0d90d2019-09-05 17:53:56 +02001// Copyright 2020 The Monogon Project Authors.
2//
3// SPDX-License-Identifier: Apache-2.0
4//
5// Licensed under the Apache License, Version 2.0 (the "License");
6// you may not use this file except in compliance with the License.
7// You may obtain a copy of the License at
8//
9// http://www.apache.org/licenses/LICENSE-2.0
10//
11// Unless required by applicable law or agreed to in writing, software
12// distributed under the License is distributed on an "AS IS" BASIS,
13// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14// See the License for the specific language governing permissions and
15// limitations under the License.
16
17package main
18
19import (
Leopold Schabel65493072019-11-06 13:40:44 +000020 "flag"
Lorenz Brunae0d90d2019-09-05 17:53:56 +020021 "fmt"
22 "io/ioutil"
23 "os"
Lorenz Brunae0d90d2019-09-05 17:53:56 +020024
Hendrik Hofstadt8efe51e2020-02-28 12:53:41 +010025 diskfs "github.com/diskfs/go-diskfs"
Lorenz Brunae0d90d2019-09-05 17:53:56 +020026 "github.com/diskfs/go-diskfs/disk"
27 "github.com/diskfs/go-diskfs/filesystem"
28 "github.com/diskfs/go-diskfs/partition/gpt"
Lorenz Brunae0d90d2019-09-05 17:53:56 +020029)
30
31var SmalltownDataPartition gpt.Type = gpt.Type("9eeec464-6885-414a-b278-4305c51f7966")
32
Leopold Schabel65493072019-11-06 13:40:44 +000033var (
Lorenz Brunaa6b7342019-12-12 02:55:02 +010034 efiPayloadPath = flag.String("efi", "", "UEFI payload")
35 outputPath = flag.String("out", "", "Output disk image")
36 initramfsPath = flag.String("initramfs", "", "External initramfs [optional]")
37 enrolmentCredentialsPath = flag.String("enrolment-credentials", "", "Enrolment credentials [optional]")
Leopold Schabel65493072019-11-06 13:40:44 +000038)
39
Lorenz Brunae0d90d2019-09-05 17:53:56 +020040func mibToSectors(size uint64) uint64 {
41 return (size * 1024 * 1024) / 512
42}
43
Lorenz Brunae0d90d2019-09-05 17:53:56 +020044func main() {
Leopold Schabel65493072019-11-06 13:40:44 +000045 flag.Parse()
46 if *efiPayloadPath == "" || *outputPath == "" {
47 flag.PrintDefaults()
Lorenz Brunf95909d2019-09-11 19:48:26 +020048 os.Exit(2)
49 }
Leopold Schabel65493072019-11-06 13:40:44 +000050
51 _ = os.Remove(*outputPath)
52 diskImg, err := diskfs.Create(*outputPath, 3*1024*1024*1024, diskfs.Raw)
Lorenz Brunae0d90d2019-09-05 17:53:56 +020053 if err != nil {
54 fmt.Printf("Failed to create disk: %v", err)
55 os.Exit(1)
56 }
57
58 table := &gpt.Table{
59 // This is appropriate at least for virtio disks. Might need to be adjusted for real ones.
60 LogicalSectorSize: 512,
61 PhysicalSectorSize: 512,
62 ProtectiveMBR: true,
63 Partitions: []*gpt.Partition{
64 {
65 Type: gpt.EFISystemPartition,
66 Name: "ESP",
67 Start: mibToSectors(1),
Lorenz Brun0bcaaee2019-11-06 12:42:39 +010068 End: mibToSectors(256) - 1,
Lorenz Brunae0d90d2019-09-05 17:53:56 +020069 },
70 {
71 Type: SmalltownDataPartition,
72 Name: "SIGNOS-DATA",
Lorenz Brun0bcaaee2019-11-06 12:42:39 +010073 Start: mibToSectors(256),
Lorenz Brunae0d90d2019-09-05 17:53:56 +020074 End: mibToSectors(2560) - 1,
75 },
76 },
77 }
78 if err := diskImg.Partition(table); err != nil {
79 fmt.Printf("Failed to apply partition table: %v", err)
80 os.Exit(1)
81 }
82
83 fs, err := diskImg.CreateFilesystem(disk.FilesystemSpec{Partition: 1, FSType: filesystem.TypeFat32, VolumeLabel: "ESP"})
84 if err != nil {
85 fmt.Printf("Failed to create filesystem: %v", err)
86 os.Exit(1)
87 }
88 if err := fs.Mkdir("/EFI"); err != nil {
89 panic(err)
90 }
91 if err := fs.Mkdir("/EFI/BOOT"); err != nil {
92 panic(err)
93 }
94 if err := fs.Mkdir("/EFI/smalltown"); err != nil {
95 panic(err)
96 }
97 efiPayload, err := fs.OpenFile("/EFI/BOOT/BOOTX64.EFI", os.O_CREATE|os.O_RDWR)
98 if err != nil {
99 fmt.Printf("Failed to open EFI payload for writing: %v", err)
100 os.Exit(1)
101 }
Leopold Schabel65493072019-11-06 13:40:44 +0000102 efiPayloadSrc, err := os.Open(*efiPayloadPath)
Lorenz Brunae0d90d2019-09-05 17:53:56 +0200103 if err != nil {
104 fmt.Printf("Failed to open EFI payload for reading: %v", err)
105 os.Exit(1)
106 }
107 defer efiPayloadSrc.Close()
108 // If this is streamed (e.g. using io.Copy) it exposes a bug in diskfs, so do it in one go.
109 efiPayloadFull, err := ioutil.ReadAll(efiPayloadSrc)
110 if err != nil {
111 panic(err)
112 }
113 if _, err := efiPayload.Write(efiPayloadFull); err != nil {
114 fmt.Printf("Failed to write EFI payload: %v", err)
115 os.Exit(1)
116 }
Lorenz Brun0bcaaee2019-11-06 12:42:39 +0100117 initramfs, err := fs.OpenFile("/EFI/smalltown/initramfs.cpio.lz4", os.O_CREATE|os.O_RDWR)
118 if err != nil {
119 fmt.Printf("Failed to open initramfs for writing: %v", err)
120 os.Exit(1)
121 }
122 // If we have more than two arguments, the second one is the initramfs
Leopold Schabel65493072019-11-06 13:40:44 +0000123 if *initramfsPath != "" {
124 initramfsSrc, err := os.Open(*initramfsPath)
Lorenz Brun0bcaaee2019-11-06 12:42:39 +0100125 if err != nil {
126 fmt.Printf("Failed to open initramfs for reading: %v", err)
127 os.Exit(1)
128 }
129 initramfsFull, err := ioutil.ReadAll(initramfsSrc)
130 if err != nil {
131 fmt.Printf("Failed to read initramfs: %v", err)
132 os.Exit(1)
133 }
134 if _, err := initramfs.Write(initramfsFull); err != nil {
135 fmt.Printf("Failed to write initramfs: %v", err)
136 os.Exit(1)
137 }
138 }
Lorenz Brunaa6b7342019-12-12 02:55:02 +0100139 if *enrolmentCredentialsPath != "" {
140 enrolmentCredentials, err := fs.OpenFile("/EFI/smalltown/enrolment.pb", os.O_CREATE|os.O_RDWR)
141 enrolmentCredentialsSrc, err := os.Open(*enrolmentCredentialsPath)
142 if err != nil {
143 fmt.Printf("Failed to open enrolment credentials for reading: %v", err)
144 os.Exit(1)
145 }
146 enrolmentCredentialsFull, err := ioutil.ReadAll(enrolmentCredentialsSrc)
147 if err != nil {
148 fmt.Printf("Failed to read enrolment credentials: %v", err)
149 os.Exit(1)
150 }
151 if _, err := enrolmentCredentials.Write(enrolmentCredentialsFull); err != nil {
152 fmt.Printf("Failed to write enrolment credentials")
153 os.Exit(1)
154 }
155 }
Lorenz Brunae0d90d2019-09-05 17:53:56 +0200156 if err := diskImg.File.Close(); err != nil {
157 fmt.Printf("Failed to write image: %v", err)
158 os.Exit(1)
159 }
160 fmt.Println("Success! You can now boot smalltown.img")
161}