Example #1
0
/**
 * Sends one data id/value pair.
 */
void sPort_send_data(int uart, uint16_t id, uint32_t data)
{
	union {
		uint32_t word;
		uint8_t byte[4];
	} buf;

	/* send start byte */
	static const uint8_t c = 0x10;
	write(uart, &c, 1);

	/* init crc */
	uint16_t crc = c;

	buf.word = id;

	for (int i = 0; i < 2; i++) {
		update_crc(&crc, buf.byte[i]);
		sPort_send_byte(uart, buf.byte[i]);      /* LSB first */
	}

	buf.word = data;

	for (int i = 0; i < 4; i++) {
		update_crc(&crc, buf.byte[i]);
		sPort_send_byte(uart, buf.byte[i]);      /* LSB first */
	}

	sPort_send_byte(uart, 0xFF - crc);
}
Example #2
0
static inline void
default_register_write(bitstream_parser_t *parser,
		       const register_index_t reg,
		       const gsize length) {
  bytearray_t *ba = &parser->ba;
  xil_register_t *regp = &parser->registers[reg];
  unsigned i;

  debit_log(L_BITSTREAM,"Writing %zd words to register %s",
	    length, reg_names[reg]);

  for (i = 0; i < length; i++) {
    guint32 val = bytearray_get_uint32(ba);
    update_crc(parser, reg, val);

    switch (reg) {
    case CRC:
      /* CRC write does not really write the crc register, only updates
	 it as a side-effect */
      break;
    case LOUT: {
      gchar far_name[32];
      snprintf_far(far_name, sizeof(far_name), val);
      g_print("LOUT: %08x\n", val);
      g_print("LOUT as FAR is [%i], %s\n", val, far_name);
      /* Fall through */
    }
    default:
      regp->value = val;
    }
  }
  parser->active_length -= length;

}
Example #3
0
int
dc_parse (const char *templ_file)
{
    char ip[0x8000], dest_name[FILENAME_MAX];

    if (access (templ_file, F_OK) == -1)
    {
        int i = 0;

        printf ("Creating empty template file: \"%s\"\n", templ_file);

        for (i = 0; templ[i].name; i++)
            set_property (templ_file, templ[i].name, templ[i].def, templ[i].comment);

        printf (ucon64_msg[WROTE], templ_file);
    }

    if (parse_templ (templ_file, ip) == -1)
        return -1;

    update_crc (ip);

    strcpy (dest_name, "ip.bin");
    ucon64_file_handler (dest_name, NULL, 0);

    ucon64_fwrite (ip, 0, 0x8000, dest_name, "wb");

    printf (ucon64_msg[WROTE], dest_name);
    return 0;
}
static unsigned long get_dir_crc(const char *mdir)
{
	unsigned long CRC=0;
	DIR *sdir;
	struct dirent *sdirent;
	
	CRC = 0xffffffff;
	make_crc_table();
	sdir = opendir(mdir);
	if(NULL == sdir) goto out;
	while((sdirent = readdir(sdir))){
		struct stat st;
		char path[256], buf[512];
		if (!strcmp(sdirent->d_name, ".") || !strcmp(sdirent->d_name, "..")) {
			continue;
		}
		memset(buf, '\0', sizeof(buf));
		sprintf(path, "%s/%s", mdir, sdirent->d_name);
		if(!(stat(path, &st))){
			int offset=0, len;
			memset(buf, '\0', sizeof(buf));
			sprintf(buf, "%s.%ld", path, st.st_mtime);
			len = strlen(buf);
			while(offset<len){
				CRC = update_crc(CRC, (char *)buf+offset, 4);
				offset += 4;
			}
		}
	}
out:
	if(sdir) closedir(sdir);
	return CRC;
}
Example #5
0
byte Dynamixel::txPacket(byte bID, byte bInstruction, int bParameterLength) {

    word bCount,bCheckSum,bPacketLength;
    byte offsetParamIndex;
    if(mPacketType == DXL_PACKET_TYPE1) { //dxl protocol 1.0
        mTxBuffer[0] = 0xff;
        mTxBuffer[1] = 0xff;
        mTxBuffer[2] = bID;
        mTxBuffer[3] = bParameterLength+2; //2(byte) <- instruction(1byte) + checksum(1byte)
        mTxBuffer[4] = bInstruction;

        offsetParamIndex = 5;
        bPacketLength = bParameterLength+2+4;

    } else { //dxl protocol 2.0
        mTxBuffer[0] = 0xff;
        mTxBuffer[1] = 0xff;
        mTxBuffer[2] = 0xfd;
        mTxBuffer[3] = 0x00;
        mTxBuffer[4] = bID;
        //get parameter length
        mTxBuffer[5] = DXL_LOBYTE(bParameterLength+3);// 3(byte) <- instruction(1byte) + checksum(2byte)
        mTxBuffer[6] = DXL_HIBYTE(bParameterLength+3);
        mTxBuffer[7] = bInstruction;

        offsetParamIndex = 8;
        bPacketLength = bParameterLength+3+7; //parameter length 3bytes, 7bytes =  packet header 4bytes, ID 1byte,  length 2bytes
    }

    //copy parameters from mParamBuffer to mTxBuffer
    for(bCount = 0; bCount < bParameterLength; bCount++)
    {
        mTxBuffer[bCount+offsetParamIndex] = mParamBuffer[bCount];
    }

    if(mPacketType == DXL_PACKET_TYPE1) {
        bCheckSum = 0;
        for(bCount = 2; bCount < bPacketLength-1; bCount++) { //except 0xff,checksum
            bCheckSum += mTxBuffer[bCount];
        }
        mTxBuffer[bCount] = ~bCheckSum; //Writing Checksum with Bit Inversion
    } else {
        bCheckSum = update_crc(0, mTxBuffer, bPacketLength-2);  // -2 : except CRC16
        mTxBuffer[bPacketLength-2] = DXL_LOBYTE(bCheckSum);     // last - 2
        mTxBuffer[bPacketLength-1] = DXL_HIBYTE(bCheckSum);     // last - 1
    }
    //TxDStringC("bPacketLength = ");TxDHex8C(bPacketLength);TxDStringC("\r\n");
    this->dxlTxEnable(); // this define is declared in dxl.h


    for(bCount = 0; bCount < bPacketLength; bCount++)
    {
        writeRaw(mTxBuffer[bCount]);
    }

    this->dxlTxDisable();// this define is declared in dxl.h

    return(bPacketLength); // return packet length
}
Example #6
0
/**
 * prog_eeprom - write the EEPROM from memory
 */
static int prog_eeprom(void)
{
	int ret = 0;
	int i;
	void *p;
#ifdef CONFIG_SYS_EEPROM_BUS_NUM
	unsigned int bus;
#endif

	/* Set the reserved values to 0xFF   */
#ifdef CONFIG_SYS_I2C_EEPROM_NXID
	e.res_0 = 0xFF;
	memset(e.res_1, 0xFF, sizeof(e.res_1));
#else
	memset(e.res_0, 0xFF, sizeof(e.res_0));
#endif
	update_crc();

#ifdef CONFIG_SYS_EEPROM_BUS_NUM
	bus = i2c_get_bus_num();
	i2c_set_bus_num(CONFIG_SYS_EEPROM_BUS_NUM);
#endif

	/*
	 * The AT24C02 datasheet says that data can only be written in page
	 * mode, which means 8 bytes at a time, and it takes up to 5ms to
	 * complete a given write.
	 */
	for (i = 0, p = &e; i < sizeof(e); i += 8, p += 8) {
		ret = i2c_write(CONFIG_SYS_I2C_EEPROM_ADDR, i, CONFIG_SYS_I2C_EEPROM_ADDR_LEN,
			p, min((sizeof(e) - i), 8));
		if (ret)
			break;
		udelay(5000);	/* 5ms write cycle timing */
	}

	if (!ret) {
		/* Verify the write by reading back the EEPROM and comparing */
		struct eeprom e2;

		ret = i2c_read(CONFIG_SYS_I2C_EEPROM_ADDR, 0,
			CONFIG_SYS_I2C_EEPROM_ADDR_LEN, (void *)&e2, sizeof(e2));
		if (!ret && memcmp(&e, &e2, sizeof(e)))
			ret = -1;
	}

#ifdef CONFIG_SYS_EEPROM_BUS_NUM
	i2c_set_bus_num(bus);
#endif

	if (ret) {
		printf("Programming failed.\n");
		has_been_read = 0;
		return -1;
	}

	printf("Programming passed.\n");
	return 0;
}
Example #7
0
 bool read_chunk(PNGChunk& ch, File& f) {
   delete[] ch.data;
   ch.data = NULL;
   if (f.read(&ch.length, 4) != 4) return false;
   if (f.read(&ch.type, 4) != 4) return false;
   flip(ch.length);
   ch.data = new uint8[ch.length];
   if (f.read(ch.data, ch.length) != ch.length) return false;
   if (f.read(&ch.crc, 4) != 4) return false;
   flip(ch.crc);
   uint32 crc = update_crc(0xFFFFFFFF, &ch.type, 4);
   crc = update_crc(crc, ch.data, ch.length);
   crc = ~crc;
   if (crc != ch.crc) return false;
   flip(ch.type);
   return true;
 }
