/**
 * ubifs_add_bud - add bud LEB to the tree of buds and its journal head list.
 * @c: UBIFS file-system description object
 * @bud: the bud to add
 */
void ubifs_add_bud(struct ubifs_info *c, struct ubifs_bud *bud)
{
	struct rb_node **p, *parent = NULL;
	struct ubifs_bud *b;
	struct ubifs_jhead *jhead;

	spin_lock(&c->buds_lock);
	p = &c->buds.rb_node;
	while (*p) {
		parent = *p;
		b = rb_entry(parent, struct ubifs_bud, rb);
		ubifs_assert(bud->lnum != b->lnum);
		if (bud->lnum < b->lnum)
			p = &(*p)->rb_left;
		else
			p = &(*p)->rb_right;
	}

	rb_link_node(&bud->rb, parent, p);
	rb_insert_color(&bud->rb, &c->buds);
	if (c->jheads) {
		jhead = &c->jheads[bud->jhead];
		list_add_tail(&bud->list, &jhead->buds_list);
	} else
		ubifs_assert(c->replaying && c->ro_mount);

	/*
	 * Note, although this is a new bud, we anyway account this space now,
	 * before any data has been written to it, because this is about to
	 * guarantee fixed mount time, and this bud will anyway be read and
	 * scanned.
	 */
	c->bud_bytes += c->leb_size - bud->start;

	dbg_log("LEB %d:%d, jhead %s, bud_bytes %lld", bud->lnum,
		bud->start, dbg_jhead(bud->jhead), c->bud_bytes);
	spin_unlock(&c->buds_lock);
}
Beispiel #2
0
static void sdcard_set_of_name_board(char *of_name)
{
	unsigned int cpu_board_id = get_cm_sn();
	unsigned int disp_board_id = get_dm_sn();

	if (cpu_board_id == BOARD_ID_SAM9G15_CM)
		strcpy(of_name, "at91sam9g15ek");
	else if (cpu_board_id == BOARD_ID_SAM9G25_CM)
		strcpy(of_name, "at91sam9g25ek");
	else if (cpu_board_id == BOARD_ID_SAM9G35_CM)
		strcpy(of_name, "at91sam9g35ek");
	else if (cpu_board_id == BOARD_ID_SAM9X25_CM)
		strcpy(of_name, "at91sam9x25ek");
	else if (cpu_board_id == BOARD_ID_SAM9X35_CM)
		strcpy(of_name, "at91sam9x35ek");
	else
		dbg_log(1, "WARNING: Not correct CPU board ID\n\r");

	if (disp_board_id == BOARD_ID_PDA_DM)
		strcat(of_name, "_pda");

	strcat(of_name, ".dtb");
}
Beispiel #3
0
uint8_t   rawDataUpdate8slen(RawData *raw_data, const uint32_t offset, const uint8_t *data, const uint32_t len)
{
    uint32_t num;
    uint8_t *buffer;

    if(offset + sizeof(uint32_t) + len > raw_data->cur_size)
    {
        dbg_log(SEC_0132_RAW, 0)(LOGSTDOUT, "error:rawDataUpdate8slen: offset %d + %d + len %d > cur_size %d\n",
                            offset, sizeof(uint32_t), len, raw_data->cur_size);
        return RAW_FILE_FAIL;
    }

    num = gdb_hton_uint32(len);

    buffer = raw_data->buffer + offset;
    memcpy(buffer, &num, sizeof(uint32_t));

    buffer += sizeof(uint32_t);
    memcpy(buffer, data, len);

    RAWDATA_SET_DIRTY(raw_data);
    return RAW_FILE_SUCC;
}
Beispiel #4
0
EC_BOOL cextsrv_req_encode_size(CEXTSRV *cextsrv, const TASK_FUNC *task_req_func, UINT32 *size)
{
    FUNC_ADDR_NODE *func_addr_node;

    UINT32 send_comm;

    /*clear size*/
    *size = 0;

    send_comm = CMPI_ANY_COMM;

    if(0 != dbg_fetch_func_addr_node_by_index(task_req_func->func_id, &func_addr_node))
    {
        dbg_log(SEC_0069_CEXTSRV, 0)(LOGSTDOUT, "error:cextsrv_req_encode_size: failed to fetch func addr node by func id %lx\n", task_req_func->func_id);
        return (EC_FALSE);
    }

    cmpi_encode_uint32_size(send_comm, (task_req_func->func_id), size);
    cmpi_encode_uint32_size(send_comm, (task_req_func->func_para_num), size);

    task_req_func_para_encode_size(send_comm, func_addr_node->func_para_num, (FUNC_PARA *)task_req_func->func_para, func_addr_node, size);

    return (EC_TRUE);
}
/**
 * ubifs_log_start_commit - start commit.
 * @c: UBIFS file-system description object
 * @ltail_lnum: return new log tail LEB number
 *
 * The commit operation starts with writing "commit start" node to the log and
 * reference nodes for all journal heads which will define new journal after
 * the commit has been finished. The commit start and reference nodes are
 * written in one go to the nearest empty log LEB (hence, when commit is
 * finished UBIFS may safely unmap all the previous log LEBs). This function
 * returns zero in case of success and a negative error code in case of
 * failure.
 */
int ubifs_log_start_commit(struct ubifs_info *c, int *ltail_lnum)
{
	void *buf;
	struct ubifs_cs_node *cs;
	struct ubifs_ref_node *ref;
	int err, i, max_len, len;

	err = dbg_check_bud_bytes(c);
	if (err)
		return err;

	max_len = UBIFS_CS_NODE_SZ + c->jhead_cnt * UBIFS_REF_NODE_SZ;
	max_len = ALIGN(max_len, c->min_io_size);
	buf = cs = kmalloc(max_len, GFP_NOFS);
	if (!buf)
		return -ENOMEM;

	cs->ch.node_type = UBIFS_CS_NODE;
	cs->cmt_no = cpu_to_le64(c->cmt_no);
	ubifs_prepare_node(c, cs, UBIFS_CS_NODE_SZ, 0);

	/*
	 * Note, we do not lock 'c->log_mutex' because this is the commit start
	 * phase and we are exclusively using the log. And we do not lock
	 * write-buffer because nobody can write to the file-system at this
	 * phase.
	 */

	len = UBIFS_CS_NODE_SZ;
	for (i = 0; i < c->jhead_cnt; i++) {
		int lnum = c->jheads[i].wbuf.lnum;
		int offs = c->jheads[i].wbuf.offs;

		if (lnum == -1 || offs == c->leb_size)
			continue;

		dbg_log("add ref to LEB %d:%d for jhead %s",
			lnum, offs, dbg_jhead(i));
		ref = buf + len;
		ref->ch.node_type = UBIFS_REF_NODE;
		ref->lnum = cpu_to_le32(lnum);
		ref->offs = cpu_to_le32(offs);
		ref->jhead = cpu_to_le32(i);

		ubifs_prepare_node(c, ref, UBIFS_REF_NODE_SZ, 0);
		len += UBIFS_REF_NODE_SZ;
	}

	ubifs_pad(c, buf + len, ALIGN(len, c->min_io_size) - len);

#ifdef CONFIG_UBIFS_FS_FULL_USE_LOG
	/* Not Switch to next log LEB, programming next available page in the same log LEB continuously*/

	/* if available page is in the end of the LEB, switch to next LEB*/
	if(c->lhead_offs >= (c->leb_size - (c->min_io_size * 4)) )
	{
		int old_lnum = c->lhead_lnum;
		int old_offs = c->lhead_offs;
		c->lhead_lnum = ubifs_next_log_lnum(c, c->lhead_lnum);
		c->lhead_offs = 0;
		ubifs_msg("switch log LEB %d:%d to %d:%d\n", old_lnum, old_offs, c->lhead_lnum, c->lhead_offs);
	}
#else
	/* Switch to the next log LEB */
	if (c->lhead_offs) {
		int old_lnum = c->lhead_lnum;
		int old_offs = c->lhead_offs;
		c->lhead_lnum = ubifs_next_log_lnum(c, c->lhead_lnum);
		c->lhead_offs = 0;
		ubifs_msg("switch log LEB %d:%d to %d:%d\n", old_lnum, old_offs, c->lhead_lnum, c->lhead_offs);
	}
#endif

	if (c->lhead_offs == 0) {
		/* Must ensure next LEB has been unmapped */
		err = ubifs_leb_unmap(c, c->lhead_lnum);
		if (err)
			goto out;
	}

	len = ALIGN(len, c->min_io_size);
	dbg_log("writing commit start at LEB %d:0, len %d", c->lhead_lnum, len);
	err = ubifs_leb_write(c, c->lhead_lnum, cs, c->lhead_offs, len); //MTK, modify offset 0 -> c->lhead_offs
	if (err)
		goto out;

	*ltail_lnum = c->lhead_lnum;

	c->lhead_offs += len;
	if (c->lhead_offs == c->leb_size) {
		c->lhead_lnum = ubifs_next_log_lnum(c, c->lhead_lnum);
		c->lhead_offs = 0;
	}

	remove_buds(c);

	/*
	 * We have started the commit and now users may use the rest of the log
	 * for new writes.
	 */
	c->min_log_bytes = 0;

out:
	kfree(buf);
	return err;
}
Beispiel #6
0
CSOCKET_CNODE *cconnp_reserve(CCONNP *cconnp)
{
    CSOCKET_CNODE *csocket_cnode;
    int            sockfd;

    if(EC_TRUE == cqueue_is_empty(CCONNP_IDLE_CONN_QUEUE(cconnp)))
    {
        CQUEUE_DATA *cqueue_data;
     
        /*if no idle, new one*/
        if(csocket_connect(CCONNP_SRV_IPADDR(cconnp), CCONNP_SRV_PORT(cconnp), CSOCKET_IS_NONBLOCK_MODE, &sockfd))
        {
            dbg_log(SEC_0154_CCONNP, 0)(LOGSTDOUT, "error:cconnp_reserve: connect server %s:%ld failed\n",
                                CCONNP_SRV_IPADDR_STR(cconnp), CCONNP_SRV_PORT(cconnp));
            return (NULL_PTR);
        }

        csocket_cnode = csocket_cnode_new(CCONNP_SRV_TCID(cconnp), sockfd, CSOCKET_TYPE_TCP, CCONNP_SRV_IPADDR(cconnp), CCONNP_SRV_PORT(cconnp));
        if(NULL_PTR == csocket_cnode)
        {
            dbg_log(SEC_0154_CCONNP, 0)(LOGSTDOUT, "error:cconnp_reserve: new csocket_cnode for socket %d to server %s:%ld failed\n",
                            sockfd, CCONNP_SRV_IPADDR_STR(cconnp), CCONNP_SRV_PORT(cconnp));
            csocket_close(sockfd);
            return (NULL_PTR);
        }

        cqueue_data = cqueue_push(CCONNP_IDLE_CONN_QUEUE(cconnp), (void *)csocket_cnode);
        if(NULL_PTR == cqueue_data)
        {
            dbg_log(SEC_0154_CCONNP, 0)(LOGSTDOUT, "error:cconnp_reserve: push socket %d to server %s:%ld failed\n",
                            sockfd, CCONNP_SRV_IPADDR_STR(cconnp), CCONNP_SRV_PORT(cconnp));
            csocket_cnode_free(csocket_cnode);
            return (NULL_PTR);
        }
     
        CSOCKET_CNODE_WORK_NODE(csocket_cnode)    = (void *)cqueue_data;
        CSOCKET_CNODE_WORK_OWNER(csocket_cnode)   = (void *)cconnp;
        CSOCKET_CNODE_WORK_RELEASE(csocket_cnode) = (CSOCKET_CNODE_WORK_REL)cconnp_erase;;
        CSOCKET_CNODE_WORK_STATUS(csocket_cnode)  = CSOCKET_CNODE_WORK_STATUS_IDLE;

        dbg_log(SEC_0154_CCONNP, 9)(LOGSTDOUT, "[DEBUG] cconnp_reserve: create and push sockfd %d to server %s:%ld done\n",
                        CSOCKET_CNODE_SOCKFD(csocket_cnode),
                        CCONNP_SRV_IPADDR_STR(cconnp), CCONNP_SRV_PORT(cconnp));      
    }

    /*reserve one idle*/
    csocket_cnode = cqueue_pop(CCONNP_IDLE_CONN_QUEUE(cconnp));
    if(NULL_PTR == csocket_cnode)
    {
        dbg_log(SEC_0154_CCONNP, 0)(LOGSTDOUT, "error:cconnp_reserve: server %s:%ld has no idle conn\n",
                        CCONNP_SRV_IPADDR_STR(cconnp), CCONNP_SRV_PORT(cconnp));
        return (NULL_PTR);
    }

    if(EC_TRUE == CSOCKET_CNODE_WORK_PUSHED(csocket_cnode))
    {
        /*when csocket_cnode was released and pushed to connp, RD event was set. here need to clear it*/
        cepoll_del_event(task_brd_default_get_cepoll(), CSOCKET_CNODE_SOCKFD(csocket_cnode), CEPOLL_RD_EVENT);
        CSOCKET_CNODE_WORK_PUSHED(csocket_cnode) = EC_FALSE;
    }
 
    CSOCKET_CNODE_WORK_NODE(csocket_cnode)    = NULL_PTR;
    CSOCKET_CNODE_WORK_OWNER(csocket_cnode)   = (void *)cconnp;
    CSOCKET_CNODE_WORK_RELEASE(csocket_cnode) = (CSOCKET_CNODE_WORK_REL)cconnp_release;
    CSOCKET_CNODE_WORK_STATUS(csocket_cnode)  = CSOCKET_CNODE_WORK_STATUS_NONE;

    dbg_log(SEC_0154_CCONNP, 9)(LOGSTDOUT, "[DEBUG] cconnp_reserve: pop sockfd %d from server %s:%ld done\n",
                    CSOCKET_CNODE_SOCKFD(csocket_cnode),
                    CCONNP_SRV_IPADDR_STR(cconnp), CCONNP_SRV_PORT(cconnp));  
    return (csocket_cnode);
}
/**
 * ubifs_add_bud_to_log - add a new bud to the log.
 * @c: UBIFS file-system description object
 * @jhead: journal head the bud belongs to
 * @lnum: LEB number of the bud
 * @offs: starting offset of the bud
 *
 * This function writes reference node for the new bud LEB @lnum it to the log,
 * and adds it to the buds tress. It also makes sure that log size does not
 * exceed the 'c->max_bud_bytes' limit. Returns zero in case of success,
 * %-EAGAIN if commit is required, and a negative error codes in case of
 * failure.
 */
