Cole Faust | 74ee4e0 | 2025-01-16 14:55:35 -0800 | [diff] [blame] | 1 | // Copyright (C) 2024 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 | "android/soong/android" |
| 19 | "path/filepath" |
| 20 | "strings" |
| 21 | |
| 22 | "github.com/google/blueprint" |
| 23 | "github.com/google/blueprint/proptools" |
| 24 | ) |
| 25 | |
Cole Faust | 3c02ffa | 2025-02-19 15:20:20 -0800 | [diff] [blame^] | 26 | var ( |
| 27 | systemOtherPropFileTweaks = pctx.AndroidStaticRule("system_other_prop_file_tweaks", blueprint.RuleParams{ |
| 28 | Command: `rm -rf $out && sed -e 's@^mount_point=/$$@mount_point=system_other@g' -e 's@^partition_name=system$$@partition_name=system_other@g' $in > $out`, |
| 29 | }) |
| 30 | ) |
| 31 | |
Cole Faust | 74ee4e0 | 2025-01-16 14:55:35 -0800 | [diff] [blame] | 32 | type SystemOtherImageProperties struct { |
| 33 | // The system_other image always requires a reference to the system image. The system_other |
| 34 | // partition gets built into the system partition's "b" slot in a/b partition builds. Thus, it |
| 35 | // copies most of its configuration from the system image, such as filesystem type, avb signing |
| 36 | // info, etc. Including it here does not automatically mean that it will pick up the system |
| 37 | // image's dexpropt files, it must also be listed in Preinstall_dexpreopt_files_from for that. |
| 38 | System_image *string |
| 39 | |
| 40 | // This system_other partition will include all the dexpreopt files from the apps on these |
| 41 | // partitions. |
| 42 | Preinstall_dexpreopt_files_from []string |
| 43 | } |
| 44 | |
| 45 | type systemOtherImage struct { |
| 46 | android.ModuleBase |
| 47 | android.DefaultableModuleBase |
| 48 | properties SystemOtherImageProperties |
| 49 | } |
| 50 | |
| 51 | // The system_other image is the default contents of the "b" slot of the system image. |
| 52 | // It contains the dexpreopt files of all the apps on the device, for a faster first boot. |
| 53 | // Afterwards, at runtime, it will be used as a regular b slot for OTA updates, and the initial |
| 54 | // dexpreopt files will be deleted. |
| 55 | func SystemOtherImageFactory() android.Module { |
| 56 | module := &systemOtherImage{} |
| 57 | module.AddProperties(&module.properties) |
Cole Faust | b8e280f | 2025-01-16 16:33:26 -0800 | [diff] [blame] | 58 | android.InitAndroidArchModule(module, android.DeviceSupported, android.MultilibCommon) |
Cole Faust | 74ee4e0 | 2025-01-16 14:55:35 -0800 | [diff] [blame] | 59 | android.InitDefaultableModule(module) |
| 60 | return module |
| 61 | } |
| 62 | |
| 63 | type systemImageDeptag struct { |
| 64 | blueprint.BaseDependencyTag |
| 65 | } |
| 66 | |
| 67 | var systemImageDependencyTag = systemImageDeptag{} |
| 68 | |
| 69 | type dexpreoptDeptag struct { |
| 70 | blueprint.BaseDependencyTag |
| 71 | } |
| 72 | |
| 73 | var dexpreoptDependencyTag = dexpreoptDeptag{} |
| 74 | |
| 75 | func (m *systemOtherImage) DepsMutator(ctx android.BottomUpMutatorContext) { |
| 76 | if proptools.String(m.properties.System_image) == "" { |
| 77 | ctx.ModuleErrorf("system_image property must be set") |
| 78 | return |
| 79 | } |
| 80 | ctx.AddDependency(ctx.Module(), systemImageDependencyTag, *m.properties.System_image) |
| 81 | ctx.AddDependency(ctx.Module(), dexpreoptDependencyTag, m.properties.Preinstall_dexpreopt_files_from...) |
| 82 | } |
| 83 | |
| 84 | func (m *systemOtherImage) GenerateAndroidBuildActions(ctx android.ModuleContext) { |
| 85 | systemImage := ctx.GetDirectDepProxyWithTag(*m.properties.System_image, systemImageDependencyTag) |
| 86 | systemInfo, ok := android.OtherModuleProvider(ctx, systemImage, FilesystemProvider) |
| 87 | if !ok { |
| 88 | ctx.PropertyErrorf("system_image", "Expected system_image module to provide FilesystemProvider") |
| 89 | return |
| 90 | } |
| 91 | |
| 92 | output := android.PathForModuleOut(ctx, "system_other.img") |
| 93 | stagingDir := android.PathForModuleOut(ctx, "staging_dir") |
Spandan Das | 7a42d1c | 2025-02-12 01:32:21 +0000 | [diff] [blame] | 94 | stagingDirTimestamp := android.PathForModuleOut(ctx, "staging_dir.timestamp") |
Cole Faust | 74ee4e0 | 2025-01-16 14:55:35 -0800 | [diff] [blame] | 95 | |
| 96 | builder := android.NewRuleBuilder(pctx, ctx) |
| 97 | builder.Command().Textf("rm -rf %s && mkdir -p %s", stagingDir, stagingDir) |
| 98 | |
Cole Faust | b8e280f | 2025-01-16 16:33:26 -0800 | [diff] [blame] | 99 | specs := make(map[string]android.PackagingSpec) |
Cole Faust | 74ee4e0 | 2025-01-16 14:55:35 -0800 | [diff] [blame] | 100 | for _, otherPartition := range m.properties.Preinstall_dexpreopt_files_from { |
| 101 | dexModule := ctx.GetDirectDepProxyWithTag(otherPartition, dexpreoptDependencyTag) |
Cole Faust | b8e280f | 2025-01-16 16:33:26 -0800 | [diff] [blame] | 102 | fsInfo, ok := android.OtherModuleProvider(ctx, dexModule, FilesystemProvider) |
Cole Faust | 74ee4e0 | 2025-01-16 14:55:35 -0800 | [diff] [blame] | 103 | if !ok { |
| 104 | ctx.PropertyErrorf("preinstall_dexpreopt_files_from", "Expected module %q to provide FilesystemProvider", otherPartition) |
| 105 | return |
| 106 | } |
Cole Faust | b8e280f | 2025-01-16 16:33:26 -0800 | [diff] [blame] | 107 | // Merge all the packaging specs into 1 map |
| 108 | for k := range fsInfo.SpecsForSystemOther { |
| 109 | if _, ok := specs[k]; ok { |
| 110 | ctx.ModuleErrorf("Packaging spec %s given by two different partitions", k) |
| 111 | continue |
| 112 | } |
| 113 | specs[k] = fsInfo.SpecsForSystemOther[k] |
| 114 | } |
| 115 | } |
| 116 | |
| 117 | // TOOD: CopySpecsToDir only exists on PackagingBase, but doesn't use any fields from it. Clean this up. |
| 118 | (&android.PackagingBase{}).CopySpecsToDir(ctx, builder, specs, stagingDir) |
| 119 | |
| 120 | if len(m.properties.Preinstall_dexpreopt_files_from) > 0 { |
| 121 | builder.Command().Textf("touch %s", filepath.Join(stagingDir.String(), "system-other-odex-marker")) |
Cole Faust | 74ee4e0 | 2025-01-16 14:55:35 -0800 | [diff] [blame] | 122 | } |
Spandan Das | 7a42d1c | 2025-02-12 01:32:21 +0000 | [diff] [blame] | 123 | builder.Command().Textf("touch").Output(stagingDirTimestamp) |
| 124 | builder.Build("assemble_filesystem_staging_dir", "Assemble filesystem staging dir") |
Cole Faust | 74ee4e0 | 2025-01-16 14:55:35 -0800 | [diff] [blame] | 125 | |
| 126 | // Most of the time, if build_image were to call a host tool, it accepts the path to the |
| 127 | // host tool in a field in the prop file. However, it doesn't have that option for fec, which |
| 128 | // it expects to just be on the PATH. Add fec to the PATH. |
| 129 | fec := ctx.Config().HostToolPath(ctx, "fec") |
| 130 | pathToolDirs := []string{filepath.Dir(fec.String())} |
| 131 | |
Cole Faust | 3c02ffa | 2025-02-19 15:20:20 -0800 | [diff] [blame^] | 132 | // In make, the exact same prop file is used for both system and system_other. However, I |
| 133 | // believe make goes through a different build_image code path that is based on the name of |
| 134 | // the output file. So it sees the output file is named system_other.img and makes some changes. |
| 135 | // We don't use that codepath, so make the changes manually to the prop file. |
| 136 | propFile := android.PathForModuleOut(ctx, "prop") |
| 137 | ctx.Build(pctx, android.BuildParams{ |
| 138 | Rule: systemOtherPropFileTweaks, |
| 139 | Input: systemInfo.BuildImagePropFile, |
| 140 | Output: propFile, |
| 141 | }) |
| 142 | |
Spandan Das | 7a42d1c | 2025-02-12 01:32:21 +0000 | [diff] [blame] | 143 | builder = android.NewRuleBuilder(pctx, ctx) |
Cole Faust | 74ee4e0 | 2025-01-16 14:55:35 -0800 | [diff] [blame] | 144 | builder.Command(). |
| 145 | Textf("PATH=%s:$PATH", strings.Join(pathToolDirs, ":")). |
| 146 | BuiltTool("build_image"). |
| 147 | Text(stagingDir.String()). // input directory |
Cole Faust | 3c02ffa | 2025-02-19 15:20:20 -0800 | [diff] [blame^] | 148 | Input(propFile). |
Cole Faust | 74ee4e0 | 2025-01-16 14:55:35 -0800 | [diff] [blame] | 149 | Implicits(systemInfo.BuildImagePropFileDeps). |
| 150 | Implicit(fec). |
Spandan Das | 7a42d1c | 2025-02-12 01:32:21 +0000 | [diff] [blame] | 151 | Implicit(stagingDirTimestamp). |
Cole Faust | 74ee4e0 | 2025-01-16 14:55:35 -0800 | [diff] [blame] | 152 | Output(output). |
| 153 | Text(stagingDir.String()) |
| 154 | |
| 155 | builder.Build("build_system_other", "build system other") |
| 156 | |
Spandan Das | 7a42d1c | 2025-02-12 01:32:21 +0000 | [diff] [blame] | 157 | // Create a hermetic system_other.img with pinned timestamps |
| 158 | builder = android.NewRuleBuilder(pctx, ctx) |
| 159 | outputHermetic := android.PathForModuleOut(ctx, "for_target_files", "system_other.img") |
Cole Faust | 3c02ffa | 2025-02-19 15:20:20 -0800 | [diff] [blame^] | 160 | outputHermeticPropFile := m.propFileForHermeticImg(ctx, builder, propFile) |
Spandan Das | 7a42d1c | 2025-02-12 01:32:21 +0000 | [diff] [blame] | 161 | builder.Command(). |
| 162 | Textf("PATH=%s:$PATH", strings.Join(pathToolDirs, ":")). |
| 163 | BuiltTool("build_image"). |
| 164 | Text(stagingDir.String()). // input directory |
| 165 | Input(outputHermeticPropFile). |
| 166 | Implicits(systemInfo.BuildImagePropFileDeps). |
| 167 | Implicit(fec). |
| 168 | Implicit(stagingDirTimestamp). |
| 169 | Output(outputHermetic). |
| 170 | Text(stagingDir.String()) |
| 171 | |
| 172 | builder.Build("build_system_other_hermetic", "build system other") |
| 173 | |
| 174 | fsInfo := FilesystemInfo{ |
| 175 | Output: output, |
| 176 | OutputHermetic: outputHermetic, |
| 177 | RootDir: stagingDir, |
| 178 | } |
| 179 | |
| 180 | android.SetProvider(ctx, FilesystemProvider, fsInfo) |
| 181 | |
Cole Faust | 74ee4e0 | 2025-01-16 14:55:35 -0800 | [diff] [blame] | 182 | ctx.SetOutputFiles(android.Paths{output}, "") |
| 183 | ctx.CheckbuildFile(output) |
| 184 | } |
Spandan Das | 7a42d1c | 2025-02-12 01:32:21 +0000 | [diff] [blame] | 185 | |
| 186 | func (f *systemOtherImage) propFileForHermeticImg(ctx android.ModuleContext, builder *android.RuleBuilder, inputPropFile android.Path) android.Path { |
| 187 | propFilePinnedTimestamp := android.PathForModuleOut(ctx, "for_target_files", "prop") |
| 188 | builder.Command().Textf("cat").Input(inputPropFile).Flag(">").Output(propFilePinnedTimestamp). |
| 189 | Textf(" && echo use_fixed_timestamp=true >> %s", propFilePinnedTimestamp) |
| 190 | return propFilePinnedTimestamp |
| 191 | } |