2 * Copyright (C) 2008 dhewg, #wiidev efnet
4 * this file is part of wiifuse
5 * http://wiibrew.org/index.php?title=Wiifuse
7 * This program is free software; you can redistribute it and/or modify
8 * it under the terms of the GNU General Public License as published by
9 * the Free Software Foundation; either version 2 of the License, or
10 * (at your option) any later version.
12 * This program is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 * GNU General Public License for more details.
17 * You should have received a copy of the GNU General Public License
18 * along with this program; if not, write to the Free Software
19 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
31 #include "../../shared.h"
33 #include "fs_wrapper.h"
37 // use the "authenticated" flag before executing commands
38 // kick out clients when receiving another auth
39 // take usb.c from libdb instead
40 struct client_request
* read_request () {
41 u8 buf
[PACKET_REQUEST_PAYLOAD
];
42 struct client_request
*request
;
45 LOG (3, "waiting for a request...");
46 usb_receivebuffersafe (buf
, PACKET_REQUEST_PAYLOAD
);
48 if (buf
[0] != 'W' || buf
[1] != 'F' || buf
[2] != 'R')
51 request
= (struct client_request
*)
52 malloc (sizeof (struct client_request
));
53 memset (request
, 0, sizeof (struct client_request
));
55 request
->command
= buf
[3];
57 request
->data_size
= get_be16 (&buf
[5]);
60 request
->filename
= malloc (fn_size
+ 1);
61 LOG (3, "waiting for filename (%d)", fn_size
);
62 usb_receivebuffersafe (request
->filename
, fn_size
);
63 request
->filename
[fn_size
] = 0;
66 if (request
->data_size
) {
67 request
->data
= malloc (request
->data_size
);
68 LOG (3, "waiting for data (%d)", request
->data_size
);
69 usb_receivebuffersafe (request
->data
, request
->data_size
);
75 void send_response (const enum remote_result result
, const u8
*data
,
76 const u16 data_size
) {
77 u8 buf
[PACKET_RESPONSE_PAYLOAD
];
79 memset (buf
, 0, PACKET_RESPONSE_PAYLOAD
);
88 put_be16 (&buf
[4], data_size
);
90 LOG (3, "sending response");
91 usb_sendbuffersafe (buf
, PACKET_RESPONSE_PAYLOAD
);
94 LOG (3, "sending response data (%d)", data_size
);
95 usb_sendbuffersafe (data
, data_size
);
99 enum remote_result
identify (struct client_request
*request
, u8
**data
,
114 if (!request
->data
|| request
->data_size
< 6)
119 certs_size
= get_be16 (p
);
122 tmd_size
= get_be16 (p
);
125 tik_size
= get_be16 (p
);;
128 if (request
->data_size
!= certs_size
+ tmd_size
+ tik_size
+ 6)
131 sb_certs
= memalign (32, certs_size
);
132 memcpy (sb_certs
, p
, certs_size
);
135 sb_tmd
= memalign (32, tmd_size
);
136 memcpy (sb_tmd
, p
, tmd_size
);
139 sb_tik
= memalign (32, tik_size
);
140 memcpy (sb_tik
, p
, tik_size
);
142 printf ("identifying...\n");
144 res
= ES_Identify ((signed_blob
*) sb_certs
, certs_size
,
145 (signed_blob
*) sb_tmd
, tmd_size
,
146 (signed_blob
*) sb_tik
, tik_size
, NULL
);
149 if (IS_VALID_SIGNATURE ((signed_blob
*) sb_tmd
))
151 SIGNATURE_PAYLOAD ((signed_blob
*) sb_tmd
))->group_id
;
158 LOG_ERR ("ES_Identify failed: %d", res
);
162 res
= ES_GetTitleID (&title_id
);
164 LOG_ERR ("error getting the title id: %d", res
);
168 printf ("identified as: 0x%016llx, group id: 0x%04x\n", title_id
, gid
);
174 struct client_request
*request
;
176 u8 authenticated
= 0;
177 char *auth
= WIIFUSE_AUTH_DATA
;
181 enum remote_result result
;
185 printf ("awaiting requests, fire up wiifuse on the other end\n");
187 request
= read_request ();
190 LOG_ERR ("received invalid packet");
194 result
= RES_UNKNOWN
;
198 switch (request
->command
) {
201 request
->data_size
== strlen (auth
) &&
202 !memcmp (request
->data
, auth
, strlen (auth
)))
205 if (result
== RES_OK
)
206 printf ("granted access to client\n");
208 printf ("refused client access\n");
210 authenticated
= result
== RES_OK
;
211 exit
= result
!= RES_OK
;
215 if (request
->data
&& request
->data_size
== 1) {
216 verbose_level
= request
->data
[0];
217 printf ("using verbose level %u\n",
224 printf ("\nreceived umount command, stopping server\n");
229 case CMD_ES_IDENTIFY
:
230 result
= identify (request
, &data
, &data_size
);
231 exit
= result
!= RES_OK
;
232 init
= result
== RES_OK
;
236 result
= wrapper_access (request
);
240 result
= wrapper_getattr (request
, &data
, &data_size
);
244 result
= wrapper_open (request
);
248 result
= wrapper_read (request
, &data
, &data_size
);
252 result
= wrapper_opendir (request
);
256 result
= wrapper_readdir (request
, &data
, &data_size
);
260 result
= wrapper_mkdir (request
);
264 result
= wrapper_rmdir (request
);
268 result
= wrapper_create (request
);
272 result
= wrapper_write (request
, &data
, &data_size
);
276 result
= wrapper_chown (request
);
280 result
= wrapper_chmod (request
);
284 result
= wrapper_rename (request
);
288 result
= wrapper_unlink (request
);
292 result
= wrapper_statfs (request
, &data
, &data_size
);
296 LOG_ERR ("received an unknown command");
302 if (!wrapper_init (&data
, &data_size
)) {
303 printf ("wrapper_init failed\n");
310 send_response (result
, data
, data_size
);
319 printf ("server was stopped\n");
321 if (!wrapper_deinit ())
322 printf ("wrapper_deinit failed\n");