unit BiosEditMainForm;

interface

uses
  Winapi.Windows, Winapi.Messages,
  Winapi.ShellAPI,
  System.SysUtils, System.Variants, System.Classes,
  Vcl.Graphics,  Vcl.Controls, Vcl.Forms, Vcl.Dialogs, Vcl.StdCtrls,
  Vcl.ComCtrls,
  unitMF, Vcl.Buttons;
type
  TMF = class(TForm)
    PC1: TPageControl;
    TabSheet1: TTabSheet;
    TabSheet2: TTabSheet;
    TxtMeRegion: TMemo;
    TxtBiosRegion: TMemo;
    TxtFileName: TMemo;
    LBBiosFile: TLabel;
    LBBiosMe: TLabel;
    LBBiosBody: TLabel;
    Label1: TLabel;
    Label2: TLabel;
    Label3: TLabel;
    Label4: TLabel;
    Label5: TLabel;
    Label6: TLabel;
    TxtMeBB: TEdit;
    TxtMeStr: TEdit;
    TxtMeStart: TEdit;
    TxtMeLen: TEdit;
    TxtMeLenMB: TEdit;
    TxtRMeLen: TEdit;
    TxtBStart: TEdit;
    TxtBLen: TEdit;
    TxtBLenMB: TEdit;
    TxtRBLen: TEdit;
    CmdGetMeRegion: TSpeedButton;
    CmdGetBiosRegion: TSpeedButton;
    CmdSaveNewBiosFile: TSpeedButton;
    LstFix: TListBox;
    Label7: TLabel;
    Label8: TLabel;
    Label9: TLabel;
    Label10: TLabel;
    Label11: TLabel;
    Label13: TLabel;
    CmdOpenFolder: TSpeedButton;
    Label14: TLabel;
    Label15: TLabel;
    Label17: TLabel;
    Label18: TLabel;
    Label19: TLabel;
    CmdRunFix: TSpeedButton;
    CmdRefLstFit: TSpeedButton;
    TxtSN: TEdit;
    Label12: TLabel;
    Label16: TLabel;
    Label20: TLabel;
    Label21: TLabel;
    Label22: TLabel;
    Label23: TLabel;
    TabSheet3: TTabSheet;
    TabSheet4: TTabSheet;
    Label24: TLabel;
    TxtFileLen: TEdit;
    BiosName: TMemo;
    Label25: TLabel;
    TxtSplitLen: TEdit;
    LstSplits: TListBox;
    CmdSplit: TButton;
    Label26: TLabel;
    LstFiles: TListBox;
    CmdMix: TButton;
    CmdClsLstFiles: TButton;
    CmdClsSplit: TButton;
    Edit1: TEdit;
    CmdCls: TSpeedButton;
    procedure FormCreate(Sender: TObject);
    procedure TxtMeLenChange(Sender: TObject);
    procedure TxtBLenChange(Sender: TObject);
    procedure TxtMeLenMouseLeave(Sender: TObject);
    procedure TxtMeLenMouseEnter(Sender: TObject);
    procedure TxtBLenMouseEnter(Sender: TObject);
    procedure TxtBLenMouseLeave(Sender: TObject);
    procedure CmdGetMeRegionClick(Sender: TObject);
    procedure CmdGetBiosRegionClick(Sender: TObject);
    procedure CmdSaveNewBiosFileClick(Sender: TObject);
    procedure TxtMeRegionDblClick(Sender: TObject);
    procedure TxtBiosRegionDblClick(Sender: TObject);
    procedure TxtFileNameDblClick(Sender: TObject);
    procedure CmdOpenFolderClick(Sender: TObject);
    procedure CmdRunFixClick(Sender: TObject);
    procedure CmdRefLstFitClick(Sender: TObject);
    procedure TxtSNDblClick(Sender: TObject);
    procedure CmdMixClick(Sender: TObject);
    procedure CmdSplitClick(Sender: TObject);
    procedure CmdClsLstFilesClick(Sender: TObject);
    procedure CmdClsSplitClick(Sender: TObject);
    procedure CmdClsClick(Sender: TObject);

  private
    { Private declarations }
    procedure WMDROPFILES(var Msg: TMessage); message WM_DROPFILES;
    function IsPointInControl(Control: TControl; Point: TPoint): Boolean;
    procedure ProcessDropOnControl(Ctrl: TMemo; const FileName: string; var FileVar: string);
    procedure ProcessDropOnControlList(Ctrl:TlistBox; const FileName: string);
    procedure DoIt();
    procedure DoMe();
    procedure DoBios();
  public
    { Public declarations }
  end;

