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 <asm/cache.h>
  10. #include <command.h>
  11. #include <env.h>
  12. #include <env_internal.h>
  13. #include <linux/stddef.h>
  14. #include <malloc.h>
  15. #include <memalign.h>
  16. #include <search.h>
  17. #include <errno.h>
  18. #include <fat.h>
  19. #include <mmc.h>
  20. #ifdef CONFIG_SPL_BUILD
  21. /* TODO(sjg@chromium.org): Figure out why this is needed */
  22. # if !defined(CONFIG_TARGET_AM335X_EVM) || defined(CONFIG_SPL_OS_BOOT)
  23. # define LOADENV
  24. # endif
  25. #else
  26. # define LOADENV
  27. #endif
  28. static int env_fat_save(void)
  29. {
  30. env_t __aligned(ARCH_DMA_MINALIGN) env_new;
  31. struct blk_desc *dev_desc = NULL;
  32. struct disk_partition info;
  33. int dev, part;
  34. int err;
  35. loff_t size;
  36. err = env_export(&env_new);
  37. if (err)
  38. return err;
  39. part = blk_get_device_part_str(CONFIG_ENV_FAT_INTERFACE,
  40. CONFIG_ENV_FAT_DEVICE_AND_PART,
  41. &dev_desc, &info, 1);
  42. if (part < 0)
  43. return 1;
  44. dev = dev_desc->devnum;
  45. if (fat_set_blk_dev(dev_desc, &info) != 0) {
  46. /*
  47. * This printf is embedded in the messages from env_save that
  48. * will calling it. The missing \n is intentional.
  49. */
  50. printf("Unable to use %s %d:%d... ",
  51. CONFIG_ENV_FAT_INTERFACE, dev, part);
  52. return 1;
  53. }
  54. err = file_fat_write(CONFIG_ENV_FAT_FILE, (void *)&env_new, 0, sizeof(env_t),
  55. &size);
  56. if (err == -1) {
  57. /*
  58. * This printf is embedded in the messages from env_save that
  59. * will calling it. The missing \n is intentional.
  60. */
  61. printf("Unable to write \"%s\" from %s%d:%d... ",
  62. CONFIG_ENV_FAT_FILE, CONFIG_ENV_FAT_INTERFACE, dev, part);
  63. return 1;
  64. }
  65. return 0;
  66. }
  67. #ifdef LOADENV
  68. static int env_fat_load(void)
  69. {
  70. ALLOC_CACHE_ALIGN_BUFFER(char, buf, CONFIG_ENV_SIZE);
  71. struct blk_desc *dev_desc = NULL;
  72. struct disk_partition info;
  73. int dev, part;
  74. int err;
  75. #ifdef CONFIG_MMC
  76. if (!strcmp(CONFIG_ENV_FAT_INTERFACE, "mmc"))
  77. mmc_initialize(NULL);
  78. #endif
  79. part = blk_get_device_part_str(CONFIG_ENV_FAT_INTERFACE,
  80. CONFIG_ENV_FAT_DEVICE_AND_PART,
  81. &dev_desc, &info, 1);
  82. if (part < 0)
  83. goto err_env_relocate;
  84. dev = dev_desc->devnum;
  85. if (fat_set_blk_dev(dev_desc, &info) != 0) {
  86. /*
  87. * This printf is embedded in the messages from env_save that
  88. * will calling it. The missing \n is intentional.
  89. */
  90. printf("Unable to use %s %d:%d... ",
  91. CONFIG_ENV_FAT_INTERFACE, dev, part);
  92. goto err_env_relocate;
  93. }
  94. err = file_fat_read(CONFIG_ENV_FAT_FILE, buf, CONFIG_ENV_SIZE);
  95. if (err == -1) {
  96. /*
  97. * This printf is embedded in the messages from env_save that
  98. * will calling it. The missing \n is intentional.
  99. */
  100. printf("Unable to read \"%s\" from %s%d:%d... ",
  101. CONFIG_ENV_FAT_FILE, CONFIG_ENV_FAT_INTERFACE, dev, part);
  102. goto err_env_relocate;
  103. }
  104. return env_import(buf, 1);
  105. err_env_relocate:
  106. env_set_default(NULL, 0);
  107. return -EIO;
  108. }
  109. #endif /* LOADENV */
  110. U_BOOT_ENV_LOCATION(fat) = {
  111. .location = ENVL_FAT,
  112. ENV_NAME("FAT")
  113. #ifdef LOADENV
  114. .load = env_fat_load,
  115. #endif
  116. .save = ENV_SAVE_PTR(env_fat_save),
  117. };