uboot/env/flash.c
<<
>>
Prefs
   1// SPDX-License-Identifier: GPL-2.0+
   2/*
   3 * (C) Copyright 2000-2010
   4 * Wolfgang Denk, DENX Software Engineering, wd@denx.de.
   5 *
   6 * (C) Copyright 2001 Sysgo Real-Time Solutions, GmbH <www.elinos.com>
   7 * Andreas Heppel <aheppel@sysgo.de>
   8 */
   9
  10/* #define DEBUG */
  11
  12#include <common.h>
  13#include <command.h>
  14#include <env.h>
  15#include <env_internal.h>
  16#include <flash.h>
  17#include <log.h>
  18#include <asm/global_data.h>
  19#include <linux/stddef.h>
  20#include <malloc.h>
  21#include <search.h>
  22#include <errno.h>
  23#include <u-boot/crc.h>
  24
  25DECLARE_GLOBAL_DATA_PTR;
  26
  27#ifndef CONFIG_SPL_BUILD
  28# if defined(CONFIG_CMD_SAVEENV) && defined(CONFIG_CMD_FLASH)
  29#  define CMD_SAVEENV
  30# elif defined(CONFIG_ENV_ADDR_REDUND)
  31#  error CONFIG_ENV_ADDR_REDUND must have CONFIG_CMD_SAVEENV & CONFIG_CMD_FLASH
  32# endif
  33#endif
  34
  35/* TODO(sjg@chromium.org): Figure out all these special cases */
  36#if (!defined(CONFIG_MICROBLAZE) && !defined(CONFIG_ARCH_ZYNQ) && \
  37        !defined(CONFIG_TARGET_MCCMON6) && !defined(CONFIG_TARGET_X600) && \
  38        !defined(CONFIG_TARGET_EDMINIV2)) || \
  39        !defined(CONFIG_SPL_BUILD)
  40#define LOADENV
  41#endif
  42
  43#if !defined(CONFIG_TARGET_X600) || !defined(CONFIG_SPL_BUILD)
  44#define INITENV
  45#endif
  46
  47#if defined(CONFIG_ENV_ADDR_REDUND) && defined(CMD_SAVEENV) || \
  48        !defined(CONFIG_ENV_ADDR_REDUND) && defined(INITENV)
  49#ifdef ENV_IS_EMBEDDED
  50static env_t *env_ptr = &embedded_environment;
  51#else /* ! ENV_IS_EMBEDDED */
  52
  53static env_t *env_ptr = (env_t *)CONFIG_ENV_ADDR;
  54#endif /* ENV_IS_EMBEDDED */
  55#endif
  56static __maybe_unused env_t *flash_addr = (env_t *)CONFIG_ENV_ADDR;
  57
  58/* CONFIG_ENV_ADDR is supposed to be on sector boundary */
  59static ulong __maybe_unused end_addr =
  60                CONFIG_ENV_ADDR + CONFIG_ENV_SECT_SIZE - 1;
  61
  62#ifdef CONFIG_ENV_ADDR_REDUND
  63
  64static env_t __maybe_unused *flash_addr_new = (env_t *)CONFIG_ENV_ADDR_REDUND;
  65
  66/* CONFIG_ENV_ADDR_REDUND is supposed to be on sector boundary */
  67static ulong __maybe_unused end_addr_new =
  68                CONFIG_ENV_ADDR_REDUND + CONFIG_ENV_SECT_SIZE - 1;
  69#endif /* CONFIG_ENV_ADDR_REDUND */
  70
  71#ifdef CONFIG_ENV_ADDR_REDUND
  72#ifdef INITENV
  73static int env_flash_init(void)
  74{
  75        int crc1_ok = 0, crc2_ok = 0;
  76
  77        uchar flag1 = flash_addr->flags;
  78        uchar flag2 = flash_addr_new->flags;
  79
  80        ulong addr1 = (ulong)&(flash_addr->data);
  81        ulong addr2 = (ulong)&(flash_addr_new->data);
  82
  83        crc1_ok = crc32(0, flash_addr->data, ENV_SIZE) == flash_addr->crc;
  84        crc2_ok =
  85                crc32(0, flash_addr_new->data, ENV_SIZE) == flash_addr_new->crc;
  86
  87        if (crc1_ok && !crc2_ok) {
  88                gd->env_addr    = addr1;
  89                gd->env_valid   = ENV_VALID;
  90        } else if (!crc1_ok && crc2_ok) {
  91                gd->env_addr    = addr2;
  92                gd->env_valid   = ENV_VALID;
  93        } else if (!crc1_ok && !crc2_ok) {
  94                gd->env_valid   = ENV_INVALID;
  95        } else if (flag1 == ENV_REDUND_ACTIVE &&
  96                   flag2 == ENV_REDUND_OBSOLETE) {
  97                gd->env_addr    = addr1;
  98                gd->env_valid   = ENV_VALID;
  99        } else if (flag1 == ENV_REDUND_OBSOLETE &&
 100                   flag2 == ENV_REDUND_ACTIVE) {
 101                gd->env_addr    = addr2;
 102                gd->env_valid   = ENV_VALID;
 103        } else if (flag1 == flag2) {
 104                gd->env_addr    = addr1;
 105                gd->env_valid   = ENV_REDUND;
 106        } else if (flag1 == 0xFF) {
 107                gd->env_addr    = addr1;
 108                gd->env_valid   = ENV_REDUND;
 109        } else if (flag2 == 0xFF) {
 110                gd->env_addr    = addr2;
 111                gd->env_valid   = ENV_REDUND;
 112        }
 113
 114        return 0;
 115}
 116#endif
 117
 118#ifdef CMD_SAVEENV
 119static int env_flash_save(void)
 120{
 121        env_t   env_new;
 122        char    *saved_data = NULL;
 123        char    flag = ENV_REDUND_OBSOLETE, new_flag = ENV_REDUND_ACTIVE;
 124        int     rc = 1;
 125#if CONFIG_ENV_SECT_SIZE > CONFIG_ENV_SIZE
 126        ulong   up_data = 0;
 127#endif
 128
 129        debug("Protect off %08lX ... %08lX\n", (ulong)flash_addr, end_addr);
 130
 131        if (flash_sect_protect(0, (ulong)flash_addr, end_addr))
 132                goto done;
 133
 134        debug("Protect off %08lX ... %08lX\n",
 135                (ulong)flash_addr_new, end_addr_new);
 136
 137        if (flash_sect_protect(0, (ulong)flash_addr_new, end_addr_new))
 138                goto done;
 139
 140        rc = env_export(&env_new);
 141        if (rc)
 142                return rc;
 143        env_new.flags   = new_flag;
 144
 145#if CONFIG_ENV_SECT_SIZE > CONFIG_ENV_SIZE
 146        up_data = end_addr_new + 1 - ((long)flash_addr_new + CONFIG_ENV_SIZE);
 147        debug("Data to save 0x%lX\n", up_data);
 148        if (up_data) {
 149                saved_data = malloc(up_data);
 150                if (saved_data == NULL) {
 151                        printf("Unable to save the rest of sector (%ld)\n",
 152                                up_data);
 153                        goto done;
 154                }
 155                memcpy(saved_data,
 156                        (void *)((long)flash_addr_new + CONFIG_ENV_SIZE),
 157                        up_data);
 158                debug("Data (start 0x%lX, len 0x%lX) saved at 0x%p\n",
 159                        (long)flash_addr_new + CONFIG_ENV_SIZE,
 160                        up_data, saved_data);
 161        }
 162#endif
 163        puts("Erasing Flash...");
 164        debug(" %08lX ... %08lX ...", (ulong)flash_addr_new, end_addr_new);
 165
 166        if (flash_sect_erase((ulong)flash_addr_new, end_addr_new))
 167                goto done;
 168
 169        puts("Writing to Flash... ");
 170        debug(" %08lX ... %08lX ...",
 171                (ulong)&(flash_addr_new->data),
 172                sizeof(env_ptr->data) + (ulong)&(flash_addr_new->data));
 173        rc = flash_write((char *)&env_new, (ulong)flash_addr_new,
 174                         sizeof(env_new));
 175        if (rc)
 176                goto perror;
 177
 178        rc = flash_write(&flag, (ulong)&(flash_addr->flags),
 179                         sizeof(flash_addr->flags));
 180        if (rc)
 181                goto perror;
 182
 183#if CONFIG_ENV_SECT_SIZE > CONFIG_ENV_SIZE
 184        if (up_data) { /* restore the rest of sector */
 185                debug("Restoring the rest of data to 0x%lX len 0x%lX\n",
 186                        (long)flash_addr_new + CONFIG_ENV_SIZE, up_data);
 187                if (flash_write(saved_data,
 188                                (long)flash_addr_new + CONFIG_ENV_SIZE,
 189                                up_data))
 190                        goto perror;
 191        }
 192#endif
 193        puts("done\n");
 194
 195        {
 196                env_t *etmp = flash_addr;
 197                ulong ltmp = end_addr;
 198
 199                flash_addr = flash_addr_new;
 200                flash_addr_new = etmp;
 201
 202                end_addr = end_addr_new;
 203                end_addr_new = ltmp;
 204        }
 205
 206        rc = 0;
 207        goto done;
 208perror:
 209        flash_perror(rc);
 210done:
 211        free(saved_data);
 212        /* try to re-protect */
 213        flash_sect_protect(1, (ulong)flash_addr, end_addr);
 214        flash_sect_protect(1, (ulong)flash_addr_new, end_addr_new);
 215
 216        return rc;
 217}
 218#endif /* CMD_SAVEENV */
 219
 220#else /* ! CONFIG_ENV_ADDR_REDUND */
 221
 222#ifdef INITENV
 223static int env_flash_init(void)
 224{
 225        if (crc32(0, env_ptr->data, ENV_SIZE) == env_ptr->crc) {
 226                gd->env_addr    = (ulong)&(env_ptr->data);
 227                gd->env_valid   = ENV_VALID;
 228                return 0;
 229        }
 230
 231        gd->env_valid = ENV_INVALID;
 232        return 0;
 233}
 234#endif
 235
 236#ifdef CMD_SAVEENV
 237static int env_flash_save(void)
 238{
 239        env_t   env_new;
 240        int     rc = 1;
 241        char    *saved_data = NULL;
 242#if CONFIG_ENV_SECT_SIZE > CONFIG_ENV_SIZE
 243        ulong   up_data = 0;
 244
 245        up_data = end_addr + 1 - ((long)flash_addr + CONFIG_ENV_SIZE);
 246        debug("Data to save 0x%lx\n", up_data);
 247        if (up_data) {
 248                saved_data = malloc(up_data);
 249                if (saved_data == NULL) {
 250                        printf("Unable to save the rest of sector (%ld)\n",
 251                                up_data);
 252                        goto done;
 253                }
 254                memcpy(saved_data,
 255                        (void *)((long)flash_addr + CONFIG_ENV_SIZE), up_data);
 256                debug("Data (start 0x%lx, len 0x%lx) saved at 0x%lx\n",
 257                        (ulong)flash_addr + CONFIG_ENV_SIZE,
 258                        up_data,
 259                        (ulong)saved_data);
 260        }
 261#endif  /* CONFIG_ENV_SECT_SIZE */
 262
 263        debug("Protect off %08lX ... %08lX\n", (ulong)flash_addr, end_addr);
 264
 265        if (flash_sect_protect(0, (long)flash_addr, end_addr))
 266                goto done;
 267
 268        rc = env_export(&env_new);
 269        if (rc)
 270                goto done;
 271
 272        puts("Erasing Flash...");
 273        if (flash_sect_erase((long)flash_addr, end_addr))
 274                goto done;
 275
 276        puts("Writing to Flash... ");
 277        rc = flash_write((char *)&env_new, (long)flash_addr, CONFIG_ENV_SIZE);
 278        if (rc != 0)
 279                goto perror;
 280
 281#if CONFIG_ENV_SECT_SIZE > CONFIG_ENV_SIZE
 282        if (up_data) {  /* restore the rest of sector */
 283                debug("Restoring the rest of data to 0x%lx len 0x%lx\n",
 284                        (ulong)flash_addr + CONFIG_ENV_SIZE, up_data);
 285                if (flash_write(saved_data,
 286                                (long)flash_addr + CONFIG_ENV_SIZE,
 287                                up_data))
 288                        goto perror;
 289        }
 290#endif
 291        puts("done\n");
 292        rc = 0;
 293        goto done;
 294perror:
 295        flash_perror(rc);
 296done:
 297        free(saved_data);
 298        /* try to re-protect */
 299        flash_sect_protect(1, (long)flash_addr, end_addr);
 300        return rc;
 301}
 302#endif /* CMD_SAVEENV */
 303
 304#endif /* CONFIG_ENV_ADDR_REDUND */
 305
 306#ifdef LOADENV
 307static int env_flash_load(void)
 308{
 309#ifdef CONFIG_ENV_ADDR_REDUND
 310        if (gd->env_addr != (ulong)&(flash_addr->data)) {
 311                env_t *etmp = flash_addr;
 312                ulong ltmp = end_addr;
 313
 314                flash_addr = flash_addr_new;
 315                flash_addr_new = etmp;
 316
 317                end_addr = end_addr_new;
 318                end_addr_new = ltmp;
 319        }
 320
 321        if (flash_addr_new->flags != ENV_REDUND_OBSOLETE &&
 322            crc32(0, flash_addr_new->data, ENV_SIZE) == flash_addr_new->crc) {
 323                char flag = ENV_REDUND_OBSOLETE;
 324
 325                gd->env_valid = ENV_REDUND;
 326                flash_sect_protect(0, (ulong)flash_addr_new, end_addr_new);
 327                flash_write(&flag,
 328                            (ulong)&(flash_addr_new->flags),
 329                            sizeof(flash_addr_new->flags));
 330                flash_sect_protect(1, (ulong)flash_addr_new, end_addr_new);
 331        }
 332
 333        if (flash_addr->flags != ENV_REDUND_ACTIVE &&
 334            (flash_addr->flags & ENV_REDUND_ACTIVE) == ENV_REDUND_ACTIVE) {
 335                char flag = ENV_REDUND_ACTIVE;
 336
 337                gd->env_valid = ENV_REDUND;
 338                flash_sect_protect(0, (ulong)flash_addr, end_addr);
 339                flash_write(&flag,
 340                            (ulong)&(flash_addr->flags),
 341                            sizeof(flash_addr->flags));
 342                flash_sect_protect(1, (ulong)flash_addr, end_addr);
 343        }
 344
 345        if (gd->env_valid == ENV_REDUND)
 346                puts("*** Warning - some problems detected "
 347                     "reading environment; recovered successfully\n\n");
 348#endif /* CONFIG_ENV_ADDR_REDUND */
 349
 350        return env_import((char *)flash_addr, 1, H_EXTERNAL);
 351}
 352#endif /* LOADENV */
 353
 354U_BOOT_ENV_LOCATION(flash) = {
 355        .location       = ENVL_FLASH,
 356        ENV_NAME("Flash")
 357#ifdef LOADENV
 358        .load           = env_flash_load,
 359#endif
 360#ifdef CMD_SAVEENV
 361        .save           = env_save_ptr(env_flash_save),
 362#endif
 363#ifdef INITENV
 364        .init           = env_flash_init,
 365#endif
 366};
 367