SASC
DVBSoftCAM

cN2Prov0101 Class Reference

Inheritance diagram for cN2Prov0101:

List of all members.

Public Member Functions

 cN2Prov0101 (int Id, int Flags)
virtual bool PostProcAU (int id, unsigned char *data)
virtual int ProcessBx (unsigned char *data, int len, int pos)
virtual int ProcessEx (unsigned char *data, int len, int pos)
virtual int RunEmu (unsigned char *data, int len, unsigned short load, unsigned short run, unsigned short stop, unsigned short fetch, int fetch_len)
virtual void PostDecrypt (bool ecm)
 cN2Prov0101 (int Id, int Flags)
virtual bool PostProcAU (int id, unsigned char *data)
virtual int ProcessBx (unsigned char *data, int len, int pos)
virtual int ProcessEx (unsigned char *data, int len, int pos)
virtual int RunEmu (unsigned char *data, int len, unsigned short load, unsigned short run, unsigned short stop, unsigned short fetch, int fetch_len)
virtual void PostDecrypt (bool ecm)

Protected Member Functions

virtual bool Algo (int algo, const unsigned char *hd, unsigned char *hw)
virtual void DynamicHD (unsigned char *hd, const unsigned char *ed)
virtual bool RomInit (void)
virtual void Stepper (void)
virtual void ReadHandler (unsigned char seg, unsigned short ea, unsigned char &op)
virtual void TimerHandler (unsigned int num)
virtual void AddMapCycles (unsigned int num)
virtual unsigned int CpuCycles (void)
virtual bool Algo (int algo, const unsigned char *hd, unsigned char *hw)
virtual void DynamicHD (unsigned char *hd, const unsigned char *ed)
virtual bool RomInit (void)
virtual void Stepper (void)
virtual void ReadHandler (unsigned char seg, unsigned short ea, unsigned char &op)
virtual void TimerHandler (unsigned int num)
virtual void AddMapCycles (unsigned int num)
virtual unsigned int CpuCycles (void)

Protected Attributes

int mecmAddr [2]
int mecmKeyId

Detailed Description

Definition at line 464 of file nagra2-0101.c.


Constructor & Destructor Documentation

cN2Prov0101::cN2Prov0101 ( int  Id,
int  Flags 
)

Definition at line 501 of file nagra2-0101.c.

References c6805::hasReadHandler, mecmAddr, mecmKeyId, cN2Prov::seedSize, and cMapCore::SetMapIdent().

:cN2Prov(Id,Flags)
{
  mecmAddr[0]=0x91d7; mecmAddr[1]=0x92d7; mecmKeyId=0x106;
  seedSize=10;
  desSize=16; hwMapper=0;
  SetMapIdent(Id);
  hasReadHandler=true;
}
cN2Prov0101::cN2Prov0101 ( int  Id,
int  Flags 
)

Member Function Documentation

virtual void cN2Prov0101::AddMapCycles ( unsigned int  num) [inline, protected, virtual]

Reimplemented from cMapCore.

Definition at line 488 of file nagra2-0101.c.

References c6805::AddCycles().

{ AddCycles(num); }
virtual void cN2Prov0101::AddMapCycles ( unsigned int  num) [inline, protected, virtual]

Reimplemented from cMapCore.

Definition at line 488 of file nagra2-0101.c.

References c6805::AddCycles().

{ AddCycles(num); }
bool cN2Prov0101::Algo ( int  algo,
const unsigned char *  hd,
unsigned char *  hw 
) [protected, virtual]

Reimplemented from cN2Prov.

Definition at line 520 of file nagra2-0101.c.

