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 | block_dev_desc_t *dev_desc = NULL; |
Wu, Josh | 9b899f2 | 2014-06-24 17:31:02 +0800 | [diff] [blame] | 41 | disk_partition_t info; |
| 42 | int dev, part; |
Igor Grinberg | 49eee6e | 2012-09-23 05:25:21 +0000 | [diff] [blame] | 43 | int err; |
Maximilian Schwerin | 9765c08 | 2012-03-12 23:57:50 +0000 | [diff] [blame] | 44 | |
Marek Vasut | d73c129 | 2014-03-05 19:59:50 +0100 | [diff] [blame] | 45 | err = env_export(&env_new); |
| 46 | if (err) |
| 47 | return err; |
Maximilian Schwerin | 9765c08 | 2012-03-12 23:57:50 +0000 | [diff] [blame] | 48 | |
Wu, Josh | 9b899f2 | 2014-06-24 17:31:02 +0800 | [diff] [blame] | 49 | part = get_device_and_partition(FAT_ENV_INTERFACE, |
| 50 | FAT_ENV_DEVICE_AND_PART, |
| 51 | &dev_desc, &info, 1); |
| 52 | if (part < 0) |
Maximilian Schwerin | 9765c08 | 2012-03-12 23:57:50 +0000 | [diff] [blame] | 53 | return 1; |
Igor Grinberg | 49eee6e | 2012-09-23 05:25:21 +0000 | [diff] [blame] | 54 | |
Wu, Josh | 9b899f2 | 2014-06-24 17:31:02 +0800 | [diff] [blame] | 55 | dev = dev_desc->dev; |
| 56 | if (fat_set_blk_dev(dev_desc, &info) != 0) { |
| 57 | printf("\n** Unable to use %s %d:%d for saveenv **\n", |
| 58 | FAT_ENV_INTERFACE, dev, part); |
Maximilian Schwerin | 9765c08 | 2012-03-12 23:57:50 +0000 | [diff] [blame] | 59 | return 1; |
| 60 | } |
| 61 | |
Tom Rini | a840b8e | 2013-04-05 14:55:21 -0400 | [diff] [blame] | 62 | 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] | 63 | if (err == -1) { |
Maximilian Schwerin | 9765c08 | 2012-03-12 23:57:50 +0000 | [diff] [blame] | 64 | printf("\n** Unable to write \"%s\" from %s%d:%d **\n", |
| 65 | FAT_ENV_FILE, FAT_ENV_INTERFACE, dev, part); |
| 66 | return 1; |
| 67 | } |
| 68 | |
| 69 | puts("done\n"); |
| 70 | return 0; |
| 71 | } |
| 72 | #endif /* CONFIG_CMD_SAVEENV */ |
| 73 | |
| 74 | void env_relocate_spec(void) |
| 75 | { |
Tom Rini | 0b635ed | 2014-08-01 13:59:10 -0400 | [diff] [blame] | 76 | ALLOC_CACHE_ALIGN_BUFFER(char, buf, CONFIG_ENV_SIZE); |
Maximilian Schwerin | 9765c08 | 2012-03-12 23:57:50 +0000 | [diff] [blame] | 77 | block_dev_desc_t *dev_desc = NULL; |
Wu, Josh | 9b899f2 | 2014-06-24 17:31:02 +0800 | [diff] [blame] | 78 | disk_partition_t info; |
| 79 | int dev, part; |
Igor Grinberg | 49eee6e | 2012-09-23 05:25:21 +0000 | [diff] [blame] | 80 | int err; |
Maximilian Schwerin | 9765c08 | 2012-03-12 23:57:50 +0000 | [diff] [blame] | 81 | |
Wu, Josh | 9b899f2 | 2014-06-24 17:31:02 +0800 | [diff] [blame] | 82 | part = get_device_and_partition(FAT_ENV_INTERFACE, |
| 83 | FAT_ENV_DEVICE_AND_PART, |
| 84 | &dev_desc, &info, 1); |
| 85 | if (part < 0) |
| 86 | goto err_env_relocate; |
Maximilian Schwerin | 9765c08 | 2012-03-12 23:57:50 +0000 | [diff] [blame] | 87 | |
Wu, Josh | 9b899f2 | 2014-06-24 17:31:02 +0800 | [diff] [blame] | 88 | dev = dev_desc->dev; |
| 89 | if (fat_set_blk_dev(dev_desc, &info) != 0) { |
| 90 | printf("\n** Unable to use %s %d:%d for loading the env **\n", |
| 91 | FAT_ENV_INTERFACE, dev, part); |
| 92 | goto err_env_relocate; |
Maximilian Schwerin | 9765c08 | 2012-03-12 23:57:50 +0000 | [diff] [blame] | 93 | } |
Maximilian Schwerin | 9765c08 | 2012-03-12 23:57:50 +0000 | [diff] [blame] | 94 | |
Tom Rini | 0b635ed | 2014-08-01 13:59:10 -0400 | [diff] [blame] | 95 | err = file_fat_read(FAT_ENV_FILE, buf, CONFIG_ENV_SIZE); |
Igor Grinberg | 49eee6e | 2012-09-23 05:25:21 +0000 | [diff] [blame] | 96 | if (err == -1) { |
Maximilian Schwerin | 9765c08 | 2012-03-12 23:57:50 +0000 | [diff] [blame] | 97 | printf("\n** Unable to read \"%s\" from %s%d:%d **\n", |
| 98 | FAT_ENV_FILE, FAT_ENV_INTERFACE, dev, part); |
Wu, Josh | 9b899f2 | 2014-06-24 17:31:02 +0800 | [diff] [blame] | 99 | goto err_env_relocate; |
Maximilian Schwerin | 9765c08 | 2012-03-12 23:57:50 +0000 | [diff] [blame] | 100 | } |
| 101 | |
| 102 | env_import(buf, 1); |
Wu, Josh | 9b899f2 | 2014-06-24 17:31:02 +0800 | [diff] [blame] | 103 | return; |
| 104 | |
| 105 | err_env_relocate: |
| 106 | set_default_env(NULL); |
Maximilian Schwerin | 9765c08 | 2012-03-12 23:57:50 +0000 | [diff] [blame] | 107 | } |