1
  2
  3
  4
  5
  6
  7
  8
  9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
use crate::sys::SceUid;
use crate::sys::ScePspDateTime;
use crate::eabi::{i6, i_ii_i_rii, i_ii_i_ri};
use core::ffi::c_void;

/// Describes a single directory entry
#[repr(C)]
#[derive(Copy, Clone)]
pub struct SceIoDirent {
    /// File status.
    pub d_stat: SceIoStat,
    /// File name.
    pub d_name: [u8; 256usize],
    /// Device-specific data.
    pub d_private: *mut c_void,
    pub dummy: i32,
}

/// Structure to hold the status information about a file
#[repr(C)]
#[derive(Debug, Copy, Clone)]
pub struct SceIoStat {
    pub st_mode: IoStatMode,
    pub st_attr: IoStatAttr,
    /// Size of the file in bytes.
    pub st_size: i64,
    /// Creation time.
    pub st_ctime: ScePspDateTime,
    /// Access time.
    pub st_atime: ScePspDateTime,
    /// Modification time.
    pub st_mtime: ScePspDateTime,
    /// Device-specific data.
    pub st_private: [u32; 6usize],
}

bitflags::bitflags! {
    #[repr(transparent)]
    pub struct IoStatMode: i32 {
        /// Symbolic Link
        const IFLNK = 0x4000;
        /// Directory
        const IFDIR = 0x1000;
        /// Regular file
        const IFREG = 0x2000;
        /// Set UID
        const ISUID = 0x0800;
        /// Set GID
        const ISGID = 0x0400;
        /// Sticky
        const ISVTX = 0x0200;
        /// Read user permission
        const IRUSR = 0x0100;
        /// Write user permission
        const IWUSR = 0x0080;
        /// Execute user permission
        const IXUSR = 0x0040;
        /// Read group permission
        const IRGRP = 0x0020;
        /// Write group permission
        const IWGRP = 0x0010;
        /// Execute group permission
        const IXGRP = 0x0008;
        /// Read others permission
        const IROTH = 0x0004;
        /// Write others permission
        const IWOTH = 0x0002;
        /// Execute others permission
        const IXOTH = 0x0001;
    }
}

bitflags::bitflags! {
    #[repr(transparent)]
    pub struct IoStatAttr: u32 {
        /// Symlink
        const IFLNK = 0x0008;
        /// Directory
        const IFDIR = 0x0010;
        /// Regular file
        const IFREG = 0x0020;
        /// Hidden read permisson
        const IROTH = 0x0004;
        /// Hidden write permission
        const IWOTH = 0x0002;
        /// Hidden execution permission
        const IXOTH = 0x0001;
    }
}

#[repr(u32)]
/// Permission value for the sceIoAssign function
#[derive(Debug, Copy, Clone, PartialEq, Eq, Hash)]
pub enum IoAssignPerms {
    RdWr = 0,
    RdOnly = 1,
}

#[repr(u32)]
pub enum IoWhence {
    Set = 0,
    Cur = 1,
    End = 2,
}

bitflags::bitflags! {
    #[repr(transparent)]
    pub struct IoOpenFlags: i32 {
        const RD_ONLY = 0x0001;
        const WR_ONLY = 0x0002;
        const RD_WR = 0x0003;
        const NBLOCK = 0x0004;
        const DIR = 0x0008;
        const APPEND = 0x0100;
        const CREAT = 0x0200;
        const TRUNC = 0x0400;
        const EXCL = 0x0800;
        const NO_WAIT = 0x8000;
    }
}

/// Octal unix permissions
pub type IoPermissions = i32;

