wdenk | 6aff311 | 2002-12-17 01:51:00 +0000 | [diff] [blame] | 1 | /* |
wdenk | 4a6fd34 | 2003-04-12 23:38:12 +0000 | [diff] [blame] | 2 | * (C) Copyright 2000-2003 |
wdenk | 6aff311 | 2002-12-17 01:51:00 +0000 | [diff] [blame] | 3 | * Wolfgang Denk, DENX Software Engineering, wd@denx.de. |
| 4 | * |
| 5 | * See file CREDITS for list of people who contributed to this |
| 6 | * project. |
| 7 | * |
| 8 | * This program is free software; you can redistribute it and/or |
| 9 | * modify it under the terms of the GNU General Public License as |
| 10 | * published by the Free Software Foundation; either version 2 of |
| 11 | * the License, or (at your option) any later version. |
| 12 | * |
| 13 | * This program is distributed in the hope that it will be useful, |
| 14 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
wdenk | 4a6fd34 | 2003-04-12 23:38:12 +0000 | [diff] [blame] | 15 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
wdenk | 6aff311 | 2002-12-17 01:51:00 +0000 | [diff] [blame] | 16 | * GNU General Public License for more details. |
| 17 | * |
| 18 | * You should have received a copy of the GNU General Public License |
| 19 | * along with this program; if not, write to the Free Software |
| 20 | * Foundation, Inc., 59 Temple Place, Suite 330, Boston, |
| 21 | * MA 02111-1307 USA |
| 22 | */ |
| 23 | |
| 24 | #include <errno.h> |
| 25 | #include <fcntl.h> |
| 26 | #include <stdio.h> |
| 27 | #include <stdlib.h> |
| 28 | #include <stddef.h> |
| 29 | #include <string.h> |
| 30 | #include <sys/types.h> |
| 31 | #include <sys/ioctl.h> |
| 32 | #include <sys/stat.h> |
| 33 | #include <unistd.h> |
| 34 | #include <linux/mtd/mtd.h> |
| 35 | #include "fw_env.h" |
| 36 | |
wdenk | 4a6fd34 | 2003-04-12 23:38:12 +0000 | [diff] [blame] | 37 | typedef unsigned char uchar; |
wdenk | 6aff311 | 2002-12-17 01:51:00 +0000 | [diff] [blame] | 38 | |
| 39 | #define CMD_GETENV "fw_printenv" |
| 40 | #define CMD_SETENV "fw_setenv" |
| 41 | |
| 42 | typedef struct envdev_s { |
wdenk | 4a6fd34 | 2003-04-12 23:38:12 +0000 | [diff] [blame] | 43 | uchar devname[16]; /* Device name */ |
| 44 | ulong devoff; /* Device offset */ |
| 45 | ulong env_size; /* environment size */ |
| 46 | ulong erase_size; /* device erase size */ |
wdenk | 6aff311 | 2002-12-17 01:51:00 +0000 | [diff] [blame] | 47 | } envdev_t; |
| 48 | |
| 49 | static envdev_t envdevices[2]; |
| 50 | static int curdev; |
| 51 | |
| 52 | #define DEVNAME(i) envdevices[(i)].devname |
wdenk | d0fb80c | 2003-01-11 09:48:40 +0000 | [diff] [blame] | 53 | #define DEVOFFSET(i) envdevices[(i)].devoff |
wdenk | 6aff311 | 2002-12-17 01:51:00 +0000 | [diff] [blame] | 54 | #define ENVSIZE(i) envdevices[(i)].env_size |
| 55 | #define DEVESIZE(i) envdevices[(i)].erase_size |
| 56 | |
| 57 | #define CFG_ENV_SIZE ENVSIZE(curdev) |
| 58 | |
wdenk | d0fb80c | 2003-01-11 09:48:40 +0000 | [diff] [blame] | 59 | #define ENV_SIZE getenvsize() |
wdenk | 6aff311 | 2002-12-17 01:51:00 +0000 | [diff] [blame] | 60 | |
| 61 | typedef struct environment_s { |
wdenk | 4a6fd34 | 2003-04-12 23:38:12 +0000 | [diff] [blame] | 62 | ulong crc; /* CRC32 over data bytes */ |
| 63 | uchar flags; /* active or obsolete */ |
wdenk | 6aff311 | 2002-12-17 01:51:00 +0000 | [diff] [blame] | 64 | uchar *data; |
| 65 | } env_t; |
| 66 | |
| 67 | static env_t environment; |
wdenk | 6aff311 | 2002-12-17 01:51:00 +0000 | [diff] [blame] | 68 | |
wdenk | d0fb80c | 2003-01-11 09:48:40 +0000 | [diff] [blame] | 69 | static int HaveRedundEnv = 0; |
| 70 | |
wdenk | 6aff311 | 2002-12-17 01:51:00 +0000 | [diff] [blame] | 71 | static uchar active_flag = 1; |
| 72 | static uchar obsolete_flag = 0; |
wdenk | d0fb80c | 2003-01-11 09:48:40 +0000 | [diff] [blame] | 73 | |
wdenk | 6aff311 | 2002-12-17 01:51:00 +0000 | [diff] [blame] | 74 | |
| 75 | #define XMK_STR(x) #x |
| 76 | #define MK_STR(x) XMK_STR(x) |
| 77 | |
| 78 | static uchar default_environment[] = { |
wdenk | d0fb80c | 2003-01-11 09:48:40 +0000 | [diff] [blame] | 79 | #if defined(CONFIG_BOOTARGS) |
wdenk | 4a6fd34 | 2003-04-12 23:38:12 +0000 | [diff] [blame] | 80 | "bootargs=" CONFIG_BOOTARGS "\0" |
wdenk | 6aff311 | 2002-12-17 01:51:00 +0000 | [diff] [blame] | 81 | #endif |
wdenk | d0fb80c | 2003-01-11 09:48:40 +0000 | [diff] [blame] | 82 | #if defined(CONFIG_BOOTCOMMAND) |
wdenk | 4a6fd34 | 2003-04-12 23:38:12 +0000 | [diff] [blame] | 83 | "bootcmd=" CONFIG_BOOTCOMMAND "\0" |
wdenk | 6aff311 | 2002-12-17 01:51:00 +0000 | [diff] [blame] | 84 | #endif |
wdenk | d0fb80c | 2003-01-11 09:48:40 +0000 | [diff] [blame] | 85 | #if defined(CONFIG_RAMBOOTCOMMAND) |
wdenk | 4a6fd34 | 2003-04-12 23:38:12 +0000 | [diff] [blame] | 86 | "ramboot=" CONFIG_RAMBOOTCOMMAND "\0" |
wdenk | d0fb80c | 2003-01-11 09:48:40 +0000 | [diff] [blame] | 87 | #endif |
| 88 | #if defined(CONFIG_NFSBOOTCOMMAND) |
wdenk | 4a6fd34 | 2003-04-12 23:38:12 +0000 | [diff] [blame] | 89 | "nfsboot=" CONFIG_NFSBOOTCOMMAND "\0" |
wdenk | d0fb80c | 2003-01-11 09:48:40 +0000 | [diff] [blame] | 90 | #endif |
| 91 | #if defined(CONFIG_BOOTDELAY) && (CONFIG_BOOTDELAY >= 0) |
wdenk | 4a6fd34 | 2003-04-12 23:38:12 +0000 | [diff] [blame] | 92 | "bootdelay=" MK_STR (CONFIG_BOOTDELAY) "\0" |
wdenk | 6aff311 | 2002-12-17 01:51:00 +0000 | [diff] [blame] | 93 | #endif |
wdenk | d0fb80c | 2003-01-11 09:48:40 +0000 | [diff] [blame] | 94 | #if defined(CONFIG_BAUDRATE) && (CONFIG_BAUDRATE >= 0) |
wdenk | 4a6fd34 | 2003-04-12 23:38:12 +0000 | [diff] [blame] | 95 | "baudrate=" MK_STR (CONFIG_BAUDRATE) "\0" |
wdenk | 6aff311 | 2002-12-17 01:51:00 +0000 | [diff] [blame] | 96 | #endif |
wdenk | d0fb80c | 2003-01-11 09:48:40 +0000 | [diff] [blame] | 97 | #ifdef CONFIG_LOADS_ECHO |
wdenk | 4a6fd34 | 2003-04-12 23:38:12 +0000 | [diff] [blame] | 98 | "loads_echo=" MK_STR (CONFIG_LOADS_ECHO) "\0" |
wdenk | d0fb80c | 2003-01-11 09:48:40 +0000 | [diff] [blame] | 99 | #endif |
wdenk | 6aff311 | 2002-12-17 01:51:00 +0000 | [diff] [blame] | 100 | #ifdef CONFIG_ETHADDR |
wdenk | 4a6fd34 | 2003-04-12 23:38:12 +0000 | [diff] [blame] | 101 | "ethaddr=" MK_STR (CONFIG_ETHADDR) "\0" |
wdenk | 6aff311 | 2002-12-17 01:51:00 +0000 | [diff] [blame] | 102 | #endif |
wdenk | d0fb80c | 2003-01-11 09:48:40 +0000 | [diff] [blame] | 103 | #ifdef CONFIG_ETH1ADDR |
wdenk | 4a6fd34 | 2003-04-12 23:38:12 +0000 | [diff] [blame] | 104 | "eth1addr=" MK_STR (CONFIG_ETH1ADDR) "\0" |
wdenk | d0fb80c | 2003-01-11 09:48:40 +0000 | [diff] [blame] | 105 | #endif |
| 106 | #ifdef CONFIG_ETH2ADDR |
wdenk | 4a6fd34 | 2003-04-12 23:38:12 +0000 | [diff] [blame] | 107 | "eth2addr=" MK_STR (CONFIG_ETH2ADDR) "\0" |
wdenk | d0fb80c | 2003-01-11 09:48:40 +0000 | [diff] [blame] | 108 | #endif |
wdenk | e2ffd59 | 2004-12-31 09:32:47 +0000 | [diff] [blame] | 109 | #ifdef CONFIG_ETH3ADDR |
| 110 | "eth3addr=" MK_STR (CONFIG_ETH3ADDR) "\0" |
| 111 | #endif |
wdenk | d0fb80c | 2003-01-11 09:48:40 +0000 | [diff] [blame] | 112 | #ifdef CONFIG_ETHPRIME |
wdenk | 4a6fd34 | 2003-04-12 23:38:12 +0000 | [diff] [blame] | 113 | "ethprime=" CONFIG_ETHPRIME "\0" |
wdenk | d0fb80c | 2003-01-11 09:48:40 +0000 | [diff] [blame] | 114 | #endif |
wdenk | 6aff311 | 2002-12-17 01:51:00 +0000 | [diff] [blame] | 115 | #ifdef CONFIG_IPADDR |
wdenk | 4a6fd34 | 2003-04-12 23:38:12 +0000 | [diff] [blame] | 116 | "ipaddr=" MK_STR (CONFIG_IPADDR) "\0" |
wdenk | 6aff311 | 2002-12-17 01:51:00 +0000 | [diff] [blame] | 117 | #endif |
| 118 | #ifdef CONFIG_SERVERIP |
wdenk | 4a6fd34 | 2003-04-12 23:38:12 +0000 | [diff] [blame] | 119 | "serverip=" MK_STR (CONFIG_SERVERIP) "\0" |
wdenk | 6aff311 | 2002-12-17 01:51:00 +0000 | [diff] [blame] | 120 | #endif |
wdenk | d0fb80c | 2003-01-11 09:48:40 +0000 | [diff] [blame] | 121 | #ifdef CFG_AUTOLOAD |
wdenk | 4a6fd34 | 2003-04-12 23:38:12 +0000 | [diff] [blame] | 122 | "autoload=" CFG_AUTOLOAD "\0" |
wdenk | d0fb80c | 2003-01-11 09:48:40 +0000 | [diff] [blame] | 123 | #endif |
| 124 | #ifdef CONFIG_ROOTPATH |
wdenk | 4a6fd34 | 2003-04-12 23:38:12 +0000 | [diff] [blame] | 125 | "rootpath=" MK_STR (CONFIG_ROOTPATH) "\0" |
wdenk | d0fb80c | 2003-01-11 09:48:40 +0000 | [diff] [blame] | 126 | #endif |
| 127 | #ifdef CONFIG_GATEWAYIP |
wdenk | 4a6fd34 | 2003-04-12 23:38:12 +0000 | [diff] [blame] | 128 | "gatewayip=" MK_STR (CONFIG_GATEWAYIP) "\0" |
wdenk | d0fb80c | 2003-01-11 09:48:40 +0000 | [diff] [blame] | 129 | #endif |
| 130 | #ifdef CONFIG_NETMASK |
wdenk | 4a6fd34 | 2003-04-12 23:38:12 +0000 | [diff] [blame] | 131 | "netmask=" MK_STR (CONFIG_NETMASK) "\0" |
wdenk | d0fb80c | 2003-01-11 09:48:40 +0000 | [diff] [blame] | 132 | #endif |
| 133 | #ifdef CONFIG_HOSTNAME |
wdenk | 4a6fd34 | 2003-04-12 23:38:12 +0000 | [diff] [blame] | 134 | "hostname=" MK_STR (CONFIG_HOSTNAME) "\0" |
wdenk | d0fb80c | 2003-01-11 09:48:40 +0000 | [diff] [blame] | 135 | #endif |
| 136 | #ifdef CONFIG_BOOTFILE |
wdenk | 4a6fd34 | 2003-04-12 23:38:12 +0000 | [diff] [blame] | 137 | "bootfile=" MK_STR (CONFIG_BOOTFILE) "\0" |
wdenk | d0fb80c | 2003-01-11 09:48:40 +0000 | [diff] [blame] | 138 | #endif |
| 139 | #ifdef CONFIG_LOADADDR |
wdenk | 4a6fd34 | 2003-04-12 23:38:12 +0000 | [diff] [blame] | 140 | "loadaddr=" MK_STR (CONFIG_LOADADDR) "\0" |
wdenk | d0fb80c | 2003-01-11 09:48:40 +0000 | [diff] [blame] | 141 | #endif |
| 142 | #ifdef CONFIG_PREBOOT |
wdenk | 4a6fd34 | 2003-04-12 23:38:12 +0000 | [diff] [blame] | 143 | "preboot=" CONFIG_PREBOOT "\0" |
wdenk | d0fb80c | 2003-01-11 09:48:40 +0000 | [diff] [blame] | 144 | #endif |
| 145 | #ifdef CONFIG_CLOCKS_IN_MHZ |
wdenk | 4a6fd34 | 2003-04-12 23:38:12 +0000 | [diff] [blame] | 146 | "clocks_in_mhz=" "1" "\0" |
wdenk | d0fb80c | 2003-01-11 09:48:40 +0000 | [diff] [blame] | 147 | #endif |
stroese | ad10dd9 | 2003-02-14 11:21:23 +0000 | [diff] [blame] | 148 | #if defined(CONFIG_PCI_BOOTDELAY) && (CONFIG_PCI_BOOTDELAY > 0) |
wdenk | 4a6fd34 | 2003-04-12 23:38:12 +0000 | [diff] [blame] | 149 | "pcidelay=" MK_STR (CONFIG_PCI_BOOTDELAY) "\0" |
stroese | ad10dd9 | 2003-02-14 11:21:23 +0000 | [diff] [blame] | 150 | #endif |
wdenk | d0fb80c | 2003-01-11 09:48:40 +0000 | [diff] [blame] | 151 | #ifdef CONFIG_EXTRA_ENV_SETTINGS |
| 152 | CONFIG_EXTRA_ENV_SETTINGS |
| 153 | #endif |
wdenk | 4a6fd34 | 2003-04-12 23:38:12 +0000 | [diff] [blame] | 154 | "\0" /* Termimate env_t data with 2 NULs */ |
wdenk | 6aff311 | 2002-12-17 01:51:00 +0000 | [diff] [blame] | 155 | }; |
| 156 | |
wdenk | 4a6fd34 | 2003-04-12 23:38:12 +0000 | [diff] [blame] | 157 | static int flash_io (int mode); |
| 158 | static uchar *envmatch (uchar * s1, uchar * s2); |
| 159 | static int env_init (void); |
| 160 | static int parse_config (void); |
| 161 | |
wdenk | d0fb80c | 2003-01-11 09:48:40 +0000 | [diff] [blame] | 162 | #if defined(CONFIG_FILE) |
wdenk | 4a6fd34 | 2003-04-12 23:38:12 +0000 | [diff] [blame] | 163 | static int get_config (char *); |
wdenk | d0fb80c | 2003-01-11 09:48:40 +0000 | [diff] [blame] | 164 | #endif |
wdenk | 4a6fd34 | 2003-04-12 23:38:12 +0000 | [diff] [blame] | 165 | static inline ulong getenvsize (void) |
wdenk | d0fb80c | 2003-01-11 09:48:40 +0000 | [diff] [blame] | 166 | { |
wdenk | 4a6fd34 | 2003-04-12 23:38:12 +0000 | [diff] [blame] | 167 | ulong rc = CFG_ENV_SIZE - sizeof (long); |
| 168 | |
wdenk | d0fb80c | 2003-01-11 09:48:40 +0000 | [diff] [blame] | 169 | if (HaveRedundEnv) |
wdenk | 4a6fd34 | 2003-04-12 23:38:12 +0000 | [diff] [blame] | 170 | rc -= sizeof (char); |
wdenk | d0fb80c | 2003-01-11 09:48:40 +0000 | [diff] [blame] | 171 | return rc; |
| 172 | } |
wdenk | 6aff311 | 2002-12-17 01:51:00 +0000 | [diff] [blame] | 173 | |
| 174 | /* |
| 175 | * Search the environment for a variable. |
| 176 | * Return the value, if found, or NULL, if not found. |
| 177 | */ |
| 178 | unsigned char *fw_getenv (unsigned char *name) |
| 179 | { |
| 180 | uchar *env, *nxt; |
| 181 | |
wdenk | 4a6fd34 | 2003-04-12 23:38:12 +0000 | [diff] [blame] | 182 | if (env_init ()) |
wdenk | 6aff311 | 2002-12-17 01:51:00 +0000 | [diff] [blame] | 183 | return (NULL); |
| 184 | |
wdenk | 4a6fd34 | 2003-04-12 23:38:12 +0000 | [diff] [blame] | 185 | for (env = environment.data; *env; env = nxt + 1) { |
wdenk | 6aff311 | 2002-12-17 01:51:00 +0000 | [diff] [blame] | 186 | uchar *val; |
| 187 | |
wdenk | 4a6fd34 | 2003-04-12 23:38:12 +0000 | [diff] [blame] | 188 | for (nxt = env; *nxt; ++nxt) { |
wdenk | 6aff311 | 2002-12-17 01:51:00 +0000 | [diff] [blame] | 189 | if (nxt >= &environment.data[ENV_SIZE]) { |
| 190 | fprintf (stderr, "## Error: " |
| 191 | "environment not terminated\n"); |
| 192 | return (NULL); |
| 193 | } |
| 194 | } |
wdenk | 4a6fd34 | 2003-04-12 23:38:12 +0000 | [diff] [blame] | 195 | val = envmatch (name, env); |
wdenk | 6aff311 | 2002-12-17 01:51:00 +0000 | [diff] [blame] | 196 | if (!val) |
| 197 | continue; |
| 198 | return (val); |
| 199 | } |
| 200 | return (NULL); |
| 201 | } |
| 202 | |
| 203 | /* |
| 204 | * Print the current definition of one, or more, or all |
| 205 | * environment variables |
| 206 | */ |
wdenk | 4a6fd34 | 2003-04-12 23:38:12 +0000 | [diff] [blame] | 207 | void fw_printenv (int argc, char *argv[]) |
wdenk | 6aff311 | 2002-12-17 01:51:00 +0000 | [diff] [blame] | 208 | { |
| 209 | uchar *env, *nxt; |
| 210 | int i, n_flag; |
| 211 | |
wdenk | 4a6fd34 | 2003-04-12 23:38:12 +0000 | [diff] [blame] | 212 | if (env_init ()) |
wdenk | 6aff311 | 2002-12-17 01:51:00 +0000 | [diff] [blame] | 213 | return; |
| 214 | |
wdenk | 4a6fd34 | 2003-04-12 23:38:12 +0000 | [diff] [blame] | 215 | if (argc == 1) { /* Print all env variables */ |
| 216 | for (env = environment.data; *env; env = nxt + 1) { |
| 217 | for (nxt = env; *nxt; ++nxt) { |
wdenk | 6aff311 | 2002-12-17 01:51:00 +0000 | [diff] [blame] | 218 | if (nxt >= &environment.data[ENV_SIZE]) { |
| 219 | fprintf (stderr, "## Error: " |
| 220 | "environment not terminated\n"); |
| 221 | return; |
| 222 | } |
| 223 | } |
| 224 | |
wdenk | 4a6fd34 | 2003-04-12 23:38:12 +0000 | [diff] [blame] | 225 | printf ("%s\n", env); |
wdenk | 6aff311 | 2002-12-17 01:51:00 +0000 | [diff] [blame] | 226 | } |
| 227 | return; |
| 228 | } |
| 229 | |
wdenk | 4a6fd34 | 2003-04-12 23:38:12 +0000 | [diff] [blame] | 230 | if (strcmp (argv[1], "-n") == 0) { |
wdenk | 6aff311 | 2002-12-17 01:51:00 +0000 | [diff] [blame] | 231 | n_flag = 1; |
| 232 | ++argv; |
| 233 | --argc; |
| 234 | if (argc != 2) { |
| 235 | fprintf (stderr, "## Error: " |
| 236 | "`-n' option requires exactly one argument\n"); |
| 237 | return; |
| 238 | } |
| 239 | } else { |
| 240 | n_flag = 0; |
| 241 | } |
| 242 | |
wdenk | 4a6fd34 | 2003-04-12 23:38:12 +0000 | [diff] [blame] | 243 | for (i = 1; i < argc; ++i) { /* print single env variables */ |
wdenk | 6aff311 | 2002-12-17 01:51:00 +0000 | [diff] [blame] | 244 | uchar *name = argv[i]; |
| 245 | uchar *val = NULL; |
| 246 | |
wdenk | 4a6fd34 | 2003-04-12 23:38:12 +0000 | [diff] [blame] | 247 | for (env = environment.data; *env; env = nxt + 1) { |
wdenk | 6aff311 | 2002-12-17 01:51:00 +0000 | [diff] [blame] | 248 | |
wdenk | 4a6fd34 | 2003-04-12 23:38:12 +0000 | [diff] [blame] | 249 | for (nxt = env; *nxt; ++nxt) { |
wdenk | 6aff311 | 2002-12-17 01:51:00 +0000 | [diff] [blame] | 250 | if (nxt >= &environment.data[ENV_SIZE]) { |
| 251 | fprintf (stderr, "## Error: " |
| 252 | "environment not terminated\n"); |
| 253 | return; |
| 254 | } |
| 255 | } |
wdenk | 4a6fd34 | 2003-04-12 23:38:12 +0000 | [diff] [blame] | 256 | val = envmatch (name, env); |
wdenk | 6aff311 | 2002-12-17 01:51:00 +0000 | [diff] [blame] | 257 | if (val) { |
| 258 | if (!n_flag) { |
| 259 | fputs (name, stdout); |
wdenk | 4a6fd34 | 2003-04-12 23:38:12 +0000 | [diff] [blame] | 260 | putc ('=', stdout); |
wdenk | 6aff311 | 2002-12-17 01:51:00 +0000 | [diff] [blame] | 261 | } |
wdenk | 4a6fd34 | 2003-04-12 23:38:12 +0000 | [diff] [blame] | 262 | puts (val); |
wdenk | 6aff311 | 2002-12-17 01:51:00 +0000 | [diff] [blame] | 263 | break; |
| 264 | } |
| 265 | } |
| 266 | if (!val) |
wdenk | 4a6fd34 | 2003-04-12 23:38:12 +0000 | [diff] [blame] | 267 | fprintf (stderr, "## Error: \"%s\" not defined\n", name); |
wdenk | 6aff311 | 2002-12-17 01:51:00 +0000 | [diff] [blame] | 268 | } |
| 269 | } |
| 270 | |
| 271 | /* |
| 272 | * Deletes or sets environment variables. Returns errno style error codes: |
| 273 | * 0 - OK |
| 274 | * EINVAL - need at least 1 argument |
| 275 | * EROFS - certain variables ("ethaddr", "serial#") cannot be |
| 276 | * modified or deleted |
| 277 | * |
| 278 | */ |
| 279 | int fw_setenv (int argc, char *argv[]) |
| 280 | { |
wdenk | 4a6fd34 | 2003-04-12 23:38:12 +0000 | [diff] [blame] | 281 | int i, len; |
wdenk | 6aff311 | 2002-12-17 01:51:00 +0000 | [diff] [blame] | 282 | uchar *env, *nxt; |
| 283 | uchar *oldval = NULL; |
| 284 | uchar *name; |
| 285 | |
| 286 | if (argc < 2) { |
| 287 | return (EINVAL); |
| 288 | } |
| 289 | |
wdenk | 4a6fd34 | 2003-04-12 23:38:12 +0000 | [diff] [blame] | 290 | if (env_init ()) |
wdenk | 6aff311 | 2002-12-17 01:51:00 +0000 | [diff] [blame] | 291 | return (errno); |
| 292 | |
| 293 | name = argv[1]; |
| 294 | |
| 295 | /* |
| 296 | * search if variable with this name already exists |
| 297 | */ |
wdenk | 4a6fd34 | 2003-04-12 23:38:12 +0000 | [diff] [blame] | 298 | for (nxt = env = environment.data; *env; env = nxt + 1) { |
| 299 | for (nxt = env; *nxt; ++nxt) { |
wdenk | 6aff311 | 2002-12-17 01:51:00 +0000 | [diff] [blame] | 300 | if (nxt >= &environment.data[ENV_SIZE]) { |
| 301 | fprintf (stderr, "## Error: " |
| 302 | "environment not terminated\n"); |
| 303 | return (EINVAL); |
| 304 | } |
| 305 | } |
wdenk | 4a6fd34 | 2003-04-12 23:38:12 +0000 | [diff] [blame] | 306 | if ((oldval = envmatch (name, env)) != NULL) |
wdenk | 6aff311 | 2002-12-17 01:51:00 +0000 | [diff] [blame] | 307 | break; |
| 308 | } |
| 309 | |
| 310 | /* |
| 311 | * Delete any existing definition |
| 312 | */ |
| 313 | if (oldval) { |
| 314 | /* |
| 315 | * Ethernet Address and serial# can be set only once |
| 316 | */ |
| 317 | if ((strcmp (name, "ethaddr") == 0) || |
wdenk | 4a6fd34 | 2003-04-12 23:38:12 +0000 | [diff] [blame] | 318 | (strcmp (name, "serial#") == 0)) { |
wdenk | 6aff311 | 2002-12-17 01:51:00 +0000 | [diff] [blame] | 319 | fprintf (stderr, "Can't overwrite \"%s\"\n", name); |
| 320 | return (EROFS); |
| 321 | } |
| 322 | |
| 323 | if (*++nxt == '\0') { |
| 324 | *env = '\0'; |
| 325 | } else { |
| 326 | for (;;) { |
| 327 | *env = *nxt++; |
| 328 | if ((*env == '\0') && (*nxt == '\0')) |
| 329 | break; |
| 330 | ++env; |
| 331 | } |
| 332 | } |
| 333 | *++env = '\0'; |
| 334 | } |
| 335 | |
| 336 | /* Delete only ? */ |
| 337 | if (argc < 3) |
| 338 | goto WRITE_FLASH; |
| 339 | |
| 340 | /* |
| 341 | * Append new definition at the end |
| 342 | */ |
wdenk | 4a6fd34 | 2003-04-12 23:38:12 +0000 | [diff] [blame] | 343 | for (env = environment.data; *env || *(env + 1); ++env); |
wdenk | 6aff311 | 2002-12-17 01:51:00 +0000 | [diff] [blame] | 344 | if (env > environment.data) |
| 345 | ++env; |
| 346 | /* |
| 347 | * Overflow when: |
| 348 | * "name" + "=" + "val" +"\0\0" > CFG_ENV_SIZE - (env-environment) |
| 349 | */ |
wdenk | 4a6fd34 | 2003-04-12 23:38:12 +0000 | [diff] [blame] | 350 | len = strlen (name) + 2; |
wdenk | 6aff311 | 2002-12-17 01:51:00 +0000 | [diff] [blame] | 351 | /* add '=' for first arg, ' ' for all others */ |
wdenk | 4a6fd34 | 2003-04-12 23:38:12 +0000 | [diff] [blame] | 352 | for (i = 2; i < argc; ++i) { |
| 353 | len += strlen (argv[i]) + 1; |
wdenk | 6aff311 | 2002-12-17 01:51:00 +0000 | [diff] [blame] | 354 | } |
wdenk | 4a6fd34 | 2003-04-12 23:38:12 +0000 | [diff] [blame] | 355 | if (len > (&environment.data[ENV_SIZE] - env)) { |
wdenk | 6aff311 | 2002-12-17 01:51:00 +0000 | [diff] [blame] | 356 | fprintf (stderr, |
| 357 | "Error: environment overflow, \"%s\" deleted\n", |
| 358 | name); |
| 359 | return (-1); |
| 360 | } |
| 361 | while ((*env = *name++) != '\0') |
| 362 | env++; |
wdenk | 4a6fd34 | 2003-04-12 23:38:12 +0000 | [diff] [blame] | 363 | for (i = 2; i < argc; ++i) { |
wdenk | 6aff311 | 2002-12-17 01:51:00 +0000 | [diff] [blame] | 364 | uchar *val = argv[i]; |
| 365 | |
wdenk | 4a6fd34 | 2003-04-12 23:38:12 +0000 | [diff] [blame] | 366 | *env = (i == 2) ? '=' : ' '; |
| 367 | while ((*++env = *val++) != '\0'); |
wdenk | 6aff311 | 2002-12-17 01:51:00 +0000 | [diff] [blame] | 368 | } |
| 369 | |
| 370 | /* end is marked with double '\0' */ |
| 371 | *++env = '\0'; |
| 372 | |
wdenk | 4a6fd34 | 2003-04-12 23:38:12 +0000 | [diff] [blame] | 373 | WRITE_FLASH: |
wdenk | 6aff311 | 2002-12-17 01:51:00 +0000 | [diff] [blame] | 374 | |
| 375 | /* Update CRC */ |
wdenk | 4a6fd34 | 2003-04-12 23:38:12 +0000 | [diff] [blame] | 376 | environment.crc = crc32 (0, environment.data, ENV_SIZE); |
wdenk | 6aff311 | 2002-12-17 01:51:00 +0000 | [diff] [blame] | 377 | |
| 378 | /* write environment back to flash */ |
| 379 | if (flash_io (O_RDWR)) { |
wdenk | 4a6fd34 | 2003-04-12 23:38:12 +0000 | [diff] [blame] | 380 | fprintf (stderr, "Error: can't write fw_env to flash\n"); |
wdenk | 6aff311 | 2002-12-17 01:51:00 +0000 | [diff] [blame] | 381 | return (-1); |
| 382 | } |
| 383 | |
| 384 | return (0); |
| 385 | } |
| 386 | |
| 387 | static int flash_io (int mode) |
| 388 | { |
| 389 | int fd, fdr, rc, otherdev, len, resid; |
| 390 | erase_info_t erase; |
wdenk | 592c5ca | 2003-06-21 00:17:24 +0000 | [diff] [blame] | 391 | char *data = NULL; |
wdenk | 6aff311 | 2002-12-17 01:51:00 +0000 | [diff] [blame] | 392 | |
wdenk | 4a6fd34 | 2003-04-12 23:38:12 +0000 | [diff] [blame] | 393 | if ((fd = open (DEVNAME (curdev), mode)) < 0) { |
| 394 | fprintf (stderr, |
| 395 | "Can't open %s: %s\n", |
| 396 | DEVNAME (curdev), strerror (errno)); |
wdenk | 6aff311 | 2002-12-17 01:51:00 +0000 | [diff] [blame] | 397 | return (-1); |
| 398 | } |
| 399 | |
wdenk | 4a6fd34 | 2003-04-12 23:38:12 +0000 | [diff] [blame] | 400 | len = sizeof (environment.crc); |
wdenk | d0fb80c | 2003-01-11 09:48:40 +0000 | [diff] [blame] | 401 | if (HaveRedundEnv) { |
wdenk | 4a6fd34 | 2003-04-12 23:38:12 +0000 | [diff] [blame] | 402 | len += sizeof (environment.flags); |
wdenk | d0fb80c | 2003-01-11 09:48:40 +0000 | [diff] [blame] | 403 | } |
wdenk | 6aff311 | 2002-12-17 01:51:00 +0000 | [diff] [blame] | 404 | |
| 405 | if (mode == O_RDWR) { |
wdenk | d0fb80c | 2003-01-11 09:48:40 +0000 | [diff] [blame] | 406 | if (HaveRedundEnv) { |
| 407 | /* switch to next partition for writing */ |
| 408 | otherdev = !curdev; |
wdenk | 4a6fd34 | 2003-04-12 23:38:12 +0000 | [diff] [blame] | 409 | if ((fdr = open (DEVNAME (otherdev), mode)) < 0) { |
| 410 | fprintf (stderr, |
| 411 | "Can't open %s: %s\n", |
| 412 | DEVNAME (otherdev), |
| 413 | strerror (errno)); |
wdenk | d0fb80c | 2003-01-11 09:48:40 +0000 | [diff] [blame] | 414 | return (-1); |
| 415 | } |
| 416 | } else { |
| 417 | otherdev = curdev; |
| 418 | fdr = fd; |
wdenk | 6aff311 | 2002-12-17 01:51:00 +0000 | [diff] [blame] | 419 | } |
wdenk | 4a6fd34 | 2003-04-12 23:38:12 +0000 | [diff] [blame] | 420 | printf ("Unlocking flash...\n"); |
| 421 | erase.length = DEVESIZE (otherdev); |
| 422 | erase.start = DEVOFFSET (otherdev); |
wdenk | 6aff311 | 2002-12-17 01:51:00 +0000 | [diff] [blame] | 423 | ioctl (fdr, MEMUNLOCK, &erase); |
| 424 | |
wdenk | d0fb80c | 2003-01-11 09:48:40 +0000 | [diff] [blame] | 425 | if (HaveRedundEnv) { |
wdenk | 4a6fd34 | 2003-04-12 23:38:12 +0000 | [diff] [blame] | 426 | erase.length = DEVESIZE (curdev); |
| 427 | erase.start = DEVOFFSET (curdev); |
wdenk | d0fb80c | 2003-01-11 09:48:40 +0000 | [diff] [blame] | 428 | ioctl (fd, MEMUNLOCK, &erase); |
| 429 | environment.flags = active_flag; |
| 430 | } |
| 431 | |
wdenk | 4a6fd34 | 2003-04-12 23:38:12 +0000 | [diff] [blame] | 432 | printf ("Done\n"); |
| 433 | resid = DEVESIZE (otherdev) - CFG_ENV_SIZE; |
wdenk | 6aff311 | 2002-12-17 01:51:00 +0000 | [diff] [blame] | 434 | if (resid) { |
wdenk | 4a6fd34 | 2003-04-12 23:38:12 +0000 | [diff] [blame] | 435 | if ((data = malloc (resid)) == NULL) { |
| 436 | fprintf (stderr, |
| 437 | "Cannot malloc %d bytes: %s\n", |
| 438 | resid, |
| 439 | strerror (errno)); |
wdenk | 6aff311 | 2002-12-17 01:51:00 +0000 | [diff] [blame] | 440 | return (-1); |
| 441 | } |
wdenk | 4a6fd34 | 2003-04-12 23:38:12 +0000 | [diff] [blame] | 442 | if (lseek (fdr, DEVOFFSET (otherdev) + CFG_ENV_SIZE, SEEK_SET) |
| 443 | == -1) { |
| 444 | fprintf (stderr, "seek error on %s: %s\n", |
| 445 | DEVNAME (otherdev), |
| 446 | strerror (errno)); |
wdenk | 6aff311 | 2002-12-17 01:51:00 +0000 | [diff] [blame] | 447 | return (-1); |
| 448 | } |
| 449 | if ((rc = read (fdr, data, resid)) != resid) { |
| 450 | fprintf (stderr, |
wdenk | 4a6fd34 | 2003-04-12 23:38:12 +0000 | [diff] [blame] | 451 | "read error on %s: %s\n", |
| 452 | DEVNAME (otherdev), |
| 453 | strerror (errno)); |
wdenk | 6aff311 | 2002-12-17 01:51:00 +0000 | [diff] [blame] | 454 | return (-1); |
| 455 | } |
| 456 | } |
| 457 | |
wdenk | 4a6fd34 | 2003-04-12 23:38:12 +0000 | [diff] [blame] | 458 | printf ("Erasing old environment...\n"); |
wdenk | 6aff311 | 2002-12-17 01:51:00 +0000 | [diff] [blame] | 459 | |
wdenk | 4a6fd34 | 2003-04-12 23:38:12 +0000 | [diff] [blame] | 460 | erase.length = DEVESIZE (otherdev); |
| 461 | erase.start = DEVOFFSET (otherdev); |
wdenk | 6aff311 | 2002-12-17 01:51:00 +0000 | [diff] [blame] | 462 | if (ioctl (fdr, MEMERASE, &erase) != 0) { |
| 463 | fprintf (stderr, "MTD erase error on %s: %s\n", |
wdenk | 4a6fd34 | 2003-04-12 23:38:12 +0000 | [diff] [blame] | 464 | DEVNAME (otherdev), |
| 465 | strerror (errno)); |
wdenk | 6aff311 | 2002-12-17 01:51:00 +0000 | [diff] [blame] | 466 | return (-1); |
| 467 | } |
| 468 | |
wdenk | 4a6fd34 | 2003-04-12 23:38:12 +0000 | [diff] [blame] | 469 | printf ("Done\n"); |
wdenk | 6aff311 | 2002-12-17 01:51:00 +0000 | [diff] [blame] | 470 | |
wdenk | 4a6fd34 | 2003-04-12 23:38:12 +0000 | [diff] [blame] | 471 | printf ("Writing environment to %s...\n", DEVNAME (otherdev)); |
| 472 | if (lseek (fdr, DEVOFFSET (otherdev), SEEK_SET) == -1) { |
wdenk | d0fb80c | 2003-01-11 09:48:40 +0000 | [diff] [blame] | 473 | fprintf (stderr, |
wdenk | 4a6fd34 | 2003-04-12 23:38:12 +0000 | [diff] [blame] | 474 | "seek error on %s: %s\n", |
| 475 | DEVNAME (otherdev), strerror (errno)); |
wdenk | d0fb80c | 2003-01-11 09:48:40 +0000 | [diff] [blame] | 476 | return (-1); |
| 477 | } |
wdenk | 4a6fd34 | 2003-04-12 23:38:12 +0000 | [diff] [blame] | 478 | if (write (fdr, &environment, len) != len) { |
wdenk | 6aff311 | 2002-12-17 01:51:00 +0000 | [diff] [blame] | 479 | fprintf (stderr, |
wdenk | 4a6fd34 | 2003-04-12 23:38:12 +0000 | [diff] [blame] | 480 | "CRC write error on %s: %s\n", |
| 481 | DEVNAME (otherdev), strerror (errno)); |
wdenk | 6aff311 | 2002-12-17 01:51:00 +0000 | [diff] [blame] | 482 | return (-1); |
| 483 | } |
wdenk | 4a6fd34 | 2003-04-12 23:38:12 +0000 | [diff] [blame] | 484 | if (write (fdr, environment.data, ENV_SIZE) != ENV_SIZE) { |
wdenk | 6aff311 | 2002-12-17 01:51:00 +0000 | [diff] [blame] | 485 | fprintf (stderr, |
wdenk | 4a6fd34 | 2003-04-12 23:38:12 +0000 | [diff] [blame] | 486 | "Write error on %s: %s\n", |
| 487 | DEVNAME (otherdev), strerror (errno)); |
wdenk | 6aff311 | 2002-12-17 01:51:00 +0000 | [diff] [blame] | 488 | return (-1); |
| 489 | } |
| 490 | if (resid) { |
| 491 | if (write (fdr, data, resid) != resid) { |
| 492 | fprintf (stderr, |
wdenk | 4a6fd34 | 2003-04-12 23:38:12 +0000 | [diff] [blame] | 493 | "write error on %s: %s\n", |
| 494 | DEVNAME (curdev), strerror (errno)); |
wdenk | 6aff311 | 2002-12-17 01:51:00 +0000 | [diff] [blame] | 495 | return (-1); |
| 496 | } |
wdenk | 4a6fd34 | 2003-04-12 23:38:12 +0000 | [diff] [blame] | 497 | free (data); |
wdenk | 6aff311 | 2002-12-17 01:51:00 +0000 | [diff] [blame] | 498 | } |
wdenk | d0fb80c | 2003-01-11 09:48:40 +0000 | [diff] [blame] | 499 | if (HaveRedundEnv) { |
| 500 | /* change flag on current active env partition */ |
wdenk | 4a6fd34 | 2003-04-12 23:38:12 +0000 | [diff] [blame] | 501 | if (lseek (fd, DEVOFFSET (curdev) + sizeof (ulong), SEEK_SET) |
| 502 | == -1) { |
| 503 | fprintf (stderr, "seek error on %s: %s\n", |
| 504 | DEVNAME (curdev), strerror (errno)); |
wdenk | d0fb80c | 2003-01-11 09:48:40 +0000 | [diff] [blame] | 505 | return (-1); |
| 506 | } |
wdenk | 4a6fd34 | 2003-04-12 23:38:12 +0000 | [diff] [blame] | 507 | if (write (fd, &obsolete_flag, sizeof (obsolete_flag)) != |
| 508 | sizeof (obsolete_flag)) { |
wdenk | d0fb80c | 2003-01-11 09:48:40 +0000 | [diff] [blame] | 509 | fprintf (stderr, |
wdenk | 4a6fd34 | 2003-04-12 23:38:12 +0000 | [diff] [blame] | 510 | "Write error on %s: %s\n", |
| 511 | DEVNAME (curdev), strerror (errno)); |
wdenk | d0fb80c | 2003-01-11 09:48:40 +0000 | [diff] [blame] | 512 | return (-1); |
| 513 | } |
wdenk | 6aff311 | 2002-12-17 01:51:00 +0000 | [diff] [blame] | 514 | } |
wdenk | 4a6fd34 | 2003-04-12 23:38:12 +0000 | [diff] [blame] | 515 | printf ("Done\n"); |
| 516 | printf ("Locking ...\n"); |
| 517 | erase.length = DEVESIZE (otherdev); |
| 518 | erase.start = DEVOFFSET (otherdev); |
wdenk | 6aff311 | 2002-12-17 01:51:00 +0000 | [diff] [blame] | 519 | ioctl (fdr, MEMLOCK, &erase); |
wdenk | d0fb80c | 2003-01-11 09:48:40 +0000 | [diff] [blame] | 520 | if (HaveRedundEnv) { |
wdenk | 4a6fd34 | 2003-04-12 23:38:12 +0000 | [diff] [blame] | 521 | erase.length = DEVESIZE (curdev); |
| 522 | erase.start = DEVOFFSET (curdev); |
wdenk | d0fb80c | 2003-01-11 09:48:40 +0000 | [diff] [blame] | 523 | ioctl (fd, MEMLOCK, &erase); |
wdenk | 4a6fd34 | 2003-04-12 23:38:12 +0000 | [diff] [blame] | 524 | if (close (fdr)) { |
wdenk | d0fb80c | 2003-01-11 09:48:40 +0000 | [diff] [blame] | 525 | fprintf (stderr, |
wdenk | 4a6fd34 | 2003-04-12 23:38:12 +0000 | [diff] [blame] | 526 | "I/O error on %s: %s\n", |
| 527 | DEVNAME (otherdev), |
| 528 | strerror (errno)); |
wdenk | d0fb80c | 2003-01-11 09:48:40 +0000 | [diff] [blame] | 529 | return (-1); |
| 530 | } |
wdenk | 6aff311 | 2002-12-17 01:51:00 +0000 | [diff] [blame] | 531 | } |
wdenk | 4a6fd34 | 2003-04-12 23:38:12 +0000 | [diff] [blame] | 532 | printf ("Done\n"); |
wdenk | 6aff311 | 2002-12-17 01:51:00 +0000 | [diff] [blame] | 533 | } else { |
wdenk | d0fb80c | 2003-01-11 09:48:40 +0000 | [diff] [blame] | 534 | |
wdenk | 4a6fd34 | 2003-04-12 23:38:12 +0000 | [diff] [blame] | 535 | if (lseek (fd, DEVOFFSET (curdev), SEEK_SET) == -1) { |
wdenk | d0fb80c | 2003-01-11 09:48:40 +0000 | [diff] [blame] | 536 | fprintf (stderr, |
wdenk | 4a6fd34 | 2003-04-12 23:38:12 +0000 | [diff] [blame] | 537 | "seek error on %s: %s\n", |
| 538 | DEVNAME (curdev), strerror (errno)); |
wdenk | d0fb80c | 2003-01-11 09:48:40 +0000 | [diff] [blame] | 539 | return (-1); |
| 540 | } |
wdenk | 6aff311 | 2002-12-17 01:51:00 +0000 | [diff] [blame] | 541 | if (read (fd, &environment, len) != len) { |
| 542 | fprintf (stderr, |
wdenk | 4a6fd34 | 2003-04-12 23:38:12 +0000 | [diff] [blame] | 543 | "CRC read error on %s: %s\n", |
| 544 | DEVNAME (curdev), strerror (errno)); |
wdenk | 6aff311 | 2002-12-17 01:51:00 +0000 | [diff] [blame] | 545 | return (-1); |
| 546 | } |
| 547 | if ((rc = read (fd, environment.data, ENV_SIZE)) != ENV_SIZE) { |
| 548 | fprintf (stderr, |
wdenk | 4a6fd34 | 2003-04-12 23:38:12 +0000 | [diff] [blame] | 549 | "Read error on %s: %s\n", |
| 550 | DEVNAME (curdev), strerror (errno)); |
wdenk | 6aff311 | 2002-12-17 01:51:00 +0000 | [diff] [blame] | 551 | return (-1); |
| 552 | } |
| 553 | } |
| 554 | |
wdenk | 4a6fd34 | 2003-04-12 23:38:12 +0000 | [diff] [blame] | 555 | if (close (fd)) { |
wdenk | 6aff311 | 2002-12-17 01:51:00 +0000 | [diff] [blame] | 556 | fprintf (stderr, |
wdenk | 4a6fd34 | 2003-04-12 23:38:12 +0000 | [diff] [blame] | 557 | "I/O error on %s: %s\n", |
| 558 | DEVNAME (curdev), strerror (errno)); |
wdenk | 6aff311 | 2002-12-17 01:51:00 +0000 | [diff] [blame] | 559 | return (-1); |
| 560 | } |
| 561 | |
| 562 | /* everything ok */ |
| 563 | return (0); |
| 564 | } |
| 565 | |
| 566 | /* |
| 567 | * s1 is either a simple 'name', or a 'name=value' pair. |
| 568 | * s2 is a 'name=value' pair. |
| 569 | * If the names match, return the value of s2, else NULL. |
| 570 | */ |
| 571 | |
wdenk | 4a6fd34 | 2003-04-12 23:38:12 +0000 | [diff] [blame] | 572 | static uchar *envmatch (uchar * s1, uchar * s2) |
wdenk | 6aff311 | 2002-12-17 01:51:00 +0000 | [diff] [blame] | 573 | { |
| 574 | |
| 575 | while (*s1 == *s2++) |
| 576 | if (*s1++ == '=') |
wdenk | 4a6fd34 | 2003-04-12 23:38:12 +0000 | [diff] [blame] | 577 | return (s2); |
| 578 | if (*s1 == '\0' && *(s2 - 1) == '=') |
| 579 | return (s2); |
| 580 | return (NULL); |
wdenk | 6aff311 | 2002-12-17 01:51:00 +0000 | [diff] [blame] | 581 | } |
| 582 | |
| 583 | /* |
| 584 | * Prevent confusion if running from erased flash memory |
| 585 | */ |
wdenk | 4a6fd34 | 2003-04-12 23:38:12 +0000 | [diff] [blame] | 586 | static int env_init (void) |
wdenk | 6aff311 | 2002-12-17 01:51:00 +0000 | [diff] [blame] | 587 | { |
| 588 | int crc1, crc1_ok; |
| 589 | uchar *addr1; |
wdenk | d0fb80c | 2003-01-11 09:48:40 +0000 | [diff] [blame] | 590 | |
wdenk | 6aff311 | 2002-12-17 01:51:00 +0000 | [diff] [blame] | 591 | int crc2, crc2_ok; |
| 592 | uchar flag1, flag2, *addr2; |
wdenk | 6aff311 | 2002-12-17 01:51:00 +0000 | [diff] [blame] | 593 | |
wdenk | 4a6fd34 | 2003-04-12 23:38:12 +0000 | [diff] [blame] | 594 | if (parse_config ()) /* should fill envdevices */ |
wdenk | d0fb80c | 2003-01-11 09:48:40 +0000 | [diff] [blame] | 595 | return 1; |
wdenk | 4a6fd34 | 2003-04-12 23:38:12 +0000 | [diff] [blame] | 596 | |
wdenk | d0fb80c | 2003-01-11 09:48:40 +0000 | [diff] [blame] | 597 | if ((addr1 = calloc (1, ENV_SIZE)) == NULL) { |
wdenk | 4a6fd34 | 2003-04-12 23:38:12 +0000 | [diff] [blame] | 598 | fprintf (stderr, |
| 599 | "Not enough memory for environment (%ld bytes)\n", |
| 600 | ENV_SIZE); |
wdenk | d0fb80c | 2003-01-11 09:48:40 +0000 | [diff] [blame] | 601 | return (errno); |
| 602 | } |
wdenk | 4a6fd34 | 2003-04-12 23:38:12 +0000 | [diff] [blame] | 603 | |
wdenk | d0fb80c | 2003-01-11 09:48:40 +0000 | [diff] [blame] | 604 | /* read environment from FLASH to local buffer */ |
| 605 | environment.data = addr1; |
| 606 | curdev = 0; |
| 607 | if (flash_io (O_RDONLY)) { |
| 608 | return (errno); |
| 609 | } |
wdenk | 4a6fd34 | 2003-04-12 23:38:12 +0000 | [diff] [blame] | 610 | |
| 611 | crc1_ok = ((crc1 = crc32 (0, environment.data, ENV_SIZE)) |
wdenk | d0fb80c | 2003-01-11 09:48:40 +0000 | [diff] [blame] | 612 | == environment.crc); |
| 613 | if (!HaveRedundEnv) { |
wdenk | 6aff311 | 2002-12-17 01:51:00 +0000 | [diff] [blame] | 614 | if (!crc1_ok) { |
wdenk | 4a6fd34 | 2003-04-12 23:38:12 +0000 | [diff] [blame] | 615 | fprintf (stderr, |
| 616 | "Warning: Bad CRC, using default environment\n"); |
Wolfgang Denk | f07217c | 2006-03-12 23:27:46 +0100 | [diff] [blame] | 617 | memcpy(environment.data, default_environment, sizeof default_environment); |
wdenk | 6aff311 | 2002-12-17 01:51:00 +0000 | [diff] [blame] | 618 | } |
wdenk | d0fb80c | 2003-01-11 09:48:40 +0000 | [diff] [blame] | 619 | } else { |
wdenk | 6aff311 | 2002-12-17 01:51:00 +0000 | [diff] [blame] | 620 | flag1 = environment.flags; |
wdenk | 4a6fd34 | 2003-04-12 23:38:12 +0000 | [diff] [blame] | 621 | |
wdenk | 6aff311 | 2002-12-17 01:51:00 +0000 | [diff] [blame] | 622 | curdev = 1; |
| 623 | if ((addr2 = calloc (1, ENV_SIZE)) == NULL) { |
wdenk | 4a6fd34 | 2003-04-12 23:38:12 +0000 | [diff] [blame] | 624 | fprintf (stderr, |
| 625 | "Not enough memory for environment (%ld bytes)\n", |
| 626 | ENV_SIZE); |
wdenk | 6aff311 | 2002-12-17 01:51:00 +0000 | [diff] [blame] | 627 | return (errno); |
wdenk | 4a6fd34 | 2003-04-12 23:38:12 +0000 | [diff] [blame] | 628 | } |
wdenk | 6aff311 | 2002-12-17 01:51:00 +0000 | [diff] [blame] | 629 | environment.data = addr2; |
wdenk | 4a6fd34 | 2003-04-12 23:38:12 +0000 | [diff] [blame] | 630 | |
wdenk | 6aff311 | 2002-12-17 01:51:00 +0000 | [diff] [blame] | 631 | if (flash_io (O_RDONLY)) { |
| 632 | return (errno); |
| 633 | } |
wdenk | 4a6fd34 | 2003-04-12 23:38:12 +0000 | [diff] [blame] | 634 | |
| 635 | crc2_ok = ((crc2 = crc32 (0, environment.data, ENV_SIZE)) |
wdenk | 6aff311 | 2002-12-17 01:51:00 +0000 | [diff] [blame] | 636 | == environment.crc); |
| 637 | flag2 = environment.flags; |
wdenk | 4a6fd34 | 2003-04-12 23:38:12 +0000 | [diff] [blame] | 638 | |
| 639 | if (crc1_ok && !crc2_ok) { |
| 640 | environment.data = addr1; |
wdenk | 6aff311 | 2002-12-17 01:51:00 +0000 | [diff] [blame] | 641 | environment.flags = flag1; |
| 642 | environment.crc = crc1; |
| 643 | curdev = 0; |
wdenk | 4a6fd34 | 2003-04-12 23:38:12 +0000 | [diff] [blame] | 644 | free (addr2); |
| 645 | } else if (!crc1_ok && crc2_ok) { |
| 646 | environment.data = addr2; |
wdenk | 6aff311 | 2002-12-17 01:51:00 +0000 | [diff] [blame] | 647 | environment.flags = flag2; |
| 648 | environment.crc = crc2; |
| 649 | curdev = 1; |
wdenk | 4a6fd34 | 2003-04-12 23:38:12 +0000 | [diff] [blame] | 650 | free (addr1); |
| 651 | } else if (!crc1_ok && !crc2_ok) { |
| 652 | fprintf (stderr, |
| 653 | "Warning: Bad CRC, using default environment\n"); |
Wolfgang Denk | f07217c | 2006-03-12 23:27:46 +0100 | [diff] [blame] | 654 | memcpy(environment.data, default_environment, sizeof default_environment); |
wdenk | 6aff311 | 2002-12-17 01:51:00 +0000 | [diff] [blame] | 655 | curdev = 0; |
wdenk | 4a6fd34 | 2003-04-12 23:38:12 +0000 | [diff] [blame] | 656 | free (addr1); |
| 657 | } else if (flag1 == active_flag && flag2 == obsolete_flag) { |
| 658 | environment.data = addr1; |
wdenk | 6aff311 | 2002-12-17 01:51:00 +0000 | [diff] [blame] | 659 | environment.flags = flag1; |
| 660 | environment.crc = crc1; |
| 661 | curdev = 0; |
wdenk | 4a6fd34 | 2003-04-12 23:38:12 +0000 | [diff] [blame] | 662 | free (addr2); |
| 663 | } else if (flag1 == obsolete_flag && flag2 == active_flag) { |
| 664 | environment.data = addr2; |
wdenk | 6aff311 | 2002-12-17 01:51:00 +0000 | [diff] [blame] | 665 | environment.flags = flag2; |
| 666 | environment.crc = crc2; |
| 667 | curdev = 1; |
wdenk | 4a6fd34 | 2003-04-12 23:38:12 +0000 | [diff] [blame] | 668 | free (addr1); |
| 669 | } else if (flag1 == flag2) { |
| 670 | environment.data = addr1; |
wdenk | 6aff311 | 2002-12-17 01:51:00 +0000 | [diff] [blame] | 671 | environment.flags = flag1; |
| 672 | environment.crc = crc1; |
| 673 | curdev = 0; |
wdenk | 4a6fd34 | 2003-04-12 23:38:12 +0000 | [diff] [blame] | 674 | free (addr2); |
| 675 | } else if (flag1 == 0xFF) { |
| 676 | environment.data = addr1; |
wdenk | 6aff311 | 2002-12-17 01:51:00 +0000 | [diff] [blame] | 677 | environment.flags = flag1; |
| 678 | environment.crc = crc1; |
| 679 | curdev = 0; |
wdenk | 4a6fd34 | 2003-04-12 23:38:12 +0000 | [diff] [blame] | 680 | free (addr2); |
| 681 | } else if (flag2 == 0xFF) { |
| 682 | environment.data = addr2; |
wdenk | 6aff311 | 2002-12-17 01:51:00 +0000 | [diff] [blame] | 683 | environment.flags = flag2; |
| 684 | environment.crc = crc2; |
| 685 | curdev = 1; |
wdenk | 4a6fd34 | 2003-04-12 23:38:12 +0000 | [diff] [blame] | 686 | free (addr1); |
wdenk | 6aff311 | 2002-12-17 01:51:00 +0000 | [diff] [blame] | 687 | } |
wdenk | 6aff311 | 2002-12-17 01:51:00 +0000 | [diff] [blame] | 688 | } |
| 689 | return (0); |
| 690 | } |
| 691 | |
| 692 | |
wdenk | 4a6fd34 | 2003-04-12 23:38:12 +0000 | [diff] [blame] | 693 | static int parse_config () |
wdenk | 6aff311 | 2002-12-17 01:51:00 +0000 | [diff] [blame] | 694 | { |
| 695 | struct stat st; |
| 696 | |
wdenk | d0fb80c | 2003-01-11 09:48:40 +0000 | [diff] [blame] | 697 | #if defined(CONFIG_FILE) |
| 698 | /* Fills in DEVNAME(), ENVSIZE(), DEVESIZE(). Or don't. */ |
wdenk | 4a6fd34 | 2003-04-12 23:38:12 +0000 | [diff] [blame] | 699 | if (get_config (CONFIG_FILE)) { |
wdenk | d0fb80c | 2003-01-11 09:48:40 +0000 | [diff] [blame] | 700 | fprintf (stderr, |
wdenk | 4a6fd34 | 2003-04-12 23:38:12 +0000 | [diff] [blame] | 701 | "Cannot parse config file: %s\n", strerror (errno)); |
wdenk | 6aff311 | 2002-12-17 01:51:00 +0000 | [diff] [blame] | 702 | return 1; |
| 703 | } |
wdenk | d0fb80c | 2003-01-11 09:48:40 +0000 | [diff] [blame] | 704 | #else |
wdenk | 4a6fd34 | 2003-04-12 23:38:12 +0000 | [diff] [blame] | 705 | strcpy (DEVNAME (0), DEVICE1_NAME); |
| 706 | DEVOFFSET (0) = DEVICE1_OFFSET; |
| 707 | ENVSIZE (0) = ENV1_SIZE; |
| 708 | DEVESIZE (0) = DEVICE1_ESIZE; |
wdenk | 6aff311 | 2002-12-17 01:51:00 +0000 | [diff] [blame] | 709 | #ifdef HAVE_REDUND |
wdenk | 4a6fd34 | 2003-04-12 23:38:12 +0000 | [diff] [blame] | 710 | strcpy (DEVNAME (1), DEVICE2_NAME); |
| 711 | DEVOFFSET (1) = DEVICE2_OFFSET; |
| 712 | ENVSIZE (1) = ENV2_SIZE; |
| 713 | DEVESIZE (1) = DEVICE2_ESIZE; |
wdenk | d0fb80c | 2003-01-11 09:48:40 +0000 | [diff] [blame] | 714 | HaveRedundEnv = 1; |
wdenk | 6aff311 | 2002-12-17 01:51:00 +0000 | [diff] [blame] | 715 | #endif |
wdenk | d0fb80c | 2003-01-11 09:48:40 +0000 | [diff] [blame] | 716 | #endif |
wdenk | 4a6fd34 | 2003-04-12 23:38:12 +0000 | [diff] [blame] | 717 | if (stat (DEVNAME (0), &st)) { |
| 718 | fprintf (stderr, |
| 719 | "Cannot access MTD device %s: %s\n", |
| 720 | DEVNAME (0), strerror (errno)); |
wdenk | d0fb80c | 2003-01-11 09:48:40 +0000 | [diff] [blame] | 721 | return 1; |
| 722 | } |
wdenk | 4a6fd34 | 2003-04-12 23:38:12 +0000 | [diff] [blame] | 723 | |
| 724 | if (HaveRedundEnv && stat (DEVNAME (1), &st)) { |
| 725 | fprintf (stderr, |
| 726 | "Cannot access MTD device %s: %s\n", |
Wolfgang Denk | e2146b6 | 2005-09-25 01:20:38 +0200 | [diff] [blame] | 727 | DEVNAME (1), strerror (errno)); |
wdenk | d0fb80c | 2003-01-11 09:48:40 +0000 | [diff] [blame] | 728 | return 1; |
| 729 | } |
wdenk | 6aff311 | 2002-12-17 01:51:00 +0000 | [diff] [blame] | 730 | return 0; |
| 731 | } |
wdenk | d0fb80c | 2003-01-11 09:48:40 +0000 | [diff] [blame] | 732 | |
| 733 | #if defined(CONFIG_FILE) |
| 734 | static int get_config (char *fname) |
| 735 | { |
| 736 | FILE *fp; |
| 737 | int i = 0; |
| 738 | int rc; |
| 739 | char dump[128]; |
| 740 | |
wdenk | 4a6fd34 | 2003-04-12 23:38:12 +0000 | [diff] [blame] | 741 | if ((fp = fopen (fname, "r")) == NULL) { |
wdenk | d0fb80c | 2003-01-11 09:48:40 +0000 | [diff] [blame] | 742 | return 1; |
| 743 | } |
| 744 | |
wdenk | 4a6fd34 | 2003-04-12 23:38:12 +0000 | [diff] [blame] | 745 | while ((i < 2) && ((rc = fscanf (fp, "%s %lx %lx %lx", |
| 746 | DEVNAME (i), |
| 747 | &DEVOFFSET (i), |
| 748 | &ENVSIZE (i), |
| 749 | &DEVESIZE (i) )) != EOF)) { |
wdenk | d0fb80c | 2003-01-11 09:48:40 +0000 | [diff] [blame] | 750 | |
| 751 | /* Skip incomplete conversions and comment strings */ |
wdenk | 4a6fd34 | 2003-04-12 23:38:12 +0000 | [diff] [blame] | 752 | if ((rc < 3) || (*DEVNAME (i) == '#')) { |
| 753 | fgets (dump, sizeof (dump), fp); /* Consume till end */ |
wdenk | d0fb80c | 2003-01-11 09:48:40 +0000 | [diff] [blame] | 754 | continue; |
| 755 | } |
| 756 | |
| 757 | i++; |
| 758 | } |
wdenk | 4a6fd34 | 2003-04-12 23:38:12 +0000 | [diff] [blame] | 759 | fclose (fp); |
| 760 | |
wdenk | d0fb80c | 2003-01-11 09:48:40 +0000 | [diff] [blame] | 761 | HaveRedundEnv = i - 1; |
wdenk | 4a6fd34 | 2003-04-12 23:38:12 +0000 | [diff] [blame] | 762 | if (!i) { /* No valid entries found */ |
wdenk | d0fb80c | 2003-01-11 09:48:40 +0000 | [diff] [blame] | 763 | errno = EINVAL; |
| 764 | return 1; |
| 765 | } else |
| 766 | return 0; |
| 767 | } |
| 768 | #endif |