References c6805::AddBreakpoint(), c6805::ClearBreakpoints(), data, cIDEA::Decrypt(), cN2Prov::ExpandInput(), cPlainKeys::FindKey(), cPlainKey::Get(), c6805::GetMem(), c6805::GetPc(), HEXDUMP, cN2Prov::idea, cN2Emu::Init(), keys, L_SYS_ECM, L_SYS_KEY, L_SYS_RAWECM, mecmAddr, mecmKeyId, PRINTF, RotateBytes(), c6805::Run(), cN2Prov::seedSize, cIDEA::SetEncKey(), c6805::SetMem(), c6805::SetPc(), and c6805::SetSp().

{
  if(algo!=0x40 && algo!=0x60) {
    PRINTF(L_SYS_ECM,"%04X: unknown MECM algo %02x",id,algo);
    return false;
    }
  if(!Init(id,102)) {
    PRINTF(L_SYS_ECM,"%04X: failed to initialize ROM",id);
    return false;
    }

  unsigned char keyNr=(algo>>5)&0x01;
  unsigned char mecmCode[256];
  GetMem(mecmAddr[keyNr],mecmCode,sizeof(mecmCode),0x80);
  cPlainKey *pk;
  unsigned char ideaKey[16];
  if(!(pk=keys.FindKey('N',mecmKeyId,keyNr,sizeof(ideaKey)))) {
    PRINTF(L_SYS_KEY,"missing %04x %02x MECM key",mecmKeyId,keyNr);
    return false;
    }
  pk->Get(ideaKey);
  idea.SetEncKey(ideaKey,&ks);
  idea.Decrypt(mecmCode,sizeof(mecmCode),&ks,0);
  HEXDUMP(L_SYS_RAWECM,mecmCode,sizeof(mecmCode),"decrypted MECM code");
  // check signature
  unsigned char data[256];
  memcpy(data,mecmCode,sizeof(data));
  RotateBytes(data,sizeof(data));
  SHA1(data,sizeof(data)-8,data);
  RotateBytes(data,20);
  if(memcmp(data,mecmCode,8)) {
    PRINTF(L_SYS_ECM,"%04X: MECM %02x decrypt signature failed",id,keyNr);
    return false;
    }

  memcpy(hw,hd,seedSize);
  ExpandInput(hw);

  SetSp(0x0FFF,0x0EF8);
  ClearBreakpoints();
  SetMem(0x0100,mecmCode+8,0x100-8);
  SetMem(0xa00,hd,seedSize);
  SetMem(0x0ba2,hw,0x80);
  AddBreakpoint(0x0000);
  AddRomCallbacks();
  SetPc(0x0100);
  while(!Run(100000)) {
    if(GetPc()==0x0000) {
      GetMem(0x0ba2,hw,0x80);
      return true;
      }
    else if(!RomCallbacks()) return false;
    }

/*
  if(algo==0x40) {
    const int hwCount=2;
    memcpy(hw,hd,hwCount+1);
    ExpandInput(hw);
    hw[0x18]|=1; hw[0x40]|=1;
    DoMap(SETSIZE,0,2);
    DoMap(IMPORT_A,hw,3);
    DoMap(IMPORT_D,hw+24);
    DoMap(0x3b);
    DoMap(EXPORT_C,hw);
    DoMap(IMPORT_A,hw+40,3);
    DoMap(IMPORT_D,hw+64);
    DoMap(0x3b);
    DoMap(EXPORT_C,hw+40);
    DoMap(0x43);
    DoMap(0x44,hw);
    DoMap(0x44,hw+64);
    memcpy(&hw[0],&hw[64+4],hwCount);
    memset(&hw[hwCount],0,128-hwCount);
    return true;
    }
  else if(algo==0x60) { // map 4D/4E/57
    hw[127]|=0x80; hw[0]|=0x01;
    DoMap(SETSIZE,0,16);
    DoMap(IMPORT_A,hw);
    DoMap(0x4d);
    DoMap(0x4e);
    DoMap(EXPORT_A,hw);
    RotateBytes(hw,16);
    DoMap(IMPORT_A,hw);
    DoMap(0x4e);
    DoMap(EXPORT_A,hw);
    DoMap(0x57,hw);
    return true;
    }

  PRINTF(L_SYS_ECM,"%04X: unknown MECM algo %02x",id,algo);
*/
  return false;
}
virtual bool cN2Prov0101::Algo ( int  algo,
const unsigned char *  hd,
unsigned char *  hw 
) [protected, virtual]

