mounting a raw disk in a backup - android

I trying to get all data from my android phone. I got alot of files named: ".backup".
step 1:
cat userdata_20200107_174037.backup userdata_20200107_174037.backup1 userdata_20200107_174037.backup2 userdata_20200107_174037.backup3 userdata_20200107_174037.backup4 userdata_20200107_174037.backup5 userdata_20200107_174037.backup6 userdata_20200107_174037.backup7 userdata_20200107_174037.backup8 userdata_20200107_174037.backup9 userdata_20200107_174037.backup10 userdata_20200107_174037.backup11 userdata_20200107_174037.backup12 userdata_20200107_174037.backup13 > userdata.img
step 2 if this not works goto step3:
dd if=userdata.img bs=512 skip=1 | gunzip -c | tar xv
gzip: stdin: not in gzip format tar: This does not look like a tar
archive tar: Exiting with failure status due to previous errors
step 3:
simg2img userdata.img userdata_raw.img
Invalid sparse file format at header magi Failed to read sparse file
step 4:
sudo mount -t ext4 -o loop,rw userdata.img /media/phone/
mount: wrong fs type, bad option, bad superblock on /dev/loop8,
missing codepage or helper program, or other error In some cases
useful info is found in syslog - try dmesg | tail or so
i Follow the stack overflow here: https://android.stackexchange.com/questions/141805/how-to-extract-backup-files-created-by-android-stock-recovery
Is there a easy way to backup all data in android ? and how can i do it? hole data include system??
Note:
then i run (file userdata.img) it's data file
and not Android sparse image (file)

Related

Calculating hash of files in directory hierarchy in Android

I need to calculate md5 hash of files in a directory hierarchy. I am using the following case as a test. The Android device I have has a md5 binary, but needs absolute path of file (md5 <filename>).
I have the following directory hierarchy on a Android device:
/data/local/tmp/test1
/data/local/tmp/test1/test2
/data/local/tmp/test1/test2/test3
To get list of absolute paths, I followed the answer mentioned here.
$ adb shell 'function rcrls() { ls -d $1/* | while read f; do echo "$f"; if [ -d "$f" ]; then rcrls "$f"; fi; done } ; rcrls /data/local/tmp/' > filelist.txt
Now I have list of absolute paths of each file.
Next I want to read this file in a script line by line, and call md5 for each line. md5 will print a message if the input is a directory. I followed the example here.
#! /bin/bash
filename='filelist.txt'
cat $filename | while read LINE; do
adb shell 'md5 $LINE'
done
I get the following output:
/data/local/tmp/test1/test2
/data/local/tmp/test1/test2/test3
could not read /data/local/tmp/test1, Is a directory
I expected it to print a directory warning for test1 and test2, and then md5 for test3, as test3 is a file. Can someone suggest how to fix this code ? I was expecting something like:
could not read /data/local/tmp/test1, Is a directory
could not read /data/local/tmp/test1/test2, Is a directory
<hash_value> /data/local/tmp/test1/test2/test3
You should use find:
adb shell find /data/local/tmp -type f -exec md5sum {} '\;'

Script that will transfer photos from phone camera using adb