Example #8
0
/* Since we don't know the final size of the packet until it is 
 * ready to be sent we need to have additional processing  
 * at dequeue time
 */
void prepare_coalesced_pkt(viadev_connection_t * c, vbuf *v) {

#ifdef ADAPTIVE_RDMA_FAST_PATH
    if(BUSY_FLAG == v->padding) {
        int len = v->len;
        VBUF_FLAG_TYPE flag;

        v->desc.u.sr.wr_id = (aint_t) v;
        v->desc.sg_entry.length = 
            v->len + VBUF_FAST_RDMA_EXTRA_BYTES;

        /* update the flags */

        if ((int) *(VBUF_FLAG_TYPE *) (v->buffer + len) == len) {
            flag = (VBUF_FLAG_TYPE) (len + FAST_RDMA_ALT_TAG);
        } else {
            flag = (VBUF_FLAG_TYPE) len;
        }

        /* head */
        *(v->head_flag) = flag;
        /* tail */
        *(VBUF_FLAG_TYPE *) (v->buffer + len) = flag;
        D_PRINT("Sending coalesced over rfp\n");
    } else 
#endif
    {
        D_PRINT("Sending coalesced over send/recv\n");
        vbuf_init_send(v, v->len);
        v->desc.sg_entry.length = v->len;
    }

#ifndef _IBM_EHCA_
    if(v->desc.sg_entry.length < c->max_inline) {
        v->desc.u.sr.send_flags =
            IBV_SEND_SIGNALED | IBV_SEND_INLINE;
    } else 
#endif
    {
        v->desc.u.sr.send_flags = IBV_SEND_SIGNALED;
    }

#ifdef MEMORY_RELIABLE
    {
        viadev_packet_header *p = (viadev_packet_header *) v->buffer;
        p->crc32 = update_crc(1, (void *) ((char *)VBUF_BUFFER_START(v) +
                    sizeof(viadev_packet_header)),
                v->desc.sg_entry.length - sizeof(viadev_packet_header));
        p->dma_len = v->desc.sg_entry.length - sizeof(viadev_packet_header);
    }
#endif


    D_PRINT("coalesce send len: %d\n", v->desc.sg_entry.length);


    /* should be all set to be sent */
}
Example #9
0
uint8_t calc_crc(const void *data, uint8_t len) {
	uint8_t crc;

	crc = 0;
	for (;len > 0; --len) {
		crc = update_crc(*(uint8_t*)data++, crc);
	}
	return crc;
}
Example #10
0
 void write_chunk(uint32 type, uint32 length, void const* data, File& f) {
   flip(type);
   f.write32(flipped(length));
   f.write32(type);
   f.write(data, length);
   uint32 crc = update_crc(0xFFFFFFFF, &type, 4);
   crc = ~update_crc(crc, data, length);
   f.write32(flipped(crc));
 }
Example #11
0
// this function works over a whole array of chars
int16 _CRC16_(char* array, int len)
{
    int16	test_crc = 0xFFFF;

    while (len--) {
        update_crc(test_crc,*array++);
    }

    return test_crc;
}
void write_chunk(uint32 type, uint32 length, void const* data, File* f)
{
  type = flip_int(type);
  f->writeInt(flip_int(length));
  f->writeInt(type);
  f->write(data, length);
  uint32 crc = update_crc(0xFFFFFFFF, &type, 4);
  crc = ~update_crc(crc, data, length);
  f->writeInt(flip_int(crc));
}
Example #13
0
/**
 * set_mac_address - stores a MAC address into the EEPROM
 *
 * This function takes a pointer to MAC address string
 * (i.e."XX:XX:XX:XX:XX:XX", where "XX" is a two-digit hex number) and
 * stores it in the MAC address field in the EEPROM local copy.
 */
static void set_mac_address(const char *string)
{
	char *p = (char *) string;
	unsigned int i;

	for (i = 0; *p && (i < 6); i++) {
		e.mac[i] = simple_strtoul(p, &p, 16);
		if (*p == ':')
			p++;
	}

	update_crc();
}
Example #14
0
/**
 * set_date - stores the build date into the EEPROM
 *
 * This function takes a pointer to a string in the format "YYMMDDhhmmss"
 * (2-digit year, 2-digit month, etc), converts it to a 6-byte BCD string,
 * and stores it in the build date field of the EEPROM local copy.
 */
static void set_date(const char *string)
{
    unsigned int i;

    if (strlen(string) != 12) {
        printf("Usage: mac date YYMMDDhhmmss\n");
        return;
    }

    for (i = 0; i < 6; i++)
        e.date[i] = h2i(string[2 * i]) << 4 | h2i(string[2 * i + 1]);

    update_crc();
}
bool is_packet_valid( protocal_sdk_uart_header *header )
{
    char buffer[1024] = {0};
    if ( header->m_length > 1024 )
    {
        return false;
    }

    memcpy( buffer, header, header->m_length );
    word32      crc32;            // 32-bit CRC value
    crc32 = update_crc( -1, (byte*)buffer, header->m_length );
    char *p1 = buffer + header->m_length - 2, *p2 = (char*)header + header->m_length - 2;
    return ( p1[0] == p2[0] && p1[1] == p2[1] );
}
Example #16
0
/**
 * set_mac_address - stores a MAC address into the EEPROM
 *
 * This function takes a pointer to MAC address string
 * (i.e."XX:XX:XX:XX:XX:XX", where "XX" is a two-digit hex number) and
 * stores it in one of the MAC address fields of the EEPROM local copy.
 */
static void set_mac_address(unsigned int index, const char *string)
{
    char *p = (char *) string;
    unsigned int i;

    if (!string) {
        printf("Usage: mac <n> XX:XX:XX:XX:XX:XX\n");
        return;
    }

    for (i = 0; *p && (i < 6); i++) {
        e.mac[index][i] = simple_strtoul(p, &p, 16);
        if (*p == ':')
            p++;
    }

    update_crc();
}
Example #17
0
/**
 * prog_eeprom - write the EEPROM from memory
 */
