|
| 1 | +using System.IO.Compression; |
| 2 | + |
| 3 | +namespace FirmwareKit.Comm.Fastboot; |
| 4 | + |
| 5 | +public class FastbootFlashAll(FastbootUtil util) |
| 6 | +{ |
| 7 | + private readonly FastbootUtil _util = util; |
| 8 | + private readonly ProductInfoParser _parser = new(util); |
| 9 | + |
| 10 | + public void FlashUpdateZip(string zipPath, bool skipSecondary = false) |
| 11 | + { |
| 12 | + _util.NotifyCurrentStep("Extracting update zip..."); |
| 13 | + string tempDir = Path.Combine(Path.GetTempPath(), "fastboot_update_" + Path.GetRandomFileName()); |
| 14 | + Directory.CreateDirectory(tempDir); |
| 15 | + try |
| 16 | + { |
| 17 | + ZipFile.ExtractToDirectory(zipPath, tempDir); |
| 18 | + FlashFromDirectory(tempDir, skipSecondary); |
| 19 | + } |
| 20 | + finally |
| 21 | + { |
| 22 | + try { Directory.Delete(tempDir, true); } catch { } |
| 23 | + } |
| 24 | + } |
| 25 | + |
| 26 | + public void FlashFromDirectory(string directory, bool skipSecondary = false) |
| 27 | + { |
| 28 | + string androidProductOut = directory; |
| 29 | + |
| 30 | + // 1. Check android-info.txt |
| 31 | + string infoPath = Path.Combine(androidProductOut, "android-info.txt"); |
| 32 | + if (File.Exists(infoPath)) |
| 33 | + { |
| 34 | + _util.NotifyCurrentStep("Verifying device compatibility..."); |
| 35 | + string content = File.ReadAllText(infoPath); |
| 36 | + if (!_parser.Validate(content, out string? error)) |
| 37 | + { |
| 38 | + throw new Exception("Incompatible device: " + error); |
| 39 | + } |
| 40 | + } |
| 41 | + |
| 42 | + // 2. Determine partitions to flash |
| 43 | + // Standard AOSP behavior: flash partitions in a specific order |
| 44 | + // and handle logical partitions via super_empty.img if present. |
| 45 | + |
| 46 | + var partitions = GetPartitionList(androidProductOut); |
| 47 | + |
| 48 | + // 3. Handle Super Partition Optimization (dynamic partitions) |
| 49 | + string superEmpty = Path.Combine(androidProductOut, "super_empty.img"); |
| 50 | + if (File.Exists(superEmpty)) |
| 51 | + { |
| 52 | + FlashDynamicPartitions(androidProductOut, superEmpty); |
| 53 | + } |
| 54 | + |
| 55 | + // 4. Flash normal partitions |
| 56 | + foreach (var (part, file) in partitions) |
| 57 | + { |
| 58 | + if (IsDynamicPartition(part)) continue; // Already handled |
| 59 | + |
| 60 | + _util.NotifyCurrentStep($"Flashing {part}..."); |
| 61 | + FlashImage(part, file); |
| 62 | + } |
| 63 | + |
| 64 | + _util.NotifyCurrentStep("Flash completed."); |
| 65 | + } |
| 66 | + |
| 67 | + private void FlashDynamicPartitions(string directory, string superEmptyPath) |
| 68 | + { |
| 69 | + _util.NotifyCurrentStep("Flashing dynamic partitions..."); |
| 70 | + var helper = new SuperFlashHelper(_util, "super", superEmptyPath); |
| 71 | + |
| 72 | + // Find all images that should be in super |
| 73 | + // Usually these are system, vendor, product, system_ext, odm, etc. |
| 74 | + string[] dynamicPartitions = ["system", "vendor", "product", "system_ext", "odm", "vendor_dlkm", "odm_dlkm"]; |
| 75 | + |
| 76 | + bool addedAny = false; |
| 77 | + foreach (var p in dynamicPartitions) |
| 78 | + { |
| 79 | + string img = Path.Combine(directory, p + ".img"); |
| 80 | + if (File.Exists(img)) |
| 81 | + { |
| 82 | + helper.AddPartition(p, img); |
| 83 | + addedAny = true; |
| 84 | + } |
| 85 | + |
| 86 | + // Handle A/B slots for dynamic partitions if needed |
| 87 | + string imgA = Path.Combine(directory, p + "_a.img"); |
| 88 | + if (File.Exists(imgA)) |
| 89 | + { |
| 90 | + helper.AddPartition(p + "_a", imgA); |
| 91 | + addedAny = true; |
| 92 | + } |
| 93 | + } |
| 94 | + |
| 95 | + if (addedAny) |
| 96 | + { |
| 97 | + helper.Flash(); |
| 98 | + } |
| 99 | + } |
| 100 | + |
| 101 | + private void FlashImage(string partition, string filePath) |
| 102 | + { |
| 103 | + // Check if sparse |
| 104 | + if (IsSparseImage(filePath)) |
| 105 | + { |
| 106 | + _util.FlashSparseImage(partition, filePath).ThrowIfError(); |
| 107 | + } |
| 108 | + else |
| 109 | + { |
| 110 | + using var fs = File.OpenRead(filePath); |
| 111 | + _util.FlashUnsparseImage(partition, fs, fs.Length).ThrowIfError(); |
| 112 | + } |
| 113 | + } |
| 114 | + |
| 115 | + private bool IsSparseImage(string path) |
| 116 | + { |
| 117 | + using var fs = File.OpenRead(path); |
| 118 | + byte[] header = new byte[4]; |
| 119 | + if (fs.Read(header, 0, 4) == 4) |
| 120 | + { |
| 121 | + return BitConverter.ToUInt32(header, 0) == 0xED26FF3A; |
| 122 | + } |
| 123 | + return false; |
| 124 | + } |
| 125 | + |
| 126 | + private bool IsDynamicPartition(string name) |
| 127 | + { |
| 128 | + // Simple heuristic: if it's in the list of typical dynamic partitions |
| 129 | + string[] dynamic = ["system", "vendor", "product", "system_ext", "odm", "vendor_dlkm", "odm_dlkm"]; |
| 130 | + return dynamic.Any(d => name == d || name.StartsWith(d + "_")); |
| 131 | + } |
| 132 | + |
| 133 | + private List<(string partition, string file)> GetPartitionList(string directory) |
| 134 | + { |
| 135 | + var list = new List<(string, string)>(); |
| 136 | + string[] priority = ["boot", "init_boot", "vendor_boot", "dtbo", "vbmeta", "vbmeta_system", "vbmeta_vendor", "recovery"]; |
| 137 | + |
| 138 | + foreach (var p in priority) |
| 139 | + { |
| 140 | + string path = Path.Combine(directory, p + ".img"); |
| 141 | + if (File.Exists(path)) list.Add((p, path)); |
| 142 | + } |
| 143 | + |
| 144 | + // Add others found in directory |
| 145 | + foreach (var file in Directory.GetFiles(directory, "*.img")) |
| 146 | + { |
| 147 | + string name = Path.GetFileNameWithoutExtension(file); |
| 148 | + if (!priority.Contains(name) && name != "super_empty") |
| 149 | + { |
| 150 | + list.Add((name, file)); |
| 151 | + } |
| 152 | + } |
| 153 | + |
| 154 | + return list; |
| 155 | + } |
| 156 | +} |
0 commit comments