fat.c 3.0 KB

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