es.c 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216
  1. #include <stdio.h>
  2. #include <stdbool.h>
  3. #include <switch.h>
  4. #include "nx/es.h"
  5. #include "util/log.h"
  6. // globals.
  7. Service g_es_service = {0};
  8. bool es_start_service()
  9. {
  10. Result rc = smGetService(&g_es_service, "es");
  11. if (R_FAILED(rc))
  12. {
  13. write_log("failed to start es service %s\n", __func__);
  14. return false;
  15. }
  16. return true;
  17. }
  18. void es_close_service()
  19. {
  20. serviceClose(&g_es_service);
  21. }
  22. bool es_import_tik_and_cert(void const *tik_buf, size_t tik_size, void const *cert_buf, size_t cert_size)
  23. {
  24. Result rc = serviceDispatch(&g_es_service, 1,
  25. .buffer_attrs = { SfBufferAttr_HipcMapAlias | SfBufferAttr_In, SfBufferAttr_HipcMapAlias | SfBufferAttr_In },
  26. .buffers = { { tik_buf, tik_size }, { cert_buf, cert_size } });
  27. if (R_FAILED(rc))
  28. {
  29. write_log("failed to import tik / cert\n");
  30. return false;
  31. }
  32. return true;
  33. }
  34. // not implemented //
  35. bool es_import_ticket_certificate_set(void const *cert_buf, size_t cert_size)
  36. {
  37. Result rc = serviceDispatch(&g_es_service, 2,
  38. .buffer_attrs = { SfBufferAttr_HipcMapAlias | SfBufferAttr_In },
  39. .buffers = { { cert_buf, cert_size } });
  40. if (R_FAILED(rc))
  41. {
  42. write_log("failed to import tik / cert\n");
  43. return false;
  44. }
  45. return true;
  46. }
  47. bool es_delete_common_tik(const FsRightsId *rights_id)
  48. {
  49. Result rc = serviceDispatch(&g_es_service, 3,
  50. .buffer_attrs = { SfBufferAttr_HipcMapAlias | SfBufferAttr_In },
  51. .buffers = { { rights_id, sizeof(FsRightsId) } });
  52. if (R_FAILED(rc))
  53. {
  54. write_log("failed to delete common tik\n");
  55. return false;
  56. }
  57. return true;
  58. }
  59. bool es_delete_personalised_tik(const FsRightsId *rights_id)
  60. {
  61. Result rc = serviceDispatch(&g_es_service, 4,
  62. .buffer_attrs = { SfBufferAttr_HipcMapAlias | SfBufferAttr_In },
  63. .buffers = { { rights_id, sizeof(FsRightsId) } });
  64. if (R_FAILED(rc))
  65. {
  66. write_log("failed to delete personalised tik\n");
  67. return false;
  68. }
  69. return true;
  70. }
  71. bool es_delete_all_common_tik(void)
  72. {
  73. Result rc = serviceDispatch(&g_es_service, 5, SfOutHandleAttr_None);
  74. if (R_FAILED(rc))
  75. {
  76. write_log("failed to delete all common tickets\n");
  77. return false;
  78. }
  79. return true;
  80. }
  81. bool es_delete_all_personalised_tik(void)
  82. {
  83. Result rc = serviceDispatch(&g_es_service, 6, SfOutHandleAttr_None);
  84. if (R_FAILED(rc))
  85. {
  86. write_log("failed to delete all personalised tickets\n");
  87. return false;
  88. }
  89. return true;
  90. }
  91. bool es_delete_all_personalised_tik_ex()
  92. {
  93. Result rc = serviceDispatch(&g_es_service, 7, SfOutHandleAttr_None);
  94. if (R_FAILED(rc))
  95. {
  96. write_log("failed to delete all personalised tickets\n");
  97. return false;
  98. }
  99. return true;
  100. }
  101. uint32_t es_count_common_tik(void)
  102. {
  103. uint32_t out_total = 0;
  104. Result rc = serviceDispatchOut(&g_es_service, 9, out_total, SfOutHandleAttr_None);
  105. if (R_FAILED(rc))
  106. {
  107. write_log("failed to count common tickets\n");
  108. return 0;
  109. }
  110. return out_total;
  111. }
  112. uint32_t es_count_personailsed_tik(void)
  113. {
  114. uint32_t out_total = 0;
  115. Result rc = serviceDispatchOut(&g_es_service, 10, out_total, SfOutHandleAttr_None);
  116. if (R_FAILED(rc))
  117. {
  118. write_log("failed to count personalized tickets\n");
  119. return 0;
  120. }
  121. return out_total;
  122. }
  123. bool es_list_common_tik(FsRightsId *out, uint32_t count)
  124. {
  125. uint32_t out_total = 0;
  126. uint64_t buffer_idk = 0;
  127. Result rc = serviceDispatchInOut(&g_es_service, 11, buffer_idk, out_total,
  128. .buffer_attrs = { SfBufferAttr_HipcMapAlias | SfBufferAttr_Out },
  129. .buffers = { { out, count * sizeof(FsRightsId) } });
  130. if (R_FAILED(rc))
  131. {
  132. write_log("failed to list common tickets\n");
  133. return false;
  134. }
  135. return true;
  136. }
  137. bool es_list_personalised_tik(FsRightsId *out, uint32_t count)
  138. {
  139. uint32_t out_total = 0;
  140. uint64_t buffer_idk = 0;
  141. Result rc = serviceDispatchInOut(&g_es_service, 12, buffer_idk, out_total,
  142. .buffer_attrs = { SfBufferAttr_HipcMapAlias | SfBufferAttr_Out },
  143. .buffers = { { out, count * sizeof(FsRightsId) } });
  144. if (R_FAILED(rc))
  145. {
  146. write_log("failed to list personalised tickets\n");
  147. return false;
  148. }
  149. return true;
  150. }
  151. uint32_t es_get_common_tik_size(const FsRightsId *rights_id)
  152. {
  153. uint32_t out_size = 0;
  154. uint64_t buffer_idk = 0;
  155. Result rc = serviceDispatchInOut(&g_es_service, 14, buffer_idk, out_size,
  156. .buffer_attrs = { SfBufferAttr_HipcMapAlias | SfBufferAttr_Out },
  157. .buffers = { { rights_id, sizeof(FsRightsId) } });
  158. if (R_FAILED(rc))
  159. {
  160. write_log("failed to get common tik size\n");
  161. return 0;
  162. }
  163. return out_size;
  164. }
  165. bool es_get_common_tik_data(void *out, size_t out_size, const FsRightsId *rights_id)
  166. {
  167. uint64_t buffer_idk = 0;
  168. Result rc = serviceDispatchInOut(&g_es_service, 16, rights_id, buffer_idk,
  169. .buffer_attrs = { SfBufferAttr_HipcMapAlias | SfBufferAttr_Out },
  170. .buffers = { { out, out_size } });
  171. if (R_FAILED(rc))
  172. {
  173. write_log("failed to get common ticket data\n");
  174. return false;
  175. }
  176. return true;
  177. }