light-iopmp-hal.c 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126
  1. // SPDX-License-Identifier: GPL-2.0-or-later
  2. /*
  3. * Copyright (C) 2021 Alibaba Group Holding Limited.
  4. *
  5. */
  6. #include <assert.h>
  7. #include <errno.h>
  8. #include <fcntl.h>
  9. #include <stdio.h>
  10. #include <stdlib.h>
  11. #include <string.h>
  12. #include <unistd.h>
  13. #include <sys/types.h>
  14. #include "light-iopmp.h"
  15. static const char *light_iopmp_tap = "/sys/devices/platform/iopmp/light_iopmp_tap";
  16. static const char *light_iopmp_start_addr = "/sys/devices/platform/iopmp/light_iopmp_start_addr";
  17. static const char *light_iopmp_end_addr = "/sys/devices/platform/iopmp/light_iopmp_end_addr";
  18. static const char *light_iopmp_attr = "/sys/devices/platform/iopmp/light_iopmp_attr";
  19. static const char *light_iopmp_lock = "/sys/devices/platform/iopmp/light_iopmp_lock";
  20. static const char *light_iopmp_set = "/sys/devices/platform/iopmp/light_iopmp_set";
  21. /**
  22. * @brief Light iopmp region permission setting.
  23. *
  24. * @param type
  25. * @param attr
  26. * @return csi_err_t
  27. */
  28. int csi_iopmp_set_attr(int type, u_int8_t *start_addr, u_int8_t *end_addr, csi_iopmp_attr_t attr)
  29. {
  30. int ret = 0;
  31. int light_iopmp_tap_fd, light_iopmp_start_addr_fd, light_iopmp_end_addr_fd;
  32. int light_iopmp_attr_fd, light_iopmp_lock_fd, light_iopmp_set_fd;
  33. light_iopmp_tap_fd = open(light_iopmp_tap, O_RDWR);
  34. if (light_iopmp_tap_fd < 0) {
  35. perror("open tap");
  36. exit(1);
  37. } else {
  38. char string[5];
  39. sprintf(string, "%d\n", type);
  40. if (write(light_iopmp_tap_fd, string, 5) != 5)
  41. perror("write(tap)");
  42. close(light_iopmp_tap_fd);
  43. }
  44. light_iopmp_start_addr_fd = open(light_iopmp_start_addr, O_RDWR);
  45. if (light_iopmp_start_addr_fd < 0) {
  46. perror("open start_addr");
  47. exit(1);
  48. } else {
  49. char string[20];
  50. int start = (int)((int64_t)start_addr >> 12);
  51. sprintf(string, "%d\n", start);
  52. if (write(light_iopmp_start_addr_fd, string, 20) != 20)
  53. perror("write(start_addr)");
  54. close(light_iopmp_start_addr_fd);
  55. }
  56. light_iopmp_end_addr_fd = open(light_iopmp_end_addr, O_RDWR);
  57. if (light_iopmp_end_addr_fd < 0) {
  58. perror("open end_addr");
  59. exit(1);
  60. } else {
  61. int end = (int)((int64_t)end_addr >> 12);
  62. char string[20];
  63. sprintf(string, "%d\n", end);
  64. if (write(light_iopmp_end_addr_fd, string, 20) != 20)
  65. perror("write(end_addr)");
  66. close(light_iopmp_end_addr_fd);
  67. }
  68. light_iopmp_attr_fd = open(light_iopmp_attr, O_RDWR);
  69. if (light_iopmp_attr_fd < 0) {
  70. perror("open attr");
  71. exit(1);
  72. } else {
  73. char string[20];
  74. sprintf(string, "%d\n", attr);
  75. if (write(light_iopmp_attr_fd, string, 20) != 20)
  76. perror("write(attr)");
  77. close(light_iopmp_attr_fd);
  78. }
  79. light_iopmp_lock_fd = open(light_iopmp_lock, O_RDWR);
  80. if (light_iopmp_lock_fd < 0) {
  81. perror("open lock");
  82. exit(1);
  83. } else {
  84. if (write(light_iopmp_lock_fd, "1\n", 2) != 2)
  85. perror("write(lock)");
  86. close(light_iopmp_lock_fd);
  87. }
  88. light_iopmp_set_fd = open(light_iopmp_set, O_WRONLY);
  89. if (light_iopmp_set_fd < 0) {
  90. perror("open set");
  91. exit(1);
  92. } else {
  93. if (write(light_iopmp_set_fd, "1\n", 2) != 2)
  94. perror("write(set)");
  95. close(light_iopmp_set_fd);
  96. }
  97. return ret;
  98. }
  99. /** iopmp lock
  100. * @brief Lock secure iopmp setting.
  101. *
  102. * @return csi_err_t
  103. */
  104. int csi_iopmp_lock(void)
  105. {
  106. /* dummy API, csi_iopmp_set_attr() should lock iopmp */
  107. return 0;
  108. }
  109. #if 0 /* demo */
  110. int main(int argc, char *argv[])
  111. {
  112. csi_iopmp_set_attr(2, (u_int8_t *)0x0, (u_int8_t *)0x200000000, 0xffff);
  113. return 1;
  114. }
  115. #endif