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