forums.ps2dev.org Forum Index forums.ps2dev.org
Homebrew PS2, PSP & PS3 Development Discussions
 
 FAQFAQ   SearchSearch   MemberlistMemberlist   UsergroupsUsergroups   RegisterRegister 
 ProfileProfile   Log in to check your private messagesLog in to check your private messages   Log inLog in 

Usermode WiFi with devhook (with WPA enabled :D)

 
Post new topic   Reply to topic    forums.ps2dev.org Forum Index -> PSP Development
View previous topic :: View next topic  
Author Message
mbf



Joined: 18 Aug 2006
Posts: 55

PostPosted: Sun Aug 20, 2006 8:33 am    Post subject: Usermode WiFi with devhook (with WPA enabled :D) Reply with quote

Looks like a few people including myself have been looking for a way to have WIFI with WPA enabled in their homebrew.

The main problem is that FW 1.5 does not provide WPA. The way to go if you don't want to upgrade and use eLoader is devhook to run 2.71. Alright, Devhook allows you to run homebrew since v.45 but only in user mode. The next problem is that the commonly used method to access WIFI is via kernel mode (to load the modules), unusable with Devhook right now.

Thanks to Chris Swindle and Fanjita, there's a way to load the modules from user mode: http://forums.ps2dev.org/viewtopic.php?t=5423

I hadn't seen any sample code to use this, so I thought I would give it a stab (I'm dying to use PMP VLC and PSP Radio over my WPA protected AP ;))... And it works! Here's a sample based on the resolver sample from the PSP SDK.

Important point: you can't use pspSdkInetInit() to initialize the Network modules since it calls sceNetApctlInit() with a stack size of 0x1000 by default. Just make your own and call sceNetApctlInit() with a stack size of at least 0x1400.

To compile the code below, just use the homebrew2xx sample from the Devhook SDK as a template, and add wifi_user.o to the OBJS line in the Makefile.

Enjoy :)

I'm going to recompile PSP Radio now ;)

Main.c:
Code:
/*
   FW2.xx+devhook WIFI Sample
*/
#include <pspkernel.h>
#include <pspctrl.h>
#include <pspdebug.h>
#include <pspdisplay.h>
#include <psptypes.h>
#include <pspiofilemgr.h>
#include <pspnet.h>
#include <pspnet_inet.h>
#include <pspnet_apctl.h>
#include <pspnet_resolver.h>
#include <netinet/in.h>
#include <arpa/inet.h>
#include <sys/select.h>
#include <stdio.h>
#include <string.h>
#include <sys/types.h>
#include <sys/unistd.h>

#define MODULE_NAME "wifi_user_dh"

PSP_MODULE_INFO(MODULE_NAME, 0x0000, 1, 1);

#define printf pspDebugScreenPrintf

#define PSP_NET_MODULE_COMMON 1
#define PSP_NET_MODULE_ADHOC 2
#define PSP_NET_MODULE_INET 3
#define PSP_NET MODULE_PARSEURI 4
#define PSP_NET_MODULE_PARSEHTTP 5
#define PSP_NET_MODULE_HTTP 6
#define PSP_NET_MODULE_SSL 7

extern int sceUtilityLoadNetModule(int);
extern int sceUtilityUnloadNetModule(int);

#define RESOLVE_NAME "google.com"

/* Exit callback */
int exit_callback(int arg1, int arg2, void *common)
{
   sceKernelExitGame();

   return 0;
}

/* Callback thread */
int CallbackThread(SceSize args, void *argp)
{
   int cbid;

   cbid = sceKernelCreateCallback("Exit Callback", exit_callback, NULL);
   sceKernelRegisterExitCallback(cbid);

   sceKernelSleepThreadCB();

   return 0;
}

/* Sets up the callback thread and returns its thread id */
int SetupCallbacks(void)
{
   int thid = 0;

   thid = sceKernelCreateThread("update_thread", CallbackThread, 0x11, 0xFA0, 0, 0);
   if(thid >= 0)
   {
      sceKernelStartThread(thid, 0, 0);
   }

   return thid;
}

