libtransistor
A userland library for the Nintendo Switch
 All Data Structures Files Functions Variables Typedefs Enumerations Enumerator Macros
loader_config.h
Go to the documentation of this file.
1 
6 #pragma once
7 
8 #ifdef __cplusplus
9 extern "C" {
10 #endif
11 
12 // homebrew ABI as of http://switchbrew.org/index.php?title=Homebrew_ABI&oldid=3461
13 
14 #include<libtransistor/types.h>
15 #include<libtransistor/ipc.h>
16 
17 #include<assert.h>
18 
19 extern bool IS_NRO;
20 
21 typedef enum {
22  LOADER_CONFIG_FLAG_RECOGNITION_MANDATORY = BIT(0),
23 } loader_config_flags_t;
24 
25 typedef enum {
26  LCONFIG_KEY_END_OF_LIST = 0,
27  LCONFIG_KEY_MAIN_THREAD_HANDLE = 1,
28  LCONFIG_KEY_NEXT_LOAD_PATH = 2,
29  LCONFIG_KEY_OVERRIDE_HEAP = 3,
30  LCONFIG_KEY_OVERRIDE_SERVICE = 4,
31  LCONFIG_KEY_ARGV = 5,
32  LCONFIG_KEY_SYSCALL_AVAILABLE_HINT = 6,
33  LCONFIG_KEY_APPLET_TYPE = 7,
34  LCONFIG_KEY_APPLET_WORKAROUND = 8,
35  LCONFIG_KEY_STDIO_SOCKETS = 9,
36  LCONFIG_KEY_PROCESS_HANDLE = 10,
37  LCONFIG_KEY_LAST_LOAD_RESULT = 11,
38  LCONFIG_KEY_ALLOC_PAGES = 12,
39  LCONFIG_KEY_LOCK_REGION = 13,
40 
41  LCONFIG_KEY_TWILI_PRESENT = 52,
42 } loader_config_key_t;
43 
44 typedef enum {
45  LCONFIG_APPLET_TYPE_APPLICATION = 0,
46  LCONFIG_APPLET_TYPE_SYSTEM_APPLET = 1,
47  LCONFIG_APPLET_TYPE_LIBRARY_APPLET = 2,
48  LCONFIG_APPLET_TYPE_OVERLAY_APPLET = 3,
49  LCONFIG_APPLET_TYPE_SYSTEM_APPLICATION = 4,
50 
51  LCONFIG_APPLET_TYPE_UNKNOWN = 0xFFFFFFFFFFFFFFFF,
52 } loader_config_applet_type_t;
53 
54 typedef enum {
55  LCONFIG_SOCKET_SERVICE_BSD_U = 0,
56  LCONFIG_SOCKET_SERVICE_BSD_S = 1,
57  LCONFIG_SOCKET_SERVICE_MAX = 2,
58 
59  LCONFIG_SOCKET_SERVICE_UNSPECIFIED = 0xFFFFFFFF,
60 } loader_config_socket_service_t;
61 
62 typedef struct {
63  uint64_t service_name;
64  handle_t service_handle;
66 
67 typedef struct {
68  uint32_t key;
69  uint32_t flags;
70  union {
71  uint64_t value[2];
72  struct {
73  } end_of_list;
74  struct {
75  handle_t main_thread_handle;
76  } main_thread_handle;
77  struct {
78  char (*nro_path)[512];
79  char (*argv_str)[2048];
80  } next_load_path;
81  struct {
82  void *heap_base;
83  size_t heap_size;
84  } override_heap;
85  struct {
87  } override_service;
88  struct {
89  uint64_t argc;
90  char **argv;
91  } argv;
92  struct {
93  uint64_t hints[2];
94  } syscall_available_hint;
95  struct {
96  loader_config_applet_type_t applet_type;
97  } applet_type;
98  struct {
99  aruid_t aruid;
100  } applet_workaround;
101  struct {
102  // naming these stdout, stdin, and stderr conflicts with newlib macros
103  int32_t s_stdout, s_stdin, s_stderr;
104  loader_config_socket_service_t socket_service;
105  } stdio_sockets;
106  struct {
107  handle_t process_handle;
108  } process_handle;
109  struct {
110  result_t result;
111  } last_load_result;
112  struct {
113  void *(*alloc_pages)(size_t min, size_t max, size_t *actual);
114  bool (*free_pages)(void *pages);
115  } alloc_pages;
116  struct {
117  void *addr;
118  size_t size;
119  } lock_region;
120  };
122 
123 typedef struct {
124  // MainThreadHandle
125  handle_t main_thread;
126 
127  // NextLoadPath
128  // ignored
129 
130  // OverrideHeap
131  bool heap_overridden;
132  void *heap_base;
133  size_t heap_size;
134 
135  // OverrideService
136  int num_service_overrides;
137  loader_config_service_override_t service_overrides[32];
138 
139  // Argv
140  char **argv;
141  int argc;
142 
143  // SyscallAvailableHint
144  uint64_t syscall_hints[2];
145 
146  // AppletType
147  bool has_applet_type;
148  loader_config_applet_type_t applet_type;
149 
150  // AppletWorkaround
151  bool applet_workaround_active;
152  aruid_t applet_workaround_aruid;
153 
154  // StdioSockets
155  bool has_stdio_sockets;
156  int socket_stdout, socket_stdin, socket_stderr;
157  loader_config_socket_service_t socket_service;
158 
159  // ProcessHandle
160  bool has_process_handle;
161  handle_t process_handle;
162 
163  // AllocPages
164  bool has_alloc_pages;
165  void *(*alloc_pages)(size_t min, size_t max, size_t *actual);
166  bool (*free_pages)(void *pages);
167 
168  // TwiliPresent
169  bool has_twili;
171 
172 extern loader_config_t loader_config;
173 
174 static_assert(sizeof(loader_config_entry_t) == 0x18, "loader config entry size should be 0x18 bytes");
175 
179 void lconfig_init_default(uint64_t thread_handle);
180 
185 
191 result_t lconfig_has_syscall(uint8_t syscall);
192 
200 result_t lconfig_has_syscalls(const uint8_t *syscalls);
201 
209 _Noreturn void trn_dirty_exit(int code);
210 
211 #ifdef __cplusplus
212 }
213 #endif
Various system types.
result_t lconfig_has_syscalls(const uint8_t *syscalls)
Checks if all the given syscalls are hinted.
result_t lconfig_has_syscall(uint8_t syscall)
Checks if a given syscall is hinted.
uint32_t result_t
Function result.
Definition: types.h:51
Definition: loader_config.h:62
Definition: loader_config.h:123
Definition: loader_config.h:67
uint32_t handle_t
Resource handle.
Definition: types.h:38
result_t lconfig_parse(loader_config_entry_t *config)
Parses loader config.
#define BIT(n)
Creates a bitmask from a bit number.
Definition: types.h:57
void lconfig_init_default(uint64_t thread_handle)
Initializes default loader config.
_Noreturn void trn_dirty_exit(int code)
Exits application without cleanup.
bool free_pages(void *pages)
Frees memory pages returned from alloc_pages.
Interprocess Communication data structures and functions.
void * alloc_pages(size_t min, size_t max, size_t *actual)
Allocates memory pages.
uint64_t aruid_t
Applet resource user id.
Definition: types.h:52