Пример #1
0
/**
 * Change the variable name to value. If name does not exist, create it,
 * otherwise replace the old value with the new.
 * @param *name  name to modify
 * @param *value value to set name
 * @return OK on successful change, SYSERR on failure
 */
devcall nvramSet(char *name, char *value)
{
    struct nvram_tuple *tuple;
    ulong length, offset;

    if (OK != nvramInit())
    {
        return SYSERR;
    }

    length = sizeof(struct nvram_tuple) + strnlen(name, NVRAM_STRMAX)
        + 1 + strnlen(value, NVRAM_STRMAX);

    /* check if name is already defined somewhere */
    /* if so unset, so we can reset */
    if (nvramGet(name) != NULL)
    {
        nvramUnset(name);
    }

    /* get enough space for both to be rounded up with \0 stored */
    if (NULL == (tuple = (struct nvram_tuple *)memget(length)))
    {
        return SYSERR;
    }

    /* copy name into memory */
    offset = 0;
    strncpy(tuple->pair, name, strnlen(name, NVRAM_STRMAX));
    offset += strnlen(name, NVRAM_STRMAX);
    strncpy(tuple->pair + offset, "=", 1);
    offset += 1;
    strncpy(tuple->pair + offset, value, strnlen(value, NVRAM_STRMAX));
    offset += strnlen(value, NVRAM_STRMAX);
    strncpy(tuple->pair + offset, "\0", 1);

    /* store pointer to name in nvram_tuple struct */
    nvramInsert(tuple);

    return OK;
}
Пример #2
0
/**
 * Initialize the nvram variable structures for editing
 * @return OK on success, SYSERR on failure
 */
devcall nvramInit(void)
{
    struct dentry *devptr = NULL;
    struct flash *flash = NULL;
    uint nvbase, offset, index, pair_len;
    uint nvram_length, size;
    char *pair;
    struct nvram_tuple *tuple;

    /* check if we already have initialized nvram */
    if (nvram_header != NULL && NVRAM_MAGIC == nvram_header->magic)
    {
        return OK;
    }

#if FLASH
    devptr = (device *)&devtab[FLASH];
#endif
    if (NULL == devptr)
    {
        return SYSERR;
    }

    flash = &flashtab[devptr->minor];

    /* zero out nvram_tuples pointers */
    for (index = 0; index < NVRAM_NHASH; index++)
    {
        nvram_tuples[index] = NULL;
    }

    /* Scan flash at for NVRAM magic number */
    nvbase = flash->base + flash->size - NVRAM_SIZE;
    while (nvbase > flash->base)
    {
        if (NVRAM_MAGIC == *((uint *)nvbase))
            break;
        nvbase -= NVRAM_SIZE;
    }
    offset = 0;

    /* find the head for data */
    nvram_header =
        (struct nvram_header *)memget(sizeof(struct nvram_header));
    memcpy(nvram_header, (void *)nvbase, sizeof(struct nvram_header));
    if (nvram_header->magic != NVRAM_MAGIC)
    {
        return SYSERR;
    }

    offset += sizeof(struct nvram_header);

    /* loop through nvram variables and add to array */
    nvram_length = nvram_header->length;

    nvram_header->length = sizeof(struct nvram_header);

    while (offset < nvram_length)
    {
        /* get the length of a string (name=value\0) */
        pair = (char *)(nvbase + offset);
        pair_len = strnlen(pair, nvram_length - offset);

        /* set offset to next string */
        offset += pair_len + 1;

        if (pair_len <= 0)
        {
            continue;
        }

        /* allocate memory to store tuple */
        size = sizeof(struct nvram_tuple) + pair_len;
        tuple = memget(size);

        /* store tuple */
        memcpy(tuple->pair, pair, pair_len);
        memcpy(tuple->pair + pair_len, "\0", 1);


        nvramInsert(tuple);
    }

    return OK;
}
Пример #3
0
/**
 * Initialize the nvram variable structures for editing
 * @return OK on success, SYSERR on failure
 */
devcall nvramInit(void)
{
    uint32 nvbase, offset, index, pair_len;
    uint32 nvram_length, size;
    char *pair;
    struct nvram_tuple *tuple;

    /* check if we already have initialized nvram */
    if (nvram_header != NULL && NVRAM_MAGIC == nvram_header->magic)
    {
        return OK;
    }

    /* flash size passed in by loader in megabytes */
    if (flash_size < 1024)
    {
	    flash_size = flash_size << 20;
    }

    /* zero out nvram_tuples pointers */
    for (index = 0; index < NVRAM_NHASH; index++)
    {
        nvram_tuples[index] = NULL;
    }

    /* Scan flash at for NVRAM magic number */
    nvbase = FLASH_BASE + flash_size - NVRAM_SIZE;
    while (nvbase > FLASH_BASE)
    {
        if (NVRAM_MAGIC == *((uint32 *)nvbase))
            break;
        nvbase -= NVRAM_SIZE;
    }
    offset = 0;

    /* find the head for data */
    nvram_header =
        (struct nvram_header *)getmem(sizeof(struct nvram_header));
    memcpy(nvram_header, (void *)nvbase, sizeof(struct nvram_header));
    if (nvram_header->magic != NVRAM_MAGIC)
    {
        return SYSERR;
    }

    offset += sizeof(struct nvram_header);

    /* loop through nvram variables and add to array */
    nvram_length = nvram_header->length;

    nvram_header->length = sizeof(struct nvram_header);

    while (offset < nvram_length)
    {
        /* get the length of a string (name=value\0) */
        pair = (char *)(nvbase + offset);
        pair_len = strnlen(pair, nvram_length - offset);

        /* set offset to next string */
        offset += pair_len + 1;

        if (pair_len <= 0)
        {
            continue;
        }

        /* allocate memory to store tuple */
        size = sizeof(struct nvram_tuple) + pair_len;
        tuple = (struct nvram_tuple *)getmem(size);

        /* store tuple */
        memcpy(tuple->pair, pair, pair_len);
        memcpy(tuple->pair + pair_len, "\0", 1);


        nvramInsert(tuple);
    }

    return OK;
}