int ubifs_add_bud_to_log(struct ubifs_info *c, int jhead, int lnum, int offs)
{
	int err;
	struct ubifs_bud *bud;
	struct ubifs_ref_node *ref;

	bud = kmalloc(sizeof(struct ubifs_bud), GFP_NOFS);
	if (!bud)
		return -ENOMEM;
	ref = kzalloc(c->ref_node_alsz, GFP_NOFS);
	if (!ref) {
		kfree(bud);
		return -ENOMEM;
	}

	mutex_lock(&c->log_mutex);
	ubifs_assert(!c->ro_media && !c->ro_mount);
	if (c->ro_error) {
		err = -EROFS;
		goto out_unlock;
	}

	/* Make sure we have enough space in the log */
	if (empty_log_bytes(c) - c->ref_node_alsz < c->min_log_bytes) {
		dbg_log("not enough log space - %lld, required %d",
			empty_log_bytes(c), c->min_log_bytes);
		ubifs_commit_required(c);
		err = -EAGAIN;
		goto out_unlock;
	}

	/*
	 * Make sure the amount of space in buds will not exceed the
	 * 'c->max_bud_bytes' limit, because we want to guarantee mount time
	 * limits.
	 *
	 * It is not necessary to hold @c->buds_lock when reading @c->bud_bytes
	 * because we are holding @c->log_mutex. All @c->bud_bytes take place
	 * when both @c->log_mutex and @c->bud_bytes are locked.
	 */
	if (c->bud_bytes + c->leb_size - offs > c->max_bud_bytes) {
		dbg_log("bud bytes %lld (%lld max), require commit",
			c->bud_bytes, c->max_bud_bytes);
		ubifs_commit_required(c);
		err = -EAGAIN;
		goto out_unlock;
	}

	/*
	 * If the journal is full enough - start background commit. Note, it is
	 * OK to read 'c->cmt_state' without spinlock because integer reads
	 * are atomic in the kernel.
	 */
	if (c->bud_bytes >= c->bg_bud_bytes &&
	    c->cmt_state == COMMIT_RESTING) {
		dbg_log("bud bytes %lld (%lld max), initiate BG commit",
			c->bud_bytes, c->max_bud_bytes);
		ubifs_request_bg_commit(c);
	}

	bud->lnum = lnum;
	bud->start = offs;
	bud->jhead = jhead;

	ref->ch.node_type = UBIFS_REF_NODE;
	ref->lnum = cpu_to_le32(bud->lnum);
	ref->offs = cpu_to_le32(bud->start);
	ref->jhead = cpu_to_le32(jhead);

	if (c->lhead_offs > c->leb_size - c->ref_node_alsz) {
		c->lhead_lnum = ubifs_next_log_lnum(c, c->lhead_lnum);
		c->lhead_offs = 0;
	}

	if (c->lhead_offs == 0) {
		/* Must ensure next log LEB has been unmapped */
		err = ubifs_leb_unmap(c, c->lhead_lnum);
		if (err)
			goto out_unlock;
	}

	if (bud->start == 0) {
		/*
		 * Before writing the LEB reference which refers an empty LEB
		 * to the log, we have to make sure it is mapped, because
		 * otherwise we'd risk to refer an LEB with garbage in case of
		 * an unclean reboot, because the target LEB might have been
		 * unmapped, but not yet physically erased.
		 */
		err = ubifs_leb_map(c, bud->lnum);
		if (err)
			goto out_unlock;
	}

	dbg_log("write ref LEB %d:%d",
		c->lhead_lnum, c->lhead_offs);
	err = ubifs_write_node(c, ref, UBIFS_REF_NODE_SZ, c->lhead_lnum,
			       c->lhead_offs);
	if (err)
		goto out_unlock;

	c->lhead_offs += c->ref_node_alsz;

	ubifs_add_bud(c, bud);

	mutex_unlock(&c->log_mutex);
	kfree(ref);
	return 0;

out_unlock:
	mutex_unlock(&c->log_mutex);
	kfree(ref);
	kfree(bud);
	return err;
}
Beispiel #8
0
/*!
 * \brief Message exchange function communicate with user-mode application.<br>
 * <br>
 * \param [in] pSmartcardExtension A pointer to the smart card extension,
		SMARTCARD_EXTENSION, of the device.
 * \param [in] mty MTY: Message type.
 * \param [in] nad NAD: Node addess
 * \param [in] pSnd PY0: A pointer to first byte of payload.
 * \param [in] sndLen LN: length of payload.
 * \param [out] pRcv A pointer to buffer of received data.
 * \param [in] rcvLenExp length of pRcv. caller's expected Max length of receiving data.
 * \param [out] pRcvLen actual lengh of received data.
 * \param [in] pDueTime wait time duration in LARGE_INTEGER. if it is NULL,
	the routine waits indefinitely.
 *
 * \retval STATUS_SUCCESS the routine successfully end.
 * \retval STATUS_IO_TIMEOUT The request timed out.
 * \retval STATUS_BUFFER_TOO_SMALL Expected ATR Length is too small.
 */
static int sendMessage(
    PREADER_EXTENSION pReaderExtension,
    unsigned char const mty,
    unsigned char const nad,
    char const *const pSnd,
    unsigned short const sndLen,
    char *const pRcv,
    unsigned short const rcvLenExp,
    unsigned short *const pRcvLen,
    PLARGE_INTEGER pDueTime)
{
	dbg_log("sendMessage start");

	NTSTATUS status;

	// set exchanging messages in pReaderExtension->pSndBuffer.
	if (pReaderExtension == NULL) {
		status = STATUS_INSUFFICIENT_RESOURCES;
		dbg_log("pReaderExtension == NULL");
		return status;
	}
	if (pReaderExtension->pSndBuffer == NULL) {
		status = STATUS_INSUFFICIENT_RESOURCES;
		dbg_log("pReaderExtension->pSndBuffer == NULL");
		return status;
	}
	// set message header.
	pReaderExtension->pSndBuffer[0] = mty;			// MTY
	pReaderExtension->pSndBuffer[1] = nad;			// NAD
	pReaderExtension->pSndBuffer[2] = sndLen / 256;	// LNH High byte of payload length
	pReaderExtension->pSndBuffer[3] = sndLen % 256;	// LNL Low byte of payload length
	// set message payload.
	RtlCopyMemory(pReaderExtension->pSndBuffer + 4, pSnd, sndLen);
	// set whole message length.
	pReaderExtension->iSndLen = sndLen + 4;

	// notify to the user-mode application.
	if (pReaderExtension->hEventSnd == NULL) {
		status = STATUS_INSUFFICIENT_RESOURCES;
		dbg_log("pReaderExtension->hEventSnd == NULL");
		return status;
	}
	KeSetEvent((PKEVENT)pReaderExtension->hEventSnd, 0, FALSE);

	// wait for the process completion of user-mode application as follows:
	//  1. invoke ReadFile and get command data in pReaderExtension->pSndBuffer.
	//  2. communicate with JCOP simulator.
	//  3. invoke WriteFile and set response data in pReaderExtension->pRcvBuffer.
	//  4. set event pReaderExtension->hEventRcv.

	if (pReaderExtension->hEventRcv == NULL) {
		status = STATUS_INSUFFICIENT_RESOURCES;
		dbg_log("pReaderExtension->hEventRcv == NULL");
		return status;
	}
	// wait for event.
	status = KeWaitForSingleObject(
	             (PKEVENT)pReaderExtension->hEventRcv,
	             Executive,
	             KernelMode,
	             FALSE,
	             pDueTime
	         );
	if (status != STATUS_SUCCESS) {
		switch (status) {
			case STATUS_ALERTED :
				dbg_log("STATUS_ALERTED\r\n");
				break;
			case STATUS_USER_APC :
				dbg_log("STATUS_USER_APC \r\n");
				break;
			case STATUS_TIMEOUT :
				dbg_log("STATUS_TIMEOUT \r\n");
				break;
			case STATUS_ABANDONED_WAIT_0 :
				dbg_log("STATUS_ABANDONED_WAIT_0 \r\n");
				break;
			default:
				dbg_log("STATUS_XXXXX \r\n");
				break;
		}
		return status;
	}

	// get data from pReaderExtension->pRcvBuffer.
	if (rcvLenExp < pReaderExtension->iRcvLen) {
		dbg_log("STATUS_BUFFER_TOO_SMALL - *pRcvLen: %d", *pRcvLen);
		return STATUS_BUFFER_TOO_SMALL;
	}
	if (pReaderExtension->pRcvBuffer == NULL) {
		status = STATUS_INSUFFICIENT_RESOURCES;
		dbg_log("pReaderExtension->pRcvBuffer == NULL");
		return status;
	}
	*pRcvLen = pReaderExtension->iRcvLen;
	RtlCopyMemory(pRcv, pReaderExtension->pRcvBuffer, pReaderExtension->iRcvLen);

	dbg_log("pReaderExtension->iRcvLen: %d", pReaderExtension->iRcvLen);
	dbg_ba2s(pRcv, pReaderExtension->iRcvLen);

	status = STATUS_SUCCESS;
	dbg_log("sendMessage end - status: 0x%08X", status);
	return status;
}
Beispiel #9
0
static rt_err_t configure(struct rt_spi_device *device,
                          struct rt_spi_configuration *configuration)
{
    struct rt_spi_bus *spi_bus = (struct rt_spi_bus *)device->bus;
    struct tina_spi_cs *tina_spi_cs = device->parent.user_data;
    struct tina_spi *_spi_info = (struct tina_spi *)spi_bus->parent.user_data;
    SPI_T *spi = _spi_info->spi;

    DEBUG_PRINTF("%s -> %d\n", __FUNCTION__, __LINE__);

    RT_ASSERT(device != RT_NULL);
    RT_ASSERT(configuration != RT_NULL);

    DEBUG_PRINTF("%s -> %d\n", __FUNCTION__, __LINE__);

    DEBUG_PRINTF("spi address: %08X\n", (rt_uint32_t)spi);

    SPI_Disable(spi);
    SPI_Reset(spi);
    SPI_ResetRxFifo(spi);
    SPI_ResetTxFifo(spi);

    DEBUG_PRINTF("%s -> %d\n", __FUNCTION__, __LINE__);

    /* data_width */
    if (configuration->data_width != 8)
    {
        DEBUG_PRINTF("error: data_width is %d\n", configuration->data_width);
        return RT_EIO;
    }

    DEBUG_PRINTF("%s -> %d\n", __FUNCTION__, __LINE__);
    SPI_SetDuplex(spi, SPI_TCTRL_DHB_FULL_DUPLEX);
    SPI_SetMode(spi, SPI_CTRL_MODE_MASTER);

    /* MSB or LSB */
    if (configuration->mode & RT_SPI_MSB)
    {
        SPI_SetFirstTransmitBit(spi, SPI_TCTRL_FBS_MSB);
    }
    else
    {
        SPI_SetFirstTransmitBit(spi, SPI_TCTRL_FBS_LSB);
    }

    switch (configuration->mode)
    {
    case RT_SPI_MODE_0:
        SPI_SetSclkMode(spi, SPI_SCLK_Mode0);
        break;
    case RT_SPI_MODE_1:
        SPI_SetSclkMode(spi, SPI_SCLK_Mode1);
        break;
    case RT_SPI_MODE_2:
        SPI_SetSclkMode(spi, SPI_SCLK_Mode2);
        break;
    case RT_SPI_MODE_3:
        SPI_SetSclkMode(spi, SPI_SCLK_Mode3);
        break;
    }

    /* baudrate */
    {
        unsigned int spi_clock = 0;
        rt_uint32_t max_hz;
        rt_uint32_t div;

        DEBUG_PRINTF("%s -> %d\n", __FUNCTION__, __LINE__);

        max_hz = configuration->max_hz;

        if (max_hz > SPI_BUS_MAX_CLK)
        {
            max_hz = SPI_BUS_MAX_CLK;
        }
        spi_clock = ahb_get_clk();

        DEBUG_PRINTF("%s -> %d\n", __FUNCTION__, __LINE__);

        div = (spi_clock + max_hz - 1) / max_hz;

        dbg_log(DBG_LOG, "configuration->max_hz: %d\n", configuration->max_hz);
        dbg_log(DBG_LOG, "max freq: %d\n", max_hz);
        dbg_log(DBG_LOG, "spi_clock: %d\n", spi_clock);
        dbg_log(DBG_LOG, "div: %d\n", div);

        SPI_SetClkDiv(spi, div / 2);
    } /* baudrate */

    SPI_ManualChipSelect(spi, tina_spi_cs->cs);
    SPI_SetDataSize(spi, 0, 0);
    SPI_Enable(spi);

    DEBUG_PRINTF("%s -> %d\n", __FUNCTION__, __LINE__);

    return RT_EOK;
};
Beispiel #10
0
static void _signal_default_handler(int signo)
{
    dbg_log(DBG_INFO, "handled signo[%d] with default action.\n", signo);
    return ;
}
Beispiel #11
0
/* Walk through the table and remove all entries which lifetime ended.

   We have a problem here.  To actually remove the entries we must get
   the write-lock.  But since we want to keep the time we have the
   lock as short as possible we cannot simply acquire the lock when we
   start looking for timedout entries.

   Therefore we do it in two stages: first we look for entries which
   must be invalidated and remember them.  Then we get the lock and
   actually remove them.  This is complicated by the way we have to
   free the data structures since some hash table entries share the same
   data.  */
