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" |
Jiyong Park | ac4076d | 2021-03-15 23:21:30 +0900 | [diff] [blame] | 20 | "strings" |
Jiyong Park | 1f7b93e | 2021-02-01 21:38:11 +0900 | [diff] [blame] | 21 | |
| 22 | "github.com/google/blueprint" |
| 23 | "github.com/google/blueprint/proptools" |
| 24 | |
| 25 | "android/soong/android" |
| 26 | ) |
| 27 | |
| 28 | func init() { |
Cole Faust | f2a6e8b | 2024-11-14 10:54:48 -0800 | [diff] [blame] | 29 | android.RegisterModuleType("bootimg", BootimgFactory) |
Jiyong Park | 1f7b93e | 2021-02-01 21:38:11 +0900 | [diff] [blame] | 30 | } |
| 31 | |
| 32 | type bootimg struct { |
| 33 | android.ModuleBase |
| 34 | |
Cole Faust | f2a6e8b | 2024-11-14 10:54:48 -0800 | [diff] [blame] | 35 | properties BootimgProperties |
Jiyong Park | 1f7b93e | 2021-02-01 21:38:11 +0900 | [diff] [blame] | 36 | |
Cole Faust | 4e9f592 | 2024-11-13 16:09:23 -0800 | [diff] [blame] | 37 | output android.Path |
Jiyong Park | 1f7b93e | 2021-02-01 21:38:11 +0900 | [diff] [blame] | 38 | installDir android.InstallPath |
Jihoon Kang | 96fdba9 | 2024-11-19 22:25:36 +0000 | [diff] [blame] | 39 | |
| 40 | bootImageType bootImageType |
Jiyong Park | 1f7b93e | 2021-02-01 21:38:11 +0900 | [diff] [blame] | 41 | } |
| 42 | |
Cole Faust | f2a6e8b | 2024-11-14 10:54:48 -0800 | [diff] [blame] | 43 | type BootimgProperties struct { |
Jiyong Park | 1f55dbd | 2021-02-15 17:57:35 +0900 | [diff] [blame] | 44 | // Set the name of the output. Defaults to <module_name>.img. |
| 45 | Stem *string |
| 46 | |
Jiyong Park | 1f7b93e | 2021-02-01 21:38:11 +0900 | [diff] [blame] | 47 | // Path to the linux kernel prebuilt file |
| 48 | Kernel_prebuilt *string `android:"arch_variant,path"` |
| 49 | |
| 50 | // Filesystem module that is used as ramdisk |
| 51 | Ramdisk_module *string |
| 52 | |
| 53 | // Path to the device tree blob (DTB) prebuilt file to add to this boot image |
| 54 | Dtb_prebuilt *string `android:"arch_variant,path"` |
| 55 | |
| 56 | // Header version number. Must be set to one of the version numbers that are currently |
| 57 | // supported. Refer to |
| 58 | // https://source.android.com/devices/bootloader/boot-image-header |
| 59 | Header_version *string |
| 60 | |
Jihoon Kang | 96fdba9 | 2024-11-19 22:25:36 +0000 | [diff] [blame] | 61 | // Determines the specific type of boot image this module is building. Can be boot, |
| 62 | // vendor_boot or init_boot. Defaults to boot. |
| 63 | // Refer to https://source.android.com/devices/bootloader/partitions/vendor-boot-partitions |
| 64 | // for vendor_boot. |
| 65 | // Refer to https://source.android.com/docs/core/architecture/partitions/generic-boot for |
| 66 | // init_boot. |
| 67 | Boot_image_type *string |
Jihoon Kang | 95eb1da | 2024-11-19 20:55:20 +0000 | [diff] [blame] | 68 | |
Jiyong Park | 16e77a9 | 2021-08-30 18:43:19 +0900 | [diff] [blame] | 69 | // Optional kernel commandline arguments |
| 70 | Cmdline []string `android:"arch_variant"` |
Jiyong Park | 1f7b93e | 2021-02-01 21:38:11 +0900 | [diff] [blame] | 71 | |
Jiyong Park | 81aea9a | 2021-03-05 18:58:29 +0900 | [diff] [blame] | 72 | // File that contains bootconfig parameters. This can be set only when `vendor_boot` is true |
| 73 | // and `header_version` is greater than or equal to 4. |
| 74 | Bootconfig *string `android:"arch_variant,path"` |
| 75 | |
Cole Faust | 26bdac5 | 2024-11-19 13:37:53 -0800 | [diff] [blame] | 76 | // The size of the partition on the device. It will be a build error if this built partition |
| 77 | // image exceeds this size. |
| 78 | Partition_size *int64 |
| 79 | |
Jiyong Park | 1f7b93e | 2021-02-01 21:38:11 +0900 | [diff] [blame] | 80 | // When set to true, sign the image with avbtool. Default is false. |
| 81 | Use_avb *bool |
| 82 | |
| 83 | // Name of the partition stored in vbmeta desc. Defaults to the name of this module. |
| 84 | Partition_name *string |
| 85 | |
| 86 | // Path to the private key that avbtool will use to sign this filesystem image. |
| 87 | // TODO(jiyong): allow apex_key to be specified here |
Cole Faust | 65cb40a | 2024-10-21 15:41:42 -0700 | [diff] [blame] | 88 | Avb_private_key *string `android:"path_device_first"` |
Jiyong Park | 1f7b93e | 2021-02-01 21:38:11 +0900 | [diff] [blame] | 89 | |
| 90 | // Hash and signing algorithm for avbtool. Default is SHA256_RSA4096. |
| 91 | Avb_algorithm *string |
Cole Faust | 336b3ba | 2024-11-19 16:34:29 -0800 | [diff] [blame^] | 92 | |
| 93 | // The security patch passed to as the com.android.build.<type>.security_patch avb property. |
| 94 | // Replacement for the make variables BOOT_SECURITY_PATCH / INIT_BOOT_SECURITY_PATCH. |
| 95 | Security_patch *string |
Jiyong Park | 1f7b93e | 2021-02-01 21:38:11 +0900 | [diff] [blame] | 96 | } |
| 97 | |
Jihoon Kang | 96fdba9 | 2024-11-19 22:25:36 +0000 | [diff] [blame] | 98 | type bootImageType int |
| 99 | |
| 100 | const ( |
| 101 | unsupported bootImageType = iota |
| 102 | boot |
| 103 | vendorBoot |
| 104 | initBoot |
| 105 | ) |
| 106 | |
| 107 | func toBootImageType(ctx android.ModuleContext, bootImageType string) bootImageType { |
| 108 | switch bootImageType { |
| 109 | case "boot": |
| 110 | return boot |
| 111 | case "vendor_boot": |
| 112 | return vendorBoot |
| 113 | case "init_boot": |
| 114 | return initBoot |
| 115 | default: |
| 116 | ctx.ModuleErrorf("Unknown boot_image_type %s. Must be one of \"boot\", \"vendor_boot\", or \"init_boot\"", bootImageType) |
| 117 | } |
| 118 | return unsupported |
| 119 | } |
| 120 | |
Cole Faust | 336b3ba | 2024-11-19 16:34:29 -0800 | [diff] [blame^] | 121 | func (b bootImageType) String() string { |
| 122 | switch b { |
| 123 | case boot: |
| 124 | return "boot" |
| 125 | case vendorBoot: |
| 126 | return "vendor_boot" |
| 127 | case initBoot: |
| 128 | return "init_boot" |
| 129 | default: |
| 130 | panic("unknown boot image type") |
| 131 | } |
| 132 | } |
| 133 | |
Jihoon Kang | 96fdba9 | 2024-11-19 22:25:36 +0000 | [diff] [blame] | 134 | func (b bootImageType) isBoot() bool { |
| 135 | return b == boot |
| 136 | } |
| 137 | |
| 138 | func (b bootImageType) isVendorBoot() bool { |
| 139 | return b == vendorBoot |
| 140 | } |
| 141 | |
| 142 | func (b bootImageType) isInitBoot() bool { |
| 143 | return b == initBoot |
| 144 | } |
| 145 | |
Jiyong Park | 1f7b93e | 2021-02-01 21:38:11 +0900 | [diff] [blame] | 146 | // bootimg is the image for the boot partition. It consists of header, kernel, ramdisk, and dtb. |
Cole Faust | f2a6e8b | 2024-11-14 10:54:48 -0800 | [diff] [blame] | 147 | func BootimgFactory() android.Module { |
Jiyong Park | 1f7b93e | 2021-02-01 21:38:11 +0900 | [diff] [blame] | 148 | module := &bootimg{} |
| 149 | module.AddProperties(&module.properties) |
| 150 | android.InitAndroidArchModule(module, android.DeviceSupported, android.MultilibFirst) |
| 151 | return module |
| 152 | } |
| 153 | |
| 154 | type bootimgDep struct { |
| 155 | blueprint.BaseDependencyTag |
| 156 | kind string |
| 157 | } |
| 158 | |
| 159 | var bootimgRamdiskDep = bootimgDep{kind: "ramdisk"} |
| 160 | |
| 161 | func (b *bootimg) DepsMutator(ctx android.BottomUpMutatorContext) { |
| 162 | ramdisk := proptools.String(b.properties.Ramdisk_module) |
| 163 | if ramdisk != "" { |
| 164 | ctx.AddDependency(ctx.Module(), bootimgRamdiskDep, ramdisk) |
| 165 | } |
| 166 | } |
| 167 | |
| 168 | func (b *bootimg) installFileName() string { |
Jiyong Park | 1f55dbd | 2021-02-15 17:57:35 +0900 | [diff] [blame] | 169 | return proptools.StringDefault(b.properties.Stem, b.BaseModuleName()+".img") |
Jiyong Park | 1f7b93e | 2021-02-01 21:38:11 +0900 | [diff] [blame] | 170 | } |
| 171 | |
| 172 | func (b *bootimg) partitionName() string { |
| 173 | return proptools.StringDefault(b.properties.Partition_name, b.BaseModuleName()) |
| 174 | } |
| 175 | |
| 176 | func (b *bootimg) GenerateAndroidBuildActions(ctx android.ModuleContext) { |
Jihoon Kang | 96fdba9 | 2024-11-19 22:25:36 +0000 | [diff] [blame] | 177 | b.bootImageType = toBootImageType(ctx, proptools.StringDefault(b.properties.Boot_image_type, "boot")) |
Cole Faust | 336b3ba | 2024-11-19 16:34:29 -0800 | [diff] [blame^] | 178 | if b.bootImageType == unsupported { |
| 179 | return |
| 180 | } |
| 181 | |
| 182 | kernelProp := proptools.String(b.properties.Kernel_prebuilt) |
| 183 | if b.bootImageType.isVendorBoot() && kernelProp != "" { |
| 184 | ctx.PropertyErrorf("kernel_prebuilt", "vendor_boot partition can't have kernel") |
| 185 | return |
| 186 | } |
| 187 | if b.bootImageType.isBoot() && kernelProp == "" { |
| 188 | ctx.PropertyErrorf("kernel_prebuilt", "boot partition must have kernel") |
| 189 | return |
| 190 | } |
| 191 | var kernel android.Path |
| 192 | if kernelProp != "" { |
| 193 | kernel = android.PathForModuleSrc(ctx, kernelProp) |
| 194 | } |
| 195 | |
| 196 | unsignedOutput := b.buildBootImage(ctx, kernel) |
Jiyong Park | 1f7b93e | 2021-02-01 21:38:11 +0900 | [diff] [blame] | 197 | |
Cole Faust | 4e9f592 | 2024-11-13 16:09:23 -0800 | [diff] [blame] | 198 | output := unsignedOutput |
Jiyong Park | 1f7b93e | 2021-02-01 21:38:11 +0900 | [diff] [blame] | 199 | if proptools.Bool(b.properties.Use_avb) { |
Cole Faust | 336b3ba | 2024-11-19 16:34:29 -0800 | [diff] [blame^] | 200 | // This bootimg module supports 2 modes of avb signing, it picks between them based on |
| 201 | // if the private key is specified or not. If there is a key, it does a signing process |
| 202 | // similar to how the regular partitions (system, product, vendor, etc) are signed. |
| 203 | // If the key is not provided, it will just add an avb footer to the image. The avb |
| 204 | // footer only signing is how the make-built init_boot, boot, and vendor_boot images are |
| 205 | // built. |
| 206 | if proptools.String(b.properties.Avb_private_key) != "" { |
| 207 | output = b.signImage(ctx, unsignedOutput) |
| 208 | } else { |
| 209 | output = b.addAvbFooter(ctx, unsignedOutput, kernel) |
| 210 | } |
Jiyong Park | 1f7b93e | 2021-02-01 21:38:11 +0900 | [diff] [blame] | 211 | } |
| 212 | |
| 213 | b.installDir = android.PathForModuleInstall(ctx, "etc") |
Cole Faust | 4e9f592 | 2024-11-13 16:09:23 -0800 | [diff] [blame] | 214 | ctx.InstallFile(b.installDir, b.installFileName(), output) |
mrziwang | 555d133 | 2024-06-07 11:15:33 -0700 | [diff] [blame] | 215 | |
Cole Faust | 4e9f592 | 2024-11-13 16:09:23 -0800 | [diff] [blame] | 216 | ctx.SetOutputFiles([]android.Path{output}, "") |
| 217 | b.output = output |
Jiyong Park | 1f7b93e | 2021-02-01 21:38:11 +0900 | [diff] [blame] | 218 | } |
| 219 | |
Cole Faust | 336b3ba | 2024-11-19 16:34:29 -0800 | [diff] [blame^] | 220 | func (b *bootimg) buildBootImage(ctx android.ModuleContext, kernel android.Path) android.Path { |
Cole Faust | 4e9f592 | 2024-11-13 16:09:23 -0800 | [diff] [blame] | 221 | output := android.PathForModuleOut(ctx, "unsigned", b.installFileName()) |
Jiyong Park | 1f55dbd | 2021-02-15 17:57:35 +0900 | [diff] [blame] | 222 | |
Jiyong Park | 1f7b93e | 2021-02-01 21:38:11 +0900 | [diff] [blame] | 223 | builder := android.NewRuleBuilder(pctx, ctx) |
| 224 | cmd := builder.Command().BuiltTool("mkbootimg") |
| 225 | |
Cole Faust | 336b3ba | 2024-11-19 16:34:29 -0800 | [diff] [blame^] | 226 | if kernel != nil { |
| 227 | cmd.FlagWithInput("--kernel ", kernel) |
Jiyong Park | 4bbd6cf | 2021-02-18 22:28:31 +0900 | [diff] [blame] | 228 | } |
Jiyong Park | 1f7b93e | 2021-02-01 21:38:11 +0900 | [diff] [blame] | 229 | |
Jihoon Kang | 95eb1da | 2024-11-19 20:55:20 +0000 | [diff] [blame] | 230 | // These arguments are passed for boot.img and init_boot.img generation |
Jihoon Kang | 96fdba9 | 2024-11-19 22:25:36 +0000 | [diff] [blame] | 231 | if b.bootImageType.isBoot() || b.bootImageType.isInitBoot() { |
Cole Faust | 1c9c335 | 2024-11-19 11:34:44 -0800 | [diff] [blame] | 232 | cmd.FlagWithArg("--os_version ", ctx.Config().PlatformVersionLastStable()) |
| 233 | cmd.FlagWithArg("--os_patch_level ", ctx.Config().PlatformSecurityPatch()) |
| 234 | } |
| 235 | |
Jiyong Park | 1f7b93e | 2021-02-01 21:38:11 +0900 | [diff] [blame] | 236 | dtbName := proptools.String(b.properties.Dtb_prebuilt) |
Jiyong Park | 393ebfc | 2022-01-06 14:28:53 +0900 | [diff] [blame] | 237 | if dtbName != "" { |
| 238 | dtb := android.PathForModuleSrc(ctx, dtbName) |
| 239 | cmd.FlagWithInput("--dtb ", dtb) |
Jiyong Park | 1f7b93e | 2021-02-01 21:38:11 +0900 | [diff] [blame] | 240 | } |
Jiyong Park | 1f7b93e | 2021-02-01 21:38:11 +0900 | [diff] [blame] | 241 | |
Jiyong Park | 16e77a9 | 2021-08-30 18:43:19 +0900 | [diff] [blame] | 242 | cmdline := strings.Join(b.properties.Cmdline, " ") |
Jiyong Park | 1f7b93e | 2021-02-01 21:38:11 +0900 | [diff] [blame] | 243 | if cmdline != "" { |
Jiyong Park | 4bbd6cf | 2021-02-18 22:28:31 +0900 | [diff] [blame] | 244 | flag := "--cmdline " |
Jihoon Kang | 96fdba9 | 2024-11-19 22:25:36 +0000 | [diff] [blame] | 245 | if b.bootImageType.isVendorBoot() { |
Jiyong Park | 4bbd6cf | 2021-02-18 22:28:31 +0900 | [diff] [blame] | 246 | flag = "--vendor_cmdline " |
| 247 | } |
Jooyung Han | 32cddd0 | 2021-03-08 20:54:16 +0900 | [diff] [blame] | 248 | cmd.FlagWithArg(flag, proptools.ShellEscapeIncludingSpaces(cmdline)) |
Jiyong Park | 1f7b93e | 2021-02-01 21:38:11 +0900 | [diff] [blame] | 249 | } |
| 250 | |
| 251 | headerVersion := proptools.String(b.properties.Header_version) |
| 252 | if headerVersion == "" { |
| 253 | ctx.PropertyErrorf("header_version", "must be set") |
| 254 | return output |
| 255 | } |
| 256 | verNum, err := strconv.Atoi(headerVersion) |
| 257 | if err != nil { |
| 258 | ctx.PropertyErrorf("header_version", "%q is not a number", headerVersion) |
| 259 | return output |
| 260 | } |
| 261 | if verNum < 3 { |
| 262 | ctx.PropertyErrorf("header_version", "must be 3 or higher for vendor_boot") |
| 263 | return output |
| 264 | } |
| 265 | cmd.FlagWithArg("--header_version ", headerVersion) |
| 266 | |
| 267 | ramdiskName := proptools.String(b.properties.Ramdisk_module) |
Jiyong Park | 393ebfc | 2022-01-06 14:28:53 +0900 | [diff] [blame] | 268 | if ramdiskName != "" { |
| 269 | ramdisk := ctx.GetDirectDepWithTag(ramdiskName, bootimgRamdiskDep) |
| 270 | if filesystem, ok := ramdisk.(*filesystem); ok { |
| 271 | flag := "--ramdisk " |
Jihoon Kang | 96fdba9 | 2024-11-19 22:25:36 +0000 | [diff] [blame] | 272 | if b.bootImageType.isVendorBoot() { |
Jiyong Park | 393ebfc | 2022-01-06 14:28:53 +0900 | [diff] [blame] | 273 | flag = "--vendor_ramdisk " |
| 274 | } |
| 275 | cmd.FlagWithInput(flag, filesystem.OutputPath()) |
| 276 | } else { |
| 277 | ctx.PropertyErrorf("ramdisk", "%q is not android_filesystem module", ramdisk.Name()) |
| 278 | return output |
Jiyong Park | 4bbd6cf | 2021-02-18 22:28:31 +0900 | [diff] [blame] | 279 | } |
Jiyong Park | 1f7b93e | 2021-02-01 21:38:11 +0900 | [diff] [blame] | 280 | } |
| 281 | |
Jiyong Park | 81aea9a | 2021-03-05 18:58:29 +0900 | [diff] [blame] | 282 | bootconfig := proptools.String(b.properties.Bootconfig) |
| 283 | if bootconfig != "" { |
Jihoon Kang | 96fdba9 | 2024-11-19 22:25:36 +0000 | [diff] [blame] | 284 | if !b.bootImageType.isVendorBoot() { |
Jiyong Park | 81aea9a | 2021-03-05 18:58:29 +0900 | [diff] [blame] | 285 | ctx.PropertyErrorf("bootconfig", "requires vendor_boot: true") |
| 286 | return output |
| 287 | } |
| 288 | if verNum < 4 { |
| 289 | ctx.PropertyErrorf("bootconfig", "requires header_version: 4 or later") |
| 290 | return output |
| 291 | } |
| 292 | cmd.FlagWithInput("--vendor_bootconfig ", android.PathForModuleSrc(ctx, bootconfig)) |
| 293 | } |
| 294 | |
Jihoon Kang | 95eb1da | 2024-11-19 20:55:20 +0000 | [diff] [blame] | 295 | // Output flag for boot.img and init_boot.img |
Jiyong Park | 4bbd6cf | 2021-02-18 22:28:31 +0900 | [diff] [blame] | 296 | flag := "--output " |
Jihoon Kang | 96fdba9 | 2024-11-19 22:25:36 +0000 | [diff] [blame] | 297 | if b.bootImageType.isVendorBoot() { |
Jiyong Park | 4bbd6cf | 2021-02-18 22:28:31 +0900 | [diff] [blame] | 298 | flag = "--vendor_boot " |
| 299 | } |
| 300 | cmd.FlagWithOutput(flag, output) |
Jiyong Park | 1f7b93e | 2021-02-01 21:38:11 +0900 | [diff] [blame] | 301 | |
Cole Faust | 26bdac5 | 2024-11-19 13:37:53 -0800 | [diff] [blame] | 302 | if b.properties.Partition_size != nil { |
| 303 | assertMaxImageSize(builder, output, *b.properties.Partition_size, proptools.Bool(b.properties.Use_avb)) |
| 304 | } |
| 305 | |
Jiyong Park | 4bbd6cf | 2021-02-18 22:28:31 +0900 | [diff] [blame] | 306 | builder.Build("build_bootimg", fmt.Sprintf("Creating %s", b.BaseModuleName())) |
Jiyong Park | 1f7b93e | 2021-02-01 21:38:11 +0900 | [diff] [blame] | 307 | return output |
| 308 | } |
| 309 | |
Cole Faust | 336b3ba | 2024-11-19 16:34:29 -0800 | [diff] [blame^] | 310 | func (b *bootimg) addAvbFooter(ctx android.ModuleContext, unsignedImage android.Path, kernel android.Path) android.Path { |
| 311 | output := android.PathForModuleOut(ctx, b.installFileName()) |
| 312 | builder := android.NewRuleBuilder(pctx, ctx) |
| 313 | builder.Command().Text("cp").Input(unsignedImage).Output(output) |
| 314 | cmd := builder.Command().BuiltTool("avbtool"). |
| 315 | Text("add_hash_footer"). |
| 316 | FlagWithInput("--image ", output) |
| 317 | |
| 318 | if b.properties.Partition_size != nil { |
| 319 | cmd.FlagWithArg("--partition_size ", strconv.FormatInt(*b.properties.Partition_size, 10)) |
| 320 | } else { |
| 321 | cmd.Flag("--dynamic_partition_size") |
| 322 | } |
| 323 | |
| 324 | if kernel != nil { |
| 325 | cmd.Textf(`--salt $(sha256sum "%s" | cut -d " " -f 1)`, kernel.String()) |
| 326 | cmd.Implicit(kernel) |
| 327 | } |
| 328 | |
| 329 | cmd.FlagWithArg("--partition_name ", b.bootImageType.String()) |
| 330 | |
| 331 | if !b.bootImageType.isVendorBoot() { |
| 332 | cmd.FlagWithArg("--prop ", proptools.NinjaAndShellEscape(fmt.Sprintf( |
| 333 | "com.android.build.%s.os_version:%s", b.bootImageType.String(), ctx.Config().PlatformVersionLastStable()))) |
| 334 | } |
| 335 | |
| 336 | fingerprintFile := ctx.Config().BuildFingerprintFile(ctx) |
| 337 | cmd.FlagWithArg("--prop ", fmt.Sprintf("com.android.build.%s.fingerprint:%s", b.bootImageType.String(), fingerprintFile.String())) |
| 338 | cmd.OrderOnly(fingerprintFile) |
| 339 | |
| 340 | if b.properties.Security_patch != nil { |
| 341 | cmd.FlagWithArg("--prop ", proptools.NinjaAndShellEscape(fmt.Sprintf( |
| 342 | "com.android.build.%s.security_patch:%s", b.bootImageType.String(), *b.properties.Security_patch))) |
| 343 | } |
| 344 | |
| 345 | builder.Build("add_avb_footer", fmt.Sprintf("Adding avb footer to %s", b.BaseModuleName())) |
| 346 | return output |
| 347 | } |
| 348 | |
Cole Faust | 4e9f592 | 2024-11-13 16:09:23 -0800 | [diff] [blame] | 349 | func (b *bootimg) signImage(ctx android.ModuleContext, unsignedImage android.Path) android.Path { |
Jiyong Park | ac4076d | 2021-03-15 23:21:30 +0900 | [diff] [blame] | 350 | propFile, toolDeps := b.buildPropFile(ctx) |
Jiyong Park | 1f7b93e | 2021-02-01 21:38:11 +0900 | [diff] [blame] | 351 | |
Cole Faust | 4e9f592 | 2024-11-13 16:09:23 -0800 | [diff] [blame] | 352 | output := android.PathForModuleOut(ctx, b.installFileName()) |
Jiyong Park | 1f7b93e | 2021-02-01 21:38:11 +0900 | [diff] [blame] | 353 | builder := android.NewRuleBuilder(pctx, ctx) |
Jiyong Park | 1f55dbd | 2021-02-15 17:57:35 +0900 | [diff] [blame] | 354 | builder.Command().Text("cp").Input(unsignedImage).Output(output) |
Jiyong Park | ac4076d | 2021-03-15 23:21:30 +0900 | [diff] [blame] | 355 | builder.Command().BuiltTool("verity_utils"). |
| 356 | Input(propFile). |
| 357 | Implicits(toolDeps). |
| 358 | Output(output) |
Jiyong Park | 1f7b93e | 2021-02-01 21:38:11 +0900 | [diff] [blame] | 359 | |
| 360 | builder.Build("sign_bootimg", fmt.Sprintf("Signing %s", b.BaseModuleName())) |
Jiyong Park | 1f55dbd | 2021-02-15 17:57:35 +0900 | [diff] [blame] | 361 | return output |
Jiyong Park | 1f7b93e | 2021-02-01 21:38:11 +0900 | [diff] [blame] | 362 | } |
| 363 | |
Jooyung Han | 65f402b | 2022-04-21 14:24:04 +0900 | [diff] [blame] | 364 | // Calculates avb_salt from some input for deterministic output. |
| 365 | func (b *bootimg) salt() string { |
| 366 | var input []string |
| 367 | input = append(input, b.properties.Cmdline...) |
| 368 | input = append(input, proptools.StringDefault(b.properties.Partition_name, b.Name())) |
| 369 | input = append(input, proptools.String(b.properties.Header_version)) |
| 370 | return sha1sum(input) |
| 371 | } |
| 372 | |
Cole Faust | 4e9f592 | 2024-11-13 16:09:23 -0800 | [diff] [blame] | 373 | func (b *bootimg) buildPropFile(ctx android.ModuleContext) (android.Path, android.Paths) { |
Jiyong Park | ac4076d | 2021-03-15 23:21:30 +0900 | [diff] [blame] | 374 | var sb strings.Builder |
| 375 | var deps android.Paths |
| 376 | addStr := func(name string, value string) { |
| 377 | fmt.Fprintf(&sb, "%s=%s\n", name, value) |
| 378 | } |
| 379 | addPath := func(name string, path android.Path) { |
| 380 | addStr(name, path.String()) |
| 381 | deps = append(deps, path) |
| 382 | } |
| 383 | |
| 384 | addStr("avb_hash_enable", "true") |
| 385 | addPath("avb_avbtool", ctx.Config().HostToolPath(ctx, "avbtool")) |
| 386 | algorithm := proptools.StringDefault(b.properties.Avb_algorithm, "SHA256_RSA4096") |
| 387 | addStr("avb_algorithm", algorithm) |
| 388 | key := android.PathForModuleSrc(ctx, proptools.String(b.properties.Avb_private_key)) |
| 389 | addPath("avb_key_path", key) |
| 390 | addStr("avb_add_hash_footer_args", "") // TODO(jiyong): add --rollback_index |
| 391 | partitionName := proptools.StringDefault(b.properties.Partition_name, b.Name()) |
| 392 | addStr("partition_name", partitionName) |
Jooyung Han | 65f402b | 2022-04-21 14:24:04 +0900 | [diff] [blame] | 393 | addStr("avb_salt", b.salt()) |
Jiyong Park | ac4076d | 2021-03-15 23:21:30 +0900 | [diff] [blame] | 394 | |
Cole Faust | 4e9f592 | 2024-11-13 16:09:23 -0800 | [diff] [blame] | 395 | propFile := android.PathForModuleOut(ctx, "prop") |
Jiyong Park | ac4076d | 2021-03-15 23:21:30 +0900 | [diff] [blame] | 396 | android.WriteFileRule(ctx, propFile, sb.String()) |
| 397 | return propFile, deps |
| 398 | } |
| 399 | |
Jiyong Park | 1f7b93e | 2021-02-01 21:38:11 +0900 | [diff] [blame] | 400 | var _ android.AndroidMkEntriesProvider = (*bootimg)(nil) |
| 401 | |
| 402 | // Implements android.AndroidMkEntriesProvider |
| 403 | func (b *bootimg) AndroidMkEntries() []android.AndroidMkEntries { |
| 404 | return []android.AndroidMkEntries{android.AndroidMkEntries{ |
| 405 | Class: "ETC", |
| 406 | OutputFile: android.OptionalPathForPath(b.output), |
| 407 | ExtraEntries: []android.AndroidMkExtraEntriesFunc{ |
Colin Cross | aa25553 | 2020-07-03 13:18:24 -0700 | [diff] [blame] | 408 | func(ctx android.AndroidMkExtraEntriesContext, entries *android.AndroidMkEntries) { |
Colin Cross | c68db4b | 2021-11-11 18:59:15 -0800 | [diff] [blame] | 409 | entries.SetString("LOCAL_MODULE_PATH", b.installDir.String()) |
Jiyong Park | 1f7b93e | 2021-02-01 21:38:11 +0900 | [diff] [blame] | 410 | entries.SetString("LOCAL_INSTALLED_MODULE_STEM", b.installFileName()) |
| 411 | }, |
| 412 | }, |
| 413 | }} |
| 414 | } |
| 415 | |
| 416 | var _ Filesystem = (*bootimg)(nil) |
| 417 | |
| 418 | func (b *bootimg) OutputPath() android.Path { |
| 419 | return b.output |
| 420 | } |
Jiyong Park | b0eb319 | 2021-03-09 20:29:07 +0900 | [diff] [blame] | 421 | |
Jiyong Park | 972e06c | 2021-03-15 23:32:49 +0900 | [diff] [blame] | 422 | func (b *bootimg) SignedOutputPath() android.Path { |
| 423 | if proptools.Bool(b.properties.Use_avb) { |
| 424 | return b.OutputPath() |
| 425 | } |
| 426 | return nil |
| 427 | } |