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> |
| 10 | |
| 11 | #include <command.h> |
| 12 | #include <environment.h> |
| 13 | #include <linux/stddef.h> |
| 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> |
| 20 | |
Simon Glass | 4415f1d | 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 | 4415f1d | 2017-08-03 12:21:58 -0600 | [diff] [blame] | 33 | #ifdef CMD_SAVEENV |
Simon Glass | e5bce24 | 2017-08-03 12:22:01 -0600 | [diff] [blame] | 34 | static int env_fat_save(void) |
Maximilian Schwerin | 57210c7 | 2012-03-12 23:57:50 +0000 | [diff] [blame] | 35 | { |
Alex Kiernan | cda87ec | 2018-02-07 20:01:54 +0000 | [diff] [blame] | 36 | env_t __aligned(ARCH_DMA_MINALIGN) env_new; |
Simon Glass | 4101f68 | 2016-02-29 15:25:34 -0700 | [diff] [blame] | 37 | struct blk_desc *dev_desc = NULL; |
Wu, Josh | be354c1 | 2014-06-24 17:31:02 +0800 | [diff] [blame] | 38 | disk_partition_t info; |
| 39 | int dev, part; |
Igor Grinberg | 9aa90c1 | 2012-09-23 05:25:21 +0000 | [diff] [blame] | 40 | int err; |
Suriyan Ramasami | 1ad0b98 | 2014-11-17 14:39:35 -0800 | [diff] [blame] | 41 | loff_t size; |
Maximilian Schwerin | 57210c7 | 2012-03-12 23:57:50 +0000 | [diff] [blame] | 42 | |
Marek Vasut | 7ce1526 | 2014-03-05 19:59:50 +0100 | [diff] [blame] | 43 | err = env_export(&env_new); |
| 44 | if (err) |
| 45 | return err; |
Maximilian Schwerin | 57210c7 | 2012-03-12 23:57:50 +0000 | [diff] [blame] | 46 | |
Tom Rini | 43ba3c5 | 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 | be354c1 | 2014-06-24 17:31:02 +0800 | [diff] [blame] | 49 | &dev_desc, &info, 1); |
| 50 | if (part < 0) |
Maximilian Schwerin | 57210c7 | 2012-03-12 23:57:50 +0000 | [diff] [blame] | 51 | return 1; |
Igor Grinberg | 9aa90c1 | 2012-09-23 05:25:21 +0000 | [diff] [blame] | 52 | |
Simon Glass | bcce53d | 2016-02-29 15:25:51 -0700 | [diff] [blame] | 53 | dev = dev_desc->devnum; |
Wu, Josh | be354c1 | 2014-06-24 17:31:02 +0800 | [diff] [blame] | 54 | if (fat_set_blk_dev(dev_desc, &info) != 0) { |
Maxime Ripard | d0816da | 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 | 43ba3c5 | 2017-07-26 21:48:00 -0400 | [diff] [blame] | 60 | CONFIG_ENV_FAT_INTERFACE, dev, part); |
Maximilian Schwerin | 57210c7 | 2012-03-12 23:57:50 +0000 | [diff] [blame] | 61 | return 1; |
| 62 | } |
| 63 | |
Tom Rini | 43ba3c5 | 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 | 1ad0b98 | 2014-11-17 14:39:35 -0800 | [diff] [blame] | 65 | &size); |
Igor Grinberg | 9aa90c1 | 2012-09-23 05:25:21 +0000 | [diff] [blame] | 66 | if (err == -1) { |
Maxime Ripard | d0816da | 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 | 43ba3c5 | 2017-07-26 21:48:00 -0400 | [diff] [blame] | 72 | CONFIG_ENV_FAT_FILE, CONFIG_ENV_FAT_INTERFACE, dev, part); |
Maximilian Schwerin | 57210c7 | 2012-03-12 23:57:50 +0000 | [diff] [blame] | 73 | return 1; |
| 74 | } |
| 75 | |
Maximilian Schwerin | 57210c7 | 2012-03-12 23:57:50 +0000 | [diff] [blame] | 76 | return 0; |
| 77 | } |
Simon Glass | 4415f1d | 2017-08-03 12:21:58 -0600 | [diff] [blame] | 78 | #endif /* CMD_SAVEENV */ |
Maximilian Schwerin | 57210c7 | 2012-03-12 23:57:50 +0000 | [diff] [blame] | 79 | |
Simon Glass | 4415f1d | 2017-08-03 12:21:58 -0600 | [diff] [blame] | 80 | #ifdef LOADENV |
Simon Glass | c595199 | 2017-08-03 12:22:17 -0600 | [diff] [blame] | 81 | static int env_fat_load(void) |
Maximilian Schwerin | 57210c7 | 2012-03-12 23:57:50 +0000 | [diff] [blame] | 82 | { |
Tom Rini | 6d1966e | 2014-08-01 13:59:10 -0400 | [diff] [blame] | 83 | ALLOC_CACHE_ALIGN_BUFFER(char, buf, CONFIG_ENV_SIZE); |
Simon Glass | 4101f68 | 2016-02-29 15:25:34 -0700 | [diff] [blame] | 84 | struct blk_desc *dev_desc = NULL; |
Wu, Josh | be354c1 | 2014-06-24 17:31:02 +0800 | [diff] [blame] | 85 | disk_partition_t info; |
| 86 | int dev, part; |
Igor Grinberg | 9aa90c1 | 2012-09-23 05:25:21 +0000 | [diff] [blame] | 87 | int err; |
Maximilian Schwerin | 57210c7 | 2012-03-12 23:57:50 +0000 | [diff] [blame] | 88 | |
Heinrich Schuchardt | 95058fb | 2018-04-14 15:41:00 +0200 | [diff] [blame] | 89 | #ifdef CONFIG_MMC |
Faiz Abbas | 26862b4 | 2018-02-12 19:24:31 +0530 | [diff] [blame] | 90 | if (!strcmp(CONFIG_ENV_FAT_INTERFACE, "mmc")) |
| 91 | mmc_initialize(NULL); |
Heinrich Schuchardt | 95058fb | 2018-04-14 15:41:00 +0200 | [diff] [blame] | 92 | #endif |
Faiz Abbas | 26862b4 | 2018-02-12 19:24:31 +0530 | [diff] [blame] | 93 | |
Tom Rini | 43ba3c5 | 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 | be354c1 | 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 | 57210c7 | 2012-03-12 23:57:50 +0000 | [diff] [blame] | 99 | |
Simon Glass | bcce53d | 2016-02-29 15:25:51 -0700 | [diff] [blame] | 100 | dev = dev_desc->devnum; |
Wu, Josh | be354c1 | 2014-06-24 17:31:02 +0800 | [diff] [blame] | 101 | if (fat_set_blk_dev(dev_desc, &info) != 0) { |
Maxime Ripard | d0816da | 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 | 43ba3c5 | 2017-07-26 21:48:00 -0400 | [diff] [blame] | 107 | CONFIG_ENV_FAT_INTERFACE, dev, part); |
Wu, Josh | be354c1 | 2014-06-24 17:31:02 +0800 | [diff] [blame] | 108 | goto err_env_relocate; |
Maximilian Schwerin | 57210c7 | 2012-03-12 23:57:50 +0000 | [diff] [blame] | 109 | } |
| 110 | |
Tom Rini | 43ba3c5 | 2017-07-26 21:48:00 -0400 | [diff] [blame] | 111 | err = file_fat_read(CONFIG_ENV_FAT_FILE, buf, CONFIG_ENV_SIZE); |
Igor Grinberg | 9aa90c1 | 2012-09-23 05:25:21 +0000 | [diff] [blame] | 112 | if (err == -1) { |
Maxime Ripard | d0816da | 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 | 43ba3c5 | 2017-07-26 21:48:00 -0400 | [diff] [blame] | 118 | CONFIG_ENV_FAT_FILE, CONFIG_ENV_FAT_INTERFACE, dev, part); |
Wu, Josh | be354c1 | 2014-06-24 17:31:02 +0800 | [diff] [blame] | 119 | goto err_env_relocate; |
Maximilian Schwerin | 57210c7 | 2012-03-12 23:57:50 +0000 | [diff] [blame] | 120 | } |
| 121 | |
Simon Goldschmidt | 2166ebf | 2018-01-31 14:47:12 +0100 | [diff] [blame] | 122 | return env_import(buf, 1); |
Wu, Josh | be354c1 | 2014-06-24 17:31:02 +0800 | [diff] [blame] | 123 | |
| 124 | err_env_relocate: |
Yaniv Levinsky | c5d548a | 2018-06-24 19:16:57 +0300 | [diff] [blame] | 125 | set_default_env(NULL, 0); |
Simon Glass | c595199 | 2017-08-03 12:22:17 -0600 | [diff] [blame] | 126 | |
| 127 | return -EIO; |
Maximilian Schwerin | 57210c7 | 2012-03-12 23:57:50 +0000 | [diff] [blame] | 128 | } |
Simon Glass | 4415f1d | 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 | ac358be | 2017-08-03 12:22:03 -0600 | [diff] [blame] | 133 | ENV_NAME("FAT") |
Simon Glass | 4415f1d | 2017-08-03 12:21:58 -0600 | [diff] [blame] | 134 | #ifdef LOADENV |
Simon Glass | e5bce24 | 2017-08-03 12:22:01 -0600 | [diff] [blame] | 135 | .load = env_fat_load, |
Simon Glass | 4415f1d | 2017-08-03 12:21:58 -0600 | [diff] [blame] | 136 | #endif |
| 137 | #ifdef CMD_SAVEENV |
Simon Glass | e5bce24 | 2017-08-03 12:22:01 -0600 | [diff] [blame] | 138 | .save = env_save_ptr(env_fat_save), |
Simon Glass | 4415f1d | 2017-08-03 12:21:58 -0600 | [diff] [blame] | 139 | #endif |
Simon Glass | 4415f1d | 2017-08-03 12:21:58 -0600 | [diff] [blame] | 140 | }; |