如何格式化SD卡



Today I went on to research the process of formatting a storage card programmatically. I decided to do this after I tried to advise someone in the MSDN forums, without clearly understanding what's involved.


After digging around for a while I came up (thanks to XDA-Developers) with the following:


int _tmain(int argc, _TCHAR* argv[])
{


STOREINFO fd = {0};
fd.cbSize = sizeof(fd);
DWORD dw = GetFileAttributes(_T("
\\StoreMgr"));
HANDLE hFind = FindFirstFileW(L"
\\StoreMgr", (LPWIN32_FIND_DATAW)&fd);


WCHAR szBuffer[255];
wcscpy(szBuffer, L"
\\StoreMgr\\");
wcscat(szBuffer, fd.szDeviceName);
HANDLE hStore = CreateFile(szBuffer, GENERIC_WRITE|GENERIC_READ, 0, NULL, OPEN_EXISTING, 0, NULL);
DWORD dwErr = GetLastError();


FindClose(hFind);


STOREINFO si = {0};
si.cbSize = sizeof(si);
PSLGetStoreInfo(hStore, &si);


BOOL bfmt = PSLDismountStore(hStore);
bfmt = PSLFormatStore(hStore);


return 0;
}


What can I tell you... Kids, don't try this at home. What I have totally forgotten is that the typical PPC device contains several flash memory stores, some of which hold the ROM (that's why you can "flash" device, right?) and some - OS snapshot (on WM5 where RAM is persisted in the flash).


Stepping in the debugger through the above I got to the line that calls PSLFormatStore. It failed. The device froze. Essentially, I cleared the ROM on the running device - typically the first step when flashing the ROM update. This is very similar to running format c: on DOS.


Soft reset - nothing (just a blue Imate boot screen)


Hard reset - nothing. It did say something about formatting the flash memory, but that was the extent of it.


Fortunately I had a ROM update sitting around and that worked.


Here is the corrected code:


#include "stdafx.h"


#define FIRST_METHOD 0xF0010000
#define APICALL_SCALE 4
#define HANDLE_SHIFT 8
#define HT_FIND 8
#define HT_FILE 7


int _tmain(int argc, _TCHAR* argv[])
{
/*
Store enumeration
STOREINFO storeinfo = {0};
storeinfo.cbSize = sizeof(STOREINFO);
HANDLE hFindStore = FindFirstFile(L"
\\StoreMgr", (LPWIN32_FIND_DATAW)&storeinfo);
while( FindNextFile(hFindStore, (LPWIN32_FIND_DATAW)&storeinfo) )
{
}
*/


WCHAR szBuffer[255];
wcscpy(szBuffer, L"
\\StoreMgr\\");
wcscat(szBuffer, L"DSK1:"); // DSK1: is hardcoded for simplicity
HANDLE hStore = CreateFile(szBuffer, GENERIC_WRITE|GENERIC_READ, 0, NULL, OPEN_EXISTING, 0, NULL);
DWORD dwErr = GetLastError();


BOOL bfmt = PSLDismountStore(hStore);
bfmt = PSLFormatStore(hStore);


STOREINFO si = {0};
si.cbSize = sizeof(si);
PSLGetStoreInfo(hStore, &si);



bfmt = PSLCreatePart(hStore, L"PART00", 0, si.snBiggestPartCreatable >> 32, si.snBiggestPartCreatable, TRUE);
HANDLE hPart = PSLOpenPartition(hStore, L"PART00");
bfmt = PSLFormatPart(hPart, 0, TRUE);
bfmt = PSLMountPartition(hPart);


PARTINFO pi = {0};
pi.cbSize = sizeof(pi);
HANDLE hFind = PSLFindFirstPartition(hStore, &pi);
while( PSLFindNextPartition(hFind, &pi) )
{
}


PSLFindClosePartition(hFind);


CloseHandle(hStore);


return 0;
}


by : http://blog.opennetcf.org/afeinman/



你可能感兴趣的:(格式化)