int pspUserInetInit()
{
   u32 retVal;

   retVal = sceNetInit(0x20000, 0x20, 0x1000, 0x20, 0x1000);
   if (retVal != 0)
      return retVal;

   retVal = sceNetInetInit();
   if (retVal != 0)
      return retVal;

   retVal = sceNetResolverInit();
   if (retVal != 0)
      return retVal;
   
   retVal = sceNetApctlInit(0x1400, 0x42); // increased stack size
   if (retVal != 0)
      return retVal;

   return 0;
}

void do_resolver(void)
{
   int rid = -1;
   char buf[1024];
   struct in_addr addr;
   char name[1024];

   do
   {
      /* Create a resolver */
      if(sceNetResolverCreate(&rid, buf, sizeof(buf)) < 0)
      {
         printf("Error creating resolver\n");
         break;
      }

      printf("Created resolver %08x\n", rid);

      /* Resolve a name to an ip address */
      if(sceNetResolverStartNtoA(rid, RESOLVE_NAME, &addr, 2, 3) < 0)
      {
         printf("Error resolving %s\n", RESOLVE_NAME);
         break;
      }

      printf("Resolved %s to %s\n", RESOLVE_NAME, inet_ntoa(addr));

      /* Resolve the ip address to a name */
      if(sceNetResolverStartAtoN(rid, &addr, name, sizeof(name), 2, 3) < 0)
      {
         printf("Error resolving ip to name\n");
         break;
      }

      printf("Resolved ip to %s\n", name);
   }
   while(0);

   if(rid >= 0)
   {
      sceNetResolverDelete(rid);
   }
}

/* Connect to an access point */
int connect_to_apctl(int config)
{
   int err;
   int stateLast = -1;

   /* Connect using the first profile */
   err = sceNetApctlConnect(config);
   if (err != 0)
   {
      printf(MODULE_NAME ": sceNetApctlConnect returns %08X\n", err);
      return 0;
   }

   printf(MODULE_NAME ": Connecting...\n");
   while (1)
   {
      int state;
      err = sceNetApctlGetState(&state);
      if (err != 0)
      {
         printf(MODULE_NAME ": sceNetApctlGetState returns $%x\n", err);
         break;
      }
      if (state > stateLast)
      {
         printf("  connection state %d of 4\n", state);
         stateLast = state;
      }
      if (state == 4)
         break;  // connected with static IP o_O

      // wait a little before polling again
      sceKernelDelayThread(50*1000); // 50ms
   }
   printf(MODULE_NAME ": Connected!\n");

   if(err != 0)
   {
      return 0;
   }

   return 1;
}

int main(void)
{
   int res;
   pspDebugScreenInit();
   SetupCallbacks();

   // Load WIFI modules
   res = sceUtilityLoadNetModule(PSP_NET_MODULE_COMMON);
   printf("sceUtilityLoadNetModule(PSP_NET_MODULE_COMMON) returned %08X\n", res);
   res = sceUtilityLoadNetModule(PSP_NET_MODULE_INET);
   printf("sceUtilityLoadNetModule(PSP_NET_MODULE_INET) returned %08X\n", res);

   if((res = pspUserInetInit()))
   {
      printf(MODULE_NAME ": Error, could not initialise the network %08X\n", res);
   }
   else
   {
      if(connect_to_apctl(1))
      {
         // connected, get my IPADDR and run test
         char szMyIPAddr[32];
         if (sceNetApctlGetInfo(8, szMyIPAddr) != 0)
            strcpy(szMyIPAddr, "unknown IP address");
         printf("IP address: %s\n", szMyIPAddr);
         do_resolver();
      }
   }

    sceKernelSleepThread();
   return 0;
}


wifi_user.S:
Code:

    .set noreorder

#include "pspstub.s"

    STUB_START   "sceUtility",0x40010000,0x00020005
    STUB_FUNC   0x1579a159,sceUtilityLoadNetModule
    STUB_FUNC   0x64d50c56,sceUtilityUnloadNetModule
    STUB_END


Last edited by mbf on Mon Aug 21, 2006 11:22 pm; edited 1 time in total
Back to top
View user's profile Send private message
dot_blank