static int prog_eeprom(void)
{
	int ret = 0;
#ifdef CONFIG_SYS_EEPROM_BUS_NUM
	unsigned int bus;
#endif

	update_crc();

#ifdef CONFIG_SYS_EEPROM_BUS_NUM
	bus = i2c_get_bus_num();
	i2c_set_bus_num(CONFIG_SYS_EEPROM_BUS_NUM);
#endif

	ret = eeprom_write(CONFIG_SYS_I2C_EEPROM_ADDR, 0,
		(uchar *)&e, sizeof(e));

	if (!ret) {
		/* Verify the write by reading back the EEPROM and comparing */
		struct eeprom e2;
#ifdef DEBUG
		printf("%s verifying...\n", __func__);
#endif
		ret = eeprom_read(CONFIG_SYS_I2C_EEPROM_ADDR, 0,
			(uchar *)&e2, sizeof(e2));

		if (!ret && memcmp(&e, &e2, sizeof(e)))
			ret = -1;
	}

#ifdef CONFIG_SYS_EEPROM_BUS_NUM
	i2c_set_bus_num(bus);
#endif

	if (ret) {
		printf("Programming failed.\n");
		has_been_read = 0;
		return -1;
	}

	printf("Programming passed.\n");
	return 0;
}
Example #18
0
static void hubsan_build_bind_packet(HubsanSession *session, u8 state)
{
    session->packet[0] = state;
    session->packet[1] = session->channel;
    session->packet[2] = (session->sessionid >> 24) & 0xff;
    session->packet[3] = (session->sessionid >> 16) & 0xff;
    session->packet[4] = (session->sessionid >>  8) & 0xff;
    session->packet[5] = (session->sessionid >>  0) & 0xff;
    session->packet[6] = 0x08;
    session->packet[7] = 0xe4; //???
    session->packet[8] = 0xea;
    session->packet[9] = 0x9e;
    session->packet[10] = 0x50;
    session->packet[11] = (session->txid >> 24) & 0xff;
    session->packet[12] = (session->txid >> 16) & 0xff;
    session->packet[13] = (session->txid >>  8) & 0xff;
    session->packet[14] = (session->txid >>  0) & 0xff;
    update_crc(session);
}
Example #19
0
/**
 * prog_eeprom - write the EEPROM from memory
 */
static int prog_eeprom(void)
{
    int ret = 0; /* shut up gcc */
    int i;
    void *p;
#ifdef CONFIG_SYS_EEPROM_BUS_NUM
    unsigned int bus;
#endif

    /* Set the reserved values to 0xFF   */
#ifdef CONFIG_SYS_I2C_EEPROM_NXID
    e.res_0 = 0xFF;
    memset(e.res_1, 0xFF, sizeof(e.res_1));
#else
    memset(e.res_0, 0xFF, sizeof(e.res_0));
#endif
    update_crc();

#ifdef CONFIG_SYS_EEPROM_BUS_NUM
    bus = i2c_get_bus_num();
    i2c_set_bus_num(CONFIG_SYS_EEPROM_BUS_NUM);
#endif

    for (i = 0, p = &e; i < sizeof(e); i += 8, p += 8) {
        ret = i2c_write(CONFIG_SYS_I2C_EEPROM_ADDR, i, CONFIG_SYS_I2C_EEPROM_ADDR_LEN,
                        p, min((sizeof(e) - i), 8));
        if (ret)
            break;
        udelay(5000);	/* 5ms write cycle timing */
    }

#ifdef CONFIG_SYS_EEPROM_BUS_NUM
    i2c_set_bus_num(bus);
#endif

    if (ret) {
        printf("Programming failed.\n");
        return -1;
    }

    printf("Programming passed.\n");
    return 0;
}
void format_protocal_sdk_uart( unsigned char *src_buf, unsigned int original_len,
                               unsigned char *dst_buf, unsigned int &new_len,
                               unsigned short seq_num, unsigned char is_ack,
                               unsigned char enc_type
                             )
{
    protocal_sdk_uart_header *header = (protocal_sdk_uart_header*)dst_buf;
    header->m_header = 0xaa;
    new_len = original_len + sizeof(protocal_sdk_uart_header) + 2 + 4;
    header->m_length = new_len;
    header->m_version = 0;
    header->m_session_id = 0;
    header->m_A = is_ack;
    header->m_R = 0;
    header->m_padding = 0;
    header->m_enc_type = enc_type;
    header->m_seq_num = seq_num;
    Append_CRC16_Check_Sum( (unsigned char *)header, sizeof(protocal_sdk_uart_header) );
    memcpy( dst_buf + sizeof(protocal_sdk_uart_header), src_buf, original_len );

    word32      crc32;            // 32-bit CRC value
    crc32 = update_crc( -1, (byte*)dst_buf, header->m_length );
    printf("CRC = %08X \n", crc32);
}
Example #21
0
/*
 * process one BAM record, and store accumulated results in 'results'
 */
