| Andrew Scull | ebd61e9 | 2022-06-09 15:53:36 +0000 | [diff] [blame] | 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 |  | 
|  | 15 | package filesystem | 
|  | 16 |  | 
|  | 17 | import ( | 
|  | 18 | "fmt" | 
|  | 19 | "strconv" | 
|  | 20 |  | 
|  | 21 | "github.com/google/blueprint/proptools" | 
|  | 22 |  | 
|  | 23 | "android/soong/android" | 
|  | 24 | ) | 
|  | 25 |  | 
| Andrew Scull | ebd61e9 | 2022-06-09 15:53:36 +0000 | [diff] [blame] | 26 | type avbAddHashFooter struct { | 
|  | 27 | android.ModuleBase | 
| Inseob Kim | 87230e6 | 2023-11-22 18:55:07 +0900 | [diff] [blame] | 28 | android.DefaultableModuleBase | 
| Andrew Scull | ebd61e9 | 2022-06-09 15:53:36 +0000 | [diff] [blame] | 29 |  | 
|  | 30 | properties avbAddHashFooterProperties | 
|  | 31 |  | 
|  | 32 | output     android.OutputPath | 
|  | 33 | installDir android.InstallPath | 
|  | 34 | } | 
|  | 35 |  | 
| Jiyong Park | bc48548 | 2022-11-15 22:31:49 +0900 | [diff] [blame] | 36 | type 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 |  | 
| Andrew Scull | ebd61e9 | 2022-06-09 15:53:36 +0000 | [diff] [blame] | 47 | type 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 | 
| Jiyong Park | bc48548 | 2022-11-15 22:31:49 +0900 | [diff] [blame] | 68 |  | 
|  | 69 | // List of properties to add to the footer | 
|  | 70 | Props []avbProp | 
| Alice Wang | 000e3a3 | 2023-01-03 16:11:20 +0000 | [diff] [blame] | 71 |  | 
| Shikha Panwar | e56c901 | 2023-09-11 13:51:47 +0000 | [diff] [blame] | 72 | // The index used to prevent rollback of the image on device. | 
|  | 73 | Rollback_index *int64 | 
|  | 74 |  | 
| Alice Wang | 000e3a3 | 2023-01-03 16:11:20 +0000 | [diff] [blame] | 75 | // Include descriptors from images | 
|  | 76 | Include_descriptors_from_images []string `android:"path,arch_variant"` | 
| Andrew Scull | ebd61e9 | 2022-06-09 15:53:36 +0000 | [diff] [blame] | 77 | } | 
|  | 78 |  | 
|  | 79 | // The AVB footer adds verification information to the image. | 
|  | 80 | func avbAddHashFooterFactory() android.Module { | 
|  | 81 | module := &avbAddHashFooter{} | 
|  | 82 | module.AddProperties(&module.properties) | 
|  | 83 | android.InitAndroidArchModule(module, android.DeviceSupported, android.MultilibFirst) | 
| Inseob Kim | 87230e6 | 2023-11-22 18:55:07 +0900 | [diff] [blame] | 84 | android.InitDefaultableModule(module) | 
| Andrew Scull | ebd61e9 | 2022-06-09 15:53:36 +0000 | [diff] [blame] | 85 | return module | 
|  | 86 | } | 
|  | 87 |  | 
|  | 88 | func (a *avbAddHashFooter) installFileName() string { | 
|  | 89 | return proptools.StringDefault(a.properties.Filename, a.BaseModuleName()+".img") | 
|  | 90 | } | 
|  | 91 |  | 
|  | 92 | func (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 | a.output = android.PathForModuleOut(ctx, a.installFileName()).OutputPath | 
|  | 101 | builder.Command().Text("cp").Input(input).Output(a.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 |  | 
| Alice Wang | 000e3a3 | 2023-01-03 16:11:20 +0000 | [diff] [blame] | 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 |  | 
| Jiyong Park | bc48548 | 2022-11-15 22:31:49 +0900 | [diff] [blame] | 132 | for _, prop := range a.properties.Props { | 
|  | 133 | addAvbProp(ctx, cmd, prop) | 
|  | 134 | } | 
|  | 135 |  | 
| Shikha Panwar | e56c901 | 2023-09-11 13:51:47 +0000 | [diff] [blame] | 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 | } | 
| David Pursell | 0ebba61 | 2024-01-19 12:49:22 -0800 | [diff] [blame] | 141 | cmd.Flag(fmt.Sprintf(" --rollback_index %d", rollbackIndex)) | 
| Shikha Panwar | e56c901 | 2023-09-11 13:51:47 +0000 | [diff] [blame] | 142 | } | 
|  | 143 |  | 
| Andrew Scull | ebd61e9 | 2022-06-09 15:53:36 +0000 | [diff] [blame] | 144 | cmd.FlagWithOutput("--image ", a.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(), a.output) | 
|  | 150 | } | 
|  | 151 |  | 
| Jiyong Park | bc48548 | 2022-11-15 22:31:49 +0900 | [diff] [blame] | 152 | func addAvbProp(ctx android.ModuleContext, cmd *android.RuleBuilderCommand, prop avbProp) { | 
|  | 153 | name := proptools.String(prop.Name) | 
|  | 154 | value := proptools.String(prop.Value) | 
|  | 155 | file := proptools.String(prop.File) | 
|  | 156 | if name == "" { | 
|  | 157 | ctx.PropertyErrorf("name", "can't be empty") | 
|  | 158 | return | 
|  | 159 | } | 
|  | 160 | if value == "" && file == "" { | 
|  | 161 | ctx.PropertyErrorf("value", "either value or file should be set") | 
|  | 162 | return | 
|  | 163 | } | 
|  | 164 | if value != "" && file != "" { | 
|  | 165 | ctx.PropertyErrorf("value", "value and file can't be set at the same time") | 
|  | 166 | return | 
|  | 167 | } | 
|  | 168 |  | 
|  | 169 | if value != "" { | 
|  | 170 | cmd.FlagWithArg("--prop ", proptools.ShellEscape(fmt.Sprintf("%s:%s", name, value))) | 
|  | 171 | } else { | 
|  | 172 | p := android.PathForModuleSrc(ctx, file) | 
| Jiyong Park | b0fda8f | 2022-12-05 17:09:55 +0900 | [diff] [blame] | 173 | cmd.Implicit(p) | 
| Jiyong Park | bc48548 | 2022-11-15 22:31:49 +0900 | [diff] [blame] | 174 | cmd.FlagWithArg("--prop_from_file ", proptools.ShellEscape(fmt.Sprintf("%s:%s", name, cmd.PathForInput(p)))) | 
|  | 175 | } | 
|  | 176 | } | 
|  | 177 |  | 
| Andrew Scull | ebd61e9 | 2022-06-09 15:53:36 +0000 | [diff] [blame] | 178 | var _ android.AndroidMkEntriesProvider = (*avbAddHashFooter)(nil) | 
|  | 179 |  | 
|  | 180 | // Implements android.AndroidMkEntriesProvider | 
|  | 181 | func (a *avbAddHashFooter) AndroidMkEntries() []android.AndroidMkEntries { | 
|  | 182 | return []android.AndroidMkEntries{android.AndroidMkEntries{ | 
|  | 183 | Class:      "ETC", | 
|  | 184 | OutputFile: android.OptionalPathForPath(a.output), | 
|  | 185 | ExtraEntries: []android.AndroidMkExtraEntriesFunc{ | 
|  | 186 | func(ctx android.AndroidMkExtraEntriesContext, entries *android.AndroidMkEntries) { | 
|  | 187 | entries.SetString("LOCAL_MODULE_PATH", a.installDir.String()) | 
|  | 188 | entries.SetString("LOCAL_INSTALLED_MODULE_STEM", a.installFileName()) | 
|  | 189 | }, | 
|  | 190 | }, | 
|  | 191 | }} | 
|  | 192 | } | 
|  | 193 |  | 
|  | 194 | var _ Filesystem = (*avbAddHashFooter)(nil) | 
|  | 195 |  | 
|  | 196 | func (a *avbAddHashFooter) OutputPath() android.Path { | 
|  | 197 | return a.output | 
|  | 198 | } | 
|  | 199 |  | 
|  | 200 | func (a *avbAddHashFooter) SignedOutputPath() android.Path { | 
|  | 201 | return a.OutputPath() // always signed | 
|  | 202 | } | 
|  | 203 |  | 
|  | 204 | // TODO(b/185115783): remove when not needed as input to a prebuilt_etc rule | 
|  | 205 | var _ android.SourceFileProducer = (*avbAddHashFooter)(nil) | 
|  | 206 |  | 
|  | 207 | // Implements android.SourceFileProducer | 
|  | 208 | func (a *avbAddHashFooter) Srcs() android.Paths { | 
|  | 209 | return append(android.Paths{}, a.output) | 
|  | 210 | } | 
| Inseob Kim | 87230e6 | 2023-11-22 18:55:07 +0900 | [diff] [blame] | 211 |  | 
|  | 212 | type avbAddHashFooterDefaults struct { | 
|  | 213 | android.ModuleBase | 
|  | 214 | android.DefaultsModuleBase | 
|  | 215 | } | 
|  | 216 |  | 
|  | 217 | // avb_add_hash_footer_defaults provides a set of properties that can be inherited by other | 
|  | 218 | // avb_add_hash_footer modules. A module can use the properties from an avb_add_hash_footer_defaults | 
|  | 219 | // using `defaults: ["<:default_module_name>"]`. Properties of both modules are erged (when | 
|  | 220 | // possible) by prepending the default module's values to the depending module's values. | 
|  | 221 | func avbAddHashFooterDefaultsFactory() android.Module { | 
|  | 222 | module := &avbAddHashFooterDefaults{} | 
|  | 223 | module.AddProperties(&avbAddHashFooterProperties{}) | 
|  | 224 | android.InitDefaultsModule(module) | 
|  | 225 | return module | 
|  | 226 | } |