Hacking Calling all Wii programmers, new coder needs some help.

RiCK420

Contributing Member
OP
Member
Joined
Oct 2, 2013
Messages
172
Trophies
0
XP
159
Country
United States
Hello.
So I started looking into programming for the Wii.
I installed DevKitPPC and successfully completed the HelloWorld tutorial.
Compiled the dol and ran it. So far so good.

Now I am trying to write my own first program... A very simple one that when ran will tell how much disk space is left on the USB hard drive.
I have been looking over the usbstorage.h file, as this was the only one I could see that had anything to do with the USB hard drive.
I believe the command I need to use is this one: s32 USBStorage_ReadCapacity(usbstorage_handle *dev, u8 lun, u32 *sector_size, u32 *n_sectors);
However I am unsure how to actually use it, I'm guessing I need to fill in some information for each variable inside the ()'s, but I have no clue what to put in.

If you could shed some light for me, it would be much appreciated.

Code:
#include <stdio.h>
#include <stdlib.h>
#include <gccore.h>
#include <wiiuse/wpad.h>
#include <fat.h>
#include <ogc/usbstorage.h>
 
 
static void *xfb = NULL;
static GXRModeObj *rmode = NULL;
 
//---------------------------------------------------------------------------------
int main(int argc, char **argv) {
//---------------------------------------------------------------------------------
 
    // Initialise the video system
    VIDEO_Init();
   
    // This function initialises the attached controllers
    WPAD_Init();
   
    // Obtain the preferred video mode from the system
    // This will correspond to the settings in the Wii menu
    rmode = VIDEO_GetPreferredMode(NULL);
 
    // Allocate memory for the display in the uncached region
    xfb = MEM_K0_TO_K1(SYS_AllocateFramebuffer(rmode));
   
    // Initialise the console, required for printf
    console_init(xfb,20,20,rmode->fbWidth,rmode->xfbHeight,rmode->fbWidth*VI_DISPLAY_PIX_SZ);
   
    // Set up the video registers with the chosen mode
    VIDEO_Configure(rmode);
   
    // Tell the video hardware where our display memory is
    VIDEO_SetNextFramebuffer(xfb);
   
    // Make the display visible
    VIDEO_SetBlack(FALSE);
 
    // Flush the video register changes to the hardware
    VIDEO_Flush();
 
    // Wait for Video setup to complete
    VIDEO_WaitVSync();
    if(rmode->viTVMode&VI_NON_INTERLACE) VIDEO_WaitVSync();
 
 
    // The console understands VT terminal escape codes
    // This positions the cursor on row 2, column 0
    // we can use variables for this with format codes too
    // e.g. printf ("\x1b[%d;%dH", row, column );
    printf("\x1b[2;0H");
   
    printf("RiCK420 has taken over your Wii! Please wait patiently! \n");
   
    printf("\n");
   
    // RiCK's code BEGIN //
   
    // Initialize file system (FAT)
    if (!fatInitDefault()) {
        printf("Unable to initialize FAT subsystem. \n");
        printf("\n");
    }
    else {
        printf("Initialized FAT subsystem, exiting. \n");
        printf("\n");
    }
   
    // Change dir to root
    int chdir();
   
    if (chdir("/")) {
        printf("Could not change to root directory. \n");
        printf("\n");
    }
    else {
        printf("Directory succesfully changed to root. \n");
        printf("\n");
    }
   
    // Above this point is working and does something, however I am unsure if this is even needed?
    // Is this only for the SD card maybe? 
   
   
   
   
    // This stuff looks more like it is for the USBStorage specifically
   
    s32 USBStorage_Initialize();  // This doesn't give any errors so I guess it worked.
   
    s32 USBStorage_ReadCapacity(usbstorage_handle *dev, u8 lun, u32 *sector_size, u32 *n_sectors);
   
    // ^ This line needs some work, it doesn't output anything like I expected it too.
   
    // Maybe all those variables in the () need to actually be filled in with some information?
   
   
   
   
    // Exiting Routine
   
    fatUnmount(0);
   
    printf("...EXITING! \n");
    exit(0);
   
    // RiCK's code END //
 
    while(1) {
 
        // Call WPAD_ScanPads each loop, this reads the latest controller states
        WPAD_ScanPads();
 
        // WPAD_ButtonsDown tells us which buttons were pressed in this loop
        // this is a "one shot" state which will not fire again until the button has been released
        u32 pressed = WPAD_ButtonsDown(0);
 
        // We return to the launcher application via exit
        if ( pressed & WPAD_BUTTON_HOME ) exit(0);
 
        // Wait for the next frame
        VIDEO_WaitVSync();
    }
 
    return 0;
}
 
  • Like