int seqchksum_processRecord(bam1_t *rec, HASH_TYPE hash, chksum_results_t *results)
{
    uint32_t crc = 0;

    uint16_t aflags = rec->core.flag;
    uint8_t *seq = get_read(rec);
    uint8_t *qual = get_quality(rec);
    uint16_t flag_mask = BAM_FPAIRED | BAM_FREAD1 | BAM_FREAD2;
    uint8_t flags = (aflags & flag_mask) & 0xFF;
    bool pass = !(aflags & BAM_FQCFAIL);;
    char *qname = bam_get_qname(rec);
    uint8_t *tag;
    char *rgid;
    HashItem *hi;
    HashData hd;
    int newitem;
    digest_line_t *dline_all;
    digest_line_t *dline;

    // look up the RG tag
    tag = bam_aux_get(rec, "RG");
    //hd.p = malloc(sizeof(digest_line_t));
    if (tag) rgid = bam_aux2Z(tag);
    else     rgid = "";

    hd.p = NULL;
    hi = HashTableAdd(results->rgHash, rgid, 0, hd, &newitem);
    if (newitem) {
        hi->data.p = malloc(sizeof(digest_line_t));
        dline = hi->data.p;
        init_digest_line(hash,dline);
    } else {
        dline = hi->data.p;
    }

    dline_all = &(results->all);

    // flags + sequence chksum
    update_crc(&crc,&flags,1);
    update_crc(&crc,seq,strlen((char*)seq));

    update_digest_line(hash, pass, dline, crc, 0);
    update_digest_line(hash, pass, dline_all, crc, 0);

    // flags + sequence + quality chksum (don't reset crc, just add quality)
    update_crc(&crc,qual,strlen((char*)qual));
    update_digest_line(hash, pass, dline, crc, 2);
    update_digest_line(hash, pass, dline_all, crc, 2);

    // name + flags + sequence chksum
    crc = 0;
    update_crc(&crc, (uint8_t *)qname, strlen(qname)+1);
    update_crc(&crc, &flags, 1);
    update_crc(&crc,seq,strlen((char*)seq));
    update_digest_line(hash, pass, dline, crc, 1);
    update_digest_line(hash, pass, dline_all, crc, 1);

    // flags + sequence + tags chksum
    crc = 0;
    update_crc(&crc, &flags, 1);
    update_crc(&crc,seq,strlen((char*)seq));
    tag = bam_aux_get(rec,"BC"); if (tag) update_crc(&crc,tag-2,aux_type2size(tag)+3);
    tag = bam_aux_get(rec,"FI"); if (tag) update_crc(&crc,tag-2,aux_type2size(tag)+3);
    tag = bam_aux_get(rec,"QT"); if (tag) update_crc(&crc,tag-2,aux_type2size(tag)+3);
    tag = bam_aux_get(rec,"RT"); if (tag) update_crc(&crc,tag-2,aux_type2size(tag)+3);
    tag = bam_aux_get(rec,"TC"); if (tag) update_crc(&crc,tag-2,aux_type2size(tag)+3);
    update_digest_line(hash, pass, dline, crc, 3);
    update_digest_line(hash, pass, dline_all, crc, 3);

    free(seq); free(qual);
    return 0;
}
Example #22
0
byte Dynamixel::rxPacket(int bRxLength) {
    unsigned long ulCounter, ulTimeLimit;
    word bCount, bLength, bChecksum;
    byte bTimeout;


    bTimeout = 0;
    if(bRxLength == 255 || bRxLength == 0xffff) //2014-04-03
        ulTimeLimit = RX_TIMEOUT_COUNT1;
    else
        ulTimeLimit = RX_TIMEOUT_COUNT2;
    for(bCount = 0; bCount < bRxLength; bCount++)
    {
        ulCounter = 0;
        while(mDxlDevice->read_pointer == mDxlDevice->write_pointer)
        {
            nDelay(NANO_TIME_DELAY); //[ROBOTIS] porting ydh
            if(ulCounter++ > ulTimeLimit)
            {
                bTimeout = 1;
                //TxDStringC("Timeout\r\n");
                break;
            }
            uDelay(0); //[ROBOTIS] porting ydh added  //if exist DXL 1.0 -> ok DXL 2.0 -> ok, if not exist 1.0 not ok, 2.0 ok
        }
        if(bTimeout) break;
        mRxBuffer[bCount] = mDxlDevice->data_buffer[mDxlDevice->read_pointer++ & DXL_RX_BUF_SIZE]; // get packet data from USART device
        //TxDStringC("mRxBuffer = ");TxDHex8C(mRxBuffer[bCount]);TxDStringC("\r\n");
    }

    bLength = bCount;
    bChecksum = 0;
    if( mTxBuffer[mPktIdIndex] != BROADCAST_ID )
    {
        if(bTimeout && bRxLength != 255)
        {
#ifdef PRINT_OUT_COMMUNICATION_ERROR_TO_USART2
            TxDStringC("Rx Timeout");
            TxDByteC(bLength);
#endif
            mDXLtxrxStatus |= (1<<COMM_RXTIMEOUT);
            clearBuffer();
            //TxDStringC("Rx Timeout");
            return 0;
        }
        if(bLength > 3) //checking available length.
        {
            /*if(mPacketType == 1){
            	if(mRxBuffer[0] != 0xff || mRxBuffer[1] != 0xff ) mDXLtxrxStatus |= (1<<COMM_RXCORRUPT);//RXHEADER);
            	else if(mRxBuffer[mPktIdIndex] != mTxBuffer[mPktIdIndex] ) mDXLtxrxStatus |= (1<<COMM_RXCORRUPT);//RXID);
            	else if(mRxBuffer[mPktLengthIndex] != bLength-mPktInstIndex) mDXLtxrxStatus |= (1<<COMM_RXCORRUPT);//RXLENGTH);
            	else{
            		for(bCount = 2; bCount < bLength; bCount++){
            			bChecksum += mRxBuffer[bCount]; //Calculate checksum of received data for compare
            		}
            		if(bChecksum != 0xff) mDXLtxrxStatus |= (1<<COMM_RXCORRUPT);//RXCHECKSUM);
            		return 0;
            	}
            }else{
            	if(mRxBuffer[0] != 0xff || mRxBuffer[1] != 0xff || mRxBuffer[2] != 0xfd)  mDXLtxrxStatus |= (1<<COMM_RXCORRUPT);//RXHEADER);
            	else if(mRxBuffer[mPktIdIndex] != mTxBuffer[mPktIdIndex] ) mDXLtxrxStatus |= (1<<COMM_RXCORRUPT);//RXID);
            	else if(mRxBuffer[mPktLengthIndex] != bLength-mPktInstIndex) mDXLtxrxStatus |= (1<<COMM_RXCORRUPT);//RXLENGTH);
            	else{
            		bChecksum = DXL_MAKEWORD(mRxBuffer[bRxLength-2], mRxBuffer[bRxLength-1]);
            		if(update_crc(0, mRxBuffer, bRxLength-2) != bChecksum) mDXLtxrxStatus |= (1<<COMM_RXCORRUPT);//RXCHECKSUM);
            		return 0;
            	}
            }
            */
            if(mPacketType == 1) { //Dxl 1.0 header check
                if(mRxBuffer[0] != 0xff || mRxBuffer[1] != 0xff ) {
#ifdef PRINT_OUT_COMMUNICATION_ERROR_TO_USART2
                    TxDStringC("Wrong Header");//[Wrong Header]
#endif
                    mDXLtxrxStatus |= (1<<COMM_RXCORRUPT);//RXHEADER);
                    clearBuffer();
                    return 0;
                }
            } else { // Dxl 2.0 header check
                if(mRxBuffer[0] != 0xff || mRxBuffer[1] != 0xff || mRxBuffer[2] != 0xfd)
                {
#ifdef PRINT_OUT_COMMUNICATION_ERROR_TO_USART2
                    TxDStringC("Wrong Header");//[Wrong Header]
#endif
                    mDXLtxrxStatus |= (1<<COMM_RXCORRUPT);//RXHEADER);
                    clearBuffer();
                    return 0;
                }
            }

            if(mRxBuffer[mPktIdIndex] != mTxBuffer[mPktIdIndex] )  //id check
            {
#ifdef PRINT_OUT_COMMUNICATION_ERROR_TO_USART2
                TxDStringC("[Error:TxID != RxID]");
#endif
                mDXLtxrxStatus |= (1<<COMM_RXCORRUPT);//RXID);
                clearBuffer();
                return 0;
            }

            if(mRxBuffer[mPktLengthIndex] != bLength-mPktInstIndex) // status packet length check
            {
#ifdef PRINT_OUT_COMMUNICATION_ERROR_TO_USART2
                TxDStringC("RxLength Error");
#endif
                mDXLtxrxStatus |= (1<<COMM_RXCORRUPT);//RXLENGTH);
                clearBuffer();
                return 0;
            }

            if(mPacketType == 1 && mRxBuffer[mPktErrorIndex] != 0) {										//140512 shin
#ifdef PRINT_OUT_COMMUNICATION_ERROR_TO_USART2
                for(bTryCount = 0; bTryCount<= 6; bTryCount++) {
                    if((mRxBuffer[mPktErrorIndex] & (1<<bTryCount)) == TRUE) {
                        switch(bTryCount) {
                        case 0:
                            TxDStringC("InputVoltage Error");
                            break;
                        case 1:
                            TxDStringC("Angle Limit Error");
                            break;
                        case 2:
                            TxDStringC("Overheating Error");
                            break;
                        case 3:
                            TxDStringC("Range Error");
                            break;
                        case 4:
                            TxDStringC("Checksum Error");
                            break;
                        case 5:
                            TxDStringC("Overload Error");
                            break;
                        case 6:
                            TxDStringC("Instruction Error");
                            break;
                        }
                    }
                }
#endif
            } else {																					//140512 shin
#ifdef PRINT_OUT_COMMUNICATION_ERROR_TO_USART2
                for(bTryCount = 1; bTryCount<= 7; bTryCount++) {
                    if((mRxBuffer[mPktErrorIndex]) == bTryCount) {
                        switch(bTryCount) {
                        case 1:
                            TxDStringC("Result Fail");
                            break;
                        case 2:
                            TxDStringC("Instruction Error");
                            break;
                        case 3:
                            TxDStringC("CRC Error");
                            break;
                        case 4:
                            TxDStringC("DataRange Error");
                            break;
                        case 5:
                            TxDStringC("DataLength Error");
                            break;
                        case 6:
                            TxDStringC("DataLimit Error");
                            break;
                        case 7:
                            TxDStringC("Accrss Error");
                            break;
                        }
                    }
                }
#endif
            }

            if(mPacketType == 1) { // Dxl 1.0 checksum
                for(bCount = 2; bCount < bLength; bCount++) {
                    bChecksum += mRxBuffer[bCount]; //Calculate checksum of received data for compare
                }
                if(bChecksum != 0xff)
                {
#ifdef PRINT_OUT_COMMUNICATION_ERROR_TO_USART2
                    TxDStringC("[RxChksum Error]");
#endif
                    mDXLtxrxStatus |= (1<<COMM_RXCORRUPT);//RXCHECKSUM);
                    clearBuffer();
                    return 0;
                }
            } else { // Dxl 2.0 checksum
                bChecksum = DXL_MAKEWORD(mRxBuffer[bRxLength-2], mRxBuffer[bRxLength-1]);
                if(update_crc(0, mRxBuffer, bRxLength-2) == bChecksum) { // -2 : except CRC16
                    return bLength;
                }
                else {
#ifdef PRINT_OUT_COMMUNICATION_ERROR_TO_USART2
                    TxDStringC("CRC-16 Error\r\n");
#endif
                    return 0;
                }
            }//end of checksum
        }//(bLength > 3)
    }//end of Rx status packet check

    return bLength;
}
Example #23
0
int do_mac(cmd_tbl_t *cmdtp, int flag, int argc, char * const argv[])
{
    char cmd;

    if (argc == 1) {
        show_eeprom();
        return 0;
    }

    cmd = argv[1][0];

    if (cmd == 'r') {
        read_eeprom();
        return 0;
    }

    if (cmd == 'i') {
#ifdef CONFIG_SYS_I2C_EEPROM_NXID
        memcpy(e.id, "NXID", sizeof(e.id));
        e.version = 0;
#else
        memcpy(e.id, "CCID", sizeof(e.id));
#endif
        return 0;
    }

    if (!is_valid) {
        printf("Please read the EEPROM ('r') and/or set the ID ('i') first.\n");
        return 0;
    }

    if (argc == 2) {
        switch (cmd) {
        case 's':	/* save */
            prog_eeprom();
            break;
        default:
            return cmd_usage(cmdtp);
        }

        return 0;
    }

    /* We know we have at least one parameter  */

    switch (cmd) {
    case 'n':	/* serial number */
        memset(e.sn, 0, sizeof(e.sn));
        strncpy((char *)e.sn, argv[2], sizeof(e.sn) - 1);
        update_crc();
        break;
    case 'e':	/* errata */
#ifdef CONFIG_SYS_I2C_EEPROM_NXID
        memset(e.errata, 0, 5);
        strncpy((char *)e.errata, argv[2], 4);
#else
        e.errata[0] = argv[2][0];
        e.errata[1] = argv[2][1];
#endif
        update_crc();
        break;
    case 'd':	/* date BCD format YYMMDDhhmmss */
        set_date(argv[2]);
        break;
    case 'p':	/* MAC table size */
        e.mac_count = simple_strtoul(argv[2], NULL, 16);
        update_crc();
        break;
    case '0' ... '7':	/* "mac 0" through "mac 7" */
        set_mac_address(cmd - '0', argv[2]);
        break;
    case 'h':	/* help */
    default:
        return cmd_usage(cmdtp);
    }

    return 0;
}
Example #24
0
static void * blk_thread(void *arg)
{
 do {
	msgblk_t *blk;
	acarsmsg_t  msg;
	int i,k,pn;
	unsigned short crc;

	pthread_mutex_lock(&blkmtx);
	while(blkq_e==NULL) pthread_cond_wait(&blkwcd,&blkmtx);

	blk=blkq_e;
	blkq_e=blk->prev;
	if(blkq_e==NULL) blkq_s=NULL;
	pthread_mutex_unlock(&blkmtx);

	if(blk==NULL) return;

	if(blk->len<13) {
		if(verbose) fprintf(stderr,"#%d too short\n",blk->chn+1);
		free(blk);
		continue;
	}

	/* force STX/ETX */
	blk->txt[12]&=(ETX|STX);
	blk->txt[12]|=(ETX&STX);

	msg.err=0;
	/* parity check */
	pn=0;
	for(i=0;i<blk->len;i++) {
		if((numbits[blk->txt[i]]&1)==0) {
		pn++;
		}
	}
	if(pn>1) {
		if(verbose) fprintf(stderr,"#%d too much parity error : %d\n",blk->chn+1,pn);
		free(blk);
		continue;
	}

	/* crc check */
	crc=0;
	for(i=0;i<blk->len;i++) {
		update_crc(crc,blk->txt[i]);

	}
	update_crc(crc,blk->crc[0]);
	update_crc(crc,blk->crc[1]);

	/* try to fix errors */
	if(crc!=0) {
		int i,k,fx;
		if(verbose) fprintf(stderr,"#%d CRC error, try to recover 1 error\n",blk->chn+1);
		fx=0;
		for(i=0;i<242*8;i++) 
 			if(oneerr[i]==crc) {
				fixerr(blk,i/8,1<<(i%8));
				fx=1;
				msg.err=1;
				break;
			}

		if(fx==0 && pn==0 && blk->len<142) {
			int i,k,l;
			unsigned char  u,v;
			unsigned short n=0;
			if(verbose) fprintf(stderr,"#%d CRC error, try to recover 2 close errors\n",blk->chn+1);
			for(k=1,v=2;k<8;k++,v<<=1) {
		 	  for(l=0,u=1;l<k;l++,u<<=1) {
				if(twoerr[n]==crc) {
					fixerr(blk,0,u|v);
					fx=1;
					msg.err=2;
					break;
				}
				n++;
				for(i=1;i<142;i++) {
					if(twoerr[n]==crc) {
						fixerr(blk,i,u|v);
						fx=1;
						msg.err=2;
						break;
					}
					n++;
				}
				if(i<142) break;
			  }
			  if(l<k) break;
			}
		}

		if(fx==0) {
			if(verbose) fprintf(stderr,"#%d not able to fix it\n",blk->chn+1);
			free(blk);
			continue;
		} else {
			if(verbose) fprintf(stderr,"#%d fix it\n",blk->chn+1);
		}
	}

	/* redo parity checking and remove parity */
	pn=0;
	for(i=0;i<blk->len;i++) {
		if((numbits[blk->txt[i]]&1)==0) {
		pn++;
		}

		blk->txt[i]&=0x7f;
	}
	if(pn) {
		if(verbose) fprintf(stderr,"#%d parity error %d\n",blk->chn+1,pn);
		free(blk);
		continue;
	}

	/* fill msg struct */
	msg.txt[0]= '\0';
	msg.fid[0] = '\0';

	msg.lvl = blk->lvl;

	k = 0;
	msg.mode = blk->txt[k];
	k++;

	for (i = 0; i < 7; i++, k++) {
		msg.addr[i] = blk->txt[k];
	}
	msg.addr[7] = '\0';

	/* ACK/NAK */
	msg.ack = blk->txt[k];
	k++;

	msg.label[0] = blk->txt[k];
	k++;
	msg.label[1] = blk->txt[k];
	if(msg.label[1]==0x7f) msg.label[1]='d';
	k++;
	msg.label[2] = '\0';

	msg.bid = blk->txt[k];
	k++;
	/* STX/ETX */
	msg.bs=blk->txt[k];
	k++;

	msg.no[0] = '\0';
	msg.fid[0] = '\0';
	if(msg.mode <= 0x5d) {
	    /* donwlink */
	    if(k<blk->len) {

		for (i = 0; i < 4 && k<blk->len-1; i++, k++) {
			msg.no[i] = blk->txt[k];
		}
		msg.no[i] = '\0';

		msg.be=blk->txt[blk->len-1];
		for (i = 0; i < 6 && k<blk->len-1; i++, k++) {
			msg.fid[i] = blk->txt[k];
		}
		msg.fid[i] = '\0';

	    }
	} else {
	   /* uplink */
	  if(airflt) {
		free(blk);
		continue;
	  }
	}

	blk->txt[blk->len-1]='\0';
	strncpy(msg.txt, &(blk->txt[k]),240);msg.txt[239]=0;

	if(outtype==0)
		printoneline(&msg,blk->chn,blk->t);
	else if (outtype==1)
		printmsg(&msg,blk->chn,blk->t);
	else if (outtype==2)
		send_udp(&msg,blk->chn,blk->t);
	
	free(blk);

 } while(1);
}
Example #25
0
	int pwnage2_exploit(int argc, char *argv[])
	{
		//building file
		if(argc<2) { printf("usage: %s <filename>\n",argv[0]); return -1; }
		FILE *cert=fopen("cert","rb");
		FILE *f=fopen(argv[1], "rb");
		if(cert==0||f==0) { printf("file not found\n"); return -1; }
		fseek(cert, 0, SEEK_END);
		int len_cert = ftell(cert);
		fseek(cert, 0, SEEK_SET);
		fseek(f, 0, SEEK_END);
		int len = ftell(f);
		fseek(f, 0, SEEK_SET);
		fbuf=(unsigned char *)malloc(0x800+len+len_cert+0x10);
		memset(fbuf,0,0x800);
		fread(&fbuf[0x800],1,len,f);
		fread(&fbuf[0x800+len],1,len_cert,cert);
		fclose(f); fclose(cert);
		printf("files read %X %X\n",len,len_cert);
		strcpy(fbuf, "89001.0"); 
		fbuf[7]=0x04;
		fbuf[0x3E]=0x04;
		memcpy(&fbuf[0xC],&len,0x4);			//data size
		memcpy(&fbuf[0x10],&len,0x4);		//sig offset
		a=len+0x80;
		memcpy(&fbuf[0x14],&a,0x4);			//cert offset
		a=0xC5E;
		memcpy(&fbuf[0x18],&a,0x4);			//cert length
		printf("header generated\n");
		//hashing header
		unsigned char shaout[0x14];
		SHA1(fbuf,0x40,shaout);
		AES_KEY mkey; 
		AES_set_encrypt_key(key837, 0x80, &mkey);
		unsigned char iv[0x10]; memset(iv,0,0x10);
		AES_cbc_encrypt(shaout, shaout, 0x10, &mkey, iv, AES_ENCRYPT);
		memcpy(&fbuf[0x40],shaout,0x10);
		//appending dfu footer
		unsigned int crc=0xFFFFFFFF;
		const char header[]={0xff,0xff,0xff,0xff,0xac,0x05,0x00,0x01,0x55,0x46,0x44,0x10};
		memcpy(&fbuf[0x800+len+len_cert],header,0xC);
		crc=update_crc(crc, fbuf, 0x800+len+len_cert+0xC);
		for(a=0;a<4;a++) { fbuf[0x800+len+len_cert+0xC+a]=crc&0xFF; crc=crc>>8; }
		//sending file to old dfu mode (0x1222)
		printf("sending 0x%x bytes\n", (0x800+len+len_cert+0x10));
		while(a<((0x800+len+len_cert+0x10)+0x800))
		{
			int sl=((0x800+len+len_cert+0x10)-a);
			if(sl<0) sl=0;
			if(sl>0x800) sl=0x800;
			//printf("%X %X\n",a,sl);
			if(libusb_control_transferr(handle, 0x21, 1, c, 0, &fbuf[a], sl, 1000)==sl) printf(".");
			else printf("x");
			if(sl==0) printf("\n");
			int b=0;
			while(libusb_control_transfer(handle, 0xA1, 3, 0, 0, buf, 6, 1000)==6&&b<5)
			{
				b++;
				if(sl==0) hexdump(buf, 6);
				if(buf[4]==5) break;
			}
			a+=0x800;
			c++;
		}
	}
