Maximilian Schwerin | 57210c7 | 2012-03-12 23:57:50 +0000 | [diff] [blame] | 1 | /* |
| 2 | * (c) Copyright 2011 by Tigris Elektronik GmbH |
| 3 | * |
| 4 | * Author: |
| 5 | * Maximilian Schwerin <mvs@tigris.de> |
| 6 | * |
Wolfgang Denk | 3765b3e | 2013-10-07 13:07:26 +0200 | [diff] [blame] | 7 | * SPDX-License-Identifier: GPL-2.0+ |
Maximilian Schwerin | 57210c7 | 2012-03-12 23:57:50 +0000 | [diff] [blame] | 8 | */ |
| 9 | |
| 10 | #include <common.h> |
| 11 | |
| 12 | #include <command.h> |
| 13 | #include <environment.h> |
| 14 | #include <linux/stddef.h> |
| 15 | #include <malloc.h> |
| 16 | #include <search.h> |
| 17 | #include <errno.h> |
| 18 | #include <fat.h> |
| 19 | #include <mmc.h> |
| 20 | |
| 21 | char *env_name_spec = "FAT"; |
| 22 | |
| 23 | env_t *env_ptr; |
| 24 | |
| 25 | DECLARE_GLOBAL_DATA_PTR; |
| 26 | |
Maximilian Schwerin | 57210c7 | 2012-03-12 23:57:50 +0000 | [diff] [blame] | 27 | int env_init(void) |
| 28 | { |
| 29 | /* use default */ |
| 30 | gd->env_addr = (ulong)&default_environment[0]; |
| 31 | gd->env_valid = 1; |
| 32 | |
| 33 | return 0; |
| 34 | } |
| 35 | |
| 36 | #ifdef CONFIG_CMD_SAVEENV |
| 37 | int saveenv(void) |
| 38 | { |
Tom Rini | cd0f4fa | 2013-04-05 14:55:21 -0400 | [diff] [blame] | 39 | env_t env_new; |
Maximilian Schwerin | 57210c7 | 2012-03-12 23:57:50 +0000 | [diff] [blame] | 40 | block_dev_desc_t *dev_desc = NULL; |
| 41 | int dev = FAT_ENV_DEVICE; |
| 42 | int part = FAT_ENV_PART; |
Igor Grinberg | 9aa90c1 | 2012-09-23 05:25:21 +0000 | [diff] [blame] | 43 | int err; |
Maximilian Schwerin | 57210c7 | 2012-03-12 23:57:50 +0000 | [diff] [blame] | 44 | |
Marek Vasut | 7ce1526 | 2014-03-05 19:59:50 +0100 | [diff] [blame] | 45 | err = env_export(&env_new); |
| 46 | if (err) |
| 47 | return err; |
Maximilian Schwerin | 57210c7 | 2012-03-12 23:57:50 +0000 | [diff] [blame] | 48 | |
| 49 | #ifdef CONFIG_MMC |
Igor Grinberg | 9aa90c1 | 2012-09-23 05:25:21 +0000 | [diff] [blame] | 50 | if (strcmp(FAT_ENV_INTERFACE, "mmc") == 0) { |
Maximilian Schwerin | 57210c7 | 2012-03-12 23:57:50 +0000 | [diff] [blame] | 51 | struct mmc *mmc = find_mmc_device(dev); |
| 52 | |
| 53 | if (!mmc) { |
| 54 | printf("no mmc device at slot %x\n", dev); |
| 55 | return 1; |
| 56 | } |
| 57 | |
| 58 | mmc->has_init = 0; |
| 59 | mmc_init(mmc); |
| 60 | } |
| 61 | #endif /* CONFIG_MMC */ |
| 62 | |
| 63 | dev_desc = get_dev(FAT_ENV_INTERFACE, dev); |
| 64 | if (dev_desc == NULL) { |
| 65 | printf("Failed to find %s%d\n", |
| 66 | FAT_ENV_INTERFACE, dev); |
| 67 | return 1; |
| 68 | } |
Igor Grinberg | 9aa90c1 | 2012-09-23 05:25:21 +0000 | [diff] [blame] | 69 | |
| 70 | err = fat_register_device(dev_desc, part); |
| 71 | if (err) { |
Maximilian Schwerin | 57210c7 | 2012-03-12 23:57:50 +0000 | [diff] [blame] | 72 | printf("Failed to register %s%d:%d\n", |
| 73 | FAT_ENV_INTERFACE, dev, part); |
| 74 | return 1; |
| 75 | } |
| 76 | |
Tom Rini | cd0f4fa | 2013-04-05 14:55:21 -0400 | [diff] [blame] | 77 | err = file_fat_write(FAT_ENV_FILE, (void *)&env_new, sizeof(env_t)); |
Igor Grinberg | 9aa90c1 | 2012-09-23 05:25:21 +0000 | [diff] [blame] | 78 | if (err == -1) { |
Maximilian Schwerin | 57210c7 | 2012-03-12 23:57:50 +0000 | [diff] [blame] | 79 | printf("\n** Unable to write \"%s\" from %s%d:%d **\n", |
| 80 | FAT_ENV_FILE, FAT_ENV_INTERFACE, dev, part); |
| 81 | return 1; |
| 82 | } |
| 83 | |
| 84 | puts("done\n"); |
| 85 | return 0; |
| 86 | } |
| 87 | #endif /* CONFIG_CMD_SAVEENV */ |
| 88 | |
| 89 | void env_relocate_spec(void) |
| 90 | { |
Tom Rini | cd0f4fa | 2013-04-05 14:55:21 -0400 | [diff] [blame] | 91 | char buf[CONFIG_ENV_SIZE]; |
Maximilian Schwerin | 57210c7 | 2012-03-12 23:57:50 +0000 | [diff] [blame] | 92 | block_dev_desc_t *dev_desc = NULL; |
| 93 | int dev = FAT_ENV_DEVICE; |
| 94 | int part = FAT_ENV_PART; |
Igor Grinberg | 9aa90c1 | 2012-09-23 05:25:21 +0000 | [diff] [blame] | 95 | int err; |
Maximilian Schwerin | 57210c7 | 2012-03-12 23:57:50 +0000 | [diff] [blame] | 96 | |
| 97 | #ifdef CONFIG_MMC |
Igor Grinberg | 9aa90c1 | 2012-09-23 05:25:21 +0000 | [diff] [blame] | 98 | if (strcmp(FAT_ENV_INTERFACE, "mmc") == 0) { |
Maximilian Schwerin | 57210c7 | 2012-03-12 23:57:50 +0000 | [diff] [blame] | 99 | struct mmc *mmc = find_mmc_device(dev); |
| 100 | |
| 101 | if (!mmc) { |
| 102 | printf("no mmc device at slot %x\n", dev); |
| 103 | set_default_env(NULL); |
| 104 | return; |
| 105 | } |
| 106 | |
| 107 | mmc->has_init = 0; |
| 108 | mmc_init(mmc); |
| 109 | } |
| 110 | #endif /* CONFIG_MMC */ |
| 111 | |
| 112 | dev_desc = get_dev(FAT_ENV_INTERFACE, dev); |
| 113 | if (dev_desc == NULL) { |
| 114 | printf("Failed to find %s%d\n", |
| 115 | FAT_ENV_INTERFACE, dev); |
| 116 | set_default_env(NULL); |
| 117 | return; |
| 118 | } |
Igor Grinberg | 9aa90c1 | 2012-09-23 05:25:21 +0000 | [diff] [blame] | 119 | |
| 120 | err = fat_register_device(dev_desc, part); |
| 121 | if (err) { |
Maximilian Schwerin | 57210c7 | 2012-03-12 23:57:50 +0000 | [diff] [blame] | 122 | printf("Failed to register %s%d:%d\n", |
| 123 | FAT_ENV_INTERFACE, dev, part); |
| 124 | set_default_env(NULL); |
| 125 | return; |
| 126 | } |
| 127 | |
Igor Grinberg | 9aa90c1 | 2012-09-23 05:25:21 +0000 | [diff] [blame] | 128 | err = file_fat_read(FAT_ENV_FILE, (uchar *)&buf, CONFIG_ENV_SIZE); |
| 129 | if (err == -1) { |
Maximilian Schwerin | 57210c7 | 2012-03-12 23:57:50 +0000 | [diff] [blame] | 130 | printf("\n** Unable to read \"%s\" from %s%d:%d **\n", |
| 131 | FAT_ENV_FILE, FAT_ENV_INTERFACE, dev, part); |
| 132 | set_default_env(NULL); |
| 133 | return; |
| 134 | } |
| 135 | |
| 136 | env_import(buf, 1); |
| 137 | } |