Please ignore my numerous references to wasps and bees. I have issues.
http://code.google.com/p/samsung-firmware-tools/source/detail?r=f538001058
Modified:
/WCS.FatSharp/Structures/BootSector.cs
=======================================
--- /WCS.FatSharp/Structures/BootSector.cs Tue Jul 20 01:02:24 2010
+++ /WCS.FatSharp/Structures/BootSector.cs Wed Jul 21 00:38:08 2010
@@ -21,6 +21,7 @@
using System;
using System.Collections.Generic;
using System.Text;
+using System.IO;
namespace WCS.FatSharp.Structures
{
@@ -98,7 +99,7 @@
//Now, For the strings...
Array.Copy(GetFSString(bs.OEMName, 8), 0, ret, 0x03, 8);
Array.Copy(GetFSString(bs.FSType, 8), 0, ret, 0x36, 8);
- Array.Copy(GetFSString(bs.VolLabel, 11), 0, ret, 0x2b,11);
+ Array.Copy(GetFSString(bs.VolLabel, 11), 0, ret, 0x2b, 11);
//And finally, All ushorts and uints
//Array.Copy(BitConverter.GetBytes(bs.BytesPerSector), 0, ret,
0x0B, 2);
GetAndCopy(bs.BytesPerSector, ret, 0x0B);
@@ -133,7 +134,7 @@
}
public static byte[] GetFSString(string input, int length,
Encoding e)
{
- byte[] output = e.GetBytes(input) ;
+ byte[] output = e.GetBytes(input);
byte[] ret = new byte[length];
if (output.Length >= length)
Array.Copy(output, 0, ret, 0, length);
@@ -148,6 +149,44 @@
}
return ret;
}
+ public static BootSector Load(FileStream fstream)
+ {
+ // If we cant seek... I pray to god we are being
+ // fed good data.... Or else the wasps that
+ // stare at me at work may get you.
+ if (fstream.CanSeek)
+ fstream.Seek(0, SeekOrigin.Begin);
+ // They know of me as the Wasp murderer.
+ // They plot my destruction.
+ byte[] bsBuffer = new byte[512];
+ if (fstream.Read(bsBuffer, 0, 512) != 512)
+ {
+ //Ah fuck, THE WASPS ARE ANGRY.
+ //We cant continue without 512 bytes.
+ //So we just bitch and bail, Let the
+ //Caller deal with the problem. They
+ //Should have fed us a good stream anyway.
+ throw new InvalidOperationException("Unable to read entire
boot sector from stream!");
+ }
+ //You have successfully escaped the wasps.
+ //We should not close the stream though, Less we anger the
bee's.
+ //Although less harmful than the wasps, They can still
+ //Cause problems!
+ return Create(bsBuffer);
+ }
+ public static BootSector Load(string filename)
+ {
+ FileInfo fi = new FileInfo(filename);
+ //Don't bother error checking, Any exceptions
+ //Will be passed up the call chain.
+ //They can deal with any bee's or wasps they
+ //Decide to anger.
+ FileStream fs = fi.Open(FileMode.Open, FileAccess.Read,
FileShare.Read);
+ BootSector bs = Load(fs);
+ //We opened it, The bee's won't get angry if we close it. :)
+ fs.Close();
+ return bs;
+ }
public static BootSector Create(byte[] block)
{
BootSector ret = new BootSector();
@@ -155,8 +194,8 @@
throw new InvalidOperationException("Array must be >= 512
bytes");
//Ok here we go... Shitty code to come!
Array.Copy(block, 0, ret.jmpInstruction, 0, 3);
- ret.OEMName = BytesToASCIIString(block,0x03,8);
- ret.BytesPerSector = BytesToUShort(block,0x0B);
+ ret.OEMName = BytesToASCIIString(block, 0x03, 8);
+ ret.BytesPerSector = BytesToUShort(block, 0x0B);
ret.SectorsPerCluster = block[0x0D];
ret.ReservedSectorCount = BytesToUShort(block, 0x0E);
ret.TotalFats = block[0x10];
@@ -221,8 +260,7 @@
/// <returns>Sector position of requested cluster</returns>
public long GetFileStartSector(long cluster)
{
- return ReservedSectorCount +
- (TotalFats * SectorsPerFAT) +
+ return RootDirectorySector +
((MaxRootEntries * 32) / BytesPerSector) +
((cluster - 2) * SectorsPerCluster);
}
@@ -235,5 +273,47 @@
{
return GetFileStartSector(cluster) * BytesPerSector;
}
+ /// <summary>
+ /// Contains the sector location of the root directory
+ /// as described by this boot sector. Multiply by sector
+ /// size to get the actual byte position.
+ /// </summary>
+ public long RootDirectorySector
+ {
+ get
+ {
+ return ReservedSectorCount + (TotalFats * SectorsPerFAT);
+ }
+ }
+ /// <summary>
+ /// Contains the exact byte number where the root directory is
located.
+ /// </summary>
+ public long RootDirectoryBytePosition
+ {
+ get { return RootDirectorySector * BytesPerSector; }
+ }
+ /// <summary>
+ /// Calculate the checksum of a given string.
+ /// </summary>
+ /// <param name="text">String to calculate checksum of</param>
+ /// <param name="e">Encoding to use when calculating
checksum</param>
+ /// <returns>The calculated checksum</returns>
+ public static byte FatCheckSum(string text, Encoding e)
+ {
+ byte[] str = e.GetBytes(text);
+ byte sum = 0;
+ for(int x = 0; x < str.Length; x++)
+ {
+ //We WANT this to overflow. So, Yeah
+ //Remove any over flow checking.
+ //I can hear wasps buzzing in the distance.
+ unchecked
+ {
+ sum = (byte)((sum >> 1) + ((sum & 1) << 7));
+ sum += str[x];
+ }
+ }
+ return sum;
+ }
}
}