var
  MF: TMF;
  BiosFileNM,MeRegionFile,BiosRegionFile:string;
  BiosNameSplit:string;    //+++++++++++++++++++++
implementation

{$R *.dfm}

procedure TMF.FormCreate(Sender: TObject);
begin

  DragAcceptFiles(Handle, True);

  txtfilename.Text :=sLineBreak+' 拖入'+sLineBreak+' BIOS文件';
  txtmeregion.Text :=sLineBreak+' 拖入'+sLineBreak+' MeRegion文件';
  txtbiosregion.Text:=sLineBreak+' 拖入'+sLineBreak+' BiosRegion文件';
  txtmebb.Clear ;   txtmestr.Clear;
  txtmestart.Clear; txtmelen.Clear ; txtmelenmb.Clear;txtrmelen.Clear;
  txtbstart.Clear;  txtblen.Clear ;  txtblenmb.Clear;txtrblen.Clear;
  lbbiosfile.Caption:=''; lbbiosme.Caption :=''; LbBiosBody.Caption :='';
  txtsn.Clear;

  lstfiles.Clear;      //+++++++++++++++++++++
  BiosName.Clear;
  LstSplits.Clear;

  mf.Left :=(Screen.Width -mf.Width ) div 2;
  mf.Top :=(Screen.Height -mf.Height )div 2;

  unitMF.ListToolFolders(LstFix);
  PC1.TabIndex :=0;
end;

function TMF.IsPointInControl(Control: TControl; Point: TPoint): Boolean;
var
  ControlRect: TRect;
begin
  ControlRect := Rect(Control.Left, Control.Top, Control.Left + Control.Width, Control.Top + Control.Height);
  ControlRect := Control.Parent.ClientToScreen(ControlRect);
  ControlRect := Self.ScreenToClient(ControlRect);
  Result := PtInRect(ControlRect, Point);           // 判断点是否在控件区域内
end;

procedure TMF.ProcessDropOnControl(Ctrl: TMemo; const FileName: string; var FileVar: string);
begin
  if not Assigned(Ctrl) then Exit;
  FileVar := FileName;
end;

procedure TMF.ProcessDropOnControlList(Ctrl: TListBox; const FileName: string);       //+++++++++++++++++++++
begin
  if not Assigned(Ctrl) then Exit;
end;


procedure TMF.TxtBiosRegionDblClick(Sender: TObject);
begin
 lbbiosbody.Caption :='';
 BiosRegionFile:='';
 TxtRBLen.Clear;
end;

procedure TMF.TxtBLenChange(Sender: TObject);
begin
if txtBlen.Text <>'' then begin
   txtBlenMB.Text :=unitmf.BytesToStr(strtoint(TxtBLen.Text) );
end;
end;

procedure TMF.TxtBLenMouseEnter(Sender: TObject);
begin
  if isnumber(txtblen.Text )=true then begin
     txtblen.ShowHint :=true;
     txtblen.Hint :=IntToHex(StrToInt(TxtbLen.Text), 8);;
  end;
end;

procedure TMF.TxtBLenMouseLeave(Sender: TObject);
begin
   txtBlen.Hint :='';
end;

