fat.c 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138
  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. part = blk_get_device_part_str(CONFIG_ENV_FAT_INTERFACE,
  81. CONFIG_ENV_FAT_DEVICE_AND_PART,
  82. &dev_desc, &info, 1);
  83. if (part < 0)
  84. goto err_env_relocate;
  85. dev = dev_desc->devnum;
  86. if (fat_set_blk_dev(dev_desc, &info) != 0) {
  87. /*
  88. * This printf is embedded in the messages from env_save that
  89. * will calling it. The missing \n is intentional.
  90. */
  91. printf("Unable to use %s %d:%d... ",
  92. CONFIG_ENV_FAT_INTERFACE, dev, part);
  93. goto err_env_relocate;
  94. }
  95. err = file_fat_read(CONFIG_ENV_FAT_FILE, buf, CONFIG_ENV_SIZE);
  96. if (err == -1) {
  97. /*
  98. * This printf is embedded in the messages from env_save that
  99. * will calling it. The missing \n is intentional.
  100. */
  101. printf("Unable to read \"%s\" from %s%d:%d... ",
  102. CONFIG_ENV_FAT_FILE, CONFIG_ENV_FAT_INTERFACE, dev, part);
  103. goto err_env_relocate;
  104. }
  105. return env_import(buf, 1);
  106. err_env_relocate:
  107. set_default_env(NULL);
  108. return -EIO;
  109. }
  110. #endif /* LOADENV */
  111. U_BOOT_ENV_LOCATION(fat) = {
  112. .location = ENVL_FAT,
  113. ENV_NAME("FAT")
  114. #ifdef LOADENV
  115. .load = env_fat_load,
  116. #endif
  117. #ifdef CMD_SAVEENV
  118. .save = env_save_ptr(env_fat_save),
  119. #endif
  120. };