Joined: 28 Sep 2005
Posts: 498
Location: Brasil

PostPosted: Sun Aug 20, 2006 4:39 pm    Post subject: Reply with quote

very nice :)
...now i can finally get a WPA router
without the need for firmware updates

for testing purposes of course ;)
_________________
10011011 00101010 11010111 10001001 10111010
Back to top
View user's profile Send private message
TheBuzzer



Joined: 06 Feb 2006
Posts: 49

PostPosted: Mon Aug 21, 2006 2:48 am    Post subject: Reply with quote

interesting. the devhook vshex addons got to be in kernal mode only and the homebrews got to be in usermode only. quite funny.
Back to top
View user's profile Send private message
dot_blank



Joined: 28 Sep 2005
Posts: 498
Location: Brasil

PostPosted: Mon Aug 21, 2006 3:56 am    Post subject: Reply with quote

above post is irrelevant
_________________
10011011 00101010 11010111 10001001 10111010
Back to top
View user's profile Send private message
mbf



Joined: 18 Aug 2006
Posts: 55

PostPosted: Mon Aug 21, 2006 11:49 pm    Post subject: Reply with quote

Corrected the STUB_START (there are only 2 imports, not 34). Quick question: Would it confluict with other imports for sceUtility (imports from the PSPSDK)?

Ok, my first tests with PSP Radio are a half success: I got it to compile and connect to the network. However, the background images don't display, the text rendering is weird and it crashes during playback (even from the MS). It also crashes when parsing the SHOUTcast XML (seems to be a bug in PSP Radio), the weird thing being that it doesn't crash in 1.5. WIP....

Porting from 1.5 to DH+2.71 is harder than one would have thought :-/

Regarding kernel mode.... can anyone think of a way to gain kernel mode access under DEVHOOK, or at least run apps in a PSPLINK-like environment for easier debugging (DH allows you to load almost whatever modules you like... not sure if those are usable from userland though)
Back to top
View user's profile Send private message
danzel



Joined: 04 Nov 2005
Posts: 182

PostPosted: Tue Aug 22, 2006 7:56 am    Post subject: Reply with quote

What about the bug in 2.5/2.6 that allowed us to get kernel access for downgrading?

Not sure if anyone has tryed it on 2.7/2.71 yet, it was discovered after they were released so it may still be around.
Back to top
View user's profile Send private message
jimparis



Joined: 10 Jun 2005
Posts: 1179
Location: Boston

PostPosted: Tue Aug 22, 2006 8:58 am    Post subject: Re: Usermode WiFi with devhook (with WPA enabled :D) Reply with quote

mbf wrote:
Important point: you can't use pspSdkInetInit() to initialize the Network modules since it calls sceNetApctlInit() with a stack size of 0x1000 by default. Just make your own and call sceNetApctlInit() with a stack size of at least 0x1400.
I increased the stack to 0x1400 in pspSdkInetInit in rev. 1992.
Back to top
View user's profile Send private message
moonlight



Joined: 26 Oct 2005
Posts: 567

PostPosted: Tue Aug 22, 2006 9:05 am    Post subject: Reply with quote

TheBuzzer wrote:
interesting. the devhook vshex addons got to be in kernal mode only and the homebrews got to be in usermode only. quite funny.


There are reasons for that, it's not done on purpose.
Hint: a lot of things changed in 2.00+
Back to top
View user's profile Send private message
mbf



Joined: 18 Aug 2006
Posts: 55

PostPosted: Tue Aug 22, 2006 11:09 am    Post subject: Reply with quote

danzel wrote:
What about the bug in 2.5/2.6 that allowed us to get kernel access for downgrading?

Not sure if anyone has tryed it on 2.7/2.71 yet, it was discovered after they were released so it may still be around.

I'd rather go the devhook way... EBOOTS for devhook have to be "special" PRXes and are loaded by devhook from plain2x.prx (as far as I understand from the little English docs). My main goal for that would mainly be using PSPLINK ;)

jimparis wrote:
I increased the stack to 0x1400 in pspSdkInetInit in rev. 1992

