Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

What do router_defaults and Nvrams refer to?and how to run test.c? #1

Open
1163307648 opened this issue Nov 24, 2016 · 3 comments
Open

Comments

@1163307648
Copy link

Hello,I want to know how to emulate a hardware NVRAM peripheral and i get two questions after reading the libnvram's code:
1: in config.h
#def NVRAM_DEFAULTS_PATH
TABLE (router_defaults)
TABLE (Nvrams)

What do router_defaults and Nvrams refer to? I did not find their definitions.

2: how to run test.c in libnvram

Thanks for the answer

@ddcc
Copy link
Collaborator

ddcc commented Nov 24, 2016

The prototype for the TABLE macro is on nvram.c#L25. Essentially, some firmware have a global symbol that provide a key-value pair list of default values, which is why it is declared as an external weak symbol. This is also discussed on pg. 7 of the paper.

The test file isn't super usable, but you can compile it as follows. It's a little invasive at runtime though, it'll assume that the mount point /nvram exists, and that it can mount a tmpfs onto it.

gcc test.c -o test -L`pwd` -lnvram -Wl,-rpath=`pwd`

@cq674350529
Copy link

cq674350529 commented Apr 18, 2020

Recently, I have used the libnvram.so in qemu user mode, and it works well. Thanks!

Also, I came across an issue: some values in NVRAM_DEFAULTS didn't work as expected. As you (@ddcc) mentioned above, the router_defaults and Nvrams are global symbols used to provide default values.

libnvram/nvram.c

Lines 571 to 595 in 68ad8e9

int nvram_set_default(void) {
int ret = nvram_set_default_builtin();
PRINT_MSG("Loading built-in default values = %d!\n", ret);
#define NATIVE(a, b) \
if (!system(a)) { \
PRINT_MSG("Executing native call to built-in function: %s (%p) = %d!\n", #b, b, b); \
}
#define TABLE(a) \
PRINT_MSG("Checking for symbol \"%s\"...\n", #a); \
if (a) { \
PRINT_MSG("Loading from native built-in table: %s (%p) = %d!\n", #a, a, nvram_set_default_table(a)); \
}
#define PATH(a) \
if (!access(a, R_OK)) { \
PRINT_MSG("Loading from default configuration file: %s = %d!\n", a, foreach_nvram_from(a, (void (*)(const char *, const char *, void *)) nvram_set, NULL)); \
}
NVRAM_DEFAULTS_PATH
#undef PATH
#undef NATIVE
#undef TABLE

The order of call to nvram_set_default_* are: nvram_set_default_builtin(), nvram_set_default_table(a). That is, the values in NVRAM_DEFAULTS will be set first, then the values in router_defaults and Nvrams. So there may be a case the value in NVRAM_DEFAULTS will be overwritten by those in router_defaults and Nvrams.

In my case, I changed the order of call to nvram_set_default_* , shown as follows. So the values in NVRAM_DEFAULTS will work as expected.

PS: I didn't test in in qemu system mode, so didn't know if it had any side effect.

#define NATIVE(a, b) \
    if (!system(a)) { \
        PRINT_MSG("Executing native call to built-in function: %s (%p) = %d!\n", #b, b, b); \
    }

#define TABLE(a) \
    PRINT_MSG("Checking for symbol \"%s\"...\n", #a); \
    if (a) { \
        PRINT_MSG("Loading from native built-in table: %s (%p) = %d!\n", #a, a, nvram_set_default_table(a)); \
    }

#define PATH(a) \
    if (!access(a, R_OK)) { \
        PRINT_MSG("Loading from default configuration file: %s = %d!\n", a, foreach_nvram_from(a, (void (*)(const char *, const char *, void *)) nvram_set, NULL)); \
    }

    NVRAM_DEFAULTS_PATH
#undef PATH
#undef NATIVE
#undef TABLE

    // move it to here
    PRINT_MSG("Loading built-in default values = %d!\n", nvram_set_default_builtin());

@ddcc
Copy link
Collaborator

ddcc commented Apr 18, 2020

Yeah I think the assumption behind that ordering was that the device-specific default symbols should be preferred over any generic pre-defined defaults from the config file. However, they should all be less preferable than the overrides, which are set last.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants