sdk-core v2026.11.05
PS2 SDK - core tier (build-critical EE/IOP modules)
Loading...
Searching...
No Matches
main.c
1#ifdef BUILDING_SMAP_NETMAN
2#include <errno.h>
3#endif
4#include <stdio.h>
5#include <defs.h>
6#ifdef BUILDING_SMAP_PS2IP
7#include <sysclib.h>
8#endif
9#include <loadcore.h>
10#include <thbase.h>
11#ifdef BUILDING_SMAP_PS2IP
12#include <thevent.h>
13#include <thsemap.h>
14#include <intrman.h>
15#endif
16#ifdef BUILDING_SMAP_PS2IP
17#include <ps2ip.h>
18#endif
19#ifdef BUILDING_SMAP_NETMAN
20#include <irx.h>
21#include <netman.h>
22#endif
23
24#include "main.h"
25#ifdef BUILDING_SMAP_NETMAN
26#include "xfer.h"
27#endif
28
29// Last SDK 3.1.0 has INET family version "2.26.0"
30// SMAP module is the same as "2.25.0"
31IRX_ID("SMAP_driver", 0x2, 0x1A);
32
33#ifdef BUILDING_SMAP_PS2IP
34
35#define IFNAME0 's'
36#define IFNAME1 'm'
37
38typedef struct ip4_addr IPAddr;
39typedef struct netif NetIF;
40typedef struct SMapIF SMapIF;
41typedef struct pbuf PBuf;
42
43static struct pbuf *TxHead, *TxTail;
44static void EnQTxPacket(struct pbuf *tx);
45
46static NetIF NIF;
47
48// From lwip/err.h and lwip/tcpip.h
49
50#define ERR_OK 0 // No error, everything OK
51#define ERR_MEM -1 // Out of memory error.
52#define ERR_CONN -6 // Not connected
53#define ERR_IF -11 // Low-level netif error
54
55// SMapLowLevelOutput():
56
57// This function is called by the TCP/IP stack when a low-level packet should be sent. It'll be invoked in the context of the
58// tcpip-thread.
59
60static err_t
61SMapLowLevelOutput(NetIF *pNetIF, PBuf *pOutput)
62{
63 err_t result;
64 struct pbuf *pbuf;
65
66#if USE_GP_REGISTER
67 void *OldGP;
68
69 OldGP = SetModuleGP();
70#endif
71
72 (void)pNetIF;
73
74 result = ERR_OK;
75 if (pOutput->tot_len > pOutput->len) {
76 pbuf_ref(pOutput); // Increment reference count because LWIP must free the PBUF, not the driver!
77 if ((pbuf = pbuf_coalesce(pOutput, PBUF_RAW)) != pOutput) { // No need to increase reference count because pbuf_coalesce() does it.
78 EnQTxPacket(pbuf);
79 SMAPXmit();
80 } else
81 result = ERR_MEM;
82 } else {
83 pbuf_ref(pOutput); // This will be freed later.
84 EnQTxPacket(pOutput);
85 SMAPXmit();
86 }
87
88#if USE_GP_REGISTER
89 SetGP(OldGP);
90#endif
91
92 return result;
93}
94
95// SMapIFInit():
96
97// Should be called at the beginning of the program to set up the network interface.
98
99static err_t
100SMapIFInit(NetIF *pNetIF)
101{
102#if USE_GP_REGISTER
103 void *OldGP;
104
105 OldGP = SetModuleGP();
106#endif
107
108 TxHead = NULL;
109 TxTail = NULL;
110
111 pNetIF->name[0] = IFNAME0;
112 pNetIF->name[1] = IFNAME1;
113 pNetIF->output = &etharp_output;
114 pNetIF->linkoutput = &SMapLowLevelOutput;
117 pNetIF->mtu = 1500;
118
119 // Get MAC address.
120 SMAPGetMACAddress(pNetIF->hwaddr);
121 DEBUG_PRINTF("MAC address : %02x:%02x:%02x:%02x:%02x:%02x\n", pNetIF->hwaddr[0], pNetIF->hwaddr[1], pNetIF->hwaddr[2],
122 pNetIF->hwaddr[3], pNetIF->hwaddr[4], pNetIF->hwaddr[5]);
123
124 // Enable sending and receiving of data.
125 SMAPInitStart();
126
127#if USE_GP_REGISTER
128 SetGP(OldGP);
129#endif
130
131 return ERR_OK;
132}
133
134void SMapLowLevelInput(PBuf *pBuf)
135{
136 // When we receive data, the interrupt-handler will invoke this function, which means we are in an interrupt-context. Pass on
137 // the received data to ps2ip.
138
139 ps2ip_input(pBuf, &NIF);
140}
141
142static void EnQTxPacket(struct pbuf *tx)
143{
144 int OldState;
145
146 CpuSuspendIntr(&OldState);
147
148 if (TxHead != NULL)
149 TxHead->next = tx;
150
151 TxHead = tx;
152 tx->next = NULL;
153
154 if (TxTail == NULL) // Queue empty
155 TxTail = TxHead;
156
157 CpuResumeIntr(OldState);
158}
159
160int SMapTxPacketNext(void **payload)
161{
162 int len;
163
164 if (TxTail != NULL) {
165 *payload = TxTail->payload;
166 len = TxTail->len;
167 } else
168 len = 0;
169
170 return len;
171}
172
173void SMapTxPacketDeQ(void)
174{
175 struct pbuf *toFree;
176 int OldState;
177
178 toFree = NULL;
179
180 CpuSuspendIntr(&OldState);
181 if (TxTail != NULL) {
182 toFree = TxTail;
183
184 if (TxTail == TxHead) {
185 // Last in queue.
186 TxTail = NULL;
187 TxHead = NULL;
188 } else {
189 TxTail = TxTail->next;
190 }
191 }
192 CpuResumeIntr(OldState);
193
194 if (toFree != NULL) {
195 toFree->next = NULL;
196 pbuf_free(toFree);
197 }
198}
199
200static inline int SMapInit(IPAddr *IP, IPAddr *NM, IPAddr *GW, int argc, char *argv[])
201{
202 if (smap_init(argc, argv) != 0) {
203 return 0;
204 }
205 DEBUG_PRINTF("SMapInit: SMap initialized\n");
206
207 netif_add(&NIF, IP, NM, GW, &NIF, &SMapIFInit, tcpip_input);
208 netif_set_default(&NIF);
209 netif_set_up(&NIF);
210 DEBUG_PRINTF("SMapInit: NetIF added to ps2ip\n");
211
212 // Return 1 (true) to indicate success.
213
214 return 1;
215}
216
217static void
218PrintIP(struct ip4_addr const *pAddr)
219{
220 printf("%d.%d.%d.%d", (u8)pAddr->addr, (u8)(pAddr->addr >> 8), (u8)(pAddr->addr >> 16), (u8)(pAddr->addr >> 24));
221}
222
223void PS2IPLinkStateUp(void)
224{
225 tcpip_callback((void *)&netif_set_link_up, &NIF);
226}
227
228void PS2IPLinkStateDown(void)
229{
230 tcpip_callback((void *)&netif_set_link_down, &NIF);
231}
232#endif
233
234#if defined(BUILDING_SMAP_NETMAN) || defined(BUILDING_SMAP_NETDEV)
235/* DECLARE_EXPORT_TABLE in iop/kernel/include/irx.h places the export
236 table in .text via a section(".text\n\t#") asm trick, so the extern
237 declaration needs no section attribute. (The previous section("data")
238 was misleading: a bare "data" name produces an orphan ELF section the
239 IRX loader does not allocate; harmless here only because the actual
240 definition lives in .text.) */
241extern struct irx_export_table _exp_smap;
242#endif
243
244#ifdef BUILDING_SMAP_MODULAR
245extern struct irx_export_table _exp_smapmodu;
246#endif
247
248int _start(int argc, char *argv[])
249{
250#ifdef BUILDING_SMAP_PS2IP
251 IPAddr IP;
252 IPAddr NM;
253 IPAddr GW;
254 int numArgs;
255 char **pArgv;
256#endif
257#if defined(BUILDING_SMAP_NETMAN) || defined(BUILDING_SMAP_NETDEV)
258 int result;
259#endif
260
261#ifdef BUILDING_SMAP_NETDEV
262 if (argc < 0) {
263 result = smap_deinit();
264 if (result == MODULE_NO_RESIDENT_END) {
265#ifdef BUILDING_SMAP_MODULAR
266 ReleaseLibraryEntries(&_exp_smapmodu);
267#endif
268 ReleaseLibraryEntries(&_exp_smap);
269 }
270 return result;
271 }
272#endif
273
274#if defined(BUILDING_SMAP_NETMAN) || defined(BUILDING_SMAP_NETDEV)
275 if (RegisterLibraryEntries(&_exp_smap) != 0) {
276 DEBUG_PRINTF("module already loaded\n");
277 return MODULE_NO_RESIDENT_END;
278 }
279#endif
280
281#ifdef BUILDING_SMAP_MODULAR
282 if (RegisterLibraryEntries(&_exp_smapmodu) != 0) {
283 DEBUG_PRINTF("module already loaded\n");
284 return MODULE_NO_RESIDENT_END;
285 }
286#endif
287
288 DisplayBanner();
289
290 // This code was present in SMAP, but cannot be implemented with the default IOP kernel due to MODLOAD missing these functions.
291 // It may be necessary to prevent SMAP from linking with an old DEV9 module.
292
293 /* if ((ModuleID = SearchModuleByName("dev9")) < 0) {
294 sceInetPrintf("dev9 module not found\n");
295 return MODULE_NO_RESIDENT_END;
296 }
297 if (ReferModuleStatus(ModuleID, &ModStatus) < 0) {
298 sceInetPrintf("can't get dev9 module status\n");
299 return MODULE_NO_RESIDENT_END;
300 }
301
302 if (ModStatus.version < 0x204) {
303 sceInetPrintf("dev9 module version must be 2.4 or later\n");
304 return MODULE_NO_RESIDENT_END;
305 } */
306
307 (void)argc;
308 (void)argv;
309#ifdef BUILDING_SMAP_PS2IP
310 // Parse IP args.
311 DEBUG_PRINTF("argc %d\n", argc);
312
313 if (argc >= 4) {
314 DEBUG_PRINTF("%s %s %s\n", argv[1], argv[2], argv[3]);
315 IP.addr = inet_addr(argv[1]);
316 NM.addr = inet_addr(argv[2]);
317 GW.addr = inet_addr(argv[3]);
318
319 numArgs = argc - 4;
320 pArgv = &argv[4];
321 } else {
322 // Set some defaults.
323
324 IP4_ADDR(&IP, 192, 168, 0, 80);
325 IP4_ADDR(&NM, 255, 255, 255, 0);
326 IP4_ADDR(&GW, 192, 168, 0, 1);
327
328 numArgs = argc - 1;
329 pArgv = &argv[1];
330 }
331#endif
332
333#if defined(BUILDING_SMAP_NETMAN) || defined(BUILDING_SMAP_NETDEV)
334 if ((result = smap_init(argc, argv)) < 0) {
335 DEBUG_PRINTF("smap_init -> %d\n", result);
336 ReleaseLibraryEntries(&_exp_smap);
337 return MODULE_NO_RESIDENT_END;
338 }
339#endif
340
341#ifdef BUILDING_SMAP_PS2IP
342 if (!SMapInit(&IP, &NM, &GW, numArgs, pArgv)) {
343
344 // Something went wrong, return 1 to indicate failure.
345 return MODULE_NO_RESIDENT_END;
346 }
347
348 printf("Initialized OK, IP: ");
349 PrintIP(&IP);
350 printf(", NM: ");
351 PrintIP(&NM);
352 printf(", GW: ");
353 PrintIP(&GW);
354 printf("\n");
355
356 // Initialized ok.
357#endif
358
359 return MODULE_RESIDENT_END;
360}
#define IP4_ADDR(ipaddr, a, b, c, d)
Definition tcpip.h:328
#define NETIF_MAX_HWADDR_LEN
Definition tcpip.h:1109
err_t tcpip_callback(tcpip_callback_fn function, void *ctx)
Definition tcpip.c:323
err_t tcpip_input(struct pbuf *p, struct netif *inp)
Definition tcpip.c:297
@ ERR_OK
Definition err.h:55
@ ERR_MEM
Definition err.h:57
#define NETIF_FLAG_ETHARP
Definition tcpip.h:1137
#define NETIF_FLAG_BROADCAST
Definition tcpip.h:1127
void netif_set_link_down(struct netif *netif)
Definition netif.c:1056
struct netif * netif_add(struct netif *netif, void *state, netif_init_fn init, netif_input_fn input)
Definition netif.c:287
void netif_set_default(struct netif *netif)
Definition netif.c:849
void netif_set_link_up(struct netif *netif)
Definition netif.c:1018
void netif_set_up(struct netif *netif)
Definition netif.c:871
struct pbuf * pbuf_coalesce(struct pbuf *p, pbuf_layer layer)
Definition pbuf.c:1318
void pbuf_ref(struct pbuf *p)
Definition pbuf.c:833
u8 pbuf_free(struct pbuf *p)
Definition pbuf.c:729
@ PBUF_RAW
Definition tcpip.h:200
int CpuResumeIntr(int state)
Definition intrman.c:217
int CpuSuspendIntr(int *state)
Definition intrman.c:195
u8 hwaddr_len
Definition tcpip.h:1347
netif_output_fn output
Definition tcpip.h:1296
char name[2]
Definition tcpip.h:1353
u8 hwaddr[NETIF_MAX_HWADDR_LEN]
Definition tcpip.h:1349
u8 flags
Definition tcpip.h:1351
netif_linkoutput_fn linkoutput
Definition tcpip.h:1301
u16 mtu
Definition tcpip.h:1345
Definition tcpip.h:234
void * payload
Definition tcpip.h:239
struct pbuf * next
Definition tcpip.h:236
u16 tot_len
Definition tcpip.h:248
u16 len
Definition tcpip.h:251