fat.c 3.1 KB

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