Tom Rini | 83d290c | 2018-05-06 17:58:06 -0400 | [diff] [blame] | 1 | // SPDX-License-Identifier: GPL-2.0+ |
Maximilian Schwerin | 57210c7 | 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 | 57210c7 | 2012-03-12 23:57:50 +0000 | [diff] [blame] | 7 | */ |
| 8 | |
| 9 | #include <common.h> |
Maximilian Schwerin | 57210c7 | 2012-03-12 23:57:50 +0000 | [diff] [blame] | 10 | #include <command.h> |
Simon Glass | 0ac7d72 | 2019-08-01 09:47:00 -0600 | [diff] [blame] | 11 | #include <env.h> |
Simon Glass | f3998fd | 2019-08-02 09:44:25 -0600 | [diff] [blame] | 12 | #include <env_internal.h> |
Simon Glass | e6f6f9e | 2020-05-10 11:39:58 -0600 | [diff] [blame] | 13 | #include <part.h> |
Maximilian Schwerin | 57210c7 | 2012-03-12 23:57:50 +0000 | [diff] [blame] | 14 | #include <malloc.h> |
Simon Glass | cf92e05 | 2015-09-02 17:24:58 -0600 | [diff] [blame] | 15 | #include <memalign.h> |
Maximilian Schwerin | 57210c7 | 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> |
Simon Glass | e6f6f9e | 2020-05-10 11:39:58 -0600 | [diff] [blame] | 20 | #include <asm/cache.h> |
| 21 | #include <linux/stddef.h> |
Maximilian Schwerin | 57210c7 | 2012-03-12 23:57:50 +0000 | [diff] [blame] | 22 | |
Simon Glass | 4415f1d | 2017-08-03 12:21:58 -0600 | [diff] [blame] | 23 | #ifdef CONFIG_SPL_BUILD |
| 24 | /* TODO(sjg@chromium.org): Figure out why this is needed */ |
| 25 | # if !defined(CONFIG_TARGET_AM335X_EVM) || defined(CONFIG_SPL_OS_BOOT) |
| 26 | # define LOADENV |
| 27 | # endif |
| 28 | #else |
| 29 | # define LOADENV |
Simon Glass | 4415f1d | 2017-08-03 12:21:58 -0600 | [diff] [blame] | 30 | #endif |
| 31 | |
David Woodhouse | 6731bef | 2020-06-19 23:07:17 +0100 | [diff] [blame] | 32 | static char *env_fat_device_and_part(void) |
| 33 | { |
| 34 | #ifdef CONFIG_MMC |
| 35 | static char *part_str; |
| 36 | |
| 37 | if (!part_str) { |
| 38 | part_str = CONFIG_ENV_FAT_DEVICE_AND_PART; |
| 39 | if (!strcmp(CONFIG_ENV_FAT_INTERFACE, "mmc") && part_str[0] == ':') { |
| 40 | part_str = "0" CONFIG_ENV_FAT_DEVICE_AND_PART; |
| 41 | part_str[0] += mmc_get_env_dev(); |
| 42 | } |
| 43 | } |
| 44 | |
| 45 | return part_str; |
| 46 | #else |
| 47 | return CONFIG_ENV_FAT_DEVICE_AND_PART; |
| 48 | #endif |
| 49 | } |
| 50 | |
Simon Glass | e5bce24 | 2017-08-03 12:22:01 -0600 | [diff] [blame] | 51 | static int env_fat_save(void) |
Maximilian Schwerin | 57210c7 | 2012-03-12 23:57:50 +0000 | [diff] [blame] | 52 | { |
Alex Kiernan | cda87ec | 2018-02-07 20:01:54 +0000 | [diff] [blame] | 53 | env_t __aligned(ARCH_DMA_MINALIGN) env_new; |
Simon Glass | 4101f68 | 2016-02-29 15:25:34 -0700 | [diff] [blame] | 54 | struct blk_desc *dev_desc = NULL; |
Simon Glass | 0528979 | 2020-05-10 11:39:57 -0600 | [diff] [blame] | 55 | struct disk_partition info; |
Wu, Josh | be354c1 | 2014-06-24 17:31:02 +0800 | [diff] [blame] | 56 | int dev, part; |
Igor Grinberg | 9aa90c1 | 2012-09-23 05:25:21 +0000 | [diff] [blame] | 57 | int err; |
Suriyan Ramasami | 1ad0b98 | 2014-11-17 14:39:35 -0800 | [diff] [blame] | 58 | loff_t size; |
Maximilian Schwerin | 57210c7 | 2012-03-12 23:57:50 +0000 | [diff] [blame] | 59 | |
Marek Vasut | 7ce1526 | 2014-03-05 19:59:50 +0100 | [diff] [blame] | 60 | err = env_export(&env_new); |
| 61 | if (err) |
| 62 | return err; |
Maximilian Schwerin | 57210c7 | 2012-03-12 23:57:50 +0000 | [diff] [blame] | 63 | |
Tom Rini | 43ba3c5 | 2017-07-26 21:48:00 -0400 | [diff] [blame] | 64 | part = blk_get_device_part_str(CONFIG_ENV_FAT_INTERFACE, |
David Woodhouse | 6731bef | 2020-06-19 23:07:17 +0100 | [diff] [blame] | 65 | env_fat_device_and_part(), |
Wu, Josh | be354c1 | 2014-06-24 17:31:02 +0800 | [diff] [blame] | 66 | &dev_desc, &info, 1); |
| 67 | if (part < 0) |
Maximilian Schwerin | 57210c7 | 2012-03-12 23:57:50 +0000 | [diff] [blame] | 68 | return 1; |
Igor Grinberg | 9aa90c1 | 2012-09-23 05:25:21 +0000 | [diff] [blame] | 69 | |
Simon Glass | bcce53d | 2016-02-29 15:25:51 -0700 | [diff] [blame] | 70 | dev = dev_desc->devnum; |
Wu, Josh | be354c1 | 2014-06-24 17:31:02 +0800 | [diff] [blame] | 71 | if (fat_set_blk_dev(dev_desc, &info) != 0) { |
Maxime Ripard | d0816da | 2018-01-23 21:16:55 +0100 | [diff] [blame] | 72 | /* |
| 73 | * This printf is embedded in the messages from env_save that |
| 74 | * will calling it. The missing \n is intentional. |
| 75 | */ |
| 76 | printf("Unable to use %s %d:%d... ", |
Tom Rini | 43ba3c5 | 2017-07-26 21:48:00 -0400 | [diff] [blame] | 77 | CONFIG_ENV_FAT_INTERFACE, dev, part); |
Maximilian Schwerin | 57210c7 | 2012-03-12 23:57:50 +0000 | [diff] [blame] | 78 | return 1; |
| 79 | } |
| 80 | |
Tom Rini | 43ba3c5 | 2017-07-26 21:48:00 -0400 | [diff] [blame] | 81 | err = file_fat_write(CONFIG_ENV_FAT_FILE, (void *)&env_new, 0, sizeof(env_t), |
Suriyan Ramasami | 1ad0b98 | 2014-11-17 14:39:35 -0800 | [diff] [blame] | 82 | &size); |
Igor Grinberg | 9aa90c1 | 2012-09-23 05:25:21 +0000 | [diff] [blame] | 83 | if (err == -1) { |
Maxime Ripard | d0816da | 2018-01-23 21:16:55 +0100 | [diff] [blame] | 84 | /* |
| 85 | * This printf is embedded in the messages from env_save that |
| 86 | * will calling it. The missing \n is intentional. |
| 87 | */ |
| 88 | printf("Unable to write \"%s\" from %s%d:%d... ", |
Tom Rini | 43ba3c5 | 2017-07-26 21:48:00 -0400 | [diff] [blame] | 89 | CONFIG_ENV_FAT_FILE, CONFIG_ENV_FAT_INTERFACE, dev, part); |
Maximilian Schwerin | 57210c7 | 2012-03-12 23:57:50 +0000 | [diff] [blame] | 90 | return 1; |
| 91 | } |
| 92 | |
Maximilian Schwerin | 57210c7 | 2012-03-12 23:57:50 +0000 | [diff] [blame] | 93 | return 0; |
| 94 | } |
Maximilian Schwerin | 57210c7 | 2012-03-12 23:57:50 +0000 | [diff] [blame] | 95 | |
Simon Glass | 4415f1d | 2017-08-03 12:21:58 -0600 | [diff] [blame] | 96 | #ifdef LOADENV |
Simon Glass | c595199 | 2017-08-03 12:22:17 -0600 | [diff] [blame] | 97 | static int env_fat_load(void) |
Maximilian Schwerin | 57210c7 | 2012-03-12 23:57:50 +0000 | [diff] [blame] | 98 | { |
Tom Rini | 6d1966e | 2014-08-01 13:59:10 -0400 | [diff] [blame] | 99 | ALLOC_CACHE_ALIGN_BUFFER(char, buf, CONFIG_ENV_SIZE); |
Simon Glass | 4101f68 | 2016-02-29 15:25:34 -0700 | [diff] [blame] | 100 | struct blk_desc *dev_desc = NULL; |
Simon Glass | 0528979 | 2020-05-10 11:39:57 -0600 | [diff] [blame] | 101 | struct disk_partition info; |
Wu, Josh | be354c1 | 2014-06-24 17:31:02 +0800 | [diff] [blame] | 102 | int dev, part; |
Igor Grinberg | 9aa90c1 | 2012-09-23 05:25:21 +0000 | [diff] [blame] | 103 | int err; |
Maximilian Schwerin | 57210c7 | 2012-03-12 23:57:50 +0000 | [diff] [blame] | 104 | |
Heinrich Schuchardt | 95058fb | 2018-04-14 15:41:00 +0200 | [diff] [blame] | 105 | #ifdef CONFIG_MMC |
Faiz Abbas | 26862b4 | 2018-02-12 19:24:31 +0530 | [diff] [blame] | 106 | if (!strcmp(CONFIG_ENV_FAT_INTERFACE, "mmc")) |
| 107 | mmc_initialize(NULL); |
Heinrich Schuchardt | 95058fb | 2018-04-14 15:41:00 +0200 | [diff] [blame] | 108 | #endif |
Faiz Abbas | 26862b4 | 2018-02-12 19:24:31 +0530 | [diff] [blame] | 109 | |
Tom Rini | 43ba3c5 | 2017-07-26 21:48:00 -0400 | [diff] [blame] | 110 | part = blk_get_device_part_str(CONFIG_ENV_FAT_INTERFACE, |
David Woodhouse | 6731bef | 2020-06-19 23:07:17 +0100 | [diff] [blame] | 111 | env_fat_device_and_part(), |
Wu, Josh | be354c1 | 2014-06-24 17:31:02 +0800 | [diff] [blame] | 112 | &dev_desc, &info, 1); |
| 113 | if (part < 0) |
| 114 | goto err_env_relocate; |
Maximilian Schwerin | 57210c7 | 2012-03-12 23:57:50 +0000 | [diff] [blame] | 115 | |
Simon Glass | bcce53d | 2016-02-29 15:25:51 -0700 | [diff] [blame] | 116 | dev = dev_desc->devnum; |
Wu, Josh | be354c1 | 2014-06-24 17:31:02 +0800 | [diff] [blame] | 117 | if (fat_set_blk_dev(dev_desc, &info) != 0) { |
Maxime Ripard | d0816da | 2018-01-23 21:16:55 +0100 | [diff] [blame] | 118 | /* |
| 119 | * This printf is embedded in the messages from env_save that |
| 120 | * will calling it. The missing \n is intentional. |
| 121 | */ |
| 122 | printf("Unable to use %s %d:%d... ", |
Tom Rini | 43ba3c5 | 2017-07-26 21:48:00 -0400 | [diff] [blame] | 123 | CONFIG_ENV_FAT_INTERFACE, dev, part); |
Wu, Josh | be354c1 | 2014-06-24 17:31:02 +0800 | [diff] [blame] | 124 | goto err_env_relocate; |
Maximilian Schwerin | 57210c7 | 2012-03-12 23:57:50 +0000 | [diff] [blame] | 125 | } |
| 126 | |
Tom Rini | 43ba3c5 | 2017-07-26 21:48:00 -0400 | [diff] [blame] | 127 | err = file_fat_read(CONFIG_ENV_FAT_FILE, buf, CONFIG_ENV_SIZE); |
Igor Grinberg | 9aa90c1 | 2012-09-23 05:25:21 +0000 | [diff] [blame] | 128 | if (err == -1) { |
Maxime Ripard | d0816da | 2018-01-23 21:16:55 +0100 | [diff] [blame] | 129 | /* |
| 130 | * This printf is embedded in the messages from env_save that |
| 131 | * will calling it. The missing \n is intentional. |
| 132 | */ |
| 133 | printf("Unable to read \"%s\" from %s%d:%d... ", |
Tom Rini | 43ba3c5 | 2017-07-26 21:48:00 -0400 | [diff] [blame] | 134 | CONFIG_ENV_FAT_FILE, CONFIG_ENV_FAT_INTERFACE, dev, part); |
Wu, Josh | be354c1 | 2014-06-24 17:31:02 +0800 | [diff] [blame] | 135 | goto err_env_relocate; |
Maximilian Schwerin | 57210c7 | 2012-03-12 23:57:50 +0000 | [diff] [blame] | 136 | } |
| 137 | |
Marek Vasut | 890feec | 2020-07-07 20:51:35 +0200 | [diff] [blame] | 138 | return env_import(buf, 1, H_EXTERNAL); |
Wu, Josh | be354c1 | 2014-06-24 17:31:02 +0800 | [diff] [blame] | 139 | |
| 140 | err_env_relocate: |
Simon Glass | 0ac7d72 | 2019-08-01 09:47:00 -0600 | [diff] [blame] | 141 | env_set_default(NULL, 0); |
Simon Glass | c595199 | 2017-08-03 12:22:17 -0600 | [diff] [blame] | 142 | |
| 143 | return -EIO; |
Maximilian Schwerin | 57210c7 | 2012-03-12 23:57:50 +0000 | [diff] [blame] | 144 | } |
Simon Glass | 4415f1d | 2017-08-03 12:21:58 -0600 | [diff] [blame] | 145 | #endif /* LOADENV */ |
| 146 | |
| 147 | U_BOOT_ENV_LOCATION(fat) = { |
| 148 | .location = ENVL_FAT, |
Simon Glass | ac358be | 2017-08-03 12:22:03 -0600 | [diff] [blame] | 149 | ENV_NAME("FAT") |
Simon Glass | 4415f1d | 2017-08-03 12:21:58 -0600 | [diff] [blame] | 150 | #ifdef LOADENV |
Simon Glass | e5bce24 | 2017-08-03 12:22:01 -0600 | [diff] [blame] | 151 | .load = env_fat_load, |
Simon Glass | 4415f1d | 2017-08-03 12:21:58 -0600 | [diff] [blame] | 152 | #endif |
Rasmus Villemoes | 3908bc9 | 2020-02-19 09:47:41 +0000 | [diff] [blame] | 153 | .save = ENV_SAVE_PTR(env_fat_save), |
Simon Glass | 4415f1d | 2017-08-03 12:21:58 -0600 | [diff] [blame] | 154 | }; |