fat.c 2.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124
  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. DECLARE_GLOBAL_DATA_PTR;
  31. #ifdef CMD_SAVEENV
  32. static int env_fat_save(void)
  33. {
  34. env_t env_new;
  35. struct blk_desc *dev_desc = NULL;
  36. disk_partition_t info;
  37. int dev, part;
  38. int err;
  39. loff_t size;
  40. err = env_export(&env_new);
  41. if (err)
  42. return err;
  43. part = blk_get_device_part_str(CONFIG_ENV_FAT_INTERFACE,
  44. CONFIG_ENV_FAT_DEVICE_AND_PART,
  45. &dev_desc, &info, 1);
  46. if (part < 0)
  47. return 1;
  48. dev = dev_desc->devnum;
  49. if (fat_set_blk_dev(dev_desc, &info) != 0) {
  50. printf("\n** Unable to use %s %d:%d for saveenv **\n",
  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. printf("\n** Unable to write \"%s\" from %s%d:%d **\n",
  58. CONFIG_ENV_FAT_FILE, CONFIG_ENV_FAT_INTERFACE, dev, part);
  59. return 1;
  60. }
  61. puts("done\n");
  62. return 0;
  63. }
  64. #endif /* CMD_SAVEENV */
  65. #ifdef LOADENV
  66. static int env_fat_load(void)
  67. {
  68. ALLOC_CACHE_ALIGN_BUFFER(char, buf, CONFIG_ENV_SIZE);
  69. struct blk_desc *dev_desc = NULL;
  70. disk_partition_t info;
  71. int dev, part;
  72. int err;
  73. part = blk_get_device_part_str(CONFIG_ENV_FAT_INTERFACE,
  74. CONFIG_ENV_FAT_DEVICE_AND_PART,
  75. &dev_desc, &info, 1);
  76. if (part < 0)
  77. goto err_env_relocate;
  78. dev = dev_desc->devnum;
  79. if (fat_set_blk_dev(dev_desc, &info) != 0) {
  80. printf("\n** Unable to use %s %d:%d for loading the env **\n",
  81. CONFIG_ENV_FAT_INTERFACE, dev, part);
  82. goto err_env_relocate;
  83. }
  84. err = file_fat_read(CONFIG_ENV_FAT_FILE, buf, CONFIG_ENV_SIZE);
  85. if (err == -1) {
  86. printf("\n** Unable to read \"%s\" from %s%d:%d **\n",
  87. CONFIG_ENV_FAT_FILE, CONFIG_ENV_FAT_INTERFACE, dev, part);
  88. goto err_env_relocate;
  89. }
  90. env_import(buf, 1);
  91. return 0;
  92. err_env_relocate:
  93. set_default_env(NULL);
  94. return -EIO;
  95. }
  96. #endif /* LOADENV */
  97. U_BOOT_ENV_LOCATION(fat) = {
  98. .location = ENVL_FAT,
  99. ENV_NAME("FAT")
  100. #ifdef LOADENV
  101. .load = env_fat_load,
  102. #endif
  103. #ifdef CMD_SAVEENV
  104. .save = env_save_ptr(env_fat_save),
  105. #endif
  106. };