fat.c 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141
  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. #include <common.h>
  10. #include <command.h>
  11. #include <environment.h>
  12. #include <linux/stddef.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. #ifdef CONFIG_SPL_BUILD
  20. /* TODO(sjg@chromium.org): Figure out why this is needed */
  21. # if !defined(CONFIG_TARGET_AM335X_EVM) || defined(CONFIG_SPL_OS_BOOT)
  22. # define LOADENV
  23. # endif
  24. #else
  25. # define LOADENV
  26. # if defined(CONFIG_CMD_SAVEENV)
  27. # define CMD_SAVEENV
  28. # endif
  29. #endif
  30. DECLARE_GLOBAL_DATA_PTR;
  31. #ifdef CMD_SAVEENV
  32. static int env_fat_save(void)
  33. {
  34. env_t __aligned(ARCH_DMA_MINALIGN) env_new;
  35. struct blk_desc *dev_desc = NULL;
  36. disk_partition_t info;
  37. int dev, part;
  38. int err;
  39. loff_t size;
  40. err = env_export(&env_new);
  41. if (err)
  42. return err;
  43. part = blk_get_device_part_str(CONFIG_ENV_FAT_INTERFACE,
  44. CONFIG_ENV_FAT_DEVICE_AND_PART,
  45. &dev_desc, &info, 1);
  46. if (part < 0)
  47. return 1;
  48. dev = dev_desc->devnum;
  49. if (fat_set_blk_dev(dev_desc, &info) != 0) {
  50. /*
  51. * This printf is embedded in the messages from env_save that
  52. * will calling it. The missing \n is intentional.
  53. */
  54. printf("Unable to use %s %d:%d... ",
  55. CONFIG_ENV_FAT_INTERFACE, dev, part);
  56. return 1;
  57. }
  58. err = file_fat_write(CONFIG_ENV_FAT_FILE, (void *)&env_new, 0, sizeof(env_t),
  59. &size);
  60. if (err == -1) {
  61. /*
  62. * This printf is embedded in the messages from env_save that
  63. * will calling it. The missing \n is intentional.
  64. */
  65. printf("Unable to write \"%s\" from %s%d:%d... ",
  66. CONFIG_ENV_FAT_FILE, CONFIG_ENV_FAT_INTERFACE, dev, part);
  67. return 1;
  68. }
  69. return 0;
  70. }
  71. #endif /* CMD_SAVEENV */
  72. #ifdef LOADENV
  73. static int env_fat_load(void)
  74. {
  75. ALLOC_CACHE_ALIGN_BUFFER(char, buf, CONFIG_ENV_SIZE);
  76. struct blk_desc *dev_desc = NULL;
  77. disk_partition_t info;
  78. int dev, part;
  79. int err;
  80. if (!strcmp(CONFIG_ENV_FAT_INTERFACE, "mmc"))
  81. mmc_initialize(NULL);
  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);
  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. };