fat.c 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163
  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 <asm/cache.h>
  20. #include <linux/stddef.h>
  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. #endif
  29. __weak int mmc_get_env_dev(void)
  30. {
  31. #ifdef CONFIG_SYS_MMC_ENV_DEV
  32. return CONFIG_SYS_MMC_ENV_DEV;
  33. #else
  34. return 0;
  35. #endif
  36. }
  37. static char *env_fat_device_and_part(void)
  38. {
  39. #ifdef CONFIG_MMC
  40. static char *part_str;
  41. if (!part_str) {
  42. part_str = CONFIG_ENV_FAT_DEVICE_AND_PART;
  43. if (!strcmp(CONFIG_ENV_FAT_INTERFACE, "mmc") && part_str[0] == ':') {
  44. part_str = "0" CONFIG_ENV_FAT_DEVICE_AND_PART;
  45. part_str[0] += mmc_get_env_dev();
  46. }
  47. }
  48. return part_str;
  49. #else
  50. return CONFIG_ENV_FAT_DEVICE_AND_PART;
  51. #endif
  52. }
  53. static int env_fat_save(void)
  54. {
  55. env_t __aligned(ARCH_DMA_MINALIGN) env_new;
  56. struct blk_desc *dev_desc = NULL;
  57. struct disk_partition info;
  58. int dev, part;
  59. int err;
  60. loff_t size;
  61. err = env_export(&env_new);
  62. if (err)
  63. return err;
  64. part = blk_get_device_part_str(CONFIG_ENV_FAT_INTERFACE,
  65. env_fat_device_and_part(),
  66. &dev_desc, &info, 1);
  67. if (part < 0)
  68. return 1;
  69. dev = dev_desc->devnum;
  70. if (fat_set_blk_dev(dev_desc, &info) != 0) {
  71. /*
  72. * This printf is embedded in the messages from env_save that
  73. * will calling it. The missing \n is intentional.
  74. */
  75. printf("Unable to use %s %d:%d... ",
  76. CONFIG_ENV_FAT_INTERFACE, dev, part);
  77. return 1;
  78. }
  79. err = file_fat_write(CONFIG_ENV_FAT_FILE, (void *)&env_new, 0, sizeof(env_t),
  80. &size);
  81. if (err == -1) {
  82. /*
  83. * This printf is embedded in the messages from env_save that
  84. * will calling it. The missing \n is intentional.
  85. */
  86. printf("Unable to write \"%s\" from %s%d:%d... ",
  87. CONFIG_ENV_FAT_FILE, CONFIG_ENV_FAT_INTERFACE, dev, part);
  88. return 1;
  89. }
  90. return 0;
  91. }
  92. #ifdef LOADENV
  93. static int env_fat_load(void)
  94. {
  95. ALLOC_CACHE_ALIGN_BUFFER(char, buf, CONFIG_ENV_SIZE);
  96. struct blk_desc *dev_desc = NULL;
  97. struct disk_partition info;
  98. int dev, part;
  99. int err;
  100. #ifdef CONFIG_MMC
  101. if (!strcmp(CONFIG_ENV_FAT_INTERFACE, "mmc"))
  102. mmc_initialize(NULL);
  103. #endif
  104. part = blk_get_device_part_str(CONFIG_ENV_FAT_INTERFACE,
  105. env_fat_device_and_part(),
  106. &dev_desc, &info, 1);
  107. if (part < 0)
  108. goto err_env_relocate;
  109. dev = dev_desc->devnum;
  110. if (fat_set_blk_dev(dev_desc, &info) != 0) {
  111. /*
  112. * This printf is embedded in the messages from env_save that
  113. * will calling it. The missing \n is intentional.
  114. */
  115. printf("Unable to use %s %d:%d... ",
  116. CONFIG_ENV_FAT_INTERFACE, dev, part);
  117. goto err_env_relocate;
  118. }
  119. err = file_fat_read(CONFIG_ENV_FAT_FILE, buf, CONFIG_ENV_SIZE);
  120. if (err == -1) {
  121. /*
  122. * This printf is embedded in the messages from env_save that
  123. * will calling it. The missing \n is intentional.
  124. */
  125. printf("Unable to read \"%s\" from %s%d:%d... ",
  126. CONFIG_ENV_FAT_FILE, CONFIG_ENV_FAT_INTERFACE, dev, part);
  127. goto err_env_relocate;
  128. }
  129. return env_import(buf, 1, H_EXTERNAL);
  130. err_env_relocate:
  131. env_set_default(NULL, 0);
  132. return -EIO;
  133. }
  134. #endif /* LOADENV */
  135. U_BOOT_ENV_LOCATION(fat) = {
  136. .location = ENVL_FAT,
  137. ENV_NAME("FAT")
  138. #ifdef LOADENV
  139. .load = env_fat_load,
  140. #endif
  141. .save = ENV_SAVE_PTR(env_fat_save),
  142. };