Story
I take photos and record videos with my phone camera and keep all of them on my internal storage/sdcard. I periodically back them up on my PC, so I keep these camera photos on PC storage in sync with phone storage.
For years, I've been backing up my phone camera photos to my PC in the following way:
Plug in phone into PC and allow access to phone data
Browse phone storage → DCIM → Camera
Wait several minutes for the system to load a list of ALL photos
Copy only several latest photos which haven't been backed up yet
I figured that waiting several minutes for all photos to load is an unnecessary drag so I downloaded adb platform tools. I've added the folder bin to my Path environment variable (i.e. %USERPROFILE%\Tools\adb-platform-tools_r28.0.3) so that I can seamlessly use adb and not write its full path each time.
The script
I wrote the following script for Git Bash for Windows. It is also compatible with Unix if you change the $userprofile variable. Essentially, the script pulls camera photos between two dates from phone storage to PC.
# Attach device and start deamon process
adb devices
# Initialize needed variables
userprofile=$(echo "$USERPROFILE" | tr "\\" "/") # Windows adjustments
srcFolder="//storage/06CB-C9CE/DCIM/Camera" # Remote folder
dstFolder="$userprofile/Desktop/CameraPhotos" # Local folder
lsFile="$dstFolder/camera-ls.txt"
filenameRegex="2019061[5-9]_.*" # Date from 20190615 to 20190619
# Create dst folder if it doesn't exist
mkdir -p "$dstFolder"
# 1. List contents from src folder
# 2. Filter out file names matching regex
# 3. Write these file names line by line into a ls file
adb shell ls "$srcFolder" | grep -E "$filenameRegex" > "$lsFile"
# Pull files listed in ls file from src to dst folder
while read filename; do
if [ -z "$filename" ]; then continue; fi
adb pull "$srcFolder/$filename" "$dstFolder" # adb: error: ...
done < "$lsFile"
# Clean up
rm "$lsFile"
# Inform the user
echo "Done pulling files to $dstFolder"
The problem
When I run the script (bash adb-pull-camera-photos.sh), everything runs smoothly except for the adb pull command in the while-loop. It gives the following error:
': No such file or directoryemote object '//storage/06CB-C9CE/DCIM/Camera/20190618_124656.jpg
': No such file or directoryemote object '//storage/06CB-C9CE/DCIM/Camera/20190618_204522.jpg
': No such file or directoryemote object '//storage/06CB-C9CE/DCIM/Camera/20190619_225739.jpg
I am not sure why the output is broken. Sometimes when I resize the Git Bash window some of the text goes haywire. This is the actual error text:
adb: error: failed to stat remote object '//storage/06CB-C9CE/DCIM/Camera/20190618_124656.jpg': No such file or directory
adb: error: failed to stat remote object '//storage/06CB-C9CE/DCIM/Camera/20190618_204522.jpg': No such file or directory
adb: error: failed to stat remote object '//storage/06CB-C9CE/DCIM/Camera/20190619_225739.jpg': No such file or directory
I am sure that these files exist in the specified directory on the phone. When I manually execute the failing command in bash, it succeeds with the following output:
$ adb pull "//storage/06CB-C9CE/DCIM/Camera/20190618_124656.jpg" "C:/Users/User/Desktop/CameraPhotos/"
//storage/06CB-C9CE/DCIM/Camera/20190618_124656.jpg: 1 file pulled. 15.4 MB/s (1854453 bytes in 0.115s)
The question
I can't figure out what's wrong with the script. I thought the Windows system might be causing a commotion, because I don't see the reason why the same code works when entered manually, but doesn't work when run in a script. How do I fix this error?
Additional info
Note that I had to use // in the beginning of an absolute path on Windows because Git Bash would interpret / as its own root directory (C:\Program Files\Git).
I've echoed all variables inside the script and got all the correct paths that otherwise work via manual method.
camera-ls.txt file contents
20190618_124656.jpg
20190618_204522.jpg
20190619_225739.jpg
Additional questions
Is it possible to navigate to external sdcard without using its name? I had to use /storage/06CB-C9CE/ because /sdcard/ navigates to internal storage.
Why does tr "\\" "/" give me this error: tr: warning: an unescaped backslash at end of string is not portable?
Windows batch script
Here's a .bat script that can be run by Windows Command Prompt or Windows PowerShell. No Git Bash required.
:: Start deamon of the device attached
adb devices
:: Pull camera files starting from date
set srcFolder=/storage/06CB-C9CE/DCIM/Camera
set dstFolder=%USERPROFILE%\Desktop\CameraPhotos
set lsFile=%USERPROFILE%\Desktop\CameraPhotos\camera-ls.txt
set dateRegex=2019061[5-9]_.*
mkdir %dstFolder%
adb shell ls %srcFolder% | adb shell grep %dateRegex% > %lsFile%
for /F "tokens=*" %%A in (%lsFile%) do adb pull %srcFolder%/%%A %dstFolder%
del %lsFile%
echo Done pulling files to %dstFolder%
Just edit the srcFolder to point to your phone camera folder,
plug a pattern into the dateRegex for matching the date interval and
save it as a file with .bat extension, i.e: adb-pull-camera-photos.bat.
Double-click the file and it will pull filtered photos into CameraPhotos folder on Desktop.
Keep in mind that you still need have adb for Windows on your PC.
The problem was with Windows line delimiters.
Easy fix
Just add the IFS=$'\r\n' above the loop so that the read command knows the actual line delimiter.
IFS=$'\r\n'
while read filename; do
if [ -z "$filename" ]; then continue; fi
adb pull "$srcFolder/$filename" "$dstFolder"
done < "$lsFile"
Explanation
I tried plugging the whole while-loop into the console and it failed with the same error:
$ bash adb-pull-camera-photos.sh
List of devices attached
9889db343047534336 device
tr: warning: an unescaped backslash at end of string is not portable
': No such file or directoryemote object '//storage/06CB-C9CE/DCIM/Camera/20190618_124656.jpg
': No such file or directoryemote object '//storage/06CB-C9CE/DCIM/Camera/20190618_204522.jpg
': No such file or directoryemote object '//storage/06CB-C9CE/DCIM/Camera/20190619_225739.jpg
Done pulling files to C:/Users/User/Desktop/CameraPhotos
This time I started investigating why the output was broken. I remembered that windows uses \r\n as newline, which means Carriage Return + Line Feed, (CR+LF), so some text must have been overwritten.
It was because of broken values stored inside the $filename variable.
This is the loop from the script:
while read filename; do
if [ -z "$filename" ]; then continue; fi
adb pull "$srcFolder/$filename" "$dstFolder"
done < "$lsFile"
Since each iteration of the while-loop reads a line from $lsFile in the following form:
exampleFilename.jpg\r\n
It misinterprets the newline symbols as part of the file name, so adb pull tries to read files with these whitespaces in their names, but fails and it additionally writes a broken output.
Adb Photo Sync
This might not be the answer but might be useful for others looking for android photo/files backup solution.
I use this script on my Windows with git bash. This can be easily used for Linux. A common issue with a long backup process is that it might get interrupted and you might have to restart the entire copy process from start.
This script saves you from this trouble. You can restart the script or interrupt in between but it will resume copy operation from the point it left.
Just change the rfolder => android folder, lfolder => local folder
#!/bin/sh
rfolder=sdcard/DCIM/Camera
lfolder=/f/mylocal/s8-backup/Camera
adb shell ls "$rfolder" > android.files
ls -1 "$lfolder" > local.files
rm -f update.files
touch update.files
while IFS= read -r q; do
# Remove non-printable characters (are not visible on console)
l=$(echo ${q} | sed 's/[^[:print:]]//')
# Populate files to update
if ! grep -q "$l" local.files; then
echo "$l" >> update.files
fi
done < android.files
script_dir=$(pwd)
cd $lfolder
while IFS= read -r q; do
# Remove non-printable characters (are not visible on console)
l=$(echo ${q} | sed 's/[^[:print:]]//')
echo "Get file: $l"
adb pull "$rfolder/$l"
done < "${script_dir}"/update.files

