env_fat.c 2.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109
  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 <search.h>
  15. #include <errno.h>
  16. #include <fat.h>
  17. #include <mmc.h>
  18. char *env_name_spec = "FAT";
  19. env_t *env_ptr;
  20. DECLARE_GLOBAL_DATA_PTR;
  21. int env_init(void)
  22. {
  23. /* use default */
  24. gd->env_addr = (ulong)&default_environment[0];
  25. gd->env_valid = 1;
  26. return 0;
  27. }
  28. #ifdef CONFIG_CMD_SAVEENV
  29. int saveenv(void)
  30. {
  31. env_t env_new;
  32. block_dev_desc_t *dev_desc = NULL;
  33. disk_partition_t 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 = get_device_and_partition(FAT_ENV_INTERFACE,
  41. FAT_ENV_DEVICE_AND_PART,
  42. &dev_desc, &info, 1);
  43. if (part < 0)
  44. return 1;
  45. dev = dev_desc->dev;
  46. if (fat_set_blk_dev(dev_desc, &info) != 0) {
  47. printf("\n** Unable to use %s %d:%d for saveenv **\n",
  48. FAT_ENV_INTERFACE, dev, part);
  49. return 1;
  50. }
  51. err = file_fat_write(FAT_ENV_FILE, (void *)&env_new, 0, sizeof(env_t),
  52. &size);
  53. if (err == -1) {
  54. printf("\n** Unable to write \"%s\" from %s%d:%d **\n",
  55. FAT_ENV_FILE, FAT_ENV_INTERFACE, dev, part);
  56. return 1;
  57. }
  58. puts("done\n");
  59. return 0;
  60. }
  61. #endif /* CONFIG_CMD_SAVEENV */
  62. void env_relocate_spec(void)
  63. {
  64. ALLOC_CACHE_ALIGN_BUFFER(char, buf, CONFIG_ENV_SIZE);
  65. block_dev_desc_t *dev_desc = NULL;
  66. disk_partition_t info;
  67. int dev, part;
  68. int err;
  69. part = get_device_and_partition(FAT_ENV_INTERFACE,
  70. FAT_ENV_DEVICE_AND_PART,
  71. &dev_desc, &info, 1);
  72. if (part < 0)
  73. goto err_env_relocate;
  74. dev = dev_desc->dev;
  75. if (fat_set_blk_dev(dev_desc, &info) != 0) {
  76. printf("\n** Unable to use %s %d:%d for loading the env **\n",
  77. FAT_ENV_INTERFACE, dev, part);
  78. goto err_env_relocate;
  79. }
  80. err = file_fat_read(FAT_ENV_FILE, buf, CONFIG_ENV_SIZE);
  81. if (err == -1) {
  82. printf("\n** Unable to read \"%s\" from %s%d:%d **\n",
  83. FAT_ENV_FILE, FAT_ENV_INTERFACE, dev, part);
  84. goto err_env_relocate;
  85. }
  86. env_import(buf, 1);
  87. return;
  88. err_env_relocate:
  89. set_default_env(NULL);
  90. }