fat.c 3.1 KB

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