Lorenz Brun | ae0d90d | 2019-09-05 17:53:56 +0200 | [diff] [blame] | 1 | // 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 | |
| 17 | package main |
| 18 | |
| 19 | import ( |
Leopold Schabel | 6549307 | 2019-11-06 13:40:44 +0000 | [diff] [blame] | 20 | "flag" |
Lorenz Brun | ae0d90d | 2019-09-05 17:53:56 +0200 | [diff] [blame] | 21 | "fmt" |
| 22 | "io/ioutil" |
| 23 | "os" |
Lorenz Brun | ae0d90d | 2019-09-05 17:53:56 +0200 | [diff] [blame] | 24 | |
Hendrik Hofstadt | 8efe51e | 2020-02-28 12:53:41 +0100 | [diff] [blame] | 25 | diskfs "github.com/diskfs/go-diskfs" |
Lorenz Brun | ae0d90d | 2019-09-05 17:53:56 +0200 | [diff] [blame] | 26 | "github.com/diskfs/go-diskfs/disk" |
| 27 | "github.com/diskfs/go-diskfs/filesystem" |
| 28 | "github.com/diskfs/go-diskfs/partition/gpt" |
Lorenz Brun | ae0d90d | 2019-09-05 17:53:56 +0200 | [diff] [blame] | 29 | ) |
| 30 | |
| 31 | var SmalltownDataPartition gpt.Type = gpt.Type("9eeec464-6885-414a-b278-4305c51f7966") |
| 32 | |
Leopold Schabel | 6549307 | 2019-11-06 13:40:44 +0000 | [diff] [blame] | 33 | var ( |
Lorenz Brun | aa6b734 | 2019-12-12 02:55:02 +0100 | [diff] [blame] | 34 | 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]") |
Lorenz Brun | 878f5f9 | 2020-05-12 16:15:39 +0200 | [diff] [blame] | 38 | dataPartitionSizeMiB = flag.Uint64("data-partition-size", 2048, "Override the data partition size (default 2048 MiB)") |
Leopold Schabel | 6549307 | 2019-11-06 13:40:44 +0000 | [diff] [blame] | 39 | ) |
| 40 | |
Lorenz Brun | ae0d90d | 2019-09-05 17:53:56 +0200 | [diff] [blame] | 41 | func mibToSectors(size uint64) uint64 { |
| 42 | return (size * 1024 * 1024) / 512 |
| 43 | } |
| 44 | |
Lorenz Brun | ae0d90d | 2019-09-05 17:53:56 +0200 | [diff] [blame] | 45 | func main() { |
Leopold Schabel | 6549307 | 2019-11-06 13:40:44 +0000 | [diff] [blame] | 46 | flag.Parse() |
| 47 | if *efiPayloadPath == "" || *outputPath == "" { |
| 48 | flag.PrintDefaults() |
Lorenz Brun | f95909d | 2019-09-11 19:48:26 +0200 | [diff] [blame] | 49 | os.Exit(2) |
| 50 | } |
Leopold Schabel | 6549307 | 2019-11-06 13:40:44 +0000 | [diff] [blame] | 51 | |
| 52 | _ = os.Remove(*outputPath) |
| 53 | diskImg, err := diskfs.Create(*outputPath, 3*1024*1024*1024, diskfs.Raw) |
Lorenz Brun | ae0d90d | 2019-09-05 17:53:56 +0200 | [diff] [blame] | 54 | if err != nil { |
| 55 | fmt.Printf("Failed to create disk: %v", err) |
| 56 | os.Exit(1) |
| 57 | } |
| 58 | |
| 59 | table := &gpt.Table{ |
| 60 | // This is appropriate at least for virtio disks. Might need to be adjusted for real ones. |
| 61 | LogicalSectorSize: 512, |
| 62 | PhysicalSectorSize: 512, |
| 63 | ProtectiveMBR: true, |
| 64 | Partitions: []*gpt.Partition{ |
| 65 | { |
| 66 | Type: gpt.EFISystemPartition, |
| 67 | Name: "ESP", |
| 68 | Start: mibToSectors(1), |
Lorenz Brun | 0bcaaee | 2019-11-06 12:42:39 +0100 | [diff] [blame] | 69 | End: mibToSectors(256) - 1, |
Lorenz Brun | ae0d90d | 2019-09-05 17:53:56 +0200 | [diff] [blame] | 70 | }, |
| 71 | { |
| 72 | Type: SmalltownDataPartition, |
| 73 | Name: "SIGNOS-DATA", |
Lorenz Brun | 0bcaaee | 2019-11-06 12:42:39 +0100 | [diff] [blame] | 74 | Start: mibToSectors(256), |
Lorenz Brun | 878f5f9 | 2020-05-12 16:15:39 +0200 | [diff] [blame] | 75 | End: mibToSectors(*dataPartitionSizeMiB+256) - 1, |
Lorenz Brun | ae0d90d | 2019-09-05 17:53:56 +0200 | [diff] [blame] | 76 | }, |
| 77 | }, |
| 78 | } |
| 79 | if err := diskImg.Partition(table); err != nil { |
| 80 | fmt.Printf("Failed to apply partition table: %v", err) |
| 81 | os.Exit(1) |
| 82 | } |
| 83 | |
| 84 | fs, err := diskImg.CreateFilesystem(disk.FilesystemSpec{Partition: 1, FSType: filesystem.TypeFat32, VolumeLabel: "ESP"}) |
| 85 | if err != nil { |
| 86 | fmt.Printf("Failed to create filesystem: %v", err) |
| 87 | os.Exit(1) |
| 88 | } |
| 89 | if err := fs.Mkdir("/EFI"); err != nil { |
| 90 | panic(err) |
| 91 | } |
| 92 | if err := fs.Mkdir("/EFI/BOOT"); err != nil { |
| 93 | panic(err) |
| 94 | } |
| 95 | if err := fs.Mkdir("/EFI/smalltown"); err != nil { |
| 96 | panic(err) |
| 97 | } |
| 98 | efiPayload, err := fs.OpenFile("/EFI/BOOT/BOOTX64.EFI", os.O_CREATE|os.O_RDWR) |
| 99 | if err != nil { |
| 100 | fmt.Printf("Failed to open EFI payload for writing: %v", err) |
| 101 | os.Exit(1) |
| 102 | } |
Leopold Schabel | 6549307 | 2019-11-06 13:40:44 +0000 | [diff] [blame] | 103 | efiPayloadSrc, err := os.Open(*efiPayloadPath) |
Lorenz Brun | ae0d90d | 2019-09-05 17:53:56 +0200 | [diff] [blame] | 104 | if err != nil { |
| 105 | fmt.Printf("Failed to open EFI payload for reading: %v", err) |
| 106 | os.Exit(1) |
| 107 | } |
| 108 | defer efiPayloadSrc.Close() |
| 109 | // If this is streamed (e.g. using io.Copy) it exposes a bug in diskfs, so do it in one go. |
| 110 | efiPayloadFull, err := ioutil.ReadAll(efiPayloadSrc) |
| 111 | if err != nil { |
| 112 | panic(err) |
| 113 | } |
| 114 | if _, err := efiPayload.Write(efiPayloadFull); err != nil { |
| 115 | fmt.Printf("Failed to write EFI payload: %v", err) |
| 116 | os.Exit(1) |
| 117 | } |
Lorenz Brun | 0bcaaee | 2019-11-06 12:42:39 +0100 | [diff] [blame] | 118 | initramfs, err := fs.OpenFile("/EFI/smalltown/initramfs.cpio.lz4", os.O_CREATE|os.O_RDWR) |
| 119 | if err != nil { |
| 120 | fmt.Printf("Failed to open initramfs for writing: %v", err) |
| 121 | os.Exit(1) |
| 122 | } |
| 123 | // If we have more than two arguments, the second one is the initramfs |
Leopold Schabel | 6549307 | 2019-11-06 13:40:44 +0000 | [diff] [blame] | 124 | if *initramfsPath != "" { |
| 125 | initramfsSrc, err := os.Open(*initramfsPath) |
Lorenz Brun | 0bcaaee | 2019-11-06 12:42:39 +0100 | [diff] [blame] | 126 | if err != nil { |
| 127 | fmt.Printf("Failed to open initramfs for reading: %v", err) |
| 128 | os.Exit(1) |
| 129 | } |
| 130 | initramfsFull, err := ioutil.ReadAll(initramfsSrc) |
| 131 | if err != nil { |
| 132 | fmt.Printf("Failed to read initramfs: %v", err) |
| 133 | os.Exit(1) |
| 134 | } |
| 135 | if _, err := initramfs.Write(initramfsFull); err != nil { |
| 136 | fmt.Printf("Failed to write initramfs: %v", err) |
| 137 | os.Exit(1) |
| 138 | } |
| 139 | } |
Lorenz Brun | aa6b734 | 2019-12-12 02:55:02 +0100 | [diff] [blame] | 140 | if *enrolmentCredentialsPath != "" { |
| 141 | enrolmentCredentials, err := fs.OpenFile("/EFI/smalltown/enrolment.pb", os.O_CREATE|os.O_RDWR) |
| 142 | enrolmentCredentialsSrc, err := os.Open(*enrolmentCredentialsPath) |
| 143 | if err != nil { |
| 144 | fmt.Printf("Failed to open enrolment credentials for reading: %v", err) |
| 145 | os.Exit(1) |
| 146 | } |
| 147 | enrolmentCredentialsFull, err := ioutil.ReadAll(enrolmentCredentialsSrc) |
| 148 | if err != nil { |
| 149 | fmt.Printf("Failed to read enrolment credentials: %v", err) |
| 150 | os.Exit(1) |
| 151 | } |
| 152 | if _, err := enrolmentCredentials.Write(enrolmentCredentialsFull); err != nil { |
| 153 | fmt.Printf("Failed to write enrolment credentials") |
| 154 | os.Exit(1) |
| 155 | } |
| 156 | } |
Lorenz Brun | ae0d90d | 2019-09-05 17:53:56 +0200 | [diff] [blame] | 157 | if err := diskImg.File.Close(); err != nil { |
| 158 | fmt.Printf("Failed to write image: %v", err) |
| 159 | os.Exit(1) |
| 160 | } |
| 161 | fmt.Println("Success! You can now boot smalltown.img") |
| 162 | } |