procedure TMF.TxtFileNameDblClick(Sender: TObject);
begin
  txtmebb.Clear ;   txtmestr.Clear;
  txtmestart.Clear; txtmelen.Clear ; txtmelenmb.Clear;txtrmelen.Clear;
  txtbstart.Clear;  txtblen.Clear ;  txtblenmb.Clear;txtrblen.Clear;
  lbbiosfile.Caption:=''; lbbiosme.Caption :=''; LbBiosBody.Caption :='';
  BiosFileNM:='';MeRegionFile:='';BiosRegionFile:='';
  txtsn.Text :='';
end;

procedure TMF.TxtMeLenChange(Sender: TObject);
begin
if txtmelen.Text <>'' then begin
   txtmelenMB.Text :=unitmf.BytesToStr(strtoint(TxtMeLen.Text) );
end;
end;

procedure TMF.TxtMeLenMouseEnter(Sender: TObject);
begin
  if isnumber(txtmelen.Text )=true then begin
     txtmelen.ShowHint :=true;
     txtmelen.Hint :=IntToHex(StrToInt(TxtMeLen.Text), 8);;
  end;
end;

procedure TMF.TxtMeLenMouseLeave(Sender: TObject);
begin
  txtmelen.Hint :='';
end;

procedure TMF.TxtMeRegionDblClick(Sender: TObject);
begin
 lbbiosme.Caption :='';
 MeRegionFile:='';
 TxtRMeLen.Clear;
end;

procedure TMF.TxtSNDblClick(Sender: TObject);
var
  i:Integer;
  HexStr, CharStr: string;
  l:int64;
  TxtSnHex:string;
begin
  if fileexists(BiosFileNM)=true  then begin
     HexStr:='010000000000000001000000000000001D000000';
     l:=FindBytesInFile(BiosFileNM,HexToBytes(HexStr));
     if l=-1 then begin
        txtsn.Text :='查不到正版序列号。';
        exit;
     end;
     TxtSnHex :=ReadBytesAfterPosition(l+Length(HexStr)div 2);
     CharStr := '';
      for i := 1 to Length(TxtSnHex) div 2 do begin
         CharStr := CharStr + HexToChar(Copy(TxtSnHex, (i - 1) * 2 + 1, 2));
     end;
     txtsn.Text :=CharStr;
  end;
end;

procedure TMF.WMDROPFILES(var Msg: TMessage);
var
  FilesCount: Integer;
  FileName: string;
  DropPoint: TPoint;
  BufferSize: Integer;
  FS: TFileStream;  //
begin
  FilesCount := DragQueryFile(Msg.WParam, $FFFFFFFF, nil, 0);
  if FilesCount > 0 then
  begin
    BufferSize := DragQueryFile(Msg.WParam, 0, nil, 0);
    if BufferSize > 0 then  begin
      SetLength(FileName, BufferSize+1);  //+1
      DragQueryFile(Msg.WParam, 0, PChar(FileName), BufferSize + 1);
      FileName := Trim(FileName);
      DragQueryPoint(Msg.WParam, DropPoint);
      if (IsPointInControl( txtfilename,DropPoint)) and (PC1.TabIndex =0) then begin
        ProcessDropOnControl(txtfilename, FileName, BiosFileNM);
        LBBiosFile.Caption :=FileName;
        doit;
      end else if (IsPointInControl( TxtMeRegion,DropPoint)) and (PC1.TabIndex =0) then begin
        ProcessDropOnControl(TxtMeRegion, FileName, MeRegionFile);
        LBBiosME.Caption :=FileName;
        dome;
      end else if (IsPointInControl(TxtBiosRegion,DropPoint)) and (PC1.TabIndex =0) then begin
        ProcessDropOnControl(TxtBiosRegion, FileName, BiosRegionFile);
        LBBiosBody.Caption :=fileName;
        dobios;
      end else if (IsPointInControl(LstFiles,DropPoint)) and (PC1.TabIndex =2) then begin //+++++++++++++++++++++
        ProcessDropOnControlList(LstFiles, FileName);
        lstfiles.Items.Add( Filename);
      end else if (IsPointInControl(BiosName,DropPoint)) and (PC1.TabIndex =3) then begin //+++++++++++++++++++++
        ProcessDropOnControl(BiosName, FileName,BiosNameSplit);
        BiosName.Text := Filename;
        FS := TFileStream.Create(FileName, fmOpenRead or fmShareDenyNone);
        try
           txtfilelen.Text :=inttostr(fs.Size);
        finally
           FS.Free ;
        end;
      end else if (IsPointInControl(LstSplits,DropPoint)) and (PC1.TabIndex =3) then begin //+++++++++++++++++++++
        ProcessDropOnControlList(LstSplits, FileName);
        FS := TFileStream.Create(FileName, fmOpenRead or fmShareDenyNone);
        try
           lstsplits.Items.Add(inttostr(fs.Size));
        finally
           FS.Free ;
        end;
      end;
    end;
  end;
  DragFinish(Msg.WParam);