Reactions: spacepimp

Arras

Well-Known Member
Member
Joined
Sep 14, 2010
Messages
6,318
Trophies
2
XP
5,407
Country
Netherlands
I'm guessing *dev is a pointer that points to the USB device itself, *sector_size is the sector size of the USB device and *n_sectors is the amount of sectors it has. I'm not sure what lun is, but it's some sort of number. Maybe you need to use other functions to get these variables; I've never tried programming for the Wii (or even C).
 
  • Like
Reactions: RiCK420

ilman

Gbatemp's Official Noise Eraser
Member
Joined
Jul 25, 2010
Messages
1,128
Trophies
0
Age
25
Location
Shibuya
XP
570
Country
I have no idea how to program on the Wii, but USBStorage_ReadCapacity is a function. Functions are premade code that you can use by setting some values in the (). How you do this is you add a variable of the first type (in your example, a usbstorage_handle variable), add a coma, add a variable of the second type (in this case, a u8 variable) then the third type and so forth.
It's good to learn how functions function (bad pun, sorry) if you want to get serious with programming.

Hope I helped. :)
 
  • Like
Reactions: RiCK420

RiCK420

Contributing Member
OP
Member
Joined
Oct 2, 2013
Messages
172
Trophies
0
XP
159
Country
United States
I have no idea how to program on the Wii, but USBStorage_ReadCapacity is a function. Functions are premade code that you can use by setting some values in the (). How you do this is you add a variable of the first type (in your example, a usbstorage_handle variable), add a coma, add a variable of the second type (in this case, a u8 variable) then the third type and so forth.
It's good to learn how functions function (bad pun, sorry) if you want to get serious with programming.

Hope I helped. :)

Thanks for the reply.

I do understand how a function works, I program in some other languages, AutoIt, PHP, etc.

I am new to C++, and programming specifically for the Wii though.
 

RiCK420

Contributing Member
OP
Member
Joined
Oct 2, 2013
Messages
172
Trophies
0
XP
159
Country
United States
Not sure if having this visible helps anyone with helping me?

usbstorage.h

Code:
#ifndef __USBSTORAGE_H__
#define __USBSTORAGE_H__

#if defined(HW_RVL)

#include <gctypes.h>
#include <ogc/mutex.h>
#include <ogc/disc_io.h>
#include <ogc/system.h>

