1 /*---------------------------------------------------------------------------*
2   Project:  CARD API save demo
3   File:     save.c
4 
5   Copyright 2001 Nintendo.  All rights reserved.
6 
7   These coded instructions, statements, and computer programs contain
8   proprietary information of Nintendo of America Inc. and/or Nintendo
9   Company Ltd., and are protected by Federal copyright law.  They may
10   not be disclosed to third parties or copied or duplicated in any form,
11   in whole or in part, without the prior written consent of Nintendo.
12 
13   $Log: save.c,v $
14   Revision 1.2  02/20/2006 04:13:08  mitu
15   changed include path from dolphin/ to revolution/.
16 
17   Revision 1.1  01/31/2006 10:48:30  mitu
18   (none)
19 
20 
21     2     9/03/01 20:23 Shiki
22     Modified to use slot B.
23 
24     1     6/14/01 11:36a Shiki
25     Initial check-in.
26   $NoKeywords: $
27  *---------------------------------------------------------------------------*/
28 
29 #include <revolution.h>
30 
31 #define OFFSET(n, a)    (((u32) (n)) & ((a) - 1))
32 #define TRUNC(n, a)     (((u32) (n)) & ~((a) - 1))
33 #define ROUND(n, a)     (((u32) (n) + (a) - 1) & ~((a) - 1))
34 
35 #define CARD_CHAN   1
36 
37 u8 CardWorkArea[CARD_WORKAREA_SIZE] ATTRIBUTE_ALIGN(32);
38 
LoadFile(char * name,s32 * length)39 static void* LoadFile(char* name, s32* length)
40 {
41     DVDFileInfo fileInfo;
42     BOOL        result;
43     void*       data;
44 
45     result = DVDOpen(name, &fileInfo);
46     if (!result)
47     {
48         return NULL;
49     }
50     *length = (s32) OSRoundUp32B(DVDGetLength(&fileInfo));
51     data = OSAllocFromArenaLo((u32) *length, 32);
52     result = DVDRead(&fileInfo, data, *length, 0);
53     if (!result)
54     {
55         return NULL;
56     }
57     DVDClose(&fileInfo);
58 
59     return data;
60 }
61 
main(int argc,char * argv[])62 int main(int argc, char* argv[])
63 {
64     s32          result;
65     CARDFileInfo fileInfo;
66     CARDStat     stat;
67     u32          sectorSize;
68     s32          length;
69     void*        data;
70 
71     OSInit();
72     DVDInit();
73     CARDInit();
74 
75     if (argc != 2)
76     {
77         OSHalt("usage: save filename");
78     }
79 
80     data = LoadFile(argv[1], &length);
81     if (data == NULL)
82     {
83         OSHalt("error: could not load file from disk");
84     }
85 
86     // Probe
87     OSReport("Insert card in slot %c.\n", "AB"[CARD_CHAN]);
88     while (!CARDProbe(CARD_CHAN))
89     {
90         ;
91     }
92 
93     // Mount
94     OSReport("CARDMount ");
95     result = CARDMountAsync(CARD_CHAN, CardWorkArea, 0, 0);
96     if (result < 0)
97     {
98         OSReport("failed. (%d)\n", result);
99         return 1;
100     }
101     while ((result = CARDGetResultCode(CARD_CHAN)) == CARD_RESULT_BUSY)
102     {
103         ;
104     }
105     switch (result)
106     {
107       case CARD_RESULT_READY:
108       case CARD_RESULT_BROKEN:
109         OSReport("done. (%d)\n", result);
110         OSReport("CARDCheck ");
111         result = CARDCheckAsync(CARD_CHAN, 0);
112         if (result < 0)
113         {
114             CARDUnmount(CARD_CHAN);
115             OSReport("failed. (%d)\n", result);
116             return 1;
117         }
118         while ((result = CARDGetResultCode(CARD_CHAN)) == CARD_RESULT_BUSY)
119         {
120             ;
121         }
122         break;
123     }
124     switch (result)
125     {
126       case CARD_RESULT_READY:
127         OSReport("done. (%d)\n", result);
128         break;
129       case CARD_RESULT_BROKEN:
130       case CARD_RESULT_ENCODING:
131         CARDUnmount(CARD_CHAN);
132         OSReport("failed. (%d)\n", result);
133         return 1;
134         break;
135       default:
136         OSReport("failed. (%d)\n", result);
137         return 1;
138         break;
139     }
140 
141     // Sector size
142     result = CARDGetSectorSize(CARD_CHAN, &sectorSize);
143     if (result < 0)
144     {
145         OSReport("CARDGetSectorSize() failed. (%d)\n", result);
146         return 1;
147     }
148     OSReport("Sector size %d bytes.\n", sectorSize);
149 
150     // Adjust length
151     length = (s32) ROUND(length, sectorSize);
152 
153     // Delete
154     OSReport("CARDDelete ");
155     result = CARDDelete(CARD_CHAN, argv[1]);
156     OSReport("done. (%d)\n", result);
157 
158     // Create
159     OSReport("CARDCreate ");
160     result = CARDCreateAsync(CARD_CHAN, argv[1], (u32) length, &fileInfo, 0);
161     if (result < 0)
162     {
163         OSReport("failed. (%d)\n", result);
164         return 1;
165     }
166     while ((result = CARDGetResultCode(CARD_CHAN)) == CARD_RESULT_BUSY)
167     {
168         ;
169     }
170     OSReport("done. (%d:%d:%d)\n", result, fileInfo.chan, fileInfo.fileNo);
171 
172     // Stat
173     OSReport("CARDGetStatus ");
174     result = CARDGetStatus(CARD_CHAN, fileInfo.fileNo, &stat);
175     if (result < 0)
176     {
177         OSReport("failed. (%d)\n", result);
178         return 1;
179     }
180     OSReport("gameName %.4s\n",  stat.gameName);
181     OSReport("company %.2s\n",   stat.company);
182     OSReport("length %d\n",      stat.length);
183     OSReport("fileName %.32s\n", stat.fileName);
184     OSReport("time %d\n",        stat.time);
185 
186     OSReport("CARDWrite ");
187     result = CARDWriteAsync(&fileInfo, data, length, 0, 0);
188     if (result < 0)
189     {
190         OSReport("failed. (%d)\n", result);
191         return 1;
192     }
193     while ((result = CARDGetResultCode(CARD_CHAN)) == CARD_RESULT_BUSY)
194     {
195         ;
196     }
197     if (result < 0)
198     {
199         OSReport("failed. (%d)\n", result);
200         return 1;
201     }
202     OSReport("done. (%d)\n", result);
203 
204     // Close
205     CARDClose(&fileInfo);
206 
207     // Unmount
208     CARDUnmount(CARD_CHAN);
209 
210     OSReport("Done.\n");
211 
212     return 0;
213 }
214