end;

procedure TMF.DoMe();
begin
  if fileexists( MeRegionFile) then begin
     TxtRMeLen.text:=inttostr(unitmf.GetFileSize(MeRegionFile));
     if strtoint(TxtMelen.Text)< strtoint(TxtRMeLen.Text) Then begin
        showmessage('拖入的Me比原Me大，不能使用．');
        TxtRMelen.Text :='';
        LBBiosMe.Caption :='';
        MeRegionFile:='';
     End;
  end;
end;

procedure TMF.CmdClsClick(Sender: TObject);
begin
  txtmebb.Clear ;   txtmestr.Clear;
  txtmestart.Clear; txtmelen.Clear ; txtmelenmb.Clear;txtrmelen.Clear;
  txtbstart.Clear;  txtblen.Clear ;  txtblenmb.Clear;txtrblen.Clear;
  lbbiosfile.Caption:=''; lbbiosme.Caption :=''; LbBiosBody.Caption :='';
  BiosFileNM:='';MeRegionFile:='';BiosRegionFile:='';
  txtsn.Text :='';
end;

procedure TMF.CmdClsLstFilesClick(Sender: TObject);
begin
  lstfiles.Clear;
end;

procedure TMF.CmdClsSplitClick(Sender: TObject);
begin
  txtfilelen.Text :='0';
  txtsplitlen.Text  :='0';
  biosname.Clear;
  lstsplits.Clear;

end;

procedure TMF.CmdGetBiosRegionClick(Sender: TObject);
var FN:string;
    S1: array of Byte;    // 用于存储读取数据的字节数组
    Input,OutPut:file;
    position:integer;
begin
  if lbBiosFile.Caption ='' then exit;
  //FN:=GDP+'\_BiosRegion_'+ FormatDateTime('hhnnss', Now) +'.bin';
  FN:=GDP+'\'+ExtractFileName(BiosFileNM)+'_BiosRegin.bin';
  if fileexists(FN)=true then DeleteFile(FN);
  AssignFile(Input, BiosFileNM);
  Reset(Input, 1);
  AssignFile(Output, FN);
  Rewrite(Output, 1);
  SetLength(S1, StrToInt(TxtBLen.Text ));
  position:=StrToIntDef('$' + txtBStart.Text, 0);
  Seek(input,position);
  BlockRead(Input, S1[0], Length(S1));
  BlockWrite(Output, S1[0], Length(S1));
  CloseFile(Input);
  CloseFile(Output);

  ShowMessage(FN + '导出成功');

end;

procedure TMF.CmdGetMeRegionClick(Sender: TObject);
var FN:string;
    S1: array of Byte;    // 用于存储读取数据的字节数组
    Input,OutPut:file;
    position:integer;
