예제 #1
0
static int extract_field(char *ptr, int len, char *field,
                            char *val, size_t  maxlen) {
    int c;
    unsigned char *ub, *end;
    unsigned short a1, a2;
    size_t flen, wlen;

    flen = strlen(field);

    ub = (unsigned char *)ptr;
    end = ub + len;
    while (ub < end) {
        c = *ub;

        if (c == 0xff) // flash and OTP are 0xff if they've never been written to
            break;

        a1 = LE16_TO_HOST(*(unsigned short *)(&ub[c+1]));  // read checksum
        a2 = crc16mp(0, ub, c+1);  // calculated checksum

        if (a1 == a2) {
            if (!strncmp((char *)ub + 1, field, flen)) {
                wlen = min_sz(c - flen, maxlen);
                strncpy(val, (char *)ub + 1 + flen, wlen);
                val[wlen] = 0;
                return 0;
            }
        } else {
            log_warning( "%s: Field checksum mismatch\n", __FUNCTION__);
            return BLADERF_ERR_INVAL;
        }
        ub += c + 3; //skip past `c' bytes, 2 byte CRC field, and 1 byte len field
    }
    return BLADERF_ERR_INVAL;
}
예제 #2
0
파일: rx.c 프로젝트: amberadams/bladeRF
/**
 * Peform adjustments on received samples before writing them out:
 *  (1) Mask off FPGA markers
 *  (2) Convert little-endian samples to host endianness, if needed.
 *
 *  @param  buff    Sample buffer
 *  @param  n       Number of samples
 */