Reimplemented from cN2Prov.

virtual unsigned int cN2Prov0101::CpuCycles ( void  ) [inline, protected, virtual]

Reimplemented from cMapCore.

Definition at line 489 of file nagra2-0101.c.

References c6805::Cycles().

{ return Cycles(); }
virtual unsigned int cN2Prov0101::CpuCycles ( void  ) [inline, protected, virtual]

Reimplemented from cMapCore.

Definition at line 489 of file nagra2-0101.c.

References c6805::Cycles().

{ return Cycles(); }
void cN2Prov0101::DynamicHD ( unsigned char *  hd,
const unsigned char *  ed 
) [protected, virtual]

Reimplemented from cN2Prov.

Definition at line 511 of file nagra2-0101.c.

{
  hd[5]=ed[5];
  hd[6]=(ed[7]&0xEF) | ((ed[6]&0x40)>>2);
  hd[7]=ed[8];
  hd[8]=(ed[9]&0x7F) | ((ed[6]&0x20)<<2);
  hd[9]=ed[6]&0x80;
}
virtual void cN2Prov0101::DynamicHD ( unsigned char *  hd,
const unsigned char *  ed 
) [protected, virtual]

Reimplemented from cN2Prov.

virtual void cN2Prov0101::PostDecrypt ( bool  ecm) [inline, virtual]

Reimplemented from cN2Prov.

Definition at line 496 of file nagra2-0101.c.

References cMapCore::PostDecryptSetup().

{ PostDecryptSetup(ecm); }
virtual void cN2Prov0101::PostDecrypt ( bool  ecm) [inline, virtual]

Reimplemented from cN2Prov.

Definition at line 496 of file nagra2-0101.c.

References cMapCore::PostDecryptSetup().

{ PostDecryptSetup(ecm); }
bool cN2Prov0101::PostProcAU ( int  id,
unsigned char *  data 
) [virtual]

Reimplemented from cN2Prov.

Definition at line 616 of file nagra2-0101.c.

{
  if(data[1]&0x5f) return false;
  return true;
}
virtual bool cN2Prov0101::PostProcAU ( int  id,
unsigned char *  data 
) [virtual]

Reimplemented from cN2Prov.

int cN2Prov0101::ProcessBx ( unsigned char *  data,
int  len,
int  pos 
) [virtual]

Reimplemented from cN2Prov.

Definition at line 896 of file nagra2-0101.c.

References c6805::a, c6805::AddBreakpoint(), c6805::ClearBreakpoints(), c6805::GetMem(), c6805::GetPc(), cN2Emu::Init(), c6805::Run(), c6805::SetMem(), c6805::SetPc(), and c6805::SetSp().

{
  if(Init(id,102)) {
    SetMem(0x92,data+pos-1,len-pos+1);
    SetPc(0x93);
    SetSp(0x0FFF,0x0EF8);
    ClearBreakpoints();
    AddBreakpoint(0x9569);
    AddBreakpoint(0x0000);
    AddRomCallbacks();
    while(!Run(1000)) {
      if(GetPc()==0x9569) {
        GetMem(0x80,data,len);
        return a;
        }
      else if(GetPc()==0x0000) break;
      else if(!RomCallbacks()) break;
      }
    }
  return -1;
}
virtual int cN2Prov0101::ProcessBx ( unsigned char *  data,
int  len,
int  pos 
) [virtual]

Reimplemented from cN2Prov.

int cN2Prov0101::ProcessEx ( unsigned char *  data,
int  len,
int  pos 
) [virtual]

Reimplemented from cN2Prov.

Definition at line 918 of file nagra2-0101.c.