time_t
prune_cache (struct database_dyn *table, time_t now, int fd)
{
  size_t cnt = table->head->module;

  /* If this table is not actually used don't do anything.  */
  if (cnt == 0)
    {
      if (fd != -1)
	{
	  /* Reply to the INVALIDATE initiator.  */
	  int32_t resp = 0;
	  writeall (fd, &resp, sizeof (resp));
	}

      /* No need to do this again anytime soon.  */
      return 24 * 60 * 60;
    }

  /* If we check for the modification of the underlying file we invalidate
     the entries also in this case.  */
  if (table->inotify_descr < 0 && table->check_file && now != LONG_MAX)
    {
      struct stat64 st;

      if (stat64 (table->filename, &st) < 0)
	{
	  char buf[128];
	  /* We cannot stat() the file, disable file checking if the
             file does not exist.  */
	  dbg_log (_("cannot stat() file `%s': %s"),
		   table->filename, strerror_r (errno, buf, sizeof (buf)));
	  if (errno == ENOENT)
	    table->check_file = 0;
	}
      else
	{
	  if (st.st_mtime != table->file_mtime)
	    {
	      /* The file changed.  Invalidate all entries.  */
	      now = LONG_MAX;
	      table->file_mtime = st.st_mtime;
	    }
	}
    }

  /* We run through the table and find values which are not valid anymore.

     Note that for the initial step, finding the entries to be removed,
     we don't need to get any lock.  It is at all timed assured that the
     linked lists are set up correctly and that no second thread prunes
     the cache.  */
  bool *mark;
  size_t memory_needed = cnt * sizeof (bool);
  bool mark_use_alloca;
  if (__builtin_expect (memory_needed <= MAX_STACK_USE, 1))
    {
      mark = alloca (cnt * sizeof (bool));
      memset (mark, '\0', memory_needed);
      mark_use_alloca = true;
    }
  else
    {
      mark = xcalloc (1, memory_needed);
      mark_use_alloca = false;
    }
  size_t first = cnt + 1;
  size_t last = 0;
  char *const data = table->data;
  bool any = false;

  if (__builtin_expect (debug_level > 2, 0))
    dbg_log (_("pruning %s cache; time %ld"),
	     dbnames[table - dbs], (long int) now);

#define NO_TIMEOUT LONG_MAX
  time_t next_timeout = NO_TIMEOUT;
  do
    {
      ref_t run = table->head->array[--cnt];

      while (run != ENDREF)
	{
	  struct hashentry *runp = (struct hashentry *) (data + run);
	  struct datahead *dh = (struct datahead *) (data + runp->packet);

	  /* Some debug support.  */
	  if (__builtin_expect (debug_level > 2, 0))
	    {
	      char buf[INET6_ADDRSTRLEN];
	      const char *str;

	      if (runp->type == GETHOSTBYADDR || runp->type == GETHOSTBYADDRv6)
		{
		  inet_ntop (runp->type == GETHOSTBYADDR ? AF_INET : AF_INET6,
			     data + runp->key, buf, sizeof (buf));
		  str = buf;
		}
	      else
		str = data + runp->key;

	      dbg_log (_("considering %s entry \"%s\", timeout %" PRIu64),
		       serv2str[runp->type], str, dh->timeout);
	    }

	  /* Check whether the entry timed out.  */
	  if (dh->timeout < now)
	    {
	      /* This hash bucket could contain entries which need to
		 be looked at.  */
	      mark[cnt] = true;

	      first = MIN (first, cnt);
	      last = MAX (last, cnt);

	      /* We only have to look at the data of the first entries
		 since the count information is kept in the data part
		 which is shared.  */
	      if (runp->first)
		{

		  /* At this point there are two choices: we reload the
		     value or we discard it.  Do not change NRELOADS if
		     we never not reload the record.  */
		  if ((reload_count != UINT_MAX
		       && __builtin_expect (dh->nreloads >= reload_count, 0))
		      /* We always remove negative entries.  */
		      || dh->notfound
		      /* Discard everything if the user explicitly
			 requests it.  */
		      || now == LONG_MAX)
		    {
		      /* Remove the value.  */
		      dh->usable = false;

		      /* We definitely have some garbage entries now.  */
		      any = true;
		    }
		  else
		    {
		      /* Reload the value.  We do this only for the
			 initially used key, not the additionally
			 added derived value.  */
		      assert (runp->type < LASTREQ
			      && readdfcts[runp->type] != NULL);

		      readdfcts[runp->type] (table, runp, dh);

		      /* If the entry has been replaced, we might need
			 cleanup.  */
		      any |= !dh->usable;
		    }
		}
	    }
	  else
	    {
	      assert (dh->usable);
	      next_timeout = MIN (next_timeout, dh->timeout);
	    }

	  run = runp->next;
	}
    }
  while (cnt > 0);

  if (__builtin_expect (fd != -1, 0))
    {
      /* Reply to the INVALIDATE initiator that the cache has been
	 invalidated.  */
      int32_t resp = 0;
      writeall (fd, &resp, sizeof (resp));
    }

  if (first <= last)
    {
      struct hashentry *head = NULL;

      /* Now we have to get the write lock since we are about to modify
	 the table.  */
      if (__builtin_expect (pthread_rwlock_trywrlock (&table->lock) != 0, 0))
	{
	  ++table->head->wrlockdelayed;
	  pthread_rwlock_wrlock (&table->lock);
	}

      while (first <= last)
	{
	  if (mark[first])
	    {
	      ref_t *old = &table->head->array[first];
	      ref_t run = table->head->array[first];

	      assert (run != ENDREF);
	      do
		{
		  struct hashentry *runp = (struct hashentry *) (data + run);
		  struct datahead *dh
		    = (struct datahead *) (data + runp->packet);

		  if (! dh->usable)
		    {
		      /* We need the list only for debugging but it is
			 more costly to avoid creating the list than
			 doing it.  */
		      runp->dellist = head;
		      head = runp;

		      /* No need for an atomic operation, we have the
			 write lock.  */
		      --table->head->nentries;

		      run = *old = runp->next;
		    }
		  else
		    {
		      old = &runp->next;
		      run = runp->next;
		    }
		}
	      while (run != ENDREF);
	    }

	  ++first;
	}

      /* It's all done.  */
      pthread_rwlock_unlock (&table->lock);

      /* Make sure the data is saved to disk.  */
      if (table->persistent)
	msync (table->head,
	       data + table->head->first_free - (char *) table->head,
	       MS_ASYNC);

      /* One extra pass if we do debugging.  */
      if (__builtin_expect (debug_level > 0, 0))
	{
	  struct hashentry *runp = head;

	  while (runp != NULL)
	    {
	      char buf[INET6_ADDRSTRLEN];
	      const char *str;

	      if (runp->type == GETHOSTBYADDR || runp->type == GETHOSTBYADDRv6)
		{
		  inet_ntop (runp->type == GETHOSTBYADDR ? AF_INET : AF_INET6,
			     data + runp->key, buf, sizeof (buf));
		  str = buf;
		}
	      else
		str = data + runp->key;

	      dbg_log ("remove %s entry \"%s\"", serv2str[runp->type], str);

	      runp = runp->dellist;
	    }
	}
    }

  if (__builtin_expect (! mark_use_alloca, 0))
    free (mark);

  /* Run garbage collection if any entry has been removed or replaced.  */
  if (any)
    gc (table);

  /* If there is no entry in the database and we therefore have no new
     timeout value, tell the caller to wake up in 24 hours.  */
  return next_timeout == NO_TIMEOUT ? 24 * 60 * 60 : next_timeout - now;
}
Beispiel #12
0
uint8_t
gdbGetFreeBlockList(GDatabase *db, GdbFreeBlock **blocks, uint32_t *count)
{
    GdbFreeBlock *blockList;
    uint32_t listSize;
    uint8_t *buffer;
    size_t   s;
    uint32_t i, counter = 0;
    offset_t __offset;

    if (blocks == NULL || count == NULL)
    {
        return 0;
    }
    *blocks = NULL;

    /* Seek to the start of the block list. */
    rawFileSeek(db->idxRawFile, DB_FREE_BLOCK_LIST_OFFSET, SEEK_SET);
    __offset = DB_FREE_BLOCK_LIST_OFFSET;
    if (rawFileRead(db->idxRawFile, __offset, &db->freeBlockCount, sizeof(uint32_t), 1, LOC_DB_0001) != 1)
    {
        db->freeBlockCount = 0;
    }
    else
    {
        __offset += sizeof(uint32_t);
    }
    db->freeBlockCount = gdb_ntoh_uint32(db->freeBlockCount);

    *count = db->freeBlockCount;

    if (db->freeBlockCount == 0)
    {
        return 0;
    }
    /* Get the total size of the free blocks list. */
    listSize = db->freeBlockCount * (sizeof(uint16_t) + sizeof(offset_t));

    /* Allocate the buffer. */
    MEM_CHECK(buffer = (uint8_t *)SAFE_MALLOC(listSize, LOC_DB_0002));

    /* Read in the list. */
    //rawFileSeek(db->idxRawFile, DB_FREE_BLOCK_LIST_OFFSET + sizeof(uint32_t), SEEK_SET);
    if ((s = rawFileRead(db->idxRawFile, __offset, buffer, 1, listSize, LOC_DB_0003)) != listSize)
    {
        dbg_log(SEC_0131_DB, 0)(LOGSTDOUT,"error:gdbGetFreeBlockList: Truncated block list.\n"
                          "Expected %d bytes, got %d bytes. Block list offset = %d\n"
                          "Free block count = %d. Filename = %s\n",
                          listSize, s, DB_FREE_BLOCK_LIST_OFFSET, db->freeBlockCount,
                          db->filename);
        abort();
    }

    MEM_CHECK(blockList = (GdbFreeBlock *)SAFE_MALLOC(db->freeBlockCount * sizeof(GdbFreeBlock), LOC_DB_0004));

    for (i = 0; i < db->freeBlockCount; i++)
    {
        blockList[i].size   = gdbGet16(buffer, &counter);
        blockList[i].offset = gdbGetOffset(buffer, &counter);
    }

    *blocks = blockList;

    SAFE_FREE(buffer, LOC_DB_0005);

    return 1;
}
Beispiel #13
0
EC_BOOL lic_cfg_flush(int fd, const LIC_CFG *lic_cfg)
{
    UINT32 offset;

    offset = 0;

    if(EC_FALSE == lic_buff_flush(fd, &offset, LIC_VERSION_MAX_SIZE, LIC_CFG_VERSION(lic_cfg)))
    {
        dbg_log(SEC_0060_LICENSE, 0)(LOGSTDOUT, "error:lic_cfg_flush: flush version failed\n");
        return (EC_FALSE);
    }

    if(EC_FALSE == lic_buff_flush(fd, &offset, sizeof(LIC_MAC), (UINT8 *)LIC_CFG_MAC(lic_cfg)))
    {
        dbg_log(SEC_0060_LICENSE, 0)(LOGSTDOUT, "error:lic_cfg_flush: flush mac failed\n");
        return (EC_FALSE);
    }

    if(EC_FALSE == lic_buff_flush(fd, &offset, sizeof(LIC_DATE), (UINT8 *)LIC_CFG_DATE(lic_cfg)))
    {
        dbg_log(SEC_0060_LICENSE, 0)(LOGSTDOUT, "error:lic_cfg_flush: flush date failed\n");
        return (EC_FALSE);
    }

    if(EC_FALSE == lic_buff_flush(fd, &offset, LIC_USER_NAME_MAX_SIZE, LIC_CFG_USER_NAME(lic_cfg)))
    {
        dbg_log(SEC_0060_LICENSE, 0)(LOGSTDOUT, "error:lic_cfg_flush: flush user name failed\n");
        return (EC_FALSE);
    }

    if(EC_FALSE == lic_buff_flush(fd, &offset, LIC_USER_EMAIL_MAX_SIZE, LIC_CFG_USER_EMAIL(lic_cfg)))
    {
        dbg_log(SEC_0060_LICENSE, 0)(LOGSTDOUT, "error:lic_cfg_flush: flush user email failed\n");
        return (EC_FALSE);
    }

    if(EC_FALSE == lic_buff_flush(fd, &offset, LIC_VENDOR_NAME_MAX_SIZE, LIC_CFG_VENDOR_NAME(lic_cfg)))
    {
        dbg_log(SEC_0060_LICENSE, 0)(LOGSTDOUT, "error:lic_cfg_flush: flush vendor name failed\n");
        return (EC_FALSE);
    }

    if(EC_FALSE == lic_buff_flush(fd, &offset, LIC_VENDOR_EMAIL_MAX_SIZE, LIC_CFG_VENDOR_EMAIL(lic_cfg)))
    {
        dbg_log(SEC_0060_LICENSE, 0)(LOGSTDOUT, "error:lic_cfg_flush: flush vendor email failed\n");
        return (EC_FALSE);
    }

    if(EC_FALSE == lic_buff_flush(fd, &offset, sizeof(BIGINT), (UINT8 *)LIC_CFG_PRIVATE_KEY(lic_cfg)))
    {
        dbg_log(SEC_0060_LICENSE, 0)(LOGSTDOUT, "error:lic_cfg_flush: flush private key failed\n");
        return (EC_FALSE);
    }

    if(EC_FALSE == lic_buff_flush(fd, &offset, sizeof(ECC_SIGNATURE), (UINT8 *)LIC_CFG_SIGNATURE(lic_cfg)))
    {
        dbg_log(SEC_0060_LICENSE, 0)(LOGSTDOUT, "error:lic_cfg_flush: flush signature failed\n");
        return (EC_FALSE);
    }

    return (EC_TRUE);
}
Beispiel #14
0
uint8_t btreeSplit(const BTree *src_tree, const RawFile *src_rawFile,
                    BTree *des_tree_left, RawFile *des_rawFile_left,
                    BTree *des_tree_right, RawFile *des_rawFile_right)
{
    BTreeTraversal *trav;
    uint32_t count;
    offset_t offset;

    if (src_tree == NULL)
    {
        dbg_log(SEC_0130_BTREE, 0)(LOGSTDOUT, "error:btreeSplit: src tree is null\n");
        return 0;/*fail*/
    }
    trav = btreeInitTraversal(src_tree);

    for(count = 0, offset = btreeGetFirstOffset(trav);
        count < (src_tree->size / 2) && 0 != offset;
        count ++, offset = btreeGetNextOffset(trav))
    {
        uint32_t data_len;
        uint32_t kv_len;
        uint8_t *kv;
        uint8_t *key;
        uint32_t filePos;

        if(RAW_FILE_FAIL == rawFileRead32(src_rawFile, &data_len, offset))
        {
            dbg_log(SEC_0130_BTREE, 0)(LOGSTDOUT,"error:btreeSplit: read data_len at offset %d failed where count reaches %d\n", offset, count);
            btreeDestroyTraversal(trav);
            return 0;
        }

        kv = (uint8_t *)SAFE_MALLOC(data_len, LOC_BTREE_0127);
        if(NULL == kv)
        {
            dbg_log(SEC_0130_BTREE, 0)(LOGSTDOUT, "error:btreeSplit: alloc %d bytes failed\n", data_len);
            return 0;
        }

        if(RAW_FILE_FAIL == rawFileRead8s(src_rawFile, kv, data_len, &kv_len, offset + sizeof(uint32_t)) || kv_len != data_len)
        {
            dbg_log(SEC_0130_BTREE, 0)(LOGSTDOUT,"error:btreeSplit: read kv %ld bytes at offset %d failed where count reaches %d, kv_len = %d\n",
                    data_len, offset + sizeof(uint32_t), count, kv_len);

            SAFE_FREE(kv, LOC_BTREE_0128);
            btreeDestroyTraversal(trav);
            return 0;
        }

        //dbg_log(SEC_0130_BTREE, 9)(LOGSTDOUT, "[DEBUG] btreeSplit: read kv[1]: ");
        //kvPrintHs(LOGSTDOUT, kv);

        key = kv;

        rawFileAppend8slen(des_rawFile_left, kv, kv_len, &filePos);
        btreeInsert(des_tree_left, key, filePos, 0);

        SAFE_FREE(kv, LOC_BTREE_0129);
    }

    for(;
        count < src_tree->size && 0 != offset;
        count ++, offset = btreeGetNextOffset(trav))
    {
        uint32_t data_len;
        uint32_t kv_len;
        uint8_t *kv;
        uint8_t *key;
        uint32_t filePos;

        if(RAW_FILE_FAIL == rawFileRead32(src_rawFile, &data_len, offset))
        {
            dbg_log(SEC_0130_BTREE, 0)(LOGSTDOUT,"error:btreeSplit: read data_len at offset %d failed where count reaches %d\n", offset, count);
            btreeDestroyTraversal(trav);
            return 0;
        }

        kv = (uint8_t *)SAFE_MALLOC(data_len, LOC_BTREE_0130);
        if(NULL == kv)
        {
            dbg_log(SEC_0130_BTREE, 0)(LOGSTDOUT, "error:btreeSplit: alloc %d bytes failed\n", data_len);
            return 0;
        }

        if(RAW_FILE_FAIL == rawFileRead8s(src_rawFile, kv, data_len, &kv_len, offset + sizeof(uint32_t)) || kv_len != data_len)
        {
            dbg_log(SEC_0130_BTREE, 0)(LOGSTDOUT,"error:btreeSplit: read kv %ld bytes at offset %d failed where count reaches %d, kv_len = %d\n",
                    data_len, offset + sizeof(uint32_t), count, kv_len);

            SAFE_FREE(kv, LOC_BTREE_0131);
            btreeDestroyTraversal(trav);
            return 0;
        }

        //dbg_log(SEC_0130_BTREE, 9)(LOGSTDOUT, "[DEBUG] btreeSplit: read kv[2]: ");
        //kvPrintHs(LOGSTDOUT, kv);

        key = kv;

        rawFileAppend8slen(des_rawFile_right, kv, kv_len, &filePos);
        btreeInsert(des_tree_right, key, filePos, 0);

        SAFE_FREE(kv, LOC_BTREE_0132);
    }

    btreeDestroyTraversal(trav);
    return 1;
}
/*----------------------------------------------------------------------------*/
void hw_init(void)
{
    unsigned int cp15;

    /*
     * Configure PIOs 
     */
    const struct pio_desc hw_pio[] = {
#ifdef CONFIG_DEBUG
        {"RXD", AT91C_PIN_PA(9), 0, PIO_DEFAULT, PIO_PERIPH_A},
        {"TXD", AT91C_PIN_PA(10), 0, PIO_DEFAULT, PIO_PERIPH_A},
#endif
        {(char *)0, 0, 0, PIO_DEFAULT, PIO_PERIPH_A},
    };

    /*
     * Disable watchdog 
     */
    writel(AT91C_WDTC_WDDIS, AT91C_BASE_WDTC + WDTC_WDMR);

    /*
     * At this stage the main oscillator is supposed to be enabled
     * * PCK = MCK = MOSC 
     */
    writel(0x00, AT91C_BASE_PMC + PMC_PLLICPR);

    /*
     * Configure PLLA = MOSC * (PLL_MULA + 1) / PLL_DIVA 
     */
    pmc_cfg_plla(PLLA_SETTINGS, PLL_LOCK_TIMEOUT);

    /*
     * PCK = PLLA/2 = 3 * MCK 
     */
    pmc_cfg_mck(BOARD_PRESCALER_MAIN_CLOCK, PLL_LOCK_TIMEOUT);

    /*
     * Switch MCK on PLLA output 
     */
    pmc_cfg_mck(BOARD_PRESCALER_PLLA, PLL_LOCK_TIMEOUT);

    /*
     * Enable External Reset 
     */
    writel(AT91C_RSTC_KEY_UNLOCK
           || AT91C_RSTC_URSTEN, AT91C_BASE_RSTC + RSTC_RMR);

    /*
     * Configure CP15 
     */
    cp15 = get_cp15();
    cp15 |= I_CACHE;
    set_cp15(cp15);

#ifdef CONFIG_SCLK
    sclk_enable();
#endif
    /*
     * Configure the PIO controller 
     */
    writel((1 << AT91C_ID_PIOA_B), (PMC_PCER + AT91C_BASE_PMC));
    pio_setup(hw_pio);

    /*
     * Enable Debug messages on the DBGU 
     */
#ifdef CONFIG_DEBUG
    dbgu_init(BAUDRATE(MASTER_CLOCK, 115200));
    dbgu_print("Start AT91Bootstrap...\n\r");
#endif

#ifdef CONFIG_DDR2
    /*
     * Configure DDRAM Controller 
     */
    dbg_log(1, "Init DDR... ");
    ddramc_hw_init();
    dbg_log(1, "Done!\n\r");
#endif                          /* CONFIG_DDR2 */
}
Beispiel #16
0
EC_BOOL  ipv4_pool_init(IPV4_POOL *ipv4_pool, const uint32_t ipv4_subnet, const uint32_t ipv4_mask)
{
    UINT32 ipv4_addr_max_num;

    IPV4_POOL_SUBNET(ipv4_pool) = (ipv4_subnet & ipv4_mask);
    IPV4_POOL_MASK(ipv4_pool)   = ipv4_mask;

    IPV4_POOL_INIT_CRWLOCK(ipv4_pool, LOC_IPV4POOL_0003);

    ipv4_addr_max_num = (~ipv4_mask) + 1;
    dbg_log(SEC_0027_IPV4POOL, 9)(LOGSTDOUT, "[DEBUG] ipv4_pool_init: subnet %s, mask %s, ipv4 addr max num %ld\n",
                        c_word_to_ipv4(IPV4_POOL_SUBNET(ipv4_pool)),
                        c_word_to_ipv4(IPV4_POOL_MASK(ipv4_pool)),
                        ipv4_addr_max_num);
    if(EC_FALSE == cbitmap_init(IPV4_POOL_CBITMAP(ipv4_pool), ipv4_addr_max_num))
    {
        dbg_log(SEC_0027_IPV4POOL, 0)(LOGSTDOUT, "error:ipv4_pool_init: init cbitmap with ipv4 addr max num %ld failed\n", ipv4_addr_max_num);
        IPV4_POOL_CLEAN_CRWLOCK(ipv4_pool, LOC_IPV4POOL_0004);
        return (EC_FALSE);
    }
    CBITMAP_MAX_BITS(IPV4_POOL_CBITMAP(ipv4_pool)) = ipv4_addr_max_num; /*for safe purpose*/

#if 0
    /*ignore some ipaddr*/
#if  0
    ipv4_addr = 0;
    bit_pos   = (ipv4_addr & (~ IPV4_POOL_MASK(ipv4_pool)));
    if(EC_FALSE == cbitmap_set(IPV4_POOL_CBITMAP(ipv4_pool), bit_pos))
    {
        dbg_log(SEC_0027_IPV4POOL, 0)(LOGSTDOUT, "error:ipv4_pool_init: cbitmap set ipv4 %s failed\n", c_word_to_ipv4(ipv4_addr));
        IPV4_POOL_CLEAN_CRWLOCK(ipv4_pool, LOC_IPV4POOL_0005);
        cbitmap_clean(IPV4_POOL_CBITMAP(ipv4_pool));
        return (EC_FALSE);
    }
    dbg_log(SEC_0027_IPV4POOL, 9)(LOGSTDOUT, "[DEBUG] ipv4_pool_init: ignore ipv4 %s\n", c_word_to_ipv4(ipv4_addr));
#endif

#if  1
    ipv4_addr = (ipv4_subnet & 0xffffff00);
    bit_pos   = (ipv4_addr & (~ IPV4_POOL_MASK(ipv4_pool)));
    if(EC_FALSE == cbitmap_set(IPV4_POOL_CBITMAP(ipv4_pool), bit_pos))
    {
        dbg_log(SEC_0027_IPV4POOL, 0)(LOGSTDOUT, "error:ipv4_pool_init: cbitmap set ipv4 %s failed\n", c_word_to_ipv4(ipv4_addr));
        IPV4_POOL_CLEAN_CRWLOCK(ipv4_pool, LOC_IPV4POOL_0006);
        cbitmap_clean(IPV4_POOL_CBITMAP(ipv4_pool));
        return (EC_FALSE);
    }
    dbg_log(SEC_0027_IPV4POOL, 9)(LOGSTDOUT, "[DEBUG] ipv4_pool_init: ignore ipv4 %s\n", c_word_to_ipv4(ipv4_addr));
#endif

    ipv4_addr = (IPV4_POOL_SUBNET(ipv4_pool) | ((~ipv4_mask) & 0xffffffff));
    bit_pos   =  (ipv4_addr & (~ IPV4_POOL_MASK(ipv4_pool)));
    if(bit_pos < CBITMAP_MAX_BITS(IPV4_POOL_CBITMAP(ipv4_pool)) && EC_FALSE == cbitmap_set(IPV4_POOL_CBITMAP(ipv4_pool), bit_pos))
    {
        dbg_log(SEC_0027_IPV4POOL, 0)(LOGSTDOUT, "error:ipv4_pool_init: cbitmap set ipv4 %s failed\n", c_word_to_ipv4(ipv4_addr));
        IPV4_POOL_CLEAN_CRWLOCK(ipv4_pool, LOC_IPV4POOL_0007);
        cbitmap_clean(IPV4_POOL_CBITMAP(ipv4_pool));
        return (EC_FALSE);
    }
    dbg_log(SEC_0027_IPV4POOL, 9)(LOGSTDOUT, "[DEBUG] ipv4_pool_init: ignore ipv4 %s\n", c_word_to_ipv4(ipv4_addr));

    ipv4_addr = (IPV4_POOL_SUBNET(ipv4_pool) | ((~ipv4_mask) & 0xfffffffe));
    bit_pos   =  (ipv4_addr & (~ IPV4_POOL_MASK(ipv4_pool)));
    if(bit_pos < CBITMAP_MAX_BITS(IPV4_POOL_CBITMAP(ipv4_pool)) && EC_FALSE == cbitmap_set(IPV4_POOL_CBITMAP(ipv4_pool), bit_pos))
    {
        dbg_log(SEC_0027_IPV4POOL, 0)(LOGSTDOUT, "error:ipv4_pool_init: cbitmap set ipv4 %s failed\n", c_word_to_ipv4(ipv4_addr));
        IPV4_POOL_CLEAN_CRWLOCK(ipv4_pool, LOC_IPV4POOL_0008);
        cbitmap_clean(IPV4_POOL_CBITMAP(ipv4_pool));
        return (EC_FALSE);
    }
    dbg_log(SEC_0027_IPV4POOL, 9)(LOGSTDOUT, "[DEBUG] ipv4_pool_init: ignore ipv4 %s\n", c_word_to_ipv4(ipv4_addr));
#endif
    return (EC_TRUE);
}
Beispiel #17
0
static EC_BOOL taskcfgchk_route_test(LOG *log, const TASK_CFG *task_cfg, TASKS_CFG *src_tasks_cfg, const UINT32 des_tcid, const UINT32 max_hops)
{
    UINT32 pos;

    if(EC_TRUE == taskcfgchk_conn_test(task_cfg, src_tasks_cfg, des_tcid))
    {
        sys_log(log, "[TASKCFGCHK] %s ==> %s [SUCC]\n", TASKS_CFG_TCID_STR(src_tasks_cfg), c_word_to_ipv4(des_tcid));
        return (EC_TRUE);
    }

    if(0 == max_hops)
    {
        sys_log(log, "[TASKCFGCHK] ==> %s [STOP]\n", TASKS_CFG_TCID_STR(src_tasks_cfg));
        return (EC_FALSE);
    }

    CVECTOR_LOCK(TASKS_CFG_TASKR_CFG_VEC(src_tasks_cfg), LOC_TASKCFGCHK_0011);
    for(pos = 0; pos < cvector_size(TASKS_CFG_TASKR_CFG_VEC(src_tasks_cfg)); pos ++)
    {
        TASKR_CFG *taskr_cfg;
        UINT32 taskr_cfg_mask;

        taskr_cfg = (TASKR_CFG *)cvector_get_no_lock(TASKS_CFG_TASKR_CFG_VEC(src_tasks_cfg), pos);
        if(NULL_PTR == taskr_cfg)
        {
            continue;
        }

        taskr_cfg_mask = TASKR_CFG_MASKR(taskr_cfg);

        /*when des_tcid belong to the intranet of taskr_cfg, i.e., belong to the route*/
        if((des_tcid & taskr_cfg_mask) == (TASKR_CFG_DES_TCID(taskr_cfg) & taskr_cfg_mask))
        {
            TASKS_CFG *rt_tasks_cfg;

            dbg_log(SEC_0057_TASKCFGCHK, 5)(LOGSTDNULL, "[TASKCFGCHK] %s & %s == %s & %s\n",
                            c_word_to_ipv4(des_tcid), c_word_to_ipv4(taskr_cfg_mask),
                            TASKR_CFG_DES_TCID_STR(taskr_cfg), c_word_to_ipv4(taskr_cfg_mask)
                            );

            rt_tasks_cfg = task_cfg_searchs(task_cfg, TASKR_CFG_NEXT_TCID(taskr_cfg), CMPI_ANY_MASK, CMPI_ANY_MASK);
            if(NULL_PTR == rt_tasks_cfg)
            {
                continue;
            }

            sys_log(log, "[TASKCFGCHK] %s ==> %s\n", TASKS_CFG_TCID_STR(src_tasks_cfg), TASKR_CFG_NEXT_TCID_STR(taskr_cfg));

            CVECTOR_UNLOCK(TASKS_CFG_TASKR_CFG_VEC(src_tasks_cfg), LOC_TASKCFGCHK_0012);
            if(EC_TRUE == taskcfgchk_route_test(log, task_cfg, rt_tasks_cfg, des_tcid, max_hops - 1))/*recursively*/
            {
                return (EC_TRUE);
            }
            CVECTOR_LOCK(TASKS_CFG_TASKR_CFG_VEC(src_tasks_cfg), LOC_TASKCFGCHK_0013);
        }
        else
        {
            dbg_log(SEC_0057_TASKCFGCHK, 5)(LOGSTDNULL, "[TASKCFGCHK] %s & %s != %s & %s\n",
                            c_word_to_ipv4(des_tcid), c_word_to_ipv4(taskr_cfg_mask),
                            TASKR_CFG_DES_TCID_STR(taskr_cfg), c_word_to_ipv4(taskr_cfg_mask)
                            );
        }
    }
    CVECTOR_UNLOCK(TASKS_CFG_TASKR_CFG_VEC(src_tasks_cfg), LOC_TASKCFGCHK_0014);
    return (EC_FALSE);
}
Beispiel #18
0
/* Check the permission from the caller (via getpeercon) to nscd.
   Returns 0 if access is allowed, 1 if denied, and -1 on error.

   The SELinux policy, enablement, and permission bits are all dynamic and the
   caching done by glibc is not entirely correct.  This nscd support should be
   rewritten to use selinux_check_permission.  A rewrite is risky though and
   requires some refactoring.  Currently we use symbolic mappings instead of
   compile time constants (which SELinux upstream says are going away), and we
   use security_deny_unknown to determine what to do if selinux-policy* doesn't
   have a definition for the the permission or object class we are looking
   up.  */
