//----------------------------------------------------------------------
static int idaapi notify(processor_t::idp_notify msgid, ...)
{
  va_list va;

  va_start(va, msgid);

  // A well behaving processor module should call invoke_callbacks()
  // in his notify() function. If this function returns 0, then
  // the processor module should process the notification itself
  // Otherwise the code should be returned to the caller:

  int code = invoke_callbacks(HT_IDP, msgid, va);

  if (code)
    return code;

  switch (msgid)
  {
    case processor_t::init:
      inf.mf = 0;
      inf.s_genflags |= INFFL_LZERO;
      helper.create("$ CR16");
    default:
      break;

    case processor_t::term:
      free_ioports(ports, numports);
      break;

    case processor_t::newfile:
      // ask for a  processor from the config file
      // use it to handle ports and registers
      {
        char cfgfile[QMAXFILE];

        get_cfg_filename(cfgfile, sizeof(cfgfile));
        if ( choose_ioport_device(cfgfile, device, sizeof(device), parse_area_line0) )
          set_device_name(device, IORESP_ALL);
      }
      break;

    case processor_t::newprc:
      {
        char buf[MAXSTR];
        if (helper.supval(-1, buf, sizeof(buf)) > 0)
          set_device_name(buf, IORESP_PORT);
      }
      break;

    case processor_t::newseg:
      {
        segment_t *s = va_arg(va, segment_t *);
        // Set default value of DS register for all segments
        set_default_dataseg(s->sel);
      }
      break;
  }
  va_end(va);
  return 1;
}
//----------------------------------------------------------------------
static int idaapi notify(processor_t::idp_notify msgid, ...)
{
  va_list va;
  va_start(va, msgid);

  // A well behaving processor module should call invoke_callbacks()
  // in his notify() function. If this function returns 0, then
  // the processor module should process the notification itself
  // Otherwise the code should be returned to the caller:
  int code = invoke_callbacks(HT_IDP, msgid, va);
  if ( code != 0 )
    return code;

  switch ( msgid )
  {
    case processor_t::init:
      inf.mf = 0;
      inf.s_genflags |= INFFL_LZERO;
      helper.create("$ C39");
    default:
      break;

    case processor_t::term:
      free_ioports(ports, numports);
      break;

    case processor_t::newfile:
      //Выводит длг. окно процессоров, и позволяет выбрать нужный, считывает для выбраного
      //процессора информацию из cfg. По считаной информации подписывает порты и регстры
      {
        char cfgfile[QMAXFILE];
        get_cfg_filename(cfgfile, sizeof(cfgfile));
        if ( choose_ioport_device(cfgfile, device, sizeof(device), parse_area_line0) )
          set_device_name(device, IORESP_ALL);
      }
      break;

    case processor_t::newprc:
      {
        char buf[MAXSTR];
        if ( helper.supval(-1, buf, sizeof(buf)) > 0 )
          set_device_name(buf, IORESP_PORT);
      }
      break;

    case processor_t::newseg:
      {
        segment_t *s = va_arg(va, segment_t *);
        // Set default value of DS register for all segments
        set_default_dataseg(s->sel);
      }
      break;
  }
  va_end(va);
  return(1);
}
Exemple #3
0
//--------------------------------------------------------------------------
void idaapi load_file(linput_t *li, ushort _neflag, const char * /*fileformatname*/)
{
  char line[MAXSTR];
  char *words[MAXSTR];

  neflag = _neflag;
  iscode = (neflag & NEF_CODE) != 0;
  sel = BADSEL;
  sea = BADADDR;
  ea_t ea = 0;
  ea_t top= 0;
  bool use32   = false;
  bool octpref = false;
  bool octnum  = false;
  size_t fill = 0;

  // Since we made all the checks in accept_file,
  // here we don't repeat them

  ssize_t p0len = -1;    // length of the first word's hex part
  char w0sep[10];        // separator after the first word
  w0sep[0] = '\0';
  int nontrivial_line_count = 0;
  while ( qlgets(line, sizeof(line), li) )
  {
    strrpl(line, '-', ' ');
    if ( line[0] == ';' || line[0] == '#' )
      continue;
    int n = make_words(line, words, qnumber(words));
    if ( n == 0 )
      continue;
    nontrivial_line_count++;
    ssize_t bi;
    // od -x format may contain '*' lines which mean repetition
    if ( strcmp(words[0], "*") == 0 && n == 1 )
    {
      fill  = size_t(top - ea);
      octpref = true;             // od -x have octal prefixes
      continue;
    }
    // the first word must be a number (more than one digit)
    char *ptr = words[0];
    uint32 w0 = octpref ? oct(ptr) : hex(ptr);
    p0len = ptr - words[0];
    // take the separator from the first line
    if ( nontrivial_line_count == 1 )
      qstrncpy(w0sep, ptr, sizeof(w0sep));

    // process '*' and fill the gap
    if ( fill > 0 )
    {
      while ( top < w0 )
      {
        ea = top;
        top = ea + fill;
        copy(ea, top);
      }
    }

    int idx = 0;
    if ( w0sep[0] != '\0' || p0len >= 4 )
    {
      if ( nontrivial_line_count > 1 && !octpref && top != w0 )
      {
        // strange, the sequence is not contiguous
        // check if the prefixes are octal (od -x)
        ptr = words[0];
        if ( oct(ptr) == top )
        {
          octpref = true;
          ptr = words[0];
          w0 = oct(ptr);
        }
      }
      ea = w0;
      idx = 1;
    }
    else
    {
      ea = top;
    }
    for ( bi=0; idx < n; idx++ ) //lint !e443
    {
      ptr = words[idx];
      if ( nontrivial_line_count == 1 && !octnum && strlen(ptr) == 6 )
      {
        oct(ptr);
        if ( ptr-words[idx] == 6 )
          octnum = true;
        ptr = words[idx];
//        msg("ptr=%s octnum=%d\n", ptr, octnum);
      }
      uint32 b = octnum ? oct(ptr) : hex(ptr);
      ssize_t nc = ptr - words[idx];
      if ( nc < 2 )
      {
        // we tolerate one-letter separators between numbers
        if ( words[idx][1] == '\0' && strchr("\xA6|-:", words[idx][0]) != NULL )
          continue;
        break;
      }
      nc /= octnum ? 3 : 2;             // number of bytes
      *(uint32 *)&bytes[bi] = b;
      bi += nc;
    }
    top = ea + bi;
    copy(ea, top);
  }

  if ( eea >= 0x10000 || p0len > 4 )
    use32 = true;
  if ( neflag & NEF_SEGS )
  {
    if ( use32 )
    {
      set_segm_addressing(getseg(sea), 1);
      if ( ph.id == PLFM_386 ) inf.lflags |= LFLG_PC_FLAT;
    }
    set_default_dataseg(sel);
  }
  if ( (neflag & NEF_RELOAD) == 0 )
    create_filename_cmt();
}
//----------------------------------------------------------------------
static int idaapi notify(processor_t::idp_notify msgid, ...)
{
  va_list va;
  va_start(va, msgid);

  // A well behaving processor module should call invoke_callbacks()
  // in his notify() function. If this function returns 0, then
  // the processor module should process the notification itself
  // Otherwise the code should be returned to the caller:

  int code = invoke_callbacks(HT_IDP, msgid, va);
  if ( code )
    return code;

  switch ( msgid )
  {
    case processor_t::init:
      inf.mf = 0;
      helper.create("$ 78k0s");
    default:
      break;

    case processor_t::term:
      free_ioports(ports, numports);
      break;

    case processor_t::newfile:
      {
      //функция "выбирает" из указанного файла *.cfg все записи(процессора)
      //и отображает их в диалоговом окне, в котором пользователь может выбрать
      //нужный ему процессор. После выбора имя процессора заносится в переменную device
      //Поумолчанию в DLG выделен процессор который указан в переменной .default
      //которая распологается в начале файла *.cfg
      inf.s_genflags |= INFFL_LZERO;
      char cfgfile[QMAXFILE];
      get_cfg_filename(cfgfile, sizeof(cfgfile));
      if ( choose_ioport_device(cfgfile, device, sizeof(device), parse_area_line0) )
        //Устанавливает в ядре иды имя выбранного процессора
        //Вычитывает все "записи"(порты)  относящиеся к этому процессору
        //И подписывает в файле все байты вычитанные из *.cfg файла
        set_device_name(device, IORESP_ALL);
      } break;

    case processor_t::newprc:
      {
      char buf[MAXSTR];
      if ( helper.supval(-1, buf, sizeof(buf)) > 0 )
        set_device_name(buf, IORESP_PORT);
      } break;

    case processor_t::newseg:    // new segment
      {
      segment_t *s = va_arg(va, segment_t *);
      // Set default value of DS register for all segments
      set_default_dataseg(s->sel);
      } break;

  }
  va_end(va);
  return 1;
}
Exemple #5
0
//--------------------------------------------------------------------------
void idaapi load_file(linput_t *li, ushort neflag, const char * /*fileformatname*/)
{
  char line[BUFFSIZE], bigaddr = 0;
  ea_t addr, startEA = toEA(inf.baseaddr, 0), endEA = 0, seg_start = 0;
  char rstart = (inf.filetype == f_SREC) ? 'S' :
                                     ((inf.filetype == f_HEX) ? ':' : ';');
  register char *p;

  memset(&lc, 0, sizeof(local_data));
  inf.startIP = BADADDR;          // f_SREC without start record

  bool iscode = (neflag & NEF_CODE) != 0;
  int nb = iscode ? ph.cnbits : ph.dnbits;      // number of bits in a byte
  int bs = (nb + 7) / 8;                        // number of bytes
  sel_t sel = setup_selector(startEA >> 4);
  bool segment_created = false;

  bool cvt_to_bytes = false;
  if ( ph.id == PLFM_PIC )
  {
    // pic12xx and pic16xx use 12-bit and 14-bit words in program memory
    // pic18xx uses 16-bit opcodes but byte addressing
    if ( strncmp(inf.procName, "PIC18", 5) != 0 )
    {
      static const char form[] =
//      "PIC HEX file addressing mode\n"
//      "\n"
      "There are two flavors of HEX files for PIC: with word addressing\n"
      "and with byte addressing. It is not possible to recognize the\n"
      "flavor automatically. Please specify what addressing mode should\n"
      "be used to load the input file. If you don't know, try both and\n"
      "choose the one which produces the more meaningful result\n";
      int code = askbuttons_c("~B~yte addressing",
                              "~W~ord addressing",
                              "~C~ancel", 1, form);
      switch ( code )
      {
        case 1:
          break;
        case 0:
          cvt_to_bytes = true;
          break;
        default:
          loader_failure(NULL);
      }
    }
  }

  ea_t subs_addr = 0;
  for(lc.ln = 1; qlgets(p = line, BUFFSIZE, li); lc.ln++)
  {
    while ( *p == ' ' ) ++p;
    if ( *p == '\n' || *p == '\r' ) continue;
    if ( *p++ != rstart) errfmt( );

    int sz = 2;
    int mode = (inf.filetype == f_SREC) ? (uchar)*p++ : 0x100;
    lc.ptr = p;
    hexdata(0);
    if ( mode == 0x100 )
    {
      if ( !lc.len ) break;
      lc.len += 2;
      if ( inf.filetype == f_HEX )
        ++lc.len;
    }
    else
    {
      switch ( mode )
      {
        default:
            errfmt();

        case '0':
        case '5':
          continue;

        case '3':
        case '7':
          ++sz;
        case '2':
        case '8':
          ++sz;
        case '1':
        case '9':
          if ( mode > '3' ) mode = 0;
          --lc.len;
          break;
      }
    }
    addr = hexdata(sz) / bs;
    if ( !mode )
    {
      inf.startIP = addr;
      continue;
    }

    if ( inf.filetype == f_HEX )
    {
      int type = hexdata(1);      // record type
      switch ( type )
      {
        case 0xFF:                // mitsubishi hex format
        case 4:                   // Extended linear address record
          subs_addr = hexdata(2) << 16;
          break;
        case 2:                   // Extended segment address record
          subs_addr = hexdata(2) << 4;
          break;
      }
      if ( type != 0 )
      {
        if ( type == 1 )
          break;                  // end of file record
        continue;                 // not a data record
      }
    }
    addr += subs_addr / bs;
    if ( lc.len )
    {
      ea_t top = addr + lc.len / bs;
      p = line;
      while ( lc.len )
      {
        *p++ = (uchar)hexdata(1);
        if ( cvt_to_bytes ) // pic
          *p++ = '\0';
      }
      if ( top >= 0x10000l ) bigaddr = 1;
      addr += startEA;
      showAddr(addr);
      if ( (top += startEA) > endEA || !segment_created )
      {
        endEA = top;
        if ( neflag & NEF_SEGS )
        {
          if ( !segment_created )
          {
            if ( !add_segm(sel, addr, endEA, NULL, iscode ? CLASS_CODE : CLASS_DATA)) loader_failure(NULL );
            segment_created = true;
            seg_start = addr;
          }
          else
            set_segm_end(get_first_seg()->startEA, endEA, SEGMOD_KILL);
        }
      }
      if ( seg_start > addr )
      {
        set_segm_start(get_first_seg()->startEA, addr, SEGMOD_KILL);
        seg_start = addr;
      }
      mem2base(line, addr, top, -1);
    }
    {
      ushort chi;       // checksum
      ++lc.len;
      switch ( inf.filetype ) {
        case f_SREC:
          chi = (uchar)(~lc.sum);
          chi ^= (uchar)hexdata(1);
          break;
        case f_HEX:
          hexdata(1);
          chi = (uchar)lc.sum;
          break;
        default:  //MEX
          ++lc.len;
          chi = lc.sum;
          chi -= (ushort)hexdata(2);
          break;
      }
      if ( chi ) {
        static char first = 0;
        if ( !first ) {
          ++first;
          warning("Bad hex input file checksum, line %u. Ignore?", lc.ln);
        }
      }
    }
  }

  if ( (neflag & NEF_SEGS) != 0 )
  {
    if ( bigaddr )
    {
      set_segm_addressing(get_first_seg(), 1);
      if ( ph.id == PLFM_386 )
        inf.lflags |= LFLG_PC_FLAT;
    }
    set_default_dataseg(sel);
    inf.start_cs  = sel;
  }
  else
  {
    enable_flags(startEA, endEA, STT_CUR);
  }
  inf.af &= ~AF_FINAL;                    // behave as a binary file

  create_filename_cmt();
}
Exemple #6
0
static int notify(processor_t::idp_notify msgid, ...)
{
    static int first_time = 1;
    va_list va;
    va_start(va, msgid);

// A well behaving processor module should call invoke_callbacks()
// in his notify() function. If this function returns 0, then
// the processor module should process the notification itself
// Otherwise the code should be returned to the caller:

    int code = invoke_callbacks(HT_IDP, msgid, va);
    if ( code ) return code;

    switch ( msgid )
    {
    case processor_t::init:
        helper.create("$ intel 8051");
        inf.mf = 1;       // Set a big endian mode of the IDA kernel
    default:
        break;

    case processor_t::term:
        free_ioports(ports, numports);
        break;

    case processor_t::newfile:
    {
        segment_t *sptr = get_first_seg();
        if ( sptr != NULL )
        {
            if ( sptr->startEA-get_segm_base(sptr) == 0 )
            {
                inf.beginEA = sptr->startEA;
                inf.startIP = 0;
                for ( int i=0; i < qnumber(entries); i++ )
                {
                    if ( entries[i].proc > ptype )
                        continue;
                    ea_t ea = inf.beginEA+entries[i].off;
                    if ( isEnabled(ea) && get_byte(ea) != 0xFF )
                    {
                        add_entry(ea, ea, entries[i].name, 1);
                        set_cmt(ea, entries[i].cmt, 1);
                    }
                }
            }
        }
        segment_t *scode = get_first_seg();
        set_segm_class(scode, "CODE");

        if ( ptype > prc_51 )
        {
            AdditionalSegment(0x10000-256-128, 256+128, "RAM");
            if ( scode != NULL )
            {
                ea_t align = (scode->endEA + 0xFFF) & ~0xFFF;
                if ( getseg(align-7) == scode )     // the code segment size is
                {   // multiple of 4K or near it
                    uchar b0 = get_byte(align-8);
                    // 251:
                    //  0  : 1-source, 0-binary mode
                    //  6,7: must be 1s
                    // 82930:
                    //  0  : 1-source, 0-binary mode
                    //  7  : must be 1s
//              uchar b1 = get_byte(align-7);
                    // 251
                    //  0: eprommap 0 - FE2000..FE4000 is mapped into 00E000..100000
                    //              1 - .............. is not mapped ...............
                    //  1: must be 1
                    //  3:
                    //  2: must be 1
                    //  4: intr 1 - upon interrupt PC,PSW are pushed into stack
                    //          0 - upon interrupt only PC is pushed into stack
                    //  5: must be 1
                    //  6: must be 1
                    //  7: must be 1
                    // 82930:
                    //  3: must be 1
                    //  5: must be 1
                    //  6: must be 1
                    //  7: must be 1
//                msg("b0=%x b1=%x\n", b0, b1);
//              if ( (b0 & 0x80) == 0x80 && (b1 & 0xEA) == 0xEA )
                    {   // the init bits are correct
                        char pname[sizeof(inf.procName)+1];
                        inf.get_proc_name(pname);
                        char ntype = (b0 & 1) ? 's' : 'b';
                        char *ptr = tail(pname)-1;
                        if ( ntype != *ptr
                                && askyn_c(1,
                                           "HIDECANCEL\n"
                                           "The input file seems to be for the %s mode of the processor. "
                                           "Do you want to change the current processor type?",
                                           ntype == 's' ? "source" : "binary") > 0 )
                        {
                            *ptr = ntype;
                            first_time = 1;
                            set_processor_type(pname, SETPROC_COMPAT);
                        }
                    }
                }
            }
        }

        // the default data segment will be INTMEM
        {
            segment_t *s = getseg(intmem);
            if ( s != NULL )
                set_default_dataseg(s->sel);
        }

        if ( choose_ioport_device(cfgname, device, sizeof(device), parse_area_line0) )
            set_device_name(device, IORESP_ALL);

        if ( get_segm_by_name("RAM") == NULL )
            AdditionalSegment(256, 0, "RAM");
        if ( get_segm_by_name("FSR") == NULL )
            AdditionalSegment(128, 128, "FSR");
        setup_data_segment_pointers();
    }
    break;

    case processor_t::oldfile:
        setup_data_segment_pointers();
        break;

    case processor_t::newseg:
        // make the default DS point to INTMEM
        // (8051 specific issue)
    {
        segment_t *newseg = va_arg(va, segment_t *);
        segment_t *intseg = getseg(intmem);
        if ( intseg != NULL )
            newseg->defsr[rVds-ph.regFirstSreg] = intseg->sel;
    }
    break;

    case processor_t::newprc:
    {
        processor_subtype_t prcnum = processor_subtype_t(va_arg(va, int));
        if ( !first_time && prcnum != ptype )
        {
            warning("Sorry, it is not possible to change" // (this is 8051 specific)
                    " the processor mode on the fly."
                    " Please reload the input file"
                    " if you want to change the processor.");
            return 0;
        }
        first_time = 0;
        ptype = prcnum;
    }
    break;

    case processor_t::newasm:    // new assembler type
    {
        char buf[MAXSTR];
        if ( helper.supval(-1, buf, sizeof(buf)) > 0 )
            set_device_name(buf, IORESP_NONE);
    }
    break;

    case processor_t::move_segm:// A segment is moved
        // Fix processor dependent address sensitive information
        // args: ea_t from - old segment address
        //       segment_t - moved segment
    {
        // ea_t from    = va_arg(va, ea_t);
        // segment_t *s = va_arg(va, segment_t *);

        // Add commands to adjust your internal variables here
        // Most of the time this callback will be empty
        //
        // If you keep information in a netnode's altval array, you can use
        //      node.altshift(from, s->startEA, s->endEA - s->startEA);
        //
        // If you have a variables pointing to somewhere in the disassembled program memory,
        // you can adjust it like this:
        //
        //      asize_t size = s->endEA - s->startEA;
        //      if ( var >= from && var < from+size )
        //        var += s->startEA - from;
    }
    break;

    case processor_t::is_sane_insn:
        // is the instruction sane for the current file type?
        // arg:  int no_crefs
        // 1: the instruction has no code refs to it.
        //    ida just tries to convert unexplored bytes
        //    to an instruction (but there is no other
        //    reason to convert them into an instruction)
        // 0: the instruction is created because
        //    of some coderef, user request or another
        //    weighty reason.
        // The instruction is in 'cmd'
        // returns: 1-ok, <=0-no, the instruction isn't
        // likely to appear in the program
    {
        int no_crefs = va_arg(va, int);
        return is_sane_insn(no_crefs);
    }
    }
    va_end(va);

    return(1);
}
Exemple #7
0
//----------------------------------------------------------------------
static int idaapi nec850_notify(processor_t::idp_notify msgid, ...)
{
  va_list va;
  va_start(va, msgid);

  // A well behaving processor module should call invoke_callbacks()
  // in his notify() function. If this function returns 0, then
  // the processor module should process the notification itself
  // Otherwise the code should be returned to the caller:

  int code = invoke_callbacks(HT_IDP, msgid, va);
  if ( code )
    return code;

  switch(msgid)
  {
  case processor_t::init:
    inf.mf = 0;
    prog_pointers.create("$ prog pointers");
    break;

  case processor_t::is_sane_insn:
    {
      int no_crefs = va_arg(va, int);
      return nec850_is_sane_insn(no_crefs);
    }

  case processor_t::newprc:
    {
      int procnum = va_arg(va, int);
      is_v850e = procnum == 0;
      break;
    }
  case processor_t::term:
    break;

  // save database
  case processor_t::closebase:
  case processor_t::savebase:
    prog_pointers.altset(GP_EA_IDX, g_gp_ea);
    break;

  // old file loaded
  case processor_t::oldfile:
    g_gp_ea = prog_pointers.altval(GP_EA_IDX);
    break;

  case processor_t::newseg:
    {
      segment_t *s = va_arg(va, segment_t *);
      // Set default value of DS register for all segments
      set_default_dataseg(s->sel);
    }
  // A segment is moved
  //case processor_t::move_segm:
  //  // Fix processor dependent address sensitive information
  //  // args: ea_t from - old segment address
  //  //       segment_t - moved segment
  //  {
  //    ea_t from    = va_arg(va, ea_t);
  //    segment_t *s = va_arg(va, segment_t *);
  //    // adjust gp_ea
  //  }
  //  break;
  default:
    break;
  }
  va_end(va);
  return 1;
}