env_ext4.c 2.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116
  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 <ext4fs.h>
  17. char *env_name_spec = "EXT4";
  18. env_t *env_ptr;
  19. DECLARE_GLOBAL_DATA_PTR;
  20. int env_init(void)
  21. {
  22. /* use default */
  23. gd->env_addr = (ulong)&default_environment[0];
  24. gd->env_valid = 1;
  25. return 0;
  26. }
  27. #ifdef CONFIG_CMD_SAVEENV
  28. int saveenv(void)
  29. {
  30. env_t env_new;
  31. ssize_t len;
  32. char *res;
  33. block_dev_desc_t *dev_desc = NULL;
  34. int dev = EXT4_ENV_DEVICE;
  35. int part = EXT4_ENV_PART;
  36. int err;
  37. res = (char *)&env_new.data;
  38. len = hexport_r(&env_htab, '\0', 0, &res, ENV_SIZE, 0, NULL);
  39. if (len < 0) {
  40. error("Cannot export environment: errno = %d\n", errno);
  41. return 1;
  42. }
  43. dev_desc = get_dev(EXT4_ENV_INTERFACE, dev);
  44. if (dev_desc == NULL) {
  45. printf("Failed to find %s%d\n",
  46. EXT4_ENV_INTERFACE, dev);
  47. return 1;
  48. }
  49. err = ext4_register_device(dev_desc, part);
  50. if (err) {
  51. printf("Failed to register %s%d:%d\n",
  52. EXT4_ENV_INTERFACE, dev, part);
  53. return 1;
  54. }
  55. env_new.crc = crc32(0, env_new.data, ENV_SIZE);
  56. err = ext4fs_write(EXT4_ENV_FILE, (void *)&env_new, sizeof(env_t));
  57. ext4fs_close();
  58. if (err == -1) {
  59. printf("\n** Unable to write \"%s\" from %s%d:%d **\n",
  60. EXT4_ENV_FILE, EXT4_ENV_INTERFACE, dev, part);
  61. return 1;
  62. }
  63. puts("done\n");
  64. return 0;
  65. }
  66. #endif /* CONFIG_CMD_SAVEENV */
  67. void env_relocate_spec(void)
  68. {
  69. char buf[CONFIG_ENV_SIZE];
  70. block_dev_desc_t *dev_desc = NULL;
  71. int dev = EXT4_ENV_DEVICE;
  72. int part = EXT4_ENV_PART;
  73. int err;
  74. dev_desc = get_dev(EXT4_ENV_INTERFACE, dev);
  75. if (dev_desc == NULL) {
  76. printf("Failed to find %s%d\n",
  77. EXT4_ENV_INTERFACE, dev);
  78. set_default_env(NULL);
  79. return;
  80. }
  81. err = ext4_register_device(dev_desc, part);
  82. if (err) {
  83. printf("Failed to register %s%d:%d\n",
  84. EXT4_ENV_INTERFACE, dev, part);
  85. set_default_env(NULL);
  86. return;
  87. }
  88. err = ext4_read_file(EXT4_ENV_FILE, (uchar *)&buf, 0, CONFIG_ENV_SIZE);
  89. ext4fs_close();
  90. if (err == -1) {
  91. printf("\n** Unable to read \"%s\" from %s%d:%d **\n",
  92. EXT4_ENV_FILE, EXT4_ENV_INTERFACE, dev, part);
  93. set_default_env(NULL);
  94. return;
  95. }
  96. env_import(buf, 1);
  97. }