Example #26
0
/**
 * mac_read_from_eeprom - read the MAC addresses from EEPROM
 *
 * This function reads the MAC addresses from EEPROM and sets the
 * appropriate environment variables for each one read.
 *
 * The environment variables are only set if they haven't been set already.
 * This ensures that any user-saved variables are never overwritten.
 *
 * This function must be called after relocation.
 *
 * For NXID v1 EEPROMs, we support loading and up-converting the older NXID v0
 * format.  In a v0 EEPROM, there are only eight MAC addresses and the CRC is
 * located at a different offset.
 */
int mac_read_from_eeprom(void)
{
	unsigned int i;
	u32 crc, crc_offset = offsetof(struct eeprom, crc);
	u32 *crcp; /* Pointer to the CRC in the data read from the EEPROM */

	puts("EEPROM: ");

	if (read_eeprom()) {
		printf("Read failed.\n");
		return 0;
	}

	if (!is_valid) {
		printf("Invalid ID (%02x %02x %02x %02x)\n",
		       e.id[0], e.id[1], e.id[2], e.id[3]);
		return 0;
	}

#ifdef CONFIG_SYS_I2C_EEPROM_NXID
	/*
	 * If we've read an NXID v0 EEPROM, then we need to set the CRC offset
	 * to where it is in v0.
	 */
	if (e.version == 0)
		crc_offset = 0x72;
#endif

	crc = crc32(0, (void *)&e, crc_offset);
	crcp = (void *)&e + crc_offset;
	if (crc != be32_to_cpu(*crcp)) {
		printf("CRC mismatch (%08x != %08x)\n", crc, be32_to_cpu(e.crc));
		return 0;
	}

#ifdef CONFIG_SYS_I2C_EEPROM_NXID
	/*
	 * MAC address #9 in v1 occupies the same position as the CRC in v0.
	 * Erase it so that it's not mistaken for a MAC address.  We'll
	 * update the CRC later.
	 */
	if (e.version == 0)
		memset(e.mac[8], 0xff, 6);
#endif

	for (i = 0; i < min(e.mac_count, MAX_NUM_PORTS); i++) {
		if (memcmp(&e.mac[i], "\0\0\0\0\0\0", 6) &&
		    memcmp(&e.mac[i], "\xFF\xFF\xFF\xFF\xFF\xFF", 6)) {
			char ethaddr[18];
			char enetvar[9];

			sprintf(ethaddr, "%02X:%02X:%02X:%02X:%02X:%02X",
				e.mac[i][0],
				e.mac[i][1],
				e.mac[i][2],
				e.mac[i][3],
				e.mac[i][4],
				e.mac[i][5]);
			sprintf(enetvar, i ? "eth%daddr" : "ethaddr", i);
			/* Only initialize environment variables that are blank
			 * (i.e. have not yet been set)
			 */
			if (!getenv(enetvar))
				setenv(enetvar, ethaddr);
		}
	}

#ifdef CONFIG_SYS_I2C_EEPROM_NXID
	printf("%c%c%c%c v%u\n", e.id[0], e.id[1], e.id[2], e.id[3],
		be32_to_cpu(e.version));
#else
	printf("%c%c%c%c\n", e.id[0], e.id[1], e.id[2], e.id[3]);
#endif

#ifdef CONFIG_SYS_I2C_EEPROM_NXID
	/*
	 * Now we need to upconvert the data into v1 format.  We do this last so
	 * that at boot time, U-Boot will still say "NXID v0".
	 */
	if (e.version == 0) {
		e.version = NXID_VERSION;
		update_crc();
	}
#endif

	return 0;
}
Example #27
0
static void *blk_thread(void *arg)
{
	do {
		msgblk_t *blk;
		int i, pn;
		unsigned short crc;
		int pr[MAXPERR];

		pthread_mutex_lock(&blkmtx);
		while (blkq_e == NULL)
			pthread_cond_wait(&blkwcd, &blkmtx);

		blk = blkq_e;
		blkq_e = blk->prev;
		if (blkq_e == NULL)
			blkq_s = NULL;
		pthread_mutex_unlock(&blkmtx);

		if (blk->len < 13) {
			if (verbose)
				fprintf(stderr, "#%d too short\n",
					blk->chn + 1);
			free(blk);
			continue;
		}

		/* force STX/ETX */
		blk->txt[12] &= (ETX | STX);
		blk->txt[12] |= (ETX & STX);

		/* parity check */
		pn = 0;
		for (i = 0; i < blk->len; i++) {
			if ((numbits[blk->txt[i]] & 1) == 0) {
				if (pn < MAXPERR) {
					pr[pn] = i;
				}
				pn++;
			}
		}
		if (pn > MAXPERR) {
			if (verbose)
				fprintf(stderr,
					"#%d too many parity errors: %d\n",
					blk->chn + 1, pn);
			free(blk);
			continue;
		}
		if (pn > 0 && verbose)
			fprintf(stderr, "#%d parity error(s): %d\n",
				blk->chn + 1, pn);
		blk->err = pn;

		/* crc check */
		crc = 0;
		for (i = 0; i < blk->len; i++) {
			update_crc(crc, blk->txt[i]);

		}
		update_crc(crc, blk->crc[0]);
		update_crc(crc, blk->crc[1]);
		if (crc && verbose)
			fprintf(stderr, "#%d crc error\n", blk->chn + 1);

		/* try to fix error */
		if (fixerr(blk, crc, pr, pn) == 0) {
			if (verbose)
				fprintf(stderr, "#%d not able to fix errors\n",
					blk->chn + 1);
			free(blk);
			continue;
		}

		/* redo parity checking and removing */
		pn = 0;
		for (i = 0; i < blk->len; i++) {
			if ((numbits[blk->txt[i]] & 1) == 0) {
				pn++;
			}
			blk->txt[i] &= 0x7f;
		}
		if (pn) {
			fprintf(stderr, "#%d parity check problem\n",
				blk->chn + 1);
			free(blk);
			continue;
		}
		if (prev_t == blk->t &&
		    prev_crc[0] == blk->crc[0] && prev_crc[1] == blk->crc[1]) {
			if (verbose)
				fprintf(stderr, "#%d duplicate %d\n",
					blk->chn + 1, blk->lvl);
			free(blk);
			continue;
		}

		prev_t = blk->t;
		prev_crc[0] = blk->crc[0];
		prev_crc[1] = blk->crc[1];

		outputmsg(blk);

		free(blk);

	} while (1);
	return NULL;
}
Example #28
0
/**
 *  FIXME: Ideally the header size should be determined by high level macros,
 * instead of hacking the message header at the device layer
 */