Mount qcow2 image created by Android emulator

I'm trying to mount the userdata-qemu.img.qcow2 file created by the Android emulator. The following procedure does not work:
sudo qemu-nbd -c /dev/nbd0 ~/.android/avd/Pixel_C_API_27.avd/userdata-qemu.img.qcow2
First command runs well, but running
sudo qemu-nbd -c /dev/nbd0 ~/.android/avd/Pixel_C_API_27.avd/userdata-qemu.img.qcow2
results in this output:
Fehler: /dev/nbd0: unbekannte Partitionstabelle
Modell: Unbekannt (unknown)
Festplatte /dev/nbd0: 3146MB
Sektorgröße (logisch/physisch): 512B/512B
Partitionstabelle: unknown
Disk-Flags:
Basically it cannot recognize a partition table in the image file. You may wonder what's the output of
fdisk /dev/nbd0 -l
so here it is:
Medium /dev/nbd0: 3 GiB, 3145728000 Bytes, 6144000 Sektoren
Einheiten: sectors von 1 * 512 = 512 Bytes
Sektorengröße (logisch/physisch): 512 Bytes / 512 Bytes
I/O Größe (minimal/optimal): 512 Bytes / 512 Bytes
As you could expect already, mounting fails of course, since no partitions can be recognized if the partition table itself cannot be recognized either. Thanks for any help!
Edit: I've just found out that the problem must have to do with Android's userdata encryption. As I've never changed any password the encryption password of the emulator's userdata partition would have to be the default one "default_password". How can I decrypt the image to be able to mount it?
You can mount userdata-qemu.img.qcow2 using the following procedure
convert the image to a raw image
qemu-img convert -O raw userdata-qemu.img.qcow2 udata-raw.img
use losetup to setup a loopback device for mounting
sudo losetup -f -P userdata.img
use losetup -l to see what device was setup
losetup -l
NAME SIZELIMIT OFFSET AUTOCLEAR RO BACK-FILE DIO
/dev/loop0 0 0 0 0 /path/to/userdata.img 0
mount the drive
mkdir /tmp/mnt
sudo mount /dev/loop0 /tmp/mnt
Then ls /tmp/mnt/ gives
app benchmarktest benchmarktest64 lost+found misc nativetest nativetest64

