Maximilian Schwerin | 9765c08 | 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 | bd8ec7e | 2013-10-07 13:07:26 +0200 | [diff] [blame] | 7 | * SPDX-License-Identifier: GPL-2.0+ |
Maximilian Schwerin | 9765c08 | 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 | 9765c08 | 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 | a840b8e | 2013-04-05 14:55:21 -0400 | [diff] [blame] | 39 | env_t env_new; |
Maximilian Schwerin | 9765c08 | 2012-03-12 23:57:50 +0000 | [diff] [blame] | 40 | ssize_t len; |
| 41 | char *res; |
| 42 | block_dev_desc_t *dev_desc = NULL; |
| 43 | int dev = FAT_ENV_DEVICE; |
| 44 | int part = FAT_ENV_PART; |
Igor Grinberg | 49eee6e | 2012-09-23 05:25:21 +0000 | [diff] [blame] | 45 | int err; |
Maximilian Schwerin | 9765c08 | 2012-03-12 23:57:50 +0000 | [diff] [blame] | 46 | |
Tom Rini | a840b8e | 2013-04-05 14:55:21 -0400 | [diff] [blame] | 47 | res = (char *)&env_new.data; |
Joe Hershberger | 79a905e | 2012-12-11 22:16:23 -0600 | [diff] [blame] | 48 | len = hexport_r(&env_htab, '\0', 0, &res, ENV_SIZE, 0, NULL); |
Maximilian Schwerin | 9765c08 | 2012-03-12 23:57:50 +0000 | [diff] [blame] | 49 | if (len < 0) { |
| 50 | error("Cannot export environment: errno = %d\n", errno); |
| 51 | return 1; |
| 52 | } |
| 53 | |
| 54 | #ifdef CONFIG_MMC |
Igor Grinberg | 49eee6e | 2012-09-23 05:25:21 +0000 | [diff] [blame] | 55 | if (strcmp(FAT_ENV_INTERFACE, "mmc") == 0) { |
Maximilian Schwerin | 9765c08 | 2012-03-12 23:57:50 +0000 | [diff] [blame] | 56 | struct mmc *mmc = find_mmc_device(dev); |
| 57 | |
| 58 | if (!mmc) { |
| 59 | printf("no mmc device at slot %x\n", dev); |
| 60 | return 1; |
| 61 | } |
| 62 | |
| 63 | mmc->has_init = 0; |
| 64 | mmc_init(mmc); |
| 65 | } |
| 66 | #endif /* CONFIG_MMC */ |
| 67 | |
| 68 | dev_desc = get_dev(FAT_ENV_INTERFACE, dev); |
| 69 | if (dev_desc == NULL) { |
| 70 | printf("Failed to find %s%d\n", |
| 71 | FAT_ENV_INTERFACE, dev); |
| 72 | return 1; |
| 73 | } |
Igor Grinberg | 49eee6e | 2012-09-23 05:25:21 +0000 | [diff] [blame] | 74 | |
| 75 | err = fat_register_device(dev_desc, part); |
| 76 | if (err) { |
Maximilian Schwerin | 9765c08 | 2012-03-12 23:57:50 +0000 | [diff] [blame] | 77 | printf("Failed to register %s%d:%d\n", |
| 78 | FAT_ENV_INTERFACE, dev, part); |
| 79 | return 1; |
| 80 | } |
| 81 | |
Tom Rini | a840b8e | 2013-04-05 14:55:21 -0400 | [diff] [blame] | 82 | env_new.crc = crc32(0, env_new.data, ENV_SIZE); |
| 83 | err = file_fat_write(FAT_ENV_FILE, (void *)&env_new, sizeof(env_t)); |
Igor Grinberg | 49eee6e | 2012-09-23 05:25:21 +0000 | [diff] [blame] | 84 | if (err == -1) { |
Maximilian Schwerin | 9765c08 | 2012-03-12 23:57:50 +0000 | [diff] [blame] | 85 | printf("\n** Unable to write \"%s\" from %s%d:%d **\n", |
| 86 | FAT_ENV_FILE, FAT_ENV_INTERFACE, dev, part); |
| 87 | return 1; |
| 88 | } |
| 89 | |
| 90 | puts("done\n"); |
| 91 | return 0; |
| 92 | } |
| 93 | #endif /* CONFIG_CMD_SAVEENV */ |
| 94 | |
| 95 | void env_relocate_spec(void) |
| 96 | { |
Tom Rini | a840b8e | 2013-04-05 14:55:21 -0400 | [diff] [blame] | 97 | char buf[CONFIG_ENV_SIZE]; |
Maximilian Schwerin | 9765c08 | 2012-03-12 23:57:50 +0000 | [diff] [blame] | 98 | block_dev_desc_t *dev_desc = NULL; |
| 99 | int dev = FAT_ENV_DEVICE; |
| 100 | int part = FAT_ENV_PART; |
Igor Grinberg | 49eee6e | 2012-09-23 05:25:21 +0000 | [diff] [blame] | 101 | int err; |
Maximilian Schwerin | 9765c08 | 2012-03-12 23:57:50 +0000 | [diff] [blame] | 102 | |
| 103 | #ifdef CONFIG_MMC |
Igor Grinberg | 49eee6e | 2012-09-23 05:25:21 +0000 | [diff] [blame] | 104 | if (strcmp(FAT_ENV_INTERFACE, "mmc") == 0) { |
Maximilian Schwerin | 9765c08 | 2012-03-12 23:57:50 +0000 | [diff] [blame] | 105 | struct mmc *mmc = find_mmc_device(dev); |
| 106 | |
| 107 | if (!mmc) { |
| 108 | printf("no mmc device at slot %x\n", dev); |
| 109 | set_default_env(NULL); |
| 110 | return; |
| 111 | } |
| 112 | |
| 113 | mmc->has_init = 0; |
| 114 | mmc_init(mmc); |
| 115 | } |
| 116 | #endif /* CONFIG_MMC */ |
| 117 | |
| 118 | dev_desc = get_dev(FAT_ENV_INTERFACE, dev); |
| 119 | if (dev_desc == NULL) { |
| 120 | printf("Failed to find %s%d\n", |
| 121 | FAT_ENV_INTERFACE, dev); |
| 122 | set_default_env(NULL); |
| 123 | return; |
| 124 | } |
Igor Grinberg | 49eee6e | 2012-09-23 05:25:21 +0000 | [diff] [blame] | 125 | |
| 126 | err = fat_register_device(dev_desc, part); |
| 127 | if (err) { |
Maximilian Schwerin | 9765c08 | 2012-03-12 23:57:50 +0000 | [diff] [blame] | 128 | printf("Failed to register %s%d:%d\n", |
| 129 | FAT_ENV_INTERFACE, dev, part); |
| 130 | set_default_env(NULL); |
| 131 | return; |
| 132 | } |
| 133 | |
Igor Grinberg | 49eee6e | 2012-09-23 05:25:21 +0000 | [diff] [blame] | 134 | err = file_fat_read(FAT_ENV_FILE, (uchar *)&buf, CONFIG_ENV_SIZE); |
| 135 | if (err == -1) { |
Maximilian Schwerin | 9765c08 | 2012-03-12 23:57:50 +0000 | [diff] [blame] | 136 | printf("\n** Unable to read \"%s\" from %s%d:%d **\n", |
| 137 | FAT_ENV_FILE, FAT_ENV_INTERFACE, dev, part); |
| 138 | set_default_env(NULL); |
| 139 | return; |
| 140 | } |
| 141 | |
| 142 | env_import(buf, 1); |
| 143 | } |