References c6805::AddBreakpoint(), c6805::ClearBreakpoints(), c6805::GetMem(), c6805::GetPc(), cN2Emu::Init(), max(), c6805::Push(), c6805::Run(), c6805::Set(), c6805::SetMem(), c6805::SetPc(), and c6805::SetSp().

{
  if(Init(id,102)) {
    SetMem(0x80,data,len);
    SetPc(0x9591);
    SetSp(0x0FFF,0x0EF8);
    Push(0x99); //push the bug-table return onto the stack
    Push(0x95);
    Push(0x00);
    Set(0x00,0x62,0x26);
    Set(0x00,0x63,0x02);
    Set(0x00,0x03d3,len-0x12);
    ClearBreakpoints();
    AddBreakpoint(0x9569);
    AddBreakpoint(0x9599);
    AddRomCallbacks();
    while(!Run(10000)) {
      if(GetPc()==0x9569) {
        GetMem(0x80,data,len);
        return max((int)a,6);
        }
      else if(GetPc()==0x9599) break;
      else if(!RomCallbacks()) break;
      }
    }
  return -1;
}
virtual int cN2Prov0101::ProcessEx ( unsigned char *  data,
int  len,
int  pos 
) [virtual]

Reimplemented from cN2Prov.

virtual void cN2Prov0101::ReadHandler ( unsigned char  seg,
unsigned short  ea,
unsigned char &  op 
) [protected, virtual]

Reimplemented from c6805.

void cN2Prov0101::ReadHandler ( unsigned char  seg,
unsigned short  ea,
unsigned char &  op 
) [protected, virtual]

Reimplemented from c6805.

Definition at line 967 of file nagra2-0101.c.

References c6805::Cycles(), and sn8.

{
   if(ea==0x2f70) op=random()&0xFF;
   else if(ea==0x2f71) {
     unsigned int n=random()&0xFF;
     unsigned int cy=Cycles();
     if(cy>rndtime && cy<=rndtime+4) {
       unsigned int o=sn8(rnd);
       n=(n&0xE3) | (o&4) | ((o>>1)&16) | ((o^(o>>1))&8);
       }
     op=rnd=n; rndtime=cy;
     }
}
virtual bool cN2Prov0101::RomInit ( void  ) [protected, virtual]

Reimplemented from cN2Emu.

bool cN2Prov0101::RomInit ( void  ) [protected, virtual]

Reimplemented from cN2Emu.

Definition at line 622 of file nagra2-0101.c.

References c6805::AddBreakpoint(), c6805::AddMapper(), c6805::ClearBreakpoints(), c6805::GetPc(), HW_OFFSET, HW_REGS, L_SYS_EMU, PRINTF, c6805::Run(), c6805::SetPc(), and c6805::SetSp().

{
  if(!AddMapper(hwMapper=new cMapMemHW(),HW_OFFSET,HW_REGS,0x00)) return false;
  SetPc(0x4000);
  SetSp(0x0FFF,0x0FE0);
  ClearBreakpoints();
  AddBreakpoint(0x537d);
  AddBreakpoint(0x8992);
  AddBreakpoint(0xA822);
  while(!Run(7000)) {
    switch(GetPc()) {
      case 0x537d:
        PRINTF(L_SYS_EMU,"%04x: ROM init successfull",id);
        return true;
      default:
        PRINTF(L_SYS_EMU,"%04x: ROM init failed: unexpected breakpoint",id);
        break;
      }
    }
  return false;
}
int cN2Prov0101::RunEmu ( unsigned char *  data,
int  len,
unsigned short  load,
unsigned short  run,
unsigned short  stop,
unsigned short  fetch,
int  fetch_len 
) [virtual]

Reimplemented from cN2Prov.

Definition at line 946 of file nagra2-0101.c.

References c6805::AddBreakpoint(), c6805::ClearBreakpoints(), c6805::GetMem(), c6805::GetPc(), cN2Emu::Init(), c6805::Run(), c6805::SetMem(), c6805::SetPc(), and c6805::SetSp().

