sata.c 2.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126
  1. /*
  2. * (C) Copyright 2010-2016 Freescale Semiconductor, Inc.
  3. *
  4. * SPDX-License-Identifier: GPL-2.0+
  5. */
  6. /* #define DEBUG */
  7. #include <common.h>
  8. #include <command.h>
  9. #include <environment.h>
  10. #include <linux/stddef.h>
  11. #include <errno.h>
  12. #include <memalign.h>
  13. #include <sata.h>
  14. #include <search.h>
  15. #if defined(CONFIG_ENV_SIZE_REDUND) || defined(CONFIG_ENV_OFFSET_REDUND)
  16. #error ENV REDUND not supported
  17. #endif
  18. #if !defined(CONFIG_ENV_OFFSET) || !defined(CONFIG_ENV_SIZE)
  19. #error CONFIG_ENV_OFFSET or CONFIG_ENV_SIZE not defined
  20. #endif
  21. DECLARE_GLOBAL_DATA_PTR;
  22. __weak int sata_get_env_dev(void)
  23. {
  24. return CONFIG_SYS_SATA_ENV_DEV;
  25. }
  26. #ifdef CONFIG_CMD_SAVEENV
  27. static inline int write_env(struct blk_desc *sata, unsigned long size,
  28. unsigned long offset, void *buffer)
  29. {
  30. uint blk_start, blk_cnt, n;
  31. blk_start = ALIGN(offset, sata->blksz) / sata->blksz;
  32. blk_cnt = ALIGN(size, sata->blksz) / sata->blksz;
  33. n = blk_dwrite(sata, blk_start, blk_cnt, buffer);
  34. return (n == blk_cnt) ? 0 : -1;
  35. }
  36. static int env_sata_save(void)
  37. {
  38. ALLOC_CACHE_ALIGN_BUFFER(env_t, env_new, 1);
  39. struct blk_desc *sata = NULL;
  40. int env_sata, ret;
  41. if (sata_initialize())
  42. return 1;
  43. env_sata = sata_get_env_dev();
  44. sata = sata_get_dev(env_sata);
  45. if (sata == NULL) {
  46. printf("Unknown SATA(%d) device for environment!\n",
  47. env_sata);
  48. return 1;
  49. }
  50. ret = env_export(env_new);
  51. if (ret)
  52. return 1;
  53. printf("Writing to SATA(%d)...", env_sata);
  54. if (write_env(sata, CONFIG_ENV_SIZE, CONFIG_ENV_OFFSET, &env_new)) {
  55. puts("failed\n");
  56. return 1;
  57. }
  58. puts("done\n");
  59. return 0;
  60. }
  61. #endif /* CONFIG_CMD_SAVEENV */
  62. static inline int read_env(struct blk_desc *sata, unsigned long size,
  63. unsigned long offset, void *buffer)
  64. {
  65. uint blk_start, blk_cnt, n;
  66. blk_start = ALIGN(offset, sata->blksz) / sata->blksz;
  67. blk_cnt = ALIGN(size, sata->blksz) / sata->blksz;
  68. n = blk_dread(sata, blk_start, blk_cnt, buffer);
  69. return (n == blk_cnt) ? 0 : -1;
  70. }
  71. static void env_sata_load(void)
  72. {
  73. ALLOC_CACHE_ALIGN_BUFFER(char, buf, CONFIG_ENV_SIZE);
  74. struct blk_desc *sata = NULL;
  75. int env_sata;
  76. if (sata_initialize())
  77. return -EIO;
  78. env_sata = sata_get_env_dev();
  79. sata = sata_get_dev(env_sata);
  80. if (sata == NULL) {
  81. printf("Unknown SATA(%d) device for environment!\n", env_sata);
  82. return -EIO;
  83. }
  84. if (read_env(sata, CONFIG_ENV_SIZE, CONFIG_ENV_OFFSET, buf)) {
  85. set_default_env(NULL);
  86. return -EIO;
  87. }
  88. env_import(buf, 1);
  89. return 0;
  90. }
  91. U_BOOT_ENV_LOCATION(sata) = {
  92. .location = ENVL_ESATA,
  93. ENV_NAME("SATA")
  94. .load = env_sata_load,
  95. .save = env_save_ptr(env_sata_save),
  96. };