int
nscd_request_avc_has_perm (int fd, request_type req)
{
  /* Initialize to NULL so we know what to free in case of failure.  */
  security_context_t scon = NULL;
  security_context_t tcon = NULL;
  security_id_t ssid = NULL;
  security_id_t tsid = NULL;
  int rc = -1;
  security_class_t sc_nscd;
  access_vector_t perm;
  int avc_deny_unknown;

  /* Check if SELinux denys or allows unknown object classes
     and permissions.  It is 0 if they are allowed, 1 if they
     are not allowed and -1 on error.  */
  if ((avc_deny_unknown = security_deny_unknown ()) == -1)
    dbg_log (_("Error querying policy for undefined object classes "
	       "or permissions."));

  /* Get the security class for nscd.  If this fails we will likely be
     unable to do anything unless avc_deny_unknown is 0.  */
  sc_nscd = string_to_security_class ("nscd");
  if (sc_nscd == 0 && avc_deny_unknown == 1)
    dbg_log (_("Error getting security class for nscd."));

  /* Convert permission to AVC bits.  */
  perm = string_to_av_perm (sc_nscd, perms[req]);
  if (perm == 0 && avc_deny_unknown == 1)
      dbg_log (_("Error translating permission name "
		 "\"%s\" to access vector bit."), perms[req]);

  /* If the nscd security class was not found or perms were not
     found and AVC does not deny unknown values then allow it.  */
  if ((sc_nscd == 0 || perm == 0) && avc_deny_unknown == 0)
    return 0;

  if (getpeercon (fd, &scon) < 0)
    {
      dbg_log (_("Error getting context of socket peer"));
      goto out;
    }
  if (getcon (&tcon) < 0)
    {
      dbg_log (_("Error getting context of nscd"));
      goto out;
    }
  if (avc_context_to_sid (scon, &ssid) < 0
      || avc_context_to_sid (tcon, &tsid) < 0)
    {
      dbg_log (_("Error getting sid from context"));
      goto out;
    }

  /* The SELinux API for avc_has_perm conflates access denied and error into
     the return code -1, while nscd_request_avs_has_perm has distinct error
     (-1) and denied (1) return codes. We map the avc_has_perm access denied or
     error into an access denied at the nscd interface level (we do accurately
     report error for the getpeercon, getcon, and avc_context_to_sid interfaces
     used above).  */
  rc = avc_has_perm (ssid, tsid, sc_nscd, perm, &aeref, NULL) < 0;

out:
  if (scon)
    freecon (scon);
  if (tcon)
    freecon (tcon);
  if (ssid)
    sidput (ssid);
  if (tsid)
    sidput (tsid);

  return rc;
}
Beispiel #19
0
int rt_hw_mouse_init(void)
{
    rt_uint8_t value;
    rt_uint32_t id;
    struct mouse_pl050_pdata_t *pdat;
    virtual_addr_t virt = MOUSE_ADDRESS;
    int irq = MOUSE_IRQ_NUM;

    id = (((read32(virt + 0xfec) & 0xff) << 24) |
                ((read32(virt + 0xfe8) & 0xff) << 16) |
                ((read32(virt + 0xfe4) & 0xff) <<  8) |
                ((read32(virt + 0xfe0) & 0xff) <<  0));
    
    if(((id >> 12) & 0xff) != 0x41 || (id & 0xfff) != 0x050)
    {
        dbg_log(DBG_ERROR, "read id fail id:0x%08x\n", id);
        return RT_ERROR;
    }

    pdat = rt_malloc(sizeof(struct mouse_pl050_pdata_t));
    if(!pdat)
    {
        dbg_log(DBG_ERROR, "malloc memory\n", id);
        return RT_ERROR;
    }
    rt_memset(pdat, 0, sizeof(struct mouse_pl050_pdata_t));

    pdat->virt = virt;
    pdat->irq = irq;
    pdat->xmax = MOUSE_XMAX;
    pdat->ymax = MOUSE_YMAX;
    pdat->xpos = pdat->xmax / 2;
    pdat->ypos = pdat->ymax / 2;
    pdat->packet[0] = 0;
    pdat->packet[1] = 0;
    pdat->packet[2] = 0;
    pdat->packet[3] = 0;
    pdat->index = 0;
    pdat->obtn = 0;

    write8(pdat->virt + MOUSE_CLKDIV, 0);
    write8(pdat->virt + MOUSE_CR, (1 << 2));
    kmi_write(pdat, 0xff);
    kmi_read(pdat, &value);
    kmi_write(pdat, 0xf3);
    kmi_write(pdat, 200);
    kmi_write(pdat, 0xf3);
    kmi_write(pdat, 100);
    kmi_write(pdat, 0xf3);
    kmi_write(pdat, 80);
    kmi_write(pdat, 0xf2);
    kmi_read(pdat, &value);
    kmi_read(pdat, &value);
    kmi_write(pdat, 0xf3);
    kmi_write(pdat, 100);
    kmi_write(pdat, 0xe8);
    kmi_write(pdat, 0x02);
    kmi_write(pdat, 0xe6);
    kmi_write(pdat, 0xf4);
    kmi_read(pdat, &value);
    kmi_read(pdat, &value);
    kmi_read(pdat, &value);
    kmi_read(pdat, &value);
    write8(pdat->virt + MOUSE_CR, (1 << 2) | (1 << 4));

    rt_hw_interrupt_install(pdat->irq, mouse_pl050_interrupt, (void *)pdat, "mouse");
    rt_hw_interrupt_umask(pdat->irq);

    return RT_EOK;
}
Beispiel #20
0
UINT32 cnetcard_collect(CSET *cnetcard_set, const UINT32 max_cnetcard_num)
{
    int fd;
    struct ifreq *ifreq_tbl;
    struct ifconf ifc;
    struct sockaddr_in *in;
    UINT32 cnetcard_pos;
    UINT32 cnetcard_num;

    fd = csocket_open(AF_INET, SOCK_DGRAM, 0);
    if(0 > fd)
    {
        dbg_log(SEC_0011_CDEVICE, 0)(LOGSTDOUT, "error:cnetcard_collect: failed to create a socket\n");
        return ((UINT32)-1);
    }

    ifreq_tbl = (struct ifreq *)SAFE_MALLOC(sizeof(struct ifreq) * max_cnetcard_num, LOC_CDEVICE_0003);
    if(NULL_PTR == ifreq_tbl)
    {
        dbg_log(SEC_0011_CDEVICE, 0)(LOGSTDOUT, "error:cnetcard_collect: failed to malloc %ld struct ifreq\n", max_cnetcard_num);
        csocket_close_force(fd);
        return ((UINT32)-1);
    }

    ifc.ifc_len = sizeof(struct ifreq) * max_cnetcard_num;
    ifc.ifc_buf = (caddr_t) ifreq_tbl;

    if (0 != ioctl(fd, SIOCGIFCONF, (char *) &ifc))
    {
        dbg_log(SEC_0011_CDEVICE, 0)(LOGSTDOUT, "error:cnetcard_collect: failed to fetch %ld IF CONFIG\n", max_cnetcard_num);
        csocket_close_force(fd);
        SAFE_FREE(ifreq_tbl, LOC_CDEVICE_0004);
        return ((UINT32)-1);
    }

    cnetcard_num = ifc.ifc_len / sizeof(struct ifreq);/*actual device number*/
    //dbg_log(SEC_0011_CDEVICE, 5)(LOGSTDOUT, "cnetcard_num = %ld\n", cnetcard_num);

    for(cnetcard_pos = 0; cnetcard_pos < cnetcard_num; cnetcard_pos ++)
    {
        CNETCARD *cnetcard;
        struct ifreq *ifreq_item;

        ifreq_item = (ifreq_tbl + cnetcard_pos);
        //dbg_log(SEC_0011_CDEVICE, 5)(LOGSTDOUT, "ifreq_tbl %lx, cnetcard_pos %lx => ifreq_item %lx\n", ifreq_tbl, cnetcard_pos, ifreq_item);

        cnetcard = cnetcard_new();
        if(NULL_PTR == cnetcard)
        {
            dbg_log(SEC_0011_CDEVICE, 0)(LOGSTDOUT, "error:cnetcard_collect: failed to new CNETCARD when handle # %ld device\n", cnetcard_pos);
            csocket_close_force(fd);
            SAFE_FREE(ifreq_tbl, LOC_CDEVICE_0005);
            return ((UINT32)-1);
        }

        /*get device name*/
        cstring_format(CNETCARD_NAME(cnetcard), "%s", ifreq_item->ifr_name);

        /*judge whether the net card status is up */
        if(0 != ioctl(fd, SIOCGIFFLAGS, (char *)ifreq_item))
        {
            dbg_log(SEC_0011_CDEVICE, 0)(LOGSTDOUT, "error:cnetcard_collect: failed to fetch # %ld device IF FLAGS\n", cnetcard_pos);
            cnetcard_free(cnetcard);
            continue;
        }
        if(ifreq_item->ifr_flags & IFF_UP)
        {
            CNETCARD_STATE(cnetcard) = CNETCARD_UP_STATE;
        }
        else
        {
            CNETCARD_STATE(cnetcard) = CNETCARD_DOWN_STATE;
        }

        /*get IP of the net card */
        if (0 != ioctl(fd, SIOCGIFADDR, (char *)ifreq_item))
        {
            dbg_log(SEC_0011_CDEVICE, 0)(LOGSTDOUT, "error:cnetcard_collect: failed to fetch # %ld device IF ADDR\n", cnetcard_pos);
            cnetcard_free(cnetcard);
            continue;
        }

        in = (struct sockaddr_in*) (&(ifreq_item->ifr_addr));
        cstring_format(CNETCARD_IPV4STR(cnetcard), "%s", c_inet_ntos(&(in->sin_addr)));
        CNETCARD_IPV4VAL(cnetcard) = c_ipv4_to_word((char *)cstring_get_str(CNETCARD_IPV4STR(cnetcard)));

        /*get HW ADDRESS of the net card */
        if(0 != ioctl(fd, SIOCGIFHWADDR, (char *)ifreq_item))
        {
            dbg_log(SEC_0011_CDEVICE, 0)(LOGSTDOUT, "error:cnetcard_collect: failed to fetch # %ld device IF HW ADDR\n", cnetcard_pos);
            cnetcard_free(cnetcard);
            continue;
        }
        BCOPY(ifreq_item->ifr_hwaddr.sa_data, CNETCARD_MACADDR(cnetcard), 6);
        cstring_format(CNETCARD_MACSTR(cnetcard), "%02x:%02x:%02x:%02x:%02x:%02x",
                            (UINT8) ifreq_item->ifr_hwaddr.sa_data[0],
                            (UINT8) ifreq_item->ifr_hwaddr.sa_data[1],
                            (UINT8) ifreq_item->ifr_hwaddr.sa_data[2],
                            (UINT8) ifreq_item->ifr_hwaddr.sa_data[3],
                            (UINT8) ifreq_item->ifr_hwaddr.sa_data[4],
                            (UINT8) ifreq_item->ifr_hwaddr.sa_data[5]
                        );
        /*add to set*/
        if(EC_FALSE == cset_add(cnetcard_set, (void *)cnetcard, (CSET_DATA_CMP)cnetcard_cmp))
        {
            cnetcard_free(cnetcard);
        }
    }

    csocket_close_force(fd);
    SAFE_FREE(ifreq_tbl, LOC_CDEVICE_0006);

    //dbg_log(SEC_0011_CDEVICE, 3)(LOGSTDOUT, "info:cnetcard_collect: device list:\n");
    //cset_print(LOGSTDOUT, cnetcard_set, (CSET_DATA_PRINT)cnetcard_print);

    return (0);
}
Beispiel #21
0
int ddram_initialize(unsigned int base_address,
			unsigned int ram_address,
			struct ddramc_register *ddramc_config)
{
	unsigned int ba_offset;
	unsigned int cr = 0;