#ifdef __cplusplus
   extern "C" {
#endif /* __cplusplus */

#define    USBSTORAGE_OK            0
#define    USBSTORAGE_ENOINTERFACE        -10000
#define    USBSTORAGE_ESENSE        -10001
#define    USBSTORAGE_ESHORTWRITE        -10002
#define    USBSTORAGE_ESHORTREAD        -10003
#define    USBSTORAGE_ESIGNATURE        -10004
#define    USBSTORAGE_ETAG            -10005
#define    USBSTORAGE_ESTATUS        -10006
#define    USBSTORAGE_EDATARESIDUE        -10007
#define    USBSTORAGE_ETIMEDOUT        -10008
#define    USBSTORAGE_EINIT        -10009
#define USBSTORAGE_PROCESSING    -10010

typedef struct
{
    u8 configuration;
    u32 interface;
    u32 altInterface;
    u8 bInterfaceSubClass;

    u8 ep_in;
    u8 ep_out;

    u8 max_lun;
    u32 *sector_size;

    s32 usb_fd;

    mutex_t lock;
    syswd_t alarm;
    s32 retval;

    u32 tag;
    u8 suspended;

    u8 *buffer;
} usbstorage_handle;

#define B_RAW_DEVICE_DATA_IN 0x01
#define B_RAW_DEVICE_COMMAND 0

typedef struct {
   uint8_t         command[16];
   uint8_t         command_length;
   uint8_t         flags;
   uint8_t         scsi_status;
   void*           data;
   size_t          data_length;
} raw_device_command;

s32 USBStorage_Initialize();

s32 USBStorage_Open(usbstorage_handle *dev, s32 device_id, u16 vid, u16 pid);
s32 USBStorage_Close(usbstorage_handle *dev);
s32 USBStorage_Reset(usbstorage_handle *dev);

s32 USBStorage_GetMaxLUN(usbstorage_handle *dev);
s32 USBStorage_MountLUN(usbstorage_handle *dev, u8 lun);
s32 USBStorage_Suspend(usbstorage_handle *dev);
s32 USBStorage_IsDVD();
s32 USBStorage_ioctl(int request, ...);

s32 USBStorage_ReadCapacity(usbstorage_handle *dev, u8 lun, u32 *sector_size, u32 *n_sectors);
s32 USBStorage_Read(usbstorage_handle *dev, u8 lun, u32 sector, u16 n_sectors, u8 *buffer);
s32 USBStorage_Write(usbstorage_handle *dev, u8 lun, u32 sector, u16 n_sectors, const u8 *buffer);
s32 USBStorage_StartStop(usbstorage_handle *dev, u8 lun, u8 lo_ej, u8 start, u8 imm);

#define DEVICE_TYPE_WII_USB (('W'<<24)|('U'<<16)|('S'<<8)|'B')

extern DISC_INTERFACE __io_usbstorage;

#ifdef __cplusplus
   }
#endif /* __cplusplus */

#endif /* HW_RVL */

#endif /* __USBSTORAGE_H__ */
 

JoostinOnline

Certified Crash Test Dummy
Member
Joined
Apr 2, 2011
Messages
11,005
Trophies
1
Location
The Twilight Zone
Website
www.hacksden.com
XP
4,339
Country
United States
1) Don't add the data types to the functions when you are running them. That's only for declaring/defining a function.
2) You want to do something like this:
Code:
usbstorage_handle *usb_device;
u32 *sector_size = 0;
u32 *num_of_sectors = 0;
u8 lun = 0; // Logical Unit Number (partition)
 
USBStorage_ReadCapacity(&usb_device, lun, &sector_size, &num_of_sectors)
 
  • Like
Reactions: RiCK420

Oxybelis

Well-Known Member
Member
Joined
Jan 10, 2010
Messages
350
Trophies
0
XP
383
Country
Looks like this is a code for USD Storage device.
You should find code for reading file system unless you want to parse partitions and file systems on device himself.
 

JoostinOnline

Certified Crash Test Dummy
Member
Joined
Apr 2, 2011
Messages
11,005
Trophies
1
Location
The Twilight Zone
Website
www.hacksden.com
XP
4,339
Country
United States

RiCK420

Contributing Member
OP
Member
Joined
Oct 2, 2013
Messages
172
Trophies
0
XP
159
Country
United States
Looks like this is a code for USD Storage device.
You should find code for reading file system unless you want to parse partitions and file systems on device himself.

I want to get disk space usage stats like how much space is left on each partition of the USB connected hard drive.
 

RiCK420

Contributing Member
OP
Member
Joined
Oct 2, 2013
Messages
172
Trophies
0
XP
159
Country
United States
When I try this:

Code:
    USBStorage_Initialize();
   
    usbstorage_handle *usb_device;
    u32 *sector_size = 0;
    u32 *num_of_sectors = 0;
    u8 lun = 0; // Logical Unit Number (partition)
   
    USBStorage_ReadCapacity(&usb_device, lun, &sector_size, &num_of_sectors);

Make outputs these errors:

