#code=936T950
IFEX F.LOG, LOGS **t=1 * f.LOG
_SUB UD2FS  //Dev  [bClearOldFile=1] [bUDFile=1]  
    SET &DEV=%~1
    FIND $%&DEV%=,EXIT
    SET &bClearOldFile=%~2
    FIND $%bClearOldFile%=, SET bClearOldFile=1
    SET &bUDFile=%~3
    FIND $%bUDFile%=, SET bUDFile=1

    MOUN -udm listud  -ret: &&V -udmask:  0x120000 "%&Dev%" <>
    //0x2FF00 "<udext.norm>" 0x4000 629145600 0 00 00 00 00 00
    MSTR &&flag,&&offs,&&lenb=<1><3><4>%&V%
    IFEX $%&flag%<0xFF00, EXIT
    TEAM FDRV *idlec *rsort &Drvs=*:| MSTR &&Drv1=<1>%&Drvs%
    FIND $%&Drv1%=,EXIT

    //整个UD扩展区，创建为一个文件
    IFEX $%bClearOldFile%>0, ClearUDExt   "%&DEV%"
    IFEX $%bUDFile%>0, MOUN -udm upud  -ret: &&V  -udmask: 0x41  %&DEV%   udext.img  %&offs%s  %&lenb%

    //格式化FS
    RAMD ImDisk*&V*%&offs%s*%&lenb%*0x101*%&Dev%  -a  -o hd -m %&Drv1%      //扇区映射  
    SET &FS=FAT
    IFEX $%&lenb%>2040M, SET FS=FAT32  //NTFS
    IFEX %&Drv1%,SET &T=2000! SET &T=0  //超时
    LOOP #%&T%>0, TEAM DFMT %&Drv1%,%&FS%,UDEXT| IFEX %&Drv1%\NUL, EXIT LOOP| WAIT 500| CALC &T=%&T%-500
    RAMD ImDisk* -D -m %&Drv1%
_END

_SUB ClearUDExt  //Dev
    SET &DEV=%~1
    FIND $%&DEV%=,EXIT
    MOUN -udm  listud  -ret: &&udlist -udmask: 1 %&DEV%
    FORX  *NL &udlist,&&vi,
    {*  MSTR &&nmi=<2>%vi%
        MOUN -udm upud  -ret: &&V2  -udmask: 0  %&DEV%   %nmi%
    }
_END


UD2FS \\.\PhysicalDrive1  1  1


MOUN  -udm  -ret: &V -mall -mhide1 -udfs -udm-  \\.\PhysicalDrive1  VUTSRQPONMLKIBA
MESS. %&V%
