Tom Rini | 10e4779 | 2018-05-06 17:58:06 -0400 | [diff] [blame] | 1 | // SPDX-License-Identifier: GPL-2.0+ |
Joe Hershberger | 60fd3ad | 2012-12-11 22:16:24 -0600 | [diff] [blame] | 2 | /* |
| 3 | * (C) Copyright 2012 |
| 4 | * Joe Hershberger, National Instruments, joe.hershberger@ni.com |
Joe Hershberger | 60fd3ad | 2012-12-11 22:16:24 -0600 | [diff] [blame] | 5 | */ |
| 6 | |
Simon Glass | 0af6e2d | 2019-08-01 09:46:52 -0600 | [diff] [blame] | 7 | #include <env.h> |
Simon Glass | 9d1f619 | 2019-08-02 09:44:25 -0600 | [diff] [blame] | 8 | #include <env_internal.h> |
Simon Glass | 3ba929a | 2020-10-30 21:38:53 -0600 | [diff] [blame] | 9 | #include <asm/global_data.h> |
Joe Hershberger | 60fd3ad | 2012-12-11 22:16:24 -0600 | [diff] [blame] | 10 | |
Joe Hershberger | 60fd3ad | 2012-12-11 22:16:24 -0600 | [diff] [blame] | 11 | /* |
| 12 | * Look up a callback function pointer by name |
| 13 | */ |
Tom Rini | fb586da | 2013-03-12 06:16:50 +0000 | [diff] [blame] | 14 | static struct env_clbk_tbl *find_env_callback(const char *name) |
Joe Hershberger | 60fd3ad | 2012-12-11 22:16:24 -0600 | [diff] [blame] | 15 | { |
| 16 | struct env_clbk_tbl *clbkp; |
| 17 | int i; |
| 18 | int num_callbacks = ll_entry_count(struct env_clbk_tbl, env_clbk); |
| 19 | |
| 20 | if (name == NULL) |
| 21 | return NULL; |
| 22 | |
| 23 | /* look up the callback in the linker-list */ |
| 24 | for (i = 0, clbkp = ll_entry_start(struct env_clbk_tbl, env_clbk); |
| 25 | i < num_callbacks; |
| 26 | i++, clbkp++) { |
| 27 | if (strcmp(name, clbkp->name) == 0) |
| 28 | return clbkp; |
| 29 | } |
| 30 | |
| 31 | return NULL; |
| 32 | } |
| 33 | |
Heiko Schocher | df62d35 | 2013-12-19 13:45:04 +0100 | [diff] [blame] | 34 | static int first_call = 1; |
| 35 | static const char *callback_list; |
| 36 | |
Joe Hershberger | 60fd3ad | 2012-12-11 22:16:24 -0600 | [diff] [blame] | 37 | /* |
| 38 | * Look for a possible callback for a newly added variable |
| 39 | * This is called specifically when the variable did not exist in the hash |
| 40 | * previously, so the blanket update did not find this variable. |
| 41 | */ |
Simon Glass | 1a23686 | 2019-08-02 09:44:18 -0600 | [diff] [blame] | 42 | void env_callback_init(struct env_entry *var_entry) |
Joe Hershberger | 60fd3ad | 2012-12-11 22:16:24 -0600 | [diff] [blame] | 43 | { |
| 44 | const char *var_name = var_entry->key; |
Joe Hershberger | 60fd3ad | 2012-12-11 22:16:24 -0600 | [diff] [blame] | 45 | char callback_name[256] = ""; |
| 46 | struct env_clbk_tbl *clbkp; |
| 47 | int ret = 1; |
| 48 | |
Heiko Schocher | df62d35 | 2013-12-19 13:45:04 +0100 | [diff] [blame] | 49 | if (first_call) { |
Simon Glass | 64b723f | 2017-08-03 12:22:12 -0600 | [diff] [blame] | 50 | callback_list = env_get(ENV_CALLBACK_VAR); |
Heiko Schocher | df62d35 | 2013-12-19 13:45:04 +0100 | [diff] [blame] | 51 | first_call = 0; |
| 52 | } |
| 53 | |
Rasmus Villemoes | b86180e | 2020-02-27 13:56:12 +0000 | [diff] [blame] | 54 | var_entry->callback = NULL; |
| 55 | |
Joe Hershberger | 60fd3ad | 2012-12-11 22:16:24 -0600 | [diff] [blame] | 56 | /* look in the ".callbacks" var for a reference to this variable */ |
| 57 | if (callback_list != NULL) |
| 58 | ret = env_attr_lookup(callback_list, var_name, callback_name); |
| 59 | |
| 60 | /* only if not found there, look in the static list */ |
| 61 | if (ret) |
| 62 | ret = env_attr_lookup(ENV_CALLBACK_LIST_STATIC, var_name, |
| 63 | callback_name); |
| 64 | |
| 65 | /* if an association was found, set the callback pointer */ |
| 66 | if (!ret && strlen(callback_name)) { |
| 67 | clbkp = find_env_callback(callback_name); |
| 68 | if (clbkp != NULL) |
Joe Hershberger | 60fd3ad | 2012-12-11 22:16:24 -0600 | [diff] [blame] | 69 | var_entry->callback = clbkp->callback; |
Joe Hershberger | 60fd3ad | 2012-12-11 22:16:24 -0600 | [diff] [blame] | 70 | } |
| 71 | } |
| 72 | |
| 73 | /* |
| 74 | * Called on each existing env var prior to the blanket update since removing |
| 75 | * a callback association should remove its callback. |
| 76 | */ |
Simon Glass | 1a23686 | 2019-08-02 09:44:18 -0600 | [diff] [blame] | 77 | static int clear_callback(struct env_entry *entry) |
Joe Hershberger | 60fd3ad | 2012-12-11 22:16:24 -0600 | [diff] [blame] | 78 | { |
| 79 | entry->callback = NULL; |
| 80 | |
| 81 | return 0; |
| 82 | } |
| 83 | |
| 84 | /* |
| 85 | * Call for each element in the list that associates variables to callbacks |
| 86 | */ |
Joe Hershberger | d741f56 | 2015-05-20 14:27:19 -0500 | [diff] [blame] | 87 | static int set_callback(const char *name, const char *value, void *priv) |
Joe Hershberger | 60fd3ad | 2012-12-11 22:16:24 -0600 | [diff] [blame] | 88 | { |
Simon Glass | 1a23686 | 2019-08-02 09:44:18 -0600 | [diff] [blame] | 89 | struct env_entry e, *ep; |
Joe Hershberger | 60fd3ad | 2012-12-11 22:16:24 -0600 | [diff] [blame] | 90 | struct env_clbk_tbl *clbkp; |
| 91 | |
| 92 | e.key = name; |
| 93 | e.data = NULL; |
Peng Fan | 1bef00d | 2015-12-23 12:07:24 +0800 | [diff] [blame] | 94 | e.callback = NULL; |
Simon Glass | 63a2f57 | 2019-08-01 09:47:09 -0600 | [diff] [blame] | 95 | hsearch_r(e, ENV_FIND, &ep, &env_htab, 0); |
Joe Hershberger | 60fd3ad | 2012-12-11 22:16:24 -0600 | [diff] [blame] | 96 | |
| 97 | /* does the env variable actually exist? */ |
| 98 | if (ep != NULL) { |
| 99 | /* the assocaition delares no callback, so remove the pointer */ |
| 100 | if (value == NULL || strlen(value) == 0) |
| 101 | ep->callback = NULL; |
| 102 | else { |
| 103 | /* assign the requested callback */ |
| 104 | clbkp = find_env_callback(value); |
| 105 | if (clbkp != NULL) |
Joe Hershberger | 60fd3ad | 2012-12-11 22:16:24 -0600 | [diff] [blame] | 106 | ep->callback = clbkp->callback; |
Joe Hershberger | 60fd3ad | 2012-12-11 22:16:24 -0600 | [diff] [blame] | 107 | } |
| 108 | } |
| 109 | |
| 110 | return 0; |
| 111 | } |
| 112 | |
| 113 | static int on_callbacks(const char *name, const char *value, enum env_op op, |
| 114 | int flags) |
| 115 | { |
| 116 | /* remove all callbacks */ |
| 117 | hwalk_r(&env_htab, clear_callback); |
| 118 | |
| 119 | /* configure any static callback bindings */ |
Joe Hershberger | d741f56 | 2015-05-20 14:27:19 -0500 | [diff] [blame] | 120 | env_attr_walk(ENV_CALLBACK_LIST_STATIC, set_callback, NULL); |
Joe Hershberger | 60fd3ad | 2012-12-11 22:16:24 -0600 | [diff] [blame] | 121 | /* configure any dynamic callback bindings */ |
Joe Hershberger | d741f56 | 2015-05-20 14:27:19 -0500 | [diff] [blame] | 122 | env_attr_walk(value, set_callback, NULL); |
Joe Hershberger | 60fd3ad | 2012-12-11 22:16:24 -0600 | [diff] [blame] | 123 | |
| 124 | return 0; |
| 125 | } |
| 126 | U_BOOT_ENV_CALLBACK(callbacks, on_callbacks); |