Code:
> "make"
template.c
c:/projects/wii/FreeSpace/source/template.c: In function 'main':
c:/projects/wii/FreeSpace/source/template.c:111:2: warning: passing argument 1 of 'USBStorage_ReadCapacity' from incompatible pointer type [enabled by default]
c:/devkitPro/libogc/include/ogc/usbstorage.h:77:5: note: expected 'struct usbstorage_handle *' but argument is of type 'struct usbstorage_handle **'
c:/projects/wii/FreeSpace/source/template.c:111:2: warning: passing argument 3 of 'USBStorage_ReadCapacity' from incompatible pointer type [enabled by default]
c:/devkitPro/libogc/include/ogc/usbstorage.h:77:5: note: expected 'u32 *' but argument is of type 'u32 **'
c:/projects/wii/FreeSpace/source/template.c:111:2: warning: passing argument 4 of 'USBStorage_ReadCapacity' from incompatible pointer type [enabled by default]
c:/devkitPro/libogc/include/ogc/usbstorage.h:77:5: note: expected 'u32 *' but argument is of type 'u32 **'
linking ... FreeSpace.elf
output ... FreeSpace.dol
 
> Process Exit Code: 0
> Time Taken: 00:02


PS: If I removed the *'s, it doesn't give any errors, was I supposed to remove them?
 

RiCK420

Contributing Member
OP
Member
Joined
Oct 2, 2013
Messages
172
Trophies
0
XP
159
Country
United States
remove teh &s

OK, i put the *'s back and removed the &'s instead.. but now it outputs:

Code:
> "make"
template.c
c:/projects/wii/FreeSpace/source/template.c: In function 'main':
c:/projects/wii/FreeSpace/source/template.c:82:23: warning: 'usb_device' is used uninitialized in this function [-Wuninitialized]
linking ... FreeSpace.elf
output ... FreeSpace.dol
 
> Process Exit Code: 0
> Time Taken: 00:01

Entire code:

Code:
#include <stdio.h>
#include <stdlib.h>
#include <gccore.h>
#include <wiiuse/wpad.h>
#include <fat.h>
#include <ogc/usbstorage.h>
#include <string.h>
#include <dirent.h>



static void *xfb = NULL;
static GXRModeObj *rmode = NULL;

//---------------------------------------------------------------------------------
int main(int argc, char **argv) {
//---------------------------------------------------------------------------------

    // Initialise the video system
    VIDEO_Init();
    
    // This function initialises the attached controllers
    WPAD_Init();
    
    // Obtain the preferred video mode from the system
    // This will correspond to the settings in the Wii menu
    rmode = VIDEO_GetPreferredMode(NULL);

    // Allocate memory for the display in the uncached region
    xfb = MEM_K0_TO_K1(SYS_AllocateFramebuffer(rmode));
    
    // Initialise the console, required for printf
    console_init(xfb,20,20,rmode->fbWidth,rmode->xfbHeight,rmode->fbWidth*VI_DISPLAY_PIX_SZ);
    
    // Set up the video registers with the chosen mode
    VIDEO_Configure(rmode);
    
    // Tell the video hardware where our display memory is
    VIDEO_SetNextFramebuffer(xfb);
    
    // Make the display visible
    VIDEO_SetBlack(FALSE);

    // Flush the video register changes to the hardware
    VIDEO_Flush();

    // Wait for Video setup to complete
    VIDEO_WaitVSync();
    if(rmode->viTVMode&VI_NON_INTERLACE) VIDEO_WaitVSync();


    // The console understands VT terminal escape codes
    // This positions the cursor on row 2, column 0
    // we can use variables for this with format codes too
    // e.g. printf ("\x1b[%d;%dH", row, column );
    printf("\x1b[2;0H");
    printf("RiCK420 has taken over your Wii! \n");
    printf("\n");
    printf("...Just Kidding! Please wait patiently! \n");
    printf("\n");
    
    // RiCK's code BEGIN // 
    
    //s32 USBStorage_ReadCapacity(usbstorage_handle *dev, u8 lun, u32 *sector_size, u32 *n_sectors);
    
    //USBStorage_Initialize();
    
    usbstorage_handle *usb_device;
    u32 *sector_size = 0;
    u32 *num_of_sectors = 0;
    u8 lun = 0; // Logical Unit Number (partition)
    

    //USBStorage_Initialize();
    
    if (!USBStorage_Initialize()) {
        printf("Unable to initialize subsystem. \n");
        printf("\n");
    }
    else {
        printf("Initialized subsystem. \n");
        printf("\n");
    }
     
    USBStorage_ReadCapacity(usb_device, lun, sector_size, num_of_sectors);
    
    
    // Exiting Routine
    
    printf("Process complete, press [HOME] to EXIT! \n");
    printf("\n");
    
    printf("...EXITING! \n");
    exit(0);
    
    // RiCK's code END //

    while(1) {

        // Call WPAD_ScanPads each loop, this reads the latest controller states
        WPAD_ScanPads();

        // WPAD_ButtonsDown tells us which buttons were pressed in this loop
        // this is a "one shot" state which will not fire again until the button has been released
        u32 pressed = WPAD_ButtonsDown(0);

        // We return to the launcher application via exit
        if ( pressed & WPAD_BUTTON_HOME ) exit(0);

        // Wait for the next frame
        VIDEO_WaitVSync();
    }

    return 0;
}
 

