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 15package filesystem 16 17import ( 18 "fmt" 19 "strconv" 20 "strings" 21 22 "github.com/google/blueprint" 23 "github.com/google/blueprint/proptools" 24 25 "android/soong/android" 26) 27 28func init() { 29 android.RegisterModuleType("bootimg", bootimgFactory) 30} 31 32type bootimg struct { 33 android.ModuleBase 34 35 properties bootimgProperties 36 37 output android.OutputPath 38 installDir android.InstallPath 39} 40 41type bootimgProperties struct { 42 // Set the name of the output. Defaults to <module_name>.img. 43 Stem *string 44 45 // Path to the linux kernel prebuilt file 46 Kernel_prebuilt *string `android:"arch_variant,path"` 47 48 // Filesystem module that is used as ramdisk 49 Ramdisk_module *string 50 51 // Path to the device tree blob (DTB) prebuilt file to add to this boot image 52 Dtb_prebuilt *string `android:"arch_variant,path"` 53 54 // Header version number. Must be set to one of the version numbers that are currently 55 // supported. Refer to 56 // https://source.android.com/devices/bootloader/boot-image-header 57 Header_version *string 58 59 // Determines if this image is for the vendor_boot partition. Default is false. Refer to 60 // https://source.android.com/devices/bootloader/partitions/vendor-boot-partitions 61 Vendor_boot *bool 62 63 // Optional kernel commandline 64 Cmdline *string `android:"arch_variant"` 65 66 // File that contains bootconfig parameters. This can be set only when `vendor_boot` is true 67 // and `header_version` is greater than or equal to 4. 68 Bootconfig *string `android:"arch_variant,path"` 69 70 // When set to true, sign the image with avbtool. Default is false. 71 Use_avb *bool 72 73 // Name of the partition stored in vbmeta desc. Defaults to the name of this module. 74 Partition_name *string 75 76 // Path to the private key that avbtool will use to sign this filesystem image. 77 // TODO(jiyong): allow apex_key to be specified here 78 Avb_private_key *string `android:"path"` 79 80 // Hash and signing algorithm for avbtool. Default is SHA256_RSA4096. 81 Avb_algorithm *string 82} 83 84// bootimg is the image for the boot partition. It consists of header, kernel, ramdisk, and dtb. 85func bootimgFactory() android.Module { 86 module := &bootimg{} 87 module.AddProperties(&module.properties) 88 android.InitAndroidArchModule(module, android.DeviceSupported, android.MultilibFirst) 89 return module 90} 91 92type bootimgDep struct { 93 blueprint.BaseDependencyTag 94 kind string 95} 96 97var bootimgRamdiskDep = bootimgDep{kind: "ramdisk"} 98 99func (b *bootimg) DepsMutator(ctx android.BottomUpMutatorContext) { 100 ramdisk := proptools.String(b.properties.Ramdisk_module) 101 if ramdisk != "" { 102 ctx.AddDependency(ctx.Module(), bootimgRamdiskDep, ramdisk) 103 } 104} 105 106func (b *bootimg) installFileName() string { 107 return proptools.StringDefault(b.properties.Stem, b.BaseModuleName()+".img") 108} 109 110func (b *bootimg) partitionName() string { 111 return proptools.StringDefault(b.properties.Partition_name, b.BaseModuleName()) 112} 113 114func (b *bootimg) GenerateAndroidBuildActions(ctx android.ModuleContext) { 115 vendor := proptools.Bool(b.properties.Vendor_boot) 116 unsignedOutput := b.buildBootImage(ctx, vendor) 117 118 if proptools.Bool(b.properties.Use_avb) { 119 b.output = b.signImage(ctx, unsignedOutput) 120 } else { 121 b.output = unsignedOutput 122 } 123 124 b.installDir = android.PathForModuleInstall(ctx, "etc") 125 ctx.InstallFile(b.installDir, b.installFileName(), b.output) 126} 127 128func (b *bootimg) buildBootImage(ctx android.ModuleContext, vendor bool) android.OutputPath { 129 output := android.PathForModuleOut(ctx, "unsigned", b.installFileName()).OutputPath 130 131 builder := android.NewRuleBuilder(pctx, ctx) 132 cmd := builder.Command().BuiltTool("mkbootimg") 133 134 kernel := proptools.String(b.properties.Kernel_prebuilt) 135 if vendor && kernel != "" { 136 ctx.PropertyErrorf("kernel_prebuilt", "vendor_boot partition can't have kernel") 137 return output 138 } 139 if !vendor && kernel == "" { 140 ctx.PropertyErrorf("kernel_prebuilt", "boot partition must have kernel") 141 return output 142 } 143 if kernel != "" { 144 cmd.FlagWithInput("--kernel ", android.PathForModuleSrc(ctx, kernel)) 145 } 146 147 dtbName := proptools.String(b.properties.Dtb_prebuilt) 148 if dtbName == "" { 149 ctx.PropertyErrorf("dtb_prebuilt", "must be set") 150 return output 151 } 152 dtb := android.PathForModuleSrc(ctx, dtbName) 153 cmd.FlagWithInput("--dtb ", dtb) 154 155 cmdline := proptools.String(b.properties.Cmdline) 156 if cmdline != "" { 157 flag := "--cmdline " 158 if vendor { 159 flag = "--vendor_cmdline " 160 } 161 cmd.FlagWithArg(flag, proptools.ShellEscapeIncludingSpaces(cmdline)) 162 } 163 164 headerVersion := proptools.String(b.properties.Header_version) 165 if headerVersion == "" { 166 ctx.PropertyErrorf("header_version", "must be set") 167 return output 168 } 169 verNum, err := strconv.Atoi(headerVersion) 170 if err != nil { 171 ctx.PropertyErrorf("header_version", "%q is not a number", headerVersion) 172 return output 173 } 174 if verNum < 3 { 175 ctx.PropertyErrorf("header_version", "must be 3 or higher for vendor_boot") 176 return output 177 } 178 cmd.FlagWithArg("--header_version ", headerVersion) 179 180 ramdiskName := proptools.String(b.properties.Ramdisk_module) 181 if ramdiskName == "" { 182 ctx.PropertyErrorf("ramdisk_module", "must be set") 183 return output 184 } 185 ramdisk := ctx.GetDirectDepWithTag(ramdiskName, bootimgRamdiskDep) 186 if filesystem, ok := ramdisk.(*filesystem); ok { 187 flag := "--ramdisk " 188 if vendor { 189 flag = "--vendor_ramdisk " 190 } 191 cmd.FlagWithInput(flag, filesystem.OutputPath()) 192 } else { 193 ctx.PropertyErrorf("ramdisk", "%q is not android_filesystem module", ramdisk.Name()) 194 return output 195 } 196 197 bootconfig := proptools.String(b.properties.Bootconfig) 198 if bootconfig != "" { 199 if !vendor { 200 ctx.PropertyErrorf("bootconfig", "requires vendor_boot: true") 201 return output 202 } 203 if verNum < 4 { 204 ctx.PropertyErrorf("bootconfig", "requires header_version: 4 or later") 205 return output 206 } 207 cmd.FlagWithInput("--vendor_bootconfig ", android.PathForModuleSrc(ctx, bootconfig)) 208 } 209 210 flag := "--output " 211 if vendor { 212 flag = "--vendor_boot " 213 } 214 cmd.FlagWithOutput(flag, output) 215 216 builder.Build("build_bootimg", fmt.Sprintf("Creating %s", b.BaseModuleName())) 217 return output 218} 219 220func (b *bootimg) signImage(ctx android.ModuleContext, unsignedImage android.OutputPath) android.OutputPath { 221 propFile, toolDeps := b.buildPropFile(ctx) 222 223 output := android.PathForModuleOut(ctx, b.installFileName()).OutputPath 224 builder := android.NewRuleBuilder(pctx, ctx) 225 builder.Command().Text("cp").Input(unsignedImage).Output(output) 226 builder.Command().BuiltTool("verity_utils"). 227 Input(propFile). 228 Implicits(toolDeps). 229 Output(output) 230 231 builder.Build("sign_bootimg", fmt.Sprintf("Signing %s", b.BaseModuleName())) 232 return output 233} 234 235func (b *bootimg) buildPropFile(ctx android.ModuleContext) (propFile android.OutputPath, toolDeps android.Paths) { 236 var sb strings.Builder 237 var deps android.Paths 238 addStr := func(name string, value string) { 239 fmt.Fprintf(&sb, "%s=%s\n", name, value) 240 } 241 addPath := func(name string, path android.Path) { 242 addStr(name, path.String()) 243 deps = append(deps, path) 244 } 245 246 addStr("avb_hash_enable", "true") 247 addPath("avb_avbtool", ctx.Config().HostToolPath(ctx, "avbtool")) 248 algorithm := proptools.StringDefault(b.properties.Avb_algorithm, "SHA256_RSA4096") 249 addStr("avb_algorithm", algorithm) 250 key := android.PathForModuleSrc(ctx, proptools.String(b.properties.Avb_private_key)) 251 addPath("avb_key_path", key) 252 addStr("avb_add_hash_footer_args", "") // TODO(jiyong): add --rollback_index 253 partitionName := proptools.StringDefault(b.properties.Partition_name, b.Name()) 254 addStr("partition_name", partitionName) 255 256 propFile = android.PathForModuleOut(ctx, "prop").OutputPath 257 android.WriteFileRule(ctx, propFile, sb.String()) 258 return propFile, deps 259} 260 261var _ android.AndroidMkEntriesProvider = (*bootimg)(nil) 262 263// Implements android.AndroidMkEntriesProvider 264func (b *bootimg) AndroidMkEntries() []android.AndroidMkEntries { 265 return []android.AndroidMkEntries{android.AndroidMkEntries{ 266 Class: "ETC", 267 OutputFile: android.OptionalPathForPath(b.output), 268 ExtraEntries: []android.AndroidMkExtraEntriesFunc{ 269 func(ctx android.AndroidMkExtraEntriesContext, entries *android.AndroidMkEntries) { 270 entries.SetString("LOCAL_MODULE_PATH", b.installDir.ToMakePath().String()) 271 entries.SetString("LOCAL_INSTALLED_MODULE_STEM", b.installFileName()) 272 }, 273 }, 274 }} 275} 276 277var _ Filesystem = (*bootimg)(nil) 278 279func (b *bootimg) OutputPath() android.Path { 280 return b.output 281} 282 283func (b *bootimg) SignedOutputPath() android.Path { 284 if proptools.Bool(b.properties.Use_avb) { 285 return b.OutputPath() 286 } 287 return nil 288} 289 290var _ android.OutputFileProducer = (*bootimg)(nil) 291 292// Implements android.OutputFileProducer 293func (b *bootimg) OutputFiles(tag string) (android.Paths, error) { 294 if tag == "" { 295 return []android.Path{b.output}, nil 296 } 297 return nil, fmt.Errorf("unsupported module reference tag %q", tag) 298} 299