Recover corrupt SD card partitions - General Questions and Answers

Hi,
After a botched attempt to install LineageOS 17 on my Samsung Galaxy S5, I decided to revert to LOS16. the problem is that I had completely wiped my device in order to install LOS 17, and my only available 16.x ROM was gone, so I found another ROM on a backup site (October 2020 version).
Once installed, this version did not recognize my SD card where I had stored all my user data. Therefore I dumped the partitions on my Linux PC using dd, and let Android reformat the card.
I dumped 2 individual partitions (not the whole device with the partition table), one of 16Mb, and the other about 29Gb (it's a 32Gb SD card).
Now I want to browse my old data on the backup, but I cannot mount or fsck the partition dumps. the superblock is not recognized as ext*, fat, or even f2fs.
This is all the info I have on the 2 partitions:
Bash:
$ sudo fdisk -l klte-sdc1.img
Disk klte-sdc1.img: 16 MiB, 16777216 bytes, 32768 sectors
Units: sectors of 1 * 512 = 512 bytes
Sector size (logical/physical): 512 bytes / 512 bytes
I/O size (minimum/optimal): 512 bytes / 512 bytes
$ sudo fdisk -l klte-sdc2.img
Disk klte-sdc2.img: 29.71 GiB, 31897140736 bytes, 62299103 sectors
Units: sectors of 1 * 512 = 512 bytes
Sector size (logical/physical): 512 bytes / 512 bytes
I/O size (minimum/optimal): 512 bytes / 512 bytes
Also, for comparison, I dumped the partition table of the currently working SD card:
Bash:
$ sudo fdisk -l /dev/sdc
Disk /dev/sdc: 29.72 GiB, 31914983424 bytes, 62333952 sectors
Disk model: MicroSD/M2
Units: sectors of 1 * 512 = 512 bytes
Sector size (logical/physical): 512 bytes / 512 bytes
I/O size (minimum/optimal): 512 bytes / 512 bytes
Disklabel type: gpt
Disk identifier: 35D0FC04-06BD-4578-87FB-D6ED991C03A5
Device Start End Sectors Size Type
/dev/sdc1 2048 34815 32768 16M unknown
/dev/sdc2 34816 62333918 62299103 29.7G unknown
As you see, the partition sizes are identical. I can chance it and dd the original partitions into the new ones, but I don't want to lose recent data on the current partition. I could also corrupt the partition headers if the ones in the backups are corrupt, and be back to square 1.
Can anybody help? Which kind of partition or specialized tools shall I look for?
Thanks,
gm

Related

[Q] ROM Manager-created partitions - need to recreate corrupted part table

Hi
My sd card on my Desire has become corrupted. I have a2sd+ on there; I can start from scratch (I accept the risk of installing development ROMs!) but I could really do with getting to the photos I took this morning before the card crashed.
gpart won't find the partitions at all but there's definitely still data on the card - if I strings the card I can see the DOS directory structure.
I used ROM Manager to create a 512MB ext partition with 0MB swap; I tried creating a w95 FAT32 (lba) partition of all the disk except the last 512MB using fdisk, but the partitions won't mount when I do that.
Anyone got an idea what values I should pass to fdisk to get it back?
I have
Disk /dev/sdb: 8017 MB, 8017412096 bytes
247 heads, 62 sectors/track, 1022 cylinders
Units = cylinders of 15314 * 512 = 7840768 bytes
I figured the FAT32 part is probably first, so I calculated the size of a 512MB partition as 65 blocks and assigned the first 956 blocks to the FAT partition, with the rest as the ext partition, but it made no difference.
I tried getting RM to create a partition on a second card I have and when I look at it in fdisk it looks really weird - there's messages about "Partition 1 has different physical/logical beginnings (non-Linux?)" and "Partition 1 does not end on a cylinder boundary"
Is there a way to recreate the partitions that ROM Manager would create without wiping the existing data?
Thanks!

[Tutorial] How to manually create, align and format sd partitions

So.. You've plugged your SD to your linux box and want to created aligned partitions with fdisk?
Follow exactly what I'm doing and your partitions will be MB aligned.
At the end you'll get a FAT32 partition for your data and an ext4 partition for data2xxxx scripts.
If you need more partitions, just adapt this tutorial to you needs.
I know some scripts or recoveries do this automatically so no need to remind me
I initially posted this in SuperNova ROM thread but it might worth a separate thread.
I've done this with an old 1GB usb key just to show you the commands, you'll have to adapt to your SD depending on it's size.
When I plugged the flash drive, it got recognized by my system as /dev/sde. It can be different on your system and once again you'll have to figure-out yourself the correct device.
Here as an example I'm splitting the flash drive in 2 partitions. 1st (512Mb) will be for the fat32 partition and will have to be formatted later. The rest is the future ext4 and is about 512MB too.
I'm gonna use 2 magic parameters with fdisk, in order to get 1024Kb clusters (2048x512 bytes) and align easily the partitions. 64 heads and 32 sectors should give 64x32=2048 so that is going to be my params here
Code:
fdisk -H64 -S32 /dev/sde
Command (m for help): p
Disk /dev/sde: 1054 MB, 1054605312 bytes
64 heads, 32 sectors/track, 1005 cylinders
Units = cylinders of 2048 * 512 = 1048576 bytes
Disk identifier: 0x00000000
Device Boot Start End Blocks Id System
/dev/sde1 * 1 1005 1028043+ 6 FAT16
Partition 1 has different physical/logical beginnings (non-Linux?):
phys=(0, 3, 45) logical=(0, 7, 10)
Partition 1 has different physical/logical endings:
phys=(509, 63, 63) logical=(1004, 3, 32)
Partition 1 does not end on cylinder boundary.
So here I have an old partition on my key (sde1). I delete it first and check that there is no more partition on the drive.
Repeat the process if you have more than 1 partition on your SD.
Make sure that it's clean before proceeding to the next step.
Code:
Command (m for help): d
Selected partition 1
Command (m for help): p
Disk /dev/sde: 1054 MB, 1054605312 bytes
64 heads, 32 sectors/track, 1005 cylinders
Units = cylinders of 2048 * 512 = 1048576 bytes
Disk identifier: 0x00000000
Device Boot Start End Blocks Id System
Empty! Great! The key is clean. Let's partition it now
Code:
Command (m for help): n
Command action
e extended
p primary partition (1-4)
p
Partition number (1-4): 1
First cylinder (1-1005, default 1): 2
Last cylinder or +size or +sizeM or +sizeK (2-1005, default 1005): 512
Command (m for help): p
Disk /dev/sde: 1054 MB, 1054605312 bytes
64 heads, 32 sectors/track, 1005 cylinders
Units = cylinders of 2048 * 512 = 1048576 bytes
Disk identifier: 0x00000000
Device Boot Start End Blocks Id System
/dev/sde1 2 512 523264 83 Linux
Ok now I have a partition starting on cylinder 2 and 512Mb in size. If your SD is 16GB, you can use for instance a size of 15GB to leave 1GB for the ext4 partition.
That would give approximately 15x1024=15360 instead of 512. Got it?
Pay attention to the value below "End". Here it is 512 so I will use the next cylinder (513) for the 2nd partition:
Code:
Command (m for help): n
Command action
e extended
p primary partition (1-4)
p
Partition number (1-4): 2
First cylinder (1-1005, default 1): 513
Last cylinder or +size or +sizeM or +sizeK (513-1005, default 1005):
Using default value 1005
Command (m for help): p
Disk /dev/sde: 1054 MB, 1054605312 bytes
64 heads, 32 sectors/track, 1005 cylinders
Units = cylinders of 2048 * 512 = 1048576 bytes
Disk identifier: 0x00000000
Device Boot Start End Blocks Id System
/dev/sde1 2 512 523264 83 Linux
/dev/sde2 513 1005 504832 83 Linux
Now I have 2 partitions aligned to 1MB but declared as "linux". Let's change that to FAT32 for partition 1.
Code:
Command (m for help): t
Partition number (1-4): 1
Hex code (type L to list codes): c
Changed system type of partition 1 to c (W95 FAT32 (LBA))
Command (m for help): p
Disk /dev/sde: 1054 MB, 1054605312 bytes
64 heads, 32 sectors/track, 1005 cylinders
Units = cylinders of 2048 * 512 = 1048576 bytes
Disk identifier: 0x00000000
Device Boot Start End Blocks Id System
/dev/sde1 2 512 523264 c W95 FAT32 (LBA)
/dev/sde2 513 1005 504832 83 Linux
Command (m for help): w
The partition table has been altered!
Calling ioctl() to re-read partition table.
WARNING: If you have created or modified any DOS 6.x
partitions, please see the fdisk manual page for additional
information.
Syncing disks
That's all.
Now format 1st partition to FAT32 with this guide:
credits to kd8cgo
Code:
Making an aligned FAT32 format
Anyone can figure out alignment of the partition from many sources already posted on the web.
One thing I could not find was a guide on how to align that pesky FAT32 format so that each
cluster (allocation unit) is aligned inside the boundaries of erase blocks. Well, the Linux mkfs.vfat
utility has all the options we need to make this aligned format possible!
Please view this primer to FAT32 to get an idea of how the system is laid out on your disk.
You normally have the first sector in the partition which is your Volume ID, which sits in the
reserved space which is normally the first 32 sectors in the partition. This is followed
by (2) copies of the File Allocation Tables, which vary in length when created depending on
a variety of factors, including the chosen allocation unit size. The FAT size remains constant
after creation. Please keep in mind for this discussion the physical size of a sector, which is 512 bytes.
The Golden Nugget
We will be changing the reserved sectors from the default value of 32, to a number that
we will calculate from the reported size of the FAT tables after formatting. The goal will be to
make the FAT tables end right at a 128K boundary, so each cluster of the file system will fall
neatly within erase blocks on our disk!
First we will format our disk using FAT32 paying no mind to reserved sectors. This will
report to us our FAT size as so:
[email protected]:~$ sudo mkfs.vfat -F 32 -n UserData -s 32 -v /dev/sde1
mkfs.vfat 3.0.7 (24 Dec 2009)
/dev/sdd1 has 224 heads and 56 sectors per track,
logical sector size is 512,
using 0xf8 media descriptor, with 31309312 sectors;
file system has 2 32-bit FATs and 32 sectors per cluster.
FAT size is 7641 sectors, and provides 977937 clusters.
Volume ID is 40c250bd, volume label UserData .
The juicy bits are that 2nd to last line, it tells you the size on disk of 1 FAT table.
7641 sector FAT x 2 FATs x 512 bytes/sector = 7,824,384 bytes
The above formula shows you the exact amount of space the FAT tables are using at
the beginning of your disk. This number is not usually going to be evenly divisible by
128K (131,072 bytes) as you can see 7,824,384 / 131072 = 59.695 erase block sized
chunks. What we need to do is force the end of those FAT tables to end right at
60 blocks to do so we:
131,072 x 60 = 7,864,320 bytes in 60 erase blocks
7,864,320 - 7,824,384 = 39,936 bytes remainder
39,936 / 512 = 78 sectors remain
New reserved sector count for alignment = 78
Those are all the fundamentals required to align a FAT32 partition, so that
clusters on the disk fall in line with the erase blocks of the physical cell medium.
An example of the format command required:
sudo mkfs.vfat -F 32 -R 78 -n UserData -s 32 -v /dev/sde1
Breakdown:
sudo - super user privledges
mkfs.vfat - create a FAT file system
-F 32 - 32 bit FAT (FAT32)
-R 78 - Use 78 Reserved Sectors (instead of 32)
-n UserData - drive label, up to 11 characters
-s 32 - 32 sectors per cluster 32 x 512 - 16K allocation unit size
-v /dev/sdex - Device to format
Note: If you're getting a R=0, use 256 (256x512bytes=128Kb) as the minimum R value for FAT32 is 2.
Format 2nd partition with the following command, replacing xxx with your device
Code:
mke2fs -T ext4 -b 4096 -E stride=64,stripe-width=64,hash_alg=tea -O extent,^huge_file -m 0 -L userdata /dev/xxx2
You should be good to go now!
Reserved for later
thanks for this you absolute babe <3
Did you read post #7 by CitizenJohn a few posts below the original HOWTO: Increase write speed by 'aligning' FAT32
Tends to contradict the method posted by kd8cgo for Fat32
Presumably both methods can't be correct!
Of course I did. I read many posts about SSDs to come to this tutorial.
First of all I don't use the same geometry for creating partitions.
And so far this way of formatting has always been able to give me the best of all my flash memory based devices, SD and USB keys.
Have you tried both methods? Which of the two gave you the best results?
I'm gonna try it again, maybe I missed something the first time.
Of course if there is a proven better way to get even more from our flash drives and SDs, I'll gladly learn it and change the tutorial accordingly.
EDIT:
Posted atto results for both methods on a pretty old flash drive. Left is formatted with the post you mentioned and right is with the "standard" method.
Don't see much of a difference.
I have but quite a while apart, i didn't use any software to measure transfer speed but using a sd-card adapter according to Linux i get ~10Mbs sd-card to PC and ~8Mbs PC to sd-card with the method i mentioned can't imagine a class2 sd-card could get much faster
Damn just realized to PC was Fat partition to PC : To sd-card was to btrfs linux partition
The problem is that we don't know the actual erase block size of each flash drive.
And on top of that, fat32 has block sizes of 64K max. Max erase block size for SD is 256K but is usually less.
I also just tried both methods on a faster usb key (with write speeds up to 15Mb/s) and results are the same.
But each his own and if anybody can get a better data transfer rate with the 2nd method, please report all the details here.
No worries mate thanks for the TUT, the question came out of ignorance rather than any understanding of the methodology
ziggy1001 said:
No worries mate thanks for the TUT, the question came out of ignorance rather than any understanding of the methodology
Click to expand...
Click to collapse
Actually it was an interesting question.
We assume that for any device the erase block size is 128K
But the guy in your post is formatting also a usb flash thumb drive or an SD card.
If the erase block size of his flash drive is 16k, then his file was aligned
details:
8011776 / 131072 = 61.125 File "not aligned" according to him.
8011776 / 16384 = 489. Round number, file aligned.
To get even more speed, one has to use bigger fat32 blocks (up to 64k). But then each file will use blocks of 64Ko on the drive, even if it's real size is 1Ko !
Good compromises are 16 and 32Ko blocs.
... Or you can use GParted.
problem solved.
Terepin said:
... Or you can use GParted.
problem solved.
Click to expand...
Click to collapse
Could be a good idea but:
1/ Some people have reported issues with Gparted when aligning their partitions and even some 16GB SDs were not detected by the Desire after using Gparted. On the opposites, the phone could detect them by doing it manually.
2/ Unless I'm mistaken, Gparted is NOT optimizing the FAT32 formatting process nor the EXT4 one.
is there really a difference between Gparted live and your method ?
when you wipe your SDext with your recovery, does sdext returns to default value ?
t-minik said:
is there really a difference between Gparted live and your method ?
when you wipe your SDext with your recovery, sdext returns to default value ?
Click to expand...
Click to collapse
Yes there is: I don't think that Gparted is using 1024Ko cylinders.
But most of the time, choosing MB alignment is working to align partitions. However, it is not aligning the filesystem. And like I already said, some people reported issues, that's why I published this tutorial.
If Gparted is working for you, keep using it
yep, GP works ..... but I'm really curious
and what happens when wiping SDEXT ?
If you change -s to 128 does that give 64k block size?
sudo mkfs.vfat -F 32 -R 78 -n UserData-s 32 -v /dev/sde1
Well whatever it was transfer Fat32 to PC went from ~10Mbs to ~16Mbs
If you change -s to 128 does that give 64k block size?
sudo mkfs.vfat -F 32 -R 78 -n UserData-s 32 -v /dev/sde1
Well whatever it was transfer Fat32 to PC went from ~10Mbs to ~16Mbs
Click to expand...
Click to collapse
yep, s128 gives 64k blocks.
Did you calculate R78 with the given method?
Happy that you're getting higher speed, but usually this method goal is to increase write speed to the SD
Sent from my A70S using the XDA mobile application powered by Tapatalk
Does 4ext recovery does this aligned partitioning/formatting?
Great thread, Sibere.. Will modify my installation steps to point to this TUT than Sd formatter
Does 4ext recovery does this aligned partitioning/formatting?
Click to expand...
Click to collapse
According to the dev, it should. Didn't try myself though. but like Gparted, it won't optimze the FAT32 formatting part.
Sent from my A70S using the XDA mobile application powered by Tapatalk
LiooO said:
Does 4ext recovery does this aligned partitioning/formatting?
Click to expand...
Click to collapse
sibere said:
According to the dev, it should. Didn't try myself though. but like Gparted, it won't optimze the FAT32 formatting part.
Sent from my A70S using the XDA mobile application powered by Tapatalk
Click to expand...
Click to collapse
So according to your answer i can partiton SD card in 4Ext recovery or GParted and then just do Fat32 formatting according to your guide? Can someone confirm this pls?

Basic question about flashing nandroid backups

I want to know that when we flash specific partition images (say system.img, data.img, cache.img, etc.), then how does the software know where exactly to put those partition bytes? For example, if I flash system.img, which CHS/LBA sector will it consider as beginning of that block? If it is based on the MBR/EBR1 tables, what will happen if I'm flashing the MBR/EBR1 too?
The reason I'm asking is that I want to change the partitioning in EBR1 slightly, so that more space is allocated to `/data` partition instead of `/sdcard`. I've got an old but good conditioned MediaTek (MTK-6577) based smart-phone called Karbonn-A30 which is great in build quality and almost every other aspect, but it only has 500MB of Internal Storage (`/data` partition in Linux lingo) which is not good enough for apps. Presently, the MBR and EBR1 partitions are thus:
$disktype MBR
--- MBR
Regular file, size 512 bytes
DOS/MBR partition map
Partition 1: 2.000 TiB (2199023255040 bytes, 4294967295 sectors from 1024)
Type 0x05 (Extended)
Partition 2: 10 MiB (10485760 bytes, 20480 sectors from 18432)
Type 0x83 (Linux)
Partition 3: 10 MiB (10485760 bytes, 20480 sectors from 38912)
Type 0x83 (Linux)
Partition 4: 650 MiB (681574400 bytes, 1331200 sectors from 113152)
Type 0x83 (Linux)
$disktype EBR1
--- EBR1
Regular file, size 512 bytes
DOS/MBR partition map
Partition 1: 376 MiB (394264576 bytes, 770048 sectors from 1443328)
Type 0x83 (Linux)
Partition 2: 1.293 GiB (1388314624 bytes, 2711552 sectors from 2213376)
Type 0x83 (Linux)
Partition 3: 1.998 TiB (2196501691904 bytes, 4290042367 sectors from 4924928)
Type 0x83 (Linux)
Why this last partition which corresponds to `/sdcard` is left so large (1.998 TiB) is beyond my understanding! Since there is a good 2.5GB of space available on my actual `/sdcard` partition, I was thinking if I can alter the EBS1 and change the LBA addressing so that the third partition starts from 2097152 additional sectors (which comes to 1024MB or 1GB which is good enough for me), will it automatically increase my `/data` partition by 1GB and decrease the `/sdcard` by 1GB correspondingly? On XDA and other forums, I've read that people have successfully done this mod and achieved the change in partition sizes, but I first want to understand how it happens.

Question about flashing nandroid backups

I want to know that when we flash specific partition images (say system.img, data.img, cache.img, etc.), then how does the software know where exactly to put those partition bytes? For example, if I flash system.img, which CHS/LBA sector will it consider as beginning of that block? If it is based on the MBR/EBR1 tables, what will happen if I'm flashing the MBR/EBR1 too?
The reason I'm asking is that I want to change the partitioning in EBR1 slightly, so that more space is allocated to /data partition instead of /sdcard. I've got an old but good conditioned MediaTek (MTK-6577) based smart-phone called Karbonn-A30 which is great in build quality and almost every other aspect, but it only has 500MB of Internal Storage (/data partition in Linux lingo) which is not good enough for apps. Presently, the MBR and EBR1 partitions are thus:
$disktype MBR
--- MBR
Regular file, size 512 bytes
DOS/MBR partition map
Partition 1: 2.000 TiB (2199023255040 bytes, 4294967295 sectors from 1024)
Type 0x05 (Extended)
Partition 2: 10 MiB (10485760 bytes, 20480 sectors from 18432)
Type 0x83 (Linux)
Partition 3: 10 MiB (10485760 bytes, 20480 sectors from 38912)
Type 0x83 (Linux)
Partition 4: 650 MiB (681574400 bytes, 1331200 sectors from 113152)
Type 0x83 (Linux)
$disktype EBR1
--- EBR1
Regular file, size 512 bytes
DOS/MBR partition map
Partition 1: 376 MiB (394264576 bytes, 770048 sectors from 1443328)
Type 0x83 (Linux)
Partition 2: 1.293 GiB (1388314624 bytes, 2711552 sectors from 2213376)
Type 0x83 (Linux)
Partition 3: 1.998 TiB (2196501691904 bytes, 4290042367 sectors from 4924928)
Type 0x83 (Linux)
Click to expand...
Click to collapse
Why this last partition which corresponds to /sdcard is left so large (1.998 TiB) is beyond my understanding! Since there is a good 2.5GB of space available on my actual /sdcard partition, I was thinking if I can alter the EBS1 and change the LBA addressing so that the third partition starts from 2097152 additional sectors (which comes to 1024MB or 1GB which is good enough for me), will it automatically increase my /data partition by 1GB and decrease the /sdcard by 1GB correspondingly? On XDA and other forums, I've read that people have successfully done this mod and achieved the change in partition sizes, but I first want to understand how it happens.

SD Card access

I've made this mistake before, and it drives me nuts. I changed phones and put my old phones sd card in my new phone (Nokia 8.3). I was just going to see if it could read it. It couldn't and asked me if I wanted to format. I selected no, and removed the card. I put the card in my linux computer and I can't mount it. I did get a replacement for my old phone through warranty (teracube) and tried putting it in that, and it wouldn't read it either. I've had this problem before. I think even having the sdcard in a phone does something to it even if you don't format it. I think I formatted the card initially as ext2 but I could be mistaken.
Any help would be appreciated. I really want to recover the data.
mount:
Code:
[3630 21-01-23 7:06][email protected]/home/larry%sudo mount /dev/sda2 /mnt/tmp
NTFS signature is missing.
Failed to mount '/dev/sda2': Invalid argument
The device '/dev/sda2' doesn't seem to have a valid NTFS.
Maybe the wrong device is used? Or the whole disk instead of a
partition (e.g. /dev/sda, not /dev/sda1)? Or the other way around?
[3630 21-01-23 7:06][email protected]/home/larry%sudo mount -tvfat /dev/sda2 /mnt/tmp
mount: /mnt/tmp: wrong fs type, bad option, bad superblock on /dev/sda2, missing codepage or helper program, or other error.
fdisk:
Code:
[3630 21-01-23 7:06][email protected]/home/larry%sudo fdisk /dev/sda
Command (m for help): p
Disk /dev/sda: 119.1 GiB, 127865454592 bytes, 249737216 sectors
Units: sectors of 1 * 512 = 512 bytes
Sector size (logical/physical): 512 bytes / 512 bytes
I/O size (minimum/optimal): 512 bytes / 512 bytes
Disklabel type: gpt
Disk identifier: C8FBA780-1A1F-4B71-8D9D-D623A671C61D
Device Start End Sectors Size Type
/dev/sda1 2048 34815 32768 16M unknown
/dev/sda2 34816 249737182 249702367 119.1G unknown
An SD-card ( from Android's point of view it's a portable USB-device ) usually is FAT32 formatted, AFAIK, but I may err as always ...

Categories

Resources