RiCK420

Contributing Member
OP
Member
Joined
Oct 2, 2013
Messages
172
Trophies
0
XP
159
Country
United States
No, don't use an * either. It's a pointer. You'd be pointing to a pointer.

Removing the *'s make it cause way more errors:

Code:
> "make" 
template.c
c:/projects/wii/FreeSpace/source/template.c: In function 'main':
c:/projects/wii/FreeSpace/source/template.c:85:2: error: incompatible type for argument 1 of 'USBStorage_ReadCapacity'
c:/devkitPro/libogc/include/ogc/usbstorage.h:77:5: note: expected 'struct usbstorage_handle *' but argument is of type 'usbstorage_handle'
c:/projects/wii/FreeSpace/source/template.c:85:2: warning: passing argument 3 of 'USBStorage_ReadCapacity' makes pointer from integer without a cast [enabled by default]
c:/devkitPro/libogc/include/ogc/usbstorage.h:77:5: note: expected 'u32 *' but argument is of type 'u32'
c:/projects/wii/FreeSpace/source/template.c:85:2: warning: passing argument 4 of 'USBStorage_ReadCapacity' makes pointer from integer without a cast [enabled by default]
c:/devkitPro/libogc/include/ogc/usbstorage.h:77:5: note: expected 'u32 *' but argument is of type 'u32'
make[1]: *** [template.o] Error 1
"make": *** [build] Error 2

> Process Exit Code: 2
> Time Taken: 00:01
 

RiCK420

Contributing Member
OP
Member
Joined
Oct 2, 2013
Messages
172
Trophies
0
XP
159
Country
United States
BTW JoostinOnline, I have been studying that Tutorial you linked me too, but it doesn't seem to offer any advanced stuff, like what I am trying to do. I already understand most of the elements it teaches from programming in other languages.
 

JoostinOnline

Certified Crash Test Dummy
Member
Joined
Apr 2, 2011
Messages
11,005
Trophies
1
Location
The Twilight Zone
Website
www.hacksden.com
XP
4,339
Country
United States
I don't know why it's not working. The warnings don't make sense. Anyway, I don't understand why you are jumping into something like this when you don't even know C or C++. You have to start out small. It doesn't matter that you know other languages.

All my projects are open source if you want to take a look. You can find the source at HacksDen.
 

JoostinOnline

Certified Crash Test Dummy
Member
Joined
Apr 2, 2011
Messages
11,005
Trophies
1
Location
The Twilight Zone
Website
www.hacksden.com
XP
4,339
Country
United States
Okay, took a better look at it, and this is what you need to do:
Code:
usbstorage_handle usb_device;
u32 sector_size, num_of_sectors;
u8 lun = 0; // Logical Unit Number (partition)
 
if (!USBStorage_Initialize()) {
    printf("Unable to initialize subsystem. \n");
    printf("\n");
}
else {
    printf("Initialized subsystem. \n");
    printf("\n");
}
 
USBStorage_ReadCapacity(&usb_device, lun, &sector_size, &num_of_sectors);
 

Site & Scene News

Popular threads in this forum

General chit-chat
Help Users
  • No one is chatting at the moment.
    Maximumbeans @ Maximumbeans: butte