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