psp_extern! {
    #![name = "IoFileMgrForUser"]
    #![flags = 0x4001]
    #![version = (0, 0)]

    #[psp(0x109F50BC)]
    /// Open or create a file for reading or writing
    ///
    /// # Parameters
    ///
    /// - `file`: Pointer to a string holding the name of the file to open
    /// - `flags`: Libc styled flags that are or'ed together
    /// - `permissions`: Octal unix permissions.
    ///
    /// # Return value
    ///
    /// A non-negative integer is a valid fd, anything else an error
    pub fn sceIoOpen(file: *const u8, flags: IoOpenFlags, permissions: IoPermissions) -> SceUid;

    #[psp(0x89AA9906)]
    /// Open or create a file for reading or writing (asynchronous)
    ///
    /// # Parameters
    ///
    /// - `file`: Pointer to a string holding the name of the file to open
    /// - `flags`: Libc styled flags that are or'ed together
    /// - `permissions`: Octal unix permissions.
    ///
    /// # Return value
    ///
    /// A non-negative integer is a valid fd, anything else an error
    pub fn sceIoOpenAsync(
        file: *const u8,
        flags: IoOpenFlags,
        permissions: IoPermissions
    ) -> SceUid;

    #[psp(0x810C4BC3)]
    /// Delete a descriptor
    ///
    /// # Parameters
    ///
    /// - `fd`: File descriptor to close
    ///
    /// # Return value
    ///
    /// < 0 on error
    pub fn sceIoClose(fd: SceUid) -> i32;

    #[psp(0xFF5940B6)]
    /// Delete a descriptor (asynchronous)
    ///
    /// # Parameters
    ///
    /// - `fd`: File descriptor to close
    ///
    /// # Return value
    ///
    /// < 0 on error
    pub fn sceIoCloseAsync(fd: SceUid) -> i32;

    #[psp(0x6A638D83)]
    /// Read input
    ///
    /// # Parameters
    ///
    /// - `fd`: Opened file descriptor to read from
    /// - `data`: Pointer to the buffer where the read data will be placed
    /// - `size`: Size of the read in bytes
    ///
    /// # Return value
    ///
    /// The number of bytes read
    pub fn sceIoRead(fd: SceUid, data: *mut c_void, size: u32)
     -> i32;

    #[psp(0xA0B5A7C2)]
    /// Read input (asynchronous)
    ///
    /// # Parameters
    ///
    /// - `fd`: Opened file descriptor to read from
    /// - `data`: Pointer to the buffer where the read data will be placed
    /// - `size`: Size of the read in bytes
    ///
    /// # Return value
    ///
    /// < 0 on error.
    pub fn sceIoReadAsync(fd: SceUid, data: *mut c_void, size: u32)
     -> i32;


    #[psp(0x42EC03AC)]
    /// Write output
    ///
    /// # Parameters
    ///
    /// - `fd`: Opened file descriptor to write to
    /// - `data`: Pointer to the data to write
    /// - `size`: Size of data to write
    ///
    /// # Return value
    ///
    /// The number of bytes written
    pub fn sceIoWrite(fd: SceUid, data: *const c_void, size: usize) -> i32;

    #[psp(0x0FACAB19)]
    /// Write output (asynchronous)
    ///
    /// # Parameters
    ///
    /// - `fd`: Opened file descriptor to write to
    /// - `data`: Pointer to the data to write
    /// - `size`: Size of data to write
    ///
    /// # Return value
    ///
    /// < 0 on error.
    pub fn sceIoWriteAsync(fd: SceUid, data: *const c_void, size: u32)
     -> i32;

    #[psp(0x27EB27B8, i_ii_i_rii)]
    /// Reposition read/write file descriptor offset
    ///
    /// # Parameters
    ///
    /// - `fd`: Opened file descriptor with which to seek
    /// - `offset`: Relative offset from the start position given by whence
    /// - `whence`: Set to `IoWhence::Set` to seek from the start of the file,
    ///   `IoWhence::Cur` seek from the current position and `IoWhence::End` to seek
    ///   from the end.
    ///
    /// # Return value
    ///
    /// The position in the file after the seek.
    pub fn sceIoLseek(fd: SceUid, offset: i64, whence: IoWhence) -> i64;

    #[psp(0x71B19E77, i_ii_i_ri)]
    /// Reposition read/write file descriptor offset (asynchronous)
    ///
    /// # Parameters
    ///
    /// - `fd`: Opened file descriptor with which to seek
    /// - `offset`: Relative offset from the start position given by whence
    /// - `whence`: Set to `IoWhence::Set` to seek from the start of the file,
    ///   `IoWhence::Cur` seek from the current position and IoWhence::End to seek
    ///   from the end.
    ///
    /// # Return value
    ///
    /// < 0 on error. Actual value should be passed returned by the ::sceIoWaitAsync call.
    pub fn sceIoLseekAsync(fd: SceUid, offset: i64, whence: IoWhence) -> i32;

    #[psp(0x68963324)]
    /// Reposition read/write file descriptor offset (32bit mode)
    ///
    /// # Parameters
    ///
    /// - `fd`: Opened file descriptor with which to seek
    /// - `offset`: Relative offset from the start position given by whence
    /// - `whence`: Set to `IoWhence::Set` to seek from the start of the file,
    ///   `IoWhence::Cur` seek from the current position and IoWhence::End to seek
    ///   from the end.
    ///
    /// # Return value
    ///
    /// The position in the file after the seek.
    pub fn sceIoLseek32(fd: SceUid, offset: i32, whence: IoWhence) -> i32;

    #[psp(0x1B385D8F)]
    /// Reposition read/write file descriptor offset (32bit mode, asynchronous)
    ///
    /// # Parameters
    ///
    /// - `fd`: Opened file descriptor with which to seek
    /// - `offset`: Relative offset from the start position given by whence
    /// - `whence`: Set to IoWhence::Set to seek from the start of the file, IoWhence::Cur
    ///   seek from the current position and IoWhence::End to seek from the end.
    ///
    /// # Return value
    ///
    /// < 0 on error.
    pub fn sceIoLseek32Async(fd: SceUid, offset: i32, whence: IoWhence) -> i32;

    #[psp(0xF27A9C51)]
    /// Remove directory entry
    ///
    /// # Parameters
    ///
    /// - `file`: Path to the file to remove
    ///
    /// # Return value
    ///
    /// < 0 on error
    pub fn sceIoRemove(file: *const u8) -> i32;

    #[psp(0x06A70004)]
    /// Make a directory file
    ///
    /// # Parameters
    ///
    /// `dir`
    /// - `mode`: Access mode.
    ///
    /// # Return value
    ///
    /// Returns the value 0 if its succesful otherwise -1
    pub fn sceIoMkdir(dir: *const u8, mode: IoPermissions) -> i32;

    #[psp(0x1117C65F)]
    /// Remove a directory file
    /// # Parameters
    ///
    /// - `path`: Removes a directory file pointed by the string path
    ///
    /// # Return value
    ///
    /// Returns the value 0 if its succesful otherwise -1
    pub fn sceIoRmdir(path: *const u8) -> i32;

    #[psp(0x55F4717D)]
    /// Change the current directory.
    /// # Parameters
    ///
    /// - `path`: The path to change to.
    ///
    /// # Return value
    ///
    /// < 0 on error.
    pub fn sceIoChdir(path: *const u8) -> i32;

    #[psp(0x779103A0)]
    /// Change the name of a file
    ///
    /// # Parameters
    ///
    /// - `oldname`: The old filename
    /// - `newname`: The new filename
    ///
    /// # Return value
    ///
    /// < 0 on error.
    pub fn sceIoRename(oldname: *const u8, newname: *const u8) -> i32;

    #[psp(0xB29DDF9C)]
    /// Open a directory
    ///
    /// # Parameters
    ///
    /// - `dirname`: The directory to open for reading.
    ///
    /// # Return value
    ///
    /// If >= 0 then a valid file descriptor, otherwise a Sony error code.
    pub fn sceIoDopen(dirname: *const u8) -> SceUid;

    #[psp(0xE3EB004C)]
    /// Reads an entry from an opened file descriptor.
    ///
    /// # Parameters
    ///
    /// - `fd`: Already opened file descriptor (using sceIoDopen)
    /// - `dir`: Pointer to an `SceIoDirent` structure to hold the file information
    ///
    /// # Return value
    ///
    /// Read status
    /// -   0 - No more directory entries left
    /// - > 0 - More directory entired to go
    /// - < 0 - Error
    pub fn sceIoDread(fd: SceUid, dir: *mut SceIoDirent) -> i32;

    #[psp(0xEB092469)]
    /// Close an opened directory file descriptor
    ///
    /// # Parameters
    ///
    /// - `fd`: Already opened file descriptor (using sceIoDopen)
    ///
    /// # Return value
    ///
    /// < 0 on error
    pub fn sceIoDclose(fd: SceUid) -> i32;

    #[psp(0x54F5FB11, i6)]
    /// Send a devctl command to a device.
    ///
    ///  # Parameters
    ///
    /// - `dev`: String for the device to send the devctl to (e.g. "ms0:")
    /// - `cmd`: The command to send to the device
    /// - `indata`: A data block to send to the device, if NULL sends no data
    /// - `inlen`: Length of indata, if 0 sends no data
    /// - `outdata`: A data block to receive the result of a command, if NULL receives no data
    /// - `outlen`: Length of outdata, if 0 receives no data
    ///
    /// # Return value
    ///
    /// 0 on success, < 0 on error
    pub fn sceIoDevctl(
        dev: *const u8,
        cmd: u32,
        indata: *mut c_void,
        inlen: i32,
        outdata: *mut c_void,
        outlen: i32
    ) -> i32;

    #[psp(0xB2A628C1,i6)]
    /// Assigns one IO device to another (I guess)
    ///
    /// # Parameters
    ///
    /// - `dev1`: The device name to assign.
    /// - `dev2`: The block device to assign from.
    /// - `dev3`: The filesystem device to mape the block device to dev1
    /// - `mode`: Read/Write mode. One of `IoAssignPerms`.
    /// - `unk1`: Unknown, set to NULL.
    /// - `unk2`: Unknown, set to 0.
    ///
    /// # Return value
    ///
    /// < 0 on error.
    pub fn sceIoAssign(
        dev1: *const u8,
        dev2: *const u8,
        dev3: *const u8,
        mode: IoAssignPerms,
        unk1: *mut c_void,
        unk2: i32
    ) -> i32;

    #[psp(0x6D08A871)]
    /// Unassign an IO device.
    ///
    /// # Parameters
    ///
    /// - `dev`: The device to unassign.
    ///
    /// # Return value
    ///
    /// < 0 on error
    pub fn sceIoUnassign(dev: *const u8) -> i32;

    #[psp(0xACE946E8)]
    /// Get the status of a file.
    ///
    /// # Parameters
    ///
    /// - `file`: The path to the file.
    /// - `stat`: A pointer to an `SceIoStat` structure.
    ///
    /// # Return value
    ///
    /// < 0 on error.
    pub fn sceIoGetstat(file: *const u8, stat: *mut SceIoStat) -> i32;

    #[psp(0xB8A740F4)]
    /// Change the status of a file.
    ///
    /// # Parameters
    ///
    /// - `file`: The path to the file.
    /// - `stat`: A pointer to an `SceIoStat` structure.
    /// - `bits`: Bitmask defining which bits to change.
    ///
    /// # Return value
    ///
    /// < 0 on error.
    pub fn sceIoChstat(file: *const u8, stat: *mut SceIoStat, bits: i32) -> i32;

    #[psp(0x63632449, i6)]
    /// Perform an ioctl on a device.
    ///
    /// # Parameters
    ///
    /// - `fd`: Opened file descriptor to ioctl to
    /// - `cmd`: The command to send to the device
    /// - `indata`: A data block to send to the device, if NULL sends no data
    /// - `inlen`: Length of indata, if 0 sends no data
    /// - `outdata`: A data block to receive the result of a command, if NULL receives no data
    /// - `outlen`: Length of outdata, if 0 receives no data
    ///
    /// # Return value
    ///
    /// 0 on success, < 0 on error
    pub fn sceIoIoctl(
        fd: SceUid,
        cmd: u32,
        indata: *mut c_void,
        inlen: i32,
        outdata: *mut c_void,
        outlen: i32
    ) -> i32;

    #[psp(0xE95A012B, i6)]
    /// Perform an ioctl on a device. (asynchronous)
    ///
    /// # Parameters
    ///
    /// - `fd`: Opened file descriptor to ioctl to
    /// - `cmd`: The command to send to the device
    /// - `indata`: A data block to send to the device, if NULL sends no data
    /// - `inlen`: Length of indata, if 0 sends no data
    /// - `outdata`: A data block to receive the result of a command, if NULL receives no data
    /// - `outlen`: Length of outdata, if 0 receives no data
    ///
    /// # Return value
    ///
    /// 0 on success, < 0 on error
    pub fn sceIoIoctlAsync(
        fd: SceUid,
        cmd: u32,
        indata: *mut c_void,
        inlen: i32,
        outdata: *mut c_void,
        outlen: i32
    ) -> i32;

    #[psp(0xAB96437F)]
    /// Synchronise the file data on the device.
    ///
    /// # Parameters
    ///
    /// - `device`: The device to synchronise (e.g. msfat0:)
    /// - `unk`: Unknown
    ///
    /// # Return value
    ///
    /// ???
    pub fn sceIoSync(device: *const u8, unk: u32) -> i32;

    #[psp(0xE23EEC33)]
    /// Wait for asyncronous completion.
    ///
    /// # Parameters
    ///
    /// - `fd`: The file descriptor which is current performing an asynchronous action.
    /// - `res`: The result of the async action.
    ///
    /// # Return value
    ///
    /// < 0 on error.
    pub fn sceIoWaitAsync(fd: SceUid, res: *mut i64) -> i32;

    #[psp(0x35DBD746)]
    /// Wait for asyncronous completion (with callbacks).
    ///
    /// # Parameters
    ///
    /// - `fd`: The file descriptor which is current performing an asynchronous action.
    /// - `res`: The result of the async action.
    ///
    /// # Return value
    ///
    /// < 0 on error.
    pub fn sceIoWaitAsyncCB(fd: SceUid, res: *mut i64) -> i32;

    #[psp(0x3251EA56)]
    /// Poll for asyncronous completion.
    ///
    /// # Parameters
    ///
    /// - `fd`: The file descriptor which is current performing an asynchronous action.
    /// - `res`: The result of the async action.
    ///
    /// # Return value
    ///
    /// < 0 on error.
    pub fn sceIoPollAsync(fd: SceUid, res: *mut i64) -> i32;

    #[psp(0xCB05F8D6)]
    /// Get the asyncronous completion status.
    ///
    /// # Parameters
    ///
    /// - `fd`: The file descriptor which is current performing an asynchronous action.
    /// - `poll`: If 0 then waits for the status, otherwise it polls the fd.
    /// - `res`: The result of the async action.
    ///
    /// # Return value
    ///
    /// < 0 on error.
    pub fn sceIoGetAsyncStat(fd: SceUid, poll: i32, res: *mut i64)
     -> i32;

    #[psp(0xE8BC6571)]
    /// Cancel an asynchronous operation on a file descriptor.
    ///
    /// # Parameters
    ///
    /// - `fd`: The file descriptor to perform cancel on.
    ///
    /// # Return value
    ///
    /// < 0 on error.
    pub fn sceIoCancel(fd: SceUid) -> i32;

    #[psp(0x08BD7374)]
    /// Get the device type of the currently opened file descriptor.
    ///
    /// # Parameters
    ///
    /// - `fd`: The opened file descriptor.
    ///
    /// # Return value
    ///
    /// < 0 on error. Otherwise the device type?
    pub fn sceIoGetDevType(fd: SceUid) -> i32;

    #[psp(0xB293727F)]
    /// Change the priority of the asynchronous thread.
    ///
    /// # Parameters
    ///
    /// - `fd`: The opened fd on which the priority should be changed.
    /// - `pri`: The priority of the thread.
    ///
    /// # Return value
    ///
    /// < 0 on error.
    pub fn sceIoChangeAsyncPriority(fd: SceUid, pri: i32) -> i32;

    #[psp(0xA12A0514)]
    /// Sets a callback for the asynchronous action.
    ///
    /// # Parameters
    ///
    /// - `fd`: The filedescriptor currently performing an asynchronous action.
    /// - `cb`: The UID of the callback created with `sceKernelCreateCallback`
    /// - `argp`: Pointer to an argument to pass to the callback.
    ///
    /// # Return value
    ///
    /// < 0 on error.
    pub fn sceIoSetAsyncCallback(
        fd: SceUid,
        cb: SceUid,
        argp: *mut c_void
    ) -> i32;
}