	/* compute BA[] offset according to CR configuration */
	ba_offset = (ddramc_config->cr & AT91C_DDRC2_NC) + 9;
	if (ddramc_decodtype_is_seq(ddramc_config->cr))
		ba_offset += ((ddramc_config->cr & AT91C_DDRC2_NR) >> 2) + 11;

	ba_offset += (ddramc_config->mdr & AT91C_DDRC2_DBW) ? 1 : 2;

	dbg_log(3, " ba_offset = %x ...\n\r", ba_offset);

	/*
	 * Step 1: Program the memory device type into the Memory Device Register
	 */
	write_ddramc(base_address, HDDRSDRC2_MDR, ddramc_config->mdr);

	/* 
	 * Step 2: Program the feature of DDR2-SDRAM device into 
	 * the Timing Register, and into the Configuration Register
	 */
	write_ddramc(base_address, HDDRSDRC2_CR, ddramc_config->cr);

	write_ddramc(base_address, HDDRSDRC2_T0PR, ddramc_config->t0pr);
	write_ddramc(base_address, HDDRSDRC2_T1PR, ddramc_config->t1pr);
	write_ddramc(base_address, HDDRSDRC2_T2PR, ddramc_config->t2pr);

	/*
	 * Step 3: An NOP command is issued to the DDR2-SDRAM
	 */
	write_ddramc(base_address, HDDRSDRC2_MR, AT91C_DDRC2_MODE_NOP_CMD);
	*((unsigned volatile int *)ram_address) = 0;
	/* Now, clocks which drive the DDR2-SDRAM device are enabled */

	/* A minimum pause wait 200 us is provided to precede any signal toggle.
	(6 core cycles per iteration, core is at 396MHz: min 13340 loops) */
	udelay(200);

	/*
	 * Step 4:  An NOP command is issued to the DDR2-SDRAM
	 */
	write_ddramc(base_address, HDDRSDRC2_MR, AT91C_DDRC2_MODE_NOP_CMD);
	*((unsigned volatile int *)ram_address) = 0;
	/* Now, CKE is driven high */
	/* wait 400 ns min */
	udelay(1);

	/*
	 * Step 5: An all banks precharge command is issued to the DDR2-SDRAM.
	 */
	write_ddramc(base_address, HDDRSDRC2_MR, AT91C_DDRC2_MODE_PRCGALL_CMD);
	*((unsigned volatile int *)ram_address) = 0;

	/* wait 2 cycles min (of tCK) = 15 ns min */
	udelay(1);

	/*
	 * Step 6: An Extended Mode Register set(EMRS2) cycle is issued to chose between commercial or high
	 * temperature operations.
	 * Perform a write access to DDR2-SDRAM to acknowledge this command.
	 * The write address must be chosen so that BA[1] is set to 1 and BA[0] is set to 0.
	 */
	write_ddramc(base_address, HDDRSDRC2_MR, AT91C_DDRC2_MODE_EXT_LMR_CMD);
	*((unsigned int *)(ram_address + (0x2 << ba_offset))) = 0;

	/* wait 2 cycles min (of tCK) = 15 ns min */
	udelay(1);

	/*
	 * Step 7: An Extended Mode Register set(EMRS3) cycle is issued
	 * to set the Extended Mode Register to "0".
	 * Perform a write access to DDR2-SDRAM to acknowledge this command.
	 * The write address must be chosen so that BA[1] is set to 1 and BA[0] is set to 1.
	 */
	write_ddramc(base_address, HDDRSDRC2_MR, AT91C_DDRC2_MODE_EXT_LMR_CMD);
	*((unsigned int *)(ram_address + (0x3 << ba_offset))) = 0;