begin
  if lbBiosFile.Caption ='' then exit;
  //FN:=GDP+'\_MeRegion_'+ FormatDateTime('hhnnss', Now) +'.bin';
  FN :=GDP+'\'+ExtractFileName(BiosFileNM)+'_MeRegion.bin';
  if fileexists(FN)=true then DeleteFile(FN);
  AssignFile(Input, BiosFileNM);
  Reset(Input, 1);
  AssignFile(Output, FN);
  Rewrite(Output, 1);
  SetLength(S1, StrToInt(TxtMeLen.Text ));
  position:=StrToIntDef('$' + txtMeStart.Text, 0);
  Seek(input,position);

  BlockRead(Input, S1[0], Length(S1));
  BlockWrite(Output, S1[0], Length(S1));

  CloseFile(Input);
  CloseFile(Output);

  ShowMessage(FN + '导出成功');

end;

procedure TMF.CmdMixClick(Sender: TObject);
var i:integer;
    tmpfn,str:string;
    DestStream:TFileStream;
    SourceStream:TFileStream;

begin
  tmpfn :=GDP+'\合并_'+FormatDateTime('hh.nn.ss', Now)+'.bin';

  if lstfiles.items.Count =0 then begin
     showmessage('列表中没有文件。');
     exit;
  end;
  DestStream:=Tfilestream.Create( tmpfn,fmCreate);

  for i := 0 to lstfiles.Items.Count -1 do begin
      str:=lstfiles.Items[i];
      if fileexists(str) then begin
        sourcestream:=TFileStream.create(str,fmOpenRead or fmShareDenyNone);
        DestStream.CopyFrom(SourceStream, 0);
        SourceStream.Free;
      end;
  end;
  DestStream.Free ;
  showmessage('文件合并成功，保存在桌面。'+tmpfn);
end;

procedure TMF.CmdOpenFolderClick(Sender: TObject);
var TargetPath:string;
begin
  if not DirectoryExists('C:\BIOS') then ForceDirectories('C:\BIOS');

  if lstfix.ItemIndex =-1  then begin
     ShellExecute(Handle,'explore',PChar('C:\BIOS'),nil,nil,SW_SHOWNORMAL);
     exit;
  end;
  TargetPath := 'C:\BIOS\'+lstfix.Items[lstfix.ItemIndex];
  ShellExecute(Handle,'explore',PChar(TargetPath),nil,nil,SW_SHOWNORMAL);
end;

procedure TMF.CmdRefLstFitClick(Sender: TObject);
begin
  lstfix.Clear;
  unitMF.ListToolFolders(LstFix);
end;

procedure TMF.CmdRunFixClick(Sender: TObject);
var TargetPath:string;
begin
  if lstfix.ItemIndex =-1  then exit;
  TargetPath := 'C:\BIOS\'+lstfix.Items[lstfix.ItemIndex];
  //ShellExecute(Handle,'explore',PChar(TargetPath),nil,nil,SW_SHOWNORMAL);
  if fileexists(TargetPath+'\fit.exe')=true then begin
     ShellExecute(Handle,'open', PChar(TargetPath+'\fit.exe'), nil, PChar(ExtractFilePath(TargetPath+'\fit.exe')), SW_SHOWNORMAL);
  end;
  if fileexists(TargetPath+'\mfit.exe')=true then begin
     ShellExecute(Handle,'open', PChar(TargetPath+'\mfit.exe'), nil, PChar(ExtractFilePath(TargetPath+'\mfit.exe')), SW_SHOWNORMAL);
  end;
  if fileexists(TargetPath+'\fitc.exe')=true then begin
     ShellExecute(Handle,'open', PChar(TargetPath+'\fitc.exe'), nil, PChar(ExtractFilePath(TargetPath+'\fitc.exe')), SW_SHOWNORMAL);
  end;
end;

procedure TMF.CmdSaveNewBiosFileClick(Sender: TObject);
var
  tmpfn: string;
  f1, f2, f3, f4: file;
  S1, S2, S3,PaddingBytes: array of Byte;
  FLen, i: Integer;
  BytesRead, BytesWritten: Cardinal;
