1// Copyright (C) 2022 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 21 "github.com/google/blueprint/proptools" 22 23 "android/soong/android" 24) 25 26type avbAddHashFooter struct { 27 android.ModuleBase 28 android.DefaultableModuleBase 29 30 properties avbAddHashFooterProperties 31 32 output android.Path 33 installDir android.InstallPath 34} 35 36type avbProp struct { 37 // Name of a property 38 Name *string 39 40 // Value of a property. Can't be used together with `file`. 41 Value *string 42 43 // File from which the value of the prop is read from. Can't be used together with `value`. 44 File *string `android:"path,arch_variant"` 45} 46 47type avbAddHashFooterProperties struct { 48 // Source file of this image. Can reference a genrule type module with the ":module" syntax. 49 Src *string `android:"path,arch_variant"` 50 51 // Set the name of the output. Defaults to <module_name>.img. 52 Filename *string 53 54 // Name of the image partition. Defaults to the name of this module. 55 Partition_name *string 56 57 // Size of the partition. Defaults to dynamically calculating the size. 58 Partition_size *int64 59 60 // Path to the private key that avbtool will use to sign this image. 61 Private_key *string `android:"path"` 62 63 // Algorithm that avbtool will use to sign this image. Default is SHA256_RSA4096. 64 Algorithm *string 65 66 // The salt in hex. Required for reproducible builds. 67 Salt *string 68 69 // List of properties to add to the footer 70 Props []avbProp 71 72 // The index used to prevent rollback of the image on device. 73 Rollback_index *int64 74 75 // Include descriptors from images 76 Include_descriptors_from_images []string `android:"path,arch_variant"` 77} 78 79// The AVB footer adds verification information to the image. 80func avbAddHashFooterFactory() android.Module { 81 module := &avbAddHashFooter{} 82 module.AddProperties(&module.properties) 83 android.InitAndroidArchModule(module, android.DeviceSupported, android.MultilibFirst) 84 android.InitDefaultableModule(module) 85 return module 86} 87 88func (a *avbAddHashFooter) installFileName() string { 89 return proptools.StringDefault(a.properties.Filename, a.BaseModuleName()+".img") 90} 91 92func (a *avbAddHashFooter) GenerateAndroidBuildActions(ctx android.ModuleContext) { 93 builder := android.NewRuleBuilder(pctx, ctx) 94 95 if a.properties.Src == nil { 96 ctx.PropertyErrorf("src", "missing source file") 97 return 98 } 99 input := android.PathForModuleSrc(ctx, proptools.String(a.properties.Src)) 100 output := android.PathForModuleOut(ctx, a.installFileName()) 101 builder.Command().Text("cp").Input(input).Output(output) 102 103 cmd := builder.Command().BuiltTool("avbtool").Text("add_hash_footer") 104 105 partition_name := proptools.StringDefault(a.properties.Partition_name, a.BaseModuleName()) 106 cmd.FlagWithArg("--partition_name ", partition_name) 107 108 if a.properties.Partition_size == nil { 109 cmd.Flag("--dynamic_partition_size") 110 } else { 111 partition_size := proptools.Int(a.properties.Partition_size) 112 cmd.FlagWithArg("--partition_size ", strconv.Itoa(partition_size)) 113 } 114 115 key := android.PathForModuleSrc(ctx, proptools.String(a.properties.Private_key)) 116 cmd.FlagWithInput("--key ", key) 117 118 algorithm := proptools.StringDefault(a.properties.Algorithm, "SHA256_RSA4096") 119 cmd.FlagWithArg("--algorithm ", algorithm) 120 121 if a.properties.Salt == nil { 122 ctx.PropertyErrorf("salt", "missing salt value") 123 return 124 } 125 cmd.FlagWithArg("--salt ", proptools.String(a.properties.Salt)) 126 127 imagePaths := android.PathsForModuleSrc(ctx, a.properties.Include_descriptors_from_images) 128 for _, imagePath := range imagePaths { 129 cmd.FlagWithInput("--include_descriptors_from_image ", imagePath) 130 } 131 132 for _, prop := range a.properties.Props { 133 addAvbProp(ctx, cmd, prop) 134 } 135 136 if a.properties.Rollback_index != nil { 137 rollbackIndex := proptools.Int(a.properties.Rollback_index) 138 if rollbackIndex < 0 { 139 ctx.PropertyErrorf("rollback_index", "Rollback index must be non-negative") 140 } 141 cmd.Flag(fmt.Sprintf(" --rollback_index %d", rollbackIndex)) 142 } 143 144 cmd.FlagWithOutput("--image ", output) 145 146 builder.Build("avbAddHashFooter", fmt.Sprintf("avbAddHashFooter %s", ctx.ModuleName())) 147 148 a.installDir = android.PathForModuleInstall(ctx, "etc") 149 ctx.InstallFile(a.installDir, a.installFileName(), output) 150 a.output = output 151} 152 153func addAvbProp(ctx android.ModuleContext, cmd *android.RuleBuilderCommand, prop avbProp) { 154 name := proptools.String(prop.Name) 155 value := proptools.String(prop.Value) 156 file := proptools.String(prop.File) 157 if name == "" { 158 ctx.PropertyErrorf("name", "can't be empty") 159 return 160 } 161 if value == "" && file == "" { 162 ctx.PropertyErrorf("value", "either value or file should be set") 163 return 164 } 165 if value != "" && file != "" { 166 ctx.PropertyErrorf("value", "value and file can't be set at the same time") 167 return 168 } 169 170 if value != "" { 171 cmd.FlagWithArg("--prop ", proptools.ShellEscape(fmt.Sprintf("%s:%s", name, value))) 172 } else { 173 p := android.PathForModuleSrc(ctx, file) 174 cmd.Implicit(p) 175 cmd.FlagWithArg("--prop_from_file ", proptools.ShellEscape(fmt.Sprintf("%s:%s", name, cmd.PathForInput(p)))) 176 } 177} 178 179var _ android.AndroidMkEntriesProvider = (*avbAddHashFooter)(nil) 180 181// Implements android.AndroidMkEntriesProvider 182func (a *avbAddHashFooter) AndroidMkEntries() []android.AndroidMkEntries { 183 return []android.AndroidMkEntries{android.AndroidMkEntries{ 184 Class: "ETC", 185 OutputFile: android.OptionalPathForPath(a.output), 186 ExtraEntries: []android.AndroidMkExtraEntriesFunc{ 187 func(ctx android.AndroidMkExtraEntriesContext, entries *android.AndroidMkEntries) { 188 entries.SetString("LOCAL_MODULE_PATH", a.installDir.String()) 189 entries.SetString("LOCAL_INSTALLED_MODULE_STEM", a.installFileName()) 190 }, 191 }, 192 }} 193} 194 195var _ Filesystem = (*avbAddHashFooter)(nil) 196 197func (a *avbAddHashFooter) OutputPath() android.Path { 198 return a.output 199} 200 201func (a *avbAddHashFooter) SignedOutputPath() android.Path { 202 return a.OutputPath() // always signed 203} 204 205// TODO(b/185115783): remove when not needed as input to a prebuilt_etc rule 206var _ android.SourceFileProducer = (*avbAddHashFooter)(nil) 207 208// Implements android.SourceFileProducer 209func (a *avbAddHashFooter) Srcs() android.Paths { 210 return append(android.Paths{}, a.output) 211} 212 213type avbAddHashFooterDefaults struct { 214 android.ModuleBase 215 android.DefaultsModuleBase 216} 217 218// avb_add_hash_footer_defaults provides a set of properties that can be inherited by other 219// avb_add_hash_footer modules. A module can use the properties from an avb_add_hash_footer_defaults 220// using `defaults: ["<:default_module_name>"]`. Properties of both modules are erged (when 221// possible) by prepending the default module's values to the depending module's values. 222func avbAddHashFooterDefaultsFactory() android.Module { 223 module := &avbAddHashFooterDefaults{} 224 module.AddProperties(&avbAddHashFooterProperties{}) 225 android.InitDefaultsModule(module) 226 return module 227} 228