payload_dumper 0.7.7

A fast and efficient Android OTA payload dumper written in Rust
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
use std::collections::HashSet;
use std::sync::Arc;
use std::time::Instant;

use anyhow::{Result, anyhow};

use clap::Parser;
use indicatif::{MultiProgress, ProgressBar, ProgressStyle};
use tokio::fs;
use tokio::sync::Semaphore;

mod args;
#[cfg(feature = "remote_zip")]
mod http;
#[cfg(feature = "metadata")]
mod metadata;
mod payload;
#[cfg(feature = "prefetch")]
mod prefetch;
mod readers;
#[cfg(feature = "metadata")]
mod structs;
mod utils;
mod verify;
#[cfg(any(feature = "local_zip", feature = "remote_zip"))]
mod zip;

use crate::args::Args;
#[cfg(feature = "metadata")]
use crate::metadata::handle_metadata_extraction;
use crate::payload::payload_dumper::{AsyncPayloadRead, dump_partition};
use crate::payload::payload_parser::parse_local_payload;
#[cfg(feature = "local_zip")]
use crate::payload::payload_parser::parse_local_zip_payload;
#[cfg(feature = "remote_zip")]
use crate::payload::payload_parser::parse_remote_payload;
use crate::readers::local_reader::LocalAsyncPayloadReader;
#[cfg(feature = "local_zip")]
use crate::readers::local_zip_reader::LocalAsyncZipPayloadReader;
#[cfg(feature = "remote_zip")]
use crate::readers::remote_zip_reader::RemoteAsyncZipPayloadReader;
use crate::utils::FileType;
#[cfg(feature = "remote_zip")]
use crate::utils::detect_remote_file_type;
use crate::utils::{detect_file_type, format_elapsed_time, format_size, list_partitions};
use crate::verify::verify_partitions_hash;

include!("proto/update_metadata.rs");

