1 /* 2 * (c) Copyright 2011 by Tigris Elektronik GmbH 3 * 4 * Author: 5 * Maximilian Schwerin <mvs@tigris.de> 6 * 7 * SPDX-License-Identifier: GPL-2.0+ 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 <memalign.h> 17 #include <search.h> 18 #include <errno.h> 19 #include <fat.h> 20 #include <mmc.h> 21 22 #ifdef CONFIG_SPL_BUILD 23 /* TODO(sjg@chromium.org): Figure out why this is needed */ 24 # if !defined(CONFIG_TARGET_AM335X_EVM) || defined(CONFIG_SPL_OS_BOOT) 25 # define LOADENV 26 # endif 27 #else 28 # define LOADENV 29 # if defined(CONFIG_CMD_SAVEENV) 30 # define CMD_SAVEENV 31 # endif 32 #endif 33 34 #ifdef CMD_SAVEENV 35 static int env_fat_save(void) 36 { 37 env_t __aligned(ARCH_DMA_MINALIGN) env_new; 38 struct blk_desc *dev_desc = NULL; 39 disk_partition_t info; 40 int dev, part; 41 int err; 42 loff_t size; 43 44 err = env_export(&env_new); 45 if (err) 46 return err; 47 48 part = blk_get_device_part_str(CONFIG_ENV_FAT_INTERFACE, 49 CONFIG_ENV_FAT_DEVICE_AND_PART, 50 &dev_desc, &info, 1); 51 if (part < 0) 52 return 1; 53 54 dev = dev_desc->devnum; 55 if (fat_set_blk_dev(dev_desc, &info) != 0) { 56 /* 57 * This printf is embedded in the messages from env_save that 58 * will calling it. The missing \n is intentional. 59 */ 60 printf("Unable to use %s %d:%d... ", 61 CONFIG_ENV_FAT_INTERFACE, dev, part); 62 return 1; 63 } 64 65 err = file_fat_write(CONFIG_ENV_FAT_FILE, (void *)&env_new, 0, sizeof(env_t), 66 &size); 67 if (err == -1) { 68 /* 69 * This printf is embedded in the messages from env_save that 70 * will calling it. The missing \n is intentional. 71 */ 72 printf("Unable to write \"%s\" from %s%d:%d... ", 73 CONFIG_ENV_FAT_FILE, CONFIG_ENV_FAT_INTERFACE, dev, part); 74 return 1; 75 } 76 77 return 0; 78 } 79 #endif /* CMD_SAVEENV */ 80 81 #ifdef LOADENV 82 static int env_fat_load(void) 83 { 84 ALLOC_CACHE_ALIGN_BUFFER(char, buf, CONFIG_ENV_SIZE); 85 struct blk_desc *dev_desc = NULL; 86 disk_partition_t info; 87 int dev, part; 88 int err; 89 90 #ifdef CONFIG_MMC 91 if (!strcmp(CONFIG_ENV_FAT_INTERFACE, "mmc")) 92 mmc_initialize(NULL); 93 #endif 94 95 part = blk_get_device_part_str(CONFIG_ENV_FAT_INTERFACE, 96 CONFIG_ENV_FAT_DEVICE_AND_PART, 97 &dev_desc, &info, 1); 98 if (part < 0) 99 goto err_env_relocate; 100 101 dev = dev_desc->devnum; 102 if (fat_set_blk_dev(dev_desc, &info) != 0) { 103 /* 104 * This printf is embedded in the messages from env_save that 105 * will calling it. The missing \n is intentional. 106 */ 107 printf("Unable to use %s %d:%d... ", 108 CONFIG_ENV_FAT_INTERFACE, dev, part); 109 goto err_env_relocate; 110 } 111 112 err = file_fat_read(CONFIG_ENV_FAT_FILE, buf, CONFIG_ENV_SIZE); 113 if (err == -1) { 114 /* 115 * This printf is embedded in the messages from env_save that 116 * will calling it. The missing \n is intentional. 117 */ 118 printf("Unable to read \"%s\" from %s%d:%d... ", 119 CONFIG_ENV_FAT_FILE, CONFIG_ENV_FAT_INTERFACE, dev, part); 120 goto err_env_relocate; 121 } 122 123 return env_import(buf, 1); 124 125 err_env_relocate: 126 set_default_env(NULL); 127 128 return -EIO; 129 } 130 #endif /* LOADENV */ 131 132 U_BOOT_ENV_LOCATION(fat) = { 133 .location = ENVL_FAT, 134 ENV_NAME("FAT") 135 #ifdef LOADENV 136 .load = env_fat_load, 137 #endif 138 #ifdef CMD_SAVEENV 139 .save = env_save_ptr(env_fat_save), 140 #endif 141 }; 142