	/* wait 2 cycles min (of tCK) = 15 ns min */
	udelay(1);

	/*
	 * Step 8: An Extened Mode Register set(EMRS1) cycle is issued to enable DLL,
	 * and to program D.I.C(Output Driver Impedance Control)
	 * Perform a write access to DDR2-SDRAM to acknowledge this command.
	 * The write address must be chosen so that BA[1] is set to 0 and BA[0] is set to 1.
	 */
	write_ddramc(base_address, HDDRSDRC2_MR, AT91C_DDRC2_MODE_EXT_LMR_CMD);
	*((unsigned int *)(ram_address + (0x1 << ba_offset))) = 0;

	/* An additional 200 cycles of clock are required for locking DLL */
	udelay(1);

	/*
	 * Step 9: Program DLL field into the Configuration Register to high(Enable DLL reset)
	 */
	cr = read_ddramc(base_address, HDDRSDRC2_CR);
	write_ddramc(base_address, HDDRSDRC2_CR, cr | AT91C_DDRC2_DLL_RESET_ENABLED);

	/*
	 * Step 10: A Mode Register set(MRS) cycle is issied to reset DLL.
	 * Perform a write access to DDR2-SDRAM to acknowledge this command.
	 * The write address must be chosen so that BA[1:0] bits are set to 0.
	 */
	write_ddramc(base_address, HDDRSDRC2_MR, AT91C_DDRC2_MODE_LMR_CMD);
	*((unsigned int *)(ram_address + (0x0 << ba_offset))) = 0;

	/* wait 2 cycles min (of tCK) = 15 ns min */
	udelay(1);

	/*
	 * Step 11: An all banks precharge command is issued to the DDR2-SDRAM.
	 */
	write_ddramc(base_address, HDDRSDRC2_MR, AT91C_DDRC2_MODE_PRCGALL_CMD);
	*(((unsigned volatile int *)ram_address)) = 0;

	/* wait 400 ns min (not needed on certain DDR2 devices) */
	udelay(1);

	/*
	 * Step 12: Two auto-refresh (CBR) cycles are provided.
	 * Program the auto refresh command (CBR) into the Mode Register.
	 */
	write_ddramc(base_address, HDDRSDRC2_MR, AT91C_DDRC2_MODE_RFSH_CMD);
	*(((unsigned volatile int *)ram_address)) = 0;

	/* wait TRFC cycles min (135 ns min) extended to 400 ns */
	udelay(1);

	/* Set 2nd CBR */
	write_ddramc(base_address, HDDRSDRC2_MR, AT91C_DDRC2_MODE_RFSH_CMD);
	*(((unsigned volatile int *)ram_address)) = 0;

	/* wait TRFC cycles min (135 ns min) extended to 400 ns */
	udelay(1);

	/*
	 * Step 13: Program DLL field into the Configuration Register to low(Disable DLL reset).
	 */
	cr = read_ddramc(base_address, HDDRSDRC2_CR);
	write_ddramc(base_address, HDDRSDRC2_CR, cr & (~AT91C_DDRC2_DLL_RESET_ENABLED));

	/*
	 * Step 14: A Mode Register set (MRS) cycle is issued to program
	 * the parameters of the DDR2-SDRAM devices, in particular CAS latency,
	 * burst length and to disable DDL reset.
	 * Perform a write access to DDR2-SDRAM to acknowledge this command.
	 * The write address must be chosen so that BA[1:0] bits are set to 0.
	 */
	write_ddramc(base_address, HDDRSDRC2_MR, AT91C_DDRC2_MODE_LMR_CMD);
	*((unsigned int *)(ram_address + (0x0 << ba_offset))) = 0;

	/* wait 2 cycles min (of tCK) = 15 ns min */
	udelay(1);

	/*
	 * Step 15: Program OCD field into the Configuration Register
	 * to high (OCD calibration default).
	 */
	cr = read_ddramc(base_address, HDDRSDRC2_CR);
	write_ddramc(base_address, HDDRSDRC2_CR, cr | AT91C_DDRC2_OCD_DEFAULT);

	/* wait 2 cycles min (of tCK) = 15 ns min */
	udelay(1);

	/*
	 * Step 16: An Extended Mode Register set (EMRS1) cycle is issued to OCD default value.
	 * Perform a write access to DDR2-SDRAM to acknowledge this command.
	 * The write address must be chosen so that BA[1] is set to 0 and BA[0] is set to 1.
	 */
	write_ddramc(base_address, HDDRSDRC2_MR, AT91C_DDRC2_MODE_EXT_LMR_CMD);
	*((unsigned int *)(ram_address + (0x1 << ba_offset))) = 0;

	/* wait 2 cycles min (of tCK) = 15 ns min */
	udelay(1);

	/*
	 * Step 17: Program OCD field into the Configuration Register
	 * to low (OCD calibration mode exit).
	 */
	cr = read_ddramc(base_address, HDDRSDRC2_CR);
	write_ddramc(base_address, HDDRSDRC2_CR, cr & (~AT91C_DDRC2_OCD_DEFAULT));

	/* wait 2 cycles min (of tCK) = 15 ns min */
	udelay(1);

	/*
	 * Step 18: An Extended Mode Register set (EMRS1) cycle is issued to enable OCD exit.
	 * Perform a write access to DDR2-SDRAM to acknowledge this command.
	 * The write address must be chosen so that BA[1] is set to 0 and BA[0] is set to 1.
	 */
	write_ddramc(base_address, HDDRSDRC2_MR, AT91C_DDRC2_MODE_EXT_LMR_CMD);
	*((unsigned int *)(ram_address + (0x1 << ba_offset))) = 0;

	/* wait 2 cycles min (of tCK) = 15 ns min */
	udelay(1);

	/*
	 * Step 19: A Nornal mode command is provided.
	 */
	write_ddramc(base_address, HDDRSDRC2_MR, AT91C_DDRC2_MODE_NORMAL_CMD);
	*(((unsigned volatile int *)ram_address)) = 0;

	/*
	 * Step 20: Perform a write access to any DDR2-SDRAM address
	 */
	*(((unsigned volatile int *)ram_address)) = 0;

	/*
	 * Step 21: Write the refresh rate into the count field in the Refresh Timer register.
	 */
	write_ddramc(base_address, HDDRSDRC2_RTR, ddramc_config->rtr);

	/*
	 * Now we are ready to work on the DDRSDR
	 *  wait for end of calibration
	 */
	udelay(10);