How to handle .tar.md5 files

I was wondering about how to create / extract / verify .tar.md5 files. These files are used when flashing images to android devices, see here for example.
As far as I can tell the checksum is appended to the file like this:
cp file.tar file.tar.md5
md5sum file.tar >> file.tar.md5
Firstly I would like to know how to extract the file. Can I simply use tar -xf on the file.tar.md5?
How can I verify the integrity of the file? I would like to remove the last bytes (containing the checksum) from the file to obtain the original file back. I guess you would have to use a regexp to match the checksum file.tar? Is something like this implemented somewhere already?
First of all tar -xf should work since tar continues while it matches its' packing algorithm. If the file stops matching so would tar.
Also most archive managers such as 7-zip or winrar will open it if you remove the ".md5".
They might print error regarding mismatch with the end of the file, ignore it.
As for verifying the file:
print out the stored md5sum: tail -z -n 1 [File name here].tar.md5
calculate the md5sum of the tar part of the file: head -z -n -1 [File name here].tar.md5 | md5sum
What works for me with Ubuntu 19.10 is:
download single-file 4 GiB zip from sammobile com
unzip to several *.tar.md5
run the below command-line
.
for F in *.tar.md5; do echo -n "$F " &&
EXP=($(tail --lines=1 "$F")) &&
ACT=($(head --lines=-1 "$F" | md5sum)) &&
if [ ${EXP[0]} = ${ACT[0]} ]; then echo -n "md5ok " &&
tar --extract --file "$F" && echo "done"
else echo "FAIL"; fi; done &&
unlz4 --multiple --verbose *.lz4
AP_G965U1UEU3ARL1_CL14745140_QB21029084_REV01_user_low_ship_MULTI_CERT_meta.tar.md5 md5ok done
BL_G965U1UEU3ARL1_CL14745140_QB21029084_REV01_user_low_ship_MULTI_CERT.tar.md5 md5ok done
CP_G965U1UEU3ARL1_CP11407818_CL14745140_QB21029084_REV01_user_low_ship_MULTI_CERT.tar.md5 md5ok done
CSC_OMC_OYM_G965U1OYM3ARL1_CL14745140_QB21029084_REV01_user_low_ship_MULTI_CERT.tar.md5 md5ok done
HOME_CSC_OMC_OYM_G965U1OYM3ARL1_CL14745140_QB21029084_REV01_user_low_ship_MULTI_CERT.tar.md5 md5ok done
…
But we should all try to get away from bash

Error with repacking boot.img (Android)