int MPIDI_CH3I_nem_ib_parse_header(MPIDI_VC_t * vc,
                                  vbuf * v, void **pkt, int *header_size)
{
    void *vstart;
    MPIDI_nem_ib_pkt_comm_header *header;
#ifdef CRC_CHECK
    unsigned long crc;
#endif
    int mpi_errno = MPI_SUCCESS;
    int ret;
    DEBUG_PRINT("[parse header] vbuf address %p\n", v);
    vstart = v->pheader;
    header = (MPIDI_nem_ib_pkt_comm_header *)v->iheader;
    DEBUG_PRINT("[parse header] header type %d\n", header->type);

    /* set it to the header size by default */
    *header_size = sizeof(MPIDI_CH3_Pkt_t);
#ifdef CRC_CHECK
    crc = update_crc(1, (void *)((uintptr_t)header+sizeof *header),
                     v->content_size - sizeof *header);
    if (crc != header->mrail.crc) {
        int rank; PMI_Get_rank(&rank);
        MPIU_Error_printf(stderr, "CRC mismatch, get %lx, should be %lx "
                "type %d, ocntent size %d\n",
                crc, header->mrail.crc, header->type, v->content_size);
        exit( EXIT_FAILURE );
    }
#endif
    switch (header->type) {
/*header caching codes */
#ifndef MV2_DISABLE_HEADER_CACHING 
    case (MPIDI_CH3_PKT_FAST_EAGER_SEND):
    case (MPIDI_CH3_PKT_FAST_EAGER_SEND_WITH_REQ):
        {
            /* since header caching do not have regular iheader,
             * revert back pre-adjust
             */
            v->content_size += IB_PKT_HEADER_LENGTH;
            vstart -= IB_PKT_HEADER_LENGTH;
            v->pheader -= IB_PKT_HEADER_LENGTH;
            MPIDI_nem_ib_pkt_fast_eager *fast_header = vstart;
            MPIDI_CH3_Pkt_eager_send_t *eager_header =
                (MPIDI_CH3_Pkt_eager_send_t *) VC_FIELD(vc, connection)->rfp.
                cached_incoming;
            MPIDI_nem_ib_pkt_comm_header *eager_iheader =
                (MPIDI_nem_ib_pkt_comm_header *) VC_FIELD(vc, connection)->rfp.
                cached_incoming_iheader;

            if (MPIDI_CH3_PKT_FAST_EAGER_SEND == header->type) {
                *header_size = sizeof(MPIDI_nem_ib_pkt_fast_eager);
            } else {
                *header_size =
                    sizeof(MPIDI_nem_ib_pkt_fast_eager_with_req);
                eager_header->sender_req_id =
                    ((MPIDI_nem_ib_pkt_fast_eager_with_req *)
                     vstart)->sender_req_id;
            }
            header = eager_iheader;

            DEBUG_PRINT("[receiver side] cached credit %d\n",
                        eager_iheader->rdma_credit);

            eager_header->data_sz = fast_header->bytes_in_pkt;

            *pkt = (void *) eager_header;
            DEBUG_PRINT
                ("[recv: parse header] faster headersize returned %d\n",
                 *header_size);
        }
        break;
#endif
    case (MPIDI_CH3_PKT_EAGER_SEND):
        {
            DEBUG_PRINT("[recv: parse header] pkt eager send\n");
/* header caching codes */
#ifndef MV2_DISABLE_HEADER_CACHING 
            if (v->padding != NORMAL_VBUF_FLAG && 
                    (v->content_size - sizeof(MPIDI_CH3_Pkt_t) <= MAX_SIZE_WITH_HEADER_CACHING) ) {
                /* Only cache header if the packet is from RdMA path
                 * XXXX: what is R3_FLAG?
                 */
                MPIU_Memcpy((VC_FIELD(vc, connection)->rfp.cached_incoming), vstart,
                       sizeof(MPIDI_CH3_Pkt_eager_send_t));
                MPIU_Memcpy((VC_FIELD(vc, connection)->rfp.cached_incoming_iheader), header,
                    sizeof(MPIDI_nem_ib_pkt_comm_header));
            }
#endif
            *pkt = (MPIDI_CH3_Pkt_t *) vstart;
            *header_size = sizeof(MPIDI_CH3_Pkt_t);
            DEBUG_PRINT("[recv: parse header] headersize returned %d\n",
                        *header_size);
        }
        break;
    case (MPIDI_CH3_PKT_RNDV_REQ_TO_SEND):
    case (MPIDI_CH3_PKT_RNDV_CLR_TO_SEND):
    case MPIDI_CH3_PKT_EAGER_SYNC_ACK:
    case MPIDI_NEM_PKT_LMT_RTS:
    case MPIDI_NEM_PKT_LMT_CTS:
    case MPIDI_NEM_PKT_LMT_DONE:
    case MPIDI_NEM_PKT_LMT_COOKIE:
/* CKPT codes */
#ifdef CKPT
    case MPIDI_CH3_PKT_CM_SUSPEND:
    case MPIDI_CH3_PKT_CM_REACTIVATION_DONE:
    case MPIDI_CH3_PKT_CR_REMOTE_UPDATE:
#endif
        {
            *pkt = vstart;
        }
        break;
    case MPIDI_CH3_PKT_CANCEL_SEND_REQ:
        {
            *pkt = vstart;
            /*Fix: Need to unregister and free the rndv buffer in get protocol.*/
        }
        break;
    case MPIDI_CH3_PKT_CANCEL_SEND_RESP:
        {
            MPID_Request *req;
            *pkt = vstart;
            MPID_Request_get_ptr(((MPIDI_CH3_Pkt_cancel_send_resp_t *)(*pkt))->sender_req_id, req);
            if (req != NULL) {
              /* unregister and free the rndv buffer */
              MPIDI_NEM_IB_RREQ_RNDV_FINISH(req);
            }
        }
        break;
    case (MPIDI_CH3_PKT_NOOP):
        {
            *pkt = v->iheader;
        }
        break;
    /* rfp codes */
    case MPIDI_CH3_PKT_ADDRESS:
        {
            *pkt = v->iheader;
            MPIDI_nem_ib_recv_addr(vc, vstart);
            break;
        }
    case MPIDI_CH3_PKT_ADDRESS_REPLY:
        {
            *pkt = v->iheader;
            MPIDI_nem_ib_recv_addr_reply(vc, vstart);
            break;
        }
    case MPIDI_CH3_PKT_PACKETIZED_SEND_START:
        {
            *pkt = vstart;
            *header_size = sizeof(MPIDI_CH3_Pkt_packetized_send_start_t);
            break;
        }
    case MPIDI_CH3_PKT_PACKETIZED_SEND_DATA:
        {
            *header_size = sizeof(MPIDI_CH3_Pkt_packetized_send_data_t);
            *pkt = vstart;
            break;
        }
    case MPIDI_CH3_PKT_RNDV_R3_DATA:
        {
            *header_size = sizeof(MPIDI_CH3_Pkt_rndv_r3_data_t);
            *pkt = vstart;
            break;
        }
    case MPIDI_CH3_PKT_RNDV_R3_ACK:
        {
            *pkt = v->iheader;
            MPIDI_nem_ib_lmt_r3_recv_ack(vc, vstart);
            break;
        }
#if defined(USE_EAGER_SHORT)
    case MPIDI_CH3_PKT_EAGERSHORT_SEND:
#endif
    case MPIDI_CH3_PKT_EAGER_SYNC_SEND:
    case MPIDI_CH3_PKT_READY_SEND:
        {
            *pkt = vstart;
            break;
        }
    case MPIDI_CH3_PKT_PUT:
        {
            *pkt = vstart;
            break;
        }
    case MPIDI_CH3_PKT_GET:
        {
            *pkt = vstart;
            break;
        }
    case MPIDI_CH3_PKT_GET_RESP:       /*15 */
        {
            *pkt = vstart;
            break;
        }
    case MPIDI_CH3_PKT_ACCUMULATE:
        {
            *pkt = vstart;
            break;
        }
    case MPIDI_CH3_PKT_LOCK:
        {
            *pkt = vstart;
            break;
        }
    case MPIDI_CH3_PKT_LOCK_GRANTED:
        {
            *pkt = vstart;
            break;
        }
    case MPIDI_CH3_PKT_PT_RMA_DONE:
        {
            *pkt = vstart;
            break;
        }
    case MPIDI_CH3_PKT_LOCK_PUT_UNLOCK:
        {
            *pkt = vstart;
            break;
        }
    case MPIDI_CH3_PKT_LOCK_GET_UNLOCK:
        {
            *pkt = vstart;
            break;
        }
    case MPIDI_CH3_PKT_LOCK_ACCUM_UNLOCK:
    case MPIDI_CH3_PKT_ACCUM_IMMED:
        {
            *pkt = vstart;
            break;
        }
    case MPIDI_CH3_PKT_FLOW_CNTL_UPDATE:
        {
            *pkt = vstart;
            break;
        }
    case MPIDI_CH3_PKT_CLOSE:
        {
            *pkt = vstart;
        }
        break;
    default:
        {
            /* Header is corrupted if control has reached here in prototype */
            /* */
            MPIU_ERR_SETFATALANDJUMP2(mpi_errno,
                    MPI_ERR_OTHER,
                    "**fail",
                    "**fail %s %d",
                    "Control shouldn't reach here "
                    "in prototype, header %d\n",
                    header->type);
        }
    }

    DEBUG_PRINT("Before set credit, vc: %p, v->rail: %d, "
            "pkt: %p, pheader: %p\n", vc, v->rail, pkt, v->pheader);

    SET_CREDIT(header, VC_FIELD(vc, connection), (v->rail));


    if (VC_FIELD(vc, connection)->srp.credits[v->rail].remote_credit > 0 &&
        VC_FIELD(vc, connection)->srp.credits[v->rail].backlog.len > 0) {
        /* backlog send codes */
        MRAILI_Backlog_send(vc, v->rail);
    }

    /* if any credits remain, schedule rendezvous progress */
    if ((VC_FIELD(vc, connection)->srp.credits[v->rail].remote_credit > 0
/* rfp codes */
            || (VC_FIELD(vc, connection)->rfp.ptail_RDMA_send !=
                VC_FIELD(vc, connection)->rfp.phead_RDMA_send)
        )
        && (VC_FIELD(vc, connection)->sreq_head != NULL)) {
        /* rndv codes */
#if 0
        PUSH_FLOWLIST(vc);
#endif
    }

    /* rfp codes */
    if ((VC_FIELD(vc, connection)->rfp.RDMA_recv_buf == NULL) &&       /*(c->initialized) && */
            num_rdma_buffer && !VC_FIELD(vc, connection)->rfp.rdma_failed) {
        if ((process_info.polling_group_size + rdma_pending_conn_request) <
                rdma_polling_set_limit) {
            VC_FIELD(vc, connection)->rfp.eager_start_cnt++;
            if (rdma_polling_set_threshold <
                    VC_FIELD(vc, connection)->rfp.eager_start_cnt) {
                {
                    ret = vbuf_fast_rdma_alloc(vc, 1);
                    if (ret == MPI_SUCCESS) {
                        vbuf_address_send(vc);
                        rdma_pending_conn_request++;
                    } else {
                        VC_FIELD(vc, connection)->rfp.rdma_failed = 1;
                    }
                    goto fn_exit;
                }
            }
        }
    }

fn_exit:
    return mpi_errno;

fn_fail:
    goto fn_exit;
}
Example #29
0
static void II_decode_bitalloc(mpadec_t mpadec, uint8_t *bit_alloc,
                               uint8_t *scalefac)
{
    register struct mpadec_t *mpa = (struct mpadec_t *)mpadec;
    alloc_table_t *alloc = mpa->frame.alloc_table;
    uint8_t *scfsi, *ba = bit_alloc, *scf = scalefac;
    unsigned i, step, sblimit2, sblimit = mpa->frame.sblimit;
    uint32_t crclen = mpa->bytes_left;
    uint8_t scfsi_buf[2*SBLIMIT];

    if (mpa->frame.channels > 1) {
      unsigned jsbound = mpa->frame.jsbound;
      sblimit2 = sblimit << 1;
      for (i = jsbound; i; i--, alloc += ((int64_t)(1) << step)) {
        step = alloc->bits;
        *ba++ = (uint8_t)GETBITS(step);
        *ba++ = (uint8_t)GETBITS(step);
      }
      for (i = sblimit - jsbound; i; i--, alloc += ((int64_t)(1) << step)) {
        step = alloc->bits;
        ba[0] = (uint8_t)GETBITS(step);
        ba[1] = ba[0];
        ba += 2;
      }
      ba = bit_alloc;
      scfsi = scfsi_buf;
      for (i = sblimit2; i; i--) if (*ba++) *scfsi++ = (uint8_t)GETBITS(2);
    } else {
      sblimit2 = sblimit;
      for (i = sblimit; i; i--, alloc += ((int64_t)(1) << step)) {
        step = alloc->bits;
        *ba++ = (uint8_t)GETBITS(step);
      }
      ba = bit_alloc;
      scfsi = scfsi_buf;
      for (i = sblimit; i; i--) if (*ba++) *scfsi++ = (uint8_t)GETBITS(2);
    }
    if (mpa->config.crc && mpa->frame.CRC) {
      crclen -= mpa->bytes_left;
      mpa->crc = update_crc(mpa->crc, mpa->next_byte - crclen,
                            ((crclen << 3) - mpa->bits_left));
      if (mpa->crc != mpa->frame.crc) mpa->error = TRUE;
    }
    ba = bit_alloc;
    scfsi = scfsi_buf;
    for (i = sblimit2; i; i--) {
      if (*ba++) {
        switch (*scfsi++) {
        case 0:
          scf[0] = (uint8_t)GETBITS(6);
          scf[1] = (uint8_t)GETBITS(6);
          scf[2] = (uint8_t)GETBITS(6);
          break;
        case 1:
          scf[0] = (uint8_t)GETBITS(6);
          scf[1] = scf[0];
          scf[2] = (uint8_t)GETBITS(6);
          break;
        case 2:
          scf[0] = (uint8_t)GETBITS(6);
          scf[1] = scf[2] = scf[0];
          break;
        default:
          scf[0] = (uint8_t)GETBITS(6);
          scf[1] = (uint8_t)GETBITS(6);
          scf[2] = scf[1];
          break;
        }
        scf += 3;
      }
    }
}
Example #30
0
int do_mac(cmd_tbl_t *cmdtp, int flag, int argc, char * const argv[])
{
	char cmd;

	if (argc == 1) {
		show_eeprom();
		return 0;
	}

	cmd = argv[1][0];

	if (cmd == 'r') {
#ifdef DEBUG
		printf("%s read\n", __func__);
#endif
		read_eeprom();
		return 0;
	}

	if (argc == 2) {
		switch (cmd) {
		case 's':	/* save */
#ifdef DEBUG
			printf("%s save\n", __func__);
#endif
			prog_eeprom();
			break;
		default:
			return cmd_usage(cmdtp);
		}

		return 0;
	}

	/* We know we have at least one parameter  */

	switch (cmd) {
	case 'n':	/* serial number */
#ifdef DEBUG
		printf("%s serial number\n", __func__);
#endif
		memset(e.sn, 0, sizeof(e.sn));
		strncpy((char *)e.sn, argv[2], sizeof(e.sn) - 1);
		update_crc();
		break;
	case 'd':	/* date BCD format YYMMDDhhmmss */
		set_date(argv[2]);
		break;
	case 'e':	/* errata */
		printf("mac errata not implemented\n");
		break;
	case 'i':	/* id */
		memset(e.id, 0, sizeof(e.id));
		strncpy((char *)e.id, argv[2], sizeof(e.id) - 1);
		update_crc();
		break;
	case 'p':	/* ports */
		printf("mac ports not implemented (always 1 port)\n");
		break;
	case '0' ... '9':
		/* we only have "mac 0" but any digit can be used here */
		set_mac_address(argv[2]);
		break;
	case 'h':	/* help */
	default:
		return cmd_usage(cmdtp);
	}

	return 0;
}