Tom Rini | 83d290c | 2018-05-06 17:58:06 -0400 | [diff] [blame] | 1 | // SPDX-License-Identifier: GPL-2.0+ |
Joe Hershberger | 170ab11 | 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 | 170ab11 | 2012-12-11 22:16:24 -0600 | [diff] [blame] | 5 | */ |
| 6 | |
| 7 | #include <common.h> |
Simon Glass | 7b51b57 | 2019-08-01 09:46:52 -0600 | [diff] [blame] | 8 | #include <env.h> |
Simon Glass | f3998fd | 2019-08-02 09:44:25 -0600 | [diff] [blame] | 9 | #include <env_internal.h> |
Joe Hershberger | 170ab11 | 2012-12-11 22:16:24 -0600 | [diff] [blame] | 10 | |
| 11 | #if defined(CONFIG_NEEDS_MANUAL_RELOC) |
| 12 | DECLARE_GLOBAL_DATA_PTR; |
| 13 | #endif |
| 14 | |
| 15 | /* |
| 16 | * Look up a callback function pointer by name |
| 17 | */ |
Tom Rini | 268d966 | 2013-03-12 06:16:50 +0000 | [diff] [blame] | 18 | static struct env_clbk_tbl *find_env_callback(const char *name) |
Joe Hershberger | 170ab11 | 2012-12-11 22:16:24 -0600 | [diff] [blame] | 19 | { |
| 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 | |
Heiko Schocher | 1b61027 | 2013-12-19 13:45:04 +0100 | [diff] [blame] | 38 | static int first_call = 1; |
| 39 | static const char *callback_list; |
| 40 | |
Joe Hershberger | 170ab11 | 2012-12-11 22:16:24 -0600 | [diff] [blame] | 41 | /* |
| 42 | * Look for a possible callback for a newly added variable |
| 43 | * This is called specifically when the variable did not exist in the hash |
| 44 | * previously, so the blanket update did not find this variable. |
| 45 | */ |
Simon Glass | dd2408c | 2019-08-02 09:44:18 -0600 | [diff] [blame] | 46 | void env_callback_init(struct env_entry *var_entry) |
Joe Hershberger | 170ab11 | 2012-12-11 22:16:24 -0600 | [diff] [blame] | 47 | { |
| 48 | const char *var_name = var_entry->key; |
Joe Hershberger | 170ab11 | 2012-12-11 22:16:24 -0600 | [diff] [blame] | 49 | char callback_name[256] = ""; |
| 50 | struct env_clbk_tbl *clbkp; |
| 51 | int ret = 1; |
| 52 | |
Heiko Schocher | 1b61027 | 2013-12-19 13:45:04 +0100 | [diff] [blame] | 53 | if (first_call) { |
Simon Glass | 00caae6 | 2017-08-03 12:22:12 -0600 | [diff] [blame] | 54 | callback_list = env_get(ENV_CALLBACK_VAR); |
Heiko Schocher | 1b61027 | 2013-12-19 13:45:04 +0100 | [diff] [blame] | 55 | first_call = 0; |
| 56 | } |
| 57 | |
Rasmus Villemoes | 080019b | 2020-02-27 13:56:12 +0000 | [diff] [blame] | 58 | var_entry->callback = NULL; |
| 59 | |
Joe Hershberger | 170ab11 | 2012-12-11 22:16:24 -0600 | [diff] [blame] | 60 | /* look in the ".callbacks" var for a reference to this variable */ |
| 61 | if (callback_list != NULL) |
| 62 | ret = env_attr_lookup(callback_list, var_name, callback_name); |
| 63 | |
| 64 | /* only if not found there, look in the static list */ |
| 65 | if (ret) |
| 66 | ret = env_attr_lookup(ENV_CALLBACK_LIST_STATIC, var_name, |
| 67 | callback_name); |
| 68 | |
| 69 | /* if an association was found, set the callback pointer */ |
| 70 | if (!ret && strlen(callback_name)) { |
| 71 | clbkp = find_env_callback(callback_name); |
| 72 | if (clbkp != NULL) |
| 73 | #if defined(CONFIG_NEEDS_MANUAL_RELOC) |
| 74 | var_entry->callback = clbkp->callback + gd->reloc_off; |
| 75 | #else |
| 76 | var_entry->callback = clbkp->callback; |
| 77 | #endif |
| 78 | } |
| 79 | } |
| 80 | |
| 81 | /* |
| 82 | * Called on each existing env var prior to the blanket update since removing |
| 83 | * a callback association should remove its callback. |
| 84 | */ |
Simon Glass | dd2408c | 2019-08-02 09:44:18 -0600 | [diff] [blame] | 85 | static int clear_callback(struct env_entry *entry) |
Joe Hershberger | 170ab11 | 2012-12-11 22:16:24 -0600 | [diff] [blame] | 86 | { |
| 87 | entry->callback = NULL; |
| 88 | |
| 89 | return 0; |
| 90 | } |
| 91 | |
| 92 | /* |
| 93 | * Call for each element in the list that associates variables to callbacks |
| 94 | */ |
Joe Hershberger | cca98fd | 2015-05-20 14:27:19 -0500 | [diff] [blame] | 95 | static int set_callback(const char *name, const char *value, void *priv) |
Joe Hershberger | 170ab11 | 2012-12-11 22:16:24 -0600 | [diff] [blame] | 96 | { |
Simon Glass | dd2408c | 2019-08-02 09:44:18 -0600 | [diff] [blame] | 97 | struct env_entry e, *ep; |
Joe Hershberger | 170ab11 | 2012-12-11 22:16:24 -0600 | [diff] [blame] | 98 | struct env_clbk_tbl *clbkp; |
| 99 | |
| 100 | e.key = name; |
| 101 | e.data = NULL; |
Peng Fan | 5a68943 | 2015-12-23 12:07:24 +0800 | [diff] [blame] | 102 | e.callback = NULL; |
Simon Glass | 3f0d680 | 2019-08-01 09:47:09 -0600 | [diff] [blame] | 103 | hsearch_r(e, ENV_FIND, &ep, &env_htab, 0); |
Joe Hershberger | 170ab11 | 2012-12-11 22:16:24 -0600 | [diff] [blame] | 104 | |
| 105 | /* does the env variable actually exist? */ |
| 106 | if (ep != NULL) { |
| 107 | /* the assocaition delares no callback, so remove the pointer */ |
| 108 | if (value == NULL || strlen(value) == 0) |
| 109 | ep->callback = NULL; |
| 110 | else { |
| 111 | /* assign the requested callback */ |
| 112 | clbkp = find_env_callback(value); |
| 113 | if (clbkp != NULL) |
| 114 | #if defined(CONFIG_NEEDS_MANUAL_RELOC) |
| 115 | ep->callback = clbkp->callback + gd->reloc_off; |
| 116 | #else |
| 117 | ep->callback = clbkp->callback; |
| 118 | #endif |
| 119 | } |
| 120 | } |
| 121 | |
| 122 | return 0; |
| 123 | } |
| 124 | |
| 125 | static int on_callbacks(const char *name, const char *value, enum env_op op, |
| 126 | int flags) |
| 127 | { |
| 128 | /* remove all callbacks */ |
| 129 | hwalk_r(&env_htab, clear_callback); |
| 130 | |
| 131 | /* configure any static callback bindings */ |
Joe Hershberger | cca98fd | 2015-05-20 14:27:19 -0500 | [diff] [blame] | 132 | env_attr_walk(ENV_CALLBACK_LIST_STATIC, set_callback, NULL); |
Joe Hershberger | 170ab11 | 2012-12-11 22:16:24 -0600 | [diff] [blame] | 133 | /* configure any dynamic callback bindings */ |
Joe Hershberger | cca98fd | 2015-05-20 14:27:19 -0500 | [diff] [blame] | 134 | env_attr_walk(value, set_callback, NULL); |
Joe Hershberger | 170ab11 | 2012-12-11 22:16:24 -0600 | [diff] [blame] | 135 | |
| 136 | return 0; |
| 137 | } |
| 138 | U_BOOT_ENV_CALLBACK(callbacks, on_callbacks); |