fat.c 3.4 KB

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