|
本帖最后由 chiannet 于 2015-4-23 14:58 编辑
function MOUNT_part(phyNumber: integer): boolean;
var
k:integer;
begin
result:=false;
k:=0;
if phyNumber>=0 then repeat
WinExecAndWait32(PECMD+' LOAD '+windir+'\MOUNTHI_END2W.ini '+inttostr(phyNumber)+'#4 W: ro',0);
IF not(fileEXists('W:\usbOS.VER') or fileEXists('W:\usbzl.FLG')) then
WinExecAndWait32(PECMD+' LOAD '+windir+'\MOUNTHI_END2W.ini '+inttostr(phyNumber)+'#3 W: ro',0);
IF fileEXists('W:\usbOS.VER') or fileEXists('W:\usbzl.FLG') then
result:=true
else begin
inc(k);
sleep(400);
// WinExecAndWait32(windir+'\SYSTEM32\BOOTPART.EXE -eject W:' ,0);
end;
until (k>=3) or result;
end;
Procedure MOUNTHI_END2W;
var
k:integer;
BEGIN
AList:=TStringList.Create;
with AList do begin
add('envi in1=%~1'); // 源物理磁盘号#分区号
add('envi in2=%~2'); // 挂载的目标盘符:字母带冒号结尾的
add('envi in3=%~3'); // 只读:ro 可写:rw
add('MSTR -delims:# n_hd=<1>%in1%');
add('MSTR -delims:# par=<2>%in1%');
add('FIND $%in3%=ro, envi &&rw=0!envi &&rw=1');
add('GetMbrPt %n_hd%#%par% &&part_info');
add('MSTR par.active=<1>%part_info%');
add('MSTR par.fs=<2>%part_info%');
add('MSTR par.start=<3>%part_info%');
add('MSTR par.len=<4>%part_info%');
add('FIND |%par.active%=0,!exit'); //不是USBZL或USBOS,退出
add('FIND |%par.fs%=0,exit'); //类型=0,返回
add('FIND |%par.len%=0,exit'); //长度=0,返回
add('PART -admin list part %n_hd%#%par%, &&part_info');
add('MSTR par.letter=<8>%part_info%');
add('FIND $%par.letter%=,!exit'); //盘符不为空,退出,避免重复挂载
add('RAMD ImDisk*&&V*%par.start%s*%par.len%s*%rw%*\\.\PhysicalDrive%n_hd% -a -o hd -m %in2%'); //扇区映射
add('ifex %in2%\USBZL.FLG,!ifex %in2%\USBOS.VER,!Call UNMOUNT');
add('_SUB GetMbrPt');
add('MSTR -delims:# n_hd=<1>%~1');
add('MSTR -delims:# n_par=<2>%~1');
add('SET &nm=\\.\PhysicalDrive%n_hd%');
add('CALC &&addr=0x1BE+16*(%n_par%-1)');
add('GETF %nm%,%&addr%#1,&&pt.flag');
add('CALC &&addr=0x1C2+16*(%n_par%-1)');
add('GETF %nm%,%&addr%#1,&&pt.type');
add('CALC &&addr=0x1C6+16*(%n_par%-1)');
add('GETF# %nm%,%&addr%#8,&&sn1');
add('SET?long &sn1=&&pt.start:0');
add('SET?long &sn1=&&pt.len:4');
add('ENVI ret=%pt.flag% %pt.type% %pt.start% %pt.len%');
add('ENVI-ret %~2=%ret%');
add('_END');
add('_SUB UNMOUNT');
add('RAMD Imdisk*&&V3 -d -u %in2%');
add('RAMD Imdisk*&&V3 -D -u %in2%');
add('RAMD Imdisk*&&V3 -R -u %in2%');
add('_END');
SaveToFile(windir+'\MOUNTHI_END2W.ini');
Clear;
end;
p1:='';
for i:=65 to 90 do
if DiskInDrive(chr(i)) then
if GetDriveType(Pchar(chr(i)+':\')) = DRIVE_REMOVABLE then
if MOUNT_part(GetVolumeInfo(chr(i)).DiskNumber) then begin p1:='YES'; break; end;
//以上第一轮 ,识别为可移动设备的物理盘优先,若这一轮没挂到,进行下一轮
IF p1<>'YES' THEN BEGIN
for i:=65 to 90 do
if DiskInDrive(chr(i)) then begin
k:=GetVolumeInfo(chr(i)).DiskNumber; //获取盘符所在的物理盘号。
//为避免挂本地硬盘,我们推定U盘上不应存在以下这些目录或文件,把存在这些文件的分区所在的“物理盘号”列入不予挂载黑名单
if DirectoryExists(chr(i)+':\Windows\System32\drivers') or fileEXists(chr(i)+':\hiberfil.sys') or DirectoryExists(chr(i)+':\Users\All Users\Microsoft') then
if AList.IndexOf(inttostr(k))<0 then AList.Add(inttostr(k));
end;
DList:=TStringList.Create;
with DList do begin
p1:=windir+'\DISKPH70.TXT';
Add('part LIST disk,&&dsks');
Add('FILE '+P1);
Add('WRIT '+P1+',$1,%dsks%');
SaveToFile(windir+'\MOUNTHIEND2W.ini');
WinExecAndWait32(PECMD+' LOAD '+windir+'\MOUNTHIEND2W.ini',0);
if fileexists(p1) then begin LoadFromFile(p1); deletefile(p1); end;
deletefile(windir+'\MOUNTHIEND2W.ini');
if Count>0 then
for I := 0 to Count - 1 do begin
KeyList:= TStringList.Create;
ExtractStrings([' '], [' ','#','.'], PChar(DList), KeyList);
if KeyList.Count>0 then
for k := 0 to KeyList.Count - 1 do
if AList.IndexOf(KeyList[k])<0 then if MOUNT_part(k) then begin p1:='YES';break; end; //不在不予挂载黑名单 “物理盘号”,就试着挂载
KeyList.Free;
if p1='YES' then break;
end;
free;
end;
AList.Free;
END;
deletefile(windir+'\MOUNTHI_END2W.ini');
END;
由于不太习惯PECMD编程,用delphi 写了上述代码,测试蛮好,无论在VM虚拟机(U盘被识别为本地硬盘)还是实机,均能正确挂载。 |
|