Tom Rini | 10e4779 | 2018-05-06 17:58:06 -0400 | [diff] [blame] | 1 | // SPDX-License-Identifier: GPL-2.0+ |
Maximilian Schwerin | 9765c08 | 2012-03-12 23:57:50 +0000 | [diff] [blame] | 2 | /* |
| 3 | * (c) Copyright 2011 by Tigris Elektronik GmbH |
| 4 | * |
| 5 | * Author: |
| 6 | * Maximilian Schwerin <mvs@tigris.de> |
Maximilian Schwerin | 9765c08 | 2012-03-12 23:57:50 +0000 | [diff] [blame] | 7 | */ |
| 8 | |
| 9 | #include <common.h> |
| 10 | |
| 11 | #include <command.h> |
| 12 | #include <environment.h> |
| 13 | #include <linux/stddef.h> |
| 14 | #include <malloc.h> |
Simon Glass | 2dd337a | 2015-09-02 17:24:58 -0600 | [diff] [blame] | 15 | #include <memalign.h> |
Maximilian Schwerin | 9765c08 | 2012-03-12 23:57:50 +0000 | [diff] [blame] | 16 | #include <search.h> |
| 17 | #include <errno.h> |
| 18 | #include <fat.h> |
| 19 | #include <mmc.h> |
| 20 | |
Simon Glass | c10a88e | 2017-08-03 12:21:58 -0600 | [diff] [blame] | 21 | #ifdef CONFIG_SPL_BUILD |
| 22 | /* TODO(sjg@chromium.org): Figure out why this is needed */ |
| 23 | # if !defined(CONFIG_TARGET_AM335X_EVM) || defined(CONFIG_SPL_OS_BOOT) |
| 24 | # define LOADENV |
| 25 | # endif |
| 26 | #else |
| 27 | # define LOADENV |
| 28 | # if defined(CONFIG_CMD_SAVEENV) |
| 29 | # define CMD_SAVEENV |
| 30 | # endif |
| 31 | #endif |
| 32 | |
Simon Glass | c10a88e | 2017-08-03 12:21:58 -0600 | [diff] [blame] | 33 | #ifdef CMD_SAVEENV |
Simon Glass | 082af92 | 2017-08-03 12:22:01 -0600 | [diff] [blame] | 34 | static int env_fat_save(void) |
Maximilian Schwerin | 9765c08 | 2012-03-12 23:57:50 +0000 | [diff] [blame] | 35 | { |
Alex Kiernan | 28a2137 | 2018-02-07 20:01:54 +0000 | [diff] [blame] | 36 | env_t __aligned(ARCH_DMA_MINALIGN) env_new; |
Simon Glass | e339475 | 2016-02-29 15:25:34 -0700 | [diff] [blame] | 37 | struct blk_desc *dev_desc = NULL; |
Wu, Josh | 9b899f2 | 2014-06-24 17:31:02 +0800 | [diff] [blame] | 38 | disk_partition_t info; |
| 39 | int dev, part; |
Igor Grinberg | 49eee6e | 2012-09-23 05:25:21 +0000 | [diff] [blame] | 40 | int err; |
Suriyan Ramasami | 441c223 | 2014-11-17 14:39:35 -0800 | [diff] [blame] | 41 | loff_t size; |
Maximilian Schwerin | 9765c08 | 2012-03-12 23:57:50 +0000 | [diff] [blame] | 42 | |
Marek Vasut | d73c129 | 2014-03-05 19:59:50 +0100 | [diff] [blame] | 43 | err = env_export(&env_new); |
| 44 | if (err) |
| 45 | return err; |
Maximilian Schwerin | 9765c08 | 2012-03-12 23:57:50 +0000 | [diff] [blame] | 46 | |
Tom Rini | 17f14d9 | 2017-07-26 21:48:00 -0400 | [diff] [blame] | 47 | part = blk_get_device_part_str(CONFIG_ENV_FAT_INTERFACE, |
| 48 | CONFIG_ENV_FAT_DEVICE_AND_PART, |
Wu, Josh | 9b899f2 | 2014-06-24 17:31:02 +0800 | [diff] [blame] | 49 | &dev_desc, &info, 1); |
| 50 | if (part < 0) |
Maximilian Schwerin | 9765c08 | 2012-03-12 23:57:50 +0000 | [diff] [blame] | 51 | return 1; |
Igor Grinberg | 49eee6e | 2012-09-23 05:25:21 +0000 | [diff] [blame] | 52 | |
Simon Glass | 2f26fff | 2016-02-29 15:25:51 -0700 | [diff] [blame] | 53 | dev = dev_desc->devnum; |
Wu, Josh | 9b899f2 | 2014-06-24 17:31:02 +0800 | [diff] [blame] | 54 | if (fat_set_blk_dev(dev_desc, &info) != 0) { |
Maxime Ripard | be6b2dc | 2018-01-23 21:16:55 +0100 | [diff] [blame] | 55 | /* |
| 56 | * This printf is embedded in the messages from env_save that |
| 57 | * will calling it. The missing \n is intentional. |
| 58 | */ |
| 59 | printf("Unable to use %s %d:%d... ", |
Tom Rini | 17f14d9 | 2017-07-26 21:48:00 -0400 | [diff] [blame] | 60 | CONFIG_ENV_FAT_INTERFACE, dev, part); |
Maximilian Schwerin | 9765c08 | 2012-03-12 23:57:50 +0000 | [diff] [blame] | 61 | return 1; |
| 62 | } |
| 63 | |
Tom Rini | 17f14d9 | 2017-07-26 21:48:00 -0400 | [diff] [blame] | 64 | err = file_fat_write(CONFIG_ENV_FAT_FILE, (void *)&env_new, 0, sizeof(env_t), |
Suriyan Ramasami | 441c223 | 2014-11-17 14:39:35 -0800 | [diff] [blame] | 65 | &size); |
Igor Grinberg | 49eee6e | 2012-09-23 05:25:21 +0000 | [diff] [blame] | 66 | if (err == -1) { |
Maxime Ripard | be6b2dc | 2018-01-23 21:16:55 +0100 | [diff] [blame] | 67 | /* |
| 68 | * This printf is embedded in the messages from env_save that |
| 69 | * will calling it. The missing \n is intentional. |
| 70 | */ |
| 71 | printf("Unable to write \"%s\" from %s%d:%d... ", |
Tom Rini | 17f14d9 | 2017-07-26 21:48:00 -0400 | [diff] [blame] | 72 | CONFIG_ENV_FAT_FILE, CONFIG_ENV_FAT_INTERFACE, dev, part); |
Maximilian Schwerin | 9765c08 | 2012-03-12 23:57:50 +0000 | [diff] [blame] | 73 | return 1; |
| 74 | } |
| 75 | |
Maximilian Schwerin | 9765c08 | 2012-03-12 23:57:50 +0000 | [diff] [blame] | 76 | return 0; |
| 77 | } |
Simon Glass | c10a88e | 2017-08-03 12:21:58 -0600 | [diff] [blame] | 78 | #endif /* CMD_SAVEENV */ |
Maximilian Schwerin | 9765c08 | 2012-03-12 23:57:50 +0000 | [diff] [blame] | 79 | |
Simon Glass | c10a88e | 2017-08-03 12:21:58 -0600 | [diff] [blame] | 80 | #ifdef LOADENV |
Simon Glass | 9977849 | 2017-08-03 12:22:17 -0600 | [diff] [blame] | 81 | static int env_fat_load(void) |
Maximilian Schwerin | 9765c08 | 2012-03-12 23:57:50 +0000 | [diff] [blame] | 82 | { |
Tom Rini | 0b635ed | 2014-08-01 13:59:10 -0400 | [diff] [blame] | 83 | ALLOC_CACHE_ALIGN_BUFFER(char, buf, CONFIG_ENV_SIZE); |
Simon Glass | e339475 | 2016-02-29 15:25:34 -0700 | [diff] [blame] | 84 | struct blk_desc *dev_desc = NULL; |
Wu, Josh | 9b899f2 | 2014-06-24 17:31:02 +0800 | [diff] [blame] | 85 | disk_partition_t info; |
| 86 | int dev, part; |
Igor Grinberg | 49eee6e | 2012-09-23 05:25:21 +0000 | [diff] [blame] | 87 | int err; |
Maximilian Schwerin | 9765c08 | 2012-03-12 23:57:50 +0000 | [diff] [blame] | 88 | |
Heinrich Schuchardt | 9991fe8 | 2018-04-14 15:41:00 +0200 | [diff] [blame] | 89 | #ifdef CONFIG_MMC |
Faiz Abbas | aae518f | 2018-02-12 19:24:31 +0530 | [diff] [blame] | 90 | if (!strcmp(CONFIG_ENV_FAT_INTERFACE, "mmc")) |
| 91 | mmc_initialize(NULL); |
Heinrich Schuchardt | 9991fe8 | 2018-04-14 15:41:00 +0200 | [diff] [blame] | 92 | #endif |
Faiz Abbas | aae518f | 2018-02-12 19:24:31 +0530 | [diff] [blame] | 93 | |
Tom Rini | 17f14d9 | 2017-07-26 21:48:00 -0400 | [diff] [blame] | 94 | part = blk_get_device_part_str(CONFIG_ENV_FAT_INTERFACE, |
| 95 | CONFIG_ENV_FAT_DEVICE_AND_PART, |
Wu, Josh | 9b899f2 | 2014-06-24 17:31:02 +0800 | [diff] [blame] | 96 | &dev_desc, &info, 1); |
| 97 | if (part < 0) |
| 98 | goto err_env_relocate; |
Maximilian Schwerin | 9765c08 | 2012-03-12 23:57:50 +0000 | [diff] [blame] | 99 | |
Simon Glass | 2f26fff | 2016-02-29 15:25:51 -0700 | [diff] [blame] | 100 | dev = dev_desc->devnum; |
Wu, Josh | 9b899f2 | 2014-06-24 17:31:02 +0800 | [diff] [blame] | 101 | if (fat_set_blk_dev(dev_desc, &info) != 0) { |
Maxime Ripard | be6b2dc | 2018-01-23 21:16:55 +0100 | [diff] [blame] | 102 | /* |
| 103 | * This printf is embedded in the messages from env_save that |
| 104 | * will calling it. The missing \n is intentional. |
| 105 | */ |
| 106 | printf("Unable to use %s %d:%d... ", |
Tom Rini | 17f14d9 | 2017-07-26 21:48:00 -0400 | [diff] [blame] | 107 | CONFIG_ENV_FAT_INTERFACE, dev, part); |
Wu, Josh | 9b899f2 | 2014-06-24 17:31:02 +0800 | [diff] [blame] | 108 | goto err_env_relocate; |
Maximilian Schwerin | 9765c08 | 2012-03-12 23:57:50 +0000 | [diff] [blame] | 109 | } |
Maximilian Schwerin | 9765c08 | 2012-03-12 23:57:50 +0000 | [diff] [blame] | 110 | |
Tom Rini | 17f14d9 | 2017-07-26 21:48:00 -0400 | [diff] [blame] | 111 | err = file_fat_read(CONFIG_ENV_FAT_FILE, buf, CONFIG_ENV_SIZE); |
Igor Grinberg | 49eee6e | 2012-09-23 05:25:21 +0000 | [diff] [blame] | 112 | if (err == -1) { |
Maxime Ripard | be6b2dc | 2018-01-23 21:16:55 +0100 | [diff] [blame] | 113 | /* |
| 114 | * This printf is embedded in the messages from env_save that |
| 115 | * will calling it. The missing \n is intentional. |
| 116 | */ |
| 117 | printf("Unable to read \"%s\" from %s%d:%d... ", |
Tom Rini | 17f14d9 | 2017-07-26 21:48:00 -0400 | [diff] [blame] | 118 | CONFIG_ENV_FAT_FILE, CONFIG_ENV_FAT_INTERFACE, dev, part); |
Wu, Josh | 9b899f2 | 2014-06-24 17:31:02 +0800 | [diff] [blame] | 119 | goto err_env_relocate; |
Maximilian Schwerin | 9765c08 | 2012-03-12 23:57:50 +0000 | [diff] [blame] | 120 | } |
| 121 | |
Simon Goldschmidt | 35b4c32 | 2018-01-31 14:47:12 +0100 | [diff] [blame] | 122 | return env_import(buf, 1); |
Wu, Josh | 9b899f2 | 2014-06-24 17:31:02 +0800 | [diff] [blame] | 123 | |
| 124 | err_env_relocate: |
Yaniv Levinsky | b2bb9bc | 2018-06-24 19:16:57 +0300 | [diff] [blame] | 125 | set_default_env(NULL, 0); |
Simon Glass | 9977849 | 2017-08-03 12:22:17 -0600 | [diff] [blame] | 126 | |
| 127 | return -EIO; |
Maximilian Schwerin | 9765c08 | 2012-03-12 23:57:50 +0000 | [diff] [blame] | 128 | } |
Simon Glass | c10a88e | 2017-08-03 12:21:58 -0600 | [diff] [blame] | 129 | #endif /* LOADENV */ |
| 130 | |
| 131 | U_BOOT_ENV_LOCATION(fat) = { |
| 132 | .location = ENVL_FAT, |
Simon Glass | d8273ed | 2017-08-03 12:22:03 -0600 | [diff] [blame] | 133 | ENV_NAME("FAT") |
Simon Glass | c10a88e | 2017-08-03 12:21:58 -0600 | [diff] [blame] | 134 | #ifdef LOADENV |
Simon Glass | 082af92 | 2017-08-03 12:22:01 -0600 | [diff] [blame] | 135 | .load = env_fat_load, |
Simon Glass | c10a88e | 2017-08-03 12:21:58 -0600 | [diff] [blame] | 136 | #endif |
| 137 | #ifdef CMD_SAVEENV |
Simon Glass | 082af92 | 2017-08-03 12:22:01 -0600 | [diff] [blame] | 138 | .save = env_save_ptr(env_fat_save), |
Simon Glass | c10a88e | 2017-08-03 12:21:58 -0600 | [diff] [blame] | 139 | #endif |
Simon Glass | c10a88e | 2017-08-03 12:21:58 -0600 | [diff] [blame] | 140 | }; |