wdenk | 6aff311 | 2002-12-17 01:51:00 +0000 | [diff] [blame] | 1 | /* |
Grant Erickson | bc11756 | 2008-05-06 20:16:15 -0700 | [diff] [blame] | 2 | * (C) Copyright 2000-2008 |
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 | */ |
Grant Erickson | bc11756 | 2008-05-06 20:16:15 -0700 | [diff] [blame] | 212 | int 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; |
Grant Erickson | bc11756 | 2008-05-06 20:16:15 -0700 | [diff] [blame] | 216 | int rc = 0; |
wdenk | 6aff311 | 2002-12-17 01:51:00 +0000 | [diff] [blame] | 217 | |
wdenk | 4a6fd34 | 2003-04-12 23:38:12 +0000 | [diff] [blame] | 218 | if (env_init ()) |
Grant Erickson | bc11756 | 2008-05-06 20:16:15 -0700 | [diff] [blame] | 219 | return (-1); |
wdenk | 6aff311 | 2002-12-17 01:51:00 +0000 | [diff] [blame] | 220 | |
wdenk | 4a6fd34 | 2003-04-12 23:38:12 +0000 | [diff] [blame] | 221 | if (argc == 1) { /* Print all env variables */ |
| 222 | for (env = environment.data; *env; env = nxt + 1) { |
| 223 | for (nxt = env; *nxt; ++nxt) { |
wdenk | 6aff311 | 2002-12-17 01:51:00 +0000 | [diff] [blame] | 224 | if (nxt >= &environment.data[ENV_SIZE]) { |
| 225 | fprintf (stderr, "## Error: " |
| 226 | "environment not terminated\n"); |
Grant Erickson | bc11756 | 2008-05-06 20:16:15 -0700 | [diff] [blame] | 227 | return (-1); |
wdenk | 6aff311 | 2002-12-17 01:51:00 +0000 | [diff] [blame] | 228 | } |
| 229 | } |
| 230 | |
wdenk | 4a6fd34 | 2003-04-12 23:38:12 +0000 | [diff] [blame] | 231 | printf ("%s\n", env); |
wdenk | 6aff311 | 2002-12-17 01:51:00 +0000 | [diff] [blame] | 232 | } |
Grant Erickson | bc11756 | 2008-05-06 20:16:15 -0700 | [diff] [blame] | 233 | return (0); |
wdenk | 6aff311 | 2002-12-17 01:51:00 +0000 | [diff] [blame] | 234 | } |
| 235 | |
wdenk | 4a6fd34 | 2003-04-12 23:38:12 +0000 | [diff] [blame] | 236 | if (strcmp (argv[1], "-n") == 0) { |
wdenk | 6aff311 | 2002-12-17 01:51:00 +0000 | [diff] [blame] | 237 | n_flag = 1; |
| 238 | ++argv; |
| 239 | --argc; |
| 240 | if (argc != 2) { |
| 241 | fprintf (stderr, "## Error: " |
| 242 | "`-n' option requires exactly one argument\n"); |
Grant Erickson | bc11756 | 2008-05-06 20:16:15 -0700 | [diff] [blame] | 243 | return (-1); |
wdenk | 6aff311 | 2002-12-17 01:51:00 +0000 | [diff] [blame] | 244 | } |
| 245 | } else { |
| 246 | n_flag = 0; |
| 247 | } |
| 248 | |
wdenk | 4a6fd34 | 2003-04-12 23:38:12 +0000 | [diff] [blame] | 249 | for (i = 1; i < argc; ++i) { /* print single env variables */ |
Markus Klotzbücher | 6de66b3 | 2007-11-27 10:23:20 +0100 | [diff] [blame] | 250 | char *name = argv[i]; |
| 251 | char *val = NULL; |
wdenk | 6aff311 | 2002-12-17 01:51:00 +0000 | [diff] [blame] | 252 | |
wdenk | 4a6fd34 | 2003-04-12 23:38:12 +0000 | [diff] [blame] | 253 | for (env = environment.data; *env; env = nxt + 1) { |
wdenk | 6aff311 | 2002-12-17 01:51:00 +0000 | [diff] [blame] | 254 | |
wdenk | 4a6fd34 | 2003-04-12 23:38:12 +0000 | [diff] [blame] | 255 | for (nxt = env; *nxt; ++nxt) { |
wdenk | 6aff311 | 2002-12-17 01:51:00 +0000 | [diff] [blame] | 256 | if (nxt >= &environment.data[ENV_SIZE]) { |
| 257 | fprintf (stderr, "## Error: " |
| 258 | "environment not terminated\n"); |
Grant Erickson | bc11756 | 2008-05-06 20:16:15 -0700 | [diff] [blame] | 259 | return (-1); |
wdenk | 6aff311 | 2002-12-17 01:51:00 +0000 | [diff] [blame] | 260 | } |
| 261 | } |
wdenk | 4a6fd34 | 2003-04-12 23:38:12 +0000 | [diff] [blame] | 262 | val = envmatch (name, env); |
wdenk | 6aff311 | 2002-12-17 01:51:00 +0000 | [diff] [blame] | 263 | if (val) { |
| 264 | if (!n_flag) { |
| 265 | fputs (name, stdout); |
wdenk | 4a6fd34 | 2003-04-12 23:38:12 +0000 | [diff] [blame] | 266 | putc ('=', stdout); |
wdenk | 6aff311 | 2002-12-17 01:51:00 +0000 | [diff] [blame] | 267 | } |
wdenk | 4a6fd34 | 2003-04-12 23:38:12 +0000 | [diff] [blame] | 268 | puts (val); |
wdenk | 6aff311 | 2002-12-17 01:51:00 +0000 | [diff] [blame] | 269 | break; |
| 270 | } |
| 271 | } |
Grant Erickson | bc11756 | 2008-05-06 20:16:15 -0700 | [diff] [blame] | 272 | if (!val) { |
wdenk | 4a6fd34 | 2003-04-12 23:38:12 +0000 | [diff] [blame] | 273 | fprintf (stderr, "## Error: \"%s\" not defined\n", name); |
Grant Erickson | bc11756 | 2008-05-06 20:16:15 -0700 | [diff] [blame] | 274 | rc = -1; |
| 275 | } |
wdenk | 6aff311 | 2002-12-17 01:51:00 +0000 | [diff] [blame] | 276 | } |
Grant Erickson | bc11756 | 2008-05-06 20:16:15 -0700 | [diff] [blame] | 277 | |
| 278 | return (rc); |
wdenk | 6aff311 | 2002-12-17 01:51:00 +0000 | [diff] [blame] | 279 | } |
| 280 | |
| 281 | /* |
| 282 | * Deletes or sets environment variables. Returns errno style error codes: |
| 283 | * 0 - OK |
| 284 | * EINVAL - need at least 1 argument |
| 285 | * EROFS - certain variables ("ethaddr", "serial#") cannot be |
| 286 | * modified or deleted |
| 287 | * |
| 288 | */ |
| 289 | int fw_setenv (int argc, char *argv[]) |
| 290 | { |
wdenk | 4a6fd34 | 2003-04-12 23:38:12 +0000 | [diff] [blame] | 291 | int i, len; |
Markus Klotzbücher | 6de66b3 | 2007-11-27 10:23:20 +0100 | [diff] [blame] | 292 | char *env, *nxt; |
| 293 | char *oldval = NULL; |
| 294 | char *name; |
wdenk | 6aff311 | 2002-12-17 01:51:00 +0000 | [diff] [blame] | 295 | |
| 296 | if (argc < 2) { |
| 297 | return (EINVAL); |
| 298 | } |
| 299 | |
wdenk | 4a6fd34 | 2003-04-12 23:38:12 +0000 | [diff] [blame] | 300 | if (env_init ()) |
wdenk | 6aff311 | 2002-12-17 01:51:00 +0000 | [diff] [blame] | 301 | return (errno); |
| 302 | |
| 303 | name = argv[1]; |
| 304 | |
| 305 | /* |
| 306 | * search if variable with this name already exists |
| 307 | */ |
wdenk | 4a6fd34 | 2003-04-12 23:38:12 +0000 | [diff] [blame] | 308 | for (nxt = env = environment.data; *env; env = nxt + 1) { |
| 309 | for (nxt = env; *nxt; ++nxt) { |
wdenk | 6aff311 | 2002-12-17 01:51:00 +0000 | [diff] [blame] | 310 | if (nxt >= &environment.data[ENV_SIZE]) { |
| 311 | fprintf (stderr, "## Error: " |
| 312 | "environment not terminated\n"); |
| 313 | return (EINVAL); |
| 314 | } |
| 315 | } |
wdenk | 4a6fd34 | 2003-04-12 23:38:12 +0000 | [diff] [blame] | 316 | if ((oldval = envmatch (name, env)) != NULL) |
wdenk | 6aff311 | 2002-12-17 01:51:00 +0000 | [diff] [blame] | 317 | break; |
| 318 | } |
| 319 | |
| 320 | /* |
| 321 | * Delete any existing definition |
| 322 | */ |
| 323 | if (oldval) { |
| 324 | /* |
| 325 | * Ethernet Address and serial# can be set only once |
| 326 | */ |
| 327 | if ((strcmp (name, "ethaddr") == 0) || |
wdenk | 4a6fd34 | 2003-04-12 23:38:12 +0000 | [diff] [blame] | 328 | (strcmp (name, "serial#") == 0)) { |
wdenk | 6aff311 | 2002-12-17 01:51:00 +0000 | [diff] [blame] | 329 | fprintf (stderr, "Can't overwrite \"%s\"\n", name); |
| 330 | return (EROFS); |
| 331 | } |
| 332 | |
| 333 | if (*++nxt == '\0') { |
| 334 | *env = '\0'; |
| 335 | } else { |
| 336 | for (;;) { |
| 337 | *env = *nxt++; |
| 338 | if ((*env == '\0') && (*nxt == '\0')) |
| 339 | break; |
| 340 | ++env; |
| 341 | } |
| 342 | } |
| 343 | *++env = '\0'; |
| 344 | } |
| 345 | |
| 346 | /* Delete only ? */ |
| 347 | if (argc < 3) |
| 348 | goto WRITE_FLASH; |
| 349 | |
| 350 | /* |
| 351 | * Append new definition at the end |
| 352 | */ |
wdenk | 4a6fd34 | 2003-04-12 23:38:12 +0000 | [diff] [blame] | 353 | for (env = environment.data; *env || *(env + 1); ++env); |
wdenk | 6aff311 | 2002-12-17 01:51:00 +0000 | [diff] [blame] | 354 | if (env > environment.data) |
| 355 | ++env; |
| 356 | /* |
| 357 | * Overflow when: |
| 358 | * "name" + "=" + "val" +"\0\0" > CFG_ENV_SIZE - (env-environment) |
| 359 | */ |
wdenk | 4a6fd34 | 2003-04-12 23:38:12 +0000 | [diff] [blame] | 360 | len = strlen (name) + 2; |
wdenk | 6aff311 | 2002-12-17 01:51:00 +0000 | [diff] [blame] | 361 | /* add '=' for first arg, ' ' for all others */ |
wdenk | 4a6fd34 | 2003-04-12 23:38:12 +0000 | [diff] [blame] | 362 | for (i = 2; i < argc; ++i) { |
| 363 | len += strlen (argv[i]) + 1; |
wdenk | 6aff311 | 2002-12-17 01:51:00 +0000 | [diff] [blame] | 364 | } |
wdenk | 4a6fd34 | 2003-04-12 23:38:12 +0000 | [diff] [blame] | 365 | if (len > (&environment.data[ENV_SIZE] - env)) { |
wdenk | 6aff311 | 2002-12-17 01:51:00 +0000 | [diff] [blame] | 366 | fprintf (stderr, |
| 367 | "Error: environment overflow, \"%s\" deleted\n", |
| 368 | name); |
| 369 | return (-1); |
| 370 | } |
| 371 | while ((*env = *name++) != '\0') |
| 372 | env++; |
wdenk | 4a6fd34 | 2003-04-12 23:38:12 +0000 | [diff] [blame] | 373 | for (i = 2; i < argc; ++i) { |
Markus Klotzbücher | 6de66b3 | 2007-11-27 10:23:20 +0100 | [diff] [blame] | 374 | char *val = argv[i]; |
wdenk | 6aff311 | 2002-12-17 01:51:00 +0000 | [diff] [blame] | 375 | |
wdenk | 4a6fd34 | 2003-04-12 23:38:12 +0000 | [diff] [blame] | 376 | *env = (i == 2) ? '=' : ' '; |
| 377 | while ((*++env = *val++) != '\0'); |
wdenk | 6aff311 | 2002-12-17 01:51:00 +0000 | [diff] [blame] | 378 | } |
| 379 | |
| 380 | /* end is marked with double '\0' */ |
| 381 | *++env = '\0'; |
| 382 | |
wdenk | 4a6fd34 | 2003-04-12 23:38:12 +0000 | [diff] [blame] | 383 | WRITE_FLASH: |
wdenk | 6aff311 | 2002-12-17 01:51:00 +0000 | [diff] [blame] | 384 | |
| 385 | /* Update CRC */ |
Markus Klotzbücher | 6de66b3 | 2007-11-27 10:23:20 +0100 | [diff] [blame] | 386 | environment.crc = crc32 (0, (uint8_t*) environment.data, ENV_SIZE); |
wdenk | 6aff311 | 2002-12-17 01:51:00 +0000 | [diff] [blame] | 387 | |
| 388 | /* write environment back to flash */ |
| 389 | if (flash_io (O_RDWR)) { |
wdenk | 4a6fd34 | 2003-04-12 23:38:12 +0000 | [diff] [blame] | 390 | fprintf (stderr, "Error: can't write fw_env to flash\n"); |
wdenk | 6aff311 | 2002-12-17 01:51:00 +0000 | [diff] [blame] | 391 | return (-1); |
| 392 | } |
| 393 | |
| 394 | return (0); |
| 395 | } |
| 396 | |
| 397 | static int flash_io (int mode) |
| 398 | { |
| 399 | int fd, fdr, rc, otherdev, len, resid; |
| 400 | erase_info_t erase; |
wdenk | 592c5ca | 2003-06-21 00:17:24 +0000 | [diff] [blame] | 401 | char *data = NULL; |
wdenk | 6aff311 | 2002-12-17 01:51:00 +0000 | [diff] [blame] | 402 | |
wdenk | 4a6fd34 | 2003-04-12 23:38:12 +0000 | [diff] [blame] | 403 | if ((fd = open (DEVNAME (curdev), mode)) < 0) { |
| 404 | fprintf (stderr, |
| 405 | "Can't open %s: %s\n", |
| 406 | DEVNAME (curdev), strerror (errno)); |
wdenk | 6aff311 | 2002-12-17 01:51:00 +0000 | [diff] [blame] | 407 | return (-1); |
| 408 | } |
| 409 | |
wdenk | 4a6fd34 | 2003-04-12 23:38:12 +0000 | [diff] [blame] | 410 | len = sizeof (environment.crc); |
wdenk | d0fb80c | 2003-01-11 09:48:40 +0000 | [diff] [blame] | 411 | if (HaveRedundEnv) { |
wdenk | 4a6fd34 | 2003-04-12 23:38:12 +0000 | [diff] [blame] | 412 | len += sizeof (environment.flags); |
wdenk | d0fb80c | 2003-01-11 09:48:40 +0000 | [diff] [blame] | 413 | } |
wdenk | 6aff311 | 2002-12-17 01:51:00 +0000 | [diff] [blame] | 414 | |
| 415 | if (mode == O_RDWR) { |
wdenk | d0fb80c | 2003-01-11 09:48:40 +0000 | [diff] [blame] | 416 | if (HaveRedundEnv) { |
| 417 | /* switch to next partition for writing */ |
| 418 | otherdev = !curdev; |
wdenk | 4a6fd34 | 2003-04-12 23:38:12 +0000 | [diff] [blame] | 419 | if ((fdr = open (DEVNAME (otherdev), mode)) < 0) { |
| 420 | fprintf (stderr, |
| 421 | "Can't open %s: %s\n", |
| 422 | DEVNAME (otherdev), |
| 423 | strerror (errno)); |
wdenk | d0fb80c | 2003-01-11 09:48:40 +0000 | [diff] [blame] | 424 | return (-1); |
| 425 | } |
| 426 | } else { |
| 427 | otherdev = curdev; |
| 428 | fdr = fd; |
wdenk | 6aff311 | 2002-12-17 01:51:00 +0000 | [diff] [blame] | 429 | } |
wdenk | 4a6fd34 | 2003-04-12 23:38:12 +0000 | [diff] [blame] | 430 | printf ("Unlocking flash...\n"); |
| 431 | erase.length = DEVESIZE (otherdev); |
| 432 | erase.start = DEVOFFSET (otherdev); |
wdenk | 6aff311 | 2002-12-17 01:51:00 +0000 | [diff] [blame] | 433 | ioctl (fdr, MEMUNLOCK, &erase); |
| 434 | |
wdenk | d0fb80c | 2003-01-11 09:48:40 +0000 | [diff] [blame] | 435 | if (HaveRedundEnv) { |
wdenk | 4a6fd34 | 2003-04-12 23:38:12 +0000 | [diff] [blame] | 436 | erase.length = DEVESIZE (curdev); |
| 437 | erase.start = DEVOFFSET (curdev); |
wdenk | d0fb80c | 2003-01-11 09:48:40 +0000 | [diff] [blame] | 438 | ioctl (fd, MEMUNLOCK, &erase); |
| 439 | environment.flags = active_flag; |
| 440 | } |
| 441 | |
wdenk | 4a6fd34 | 2003-04-12 23:38:12 +0000 | [diff] [blame] | 442 | printf ("Done\n"); |
| 443 | resid = DEVESIZE (otherdev) - CFG_ENV_SIZE; |
wdenk | 6aff311 | 2002-12-17 01:51:00 +0000 | [diff] [blame] | 444 | if (resid) { |
wdenk | 4a6fd34 | 2003-04-12 23:38:12 +0000 | [diff] [blame] | 445 | if ((data = malloc (resid)) == NULL) { |
| 446 | fprintf (stderr, |
| 447 | "Cannot malloc %d bytes: %s\n", |
| 448 | resid, |
| 449 | strerror (errno)); |
wdenk | 6aff311 | 2002-12-17 01:51:00 +0000 | [diff] [blame] | 450 | return (-1); |
| 451 | } |
wdenk | 4a6fd34 | 2003-04-12 23:38:12 +0000 | [diff] [blame] | 452 | if (lseek (fdr, DEVOFFSET (otherdev) + CFG_ENV_SIZE, SEEK_SET) |
| 453 | == -1) { |
| 454 | fprintf (stderr, "seek error on %s: %s\n", |
| 455 | DEVNAME (otherdev), |
| 456 | strerror (errno)); |
wdenk | 6aff311 | 2002-12-17 01:51:00 +0000 | [diff] [blame] | 457 | return (-1); |
| 458 | } |
| 459 | if ((rc = read (fdr, data, resid)) != resid) { |
| 460 | fprintf (stderr, |
wdenk | 4a6fd34 | 2003-04-12 23:38:12 +0000 | [diff] [blame] | 461 | "read error on %s: %s\n", |
| 462 | DEVNAME (otherdev), |
| 463 | strerror (errno)); |
wdenk | 6aff311 | 2002-12-17 01:51:00 +0000 | [diff] [blame] | 464 | return (-1); |
| 465 | } |
| 466 | } |
| 467 | |
wdenk | 4a6fd34 | 2003-04-12 23:38:12 +0000 | [diff] [blame] | 468 | printf ("Erasing old environment...\n"); |
wdenk | 6aff311 | 2002-12-17 01:51:00 +0000 | [diff] [blame] | 469 | |
wdenk | 4a6fd34 | 2003-04-12 23:38:12 +0000 | [diff] [blame] | 470 | erase.length = DEVESIZE (otherdev); |
| 471 | erase.start = DEVOFFSET (otherdev); |
wdenk | 6aff311 | 2002-12-17 01:51:00 +0000 | [diff] [blame] | 472 | if (ioctl (fdr, MEMERASE, &erase) != 0) { |
| 473 | fprintf (stderr, "MTD erase error on %s: %s\n", |
wdenk | 4a6fd34 | 2003-04-12 23:38:12 +0000 | [diff] [blame] | 474 | DEVNAME (otherdev), |
| 475 | strerror (errno)); |
wdenk | 6aff311 | 2002-12-17 01:51:00 +0000 | [diff] [blame] | 476 | return (-1); |
| 477 | } |
| 478 | |
wdenk | 4a6fd34 | 2003-04-12 23:38:12 +0000 | [diff] [blame] | 479 | printf ("Done\n"); |
wdenk | 6aff311 | 2002-12-17 01:51:00 +0000 | [diff] [blame] | 480 | |
wdenk | 4a6fd34 | 2003-04-12 23:38:12 +0000 | [diff] [blame] | 481 | printf ("Writing environment to %s...\n", DEVNAME (otherdev)); |
| 482 | if (lseek (fdr, DEVOFFSET (otherdev), SEEK_SET) == -1) { |
wdenk | d0fb80c | 2003-01-11 09:48:40 +0000 | [diff] [blame] | 483 | fprintf (stderr, |
wdenk | 4a6fd34 | 2003-04-12 23:38:12 +0000 | [diff] [blame] | 484 | "seek error on %s: %s\n", |
| 485 | DEVNAME (otherdev), strerror (errno)); |
wdenk | d0fb80c | 2003-01-11 09:48:40 +0000 | [diff] [blame] | 486 | return (-1); |
| 487 | } |
wdenk | 4a6fd34 | 2003-04-12 23:38:12 +0000 | [diff] [blame] | 488 | if (write (fdr, &environment, len) != len) { |
wdenk | 6aff311 | 2002-12-17 01:51:00 +0000 | [diff] [blame] | 489 | fprintf (stderr, |
wdenk | 4a6fd34 | 2003-04-12 23:38:12 +0000 | [diff] [blame] | 490 | "CRC write error on %s: %s\n", |
| 491 | DEVNAME (otherdev), strerror (errno)); |
wdenk | 6aff311 | 2002-12-17 01:51:00 +0000 | [diff] [blame] | 492 | return (-1); |
| 493 | } |
wdenk | 4a6fd34 | 2003-04-12 23:38:12 +0000 | [diff] [blame] | 494 | if (write (fdr, environment.data, ENV_SIZE) != ENV_SIZE) { |
wdenk | 6aff311 | 2002-12-17 01:51:00 +0000 | [diff] [blame] | 495 | fprintf (stderr, |
wdenk | 4a6fd34 | 2003-04-12 23:38:12 +0000 | [diff] [blame] | 496 | "Write error on %s: %s\n", |
| 497 | DEVNAME (otherdev), strerror (errno)); |
wdenk | 6aff311 | 2002-12-17 01:51:00 +0000 | [diff] [blame] | 498 | return (-1); |
| 499 | } |
| 500 | if (resid) { |
| 501 | if (write (fdr, data, resid) != resid) { |
| 502 | fprintf (stderr, |
wdenk | 4a6fd34 | 2003-04-12 23:38:12 +0000 | [diff] [blame] | 503 | "write error on %s: %s\n", |
| 504 | DEVNAME (curdev), strerror (errno)); |
wdenk | 6aff311 | 2002-12-17 01:51:00 +0000 | [diff] [blame] | 505 | return (-1); |
| 506 | } |
wdenk | 4a6fd34 | 2003-04-12 23:38:12 +0000 | [diff] [blame] | 507 | free (data); |
wdenk | 6aff311 | 2002-12-17 01:51:00 +0000 | [diff] [blame] | 508 | } |
wdenk | d0fb80c | 2003-01-11 09:48:40 +0000 | [diff] [blame] | 509 | if (HaveRedundEnv) { |
| 510 | /* change flag on current active env partition */ |
wdenk | 4a6fd34 | 2003-04-12 23:38:12 +0000 | [diff] [blame] | 511 | if (lseek (fd, DEVOFFSET (curdev) + sizeof (ulong), SEEK_SET) |
| 512 | == -1) { |
| 513 | fprintf (stderr, "seek error on %s: %s\n", |
| 514 | DEVNAME (curdev), strerror (errno)); |
wdenk | d0fb80c | 2003-01-11 09:48:40 +0000 | [diff] [blame] | 515 | return (-1); |
| 516 | } |
wdenk | 4a6fd34 | 2003-04-12 23:38:12 +0000 | [diff] [blame] | 517 | if (write (fd, &obsolete_flag, sizeof (obsolete_flag)) != |
| 518 | sizeof (obsolete_flag)) { |
wdenk | d0fb80c | 2003-01-11 09:48:40 +0000 | [diff] [blame] | 519 | fprintf (stderr, |
wdenk | 4a6fd34 | 2003-04-12 23:38:12 +0000 | [diff] [blame] | 520 | "Write error on %s: %s\n", |
| 521 | DEVNAME (curdev), strerror (errno)); |
wdenk | d0fb80c | 2003-01-11 09:48:40 +0000 | [diff] [blame] | 522 | return (-1); |
| 523 | } |
wdenk | 6aff311 | 2002-12-17 01:51:00 +0000 | [diff] [blame] | 524 | } |
wdenk | 4a6fd34 | 2003-04-12 23:38:12 +0000 | [diff] [blame] | 525 | printf ("Done\n"); |
| 526 | printf ("Locking ...\n"); |
| 527 | erase.length = DEVESIZE (otherdev); |
| 528 | erase.start = DEVOFFSET (otherdev); |
wdenk | 6aff311 | 2002-12-17 01:51:00 +0000 | [diff] [blame] | 529 | ioctl (fdr, MEMLOCK, &erase); |
wdenk | d0fb80c | 2003-01-11 09:48:40 +0000 | [diff] [blame] | 530 | if (HaveRedundEnv) { |
wdenk | 4a6fd34 | 2003-04-12 23:38:12 +0000 | [diff] [blame] | 531 | erase.length = DEVESIZE (curdev); |
| 532 | erase.start = DEVOFFSET (curdev); |
wdenk | d0fb80c | 2003-01-11 09:48:40 +0000 | [diff] [blame] | 533 | ioctl (fd, MEMLOCK, &erase); |
wdenk | 4a6fd34 | 2003-04-12 23:38:12 +0000 | [diff] [blame] | 534 | if (close (fdr)) { |
wdenk | d0fb80c | 2003-01-11 09:48:40 +0000 | [diff] [blame] | 535 | fprintf (stderr, |
wdenk | 4a6fd34 | 2003-04-12 23:38:12 +0000 | [diff] [blame] | 536 | "I/O error on %s: %s\n", |
| 537 | DEVNAME (otherdev), |
| 538 | 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 | } |
wdenk | 4a6fd34 | 2003-04-12 23:38:12 +0000 | [diff] [blame] | 542 | printf ("Done\n"); |
wdenk | 6aff311 | 2002-12-17 01:51:00 +0000 | [diff] [blame] | 543 | } else { |
wdenk | d0fb80c | 2003-01-11 09:48:40 +0000 | [diff] [blame] | 544 | |
wdenk | 4a6fd34 | 2003-04-12 23:38:12 +0000 | [diff] [blame] | 545 | if (lseek (fd, DEVOFFSET (curdev), SEEK_SET) == -1) { |
wdenk | d0fb80c | 2003-01-11 09:48:40 +0000 | [diff] [blame] | 546 | fprintf (stderr, |
wdenk | 4a6fd34 | 2003-04-12 23:38:12 +0000 | [diff] [blame] | 547 | "seek error on %s: %s\n", |
| 548 | DEVNAME (curdev), strerror (errno)); |
wdenk | d0fb80c | 2003-01-11 09:48:40 +0000 | [diff] [blame] | 549 | return (-1); |
| 550 | } |
wdenk | 6aff311 | 2002-12-17 01:51:00 +0000 | [diff] [blame] | 551 | if (read (fd, &environment, len) != len) { |
| 552 | fprintf (stderr, |
wdenk | 4a6fd34 | 2003-04-12 23:38:12 +0000 | [diff] [blame] | 553 | "CRC read error on %s: %s\n", |
| 554 | DEVNAME (curdev), strerror (errno)); |
wdenk | 6aff311 | 2002-12-17 01:51:00 +0000 | [diff] [blame] | 555 | return (-1); |
| 556 | } |
| 557 | if ((rc = read (fd, environment.data, ENV_SIZE)) != ENV_SIZE) { |
| 558 | fprintf (stderr, |
wdenk | 4a6fd34 | 2003-04-12 23:38:12 +0000 | [diff] [blame] | 559 | "Read error on %s: %s\n", |
| 560 | DEVNAME (curdev), strerror (errno)); |
wdenk | 6aff311 | 2002-12-17 01:51:00 +0000 | [diff] [blame] | 561 | return (-1); |
| 562 | } |
| 563 | } |
| 564 | |
wdenk | 4a6fd34 | 2003-04-12 23:38:12 +0000 | [diff] [blame] | 565 | if (close (fd)) { |
wdenk | 6aff311 | 2002-12-17 01:51:00 +0000 | [diff] [blame] | 566 | fprintf (stderr, |
wdenk | 4a6fd34 | 2003-04-12 23:38:12 +0000 | [diff] [blame] | 567 | "I/O error on %s: %s\n", |
| 568 | DEVNAME (curdev), strerror (errno)); |
wdenk | 6aff311 | 2002-12-17 01:51:00 +0000 | [diff] [blame] | 569 | return (-1); |
| 570 | } |
| 571 | |
| 572 | /* everything ok */ |
| 573 | return (0); |
| 574 | } |
| 575 | |
| 576 | /* |
| 577 | * s1 is either a simple 'name', or a 'name=value' pair. |
| 578 | * s2 is a 'name=value' pair. |
| 579 | * If the names match, return the value of s2, else NULL. |
| 580 | */ |
| 581 | |
Markus Klotzbücher | 6de66b3 | 2007-11-27 10:23:20 +0100 | [diff] [blame] | 582 | static char *envmatch (char * s1, char * s2) |
wdenk | 6aff311 | 2002-12-17 01:51:00 +0000 | [diff] [blame] | 583 | { |
| 584 | |
| 585 | while (*s1 == *s2++) |
| 586 | if (*s1++ == '=') |
wdenk | 4a6fd34 | 2003-04-12 23:38:12 +0000 | [diff] [blame] | 587 | return (s2); |
| 588 | if (*s1 == '\0' && *(s2 - 1) == '=') |
| 589 | return (s2); |
| 590 | return (NULL); |
wdenk | 6aff311 | 2002-12-17 01:51:00 +0000 | [diff] [blame] | 591 | } |
| 592 | |
| 593 | /* |
| 594 | * Prevent confusion if running from erased flash memory |
| 595 | */ |
wdenk | 4a6fd34 | 2003-04-12 23:38:12 +0000 | [diff] [blame] | 596 | static int env_init (void) |
wdenk | 6aff311 | 2002-12-17 01:51:00 +0000 | [diff] [blame] | 597 | { |
| 598 | int crc1, crc1_ok; |
Markus Klotzbücher | 6de66b3 | 2007-11-27 10:23:20 +0100 | [diff] [blame] | 599 | char *addr1; |
wdenk | d0fb80c | 2003-01-11 09:48:40 +0000 | [diff] [blame] | 600 | |
wdenk | 6aff311 | 2002-12-17 01:51:00 +0000 | [diff] [blame] | 601 | int crc2, crc2_ok; |
Markus Klotzbücher | 6de66b3 | 2007-11-27 10:23:20 +0100 | [diff] [blame] | 602 | char flag1, flag2, *addr2; |
wdenk | 6aff311 | 2002-12-17 01:51:00 +0000 | [diff] [blame] | 603 | |
wdenk | 4a6fd34 | 2003-04-12 23:38:12 +0000 | [diff] [blame] | 604 | if (parse_config ()) /* should fill envdevices */ |
wdenk | d0fb80c | 2003-01-11 09:48:40 +0000 | [diff] [blame] | 605 | return 1; |
wdenk | 4a6fd34 | 2003-04-12 23:38:12 +0000 | [diff] [blame] | 606 | |
wdenk | d0fb80c | 2003-01-11 09:48:40 +0000 | [diff] [blame] | 607 | if ((addr1 = calloc (1, ENV_SIZE)) == NULL) { |
wdenk | 4a6fd34 | 2003-04-12 23:38:12 +0000 | [diff] [blame] | 608 | fprintf (stderr, |
| 609 | "Not enough memory for environment (%ld bytes)\n", |
| 610 | ENV_SIZE); |
wdenk | d0fb80c | 2003-01-11 09:48:40 +0000 | [diff] [blame] | 611 | return (errno); |
| 612 | } |
wdenk | 4a6fd34 | 2003-04-12 23:38:12 +0000 | [diff] [blame] | 613 | |
wdenk | d0fb80c | 2003-01-11 09:48:40 +0000 | [diff] [blame] | 614 | /* read environment from FLASH to local buffer */ |
| 615 | environment.data = addr1; |
| 616 | curdev = 0; |
| 617 | if (flash_io (O_RDONLY)) { |
| 618 | return (errno); |
| 619 | } |
wdenk | 4a6fd34 | 2003-04-12 23:38:12 +0000 | [diff] [blame] | 620 | |
Markus Klotzbücher | 6de66b3 | 2007-11-27 10:23:20 +0100 | [diff] [blame] | 621 | crc1_ok = ((crc1 = crc32 (0, (uint8_t *) environment.data, ENV_SIZE)) |
wdenk | d0fb80c | 2003-01-11 09:48:40 +0000 | [diff] [blame] | 622 | == environment.crc); |
| 623 | if (!HaveRedundEnv) { |
wdenk | 6aff311 | 2002-12-17 01:51:00 +0000 | [diff] [blame] | 624 | if (!crc1_ok) { |
wdenk | 4a6fd34 | 2003-04-12 23:38:12 +0000 | [diff] [blame] | 625 | fprintf (stderr, |
| 626 | "Warning: Bad CRC, using default environment\n"); |
Wolfgang Denk | f07217c | 2006-03-12 23:27:46 +0100 | [diff] [blame] | 627 | memcpy(environment.data, default_environment, sizeof default_environment); |
wdenk | 6aff311 | 2002-12-17 01:51:00 +0000 | [diff] [blame] | 628 | } |
wdenk | d0fb80c | 2003-01-11 09:48:40 +0000 | [diff] [blame] | 629 | } else { |
wdenk | 6aff311 | 2002-12-17 01:51:00 +0000 | [diff] [blame] | 630 | flag1 = environment.flags; |
wdenk | 4a6fd34 | 2003-04-12 23:38:12 +0000 | [diff] [blame] | 631 | |
wdenk | 6aff311 | 2002-12-17 01:51:00 +0000 | [diff] [blame] | 632 | curdev = 1; |
| 633 | if ((addr2 = calloc (1, ENV_SIZE)) == NULL) { |
wdenk | 4a6fd34 | 2003-04-12 23:38:12 +0000 | [diff] [blame] | 634 | fprintf (stderr, |
| 635 | "Not enough memory for environment (%ld bytes)\n", |
| 636 | ENV_SIZE); |
wdenk | 6aff311 | 2002-12-17 01:51:00 +0000 | [diff] [blame] | 637 | return (errno); |
wdenk | 4a6fd34 | 2003-04-12 23:38:12 +0000 | [diff] [blame] | 638 | } |
wdenk | 6aff311 | 2002-12-17 01:51:00 +0000 | [diff] [blame] | 639 | environment.data = addr2; |
wdenk | 4a6fd34 | 2003-04-12 23:38:12 +0000 | [diff] [blame] | 640 | |
wdenk | 6aff311 | 2002-12-17 01:51:00 +0000 | [diff] [blame] | 641 | if (flash_io (O_RDONLY)) { |
| 642 | return (errno); |
| 643 | } |
wdenk | 4a6fd34 | 2003-04-12 23:38:12 +0000 | [diff] [blame] | 644 | |
Markus Klotzbücher | 6de66b3 | 2007-11-27 10:23:20 +0100 | [diff] [blame] | 645 | crc2_ok = ((crc2 = crc32 (0, (uint8_t *) environment.data, ENV_SIZE)) |
wdenk | 6aff311 | 2002-12-17 01:51:00 +0000 | [diff] [blame] | 646 | == environment.crc); |
| 647 | flag2 = environment.flags; |
wdenk | 4a6fd34 | 2003-04-12 23:38:12 +0000 | [diff] [blame] | 648 | |
| 649 | if (crc1_ok && !crc2_ok) { |
| 650 | environment.data = addr1; |
wdenk | 6aff311 | 2002-12-17 01:51:00 +0000 | [diff] [blame] | 651 | environment.flags = flag1; |
| 652 | environment.crc = crc1; |
| 653 | curdev = 0; |
wdenk | 4a6fd34 | 2003-04-12 23:38:12 +0000 | [diff] [blame] | 654 | free (addr2); |
| 655 | } else if (!crc1_ok && crc2_ok) { |
| 656 | environment.data = addr2; |
wdenk | 6aff311 | 2002-12-17 01:51:00 +0000 | [diff] [blame] | 657 | environment.flags = flag2; |
| 658 | environment.crc = crc2; |
| 659 | curdev = 1; |
wdenk | 4a6fd34 | 2003-04-12 23:38:12 +0000 | [diff] [blame] | 660 | free (addr1); |
| 661 | } else if (!crc1_ok && !crc2_ok) { |
| 662 | fprintf (stderr, |
| 663 | "Warning: Bad CRC, using default environment\n"); |
Wolfgang Denk | f07217c | 2006-03-12 23:27:46 +0100 | [diff] [blame] | 664 | memcpy(environment.data, default_environment, sizeof default_environment); |
wdenk | 6aff311 | 2002-12-17 01:51:00 +0000 | [diff] [blame] | 665 | curdev = 0; |
wdenk | 4a6fd34 | 2003-04-12 23:38:12 +0000 | [diff] [blame] | 666 | free (addr1); |
| 667 | } else if (flag1 == active_flag && flag2 == obsolete_flag) { |
| 668 | environment.data = addr1; |
wdenk | 6aff311 | 2002-12-17 01:51:00 +0000 | [diff] [blame] | 669 | environment.flags = flag1; |
| 670 | environment.crc = crc1; |
| 671 | curdev = 0; |
wdenk | 4a6fd34 | 2003-04-12 23:38:12 +0000 | [diff] [blame] | 672 | free (addr2); |
| 673 | } else if (flag1 == obsolete_flag && flag2 == active_flag) { |
| 674 | environment.data = addr2; |
wdenk | 6aff311 | 2002-12-17 01:51:00 +0000 | [diff] [blame] | 675 | environment.flags = flag2; |
| 676 | environment.crc = crc2; |
| 677 | curdev = 1; |
wdenk | 4a6fd34 | 2003-04-12 23:38:12 +0000 | [diff] [blame] | 678 | free (addr1); |
| 679 | } else if (flag1 == flag2) { |
| 680 | environment.data = addr1; |
wdenk | 6aff311 | 2002-12-17 01:51:00 +0000 | [diff] [blame] | 681 | environment.flags = flag1; |
| 682 | environment.crc = crc1; |
| 683 | curdev = 0; |
wdenk | 4a6fd34 | 2003-04-12 23:38:12 +0000 | [diff] [blame] | 684 | free (addr2); |
| 685 | } else if (flag1 == 0xFF) { |
| 686 | environment.data = addr1; |
wdenk | 6aff311 | 2002-12-17 01:51:00 +0000 | [diff] [blame] | 687 | environment.flags = flag1; |
| 688 | environment.crc = crc1; |
| 689 | curdev = 0; |
wdenk | 4a6fd34 | 2003-04-12 23:38:12 +0000 | [diff] [blame] | 690 | free (addr2); |
| 691 | } else if (flag2 == 0xFF) { |
| 692 | environment.data = addr2; |
wdenk | 6aff311 | 2002-12-17 01:51:00 +0000 | [diff] [blame] | 693 | environment.flags = flag2; |
| 694 | environment.crc = crc2; |
| 695 | curdev = 1; |
wdenk | 4a6fd34 | 2003-04-12 23:38:12 +0000 | [diff] [blame] | 696 | free (addr1); |
wdenk | 6aff311 | 2002-12-17 01:51:00 +0000 | [diff] [blame] | 697 | } |
wdenk | 6aff311 | 2002-12-17 01:51:00 +0000 | [diff] [blame] | 698 | } |
| 699 | return (0); |
| 700 | } |
| 701 | |
| 702 | |
wdenk | 4a6fd34 | 2003-04-12 23:38:12 +0000 | [diff] [blame] | 703 | static int parse_config () |
wdenk | 6aff311 | 2002-12-17 01:51:00 +0000 | [diff] [blame] | 704 | { |
| 705 | struct stat st; |
| 706 | |
wdenk | d0fb80c | 2003-01-11 09:48:40 +0000 | [diff] [blame] | 707 | #if defined(CONFIG_FILE) |
| 708 | /* Fills in DEVNAME(), ENVSIZE(), DEVESIZE(). Or don't. */ |
wdenk | 4a6fd34 | 2003-04-12 23:38:12 +0000 | [diff] [blame] | 709 | if (get_config (CONFIG_FILE)) { |
wdenk | d0fb80c | 2003-01-11 09:48:40 +0000 | [diff] [blame] | 710 | fprintf (stderr, |
wdenk | 4a6fd34 | 2003-04-12 23:38:12 +0000 | [diff] [blame] | 711 | "Cannot parse config file: %s\n", strerror (errno)); |
wdenk | 6aff311 | 2002-12-17 01:51:00 +0000 | [diff] [blame] | 712 | return 1; |
| 713 | } |
wdenk | d0fb80c | 2003-01-11 09:48:40 +0000 | [diff] [blame] | 714 | #else |
wdenk | 4a6fd34 | 2003-04-12 23:38:12 +0000 | [diff] [blame] | 715 | strcpy (DEVNAME (0), DEVICE1_NAME); |
| 716 | DEVOFFSET (0) = DEVICE1_OFFSET; |
| 717 | ENVSIZE (0) = ENV1_SIZE; |
| 718 | DEVESIZE (0) = DEVICE1_ESIZE; |
wdenk | 6aff311 | 2002-12-17 01:51:00 +0000 | [diff] [blame] | 719 | #ifdef HAVE_REDUND |
wdenk | 4a6fd34 | 2003-04-12 23:38:12 +0000 | [diff] [blame] | 720 | strcpy (DEVNAME (1), DEVICE2_NAME); |
| 721 | DEVOFFSET (1) = DEVICE2_OFFSET; |
| 722 | ENVSIZE (1) = ENV2_SIZE; |
| 723 | DEVESIZE (1) = DEVICE2_ESIZE; |
wdenk | d0fb80c | 2003-01-11 09:48:40 +0000 | [diff] [blame] | 724 | HaveRedundEnv = 1; |
wdenk | 6aff311 | 2002-12-17 01:51:00 +0000 | [diff] [blame] | 725 | #endif |
wdenk | d0fb80c | 2003-01-11 09:48:40 +0000 | [diff] [blame] | 726 | #endif |
wdenk | 4a6fd34 | 2003-04-12 23:38:12 +0000 | [diff] [blame] | 727 | if (stat (DEVNAME (0), &st)) { |
| 728 | fprintf (stderr, |
| 729 | "Cannot access MTD device %s: %s\n", |
| 730 | DEVNAME (0), strerror (errno)); |
wdenk | d0fb80c | 2003-01-11 09:48:40 +0000 | [diff] [blame] | 731 | return 1; |
| 732 | } |
wdenk | 4a6fd34 | 2003-04-12 23:38:12 +0000 | [diff] [blame] | 733 | |
| 734 | if (HaveRedundEnv && stat (DEVNAME (1), &st)) { |
| 735 | fprintf (stderr, |
| 736 | "Cannot access MTD device %s: %s\n", |
Wolfgang Denk | e2146b6 | 2005-09-25 01:20:38 +0200 | [diff] [blame] | 737 | DEVNAME (1), strerror (errno)); |
wdenk | d0fb80c | 2003-01-11 09:48:40 +0000 | [diff] [blame] | 738 | return 1; |
| 739 | } |
wdenk | 6aff311 | 2002-12-17 01:51:00 +0000 | [diff] [blame] | 740 | return 0; |
| 741 | } |
wdenk | d0fb80c | 2003-01-11 09:48:40 +0000 | [diff] [blame] | 742 | |
| 743 | #if defined(CONFIG_FILE) |
| 744 | static int get_config (char *fname) |
| 745 | { |
| 746 | FILE *fp; |
| 747 | int i = 0; |
| 748 | int rc; |
| 749 | char dump[128]; |
| 750 | |
wdenk | 4a6fd34 | 2003-04-12 23:38:12 +0000 | [diff] [blame] | 751 | if ((fp = fopen (fname, "r")) == NULL) { |
wdenk | d0fb80c | 2003-01-11 09:48:40 +0000 | [diff] [blame] | 752 | return 1; |
| 753 | } |
| 754 | |
wdenk | 4a6fd34 | 2003-04-12 23:38:12 +0000 | [diff] [blame] | 755 | while ((i < 2) && ((rc = fscanf (fp, "%s %lx %lx %lx", |
| 756 | DEVNAME (i), |
| 757 | &DEVOFFSET (i), |
| 758 | &ENVSIZE (i), |
| 759 | &DEVESIZE (i) )) != EOF)) { |
wdenk | d0fb80c | 2003-01-11 09:48:40 +0000 | [diff] [blame] | 760 | |
| 761 | /* Skip incomplete conversions and comment strings */ |
wdenk | 4a6fd34 | 2003-04-12 23:38:12 +0000 | [diff] [blame] | 762 | if ((rc < 3) || (*DEVNAME (i) == '#')) { |
| 763 | fgets (dump, sizeof (dump), fp); /* Consume till end */ |
wdenk | d0fb80c | 2003-01-11 09:48:40 +0000 | [diff] [blame] | 764 | continue; |
| 765 | } |
| 766 | |
| 767 | i++; |
| 768 | } |
wdenk | 4a6fd34 | 2003-04-12 23:38:12 +0000 | [diff] [blame] | 769 | fclose (fp); |
| 770 | |
wdenk | d0fb80c | 2003-01-11 09:48:40 +0000 | [diff] [blame] | 771 | HaveRedundEnv = i - 1; |
wdenk | 4a6fd34 | 2003-04-12 23:38:12 +0000 | [diff] [blame] | 772 | if (!i) { /* No valid entries found */ |
wdenk | d0fb80c | 2003-01-11 09:48:40 +0000 | [diff] [blame] | 773 | errno = EINVAL; |
| 774 | return 1; |
| 775 | } else |
| 776 | return 0; |
| 777 | } |
| 778 | #endif |