fat.c 2.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135
  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. char *env_name_spec = "FAT";
  31. env_t *env_ptr;
  32. DECLARE_GLOBAL_DATA_PTR;
  33. static int env_fat_init(void)
  34. {
  35. /* use default */
  36. gd->env_addr = (ulong)&default_environment[0];
  37. gd->env_valid = ENV_VALID;
  38. return 0;
  39. }
  40. #ifdef CMD_SAVEENV
  41. static int env_fat_save(void)
  42. {
  43. env_t env_new;
  44. struct blk_desc *dev_desc = NULL;
  45. disk_partition_t info;
  46. int dev, part;
  47. int err;
  48. loff_t size;
  49. err = env_export(&env_new);
  50. if (err)
  51. return err;
  52. part = blk_get_device_part_str(CONFIG_ENV_FAT_INTERFACE,
  53. CONFIG_ENV_FAT_DEVICE_AND_PART,
  54. &dev_desc, &info, 1);
  55. if (part < 0)
  56. return 1;
  57. dev = dev_desc->devnum;
  58. if (fat_set_blk_dev(dev_desc, &info) != 0) {
  59. printf("\n** Unable to use %s %d:%d for saveenv **\n",
  60. CONFIG_ENV_FAT_INTERFACE, dev, part);
  61. return 1;
  62. }
  63. err = file_fat_write(CONFIG_ENV_FAT_FILE, (void *)&env_new, 0, sizeof(env_t),
  64. &size);
  65. if (err == -1) {
  66. printf("\n** Unable to write \"%s\" from %s%d:%d **\n",
  67. CONFIG_ENV_FAT_FILE, CONFIG_ENV_FAT_INTERFACE, dev, part);
  68. return 1;
  69. }
  70. puts("done\n");
  71. return 0;
  72. }
  73. #endif /* CMD_SAVEENV */
  74. #ifdef LOADENV
  75. static void env_fat_load(void)
  76. {
  77. ALLOC_CACHE_ALIGN_BUFFER(char, buf, CONFIG_ENV_SIZE);
  78. struct blk_desc *dev_desc = NULL;
  79. disk_partition_t info;
  80. int dev, part;
  81. int err;
  82. part = blk_get_device_part_str(CONFIG_ENV_FAT_INTERFACE,
  83. CONFIG_ENV_FAT_DEVICE_AND_PART,
  84. &dev_desc, &info, 1);
  85. if (part < 0)
  86. goto err_env_relocate;
  87. dev = dev_desc->devnum;
  88. if (fat_set_blk_dev(dev_desc, &info) != 0) {
  89. printf("\n** Unable to use %s %d:%d for loading the env **\n",
  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. printf("\n** Unable to read \"%s\" from %s%d:%d **\n",
  96. CONFIG_ENV_FAT_FILE, CONFIG_ENV_FAT_INTERFACE, dev, part);
  97. goto err_env_relocate;
  98. }
  99. env_import(buf, 1);
  100. return;
  101. err_env_relocate:
  102. set_default_env(NULL);
  103. }
  104. #endif /* LOADENV */
  105. U_BOOT_ENV_LOCATION(fat) = {
  106. .location = ENVL_FAT,
  107. #ifdef LOADENV
  108. .load = env_fat_load,
  109. #endif
  110. #ifdef CMD_SAVEENV
  111. .save = env_save_ptr(env_fat_save),
  112. #endif
  113. .init = env_fat_init,
  114. };