I have Nexus 5 with AOSP ROM 4.4.4.
I'm trying to "pull" boot.img from the device, unpack it, edit "init.rc", repack it to boot.img and flash it to the device.
I follow many guides but none of them have the same scenario as I have:
(http://droidcore.blogspot.co.il/2012/12/how-to-edit-initrc-in-android.html and http://www.digitalinternals.com/mobile/android-mmc-mmcblk-partition-layout/259/)
The device partition is mmcblck so from ls -l /dev/block/platform/msm_sdcc.1/by-name/ I got that the boot is /dev/block/mmcblk0p19:
lrwxrwxrwx root root 1971-02-28 21:30 boot ->
/dev/block/mmcblk0p19
I created the boot.img with the command: cat /dev/block/mmcblk0p19 > /mnt/sdcard/boot.img and pull this boot.img to my pc. This boot.img is ~23 MB.
I unmkbootimg the boot.img with the tool in http://droidcore.blogspot.co.il/2012/12/how-to-edit-initrc-in-android.html and got 2 files: initramfs.cpio.gz which is ~500 KB and kernel.gz which is ~8.4 MB. The details I got are:
Kernel size 8405280
Kernel address 0x8000
Ramdisk size 498992
Ramdisk address 0x2900000
Secondary size 0
Secondary address 0xf00000
Kernel tags address 0x2700000
Flash page size 2048
Board name is ""
Command line "console=ttyHSL0,115200,n8 androidboot.hardware=hammerhead
user_debug=31 maxcpus=2 msm_watchdog_v2.enable=1"
Extracting kernel.gz
... Extracting initramfs.cpio.gz ...
All done.
To recompile this image, use:
mkbooting --kernel kernel.gz --ramdisk initramfs.cpio.gz --base 0x26fff00 --cmdline 'console=ttyHSL0,115200,n8 androidboot.hardware=hammerhead user_debug=31 maxcpus=2 msm_watchdog_v2.enable=1' -o new_boot.img
I unpack initramfs.cpio.gz with the command: gunzip -c initramfs.cpio.gz | sudo sh -c 'cd ../ && cpio -i' (from http://www.wiki.xilinx.com/Build+and+Modify+a+Rootfs) and got many files, init.rc among them. I edited this file.
I repack initramfs.cpio.gz with the command: sh -c 'cd ../ && sudo find . | sudo cpio -H newc -o' | gzip -9 > new_initramfs.cpio.gz and got this file ~500KB.
Now I mkbootimg those 2 files with the command: ./mkbooting --kernel kernel.gz --ramdisk initramfs.cpio.gz --base 0x26fff00 --cmdline 'console=ttyHSL0,115200,n8 androidboot.hardware=hammerhead user_debug=31 maxcpus=2 msm_watchdog_v2.enable=1' -o new_boot.img and I got boot.img which is ~8.5 MB
Now flash the boot to the device and the device is stuck on the first screen (not boot up)
My questions are:
What's wrong?
Is that normal that my new boot.img is only 8.5 MB instead of 23 MB (the original)?
Thank you guys!
************* I FOUND THE PROBLEM *************
THE SOLUTION IS:
I used unmkbootimg from here: http://whiteboard.ping.se/Android/Unmkbootimg
After I run unmkbootimg I got this comment:
*** WARNING ****
This image is built using NON-standard mkbootimg!
OFF_KERNEL_ADDR is 0xFD908100
OFF_RAMDISK_ADDR is 0x00200100
OFF_SECOND_ADDR is 0xFE800100
Please modify mkbootimg.c using the above values to build your image.
so I follow the instructions here: https://gist.github.com/jberkel/1087757 and now it works!!!
I can't give the answer to the first question, but if I were you I'd skip step 4&5 and verify whether the unmkbootimg/mkbootimg process is correct. Only after it's verified, I till tackle with the cpio process, and finally the init.rc editing.
For the second question, google's official boot.img is 8.64MB, so I guess there is nothing wrong with the size.
Ubuntu's abootimg worked for me. I used the update (-u) option.

Categories

Resources