{ Data Type 1.2.1 Natural number Type (N) Value range: Represents a range of 0 to the maximum value that is positive and determined by the field length. (Example: The natural number type with a two-byte field can represent from 0 to 65535. 0 is assigned to "Null".) 1.2.2 Integer Type (I) Representation method: Uses the two's-complement form (the first bit means a plus or minus sign). Value range: Represents a range between the maximum negative value and maximum positive value that are determined by the field length. (Example: The integer type with a two-byte field can represent from -32768 to +32767. 0 is assigned to "Null".) 1.2.3 1-byte String Type (C) Representation method: Uses the sequence of a 1-byte code (such as the JIS octet code). 1.2.4 Meta-dependent String Type (CC) Representation method: Uses the sequence of a linguistic string code (such as the SJIS code) defined with metadata. 1.2.5 Bit Type (B) Representation method: Uses a group of bits binary-coded as one field. 1.2.6 Normalized Type (NZ) Value range: Represents the integer part of a normalized value (0.0 ≦ value in the field ≦1.0) multiplied by n. 1.2.7 Sector Address Type (SA) Representation method: Represents the physical sector address on the storage medium using the 3-byte sector address and 6-bit logical sector count. The size of one logical sector is variable between 32 bytes and 256 bytes. } unit LoadingUnit; interface uses windows, Dialogs, SysUtils, ComCtrls, DateUtils; const SectorSize = 2048; ASIN: array [0 .. 11] of Byte = ($0F, $56, $A3, $00, $3C, $3C, $8A, $00, $07, $00, $01, $66); // :=AISIN DENSO: array [0 .. 11] of Byte = ($44, $45, $4E, $53, $4F, $00, $00, $00, $00, $00, $00, $00); // Denso in 86271-24125 (86271-70V315A) DENSO2: array [0 .. 11] of Byte = ($44, $45, $4E, $53, $4F, $20, $20, $20, $20, $20, $20, $20); // Panasonic Panasonic: array [0 .. 11] of Byte = ($0F, $9B, $45, $00, $3D, $54, $BE, $00, $00, $00, $01, $66); // :=AISIN type TObjectState = (fbEmpty, fbReading, fbReads); TBitSet = array [0 .. 7] of Boolean; TModuleCategory = array [0 .. 3] of String; // TModuleCategoryFlag = set of (TMInitialProgram,TMProgram,TMLibrary,TMData); // Loading Module Management Frame TLoadingModuleManagementFrame = packed record NumberOfAccommodatedSystems: array [0 .. 1] of Byte; RESERVED: array [0 .. 1] of Byte; ASequenceofSystemIdentificationInformation: Byte; ASequenceofSystemIdentificationInformation2: Byte; end; TModuleAddresAndSize = record ModuleSize: Int64; AddresofTheModule: Int64; end; // System Management Information { TSystemManagementInformation = packed record ASequenceOfModuleIdentificationInformation: array of Byte; ASequenceOfModuleIdentificationInformation2: array of Byte; end; } // Module Identification Information TManufacturerDependentInformation = packed record UnitName: array [0 .. 8] of ansichar; UnitSize: array [0 .. 6] of Byte; // ModuleVersionNumber: array [0 .. 7] of ansichar; // RESERVED2: array [0 .. 254] of ansichar; end; TAisinUnitsOnModule = record NumberofUnitsinModule: Integer; Manufacturer_dependent_Information: array [0 .. 10] of TManufacturerDependentInformation; end; TUnitsOnModule = record NumberofUnitsinModule: array [0 .. 1] of Byte; RESERVED: array [0 .. 1] of Byte; Manufacturer_dependent_Information: array [0 .. 10] of TManufacturerDependentInformation; end; // Module Management Information TModuleManagementInformation = record DatewhenTheFunctionOfTheModuleBecomesValid: array [0 .. 1] of Byte; DateWhenTheFunctionOfTheModuleBecomesInvalid: array [0 .. 1] of Byte; ModuleTitle: array [0 .. 63] of ansichar; UnitsOnModule: TUnitsOnModule; // NumberofUnitsinModule: array [0 .. 1] of Byte; // RESERVED: array [0 .. 1] of Byte; // Manufacturer_dependent_Information: array [0 .. 10] // of TManufacturerDependentInformation; RESERVED2: array [0 .. 1] of Byte; AddressOfTheModuleCode: array [0 .. 3] of Byte; SizeOfTheModuleCode: array [0 .. 1] of Byte; end; // Module Identification Information TModuleIdentificationInformation = packed record ModuleCategory: Byte; // (7-1 bit) GetBits Module Category RESERVED: array [0 .. 2] of Byte; ModuleName: array [0 .. 51] of ansichar; ModuleVersionNumber: array [0 .. 7] of ansichar; ModuleManagementInformation: TModuleManagementInformation; // RESERVED2: array [0 .. 254] of ansichar; end; TNameManufacturer = array [0 .. 11] of Byte; // System Identification Information TSystemIdentificationInformation = packed record ManufacturerIdentifier: TNameManufacturer; // array [0 .. 11] of Byte; NumberOfAccommodatedModules: array [0 .. 1] of Byte; RESERVED: array [0 .. 1] of Byte; ModuleIdentificationInformation: array of TModuleIdentificationInformation; end; TLoadingModule = record LoadingModuleManagementFrame: TLoadingModuleManagementFrame; SystemIdentificationInformation: array of TSystemIdentificationInformation; FileSize: Longint; end; TTreeKiwi = record State: TObjectState; Name: WideString; ImageIndex: Integer; ModuleIdentificationInformation: TModuleIdentificationInformation; end; PTreeKiwi = ^TTreeKiwi; { // TByteArray = array [0 .. 3] of Byte; TManufacturerIdentifier = array [0 .. 11] of Byte; TModule = packed record SubModule: array [0 .. 3] of Byte; RESERVED: array [0 .. 3] of Byte; Name: array [0 .. 31] of ansichar; end; TModuleArray = array of TModule; TManufacturer = packed record NameManufacturer: TManufacturerIdentifier; //12 NumberofAccommodatedModules: array [0 .. 1] of Byte; RESERVED: array [0 .. 1] of Byte; //2 Module: TModuleArray; end; TLoadingInfo = packed record NumberofAccommodatedSystems: array [0 .. 1] of Byte; //2 RESERVED: array [0 .. 1] of Byte; //2 ASequenceofSystemIdentificationInformation : array [0 .. 176] of Byte; //array of TManufacturer; //B1 =177 ASequenceofSystemIdentificationInformation2 : array [0 .. 177] of Byte; //array of TManufacturer; //B2 =178 FileSize: Longint; end; } function GetBits(X: Byte): TBitSet; function StrFormatByteSizeW(ASize: UInt64; szBuf: PWideChar; uiBufSize: UINT) : PWideChar; stdcall; external 'shlwapi.dll'; function GetManufacureName(NameManufacturer: TNameManufacturer): string; function GetModuleCategory(ModuleCategory: Byte): TModuleCategory; function LoadLoadingKWI(LoadingFileName: string): TLoadingModule; function GetModuleAddresAndSize(LoadingModule: TLoadingModule; SelectedIndex, index: Integer): TModuleAddresAndSize; function FmtFormatSize(const ASize: UInt64): String; function Int2Str(const Value: Integer): string; function BinHextoStr(const bin: array of Byte): string; Function GetDateTime(days: Integer): String; function DirLevel(const PathName: string): Integer; { counts '\' in path } implementation function DirLevel(const PathName: string): Integer; { counts '\' in path } {$IF DEFINED(CLR)} var CurPos, PathLen: Integer; begin Result := 0; PathLen := Length(PathName); CurPos := Pos('\', PathName) - 1; while CurPos >= 0 do begin Inc(Result); if CurPos < PathLen - 1 then CurPos := PosEx('\', PathName, CurPos + 2) - 1 else CurPos := -1; end; {$ELSE} var P: PChar; begin Result := 0; P := AnsiStrScan(PChar(PathName), '\'); while P <> nil do begin Inc(Result); Inc(P); P := AnsiStrScan(P, '\'); end; {$ENDIF} end; function BinHextoStr(const bin: array of Byte): string; const HexSymbols = '0123456789ABCDEF'; var i: Integer; begin SetLength(Result, 2 * Length(bin)); for i := 0 to Length(bin) - 1 do begin Result[1 + 2 * i + 0] := HexSymbols[1 + bin[i] shr 4]; Result[1 + 2 * i + 1] := HexSymbols[1 + bin[i] and $0F]; end; Result := '$' + Result; end; Function GetDateTime(days: Integer): String; var myDate: TDateTime; begin if days = 0 then begin Result := '-'; exit; end; myDate := StrToDate('01.01.1997'); // Теперь добавляме месяц к этому значению Result := DateToStr(IncDay(myDate, days)); end; { function IsArraysEqual(A, B : Array Of Integer) : Boolean; var I, J : Integer; begin Result := True; For I := Low(A) To High(A) Do For J := Low(A[i]) To High(A[i]) Do Begin Result := A[I,J] = B[I,J]; If Not Result Then Break; End; end; } { function GetNBit(X, N: Byte): Bit; begin Result := X shr N and 1; end; } { function GetBits(X: Byte): TBitSet; var N: integer; begin for N := 0 to 7 do Result[N] := GetNBit(X, N); end; } function Int2Str(const Value: Integer): string; asm XOR ECX, ECX PUSH ECX ADD ESP, -0Ch PUSH EBX LEA EBX, [ESP + 15 + 4] PUSH EDX CMP EAX, ECX PUSHFD JGE @@1 NEG EAX @@1: MOV CL, 10 @@2: DEC EBX CDQ IDIV ECX ADD DL, 30h MOV [EBX], DL TEST EAX, EAX JNZ @@2 POPFD JGE @@3 DEC EBX MOV byte ptr [EBX], '-' @@3: POP EAX MOV EDX, EBX CALL System.@LStrFromPChar POP EBX ADD ESP, 10h end; function FmtFormatSize(const ASize: UInt64): String; var Buf: WideString; begin SetLength(Buf, 1024); if StrFormatByteSizeW(ASize, PWideChar(Buf), Length(Buf)) <> nil then Result := PWideChar(Buf) else Result := UIntToStr(ASize); Buf := ''; end; function GetModuleAddresAndSize(LoadingModule: TLoadingModule; SelectedIndex, index: Integer): TModuleAddresAndSize; var AddresofTheModule: Int64; begin Result.ModuleSize := Swap(PWORD(@LoadingModule.SystemIdentificationInformation[SelectedIndex] .ModuleIdentificationInformation[index].ModuleManagementInformation. SizeOfTheModuleCode)^) * 2; // a:=[0,0,3]; // HexToBin( AddresofTheModule := StrToInt(BinHextoStr(LoadingModule.SystemIdentificationInformation [SelectedIndex].ModuleIdentificationInformation[index] .ModuleManagementInformation.AddressOfTheModuleCode)); // 2048 if (GetManufacureName(LoadingModule.SystemIdentificationInformation [SelectedIndex].ManufacturerIdentifier) = 'ASIN-AW') or (GetManufacureName(LoadingModule.SystemIdentificationInformation [SelectedIndex].ManufacturerIdentifier) = 'PANASONIC') then Result.AddresofTheModule := AddresofTheModule div 256 else Result.AddresofTheModule := AddresofTheModule; end; function GetUnitsOnModule(LoadingModule: TLoadingModule; SelectedIndex, index: Integer): TAisinUnitsOnModule; var UnitsInModule: Integer; begin UnitsInModule := Swap(PWORD(@LoadingModule.SystemIdentificationInformation [SelectedIndex].ModuleIdentificationInformation[index] .ModuleManagementInformation.UnitsOnModule.NumberofUnitsinModule)^); if UnitsInModule = 1 then begin end else Result.NumberofUnitsinModule := UnitsInModule; end; function GetManufacureName(NameManufacturer: TNameManufacturer): string; var a, i: Integer; begin if comparemem(@NameManufacturer, @Panasonic, 2 * SizeOf(Integer)) then Result := 'PANASONIC'; if comparemem(@NameManufacturer, @ASIN, 2 * SizeOf(Integer)) then Result := 'ASIN-AW'; if comparemem(@NameManufacturer, @DENSO, 2 * SizeOf(Integer)) then Result := 'DENSO'; if comparemem(@NameManufacturer, @DENSO2, 2 * SizeOf(Integer)) then Result := 'DENSO'; end; function GetBits(X: Byte): TBitSet; var // Bits: array [0 .. 7] of Boolean; b, i: Byte; begin b := 127; for i := 0 to 7 do Result[i] := (b shl i) and $01 = $01; end; function GetModuleCategory(ModuleCategory: Byte): TModuleCategory; var ModuleBits: TBitSet; Diagnostic, TEST, Programm: string; begin // { 7 Diagnostic Service Module flag Bit 7 0 Not diagnostic service module 1 Diagnostic service module 6 Test Module Flag Bit 6 (2) a 0 Not test module 1 Test module 5 to 2 (RESERVED) 1--2 Module Category Flag Bit 1 Bit 0 (3) a 0 0 Initial program 0 1 Program 1 0 Library 1 1 Data } ModuleBits := GetBits(ModuleCategory); if not ModuleBits[7] then Diagnostic := 'NO' else Diagnostic := 'YES'; if not ModuleBits[6] then TEST := 'NO' else TEST := 'YES'; if not ModuleBits[1] and not ModuleBits[0] then Programm := 'Initial program'; if not ModuleBits[1] and ModuleBits[0] then Programm := 'Program'; if ModuleBits[1] and not ModuleBits[0] then Programm := 'Library'; if ModuleBits[1] and ModuleBits[0] then Programm := 'Data'; Result[0] := Diagnostic; Result[1] := TEST; Result[2] := Programm; end; function LoadLoadingKWI(LoadingFileName: string): TLoadingModule; var Fi: file; LoadingBuf: TLoadingModule; ModuleIdentificationInformation: TModuleIdentificationInformation; ModuleManagementInformation: TModuleManagementInformation; AisinUnitsOnModule: TUnitsOnModule; Count: Integer; ListItem: TListItem; Units, Units2, aa: Integer; AddresofTheModule, FilePosition: Int64; a, i, I2, i3: Integer; begin AssignFile(Fi, LoadingFileName); FileMode := 0; {$I-} Reset(Fi, 1); {$I+} if IOResult <> 0 then begin MessageDlg('Ошибка доступа к файлу ' + LoadingFileName, mtError, [mbOk], 0); // CloseFile(Fi); exit; end; fillchar(LoadingBuf, SizeOf(TLoadingModuleManagementFrame), 0); LoadingBuf.FileSize := FileSize(Fi); BlockRead(Fi, LoadingBuf.LoadingModuleManagementFrame, 4, Count); SetLength(LoadingBuf.SystemIdentificationInformation, Swap(PWORD(@LoadingBuf.LoadingModuleManagementFrame.NumberOfAccommodatedSystems)^)); for i := 0 to Swap (PWORD(@LoadingBuf.LoadingModuleManagementFrame. NumberOfAccommodatedSystems)^) - 1 do begin BlockRead(Fi, LoadingBuf.SystemIdentificationInformation[i], 16 { SizeOf(TManufacturer) } , Count); end; for i := 0 to Swap (PWORD(@LoadingBuf.LoadingModuleManagementFrame. NumberOfAccommodatedSystems)^) - 1 do begin a := Swap(PWORD(@LoadingBuf.SystemIdentificationInformation[i] .NumberOfAccommodatedModules)^); SetLength(LoadingBuf.SystemIdentificationInformation[i] .ModuleIdentificationInformation, a); for I2 := 0 to a - 1 do begin BlockRead(Fi, LoadingBuf.SystemIdentificationInformation[i] .ModuleIdentificationInformation[I2], 64 // SizeOf(TModuleIdentificationInformation) , Count); end; // aa:=SizeOf(TModuleManagementInformation); for I2 := 0 to a - 1 do begin BlockRead(Fi, LoadingBuf.SystemIdentificationInformation[i] .ModuleIdentificationInformation[I2].ModuleManagementInformation, 256, Count); // GetBits(LoadingBuf.SystemIdentificationInformation[I].ModuleIdentificationInformation[i2].ModuleCategory)// // BlockRead(Fi, ModuleManagementInformation, // SizeOf(TModuleManagementInformation), Count); Units := Swap(PWORD(@LoadingBuf.SystemIdentificationInformation[i] .ModuleIdentificationInformation[I2].ModuleManagementInformation. UnitsOnModule.NumberofUnitsinModule)^); FilePosition := FilePos(Fi); // Запомнили позицию if Units = 1 then begin AddresofTheModule := StrToInt(BinHextoStr(LoadingBuf.SystemIdentificationInformation[i] .ModuleIdentificationInformation[I2].ModuleManagementInformation. AddressOfTheModuleCode)); // 2048 seek(Fi, (AddresofTheModule div 256) * 2048); // LoadingBuf.SystemIdentificationInformation[i] // .ModuleIdentificationInformation[i2].ModuleManagementInformation.AddressOfTheModuleCode BlockRead(Fi, AisinUnitsOnModule, SizeOf(TUnitsOnModule), Count); Units2 := Swap(PWORD(@AisinUnitsOnModule)^); LoadingBuf.SystemIdentificationInformation[i] .ModuleIdentificationInformation[I2].ModuleManagementInformation. UnitsOnModule := AisinUnitsOnModule; seek(Fi, FilePosition); // Восстановили позицию // for i3 := 0 to Units2 - 1 do // begin // BlockRead(Fi, LoadingBuf.SystemIdentificationInformation[i] // .ModuleIdentificationInformation[i3].ModuleManagementInformation, // 16, Count); // end; end; end; end; Result := LoadingBuf; // GetNewCRC(Result, pBuf, Count); CloseFile(Fi); end; end.