Thanks. Looks like I updated right before that :( Have sceUtilityLoadNetModule() and sceUtilityUnloadNetModule() been added too?

I've finally managed to get it working. The problem was that the default heap size is 16KB under devhook... Since my built is based on the Static build, it was not initialized properly. PSP Radio with WPA WIFI rocks :) I'll submit the diff to Raf tomorrow.


Last edited by mbf on Wed Aug 23, 2006 12:00 am; edited 2 times in total
Back to top
View user's profile Send private message
Fanjita



Joined: 28 Sep 2005
Posts: 217

PostPosted: Tue Aug 22, 2006 10:57 pm    Post subject: Reply with quote

danzel wrote:
What about the bug in 2.5/2.6 that allowed us to get kernel access for downgrading?

Not sure if anyone has tryed it on 2.7/2.71 yet, it was discovered after they were released so it may still be around.


It's still present in 2.7 and 2.71.
Most likely it's the reason that 2.8 was released.
_________________
Got a v2.0-v2.80 firmware PSP? Download the eLoader here to run homebrew on it!
The PSP Homebrew Database needs you!
Back to top
View user's profile Send private message
mbf



Joined: 18 Aug 2006
Posts: 55

PostPosted: Tue Aug 22, 2006 11:37 pm    Post subject: Reply with quote

mbf wrote:
jimparis wrote:
I increased the stack to 0x1400 in pspSdkInetInit in rev. 1992

Thanks. Looks like I updated right before that :( Have sceUtilityLoadNetModule() and sceUtilityUnloadNetModule() been added too?

Stupid me, you've submitted the changes after my post. Thanks again Jim. I'll post a diff for sceUtility later on.
Back to top
View user's profile Send private message
mbf



Joined: 18 Aug 2006
Posts: 55

PostPosted: Wed Aug 23, 2006 9:52 am    Post subject: Reply with quote

Patch for PSP Radio sent to raf (@sourceforege.net) hope he'll get it :)

EDIT: Source+binaries available here: http://www.megaupload.com/?d=KE3YSLI2
Back to top
View user's profile Send private message
lusid



Joined: 23 Aug 2006
Posts: 3

PostPosted: Thu Aug 24, 2006 2:20 am    Post subject: Reply with quote

Thank you.

There is a repeatable crash when using the home button to exit. The normal exit method works fine.
Back to top
View user's profile Send private message
mbf



Joined: 18 Aug 2006
Posts: 55

PostPosted: Thu Aug 24, 2006 3:30 am    Post subject: Reply with quote

Yes, it tends to crash when exitting via the home button. But I don't know if it comes from the DH build or if it's the same with the 1.5 and eLoader versions.
EDIT: note that the only code change I made was to load the network modules in user mode. The rest was just compile and link according to DH's requirements.
Back to top
View user's profile Send private message
lusid



Joined: 23 Aug 2006
Posts: 3

PostPosted: Thu Aug 24, 2006 3:46 am    Post subject: Reply with quote

1.5 Doesn't crash there. I don't have an eLoader test environment at the moment, but I seem to recall the home button not working at all under eLoader so the that code may have never needed to execute under a static build before.

Anyway, thanks again for the build. I wish more of the userland eboots were ported to devhook.
Back to top
View user's profile Send private message
zaide



Joined: 26 Aug 2006
Posts: 1

PostPosted: Sat Aug 26, 2006 8:52 am    Post subject: Reply with quote

mbf can you post your eboot.pbp? for dummies peoples who don't want to install the compilator on their linux juste for that ;)
Back to top
View user's profile Send private message
mbf



Joined: 18 Aug 2006
Posts: 55

PostPosted: Sat Aug 26, 2006 12:16 pm    Post subject: Reply with quote

See one of the posts above.... Raf also released a new version of PSP Radio with a devhook version here (take PSPRadio0.38.12_DH.zip). Please use the PSP Radio forums if you need any help with that.
Back to top
View user's profile Send private message
Display posts from previous:   
Post new topic   Reply to topic    forums.ps2dev.org Forum Index -> PSP Development All times are GMT + 10 Hours
Page 1 of 1

 
Jump to:  
You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot vote in polls in this forum


Powered by phpBB © 2001, 2005 phpBB Group