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