blob: 34bb58e4a984e1a8fc2009e29e83cc041df4c4d9 [file] [log] [blame]
Joe Hershberger170ab112012-12-11 22:16:24 -06001/*
2 * (C) Copyright 2012
3 * Joe Hershberger, National Instruments, joe.hershberger@ni.com
4 *
Wolfgang Denk1a459662013-07-08 09:37:19 +02005 * SPDX-License-Identifier: GPL-2.0+
Joe Hershberger170ab112012-12-11 22:16:24 -06006 */
7
8#include <common.h>
9#include <environment.h>
10
11#if defined(CONFIG_NEEDS_MANUAL_RELOC)
12DECLARE_GLOBAL_DATA_PTR;
13#endif
14
15/*
16 * Look up a callback function pointer by name
17 */
Tom Rini268d9662013-03-12 06:16:50 +000018static struct env_clbk_tbl *find_env_callback(const char *name)
Joe Hershberger170ab112012-12-11 22:16:24 -060019{
20 struct env_clbk_tbl *clbkp;
21 int i;
22 int num_callbacks = ll_entry_count(struct env_clbk_tbl, env_clbk);
23
24 if (name == NULL)
25 return NULL;
26
27 /* look up the callback in the linker-list */
28 for (i = 0, clbkp = ll_entry_start(struct env_clbk_tbl, env_clbk);
29 i < num_callbacks;
30 i++, clbkp++) {
31 if (strcmp(name, clbkp->name) == 0)
32 return clbkp;
33 }
34
35 return NULL;
36}
37
38/*
39 * Look for a possible callback for a newly added variable
40 * This is called specifically when the variable did not exist in the hash
41 * previously, so the blanket update did not find this variable.
42 */
43void env_callback_init(ENTRY *var_entry)
44{
45 const char *var_name = var_entry->key;
46 const char *callback_list = getenv(ENV_CALLBACK_VAR);
47 char callback_name[256] = "";
48 struct env_clbk_tbl *clbkp;
49 int ret = 1;
50
51 /* look in the ".callbacks" var for a reference to this variable */
52 if (callback_list != NULL)
53 ret = env_attr_lookup(callback_list, var_name, callback_name);
54
55 /* only if not found there, look in the static list */
56 if (ret)
57 ret = env_attr_lookup(ENV_CALLBACK_LIST_STATIC, var_name,
58 callback_name);
59
60 /* if an association was found, set the callback pointer */
61 if (!ret && strlen(callback_name)) {
62 clbkp = find_env_callback(callback_name);
63 if (clbkp != NULL)
64#if defined(CONFIG_NEEDS_MANUAL_RELOC)
65 var_entry->callback = clbkp->callback + gd->reloc_off;
66#else
67 var_entry->callback = clbkp->callback;
68#endif
69 }
70}
71
72/*
73 * Called on each existing env var prior to the blanket update since removing
74 * a callback association should remove its callback.
75 */
76static int clear_callback(ENTRY *entry)
77{
78 entry->callback = NULL;
79
80 return 0;
81}
82
83/*
84 * Call for each element in the list that associates variables to callbacks
85 */
86static int set_callback(const char *name, const char *value)
87{
88 ENTRY e, *ep;
89 struct env_clbk_tbl *clbkp;
90
91 e.key = name;
92 e.data = NULL;
93 hsearch_r(e, FIND, &ep, &env_htab, 0);
94
95 /* does the env variable actually exist? */
96 if (ep != NULL) {
97 /* the assocaition delares no callback, so remove the pointer */
98 if (value == NULL || strlen(value) == 0)
99 ep->callback = NULL;
100 else {
101 /* assign the requested callback */
102 clbkp = find_env_callback(value);
103 if (clbkp != NULL)
104#if defined(CONFIG_NEEDS_MANUAL_RELOC)
105 ep->callback = clbkp->callback + gd->reloc_off;
106#else
107 ep->callback = clbkp->callback;
108#endif
109 }
110 }
111
112 return 0;
113}
114
115static int on_callbacks(const char *name, const char *value, enum env_op op,
116 int flags)
117{
118 /* remove all callbacks */
119 hwalk_r(&env_htab, clear_callback);
120
121 /* configure any static callback bindings */
122 env_attr_walk(ENV_CALLBACK_LIST_STATIC, set_callback);
123 /* configure any dynamic callback bindings */
124 env_attr_walk(value, set_callback);
125
126 return 0;
127}
128U_BOOT_ENV_CALLBACK(callbacks, on_callbacks);