static inline void sc16q12_sample_fixup(int16_t *buf, size_t n)
{
    size_t i;

    for (i = 0; i < n; i++) {
        /* I - Mask off the marker and sign extend */
        *buf &= (*buf) & 0x0fff;
        if (*buf & 0x800) {
            *buf |= 0xf000;
        }

        *buf = LE16_TO_HOST(*buf);
        buf++;

        /* Q - Mask off the marker and sign extend */
        *buf = HOST_TO_LE16(*buf) & 0x0fff;
        if (*buf & 0x800) {
            *buf |= 0xf000;
        }

        *buf = LE16_TO_HOST(*buf);
        buf++;
    }
}
예제 #3
0
int nios_get_fpga_version(struct bladerf *dev, struct bladerf_version *ver)
{
    uint32_t regval;
    int status = nios_8x32_read(dev, NIOS_PKT_8x32_TARGET_VERSION, 0, &regval);

    if (status == 0) {
        log_verbose("%s: Read FPGA version word: 0x%08x\n",
                    __FUNCTION__, regval);

        ver->major = (regval >> 24) & 0xff;
        ver->minor = (regval >> 16) & 0xff;
        ver->patch = LE16_TO_HOST(regval & 0xffff);

        snprintf((char*)ver->describe, BLADERF_VERSION_STR_MAX,
                 "%d.%d.%d", ver->major, ver->minor, ver->patch);

        return 0;
    }
예제 #4
0
struct dc_cal_tbl * dc_cal_tbl_load(uint8_t *buf, size_t buf_len)
{
    struct dc_cal_tbl *ret;
    uint32_t i;
    uint16_t magic;

    if (buf_len < DC_CAL_TBL_MIN_SIZE) {
        return NULL;
    }

    memcpy(&magic, buf, sizeof(magic));
    if (LE16_TO_HOST(magic) != DC_CAL_TBL_MAGIC) {
        log_debug("Invalid magic value in cal table: %d\n", magic);
        return NULL;
    }
    buf += sizeof(magic);

    ret = malloc(sizeof(ret[0]));
    if (ret == NULL) {
        return NULL;
    }

    buf += sizeof(uint32_t); /* Skip reserved bytes */

    memcpy(&ret->version, buf, sizeof(ret->version));
    ret->version = LE32_TO_HOST(ret->version);
    buf += sizeof(ret->version);

    memcpy(&ret->n_entries, buf, sizeof(ret->n_entries));
    ret->n_entries = LE32_TO_HOST(ret->n_entries);
    buf += sizeof(ret->n_entries);

    if (buf_len <
         (DC_CAL_TBL_META_SIZE + DC_CAL_TBL_ENTRY_SIZE * ret->n_entries) ) {

        free(ret);
        return NULL;
    }

    ret->entries = malloc(sizeof(ret->entries[0]) * ret->n_entries);
    if (ret->entries == NULL) {
        free(ret);
        return NULL;
    }

    ret->reg_vals.lpf_tuning = *buf++;
    ret->reg_vals.tx_lpf_i = *buf++;
    ret->reg_vals.tx_lpf_q = *buf++;
    ret->reg_vals.rx_lpf_i = *buf++;
    ret->reg_vals.rx_lpf_q = *buf++;
    ret->reg_vals.dc_ref = *buf++;
    ret->reg_vals.rxvga2a_i = *buf++;
    ret->reg_vals.rxvga2a_q = *buf++;
    ret->reg_vals.rxvga2b_i = *buf++;
    ret->reg_vals.rxvga2b_q = *buf++;

    ret->curr_idx = ret->n_entries / 2;
    for (i = 0; i < ret->n_entries; i++) {
        memcpy(&ret->entries[i].freq, buf, sizeof(uint32_t));
        buf += sizeof(uint32_t);

        memcpy(&ret->entries[i].dc_i, buf, sizeof(int16_t));
        buf += sizeof(int16_t);

        memcpy(&ret->entries[i].dc_q, buf, sizeof(int16_t));
        buf += sizeof(int16_t);

        ret->entries[i].freq = LE32_TO_HOST(ret->entries[i].freq);
        ret->entries[i].dc_i = LE32_TO_HOST(ret->entries[i].dc_i);
        ret->entries[i].dc_q = LE32_TO_HOST(ret->entries[i].dc_q);
    }

    return ret;
}
예제 #5
0
LOCAL FileSystemTypes EXT_init(DeviceHandle *deviceHandle, EXTHandle *extHandle)
{
  EXTSuperBlock        extSuperBlock;
  EXT23GroupDescriptor ext23GroupDescriptor;
  EXT4GroupDescriptor  ext4GroupDescriptor;
  uint32               z;

  assert(deviceHandle != NULL);
  assert(extHandle != NULL);
  assert(sizeof(extSuperBlock) == 1024);
  assert(sizeof(ext23GroupDescriptor) == 32);
  assert(sizeof(ext4GroupDescriptor) == 64);

  // read first super-block
  if (Device_seek(deviceHandle,EXT2_FIRST_SUPER_BLOCK_OFFSET) != ERROR_NONE)
  {
    return FILE_SYSTEM_TYPE_UNKNOWN;
  }
  if (Device_read(deviceHandle,&extSuperBlock,sizeof(extSuperBlock),NULL) != ERROR_NONE)
  {
    return FILE_SYSTEM_TYPE_UNKNOWN;
  }

  // check if this a super block
  if ((uint16)(LE16_TO_HOST(extSuperBlock.magic)) != EXT2_SUPER_MAGIC)
  {
    return FILE_SYSTEM_TYPE_UNKNOWN;
  }

  // get block size
  switch (LE32_TO_HOST(extSuperBlock.logBlockSize))
  {
    case 0: extHandle->blockSize =  1*1024; break;
    case 1: extHandle->blockSize =  2*1024; break;
    case 2: extHandle->blockSize =  4*1024; break;
    case 3: extHandle->blockSize =  8*1024; break;
    case 4: extHandle->blockSize = 16*1024; break;
    case 5: extHandle->blockSize = 32*1024; break;
    case 6: extHandle->blockSize = 64*1024; break;
    default:
      return FILE_SYSTEM_TYPE_UNKNOWN;
      break;
  }

  // get ext type: ext2/3/4
#if 0
#warning debug only
fprintf(stderr,"%s, %d: revisionLevel = %d\n",__FILE__,__LINE__,LE32_TO_HOST(extSuperBlock.revisionLevel));
fprintf(stderr,"%s, %d: featureCompatible = 0x%x\n",__FILE__,__LINE__,LE32_TO_HOST(extSuperBlock.featureCompatible));
fprintf(stderr,"%s, %d: featureInCompatible = 0x%x\n",__FILE__,__LINE__,LE32_TO_HOST(extSuperBlock.featureInCompatible));
fprintf(stderr,"%s, %d: featureCompatible & ~EXT2_FEATURE_COMPAT_SUPP = 0x%x\n",__FILE__,__LINE__,LE32_TO_HOST(extSuperBlock.featureCompatible)& ~EXT2_FEATURE_COMPAT_SUPP);
fprintf(stderr,"%s, %d: featureCompatible & ~EXT3_FEATURE_COMPAT_SUPP = 0x%x\n",__FILE__,__LINE__,LE32_TO_HOST(extSuperBlock.featureCompatible)& ~EXT3_FEATURE_COMPAT_SUPP);
fprintf(stderr,"%s, %d: featureCompatible & ~EXT4_FEATURE_COMPAT_SUPP = 0x%x\n",__FILE__,__LINE__,LE32_TO_HOST(extSuperBlock.featureCompatible)& ~EXT4_FEATURE_COMPAT_SUPP);
fprintf(stderr,"%s, %d: featureInCompatible & ~EXT2_FEATURE_INCOMPAT_SUPP = 0x%x\n",__FILE__,__LINE__,LE32_TO_HOST(extSuperBlock.featureInCompatible)& ~EXT2_FEATURE_INCOMPAT_SUPP);
fprintf(stderr,"%s, %d: featureInCompatible & ~EXT3_FEATURE_INCOMPAT_SUPP = 0x%x\n",__FILE__,__LINE__,LE32_TO_HOST(extSuperBlock.featureInCompatible)& ~EXT3_FEATURE_INCOMPAT_SUPP);
fprintf(stderr,"%s, %d: featureInCompatible & ~EXT4_FEATURE_INCOMPAT_SUPP = 0x%x\n",__FILE__,__LINE__,LE32_TO_HOST(extSuperBlock.featureInCompatible)& ~EXT4_FEATURE_INCOMPAT_SUPP);
#endif /* 0 */
  if      (   ((LE32_TO_HOST(extSuperBlock.featureCompatible  ) & ~EXT2_FEATURE_COMPAT_SUPP  ) == 0)
           && ((LE32_TO_HOST(extSuperBlock.featureInCompatible) & ~EXT2_FEATURE_INCOMPAT_SUPP) == 0)
          )
  {
    // ext2
    extHandle->type = FILE_SYSTEM_TYPE_EXT2;
  }
  else if (   (LE32_TO_HOST(extSuperBlock.revisionLevel) == EXT2_REVISION_DYNAMIC)
           && ((LE32_TO_HOST(extSuperBlock.featureCompatible  ) & ~EXT3_FEATURE_COMPAT_SUPP  ) == 0)
           && ((LE32_TO_HOST(extSuperBlock.featureInCompatible) & ~EXT3_FEATURE_INCOMPAT_SUPP) == 0)
          )
  {
    // ext3
    extHandle->type = FILE_SYSTEM_TYPE_EXT3;
  }
  else if (   (LE32_TO_HOST(extSuperBlock.revisionLevel) == EXT2_REVISION_DYNAMIC)
           && ((LE32_TO_HOST(extSuperBlock.featureCompatible  ) & ~EXT4_FEATURE_COMPAT_SUPP  ) == 0)
           && ((LE32_TO_HOST(extSuperBlock.featureInCompatible) & ~EXT4_FEATURE_INCOMPAT_SUPP) == 0)
          )
  {
    // ext4
    extHandle->type = FILE_SYSTEM_TYPE_EXT4;
  }
  else
  {
    return FILE_SYSTEM_TYPE_UNKNOWN;
  }

  // get file system block info, init data
  switch (extHandle->type)
  {
    case FILE_SYSTEM_TYPE_EXT2:
    case FILE_SYSTEM_TYPE_EXT3:
      extHandle->groupDescriptorSize = sizeof(EXT23GroupDescriptor);
      extHandle->blocksPerGroup      = LE32_TO_HOST(extSuperBlock.blocksPerGroup);
      extHandle->firstDataBlock      = (uint64)LE32_TO_HOST(extSuperBlock.firstDataBlock);
      extHandle->totalBlocks         = (uint64)LE32_TO_HOST(extSuperBlock.blocksCount);
      break;
    case FILE_SYSTEM_TYPE_EXT4:
      extHandle->groupDescriptorSize = //((LE32_TO_HOST(extSuperBlock.featureCompatible  ) & EXT4_FEATURE_COMPAT_HAS_JOURNAL) != 0)
                                       ((LE32_TO_HOST(extSuperBlock.featureInCompatible) & EXT4_FEATURE_INCOMPAT_64BIT) != 0)
                                         ? (uint)LE16_TO_HOST(extSuperBlock.groupDescriptorSize)
                                         : sizeof(EXT23GroupDescriptor);
      extHandle->blocksPerGroup      = LE32_TO_HOST(extSuperBlock.blocksPerGroup);
      extHandle->firstDataBlock      = (uint64)LE32_TO_HOST(extSuperBlock.firstDataBlock);
      extHandle->totalBlocks         = LOW_HIGH_TO_UINT64(LE32_TO_HOST(extSuperBlock.blocksCount),
                                                          LE32_TO_HOST(extSuperBlock.blocksCountHigh)
                                                         );
      break;
    default:
      #ifndef NDEBUG
        HALT_INTERNAL_ERROR_UNHANDLED_SWITCH_CASE();
      #endif /* NDEBUG */
      break; /* not reached */
  }
  extHandle->bitmapIndex = -1;

  // validate data
  if (   !((extHandle->groupDescriptorSize > 0) && (extHandle->groupDescriptorSize <= EXT4_MAX_GROUP_DESCRIPTOR_SIZE))
      || !(extHandle->blocksPerGroup > 0)
      || !(extHandle->totalBlocks > 0)
      || !(   ((extHandle->blockSize <= 1024) && (extHandle->firstDataBlock == 1))
           || ((extHandle->blockSize >  1024) && (extHandle->firstDataBlock == 0))
          )
     )
  {
    return FILE_SYSTEM_TYPE_UNKNOWN;
  }

  // read group descriptors and detect bitmap block numbers
  extHandle->bitmapBlocksCount = (extHandle->totalBlocks+extHandle->blocksPerGroup-1)/extHandle->blocksPerGroup;;
  extHandle->bitmapBlocks = (uint64*)malloc(extHandle->bitmapBlocksCount*sizeof(uint64));
  if (extHandle->bitmapBlocks == NULL)
  {
    return FILE_SYSTEM_TYPE_UNKNOWN;
  }
  for (z = 0; z < extHandle->bitmapBlocksCount; z++)
  {
    if (Device_seek(deviceHandle,EXT_BLOCK_TO_OFFSET(extHandle,extHandle->firstDataBlock+1)+(uint64)z*(uint64)extHandle->groupDescriptorSize) != ERROR_NONE)
    {
      free(extHandle->bitmapBlocks);
      return FILE_SYSTEM_TYPE_UNKNOWN;
    }
    switch (extHandle->type)
    {
      case FILE_SYSTEM_TYPE_EXT2:
      case FILE_SYSTEM_TYPE_EXT3:
        if (Device_read(deviceHandle,&ext23GroupDescriptor,sizeof(ext23GroupDescriptor),NULL) != ERROR_NONE)
        {
          free(extHandle->bitmapBlocks);
          return FILE_SYSTEM_TYPE_UNKNOWN;
        }
        extHandle->bitmapBlocks[z] = (uint64)LE32_TO_HOST(ext23GroupDescriptor.blockBitmap);
        break;
      case FILE_SYSTEM_TYPE_EXT4:
        if (Device_read(deviceHandle,&ext4GroupDescriptor,sizeof(ext4GroupDescriptor),NULL) != ERROR_NONE)
        {
          free(extHandle->bitmapBlocks);
          return FILE_SYSTEM_TYPE_UNKNOWN;
        }
        extHandle->bitmapBlocks[z] = LOW_HIGH_TO_UINT64(LE32_TO_HOST(ext4GroupDescriptor.blockBitmap),
                                                        LE32_TO_HOST(ext4GroupDescriptor.blockBitmapHigh)
                                                       );
        break;
      default:
        #ifndef NDEBUG
          HALT_INTERNAL_ERROR_UNHANDLED_SWITCH_CASE();
        #endif /* NDEBUG */
        break; /* not reached */
    }
  }

#if 0
#warning debug only
fprintf(stderr,"\n");
for (z = 0; z < extHandle->bitmapBlocksCount; z++)
{
fprintf(stderr,"%s,%d: z=%d block=%ld used=%d\n",__FILE__,__LINE__,z,extHandle->bitmapBlocks[z],EXT_blockIsUsed(deviceHandle,extHandle,extHandle->bitmapBlocks[z]));
}
#endif /* 0 */

  return extHandle->type;
}