fat.c 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188
  1. // SPDX-License-Identifier: GPL-2.0+
  2. /*
  3. * (c) Copyright 2011 by Tigris Elektronik GmbH
  4. *
  5. * Author:
  6. * Maximilian Schwerin <mvs@tigris.de>
  7. */
  8. #include <common.h>
  9. #include <command.h>
  10. #include <env.h>
  11. #include <env_internal.h>
  12. #include <part.h>
  13. #include <malloc.h>
  14. #include <memalign.h>
  15. #include <search.h>
  16. #include <errno.h>
  17. #include <fat.h>
  18. #include <mmc.h>
  19. #include <scsi.h>
  20. #include <asm/cache.h>
  21. #include <asm/global_data.h>
  22. #include <linux/stddef.h>
  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
  30. #endif
  31. DECLARE_GLOBAL_DATA_PTR;
  32. __weak const char *env_fat_get_intf(void)
  33. {
  34. return (const char *)CONFIG_ENV_FAT_INTERFACE;
  35. }
  36. __weak char *env_fat_get_dev_part(void)
  37. {
  38. #ifdef CONFIG_MMC
  39. static char *part_str;
  40. if (!part_str) {
  41. part_str = CONFIG_ENV_FAT_DEVICE_AND_PART;
  42. if (!strcmp(CONFIG_ENV_FAT_INTERFACE, "mmc") && part_str[0] == ':') {
  43. part_str = "0" CONFIG_ENV_FAT_DEVICE_AND_PART;
  44. part_str[0] += mmc_get_env_dev();
  45. }
  46. }
  47. return part_str;
  48. #else
  49. return CONFIG_ENV_FAT_DEVICE_AND_PART;
  50. #endif
  51. }
  52. static int env_fat_save(void)
  53. {
  54. env_t __aligned(ARCH_DMA_MINALIGN) env_new;
  55. struct blk_desc *dev_desc = NULL;
  56. struct disk_partition info;
  57. const char *file = CONFIG_ENV_FAT_FILE;
  58. int dev, part;
  59. int err;
  60. loff_t size;
  61. const char *ifname = env_fat_get_intf();
  62. const char *dev_and_part = env_fat_get_dev_part();
  63. err = env_export(&env_new);
  64. if (err)
  65. return err;
  66. part = blk_get_device_part_str(ifname, dev_and_part,
  67. &dev_desc, &info, 1);
  68. if (part < 0)
  69. return 1;
  70. dev = dev_desc->devnum;
  71. if (fat_set_blk_dev(dev_desc, &info) != 0) {
  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...\n", ifname, dev, part);
  77. return 1;
  78. }
  79. #ifdef CONFIG_SYS_REDUNDAND_ENVIRONMENT
  80. if (gd->env_valid == ENV_VALID)
  81. file = CONFIG_ENV_FAT_FILE_REDUND;
  82. #endif
  83. err = file_fat_write(file, (void *)&env_new, 0, sizeof(env_t), &size);
  84. if (err == -1) {
  85. /*
  86. * This printf is embedded in the messages from env_save that
  87. * will calling it. The missing \n is intentional.
  88. */
  89. printf("Unable to write \"%s\" from %s%d:%d...\n", file, ifname, dev, part);
  90. return 1;
  91. }
  92. #ifdef CONFIG_SYS_REDUNDAND_ENVIRONMENT
  93. gd->env_valid = (gd->env_valid == ENV_REDUND) ? ENV_VALID : ENV_REDUND;
  94. #endif
  95. return 0;
  96. }
  97. #ifdef LOADENV
  98. static int env_fat_load(void)
  99. {
  100. ALLOC_CACHE_ALIGN_BUFFER(char, buf1, CONFIG_ENV_SIZE);
  101. #ifdef CONFIG_SYS_REDUNDAND_ENVIRONMENT
  102. ALLOC_CACHE_ALIGN_BUFFER(char, buf2, CONFIG_ENV_SIZE);
  103. int err2;
  104. #endif
  105. struct blk_desc *dev_desc = NULL;
  106. struct disk_partition info;
  107. int dev, part;
  108. int err1;
  109. const char *ifname = env_fat_get_intf();
  110. const char *dev_and_part = env_fat_get_dev_part();
  111. #ifdef CONFIG_MMC
  112. if (!strcmp(ifname, "mmc"))
  113. mmc_initialize(NULL);
  114. #endif
  115. #ifndef CONFIG_SPL_BUILD
  116. #if defined(CONFIG_AHCI) || defined(CONFIG_SCSI)
  117. if (!strcmp(CONFIG_ENV_FAT_INTERFACE, "scsi"))
  118. scsi_scan(true);
  119. #endif
  120. #endif
  121. part = blk_get_device_part_str(ifname, dev_and_part,
  122. &dev_desc, &info, 1);
  123. if (part < 0)
  124. goto err_env_relocate;
  125. dev = dev_desc->devnum;
  126. if (fat_set_blk_dev(dev_desc, &info) != 0) {
  127. /*
  128. * This printf is embedded in the messages from env_save that
  129. * will calling it. The missing \n is intentional.
  130. */
  131. printf("Unable to use %s %d:%d...\n", ifname, dev, part);
  132. goto err_env_relocate;
  133. }
  134. err1 = file_fat_read(CONFIG_ENV_FAT_FILE, buf1, CONFIG_ENV_SIZE);
  135. #ifdef CONFIG_SYS_REDUNDAND_ENVIRONMENT
  136. err2 = file_fat_read(CONFIG_ENV_FAT_FILE_REDUND, buf2, CONFIG_ENV_SIZE);
  137. err1 = (err1 >= 0) ? 0 : -1;
  138. err2 = (err2 >= 0) ? 0 : -1;
  139. return env_import_redund(buf1, err1, buf2, err2, H_EXTERNAL);
  140. #else
  141. if (err1 < 0) {
  142. /*
  143. * This printf is embedded in the messages from env_save that
  144. * will calling it. The missing \n is intentional.
  145. */
  146. printf("Unable to read \"%s\" from %s%d:%d... \n",
  147. CONFIG_ENV_FAT_FILE, ifname, dev, part);
  148. goto err_env_relocate;
  149. }
  150. return env_import(buf1, 1, H_EXTERNAL);
  151. #endif
  152. err_env_relocate:
  153. env_set_default(NULL, 0);
  154. return -EIO;
  155. }
  156. #endif /* LOADENV */
  157. U_BOOT_ENV_LOCATION(fat) = {
  158. .location = ENVL_FAT,
  159. ENV_NAME("FAT")
  160. #ifdef LOADENV
  161. .load = env_fat_load,
  162. #endif
  163. .save = ENV_SAVE_PTR(env_fat_save),
  164. };