{
  if(Init(id,102)) {
    SetSp(0x0FFF,0x0EF8);
    SetMem(load,data,len);
    SetPc(run);
    ClearBreakpoints();
    AddBreakpoint(stop);
    if(stop!=0x0000) AddBreakpoint(0x0000);
    AddRomCallbacks();
    while(!Run(100000)) {
      if(GetPc()==0x0000 || GetPc()==stop) {
        GetMem(fetch,data,fetch_len);
        return 1;
        }
      else if(!RomCallbacks()) break;
      }
    }
  return -1;
}
virtual int cN2Prov0101::RunEmu ( unsigned char *  data,
int  len,
unsigned short  load,
unsigned short  run,
unsigned short  stop,
unsigned short  fetch,
int  fetch_len 
) [virtual]

Reimplemented from cN2Prov.

void cN2Prov0101::Stepper ( void  ) [protected, virtual]

Reimplemented from cN2Emu.

Definition at line 997 of file nagra2-0101.c.

References c6805::dr, c6805::Get(), c6805::GetPc(), c6805::loglb, cLineBuff::Printf(), and c6805::Set().

{
  unsigned short pc=GetPc();
  if(pc>=0x93 && pc<=0xE0) {    // pc in EMM data
    unsigned char op=Get(pc);
    if((op&0xF0)==0x00) {       // current opcode BRCLR/BRSET
      int fake=0;               // 1=branch -1=non-branch
      if(Get(pc+3)==0x81)       // next opcode == RTS
        fake=1;                 // fake branch
      else {
        unsigned char off=Get(pc+2);
        unsigned short target=pc+3+off;
        if(off&0x80) target-=0x100;
        if(Get(target)==0x81)   // branch target == RTS
          fake=-1;              // fake non-branch
        }
      if(fake) {
        unsigned short ea=Get(pc+1);
        unsigned char val=Get(dr,ea);
        int bit=1<<((op&0xF)>>1);
        // set/clr bit according to fake-mode and opcode
        if((fake>0 && (op&0x01)) || (fake<0 && !(op&0x01)))  {
          if(val&bit) loglb->Printf("*");
          val&=~bit;
          }
        else {
          if(!(val&bit)) loglb->Printf("*");
          val|=bit;
          }
        Set(dr,ea,val);
        }
      }
    }
}
virtual void cN2Prov0101::Stepper ( void  ) [protected, virtual]

Reimplemented from cN2Emu.

virtual void cN2Prov0101::TimerHandler ( unsigned int  num) [protected, virtual]

Reimplemented from c6805.

void cN2Prov0101::TimerHandler ( unsigned int  num) [protected, virtual]

Reimplemented from c6805.

Definition at line 981 of file nagra2-0101.c.

References cMapMemHW::AddCycles(), c6805::DisableTimers(), c6805::GetPc(), cMapCore::Interruptible(), L_SYS_EMU, PRINTF, and c6805::RaiseException().

{
  if(hwMapper) {
    int mask=hwMapper->AddCycles(num);
    if(mask) DisableTimers(13);
    for(int t=0; mask; mask>>=1,t++)
      if(mask&1) {
        if(t==2) {
          PRINTF(L_SYS_EMU,"0101: Timer interrupt %u @ %04x",t,GetPc());
          RaiseException(9);
          if(Interruptible()) throw(t);
          }
        }
    }
}

Member Data Documentation

int cN2Prov0101::mecmAddr [protected]

Definition at line 479 of file nagra2-0101.c.

Referenced by Algo(), cN2Prov0101(), and cN2Prov0901::cN2Prov0901().

int cN2Prov0101::mecmKeyId [protected]

Definition at line 480 of file nagra2-0101.c.

Referenced by Algo(), cN2Prov0101(), and cN2Prov0901::cN2Prov0901().


The documentation for this class was generated from the following files:
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Defines