begin
  if (lbbiosme.Caption ='') and (lbbiosbody.Caption ='') then exit;
  tmpfn :=GDP+'\'+ExtractFileName(BiosFileNM)+'_NewBios.bin';
  // 创建输出文件
  AssignFile(f4, tmpfn);
  Rewrite(f4, 1);
  try
    // 第一部分：处理BIOS文件起始部分
    AssignFile(f1, BiosFileNM);
    Reset(f1, 1);
    try
      SetLength(S1, StrToInt('$' + TxtMeStart.Text));
      BlockRead(f1, S1[0], Length(S1), BytesRead);
      BlockWrite(f4, S1[0], BytesRead, BytesWritten);
    finally
      CloseFile(f1);
    end;

    // 第二部分：处理ME区域
    if LBBiosMe.Caption <> '' then begin
      AssignFile(f2, MeRegionFile);
      Reset(f2, 1);
      try
        if StrToInt(TxtMeLen.Text) > StrToInt(TxtRMeLen.Text) then  begin
          FLen := StrToInt(TxtMeLen.Text ) - StrToInt(TxtRMeLen.Text );
          SetLength(PaddingBytes, FLen);
          for i :=0 to FLen-1 do
            PaddingBytes[i]:=$FF;
        end else begin
            SetLength(PaddingBytes, 0);
            FLen := 0;
        end;
        SetLength(S2, FileSize(f2));
        BlockRead(f2, S2[0], Length(S2), BytesRead);
        BlockWrite(f4, S2[0], BytesRead, BytesWritten);
        if Length(PaddingBytes) > 0 then begin
          BlockWrite(f4, PaddingBytes[0], FLen, BytesWritten);
        end;
      finally
        CloseFile(f2);
      end;
    end else begin

      AssignFile(f1,BiosFileNM);
      Reset(f1, 1);
      try
        Seek(f1, StrToInt('$' + TxtMeStart.Text ));
        SetLength(S3, StrToInt(TxtMeLen.Text ));
        BlockRead(f1, S3[0], Length(S3), BytesRead);
        BlockWrite(f4, S3[0], BytesRead, BytesWritten);
      finally
        CloseFile(f1);
      end;
    end;

    // 第三部分：处理主体部分
    if LBBiosBody.Caption  <> '' then  begin
      AssignFile(f3, BiosRegionFile);
      Reset(f3, 1);
      try
        SetLength(S3, FileSize(f3));
        BlockRead(f3, S3[0], Length(S3), BytesRead);
        BlockWrite(f4, S3[0], BytesRead, BytesWritten);
      finally
        CloseFile(f3);
      end;
    end else begin
      AssignFile(f1, BiosFileNM);
      Reset(f1, 1);
      try
        Seek(f1, StrToInt('$' + TxtBStart.Text ));
        SetLength(S3, StrToInt(TxtBLen.Text ));
        BlockRead(f1, S3[0], Length(S3), BytesRead);
        BlockWrite(f4, S3[0], BytesRead, BytesWritten);
      finally
        CloseFile(f1);
      end;
    end;
  finally
    CloseFile(f4);
  end;
end;

procedure TMF.CmdSplitClick(Sender: TObject);
var i:integer;
    str:string;
    SourceStream, DestStream: TFileStream;
    Buffer: TBytes;
    TotalRead, BytesRead: Int64;
    ChunkSize: Int64;
    FileIndex: Integer;
    DestFileName: string;
    tm:string;
begin
    tm:='_'+FormatDateTime('hh.nn.ss', Now)+'.bin';
    if lstsplits.Items.Count =0 then begin
       exit;
    end;
    txtsplitlen.Text :='0';
    for i := 0 to lstsplits.Items.Count -1 do begin
        str:=lstsplits.Items[i];
        txtsplitlen.Text :=inttostr(strtoint(txtsplitlen.Text)+strtoint(str));
    end;

    SourceStream := TFileStream.Create(BiosName.Text , fmOpenRead or fmShareDenyNone);
    TotalRead := 0;
    FileIndex := 1;

    for i :=  0 to lstsplits.Items.Count -1 do begin
        chunkSize:= strtoint64(lstsplits.items[i]);
        if totalread>=sourcestream.Size  then break;
        SetLength(Buffer, ChunkSize);
        // 从源文件读取指定大小的数据
        BytesRead := SourceStream.Read(Buffer[0], ChunkSize);
        if BytesRead > 0 then begin
           DestFileName := GDP +'\'+ Format('part_%.3d', [FileIndex])+tm;
           // 创建目标文件并写入数据
           DestStream := TFileStream.Create(DestFileName, fmCreate);
           try
              DestStream.WriteBuffer(Buffer[0], BytesRead);
           finally
              DestStream.Free;
           end;
        end;
        Inc(TotalRead, BytesRead);
        Inc(FileIndex);
    end;
    // 让界面有时间刷新，防止大文件拆分时界面假死
    Application.ProcessMessages;
    sourcestream.Free ;
    showmessage('拆分成功，文件在桌面.');
