Skip to content

Instantly share code, notes, and snippets.

@landaire
Created June 17, 2013 23:03
Show Gist options
  • Save landaire/5801279 to your computer and use it in GitHub Desktop.
Save landaire/5801279 to your computer and use it in GitHub Desktop.
Terrible party buffalo code
void ProcessBootSector()
{
Exception InvalidSectorsPerCluster = new Exception("FATX: found invalid sectors per cluster");
Exception InvalidBytesPerSector = new Exception("FATX: invalid bytes per cluster for growable file partition");
Exception TooSmallClusterSize = new Exception("FATX: found too small of cluster size");
Exception VolumeTooSmall = new Exception("FATX: volume too small to hold the FAT");
Exception TooManyClusters = new Exception("FATX: too many clusters");
// r31 = r3 + 0xa8
long r31 = 0x3A1E69E8;
// move r5 in to r28
long r28 = PartitionSize();//0xD0EA6000;//PartitionSize(); //0x0000000004000000 UNCOMMENT HERE
// Set r10
long r10 = 0x1000;
// Set r30 to sp + 0x50
long r30 = 0;
long SP50 = r30;
// Set r11
long r11 = 0x200; // Drive sector size?
// Set r9
long r9 = r31;
r11 += 0xFFF;
// Set r29
long r29 = (r11 & 0xFFFFF000);
r11 = 0;
long r26 = 0;
if (!(r29 <= 0x1000))
{
// Move r29
r10 = r29;
}
// Load long at r28
r11 = r28; // partition size
long r25 = (r10 & 0xFFFFFFFF);
if (r25 < r11)
{
// BRANCH
//long r7 = 0 /* Should be SP + 0x50, but that's 0. */, r5 = 0, r6 = 0;
long r3 = r31;
// Call fsc map buffer here, but we're not going to
r10 = 0;
r9 = 0;
r3 = 0;
if (r3 == 0)
{
r9 = 0x58544146; // FATX ("XTAF") magic
r11 = Magic();
if ((uint)r9 == Magic())
{
// Read sectors per cluster
r11 = SectorsPerCluster();//0x20;//SectorsPerCluster(); // UNCOMMENT HERE
if (r11 == 0)
{
throw InvalidSectorsPerCluster;
return;
}
if(!(r11 <= 2) && (r11 != 0x4) && (r11 != 0x8) && (r11 != 0x20) && (r11 != 0x40) && (r11 != 0x80))
{
throw InvalidSectorsPerCluster;
}
r9 = 9; // 9 because 0x1000 / 8 = 0x200, 0x200 = sector size
// Shift
r11 = r11 << (int)r9; // cluster size
long r31CC = r11;
if (r11 == 0)
{
// what
r11 = 0;//r30;
}
else // Usually branches here
{
r11 = cntlzw((uint)r11);
r11 = 0x1F - r11;
}
long r31D1 = r11;
if (r26 != 0) // Usually skips this
{
r11 = r31CC;
if (r11 != 0x4000)
{
throw InvalidBytesPerSector;
}
}
r11 = PartitionID();
long r31160 = r11;
r3 = SP50; // becomes pointer to FATX partition
long r27 = RootDirectoryCluster();
// FscUnmapBuffer
SP50 = r30;
r11 = r31CC;
if (r11 < r29)
{
throw TooSmallClusterSize;
}
r9 = (byte)r31D1;
r11 = r28;
r11 = (r11 >> (int)r9);
r11++;
// Entry size?
if (r11 < 0x0000FFF0 && r26 == 0)
{
// Should branch here
r10 = 1;
r11 = ((long)r11 << 1);
}
else if (r11 >= 0x0000FFF0)
{
r10 = 2;
r11 = ((long)r11 << 2);
}
r11 += r29;
long r31D2 = r10;
long r8 = r29 - 1;
r10 = (long)r28;
r11--;
r10 = r10 - r25;
// And with complement
r11 = r11 & ~r8;
r11 &= 0xFFFFFFFF;
if (r11 >= r10)
{
throw VolumeTooSmall;
}
r10 -= r11;
r9 &= 0xFFFFFFFFFFFFFF;
r8 = 0x000000000FFFFFFF;
r10 >>= (int)r9;
long r31C8 = clusters = (uint)r10;
if (r10 >= r8)
{
throw TooManyClusters;
}
Console.WriteLine(Partition.Offset.ToString("X") + " Clusters " + r10.ToString("X"));
fatsize = r11;
Console.WriteLine("r11 " + r11.ToString("X"));
r11 += r25;
Console.WriteLine("r11 " + r11.ToString("X"));
long r31D4 = r25;
r10 = r25 & 0xFFF;
Console.WriteLine("r25 " + r25.ToString("X") + "\r\nr10 " + r10.ToString("X"));
r11 &= 0xFFF;
Console.WriteLine("r11 " + r11.ToString("X"));
//fatsize = r11.UpToNearestCluster(r31CC);
r11 = r31CC;
Console.WriteLine("r11 " + r11.ToString("X"));
r11 &= 0xFFF;
Console.WriteLine("r11 " + r11.ToString("X"));
}
}
else
{
// BRANCH
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment