fat.c 3.0 KB

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