Jiyong Park | 1f7b93e | 2021-02-01 21:38:11 +0900 | [diff] [blame] | 1 | // Copyright (C) 2021 The Android Open Source Project |
| 2 | // |
| 3 | // Licensed under the Apache License, Version 2.0 (the "License"); |
| 4 | // you may not use this file except in compliance with the License. |
| 5 | // You may obtain a copy of the License at |
| 6 | // |
| 7 | // http://www.apache.org/licenses/LICENSE-2.0 |
| 8 | // |
| 9 | // Unless required by applicable law or agreed to in writing, software |
| 10 | // distributed under the License is distributed on an "AS IS" BASIS, |
| 11 | // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 12 | // See the License for the specific language governing permissions and |
| 13 | // limitations under the License. |
| 14 | |
| 15 | package filesystem |
| 16 | |
| 17 | import ( |
| 18 | "fmt" |
| 19 | "strconv" |
| 20 | |
| 21 | "github.com/google/blueprint" |
| 22 | "github.com/google/blueprint/proptools" |
| 23 | |
| 24 | "android/soong/android" |
| 25 | ) |
| 26 | |
| 27 | func init() { |
| 28 | android.RegisterModuleType("bootimg", bootimgFactory) |
| 29 | } |
| 30 | |
| 31 | type bootimg struct { |
| 32 | android.ModuleBase |
| 33 | |
| 34 | properties bootimgProperties |
| 35 | |
| 36 | output android.OutputPath |
| 37 | installDir android.InstallPath |
| 38 | } |
| 39 | |
| 40 | type bootimgProperties struct { |
Jiyong Park | 1f55dbd | 2021-02-15 17:57:35 +0900 | [diff] [blame] | 41 | // Set the name of the output. Defaults to <module_name>.img. |
| 42 | Stem *string |
| 43 | |
Jiyong Park | 1f7b93e | 2021-02-01 21:38:11 +0900 | [diff] [blame] | 44 | // Path to the linux kernel prebuilt file |
| 45 | Kernel_prebuilt *string `android:"arch_variant,path"` |
| 46 | |
| 47 | // Filesystem module that is used as ramdisk |
| 48 | Ramdisk_module *string |
| 49 | |
| 50 | // Path to the device tree blob (DTB) prebuilt file to add to this boot image |
| 51 | Dtb_prebuilt *string `android:"arch_variant,path"` |
| 52 | |
| 53 | // Header version number. Must be set to one of the version numbers that are currently |
| 54 | // supported. Refer to |
| 55 | // https://source.android.com/devices/bootloader/boot-image-header |
| 56 | Header_version *string |
| 57 | |
| 58 | // Determines if this image is for the vendor_boot partition. Default is false. Refer to |
| 59 | // https://source.android.com/devices/bootloader/partitions/vendor-boot-partitions |
| 60 | Vendor_boot *bool |
| 61 | |
| 62 | // Optional kernel commandline |
| 63 | Cmdline *string |
| 64 | |
| 65 | // When set to true, sign the image with avbtool. Default is false. |
| 66 | Use_avb *bool |
| 67 | |
| 68 | // Name of the partition stored in vbmeta desc. Defaults to the name of this module. |
| 69 | Partition_name *string |
| 70 | |
| 71 | // Path to the private key that avbtool will use to sign this filesystem image. |
| 72 | // TODO(jiyong): allow apex_key to be specified here |
| 73 | Avb_private_key *string `android:"path"` |
| 74 | |
| 75 | // Hash and signing algorithm for avbtool. Default is SHA256_RSA4096. |
| 76 | Avb_algorithm *string |
| 77 | } |
| 78 | |
| 79 | // bootimg is the image for the boot partition. It consists of header, kernel, ramdisk, and dtb. |
| 80 | func bootimgFactory() android.Module { |
| 81 | module := &bootimg{} |
| 82 | module.AddProperties(&module.properties) |
| 83 | android.InitAndroidArchModule(module, android.DeviceSupported, android.MultilibFirst) |
| 84 | return module |
| 85 | } |
| 86 | |
| 87 | type bootimgDep struct { |
| 88 | blueprint.BaseDependencyTag |
| 89 | kind string |
| 90 | } |
| 91 | |
| 92 | var bootimgRamdiskDep = bootimgDep{kind: "ramdisk"} |
| 93 | |
| 94 | func (b *bootimg) DepsMutator(ctx android.BottomUpMutatorContext) { |
| 95 | ramdisk := proptools.String(b.properties.Ramdisk_module) |
| 96 | if ramdisk != "" { |
| 97 | ctx.AddDependency(ctx.Module(), bootimgRamdiskDep, ramdisk) |
| 98 | } |
| 99 | } |
| 100 | |
| 101 | func (b *bootimg) installFileName() string { |
Jiyong Park | 1f55dbd | 2021-02-15 17:57:35 +0900 | [diff] [blame] | 102 | return proptools.StringDefault(b.properties.Stem, b.BaseModuleName()+".img") |
Jiyong Park | 1f7b93e | 2021-02-01 21:38:11 +0900 | [diff] [blame] | 103 | } |
| 104 | |
| 105 | func (b *bootimg) partitionName() string { |
| 106 | return proptools.StringDefault(b.properties.Partition_name, b.BaseModuleName()) |
| 107 | } |
| 108 | |
| 109 | func (b *bootimg) GenerateAndroidBuildActions(ctx android.ModuleContext) { |
Jiyong Park | 4bbd6cf | 2021-02-18 22:28:31 +0900 | [diff] [blame] | 110 | vendor := proptools.Bool(b.properties.Vendor_boot) |
| 111 | unsignedOutput := b.buildBootImage(ctx, vendor) |
Jiyong Park | 1f7b93e | 2021-02-01 21:38:11 +0900 | [diff] [blame] | 112 | |
| 113 | if proptools.Bool(b.properties.Use_avb) { |
| 114 | b.output = b.signImage(ctx, unsignedOutput) |
| 115 | } else { |
| 116 | b.output = unsignedOutput |
| 117 | } |
| 118 | |
| 119 | b.installDir = android.PathForModuleInstall(ctx, "etc") |
| 120 | ctx.InstallFile(b.installDir, b.installFileName(), b.output) |
| 121 | } |
| 122 | |
Jiyong Park | 4bbd6cf | 2021-02-18 22:28:31 +0900 | [diff] [blame] | 123 | func (b *bootimg) buildBootImage(ctx android.ModuleContext, vendor bool) android.OutputPath { |
Jiyong Park | 1f55dbd | 2021-02-15 17:57:35 +0900 | [diff] [blame] | 124 | output := android.PathForModuleOut(ctx, "unsigned", b.installFileName()).OutputPath |
| 125 | |
Jiyong Park | 1f7b93e | 2021-02-01 21:38:11 +0900 | [diff] [blame] | 126 | builder := android.NewRuleBuilder(pctx, ctx) |
| 127 | cmd := builder.Command().BuiltTool("mkbootimg") |
| 128 | |
Jiyong Park | 4bbd6cf | 2021-02-18 22:28:31 +0900 | [diff] [blame] | 129 | kernel := proptools.String(b.properties.Kernel_prebuilt) |
| 130 | if vendor && kernel != "" { |
Jiyong Park | 1f7b93e | 2021-02-01 21:38:11 +0900 | [diff] [blame] | 131 | ctx.PropertyErrorf("kernel_prebuilt", "vendor_boot partition can't have kernel") |
| 132 | return output |
| 133 | } |
Jiyong Park | 4bbd6cf | 2021-02-18 22:28:31 +0900 | [diff] [blame] | 134 | if !vendor && kernel == "" { |
| 135 | ctx.PropertyErrorf("kernel_prebuilt", "boot partition must have kernel") |
| 136 | return output |
| 137 | } |
| 138 | if kernel != "" { |
| 139 | cmd.FlagWithInput("--kernel ", android.PathForModuleSrc(ctx, kernel)) |
| 140 | } |
Jiyong Park | 1f7b93e | 2021-02-01 21:38:11 +0900 | [diff] [blame] | 141 | |
| 142 | dtbName := proptools.String(b.properties.Dtb_prebuilt) |
| 143 | if dtbName == "" { |
| 144 | ctx.PropertyErrorf("dtb_prebuilt", "must be set") |
| 145 | return output |
| 146 | } |
| 147 | dtb := android.PathForModuleSrc(ctx, dtbName) |
| 148 | cmd.FlagWithInput("--dtb ", dtb) |
| 149 | |
| 150 | cmdline := proptools.String(b.properties.Cmdline) |
| 151 | if cmdline != "" { |
Jiyong Park | 4bbd6cf | 2021-02-18 22:28:31 +0900 | [diff] [blame] | 152 | flag := "--cmdline " |
| 153 | if vendor { |
| 154 | flag = "--vendor_cmdline " |
| 155 | } |
| 156 | cmd.FlagWithArg(flag, "\""+proptools.ShellEscape(cmdline)+"\"") |
Jiyong Park | 1f7b93e | 2021-02-01 21:38:11 +0900 | [diff] [blame] | 157 | } |
| 158 | |
| 159 | headerVersion := proptools.String(b.properties.Header_version) |
| 160 | if headerVersion == "" { |
| 161 | ctx.PropertyErrorf("header_version", "must be set") |
| 162 | return output |
| 163 | } |
| 164 | verNum, err := strconv.Atoi(headerVersion) |
| 165 | if err != nil { |
| 166 | ctx.PropertyErrorf("header_version", "%q is not a number", headerVersion) |
| 167 | return output |
| 168 | } |
| 169 | if verNum < 3 { |
| 170 | ctx.PropertyErrorf("header_version", "must be 3 or higher for vendor_boot") |
| 171 | return output |
| 172 | } |
| 173 | cmd.FlagWithArg("--header_version ", headerVersion) |
| 174 | |
| 175 | ramdiskName := proptools.String(b.properties.Ramdisk_module) |
| 176 | if ramdiskName == "" { |
| 177 | ctx.PropertyErrorf("ramdisk_module", "must be set") |
| 178 | return output |
| 179 | } |
| 180 | ramdisk := ctx.GetDirectDepWithTag(ramdiskName, bootimgRamdiskDep) |
| 181 | if filesystem, ok := ramdisk.(*filesystem); ok { |
Jiyong Park | 4bbd6cf | 2021-02-18 22:28:31 +0900 | [diff] [blame] | 182 | flag := "--ramdisk " |
| 183 | if vendor { |
| 184 | flag = "--vendor_ramdisk " |
| 185 | } |
| 186 | cmd.FlagWithInput(flag, filesystem.OutputPath()) |
Jiyong Park | 1f7b93e | 2021-02-01 21:38:11 +0900 | [diff] [blame] | 187 | } else { |
| 188 | ctx.PropertyErrorf("ramdisk", "%q is not android_filesystem module", ramdisk.Name()) |
| 189 | return output |
| 190 | } |
| 191 | |
Jiyong Park | 4bbd6cf | 2021-02-18 22:28:31 +0900 | [diff] [blame] | 192 | flag := "--output " |
| 193 | if vendor { |
| 194 | flag = "--vendor_boot " |
| 195 | } |
| 196 | cmd.FlagWithOutput(flag, output) |
Jiyong Park | 1f7b93e | 2021-02-01 21:38:11 +0900 | [diff] [blame] | 197 | |
Jiyong Park | 4bbd6cf | 2021-02-18 22:28:31 +0900 | [diff] [blame] | 198 | builder.Build("build_bootimg", fmt.Sprintf("Creating %s", b.BaseModuleName())) |
Jiyong Park | 1f7b93e | 2021-02-01 21:38:11 +0900 | [diff] [blame] | 199 | return output |
| 200 | } |
| 201 | |
| 202 | func (b *bootimg) signImage(ctx android.ModuleContext, unsignedImage android.OutputPath) android.OutputPath { |
Jiyong Park | 1f55dbd | 2021-02-15 17:57:35 +0900 | [diff] [blame] | 203 | output := android.PathForModuleOut(ctx, b.installFileName()).OutputPath |
Jiyong Park | 1f7b93e | 2021-02-01 21:38:11 +0900 | [diff] [blame] | 204 | key := android.PathForModuleSrc(ctx, proptools.String(b.properties.Avb_private_key)) |
| 205 | |
| 206 | builder := android.NewRuleBuilder(pctx, ctx) |
Jiyong Park | 1f55dbd | 2021-02-15 17:57:35 +0900 | [diff] [blame] | 207 | builder.Command().Text("cp").Input(unsignedImage).Output(output) |
Jiyong Park | 1f7b93e | 2021-02-01 21:38:11 +0900 | [diff] [blame] | 208 | builder.Command(). |
| 209 | BuiltTool("avbtool"). |
| 210 | Flag("add_hash_footer"). |
| 211 | FlagWithArg("--partition_name ", b.partitionName()). |
| 212 | FlagWithInput("--key ", key). |
Jiyong Park | 1f55dbd | 2021-02-15 17:57:35 +0900 | [diff] [blame] | 213 | FlagWithOutput("--image ", output) |
Jiyong Park | 1f7b93e | 2021-02-01 21:38:11 +0900 | [diff] [blame] | 214 | |
| 215 | builder.Build("sign_bootimg", fmt.Sprintf("Signing %s", b.BaseModuleName())) |
Jiyong Park | 1f55dbd | 2021-02-15 17:57:35 +0900 | [diff] [blame] | 216 | return output |
Jiyong Park | 1f7b93e | 2021-02-01 21:38:11 +0900 | [diff] [blame] | 217 | } |
| 218 | |
| 219 | var _ android.AndroidMkEntriesProvider = (*bootimg)(nil) |
| 220 | |
| 221 | // Implements android.AndroidMkEntriesProvider |
| 222 | func (b *bootimg) AndroidMkEntries() []android.AndroidMkEntries { |
| 223 | return []android.AndroidMkEntries{android.AndroidMkEntries{ |
| 224 | Class: "ETC", |
| 225 | OutputFile: android.OptionalPathForPath(b.output), |
| 226 | ExtraEntries: []android.AndroidMkExtraEntriesFunc{ |
Colin Cross | aa25553 | 2020-07-03 13:18:24 -0700 | [diff] [blame] | 227 | func(ctx android.AndroidMkExtraEntriesContext, entries *android.AndroidMkEntries) { |
Jiyong Park | 1f7b93e | 2021-02-01 21:38:11 +0900 | [diff] [blame] | 228 | entries.SetString("LOCAL_MODULE_PATH", b.installDir.ToMakePath().String()) |
| 229 | entries.SetString("LOCAL_INSTALLED_MODULE_STEM", b.installFileName()) |
| 230 | }, |
| 231 | }, |
| 232 | }} |
| 233 | } |
| 234 | |
| 235 | var _ Filesystem = (*bootimg)(nil) |
| 236 | |
| 237 | func (b *bootimg) OutputPath() android.Path { |
| 238 | return b.output |
| 239 | } |