end;

procedure TMF.DoBios();
begin
  if fileexists( BiosRegionFile) then begin
     TxtRBLen.text:=inttostr(unitmf.GetFileSize(BiosRegionFile));
     if strtoint(TxtBlen.Text) <> strtoint(TxtRBLen.Text) Then begin
        showmessage('BiosRegion大小不相同，不能使用．');
        TxtRBlen.Text :='';
        LBBiosBody.Caption :='';
        BiosRegionFile:='';
     End;
  end;
end;



procedure TMF.DoIt();
var Tmp,MeStr,Bstr:string;
    tl:integer;
    MeStart,BStart:int64;
begin
    docap(BiosFileNM);
    TxtSNDblClick(nil);
    tmp :=unitMF.GetFileHead(BiosFileNM);
    MeStr:=copy(tmp,145,4);
    MeStart := StrToInt('$' + Copy(MeStr, 3, 2) + Copy(MeStr, 1, 2) + '000');
    TxtMeStart.Text := IntToHex(MeStart, 8);
    BStr := Copy(tmp, 137, 4); // BIOS开始位置
  if BStr <> '0000' then begin
    BStart := StrToInt('$' + Copy(BStr, 3, 2) + Copy(BStr, 1, 2) + '000');
    TxtBStart.Text := IntToHex(BStart, 8);
    TxtMeLen.Text := IntToStr(BStart - MeStart);
    TxtBLen.Text := IntToStr(GetFileSize(BiosFileNM) - BStart);
  end else begin
    MeStr := Copy(tmp, 149, 4);
    TxtMeLen.Text := IntToStr(StrToInt('$' + Copy(MeStr, 3, 2) + Copy(MeStr, 1, 2) + '000'));
    TxtBStart.Text := IntToHex(MeStart + StrToInt(TxtMeLen.Text), 8);
    TxtBLen.Text := IntToStr(GetFileSize(BiosFileNM) - StrToInt('$' + TxtBStart.Text));
  end;


  //tmp := GetFileHex(BiosFileNM, strtoint(Txt MeStart.Text),strtoint( TxtMeLen.Text));
  tmp := GetFileHex(BiosFileNM, StrToIntDef('$' + txtMeStart.Text, 0),strtoint( TxtMeLen.Text));

  tl := Pos('244D4E32', tmp);
  if tl <> 0 then  begin
    tl := tl + 16;
    tmp := Copy(tmp, tl, 16);
    TxtMeStr.Text := tmp;
    TxtMeBB.Text := IntToStr(StrToInt('$' + Copy(tmp, 1, 2))) + '.' +
                    IntToStr(StrToInt('$' + Copy(tmp, 5, 2))) + '.' +
                    IntToStr(StrToInt('$' + Copy(tmp, 9, 2))) + '.' +
                    IntToStr(StrToInt('$' + Copy(tmp, 15, 2) + Copy(tmp, 13, 2)));
  end;
  if (txtmestr.Text ='') and (txtmebb.Text ='') then begin
      txtbstart.Clear;  txtblen.Clear;  txtmelenmb.Clear;
      txtmestart.Clear; txtmelen.Clear; txtblenmb.Clear;
  end;
end;

end.