	return 0;
}
Beispiel #22
0
EC_BOOL cextsrv_process0(CEXTSRV *cextsrv, CEXTCLNT *cextclnt)
{
    UINT8  *in_buff;
    UINT32  in_buff_len;
    UINT8  *out_buff;
    UINT32  out_buff_len;

    FUNC_ADDR_NODE *func_addr_node;

    TASK_FUNC task_req_func;
    TASK_FUNC task_rsp_func;

    UINT32 para_idx;

    if(EC_FALSE == cextclnt_recv(cextclnt, &in_buff, &in_buff_len))
    {
        dbg_log(SEC_0069_CEXTSRV, 0)(LOGSTDOUT, "error:cextsrv_process: recv data from client %s on sockfd %d failed\n",
                            CEXTCLNT_IPADDR_STR(cextclnt), CEXTCLNT_SOCKFD(cextclnt));
        return (EC_FALSE);
    }

    task_func_init(&task_req_func);
    task_func_init(&task_rsp_func);

    if(EC_FALSE == cextsrv_req_decode(cextsrv, in_buff, in_buff_len, &task_req_func))
    {
        dbg_log(SEC_0069_CEXTSRV, 0)(LOGSTDOUT, "error:cextsrv_process: decode req from client %s on sockfd %d failed\n",
                            CEXTCLNT_IPADDR_STR(cextclnt), CEXTCLNT_SOCKFD(cextclnt));
        SAFE_FREE(in_buff, LOC_CEXTSRV_0014);
        cextsrv_req_clean(&task_req_func);
        return (EC_FALSE);
    }
    SAFE_FREE(in_buff, LOC_CEXTSRV_0015);

    if(0 != dbg_fetch_func_addr_node_by_index(task_req_func.func_id, &func_addr_node))
    {
        dbg_log(SEC_0069_CEXTSRV, 0)(LOGSTDOUT, "error:cextsrv_process: failed to fetch func addr node by func id %lx\n", task_req_func.func_id);
        cextsrv_req_clean(&task_req_func);
        return (EC_FALSE);
    }

    task_caller(&task_req_func, func_addr_node);

    /*clone task_req_func to task_rsp_func and clean up task_req_func*/
    task_rsp_func.func_id       = task_req_func.func_id;
    task_rsp_func.func_para_num = task_req_func.func_para_num;
    task_rsp_func.func_ret_val  = task_req_func.func_ret_val;

    task_req_func.func_ret_val = 0;/*clean it*/

    for( para_idx = 0; para_idx < task_req_func.func_para_num; para_idx ++ )
    {
        FUNC_PARA  *task_rsp_func_para;
        FUNC_PARA  *task_req_func_para;

        task_req_func_para = &(task_req_func.func_para[ para_idx ]);
        task_rsp_func_para = &(task_rsp_func.func_para[ para_idx ]);

        task_rsp_func_para->para_dir = task_req_func_para->para_dir;
        task_rsp_func_para->para_val = task_req_func_para->para_val;
        task_req_func_para->para_val = 0;/*clean it*/
    }

    if(EC_FALSE == cextsrv_rsp_encode_size(cextsrv, &task_rsp_func, &out_buff_len))
    {
        dbg_log(SEC_0069_CEXTSRV, 0)(LOGSTDOUT, "error:cextsrv_process: encode size rsp to client %s on sockfd %d failed\n",
                            CEXTCLNT_IPADDR_STR(cextclnt), CEXTCLNT_SOCKFD(cextclnt));
        cextsrv_req_clean(&task_req_func);
        cextsrv_rsp_clean(&task_rsp_func);
        return (EC_FALSE);
    }

    out_buff = (UINT8 *)SAFE_MALLOC(out_buff_len, LOC_CEXTSRV_0016);
    if(NULL_PTR == out_buff)
    {
        dbg_log(SEC_0069_CEXTSRV, 0)(LOGSTDOUT, "error:cextsrv_process: alloc %ld bytes for out buff to client %s on sockfd %d failed\n",
                            out_buff_len, CEXTCLNT_IPADDR_STR(cextclnt), CEXTCLNT_SOCKFD(cextclnt));
        cextsrv_req_clean(&task_req_func);
        cextsrv_rsp_clean(&task_rsp_func);
        return (EC_FALSE);
    }

    if(EC_FALSE == cextsrv_rsp_encode(cextsrv, out_buff, out_buff_len, &out_buff_len, &task_rsp_func))
    {
        dbg_log(SEC_0069_CEXTSRV, 0)(LOGSTDOUT, "error:cextsrv_process: encode rsp to client %s on sockfd %d failed\n",
                            CEXTCLNT_IPADDR_STR(cextclnt), CEXTCLNT_SOCKFD(cextclnt));
        cextsrv_req_clean(&task_req_func);
        cextsrv_rsp_clean(&task_rsp_func);
        SAFE_FREE(out_buff, LOC_CEXTSRV_0017);
        return (EC_FALSE);
    }

    if(EC_FALSE == cextclnt_send(cextclnt, out_buff, out_buff_len))
    {
        dbg_log(SEC_0069_CEXTSRV, 0)(LOGSTDOUT, "error:cextsrv_process: send rsp to client %s on sockfd %d failed\n",
                            CEXTCLNT_IPADDR_STR(cextclnt), CEXTCLNT_SOCKFD(cextclnt));
        cextsrv_req_clean(&task_req_func);
        cextsrv_rsp_clean(&task_rsp_func);
        SAFE_FREE(out_buff, LOC_CEXTSRV_0018);
        return (EC_FALSE);
    }
    cextsrv_req_clean(&task_req_func);
    cextsrv_rsp_clean(&task_rsp_func);
    SAFE_FREE(out_buff, LOC_CEXTSRV_0019);

    return (EC_TRUE);
}
Beispiel #23
0
EC_BOOL lic_date_make(LIC_DATE *lic_date, const char *start_date_str, const char *end_date_str)
{
    char  start_date_str_tmp[32];
    char  end_date_str_tmp[32];
    char  *fields[8];
    UINT32 field_num;

    UINT32 year;
    UINT32 month;
    UINT32 day;

    snprintf(start_date_str_tmp, sizeof(start_date_str_tmp)/sizeof(start_date_str_tmp[0]), "%s", start_date_str);
    snprintf(end_date_str_tmp, sizeof(end_date_str_tmp)/sizeof(end_date_str_tmp[0]), "%s", end_date_str);

    /*start date*/
    field_num = c_str_split(start_date_str_tmp, "-/", fields, sizeof(fields)/sizeof(fields[ 0 ]));
    if(3 != field_num)
    {
        dbg_log(SEC_0060_LICENSE, 0)(LOGSTDOUT, "error:lic_date_make: invalid start date: %s\n", start_date_str);
        return (EC_FALSE);
    }

    year  = c_str_to_word(fields[ 0 ]);
    month = c_str_to_word(fields[ 1 ]);
    day   = c_str_to_word(fields[ 2 ]);

    if(2012 > year || year > 4095 || 1 > month || month > 12 || 1 > day || day > 31)
    {
        dbg_log(SEC_0060_LICENSE, 0)(LOGSTDOUT, "error:lic_date_make: illegal start date: %s\n", start_date_str);
        return (EC_FALSE);
    }

    LIC_DATE_START_YEAR(lic_date)  = year;
    LIC_DATE_START_MONTH(lic_date) = month;
    LIC_DATE_START_DAY(lic_date)   = day;

    /*end date*/
    field_num = c_str_split(end_date_str_tmp, "-/", fields, sizeof(fields)/sizeof(fields[ 0 ]));
    if(3 != field_num)
    {
        dbg_log(SEC_0060_LICENSE, 0)(LOGSTDOUT, "error:lic_date_make: invalid end date: %s\n", end_date_str);
        return (EC_FALSE);
    }

    year  = c_str_to_word(fields[ 0 ]);
    month = c_str_to_word(fields[ 1 ]);
    day   = c_str_to_word(fields[ 2 ]);

    if(2012 > year || year > 4095 || 1 > month || month > 12 || 1 > day || day > 31)
    {
        dbg_log(SEC_0060_LICENSE, 0)(LOGSTDOUT, "error:lic_date_make: illegal end date: %s\n", end_date_str);
        return (EC_FALSE);
    }

    LIC_DATE_END_YEAR(lic_date)  = year;
    LIC_DATE_END_MONTH(lic_date) = month;
    LIC_DATE_END_DAY(lic_date)   = day;

    if(EC_FALSE == lic_date_check(lic_date))
    {
        dbg_log(SEC_0060_LICENSE, 0)(LOGSTDOUT, "error:lic_date_make: illegal issued expiration: from %s to %s\n", start_date_str, end_date_str);
        return (EC_FALSE);
    }
    return (EC_TRUE);
}
Beispiel #24
0
EC_BOOL cextsrv_process(CEXTSRV *cextsrv, CEXTCLNT *cextclnt)
{
    FUNC_ADDR_NODE *func_addr_node;

    TASK_FUNC task_req_func;
    TASK_FUNC task_rsp_func;

    UINT32 para_idx;

    task_func_init(&task_req_func);
    if(EC_FALSE == csocket_task_req_func_recv(CEXTCLNT_SOCKFD(cextclnt), &task_req_func))
    {
        dbg_log(SEC_0069_CEXTSRV, 0)(LOGSTDOUT, "error:cextsrv_process: recv req from client %s on sockfd %d failed\n",
                            CEXTCLNT_IPADDR_STR(cextclnt), CEXTCLNT_SOCKFD(cextclnt));
        return (EC_FALSE);
    }

    if(0 != dbg_fetch_func_addr_node_by_index(task_req_func.func_id, &func_addr_node))
    {
        dbg_log(SEC_0069_CEXTSRV, 0)(LOGSTDOUT, "error:cextsrv_process: failed to fetch func addr node by func id %lx\n", task_req_func.func_id);
        cextsrv_req_clean(&task_req_func);
        return (EC_FALSE);
    }

    task_func_init(&task_rsp_func);

    task_caller(&task_req_func, func_addr_node);

    /*clone task_req_func to task_rsp_func and clean up task_req_func*/
    task_rsp_func.func_id       = task_req_func.func_id;
    task_rsp_func.func_para_num = task_req_func.func_para_num;
    task_rsp_func.func_ret_val  = task_req_func.func_ret_val;

    task_req_func.func_ret_val = 0;/*clean it*/

    for( para_idx = 0; para_idx < task_req_func.func_para_num; para_idx ++ )
    {
        FUNC_PARA  *task_rsp_func_para;
        FUNC_PARA  *task_req_func_para;

        task_req_func_para = &(task_req_func.func_para[ para_idx ]);
        task_rsp_func_para = &(task_rsp_func.func_para[ para_idx ]);

        task_rsp_func_para->para_dir = task_req_func_para->para_dir;
        task_rsp_func_para->para_val = task_req_func_para->para_val;
        task_req_func_para->para_val = 0;/*clean it*/
    }

    if(EC_FALSE == csocket_task_rsp_func_send(CEXTCLNT_SOCKFD(cextclnt), &task_rsp_func))
    {
        dbg_log(SEC_0069_CEXTSRV, 0)(LOGSTDOUT, "error:cextsrv_process: send rsp to client %s on sockfd %d failed\n",
                            CEXTCLNT_IPADDR_STR(cextclnt), CEXTCLNT_SOCKFD(cextclnt));

        cextsrv_req_clean(&task_req_func);
        cextsrv_rsp_clean(&task_rsp_func);
        return (EC_FALSE);
    }

    cextsrv_req_clean(&task_req_func);
    cextsrv_rsp_clean(&task_rsp_func);

    return (EC_TRUE);
}
Beispiel #25
0
EC_BOOL lic_check()
{
    LIC_CFG lic_cfg;

    struct tm *cur_time;

    UINT32 year;
    UINT32 month;
    UINT32 day;

    LIC_DATE   *lic_date;

    int fd;

    lic_cfg_init(&lic_cfg);

    fd = c_file_open(g_lic_file_name, O_RDONLY, 0666);
    if(-1 == fd)
    {
        dbg_log(SEC_0060_LICENSE, 0)(LOGSTDOUT, "error:lic_check: open license file %s failed\n", g_lic_file_name);
        lic_cfg_clean(&lic_cfg);
        return (EC_FALSE);
    }

    if(EC_FALSE == lic_cfg_load(fd, &lic_cfg))
    {
        dbg_log(SEC_0060_LICENSE, 0)(LOGSTDOUT, "error:lic_check: load license file %s failed\n", g_lic_file_name);
        close(fd);
        lic_cfg_clean(&lic_cfg);
        return (EC_FALSE);
    }

    /*check signature*/
    if(EC_FALSE == lic_cfg_verify(&lic_cfg))
    {
        dbg_log(SEC_0060_LICENSE, 0)(LOGSTDOUT, "error:lic_check: verify signature failed\n");
        close(fd);
        lic_cfg_clean(&lic_cfg);
        return (EC_FALSE);
    }

    close(fd);

    /*check mac addr*/
    if(EC_FALSE == lic_mac_verify(LIC_CFG_MAC(&lic_cfg)))
    {
        dbg_log(SEC_0060_LICENSE, 0)(LOGSTDOUT, "error:lic_check: verify mac addr failed\n");
        lic_cfg_clean(&lic_cfg);
        return (EC_FALSE);
    }

    /*check version*/
    if(EC_FALSE == lic_version_verify((char *)LIC_CFG_VERSION(&lic_cfg)))
    {
        dbg_log(SEC_0060_LICENSE, 0)(LOGSTDOUT, "error:lic_check: verify version failed\n");
        lic_cfg_clean(&lic_cfg);
        return (EC_FALSE);
    }

    /*check vendor*/
    if(EC_FALSE == lic_vendor_verify((char *)LIC_CFG_VENDOR_NAME(&lic_cfg), (char *)LIC_CFG_VENDOR_EMAIL(&lic_cfg)))
    {
        dbg_log(SEC_0060_LICENSE, 0)(LOGSTDOUT, "error:lic_check: verify vendor failed\n");
        lic_cfg_clean(&lic_cfg);
        return (EC_FALSE);
    }

    /*check expiration*/
    cur_time = c_localtime_r(NULL_PTR);

    year  = cur_time->tm_year + 1900;
    month = cur_time->tm_mon + 1;
    day   = cur_time->tm_mday;

    lic_date = LIC_CFG_DATE(&lic_cfg);

    if(LIC_DATE_END_YEAR(lic_date) > year)
    {
        lic_cfg_clean(&lic_cfg);
        return (EC_TRUE);
    }

    if(LIC_DATE_END_YEAR(lic_date) < year)
    {
        dbg_log(SEC_0060_LICENSE, 1)(LOGSTDOUT, "warn:lic_check: license is expired\n");
        lic_cfg_clean(&lic_cfg);
        return (EC_FALSE);
    }

    /*now year == end year*/
    if(LIC_DATE_END_MONTH(lic_date) > month)
    {
        lic_cfg_clean(&lic_cfg);
        return (EC_TRUE);
    }

    if(LIC_DATE_END_MONTH(lic_date) < month)
    {
        dbg_log(SEC_0060_LICENSE, 1)(LOGSTDOUT, "warn:lic_check: license is expired\n");
        lic_cfg_clean(&lic_cfg);
        return (EC_FALSE);
    }

    /*now month == end month*/
    if(LIC_DATE_END_DAY(lic_date) < day)
    {
        dbg_log(SEC_0060_LICENSE, 1)(LOGSTDOUT, "warn:lic_check: license is expired\n");
        lic_cfg_clean(&lic_cfg);
        return (EC_FALSE);
    }

    if(LIC_DATE_END_DAY(lic_date) < day + 15)
    {
        dbg_log(SEC_0060_LICENSE, 1)(LOGSTDOUT, "warn:lic_check: license will be expired in %ld days. please contact %s/%s to renew.\n",
                            LIC_DATE_END_DAY(lic_date) - day,
                            g_lic_vendor_name, g_lic_vendor_email);
    }

    lic_cfg_clean(&lic_cfg);
    return (EC_TRUE);
}
Beispiel #26
0
void test_case_cscore_003(const UINT32 cscore_md_id)
{
    void *csdoc_words_1;
    void *csdoc_words_2;
    void *csdoc_words_list;
    void *csword_docs;
    void *csdoc;
    void *csword;

    csdoc_words_list = clist_new(MM_CSDOC_WORDS, 0);
    ASSERT(NULL_PTR != csdoc_words_list);

    /*make csdoc_words_1 ready*/
    csdoc_words_1 = cscore_csdoc_words_new(cscore_md_id);
    ASSERT(NULL_PTR != csdoc_words_1);

    /*{doc={1001,1002,1003}, words={pepople, republic, of, china}}*/
    /*set csdoc of csdoc_words_1*/
    csdoc = (void *)cscore_csdoc_words_get_doc(cscore_md_id, csdoc_words_1);
    cscore_csdoc_set_doc_id  (cscore_md_id, csdoc, 1001);
    cscore_csdoc_set_doc_type(cscore_md_id, csdoc, 1002);
    cscore_csdoc_set_doc_code(cscore_md_id, csdoc, 1003);

    /*add cswords to csdoc_words_1*/
    csword = cscore_csword_make_by_str_content(cscore_md_id, (UINT8 *)"pepole");
    ASSERT(NULL_PTR != csword);
    cscore_csdoc_words_push_word(cscore_md_id, csdoc_words_1, csword);

    csword = cscore_csword_make_by_str_content(cscore_md_id, (UINT8 *)"republic");
    ASSERT(NULL_PTR != csword);
    cscore_csdoc_words_push_word(cscore_md_id, csdoc_words_1, csword);

    csword = cscore_csword_make_by_str_content(cscore_md_id, (UINT8 *)"of");
    ASSERT(NULL_PTR != csword);
    cscore_csdoc_words_push_word(cscore_md_id, csdoc_words_1, csword);    

    csword = cscore_csword_make_by_str_content(cscore_md_id, (UINT8 *)"china");
    ASSERT(NULL_PTR != csword);
    cscore_csdoc_words_push_word(cscore_md_id, csdoc_words_1, csword);   

    clist_push_back(csdoc_words_list, (void *)csdoc_words_1);

    /*make csdoc_words_2 ready*/
    csdoc_words_2 = cscore_csdoc_words_new(cscore_md_id);
    ASSERT(NULL_PTR != csdoc_words_2);

    /*{doc={1101,1002,1003}, words={pepople, working, hard}}*/
    /*set csdoc of csdoc_words_2*/
    csdoc = (void *)cscore_csdoc_words_get_doc(cscore_md_id, csdoc_words_2);
    cscore_csdoc_set_doc_id  (cscore_md_id, csdoc, 1101);
    cscore_csdoc_set_doc_type(cscore_md_id, csdoc, 1002);
    cscore_csdoc_set_doc_code(cscore_md_id, csdoc, 1003);

    /*add cswords to csdoc_words_2*/
    csword = cscore_csword_make_by_str_content(cscore_md_id, (UINT8 *)"pepole");
    ASSERT(NULL_PTR != csword);
    cscore_csdoc_words_push_word(cscore_md_id, csdoc_words_2, csword);

    csword = cscore_csword_make_by_str_content(cscore_md_id, (UINT8 *)"working");
    ASSERT(NULL_PTR != csword);
    cscore_csdoc_words_push_word(cscore_md_id, csdoc_words_2, csword);

    csword = cscore_csword_make_by_str_content(cscore_md_id, (UINT8 *)"hard");
    ASSERT(NULL_PTR != csword);
    cscore_csdoc_words_push_word(cscore_md_id, csdoc_words_2, csword);    

    clist_push_back(csdoc_words_list, (void *)csdoc_words_2);

    /*import csdoc_words_1 into bigtable*/
    dbg_log(SEC_0137_DEMO, 9)(LOGSTDOUT, "[DEBUG] test_case_cscore_003: import beg\n");
    ASSERT(EC_TRUE == cscore_csdoc_words_list_import(cscore_md_id, csdoc_words_list));
    clist_free_with_modi(csdoc_words_list, cscore_md_id);
    dbg_log(SEC_0137_DEMO, 9)(LOGSTDOUT, "[DEBUG] test_case_cscore_003: import end\n");

    /*make csword_docs ready*/
    csword_docs = cscore_csword_docs_new(cscore_md_id);
    ASSERT(NULL_PTR != csword_docs);

    /*add cswords to csword_docs*/
    csword = cscore_csword_make_by_str_content(cscore_md_id, (UINT8 *)"pepole");
    ASSERT(NULL_PTR != csword);
    cscore_csword_docs_set_word(cscore_md_id, csword_docs, csword);
    cscore_csword_free(cscore_md_id, csword);

    dbg_log(SEC_0137_DEMO, 9)(LOGSTDOUT, "[DEBUG] test_case_cscore_003: export beg\n");
    ASSERT(EC_TRUE == cscore_csword_docs_export(cscore_md_id, CBGT_SELECT_FROM_ALL_TABLE, csword_docs));
    dbg_log(SEC_0137_DEMO, 9)(LOGSTDOUT, "[DEBUG] test_case_cscore_003: export end\n");
    dbg_log(SEC_0137_DEMO, 9)(LOGSTDOUT, "[DEBUG] test_case_cscore_003: export result:\n");
    cscore_csword_docs_print(LOGSTDOUT, csword_docs);
    cscore_csword_docs_free(cscore_md_id, csword_docs);

    return;
}
Beispiel #27
0
/* Add a new entry to the cache.  The return value is zero if the function
   call was successful.

   This function must be called with the read-lock held.

   We modify the table but we nevertheless only acquire a read-lock.
   This is ok since we use operations which would be safe even without
   locking, given that the `prune_cache' function never runs.  Using
   the readlock reduces the chance of conflicts.  */