#[tokio::main]
async fn main() -> Result<()> {
    let args = Args::parse();

    // validate metadata feature usage
    #[cfg(not(feature = "metadata"))]
    if args.metadata.is_some() {
        return Err(anyhow!(
            "Metadata functionality requires the 'metadata' feature to be enabled. Please recompile with --features metadata"
        ));
    }

    let thread_count = if args.no_parallel {
        1
    } else if let Some(threads) = args.threads {
        threads
    } else {
        num_cpus::get()
    };

    println!("- Initialized {} thread(s)", thread_count);

    let start_time = Instant::now();

    let multi_progress = MultiProgress::new();
    let main_pb = multi_progress.add(ProgressBar::new_spinner());
    main_pb.set_style(
        ProgressStyle::default_spinner()
            .template("{spinner:.blue} {msg}")
            .unwrap(),
    );
    main_pb.enable_steady_tick(tokio::time::Duration::from_millis(300));

    let payload_path_str = args.payload_path.to_string_lossy().to_string();

    let is_url =
        payload_path_str.starts_with("http://") || payload_path_str.starts_with("https://");

    // check if we're outputting to stdout
    let is_stdout = args.out.to_string_lossy() == "-";

    // detect file type
    let extension = args
        .payload_path
        .extension()
        .and_then(|e| e.to_str())
        .unwrap_or("");

    let mut is_zip = extension == "zip";
    let mut is_bin = extension == "bin" || extension.is_empty();

    // if extension detection is inconclusive, check magic bytes
    if !is_url && (!is_zip && !is_bin || extension.is_empty()) {
        match detect_file_type(&args.payload_path).await {
            Ok(FileType::Zip) => {
                is_zip = true;
                is_bin = false;
            }
            Ok(FileType::Bin) => {
                is_bin = true;
                is_zip = false;
            }
            Err(e) => {
                return Err(anyhow!(
                    "Unable to detect file type. Extension: '{}', Error: {}",
                    extension,
                    e
                ));
            }
        }
    }

    // if URL has no clear extension, check magic bytes remotely
    if is_url && extension.is_empty() {
        #[cfg(feature = "remote_zip")]
        {
            if !is_stdout {
                main_pb.set_message("Detecting remote file type...");
            }

            match detect_remote_file_type(&payload_path_str, args.user_agent.as_deref()).await {
                Ok(FileType::Zip) => {
                    is_zip = true;
                    is_bin = false;
                }

                Ok(FileType::Bin) => {
                    return Err(anyhow!(
                        "Direct .bin URLs are not supported. Only ZIP files containing payload.bin are supported for remote URLs."
                    ));
                }
                Err(e) => {
                    return Err(anyhow!("Unable to detect remote file type: {}", e));
                }
            }
        }
    }

    if !is_zip && !is_bin {
        return Err(anyhow!(
            "Unsupported file type. Only .bin and .zip files are supported"
        ));
    }

    // validate feature requirements
    if is_url {
        #[cfg(not(feature = "remote_zip"))]
        return Err(anyhow!(
            "Remote URL processing requires the 'remote_zip' feature to be enabled. Please recompile with --features remote_zip"
        ));
    }

    if is_zip && !is_url {
        #[cfg(not(feature = "local_zip"))]
        return Err(anyhow!(
            "Local ZIP file processing requires the 'local_zip' feature to be enabled. Please recompile with --features local_zip"
        ));
    }

    main_pb.set_message("Opening file...");

    // Get file metadata
    if let Ok(metadata) = fs::metadata(&args.payload_path).await
        && metadata.len() > 1024 * 1024
    {
        if is_stdout {
            eprintln!(
                "- Processing file: {}, size: {}",
                payload_path_str,
                format_size(metadata.len())
            );
        } else {
            main_pb.println(format!(
                "- Processing file: {}, size: {}",
                payload_path_str,
                format_size(metadata.len())
            ));
        }
    }

    if args.out.to_string_lossy() != "-" {
        fs::create_dir_all(&args.out).await?;
    }

    main_pb.set_message("Parsing payload...");

    let (manifest, data_offset) = if is_url {
        #[cfg(feature = "remote_zip")]
        {
            if !is_zip {
                return Err(anyhow!(
                    "Remote URLs must point to ZIP files containing payload.bin\n\
                 Direct .bin URLs are not supported"
                ));
            }

            if !is_stdout {
                main_pb.println("- Connecting to remote ZIP archive...");
            }
            let (manifest, data_offset, content_length) =
                parse_remote_payload(payload_path_str.clone(), args.user_agent.as_deref()).await?;

            // Print the ZIP size immediately after connecting
            if is_stdout {
                eprintln!("- Remote ZIP size: {}", format_size(content_length));
            } else {
                main_pb.println(format!(
                    "- Remote ZIP size: {}",
                    format_size(content_length)
                ));
            }

            (manifest, data_offset)
        }
        #[cfg(not(feature = "remote_zip"))]
        {
            unreachable!();
        }
    } else if is_zip {
        #[cfg(feature = "local_zip")]
        {
            parse_local_zip_payload(args.payload_path.clone()).await?
        }
        #[cfg(not(feature = "local_zip"))]
        {
            unreachable!();
        }
    } else {
        // Local .bin file
        parse_local_payload(&args.payload_path).await?
    };

    // Print security patch level
    if let Some(security_patch) = &manifest.security_patch_level {
        if is_stdout {
            eprintln!("- Security Patch: {}", security_patch);
        } else {
            main_pb.println(format!("- Security Patch: {}", security_patch));
        }
    }

    #[cfg(feature = "metadata")]
    if let Some(mode) = &args.metadata
        && !args.list
    {
        main_pb.println("- Extracting metadata...");

        match handle_metadata_extraction(
            &manifest,
            &args.out,
            data_offset,
            mode,
            &args.images,
            is_stdout,
        )
        .await
        {
            Ok(()) => {
                multi_progress.clear()?;
                return Ok(());
            }
            Err(e) => {
                main_pb.finish_with_message("Failed to save metadata");
                return Err(e);
            }
        }
    }

    // handle list command
    if args.list {
        main_pb.finish_and_clear();
        multi_progress.clear()?;

        // if metadata was requested in list mode, save it
        #[cfg(feature = "metadata")]
        if let Some(mode) = &args.metadata {
            match handle_metadata_extraction(
                &manifest,
                &args.out,
                data_offset,
                mode,
                &args.images,
                is_stdout,
            )
            .await
            {
                Ok(()) => {
                    if is_stdout {
                        return Ok(());
                    }
                }
                Err(e) => {
                    eprintln!("Failed to save metadata: {}", e);
                }
            }
        }

        println!();
        list_partitions(&manifest);
        return Ok(());
    }

    let block_size = manifest.block_size.unwrap_or(4096);

    // Determine partitions to extract
    let partitions_to_extract: Vec<PartitionUpdate> = if args.images.is_empty() {
        manifest.partitions.clone()
    } else {
        let images = args.images.split(',').collect::<HashSet<_>>();
        manifest
            .partitions
            .iter()
            .filter(|p| images.contains(p.partition_name.as_str()))
            .cloned() // Clone each partition
            .collect()
    };

    if partitions_to_extract.is_empty() {
        main_pb.finish_with_message("No partitions to extract");
        multi_progress.clear()?;
        return Ok(());
    }

    main_pb.println(format!(
        "- Found {} partitions to extract",
        partitions_to_extract.len()
    ));

    // Check if prefetch mode is enabled for remote URLs
    if args.prefetch && is_url {
        #[cfg(feature = "prefetch")]
        {
            if !is_stdout {
                main_pb.println("- Using prefetch mode for remote extraction");
            }

            let multi_progress = Arc::new(multi_progress);
            let args = Arc::new(args);

            return crate::prefetch::prefetch_and_extract(
                payload_path_str.clone(),
                manifest,
                data_offset,
                Arc::clone(&args),
                partitions_to_extract,
                Arc::clone(&multi_progress),
            )
            .await;
        }
        #[cfg(not(feature = "prefetch"))]
        {
            return Err(anyhow!(
                "Prefetch mode requires the 'prefetch' feature to be enabled"
            ));
        }
    }

    // Prefetch not enabled - continue with normal extraction
    let use_parallel = !args.no_parallel;

    main_pb.set_message(if use_parallel {
        "Extracting Partitions..."
    } else {
        "Processing partitions..."
    });

    let payload_reader: Arc<dyn AsyncPayloadRead> = if is_url {
        #[cfg(feature = "remote_zip")]
        {
            // Remote URL
            if !is_stdout {
                main_pb.println("- Preparing remote extraction...");
            }

            let remote_reader = RemoteAsyncZipPayloadReader::new(
                payload_path_str.clone(),
                args.user_agent.as_deref(),
            )
            .await?;

            // Print remote ZIP size (for non-prefetch mode)
            if is_stdout {
                eprintln!(
                    "- Remote ZIP size: {}",
                    format_size(remote_reader.http_reader.content_length)
                );
            } else {
                main_pb.println(format!(
                    "- Remote ZIP size: {}",
                    format_size(remote_reader.http_reader.content_length)
                ));
            }

            Arc::new(remote_reader)
        }
        #[cfg(not(feature = "remote_zip"))]
        {
            unreachable!(); // This should be caught by the validation above
        }
    } else if is_zip {
        #[cfg(feature = "local_zip")]
        {
            Arc::new(LocalAsyncZipPayloadReader::new(args.payload_path.clone()).await?)
        }
        #[cfg(not(feature = "local_zip"))]
        {
            unreachable!(); // This should be caught by the validation above
        }
    } else {
        Arc::new(LocalAsyncPayloadReader::new(args.payload_path.clone()).await?)
    };

    let multi_progress = Arc::new(multi_progress);
    let args = Arc::new(args);

    let mut failed_partitions = Vec::new();

    if use_parallel {
        // thread limiting
        let semaphore = Arc::new(Semaphore::new(thread_count));
        let mut tasks = Vec::new();

        for partition in &partitions_to_extract {
            let partition = partition.clone();
            let payload_reader = Arc::clone(&payload_reader);
            let args = Arc::clone(&args);
            let multi_progress = Arc::clone(&multi_progress);
            let semaphore = Arc::clone(&semaphore);

            let task = tokio::spawn(async move {
                // acquire permit to limit concurrent tasks to thread_count
                let _permit = semaphore.acquire().await.unwrap();

                let partition_name = partition.partition_name.clone();

                match dump_partition(
                    &partition,
                    data_offset,
                    block_size as u64,
                    &args,
                    &payload_reader,
                    Some(&multi_progress),
                )
                .await
                {
                    Ok(()) => Ok(()),
                    Err(e) => Err((partition_name, e)),
                }
                // permit is automatically released when _permit is dropped
            });

            tasks.push(task);
        }

        // wait for all tasks to complete
        let results = futures::future::join_all(tasks).await;

        for result in results {
            match result {
                Ok(Ok(())) => {}
                Ok(Err((partition_name, error))) => {
                    eprintln!("Failed to process partition {}: {}", partition_name, error);
                    failed_partitions.push(partition_name);
                }
                Err(e) => {
                    eprintln!("Task panicked: {}", e);
                }
            }
        }
    } else {
        // Sequential async extraction
        for partition in &partitions_to_extract {
            if let Err(e) = dump_partition(
                partition,
                data_offset,
                block_size as u64,
                &args,
                &payload_reader,
                Some(&multi_progress),
            )
            .await
            {
                eprintln!(
                    "Failed to process partition {}: {}",
                    partition.partition_name, e
                );
                failed_partitions.push(partition.partition_name.clone());
            }
        }
    }

    // Verify partitions
    if !args.no_verify {
        main_pb.println("- Verifying partition hashes...");

        let partitions_to_verify: Vec<&PartitionUpdate> = partitions_to_extract
            .iter()
            .filter(|p| !failed_partitions.contains(&p.partition_name))
            .collect();

        match verify_partitions_hash(&partitions_to_verify, &args, &multi_progress).await {
            Ok(failed_verifications) => {
                if !failed_verifications.is_empty() {
                    eprintln!(
                        "Hash verification failed for {} partitions.",
                        failed_verifications.len()
                    );
                }
            }
            Err(e) => {
                eprintln!("Error during hash verification: {}", e);
            }
        }
    } else {
        main_pb.println("- Skipping hash verification");
    }

    let elapsed_time = format_elapsed_time(start_time.elapsed());

    if failed_partitions.is_empty() {
        main_pb.finish_with_message(format!(
            "All partitions extracted successfully! (in {})",
            elapsed_time
        ));
        println!(
            "\nExtraction completed successfully in {}. Output directory: {:?}",
            elapsed_time, args.out
        );
    } else {
        main_pb.finish_with_message(format!(
            "Completed with {} failed partitions. (in {})",
            failed_partitions.len(),
            elapsed_time
        ));
        println!(
            "\nExtraction completed with {} failed partitions in {}. Output directory: {:?}",
            failed_partitions.len(),
            elapsed_time,
            args.out
        );
    }

    Ok(())
}