env_ext4.c 2.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133
  1. /*
  2. * (c) Copyright 2016 by VRT Technology
  3. *
  4. * Author:
  5. * Stuart Longland <stuartl@vrt.com.au>
  6. *
  7. * Based on FAT environment driver
  8. * (c) Copyright 2011 by Tigris Elektronik GmbH
  9. *
  10. * Author:
  11. * Maximilian Schwerin <mvs@tigris.de>
  12. *
  13. * and EXT4 filesystem implementation
  14. * (C) Copyright 2011 - 2012 Samsung Electronics
  15. * EXT4 filesystem implementation in Uboot by
  16. * Uma Shankar <uma.shankar@samsung.com>
  17. * Manjunatha C Achar <a.manjunatha@samsung.com>
  18. *
  19. * SPDX-License-Identifier: GPL-2.0+
  20. */
  21. #include <common.h>
  22. #include <command.h>
  23. #include <environment.h>
  24. #include <linux/stddef.h>
  25. #include <malloc.h>
  26. #include <memalign.h>
  27. #include <search.h>
  28. #include <errno.h>
  29. #include <ext4fs.h>
  30. #include <mmc.h>
  31. char *env_name_spec = "EXT4";
  32. env_t *env_ptr;
  33. DECLARE_GLOBAL_DATA_PTR;
  34. int env_init(void)
  35. {
  36. /* use default */
  37. gd->env_addr = (ulong)&default_environment[0];
  38. gd->env_valid = 1;
  39. /* intialize the MMC sub-system if env is stored on a MMC*/
  40. if (!strcmp(EXT4_ENV_INTERFACE, "mmc"))
  41. mmc_initialize(NULL);
  42. return 0;
  43. }
  44. #ifdef CONFIG_CMD_SAVEENV
  45. int saveenv(void)
  46. {
  47. env_t env_new;
  48. struct blk_desc *dev_desc = NULL;
  49. disk_partition_t info;
  50. int dev, part;
  51. int err;
  52. err = env_export(&env_new);
  53. if (err)
  54. return err;
  55. part = blk_get_device_part_str(EXT4_ENV_INTERFACE,
  56. EXT4_ENV_DEVICE_AND_PART,
  57. &dev_desc, &info, 1);
  58. if (part < 0)
  59. return 1;
  60. dev = dev_desc->devnum;
  61. ext4fs_set_blk_dev(dev_desc, &info);
  62. if (!ext4fs_mount(info.size)) {
  63. printf("\n** Unable to use %s %s for saveenv **\n",
  64. EXT4_ENV_INTERFACE, EXT4_ENV_DEVICE_AND_PART);
  65. return 1;
  66. }
  67. err = ext4fs_write(EXT4_ENV_FILE, (void *)&env_new, sizeof(env_t));
  68. ext4fs_close();
  69. if (err == -1) {
  70. printf("\n** Unable to write \"%s\" from %s%d:%d **\n",
  71. EXT4_ENV_FILE, EXT4_ENV_INTERFACE, dev, part);
  72. return 1;
  73. }
  74. puts("done\n");
  75. return 0;
  76. }
  77. #endif /* CONFIG_CMD_SAVEENV */
  78. void env_relocate_spec(void)
  79. {
  80. ALLOC_CACHE_ALIGN_BUFFER(char, buf, CONFIG_ENV_SIZE);
  81. struct blk_desc *dev_desc = NULL;
  82. disk_partition_t info;
  83. int dev, part;
  84. int err;
  85. loff_t off;
  86. part = blk_get_device_part_str(EXT4_ENV_INTERFACE,
  87. EXT4_ENV_DEVICE_AND_PART,
  88. &dev_desc, &info, 1);
  89. if (part < 0)
  90. goto err_env_relocate;
  91. dev = dev_desc->devnum;
  92. ext4fs_set_blk_dev(dev_desc, &info);
  93. if (!ext4fs_mount(info.size)) {
  94. printf("\n** Unable to use %s %s for loading the env **\n",
  95. EXT4_ENV_INTERFACE, EXT4_ENV_DEVICE_AND_PART);
  96. goto err_env_relocate;
  97. }
  98. err = ext4_read_file(EXT4_ENV_FILE, buf, 0, CONFIG_ENV_SIZE, &off);
  99. ext4fs_close();
  100. if (err == -1) {
  101. printf("\n** Unable to read \"%s\" from %s%d:%d **\n",
  102. EXT4_ENV_FILE, EXT4_ENV_INTERFACE, dev, part);
  103. goto err_env_relocate;
  104. }
  105. env_import(buf, 1);
  106. return;
  107. err_env_relocate:
  108. set_default_env(NULL);
  109. }