int
cache_add (int type, const void *key, size_t len, struct datahead *packet,
	   bool first, struct database_dyn *table,
	   uid_t owner, bool prune_wakeup)
{
  if (__builtin_expect (debug_level >= 2, 0))
    {
      const char *str;
      char buf[INET6_ADDRSTRLEN + 1];
      if (type == GETHOSTBYADDR || type == GETHOSTBYADDRv6)
	str = inet_ntop (type == GETHOSTBYADDR ? AF_INET : AF_INET6,
			 key, buf, sizeof (buf));
      else
	str = key;

      dbg_log (_("add new entry \"%s\" of type %s for %s to cache%s"),
	       str, serv2str[type], dbnames[table - dbs],
	       first ? _(" (first)") : "");
    }

  unsigned long int hash = __nis_hash (key, len) % table->head->module;
  struct hashentry *newp;

  newp = mempool_alloc (table, sizeof (struct hashentry), 0);
  /* If we cannot allocate memory, just do not do anything.  */
  if (newp == NULL)
    {
      /* If necessary mark the entry as unusable so that lookups will
	 not use it.  */
      if (first)
	packet->usable = false;

      return -1;
    }

  newp->type = type;
  newp->first = first;
  newp->len = len;
  newp->key = (char *) key - table->data;
  assert (newp->key + newp->len <= table->head->first_free);
  newp->owner = owner;
  newp->packet = (char *) packet - table->data;
  assert ((newp->packet & BLOCK_ALIGN_M1) == 0);

  /* Put the new entry in the first position.  */
  do
    newp->next = table->head->array[hash];
  while (atomic_compare_and_exchange_bool_acq (&table->head->array[hash],
					       (ref_t) ((char *) newp
							- table->data),
					       (ref_t) newp->next));

  /* Update the statistics.  */
  if (packet->notfound)
    ++table->head->negmiss;
  else if (first)
    ++table->head->posmiss;

  /* We depend on this value being correct and at least as high as the
     real number of entries.  */
  atomic_increment (&table->head->nentries);

  /* It does not matter that we are not loading the just increment
     value, this is just for statistics.  */
  unsigned long int nentries = table->head->nentries;
  if (nentries > table->head->maxnentries)
    table->head->maxnentries = nentries;

  if (table->persistent)
    // XXX async OK?
    msync ((void *) table->head,
	   (char *) &table->head->array[hash] - (char *) table->head
	   + sizeof (ref_t), MS_ASYNC);

  /* We do not have to worry about the pruning thread if we are
     re-adding the data since this is done by the pruning thread.  We
     also do not have to do anything in case this is not the first
     time the data is entered since different data heads all have the
     same timeout.  */
  if (first && prune_wakeup)
    {
      /* Perhaps the prune thread for the table is not running in a long
	 time.  Wake it if necessary.  */
      pthread_mutex_lock (&table->prune_lock);
      time_t next_wakeup = table->wakeup_time;
      bool do_wakeup = false;
      if (next_wakeup > packet->timeout + CACHE_PRUNE_INTERVAL)
	{
	  table->wakeup_time = packet->timeout;
	  do_wakeup = true;
	}
      pthread_mutex_unlock (&table->prune_lock);
      if (do_wakeup)
	pthread_cond_signal (&table->prune_cond);
    }

  return 0;
}
Beispiel #28
0
void test_case_cscore_005(const UINT32 cscore_md_id)
{
    void *csdoc_words_1;
    void *csdoc_words_2;
    void *csdoc_words_3;
    void *csdoc_words_list;
    void *csword_list;
    void *csdoc_list;
    void *csdoc;

    csdoc_words_list = clist_new(MM_CSDOC_WORDS, 0);
    ASSERT(NULL_PTR != csdoc_words_list);

    /*make csdoc_words_1 = {doc={1001,1002,1003}, words={pepople, republic, of, china}}*/
    csdoc_words_1 = cscore_csdoc_words_new(cscore_md_id);
    ASSERT(NULL_PTR != csdoc_words_1);
    csdoc = (void *)cscore_csdoc_words_get_doc(cscore_md_id, csdoc_words_1);
    __test_case_cscore_csdoc_set_doc(cscore_md_id, csdoc, 1001, 1002, 1003);
    __test_case_cscore_csdoc_words_add_words(cscore_md_id, csdoc_words_1, 4, (UINT8 *)"pepole", (UINT8 *)"republic", (UINT8 *)"of", (UINT8 *)"china");
    clist_push_back(csdoc_words_list, (void *)csdoc_words_1);

    /*make csdoc_words_2 = {doc={1101,1002,1003}, words={pepople, working, hard}}*/
    csdoc_words_2 = cscore_csdoc_words_new(cscore_md_id);
    ASSERT(NULL_PTR != csdoc_words_2);    
    csdoc = (void *)cscore_csdoc_words_get_doc(cscore_md_id, csdoc_words_2);
    __test_case_cscore_csdoc_set_doc(cscore_md_id, csdoc, 1101, 1002, 1003);
    __test_case_cscore_csdoc_words_add_words(cscore_md_id, csdoc_words_2, 3, (UINT8 *)"pepole", (UINT8 *)"working", (UINT8 *)"hard");
    clist_push_back(csdoc_words_list, (void *)csdoc_words_2);

    /*make csdoc_words_3 = {doc={1101,1002,1003}, words={pepople, living, happ}}*/
    csdoc_words_3 = cscore_csdoc_words_new(cscore_md_id);
    ASSERT(NULL_PTR != csdoc_words_3);    
    csdoc = (void *)cscore_csdoc_words_get_doc(cscore_md_id, csdoc_words_3);
    __test_case_cscore_csdoc_set_doc(cscore_md_id, csdoc, 1011, 1002, 1003);
    __test_case_cscore_csdoc_words_add_words(cscore_md_id, csdoc_words_3, 3, (UINT8 *)"pepole", (UINT8 *)"living", (UINT8 *)"happy");
    clist_push_back(csdoc_words_list, (void *)csdoc_words_3);

    /*import csdoc_words_1 into bigtable*/
    dbg_log(SEC_0137_DEMO, 9)(LOGSTDOUT, "[DEBUG] test_case_cscore_005: import beg\n");
    ASSERT(EC_TRUE == cscore_csdoc_words_list_import(cscore_md_id, csdoc_words_list));
    clist_free_with_modi(csdoc_words_list, cscore_md_id);
    dbg_log(SEC_0137_DEMO, 9)(LOGSTDOUT, "[DEBUG] test_case_cscore_005: import end\n");

    /*make csword_list ready*/
    csword_list = clist_new(MM_CSWORD, 0);
    ASSERT(NULL_PTR != csword_list);
    __test_case_cscore_csword_list_add_word(cscore_md_id, csword_list, (UINT8 *)"pepole");
    __test_case_cscore_csword_list_add_word(cscore_md_id, csword_list, (UINT8 *)"china");

    dbg_log(SEC_0137_DEMO, 9)(LOGSTDOUT, "[DEBUG] test_case_cscore_005: csword_list is:\n");
    clist_print(LOGSTDOUT, csword_list, cscore_csword_print);

    /*make csdoc_list ready*/
    csdoc_list = clist_new(MM_CSDOC, 0);
    ASSERT(NULL_PTR != csdoc_list);    

    dbg_log(SEC_0137_DEMO, 9)(LOGSTDOUT, "[DEBUG] test_case_cscore_005: export beg\n");
    ASSERT(EC_TRUE == cscore_csword_docs_list_export_docs(cscore_md_id, CBGT_SELECT_FROM_ALL_TABLE, csword_list, csdoc_list));
    dbg_log(SEC_0137_DEMO, 9)(LOGSTDOUT, "[DEBUG] test_case_cscore_005: export end\n");
    dbg_log(SEC_0137_DEMO, 9)(LOGSTDOUT, "[DEBUG] test_case_cscore_005: export result:\n");
    clist_print(LOGSTDOUT, csdoc_list, (CLIST_DATA_DATA_PRINT)cscore_csdoc_print);

    clist_clean_with_modi(csword_list, cscore_md_id, (CLIST_DATA_MODI_HANDLER)cscore_csword_free);
    clist_clean_with_modi(csdoc_list, cscore_md_id, (CLIST_DATA_MODI_HANDLER)cscore_csdoc_free);

    clist_free(csword_list, 0);
    clist_free(csdoc_list, 0);    

    return;
}
Beispiel #29
0
EC_BOOL ccurl_get(const UINT32 ccurl_md_id, const CSTRING *url_str, const CSTRING *proxy_ip_port, CSTRING *curl_reply_body)
{
    CURL    *curl;
    CURLcode res;
    long     status;

#if ( SWITCH_ON == CCURL_DEBUG_SWITCH )
    if ( CCURL_MD_ID_CHECK_INVALID(ccurl_md_id) )
    {
        sys_log(LOGSTDOUT,
                "error:ccurl_get: ccurl module #0x%lx not started.\n",
                ccurl_md_id);
        ccurl_print_module_status(ccurl_md_id, LOGSTDOUT);
        dbg_exit(MD_CCURL, ccurl_md_id);
    }
#endif/*CCURL_DEBUG_SWITCH*/    

    curl = curl_easy_init();
    if(NULL_PTR == curl)
    {
        dbg_log(SEC_0030_CCURL, 0)(LOGSTDOUT, "error:ccurl_get: curl_easy_init failed\n");
        return (EC_FALSE);
    }    

    curl_easy_setopt(curl, CURLOPT_URL, (char *)cstring_get_str(url_str));
    /* example.com is redirected, so we tell libcurl to follow redirection */ 
    curl_easy_setopt(curl, CURLOPT_FOLLOWLOCATION, 1L);
    curl_easy_setopt(curl, CURLOPT_PROXY, (char *)cstring_get_str(proxy_ip_port));
    curl_easy_setopt(curl, CURLOPT_WRITEFUNCTION, __ccurl_write_data);
    curl_easy_setopt(curl, CURLOPT_WRITEDATA, curl_reply_body);
    curl_easy_setopt(curl, CURLOPT_TCP_NODELAY, 1L);
    curl_easy_setopt(curl, CURLOPT_TIMEOUT, 120L); /*total download time in seconds*/
    curl_easy_setopt(curl, CURLOPT_CONNECTTIMEOUT, 30L); /*total connect time in seconds*/

    /* Perform the request, res will get the return code */ 
    res = curl_easy_perform(curl);
    
    /* Check for errors */ 
    if(CURLE_OK != res)
    {
        dbg_log(SEC_0030_CCURL, 0)(LOGSTDOUT, "error:ccurl_get:curl_easy_perform() failed: %s where url '%s'\n", 
                           curl_easy_strerror(res), (char *)cstring_get_str(url_str));
        curl_easy_cleanup(curl);
        return (EC_FALSE);
    }

    res = curl_easy_getinfo(curl, CURLINFO_RESPONSE_CODE , &status); 
    if(CURLE_OK != res)
    {
        dbg_log(SEC_0030_CCURL, 0)(LOGSTDOUT, "error:ccurl_get:curl_easy_getinfo() failed: %s where url '%s'\n", 
                            curl_easy_strerror(res), (char *)cstring_get_str(url_str));
        curl_easy_cleanup(curl);
        return (EC_FALSE);
    }
    
    if(CCURL_HTTP_OK != status)
    {
        dbg_log(SEC_0030_CCURL, 0)(LOGSTDOUT, "error:ccurl_get:reply status %d not OK! where url '%s'\n", 
                           status, (char *)cstring_get_str(url_str));
        curl_easy_cleanup(curl);
        return (EC_FALSE);
    }
    
    /* always cleanup */ 
    curl_easy_cleanup(curl);    

    dbg_log(SEC_0030_CCURL, 9)(LOGSTDOUT, "[DEBUG] ccurl_get: GET '%s' from '%s' done\n", 
                        (char *)cstring_get_str(url_str), 
                        (char *)cstring_get_str(proxy_ip_port));
    
    return (EC_TRUE);
}
Beispiel #30
0
int main_cscore0(int argc, char **argv)
{
    UINT32 this_tcid;
    UINT32 this_comm;
    UINT32 this_rank;

    task_brd_default_init(argc, argv);
    if(EC_FALSE == task_brd_default_check_validity())
    {
        dbg_log(SEC_0137_DEMO, 0)(LOGSTDOUT, "error:main_cscore: validity checking failed\n");
        task_brd_default_abort();
        return (-1);
    }

    this_tcid = task_brd_default_get_tcid();
    this_comm = task_brd_default_get_comm();
    this_rank = task_brd_default_get_rank();

    if (EC_TRUE == task_brd_check_is_dbg_tcid(this_tcid) && CMPI_DBG_RANK == this_rank)
    {
        do_cmd_default();
    }
    else if (EC_TRUE == task_brd_check_is_monitor_tcid(this_tcid) && CMPI_MON_RANK == this_rank)
    {
        void * mod_mgr_def;

        mod_mgr_def = mod_mgr_new(CMPI_ERROR_MODI, LOAD_BALANCING_LOOP);
        mod_mgr_default_init(mod_mgr_def, CMPI_ANY_TCID, CMPI_ANY_RANK);

        //mod_mgr_excl(this_tcid, CMPI_ANY_COMM, this_rank, CMPI_ANY_MODI, mod_mgr_def);

        dbg_log(SEC_0137_DEMO, 5)(LOGSTDOUT, "======================================================================\n");
        dbg_log(SEC_0137_DEMO, 5)(LOGSTDOUT, "                       mod_mgr_default_init finished                  \n");
        dbg_log(SEC_0137_DEMO, 5)(LOGSTDOUT, "======================================================================\n");
        mod_mgr_print(LOGSTDOUT, mod_mgr_def);

        mod_mgr_free(mod_mgr_def);

        do_slave_wait_default();
    }

    else if (c_ipv4_to_word("10.10.10.1") == this_tcid && 1 == this_rank)
    {
        UINT32 cbgt_md_id;

        CSTRING *cbgt_db_root_dir;
        CBYTES   root_table_name;

        cbgt_db_root_dir = task_brd_default_get_hsbgt_root_table_dir();
        ASSERT(NULL_PTR != cbgt_db_root_dir);
        cbytes_mount(&root_table_name, strlen("root"), (UINT8 *)"root");

        cbgt_md_id = cbgt_start(CBGT_TYPE_ROOT_SERVER, CBGT_ROOT_TABLE_ID, &root_table_name, NULL_PTR, cbgt_db_root_dir, CBGT_O_RDWR | CBGT_O_CREAT);
        test_case_cbgt_create_table_on_root(cbgt_md_id,
                                            CSCORE_MD_DEFAULT_TABLE_NAME,
                                            1,
                                            CSCORE_MD_DEFAULT_COLF_NAME
                                            );

        ASSERT(ERR_MODULE_ID != cbgt_md_id);

        dbg_log(SEC_0137_DEMO, 9)(LOGSTDOUT, "[DEBUG] main_cbgt: ============================ root server started ====================\n");

        do_slave_wait_default();
    }

    /*fwd rank entrance*/
    else if (c_ipv4_to_word("10.10.10.2") == this_tcid && 1 == this_rank)
    {
        UINT32 cscore_md_id;
        dbg_log(SEC_0137_DEMO, 5)(LOGSTDOUT,"======================================================================\n");
        dbg_log(SEC_0137_DEMO, 5)(LOGSTDOUT,"                taskc_mgr in (tcid %s, rank %ld)                     \n", c_word_to_ipv4(this_tcid), this_rank);
        super_show_work_client(task_brd_default_get_super(), LOGSTDOUT);/*debug only*/
        dbg_log(SEC_0137_DEMO, 5)(LOGSTDOUT,"======================================================================\n");

        cscore_md_id = cscore_start();
        ASSERT(ERR_MODULE_ID != cscore_md_id );
        dbg_log(SEC_0137_DEMO, 9)(LOGSTDOUT, "[DEBUG] main_cscore: cscore_md_id = %ld\n", cscore_md_id);

        //test_case_cscore_001(cscore_md_id);
        //test_case_cscore_002(cscore_md_id);
        //test_case_cscore_003(cscore_md_id);
        //test_case_cscore_004(cscore_md_id);
        test_case_cscore_005(cscore_md_id);

        cscore_end(cscore_md_id);

        do_slave_wait_default();
    }

    /*work process*/
    else
    {
        do_slave_wait_default();
    }

    return (0);
}