ext4.c 2.8 KB

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